Home | History | Annotate | Download | only in benchmarks
      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 <pthread.h>
     18 #include <semaphore.h>
     19 #include <stdatomic.h>
     20 #include <stdio.h>
     21 #include <stdlib.h>
     22 
     23 #include <benchmark/benchmark.h>
     24 
     25 static void BM_semaphore_sem_getvalue(benchmark::State& state) {
     26   sem_t semaphore;
     27   sem_init(&semaphore, 1, 1);
     28 
     29   while (state.KeepRunning()) {
     30     int dummy;
     31     sem_getvalue(&semaphore, &dummy);
     32   }
     33 }
     34 BENCHMARK(BM_semaphore_sem_getvalue);
     35 
     36 static void BM_semaphore_sem_wait_sem_post(benchmark::State& state) {
     37   sem_t semaphore;
     38   sem_init(&semaphore, 1, 1);
     39 
     40   while (state.KeepRunning()) {
     41     sem_wait(&semaphore);
     42     sem_post(&semaphore);
     43   }
     44 }
     45 BENCHMARK(BM_semaphore_sem_wait_sem_post);
     46 
     47 // This test reports the overhead of the underlying futex wake syscall on
     48 // the producer. It does not report the overhead from issuing the wake to the
     49 // point where the posted consumer thread wakes up. It suffers from
     50 // clock_gettime syscall overhead. Lock the CPU speed for consistent results
     51 // as we may not reach >50% cpu utilization.
     52 //
     53 // We will run a background thread that catches the sem_post wakeup and
     54 // loops immediately returning back to sleep in sem_wait for the next one. This
     55 // thread is run with policy SCHED_OTHER (normal policy), a middle policy.
     56 //
     57 // The primary thread will run at SCHED_IDLE (lowest priority policy) when
     58 // monitoring the background thread to detect when it hits sem_wait sleep. It
     59 // will do so with no clock running. Once we are ready, we will switch to
     60 // SCHED_FIFO (highest priority policy) to time the act of running sem_post
     61 // with the benchmark clock running. This ensures nothing else in the system
     62 // can preempt our timed activity, including the background thread. We are
     63 // also protected with the scheduling policy of letting a process hit a
     64 // resource limit rather than get hit with a context switch.
     65 //
     66 // The background thread will start executing either on another CPU, or
     67 // after we back down from SCHED_FIFO, but certainly not in the context of
     68 // the timing of the sem_post.
     69 
     70 static atomic_int BM_semaphore_sem_post_running;
     71 
     72 static void* BM_semaphore_sem_post_start_thread(void* arg) {
     73   sem_t* semaphore = reinterpret_cast<sem_t*>(arg);
     74   while ((BM_semaphore_sem_post_running > 0) && !sem_wait(semaphore)) {
     75   }
     76   BM_semaphore_sem_post_running = -1;
     77   return NULL;
     78 }
     79 
     80 class SemaphoreFixture : public benchmark::Fixture {
     81  public:
     82   void SetUp(const benchmark::State&) {
     83     sem_init(&semaphore, 0, 0);
     84 
     85     pthread_attr_t attr;
     86     pthread_attr_init(&attr);
     87 
     88     memset(&param, 0, sizeof(param));
     89     pthread_attr_setschedparam(&attr, &param);
     90     pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
     91     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
     92     pthread_t pthread;
     93     pthread_create(&pthread, &attr, BM_semaphore_sem_post_start_thread, &semaphore);
     94     pthread_attr_destroy(&attr);
     95 
     96     sched_setscheduler(0, SCHED_IDLE, &param);
     97 
     98     BM_semaphore_sem_post_running = 1;
     99     setup = true;
    100   }
    101 
    102   ~SemaphoreFixture() {
    103     if (setup) {
    104       // Only do this if the test was actually run.
    105       sched_setscheduler(0, SCHED_OTHER, &param);
    106 
    107       if (BM_semaphore_sem_post_running > 0) {
    108         BM_semaphore_sem_post_running = 0;
    109       }
    110       do {
    111         sem_post(&semaphore);
    112         sched_yield();
    113       } while (BM_semaphore_sem_post_running != -1);
    114     }
    115   }
    116 
    117   sem_t semaphore;
    118   sched_param param;
    119   bool setup = false;
    120 };
    121 
    122 BENCHMARK_F(SemaphoreFixture, semaphore_sem_post)(benchmark::State& state) {
    123   while (state.KeepRunning()) {
    124     state.PauseTiming();
    125 
    126     int trys = 3, dummy = 0;
    127     do {
    128       if (BM_semaphore_sem_post_running < 0) {
    129         sched_setscheduler(0, SCHED_OTHER, &param);
    130         fprintf(stderr, "BM_semaphore_sem_post: start_thread died unexpectedly\n");
    131         abort();
    132       }
    133       sched_yield();
    134       sem_getvalue(&semaphore, &dummy);
    135       if (dummy < 0) {  // POSIX.1-2001 possibility 1
    136         break;
    137       }
    138       if (dummy == 0) { // POSIX.1-2001 possibility 2
    139         --trys;
    140       }
    141     } while (trys);
    142 
    143     param.sched_priority = 1;
    144     sched_setscheduler(0, SCHED_FIFO, &param);
    145 
    146     state.ResumeTiming();
    147     sem_post(&semaphore);
    148 
    149     param.sched_priority = 0;
    150     sched_setscheduler(0, SCHED_IDLE, &param);
    151   }
    152 }
    153