Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2015 Davidlohr Bueso.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Block a bunch of threads and let parallel waker threads wakeup an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * equal amount of them. The program output reflects the avg latency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * for each individual thread to service its share of work. Ultimately
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * it can be used to measure futex_wake() changes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include "bench.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include "../util/debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #ifndef HAVE_PTHREAD_BARRIER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) int bench_futex_wake_parallel(int argc __maybe_unused, const char **argv __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	pr_err("%s: pthread_barrier_t unavailable, disabling this test...\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #else /* HAVE_PTHREAD_BARRIER */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) /* For the CLR_() macros */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <pthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include "../util/stat.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <subcmd/parse-options.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/time64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include "futex.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <internal/cpumap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <perf/cpumap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include <err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <sys/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) struct thread_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	pthread_t worker;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	unsigned int nwoken;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct timeval runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static unsigned int nwakes = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) /* all threads will block on the same futex -- hash bucket chaos ;) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static u_int32_t futex = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static pthread_t *blocked_worker;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static bool done = false, silent = false, fshared = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static unsigned int nblocked_threads = 0, nwaking_threads = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static pthread_mutex_t thread_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) static pthread_cond_t thread_parent, thread_worker;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static pthread_barrier_t barrier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) static struct stats waketime_stats, wakeup_stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static unsigned int threads_starting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static int futex_flag = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static const struct option options[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	OPT_UINTEGER('t', "threads", &nblocked_threads, "Specify amount of threads"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	OPT_UINTEGER('w', "nwakers", &nwaking_threads, "Specify amount of waking threads"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	OPT_BOOLEAN( 's', "silent",  &silent,   "Silent mode: do not display data/details"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	OPT_BOOLEAN( 'S', "shared",  &fshared,  "Use shared futexes instead of private ones"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	OPT_END()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static const char * const bench_futex_wake_parallel_usage[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	"perf bench futex wake-parallel <options>",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) static void *waking_workerfn(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct thread_data *waker = (struct thread_data *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct timeval start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	pthread_barrier_wait(&barrier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	gettimeofday(&start, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	waker->nwoken = futex_wake(&futex, nwakes, futex_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	if (waker->nwoken != nwakes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		warnx("couldn't wakeup all tasks (%d/%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		      waker->nwoken, nwakes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	gettimeofday(&end, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	timersub(&end, &start, &waker->runtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	pthread_exit(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) static void wakeup_threads(struct thread_data *td, pthread_attr_t thread_attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	pthread_barrier_init(&barrier, NULL, nwaking_threads + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	/* create and block all threads */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	for (i = 0; i < nwaking_threads; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		 * Thread creation order will impact per-thread latency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		 * as it will affect the order to acquire the hb spinlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		 * For now let the scheduler decide.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		if (pthread_create(&td[i].worker, &thread_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 				   waking_workerfn, (void *)&td[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			err(EXIT_FAILURE, "pthread_create");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	pthread_barrier_wait(&barrier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	for (i = 0; i < nwaking_threads; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		if (pthread_join(td[i].worker, NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 			err(EXIT_FAILURE, "pthread_join");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	pthread_barrier_destroy(&barrier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static void *blocked_workerfn(void *arg __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	pthread_mutex_lock(&thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	threads_starting--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (!threads_starting)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		pthread_cond_signal(&thread_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	pthread_cond_wait(&thread_worker, &thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	pthread_mutex_unlock(&thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	while (1) { /* handle spurious wakeups */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		if (futex_wait(&futex, 0, NULL, futex_flag) != EINTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	pthread_exit(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static void block_threads(pthread_t *w, pthread_attr_t thread_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			  struct perf_cpu_map *cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	cpu_set_t cpuset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	threads_starting = nblocked_threads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	/* create and block all threads */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	for (i = 0; i < nblocked_threads; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		CPU_ZERO(&cpuset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		CPU_SET(cpu->map[i % cpu->nr], &cpuset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		if (pthread_create(&w[i], &thread_attr, blocked_workerfn, NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			err(EXIT_FAILURE, "pthread_create");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static void print_run(struct thread_data *waking_worker, unsigned int run_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	unsigned int i, wakeup_avg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	double waketime_avg, waketime_stddev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	struct stats __waketime_stats, __wakeup_stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	init_stats(&__wakeup_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	init_stats(&__waketime_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	for (i = 0; i < nwaking_threads; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		update_stats(&__waketime_stats, waking_worker[i].runtime.tv_usec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		update_stats(&__wakeup_stats, waking_worker[i].nwoken);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	waketime_avg = avg_stats(&__waketime_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	waketime_stddev = stddev_stats(&__waketime_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	wakeup_avg = avg_stats(&__wakeup_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	printf("[Run %d]: Avg per-thread latency (waking %d/%d threads) "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	       "in %.4f ms (+-%.2f%%)\n", run_num + 1, wakeup_avg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	       nblocked_threads, waketime_avg / USEC_PER_MSEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	       rel_stddev_stats(waketime_stddev, waketime_avg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static void print_summary(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	unsigned int wakeup_avg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	double waketime_avg, waketime_stddev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	waketime_avg = avg_stats(&waketime_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	waketime_stddev = stddev_stats(&waketime_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	wakeup_avg = avg_stats(&wakeup_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	printf("Avg per-thread latency (waking %d/%d threads) in %.4f ms (+-%.2f%%)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	       wakeup_avg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	       nblocked_threads,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	       waketime_avg / USEC_PER_MSEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	       rel_stddev_stats(waketime_stddev, waketime_avg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static void do_run_stats(struct thread_data *waking_worker)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	for (i = 0; i < nwaking_threads; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		update_stats(&waketime_stats, waking_worker[i].runtime.tv_usec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		update_stats(&wakeup_stats, waking_worker[i].nwoken);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static void toggle_done(int sig __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			siginfo_t *info __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			void *uc __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	done = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) int bench_futex_wake_parallel(int argc, const char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	unsigned int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	struct sigaction act;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	pthread_attr_t thread_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	struct thread_data *waking_worker;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	struct perf_cpu_map *cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	argc = parse_options(argc, argv, options,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 			     bench_futex_wake_parallel_usage, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	if (argc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		usage_with_options(bench_futex_wake_parallel_usage, options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	memset(&act, 0, sizeof(act));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	sigfillset(&act.sa_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	act.sa_sigaction = toggle_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	sigaction(SIGINT, &act, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	cpu = perf_cpu_map__new(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (!cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		err(EXIT_FAILURE, "calloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	if (!nblocked_threads)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		nblocked_threads = cpu->nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	/* some sanity checks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	if (nwaking_threads > nblocked_threads || !nwaking_threads)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		nwaking_threads = nblocked_threads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	if (nblocked_threads % nwaking_threads)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		errx(EXIT_FAILURE, "Must be perfectly divisible");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	 * Each thread will wakeup nwakes tasks in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	 * a single futex_wait call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	nwakes = nblocked_threads/nwaking_threads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	blocked_worker = calloc(nblocked_threads, sizeof(*blocked_worker));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	if (!blocked_worker)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		err(EXIT_FAILURE, "calloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	if (!fshared)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		futex_flag = FUTEX_PRIVATE_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	printf("Run summary [PID %d]: blocking on %d threads (at [%s] "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	       "futex %p), %d threads waking up %d at a time.\n\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	       getpid(), nblocked_threads, fshared ? "shared":"private",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	       &futex, nwaking_threads, nwakes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	init_stats(&wakeup_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	init_stats(&waketime_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	pthread_attr_init(&thread_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	pthread_mutex_init(&thread_lock, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	pthread_cond_init(&thread_parent, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	pthread_cond_init(&thread_worker, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	for (j = 0; j < bench_repeat && !done; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		waking_worker = calloc(nwaking_threads, sizeof(*waking_worker));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		if (!waking_worker)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			err(EXIT_FAILURE, "calloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		/* create, launch & block all threads */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		block_threads(blocked_worker, thread_attr, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		/* make sure all threads are already blocked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		pthread_mutex_lock(&thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		while (threads_starting)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			pthread_cond_wait(&thread_parent, &thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		pthread_cond_broadcast(&thread_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		pthread_mutex_unlock(&thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		usleep(100000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		/* Ok, all threads are patiently blocked, start waking folks up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		wakeup_threads(waking_worker, thread_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		for (i = 0; i < nblocked_threads; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			ret = pthread_join(blocked_worker[i], NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 				err(EXIT_FAILURE, "pthread_join");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		do_run_stats(waking_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		if (!silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			print_run(waking_worker, j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		free(waking_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	/* cleanup & report results */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	pthread_cond_destroy(&thread_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	pthread_cond_destroy(&thread_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	pthread_mutex_destroy(&thread_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	pthread_attr_destroy(&thread_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	print_summary();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	free(blocked_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	perf_cpu_map__put(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) #endif /* HAVE_PTHREAD_BARRIER */