1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2013 Davidlohr Bueso <davidlohr@hp.com>
5 * futex-hash: Stress the hell out of the Linux kernel futex uaddr hashing.
7 * This program is particularly useful for measuring the kernel's futex hash
8 * table/function implementation. In order for it to make sense, use with as
9 * many threads and futexes as possible.
12 /* For the CLR_() macros */
19 #include <linux/compiler.h>
20 #include <linux/kernel.h>
23 #include "../util/stat.h"
24 #include <subcmd/parse-options.h>
31 static unsigned int nthreads = 0;
32 static unsigned int nsecs = 10;
33 /* amount of futexes per thread */
34 static unsigned int nfutexes = 1024;
35 static bool fshared = false, done = false, silent = false;
36 static int futex_flag = 0;
38 struct timeval bench__start, bench__end, bench__runtime;
39 static pthread_mutex_t thread_lock;
40 static unsigned int threads_starting;
41 static struct stats throughput_stats;
42 static pthread_cond_t thread_parent, thread_worker;
51 static const struct option options[] = {
52 OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
53 OPT_UINTEGER('r', "runtime", &nsecs, "Specify runtime (in seconds)"),
54 OPT_UINTEGER('f', "futexes", &nfutexes, "Specify amount of futexes per threads"),
55 OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
56 OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"),
60 static const char * const bench_futex_hash_usage[] = {
61 "perf bench futex hash <options>",
65 static void *workerfn(void *arg)
68 struct worker *w = (struct worker *) arg;
70 unsigned long ops = w->ops; /* avoid cacheline bouncing */
72 pthread_mutex_lock(&thread_lock);
74 if (!threads_starting)
75 pthread_cond_signal(&thread_parent);
76 pthread_cond_wait(&thread_worker, &thread_lock);
77 pthread_mutex_unlock(&thread_lock);
80 for (i = 0; i < nfutexes; i++, ops++) {
82 * We want the futex calls to fail in order to stress
83 * the hashing of uaddr and not measure other steps,
84 * such as internal waitqueue handling, thus enlarging
85 * the critical region protected by hb->lock.
87 ret = futex_wait(&w->futex[i], 1234, NULL, futex_flag);
89 (!ret || errno != EAGAIN || errno != EWOULDBLOCK))
90 warn("Non-expected futex return call");
98 static void toggle_done(int sig __maybe_unused,
99 siginfo_t *info __maybe_unused,
100 void *uc __maybe_unused)
102 /* inform all threads that we're done for the day */
104 gettimeofday(&bench__end, NULL);
105 timersub(&bench__end, &bench__start, &bench__runtime);
108 static void print_summary(void)
110 unsigned long avg = avg_stats(&throughput_stats);
111 double stddev = stddev_stats(&throughput_stats);
113 printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
114 !silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
115 (int)bench__runtime.tv_sec);
118 int bench_futex_hash(int argc, const char **argv)
122 struct sigaction act;
123 unsigned int i, ncpus;
124 pthread_attr_t thread_attr;
125 struct worker *worker = NULL;
127 argc = parse_options(argc, argv, options, bench_futex_hash_usage, 0);
129 usage_with_options(bench_futex_hash_usage, options);
133 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
135 sigfillset(&act.sa_mask);
136 act.sa_sigaction = toggle_done;
137 sigaction(SIGINT, &act, NULL);
139 if (!nthreads) /* default to the number of CPUs */
142 worker = calloc(nthreads, sizeof(*worker));
147 futex_flag = FUTEX_PRIVATE_FLAG;
149 printf("Run summary [PID %d]: %d threads, each operating on %d [%s] futexes for %d secs.\n\n",
150 getpid(), nthreads, nfutexes, fshared ? "shared":"private", nsecs);
152 init_stats(&throughput_stats);
153 pthread_mutex_init(&thread_lock, NULL);
154 pthread_cond_init(&thread_parent, NULL);
155 pthread_cond_init(&thread_worker, NULL);
157 threads_starting = nthreads;
158 pthread_attr_init(&thread_attr);
159 gettimeofday(&bench__start, NULL);
160 for (i = 0; i < nthreads; i++) {
162 worker[i].futex = calloc(nfutexes, sizeof(*worker[i].futex));
163 if (!worker[i].futex)
167 CPU_SET(i % ncpus, &cpu);
169 ret = pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu);
171 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
173 ret = pthread_create(&worker[i].thread, &thread_attr, workerfn,
174 (void *)(struct worker *) &worker[i]);
176 err(EXIT_FAILURE, "pthread_create");
179 pthread_attr_destroy(&thread_attr);
181 pthread_mutex_lock(&thread_lock);
182 while (threads_starting)
183 pthread_cond_wait(&thread_parent, &thread_lock);
184 pthread_cond_broadcast(&thread_worker);
185 pthread_mutex_unlock(&thread_lock);
188 toggle_done(0, NULL, NULL);
190 for (i = 0; i < nthreads; i++) {
191 ret = pthread_join(worker[i].thread, NULL);
193 err(EXIT_FAILURE, "pthread_join");
196 /* cleanup & report results */
197 pthread_cond_destroy(&thread_parent);
198 pthread_cond_destroy(&thread_worker);
199 pthread_mutex_destroy(&thread_lock);
201 for (i = 0; i < nthreads; i++) {
202 unsigned long t = worker[i].ops / bench__runtime.tv_sec;
203 update_stats(&throughput_stats, t);
206 printf("[thread %2d] futex: %p [ %ld ops/sec ]\n",
207 worker[i].tid, &worker[i].futex[0], t);
209 printf("[thread %2d] futexes: %p ... %p [ %ld ops/sec ]\n",
210 worker[i].tid, &worker[i].futex[0],
211 &worker[i].futex[nfutexes-1], t);
214 free(worker[i].futex);
222 err(EXIT_FAILURE, "calloc");