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