File Index
Files and directories
Sign in
FILE

futex-requeue.c

File preview with syntax highlighting.

linux-6.19.14/tools/perf/bench/futex-requeue.c C
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) 2013  Davidlohr Bueso <davidlohr@hp.com>
  4 *
  5 * futex-requeue: Block a bunch of threads on futex1 and requeue them
  6 *                on futex2, N at a time.
  7 *
  8 * This program is particularly useful to measure the latency of nthread
  9 * requeues without waking up any tasks (in the non-pi case) -- thus
 10 * mimicking a regular futex_wait.
 11 */
 12
 13/* For the CLR_() macros */
 14#include <string.h>
 15#include <pthread.h>
 16
 17#include <signal.h>
 18#include "../util/mutex.h"
 19#include "../util/stat.h"
 20#include <subcmd/parse-options.h>
 21#include <linux/compiler.h>
 22#include <linux/kernel.h>
 23#include <linux/time64.h>
 24#include <errno.h>
 25#include <perf/cpumap.h>
 26#include "bench.h"
 27#include "futex.h"
 28
 29#include <err.h>
 30#include <stdlib.h>
 31#include <sys/time.h>
 32#include <sys/mman.h>
 33
 34static u_int32_t futex1 = 0, futex2 = 0;
 35
 36static pthread_t *worker;
 37static bool done = false;
 38static struct mutex thread_lock;
 39static struct cond thread_parent, thread_worker;
 40static struct stats requeuetime_stats, requeued_stats;
 41static unsigned int threads_starting;
 42static int futex_flag = 0;
 43
 44static struct bench_futex_parameters params = {
 45	.nbuckets = -1,
 46	/*
 47	 * How many tasks to requeue at a time.
 48	 * Default to 1 in order to make the kernel work more.
 49	 */
 50	.nrequeue = 1,
 51};
 52
 53static const struct option options[] = {
 54	OPT_INTEGER( 'b', "buckets", &params.nbuckets, "Specify amount of hash buckets"),
 55	OPT_UINTEGER('t', "threads",  &params.nthreads, "Specify amount of threads"),
 56	OPT_UINTEGER('q', "nrequeue", &params.nrequeue, "Specify amount of threads to requeue at once"),
 57	OPT_BOOLEAN( 's', "silent",   &params.silent, "Silent mode: do not display data/details"),
 58	OPT_BOOLEAN( 'S', "shared",   &params.fshared, "Use shared futexes instead of private ones"),
 59	OPT_BOOLEAN( 'm', "mlockall", &params.mlockall, "Lock all current and future memory"),
 60	OPT_BOOLEAN( 'B', "broadcast", &params.broadcast, "Requeue all threads at once"),
 61	OPT_BOOLEAN( 'p', "pi", &params.pi, "Use PI-aware variants of FUTEX_CMP_REQUEUE"),
 62
 63	OPT_END()
 64};
 65
 66static const char * const bench_futex_requeue_usage[] = {
 67	"perf bench futex requeue <options>",
 68	NULL
 69};
 70
 71static void print_summary(void)
 72{
 73	double requeuetime_avg = avg_stats(&requeuetime_stats);
 74	double requeuetime_stddev = stddev_stats(&requeuetime_stats);
 75	unsigned int requeued_avg = avg_stats(&requeued_stats);
 76
 77	printf("Requeued %d of %d threads in %.4f ms (+-%.2f%%)\n",
 78	       requeued_avg,
 79	       params.nthreads,
 80	       requeuetime_avg / USEC_PER_MSEC,
 81	       rel_stddev_stats(requeuetime_stddev, requeuetime_avg));
 82	futex_print_nbuckets(&params);
 83}
 84
 85static void *workerfn(void *arg __maybe_unused)
 86{
 87	int ret;
 88
 89	mutex_lock(&thread_lock);
 90	threads_starting--;
 91	if (!threads_starting)
 92		cond_signal(&thread_parent);
 93	cond_wait(&thread_worker, &thread_lock);
 94	mutex_unlock(&thread_lock);
 95
 96	while (1) {
 97		if (!params.pi) {
 98			ret = futex_wait(&futex1, 0, NULL, futex_flag);
 99			if (!ret)
100				break;
101
102			if (ret && errno != EAGAIN) {
103				if (!params.silent)
104					warnx("futex_wait");
105				break;
106			}
107		} else {
108			ret = futex_wait_requeue_pi(&futex1, 0, &futex2,
109						    NULL, futex_flag);
110			if (!ret) {
111				/* got the lock at futex2 */
112				futex_unlock_pi(&futex2, futex_flag);
113				break;
114			}
115
116			if (ret && errno != EAGAIN) {
117				if (!params.silent)
118					warnx("futex_wait_requeue_pi");
119				break;
120			}
121		}
122	}
123
124	return NULL;
125}
126
127static void block_threads(pthread_t *w, struct perf_cpu_map *cpu)
128{
129	cpu_set_t *cpuset;
130	unsigned int i;
131	int nrcpus = cpu__max_cpu().cpu;
132	size_t size;
133
134	threads_starting = params.nthreads;
135
136	cpuset = CPU_ALLOC(nrcpus);
137	BUG_ON(!cpuset);
138	size = CPU_ALLOC_SIZE(nrcpus);
139
140	/* create and block all threads */
141	for (i = 0; i < params.nthreads; i++) {
142		pthread_attr_t thread_attr;
143
144		pthread_attr_init(&thread_attr);
145		CPU_ZERO_S(size, cpuset);
146		CPU_SET_S(perf_cpu_map__cpu(cpu, i % perf_cpu_map__nr(cpu)).cpu, size, cpuset);
147
148		if (pthread_attr_setaffinity_np(&thread_attr, size, cpuset)) {
149			CPU_FREE(cpuset);
150			err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
151		}
152
153		if (pthread_create(&w[i], &thread_attr, workerfn, NULL)) {
154			CPU_FREE(cpuset);
155			err(EXIT_FAILURE, "pthread_create");
156		}
157		pthread_attr_destroy(&thread_attr);
158	}
159	CPU_FREE(cpuset);
160}
161
162static void toggle_done(int sig __maybe_unused,
163			siginfo_t *info __maybe_unused,
164			void *uc __maybe_unused)
165{
166	done = true;
167}
168
169int bench_futex_requeue(int argc, const char **argv)
170{
171	int ret = 0;
172	unsigned int i, j;
173	struct sigaction act;
174	struct perf_cpu_map *cpu;
175
176	argc = parse_options(argc, argv, options, bench_futex_requeue_usage, 0);
177	if (argc)
178		goto err;
179
180	cpu = perf_cpu_map__new_online_cpus();
181	if (!cpu)
182		err(EXIT_FAILURE, "cpu_map__new");
183
184	memset(&act, 0, sizeof(act));
185	sigfillset(&act.sa_mask);
186	act.sa_sigaction = toggle_done;
187	sigaction(SIGINT, &act, NULL);
188
189	if (params.mlockall) {
190		if (mlockall(MCL_CURRENT | MCL_FUTURE))
191			err(EXIT_FAILURE, "mlockall");
192	}
193
194	if (!params.nthreads)
195		params.nthreads = perf_cpu_map__nr(cpu);
196
197	worker = calloc(params.nthreads, sizeof(*worker));
198	if (!worker)
199		err(EXIT_FAILURE, "calloc");
200
201	if (!params.fshared)
202		futex_flag = FUTEX_PRIVATE_FLAG;
203
204	if (params.nrequeue > params.nthreads)
205		params.nrequeue = params.nthreads;
206
207	if (params.broadcast)
208		params.nrequeue = params.nthreads;
209
210	futex_set_nbuckets_param(&params);
211
212	printf("Run summary [PID %d]: Requeuing %d threads (from [%s] %p to %s%p), "
213	       "%d at a time.\n\n",  getpid(), params.nthreads,
214	       params.fshared ? "shared":"private", &futex1,
215	       params.pi ? "PI ": "", &futex2, params.nrequeue);
216
217	init_stats(&requeued_stats);
218	init_stats(&requeuetime_stats);
219	mutex_init(&thread_lock);
220	cond_init(&thread_parent);
221	cond_init(&thread_worker);
222
223	for (j = 0; j < bench_repeat && !done; j++) {
224		unsigned int nrequeued = 0, wakeups = 0;
225		struct timeval start, end, runtime;
226
227		/* create, launch & block all threads */
228		block_threads(worker, cpu);
229
230		/* make sure all threads are already blocked */
231		mutex_lock(&thread_lock);
232		while (threads_starting)
233			cond_wait(&thread_parent, &thread_lock);
234		cond_broadcast(&thread_worker);
235		mutex_unlock(&thread_lock);
236
237		usleep(100000);
238
239		/* Ok, all threads are patiently blocked, start requeueing */
240		gettimeofday(&start, NULL);
241		while (nrequeued < params.nthreads) {
242			int r;
243
244			/*
245			 * For the regular non-pi case, do not wakeup any tasks
246			 * blocked on futex1, allowing us to really measure
247			 * futex_wait functionality. For the PI case the first
248			 * waiter is always awoken.
249			 */
250			if (!params.pi) {
251				r = futex_cmp_requeue(&futex1, 0, &futex2, 0,
252						      params.nrequeue,
253						      futex_flag);
254			} else {
255				r = futex_cmp_requeue_pi(&futex1, 0, &futex2,
256							 params.nrequeue,
257							 futex_flag);
258				wakeups++; /* assume no error */
259			}
260
261			if (r < 0)
262				err(EXIT_FAILURE, "couldn't requeue from %p to %p",
263				    &futex1, &futex2);
264
265			nrequeued += r;
266		}
267
268		gettimeofday(&end, NULL);
269		timersub(&end, &start, &runtime);
270
271		update_stats(&requeued_stats, nrequeued);
272		update_stats(&requeuetime_stats, runtime.tv_usec);
273
274		if (!params.silent) {
275			if (!params.pi)
276				printf("[Run %d]: Requeued %d of %d threads in "
277				       "%.4f ms\n", j + 1, nrequeued,
278				       params.nthreads,
279				       runtime.tv_usec / (double)USEC_PER_MSEC);
280			else {
281				nrequeued -= wakeups;
282				printf("[Run %d]: Awoke and Requeued (%d+%d) of "
283				       "%d threads in %.4f ms\n",
284				       j + 1, wakeups, nrequeued,
285				       params.nthreads,
286				       runtime.tv_usec / (double)USEC_PER_MSEC);
287			}
288
289		}
290
291		if (!params.pi) {
292			/* everybody should be blocked on futex2, wake'em up */
293			nrequeued = futex_wake(&futex2, nrequeued, futex_flag);
294			if (params.nthreads != nrequeued)
295				warnx("couldn't wakeup all tasks (%d/%d)",
296				      nrequeued, params.nthreads);
297		}
298
299		for (i = 0; i < params.nthreads; i++) {
300			ret = pthread_join(worker[i], NULL);
301			if (ret)
302				err(EXIT_FAILURE, "pthread_join");
303		}
304	}
305
306	/* cleanup & report results */
307	cond_destroy(&thread_parent);
308	cond_destroy(&thread_worker);
309	mutex_destroy(&thread_lock);
310
311	print_summary();
312
313	free(worker);
314	perf_cpu_map__put(cpu);
315	return ret;
316err:
317	usage_with_options(bench_futex_requeue_usage, options);
318	exit(EXIT_FAILURE);
319}
~/ Read-only file preview.