blob: 46d6e6430a9af73e7d4c883899d6f29591e36e33 [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>
Anders Lewisa7b0f882017-07-24 20:01:13 -070020#include "util.h"
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080021
Elliott Hughesb27a8402014-06-10 20:47:49 -070022// Stop GCC optimizing out our pure function.
23/* Must not be static! */ pthread_t (*pthread_self_fp)() = pthread_self;
24
Elliott Hughes281e06b2016-02-17 10:23:52 -080025static void BM_pthread_self(benchmark::State& state) {
26 while (state.KeepRunning()) {
Elliott Hughesb27a8402014-06-10 20:47:49 -070027 pthread_self_fp();
Elliott Hughesb28e4902014-03-11 11:19:06 -070028 }
Elliott Hughesb28e4902014-03-11 11:19:06 -070029}
Anders Lewisa7b0f882017-07-24 20:01:13 -070030BIONIC_BENCHMARK(BM_pthread_self);
Elliott Hughesb28e4902014-03-11 11:19:06 -070031
Elliott Hughes281e06b2016-02-17 10:23:52 -080032static void BM_pthread_getspecific(benchmark::State& state) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070033 pthread_key_t key;
34 pthread_key_create(&key, NULL);
Elliott Hughesb28e4902014-03-11 11:19:06 -070035
Elliott Hughes281e06b2016-02-17 10:23:52 -080036 while (state.KeepRunning()) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070037 pthread_getspecific(key);
38 }
39
Elliott Hughesb28e4902014-03-11 11:19:06 -070040 pthread_key_delete(key);
41}
Anders Lewisa7b0f882017-07-24 20:01:13 -070042BIONIC_BENCHMARK(BM_pthread_getspecific);
Elliott Hughesb28e4902014-03-11 11:19:06 -070043
Elliott Hughes281e06b2016-02-17 10:23:52 -080044static void BM_pthread_setspecific(benchmark::State& state) {
Yabin Cui8cf1b302014-12-03 21:36:24 -080045 pthread_key_t key;
46 pthread_key_create(&key, NULL);
Yabin Cui8cf1b302014-12-03 21:36:24 -080047
Elliott Hughes281e06b2016-02-17 10:23:52 -080048 while (state.KeepRunning()) {
Yabin Cui8cf1b302014-12-03 21:36:24 -080049 pthread_setspecific(key, NULL);
50 }
51
Yabin Cui8cf1b302014-12-03 21:36:24 -080052 pthread_key_delete(key);
53}
Anders Lewisa7b0f882017-07-24 20:01:13 -070054BIONIC_BENCHMARK(BM_pthread_setspecific);
Yabin Cui8cf1b302014-12-03 21:36:24 -080055
Elliott Hughesb28e4902014-03-11 11:19:06 -070056static void DummyPthreadOnceInitFunction() {
57}
58
Elliott Hughes281e06b2016-02-17 10:23:52 -080059static void BM_pthread_once(benchmark::State& state) {
George Burgess IV70591002017-06-27 16:23:45 -070060 static pthread_once_t once = PTHREAD_ONCE_INIT;
Elliott Hughesb28e4902014-03-11 11:19:06 -070061 pthread_once(&once, DummyPthreadOnceInitFunction);
Elliott Hughesb28e4902014-03-11 11:19:06 -070062
Elliott Hughes281e06b2016-02-17 10:23:52 -080063 while (state.KeepRunning()) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070064 pthread_once(&once, DummyPthreadOnceInitFunction);
65 }
Elliott Hughesb28e4902014-03-11 11:19:06 -070066}
Anders Lewisa7b0f882017-07-24 20:01:13 -070067BIONIC_BENCHMARK(BM_pthread_once);
Elliott Hughesb28e4902014-03-11 11:19:06 -070068
Elliott Hughes281e06b2016-02-17 10:23:52 -080069static void BM_pthread_mutex_lock(benchmark::State& state) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070070 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
Elliott Hughesb28e4902014-03-11 11:19:06 -070071
Elliott Hughes281e06b2016-02-17 10:23:52 -080072 while (state.KeepRunning()) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070073 pthread_mutex_lock(&mutex);
74 pthread_mutex_unlock(&mutex);
75 }
Elliott Hughesb28e4902014-03-11 11:19:06 -070076}
Anders Lewisa7b0f882017-07-24 20:01:13 -070077BIONIC_BENCHMARK(BM_pthread_mutex_lock);
Elliott Hughesb28e4902014-03-11 11:19:06 -070078
Elliott Hughes281e06b2016-02-17 10:23:52 -080079static void BM_pthread_mutex_lock_ERRORCHECK(benchmark::State& state) {
Elliott Hughes212e0e32014-12-01 16:43:51 -080080 pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
Elliott Hughesb28e4902014-03-11 11:19:06 -070081
Elliott Hughes281e06b2016-02-17 10:23:52 -080082 while (state.KeepRunning()) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070083 pthread_mutex_lock(&mutex);
84 pthread_mutex_unlock(&mutex);
85 }
Elliott Hughesb28e4902014-03-11 11:19:06 -070086}
Anders Lewisa7b0f882017-07-24 20:01:13 -070087BIONIC_BENCHMARK(BM_pthread_mutex_lock_ERRORCHECK);
Elliott Hughesb28e4902014-03-11 11:19:06 -070088
Elliott Hughes281e06b2016-02-17 10:23:52 -080089static void BM_pthread_mutex_lock_RECURSIVE(benchmark::State& state) {
Elliott Hughes212e0e32014-12-01 16:43:51 -080090 pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
Elliott Hughesb28e4902014-03-11 11:19:06 -070091
Elliott Hughes281e06b2016-02-17 10:23:52 -080092 while (state.KeepRunning()) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070093 pthread_mutex_lock(&mutex);
94 pthread_mutex_unlock(&mutex);
95 }
Elliott Hughesb28e4902014-03-11 11:19:06 -070096}
Anders Lewisa7b0f882017-07-24 20:01:13 -070097BIONIC_BENCHMARK(BM_pthread_mutex_lock_RECURSIVE);
Calin Juravle837a9622014-09-16 18:01:44 +010098
Yabin Cui6b9c85b2018-01-23 12:56:18 -080099#if defined(__LP64__)
100namespace {
101struct PIMutex {
102 pthread_mutex_t mutex;
103
104 PIMutex(int type) {
105 pthread_mutexattr_t attr;
106 pthread_mutexattr_init(&attr);
107 pthread_mutexattr_settype(&attr, type);
108 pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
109 pthread_mutex_init(&mutex, &attr);
110 pthread_mutexattr_destroy(&attr);
111 }
112
113 ~PIMutex() {
114 pthread_mutex_destroy(&mutex);
115 }
116};
117}
118
119static void BM_pthread_mutex_lock_PI(benchmark::State& state) {
120 PIMutex m(PTHREAD_MUTEX_NORMAL);
121
122 while (state.KeepRunning()) {
123 pthread_mutex_lock(&m.mutex);
124 pthread_mutex_unlock(&m.mutex);
125 }
126}
127BIONIC_BENCHMARK(BM_pthread_mutex_lock_PI);
128
129static void BM_pthread_mutex_lock_ERRORCHECK_PI(benchmark::State& state) {
130 PIMutex m(PTHREAD_MUTEX_ERRORCHECK);
131
132 while (state.KeepRunning()) {
133 pthread_mutex_lock(&m.mutex);
134 pthread_mutex_unlock(&m.mutex);
135 }
136}
137BIONIC_BENCHMARK(BM_pthread_mutex_lock_ERRORCHECK_PI);
138
139static void BM_pthread_mutex_lock_RECURSIVE_PI(benchmark::State& state) {
140 PIMutex m(PTHREAD_MUTEX_RECURSIVE);
141
142 while (state.KeepRunning()) {
143 pthread_mutex_lock(&m.mutex);
144 pthread_mutex_unlock(&m.mutex);
145 }
146}
147BIONIC_BENCHMARK(BM_pthread_mutex_lock_RECURSIVE_PI);
148#endif // defined(__LP64__)
149
Elliott Hughes281e06b2016-02-17 10:23:52 -0800150static void BM_pthread_rwlock_read(benchmark::State& state) {
Calin Juravle837a9622014-09-16 18:01:44 +0100151 pthread_rwlock_t lock;
152 pthread_rwlock_init(&lock, NULL);
Calin Juravle837a9622014-09-16 18:01:44 +0100153
Elliott Hughes281e06b2016-02-17 10:23:52 -0800154 while (state.KeepRunning()) {
Calin Juravle837a9622014-09-16 18:01:44 +0100155 pthread_rwlock_rdlock(&lock);
156 pthread_rwlock_unlock(&lock);
157 }
158
Calin Juravle837a9622014-09-16 18:01:44 +0100159 pthread_rwlock_destroy(&lock);
160}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700161BIONIC_BENCHMARK(BM_pthread_rwlock_read);
Calin Juravle837a9622014-09-16 18:01:44 +0100162
Elliott Hughes281e06b2016-02-17 10:23:52 -0800163static void BM_pthread_rwlock_write(benchmark::State& state) {
Calin Juravle837a9622014-09-16 18:01:44 +0100164 pthread_rwlock_t lock;
165 pthread_rwlock_init(&lock, NULL);
Calin Juravle837a9622014-09-16 18:01:44 +0100166
Elliott Hughes281e06b2016-02-17 10:23:52 -0800167 while (state.KeepRunning()) {
Calin Juravle837a9622014-09-16 18:01:44 +0100168 pthread_rwlock_wrlock(&lock);
169 pthread_rwlock_unlock(&lock);
170 }
171
Calin Juravle837a9622014-09-16 18:01:44 +0100172 pthread_rwlock_destroy(&lock);
173}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700174BIONIC_BENCHMARK(BM_pthread_rwlock_write);
Yabin Cui8cf1b302014-12-03 21:36:24 -0800175
176static void* IdleThread(void*) {
177 return NULL;
178}
179
Elliott Hughes281e06b2016-02-17 10:23:52 -0800180static void BM_pthread_create(benchmark::State& state) {
181 while (state.KeepRunning()) {
182 pthread_t thread;
Yabin Cui8cf1b302014-12-03 21:36:24 -0800183 pthread_create(&thread, NULL, IdleThread, NULL);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800184 state.PauseTiming();
Yabin Cui8cf1b302014-12-03 21:36:24 -0800185 pthread_join(thread, NULL);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800186 state.ResumeTiming();
Yabin Cui8cf1b302014-12-03 21:36:24 -0800187 }
188}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700189BIONIC_BENCHMARK(BM_pthread_create);
Yabin Cui8cf1b302014-12-03 21:36:24 -0800190
Josh Gao286b3a82017-10-24 14:00:09 -0700191static void* RunThread(void*) {
Yabin Cui8cf1b302014-12-03 21:36:24 -0800192 return NULL;
193}
194
Elliott Hughes281e06b2016-02-17 10:23:52 -0800195static void BM_pthread_create_and_run(benchmark::State& state) {
196 while (state.KeepRunning()) {
197 pthread_t thread;
198 pthread_create(&thread, NULL, RunThread, &state);
Yabin Cui8cf1b302014-12-03 21:36:24 -0800199 pthread_join(thread, NULL);
200 }
201}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700202BIONIC_BENCHMARK(BM_pthread_create_and_run);
Yabin Cui8cf1b302014-12-03 21:36:24 -0800203
Josh Gao286b3a82017-10-24 14:00:09 -0700204static void* ExitThread(void*) {
Yabin Cui8cf1b302014-12-03 21:36:24 -0800205 pthread_exit(NULL);
206}
207
Elliott Hughes281e06b2016-02-17 10:23:52 -0800208static void BM_pthread_exit_and_join(benchmark::State& state) {
209 while (state.KeepRunning()) {
Elliott Hughes281e06b2016-02-17 10:23:52 -0800210 pthread_t thread;
Josh Gao286b3a82017-10-24 14:00:09 -0700211 pthread_create(&thread, NULL, ExitThread, nullptr);
Yabin Cui8cf1b302014-12-03 21:36:24 -0800212 pthread_join(thread, NULL);
Yabin Cui8cf1b302014-12-03 21:36:24 -0800213 }
214}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700215BIONIC_BENCHMARK(BM_pthread_exit_and_join);
Yabin Cui8cf1b302014-12-03 21:36:24 -0800216
Elliott Hughes281e06b2016-02-17 10:23:52 -0800217static void BM_pthread_key_create(benchmark::State& state) {
218 while (state.KeepRunning()) {
219 pthread_key_t key;
Yabin Cui8cf1b302014-12-03 21:36:24 -0800220 pthread_key_create(&key, NULL);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800221
222 state.PauseTiming();
223 pthread_key_delete(key);
224 state.ResumeTiming();
225 }
226}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700227BIONIC_BENCHMARK(BM_pthread_key_create);
Elliott Hughes281e06b2016-02-17 10:23:52 -0800228
229static void BM_pthread_key_delete(benchmark::State& state) {
230 while (state.KeepRunning()) {
231 state.PauseTiming();
232 pthread_key_t key;
233 pthread_key_create(&key, NULL);
234 state.ResumeTiming();
235
Yabin Cui8cf1b302014-12-03 21:36:24 -0800236 pthread_key_delete(key);
237 }
238}
Anders Lewisa7b0f882017-07-24 20:01:13 -0700239BIONIC_BENCHMARK(BM_pthread_key_delete);