Linux Audio

Check our new training course

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