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