linux/tools/perf/bench/futex-lock-pi.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2015 Davidlohr Bueso.
   3 */
   4
   5/* For the CLR_() macros */
   6#include <pthread.h>
   7
   8#include <signal.h>
   9#include "../util/stat.h"
  10#include <subcmd/parse-options.h>
  11#include <linux/compiler.h>
  12#include <linux/kernel.h>
  13#include <errno.h>
  14#include "bench.h"
  15#include "futex.h"
  16
  17#include <err.h>
  18#include <stdlib.h>
  19#include <sys/time.h>
  20
  21struct worker {
  22        int tid;
  23        u_int32_t *futex;
  24        pthread_t thread;
  25        unsigned long ops;
  26};
  27
  28static u_int32_t global_futex = 0;
  29static struct worker *worker;
  30static unsigned int nsecs = 10;
  31static bool silent = false, multi = false;
  32static bool done = false, fshared = false;
  33static unsigned int ncpus, nthreads = 0;
  34static int futex_flag = 0;
  35struct timeval start, end, runtime;
  36static pthread_mutex_t thread_lock;
  37static unsigned int threads_starting;
  38static struct stats throughput_stats;
  39static pthread_cond_t thread_parent, thread_worker;
  40
  41static const struct option options[] = {
  42        OPT_UINTEGER('t', "threads",  &nthreads, "Specify amount of threads"),
  43        OPT_UINTEGER('r', "runtime", &nsecs,     "Specify runtime (in seconds)"),
  44        OPT_BOOLEAN( 'M', "multi",   &multi,     "Use multiple futexes"),
  45        OPT_BOOLEAN( 's', "silent",  &silent,    "Silent mode: do not display data/details"),
  46        OPT_BOOLEAN( 'S', "shared",  &fshared,   "Use shared futexes instead of private ones"),
  47        OPT_END()
  48};
  49
  50static const char * const bench_futex_lock_pi_usage[] = {
  51        "perf bench futex lock-pi <options>",
  52        NULL
  53};
  54
  55static void print_summary(void)
  56{
  57        unsigned long avg = avg_stats(&throughput_stats);
  58        double stddev = stddev_stats(&throughput_stats);
  59
  60        printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
  61               !silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
  62               (int) runtime.tv_sec);
  63}
  64
  65static void toggle_done(int sig __maybe_unused,
  66                        siginfo_t *info __maybe_unused,
  67                        void *uc __maybe_unused)
  68{
  69        /* inform all threads that we're done for the day */
  70        done = true;
  71        gettimeofday(&end, NULL);
  72        timersub(&end, &start, &runtime);
  73}
  74
  75static void *workerfn(void *arg)
  76{
  77        struct worker *w = (struct worker *) arg;
  78        unsigned long ops = w->ops;
  79
  80        pthread_mutex_lock(&thread_lock);
  81        threads_starting--;
  82        if (!threads_starting)
  83                pthread_cond_signal(&thread_parent);
  84        pthread_cond_wait(&thread_worker, &thread_lock);
  85        pthread_mutex_unlock(&thread_lock);
  86
  87        do {
  88                int ret;
  89        again:
  90                ret = futex_lock_pi(w->futex, NULL, futex_flag);
  91
  92                if (ret) { /* handle lock acquisition */
  93                        if (!silent)
  94                                warn("thread %d: Could not lock pi-lock for %p (%d)",
  95                                     w->tid, w->futex, ret);
  96                        if (done)
  97                                break;
  98
  99                        goto again;
 100                }
 101
 102                usleep(1);
 103                ret = futex_unlock_pi(w->futex, futex_flag);
 104                if (ret && !silent)
 105                        warn("thread %d: Could not unlock pi-lock for %p (%d)",
 106                             w->tid, w->futex, ret);
 107                ops++; /* account for thread's share of work */
 108        }  while (!done);
 109
 110        w->ops = ops;
 111        return NULL;
 112}
 113
 114static void create_threads(struct worker *w, pthread_attr_t thread_attr)
 115{
 116        cpu_set_t cpu;
 117        unsigned int i;
 118
 119        threads_starting = nthreads;
 120
 121        for (i = 0; i < nthreads; i++) {
 122                worker[i].tid = i;
 123
 124                if (multi) {
 125                        worker[i].futex = calloc(1, sizeof(u_int32_t));
 126                        if (!worker[i].futex)
 127                                err(EXIT_FAILURE, "calloc");
 128                } else
 129                        worker[i].futex = &global_futex;
 130
 131                CPU_ZERO(&cpu);
 132                CPU_SET(i % ncpus, &cpu);
 133
 134                if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
 135                        err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
 136
 137                if (pthread_create(&w[i].thread, &thread_attr, workerfn, &worker[i]))
 138                        err(EXIT_FAILURE, "pthread_create");
 139        }
 140}
 141
 142int bench_futex_lock_pi(int argc, const char **argv,
 143                        const char *prefix __maybe_unused)
 144{
 145        int ret = 0;
 146        unsigned int i;
 147        struct sigaction act;
 148        pthread_attr_t thread_attr;
 149
 150        argc = parse_options(argc, argv, options, bench_futex_lock_pi_usage, 0);
 151        if (argc)
 152                goto err;
 153
 154        ncpus = sysconf(_SC_NPROCESSORS_ONLN);
 155        nsecs = futexbench_sanitize_numeric(nsecs);
 156
 157        sigfillset(&act.sa_mask);
 158        act.sa_sigaction = toggle_done;
 159        sigaction(SIGINT, &act, NULL);
 160
 161        if (!nthreads)
 162                nthreads = ncpus;
 163        else
 164                nthreads = futexbench_sanitize_numeric(nthreads);
 165
 166        worker = calloc(nthreads, sizeof(*worker));
 167        if (!worker)
 168                err(EXIT_FAILURE, "calloc");
 169
 170        if (!fshared)
 171                futex_flag = FUTEX_PRIVATE_FLAG;
 172
 173        printf("Run summary [PID %d]: %d threads doing pi lock/unlock pairing for %d secs.\n\n",
 174               getpid(), nthreads, nsecs);
 175
 176        init_stats(&throughput_stats);
 177        pthread_mutex_init(&thread_lock, NULL);
 178        pthread_cond_init(&thread_parent, NULL);
 179        pthread_cond_init(&thread_worker, NULL);
 180
 181        threads_starting = nthreads;
 182        pthread_attr_init(&thread_attr);
 183        gettimeofday(&start, NULL);
 184
 185        create_threads(worker, thread_attr);
 186        pthread_attr_destroy(&thread_attr);
 187
 188        pthread_mutex_lock(&thread_lock);
 189        while (threads_starting)
 190                pthread_cond_wait(&thread_parent, &thread_lock);
 191        pthread_cond_broadcast(&thread_worker);
 192        pthread_mutex_unlock(&thread_lock);
 193
 194        sleep(nsecs);
 195        toggle_done(0, NULL, NULL);
 196
 197        for (i = 0; i < nthreads; i++) {
 198                ret = pthread_join(worker[i].thread, NULL);
 199                if (ret)
 200                        err(EXIT_FAILURE, "pthread_join");
 201        }
 202
 203        /* cleanup & report results */
 204        pthread_cond_destroy(&thread_parent);
 205        pthread_cond_destroy(&thread_worker);
 206        pthread_mutex_destroy(&thread_lock);
 207
 208        for (i = 0; i < nthreads; i++) {
 209                unsigned long t = worker[i].ops/runtime.tv_sec;
 210
 211                update_stats(&throughput_stats, t);
 212                if (!silent)
 213                        printf("[thread %3d] futex: %p [ %ld ops/sec ]\n",
 214                               worker[i].tid, worker[i].futex, t);
 215
 216                if (multi)
 217                        free(worker[i].futex);
 218        }
 219
 220        print_summary();
 221
 222        free(worker);
 223        return ret;
 224err:
 225        usage_with_options(bench_futex_lock_pi_usage, options);
 226        exit(EXIT_FAILURE);
 227}
 228