linux/tools/perf/bench/futex-requeue.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2013  Davidlohr Bueso <davidlohr@hp.com>
   3 *
   4 * futex-requeue: Block a bunch of threads on futex1 and requeue them
   5 *                on futex2, N at a time.
   6 *
   7 * This program is particularly useful to measure the latency of nthread
   8 * requeues without waking up any tasks -- thus mimicking a regular futex_wait.
   9 */
  10
  11/* For the CLR_() macros */
  12#include <pthread.h>
  13
  14#include <signal.h>
  15#include "../util/stat.h"
  16#include <subcmd/parse-options.h>
  17#include <linux/compiler.h>
  18#include <linux/kernel.h>
  19#include <linux/time64.h>
  20#include <errno.h>
  21#include "bench.h"
  22#include "futex.h"
  23
  24#include <err.h>
  25#include <stdlib.h>
  26#include <sys/time.h>
  27
  28static u_int32_t futex1 = 0, futex2 = 0;
  29
  30/*
  31 * How many tasks to requeue at a time.
  32 * Default to 1 in order to make the kernel work more.
  33 */
  34static unsigned int nrequeue = 1;
  35
  36static pthread_t *worker;
  37static bool done = false, silent = false, fshared = false;
  38static pthread_mutex_t thread_lock;
  39static pthread_cond_t thread_parent, thread_worker;
  40static struct stats requeuetime_stats, requeued_stats;
  41static unsigned int ncpus, threads_starting, nthreads = 0;
  42static int futex_flag = 0;
  43
  44static const struct option options[] = {
  45        OPT_UINTEGER('t', "threads",  &nthreads, "Specify amount of threads"),
  46        OPT_UINTEGER('q', "nrequeue", &nrequeue, "Specify amount of threads to requeue at once"),
  47        OPT_BOOLEAN( 's', "silent",   &silent,   "Silent mode: do not display data/details"),
  48        OPT_BOOLEAN( 'S', "shared",   &fshared,  "Use shared futexes instead of private ones"),
  49        OPT_END()
  50};
  51
  52static const char * const bench_futex_requeue_usage[] = {
  53        "perf bench futex requeue <options>",
  54        NULL
  55};
  56
  57static void print_summary(void)
  58{
  59        double requeuetime_avg = avg_stats(&requeuetime_stats);
  60        double requeuetime_stddev = stddev_stats(&requeuetime_stats);
  61        unsigned int requeued_avg = avg_stats(&requeued_stats);
  62
  63        printf("Requeued %d of %d threads in %.4f ms (+-%.2f%%)\n",
  64               requeued_avg,
  65               nthreads,
  66               requeuetime_avg / USEC_PER_MSEC,
  67               rel_stddev_stats(requeuetime_stddev, requeuetime_avg));
  68}
  69
  70static void *workerfn(void *arg __maybe_unused)
  71{
  72        pthread_mutex_lock(&thread_lock);
  73        threads_starting--;
  74        if (!threads_starting)
  75                pthread_cond_signal(&thread_parent);
  76        pthread_cond_wait(&thread_worker, &thread_lock);
  77        pthread_mutex_unlock(&thread_lock);
  78
  79        futex_wait(&futex1, 0, NULL, futex_flag);
  80        return NULL;
  81}
  82
  83static void block_threads(pthread_t *w,
  84                          pthread_attr_t thread_attr)
  85{
  86        cpu_set_t cpu;
  87        unsigned int i;
  88
  89        threads_starting = nthreads;
  90
  91        /* create and block all threads */
  92        for (i = 0; i < nthreads; i++) {
  93                CPU_ZERO(&cpu);
  94                CPU_SET(i % ncpus, &cpu);
  95
  96                if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
  97                        err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
  98
  99                if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
 100                        err(EXIT_FAILURE, "pthread_create");
 101        }
 102}
 103
 104static void toggle_done(int sig __maybe_unused,
 105                        siginfo_t *info __maybe_unused,
 106                        void *uc __maybe_unused)
 107{
 108        done = true;
 109}
 110
 111int bench_futex_requeue(int argc, const char **argv,
 112                        const char *prefix __maybe_unused)
 113{
 114        int ret = 0;
 115        unsigned int i, j;
 116        struct sigaction act;
 117        pthread_attr_t thread_attr;
 118
 119        argc = parse_options(argc, argv, options, bench_futex_requeue_usage, 0);
 120        if (argc)
 121                goto err;
 122
 123        ncpus = sysconf(_SC_NPROCESSORS_ONLN);
 124
 125        sigfillset(&act.sa_mask);
 126        act.sa_sigaction = toggle_done;
 127        sigaction(SIGINT, &act, NULL);
 128
 129        if (!nthreads)
 130                nthreads = ncpus;
 131        else
 132                nthreads = futexbench_sanitize_numeric(nthreads);
 133
 134        worker = calloc(nthreads, sizeof(*worker));
 135        if (!worker)
 136                err(EXIT_FAILURE, "calloc");
 137
 138        if (!fshared)
 139                futex_flag = FUTEX_PRIVATE_FLAG;
 140
 141        if (nrequeue > nthreads)
 142                nrequeue = nthreads;
 143
 144        printf("Run summary [PID %d]: Requeuing %d threads (from [%s] %p to %p), "
 145               "%d at a time.\n\n",  getpid(), nthreads,
 146               fshared ? "shared":"private", &futex1, &futex2, nrequeue);
 147
 148        init_stats(&requeued_stats);
 149        init_stats(&requeuetime_stats);
 150        pthread_attr_init(&thread_attr);
 151        pthread_mutex_init(&thread_lock, NULL);
 152        pthread_cond_init(&thread_parent, NULL);
 153        pthread_cond_init(&thread_worker, NULL);
 154
 155        for (j = 0; j < bench_repeat && !done; j++) {
 156                unsigned int nrequeued = 0;
 157                struct timeval start, end, runtime;
 158
 159                /* create, launch & block all threads */
 160                block_threads(worker, thread_attr);
 161
 162                /* make sure all threads are already blocked */
 163                pthread_mutex_lock(&thread_lock);
 164                while (threads_starting)
 165                        pthread_cond_wait(&thread_parent, &thread_lock);
 166                pthread_cond_broadcast(&thread_worker);
 167                pthread_mutex_unlock(&thread_lock);
 168
 169                usleep(100000);
 170
 171                /* Ok, all threads are patiently blocked, start requeueing */
 172                gettimeofday(&start, NULL);
 173                while (nrequeued < nthreads) {
 174                        /*
 175                         * Do not wakeup any tasks blocked on futex1, allowing
 176                         * us to really measure futex_wait functionality.
 177                         */
 178                        nrequeued += futex_cmp_requeue(&futex1, 0, &futex2, 0,
 179                                                       nrequeue, futex_flag);
 180                }
 181
 182                gettimeofday(&end, NULL);
 183                timersub(&end, &start, &runtime);
 184
 185                update_stats(&requeued_stats, nrequeued);
 186                update_stats(&requeuetime_stats, runtime.tv_usec);
 187
 188                if (!silent) {
 189                        printf("[Run %d]: Requeued %d of %d threads in %.4f ms\n",
 190                               j + 1, nrequeued, nthreads, runtime.tv_usec / (double)USEC_PER_MSEC);
 191                }
 192
 193                /* everybody should be blocked on futex2, wake'em up */
 194                nrequeued = futex_wake(&futex2, nrequeued, futex_flag);
 195                if (nthreads != nrequeued)
 196                        warnx("couldn't wakeup all tasks (%d/%d)", nrequeued, nthreads);
 197
 198                for (i = 0; i < nthreads; i++) {
 199                        ret = pthread_join(worker[i], NULL);
 200                        if (ret)
 201                                err(EXIT_FAILURE, "pthread_join");
 202                }
 203        }
 204
 205        /* cleanup & report results */
 206        pthread_cond_destroy(&thread_parent);
 207        pthread_cond_destroy(&thread_worker);
 208        pthread_mutex_destroy(&thread_lock);
 209        pthread_attr_destroy(&thread_attr);
 210
 211        print_summary();
 212
 213        free(worker);
 214        return ret;
 215err:
 216        usage_with_options(bench_futex_requeue_usage, options);
 217        exit(EXIT_FAILURE);
 218}
 219