Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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