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 <internal/cpumap.h>
24 #include <perf/cpumap.h>
32 /* all threads will block on the same futex */
33 static u_int32_t futex1 = 0;
36 * How many wakeups to do at a time.
37 * Default to 1 in order to make the kernel work more.
39 static unsigned int nwakes = 1;
42 static bool done = false, silent = false, fshared = false;
43 static pthread_mutex_t thread_lock;
44 static pthread_cond_t thread_parent, thread_worker;
45 static struct stats waketime_stats, wakeup_stats;
46 static unsigned int threads_starting, nthreads = 0;
47 static int futex_flag = 0;
49 static const struct option options[] = {
50 OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
51 OPT_UINTEGER('w', "nwakes", &nwakes, "Specify amount of threads to wake at once"),
52 OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
53 OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"),
57 static const char * const bench_futex_wake_usage[] = {
58 "perf bench futex wake <options>",
62 static void *workerfn(void *arg __maybe_unused)
64 pthread_mutex_lock(&thread_lock);
66 if (!threads_starting)
67 pthread_cond_signal(&thread_parent);
68 pthread_cond_wait(&thread_worker, &thread_lock);
69 pthread_mutex_unlock(&thread_lock);
72 if (futex_wait(&futex1, 0, NULL, futex_flag) != EINTR)
80 static void print_summary(void)
82 double waketime_avg = avg_stats(&waketime_stats);
83 double waketime_stddev = stddev_stats(&waketime_stats);
84 unsigned int wakeup_avg = avg_stats(&wakeup_stats);
86 printf("Wokeup %d of %d threads in %.4f ms (+-%.2f%%)\n",
89 waketime_avg / USEC_PER_MSEC,
90 rel_stddev_stats(waketime_stddev, waketime_avg));
93 static void block_threads(pthread_t *w,
94 pthread_attr_t thread_attr, struct perf_cpu_map *cpu)
99 threads_starting = nthreads;
101 /* create and block all threads */
102 for (i = 0; i < nthreads; i++) {
104 CPU_SET(cpu->map[i % cpu->nr], &cpuset);
106 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset))
107 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
109 if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
110 err(EXIT_FAILURE, "pthread_create");
114 static void toggle_done(int sig __maybe_unused,
115 siginfo_t *info __maybe_unused,
116 void *uc __maybe_unused)
121 int bench_futex_wake(int argc, const char **argv)
125 struct sigaction act;
126 pthread_attr_t thread_attr;
127 struct perf_cpu_map *cpu;
129 argc = parse_options(argc, argv, options, bench_futex_wake_usage, 0);
131 usage_with_options(bench_futex_wake_usage, options);
135 cpu = perf_cpu_map__new(NULL);
137 err(EXIT_FAILURE, "calloc");
139 memset(&act, 0, sizeof(act));
140 sigfillset(&act.sa_mask);
141 act.sa_sigaction = toggle_done;
142 sigaction(SIGINT, &act, NULL);
147 worker = calloc(nthreads, sizeof(*worker));
149 err(EXIT_FAILURE, "calloc");
152 futex_flag = FUTEX_PRIVATE_FLAG;
154 printf("Run summary [PID %d]: blocking on %d threads (at [%s] futex %p), "
155 "waking up %d at a time.\n\n",
156 getpid(), nthreads, fshared ? "shared":"private", &futex1, nwakes);
158 init_stats(&wakeup_stats);
159 init_stats(&waketime_stats);
160 pthread_attr_init(&thread_attr);
161 pthread_mutex_init(&thread_lock, NULL);
162 pthread_cond_init(&thread_parent, NULL);
163 pthread_cond_init(&thread_worker, NULL);
165 for (j = 0; j < bench_repeat && !done; j++) {
166 unsigned int nwoken = 0;
167 struct timeval start, end, runtime;
169 /* create, launch & block all threads */
170 block_threads(worker, thread_attr, cpu);
172 /* make sure all threads are already blocked */
173 pthread_mutex_lock(&thread_lock);
174 while (threads_starting)
175 pthread_cond_wait(&thread_parent, &thread_lock);
176 pthread_cond_broadcast(&thread_worker);
177 pthread_mutex_unlock(&thread_lock);
181 /* Ok, all threads are patiently blocked, start waking folks up */
182 gettimeofday(&start, NULL);
183 while (nwoken != nthreads)
184 nwoken += futex_wake(&futex1, nwakes, futex_flag);
185 gettimeofday(&end, NULL);
186 timersub(&end, &start, &runtime);
188 update_stats(&wakeup_stats, nwoken);
189 update_stats(&waketime_stats, runtime.tv_usec);
192 printf("[Run %d]: Wokeup %d of %d threads in %.4f ms\n",
193 j + 1, nwoken, nthreads, runtime.tv_usec / (double)USEC_PER_MSEC);
196 for (i = 0; i < nthreads; i++) {
197 ret = pthread_join(worker[i], NULL);
199 err(EXIT_FAILURE, "pthread_join");
204 /* cleanup & report results */
205 pthread_cond_destroy(&thread_parent);
206 pthread_cond_destroy(&thread_worker);
207 pthread_mutex_destroy(&thread_lock);
208 pthread_attr_destroy(&thread_attr);