1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2015 Davidlohr Bueso.
5 * Block a bunch of threads and let parallel waker threads wakeup an
6 * equal amount of them. The program output reflects the avg latency
7 * for each individual thread to service its share of work. Ultimately
8 * it can be used to measure futex_wake() changes.
11 #include <linux/compiler.h>
12 #include "../util/debug.h"
13 #include "../util/mutex.h"
15 #ifndef HAVE_PTHREAD_BARRIER
16 int bench_futex_wake_parallel(int argc __maybe_unused, const char **argv __maybe_unused)
18 pr_err("%s: pthread_barrier_t unavailable, disabling this test...\n", __func__);
21 #else /* HAVE_PTHREAD_BARRIER */
22 /* For the CLR_() macros */
27 #include "../util/stat.h"
28 #include <subcmd/parse-options.h>
29 #include <linux/kernel.h>
30 #include <linux/time64.h>
33 #include <perf/cpumap.h>
43 struct timeval runtime;
46 static unsigned int nwakes = 1;
48 /* all threads will block on the same futex -- hash bucket chaos ;) */
49 static u_int32_t futex = 0;
51 static pthread_t *blocked_worker;
52 static bool done = false;
53 static struct mutex thread_lock;
54 static struct cond thread_parent, thread_worker;
55 static pthread_barrier_t barrier;
56 static struct stats waketime_stats, wakeup_stats;
57 static unsigned int threads_starting;
58 static int futex_flag = 0;
60 static struct bench_futex_parameters params;
62 static const struct option options[] = {
63 OPT_UINTEGER('t', "threads", ¶ms.nthreads, "Specify amount of threads"),
64 OPT_UINTEGER('w', "nwakers", ¶ms.nwakes, "Specify amount of waking threads"),
65 OPT_BOOLEAN( 's', "silent", ¶ms.silent, "Silent mode: do not display data/details"),
66 OPT_BOOLEAN( 'S', "shared", ¶ms.fshared, "Use shared futexes instead of private ones"),
67 OPT_BOOLEAN( 'm', "mlockall", ¶ms.mlockall, "Lock all current and future memory"),
72 static const char * const bench_futex_wake_parallel_usage[] = {
73 "perf bench futex wake-parallel <options>",
77 static void *waking_workerfn(void *arg)
79 struct thread_data *waker = (struct thread_data *) arg;
80 struct timeval start, end;
82 pthread_barrier_wait(&barrier);
84 gettimeofday(&start, NULL);
86 waker->nwoken = futex_wake(&futex, nwakes, futex_flag);
87 if (waker->nwoken != nwakes)
88 warnx("couldn't wakeup all tasks (%d/%d)",
89 waker->nwoken, nwakes);
91 gettimeofday(&end, NULL);
92 timersub(&end, &start, &waker->runtime);
98 static void wakeup_threads(struct thread_data *td, pthread_attr_t thread_attr)
102 pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
104 pthread_barrier_init(&barrier, NULL, params.nwakes + 1);
106 /* create and block all threads */
107 for (i = 0; i < params.nwakes; i++) {
109 * Thread creation order will impact per-thread latency
110 * as it will affect the order to acquire the hb spinlock.
111 * For now let the scheduler decide.
113 if (pthread_create(&td[i].worker, &thread_attr,
114 waking_workerfn, (void *)&td[i]))
115 err(EXIT_FAILURE, "pthread_create");
118 pthread_barrier_wait(&barrier);
120 for (i = 0; i < params.nwakes; i++)
121 if (pthread_join(td[i].worker, NULL))
122 err(EXIT_FAILURE, "pthread_join");
124 pthread_barrier_destroy(&barrier);
127 static void *blocked_workerfn(void *arg __maybe_unused)
129 mutex_lock(&thread_lock);
131 if (!threads_starting)
132 cond_signal(&thread_parent);
133 cond_wait(&thread_worker, &thread_lock);
134 mutex_unlock(&thread_lock);
136 while (1) { /* handle spurious wakeups */
137 if (futex_wait(&futex, 0, NULL, futex_flag) != EINTR)
145 static void block_threads(pthread_t *w, pthread_attr_t thread_attr,
146 struct perf_cpu_map *cpu)
150 int nrcpus = perf_cpu_map__nr(cpu);
153 threads_starting = params.nthreads;
155 cpuset = CPU_ALLOC(nrcpus);
157 size = CPU_ALLOC_SIZE(nrcpus);
159 /* create and block all threads */
160 for (i = 0; i < params.nthreads; i++) {
161 CPU_ZERO_S(size, cpuset);
162 CPU_SET_S(perf_cpu_map__cpu(cpu, i % perf_cpu_map__nr(cpu)).cpu, size, cpuset);
164 if (pthread_attr_setaffinity_np(&thread_attr, size, cpuset)) {
166 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
169 if (pthread_create(&w[i], &thread_attr, blocked_workerfn, NULL)) {
171 err(EXIT_FAILURE, "pthread_create");
177 static void print_run(struct thread_data *waking_worker, unsigned int run_num)
179 unsigned int i, wakeup_avg;
180 double waketime_avg, waketime_stddev;
181 struct stats __waketime_stats, __wakeup_stats;
183 init_stats(&__wakeup_stats);
184 init_stats(&__waketime_stats);
186 for (i = 0; i < params.nwakes; i++) {
187 update_stats(&__waketime_stats, waking_worker[i].runtime.tv_usec);
188 update_stats(&__wakeup_stats, waking_worker[i].nwoken);
191 waketime_avg = avg_stats(&__waketime_stats);
192 waketime_stddev = stddev_stats(&__waketime_stats);
193 wakeup_avg = avg_stats(&__wakeup_stats);
195 printf("[Run %d]: Avg per-thread latency (waking %d/%d threads) "
196 "in %.4f ms (+-%.2f%%)\n", run_num + 1, wakeup_avg,
197 params.nthreads, waketime_avg / USEC_PER_MSEC,
198 rel_stddev_stats(waketime_stddev, waketime_avg));
201 static void print_summary(void)
203 unsigned int wakeup_avg;
204 double waketime_avg, waketime_stddev;
206 waketime_avg = avg_stats(&waketime_stats);
207 waketime_stddev = stddev_stats(&waketime_stats);
208 wakeup_avg = avg_stats(&wakeup_stats);
210 printf("Avg per-thread latency (waking %d/%d threads) in %.4f ms (+-%.2f%%)\n",
213 waketime_avg / USEC_PER_MSEC,
214 rel_stddev_stats(waketime_stddev, waketime_avg));
218 static void do_run_stats(struct thread_data *waking_worker)
222 for (i = 0; i < params.nwakes; i++) {
223 update_stats(&waketime_stats, waking_worker[i].runtime.tv_usec);
224 update_stats(&wakeup_stats, waking_worker[i].nwoken);
229 static void toggle_done(int sig __maybe_unused,
230 siginfo_t *info __maybe_unused,
231 void *uc __maybe_unused)
236 int bench_futex_wake_parallel(int argc, const char **argv)
240 struct sigaction act;
241 pthread_attr_t thread_attr;
242 struct thread_data *waking_worker;
243 struct perf_cpu_map *cpu;
245 argc = parse_options(argc, argv, options,
246 bench_futex_wake_parallel_usage, 0);
248 usage_with_options(bench_futex_wake_parallel_usage, options);
252 memset(&act, 0, sizeof(act));
253 sigfillset(&act.sa_mask);
254 act.sa_sigaction = toggle_done;
255 sigaction(SIGINT, &act, NULL);
257 if (params.mlockall) {
258 if (mlockall(MCL_CURRENT | MCL_FUTURE))
259 err(EXIT_FAILURE, "mlockall");
262 cpu = perf_cpu_map__new(NULL);
264 err(EXIT_FAILURE, "calloc");
266 if (!params.nthreads)
267 params.nthreads = perf_cpu_map__nr(cpu);
269 /* some sanity checks */
270 if (params.nwakes > params.nthreads ||
272 params.nwakes = params.nthreads;
274 if (params.nthreads % params.nwakes)
275 errx(EXIT_FAILURE, "Must be perfectly divisible");
277 * Each thread will wakeup nwakes tasks in
278 * a single futex_wait call.
280 nwakes = params.nthreads/params.nwakes;
282 blocked_worker = calloc(params.nthreads, sizeof(*blocked_worker));
284 err(EXIT_FAILURE, "calloc");
287 futex_flag = FUTEX_PRIVATE_FLAG;
289 printf("Run summary [PID %d]: blocking on %d threads (at [%s] "
290 "futex %p), %d threads waking up %d at a time.\n\n",
291 getpid(), params.nthreads, params.fshared ? "shared":"private",
292 &futex, params.nwakes, nwakes);
294 init_stats(&wakeup_stats);
295 init_stats(&waketime_stats);
297 pthread_attr_init(&thread_attr);
298 mutex_init(&thread_lock);
299 cond_init(&thread_parent);
300 cond_init(&thread_worker);
302 for (j = 0; j < bench_repeat && !done; j++) {
303 waking_worker = calloc(params.nwakes, sizeof(*waking_worker));
305 err(EXIT_FAILURE, "calloc");
307 /* create, launch & block all threads */
308 block_threads(blocked_worker, thread_attr, cpu);
310 /* make sure all threads are already blocked */
311 mutex_lock(&thread_lock);
312 while (threads_starting)
313 cond_wait(&thread_parent, &thread_lock);
314 cond_broadcast(&thread_worker);
315 mutex_unlock(&thread_lock);
319 /* Ok, all threads are patiently blocked, start waking folks up */
320 wakeup_threads(waking_worker, thread_attr);
322 for (i = 0; i < params.nthreads; i++) {
323 ret = pthread_join(blocked_worker[i], NULL);
325 err(EXIT_FAILURE, "pthread_join");
328 do_run_stats(waking_worker);
330 print_run(waking_worker, j);
335 /* cleanup & report results */
336 cond_destroy(&thread_parent);
337 cond_destroy(&thread_worker);
338 mutex_destroy(&thread_lock);
339 pthread_attr_destroy(&thread_attr);
343 free(blocked_worker);
344 perf_cpu_map__put(cpu);
347 #endif /* HAVE_PTHREAD_BARRIER */