2014-03-11 19:19:06 +01: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 19:23:52 +01:00
|
|
|
#include <benchmark/benchmark.h>
|
2017-07-25 05:01:13 +02:00
|
|
|
#include "util.h"
|
2015-02-18 04:58:53 +01:00
|
|
|
|
2014-06-11 05:47:49 +02:00
|
|
|
// Stop GCC optimizing out our pure function.
|
|
|
|
/* Must not be static! */ pthread_t (*pthread_self_fp)() = pthread_self;
|
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_pthread_self(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) {
|
2014-06-11 05:47:49 +02:00
|
|
|
pthread_self_fp();
|
2014-03-11 19:19:06 +01:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_self);
|
2014-03-11 19:19:06 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_pthread_getspecific(benchmark::State& state) {
|
2014-03-11 19:19:06 +01:00
|
|
|
pthread_key_t key;
|
2018-08-03 02:31:13 +02:00
|
|
|
pthread_key_create(&key, nullptr);
|
2014-03-11 19:19:06 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
while (state.KeepRunning()) {
|
2014-03-11 19:19:06 +01:00
|
|
|
pthread_getspecific(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_key_delete(key);
|
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_getspecific);
|
2014-03-11 19:19:06 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_pthread_setspecific(benchmark::State& state) {
|
2014-12-04 06:36:24 +01:00
|
|
|
pthread_key_t key;
|
2018-08-03 02:31:13 +02:00
|
|
|
pthread_key_create(&key, nullptr);
|
2014-12-04 06:36:24 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
while (state.KeepRunning()) {
|
2018-08-03 02:31:13 +02:00
|
|
|
pthread_setspecific(key, nullptr);
|
2014-12-04 06:36:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pthread_key_delete(key);
|
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_setspecific);
|
2014-12-04 06:36:24 +01:00
|
|
|
|
2020-07-22 01:11:30 +02:00
|
|
|
static void NoOpPthreadOnceInitFunction() {}
|
2014-03-11 19:19:06 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_pthread_once(benchmark::State& state) {
|
2017-06-28 01:23:45 +02:00
|
|
|
static pthread_once_t once = PTHREAD_ONCE_INIT;
|
2020-07-22 01:11:30 +02:00
|
|
|
pthread_once(&once, NoOpPthreadOnceInitFunction);
|
2014-03-11 19:19:06 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
while (state.KeepRunning()) {
|
2020-07-22 01:11:30 +02:00
|
|
|
pthread_once(&once, NoOpPthreadOnceInitFunction);
|
2014-03-11 19:19:06 +01:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_once);
|
2014-03-11 19:19:06 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_pthread_mutex_lock(benchmark::State& state) {
|
2014-03-11 19:19:06 +01:00
|
|
|
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
while (state.KeepRunning()) {
|
2014-03-11 19:19:06 +01:00
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_mutex_lock);
|
2014-03-11 19:19:06 +01:00
|
|
|
|
2021-12-15 23:26:43 +01:00
|
|
|
#if !defined(ANDROID_HOST_MUSL)
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_pthread_mutex_lock_ERRORCHECK(benchmark::State& state) {
|
2014-12-02 01:43:51 +01:00
|
|
|
pthread_mutex_t mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
|
2014-03-11 19:19:06 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
while (state.KeepRunning()) {
|
2014-03-11 19:19:06 +01:00
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_mutex_lock_ERRORCHECK);
|
2021-12-15 23:26:43 +01:00
|
|
|
#endif
|
2014-03-11 19:19:06 +01:00
|
|
|
|
2021-12-15 23:26:43 +01:00
|
|
|
#if !defined(ANDROID_HOST_MUSL)
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_pthread_mutex_lock_RECURSIVE(benchmark::State& state) {
|
2014-12-02 01:43:51 +01:00
|
|
|
pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
|
2014-03-11 19:19:06 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
while (state.KeepRunning()) {
|
2014-03-11 19:19:06 +01:00
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_mutex_lock_RECURSIVE);
|
2021-12-15 23:26:43 +01:00
|
|
|
#endif
|
2014-09-16 19:01:44 +02:00
|
|
|
|
2018-01-23 21:56:18 +01:00
|
|
|
namespace {
|
|
|
|
struct PIMutex {
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
|
2019-01-02 19:59:48 +01:00
|
|
|
explicit PIMutex(int type) {
|
2018-01-23 21:56:18 +01:00
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
pthread_mutexattr_init(&attr);
|
|
|
|
pthread_mutexattr_settype(&attr, type);
|
|
|
|
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
|
|
|
|
pthread_mutex_init(&mutex, &attr);
|
|
|
|
pthread_mutexattr_destroy(&attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
~PIMutex() {
|
|
|
|
pthread_mutex_destroy(&mutex);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static void BM_pthread_mutex_lock_PI(benchmark::State& state) {
|
|
|
|
PIMutex m(PTHREAD_MUTEX_NORMAL);
|
|
|
|
|
|
|
|
while (state.KeepRunning()) {
|
|
|
|
pthread_mutex_lock(&m.mutex);
|
|
|
|
pthread_mutex_unlock(&m.mutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BIONIC_BENCHMARK(BM_pthread_mutex_lock_PI);
|
|
|
|
|
|
|
|
static void BM_pthread_mutex_lock_ERRORCHECK_PI(benchmark::State& state) {
|
|
|
|
PIMutex m(PTHREAD_MUTEX_ERRORCHECK);
|
|
|
|
|
|
|
|
while (state.KeepRunning()) {
|
|
|
|
pthread_mutex_lock(&m.mutex);
|
|
|
|
pthread_mutex_unlock(&m.mutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BIONIC_BENCHMARK(BM_pthread_mutex_lock_ERRORCHECK_PI);
|
|
|
|
|
|
|
|
static void BM_pthread_mutex_lock_RECURSIVE_PI(benchmark::State& state) {
|
|
|
|
PIMutex m(PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
|
|
|
|
while (state.KeepRunning()) {
|
|
|
|
pthread_mutex_lock(&m.mutex);
|
|
|
|
pthread_mutex_unlock(&m.mutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BIONIC_BENCHMARK(BM_pthread_mutex_lock_RECURSIVE_PI);
|
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_pthread_rwlock_read(benchmark::State& state) {
|
2014-09-16 19:01:44 +02:00
|
|
|
pthread_rwlock_t lock;
|
2018-08-03 02:31:13 +02:00
|
|
|
pthread_rwlock_init(&lock, nullptr);
|
2014-09-16 19:01:44 +02:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
while (state.KeepRunning()) {
|
2014-09-16 19:01:44 +02:00
|
|
|
pthread_rwlock_rdlock(&lock);
|
|
|
|
pthread_rwlock_unlock(&lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_rwlock_destroy(&lock);
|
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_rwlock_read);
|
2014-09-16 19:01:44 +02:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_pthread_rwlock_write(benchmark::State& state) {
|
2014-09-16 19:01:44 +02:00
|
|
|
pthread_rwlock_t lock;
|
2018-08-03 02:31:13 +02:00
|
|
|
pthread_rwlock_init(&lock, nullptr);
|
2014-09-16 19:01:44 +02:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
while (state.KeepRunning()) {
|
2014-09-16 19:01:44 +02:00
|
|
|
pthread_rwlock_wrlock(&lock);
|
|
|
|
pthread_rwlock_unlock(&lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_rwlock_destroy(&lock);
|
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_rwlock_write);
|
2014-12-04 06:36:24 +01:00
|
|
|
|
|
|
|
static void* IdleThread(void*) {
|
2018-08-03 02:31:13 +02:00
|
|
|
return nullptr;
|
2014-12-04 06:36:24 +01:00
|
|
|
}
|
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_pthread_create(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) {
|
|
|
|
pthread_t thread;
|
2018-08-03 02:31:13 +02:00
|
|
|
pthread_create(&thread, nullptr, IdleThread, nullptr);
|
2016-02-17 19:23:52 +01:00
|
|
|
state.PauseTiming();
|
2018-08-03 02:31:13 +02:00
|
|
|
pthread_join(thread, nullptr);
|
2016-02-17 19:23:52 +01:00
|
|
|
state.ResumeTiming();
|
2014-12-04 06:36:24 +01:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_create);
|
2014-12-04 06:36:24 +01:00
|
|
|
|
2017-10-24 23:00:09 +02:00
|
|
|
static void* RunThread(void*) {
|
2018-08-03 02:31:13 +02:00
|
|
|
return nullptr;
|
2014-12-04 06:36:24 +01:00
|
|
|
}
|
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_pthread_create_and_run(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) {
|
|
|
|
pthread_t thread;
|
2018-08-03 02:31:13 +02:00
|
|
|
pthread_create(&thread, nullptr, RunThread, &state);
|
|
|
|
pthread_join(thread, nullptr);
|
2014-12-04 06:36:24 +01:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_create_and_run);
|
2014-12-04 06:36:24 +01:00
|
|
|
|
2017-10-24 23:00:09 +02:00
|
|
|
static void* ExitThread(void*) {
|
2018-08-03 02:31:13 +02:00
|
|
|
pthread_exit(nullptr);
|
2014-12-04 06:36:24 +01:00
|
|
|
}
|
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_pthread_exit_and_join(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) {
|
|
|
|
pthread_t thread;
|
2018-08-03 02:31:13 +02:00
|
|
|
pthread_create(&thread, nullptr, ExitThread, nullptr);
|
|
|
|
pthread_join(thread, nullptr);
|
2014-12-04 06:36:24 +01:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_exit_and_join);
|
2014-12-04 06:36:24 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_pthread_key_create(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) {
|
|
|
|
pthread_key_t key;
|
2018-08-03 02:31:13 +02:00
|
|
|
pthread_key_create(&key, nullptr);
|
2016-02-17 19:23:52 +01:00
|
|
|
|
|
|
|
state.PauseTiming();
|
2014-12-04 06:36:24 +01:00
|
|
|
pthread_key_delete(key);
|
2016-02-17 19:23:52 +01:00
|
|
|
state.ResumeTiming();
|
2014-12-04 06:36:24 +01:00
|
|
|
}
|
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_key_create);
|
2014-12-04 06:36:24 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_pthread_key_delete(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) {
|
|
|
|
state.PauseTiming();
|
|
|
|
pthread_key_t key;
|
2018-08-03 02:31:13 +02:00
|
|
|
pthread_key_create(&key, nullptr);
|
2016-02-17 19:23:52 +01:00
|
|
|
state.ResumeTiming();
|
|
|
|
|
2014-12-04 06:36:24 +01:00
|
|
|
pthread_key_delete(key);
|
|
|
|
}
|
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_pthread_key_delete);
|