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 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 19 | #include <benchmark/Benchmark.h> |
| 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 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 24 | BENCHMARK_NO_ARG(BM_pthread_self); |
| 25 | void BM_pthread_self::Run(int iters) { |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 26 | StartBenchmarkTiming(); |
| 27 | |
| 28 | for (int i = 0; i < iters; ++i) { |
Elliott Hughes | b27a840 | 2014-06-10 20:47:49 -0700 | [diff] [blame] | 29 | pthread_self_fp(); |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | StopBenchmarkTiming(); |
| 33 | } |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 34 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 35 | BENCHMARK_NO_ARG(BM_pthread_getspecific); |
| 36 | void BM_pthread_getspecific::Run(int iters) { |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 37 | StopBenchmarkTiming(); |
| 38 | pthread_key_t key; |
| 39 | pthread_key_create(&key, NULL); |
| 40 | StartBenchmarkTiming(); |
| 41 | |
| 42 | for (int i = 0; i < iters; ++i) { |
| 43 | pthread_getspecific(key); |
| 44 | } |
| 45 | |
| 46 | StopBenchmarkTiming(); |
| 47 | pthread_key_delete(key); |
| 48 | } |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 49 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 50 | BENCHMARK_NO_ARG(BM_pthread_setspecific); |
| 51 | void BM_pthread_setspecific::Run(int iters) { |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 52 | StopBenchmarkTiming(); |
| 53 | pthread_key_t key; |
| 54 | pthread_key_create(&key, NULL); |
| 55 | StartBenchmarkTiming(); |
| 56 | |
| 57 | for (int i = 0; i < iters; ++i) { |
| 58 | pthread_setspecific(key, NULL); |
| 59 | } |
| 60 | |
| 61 | StopBenchmarkTiming(); |
| 62 | pthread_key_delete(key); |
| 63 | } |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 64 | |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 65 | static void DummyPthreadOnceInitFunction() { |
| 66 | } |
| 67 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 68 | BENCHMARK_NO_ARG(BM_pthread_once); |
| 69 | void BM_pthread_once::Run(int iters) { |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 70 | StopBenchmarkTiming(); |
| 71 | pthread_once_t once = PTHREAD_ONCE_INIT; |
| 72 | pthread_once(&once, DummyPthreadOnceInitFunction); |
| 73 | StartBenchmarkTiming(); |
| 74 | |
| 75 | for (int i = 0; i < iters; ++i) { |
| 76 | pthread_once(&once, DummyPthreadOnceInitFunction); |
| 77 | } |
| 78 | |
| 79 | StopBenchmarkTiming(); |
| 80 | } |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 81 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 82 | BENCHMARK_NO_ARG(BM_pthread_mutex_lock); |
| 83 | void BM_pthread_mutex_lock::Run(int iters) { |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 84 | StopBenchmarkTiming(); |
| 85 | pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
| 86 | StartBenchmarkTiming(); |
| 87 | |
| 88 | for (int i = 0; i < iters; ++i) { |
| 89 | pthread_mutex_lock(&mutex); |
| 90 | pthread_mutex_unlock(&mutex); |
| 91 | } |
| 92 | |
| 93 | StopBenchmarkTiming(); |
| 94 | } |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 95 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 96 | BENCHMARK_NO_ARG(BM_pthread_mutex_lock_ERRORCHECK); |
| 97 | void BM_pthread_mutex_lock_ERRORCHECK::Run(int iters) { |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 98 | StopBenchmarkTiming(); |
Elliott Hughes | 212e0e3 | 2014-12-01 16:43:51 -0800 | [diff] [blame] | 99 | pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP; |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 100 | StartBenchmarkTiming(); |
| 101 | |
| 102 | for (int i = 0; i < iters; ++i) { |
| 103 | pthread_mutex_lock(&mutex); |
| 104 | pthread_mutex_unlock(&mutex); |
| 105 | } |
| 106 | |
| 107 | StopBenchmarkTiming(); |
| 108 | } |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 109 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 110 | BENCHMARK_NO_ARG(BM_pthread_mutex_lock_RECURSIVE); |
| 111 | void BM_pthread_mutex_lock_RECURSIVE::Run(int iters) { |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 112 | StopBenchmarkTiming(); |
Elliott Hughes | 212e0e3 | 2014-12-01 16:43:51 -0800 | [diff] [blame] | 113 | pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; |
Elliott Hughes | b28e490 | 2014-03-11 11:19:06 -0700 | [diff] [blame] | 114 | StartBenchmarkTiming(); |
| 115 | |
| 116 | for (int i = 0; i < iters; ++i) { |
| 117 | pthread_mutex_lock(&mutex); |
| 118 | pthread_mutex_unlock(&mutex); |
| 119 | } |
| 120 | |
| 121 | StopBenchmarkTiming(); |
| 122 | } |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 123 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 124 | BENCHMARK_NO_ARG(BM_pthread_rw_lock_read); |
| 125 | void BM_pthread_rw_lock_read::Run(int iters) { |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 126 | StopBenchmarkTiming(); |
| 127 | pthread_rwlock_t lock; |
| 128 | pthread_rwlock_init(&lock, NULL); |
| 129 | StartBenchmarkTiming(); |
| 130 | |
| 131 | for (int i = 0; i < iters; ++i) { |
| 132 | pthread_rwlock_rdlock(&lock); |
| 133 | pthread_rwlock_unlock(&lock); |
| 134 | } |
| 135 | |
| 136 | StopBenchmarkTiming(); |
| 137 | pthread_rwlock_destroy(&lock); |
| 138 | } |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 139 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 140 | BENCHMARK_NO_ARG(BM_pthread_rw_lock_write); |
| 141 | void BM_pthread_rw_lock_write::Run(int iters) { |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 142 | StopBenchmarkTiming(); |
| 143 | pthread_rwlock_t lock; |
| 144 | pthread_rwlock_init(&lock, NULL); |
| 145 | StartBenchmarkTiming(); |
| 146 | |
| 147 | for (int i = 0; i < iters; ++i) { |
| 148 | pthread_rwlock_wrlock(&lock); |
| 149 | pthread_rwlock_unlock(&lock); |
| 150 | } |
| 151 | |
| 152 | StopBenchmarkTiming(); |
| 153 | pthread_rwlock_destroy(&lock); |
| 154 | } |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 155 | |
| 156 | static void* IdleThread(void*) { |
| 157 | return NULL; |
| 158 | } |
| 159 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 160 | BENCHMARK_NO_ARG(BM_pthread_create); |
| 161 | void BM_pthread_create::Run(int iters) { |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 162 | StopBenchmarkTiming(); |
| 163 | pthread_t thread; |
| 164 | |
| 165 | for (int i = 0; i < iters; ++i) { |
| 166 | StartBenchmarkTiming(); |
| 167 | pthread_create(&thread, NULL, IdleThread, NULL); |
| 168 | StopBenchmarkTiming(); |
| 169 | pthread_join(thread, NULL); |
| 170 | } |
| 171 | } |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 172 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 173 | static void* RunThread(void* arg) { |
| 174 | ::testing::Benchmark* benchmark = reinterpret_cast<::testing::Benchmark*>(arg); |
| 175 | benchmark->StopBenchmarkTiming(); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 176 | return NULL; |
| 177 | } |
| 178 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 179 | BENCHMARK_NO_ARG(BM_pthread_create_and_run); |
| 180 | void BM_pthread_create_and_run::Run(int iters) { |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 181 | StopBenchmarkTiming(); |
| 182 | pthread_t thread; |
| 183 | |
| 184 | for (int i = 0; i < iters; ++i) { |
| 185 | StartBenchmarkTiming(); |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 186 | pthread_create(&thread, NULL, RunThread, this); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 187 | pthread_join(thread, NULL); |
| 188 | } |
| 189 | } |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 190 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 191 | static void* ExitThread(void* arg) { |
| 192 | ::testing::Benchmark* benchmark = reinterpret_cast<::testing::Benchmark*>(arg); |
| 193 | benchmark->StartBenchmarkTiming(); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 194 | pthread_exit(NULL); |
| 195 | } |
| 196 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 197 | BENCHMARK_NO_ARG(BM_pthread_exit_and_join); |
| 198 | void BM_pthread_exit_and_join::Run(int iters) { |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 199 | StopBenchmarkTiming(); |
| 200 | pthread_t thread; |
| 201 | |
| 202 | for (int i = 0; i < iters; ++i) { |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 203 | pthread_create(&thread, NULL, ExitThread, this); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 204 | pthread_join(thread, NULL); |
| 205 | StopBenchmarkTiming(); |
| 206 | } |
| 207 | } |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 208 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 209 | BENCHMARK_NO_ARG(BM_pthread_key_create); |
| 210 | void BM_pthread_key_create::Run(int iters) { |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 211 | StopBenchmarkTiming(); |
| 212 | pthread_key_t key; |
| 213 | |
| 214 | for (int i = 0; i < iters; ++i) { |
| 215 | StartBenchmarkTiming(); |
| 216 | pthread_key_create(&key, NULL); |
| 217 | StopBenchmarkTiming(); |
| 218 | pthread_key_delete(key); |
| 219 | } |
| 220 | } |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 221 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 222 | BENCHMARK_NO_ARG(BM_pthread_key_delete); |
| 223 | void BM_pthread_key_delete::Run(int iters) { |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 224 | StopBenchmarkTiming(); |
| 225 | pthread_key_t key; |
| 226 | |
| 227 | for (int i = 0; i < iters; ++i) { |
| 228 | pthread_key_create(&key, NULL); |
| 229 | StartBenchmarkTiming(); |
| 230 | pthread_key_delete(key); |
| 231 | StopBenchmarkTiming(); |
| 232 | } |
| 233 | } |