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 /* For the CLR_() macros */
16 #include "../util/stat.h"
17 #include <subcmd/parse-options.h>
18 #include <linux/compiler.h>
19 #include <linux/kernel.h>
20 #include <linux/time64.h>
32 struct timeval runtime;
35 static unsigned int nwakes = 1;
37 /* all threads will block on the same futex -- hash bucket chaos ;) */
38 static u_int32_t futex = 0;
40 static pthread_t *blocked_worker;
41 static bool done = false, silent = false, fshared = false;
42 static unsigned int nblocked_threads = 0, nwaking_threads = 0;
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 ncpus, threads_starting;
47 static int futex_flag = 0;
49 static const struct option options[] = {
50 OPT_UINTEGER('t', "threads", &nblocked_threads, "Specify amount of threads"),
51 OPT_UINTEGER('w', "nwakers", &nwaking_threads, "Specify amount of waking threads"),
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_parallel_usage[] = {
58 "perf bench futex wake-parallel <options>",
62 static void *waking_workerfn(void *arg)
64 struct thread_data *waker = (struct thread_data *) arg;
65 struct timeval start, end;
67 gettimeofday(&start, NULL);
69 waker->nwoken = futex_wake(&futex, nwakes, futex_flag);
70 if (waker->nwoken != nwakes)
71 warnx("couldn't wakeup all tasks (%d/%d)",
72 waker->nwoken, nwakes);
74 gettimeofday(&end, NULL);
75 timersub(&end, &start, &waker->runtime);
81 static void wakeup_threads(struct thread_data *td, pthread_attr_t thread_attr)
85 pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
87 /* create and block all threads */
88 for (i = 0; i < nwaking_threads; i++) {
90 * Thread creation order will impact per-thread latency
91 * as it will affect the order to acquire the hb spinlock.
92 * For now let the scheduler decide.
94 if (pthread_create(&td[i].worker, &thread_attr,
95 waking_workerfn, (void *)&td[i]))
96 err(EXIT_FAILURE, "pthread_create");
99 for (i = 0; i < nwaking_threads; i++)
100 if (pthread_join(td[i].worker, NULL))
101 err(EXIT_FAILURE, "pthread_join");
104 static void *blocked_workerfn(void *arg __maybe_unused)
106 pthread_mutex_lock(&thread_lock);
108 if (!threads_starting)
109 pthread_cond_signal(&thread_parent);
110 pthread_cond_wait(&thread_worker, &thread_lock);
111 pthread_mutex_unlock(&thread_lock);
113 while (1) { /* handle spurious wakeups */
114 if (futex_wait(&futex, 0, NULL, futex_flag) != EINTR)
122 static void block_threads(pthread_t *w, pthread_attr_t thread_attr)
127 threads_starting = nblocked_threads;
129 /* create and block all threads */
130 for (i = 0; i < nblocked_threads; i++) {
132 CPU_SET(i % ncpus, &cpu);
134 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
135 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
137 if (pthread_create(&w[i], &thread_attr, blocked_workerfn, NULL))
138 err(EXIT_FAILURE, "pthread_create");
142 static void print_run(struct thread_data *waking_worker, unsigned int run_num)
144 unsigned int i, wakeup_avg;
145 double waketime_avg, waketime_stddev;
146 struct stats __waketime_stats, __wakeup_stats;
148 init_stats(&__wakeup_stats);
149 init_stats(&__waketime_stats);
151 for (i = 0; i < nwaking_threads; i++) {
152 update_stats(&__waketime_stats, waking_worker[i].runtime.tv_usec);
153 update_stats(&__wakeup_stats, waking_worker[i].nwoken);
156 waketime_avg = avg_stats(&__waketime_stats);
157 waketime_stddev = stddev_stats(&__waketime_stats);
158 wakeup_avg = avg_stats(&__wakeup_stats);
160 printf("[Run %d]: Avg per-thread latency (waking %d/%d threads) "
161 "in %.4f ms (+-%.2f%%)\n", run_num + 1, wakeup_avg,
162 nblocked_threads, waketime_avg / USEC_PER_MSEC,
163 rel_stddev_stats(waketime_stddev, waketime_avg));
166 static void print_summary(void)
168 unsigned int wakeup_avg;
169 double waketime_avg, waketime_stddev;
171 waketime_avg = avg_stats(&waketime_stats);
172 waketime_stddev = stddev_stats(&waketime_stats);
173 wakeup_avg = avg_stats(&wakeup_stats);
175 printf("Avg per-thread latency (waking %d/%d threads) in %.4f ms (+-%.2f%%)\n",
178 waketime_avg / USEC_PER_MSEC,
179 rel_stddev_stats(waketime_stddev, waketime_avg));
183 static void do_run_stats(struct thread_data *waking_worker)
187 for (i = 0; i < nwaking_threads; i++) {
188 update_stats(&waketime_stats, waking_worker[i].runtime.tv_usec);
189 update_stats(&wakeup_stats, waking_worker[i].nwoken);
194 static void toggle_done(int sig __maybe_unused,
195 siginfo_t *info __maybe_unused,
196 void *uc __maybe_unused)
201 int bench_futex_wake_parallel(int argc, const char **argv)
205 struct sigaction act;
206 pthread_attr_t thread_attr;
207 struct thread_data *waking_worker;
209 argc = parse_options(argc, argv, options,
210 bench_futex_wake_parallel_usage, 0);
212 usage_with_options(bench_futex_wake_parallel_usage, options);
216 sigfillset(&act.sa_mask);
217 act.sa_sigaction = toggle_done;
218 sigaction(SIGINT, &act, NULL);
220 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
221 if (!nblocked_threads)
222 nblocked_threads = ncpus;
224 /* some sanity checks */
225 if (nwaking_threads > nblocked_threads || !nwaking_threads)
226 nwaking_threads = nblocked_threads;
228 if (nblocked_threads % nwaking_threads)
229 errx(EXIT_FAILURE, "Must be perfectly divisible");
231 * Each thread will wakeup nwakes tasks in
232 * a single futex_wait call.
234 nwakes = nblocked_threads/nwaking_threads;
236 blocked_worker = calloc(nblocked_threads, sizeof(*blocked_worker));
238 err(EXIT_FAILURE, "calloc");
241 futex_flag = FUTEX_PRIVATE_FLAG;
243 printf("Run summary [PID %d]: blocking on %d threads (at [%s] "
244 "futex %p), %d threads waking up %d at a time.\n\n",
245 getpid(), nblocked_threads, fshared ? "shared":"private",
246 &futex, nwaking_threads, nwakes);
248 init_stats(&wakeup_stats);
249 init_stats(&waketime_stats);
251 pthread_attr_init(&thread_attr);
252 pthread_mutex_init(&thread_lock, NULL);
253 pthread_cond_init(&thread_parent, NULL);
254 pthread_cond_init(&thread_worker, NULL);
256 for (j = 0; j < bench_repeat && !done; j++) {
257 waking_worker = calloc(nwaking_threads, sizeof(*waking_worker));
259 err(EXIT_FAILURE, "calloc");
261 /* create, launch & block all threads */
262 block_threads(blocked_worker, thread_attr);
264 /* make sure all threads are already blocked */
265 pthread_mutex_lock(&thread_lock);
266 while (threads_starting)
267 pthread_cond_wait(&thread_parent, &thread_lock);
268 pthread_cond_broadcast(&thread_worker);
269 pthread_mutex_unlock(&thread_lock);
273 /* Ok, all threads are patiently blocked, start waking folks up */
274 wakeup_threads(waking_worker, thread_attr);
276 for (i = 0; i < nblocked_threads; i++) {
277 ret = pthread_join(blocked_worker[i], NULL);
279 err(EXIT_FAILURE, "pthread_join");
282 do_run_stats(waking_worker);
284 print_run(waking_worker, j);
289 /* cleanup & report results */
290 pthread_cond_destroy(&thread_parent);
291 pthread_cond_destroy(&thread_worker);
292 pthread_mutex_destroy(&thread_lock);
293 pthread_attr_destroy(&thread_attr);
297 free(blocked_worker);