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>
21 #include <linux/zalloc.h>
23 #include <perf/cpumap.h>
25 #include "../util/stat.h"
26 #include <subcmd/parse-options.h>
32 static unsigned int nthreads = 0;
33 static unsigned int nsecs = 10;
34 /* amount of futexes per thread */
35 static unsigned int nfutexes = 1024;
36 static bool fshared = false, done = false, silent = false;
37 static int futex_flag = 0;
39 struct timeval bench__start, bench__end, bench__runtime;
40 static pthread_mutex_t thread_lock;
41 static unsigned int threads_starting;
42 static struct stats throughput_stats;
43 static pthread_cond_t thread_parent, thread_worker;
52 static const struct option options[] = {
53 OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
54 OPT_UINTEGER('r', "runtime", &nsecs, "Specify runtime (in seconds)"),
55 OPT_UINTEGER('f', "futexes", &nfutexes, "Specify amount of futexes per threads"),
56 OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
57 OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"),
61 static const char * const bench_futex_hash_usage[] = {
62 "perf bench futex hash <options>",
66 static void *workerfn(void *arg)
69 struct worker *w = (struct worker *) arg;
71 unsigned long ops = w->ops; /* avoid cacheline bouncing */
73 pthread_mutex_lock(&thread_lock);
75 if (!threads_starting)
76 pthread_cond_signal(&thread_parent);
77 pthread_cond_wait(&thread_worker, &thread_lock);
78 pthread_mutex_unlock(&thread_lock);
81 for (i = 0; i < nfutexes; i++, ops++) {
83 * We want the futex calls to fail in order to stress
84 * the hashing of uaddr and not measure other steps,
85 * such as internal waitqueue handling, thus enlarging
86 * the critical region protected by hb->lock.
88 ret = futex_wait(&w->futex[i], 1234, NULL, futex_flag);
90 (!ret || errno != EAGAIN || errno != EWOULDBLOCK))
91 warn("Non-expected futex return call");
99 static void toggle_done(int sig __maybe_unused,
100 siginfo_t *info __maybe_unused,
101 void *uc __maybe_unused)
103 /* inform all threads that we're done for the day */
105 gettimeofday(&bench__end, NULL);
106 timersub(&bench__end, &bench__start, &bench__runtime);
109 static void print_summary(void)
111 unsigned long avg = avg_stats(&throughput_stats);
112 double stddev = stddev_stats(&throughput_stats);
114 printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
115 !silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
116 (int)bench__runtime.tv_sec);
119 int bench_futex_hash(int argc, const char **argv)
123 struct sigaction act;
125 pthread_attr_t thread_attr;
126 struct worker *worker = NULL;
127 struct perf_cpu_map *cpu;
129 argc = parse_options(argc, argv, options, bench_futex_hash_usage, 0);
131 usage_with_options(bench_futex_hash_usage, options);
135 cpu = perf_cpu_map__new(NULL);
139 memset(&act, 0, sizeof(act));
140 sigfillset(&act.sa_mask);
141 act.sa_sigaction = toggle_done;
142 sigaction(SIGINT, &act, NULL);
144 if (!nthreads) /* default to the number of CPUs */
147 worker = calloc(nthreads, sizeof(*worker));
152 futex_flag = FUTEX_PRIVATE_FLAG;
154 printf("Run summary [PID %d]: %d threads, each operating on %d [%s] futexes for %d secs.\n\n",
155 getpid(), nthreads, nfutexes, fshared ? "shared":"private", nsecs);
157 init_stats(&throughput_stats);
158 pthread_mutex_init(&thread_lock, NULL);
159 pthread_cond_init(&thread_parent, NULL);
160 pthread_cond_init(&thread_worker, NULL);
162 threads_starting = nthreads;
163 pthread_attr_init(&thread_attr);
164 gettimeofday(&bench__start, NULL);
165 for (i = 0; i < nthreads; i++) {
167 worker[i].futex = calloc(nfutexes, sizeof(*worker[i].futex));
168 if (!worker[i].futex)
172 CPU_SET(cpu->map[i % cpu->nr], &cpuset);
174 ret = pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset);
176 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
178 ret = pthread_create(&worker[i].thread, &thread_attr, workerfn,
179 (void *)(struct worker *) &worker[i]);
181 err(EXIT_FAILURE, "pthread_create");
184 pthread_attr_destroy(&thread_attr);
186 pthread_mutex_lock(&thread_lock);
187 while (threads_starting)
188 pthread_cond_wait(&thread_parent, &thread_lock);
189 pthread_cond_broadcast(&thread_worker);
190 pthread_mutex_unlock(&thread_lock);
193 toggle_done(0, NULL, NULL);
195 for (i = 0; i < nthreads; i++) {
196 ret = pthread_join(worker[i].thread, NULL);
198 err(EXIT_FAILURE, "pthread_join");
201 /* cleanup & report results */
202 pthread_cond_destroy(&thread_parent);
203 pthread_cond_destroy(&thread_worker);
204 pthread_mutex_destroy(&thread_lock);
206 for (i = 0; i < nthreads; i++) {
207 unsigned long t = bench__runtime.tv_sec > 0 ?
208 worker[i].ops / bench__runtime.tv_sec : 0;
209 update_stats(&throughput_stats, t);
212 printf("[thread %2d] futex: %p [ %ld ops/sec ]\n",
213 worker[i].tid, &worker[i].futex[0], t);
215 printf("[thread %2d] futexes: %p ... %p [ %ld ops/sec ]\n",
216 worker[i].tid, &worker[i].futex[0],
217 &worker[i].futex[nfutexes-1], t);
220 zfree(&worker[i].futex);
229 err(EXIT_FAILURE, "calloc");