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 int nrcpus = perf_cpu_map__nr(cpu);
104 threads_starting = params.nthreads;
106 cpuset = CPU_ALLOC(nrcpus);
108 size = CPU_ALLOC_SIZE(nrcpus);
110 /* create and block all threads */
111 for (i = 0; i < params.nthreads; i++) {
112 CPU_ZERO_S(size, cpuset);
113 CPU_SET_S(perf_cpu_map__cpu(cpu, i % perf_cpu_map__nr(cpu)).cpu, size, cpuset);
115 if (pthread_attr_setaffinity_np(&thread_attr, size, cpuset)) {
117 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
120 if (pthread_create(&w[i], &thread_attr, workerfn, NULL)) {
122 err(EXIT_FAILURE, "pthread_create");
128 static void toggle_done(int sig __maybe_unused,
129 siginfo_t *info __maybe_unused,
130 void *uc __maybe_unused)
135 int bench_futex_wake(int argc, const char **argv)
139 struct sigaction act;
140 pthread_attr_t thread_attr;
141 struct perf_cpu_map *cpu;
143 argc = parse_options(argc, argv, options, bench_futex_wake_usage, 0);
145 usage_with_options(bench_futex_wake_usage, options);
149 cpu = perf_cpu_map__new(NULL);
151 err(EXIT_FAILURE, "calloc");
153 memset(&act, 0, sizeof(act));
154 sigfillset(&act.sa_mask);
155 act.sa_sigaction = toggle_done;
156 sigaction(SIGINT, &act, NULL);
158 if (params.mlockall) {
159 if (mlockall(MCL_CURRENT | MCL_FUTURE))
160 err(EXIT_FAILURE, "mlockall");
163 if (!params.nthreads)
164 params.nthreads = perf_cpu_map__nr(cpu);
166 worker = calloc(params.nthreads, sizeof(*worker));
168 err(EXIT_FAILURE, "calloc");
171 futex_flag = FUTEX_PRIVATE_FLAG;
173 printf("Run summary [PID %d]: blocking on %d threads (at [%s] futex %p), "
174 "waking up %d at a time.\n\n",
175 getpid(), params.nthreads, params.fshared ? "shared":"private",
176 &futex1, params.nwakes);
178 init_stats(&wakeup_stats);
179 init_stats(&waketime_stats);
180 pthread_attr_init(&thread_attr);
181 pthread_mutex_init(&thread_lock, NULL);
182 pthread_cond_init(&thread_parent, NULL);
183 pthread_cond_init(&thread_worker, NULL);
185 for (j = 0; j < bench_repeat && !done; j++) {
186 unsigned int nwoken = 0;
187 struct timeval start, end, runtime;
189 /* create, launch & block all threads */
190 block_threads(worker, thread_attr, cpu);
192 /* make sure all threads are already blocked */
193 pthread_mutex_lock(&thread_lock);
194 while (threads_starting)
195 pthread_cond_wait(&thread_parent, &thread_lock);
196 pthread_cond_broadcast(&thread_worker);
197 pthread_mutex_unlock(&thread_lock);
201 /* Ok, all threads are patiently blocked, start waking folks up */
202 gettimeofday(&start, NULL);
203 while (nwoken != params.nthreads)
204 nwoken += futex_wake(&futex1,
205 params.nwakes, futex_flag);
206 gettimeofday(&end, NULL);
207 timersub(&end, &start, &runtime);
209 update_stats(&wakeup_stats, nwoken);
210 update_stats(&waketime_stats, runtime.tv_usec);
212 if (!params.silent) {
213 printf("[Run %d]: Wokeup %d of %d threads in %.4f ms\n",
214 j + 1, nwoken, params.nthreads,
215 runtime.tv_usec / (double)USEC_PER_MSEC);
218 for (i = 0; i < params.nthreads; i++) {
219 ret = pthread_join(worker[i], NULL);
221 err(EXIT_FAILURE, "pthread_join");
226 /* cleanup & report results */
227 pthread_cond_destroy(&thread_parent);
228 pthread_cond_destroy(&thread_worker);
229 pthread_mutex_destroy(&thread_lock);
230 pthread_attr_destroy(&thread_attr);
235 perf_cpu_map__put(cpu);