cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

futex-wake.c (6312B)


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