Linux Audio

Check our new training course

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