1 // SPDX-License-Identifier: GPL-2.0
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/mutex.h"
18 #include "../util/stat.h"
19 #include <subcmd/parse-options.h>
20 #include <linux/compiler.h>
21 #include <linux/kernel.h>
22 #include <linux/time64.h>
24 #include <perf/cpumap.h>
33 /* all threads will block on the same futex */
34 static u_int32_t futex1 = 0;
36 static pthread_t *worker;
37 static bool done = false;
38 static struct mutex thread_lock;
39 static struct cond thread_parent, thread_worker;
40 static struct stats waketime_stats, wakeup_stats;
41 static unsigned int threads_starting;
42 static int futex_flag = 0;
44 static struct bench_futex_parameters params = {
46 * How many wakeups to do at a time.
47 * Default to 1 in order to make the kernel work more.
52 static const struct option options[] = {
53 OPT_UINTEGER('t', "threads", ¶ms.nthreads, "Specify amount of threads"),
54 OPT_UINTEGER('w', "nwakes", ¶ms.nwakes, "Specify amount of threads to wake at once"),
55 OPT_BOOLEAN( 's', "silent", ¶ms.silent, "Silent mode: do not display data/details"),
56 OPT_BOOLEAN( 'S', "shared", ¶ms.fshared, "Use shared futexes instead of private ones"),
57 OPT_BOOLEAN( 'm', "mlockall", ¶ms.mlockall, "Lock all current and future memory"),
62 static const char * const bench_futex_wake_usage[] = {
63 "perf bench futex wake <options>",
67 static void *workerfn(void *arg __maybe_unused)
69 mutex_lock(&thread_lock);
71 if (!threads_starting)
72 cond_signal(&thread_parent);
73 cond_wait(&thread_worker, &thread_lock);
74 mutex_unlock(&thread_lock);
77 if (futex_wait(&futex1, 0, NULL, futex_flag) != EINTR)
85 static void print_summary(void)
87 double waketime_avg = avg_stats(&waketime_stats);
88 double waketime_stddev = stddev_stats(&waketime_stats);
89 unsigned int wakeup_avg = avg_stats(&wakeup_stats);
91 printf("Wokeup %d of %d threads in %.4f ms (+-%.2f%%)\n",
94 waketime_avg / USEC_PER_MSEC,
95 rel_stddev_stats(waketime_stddev, waketime_avg));
98 static void block_threads(pthread_t *w,
99 pthread_attr_t thread_attr, struct perf_cpu_map *cpu)
104 int nrcpus = perf_cpu_map__nr(cpu);
105 threads_starting = params.nthreads;
107 cpuset = CPU_ALLOC(nrcpus);
109 size = CPU_ALLOC_SIZE(nrcpus);
111 /* create and block all threads */
112 for (i = 0; i < params.nthreads; i++) {
113 CPU_ZERO_S(size, cpuset);
114 CPU_SET_S(perf_cpu_map__cpu(cpu, i % perf_cpu_map__nr(cpu)).cpu, size, cpuset);
116 if (pthread_attr_setaffinity_np(&thread_attr, size, cpuset)) {
118 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
121 if (pthread_create(&w[i], &thread_attr, workerfn, NULL)) {
123 err(EXIT_FAILURE, "pthread_create");
129 static void toggle_done(int sig __maybe_unused,
130 siginfo_t *info __maybe_unused,
131 void *uc __maybe_unused)
136 int bench_futex_wake(int argc, const char **argv)
140 struct sigaction act;
141 pthread_attr_t thread_attr;
142 struct perf_cpu_map *cpu;
144 argc = parse_options(argc, argv, options, bench_futex_wake_usage, 0);
146 usage_with_options(bench_futex_wake_usage, options);
150 cpu = perf_cpu_map__new(NULL);
152 err(EXIT_FAILURE, "calloc");
154 memset(&act, 0, sizeof(act));
155 sigfillset(&act.sa_mask);
156 act.sa_sigaction = toggle_done;
157 sigaction(SIGINT, &act, NULL);
159 if (params.mlockall) {
160 if (mlockall(MCL_CURRENT | MCL_FUTURE))
161 err(EXIT_FAILURE, "mlockall");
164 if (!params.nthreads)
165 params.nthreads = perf_cpu_map__nr(cpu);
167 worker = calloc(params.nthreads, sizeof(*worker));
169 err(EXIT_FAILURE, "calloc");
172 futex_flag = FUTEX_PRIVATE_FLAG;
174 printf("Run summary [PID %d]: blocking on %d threads (at [%s] futex %p), "
175 "waking up %d at a time.\n\n",
176 getpid(), params.nthreads, params.fshared ? "shared":"private",
177 &futex1, params.nwakes);
179 init_stats(&wakeup_stats);
180 init_stats(&waketime_stats);
181 pthread_attr_init(&thread_attr);
182 mutex_init(&thread_lock);
183 cond_init(&thread_parent);
184 cond_init(&thread_worker);
186 for (j = 0; j < bench_repeat && !done; j++) {
187 unsigned int nwoken = 0;
188 struct timeval start, end, runtime;
190 /* create, launch & block all threads */
191 block_threads(worker, thread_attr, cpu);
193 /* make sure all threads are already blocked */
194 mutex_lock(&thread_lock);
195 while (threads_starting)
196 cond_wait(&thread_parent, &thread_lock);
197 cond_broadcast(&thread_worker);
198 mutex_unlock(&thread_lock);
202 /* Ok, all threads are patiently blocked, start waking folks up */
203 gettimeofday(&start, NULL);
204 while (nwoken != params.nthreads)
205 nwoken += futex_wake(&futex1,
206 params.nwakes, futex_flag);
207 gettimeofday(&end, NULL);
208 timersub(&end, &start, &runtime);
210 update_stats(&wakeup_stats, nwoken);
211 update_stats(&waketime_stats, runtime.tv_usec);
213 if (!params.silent) {
214 printf("[Run %d]: Wokeup %d of %d threads in %.4f ms\n",
215 j + 1, nwoken, params.nthreads,
216 runtime.tv_usec / (double)USEC_PER_MSEC);
219 for (i = 0; i < params.nthreads; i++) {
220 ret = pthread_join(worker[i], NULL);
222 err(EXIT_FAILURE, "pthread_join");
227 /* cleanup & report results */
228 cond_destroy(&thread_parent);
229 cond_destroy(&thread_worker);
230 mutex_destroy(&thread_lock);
231 pthread_attr_destroy(&thread_attr);
236 perf_cpu_map__put(cpu);