Linux Audio

Check our new training course

Loading...
v5.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) 2015 Davidlohr Bueso.
  4 *
  5 * Block a bunch of threads and let parallel waker threads wakeup an
  6 * equal amount of them. The program output reflects the avg latency
  7 * for each individual thread to service its share of work. Ultimately
  8 * it can be used to measure futex_wake() changes.
  9 */
 10#include "bench.h"
 11#include <linux/compiler.h>
 12#include "../util/debug.h"
 
 13
 14#ifndef HAVE_PTHREAD_BARRIER
 15int bench_futex_wake_parallel(int argc __maybe_unused, const char **argv __maybe_unused)
 16{
 17	pr_err("%s: pthread_barrier_t unavailable, disabling this test...\n", __func__);
 18	return 0;
 19}
 20#else /* HAVE_PTHREAD_BARRIER */
 21/* For the CLR_() macros */
 22#include <string.h>
 23#include <pthread.h>
 24
 25#include <signal.h>
 26#include "../util/stat.h"
 27#include <subcmd/parse-options.h>
 28#include <linux/kernel.h>
 29#include <linux/time64.h>
 30#include <errno.h>
 31#include "futex.h"
 32#include <internal/cpumap.h>
 33#include <perf/cpumap.h>
 34
 35#include <err.h>
 36#include <stdlib.h>
 37#include <sys/time.h>
 
 38
 39struct thread_data {
 40	pthread_t worker;
 41	unsigned int nwoken;
 42	struct timeval runtime;
 43};
 44
 45static unsigned int nwakes = 1;
 46
 47/* all threads will block on the same futex -- hash bucket chaos ;) */
 48static u_int32_t futex = 0;
 49
 50static pthread_t *blocked_worker;
 51static bool done = false, silent = false, fshared = false;
 52static unsigned int nblocked_threads = 0, nwaking_threads = 0;
 53static pthread_mutex_t thread_lock;
 54static pthread_cond_t thread_parent, thread_worker;
 55static pthread_barrier_t barrier;
 56static struct stats waketime_stats, wakeup_stats;
 57static unsigned int threads_starting;
 58static int futex_flag = 0;
 59
 
 
 60static const struct option options[] = {
 61	OPT_UINTEGER('t', "threads", &nblocked_threads, "Specify amount of threads"),
 62	OPT_UINTEGER('w', "nwakers", &nwaking_threads, "Specify amount of waking threads"),
 63	OPT_BOOLEAN( 's', "silent",  &silent,   "Silent mode: do not display data/details"),
 64	OPT_BOOLEAN( 'S', "shared",  &fshared,  "Use shared futexes instead of private ones"),
 
 
 65	OPT_END()
 66};
 67
 68static const char * const bench_futex_wake_parallel_usage[] = {
 69	"perf bench futex wake-parallel <options>",
 70	NULL
 71};
 72
 73static void *waking_workerfn(void *arg)
 74{
 75	struct thread_data *waker = (struct thread_data *) arg;
 76	struct timeval start, end;
 77
 78	pthread_barrier_wait(&barrier);
 79
 80	gettimeofday(&start, NULL);
 81
 82	waker->nwoken = futex_wake(&futex, nwakes, futex_flag);
 83	if (waker->nwoken != nwakes)
 84		warnx("couldn't wakeup all tasks (%d/%d)",
 85		      waker->nwoken, nwakes);
 86
 87	gettimeofday(&end, NULL);
 88	timersub(&end, &start, &waker->runtime);
 89
 90	pthread_exit(NULL);
 91	return NULL;
 92}
 93
 94static void wakeup_threads(struct thread_data *td, pthread_attr_t thread_attr)
 95{
 96	unsigned int i;
 
 97
 
 98	pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
 99
100	pthread_barrier_init(&barrier, NULL, nwaking_threads + 1);
101
102	/* create and block all threads */
103	for (i = 0; i < nwaking_threads; i++) {
104		/*
105		 * Thread creation order will impact per-thread latency
106		 * as it will affect the order to acquire the hb spinlock.
107		 * For now let the scheduler decide.
108		 */
109		if (pthread_create(&td[i].worker, &thread_attr,
110				   waking_workerfn, (void *)&td[i]))
111			err(EXIT_FAILURE, "pthread_create");
112	}
113
114	pthread_barrier_wait(&barrier);
115
116	for (i = 0; i < nwaking_threads; i++)
117		if (pthread_join(td[i].worker, NULL))
118			err(EXIT_FAILURE, "pthread_join");
119
120	pthread_barrier_destroy(&barrier);
 
121}
122
123static void *blocked_workerfn(void *arg __maybe_unused)
124{
125	pthread_mutex_lock(&thread_lock);
126	threads_starting--;
127	if (!threads_starting)
128		pthread_cond_signal(&thread_parent);
129	pthread_cond_wait(&thread_worker, &thread_lock);
130	pthread_mutex_unlock(&thread_lock);
131
132	while (1) { /* handle spurious wakeups */
133		if (futex_wait(&futex, 0, NULL, futex_flag) != EINTR)
134			break;
135	}
136
137	pthread_exit(NULL);
138	return NULL;
139}
140
141static void block_threads(pthread_t *w, pthread_attr_t thread_attr,
142			  struct perf_cpu_map *cpu)
143{
144	cpu_set_t cpuset;
145	unsigned int i;
 
 
 
 
146
147	threads_starting = nblocked_threads;
 
 
148
149	/* create and block all threads */
150	for (i = 0; i < nblocked_threads; i++) {
151		CPU_ZERO(&cpuset);
152		CPU_SET(cpu->map[i % cpu->nr], &cpuset);
 
 
 
153
154		if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset))
 
155			err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
 
156
157		if (pthread_create(&w[i], &thread_attr, blocked_workerfn, NULL))
 
158			err(EXIT_FAILURE, "pthread_create");
 
 
159	}
 
160}
161
162static void print_run(struct thread_data *waking_worker, unsigned int run_num)
163{
164	unsigned int i, wakeup_avg;
165	double waketime_avg, waketime_stddev;
166	struct stats __waketime_stats, __wakeup_stats;
167
168	init_stats(&__wakeup_stats);
169	init_stats(&__waketime_stats);
170
171	for (i = 0; i < nwaking_threads; i++) {
172		update_stats(&__waketime_stats, waking_worker[i].runtime.tv_usec);
173		update_stats(&__wakeup_stats, waking_worker[i].nwoken);
174	}
175
176	waketime_avg = avg_stats(&__waketime_stats);
177	waketime_stddev = stddev_stats(&__waketime_stats);
178	wakeup_avg = avg_stats(&__wakeup_stats);
179
180	printf("[Run %d]: Avg per-thread latency (waking %d/%d threads) "
181	       "in %.4f ms (+-%.2f%%)\n", run_num + 1, wakeup_avg,
182	       nblocked_threads, waketime_avg / USEC_PER_MSEC,
183	       rel_stddev_stats(waketime_stddev, waketime_avg));
184}
185
186static void print_summary(void)
187{
188	unsigned int wakeup_avg;
189	double waketime_avg, waketime_stddev;
190
191	waketime_avg = avg_stats(&waketime_stats);
192	waketime_stddev = stddev_stats(&waketime_stats);
193	wakeup_avg = avg_stats(&wakeup_stats);
194
195	printf("Avg per-thread latency (waking %d/%d threads) in %.4f ms (+-%.2f%%)\n",
196	       wakeup_avg,
197	       nblocked_threads,
198	       waketime_avg / USEC_PER_MSEC,
199	       rel_stddev_stats(waketime_stddev, waketime_avg));
200}
201
202
203static void do_run_stats(struct thread_data *waking_worker)
204{
205	unsigned int i;
206
207	for (i = 0; i < nwaking_threads; i++) {
208		update_stats(&waketime_stats, waking_worker[i].runtime.tv_usec);
209		update_stats(&wakeup_stats, waking_worker[i].nwoken);
210	}
211
212}
213
214static void toggle_done(int sig __maybe_unused,
215			siginfo_t *info __maybe_unused,
216			void *uc __maybe_unused)
217{
218	done = true;
219}
220
221int bench_futex_wake_parallel(int argc, const char **argv)
222{
223	int ret = 0;
224	unsigned int i, j;
225	struct sigaction act;
226	pthread_attr_t thread_attr;
227	struct thread_data *waking_worker;
228	struct perf_cpu_map *cpu;
229
230	argc = parse_options(argc, argv, options,
231			     bench_futex_wake_parallel_usage, 0);
232	if (argc) {
233		usage_with_options(bench_futex_wake_parallel_usage, options);
234		exit(EXIT_FAILURE);
235	}
236
 
237	sigfillset(&act.sa_mask);
238	act.sa_sigaction = toggle_done;
239	sigaction(SIGINT, &act, NULL);
240
241	cpu = perf_cpu_map__new(NULL);
 
 
 
 
 
242	if (!cpu)
243		err(EXIT_FAILURE, "calloc");
244
245	if (!nblocked_threads)
246		nblocked_threads = cpu->nr;
247
248	/* some sanity checks */
249	if (nwaking_threads > nblocked_threads || !nwaking_threads)
250		nwaking_threads = nblocked_threads;
 
251
252	if (nblocked_threads % nwaking_threads)
253		errx(EXIT_FAILURE, "Must be perfectly divisible");
254	/*
255	 * Each thread will wakeup nwakes tasks in
256	 * a single futex_wait call.
257	 */
258	nwakes = nblocked_threads/nwaking_threads;
259
260	blocked_worker = calloc(nblocked_threads, sizeof(*blocked_worker));
261	if (!blocked_worker)
262		err(EXIT_FAILURE, "calloc");
263
264	if (!fshared)
265		futex_flag = FUTEX_PRIVATE_FLAG;
266
267	printf("Run summary [PID %d]: blocking on %d threads (at [%s] "
268	       "futex %p), %d threads waking up %d at a time.\n\n",
269	       getpid(), nblocked_threads, fshared ? "shared":"private",
270	       &futex, nwaking_threads, nwakes);
271
272	init_stats(&wakeup_stats);
273	init_stats(&waketime_stats);
274
275	pthread_attr_init(&thread_attr);
276	pthread_mutex_init(&thread_lock, NULL);
277	pthread_cond_init(&thread_parent, NULL);
278	pthread_cond_init(&thread_worker, NULL);
279
280	for (j = 0; j < bench_repeat && !done; j++) {
281		waking_worker = calloc(nwaking_threads, sizeof(*waking_worker));
282		if (!waking_worker)
283			err(EXIT_FAILURE, "calloc");
284
285		/* create, launch & block all threads */
286		block_threads(blocked_worker, thread_attr, cpu);
287
288		/* make sure all threads are already blocked */
289		pthread_mutex_lock(&thread_lock);
290		while (threads_starting)
291			pthread_cond_wait(&thread_parent, &thread_lock);
292		pthread_cond_broadcast(&thread_worker);
293		pthread_mutex_unlock(&thread_lock);
294
295		usleep(100000);
296
297		/* Ok, all threads are patiently blocked, start waking folks up */
298		wakeup_threads(waking_worker, thread_attr);
299
300		for (i = 0; i < nblocked_threads; i++) {
301			ret = pthread_join(blocked_worker[i], NULL);
302			if (ret)
303				err(EXIT_FAILURE, "pthread_join");
304		}
305
306		do_run_stats(waking_worker);
307		if (!silent)
308			print_run(waking_worker, j);
309
310		free(waking_worker);
311	}
312
313	/* cleanup & report results */
314	pthread_cond_destroy(&thread_parent);
315	pthread_cond_destroy(&thread_worker);
316	pthread_mutex_destroy(&thread_lock);
317	pthread_attr_destroy(&thread_attr);
318
319	print_summary();
320
321	free(blocked_worker);
 
322	return ret;
323}
324#endif /* HAVE_PTHREAD_BARRIER */
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) 2015 Davidlohr Bueso.
  4 *
  5 * Block a bunch of threads and let parallel waker threads wakeup an
  6 * equal amount of them. The program output reflects the avg latency
  7 * for each individual thread to service its share of work. Ultimately
  8 * it can be used to measure futex_wake() changes.
  9 */
 10#include "bench.h"
 11#include <linux/compiler.h>
 12#include "../util/debug.h"
 13#include "../util/mutex.h"
 14
 15#ifndef HAVE_PTHREAD_BARRIER
 16int bench_futex_wake_parallel(int argc __maybe_unused, const char **argv __maybe_unused)
 17{
 18	pr_err("%s: pthread_barrier_t unavailable, disabling this test...\n", __func__);
 19	return 0;
 20}
 21#else /* HAVE_PTHREAD_BARRIER */
 22/* For the CLR_() macros */
 23#include <string.h>
 24#include <pthread.h>
 25
 26#include <signal.h>
 27#include "../util/stat.h"
 28#include <subcmd/parse-options.h>
 29#include <linux/kernel.h>
 30#include <linux/time64.h>
 31#include <errno.h>
 32#include "futex.h"
 
 33#include <perf/cpumap.h>
 34
 35#include <err.h>
 36#include <stdlib.h>
 37#include <sys/time.h>
 38#include <sys/mman.h>
 39
 40struct thread_data {
 41	pthread_t worker;
 42	unsigned int nwoken;
 43	struct timeval runtime;
 44};
 45
 46static unsigned int nwakes = 1;
 47
 48/* all threads will block on the same futex -- hash bucket chaos ;) */
 49static u_int32_t futex = 0;
 50
 51static pthread_t *blocked_worker;
 52static bool done = false;
 53static struct mutex thread_lock;
 54static struct cond thread_parent, thread_worker;
 
 55static pthread_barrier_t barrier;
 56static struct stats waketime_stats, wakeup_stats;
 57static unsigned int threads_starting;
 58static int futex_flag = 0;
 59
 60static struct bench_futex_parameters params;
 61
 62static const struct option options[] = {
 63	OPT_UINTEGER('t', "threads", &params.nthreads, "Specify amount of threads"),
 64	OPT_UINTEGER('w', "nwakers", &params.nwakes, "Specify amount of waking threads"),
 65	OPT_BOOLEAN( 's', "silent",  &params.silent, "Silent mode: do not display data/details"),
 66	OPT_BOOLEAN( 'S', "shared",  &params.fshared, "Use shared futexes instead of private ones"),
 67	OPT_BOOLEAN( 'm', "mlockall", &params.mlockall, "Lock all current and future memory"),
 68
 69	OPT_END()
 70};
 71
 72static const char * const bench_futex_wake_parallel_usage[] = {
 73	"perf bench futex wake-parallel <options>",
 74	NULL
 75};
 76
 77static void *waking_workerfn(void *arg)
 78{
 79	struct thread_data *waker = (struct thread_data *) arg;
 80	struct timeval start, end;
 81
 82	pthread_barrier_wait(&barrier);
 83
 84	gettimeofday(&start, NULL);
 85
 86	waker->nwoken = futex_wake(&futex, nwakes, futex_flag);
 87	if (waker->nwoken != nwakes)
 88		warnx("couldn't wakeup all tasks (%d/%d)",
 89		      waker->nwoken, nwakes);
 90
 91	gettimeofday(&end, NULL);
 92	timersub(&end, &start, &waker->runtime);
 93
 94	pthread_exit(NULL);
 95	return NULL;
 96}
 97
 98static void wakeup_threads(struct thread_data *td)
 99{
100	unsigned int i;
101	pthread_attr_t thread_attr;
102
103	pthread_attr_init(&thread_attr);
104	pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
105
106	pthread_barrier_init(&barrier, NULL, params.nwakes + 1);
107
108	/* create and block all threads */
109	for (i = 0; i < params.nwakes; i++) {
110		/*
111		 * Thread creation order will impact per-thread latency
112		 * as it will affect the order to acquire the hb spinlock.
113		 * For now let the scheduler decide.
114		 */
115		if (pthread_create(&td[i].worker, &thread_attr,
116				   waking_workerfn, (void *)&td[i]))
117			err(EXIT_FAILURE, "pthread_create");
118	}
119
120	pthread_barrier_wait(&barrier);
121
122	for (i = 0; i < params.nwakes; i++)
123		if (pthread_join(td[i].worker, NULL))
124			err(EXIT_FAILURE, "pthread_join");
125
126	pthread_barrier_destroy(&barrier);
127	pthread_attr_destroy(&thread_attr);
128}
129
130static void *blocked_workerfn(void *arg __maybe_unused)
131{
132	mutex_lock(&thread_lock);
133	threads_starting--;
134	if (!threads_starting)
135		cond_signal(&thread_parent);
136	cond_wait(&thread_worker, &thread_lock);
137	mutex_unlock(&thread_lock);
138
139	while (1) { /* handle spurious wakeups */
140		if (futex_wait(&futex, 0, NULL, futex_flag) != EINTR)
141			break;
142	}
143
144	pthread_exit(NULL);
145	return NULL;
146}
147
148static void block_threads(pthread_t *w, struct perf_cpu_map *cpu)
 
149{
150	cpu_set_t *cpuset;
151	unsigned int i;
152	int nrcpus = perf_cpu_map__nr(cpu);
153	size_t size;
154
155	threads_starting = params.nthreads;
156
157	cpuset = CPU_ALLOC(nrcpus);
158	BUG_ON(!cpuset);
159	size = CPU_ALLOC_SIZE(nrcpus);
160
161	/* create and block all threads */
162	for (i = 0; i < params.nthreads; i++) {
163		pthread_attr_t thread_attr;
164
165		pthread_attr_init(&thread_attr);
166		CPU_ZERO_S(size, cpuset);
167		CPU_SET_S(perf_cpu_map__cpu(cpu, i % perf_cpu_map__nr(cpu)).cpu, size, cpuset);
168
169		if (pthread_attr_setaffinity_np(&thread_attr, size, cpuset)) {
170			CPU_FREE(cpuset);
171			err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
172		}
173
174		if (pthread_create(&w[i], &thread_attr, blocked_workerfn, NULL)) {
175			CPU_FREE(cpuset);
176			err(EXIT_FAILURE, "pthread_create");
177		}
178		pthread_attr_destroy(&thread_attr);
179	}
180	CPU_FREE(cpuset);
181}
182
183static void print_run(struct thread_data *waking_worker, unsigned int run_num)
184{
185	unsigned int i, wakeup_avg;
186	double waketime_avg, waketime_stddev;
187	struct stats __waketime_stats, __wakeup_stats;
188
189	init_stats(&__wakeup_stats);
190	init_stats(&__waketime_stats);
191
192	for (i = 0; i < params.nwakes; i++) {
193		update_stats(&__waketime_stats, waking_worker[i].runtime.tv_usec);
194		update_stats(&__wakeup_stats, waking_worker[i].nwoken);
195	}
196
197	waketime_avg = avg_stats(&__waketime_stats);
198	waketime_stddev = stddev_stats(&__waketime_stats);
199	wakeup_avg = avg_stats(&__wakeup_stats);
200
201	printf("[Run %d]: Avg per-thread latency (waking %d/%d threads) "
202	       "in %.4f ms (+-%.2f%%)\n", run_num + 1, wakeup_avg,
203	       params.nthreads, waketime_avg / USEC_PER_MSEC,
204	       rel_stddev_stats(waketime_stddev, waketime_avg));
205}
206
207static void print_summary(void)
208{
209	unsigned int wakeup_avg;
210	double waketime_avg, waketime_stddev;
211
212	waketime_avg = avg_stats(&waketime_stats);
213	waketime_stddev = stddev_stats(&waketime_stats);
214	wakeup_avg = avg_stats(&wakeup_stats);
215
216	printf("Avg per-thread latency (waking %d/%d threads) in %.4f ms (+-%.2f%%)\n",
217	       wakeup_avg,
218	       params.nthreads,
219	       waketime_avg / USEC_PER_MSEC,
220	       rel_stddev_stats(waketime_stddev, waketime_avg));
221}
222
223
224static void do_run_stats(struct thread_data *waking_worker)
225{
226	unsigned int i;
227
228	for (i = 0; i < params.nwakes; i++) {
229		update_stats(&waketime_stats, waking_worker[i].runtime.tv_usec);
230		update_stats(&wakeup_stats, waking_worker[i].nwoken);
231	}
232
233}
234
235static void toggle_done(int sig __maybe_unused,
236			siginfo_t *info __maybe_unused,
237			void *uc __maybe_unused)
238{
239	done = true;
240}
241
242int bench_futex_wake_parallel(int argc, const char **argv)
243{
244	int ret = 0;
245	unsigned int i, j;
246	struct sigaction act;
 
247	struct thread_data *waking_worker;
248	struct perf_cpu_map *cpu;
249
250	argc = parse_options(argc, argv, options,
251			     bench_futex_wake_parallel_usage, 0);
252	if (argc) {
253		usage_with_options(bench_futex_wake_parallel_usage, options);
254		exit(EXIT_FAILURE);
255	}
256
257	memset(&act, 0, sizeof(act));
258	sigfillset(&act.sa_mask);
259	act.sa_sigaction = toggle_done;
260	sigaction(SIGINT, &act, NULL);
261
262	if (params.mlockall) {
263		if (mlockall(MCL_CURRENT | MCL_FUTURE))
264			err(EXIT_FAILURE, "mlockall");
265	}
266
267	cpu = perf_cpu_map__new_online_cpus();
268	if (!cpu)
269		err(EXIT_FAILURE, "calloc");
270
271	if (!params.nthreads)
272		params.nthreads = perf_cpu_map__nr(cpu);
273
274	/* some sanity checks */
275	if (params.nwakes > params.nthreads ||
276	    !params.nwakes)
277		params.nwakes = params.nthreads;
278
279	if (params.nthreads % params.nwakes)
280		errx(EXIT_FAILURE, "Must be perfectly divisible");
281	/*
282	 * Each thread will wakeup nwakes tasks in
283	 * a single futex_wait call.
284	 */
285	nwakes = params.nthreads/params.nwakes;
286
287	blocked_worker = calloc(params.nthreads, sizeof(*blocked_worker));
288	if (!blocked_worker)
289		err(EXIT_FAILURE, "calloc");
290
291	if (!params.fshared)
292		futex_flag = FUTEX_PRIVATE_FLAG;
293
294	printf("Run summary [PID %d]: blocking on %d threads (at [%s] "
295	       "futex %p), %d threads waking up %d at a time.\n\n",
296	       getpid(), params.nthreads, params.fshared ? "shared":"private",
297	       &futex, params.nwakes, nwakes);
298
299	init_stats(&wakeup_stats);
300	init_stats(&waketime_stats);
301
302	mutex_init(&thread_lock);
303	cond_init(&thread_parent);
304	cond_init(&thread_worker);
 
305
306	for (j = 0; j < bench_repeat && !done; j++) {
307		waking_worker = calloc(params.nwakes, sizeof(*waking_worker));
308		if (!waking_worker)
309			err(EXIT_FAILURE, "calloc");
310
311		/* create, launch & block all threads */
312		block_threads(blocked_worker, cpu);
313
314		/* make sure all threads are already blocked */
315		mutex_lock(&thread_lock);
316		while (threads_starting)
317			cond_wait(&thread_parent, &thread_lock);
318		cond_broadcast(&thread_worker);
319		mutex_unlock(&thread_lock);
320
321		usleep(100000);
322
323		/* Ok, all threads are patiently blocked, start waking folks up */
324		wakeup_threads(waking_worker);
325
326		for (i = 0; i < params.nthreads; i++) {
327			ret = pthread_join(blocked_worker[i], NULL);
328			if (ret)
329				err(EXIT_FAILURE, "pthread_join");
330		}
331
332		do_run_stats(waking_worker);
333		if (!params.silent)
334			print_run(waking_worker, j);
335
336		free(waking_worker);
337	}
338
339	/* cleanup & report results */
340	cond_destroy(&thread_parent);
341	cond_destroy(&thread_worker);
342	mutex_destroy(&thread_lock);
 
343
344	print_summary();
345
346	free(blocked_worker);
347	perf_cpu_map__put(cpu);
348	return ret;
349}
350#endif /* HAVE_PTHREAD_BARRIER */