blob: b932a2b323dd37654b10a111be2caf32d9ffaff0 [file] [log] [blame]
Elliott Hughesb28e4902014-03-11 11:19:06 -07001/*
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 Salyzyn7e50fb22015-02-09 08:18:10 -080017#include <pthread.h>
Elliott Hughesb28e4902014-03-11 11:19:06 -070018#include <semaphore.h>
Mark Salyzyn7e50fb22015-02-09 08:18:10 -080019#include <stdatomic.h>
20#include <stdio.h>
Elliott Hughes281e06b2016-02-17 10:23:52 -080021#include <stdlib.h>
Elliott Hughesb28e4902014-03-11 11:19:06 -070022
Elliott Hughes281e06b2016-02-17 10:23:52 -080023#include <benchmark/benchmark.h>
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080024
Elliott Hughes281e06b2016-02-17 10:23:52 -080025static void BM_semaphore_sem_getvalue(benchmark::State& state) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070026 sem_t semaphore;
27 sem_init(&semaphore, 1, 1);
Elliott Hughesb28e4902014-03-11 11:19:06 -070028
Elliott Hughes281e06b2016-02-17 10:23:52 -080029 while (state.KeepRunning()) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070030 int dummy;
31 sem_getvalue(&semaphore, &dummy);
32 }
Elliott Hughesb28e4902014-03-11 11:19:06 -070033}
Elliott Hughes281e06b2016-02-17 10:23:52 -080034BENCHMARK(BM_semaphore_sem_getvalue);
Elliott Hughesb28e4902014-03-11 11:19:06 -070035
Elliott Hughes281e06b2016-02-17 10:23:52 -080036static void BM_semaphore_sem_wait_sem_post(benchmark::State& state) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070037 sem_t semaphore;
38 sem_init(&semaphore, 1, 1);
Elliott Hughesb28e4902014-03-11 11:19:06 -070039
Elliott Hughes281e06b2016-02-17 10:23:52 -080040 while (state.KeepRunning()) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070041 sem_wait(&semaphore);
42 sem_post(&semaphore);
43 }
Elliott Hughesb28e4902014-03-11 11:19:06 -070044}
Elliott Hughes281e06b2016-02-17 10:23:52 -080045BENCHMARK(BM_semaphore_sem_wait_sem_post);
Mark Salyzyn7e50fb22015-02-09 08:18:10 -080046
Elliott Hughes281e06b2016-02-17 10:23:52 -080047// 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 Salyzyn7e50fb22015-02-09 08:18:10 -080070static atomic_int BM_semaphore_sem_post_running;
71
Elliott Hughes281e06b2016-02-17 10:23:52 -080072static 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 Salyzyn7e50fb22015-02-09 08:18:10 -080078}
79
Elliott Hughes281e06b2016-02-17 10:23:52 -080080class 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(&param, 0, sizeof(param));
89 pthread_attr_setschedparam(&attr, &param);
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, &param);
97
98 BM_semaphore_sem_post_running = 1;
Christopher Ferris17839412016-06-06 14:13:17 -070099 setup = true;
Elliott Hughes281e06b2016-02-17 10:23:52 -0800100 }
101
102 ~SemaphoreFixture() {
Christopher Ferris17839412016-06-06 14:13:17 -0700103 if (setup) {
104 // Only do this if the test was actually run.
105 sched_setscheduler(0, SCHED_OTHER, &param);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800106
Christopher Ferris17839412016-06-06 14:13:17 -0700107 if (BM_semaphore_sem_post_running > 0) {
108 BM_semaphore_sem_post_running = 0;
109 }
110 do {
111 sem_post(&semaphore);
112 sched_yield();
113 } while (BM_semaphore_sem_post_running != -1);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800114 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800115 }
Mark Salyzyn7e50fb22015-02-09 08:18:10 -0800116
117 sem_t semaphore;
Elliott Hughes281e06b2016-02-17 10:23:52 -0800118 sched_param param;
Christopher Ferris17839412016-06-06 14:13:17 -0700119 bool setup = false;
Elliott Hughes281e06b2016-02-17 10:23:52 -0800120};
Mark Salyzyn7e50fb22015-02-09 08:18:10 -0800121
Elliott Hughes281e06b2016-02-17 10:23:52 -0800122BENCHMARK_F(SemaphoreFixture, semaphore_sem_post)(benchmark::State& state) {
123 while (state.KeepRunning()) {
124 state.PauseTiming();
Mark Salyzyn7e50fb22015-02-09 08:18:10 -0800125
Mark Salyzyn7e50fb22015-02-09 08:18:10 -0800126 int trys = 3, dummy = 0;
127 do {
128 if (BM_semaphore_sem_post_running < 0) {
Elliott Hughes281e06b2016-02-17 10:23:52 -0800129 sched_setscheduler(0, SCHED_OTHER, &param);
Mark Salyzyn7e50fb22015-02-09 08:18:10 -0800130 fprintf(stderr, "BM_semaphore_sem_post: start_thread died unexpectedly\n");
Elliott Hughes281e06b2016-02-17 10:23:52 -0800131 abort();
Mark Salyzyn7e50fb22015-02-09 08:18:10 -0800132 }
133 sched_yield();
134 sem_getvalue(&semaphore, &dummy);
135 if (dummy < 0) { // POSIX.1-2001 possibility 1
136 break;
137 }
138 if (dummy == 0) { // POSIX.1-2001 possibility 2
139 --trys;
140 }
141 } while (trys);
Mark Salyzyn7e50fb22015-02-09 08:18:10 -0800142
Elliott Hughes281e06b2016-02-17 10:23:52 -0800143 param.sched_priority = 1;
144 sched_setscheduler(0, SCHED_FIFO, &param);
145
146 state.ResumeTiming();
Mark Salyzyn7e50fb22015-02-09 08:18:10 -0800147 sem_post(&semaphore);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800148
149 param.sched_priority = 0;
150 sched_setscheduler(0, SCHED_IDLE, &param);
151 }
Mark Salyzyn7e50fb22015-02-09 08:18:10 -0800152}