1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker * Copyright (C) 2014 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker *
4*8d67ca89SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*8d67ca89SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*8d67ca89SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*8d67ca89SAndroid Build Coastguard Worker *
8*8d67ca89SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*8d67ca89SAndroid Build Coastguard Worker *
10*8d67ca89SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*8d67ca89SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*8d67ca89SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8d67ca89SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*8d67ca89SAndroid Build Coastguard Worker * limitations under the License.
15*8d67ca89SAndroid Build Coastguard Worker */
16*8d67ca89SAndroid Build Coastguard Worker
17*8d67ca89SAndroid Build Coastguard Worker #include <pthread.h>
18*8d67ca89SAndroid Build Coastguard Worker
19*8d67ca89SAndroid Build Coastguard Worker #include <benchmark/benchmark.h>
20*8d67ca89SAndroid Build Coastguard Worker #include "util.h"
21*8d67ca89SAndroid Build Coastguard Worker
22*8d67ca89SAndroid Build Coastguard Worker // Stop GCC optimizing out our pure function.
23*8d67ca89SAndroid Build Coastguard Worker /* Must not be static! */ pthread_t (*pthread_self_fp)() = pthread_self;
24*8d67ca89SAndroid Build Coastguard Worker
BM_pthread_self(benchmark::State & state)25*8d67ca89SAndroid Build Coastguard Worker static void BM_pthread_self(benchmark::State& state) {
26*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
27*8d67ca89SAndroid Build Coastguard Worker pthread_self_fp();
28*8d67ca89SAndroid Build Coastguard Worker }
29*8d67ca89SAndroid Build Coastguard Worker }
30*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_pthread_self);
31*8d67ca89SAndroid Build Coastguard Worker
BM_pthread_getspecific(benchmark::State & state)32*8d67ca89SAndroid Build Coastguard Worker static void BM_pthread_getspecific(benchmark::State& state) {
33*8d67ca89SAndroid Build Coastguard Worker pthread_key_t key;
34*8d67ca89SAndroid Build Coastguard Worker pthread_key_create(&key, nullptr);
35*8d67ca89SAndroid Build Coastguard Worker
36*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
37*8d67ca89SAndroid Build Coastguard Worker pthread_getspecific(key);
38*8d67ca89SAndroid Build Coastguard Worker }
39*8d67ca89SAndroid Build Coastguard Worker
40*8d67ca89SAndroid Build Coastguard Worker pthread_key_delete(key);
41*8d67ca89SAndroid Build Coastguard Worker }
42*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_pthread_getspecific);
43*8d67ca89SAndroid Build Coastguard Worker
BM_pthread_setspecific(benchmark::State & state)44*8d67ca89SAndroid Build Coastguard Worker static void BM_pthread_setspecific(benchmark::State& state) {
45*8d67ca89SAndroid Build Coastguard Worker pthread_key_t key;
46*8d67ca89SAndroid Build Coastguard Worker pthread_key_create(&key, nullptr);
47*8d67ca89SAndroid Build Coastguard Worker
48*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
49*8d67ca89SAndroid Build Coastguard Worker pthread_setspecific(key, nullptr);
50*8d67ca89SAndroid Build Coastguard Worker }
51*8d67ca89SAndroid Build Coastguard Worker
52*8d67ca89SAndroid Build Coastguard Worker pthread_key_delete(key);
53*8d67ca89SAndroid Build Coastguard Worker }
54*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_pthread_setspecific);
55*8d67ca89SAndroid Build Coastguard Worker
NoOpPthreadOnceInitFunction()56*8d67ca89SAndroid Build Coastguard Worker static void NoOpPthreadOnceInitFunction() {}
57*8d67ca89SAndroid Build Coastguard Worker
BM_pthread_once(benchmark::State & state)58*8d67ca89SAndroid Build Coastguard Worker static void BM_pthread_once(benchmark::State& state) {
59*8d67ca89SAndroid Build Coastguard Worker static pthread_once_t once = PTHREAD_ONCE_INIT;
60*8d67ca89SAndroid Build Coastguard Worker pthread_once(&once, NoOpPthreadOnceInitFunction);
61*8d67ca89SAndroid Build Coastguard Worker
62*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
63*8d67ca89SAndroid Build Coastguard Worker pthread_once(&once, NoOpPthreadOnceInitFunction);
64*8d67ca89SAndroid Build Coastguard Worker }
65*8d67ca89SAndroid Build Coastguard Worker }
66*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_pthread_once);
67*8d67ca89SAndroid Build Coastguard Worker
BM_pthread_mutex_lock(benchmark::State & state)68*8d67ca89SAndroid Build Coastguard Worker static void BM_pthread_mutex_lock(benchmark::State& state) {
69*8d67ca89SAndroid Build Coastguard Worker pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
70*8d67ca89SAndroid Build Coastguard Worker
71*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
72*8d67ca89SAndroid Build Coastguard Worker pthread_mutex_lock(&mutex);
73*8d67ca89SAndroid Build Coastguard Worker pthread_mutex_unlock(&mutex);
74*8d67ca89SAndroid Build Coastguard Worker }
75*8d67ca89SAndroid Build Coastguard Worker }
76*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_pthread_mutex_lock);
77*8d67ca89SAndroid Build Coastguard Worker
78*8d67ca89SAndroid Build Coastguard Worker #if !defined(ANDROID_HOST_MUSL)
BM_pthread_mutex_lock_ERRORCHECK(benchmark::State & state)79*8d67ca89SAndroid Build Coastguard Worker static void BM_pthread_mutex_lock_ERRORCHECK(benchmark::State& state) {
80*8d67ca89SAndroid Build Coastguard Worker pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
81*8d67ca89SAndroid Build Coastguard Worker
82*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
83*8d67ca89SAndroid Build Coastguard Worker pthread_mutex_lock(&mutex);
84*8d67ca89SAndroid Build Coastguard Worker pthread_mutex_unlock(&mutex);
85*8d67ca89SAndroid Build Coastguard Worker }
86*8d67ca89SAndroid Build Coastguard Worker }
87*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_pthread_mutex_lock_ERRORCHECK);
88*8d67ca89SAndroid Build Coastguard Worker #endif
89*8d67ca89SAndroid Build Coastguard Worker
90*8d67ca89SAndroid Build Coastguard Worker #if !defined(ANDROID_HOST_MUSL)
BM_pthread_mutex_lock_RECURSIVE(benchmark::State & state)91*8d67ca89SAndroid Build Coastguard Worker static void BM_pthread_mutex_lock_RECURSIVE(benchmark::State& state) {
92*8d67ca89SAndroid Build Coastguard Worker pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
93*8d67ca89SAndroid Build Coastguard Worker
94*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
95*8d67ca89SAndroid Build Coastguard Worker pthread_mutex_lock(&mutex);
96*8d67ca89SAndroid Build Coastguard Worker pthread_mutex_unlock(&mutex);
97*8d67ca89SAndroid Build Coastguard Worker }
98*8d67ca89SAndroid Build Coastguard Worker }
99*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_pthread_mutex_lock_RECURSIVE);
100*8d67ca89SAndroid Build Coastguard Worker #endif
101*8d67ca89SAndroid Build Coastguard Worker
102*8d67ca89SAndroid Build Coastguard Worker namespace {
103*8d67ca89SAndroid Build Coastguard Worker struct PIMutex {
104*8d67ca89SAndroid Build Coastguard Worker pthread_mutex_t mutex;
105*8d67ca89SAndroid Build Coastguard Worker
PIMutex__anondcdb9ba80111::PIMutex106*8d67ca89SAndroid Build Coastguard Worker explicit PIMutex(int type) {
107*8d67ca89SAndroid Build Coastguard Worker pthread_mutexattr_t attr;
108*8d67ca89SAndroid Build Coastguard Worker pthread_mutexattr_init(&attr);
109*8d67ca89SAndroid Build Coastguard Worker pthread_mutexattr_settype(&attr, type);
110*8d67ca89SAndroid Build Coastguard Worker pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
111*8d67ca89SAndroid Build Coastguard Worker pthread_mutex_init(&mutex, &attr);
112*8d67ca89SAndroid Build Coastguard Worker pthread_mutexattr_destroy(&attr);
113*8d67ca89SAndroid Build Coastguard Worker }
114*8d67ca89SAndroid Build Coastguard Worker
~PIMutex__anondcdb9ba80111::PIMutex115*8d67ca89SAndroid Build Coastguard Worker ~PIMutex() {
116*8d67ca89SAndroid Build Coastguard Worker pthread_mutex_destroy(&mutex);
117*8d67ca89SAndroid Build Coastguard Worker }
118*8d67ca89SAndroid Build Coastguard Worker };
119*8d67ca89SAndroid Build Coastguard Worker }
120*8d67ca89SAndroid Build Coastguard Worker
BM_pthread_mutex_lock_PI(benchmark::State & state)121*8d67ca89SAndroid Build Coastguard Worker static void BM_pthread_mutex_lock_PI(benchmark::State& state) {
122*8d67ca89SAndroid Build Coastguard Worker PIMutex m(PTHREAD_MUTEX_NORMAL);
123*8d67ca89SAndroid Build Coastguard Worker
124*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
125*8d67ca89SAndroid Build Coastguard Worker pthread_mutex_lock(&m.mutex);
126*8d67ca89SAndroid Build Coastguard Worker pthread_mutex_unlock(&m.mutex);
127*8d67ca89SAndroid Build Coastguard Worker }
128*8d67ca89SAndroid Build Coastguard Worker }
129*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_pthread_mutex_lock_PI);
130*8d67ca89SAndroid Build Coastguard Worker
BM_pthread_mutex_lock_ERRORCHECK_PI(benchmark::State & state)131*8d67ca89SAndroid Build Coastguard Worker static void BM_pthread_mutex_lock_ERRORCHECK_PI(benchmark::State& state) {
132*8d67ca89SAndroid Build Coastguard Worker PIMutex m(PTHREAD_MUTEX_ERRORCHECK);
133*8d67ca89SAndroid Build Coastguard Worker
134*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
135*8d67ca89SAndroid Build Coastguard Worker pthread_mutex_lock(&m.mutex);
136*8d67ca89SAndroid Build Coastguard Worker pthread_mutex_unlock(&m.mutex);
137*8d67ca89SAndroid Build Coastguard Worker }
138*8d67ca89SAndroid Build Coastguard Worker }
139*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_pthread_mutex_lock_ERRORCHECK_PI);
140*8d67ca89SAndroid Build Coastguard Worker
BM_pthread_mutex_lock_RECURSIVE_PI(benchmark::State & state)141*8d67ca89SAndroid Build Coastguard Worker static void BM_pthread_mutex_lock_RECURSIVE_PI(benchmark::State& state) {
142*8d67ca89SAndroid Build Coastguard Worker PIMutex m(PTHREAD_MUTEX_RECURSIVE);
143*8d67ca89SAndroid Build Coastguard Worker
144*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
145*8d67ca89SAndroid Build Coastguard Worker pthread_mutex_lock(&m.mutex);
146*8d67ca89SAndroid Build Coastguard Worker pthread_mutex_unlock(&m.mutex);
147*8d67ca89SAndroid Build Coastguard Worker }
148*8d67ca89SAndroid Build Coastguard Worker }
149*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_pthread_mutex_lock_RECURSIVE_PI);
150*8d67ca89SAndroid Build Coastguard Worker
BM_pthread_rwlock_read(benchmark::State & state)151*8d67ca89SAndroid Build Coastguard Worker static void BM_pthread_rwlock_read(benchmark::State& state) {
152*8d67ca89SAndroid Build Coastguard Worker pthread_rwlock_t lock;
153*8d67ca89SAndroid Build Coastguard Worker pthread_rwlock_init(&lock, nullptr);
154*8d67ca89SAndroid Build Coastguard Worker
155*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
156*8d67ca89SAndroid Build Coastguard Worker pthread_rwlock_rdlock(&lock);
157*8d67ca89SAndroid Build Coastguard Worker pthread_rwlock_unlock(&lock);
158*8d67ca89SAndroid Build Coastguard Worker }
159*8d67ca89SAndroid Build Coastguard Worker
160*8d67ca89SAndroid Build Coastguard Worker pthread_rwlock_destroy(&lock);
161*8d67ca89SAndroid Build Coastguard Worker }
162*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_pthread_rwlock_read);
163*8d67ca89SAndroid Build Coastguard Worker
BM_pthread_rwlock_write(benchmark::State & state)164*8d67ca89SAndroid Build Coastguard Worker static void BM_pthread_rwlock_write(benchmark::State& state) {
165*8d67ca89SAndroid Build Coastguard Worker pthread_rwlock_t lock;
166*8d67ca89SAndroid Build Coastguard Worker pthread_rwlock_init(&lock, nullptr);
167*8d67ca89SAndroid Build Coastguard Worker
168*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
169*8d67ca89SAndroid Build Coastguard Worker pthread_rwlock_wrlock(&lock);
170*8d67ca89SAndroid Build Coastguard Worker pthread_rwlock_unlock(&lock);
171*8d67ca89SAndroid Build Coastguard Worker }
172*8d67ca89SAndroid Build Coastguard Worker
173*8d67ca89SAndroid Build Coastguard Worker pthread_rwlock_destroy(&lock);
174*8d67ca89SAndroid Build Coastguard Worker }
175*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_pthread_rwlock_write);
176*8d67ca89SAndroid Build Coastguard Worker
IdleThread(void *)177*8d67ca89SAndroid Build Coastguard Worker static void* IdleThread(void*) {
178*8d67ca89SAndroid Build Coastguard Worker return nullptr;
179*8d67ca89SAndroid Build Coastguard Worker }
180*8d67ca89SAndroid Build Coastguard Worker
BM_pthread_create(benchmark::State & state)181*8d67ca89SAndroid Build Coastguard Worker static void BM_pthread_create(benchmark::State& state) {
182*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
183*8d67ca89SAndroid Build Coastguard Worker pthread_t thread;
184*8d67ca89SAndroid Build Coastguard Worker pthread_create(&thread, nullptr, IdleThread, nullptr);
185*8d67ca89SAndroid Build Coastguard Worker state.PauseTiming();
186*8d67ca89SAndroid Build Coastguard Worker pthread_join(thread, nullptr);
187*8d67ca89SAndroid Build Coastguard Worker state.ResumeTiming();
188*8d67ca89SAndroid Build Coastguard Worker }
189*8d67ca89SAndroid Build Coastguard Worker }
190*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_pthread_create);
191*8d67ca89SAndroid Build Coastguard Worker
RunThread(void *)192*8d67ca89SAndroid Build Coastguard Worker static void* RunThread(void*) {
193*8d67ca89SAndroid Build Coastguard Worker return nullptr;
194*8d67ca89SAndroid Build Coastguard Worker }
195*8d67ca89SAndroid Build Coastguard Worker
BM_pthread_create_and_run(benchmark::State & state)196*8d67ca89SAndroid Build Coastguard Worker static void BM_pthread_create_and_run(benchmark::State& state) {
197*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
198*8d67ca89SAndroid Build Coastguard Worker pthread_t thread;
199*8d67ca89SAndroid Build Coastguard Worker pthread_create(&thread, nullptr, RunThread, &state);
200*8d67ca89SAndroid Build Coastguard Worker pthread_join(thread, nullptr);
201*8d67ca89SAndroid Build Coastguard Worker }
202*8d67ca89SAndroid Build Coastguard Worker }
203*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_pthread_create_and_run);
204*8d67ca89SAndroid Build Coastguard Worker
ExitThread(void *)205*8d67ca89SAndroid Build Coastguard Worker static void* ExitThread(void*) {
206*8d67ca89SAndroid Build Coastguard Worker pthread_exit(nullptr);
207*8d67ca89SAndroid Build Coastguard Worker }
208*8d67ca89SAndroid Build Coastguard Worker
BM_pthread_exit_and_join(benchmark::State & state)209*8d67ca89SAndroid Build Coastguard Worker static void BM_pthread_exit_and_join(benchmark::State& state) {
210*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
211*8d67ca89SAndroid Build Coastguard Worker pthread_t thread;
212*8d67ca89SAndroid Build Coastguard Worker pthread_create(&thread, nullptr, ExitThread, nullptr);
213*8d67ca89SAndroid Build Coastguard Worker pthread_join(thread, nullptr);
214*8d67ca89SAndroid Build Coastguard Worker }
215*8d67ca89SAndroid Build Coastguard Worker }
216*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_pthread_exit_and_join);
217*8d67ca89SAndroid Build Coastguard Worker
BM_pthread_key_create(benchmark::State & state)218*8d67ca89SAndroid Build Coastguard Worker static void BM_pthread_key_create(benchmark::State& state) {
219*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
220*8d67ca89SAndroid Build Coastguard Worker pthread_key_t key;
221*8d67ca89SAndroid Build Coastguard Worker pthread_key_create(&key, nullptr);
222*8d67ca89SAndroid Build Coastguard Worker
223*8d67ca89SAndroid Build Coastguard Worker state.PauseTiming();
224*8d67ca89SAndroid Build Coastguard Worker pthread_key_delete(key);
225*8d67ca89SAndroid Build Coastguard Worker state.ResumeTiming();
226*8d67ca89SAndroid Build Coastguard Worker }
227*8d67ca89SAndroid Build Coastguard Worker }
228*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_pthread_key_create);
229*8d67ca89SAndroid Build Coastguard Worker
BM_pthread_key_delete(benchmark::State & state)230*8d67ca89SAndroid Build Coastguard Worker static void BM_pthread_key_delete(benchmark::State& state) {
231*8d67ca89SAndroid Build Coastguard Worker while (state.KeepRunning()) {
232*8d67ca89SAndroid Build Coastguard Worker state.PauseTiming();
233*8d67ca89SAndroid Build Coastguard Worker pthread_key_t key;
234*8d67ca89SAndroid Build Coastguard Worker pthread_key_create(&key, nullptr);
235*8d67ca89SAndroid Build Coastguard Worker state.ResumeTiming();
236*8d67ca89SAndroid Build Coastguard Worker
237*8d67ca89SAndroid Build Coastguard Worker pthread_key_delete(key);
238*8d67ca89SAndroid Build Coastguard Worker }
239*8d67ca89SAndroid Build Coastguard Worker }
240*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_pthread_key_delete);
241