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"
14 #ifndef HAVE_PTHREAD_BARRIER
15 int bench_futex_wake_parallel(int argc __maybe_unused, const char **argv __maybe_unused)
17 pr_err("%s: pthread_barrier_t unavailable, disabling this test...\n", __func__);
20 #else /* HAVE_PTHREAD_BARRIER */
21 /* For the CLR_() macros */
26 #include "../util/stat.h"
27 #include <subcmd/parse-options.h>
28 #include <linux/kernel.h>
29 #include <linux/time64.h>
32 #include <perf/cpumap.h>
42 struct timeval runtime;
45 static unsigned int nwakes = 1;
47 /* all threads will block on the same futex -- hash bucket chaos ;) */
48 static u_int32_t futex = 0;
50 static pthread_t *blocked_worker;
51 static bool done = false;
52 static pthread_mutex_t thread_lock;
53 static pthread_cond_t thread_parent, thread_worker;
54 static pthread_barrier_t barrier;
55 static struct stats waketime_stats, wakeup_stats;
56 static unsigned int threads_starting;
57 static int futex_flag = 0;
59 static struct bench_futex_parameters params;
61 static const struct option options[] = {
62 OPT_UINTEGER('t', "threads", ¶ms.nthreads, "Specify amount of threads"),
63 OPT_UINTEGER('w', "nwakers", ¶ms.nwakes, "Specify amount of waking threads"),
64 OPT_BOOLEAN( 's', "silent", ¶ms.silent, "Silent mode: do not display data/details"),
65 OPT_BOOLEAN( 'S', "shared", ¶ms.fshared, "Use shared futexes instead of private ones"),
66 OPT_BOOLEAN( 'm', "mlockall", ¶ms.mlockall, "Lock all current and future memory"),
71 static const char * const bench_futex_wake_parallel_usage[] = {
72 "perf bench futex wake-parallel <options>",
76 static void *waking_workerfn(void *arg)
78 struct thread_data *waker = (struct thread_data *) arg;
79 struct timeval start, end;
81 pthread_barrier_wait(&barrier);
83 gettimeofday(&start, NULL);
85 waker->nwoken = futex_wake(&futex, nwakes, futex_flag);
86 if (waker->nwoken != nwakes)
87 warnx("couldn't wakeup all tasks (%d/%d)",
88 waker->nwoken, nwakes);
90 gettimeofday(&end, NULL);
91 timersub(&end, &start, &waker->runtime);
97 static void wakeup_threads(struct thread_data *td, pthread_attr_t thread_attr)
101 pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
103 pthread_barrier_init(&barrier, NULL, params.nwakes + 1);
105 /* create and block all threads */
106 for (i = 0; i < params.nwakes; i++) {
108 * Thread creation order will impact per-thread latency
109 * as it will affect the order to acquire the hb spinlock.
110 * For now let the scheduler decide.
112 if (pthread_create(&td[i].worker, &thread_attr,
113 waking_workerfn, (void *)&td[i]))
114 err(EXIT_FAILURE, "pthread_create");
117 pthread_barrier_wait(&barrier);
119 for (i = 0; i < params.nwakes; i++)
120 if (pthread_join(td[i].worker, NULL))
121 err(EXIT_FAILURE, "pthread_join");
123 pthread_barrier_destroy(&barrier);
126 static void *blocked_workerfn(void *arg __maybe_unused)
128 pthread_mutex_lock(&thread_lock);
130 if (!threads_starting)
131 pthread_cond_signal(&thread_parent);
132 pthread_cond_wait(&thread_worker, &thread_lock);
133 pthread_mutex_unlock(&thread_lock);
135 while (1) { /* handle spurious wakeups */
136 if (futex_wait(&futex, 0, NULL, futex_flag) != EINTR)
144 static void block_threads(pthread_t *w, pthread_attr_t thread_attr,
145 struct perf_cpu_map *cpu)
149 int nrcpus = perf_cpu_map__nr(cpu);
152 threads_starting = params.nthreads;
154 cpuset = CPU_ALLOC(nrcpus);
156 size = CPU_ALLOC_SIZE(nrcpus);
158 /* create and block all threads */
159 for (i = 0; i < params.nthreads; i++) {
160 CPU_ZERO_S(size, cpuset);
161 CPU_SET_S(perf_cpu_map__cpu(cpu, i % perf_cpu_map__nr(cpu)).cpu, size, cpuset);
163 if (pthread_attr_setaffinity_np(&thread_attr, size, cpuset)) {
165 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
168 if (pthread_create(&w[i], &thread_attr, blocked_workerfn, NULL)) {
170 err(EXIT_FAILURE, "pthread_create");
176 static void print_run(struct thread_data *waking_worker, unsigned int run_num)
178 unsigned int i, wakeup_avg;
179 double waketime_avg, waketime_stddev;
180 struct stats __waketime_stats, __wakeup_stats;
182 init_stats(&__wakeup_stats);
183 init_stats(&__waketime_stats);
185 for (i = 0; i < params.nwakes; i++) {
186 update_stats(&__waketime_stats, waking_worker[i].runtime.tv_usec);
187 update_stats(&__wakeup_stats, waking_worker[i].nwoken);
190 waketime_avg = avg_stats(&__waketime_stats);
191 waketime_stddev = stddev_stats(&__waketime_stats);
192 wakeup_avg = avg_stats(&__wakeup_stats);
194 printf("[Run %d]: Avg per-thread latency (waking %d/%d threads) "
195 "in %.4f ms (+-%.2f%%)\n", run_num + 1, wakeup_avg,
196 params.nthreads, waketime_avg / USEC_PER_MSEC,
197 rel_stddev_stats(waketime_stddev, waketime_avg));
200 static void print_summary(void)
202 unsigned int wakeup_avg;
203 double waketime_avg, waketime_stddev;
205 waketime_avg = avg_stats(&waketime_stats);
206 waketime_stddev = stddev_stats(&waketime_stats);
207 wakeup_avg = avg_stats(&wakeup_stats);
209 printf("Avg per-thread latency (waking %d/%d threads) in %.4f ms (+-%.2f%%)\n",
212 waketime_avg / USEC_PER_MSEC,
213 rel_stddev_stats(waketime_stddev, waketime_avg));
217 static void do_run_stats(struct thread_data *waking_worker)
221 for (i = 0; i < params.nwakes; i++) {
222 update_stats(&waketime_stats, waking_worker[i].runtime.tv_usec);
223 update_stats(&wakeup_stats, waking_worker[i].nwoken);
228 static void toggle_done(int sig __maybe_unused,
229 siginfo_t *info __maybe_unused,
230 void *uc __maybe_unused)
235 int bench_futex_wake_parallel(int argc, const char **argv)
239 struct sigaction act;
240 pthread_attr_t thread_attr;
241 struct thread_data *waking_worker;
242 struct perf_cpu_map *cpu;
244 argc = parse_options(argc, argv, options,
245 bench_futex_wake_parallel_usage, 0);
247 usage_with_options(bench_futex_wake_parallel_usage, options);
251 memset(&act, 0, sizeof(act));
252 sigfillset(&act.sa_mask);
253 act.sa_sigaction = toggle_done;
254 sigaction(SIGINT, &act, NULL);
256 if (params.mlockall) {
257 if (mlockall(MCL_CURRENT | MCL_FUTURE))
258 err(EXIT_FAILURE, "mlockall");
261 cpu = perf_cpu_map__new(NULL);
263 err(EXIT_FAILURE, "calloc");
265 if (!params.nthreads)
266 params.nthreads = perf_cpu_map__nr(cpu);
268 /* some sanity checks */
269 if (params.nwakes > params.nthreads ||
271 params.nwakes = params.nthreads;
273 if (params.nthreads % params.nwakes)
274 errx(EXIT_FAILURE, "Must be perfectly divisible");
276 * Each thread will wakeup nwakes tasks in
277 * a single futex_wait call.
279 nwakes = params.nthreads/params.nwakes;
281 blocked_worker = calloc(params.nthreads, sizeof(*blocked_worker));
283 err(EXIT_FAILURE, "calloc");
286 futex_flag = FUTEX_PRIVATE_FLAG;
288 printf("Run summary [PID %d]: blocking on %d threads (at [%s] "
289 "futex %p), %d threads waking up %d at a time.\n\n",
290 getpid(), params.nthreads, params.fshared ? "shared":"private",
291 &futex, params.nwakes, nwakes);
293 init_stats(&wakeup_stats);
294 init_stats(&waketime_stats);
296 pthread_attr_init(&thread_attr);
297 pthread_mutex_init(&thread_lock, NULL);
298 pthread_cond_init(&thread_parent, NULL);
299 pthread_cond_init(&thread_worker, NULL);
301 for (j = 0; j < bench_repeat && !done; j++) {
302 waking_worker = calloc(params.nwakes, sizeof(*waking_worker));
304 err(EXIT_FAILURE, "calloc");
306 /* create, launch & block all threads */
307 block_threads(blocked_worker, thread_attr, cpu);
309 /* make sure all threads are already blocked */
310 pthread_mutex_lock(&thread_lock);
311 while (threads_starting)
312 pthread_cond_wait(&thread_parent, &thread_lock);
313 pthread_cond_broadcast(&thread_worker);
314 pthread_mutex_unlock(&thread_lock);
318 /* Ok, all threads are patiently blocked, start waking folks up */
319 wakeup_threads(waking_worker, thread_attr);
321 for (i = 0; i < params.nthreads; i++) {
322 ret = pthread_join(blocked_worker[i], NULL);
324 err(EXIT_FAILURE, "pthread_join");
327 do_run_stats(waking_worker);
329 print_run(waking_worker, j);
334 /* cleanup & report results */
335 pthread_cond_destroy(&thread_parent);
336 pthread_cond_destroy(&thread_worker);
337 pthread_mutex_destroy(&thread_lock);
338 pthread_attr_destroy(&thread_attr);
342 free(blocked_worker);
343 perf_cpu_map__put(cpu);
346 #endif /* HAVE_PTHREAD_BARRIER */