Linux Audio

Check our new training course

Loading...
v4.6
 
  1/*
  2 * Copyright (C) 2013  Davidlohr Bueso <davidlohr@hp.com>
  3 *
  4 * futex-requeue: Block a bunch of threads on futex1 and requeue them
  5 *                on futex2, N at a time.
  6 *
  7 * This program is particularly useful to measure the latency of nthread
  8 * requeues without waking up any tasks -- thus mimicking a regular futex_wait.
  9 */
 10
 11#include "../perf.h"
 12#include "../util/util.h"
 
 
 
 13#include "../util/stat.h"
 14#include <subcmd/parse-options.h>
 15#include "../util/header.h"
 
 
 
 
 
 16#include "bench.h"
 17#include "futex.h"
 18
 19#include <err.h>
 20#include <stdlib.h>
 21#include <sys/time.h>
 22#include <pthread.h>
 23
 24static u_int32_t futex1 = 0, futex2 = 0;
 25
 26/*
 27 * How many tasks to requeue at a time.
 28 * Default to 1 in order to make the kernel work more.
 29 */
 30static unsigned int nrequeue = 1;
 31
 32static pthread_t *worker;
 33static bool done = false, silent = false, fshared = false;
 34static pthread_mutex_t thread_lock;
 35static pthread_cond_t thread_parent, thread_worker;
 36static struct stats requeuetime_stats, requeued_stats;
 37static unsigned int ncpus, threads_starting, nthreads = 0;
 38static int futex_flag = 0;
 39
 40static const struct option options[] = {
 41	OPT_UINTEGER('t', "threads",  &nthreads, "Specify amount of threads"),
 42	OPT_UINTEGER('q', "nrequeue", &nrequeue, "Specify amount of threads to requeue at once"),
 43	OPT_BOOLEAN( 's', "silent",   &silent,   "Silent mode: do not display data/details"),
 44	OPT_BOOLEAN( 'S', "shared",   &fshared,  "Use shared futexes instead of private ones"),
 45	OPT_END()
 46};
 47
 48static const char * const bench_futex_requeue_usage[] = {
 49	"perf bench futex requeue <options>",
 50	NULL
 51};
 52
 53static void print_summary(void)
 54{
 55	double requeuetime_avg = avg_stats(&requeuetime_stats);
 56	double requeuetime_stddev = stddev_stats(&requeuetime_stats);
 57	unsigned int requeued_avg = avg_stats(&requeued_stats);
 58
 59	printf("Requeued %d of %d threads in %.4f ms (+-%.2f%%)\n",
 60	       requeued_avg,
 61	       nthreads,
 62	       requeuetime_avg/1e3,
 63	       rel_stddev_stats(requeuetime_stddev, requeuetime_avg));
 64}
 65
 66static 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	futex_wait(&futex1, 0, NULL, futex_flag);
 76	return NULL;
 77}
 78
 79static void block_threads(pthread_t *w,
 80			  pthread_attr_t thread_attr)
 81{
 82	cpu_set_t cpu;
 83	unsigned int i;
 84
 85	threads_starting = nthreads;
 86
 87	/* create and block all threads */
 88	for (i = 0; i < nthreads; i++) {
 89		CPU_ZERO(&cpu);
 90		CPU_SET(i % ncpus, &cpu);
 91
 92		if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
 93			err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
 94
 95		if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
 96			err(EXIT_FAILURE, "pthread_create");
 97	}
 98}
 99
100static void toggle_done(int sig __maybe_unused,
101			siginfo_t *info __maybe_unused,
102			void *uc __maybe_unused)
103{
104	done = true;
105}
106
107int bench_futex_requeue(int argc, const char **argv,
108			const char *prefix __maybe_unused)
109{
110	int ret = 0;
111	unsigned int i, j;
112	struct sigaction act;
113	pthread_attr_t thread_attr;
 
114
115	argc = parse_options(argc, argv, options, bench_futex_requeue_usage, 0);
116	if (argc)
117		goto err;
118
119	ncpus = sysconf(_SC_NPROCESSORS_ONLN);
 
 
120
121	sigfillset(&act.sa_mask);
122	act.sa_sigaction = toggle_done;
123	sigaction(SIGINT, &act, NULL);
124
125	if (!nthreads)
126		nthreads = ncpus;
127
128	worker = calloc(nthreads, sizeof(*worker));
129	if (!worker)
130		err(EXIT_FAILURE, "calloc");
131
132	if (!fshared)
133		futex_flag = FUTEX_PRIVATE_FLAG;
134
135	if (nrequeue > nthreads)
136		nrequeue = nthreads;
137
138	printf("Run summary [PID %d]: Requeuing %d threads (from [%s] %p to %p), "
139	       "%d at a time.\n\n",  getpid(), nthreads,
140	       fshared ? "shared":"private", &futex1, &futex2, nrequeue);
141
142	init_stats(&requeued_stats);
143	init_stats(&requeuetime_stats);
144	pthread_attr_init(&thread_attr);
145	pthread_mutex_init(&thread_lock, NULL);
146	pthread_cond_init(&thread_parent, NULL);
147	pthread_cond_init(&thread_worker, NULL);
148
149	for (j = 0; j < bench_repeat && !done; j++) {
150		unsigned int nrequeued = 0;
151		struct timeval start, end, runtime;
152
153		/* create, launch & block all threads */
154		block_threads(worker, thread_attr);
155
156		/* make sure all threads are already blocked */
157		pthread_mutex_lock(&thread_lock);
158		while (threads_starting)
159			pthread_cond_wait(&thread_parent, &thread_lock);
160		pthread_cond_broadcast(&thread_worker);
161		pthread_mutex_unlock(&thread_lock);
162
163		usleep(100000);
164
165		/* Ok, all threads are patiently blocked, start requeueing */
166		gettimeofday(&start, NULL);
167		while (nrequeued < nthreads) {
168			/*
169			 * Do not wakeup any tasks blocked on futex1, allowing
170			 * us to really measure futex_wait functionality.
171			 */
172			nrequeued += futex_cmp_requeue(&futex1, 0, &futex2, 0,
173						       nrequeue, futex_flag);
174		}
175
176		gettimeofday(&end, NULL);
177		timersub(&end, &start, &runtime);
178
179		update_stats(&requeued_stats, nrequeued);
180		update_stats(&requeuetime_stats, runtime.tv_usec);
181
182		if (!silent) {
183			printf("[Run %d]: Requeued %d of %d threads in %.4f ms\n",
184			       j + 1, nrequeued, nthreads, runtime.tv_usec/1e3);
185		}
186
187		/* everybody should be blocked on futex2, wake'em up */
188		nrequeued = futex_wake(&futex2, nrequeued, futex_flag);
189		if (nthreads != nrequeued)
190			warnx("couldn't wakeup all tasks (%d/%d)", nrequeued, nthreads);
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	/* cleanup & report results */
200	pthread_cond_destroy(&thread_parent);
201	pthread_cond_destroy(&thread_worker);
202	pthread_mutex_destroy(&thread_lock);
203	pthread_attr_destroy(&thread_attr);
204
205	print_summary();
206
207	free(worker);
208	return ret;
209err:
210	usage_with_options(bench_futex_requeue_usage, options);
211	exit(EXIT_FAILURE);
212}
v5.4
  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 -- thus mimicking a regular futex_wait.
 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 <internal/cpumap.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
 32static u_int32_t futex1 = 0, futex2 = 0;
 33
 34/*
 35 * How many tasks to requeue at a time.
 36 * Default to 1 in order to make the kernel work more.
 37 */
 38static unsigned int nrequeue = 1;
 39
 40static pthread_t *worker;
 41static bool done = false, silent = false, fshared = false;
 42static pthread_mutex_t thread_lock;
 43static pthread_cond_t thread_parent, thread_worker;
 44static struct stats requeuetime_stats, requeued_stats;
 45static unsigned int threads_starting, nthreads = 0;
 46static int futex_flag = 0;
 47
 48static const struct option options[] = {
 49	OPT_UINTEGER('t', "threads",  &nthreads, "Specify amount of threads"),
 50	OPT_UINTEGER('q', "nrequeue", &nrequeue, "Specify amount of threads to requeue at once"),
 51	OPT_BOOLEAN( 's', "silent",   &silent,   "Silent mode: do not display data/details"),
 52	OPT_BOOLEAN( 'S', "shared",   &fshared,  "Use shared futexes instead of private ones"),
 53	OPT_END()
 54};
 55
 56static const char * const bench_futex_requeue_usage[] = {
 57	"perf bench futex requeue <options>",
 58	NULL
 59};
 60
 61static void print_summary(void)
 62{
 63	double requeuetime_avg = avg_stats(&requeuetime_stats);
 64	double requeuetime_stddev = stddev_stats(&requeuetime_stats);
 65	unsigned int requeued_avg = avg_stats(&requeued_stats);
 66
 67	printf("Requeued %d of %d threads in %.4f ms (+-%.2f%%)\n",
 68	       requeued_avg,
 69	       nthreads,
 70	       requeuetime_avg / USEC_PER_MSEC,
 71	       rel_stddev_stats(requeuetime_stddev, requeuetime_avg));
 72}
 73
 74static void *workerfn(void *arg __maybe_unused)
 75{
 76	pthread_mutex_lock(&thread_lock);
 77	threads_starting--;
 78	if (!threads_starting)
 79		pthread_cond_signal(&thread_parent);
 80	pthread_cond_wait(&thread_worker, &thread_lock);
 81	pthread_mutex_unlock(&thread_lock);
 82
 83	futex_wait(&futex1, 0, NULL, futex_flag);
 84	return NULL;
 85}
 86
 87static void block_threads(pthread_t *w,
 88			  pthread_attr_t thread_attr, struct perf_cpu_map *cpu)
 89{
 90	cpu_set_t cpuset;
 91	unsigned int i;
 92
 93	threads_starting = nthreads;
 94
 95	/* create and block all threads */
 96	for (i = 0; i < nthreads; i++) {
 97		CPU_ZERO(&cpuset);
 98		CPU_SET(cpu->map[i % cpu->nr], &cpuset);
 99
100		if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset))
101			err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
102
103		if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
104			err(EXIT_FAILURE, "pthread_create");
105	}
106}
107
108static void toggle_done(int sig __maybe_unused,
109			siginfo_t *info __maybe_unused,
110			void *uc __maybe_unused)
111{
112	done = true;
113}
114
115int bench_futex_requeue(int argc, const char **argv)
 
116{
117	int ret = 0;
118	unsigned int i, j;
119	struct sigaction act;
120	pthread_attr_t thread_attr;
121	struct perf_cpu_map *cpu;
122
123	argc = parse_options(argc, argv, options, bench_futex_requeue_usage, 0);
124	if (argc)
125		goto err;
126
127	cpu = perf_cpu_map__new(NULL);
128	if (!cpu)
129		err(EXIT_FAILURE, "cpu_map__new");
130
131	sigfillset(&act.sa_mask);
132	act.sa_sigaction = toggle_done;
133	sigaction(SIGINT, &act, NULL);
134
135	if (!nthreads)
136		nthreads = cpu->nr;
137
138	worker = calloc(nthreads, sizeof(*worker));
139	if (!worker)
140		err(EXIT_FAILURE, "calloc");
141
142	if (!fshared)
143		futex_flag = FUTEX_PRIVATE_FLAG;
144
145	if (nrequeue > nthreads)
146		nrequeue = nthreads;
147
148	printf("Run summary [PID %d]: Requeuing %d threads (from [%s] %p to %p), "
149	       "%d at a time.\n\n",  getpid(), nthreads,
150	       fshared ? "shared":"private", &futex1, &futex2, nrequeue);
151
152	init_stats(&requeued_stats);
153	init_stats(&requeuetime_stats);
154	pthread_attr_init(&thread_attr);
155	pthread_mutex_init(&thread_lock, NULL);
156	pthread_cond_init(&thread_parent, NULL);
157	pthread_cond_init(&thread_worker, NULL);
158
159	for (j = 0; j < bench_repeat && !done; j++) {
160		unsigned int nrequeued = 0;
161		struct timeval start, end, runtime;
162
163		/* create, launch & block all threads */
164		block_threads(worker, thread_attr, cpu);
165
166		/* make sure all threads are already blocked */
167		pthread_mutex_lock(&thread_lock);
168		while (threads_starting)
169			pthread_cond_wait(&thread_parent, &thread_lock);
170		pthread_cond_broadcast(&thread_worker);
171		pthread_mutex_unlock(&thread_lock);
172
173		usleep(100000);
174
175		/* Ok, all threads are patiently blocked, start requeueing */
176		gettimeofday(&start, NULL);
177		while (nrequeued < nthreads) {
178			/*
179			 * Do not wakeup any tasks blocked on futex1, allowing
180			 * us to really measure futex_wait functionality.
181			 */
182			nrequeued += futex_cmp_requeue(&futex1, 0, &futex2, 0,
183						       nrequeue, futex_flag);
184		}
185
186		gettimeofday(&end, NULL);
187		timersub(&end, &start, &runtime);
188
189		update_stats(&requeued_stats, nrequeued);
190		update_stats(&requeuetime_stats, runtime.tv_usec);
191
192		if (!silent) {
193			printf("[Run %d]: Requeued %d of %d threads in %.4f ms\n",
194			       j + 1, nrequeued, nthreads, runtime.tv_usec / (double)USEC_PER_MSEC);
195		}
196
197		/* everybody should be blocked on futex2, wake'em up */
198		nrequeued = futex_wake(&futex2, nrequeued, futex_flag);
199		if (nthreads != nrequeued)
200			warnx("couldn't wakeup all tasks (%d/%d)", nrequeued, nthreads);
201
202		for (i = 0; i < nthreads; i++) {
203			ret = pthread_join(worker[i], NULL);
204			if (ret)
205				err(EXIT_FAILURE, "pthread_join");
206		}
207	}
208
209	/* cleanup & report results */
210	pthread_cond_destroy(&thread_parent);
211	pthread_cond_destroy(&thread_worker);
212	pthread_mutex_destroy(&thread_lock);
213	pthread_attr_destroy(&thread_attr);
214
215	print_summary();
216
217	free(worker);
218	return ret;
219err:
220	usage_with_options(bench_futex_requeue_usage, options);
221	exit(EXIT_FAILURE);
222}