| 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> | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 20 |  | 
| Elliott Hughes | b27a840 | 2014-06-10 20:47:49 -0700 | [diff] [blame] | 21 | // Stop GCC optimizing out our pure function. | 
|  | 22 | /* Must not be static! */ pthread_t (*pthread_self_fp)() = pthread_self; | 
|  | 23 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 24 | static void BM_pthread_self(benchmark::State& state) { | 
|  | 25 | while (state.KeepRunning()) { | 
| Elliott Hughes | b27a840 | 2014-06-10 20:47:49 -0700 | [diff] [blame] | 26 | pthread_self_fp(); | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 27 | } | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 28 | } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 29 | BENCHMARK(BM_pthread_self); | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 30 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 31 | static void BM_pthread_getspecific(benchmark::State& state) { | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 32 | pthread_key_t key; | 
|  | 33 | pthread_key_create(&key, NULL); | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 34 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 35 | while (state.KeepRunning()) { | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 36 | pthread_getspecific(key); | 
|  | 37 | } | 
|  | 38 |  | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 39 | pthread_key_delete(key); | 
|  | 40 | } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 41 | BENCHMARK(BM_pthread_getspecific); | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 42 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 43 | static void BM_pthread_setspecific(benchmark::State& state) { | 
| Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 44 | pthread_key_t key; | 
|  | 45 | pthread_key_create(&key, NULL); | 
| Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 46 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 47 | while (state.KeepRunning()) { | 
| Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 48 | pthread_setspecific(key, NULL); | 
|  | 49 | } | 
|  | 50 |  | 
| Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 51 | pthread_key_delete(key); | 
|  | 52 | } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 53 | BENCHMARK(BM_pthread_setspecific); | 
| Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 54 |  | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 55 | static void DummyPthreadOnceInitFunction() { | 
|  | 56 | } | 
|  | 57 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 58 | static void BM_pthread_once(benchmark::State& state) { | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 59 | pthread_once_t once = PTHREAD_ONCE_INIT; | 
|  | 60 | pthread_once(&once, DummyPthreadOnceInitFunction); | 
| 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 | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 63 | pthread_once(&once, DummyPthreadOnceInitFunction); | 
|  | 64 | } | 
| Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 65 | } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 66 | 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 | } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 76 | 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 | } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 86 | 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 | } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 96 | BENCHMARK(BM_pthread_mutex_lock_RECURSIVE); | 
| Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 97 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 98 | static void BM_pthread_rwlock_read(benchmark::State& state) { | 
| Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 99 | pthread_rwlock_t lock; | 
|  | 100 | pthread_rwlock_init(&lock, NULL); | 
| Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 101 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 102 | while (state.KeepRunning()) { | 
| Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 103 | pthread_rwlock_rdlock(&lock); | 
|  | 104 | pthread_rwlock_unlock(&lock); | 
|  | 105 | } | 
|  | 106 |  | 
| Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 107 | pthread_rwlock_destroy(&lock); | 
|  | 108 | } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 109 | BENCHMARK(BM_pthread_rwlock_read); | 
| Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 110 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 111 | static void BM_pthread_rwlock_write(benchmark::State& state) { | 
| Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 112 | pthread_rwlock_t lock; | 
|  | 113 | pthread_rwlock_init(&lock, NULL); | 
| Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 114 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 115 | while (state.KeepRunning()) { | 
| Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 116 | pthread_rwlock_wrlock(&lock); | 
|  | 117 | pthread_rwlock_unlock(&lock); | 
|  | 118 | } | 
|  | 119 |  | 
| Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 120 | pthread_rwlock_destroy(&lock); | 
|  | 121 | } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 122 | BENCHMARK(BM_pthread_rwlock_write); | 
| Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 123 |  | 
|  | 124 | static void* IdleThread(void*) { | 
|  | 125 | return NULL; | 
|  | 126 | } | 
|  | 127 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 128 | static void BM_pthread_create(benchmark::State& state) { | 
|  | 129 | while (state.KeepRunning()) { | 
|  | 130 | pthread_t thread; | 
| Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 131 | pthread_create(&thread, NULL, IdleThread, NULL); | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 132 | state.PauseTiming(); | 
| Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 133 | pthread_join(thread, NULL); | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 134 | state.ResumeTiming(); | 
| Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 135 | } | 
|  | 136 | } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 137 | BENCHMARK(BM_pthread_create); | 
| Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 138 |  | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 139 | static void* RunThread(void* arg) { | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 140 | benchmark::State& state = *reinterpret_cast<benchmark::State*>(arg); | 
|  | 141 | state.PauseTiming(); | 
| Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 142 | return NULL; | 
|  | 143 | } | 
|  | 144 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 145 | static 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 Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 149 | pthread_join(thread, NULL); | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 150 | state.ResumeTiming(); | 
| Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 151 | } | 
|  | 152 | } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 153 | BENCHMARK(BM_pthread_create_and_run); | 
| Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 154 |  | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 155 | static void* ExitThread(void* arg) { | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 156 | benchmark::State& state = *reinterpret_cast<benchmark::State*>(arg); | 
|  | 157 | state.ResumeTiming(); | 
| Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 158 | pthread_exit(NULL); | 
|  | 159 | } | 
|  | 160 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 161 | static 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 Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 166 | pthread_join(thread, NULL); | 
| Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 167 | } | 
|  | 168 | } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 169 | BENCHMARK(BM_pthread_exit_and_join); | 
| Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 170 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 171 | static void BM_pthread_key_create(benchmark::State& state) { | 
|  | 172 | while (state.KeepRunning()) { | 
|  | 173 | pthread_key_t key; | 
| Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 174 | pthread_key_create(&key, NULL); | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 175 |  | 
|  | 176 | state.PauseTiming(); | 
|  | 177 | pthread_key_delete(key); | 
|  | 178 | state.ResumeTiming(); | 
|  | 179 | } | 
|  | 180 | } | 
|  | 181 | BENCHMARK(BM_pthread_key_create); | 
|  | 182 |  | 
|  | 183 | static 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 Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 190 | pthread_key_delete(key); | 
|  | 191 | } | 
|  | 192 | } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 193 | BENCHMARK(BM_pthread_key_delete); |