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