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 "../util/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	futex_wait(&futex1, 0, NULL, futex_flag);
64	return NULL;
65}
66
67static void print_summary(void)
68{
69	double waketime_avg = avg_stats(&waketime_stats);
70	double waketime_stddev = stddev_stats(&waketime_stats);
71	unsigned int wakeup_avg = avg_stats(&wakeup_stats);
72
73	printf("Wokeup %d of %d threads in %.4f ms (+-%.2f%%)\n",
74	       wakeup_avg,
75	       nthreads,
76	       waketime_avg/1e3,
77	       rel_stddev_stats(waketime_stddev, waketime_avg));
78}
79
80static void block_threads(pthread_t *w,
81			  pthread_attr_t thread_attr)
82{
83	cpu_set_t cpu;
84	unsigned int i;
85
86	threads_starting = nthreads;
87
88	/* create and block all threads */
89	for (i = 0; i < nthreads; i++) {
90		CPU_ZERO(&cpu);
91		CPU_SET(i % ncpus, &cpu);
92
93		if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
94			err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
95
96		if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
97			err(EXIT_FAILURE, "pthread_create");
98	}
99}
100
101static void toggle_done(int sig __maybe_unused,
102			siginfo_t *info __maybe_unused,
103			void *uc __maybe_unused)
104{
105	done = true;
106}
107
108int bench_futex_wake(int argc, const char **argv,
109		     const char *prefix __maybe_unused)
110{
111	int ret = 0;
112	unsigned int i, j;
113	struct sigaction act;
114	pthread_attr_t thread_attr;
115
116	argc = parse_options(argc, argv, options, bench_futex_wake_usage, 0);
117	if (argc) {
118		usage_with_options(bench_futex_wake_usage, options);
119		exit(EXIT_FAILURE);
120	}
121
122	ncpus = sysconf(_SC_NPROCESSORS_ONLN);
123
124	sigfillset(&act.sa_mask);
125	act.sa_sigaction = toggle_done;
126	sigaction(SIGINT, &act, NULL);
127
128	if (!nthreads)
129		nthreads = ncpus;
130
131	worker = calloc(nthreads, sizeof(*worker));
132	if (!worker)
133		err(EXIT_FAILURE, "calloc");
134
135	if (!fshared)
136		futex_flag = FUTEX_PRIVATE_FLAG;
137
138	printf("Run summary [PID %d]: blocking on %d threads (at [%s] futex %p), "
139	       "waking up %d at a time.\n\n",
140	       getpid(), nthreads, fshared ? "shared":"private",  &futex1, nwakes);
141
142	init_stats(&wakeup_stats);
143	init_stats(&waketime_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 nwoken = 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 waking folks up */
166		gettimeofday(&start, NULL);
167		while (nwoken != nthreads)
168			nwoken += futex_wake(&futex1, nwakes, futex_flag);
169		gettimeofday(&end, NULL);
170		timersub(&end, &start, &runtime);
171
172		update_stats(&wakeup_stats, nwoken);
173		update_stats(&waketime_stats, runtime.tv_usec);
174
175		if (!silent) {
176			printf("[Run %d]: Wokeup %d of %d threads in %.4f ms\n",
177			       j + 1, nwoken, nthreads, runtime.tv_usec/1e3);
178		}
179
180		for (i = 0; i < nthreads; i++) {
181			ret = pthread_join(worker[i], NULL);
182			if (ret)
183				err(EXIT_FAILURE, "pthread_join");
184		}
185
186	}
187
188	/* cleanup & report results */
189	pthread_cond_destroy(&thread_parent);
190	pthread_cond_destroy(&thread_worker);
191	pthread_mutex_destroy(&thread_lock);
192	pthread_attr_destroy(&thread_attr);
193
194	print_summary();
195
196	free(worker);
197	return ret;
198}
199