Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 17 | #include <pthread.h> |
| 18 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 19 | #include <benchmark/benchmark.h> |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 20 | #include "util.h" |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 21 | |
Elliott Hughes | b27a840 | 2014-06-10 20:47:49 -0700 | [diff] [blame] | 22 | // Stop GCC optimizing out our pure function. |
| 23 | /* Must not be static! */ pthread_t (*pthread_self_fp)() = pthread_self; |
| 24 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 25 | static void BM_pthread_self(benchmark::State& state) { |
| 26 | while (state.KeepRunning()) { |
Elliott Hughes | b27a840 | 2014-06-10 20:47:49 -0700 | [diff] [blame] | 27 | pthread_self_fp(); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 28 | } |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 29 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 30 | BIONIC_BENCHMARK(BM_pthread_self); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 31 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 32 | static void BM_pthread_getspecific(benchmark::State& state) { |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 33 | pthread_key_t key; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 34 | pthread_key_create(&key, nullptr); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 35 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 36 | while (state.KeepRunning()) { |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 37 | pthread_getspecific(key); |
| 38 | } |
| 39 | |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 40 | pthread_key_delete(key); |
| 41 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 42 | BIONIC_BENCHMARK(BM_pthread_getspecific); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 43 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 44 | static void BM_pthread_setspecific(benchmark::State& state) { |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 45 | pthread_key_t key; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 46 | pthread_key_create(&key, nullptr); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 47 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 48 | while (state.KeepRunning()) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 49 | pthread_setspecific(key, nullptr); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 52 | pthread_key_delete(key); |
| 53 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 54 | BIONIC_BENCHMARK(BM_pthread_setspecific); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 55 | |
Elliott Hughes | 68ae6ad | 2020-07-21 16:11:30 -0700 | [diff] [blame^] | 56 | static void NoOpPthreadOnceInitFunction() {} |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 57 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 58 | static void BM_pthread_once(benchmark::State& state) { |
George Burgess IV | 7059100 | 2017-06-27 16:23:45 -0700 | [diff] [blame] | 59 | static pthread_once_t once = PTHREAD_ONCE_INIT; |
Elliott Hughes | 68ae6ad | 2020-07-21 16:11:30 -0700 | [diff] [blame^] | 60 | pthread_once(&once, NoOpPthreadOnceInitFunction); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 61 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 62 | while (state.KeepRunning()) { |
Elliott Hughes | 68ae6ad | 2020-07-21 16:11:30 -0700 | [diff] [blame^] | 63 | pthread_once(&once, NoOpPthreadOnceInitFunction); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 64 | } |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 65 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 66 | BIONIC_BENCHMARK(BM_pthread_once); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 67 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 68 | static void BM_pthread_mutex_lock(benchmark::State& state) { |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 69 | pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 70 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 71 | while (state.KeepRunning()) { |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 72 | pthread_mutex_lock(&mutex); |
| 73 | pthread_mutex_unlock(&mutex); |
| 74 | } |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 75 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 76 | BIONIC_BENCHMARK(BM_pthread_mutex_lock); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 77 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 78 | static void BM_pthread_mutex_lock_ERRORCHECK(benchmark::State& state) { |
Elliott Hughes | 212e0e3 | 2014-12-01 16:43:51 -0800 | [diff] [blame] | 79 | pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 80 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 81 | while (state.KeepRunning()) { |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 82 | pthread_mutex_lock(&mutex); |
| 83 | pthread_mutex_unlock(&mutex); |
| 84 | } |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 85 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 86 | BIONIC_BENCHMARK(BM_pthread_mutex_lock_ERRORCHECK); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 87 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 88 | static void BM_pthread_mutex_lock_RECURSIVE(benchmark::State& state) { |
Elliott Hughes | 212e0e3 | 2014-12-01 16:43:51 -0800 | [diff] [blame] | 89 | pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 90 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 91 | while (state.KeepRunning()) { |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 92 | pthread_mutex_lock(&mutex); |
| 93 | pthread_mutex_unlock(&mutex); |
| 94 | } |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 95 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 96 | BIONIC_BENCHMARK(BM_pthread_mutex_lock_RECURSIVE); |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 97 | |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 98 | namespace { |
| 99 | struct PIMutex { |
| 100 | pthread_mutex_t mutex; |
| 101 | |
Chih-Hung Hsieh | 770032d | 2019-01-02 10:59:48 -0800 | [diff] [blame] | 102 | explicit PIMutex(int type) { |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 103 | pthread_mutexattr_t attr; |
| 104 | pthread_mutexattr_init(&attr); |
| 105 | pthread_mutexattr_settype(&attr, type); |
| 106 | pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT); |
| 107 | pthread_mutex_init(&mutex, &attr); |
| 108 | pthread_mutexattr_destroy(&attr); |
| 109 | } |
| 110 | |
| 111 | ~PIMutex() { |
| 112 | pthread_mutex_destroy(&mutex); |
| 113 | } |
| 114 | }; |
| 115 | } |
| 116 | |
| 117 | static void BM_pthread_mutex_lock_PI(benchmark::State& state) { |
| 118 | PIMutex m(PTHREAD_MUTEX_NORMAL); |
| 119 | |
| 120 | while (state.KeepRunning()) { |
| 121 | pthread_mutex_lock(&m.mutex); |
| 122 | pthread_mutex_unlock(&m.mutex); |
| 123 | } |
| 124 | } |
| 125 | BIONIC_BENCHMARK(BM_pthread_mutex_lock_PI); |
| 126 | |
| 127 | static void BM_pthread_mutex_lock_ERRORCHECK_PI(benchmark::State& state) { |
| 128 | PIMutex m(PTHREAD_MUTEX_ERRORCHECK); |
| 129 | |
| 130 | while (state.KeepRunning()) { |
| 131 | pthread_mutex_lock(&m.mutex); |
| 132 | pthread_mutex_unlock(&m.mutex); |
| 133 | } |
| 134 | } |
| 135 | BIONIC_BENCHMARK(BM_pthread_mutex_lock_ERRORCHECK_PI); |
| 136 | |
| 137 | static void BM_pthread_mutex_lock_RECURSIVE_PI(benchmark::State& state) { |
| 138 | PIMutex m(PTHREAD_MUTEX_RECURSIVE); |
| 139 | |
| 140 | while (state.KeepRunning()) { |
| 141 | pthread_mutex_lock(&m.mutex); |
| 142 | pthread_mutex_unlock(&m.mutex); |
| 143 | } |
| 144 | } |
| 145 | BIONIC_BENCHMARK(BM_pthread_mutex_lock_RECURSIVE_PI); |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 146 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 147 | static void BM_pthread_rwlock_read(benchmark::State& state) { |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 148 | pthread_rwlock_t lock; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 149 | pthread_rwlock_init(&lock, nullptr); |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 150 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 151 | while (state.KeepRunning()) { |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 152 | pthread_rwlock_rdlock(&lock); |
| 153 | pthread_rwlock_unlock(&lock); |
| 154 | } |
| 155 | |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 156 | pthread_rwlock_destroy(&lock); |
| 157 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 158 | BIONIC_BENCHMARK(BM_pthread_rwlock_read); |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 159 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 160 | static void BM_pthread_rwlock_write(benchmark::State& state) { |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 161 | pthread_rwlock_t lock; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 162 | pthread_rwlock_init(&lock, nullptr); |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 163 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 164 | while (state.KeepRunning()) { |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 165 | pthread_rwlock_wrlock(&lock); |
| 166 | pthread_rwlock_unlock(&lock); |
| 167 | } |
| 168 | |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 169 | pthread_rwlock_destroy(&lock); |
| 170 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 171 | BIONIC_BENCHMARK(BM_pthread_rwlock_write); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 172 | |
| 173 | static void* IdleThread(void*) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 174 | return nullptr; |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 177 | static void BM_pthread_create(benchmark::State& state) { |
| 178 | while (state.KeepRunning()) { |
| 179 | pthread_t thread; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 180 | pthread_create(&thread, nullptr, IdleThread, nullptr); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 181 | state.PauseTiming(); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 182 | pthread_join(thread, nullptr); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 183 | state.ResumeTiming(); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 184 | } |
| 185 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 186 | BIONIC_BENCHMARK(BM_pthread_create); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 187 | |
Josh Gao | 286b3a8 | 2017-10-24 14:00:09 -0700 | [diff] [blame] | 188 | static void* RunThread(void*) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 189 | return nullptr; |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 192 | static void BM_pthread_create_and_run(benchmark::State& state) { |
| 193 | while (state.KeepRunning()) { |
| 194 | pthread_t thread; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 195 | pthread_create(&thread, nullptr, RunThread, &state); |
| 196 | pthread_join(thread, nullptr); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 197 | } |
| 198 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 199 | BIONIC_BENCHMARK(BM_pthread_create_and_run); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 200 | |
Josh Gao | 286b3a8 | 2017-10-24 14:00:09 -0700 | [diff] [blame] | 201 | static void* ExitThread(void*) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 202 | pthread_exit(nullptr); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 205 | static void BM_pthread_exit_and_join(benchmark::State& state) { |
| 206 | while (state.KeepRunning()) { |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 207 | pthread_t thread; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 208 | pthread_create(&thread, nullptr, ExitThread, nullptr); |
| 209 | pthread_join(thread, nullptr); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 210 | } |
| 211 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 212 | BIONIC_BENCHMARK(BM_pthread_exit_and_join); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 213 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 214 | static void BM_pthread_key_create(benchmark::State& state) { |
| 215 | while (state.KeepRunning()) { |
| 216 | pthread_key_t key; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 217 | pthread_key_create(&key, nullptr); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 218 | |
| 219 | state.PauseTiming(); |
| 220 | pthread_key_delete(key); |
| 221 | state.ResumeTiming(); |
| 222 | } |
| 223 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 224 | BIONIC_BENCHMARK(BM_pthread_key_create); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 225 | |
| 226 | static void BM_pthread_key_delete(benchmark::State& state) { |
| 227 | while (state.KeepRunning()) { |
| 228 | state.PauseTiming(); |
| 229 | pthread_key_t key; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 230 | pthread_key_create(&key, nullptr); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 231 | state.ResumeTiming(); |
| 232 | |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 233 | pthread_key_delete(key); |
| 234 | } |
| 235 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 236 | BIONIC_BENCHMARK(BM_pthread_key_delete); |