| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 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 |  | 
| Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 17 | #include <pthread.h> | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 18 | #include <semaphore.h> | 
| Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 19 | #include <stdatomic.h> | 
 | 20 | #include <stdio.h> | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 21 | #include <stdlib.h> | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 22 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 23 | #include <benchmark/benchmark.h> | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 24 | #include "util.h" | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 25 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 26 | static void BM_semaphore_sem_getvalue(benchmark::State& state) { | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 27 |   sem_t semaphore; | 
 | 28 |   sem_init(&semaphore, 1, 1); | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 29 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 30 |   while (state.KeepRunning()) { | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 31 |     int dummy; | 
 | 32 |     sem_getvalue(&semaphore, &dummy); | 
 | 33 |   } | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 34 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 35 | BIONIC_BENCHMARK(BM_semaphore_sem_getvalue); | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 36 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 37 | static void BM_semaphore_sem_wait_sem_post(benchmark::State& state) { | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 38 |   sem_t semaphore; | 
 | 39 |   sem_init(&semaphore, 1, 1); | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 40 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 41 |   while (state.KeepRunning()) { | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 42 |     sem_wait(&semaphore); | 
 | 43 |     sem_post(&semaphore); | 
 | 44 |   } | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 45 | } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 46 | BIONIC_BENCHMARK(BM_semaphore_sem_wait_sem_post); | 
| Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 47 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 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 |  | 
| Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 71 | static atomic_int BM_semaphore_sem_post_running; | 
 | 72 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 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 NULL; | 
| Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 79 | } | 
 | 80 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 81 | class SemaphoreFixture : public benchmark::Fixture { | 
 | 82 |  public: | 
 | 83 |   void SetUp(const benchmark::State&) { | 
 | 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; | 
| Christopher Ferris | 1783941 | 2016-06-06 14:13:17 -0700 | [diff] [blame] | 100 |     setup = true; | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 101 |   } | 
 | 102 |  | 
 | 103 |   ~SemaphoreFixture() { | 
| Christopher Ferris | 1783941 | 2016-06-06 14:13:17 -0700 | [diff] [blame] | 104 |     if (setup) { | 
 | 105 |       // Only do this if the test was actually run. | 
 | 106 |       sched_setscheduler(0, SCHED_OTHER, ¶m); | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 107 |  | 
| Christopher Ferris | 1783941 | 2016-06-06 14:13:17 -0700 | [diff] [blame] | 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); | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 115 |     } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 116 |   } | 
| Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 117 |  | 
 | 118 |   sem_t semaphore; | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 119 |   sched_param param; | 
| Christopher Ferris | 1783941 | 2016-06-06 14:13:17 -0700 | [diff] [blame] | 120 |   bool setup = false; | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 121 | }; | 
| Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 122 |  | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 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) { | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 126 |   while (state.KeepRunning()) { | 
 | 127 |     state.PauseTiming(); | 
| Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 128 |  | 
| Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 129 |     int trys = 3, dummy = 0; | 
 | 130 |     do { | 
 | 131 |       if (BM_semaphore_sem_post_running < 0) { | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 132 |         sched_setscheduler(0, SCHED_OTHER, ¶m); | 
| Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 133 |         fprintf(stderr, "BM_semaphore_sem_post: start_thread died unexpectedly\n"); | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 134 |         abort(); | 
| Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 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); | 
| Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 145 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 146 |     param.sched_priority = 1; | 
 | 147 |     sched_setscheduler(0, SCHED_FIFO, ¶m); | 
 | 148 |  | 
 | 149 |     state.ResumeTiming(); | 
| Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 150 |     sem_post(&semaphore); | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 151 |  | 
 | 152 |     param.sched_priority = 0; | 
 | 153 |     sched_setscheduler(0, SCHED_IDLE, ¶m); | 
 | 154 |   } | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 155 | }*/ |