blob: d3c2de865ace9c03a136291504ea945b68f0e95d [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
Elliott Hughesb28e4902014-03-11 11:19:06 -070017#include <pthread.h>
18
Elliott Hughes281e06b2016-02-17 10:23:52 -080019#include <benchmark/benchmark.h>
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080020
Elliott Hughesb27a8402014-06-10 20:47:49 -070021// Stop GCC optimizing out our pure function.
22/* Must not be static! */ pthread_t (*pthread_self_fp)() = pthread_self;
23
Elliott Hughes281e06b2016-02-17 10:23:52 -080024static void BM_pthread_self(benchmark::State& state) {
25 while (state.KeepRunning()) {
Elliott Hughesb27a8402014-06-10 20:47:49 -070026 pthread_self_fp();
Elliott Hughesb28e4902014-03-11 11:19:06 -070027 }
Elliott Hughesb28e4902014-03-11 11:19:06 -070028}
Elliott Hughes281e06b2016-02-17 10:23:52 -080029BENCHMARK(BM_pthread_self);
Elliott Hughesb28e4902014-03-11 11:19:06 -070030
Elliott Hughes281e06b2016-02-17 10:23:52 -080031static void BM_pthread_getspecific(benchmark::State& state) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070032 pthread_key_t key;
33 pthread_key_create(&key, NULL);
Elliott Hughesb28e4902014-03-11 11:19:06 -070034
Elliott Hughes281e06b2016-02-17 10:23:52 -080035 while (state.KeepRunning()) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070036 pthread_getspecific(key);
37 }
38
Elliott Hughesb28e4902014-03-11 11:19:06 -070039 pthread_key_delete(key);
40}
Elliott Hughes281e06b2016-02-17 10:23:52 -080041BENCHMARK(BM_pthread_getspecific);
Elliott Hughesb28e4902014-03-11 11:19:06 -070042
Elliott Hughes281e06b2016-02-17 10:23:52 -080043static void BM_pthread_setspecific(benchmark::State& state) {
Yabin Cui8cf1b302014-12-03 21:36:24 -080044 pthread_key_t key;
45 pthread_key_create(&key, NULL);
Yabin Cui8cf1b302014-12-03 21:36:24 -080046
Elliott Hughes281e06b2016-02-17 10:23:52 -080047 while (state.KeepRunning()) {
Yabin Cui8cf1b302014-12-03 21:36:24 -080048 pthread_setspecific(key, NULL);
49 }
50
Yabin Cui8cf1b302014-12-03 21:36:24 -080051 pthread_key_delete(key);
52}
Elliott Hughes281e06b2016-02-17 10:23:52 -080053BENCHMARK(BM_pthread_setspecific);
Yabin Cui8cf1b302014-12-03 21:36:24 -080054
Elliott Hughesb28e4902014-03-11 11:19:06 -070055static void DummyPthreadOnceInitFunction() {
56}
57
Elliott Hughes281e06b2016-02-17 10:23:52 -080058static void BM_pthread_once(benchmark::State& state) {
George Burgess IV70591002017-06-27 16:23:45 -070059 static pthread_once_t once = PTHREAD_ONCE_INIT;
Elliott Hughesb28e4902014-03-11 11:19:06 -070060 pthread_once(&once, DummyPthreadOnceInitFunction);
Elliott Hughesb28e4902014-03-11 11:19:06 -070061
Elliott Hughes281e06b2016-02-17 10:23:52 -080062 while (state.KeepRunning()) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070063 pthread_once(&once, DummyPthreadOnceInitFunction);
64 }
Elliott Hughesb28e4902014-03-11 11:19:06 -070065}
Elliott Hughes281e06b2016-02-17 10:23:52 -080066BENCHMARK(BM_pthread_once);
Elliott Hughesb28e4902014-03-11 11:19:06 -070067
Elliott Hughes281e06b2016-02-17 10:23:52 -080068static void BM_pthread_mutex_lock(benchmark::State& state) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070069 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
Elliott Hughesb28e4902014-03-11 11:19:06 -070070
Elliott Hughes281e06b2016-02-17 10:23:52 -080071 while (state.KeepRunning()) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070072 pthread_mutex_lock(&mutex);
73 pthread_mutex_unlock(&mutex);
74 }
Elliott Hughesb28e4902014-03-11 11:19:06 -070075}
Elliott Hughes281e06b2016-02-17 10:23:52 -080076BENCHMARK(BM_pthread_mutex_lock);
Elliott Hughesb28e4902014-03-11 11:19:06 -070077
Elliott Hughes281e06b2016-02-17 10:23:52 -080078static void BM_pthread_mutex_lock_ERRORCHECK(benchmark::State& state) {
Elliott Hughes212e0e32014-12-01 16:43:51 -080079 pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
Elliott Hughesb28e4902014-03-11 11:19:06 -070080
Elliott Hughes281e06b2016-02-17 10:23:52 -080081 while (state.KeepRunning()) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070082 pthread_mutex_lock(&mutex);
83 pthread_mutex_unlock(&mutex);
84 }
Elliott Hughesb28e4902014-03-11 11:19:06 -070085}
Elliott Hughes281e06b2016-02-17 10:23:52 -080086BENCHMARK(BM_pthread_mutex_lock_ERRORCHECK);
Elliott Hughesb28e4902014-03-11 11:19:06 -070087
Elliott Hughes281e06b2016-02-17 10:23:52 -080088static void BM_pthread_mutex_lock_RECURSIVE(benchmark::State& state) {
Elliott Hughes212e0e32014-12-01 16:43:51 -080089 pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
Elliott Hughesb28e4902014-03-11 11:19:06 -070090
Elliott Hughes281e06b2016-02-17 10:23:52 -080091 while (state.KeepRunning()) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070092 pthread_mutex_lock(&mutex);
93 pthread_mutex_unlock(&mutex);
94 }
Elliott Hughesb28e4902014-03-11 11:19:06 -070095}
Elliott Hughes281e06b2016-02-17 10:23:52 -080096BENCHMARK(BM_pthread_mutex_lock_RECURSIVE);
Calin Juravle837a9622014-09-16 18:01:44 +010097
Elliott Hughes281e06b2016-02-17 10:23:52 -080098static void BM_pthread_rwlock_read(benchmark::State& state) {
Calin Juravle837a9622014-09-16 18:01:44 +010099 pthread_rwlock_t lock;
100 pthread_rwlock_init(&lock, NULL);
Calin Juravle837a9622014-09-16 18:01:44 +0100101
Elliott Hughes281e06b2016-02-17 10:23:52 -0800102 while (state.KeepRunning()) {
Calin Juravle837a9622014-09-16 18:01:44 +0100103 pthread_rwlock_rdlock(&lock);
104 pthread_rwlock_unlock(&lock);
105 }
106
Calin Juravle837a9622014-09-16 18:01:44 +0100107 pthread_rwlock_destroy(&lock);
108}
Elliott Hughes281e06b2016-02-17 10:23:52 -0800109BENCHMARK(BM_pthread_rwlock_read);
Calin Juravle837a9622014-09-16 18:01:44 +0100110
Elliott Hughes281e06b2016-02-17 10:23:52 -0800111static void BM_pthread_rwlock_write(benchmark::State& state) {
Calin Juravle837a9622014-09-16 18:01:44 +0100112 pthread_rwlock_t lock;
113 pthread_rwlock_init(&lock, NULL);
Calin Juravle837a9622014-09-16 18:01:44 +0100114
Elliott Hughes281e06b2016-02-17 10:23:52 -0800115 while (state.KeepRunning()) {
Calin Juravle837a9622014-09-16 18:01:44 +0100116 pthread_rwlock_wrlock(&lock);
117 pthread_rwlock_unlock(&lock);
118 }
119
Calin Juravle837a9622014-09-16 18:01:44 +0100120 pthread_rwlock_destroy(&lock);
121}
Elliott Hughes281e06b2016-02-17 10:23:52 -0800122BENCHMARK(BM_pthread_rwlock_write);
Yabin Cui8cf1b302014-12-03 21:36:24 -0800123
124static void* IdleThread(void*) {
125 return NULL;
126}
127
Elliott Hughes281e06b2016-02-17 10:23:52 -0800128static void BM_pthread_create(benchmark::State& state) {
129 while (state.KeepRunning()) {
130 pthread_t thread;
Yabin Cui8cf1b302014-12-03 21:36:24 -0800131 pthread_create(&thread, NULL, IdleThread, NULL);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800132 state.PauseTiming();
Yabin Cui8cf1b302014-12-03 21:36:24 -0800133 pthread_join(thread, NULL);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800134 state.ResumeTiming();
Yabin Cui8cf1b302014-12-03 21:36:24 -0800135 }
136}
Elliott Hughes281e06b2016-02-17 10:23:52 -0800137BENCHMARK(BM_pthread_create);
Yabin Cui8cf1b302014-12-03 21:36:24 -0800138
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800139static void* RunThread(void* arg) {
Elliott Hughes281e06b2016-02-17 10:23:52 -0800140 benchmark::State& state = *reinterpret_cast<benchmark::State*>(arg);
141 state.PauseTiming();
Yabin Cui8cf1b302014-12-03 21:36:24 -0800142 return NULL;
143}
144
Elliott Hughes281e06b2016-02-17 10:23:52 -0800145static void BM_pthread_create_and_run(benchmark::State& state) {
146 while (state.KeepRunning()) {
147 pthread_t thread;
148 pthread_create(&thread, NULL, RunThread, &state);
Yabin Cui8cf1b302014-12-03 21:36:24 -0800149 pthread_join(thread, NULL);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800150 state.ResumeTiming();
Yabin Cui8cf1b302014-12-03 21:36:24 -0800151 }
152}
Elliott Hughes281e06b2016-02-17 10:23:52 -0800153BENCHMARK(BM_pthread_create_and_run);
Yabin Cui8cf1b302014-12-03 21:36:24 -0800154
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800155static void* ExitThread(void* arg) {
Elliott Hughes281e06b2016-02-17 10:23:52 -0800156 benchmark::State& state = *reinterpret_cast<benchmark::State*>(arg);
157 state.ResumeTiming();
Yabin Cui8cf1b302014-12-03 21:36:24 -0800158 pthread_exit(NULL);
159}
160
Elliott Hughes281e06b2016-02-17 10:23:52 -0800161static void BM_pthread_exit_and_join(benchmark::State& state) {
162 while (state.KeepRunning()) {
163 state.PauseTiming();
164 pthread_t thread;
165 pthread_create(&thread, NULL, ExitThread, &state);
Yabin Cui8cf1b302014-12-03 21:36:24 -0800166 pthread_join(thread, NULL);
Yabin Cui8cf1b302014-12-03 21:36:24 -0800167 }
168}
Elliott Hughes281e06b2016-02-17 10:23:52 -0800169BENCHMARK(BM_pthread_exit_and_join);
Yabin Cui8cf1b302014-12-03 21:36:24 -0800170
Elliott Hughes281e06b2016-02-17 10:23:52 -0800171static void BM_pthread_key_create(benchmark::State& state) {
172 while (state.KeepRunning()) {
173 pthread_key_t key;
Yabin Cui8cf1b302014-12-03 21:36:24 -0800174 pthread_key_create(&key, NULL);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800175
176 state.PauseTiming();
177 pthread_key_delete(key);
178 state.ResumeTiming();
179 }
180}
181BENCHMARK(BM_pthread_key_create);
182
183static void BM_pthread_key_delete(benchmark::State& state) {
184 while (state.KeepRunning()) {
185 state.PauseTiming();
186 pthread_key_t key;
187 pthread_key_create(&key, NULL);
188 state.ResumeTiming();
189
Yabin Cui8cf1b302014-12-03 21:36:24 -0800190 pthread_key_delete(key);
191 }
192}
Elliott Hughes281e06b2016-02-17 10:23:52 -0800193BENCHMARK(BM_pthread_key_delete);