1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2013 Davidlohr Bueso <davidlohr@hp.com> 4 * 5 * futex-requeue: Block a bunch of threads on futex1 and requeue them 6 * on futex2, N at a time. 7 * 8 * This program is particularly useful to measure the latency of nthread 9 * requeues without waking up any tasks (in the non-pi case) -- thus 10 * mimicking a regular futex_wait. 11 */ 12 13 /* For the CLR_() macros */ 14 #include <string.h> 15 #include <pthread.h> 16 17 #include <signal.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 static u_int32_t futex1 = 0, futex2 = 0; 34 35 static pthread_t *worker; 36 static bool done = false; 37 static pthread_mutex_t thread_lock; 38 static pthread_cond_t thread_parent, thread_worker; 39 static struct stats requeuetime_stats, requeued_stats; 40 static unsigned int threads_starting; 41 static int futex_flag = 0; 42 43 static struct bench_futex_parameters params = { 44 /* 45 * How many tasks to requeue at a time. 46 * Default to 1 in order to make the kernel work more. 47 */ 48 .nrequeue = 1, 49 }; 50 51 static const struct option options[] = { 52 OPT_UINTEGER('t', "threads", ¶ms.nthreads, "Specify amount of threads"), 53 OPT_UINTEGER('q', "nrequeue", ¶ms.nrequeue, "Specify amount of threads to requeue at once"), 54 OPT_BOOLEAN( 's', "silent", ¶ms.silent, "Silent mode: do not display data/details"), 55 OPT_BOOLEAN( 'S', "shared", ¶ms.fshared, "Use shared futexes instead of private ones"), 56 OPT_BOOLEAN( 'm', "mlockall", ¶ms.mlockall, "Lock all current and future memory"), 57 OPT_BOOLEAN( 'B', "broadcast", ¶ms.broadcast, "Requeue all threads at once"), 58 OPT_BOOLEAN( 'p', "pi", ¶ms.pi, "Use PI-aware variants of FUTEX_CMP_REQUEUE"), 59 60 OPT_END() 61 }; 62 63 static const char * const bench_futex_requeue_usage[] = { 64 "perf bench futex requeue <options>", 65 NULL 66 }; 67 68 static void print_summary(void) 69 { 70 double requeuetime_avg = avg_stats(&requeuetime_stats); 71 double requeuetime_stddev = stddev_stats(&requeuetime_stats); 72 unsigned int requeued_avg = avg_stats(&requeued_stats); 73 74 printf("Requeued %d of %d threads in %.4f ms (+-%.2f%%)\n", 75 requeued_avg, 76 params.nthreads, 77 requeuetime_avg / USEC_PER_MSEC, 78 rel_stddev_stats(requeuetime_stddev, requeuetime_avg)); 79 } 80 81 static void *workerfn(void *arg __maybe_unused) 82 { 83 int ret; 84 85 pthread_mutex_lock(&thread_lock); 86 threads_starting--; 87 if (!threads_starting) 88 pthread_cond_signal(&thread_parent); 89 pthread_cond_wait(&thread_worker, &thread_lock); 90 pthread_mutex_unlock(&thread_lock); 91 92 while (1) { 93 if (!params.pi) { 94 ret = futex_wait(&futex1, 0, NULL, futex_flag); 95 if (!ret) 96 break; 97 98 if (ret && errno != EAGAIN) { 99 if (!params.silent) 100 warnx("futex_wait"); 101 break; 102 } 103 } else { 104 ret = futex_wait_requeue_pi(&futex1, 0, &futex2, 105 NULL, futex_flag); 106 if (!ret) { 107 /* got the lock at futex2 */ 108 futex_unlock_pi(&futex2, futex_flag); 109 break; 110 } 111 112 if (ret && errno != EAGAIN) { 113 if (!params.silent) 114 warnx("futex_wait_requeue_pi"); 115 break; 116 } 117 } 118 } 119 120 return NULL; 121 } 122 123 static void block_threads(pthread_t *w, 124 pthread_attr_t thread_attr, struct perf_cpu_map *cpu) 125 { 126 cpu_set_t cpuset; 127 unsigned int i; 128 129 threads_starting = params.nthreads; 130 131 /* create and block all threads */ 132 for (i = 0; i < params.nthreads; i++) { 133 CPU_ZERO(&cpuset); 134 CPU_SET(perf_cpu_map__cpu(cpu, i % perf_cpu_map__nr(cpu)).cpu, &cpuset); 135 136 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset)) 137 err(EXIT_FAILURE, "pthread_attr_setaffinity_np"); 138 139 if (pthread_create(&w[i], &thread_attr, workerfn, NULL)) 140 err(EXIT_FAILURE, "pthread_create"); 141 } 142 } 143 144 static void toggle_done(int sig __maybe_unused, 145 siginfo_t *info __maybe_unused, 146 void *uc __maybe_unused) 147 { 148 done = true; 149 } 150 151 int bench_futex_requeue(int argc, const char **argv) 152 { 153 int ret = 0; 154 unsigned int i, j; 155 struct sigaction act; 156 pthread_attr_t thread_attr; 157 struct perf_cpu_map *cpu; 158 159 argc = parse_options(argc, argv, options, bench_futex_requeue_usage, 0); 160 if (argc) 161 goto err; 162 163 cpu = perf_cpu_map__new(NULL); 164 if (!cpu) 165 err(EXIT_FAILURE, "cpu_map__new"); 166 167 memset(&act, 0, sizeof(act)); 168 sigfillset(&act.sa_mask); 169 act.sa_sigaction = toggle_done; 170 sigaction(SIGINT, &act, NULL); 171 172 if (params.mlockall) { 173 if (mlockall(MCL_CURRENT | MCL_FUTURE)) 174 err(EXIT_FAILURE, "mlockall"); 175 } 176 177 if (!params.nthreads) 178 params.nthreads = perf_cpu_map__nr(cpu); 179 180 worker = calloc(params.nthreads, sizeof(*worker)); 181 if (!worker) 182 err(EXIT_FAILURE, "calloc"); 183 184 if (!params.fshared) 185 futex_flag = FUTEX_PRIVATE_FLAG; 186 187 if (params.nrequeue > params.nthreads) 188 params.nrequeue = params.nthreads; 189 190 if (params.broadcast) 191 params.nrequeue = params.nthreads; 192 193 printf("Run summary [PID %d]: Requeuing %d threads (from [%s] %p to %s%p), " 194 "%d at a time.\n\n", getpid(), params.nthreads, 195 params.fshared ? "shared":"private", &futex1, 196 params.pi ? "PI ": "", &futex2, params.nrequeue); 197 198 init_stats(&requeued_stats); 199 init_stats(&requeuetime_stats); 200 pthread_attr_init(&thread_attr); 201 pthread_mutex_init(&thread_lock, NULL); 202 pthread_cond_init(&thread_parent, NULL); 203 pthread_cond_init(&thread_worker, NULL); 204 205 for (j = 0; j < bench_repeat && !done; j++) { 206 unsigned int nrequeued = 0, wakeups = 0; 207 struct timeval start, end, runtime; 208 209 /* create, launch & block all threads */ 210 block_threads(worker, thread_attr, cpu); 211 212 /* make sure all threads are already blocked */ 213 pthread_mutex_lock(&thread_lock); 214 while (threads_starting) 215 pthread_cond_wait(&thread_parent, &thread_lock); 216 pthread_cond_broadcast(&thread_worker); 217 pthread_mutex_unlock(&thread_lock); 218 219 usleep(100000); 220 221 /* Ok, all threads are patiently blocked, start requeueing */ 222 gettimeofday(&start, NULL); 223 while (nrequeued < params.nthreads) { 224 int r; 225 226 /* 227 * For the regular non-pi case, do not wakeup any tasks 228 * blocked on futex1, allowing us to really measure 229 * futex_wait functionality. For the PI case the first 230 * waiter is always awoken. 231 */ 232 if (!params.pi) { 233 r = futex_cmp_requeue(&futex1, 0, &futex2, 0, 234 params.nrequeue, 235 futex_flag); 236 } else { 237 r = futex_cmp_requeue_pi(&futex1, 0, &futex2, 238 params.nrequeue, 239 futex_flag); 240 wakeups++; /* assume no error */ 241 } 242 243 if (r < 0) 244 err(EXIT_FAILURE, "couldn't requeue from %p to %p", 245 &futex1, &futex2); 246 247 nrequeued += r; 248 } 249 250 gettimeofday(&end, NULL); 251 timersub(&end, &start, &runtime); 252 253 update_stats(&requeued_stats, nrequeued); 254 update_stats(&requeuetime_stats, runtime.tv_usec); 255 256 if (!params.silent) { 257 if (!params.pi) 258 printf("[Run %d]: Requeued %d of %d threads in " 259 "%.4f ms\n", j + 1, nrequeued, 260 params.nthreads, 261 runtime.tv_usec / (double)USEC_PER_MSEC); 262 else { 263 nrequeued -= wakeups; 264 printf("[Run %d]: Awoke and Requeued (%d+%d) of " 265 "%d threads in %.4f ms\n", 266 j + 1, wakeups, nrequeued, 267 params.nthreads, 268 runtime.tv_usec / (double)USEC_PER_MSEC); 269 } 270 271 } 272 273 if (!params.pi) { 274 /* everybody should be blocked on futex2, wake'em up */ 275 nrequeued = futex_wake(&futex2, nrequeued, futex_flag); 276 if (params.nthreads != nrequeued) 277 warnx("couldn't wakeup all tasks (%d/%d)", 278 nrequeued, params.nthreads); 279 } 280 281 for (i = 0; i < params.nthreads; i++) { 282 ret = pthread_join(worker[i], NULL); 283 if (ret) 284 err(EXIT_FAILURE, "pthread_join"); 285 } 286 } 287 288 /* cleanup & report results */ 289 pthread_cond_destroy(&thread_parent); 290 pthread_cond_destroy(&thread_worker); 291 pthread_mutex_destroy(&thread_lock); 292 pthread_attr_destroy(&thread_attr); 293 294 print_summary(); 295 296 free(worker); 297 perf_cpu_map__put(cpu); 298 return ret; 299 err: 300 usage_with_options(bench_futex_requeue_usage, options); 301 exit(EXIT_FAILURE); 302 } 303