xref: /linux/tools/perf/bench/futex-wake.c (revision b6ebbac51bedf9e98e837688bc838f400196da5e)
1 /*
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 
11 /* For the CLR_() macros */
12 #include <pthread.h>
13 
14 #include <signal.h>
15 #include "../util/stat.h"
16 #include <subcmd/parse-options.h>
17 #include <linux/compiler.h>
18 #include <linux/kernel.h>
19 #include <errno.h>
20 #include "bench.h"
21 #include "futex.h"
22 
23 #include <err.h>
24 #include <stdlib.h>
25 #include <sys/time.h>
26 
27 /* all threads will block on the same futex */
28 static u_int32_t futex1 = 0;
29 
30 /*
31  * How many wakeups to do at a time.
32  * Default to 1 in order to make the kernel work more.
33  */
34 static unsigned int nwakes = 1;
35 
36 pthread_t *worker;
37 static bool done = false, silent = false, fshared = false;
38 static pthread_mutex_t thread_lock;
39 static pthread_cond_t thread_parent, thread_worker;
40 static struct stats waketime_stats, wakeup_stats;
41 static unsigned int ncpus, threads_starting, nthreads = 0;
42 static int futex_flag = 0;
43 
44 static const struct option options[] = {
45 	OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
46 	OPT_UINTEGER('w', "nwakes",  &nwakes,   "Specify amount of threads to wake at once"),
47 	OPT_BOOLEAN( 's', "silent",  &silent,   "Silent mode: do not display data/details"),
48 	OPT_BOOLEAN( 'S', "shared",  &fshared,  "Use shared futexes instead of private ones"),
49 	OPT_END()
50 };
51 
52 static const char * const bench_futex_wake_usage[] = {
53 	"perf bench futex wake <options>",
54 	NULL
55 };
56 
57 static void *workerfn(void *arg __maybe_unused)
58 {
59 	pthread_mutex_lock(&thread_lock);
60 	threads_starting--;
61 	if (!threads_starting)
62 		pthread_cond_signal(&thread_parent);
63 	pthread_cond_wait(&thread_worker, &thread_lock);
64 	pthread_mutex_unlock(&thread_lock);
65 
66 	while (1) {
67 		if (futex_wait(&futex1, 0, NULL, futex_flag) != EINTR)
68 			break;
69 	}
70 
71 	pthread_exit(NULL);
72 	return NULL;
73 }
74 
75 static void print_summary(void)
76 {
77 	double waketime_avg = avg_stats(&waketime_stats);
78 	double waketime_stddev = stddev_stats(&waketime_stats);
79 	unsigned int wakeup_avg = avg_stats(&wakeup_stats);
80 
81 	printf("Wokeup %d of %d threads in %.4f ms (+-%.2f%%)\n",
82 	       wakeup_avg,
83 	       nthreads,
84 	       waketime_avg/1e3,
85 	       rel_stddev_stats(waketime_stddev, waketime_avg));
86 }
87 
88 static void block_threads(pthread_t *w,
89 			  pthread_attr_t thread_attr)
90 {
91 	cpu_set_t cpu;
92 	unsigned int i;
93 
94 	threads_starting = nthreads;
95 
96 	/* create and block all threads */
97 	for (i = 0; i < nthreads; i++) {
98 		CPU_ZERO(&cpu);
99 		CPU_SET(i % ncpus, &cpu);
100 
101 		if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
102 			err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
103 
104 		if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
105 			err(EXIT_FAILURE, "pthread_create");
106 	}
107 }
108 
109 static void toggle_done(int sig __maybe_unused,
110 			siginfo_t *info __maybe_unused,
111 			void *uc __maybe_unused)
112 {
113 	done = true;
114 }
115 
116 int bench_futex_wake(int argc, const char **argv,
117 		     const char *prefix __maybe_unused)
118 {
119 	int ret = 0;
120 	unsigned int i, j;
121 	struct sigaction act;
122 	pthread_attr_t thread_attr;
123 
124 	argc = parse_options(argc, argv, options, bench_futex_wake_usage, 0);
125 	if (argc) {
126 		usage_with_options(bench_futex_wake_usage, options);
127 		exit(EXIT_FAILURE);
128 	}
129 
130 	ncpus = sysconf(_SC_NPROCESSORS_ONLN);
131 
132 	sigfillset(&act.sa_mask);
133 	act.sa_sigaction = toggle_done;
134 	sigaction(SIGINT, &act, NULL);
135 
136 	if (!nthreads)
137 		nthreads = ncpus;
138 
139 	worker = calloc(nthreads, sizeof(*worker));
140 	if (!worker)
141 		err(EXIT_FAILURE, "calloc");
142 
143 	if (!fshared)
144 		futex_flag = FUTEX_PRIVATE_FLAG;
145 
146 	printf("Run summary [PID %d]: blocking on %d threads (at [%s] futex %p), "
147 	       "waking up %d at a time.\n\n",
148 	       getpid(), nthreads, fshared ? "shared":"private",  &futex1, nwakes);
149 
150 	init_stats(&wakeup_stats);
151 	init_stats(&waketime_stats);
152 	pthread_attr_init(&thread_attr);
153 	pthread_mutex_init(&thread_lock, NULL);
154 	pthread_cond_init(&thread_parent, NULL);
155 	pthread_cond_init(&thread_worker, NULL);
156 
157 	for (j = 0; j < bench_repeat && !done; j++) {
158 		unsigned int nwoken = 0;
159 		struct timeval start, end, runtime;
160 
161 		/* create, launch & block all threads */
162 		block_threads(worker, thread_attr);
163 
164 		/* make sure all threads are already blocked */
165 		pthread_mutex_lock(&thread_lock);
166 		while (threads_starting)
167 			pthread_cond_wait(&thread_parent, &thread_lock);
168 		pthread_cond_broadcast(&thread_worker);
169 		pthread_mutex_unlock(&thread_lock);
170 
171 		usleep(100000);
172 
173 		/* Ok, all threads are patiently blocked, start waking folks up */
174 		gettimeofday(&start, NULL);
175 		while (nwoken != nthreads)
176 			nwoken += futex_wake(&futex1, nwakes, futex_flag);
177 		gettimeofday(&end, NULL);
178 		timersub(&end, &start, &runtime);
179 
180 		update_stats(&wakeup_stats, nwoken);
181 		update_stats(&waketime_stats, runtime.tv_usec);
182 
183 		if (!silent) {
184 			printf("[Run %d]: Wokeup %d of %d threads in %.4f ms\n",
185 			       j + 1, nwoken, nthreads, runtime.tv_usec/1e3);
186 		}
187 
188 		for (i = 0; i < nthreads; i++) {
189 			ret = pthread_join(worker[i], NULL);
190 			if (ret)
191 				err(EXIT_FAILURE, "pthread_join");
192 		}
193 
194 	}
195 
196 	/* cleanup & report results */
197 	pthread_cond_destroy(&thread_parent);
198 	pthread_cond_destroy(&thread_worker);
199 	pthread_mutex_destroy(&thread_lock);
200 	pthread_attr_destroy(&thread_attr);
201 
202 	print_summary();
203 
204 	free(worker);
205 	return ret;
206 }
207