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