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)
150 threads_starting = params.nthreads;
152 /* create and block all threads */
153 for (i = 0; i < params.nthreads; i++) {
155 CPU_SET(cpu->map[i % cpu->nr], &cpuset);
157 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset))
158 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
160 if (pthread_create(&w[i], &thread_attr, blocked_workerfn, NULL))
161 err(EXIT_FAILURE, "pthread_create");
165 static void print_run(struct thread_data *waking_worker, unsigned int run_num)
167 unsigned int i, wakeup_avg;
168 double waketime_avg, waketime_stddev;
169 struct stats __waketime_stats, __wakeup_stats;
171 init_stats(&__wakeup_stats);
172 init_stats(&__waketime_stats);
174 for (i = 0; i < params.nwakes; i++) {
175 update_stats(&__waketime_stats, waking_worker[i].runtime.tv_usec);
176 update_stats(&__wakeup_stats, waking_worker[i].nwoken);
179 waketime_avg = avg_stats(&__waketime_stats);
180 waketime_stddev = stddev_stats(&__waketime_stats);
181 wakeup_avg = avg_stats(&__wakeup_stats);
183 printf("[Run %d]: Avg per-thread latency (waking %d/%d threads) "
184 "in %.4f ms (+-%.2f%%)\n", run_num + 1, wakeup_avg,
185 params.nthreads, waketime_avg / USEC_PER_MSEC,
186 rel_stddev_stats(waketime_stddev, waketime_avg));
189 static void print_summary(void)
191 unsigned int wakeup_avg;
192 double waketime_avg, waketime_stddev;
194 waketime_avg = avg_stats(&waketime_stats);
195 waketime_stddev = stddev_stats(&waketime_stats);
196 wakeup_avg = avg_stats(&wakeup_stats);
198 printf("Avg per-thread latency (waking %d/%d threads) in %.4f ms (+-%.2f%%)\n",
201 waketime_avg / USEC_PER_MSEC,
202 rel_stddev_stats(waketime_stddev, waketime_avg));
206 static void do_run_stats(struct thread_data *waking_worker)
210 for (i = 0; i < params.nwakes; i++) {
211 update_stats(&waketime_stats, waking_worker[i].runtime.tv_usec);
212 update_stats(&wakeup_stats, waking_worker[i].nwoken);
217 static void toggle_done(int sig __maybe_unused,
218 siginfo_t *info __maybe_unused,
219 void *uc __maybe_unused)
224 int bench_futex_wake_parallel(int argc, const char **argv)
228 struct sigaction act;
229 pthread_attr_t thread_attr;
230 struct thread_data *waking_worker;
231 struct perf_cpu_map *cpu;
233 argc = parse_options(argc, argv, options,
234 bench_futex_wake_parallel_usage, 0);
236 usage_with_options(bench_futex_wake_parallel_usage, options);
240 memset(&act, 0, sizeof(act));
241 sigfillset(&act.sa_mask);
242 act.sa_sigaction = toggle_done;
243 sigaction(SIGINT, &act, NULL);
245 if (params.mlockall) {
246 if (mlockall(MCL_CURRENT | MCL_FUTURE))
247 err(EXIT_FAILURE, "mlockall");
250 cpu = perf_cpu_map__new(NULL);
252 err(EXIT_FAILURE, "calloc");
254 if (!params.nthreads)
255 params.nthreads = cpu->nr;
257 /* some sanity checks */
258 if (params.nwakes > params.nthreads ||
260 params.nwakes = params.nthreads;
262 if (params.nthreads % params.nwakes)
263 errx(EXIT_FAILURE, "Must be perfectly divisible");
265 * Each thread will wakeup nwakes tasks in
266 * a single futex_wait call.
268 nwakes = params.nthreads/params.nwakes;
270 blocked_worker = calloc(params.nthreads, sizeof(*blocked_worker));
272 err(EXIT_FAILURE, "calloc");
275 futex_flag = FUTEX_PRIVATE_FLAG;
277 printf("Run summary [PID %d]: blocking on %d threads (at [%s] "
278 "futex %p), %d threads waking up %d at a time.\n\n",
279 getpid(), params.nthreads, params.fshared ? "shared":"private",
280 &futex, params.nwakes, nwakes);
282 init_stats(&wakeup_stats);
283 init_stats(&waketime_stats);
285 pthread_attr_init(&thread_attr);
286 pthread_mutex_init(&thread_lock, NULL);
287 pthread_cond_init(&thread_parent, NULL);
288 pthread_cond_init(&thread_worker, NULL);
290 for (j = 0; j < bench_repeat && !done; j++) {
291 waking_worker = calloc(params.nwakes, sizeof(*waking_worker));
293 err(EXIT_FAILURE, "calloc");
295 /* create, launch & block all threads */
296 block_threads(blocked_worker, thread_attr, cpu);
298 /* make sure all threads are already blocked */
299 pthread_mutex_lock(&thread_lock);
300 while (threads_starting)
301 pthread_cond_wait(&thread_parent, &thread_lock);
302 pthread_cond_broadcast(&thread_worker);
303 pthread_mutex_unlock(&thread_lock);
307 /* Ok, all threads are patiently blocked, start waking folks up */
308 wakeup_threads(waking_worker, thread_attr);
310 for (i = 0; i < params.nthreads; i++) {
311 ret = pthread_join(blocked_worker[i], NULL);
313 err(EXIT_FAILURE, "pthread_join");
316 do_run_stats(waking_worker);
318 print_run(waking_worker, j);
323 /* cleanup & report results */
324 pthread_cond_destroy(&thread_parent);
325 pthread_cond_destroy(&thread_worker);
326 pthread_mutex_destroy(&thread_lock);
327 pthread_attr_destroy(&thread_attr);
331 free(blocked_worker);
332 perf_cpu_map__put(cpu);
335 #endif /* HAVE_PTHREAD_BARRIER */