blob: ba891370b575cb2baf03955b147ca4cb7730603f [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>
Anders Lewisa7b0f882017-07-24 20:01:13 -070024#include "util.h"
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080025
Elliott Hughes281e06b2016-02-17 10:23:52 -080026static void BM_semaphore_sem_getvalue(benchmark::State& state) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070027 sem_t semaphore;
28 sem_init(&semaphore, 1, 1);
Elliott Hughesb28e4902014-03-11 11:19:06 -070029
Elliott Hughes281e06b2016-02-17 10:23:52 -080030 while (state.KeepRunning()) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070031 int dummy;
32 sem_getvalue(&semaphore, &dummy);
33 }
Elliott Hughesb28e4902014-03-11 11:19:06 -070034}
Anders Lewisa7b0f882017-07-24 20:01:13 -070035BIONIC_BENCHMARK(BM_semaphore_sem_getvalue);
Elliott Hughesb28e4902014-03-11 11:19:06 -070036
Elliott Hughes281e06b2016-02-17 10:23:52 -080037static void BM_semaphore_sem_wait_sem_post(benchmark::State& state) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070038 sem_t semaphore;
39 sem_init(&semaphore, 1, 1);
Elliott Hughesb28e4902014-03-11 11:19:06 -070040
Elliott Hughes281e06b2016-02-17 10:23:52 -080041 while (state.KeepRunning()) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070042 sem_wait(&semaphore);
43 sem_post(&semaphore);
44 }
Elliott Hughesb28e4902014-03-11 11:19:06 -070045}
Anders Lewisa7b0f882017-07-24 20:01:13 -070046BIONIC_BENCHMARK(BM_semaphore_sem_wait_sem_post);
Mark Salyzyn7e50fb22015-02-09 08:18:10 -080047
Elliott Hughes281e06b2016-02-17 10:23:52 -080048// 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 Salyzyn7e50fb22015-02-09 08:18:10 -080071static atomic_int BM_semaphore_sem_post_running;
72
Elliott Hughes281e06b2016-02-17 10:23:52 -080073static 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;
Yi Kong32bc0fc2018-08-02 17:31:13 -070078 return nullptr;
Mark Salyzyn7e50fb22015-02-09 08:18:10 -080079}
80
Elliott Hughes281e06b2016-02-17 10:23:52 -080081class 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(&param, 0, sizeof(param));
90 pthread_attr_setschedparam(&attr, &param);
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, &param);
98
99 BM_semaphore_sem_post_running = 1;
Christopher Ferris17839412016-06-06 14:13:17 -0700100 setup = true;
Elliott Hughes281e06b2016-02-17 10:23:52 -0800101 }
102
103 ~SemaphoreFixture() {
Christopher Ferris17839412016-06-06 14:13:17 -0700104 if (setup) {
105 // Only do this if the test was actually run.
106 sched_setscheduler(0, SCHED_OTHER, &param);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800107
Christopher Ferris17839412016-06-06 14:13:17 -0700108 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 Hughes281e06b2016-02-17 10:23:52 -0800115 }
Elliott Hughes281e06b2016-02-17 10:23:52 -0800116 }
Mark Salyzyn7e50fb22015-02-09 08:18:10 -0800117
118 sem_t semaphore;
Elliott Hughes281e06b2016-02-17 10:23:52 -0800119 sched_param param;
Christopher Ferris17839412016-06-06 14:13:17 -0700120 bool setup = false;
Elliott Hughes281e06b2016-02-17 10:23:52 -0800121};
Mark Salyzyn7e50fb22015-02-09 08:18:10 -0800122
Anders Lewisa7b0f882017-07-24 20:01:13 -0700123// 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 Hughes281e06b2016-02-17 10:23:52 -0800126 while (state.KeepRunning()) {
127 state.PauseTiming();
Mark Salyzyn7e50fb22015-02-09 08:18:10 -0800128
Mark Salyzyn7e50fb22015-02-09 08:18:10 -0800129 int trys = 3, dummy = 0;
130 do {
131 if (BM_semaphore_sem_post_running < 0) {
Elliott Hughes281e06b2016-02-17 10:23:52 -0800132 sched_setscheduler(0, SCHED_OTHER, &param);
Mark Salyzyn7e50fb22015-02-09 08:18:10 -0800133 fprintf(stderr, "BM_semaphore_sem_post: start_thread died unexpectedly\n");
Elliott Hughes281e06b2016-02-17 10:23:52 -0800134 abort();
Mark Salyzyn7e50fb22015-02-09 08:18:10 -0800135 }
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 Salyzyn7e50fb22015-02-09 08:18:10 -0800145
Elliott Hughes281e06b2016-02-17 10:23:52 -0800146 param.sched_priority = 1;
147 sched_setscheduler(0, SCHED_FIFO, &param);
148
149 state.ResumeTiming();
Mark Salyzyn7e50fb22015-02-09 08:18:10 -0800150 sem_post(&semaphore);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800151
152 param.sched_priority = 0;
153 sched_setscheduler(0, SCHED_IDLE, &param);
154 }
Anders Lewisa7b0f882017-07-24 20:01:13 -0700155}*/