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