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 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 99 | static void BM_pthread_rwlock_read(benchmark::State& state) { |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 100 | pthread_rwlock_t lock; |
| 101 | pthread_rwlock_init(&lock, NULL); |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 102 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 103 | while (state.KeepRunning()) { |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 104 | pthread_rwlock_rdlock(&lock); |
| 105 | pthread_rwlock_unlock(&lock); |
| 106 | } |
| 107 | |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 108 | pthread_rwlock_destroy(&lock); |
| 109 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 110 | BIONIC_BENCHMARK(BM_pthread_rwlock_read); |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 111 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 112 | static void BM_pthread_rwlock_write(benchmark::State& state) { |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 113 | pthread_rwlock_t lock; |
| 114 | pthread_rwlock_init(&lock, NULL); |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 115 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 116 | while (state.KeepRunning()) { |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 117 | pthread_rwlock_wrlock(&lock); |
| 118 | pthread_rwlock_unlock(&lock); |
| 119 | } |
| 120 | |
Calin Juravle | 837a962 | 2014-09-16 18:01:44 +0100 | [diff] [blame] | 121 | pthread_rwlock_destroy(&lock); |
| 122 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 123 | BIONIC_BENCHMARK(BM_pthread_rwlock_write); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 124 | |
| 125 | static void* IdleThread(void*) { |
| 126 | return NULL; |
| 127 | } |
| 128 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 129 | static void BM_pthread_create(benchmark::State& state) { |
| 130 | while (state.KeepRunning()) { |
| 131 | pthread_t thread; |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 132 | pthread_create(&thread, NULL, IdleThread, NULL); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 133 | state.PauseTiming(); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 134 | pthread_join(thread, NULL); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 135 | state.ResumeTiming(); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 138 | BIONIC_BENCHMARK(BM_pthread_create); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 139 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 140 | static void* RunThread(void* arg) { |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 141 | benchmark::State& state = *reinterpret_cast<benchmark::State*>(arg); |
| 142 | state.PauseTiming(); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 143 | return NULL; |
| 144 | } |
| 145 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 146 | static void BM_pthread_create_and_run(benchmark::State& state) { |
| 147 | while (state.KeepRunning()) { |
| 148 | pthread_t thread; |
| 149 | pthread_create(&thread, NULL, RunThread, &state); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 150 | pthread_join(thread, NULL); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 151 | state.ResumeTiming(); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 152 | } |
| 153 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 154 | BIONIC_BENCHMARK(BM_pthread_create_and_run); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 155 | |
Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 156 | static void* ExitThread(void* arg) { |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 157 | benchmark::State& state = *reinterpret_cast<benchmark::State*>(arg); |
| 158 | state.ResumeTiming(); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 159 | pthread_exit(NULL); |
| 160 | } |
| 161 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 162 | static void BM_pthread_exit_and_join(benchmark::State& state) { |
| 163 | while (state.KeepRunning()) { |
| 164 | state.PauseTiming(); |
| 165 | pthread_t thread; |
| 166 | pthread_create(&thread, NULL, ExitThread, &state); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 167 | pthread_join(thread, NULL); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 170 | BIONIC_BENCHMARK(BM_pthread_exit_and_join); |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 171 | |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 172 | static void BM_pthread_key_create(benchmark::State& state) { |
| 173 | while (state.KeepRunning()) { |
| 174 | pthread_key_t key; |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 175 | pthread_key_create(&key, NULL); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 176 | |
| 177 | state.PauseTiming(); |
| 178 | pthread_key_delete(key); |
| 179 | state.ResumeTiming(); |
| 180 | } |
| 181 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 182 | BIONIC_BENCHMARK(BM_pthread_key_create); |
Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 183 | |
| 184 | static void BM_pthread_key_delete(benchmark::State& state) { |
| 185 | while (state.KeepRunning()) { |
| 186 | state.PauseTiming(); |
| 187 | pthread_key_t key; |
| 188 | pthread_key_create(&key, NULL); |
| 189 | state.ResumeTiming(); |
| 190 | |
Yabin Cui | 8cf1b30 | 2014-12-03 21:36:24 -0800 | [diff] [blame] | 191 | pthread_key_delete(key); |
| 192 | } |
| 193 | } |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 194 | BIONIC_BENCHMARK(BM_pthread_key_delete); |