blob: e246b1b8388a3fd3bfc7c9c398a90efc26d86c79 [file] [log] [blame]
Davidlohr Bueso27db7832013-12-14 20:31:56 -08001/*
2 * Copyright (C) 2013 Davidlohr Bueso <davidlohr@hp.com>
3 *
4 * futex-wake: Block a bunch of threads on a futex and wake'em up, N at a time.
5 *
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.
9 */
10
Arnaldo Carvalho de Melo8a158582016-07-06 12:14:56 -030011/* For the CLR_() macros */
12#include <pthread.h>
13
Arnaldo Carvalho de Melo9c304f62016-07-07 11:01:46 -030014#include <signal.h>
Davidlohr Bueso27db7832013-12-14 20:31:56 -080015#include "../util/stat.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060016#include <subcmd/parse-options.h>
Arnaldo Carvalho de Melo86695f52016-07-11 10:05:52 -030017#include <linux/compiler.h>
Arnaldo Carvalho de Melo9c304f62016-07-07 11:01:46 -030018#include <linux/kernel.h>
Arnaldo Carvalho de Melo565e6912016-08-08 15:35:21 -030019#include <linux/time64.h>
Arnaldo Carvalho de Melo9c304f62016-07-07 11:01:46 -030020#include <errno.h>
Davidlohr Bueso27db7832013-12-14 20:31:56 -080021#include "bench.h"
22#include "futex.h"
23
24#include <err.h>
25#include <stdlib.h>
26#include <sys/time.h>
Davidlohr Bueso27db7832013-12-14 20:31:56 -080027
28/* all threads will block on the same futex */
29static u_int32_t futex1 = 0;
30
31/*
32 * How many wakeups to do at a time.
33 * Default to 1 in order to make the kernel work more.
34 */
35static unsigned int nwakes = 1;
36
Davidlohr Bueso27db7832013-12-14 20:31:56 -080037pthread_t *worker;
Davidlohr Bueso86c87e12014-09-29 09:41:07 -070038static bool done = false, silent = false, fshared = false;
Davidlohr Bueso27db7832013-12-14 20:31:56 -080039static pthread_mutex_t thread_lock;
40static pthread_cond_t thread_parent, thread_worker;
41static struct stats waketime_stats, wakeup_stats;
42static unsigned int ncpus, threads_starting, nthreads = 0;
Davidlohr Bueso86c87e12014-09-29 09:41:07 -070043static int futex_flag = 0;
Davidlohr Bueso27db7832013-12-14 20:31:56 -080044
45static 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"),
Davidlohr Bueso27db7832013-12-14 20:31:56 -080048 OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
Davidlohr Bueso86c87e12014-09-29 09:41:07 -070049 OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"),
Davidlohr Bueso27db7832013-12-14 20:31:56 -080050 OPT_END()
51};
52
53static const char * const bench_futex_wake_usage[] = {
54 "perf bench futex wake <options>",
55 NULL
56};
57
58static void *workerfn(void *arg __maybe_unused)
59{
60 pthread_mutex_lock(&thread_lock);
61 threads_starting--;
62 if (!threads_starting)
63 pthread_cond_signal(&thread_parent);
64 pthread_cond_wait(&thread_worker, &thread_lock);
65 pthread_mutex_unlock(&thread_lock);
66
Davidlohr Bueso598adc52015-05-08 11:38:00 -070067 while (1) {
68 if (futex_wait(&futex1, 0, NULL, futex_flag) != EINTR)
69 break;
70 }
71
72 pthread_exit(NULL);
Davidlohr Bueso27db7832013-12-14 20:31:56 -080073 return NULL;
74}
75
76static void print_summary(void)
77{
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);
81
82 printf("Wokeup %d of %d threads in %.4f ms (+-%.2f%%)\n",
83 wakeup_avg,
84 nthreads,
Arnaldo Carvalho de Melo565e6912016-08-08 15:35:21 -030085 waketime_avg / USEC_PER_MSEC,
Davidlohr Bueso27db7832013-12-14 20:31:56 -080086 rel_stddev_stats(waketime_stddev, waketime_avg));
87}
88
89static void block_threads(pthread_t *w,
90 pthread_attr_t thread_attr)
91{
92 cpu_set_t cpu;
93 unsigned int i;
94
95 threads_starting = nthreads;
96
97 /* create and block all threads */
98 for (i = 0; i < nthreads; i++) {
99 CPU_ZERO(&cpu);
100 CPU_SET(i % ncpus, &cpu);
101
102 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
103 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
104
105 if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
106 err(EXIT_FAILURE, "pthread_create");
107 }
108}
109
110static void toggle_done(int sig __maybe_unused,
111 siginfo_t *info __maybe_unused,
112 void *uc __maybe_unused)
113{
114 done = true;
115}
116
117int bench_futex_wake(int argc, const char **argv,
118 const char *prefix __maybe_unused)
119{
120 int ret = 0;
121 unsigned int i, j;
122 struct sigaction act;
123 pthread_attr_t thread_attr;
124
125 argc = parse_options(argc, argv, options, bench_futex_wake_usage, 0);
126 if (argc) {
127 usage_with_options(bench_futex_wake_usage, options);
128 exit(EXIT_FAILURE);
129 }
130
131 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
132
133 sigfillset(&act.sa_mask);
134 act.sa_sigaction = toggle_done;
135 sigaction(SIGINT, &act, NULL);
136
137 if (!nthreads)
138 nthreads = ncpus;
139
140 worker = calloc(nthreads, sizeof(*worker));
141 if (!worker)
142 err(EXIT_FAILURE, "calloc");
143
Davidlohr Bueso86c87e12014-09-29 09:41:07 -0700144 if (!fshared)
145 futex_flag = FUTEX_PRIVATE_FLAG;
146
147 printf("Run summary [PID %d]: blocking on %d threads (at [%s] futex %p), "
Davidlohr Bueso27db7832013-12-14 20:31:56 -0800148 "waking up %d at a time.\n\n",
Davidlohr Bueso86c87e12014-09-29 09:41:07 -0700149 getpid(), nthreads, fshared ? "shared":"private", &futex1, nwakes);
Davidlohr Bueso27db7832013-12-14 20:31:56 -0800150
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);
157
Davidlohr Buesod9de84a2014-06-16 11:14:23 -0700158 for (j = 0; j < bench_repeat && !done; j++) {
Davidlohr Bueso27db7832013-12-14 20:31:56 -0800159 unsigned int nwoken = 0;
160 struct timeval start, end, runtime;
161
162 /* create, launch & block all threads */
163 block_threads(worker, thread_attr);
164
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);
171
172 usleep(100000);
173
174 /* Ok, all threads are patiently blocked, start waking folks up */
175 gettimeofday(&start, NULL);
176 while (nwoken != nthreads)
Davidlohr Bueso86c87e12014-09-29 09:41:07 -0700177 nwoken += futex_wake(&futex1, nwakes, futex_flag);
Davidlohr Bueso27db7832013-12-14 20:31:56 -0800178 gettimeofday(&end, NULL);
179 timersub(&end, &start, &runtime);
180
181 update_stats(&wakeup_stats, nwoken);
182 update_stats(&waketime_stats, runtime.tv_usec);
183
184 if (!silent) {
185 printf("[Run %d]: Wokeup %d of %d threads in %.4f ms\n",
Arnaldo Carvalho de Melo565e6912016-08-08 15:35:21 -0300186 j + 1, nwoken, nthreads, runtime.tv_usec / (double)USEC_PER_MSEC);
Davidlohr Bueso27db7832013-12-14 20:31:56 -0800187 }
188
189 for (i = 0; i < nthreads; i++) {
190 ret = pthread_join(worker[i], NULL);
191 if (ret)
192 err(EXIT_FAILURE, "pthread_join");
193 }
194
195 }
196
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);
202
203 print_summary();
204
205 free(worker);
206 return ret;
207}