Linux Audio

Check our new training course

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