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 -- thus mimicking a regular futex_wait. 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 <internal/cpumap.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 32 static u_int32_t futex1 = 0, futex2 = 0; 33 34 /* 35 * How many tasks to requeue at a time. 36 * Default to 1 in order to make the kernel work more. 37 */ 38 static unsigned int nrequeue = 1; 39 40 static pthread_t *worker; 41 static bool done = false, silent = false, fshared = false; 42 static pthread_mutex_t thread_lock; 43 static pthread_cond_t thread_parent, thread_worker; 44 static struct stats requeuetime_stats, requeued_stats; 45 static unsigned int threads_starting, nthreads = 0; 46 static int futex_flag = 0; 47 48 static const struct option options[] = { 49 OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"), 50 OPT_UINTEGER('q', "nrequeue", &nrequeue, "Specify amount of threads to requeue at once"), 51 OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"), 52 OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"), 53 OPT_END() 54 }; 55 56 static const char * const bench_futex_requeue_usage[] = { 57 "perf bench futex requeue <options>", 58 NULL 59 }; 60 61 static void print_summary(void) 62 { 63 double requeuetime_avg = avg_stats(&requeuetime_stats); 64 double requeuetime_stddev = stddev_stats(&requeuetime_stats); 65 unsigned int requeued_avg = avg_stats(&requeued_stats); 66 67 printf("Requeued %d of %d threads in %.4f ms (+-%.2f%%)\n", 68 requeued_avg, 69 nthreads, 70 requeuetime_avg / USEC_PER_MSEC, 71 rel_stddev_stats(requeuetime_stddev, requeuetime_avg)); 72 } 73 74 static void *workerfn(void *arg __maybe_unused) 75 { 76 pthread_mutex_lock(&thread_lock); 77 threads_starting--; 78 if (!threads_starting) 79 pthread_cond_signal(&thread_parent); 80 pthread_cond_wait(&thread_worker, &thread_lock); 81 pthread_mutex_unlock(&thread_lock); 82 83 futex_wait(&futex1, 0, NULL, futex_flag); 84 return NULL; 85 } 86 87 static void block_threads(pthread_t *w, 88 pthread_attr_t thread_attr, struct perf_cpu_map *cpu) 89 { 90 cpu_set_t cpuset; 91 unsigned int i; 92 93 threads_starting = nthreads; 94 95 /* create and block all threads */ 96 for (i = 0; i < nthreads; i++) { 97 CPU_ZERO(&cpuset); 98 CPU_SET(cpu->map[i % cpu->nr], &cpuset); 99 100 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset)) 101 err(EXIT_FAILURE, "pthread_attr_setaffinity_np"); 102 103 if (pthread_create(&w[i], &thread_attr, workerfn, NULL)) 104 err(EXIT_FAILURE, "pthread_create"); 105 } 106 } 107 108 static void toggle_done(int sig __maybe_unused, 109 siginfo_t *info __maybe_unused, 110 void *uc __maybe_unused) 111 { 112 done = true; 113 } 114 115 int bench_futex_requeue(int argc, const char **argv) 116 { 117 int ret = 0; 118 unsigned int i, j; 119 struct sigaction act; 120 pthread_attr_t thread_attr; 121 struct perf_cpu_map *cpu; 122 123 argc = parse_options(argc, argv, options, bench_futex_requeue_usage, 0); 124 if (argc) 125 goto err; 126 127 cpu = perf_cpu_map__new(NULL); 128 if (!cpu) 129 err(EXIT_FAILURE, "cpu_map__new"); 130 131 memset(&act, 0, sizeof(act)); 132 sigfillset(&act.sa_mask); 133 act.sa_sigaction = toggle_done; 134 sigaction(SIGINT, &act, NULL); 135 136 if (!nthreads) 137 nthreads = cpu->nr; 138 139 worker = calloc(nthreads, sizeof(*worker)); 140 if (!worker) 141 err(EXIT_FAILURE, "calloc"); 142 143 if (!fshared) 144 futex_flag = FUTEX_PRIVATE_FLAG; 145 146 if (nrequeue > nthreads) 147 nrequeue = nthreads; 148 149 printf("Run summary [PID %d]: Requeuing %d threads (from [%s] %p to %p), " 150 "%d at a time.\n\n", getpid(), nthreads, 151 fshared ? "shared":"private", &futex1, &futex2, nrequeue); 152 153 init_stats(&requeued_stats); 154 init_stats(&requeuetime_stats); 155 pthread_attr_init(&thread_attr); 156 pthread_mutex_init(&thread_lock, NULL); 157 pthread_cond_init(&thread_parent, NULL); 158 pthread_cond_init(&thread_worker, NULL); 159 160 for (j = 0; j < bench_repeat && !done; j++) { 161 unsigned int nrequeued = 0; 162 struct timeval start, end, runtime; 163 164 /* create, launch & block all threads */ 165 block_threads(worker, thread_attr, cpu); 166 167 /* make sure all threads are already blocked */ 168 pthread_mutex_lock(&thread_lock); 169 while (threads_starting) 170 pthread_cond_wait(&thread_parent, &thread_lock); 171 pthread_cond_broadcast(&thread_worker); 172 pthread_mutex_unlock(&thread_lock); 173 174 usleep(100000); 175 176 /* Ok, all threads are patiently blocked, start requeueing */ 177 gettimeofday(&start, NULL); 178 while (nrequeued < nthreads) { 179 /* 180 * Do not wakeup any tasks blocked on futex1, allowing 181 * us to really measure futex_wait functionality. 182 */ 183 nrequeued += futex_cmp_requeue(&futex1, 0, &futex2, 0, 184 nrequeue, futex_flag); 185 } 186 187 gettimeofday(&end, NULL); 188 timersub(&end, &start, &runtime); 189 190 update_stats(&requeued_stats, nrequeued); 191 update_stats(&requeuetime_stats, runtime.tv_usec); 192 193 if (!silent) { 194 printf("[Run %d]: Requeued %d of %d threads in %.4f ms\n", 195 j + 1, nrequeued, nthreads, runtime.tv_usec / (double)USEC_PER_MSEC); 196 } 197 198 /* everybody should be blocked on futex2, wake'em up */ 199 nrequeued = futex_wake(&futex2, nrequeued, futex_flag); 200 if (nthreads != nrequeued) 201 warnx("couldn't wakeup all tasks (%d/%d)", nrequeued, nthreads); 202 203 for (i = 0; i < nthreads; i++) { 204 ret = pthread_join(worker[i], NULL); 205 if (ret) 206 err(EXIT_FAILURE, "pthread_join"); 207 } 208 } 209 210 /* cleanup & report results */ 211 pthread_cond_destroy(&thread_parent); 212 pthread_cond_destroy(&thread_worker); 213 pthread_mutex_destroy(&thread_lock); 214 pthread_attr_destroy(&thread_attr); 215 216 print_summary(); 217 218 free(worker); 219 return ret; 220 err: 221 usage_with_options(bench_futex_requeue_usage, options); 222 exit(EXIT_FAILURE); 223 } 224