2 * Copyright (C) 2013 Davidlohr Bueso <davidlohr@hp.com>
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.
12 #include "../util/util.h"
13 #include "../util/stat.h"
14 #include <subcmd/parse-options.h>
15 #include "../util/header.h"
24 /* all threads will block on the same futex */
25 static u_int32_t futex1
= 0;
28 * How many wakeups to do at a time.
29 * Default to 1 in order to make the kernel work more.
31 static unsigned int nwakes
= 1;
34 static bool done
= false, silent
= false, fshared
= false;
35 static pthread_mutex_t thread_lock
;
36 static pthread_cond_t thread_parent
, thread_worker
;
37 static struct stats waketime_stats
, wakeup_stats
;
38 static unsigned int ncpus
, threads_starting
, nthreads
= 0;
39 static int futex_flag
= 0;
41 static const struct option options
[] = {
42 OPT_UINTEGER('t', "threads", &nthreads
, "Specify amount of threads"),
43 OPT_UINTEGER('w', "nwakes", &nwakes
, "Specify amount of threads to wake at once"),
44 OPT_BOOLEAN( 's', "silent", &silent
, "Silent mode: do not display data/details"),
45 OPT_BOOLEAN( 'S', "shared", &fshared
, "Use shared futexes instead of private ones"),
49 static const char * const bench_futex_wake_usage
[] = {
50 "perf bench futex wake <options>",
54 static void *workerfn(void *arg __maybe_unused
)
56 pthread_mutex_lock(&thread_lock
);
58 if (!threads_starting
)
59 pthread_cond_signal(&thread_parent
);
60 pthread_cond_wait(&thread_worker
, &thread_lock
);
61 pthread_mutex_unlock(&thread_lock
);
64 if (futex_wait(&futex1
, 0, NULL
, futex_flag
) != EINTR
)
72 static void print_summary(void)
74 double waketime_avg
= avg_stats(&waketime_stats
);
75 double waketime_stddev
= stddev_stats(&waketime_stats
);
76 unsigned int wakeup_avg
= avg_stats(&wakeup_stats
);
78 printf("Wokeup %d of %d threads in %.4f ms (+-%.2f%%)\n",
82 rel_stddev_stats(waketime_stddev
, waketime_avg
));
85 static void block_threads(pthread_t
*w
,
86 pthread_attr_t thread_attr
)
91 threads_starting
= nthreads
;
93 /* create and block all threads */
94 for (i
= 0; i
< nthreads
; i
++) {
96 CPU_SET(i
% ncpus
, &cpu
);
98 if (pthread_attr_setaffinity_np(&thread_attr
, sizeof(cpu_set_t
), &cpu
))
99 err(EXIT_FAILURE
, "pthread_attr_setaffinity_np");
101 if (pthread_create(&w
[i
], &thread_attr
, workerfn
, NULL
))
102 err(EXIT_FAILURE
, "pthread_create");
106 static void toggle_done(int sig __maybe_unused
,
107 siginfo_t
*info __maybe_unused
,
108 void *uc __maybe_unused
)
113 int bench_futex_wake(int argc
, const char **argv
,
114 const char *prefix __maybe_unused
)
118 struct sigaction act
;
119 pthread_attr_t thread_attr
;
121 argc
= parse_options(argc
, argv
, options
, bench_futex_wake_usage
, 0);
123 usage_with_options(bench_futex_wake_usage
, options
);
127 ncpus
= sysconf(_SC_NPROCESSORS_ONLN
);
129 sigfillset(&act
.sa_mask
);
130 act
.sa_sigaction
= toggle_done
;
131 sigaction(SIGINT
, &act
, NULL
);
136 worker
= calloc(nthreads
, sizeof(*worker
));
138 err(EXIT_FAILURE
, "calloc");
141 futex_flag
= FUTEX_PRIVATE_FLAG
;
143 printf("Run summary [PID %d]: blocking on %d threads (at [%s] futex %p), "
144 "waking up %d at a time.\n\n",
145 getpid(), nthreads
, fshared
? "shared":"private", &futex1
, nwakes
);
147 init_stats(&wakeup_stats
);
148 init_stats(&waketime_stats
);
149 pthread_attr_init(&thread_attr
);
150 pthread_mutex_init(&thread_lock
, NULL
);
151 pthread_cond_init(&thread_parent
, NULL
);
152 pthread_cond_init(&thread_worker
, NULL
);
154 for (j
= 0; j
< bench_repeat
&& !done
; j
++) {
155 unsigned int nwoken
= 0;
156 struct timeval start
, end
, runtime
;
158 /* create, launch & block all threads */
159 block_threads(worker
, thread_attr
);
161 /* make sure all threads are already blocked */
162 pthread_mutex_lock(&thread_lock
);
163 while (threads_starting
)
164 pthread_cond_wait(&thread_parent
, &thread_lock
);
165 pthread_cond_broadcast(&thread_worker
);
166 pthread_mutex_unlock(&thread_lock
);
170 /* Ok, all threads are patiently blocked, start waking folks up */
171 gettimeofday(&start
, NULL
);
172 while (nwoken
!= nthreads
)
173 nwoken
+= futex_wake(&futex1
, nwakes
, futex_flag
);
174 gettimeofday(&end
, NULL
);
175 timersub(&end
, &start
, &runtime
);
177 update_stats(&wakeup_stats
, nwoken
);
178 update_stats(&waketime_stats
, runtime
.tv_usec
);
181 printf("[Run %d]: Wokeup %d of %d threads in %.4f ms\n",
182 j
+ 1, nwoken
, nthreads
, runtime
.tv_usec
/1e3
);
185 for (i
= 0; i
< nthreads
; i
++) {
186 ret
= pthread_join(worker
[i
], NULL
);
188 err(EXIT_FAILURE
, "pthread_join");
193 /* cleanup & report results */
194 pthread_cond_destroy(&thread_parent
);
195 pthread_cond_destroy(&thread_worker
);
196 pthread_mutex_destroy(&thread_lock
);
197 pthread_attr_destroy(&thread_attr
);