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 | |
Colin Cross | daa6b82 | 2021-12-15 14:26:43 -0800 | [diff] [blame^] | 78 | #if !defined(ANDROID_HOST_MUSL) |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 79 | static void BM_pthread_mutex_lock_ERRORCHECK(benchmark::State& state) { |
Elliott Hughes | 212e0e3 | 2014-12-01 16:43:51 -0800 | [diff] [blame] | 80 | pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 81 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 82 | while (state.KeepRunning()) { |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 83 | pthread_mutex_lock(&mutex); |
| 84 | pthread_mutex_unlock(&mutex); |
| 85 | } |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 86 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 87 | BIONIC_BENCHMARK(BM_pthread_mutex_lock_ERRORCHECK); |
Colin Cross | daa6b82 | 2021-12-15 14:26:43 -0800 | [diff] [blame^] | 88 | #endif |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 89 | |
Colin Cross | daa6b82 | 2021-12-15 14:26:43 -0800 | [diff] [blame^] | 90 | #if !defined(ANDROID_HOST_MUSL) |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 91 | static void BM_pthread_mutex_lock_RECURSIVE(benchmark::State& state) { |
Elliott Hughes | 212e0e3 | 2014-12-01 16:43:51 -0800 | [diff] [blame] | 92 | pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 93 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 94 | while (state.KeepRunning()) { |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 95 | pthread_mutex_lock(&mutex); |
| 96 | pthread_mutex_unlock(&mutex); |
| 97 | } |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 98 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 99 | BIONIC_BENCHMARK(BM_pthread_mutex_lock_RECURSIVE); |
Colin Cross | daa6b82 | 2021-12-15 14:26:43 -0800 | [diff] [blame^] | 100 | #endif |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 101 | |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 102 | namespace { |
| 103 | struct PIMutex { |
| 104 | pthread_mutex_t mutex; |
| 105 | |
Chih-Hung Hsieh | 770032d | 2019-01-02 10:59:48 -0800 | [diff] [blame] | 106 | explicit PIMutex(int type) { |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 107 | pthread_mutexattr_t attr; |
| 108 | pthread_mutexattr_init(&attr); |
| 109 | pthread_mutexattr_settype(&attr, type); |
| 110 | pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT); |
| 111 | pthread_mutex_init(&mutex, &attr); |
| 112 | pthread_mutexattr_destroy(&attr); |
| 113 | } |
| 114 | |
| 115 | ~PIMutex() { |
| 116 | pthread_mutex_destroy(&mutex); |
| 117 | } |
| 118 | }; |
| 119 | } |
| 120 | |
| 121 | static void BM_pthread_mutex_lock_PI(benchmark::State& state) { |
| 122 | PIMutex m(PTHREAD_MUTEX_NORMAL); |
| 123 | |
| 124 | while (state.KeepRunning()) { |
| 125 | pthread_mutex_lock(&m.mutex); |
| 126 | pthread_mutex_unlock(&m.mutex); |
| 127 | } |
| 128 | } |
| 129 | BIONIC_BENCHMARK(BM_pthread_mutex_lock_PI); |
| 130 | |
| 131 | static void BM_pthread_mutex_lock_ERRORCHECK_PI(benchmark::State& state) { |
| 132 | PIMutex m(PTHREAD_MUTEX_ERRORCHECK); |
| 133 | |
| 134 | while (state.KeepRunning()) { |
| 135 | pthread_mutex_lock(&m.mutex); |
| 136 | pthread_mutex_unlock(&m.mutex); |
| 137 | } |
| 138 | } |
| 139 | BIONIC_BENCHMARK(BM_pthread_mutex_lock_ERRORCHECK_PI); |
| 140 | |
| 141 | static void BM_pthread_mutex_lock_RECURSIVE_PI(benchmark::State& state) { |
| 142 | PIMutex m(PTHREAD_MUTEX_RECURSIVE); |
| 143 | |
| 144 | while (state.KeepRunning()) { |
| 145 | pthread_mutex_lock(&m.mutex); |
| 146 | pthread_mutex_unlock(&m.mutex); |
| 147 | } |
| 148 | } |
| 149 | BIONIC_BENCHMARK(BM_pthread_mutex_lock_RECURSIVE_PI); |
Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 150 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 151 | static void BM_pthread_rwlock_read(benchmark::State& state) { |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 152 | pthread_rwlock_t lock; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 153 | pthread_rwlock_init(&lock, nullptr); |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 154 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 155 | while (state.KeepRunning()) { |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 156 | pthread_rwlock_rdlock(&lock); |
| 157 | pthread_rwlock_unlock(&lock); |
| 158 | } |
| 159 | |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 160 | pthread_rwlock_destroy(&lock); |
| 161 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 162 | BIONIC_BENCHMARK(BM_pthread_rwlock_read); |
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 | static void BM_pthread_rwlock_write(benchmark::State& state) { |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 165 | pthread_rwlock_t lock; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 166 | pthread_rwlock_init(&lock, nullptr); |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 167 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 168 | while (state.KeepRunning()) { |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 169 | pthread_rwlock_wrlock(&lock); |
| 170 | pthread_rwlock_unlock(&lock); |
| 171 | } |
| 172 | |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 173 | pthread_rwlock_destroy(&lock); |
| 174 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 175 | BIONIC_BENCHMARK(BM_pthread_rwlock_write); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 176 | |
| 177 | static void* IdleThread(void*) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 178 | return nullptr; |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 181 | static void BM_pthread_create(benchmark::State& state) { |
| 182 | while (state.KeepRunning()) { |
| 183 | pthread_t thread; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 184 | pthread_create(&thread, nullptr, IdleThread, nullptr); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 185 | state.PauseTiming(); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 186 | pthread_join(thread, nullptr); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 187 | state.ResumeTiming(); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 190 | BIONIC_BENCHMARK(BM_pthread_create); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 191 | |
Josh Gao | 286b3a8 | 2017-10-24 14:00:09 -0700 | [diff] [blame] | 192 | static void* RunThread(void*) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 193 | return nullptr; |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 196 | static void BM_pthread_create_and_run(benchmark::State& state) { |
| 197 | while (state.KeepRunning()) { |
| 198 | pthread_t thread; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 199 | pthread_create(&thread, nullptr, RunThread, &state); |
| 200 | pthread_join(thread, nullptr); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 201 | } |
| 202 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 203 | BIONIC_BENCHMARK(BM_pthread_create_and_run); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 204 | |
Josh Gao | 286b3a8 | 2017-10-24 14:00:09 -0700 | [diff] [blame] | 205 | static void* ExitThread(void*) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 206 | pthread_exit(nullptr); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 209 | static void BM_pthread_exit_and_join(benchmark::State& state) { |
| 210 | while (state.KeepRunning()) { |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 211 | pthread_t thread; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 212 | pthread_create(&thread, nullptr, ExitThread, nullptr); |
| 213 | pthread_join(thread, nullptr); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 214 | } |
| 215 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 216 | BIONIC_BENCHMARK(BM_pthread_exit_and_join); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 217 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 218 | static void BM_pthread_key_create(benchmark::State& state) { |
| 219 | while (state.KeepRunning()) { |
| 220 | pthread_key_t key; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 221 | pthread_key_create(&key, nullptr); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 222 | |
| 223 | state.PauseTiming(); |
| 224 | pthread_key_delete(key); |
| 225 | state.ResumeTiming(); |
| 226 | } |
| 227 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 228 | BIONIC_BENCHMARK(BM_pthread_key_create); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 229 | |
| 230 | static void BM_pthread_key_delete(benchmark::State& state) { |
| 231 | while (state.KeepRunning()) { |
| 232 | state.PauseTiming(); |
| 233 | pthread_key_t key; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 234 | pthread_key_create(&key, nullptr); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 235 | state.ResumeTiming(); |
| 236 | |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 237 | pthread_key_delete(key); |
| 238 | } |
| 239 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 240 | BIONIC_BENCHMARK(BM_pthread_key_delete); |