1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2013 Davidlohr Bueso <davidlohr@hp.com>
5 * futex-wake: Block a bunch of threads on a futex and wake'em up, N at a time.
7 * This program is particularly useful to measure the latency of nthread wakeups
8 * in non-error situations: all waiters are queued and all wake calls wakeup
9 * one or more tasks, and thus the waitqueue is never empty.
12 /* For the CLR_() macros */
17 #include "../util/stat.h"
18 #include <subcmd/parse-options.h>
19 #include <linux/compiler.h>
20 #include <linux/kernel.h>
21 #include <linux/time64.h>
23 #include <perf/cpumap.h>
32 /* all threads will block on the same futex */
33 static u_int32_t futex1 = 0;
35 static pthread_t *worker;
36 static bool done = false;
37 static pthread_mutex_t thread_lock;
38 static pthread_cond_t thread_parent, thread_worker;
39 static struct stats waketime_stats, wakeup_stats;
40 static unsigned int threads_starting;
41 static int futex_flag = 0;
43 static struct bench_futex_parameters params = {
45 * How many wakeups to do at a time.
46 * Default to 1 in order to make the kernel work more.
51 static const struct option options[] = {
52 OPT_UINTEGER('t', "threads", ¶ms.nthreads, "Specify amount of threads"),
53 OPT_UINTEGER('w', "nwakes", ¶ms.nwakes, "Specify amount of threads to wake at once"),
54 OPT_BOOLEAN( 's', "silent", ¶ms.silent, "Silent mode: do not display data/details"),
55 OPT_BOOLEAN( 'S', "shared", ¶ms.fshared, "Use shared futexes instead of private ones"),
56 OPT_BOOLEAN( 'm', "mlockall", ¶ms.mlockall, "Lock all current and future memory"),
61 static const char * const bench_futex_wake_usage[] = {
62 "perf bench futex wake <options>",
66 static void *workerfn(void *arg __maybe_unused)
68 pthread_mutex_lock(&thread_lock);
70 if (!threads_starting)
71 pthread_cond_signal(&thread_parent);
72 pthread_cond_wait(&thread_worker, &thread_lock);
73 pthread_mutex_unlock(&thread_lock);
76 if (futex_wait(&futex1, 0, NULL, futex_flag) != EINTR)
84 static void print_summary(void)
86 double waketime_avg = avg_stats(&waketime_stats);
87 double waketime_stddev = stddev_stats(&waketime_stats);
88 unsigned int wakeup_avg = avg_stats(&wakeup_stats);
90 printf("Wokeup %d of %d threads in %.4f ms (+-%.2f%%)\n",
93 waketime_avg / USEC_PER_MSEC,
94 rel_stddev_stats(waketime_stddev, waketime_avg));
97 static void block_threads(pthread_t *w,
98 pthread_attr_t thread_attr, struct perf_cpu_map *cpu)
103 threads_starting = params.nthreads;
105 /* create and block all threads */
106 for (i = 0; i < params.nthreads; i++) {
108 CPU_SET(cpu->map[i % cpu->nr], &cpuset);
110 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset))
111 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
113 if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
114 err(EXIT_FAILURE, "pthread_create");
118 static void toggle_done(int sig __maybe_unused,
119 siginfo_t *info __maybe_unused,
120 void *uc __maybe_unused)
125 int bench_futex_wake(int argc, const char **argv)
129 struct sigaction act;
130 pthread_attr_t thread_attr;
131 struct perf_cpu_map *cpu;
133 argc = parse_options(argc, argv, options, bench_futex_wake_usage, 0);
135 usage_with_options(bench_futex_wake_usage, options);
139 cpu = perf_cpu_map__new(NULL);
141 err(EXIT_FAILURE, "calloc");
143 memset(&act, 0, sizeof(act));
144 sigfillset(&act.sa_mask);
145 act.sa_sigaction = toggle_done;
146 sigaction(SIGINT, &act, NULL);
148 if (params.mlockall) {
149 if (mlockall(MCL_CURRENT | MCL_FUTURE))
150 err(EXIT_FAILURE, "mlockall");
153 if (!params.nthreads)
154 params.nthreads = cpu->nr;
156 worker = calloc(params.nthreads, sizeof(*worker));
158 err(EXIT_FAILURE, "calloc");
161 futex_flag = FUTEX_PRIVATE_FLAG;
163 printf("Run summary [PID %d]: blocking on %d threads (at [%s] futex %p), "
164 "waking up %d at a time.\n\n",
165 getpid(), params.nthreads, params.fshared ? "shared":"private",
166 &futex1, params.nwakes);
168 init_stats(&wakeup_stats);
169 init_stats(&waketime_stats);
170 pthread_attr_init(&thread_attr);
171 pthread_mutex_init(&thread_lock, NULL);
172 pthread_cond_init(&thread_parent, NULL);
173 pthread_cond_init(&thread_worker, NULL);
175 for (j = 0; j < bench_repeat && !done; j++) {
176 unsigned int nwoken = 0;
177 struct timeval start, end, runtime;
179 /* create, launch & block all threads */
180 block_threads(worker, thread_attr, cpu);
182 /* make sure all threads are already blocked */
183 pthread_mutex_lock(&thread_lock);
184 while (threads_starting)
185 pthread_cond_wait(&thread_parent, &thread_lock);
186 pthread_cond_broadcast(&thread_worker);
187 pthread_mutex_unlock(&thread_lock);
191 /* Ok, all threads are patiently blocked, start waking folks up */
192 gettimeofday(&start, NULL);
193 while (nwoken != params.nthreads)
194 nwoken += futex_wake(&futex1,
195 params.nwakes, futex_flag);
196 gettimeofday(&end, NULL);
197 timersub(&end, &start, &runtime);
199 update_stats(&wakeup_stats, nwoken);
200 update_stats(&waketime_stats, runtime.tv_usec);
202 if (!params.silent) {
203 printf("[Run %d]: Wokeup %d of %d threads in %.4f ms\n",
204 j + 1, nwoken, params.nthreads,
205 runtime.tv_usec / (double)USEC_PER_MSEC);
208 for (i = 0; i < params.nthreads; i++) {
209 ret = pthread_join(worker[i], NULL);
211 err(EXIT_FAILURE, "pthread_join");
216 /* cleanup & report results */
217 pthread_cond_destroy(&thread_parent);
218 pthread_cond_destroy(&thread_worker);
219 pthread_mutex_destroy(&thread_lock);
220 pthread_attr_destroy(&thread_attr);
225 perf_cpu_map__put(cpu);