blob: 2f6572dd86bec357e764b170ac3f944a6c9ad9ab [file] [log] [blame]
Elliott Hughesb28e4902014-03-11 11:19:06 -07001/*
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 Hughesb28e4902014-03-11 11:19:06 -070017#include <pthread.h>
18
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080019#include <benchmark/Benchmark.h>
20
Elliott Hughesb27a8402014-06-10 20:47:49 -070021// Stop GCC optimizing out our pure function.
22/* Must not be static! */ pthread_t (*pthread_self_fp)() = pthread_self;
23
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080024BENCHMARK_NO_ARG(BM_pthread_self);
25void BM_pthread_self::Run(int iters) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070026 StartBenchmarkTiming();
27
28 for (int i = 0; i < iters; ++i) {
Elliott Hughesb27a8402014-06-10 20:47:49 -070029 pthread_self_fp();
Elliott Hughesb28e4902014-03-11 11:19:06 -070030 }
31
32 StopBenchmarkTiming();
33}
Elliott Hughesb28e4902014-03-11 11:19:06 -070034
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080035BENCHMARK_NO_ARG(BM_pthread_getspecific);
36void BM_pthread_getspecific::Run(int iters) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070037 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 Hughesb28e4902014-03-11 11:19:06 -070049
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080050BENCHMARK_NO_ARG(BM_pthread_setspecific);
51void BM_pthread_setspecific::Run(int iters) {
Yabin Cui8cf1b302014-12-03 21:36:24 -080052 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 Cui8cf1b302014-12-03 21:36:24 -080064
Elliott Hughesb28e4902014-03-11 11:19:06 -070065static void DummyPthreadOnceInitFunction() {
66}
67
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080068BENCHMARK_NO_ARG(BM_pthread_once);
69void BM_pthread_once::Run(int iters) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070070 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 Hughesb28e4902014-03-11 11:19:06 -070081
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080082BENCHMARK_NO_ARG(BM_pthread_mutex_lock);
83void BM_pthread_mutex_lock::Run(int iters) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070084 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 Hughesb28e4902014-03-11 11:19:06 -070095
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080096BENCHMARK_NO_ARG(BM_pthread_mutex_lock_ERRORCHECK);
97void BM_pthread_mutex_lock_ERRORCHECK::Run(int iters) {
Elliott Hughesb28e4902014-03-11 11:19:06 -070098 StopBenchmarkTiming();
Elliott Hughes212e0e32014-12-01 16:43:51 -080099 pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
Elliott Hughesb28e4902014-03-11 11:19:06 -0700100 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 Hughesb28e4902014-03-11 11:19:06 -0700109
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800110BENCHMARK_NO_ARG(BM_pthread_mutex_lock_RECURSIVE);
111void BM_pthread_mutex_lock_RECURSIVE::Run(int iters) {
Elliott Hughesb28e4902014-03-11 11:19:06 -0700112 StopBenchmarkTiming();
Elliott Hughes212e0e32014-12-01 16:43:51 -0800113 pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
Elliott Hughesb28e4902014-03-11 11:19:06 -0700114 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 Juravle837a9622014-09-16 18:01:44 +0100123
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800124BENCHMARK_NO_ARG(BM_pthread_rw_lock_read);
125void BM_pthread_rw_lock_read::Run(int iters) {
Calin Juravle837a9622014-09-16 18:01:44 +0100126 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 Juravle837a9622014-09-16 18:01:44 +0100139
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800140BENCHMARK_NO_ARG(BM_pthread_rw_lock_write);
141void BM_pthread_rw_lock_write::Run(int iters) {
Calin Juravle837a9622014-09-16 18:01:44 +0100142 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 Cui8cf1b302014-12-03 21:36:24 -0800155
156static void* IdleThread(void*) {
157 return NULL;
158}
159
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800160BENCHMARK_NO_ARG(BM_pthread_create);
161void BM_pthread_create::Run(int iters) {
Yabin Cui8cf1b302014-12-03 21:36:24 -0800162 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 Cui8cf1b302014-12-03 21:36:24 -0800172
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800173static void* RunThread(void* arg) {
174 ::testing::Benchmark* benchmark = reinterpret_cast<::testing::Benchmark*>(arg);
175 benchmark->StopBenchmarkTiming();
Yabin Cui8cf1b302014-12-03 21:36:24 -0800176 return NULL;
177}
178
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800179BENCHMARK_NO_ARG(BM_pthread_create_and_run);
180void BM_pthread_create_and_run::Run(int iters) {
Yabin Cui8cf1b302014-12-03 21:36:24 -0800181 StopBenchmarkTiming();
182 pthread_t thread;
183
184 for (int i = 0; i < iters; ++i) {
185 StartBenchmarkTiming();
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800186 pthread_create(&thread, NULL, RunThread, this);
Yabin Cui8cf1b302014-12-03 21:36:24 -0800187 pthread_join(thread, NULL);
188 }
189}
Yabin Cui8cf1b302014-12-03 21:36:24 -0800190
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800191static void* ExitThread(void* arg) {
192 ::testing::Benchmark* benchmark = reinterpret_cast<::testing::Benchmark*>(arg);
193 benchmark->StartBenchmarkTiming();
Yabin Cui8cf1b302014-12-03 21:36:24 -0800194 pthread_exit(NULL);
195}
196
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800197BENCHMARK_NO_ARG(BM_pthread_exit_and_join);
198void BM_pthread_exit_and_join::Run(int iters) {
Yabin Cui8cf1b302014-12-03 21:36:24 -0800199 StopBenchmarkTiming();
200 pthread_t thread;
201
202 for (int i = 0; i < iters; ++i) {
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800203 pthread_create(&thread, NULL, ExitThread, this);
Yabin Cui8cf1b302014-12-03 21:36:24 -0800204 pthread_join(thread, NULL);
205 StopBenchmarkTiming();
206 }
207}
Yabin Cui8cf1b302014-12-03 21:36:24 -0800208
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800209BENCHMARK_NO_ARG(BM_pthread_key_create);
210void BM_pthread_key_create::Run(int iters) {
Yabin Cui8cf1b302014-12-03 21:36:24 -0800211 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 Cui8cf1b302014-12-03 21:36:24 -0800221
Christopher Ferrisdf4942c2015-02-17 19:58:53 -0800222BENCHMARK_NO_ARG(BM_pthread_key_delete);
223void BM_pthread_key_delete::Run(int iters) {
Yabin Cui8cf1b302014-12-03 21:36:24 -0800224 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}