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