Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2013  Davidlohr Bueso <davidlohr@hp.com>
0004  *
0005  * futex-hash: Stress the hell out of the Linux kernel futex uaddr hashing.
0006  *
0007  * This program is particularly useful for measuring the kernel's futex hash
0008  * table/function implementation. In order for it to make sense, use with as
0009  * many threads and futexes as possible.
0010  */
0011 
0012 /* For the CLR_() macros */
0013 #include <string.h>
0014 #include <pthread.h>
0015 
0016 #include <errno.h>
0017 #include <signal.h>
0018 #include <stdlib.h>
0019 #include <linux/compiler.h>
0020 #include <linux/kernel.h>
0021 #include <linux/zalloc.h>
0022 #include <sys/time.h>
0023 #include <sys/mman.h>
0024 #include <perf/cpumap.h>
0025 
0026 #include "../util/stat.h"
0027 #include <subcmd/parse-options.h>
0028 #include "bench.h"
0029 #include "futex.h"
0030 
0031 #include <err.h>
0032 
0033 static bool done = false;
0034 static int futex_flag = 0;
0035 
0036 struct timeval bench__start, bench__end, bench__runtime;
0037 static pthread_mutex_t thread_lock;
0038 static unsigned int threads_starting;
0039 static struct stats throughput_stats;
0040 static pthread_cond_t thread_parent, thread_worker;
0041 
0042 struct worker {
0043     int tid;
0044     u_int32_t *futex;
0045     pthread_t thread;
0046     unsigned long ops;
0047 };
0048 
0049 static struct bench_futex_parameters params = {
0050     .nfutexes = 1024,
0051     .runtime  = 10,
0052 };
0053 
0054 static const struct option options[] = {
0055     OPT_UINTEGER('t', "threads", &params.nthreads, "Specify amount of threads"),
0056     OPT_UINTEGER('r', "runtime", &params.runtime, "Specify runtime (in seconds)"),
0057     OPT_UINTEGER('f', "futexes", &params.nfutexes, "Specify amount of futexes per threads"),
0058     OPT_BOOLEAN( 's', "silent",  &params.silent, "Silent mode: do not display data/details"),
0059     OPT_BOOLEAN( 'S', "shared",  &params.fshared, "Use shared futexes instead of private ones"),
0060     OPT_BOOLEAN( 'm', "mlockall", &params.mlockall, "Lock all current and future memory"),
0061     OPT_END()
0062 };
0063 
0064 static const char * const bench_futex_hash_usage[] = {
0065     "perf bench futex hash <options>",
0066     NULL
0067 };
0068 
0069 static void *workerfn(void *arg)
0070 {
0071     int ret;
0072     struct worker *w = (struct worker *) arg;
0073     unsigned int i;
0074     unsigned long ops = w->ops; /* avoid cacheline bouncing */
0075 
0076     pthread_mutex_lock(&thread_lock);
0077     threads_starting--;
0078     if (!threads_starting)
0079         pthread_cond_signal(&thread_parent);
0080     pthread_cond_wait(&thread_worker, &thread_lock);
0081     pthread_mutex_unlock(&thread_lock);
0082 
0083     do {
0084         for (i = 0; i < params.nfutexes; i++, ops++) {
0085             /*
0086              * We want the futex calls to fail in order to stress
0087              * the hashing of uaddr and not measure other steps,
0088              * such as internal waitqueue handling, thus enlarging
0089              * the critical region protected by hb->lock.
0090              */
0091             ret = futex_wait(&w->futex[i], 1234, NULL, futex_flag);
0092             if (!params.silent &&
0093                 (!ret || errno != EAGAIN || errno != EWOULDBLOCK))
0094                 warn("Non-expected futex return call");
0095         }
0096     }  while (!done);
0097 
0098     w->ops = ops;
0099     return NULL;
0100 }
0101 
0102 static void toggle_done(int sig __maybe_unused,
0103             siginfo_t *info __maybe_unused,
0104             void *uc __maybe_unused)
0105 {
0106     /* inform all threads that we're done for the day */
0107     done = true;
0108     gettimeofday(&bench__end, NULL);
0109     timersub(&bench__end, &bench__start, &bench__runtime);
0110 }
0111 
0112 static void print_summary(void)
0113 {
0114     unsigned long avg = avg_stats(&throughput_stats);
0115     double stddev = stddev_stats(&throughput_stats);
0116 
0117     printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
0118            !params.silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
0119            (int)bench__runtime.tv_sec);
0120 }
0121 
0122 int bench_futex_hash(int argc, const char **argv)
0123 {
0124     int ret = 0;
0125     cpu_set_t *cpuset;
0126     struct sigaction act;
0127     unsigned int i;
0128     pthread_attr_t thread_attr;
0129     struct worker *worker = NULL;
0130     struct perf_cpu_map *cpu;
0131     int nrcpus;
0132     size_t size;
0133 
0134     argc = parse_options(argc, argv, options, bench_futex_hash_usage, 0);
0135     if (argc) {
0136         usage_with_options(bench_futex_hash_usage, options);
0137         exit(EXIT_FAILURE);
0138     }
0139 
0140     cpu = perf_cpu_map__new(NULL);
0141     if (!cpu)
0142         goto errmem;
0143 
0144     memset(&act, 0, sizeof(act));
0145     sigfillset(&act.sa_mask);
0146     act.sa_sigaction = toggle_done;
0147     sigaction(SIGINT, &act, NULL);
0148 
0149     if (params.mlockall) {
0150         if (mlockall(MCL_CURRENT | MCL_FUTURE))
0151             err(EXIT_FAILURE, "mlockall");
0152     }
0153 
0154     if (!params.nthreads) /* default to the number of CPUs */
0155         params.nthreads = perf_cpu_map__nr(cpu);
0156 
0157     worker = calloc(params.nthreads, sizeof(*worker));
0158     if (!worker)
0159         goto errmem;
0160 
0161     if (!params.fshared)
0162         futex_flag = FUTEX_PRIVATE_FLAG;
0163 
0164     printf("Run summary [PID %d]: %d threads, each operating on %d [%s] futexes for %d secs.\n\n",
0165            getpid(), params.nthreads, params.nfutexes, params.fshared ? "shared":"private", params.runtime);
0166 
0167     init_stats(&throughput_stats);
0168     pthread_mutex_init(&thread_lock, NULL);
0169     pthread_cond_init(&thread_parent, NULL);
0170     pthread_cond_init(&thread_worker, NULL);
0171 
0172     threads_starting = params.nthreads;
0173     pthread_attr_init(&thread_attr);
0174     gettimeofday(&bench__start, NULL);
0175 
0176     nrcpus = perf_cpu_map__nr(cpu);
0177     cpuset = CPU_ALLOC(nrcpus);
0178     BUG_ON(!cpuset);
0179     size = CPU_ALLOC_SIZE(nrcpus);
0180 
0181     for (i = 0; i < params.nthreads; i++) {
0182         worker[i].tid = i;
0183         worker[i].futex = calloc(params.nfutexes, sizeof(*worker[i].futex));
0184         if (!worker[i].futex)
0185             goto errmem;
0186 
0187         CPU_ZERO_S(size, cpuset);
0188 
0189         CPU_SET_S(perf_cpu_map__cpu(cpu, i % perf_cpu_map__nr(cpu)).cpu, size, cpuset);
0190         ret = pthread_attr_setaffinity_np(&thread_attr, size, cpuset);
0191         if (ret) {
0192             CPU_FREE(cpuset);
0193             err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
0194         }
0195         ret = pthread_create(&worker[i].thread, &thread_attr, workerfn,
0196                      (void *)(struct worker *) &worker[i]);
0197         if (ret) {
0198             CPU_FREE(cpuset);
0199             err(EXIT_FAILURE, "pthread_create");
0200         }
0201 
0202     }
0203     CPU_FREE(cpuset);
0204     pthread_attr_destroy(&thread_attr);
0205 
0206     pthread_mutex_lock(&thread_lock);
0207     while (threads_starting)
0208         pthread_cond_wait(&thread_parent, &thread_lock);
0209     pthread_cond_broadcast(&thread_worker);
0210     pthread_mutex_unlock(&thread_lock);
0211 
0212     sleep(params.runtime);
0213     toggle_done(0, NULL, NULL);
0214 
0215     for (i = 0; i < params.nthreads; i++) {
0216         ret = pthread_join(worker[i].thread, NULL);
0217         if (ret)
0218             err(EXIT_FAILURE, "pthread_join");
0219     }
0220 
0221     /* cleanup & report results */
0222     pthread_cond_destroy(&thread_parent);
0223     pthread_cond_destroy(&thread_worker);
0224     pthread_mutex_destroy(&thread_lock);
0225 
0226     for (i = 0; i < params.nthreads; i++) {
0227         unsigned long t = bench__runtime.tv_sec > 0 ?
0228             worker[i].ops / bench__runtime.tv_sec : 0;
0229         update_stats(&throughput_stats, t);
0230         if (!params.silent) {
0231             if (params.nfutexes == 1)
0232                 printf("[thread %2d] futex: %p [ %ld ops/sec ]\n",
0233                        worker[i].tid, &worker[i].futex[0], t);
0234             else
0235                 printf("[thread %2d] futexes: %p ... %p [ %ld ops/sec ]\n",
0236                        worker[i].tid, &worker[i].futex[0],
0237                        &worker[i].futex[params.nfutexes-1], t);
0238         }
0239 
0240         zfree(&worker[i].futex);
0241     }
0242 
0243     print_summary();
0244 
0245     free(worker);
0246     free(cpu);
0247     return ret;
0248 errmem:
0249     err(EXIT_FAILURE, "calloc");
0250 }