4 * futex-wake: Block a bunch of threads on a futex and wake'em up, N at a time.
6 * This program is particularly useful to measure the latency of nthread wakeups
7 * in non-error situations: all waiters are queued and all wake calls wakeup
8 * one or more tasks, and thus the waitqueue is never empty.
11 /* For the CLR_() macros */
15 #include "../util/stat.h"
16 #include <subcmd/parse-options.h>
17 #include <linux/compiler.h>
18 #include <linux/kernel.h>
19 #include <linux/time64.h>
28 /* all threads will block on the same futex */
29 static u_int32_t futex1 = 0;
32 * How many wakeups to do at a time.
33 * Default to 1 in order to make the kernel work more.
35 static unsigned int nwakes = 1;
38 static bool done = false, silent = false, fshared = false;
39 static pthread_mutex_t thread_lock;
40 static pthread_cond_t thread_parent, thread_worker;
41 static struct stats waketime_stats, wakeup_stats;
42 static unsigned int ncpus, threads_starting, nthreads = 0;
43 static int futex_flag = 0;
45 static const struct option options[] = {
46 OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
47 OPT_UINTEGER('w', "nwakes", &nwakes, "Specify amount of threads to wake at once"),
48 OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
49 OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"),
53 static const char * const bench_futex_wake_usage[] = {
54 "perf bench futex wake <options>",
58 static void *workerfn(void *arg __maybe_unused)
60 pthread_mutex_lock(&thread_lock);
62 if (!threads_starting)
63 pthread_cond_signal(&thread_parent);
64 pthread_cond_wait(&thread_worker, &thread_lock);
65 pthread_mutex_unlock(&thread_lock);
68 if (futex_wait(&futex1, 0, NULL, futex_flag) != EINTR)
76 static void print_summary(void)
78 double waketime_avg = avg_stats(&waketime_stats);
79 double waketime_stddev = stddev_stats(&waketime_stats);
80 unsigned int wakeup_avg = avg_stats(&wakeup_stats);
82 printf("Wokeup %d of %d threads in %.4f ms (+-%.2f%%)\n",
85 waketime_avg / USEC_PER_MSEC,
86 rel_stddev_stats(waketime_stddev, waketime_avg));
89 static void block_threads(pthread_t *w,
90 pthread_attr_t thread_attr)
95 threads_starting = nthreads;
97 /* create and block all threads */
98 for (i = 0; i < nthreads; i++) {
100 CPU_SET(i % ncpus, &cpu);
102 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
103 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
105 if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
106 err(EXIT_FAILURE, "pthread_create");
110 static void toggle_done(int sig __maybe_unused,
111 siginfo_t *info __maybe_unused,
112 void *uc __maybe_unused)
117 int bench_futex_wake(int argc, const char **argv,
118 const char *prefix __maybe_unused)
122 struct sigaction act;
123 pthread_attr_t thread_attr;
125 argc = parse_options(argc, argv, options, bench_futex_wake_usage, 0);
127 usage_with_options(bench_futex_wake_usage, options);
131 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
133 sigfillset(&act.sa_mask);
134 act.sa_sigaction = toggle_done;
135 sigaction(SIGINT, &act, NULL);
140 worker = calloc(nthreads, sizeof(*worker));
142 err(EXIT_FAILURE, "calloc");
145 futex_flag = FUTEX_PRIVATE_FLAG;
147 printf("Run summary [PID %d]: blocking on %d threads (at [%s] futex %p), "
148 "waking up %d at a time.\n\n",
149 getpid(), nthreads, fshared ? "shared":"private", &futex1, nwakes);
151 init_stats(&wakeup_stats);
152 init_stats(&waketime_stats);
153 pthread_attr_init(&thread_attr);
154 pthread_mutex_init(&thread_lock, NULL);
155 pthread_cond_init(&thread_parent, NULL);
156 pthread_cond_init(&thread_worker, NULL);
158 for (j = 0; j < bench_repeat && !done; j++) {
159 unsigned int nwoken = 0;
160 struct timeval start, end, runtime;
162 /* create, launch & block all threads */
163 block_threads(worker, thread_attr);
165 /* make sure all threads are already blocked */
166 pthread_mutex_lock(&thread_lock);
167 while (threads_starting)
168 pthread_cond_wait(&thread_parent, &thread_lock);
169 pthread_cond_broadcast(&thread_worker);
170 pthread_mutex_unlock(&thread_lock);
174 /* Ok, all threads are patiently blocked, start waking folks up */
175 gettimeofday(&start, NULL);
176 while (nwoken != nthreads)
177 nwoken += futex_wake(&futex1, nwakes, futex_flag);
178 gettimeofday(&end, NULL);
179 timersub(&end, &start, &runtime);
181 update_stats(&wakeup_stats, nwoken);
182 update_stats(&waketime_stats, runtime.tv_usec);
185 printf("[Run %d]: Wokeup %d of %d threads in %.4f ms\n",
186 j + 1, nwoken, nthreads, runtime.tv_usec / (double)USEC_PER_MSEC);
189 for (i = 0; i < nthreads; i++) {
190 ret = pthread_join(worker[i], NULL);
192 err(EXIT_FAILURE, "pthread_join");
197 /* cleanup & report results */
198 pthread_cond_destroy(&thread_parent);
199 pthread_cond_destroy(&thread_worker);
200 pthread_mutex_destroy(&thread_lock);
201 pthread_attr_destroy(&thread_attr);