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