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> |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 24 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 25 | static void BM_semaphore_sem_getvalue(benchmark::State& state) { |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 26 | sem_t semaphore; |
| 27 | sem_init(&semaphore, 1, 1); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 28 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 29 | while (state.KeepRunning()) { |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 30 | int dummy; |
| 31 | sem_getvalue(&semaphore, &dummy); |
| 32 | } |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 33 | } |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 34 | BENCHMARK(BM_semaphore_sem_getvalue); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 35 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 36 | static void BM_semaphore_sem_wait_sem_post(benchmark::State& state) { |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 37 | sem_t semaphore; |
| 38 | sem_init(&semaphore, 1, 1); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 39 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 40 | while (state.KeepRunning()) { |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 41 | sem_wait(&semaphore); |
| 42 | sem_post(&semaphore); |
| 43 | } |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 44 | } |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 45 | BENCHMARK(BM_semaphore_sem_wait_sem_post); |
Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 46 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 47 | // This test reports the overhead of the underlying futex wake syscall on |
| 48 | // the producer. It does not report the overhead from issuing the wake to the |
| 49 | // point where the posted consumer thread wakes up. It suffers from |
| 50 | // clock_gettime syscall overhead. Lock the CPU speed for consistent results |
| 51 | // as we may not reach >50% cpu utilization. |
| 52 | // |
| 53 | // We will run a background thread that catches the sem_post wakeup and |
| 54 | // loops immediately returning back to sleep in sem_wait for the next one. This |
| 55 | // thread is run with policy SCHED_OTHER (normal policy), a middle policy. |
| 56 | // |
| 57 | // The primary thread will run at SCHED_IDLE (lowest priority policy) when |
| 58 | // monitoring the background thread to detect when it hits sem_wait sleep. It |
| 59 | // will do so with no clock running. Once we are ready, we will switch to |
| 60 | // SCHED_FIFO (highest priority policy) to time the act of running sem_post |
| 61 | // with the benchmark clock running. This ensures nothing else in the system |
| 62 | // can preempt our timed activity, including the background thread. We are |
| 63 | // also protected with the scheduling policy of letting a process hit a |
| 64 | // resource limit rather than get hit with a context switch. |
| 65 | // |
| 66 | // The background thread will start executing either on another CPU, or |
| 67 | // after we back down from SCHED_FIFO, but certainly not in the context of |
| 68 | // the timing of the sem_post. |
| 69 | |
Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 70 | static atomic_int BM_semaphore_sem_post_running; |
| 71 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 72 | static void* BM_semaphore_sem_post_start_thread(void* arg) { |
| 73 | sem_t* semaphore = reinterpret_cast<sem_t*>(arg); |
| 74 | while ((BM_semaphore_sem_post_running > 0) && !sem_wait(semaphore)) { |
| 75 | } |
| 76 | BM_semaphore_sem_post_running = -1; |
| 77 | return NULL; |
Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 80 | class SemaphoreFixture : public benchmark::Fixture { |
| 81 | public: |
| 82 | void SetUp(const benchmark::State&) { |
| 83 | sem_init(&semaphore, 0, 0); |
| 84 | |
| 85 | pthread_attr_t attr; |
| 86 | pthread_attr_init(&attr); |
| 87 | |
| 88 | memset(¶m, 0, sizeof(param)); |
| 89 | pthread_attr_setschedparam(&attr, ¶m); |
| 90 | pthread_attr_setschedpolicy(&attr, SCHED_OTHER); |
| 91 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 92 | pthread_t pthread; |
| 93 | pthread_create(&pthread, &attr, BM_semaphore_sem_post_start_thread, &semaphore); |
| 94 | pthread_attr_destroy(&attr); |
| 95 | |
| 96 | sched_setscheduler(0, SCHED_IDLE, ¶m); |
| 97 | |
| 98 | BM_semaphore_sem_post_running = 1; |
| 99 | } |
| 100 | |
| 101 | ~SemaphoreFixture() { |
| 102 | sched_setscheduler(0, SCHED_OTHER, ¶m); |
| 103 | |
| 104 | if (BM_semaphore_sem_post_running > 0) { |
| 105 | BM_semaphore_sem_post_running = 0; |
| 106 | } |
| 107 | do { |
| 108 | sem_post(&semaphore); |
| 109 | sched_yield(); |
| 110 | } while (BM_semaphore_sem_post_running != -1); |
| 111 | } |
Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 112 | |
| 113 | sem_t semaphore; |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 114 | sched_param param; |
| 115 | }; |
Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 116 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 117 | BENCHMARK_F(SemaphoreFixture, semaphore_sem_post)(benchmark::State& state) { |
| 118 | while (state.KeepRunning()) { |
| 119 | state.PauseTiming(); |
Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 120 | |
Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 121 | int trys = 3, dummy = 0; |
| 122 | do { |
| 123 | if (BM_semaphore_sem_post_running < 0) { |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 124 | sched_setscheduler(0, SCHED_OTHER, ¶m); |
Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 125 | fprintf(stderr, "BM_semaphore_sem_post: start_thread died unexpectedly\n"); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 126 | abort(); |
Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 127 | } |
| 128 | sched_yield(); |
| 129 | sem_getvalue(&semaphore, &dummy); |
| 130 | if (dummy < 0) { // POSIX.1-2001 possibility 1 |
| 131 | break; |
| 132 | } |
| 133 | if (dummy == 0) { // POSIX.1-2001 possibility 2 |
| 134 | --trys; |
| 135 | } |
| 136 | } while (trys); |
Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 137 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 138 | param.sched_priority = 1; |
| 139 | sched_setscheduler(0, SCHED_FIFO, ¶m); |
| 140 | |
| 141 | state.ResumeTiming(); |
Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 142 | sem_post(&semaphore); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 143 | |
| 144 | param.sched_priority = 0; |
| 145 | sched_setscheduler(0, SCHED_IDLE, ¶m); |
| 146 | } |
Mark Salyzyn | 7e50fb2 | 2015-02-09 08:18:10 -0800 | [diff] [blame] | 147 | } |