2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "benchmark.h"
21 // Stop GCC optimizing out our pure function.
22 /* Must not be static! */ pthread_t (*pthread_self_fp)() = pthread_self;
24 static void BM_pthread_self(int iters) {
25 StartBenchmarkTiming();
27 for (int i = 0; i < iters; ++i) {
31 StopBenchmarkTiming();
33 BENCHMARK(BM_pthread_self);
35 static void BM_pthread_getspecific(int iters) {
36 StopBenchmarkTiming();
38 pthread_key_create(&key, NULL);
39 StartBenchmarkTiming();
41 for (int i = 0; i < iters; ++i) {
42 pthread_getspecific(key);
45 StopBenchmarkTiming();
46 pthread_key_delete(key);
48 BENCHMARK(BM_pthread_getspecific);
50 static void BM_pthread_setspecific(int iters) {
51 StopBenchmarkTiming();
53 pthread_key_create(&key, NULL);
54 StartBenchmarkTiming();
56 for (int i = 0; i < iters; ++i) {
57 pthread_setspecific(key, NULL);
60 StopBenchmarkTiming();
61 pthread_key_delete(key);
63 BENCHMARK(BM_pthread_setspecific);
65 static void DummyPthreadOnceInitFunction() {
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();
74 for (int i = 0; i < iters; ++i) {
75 pthread_once(&once, DummyPthreadOnceInitFunction);
78 StopBenchmarkTiming();
80 BENCHMARK(BM_pthread_once);
82 static void BM_pthread_mutex_lock(int iters) {
83 StopBenchmarkTiming();
84 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
85 StartBenchmarkTiming();
87 for (int i = 0; i < iters; ++i) {
88 pthread_mutex_lock(&mutex);
89 pthread_mutex_unlock(&mutex);
92 StopBenchmarkTiming();
94 BENCHMARK(BM_pthread_mutex_lock);
96 static void BM_pthread_mutex_lock_ERRORCHECK(int iters) {
97 StopBenchmarkTiming();
98 pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
99 StartBenchmarkTiming();
101 for (int i = 0; i < iters; ++i) {
102 pthread_mutex_lock(&mutex);
103 pthread_mutex_unlock(&mutex);
106 StopBenchmarkTiming();
108 BENCHMARK(BM_pthread_mutex_lock_ERRORCHECK);
110 static void BM_pthread_mutex_lock_RECURSIVE(int iters) {
111 StopBenchmarkTiming();
112 pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
113 StartBenchmarkTiming();
115 for (int i = 0; i < iters; ++i) {
116 pthread_mutex_lock(&mutex);
117 pthread_mutex_unlock(&mutex);
120 StopBenchmarkTiming();
122 BENCHMARK(BM_pthread_mutex_lock_RECURSIVE);
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();
130 for (int i = 0; i < iters; ++i) {
131 pthread_rwlock_rdlock(&lock);
132 pthread_rwlock_unlock(&lock);
135 StopBenchmarkTiming();
136 pthread_rwlock_destroy(&lock);
138 BENCHMARK(BM_pthread_rw_lock_read);
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();
146 for (int i = 0; i < iters; ++i) {
147 pthread_rwlock_wrlock(&lock);
148 pthread_rwlock_unlock(&lock);
151 StopBenchmarkTiming();
152 pthread_rwlock_destroy(&lock);
154 BENCHMARK(BM_pthread_rw_lock_write);
156 static void* IdleThread(void*) {
160 static void BM_pthread_create(int iters) {
161 StopBenchmarkTiming();
164 for (int i = 0; i < iters; ++i) {
165 StartBenchmarkTiming();
166 pthread_create(&thread, NULL, IdleThread, NULL);
167 StopBenchmarkTiming();
168 pthread_join(thread, NULL);
171 BENCHMARK(BM_pthread_create);
173 static void* RunThread(void*) {
174 StopBenchmarkTiming();
178 static void BM_pthread_create_and_run(int iters) {
179 StopBenchmarkTiming();
182 for (int i = 0; i < iters; ++i) {
183 StartBenchmarkTiming();
184 pthread_create(&thread, NULL, RunThread, NULL);
185 pthread_join(thread, NULL);
188 BENCHMARK(BM_pthread_create_and_run);
190 static void* ExitThread(void*) {
191 StartBenchmarkTiming();
195 static void BM_pthread_exit_and_join(int iters) {
196 StopBenchmarkTiming();
199 for (int i = 0; i < iters; ++i) {
200 pthread_create(&thread, NULL, ExitThread, NULL);
201 pthread_join(thread, NULL);
202 StopBenchmarkTiming();
205 BENCHMARK(BM_pthread_exit_and_join);
207 static void BM_pthread_key_create(int iters) {
208 StopBenchmarkTiming();
211 for (int i = 0; i < iters; ++i) {
212 StartBenchmarkTiming();
213 pthread_key_create(&key, NULL);
214 StopBenchmarkTiming();
215 pthread_key_delete(key);
218 BENCHMARK(BM_pthread_key_create);
220 static void BM_pthread_key_delete(int iters) {
221 StopBenchmarkTiming();
224 for (int i = 0; i < iters; ++i) {
225 pthread_key_create(&key, NULL);
226 StartBenchmarkTiming();
227 pthread_key_delete(key);
228 StopBenchmarkTiming();
231 BENCHMARK(BM_pthread_key_delete);