1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include <pthread.h> 18 #include <semaphore.h> 19 #include <stdatomic.h> 20 #include <stdio.h> 21 #include <stdlib.h> 22 23 #include <benchmark/benchmark.h> 24 #include "util.h" 25 26 static void BM_semaphore_sem_getvalue(benchmark::State& state) { 27 sem_t semaphore; 28 sem_init(&semaphore, 1, 1); 29 30 while (state.KeepRunning()) { 31 int dummy; 32 sem_getvalue(&semaphore, &dummy); 33 } 34 } 35 BIONIC_BENCHMARK(BM_semaphore_sem_getvalue); 36 37 static void BM_semaphore_sem_wait_sem_post(benchmark::State& state) { 38 sem_t semaphore; 39 sem_init(&semaphore, 1, 1); 40 41 while (state.KeepRunning()) { 42 sem_wait(&semaphore); 43 sem_post(&semaphore); 44 } 45 } 46 BIONIC_BENCHMARK(BM_semaphore_sem_wait_sem_post); 47 48 // This test reports the overhead of the underlying futex wake syscall on 49 // the producer. It does not report the overhead from issuing the wake to the 50 // point where the posted consumer thread wakes up. It suffers from 51 // clock_gettime syscall overhead. Lock the CPU speed for consistent results 52 // as we may not reach >50% cpu utilization. 53 // 54 // We will run a background thread that catches the sem_post wakeup and 55 // loops immediately returning back to sleep in sem_wait for the next one. This 56 // thread is run with policy SCHED_OTHER (normal policy), a middle policy. 57 // 58 // The primary thread will run at SCHED_IDLE (lowest priority policy) when 59 // monitoring the background thread to detect when it hits sem_wait sleep. It 60 // will do so with no clock running. Once we are ready, we will switch to 61 // SCHED_FIFO (highest priority policy) to time the act of running sem_post 62 // with the benchmark clock running. This ensures nothing else in the system 63 // can preempt our timed activity, including the background thread. We are 64 // also protected with the scheduling policy of letting a process hit a 65 // resource limit rather than get hit with a context switch. 66 // 67 // The background thread will start executing either on another CPU, or 68 // after we back down from SCHED_FIFO, but certainly not in the context of 69 // the timing of the sem_post. 70 71 static atomic_int BM_semaphore_sem_post_running; 72 73 static void* BM_semaphore_sem_post_start_thread(void* arg) { 74 sem_t* semaphore = reinterpret_cast<sem_t*>(arg); 75 while ((BM_semaphore_sem_post_running > 0) && !sem_wait(semaphore)) { 76 } 77 BM_semaphore_sem_post_running = -1; 78 return nullptr; 79 } 80 81 class SemaphoreFixture : public benchmark::Fixture { 82 public: 83 void SetUp(const benchmark::State&) override { 84 sem_init(&semaphore, 0, 0); 85 86 pthread_attr_t attr; 87 pthread_attr_init(&attr); 88 89 memset(¶m, 0, sizeof(param)); 90 pthread_attr_setschedparam(&attr, ¶m); 91 pthread_attr_setschedpolicy(&attr, SCHED_OTHER); 92 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 93 pthread_t pthread; 94 pthread_create(&pthread, &attr, BM_semaphore_sem_post_start_thread, &semaphore); 95 pthread_attr_destroy(&attr); 96 97 sched_setscheduler(0, SCHED_IDLE, ¶m); 98 99 BM_semaphore_sem_post_running = 1; 100 setup = true; 101 } 102 103 ~SemaphoreFixture() override { 104 if (setup) { 105 // Only do this if the test was actually run. 106 sched_setscheduler(0, SCHED_OTHER, ¶m); 107 108 if (BM_semaphore_sem_post_running > 0) { 109 BM_semaphore_sem_post_running = 0; 110 } 111 do { 112 sem_post(&semaphore); 113 sched_yield(); 114 } while (BM_semaphore_sem_post_running != -1); 115 } 116 } 117 118 sem_t semaphore; 119 sched_param param; 120 bool setup = false; 121 }; 122 123 // This is commented out because dynamic benchmark registering doesn't currently support fixtures. 124 // Uncomment it and recompile to run this benchmark on every run. 125 /* BENCHMARK_F(SemaphoreFixture, semaphore_sem_post)(benchmark::State& state) { 126 while (state.KeepRunning()) { 127 state.PauseTiming(); 128 129 int trys = 3, dummy = 0; 130 do { 131 if (BM_semaphore_sem_post_running < 0) { 132 sched_setscheduler(0, SCHED_OTHER, ¶m); 133 fprintf(stderr, "BM_semaphore_sem_post: start_thread died unexpectedly\n"); 134 abort(); 135 } 136 sched_yield(); 137 sem_getvalue(&semaphore, &dummy); 138 if (dummy < 0) { // POSIX.1-2001 possibility 1 139 break; 140 } 141 if (dummy == 0) { // POSIX.1-2001 possibility 2 142 --trys; 143 } 144 } while (trys); 145 146 param.sched_priority = 1; 147 sched_setscheduler(0, SCHED_FIFO, ¶m); 148 149 state.ResumeTiming(); 150 sem_post(&semaphore); 151 152 param.sched_priority = 0; 153 sched_setscheduler(0, SCHED_IDLE, ¶m); 154 } 155 }*/ 156