2014-03-11 18:19:06 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2016-02-17 18:23:52 +00:00
|
|
|
#include <benchmark/benchmark.h>
|
2017-07-25 03:01:13 +00:00
|
|
|
#include "util.h"
|
2015-02-18 03:58:53 +00:00
|
|
|
|
2014-06-11 03:47:49 +00:00
|
|
|
// Stop GCC optimizing out our pure function.
|
|
|
|
/* Must not be static! */ pthread_t (*pthread_self_fp)() = pthread_self;
|
|
|
|
|
2016-02-17 18:23:52 +00:00
|
|
|
static void BM_pthread_self(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) {
|
2014-06-11 03:47:49 +00:00
|
|
|
pthread_self_fp();
|
2014-03-11 18:19:06 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 03:01:13 +00:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_self);
|
2014-03-11 18:19:06 +00:00
|
|
|
|
2016-02-17 18:23:52 +00:00
|
|
|
static void BM_pthread_getspecific(benchmark::State& state) {
|
2014-03-11 18:19:06 +00:00
|
|
|
pthread_key_t key;
|
|
|
|
pthread_key_create(&key, NULL);
|
|
|
|
|
2016-02-17 18:23:52 +00:00
|
|
|
while (state.KeepRunning()) {
|
2014-03-11 18:19:06 +00:00
|
|
|
pthread_getspecific(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_key_delete(key);
|
|
|
|
}
|
2017-07-25 03:01:13 +00:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_getspecific);
|
2014-03-11 18:19:06 +00:00
|
|
|
|
2016-02-17 18:23:52 +00:00
|
|
|
static void BM_pthread_setspecific(benchmark::State& state) {
|
2014-12-04 05:36:24 +00:00
|
|
|
pthread_key_t key;
|
|
|
|
pthread_key_create(&key, NULL);
|
|
|
|
|
2016-02-17 18:23:52 +00:00
|
|
|
while (state.KeepRunning()) {
|
2014-12-04 05:36:24 +00:00
|
|
|
pthread_setspecific(key, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_key_delete(key);
|
|
|
|
}
|
2017-07-25 03:01:13 +00:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_setspecific);
|
2014-12-04 05:36:24 +00:00
|
|
|
|
2014-03-11 18:19:06 +00:00
|
|
|
static void DummyPthreadOnceInitFunction() {
|
|
|
|
}
|
|
|
|
|
2016-02-17 18:23:52 +00:00
|
|
|
static void BM_pthread_once(benchmark::State& state) {
|
2017-06-27 23:23:45 +00:00
|
|
|
static pthread_once_t once = PTHREAD_ONCE_INIT;
|
2014-03-11 18:19:06 +00:00
|
|
|
pthread_once(&once, DummyPthreadOnceInitFunction);
|
|
|
|
|
2016-02-17 18:23:52 +00:00
|
|
|
while (state.KeepRunning()) {
|
2014-03-11 18:19:06 +00:00
|
|
|
pthread_once(&once, DummyPthreadOnceInitFunction);
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 03:01:13 +00:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_once);
|
2014-03-11 18:19:06 +00:00
|
|
|
|
2016-02-17 18:23:52 +00:00
|
|
|
static void BM_pthread_mutex_lock(benchmark::State& state) {
|
2014-03-11 18:19:06 +00:00
|
|
|
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
2016-02-17 18:23:52 +00:00
|
|
|
while (state.KeepRunning()) {
|
2014-03-11 18:19:06 +00:00
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 03:01:13 +00:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_mutex_lock);
|
2014-03-11 18:19:06 +00:00
|
|
|
|
2016-02-17 18:23:52 +00:00
|
|
|
static void BM_pthread_mutex_lock_ERRORCHECK(benchmark::State& state) {
|
2014-12-02 00:43:51 +00:00
|
|
|
pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
|
2014-03-11 18:19:06 +00:00
|
|
|
|
2016-02-17 18:23:52 +00:00
|
|
|
while (state.KeepRunning()) {
|
2014-03-11 18:19:06 +00:00
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 03:01:13 +00:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_mutex_lock_ERRORCHECK);
|
2014-03-11 18:19:06 +00:00
|
|
|
|
2016-02-17 18:23:52 +00:00
|
|
|
static void BM_pthread_mutex_lock_RECURSIVE(benchmark::State& state) {
|
2014-12-02 00:43:51 +00:00
|
|
|
pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
|
2014-03-11 18:19:06 +00:00
|
|
|
|
2016-02-17 18:23:52 +00:00
|
|
|
while (state.KeepRunning()) {
|
2014-03-11 18:19:06 +00:00
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 03:01:13 +00:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_mutex_lock_RECURSIVE);
|
2014-09-16 17:01:44 +00:00
|
|
|
|
2016-02-17 18:23:52 +00:00
|
|
|
static void BM_pthread_rwlock_read(benchmark::State& state) {
|
2014-09-16 17:01:44 +00:00
|
|
|
pthread_rwlock_t lock;
|
|
|
|
pthread_rwlock_init(&lock, NULL);
|
|
|
|
|
2016-02-17 18:23:52 +00:00
|
|
|
while (state.KeepRunning()) {
|
2014-09-16 17:01:44 +00:00
|
|
|
pthread_rwlock_rdlock(&lock);
|
|
|
|
pthread_rwlock_unlock(&lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_rwlock_destroy(&lock);
|
|
|
|
}
|
2017-07-25 03:01:13 +00:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_rwlock_read);
|
2014-09-16 17:01:44 +00:00
|
|
|
|
2016-02-17 18:23:52 +00:00
|
|
|
static void BM_pthread_rwlock_write(benchmark::State& state) {
|
2014-09-16 17:01:44 +00:00
|
|
|
pthread_rwlock_t lock;
|
|
|
|
pthread_rwlock_init(&lock, NULL);
|
|
|
|
|
2016-02-17 18:23:52 +00:00
|
|
|
while (state.KeepRunning()) {
|
2014-09-16 17:01:44 +00:00
|
|
|
pthread_rwlock_wrlock(&lock);
|
|
|
|
pthread_rwlock_unlock(&lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_rwlock_destroy(&lock);
|
|
|
|
}
|
2017-07-25 03:01:13 +00:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_rwlock_write);
|
2014-12-04 05:36:24 +00:00
|
|
|
|
|
|
|
static void* IdleThread(void*) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-02-17 18:23:52 +00:00
|
|
|
static void BM_pthread_create(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) {
|
|
|
|
pthread_t thread;
|
2014-12-04 05:36:24 +00:00
|
|
|
pthread_create(&thread, NULL, IdleThread, NULL);
|
2016-02-17 18:23:52 +00:00
|
|
|
state.PauseTiming();
|
2014-12-04 05:36:24 +00:00
|
|
|
pthread_join(thread, NULL);
|
2016-02-17 18:23:52 +00:00
|
|
|
state.ResumeTiming();
|
2014-12-04 05:36:24 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 03:01:13 +00:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_create);
|
2014-12-04 05:36:24 +00:00
|
|
|
|
2017-10-24 21:00:09 +00:00
|
|
|
static void* RunThread(void*) {
|
2014-12-04 05:36:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-02-17 18:23:52 +00:00
|
|
|
static void BM_pthread_create_and_run(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) {
|
|
|
|
pthread_t thread;
|
|
|
|
pthread_create(&thread, NULL, RunThread, &state);
|
2014-12-04 05:36:24 +00:00
|
|
|
pthread_join(thread, NULL);
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 03:01:13 +00:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_create_and_run);
|
2014-12-04 05:36:24 +00:00
|
|
|
|
2017-10-24 21:00:09 +00:00
|
|
|
static void* ExitThread(void*) {
|
2014-12-04 05:36:24 +00:00
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
|
2016-02-17 18:23:52 +00:00
|
|
|
static void BM_pthread_exit_and_join(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) {
|
|
|
|
pthread_t thread;
|
2017-10-24 21:00:09 +00:00
|
|
|
pthread_create(&thread, NULL, ExitThread, nullptr);
|
2014-12-04 05:36:24 +00:00
|
|
|
pthread_join(thread, NULL);
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 03:01:13 +00:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_exit_and_join);
|
2014-12-04 05:36:24 +00:00
|
|
|
|
2016-02-17 18:23:52 +00:00
|
|
|
static void BM_pthread_key_create(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) {
|
|
|
|
pthread_key_t key;
|
2014-12-04 05:36:24 +00:00
|
|
|
pthread_key_create(&key, NULL);
|
2016-02-17 18:23:52 +00:00
|
|
|
|
|
|
|
state.PauseTiming();
|
2014-12-04 05:36:24 +00:00
|
|
|
pthread_key_delete(key);
|
2016-02-17 18:23:52 +00:00
|
|
|
state.ResumeTiming();
|
2014-12-04 05:36:24 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 03:01:13 +00:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_key_create);
|
2014-12-04 05:36:24 +00:00
|
|
|
|
2016-02-17 18:23:52 +00:00
|
|
|
static void BM_pthread_key_delete(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) {
|
|
|
|
state.PauseTiming();
|
|
|
|
pthread_key_t key;
|
2014-12-04 05:36:24 +00:00
|
|
|
pthread_key_create(&key, NULL);
|
2016-02-17 18:23:52 +00:00
|
|
|
state.ResumeTiming();
|
|
|
|
|
2014-12-04 05:36:24 +00:00
|
|
|
pthread_key_delete(key);
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 03:01:13 +00:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_key_delete);
|