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 21 // Stop GCC optimizing out our pure function. 22 /* Must not be static! */ pthread_t (*pthread_self_fp)() = pthread_self; 23 BM_pthread_self(int iters)24static void BM_pthread_self(int iters) { 25 StartBenchmarkTiming(); 26 27 for (int i = 0; i < iters; ++i) { 28 pthread_self_fp(); 29 } 30 31 StopBenchmarkTiming(); 32 } 33 BENCHMARK(BM_pthread_self); 34 BM_pthread_getspecific(int iters)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 } 48 BENCHMARK(BM_pthread_getspecific); 49 DummyPthreadOnceInitFunction()50static void DummyPthreadOnceInitFunction() { 51 } 52 BM_pthread_once(int iters)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 } 65 BENCHMARK(BM_pthread_once); 66 BM_pthread_mutex_lock(int iters)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 } 79 BENCHMARK(BM_pthread_mutex_lock); 80 BM_pthread_mutex_lock_ERRORCHECK(int iters)81static void BM_pthread_mutex_lock_ERRORCHECK(int iters) { 82 StopBenchmarkTiming(); 83 pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER; 84 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 } 93 BENCHMARK(BM_pthread_mutex_lock_ERRORCHECK); 94 BM_pthread_mutex_lock_RECURSIVE(int iters)95static void BM_pthread_mutex_lock_RECURSIVE(int iters) { 96 StopBenchmarkTiming(); 97 pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER; 98 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 } 107 BENCHMARK(BM_pthread_mutex_lock_RECURSIVE); 108