1 // SPDX-License-Identifier: GPL-2.0
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>
21 #include <linux/zalloc.h>
24 #include <perf/cpumap.h>
26 #include "../util/stat.h"
27 #include <subcmd/parse-options.h>
33 static bool done = false;
34 static int futex_flag = 0;
36 struct timeval bench__start, bench__end, bench__runtime;
37 static pthread_mutex_t thread_lock;
38 static unsigned int threads_starting;
39 static struct stats throughput_stats;
40 static pthread_cond_t thread_parent, thread_worker;
49 static struct bench_futex_parameters params = {
54 static const struct option options[] = {
55 OPT_UINTEGER('t', "threads", ¶ms.nthreads, "Specify amount of threads"),
56 OPT_UINTEGER('r', "runtime", ¶ms.runtime, "Specify runtime (in seconds)"),
57 OPT_UINTEGER('f', "futexes", ¶ms.nfutexes, "Specify amount of futexes per threads"),
58 OPT_BOOLEAN( 's', "silent", ¶ms.silent, "Silent mode: do not display data/details"),
59 OPT_BOOLEAN( 'S', "shared", ¶ms.fshared, "Use shared futexes instead of private ones"),
60 OPT_BOOLEAN( 'm', "mlockall", ¶ms.mlockall, "Lock all current and future memory"),
64 static const char * const bench_futex_hash_usage[] = {
65 "perf bench futex hash <options>",
69 static void *workerfn(void *arg)
72 struct worker *w = (struct worker *) arg;
74 unsigned long ops = w->ops; /* avoid cacheline bouncing */
76 pthread_mutex_lock(&thread_lock);
78 if (!threads_starting)
79 pthread_cond_signal(&thread_parent);
80 pthread_cond_wait(&thread_worker, &thread_lock);
81 pthread_mutex_unlock(&thread_lock);
84 for (i = 0; i < params.nfutexes; i++, ops++) {
86 * We want the futex calls to fail in order to stress
87 * the hashing of uaddr and not measure other steps,
88 * such as internal waitqueue handling, thus enlarging
89 * the critical region protected by hb->lock.
91 ret = futex_wait(&w->futex[i], 1234, NULL, futex_flag);
93 (!ret || errno != EAGAIN || errno != EWOULDBLOCK))
94 warn("Non-expected futex return call");
102 static void toggle_done(int sig __maybe_unused,
103 siginfo_t *info __maybe_unused,
104 void *uc __maybe_unused)
106 /* inform all threads that we're done for the day */
108 gettimeofday(&bench__end, NULL);
109 timersub(&bench__end, &bench__start, &bench__runtime);
112 static void print_summary(void)
114 unsigned long avg = avg_stats(&throughput_stats);
115 double stddev = stddev_stats(&throughput_stats);
117 printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
118 !params.silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
119 (int)bench__runtime.tv_sec);
122 int bench_futex_hash(int argc, const char **argv)
126 struct sigaction act;
128 pthread_attr_t thread_attr;
129 struct worker *worker = NULL;
130 struct perf_cpu_map *cpu;
134 argc = parse_options(argc, argv, options, bench_futex_hash_usage, 0);
136 usage_with_options(bench_futex_hash_usage, options);
140 cpu = perf_cpu_map__new(NULL);
144 memset(&act, 0, sizeof(act));
145 sigfillset(&act.sa_mask);
146 act.sa_sigaction = toggle_done;
147 sigaction(SIGINT, &act, NULL);
149 if (params.mlockall) {
150 if (mlockall(MCL_CURRENT | MCL_FUTURE))
151 err(EXIT_FAILURE, "mlockall");
154 if (!params.nthreads) /* default to the number of CPUs */
155 params.nthreads = perf_cpu_map__nr(cpu);
157 worker = calloc(params.nthreads, sizeof(*worker));
162 futex_flag = FUTEX_PRIVATE_FLAG;
164 printf("Run summary [PID %d]: %d threads, each operating on %d [%s] futexes for %d secs.\n\n",
165 getpid(), params.nthreads, params.nfutexes, params.fshared ? "shared":"private", params.runtime);
167 init_stats(&throughput_stats);
168 pthread_mutex_init(&thread_lock, NULL);
169 pthread_cond_init(&thread_parent, NULL);
170 pthread_cond_init(&thread_worker, NULL);
172 threads_starting = params.nthreads;
173 pthread_attr_init(&thread_attr);
174 gettimeofday(&bench__start, NULL);
176 nrcpus = perf_cpu_map__nr(cpu);
177 cpuset = CPU_ALLOC(nrcpus);
179 size = CPU_ALLOC_SIZE(nrcpus);
181 for (i = 0; i < params.nthreads; i++) {
183 worker[i].futex = calloc(params.nfutexes, sizeof(*worker[i].futex));
184 if (!worker[i].futex)
187 CPU_ZERO_S(size, cpuset);
189 CPU_SET_S(perf_cpu_map__cpu(cpu, i % perf_cpu_map__nr(cpu)).cpu, size, cpuset);
190 ret = pthread_attr_setaffinity_np(&thread_attr, size, cpuset);
193 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
195 ret = pthread_create(&worker[i].thread, &thread_attr, workerfn,
196 (void *)(struct worker *) &worker[i]);
199 err(EXIT_FAILURE, "pthread_create");
204 pthread_attr_destroy(&thread_attr);
206 pthread_mutex_lock(&thread_lock);
207 while (threads_starting)
208 pthread_cond_wait(&thread_parent, &thread_lock);
209 pthread_cond_broadcast(&thread_worker);
210 pthread_mutex_unlock(&thread_lock);
212 sleep(params.runtime);
213 toggle_done(0, NULL, NULL);
215 for (i = 0; i < params.nthreads; i++) {
216 ret = pthread_join(worker[i].thread, NULL);
218 err(EXIT_FAILURE, "pthread_join");
221 /* cleanup & report results */
222 pthread_cond_destroy(&thread_parent);
223 pthread_cond_destroy(&thread_worker);
224 pthread_mutex_destroy(&thread_lock);
226 for (i = 0; i < params.nthreads; i++) {
227 unsigned long t = bench__runtime.tv_sec > 0 ?
228 worker[i].ops / bench__runtime.tv_sec : 0;
229 update_stats(&throughput_stats, t);
230 if (!params.silent) {
231 if (params.nfutexes == 1)
232 printf("[thread %2d] futex: %p [ %ld ops/sec ]\n",
233 worker[i].tid, &worker[i].futex[0], t);
235 printf("[thread %2d] futexes: %p ... %p [ %ld ops/sec ]\n",
236 worker[i].tid, &worker[i].futex[0],
237 &worker[i].futex[params.nfutexes-1], t);
240 zfree(&worker[i].futex);
249 err(EXIT_FAILURE, "calloc");