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.
|
|
|
|
*/
|
|
|
|
|
2015-02-09 17:18:10 +01:00
|
|
|
#include <pthread.h>
|
2014-03-11 19:19:06 +01:00
|
|
|
#include <semaphore.h>
|
2015-02-09 17:18:10 +01:00
|
|
|
#include <stdatomic.h>
|
|
|
|
#include <stdio.h>
|
2016-02-17 19:23:52 +01:00
|
|
|
#include <stdlib.h>
|
2014-03-11 19:19:06 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
#include <benchmark/benchmark.h>
|
2015-02-18 04:58:53 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_semaphore_sem_getvalue(benchmark::State& state) {
|
2014-03-11 19:19:06 +01:00
|
|
|
sem_t semaphore;
|
|
|
|
sem_init(&semaphore, 1, 1);
|
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
while (state.KeepRunning()) {
|
2014-03-11 19:19:06 +01:00
|
|
|
int dummy;
|
|
|
|
sem_getvalue(&semaphore, &dummy);
|
|
|
|
}
|
|
|
|
}
|
2016-02-17 19:23:52 +01:00
|
|
|
BENCHMARK(BM_semaphore_sem_getvalue);
|
2014-03-11 19:19:06 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_semaphore_sem_wait_sem_post(benchmark::State& state) {
|
2014-03-11 19:19:06 +01:00
|
|
|
sem_t semaphore;
|
|
|
|
sem_init(&semaphore, 1, 1);
|
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
while (state.KeepRunning()) {
|
2014-03-11 19:19:06 +01:00
|
|
|
sem_wait(&semaphore);
|
|
|
|
sem_post(&semaphore);
|
|
|
|
}
|
|
|
|
}
|
2016-02-17 19:23:52 +01:00
|
|
|
BENCHMARK(BM_semaphore_sem_wait_sem_post);
|
|
|
|
|
|
|
|
// This test reports the overhead of the underlying futex wake syscall on
|
|
|
|
// the producer. It does not report the overhead from issuing the wake to the
|
|
|
|
// point where the posted consumer thread wakes up. It suffers from
|
|
|
|
// clock_gettime syscall overhead. Lock the CPU speed for consistent results
|
|
|
|
// as we may not reach >50% cpu utilization.
|
|
|
|
//
|
|
|
|
// We will run a background thread that catches the sem_post wakeup and
|
|
|
|
// loops immediately returning back to sleep in sem_wait for the next one. This
|
|
|
|
// thread is run with policy SCHED_OTHER (normal policy), a middle policy.
|
|
|
|
//
|
|
|
|
// The primary thread will run at SCHED_IDLE (lowest priority policy) when
|
|
|
|
// monitoring the background thread to detect when it hits sem_wait sleep. It
|
|
|
|
// will do so with no clock running. Once we are ready, we will switch to
|
|
|
|
// SCHED_FIFO (highest priority policy) to time the act of running sem_post
|
|
|
|
// with the benchmark clock running. This ensures nothing else in the system
|
|
|
|
// can preempt our timed activity, including the background thread. We are
|
|
|
|
// also protected with the scheduling policy of letting a process hit a
|
|
|
|
// resource limit rather than get hit with a context switch.
|
|
|
|
//
|
|
|
|
// The background thread will start executing either on another CPU, or
|
|
|
|
// after we back down from SCHED_FIFO, but certainly not in the context of
|
|
|
|
// the timing of the sem_post.
|
2015-02-09 17:18:10 +01:00
|
|
|
|
|
|
|
static atomic_int BM_semaphore_sem_post_running;
|
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
static void* BM_semaphore_sem_post_start_thread(void* arg) {
|
|
|
|
sem_t* semaphore = reinterpret_cast<sem_t*>(arg);
|
|
|
|
while ((BM_semaphore_sem_post_running > 0) && !sem_wait(semaphore)) {
|
|
|
|
}
|
|
|
|
BM_semaphore_sem_post_running = -1;
|
|
|
|
return NULL;
|
2015-02-09 17:18:10 +01:00
|
|
|
}
|
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
class SemaphoreFixture : public benchmark::Fixture {
|
|
|
|
public:
|
|
|
|
void SetUp(const benchmark::State&) {
|
|
|
|
sem_init(&semaphore, 0, 0);
|
|
|
|
|
|
|
|
pthread_attr_t attr;
|
|
|
|
pthread_attr_init(&attr);
|
|
|
|
|
|
|
|
memset(¶m, 0, sizeof(param));
|
|
|
|
pthread_attr_setschedparam(&attr, ¶m);
|
|
|
|
pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
|
|
|
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
|
|
|
pthread_t pthread;
|
|
|
|
pthread_create(&pthread, &attr, BM_semaphore_sem_post_start_thread, &semaphore);
|
|
|
|
pthread_attr_destroy(&attr);
|
|
|
|
|
|
|
|
sched_setscheduler(0, SCHED_IDLE, ¶m);
|
|
|
|
|
|
|
|
BM_semaphore_sem_post_running = 1;
|
2016-06-06 23:13:17 +02:00
|
|
|
setup = true;
|
2016-02-17 19:23:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
~SemaphoreFixture() {
|
2016-06-06 23:13:17 +02:00
|
|
|
if (setup) {
|
|
|
|
// Only do this if the test was actually run.
|
|
|
|
sched_setscheduler(0, SCHED_OTHER, ¶m);
|
2016-02-17 19:23:52 +01:00
|
|
|
|
2016-06-06 23:13:17 +02:00
|
|
|
if (BM_semaphore_sem_post_running > 0) {
|
|
|
|
BM_semaphore_sem_post_running = 0;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
sem_post(&semaphore);
|
|
|
|
sched_yield();
|
|
|
|
} while (BM_semaphore_sem_post_running != -1);
|
2016-02-17 19:23:52 +01:00
|
|
|
}
|
|
|
|
}
|
2015-02-09 17:18:10 +01:00
|
|
|
|
|
|
|
sem_t semaphore;
|
2016-02-17 19:23:52 +01:00
|
|
|
sched_param param;
|
2016-06-06 23:13:17 +02:00
|
|
|
bool setup = false;
|
2016-02-17 19:23:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
BENCHMARK_F(SemaphoreFixture, semaphore_sem_post)(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) {
|
|
|
|
state.PauseTiming();
|
|
|
|
|
2015-02-09 17:18:10 +01:00
|
|
|
int trys = 3, dummy = 0;
|
|
|
|
do {
|
|
|
|
if (BM_semaphore_sem_post_running < 0) {
|
2016-02-17 19:23:52 +01:00
|
|
|
sched_setscheduler(0, SCHED_OTHER, ¶m);
|
2015-02-09 17:18:10 +01:00
|
|
|
fprintf(stderr, "BM_semaphore_sem_post: start_thread died unexpectedly\n");
|
2016-02-17 19:23:52 +01:00
|
|
|
abort();
|
2015-02-09 17:18:10 +01:00
|
|
|
}
|
|
|
|
sched_yield();
|
|
|
|
sem_getvalue(&semaphore, &dummy);
|
|
|
|
if (dummy < 0) { // POSIX.1-2001 possibility 1
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (dummy == 0) { // POSIX.1-2001 possibility 2
|
|
|
|
--trys;
|
|
|
|
}
|
|
|
|
} while (trys);
|
2016-02-17 19:23:52 +01:00
|
|
|
|
2015-02-09 17:18:10 +01:00
|
|
|
param.sched_priority = 1;
|
2016-02-17 19:23:52 +01:00
|
|
|
sched_setscheduler(0, SCHED_FIFO, ¶m);
|
|
|
|
|
|
|
|
state.ResumeTiming();
|
2015-02-09 17:18:10 +01:00
|
|
|
sem_post(&semaphore);
|
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
param.sched_priority = 0;
|
|
|
|
sched_setscheduler(0, SCHED_IDLE, ¶m);
|
2015-02-09 17:18:10 +01:00
|
|
|
}
|
|
|
|
}
|