futex-wake-parallel.c 8.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8 9
/*
 * Copyright (C) 2015 Davidlohr Bueso.
 *
 * Block a bunch of threads and let parallel waker threads wakeup an
 * equal amount of them. The program output reflects the avg latency
 * for each individual thread to service its share of work. Ultimately
 * it can be used to measure futex_wake() changes.
 */
J
James Yang 已提交
10 11 12
#include "bench.h"
#include <linux/compiler.h>
#include "../util/debug.h"
13

J
James Yang 已提交
14 15 16 17 18 19 20
#ifndef HAVE_PTHREAD_BARRIER
int bench_futex_wake_parallel(int argc __maybe_unused, const char **argv __maybe_unused)
{
	pr_err("%s: pthread_barrier_t unavailable, disabling this test...\n", __func__);
	return 0;
}
#else /* HAVE_PTHREAD_BARRIER */
21
/* For the CLR_() macros */
22
#include <string.h>
23 24
#include <pthread.h>

25
#include <signal.h>
26
#include "../util/stat.h"
27
#include <subcmd/parse-options.h>
28
#include <linux/kernel.h>
29
#include <linux/time64.h>
30
#include <errno.h>
31
#include "futex.h"
32
#include <perf/cpumap.h>
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

#include <err.h>
#include <stdlib.h>
#include <sys/time.h>

struct thread_data {
	pthread_t worker;
	unsigned int nwoken;
	struct timeval runtime;
};

static unsigned int nwakes = 1;

/* all threads will block on the same futex -- hash bucket chaos ;) */
static u_int32_t futex = 0;

static pthread_t *blocked_worker;
static bool done = false, silent = false, fshared = false;
static unsigned int nblocked_threads = 0, nwaking_threads = 0;
static pthread_mutex_t thread_lock;
static pthread_cond_t thread_parent, thread_worker;
J
James Yang 已提交
54
static pthread_barrier_t barrier;
55
static struct stats waketime_stats, wakeup_stats;
D
Davidlohr Bueso 已提交
56
static unsigned int threads_starting;
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
static int futex_flag = 0;

static const struct option options[] = {
	OPT_UINTEGER('t', "threads", &nblocked_threads, "Specify amount of threads"),
	OPT_UINTEGER('w', "nwakers", &nwaking_threads, "Specify amount of waking threads"),
	OPT_BOOLEAN( 's', "silent",  &silent,   "Silent mode: do not display data/details"),
	OPT_BOOLEAN( 'S', "shared",  &fshared,  "Use shared futexes instead of private ones"),
	OPT_END()
};

static const char * const bench_futex_wake_parallel_usage[] = {
	"perf bench futex wake-parallel <options>",
	NULL
};

static void *waking_workerfn(void *arg)
{
	struct thread_data *waker = (struct thread_data *) arg;
	struct timeval start, end;

J
James Yang 已提交
77 78
	pthread_barrier_wait(&barrier);

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	gettimeofday(&start, NULL);

	waker->nwoken = futex_wake(&futex, nwakes, futex_flag);
	if (waker->nwoken != nwakes)
		warnx("couldn't wakeup all tasks (%d/%d)",
		      waker->nwoken, nwakes);

	gettimeofday(&end, NULL);
	timersub(&end, &start, &waker->runtime);

	pthread_exit(NULL);
	return NULL;
}

static void wakeup_threads(struct thread_data *td, pthread_attr_t thread_attr)
{
	unsigned int i;

	pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);

J
James Yang 已提交
99 100
	pthread_barrier_init(&barrier, NULL, nwaking_threads + 1);

101 102 103 104 105 106 107 108 109 110 111 112
	/* create and block all threads */
	for (i = 0; i < nwaking_threads; i++) {
		/*
		 * Thread creation order will impact per-thread latency
		 * as it will affect the order to acquire the hb spinlock.
		 * For now let the scheduler decide.
		 */
		if (pthread_create(&td[i].worker, &thread_attr,
				   waking_workerfn, (void *)&td[i]))
			err(EXIT_FAILURE, "pthread_create");
	}

J
James Yang 已提交
113 114
	pthread_barrier_wait(&barrier);

115 116 117
	for (i = 0; i < nwaking_threads; i++)
		if (pthread_join(td[i].worker, NULL))
			err(EXIT_FAILURE, "pthread_join");
J
James Yang 已提交
118 119

	pthread_barrier_destroy(&barrier);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
}

static void *blocked_workerfn(void *arg __maybe_unused)
{
	pthread_mutex_lock(&thread_lock);
	threads_starting--;
	if (!threads_starting)
		pthread_cond_signal(&thread_parent);
	pthread_cond_wait(&thread_worker, &thread_lock);
	pthread_mutex_unlock(&thread_lock);

	while (1) { /* handle spurious wakeups */
		if (futex_wait(&futex, 0, NULL, futex_flag) != EINTR)
			break;
	}

	pthread_exit(NULL);
	return NULL;
}

D
Davidlohr Bueso 已提交
140
static void block_threads(pthread_t *w, pthread_attr_t thread_attr,
141
			  struct perf_cpu_map *cpu)
142
{
D
Davidlohr Bueso 已提交
143
	cpu_set_t cpuset;
144 145 146 147 148 149
	unsigned int i;

	threads_starting = nblocked_threads;

	/* create and block all threads */
	for (i = 0; i < nblocked_threads; i++) {
D
Davidlohr Bueso 已提交
150 151
		CPU_ZERO(&cpuset);
		CPU_SET(cpu->map[i % cpu->nr], &cpuset);
152

D
Davidlohr Bueso 已提交
153
		if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset))
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
			err(EXIT_FAILURE, "pthread_attr_setaffinity_np");

		if (pthread_create(&w[i], &thread_attr, blocked_workerfn, NULL))
			err(EXIT_FAILURE, "pthread_create");
	}
}

static void print_run(struct thread_data *waking_worker, unsigned int run_num)
{
	unsigned int i, wakeup_avg;
	double waketime_avg, waketime_stddev;
	struct stats __waketime_stats, __wakeup_stats;

	init_stats(&__wakeup_stats);
	init_stats(&__waketime_stats);

	for (i = 0; i < nwaking_threads; i++) {
		update_stats(&__waketime_stats, waking_worker[i].runtime.tv_usec);
		update_stats(&__wakeup_stats, waking_worker[i].nwoken);
	}

	waketime_avg = avg_stats(&__waketime_stats);
	waketime_stddev = stddev_stats(&__waketime_stats);
	wakeup_avg = avg_stats(&__wakeup_stats);

	printf("[Run %d]: Avg per-thread latency (waking %d/%d threads) "
	       "in %.4f ms (+-%.2f%%)\n", run_num + 1, wakeup_avg,
181
	       nblocked_threads, waketime_avg / USEC_PER_MSEC,
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	       rel_stddev_stats(waketime_stddev, waketime_avg));
}

static void print_summary(void)
{
	unsigned int wakeup_avg;
	double waketime_avg, waketime_stddev;

	waketime_avg = avg_stats(&waketime_stats);
	waketime_stddev = stddev_stats(&waketime_stats);
	wakeup_avg = avg_stats(&wakeup_stats);

	printf("Avg per-thread latency (waking %d/%d threads) in %.4f ms (+-%.2f%%)\n",
	       wakeup_avg,
	       nblocked_threads,
197
	       waketime_avg / USEC_PER_MSEC,
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
	       rel_stddev_stats(waketime_stddev, waketime_avg));
}


static void do_run_stats(struct thread_data *waking_worker)
{
	unsigned int i;

	for (i = 0; i < nwaking_threads; i++) {
		update_stats(&waketime_stats, waking_worker[i].runtime.tv_usec);
		update_stats(&wakeup_stats, waking_worker[i].nwoken);
	}

}

static void toggle_done(int sig __maybe_unused,
			siginfo_t *info __maybe_unused,
			void *uc __maybe_unused)
{
	done = true;
}

220
int bench_futex_wake_parallel(int argc, const char **argv)
221 222 223 224 225 226
{
	int ret = 0;
	unsigned int i, j;
	struct sigaction act;
	pthread_attr_t thread_attr;
	struct thread_data *waking_worker;
227
	struct perf_cpu_map *cpu;
228 229 230 231 232 233 234 235

	argc = parse_options(argc, argv, options,
			     bench_futex_wake_parallel_usage, 0);
	if (argc) {
		usage_with_options(bench_futex_wake_parallel_usage, options);
		exit(EXIT_FAILURE);
	}

236
	memset(&act, 0, sizeof(act));
237 238 239 240
	sigfillset(&act.sa_mask);
	act.sa_sigaction = toggle_done;
	sigaction(SIGINT, &act, NULL);

241
	cpu = perf_cpu_map__new(NULL);
D
Davidlohr Bueso 已提交
242 243 244
	if (!cpu)
		err(EXIT_FAILURE, "calloc");

245
	if (!nblocked_threads)
D
Davidlohr Bueso 已提交
246
		nblocked_threads = cpu->nr;
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

	/* some sanity checks */
	if (nwaking_threads > nblocked_threads || !nwaking_threads)
		nwaking_threads = nblocked_threads;

	if (nblocked_threads % nwaking_threads)
		errx(EXIT_FAILURE, "Must be perfectly divisible");
	/*
	 * Each thread will wakeup nwakes tasks in
	 * a single futex_wait call.
	 */
	nwakes = nblocked_threads/nwaking_threads;

	blocked_worker = calloc(nblocked_threads, sizeof(*blocked_worker));
	if (!blocked_worker)
		err(EXIT_FAILURE, "calloc");

	if (!fshared)
		futex_flag = FUTEX_PRIVATE_FLAG;

	printf("Run summary [PID %d]: blocking on %d threads (at [%s] "
	       "futex %p), %d threads waking up %d at a time.\n\n",
	       getpid(), nblocked_threads, fshared ? "shared":"private",
	       &futex, nwaking_threads, nwakes);

	init_stats(&wakeup_stats);
	init_stats(&waketime_stats);

	pthread_attr_init(&thread_attr);
	pthread_mutex_init(&thread_lock, NULL);
	pthread_cond_init(&thread_parent, NULL);
	pthread_cond_init(&thread_worker, NULL);

	for (j = 0; j < bench_repeat && !done; j++) {
		waking_worker = calloc(nwaking_threads, sizeof(*waking_worker));
		if (!waking_worker)
			err(EXIT_FAILURE, "calloc");

		/* create, launch & block all threads */
D
Davidlohr Bueso 已提交
286
		block_threads(blocked_worker, thread_attr, cpu);
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

		/* make sure all threads are already blocked */
		pthread_mutex_lock(&thread_lock);
		while (threads_starting)
			pthread_cond_wait(&thread_parent, &thread_lock);
		pthread_cond_broadcast(&thread_worker);
		pthread_mutex_unlock(&thread_lock);

		usleep(100000);

		/* Ok, all threads are patiently blocked, start waking folks up */
		wakeup_threads(waking_worker, thread_attr);

		for (i = 0; i < nblocked_threads; i++) {
			ret = pthread_join(blocked_worker[i], NULL);
			if (ret)
				err(EXIT_FAILURE, "pthread_join");
		}

		do_run_stats(waking_worker);
		if (!silent)
			print_run(waking_worker, j);

		free(waking_worker);
	}

	/* cleanup & report results */
	pthread_cond_destroy(&thread_parent);
	pthread_cond_destroy(&thread_worker);
	pthread_mutex_destroy(&thread_lock);
	pthread_attr_destroy(&thread_attr);

	print_summary();

	free(blocked_worker);
	return ret;
}
J
James Yang 已提交
324
#endif /* HAVE_PTHREAD_BARRIER */