blob: 92e59982f7c9013d3c32af279d6ced0b6f0c6795 [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
50static void DummyPthreadOnceInitFunction() {
51}
52
53static void BM_pthread_once(int iters) {
54 StopBenchmarkTiming();
55 pthread_once_t once = PTHREAD_ONCE_INIT;
56 pthread_once(&once, DummyPthreadOnceInitFunction);
57 StartBenchmarkTiming();
58
59 for (int i = 0; i < iters; ++i) {
60 pthread_once(&once, DummyPthreadOnceInitFunction);
61 }
62
63 StopBenchmarkTiming();
64}
65BENCHMARK(BM_pthread_once);
66
67static void BM_pthread_mutex_lock(int iters) {
68 StopBenchmarkTiming();
69 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
70 StartBenchmarkTiming();
71
72 for (int i = 0; i < iters; ++i) {
73 pthread_mutex_lock(&mutex);
74 pthread_mutex_unlock(&mutex);
75 }
76
77 StopBenchmarkTiming();
78}
79BENCHMARK(BM_pthread_mutex_lock);
80
81static void BM_pthread_mutex_lock_ERRORCHECK(int iters) {
82 StopBenchmarkTiming();
Elliott Hughes212e0e32014-12-01 16:43:51 -080083 pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
Elliott Hughesb28e4902014-03-11 11:19:06 -070084 StartBenchmarkTiming();
85
86 for (int i = 0; i < iters; ++i) {
87 pthread_mutex_lock(&mutex);
88 pthread_mutex_unlock(&mutex);
89 }
90
91 StopBenchmarkTiming();
92}
93BENCHMARK(BM_pthread_mutex_lock_ERRORCHECK);
94
95static void BM_pthread_mutex_lock_RECURSIVE(int iters) {
96 StopBenchmarkTiming();
Elliott Hughes212e0e32014-12-01 16:43:51 -080097 pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
Elliott Hughesb28e4902014-03-11 11:19:06 -070098 StartBenchmarkTiming();
99
100 for (int i = 0; i < iters; ++i) {
101 pthread_mutex_lock(&mutex);
102 pthread_mutex_unlock(&mutex);
103 }
104
105 StopBenchmarkTiming();
106}
107BENCHMARK(BM_pthread_mutex_lock_RECURSIVE);
Calin Juravle837a9622014-09-16 18:01:44 +0100108
109static void BM_pthread_rw_lock_read(int iters) {
110 StopBenchmarkTiming();
111 pthread_rwlock_t lock;
112 pthread_rwlock_init(&lock, NULL);
113 StartBenchmarkTiming();
114
115 for (int i = 0; i < iters; ++i) {
116 pthread_rwlock_rdlock(&lock);
117 pthread_rwlock_unlock(&lock);
118 }
119
120 StopBenchmarkTiming();
121 pthread_rwlock_destroy(&lock);
122}
123BENCHMARK(BM_pthread_rw_lock_read);
124
125static void BM_pthread_rw_lock_write(int iters) {
126 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_wrlock(&lock);
133 pthread_rwlock_unlock(&lock);
134 }
135
136 StopBenchmarkTiming();
137 pthread_rwlock_destroy(&lock);
138}
139BENCHMARK(BM_pthread_rw_lock_write);