blob: 42023e04ae84662b623dcf485c883e0bf095d894 [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
17#include "benchmark.h"
18
19#include <pthread.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
Elliott Hughesb28e4902014-03-11 11:19:06 -070024static void BM_pthread_self(int iters) {
25 StartBenchmarkTiming();
26
27 for (int i = 0; i < iters; ++i) {
Elliott Hughesb27a8402014-06-10 20:47:49 -070028 pthread_self_fp();
Elliott Hughesb28e4902014-03-11 11:19:06 -070029 }
30
31 StopBenchmarkTiming();
32}
33BENCHMARK(BM_pthread_self);
34
35static 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}
48BENCHMARK(BM_pthread_getspecific);
49
Yabin Cui8cf1b302014-12-03 21:36:24 -080050static 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}
63BENCHMARK(BM_pthread_setspecific);
64
Elliott Hughesb28e4902014-03-11 11:19:06 -070065static void DummyPthreadOnceInitFunction() {
66}
67
68static 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}
80BENCHMARK(BM_pthread_once);
81
82static 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}
94BENCHMARK(BM_pthread_mutex_lock);
95
96static void BM_pthread_mutex_lock_ERRORCHECK(int iters) {
97 StopBenchmarkTiming();
Elliott Hughes212e0e32014-12-01 16:43:51 -080098 pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
Elliott Hughesb28e4902014-03-11 11:19:06 -070099 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}
108BENCHMARK(BM_pthread_mutex_lock_ERRORCHECK);
109
110static void BM_pthread_mutex_lock_RECURSIVE(int iters) {
111 StopBenchmarkTiming();
Elliott Hughes212e0e32014-12-01 16:43:51 -0800112 pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
Elliott Hughesb28e4902014-03-11 11:19:06 -0700113 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}
122BENCHMARK(BM_pthread_mutex_lock_RECURSIVE);
Calin Juravle837a9622014-09-16 18:01:44 +0100123
124static 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}
138BENCHMARK(BM_pthread_rw_lock_read);
139
140static 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}
154BENCHMARK(BM_pthread_rw_lock_write);
Yabin Cui8cf1b302014-12-03 21:36:24 -0800155
156static void* IdleThread(void*) {
157 return NULL;
158}
159
160static 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}
171BENCHMARK(BM_pthread_create);
172
173static void* RunThread(void*) {
174 StopBenchmarkTiming();
175 return NULL;
176}
177
178static 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}
188BENCHMARK(BM_pthread_create_and_run);
189
190static void* ExitThread(void*) {
191 StartBenchmarkTiming();
192 pthread_exit(NULL);
193}
194
195static 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}
205BENCHMARK(BM_pthread_exit_and_join);
206
207static 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}
218BENCHMARK(BM_pthread_key_create);
219
220static 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}
231BENCHMARK(BM_pthread_key_delete);