Davidlohr Bueso | d2f3f5d | 2015-07-07 01:55:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Davidlohr Bueso. |
| 3 | */ |
| 4 | |
Arnaldo Carvalho de Melo | 8a15858 | 2016-07-06 12:14:56 -0300 | [diff] [blame] | 5 | /* For the CLR_() macros */ |
Arnaldo Carvalho de Melo | a0f213e | 2017-03-02 15:44:19 -0300 | [diff] [blame] | 6 | #include <string.h> |
Arnaldo Carvalho de Melo | 8a15858 | 2016-07-06 12:14:56 -0300 | [diff] [blame] | 7 | #include <pthread.h> |
| 8 | |
Arnaldo Carvalho de Melo | 9c304f6 | 2016-07-07 11:01:46 -0300 | [diff] [blame] | 9 | #include <signal.h> |
Davidlohr Bueso | d2f3f5d | 2015-07-07 01:55:53 -0700 | [diff] [blame] | 10 | #include "../util/stat.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 11 | #include <subcmd/parse-options.h> |
Arnaldo Carvalho de Melo | 86695f5 | 2016-07-11 10:05:52 -0300 | [diff] [blame] | 12 | #include <linux/compiler.h> |
Arnaldo Carvalho de Melo | 9c304f6 | 2016-07-07 11:01:46 -0300 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <errno.h> |
Davidlohr Bueso | d2f3f5d | 2015-07-07 01:55:53 -0700 | [diff] [blame] | 15 | #include "bench.h" |
| 16 | #include "futex.h" |
| 17 | |
| 18 | #include <err.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <sys/time.h> |
Davidlohr Bueso | d2f3f5d | 2015-07-07 01:55:53 -0700 | [diff] [blame] | 21 | |
| 22 | struct worker { |
| 23 | int tid; |
| 24 | u_int32_t *futex; |
| 25 | pthread_t thread; |
| 26 | unsigned long ops; |
| 27 | }; |
| 28 | |
| 29 | static u_int32_t global_futex = 0; |
| 30 | static struct worker *worker; |
| 31 | static unsigned int nsecs = 10; |
| 32 | static bool silent = false, multi = false; |
| 33 | static bool done = false, fshared = false; |
| 34 | static unsigned int ncpus, nthreads = 0; |
| 35 | static int futex_flag = 0; |
| 36 | struct timeval start, end, runtime; |
| 37 | static pthread_mutex_t thread_lock; |
| 38 | static unsigned int threads_starting; |
| 39 | static struct stats throughput_stats; |
| 40 | static pthread_cond_t thread_parent, thread_worker; |
| 41 | |
| 42 | static const struct option options[] = { |
| 43 | OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"), |
| 44 | OPT_UINTEGER('r', "runtime", &nsecs, "Specify runtime (in seconds)"), |
| 45 | OPT_BOOLEAN( 'M', "multi", &multi, "Use multiple futexes"), |
| 46 | OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"), |
| 47 | OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"), |
| 48 | OPT_END() |
| 49 | }; |
| 50 | |
| 51 | static const char * const bench_futex_lock_pi_usage[] = { |
Davidlohr Bueso | 9de3ffa | 2016-12-15 11:36:24 -0800 | [diff] [blame] | 52 | "perf bench futex lock-pi <options>", |
Davidlohr Bueso | d2f3f5d | 2015-07-07 01:55:53 -0700 | [diff] [blame] | 53 | NULL |
| 54 | }; |
| 55 | |
| 56 | static void print_summary(void) |
| 57 | { |
| 58 | unsigned long avg = avg_stats(&throughput_stats); |
| 59 | double stddev = stddev_stats(&throughput_stats); |
| 60 | |
| 61 | printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n", |
| 62 | !silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg), |
| 63 | (int) runtime.tv_sec); |
| 64 | } |
| 65 | |
| 66 | static void toggle_done(int sig __maybe_unused, |
| 67 | siginfo_t *info __maybe_unused, |
| 68 | void *uc __maybe_unused) |
| 69 | { |
| 70 | /* inform all threads that we're done for the day */ |
| 71 | done = true; |
| 72 | gettimeofday(&end, NULL); |
| 73 | timersub(&end, &start, &runtime); |
| 74 | } |
| 75 | |
| 76 | static void *workerfn(void *arg) |
| 77 | { |
| 78 | struct worker *w = (struct worker *) arg; |
Davidlohr Bueso | e2e1680 | 2016-10-24 13:56:52 -0700 | [diff] [blame] | 79 | unsigned long ops = w->ops; |
Davidlohr Bueso | d2f3f5d | 2015-07-07 01:55:53 -0700 | [diff] [blame] | 80 | |
| 81 | pthread_mutex_lock(&thread_lock); |
| 82 | threads_starting--; |
| 83 | if (!threads_starting) |
| 84 | pthread_cond_signal(&thread_parent); |
| 85 | pthread_cond_wait(&thread_worker, &thread_lock); |
| 86 | pthread_mutex_unlock(&thread_lock); |
| 87 | |
| 88 | do { |
| 89 | int ret; |
| 90 | again: |
Davidlohr Bueso | 73b1794 | 2016-04-20 20:14:07 -0700 | [diff] [blame] | 91 | ret = futex_lock_pi(w->futex, NULL, futex_flag); |
Davidlohr Bueso | d2f3f5d | 2015-07-07 01:55:53 -0700 | [diff] [blame] | 92 | |
| 93 | if (ret) { /* handle lock acquisition */ |
| 94 | if (!silent) |
| 95 | warn("thread %d: Could not lock pi-lock for %p (%d)", |
| 96 | w->tid, w->futex, ret); |
| 97 | if (done) |
| 98 | break; |
| 99 | |
| 100 | goto again; |
| 101 | } |
| 102 | |
| 103 | usleep(1); |
| 104 | ret = futex_unlock_pi(w->futex, futex_flag); |
| 105 | if (ret && !silent) |
| 106 | warn("thread %d: Could not unlock pi-lock for %p (%d)", |
| 107 | w->tid, w->futex, ret); |
Davidlohr Bueso | e2e1680 | 2016-10-24 13:56:52 -0700 | [diff] [blame] | 108 | ops++; /* account for thread's share of work */ |
Davidlohr Bueso | d2f3f5d | 2015-07-07 01:55:53 -0700 | [diff] [blame] | 109 | } while (!done); |
| 110 | |
Davidlohr Bueso | e2e1680 | 2016-10-24 13:56:52 -0700 | [diff] [blame] | 111 | w->ops = ops; |
Davidlohr Bueso | d2f3f5d | 2015-07-07 01:55:53 -0700 | [diff] [blame] | 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | static void create_threads(struct worker *w, pthread_attr_t thread_attr) |
| 116 | { |
| 117 | cpu_set_t cpu; |
| 118 | unsigned int i; |
| 119 | |
| 120 | threads_starting = nthreads; |
| 121 | |
| 122 | for (i = 0; i < nthreads; i++) { |
| 123 | worker[i].tid = i; |
| 124 | |
| 125 | if (multi) { |
| 126 | worker[i].futex = calloc(1, sizeof(u_int32_t)); |
| 127 | if (!worker[i].futex) |
| 128 | err(EXIT_FAILURE, "calloc"); |
| 129 | } else |
| 130 | worker[i].futex = &global_futex; |
| 131 | |
| 132 | CPU_ZERO(&cpu); |
| 133 | CPU_SET(i % ncpus, &cpu); |
| 134 | |
| 135 | if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu)) |
| 136 | err(EXIT_FAILURE, "pthread_attr_setaffinity_np"); |
| 137 | |
| 138 | if (pthread_create(&w[i].thread, &thread_attr, workerfn, &worker[i])) |
| 139 | err(EXIT_FAILURE, "pthread_create"); |
| 140 | } |
| 141 | } |
| 142 | |
Arnaldo Carvalho de Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 143 | int bench_futex_lock_pi(int argc, const char **argv) |
Davidlohr Bueso | d2f3f5d | 2015-07-07 01:55:53 -0700 | [diff] [blame] | 144 | { |
| 145 | int ret = 0; |
| 146 | unsigned int i; |
| 147 | struct sigaction act; |
| 148 | pthread_attr_t thread_attr; |
| 149 | |
| 150 | argc = parse_options(argc, argv, options, bench_futex_lock_pi_usage, 0); |
| 151 | if (argc) |
| 152 | goto err; |
| 153 | |
| 154 | ncpus = sysconf(_SC_NPROCESSORS_ONLN); |
| 155 | |
| 156 | sigfillset(&act.sa_mask); |
| 157 | act.sa_sigaction = toggle_done; |
| 158 | sigaction(SIGINT, &act, NULL); |
| 159 | |
| 160 | if (!nthreads) |
| 161 | nthreads = ncpus; |
Davidlohr Bueso | d2f3f5d | 2015-07-07 01:55:53 -0700 | [diff] [blame] | 162 | |
| 163 | worker = calloc(nthreads, sizeof(*worker)); |
| 164 | if (!worker) |
| 165 | err(EXIT_FAILURE, "calloc"); |
| 166 | |
| 167 | if (!fshared) |
| 168 | futex_flag = FUTEX_PRIVATE_FLAG; |
| 169 | |
| 170 | printf("Run summary [PID %d]: %d threads doing pi lock/unlock pairing for %d secs.\n\n", |
| 171 | getpid(), nthreads, nsecs); |
| 172 | |
| 173 | init_stats(&throughput_stats); |
| 174 | pthread_mutex_init(&thread_lock, NULL); |
| 175 | pthread_cond_init(&thread_parent, NULL); |
| 176 | pthread_cond_init(&thread_worker, NULL); |
| 177 | |
| 178 | threads_starting = nthreads; |
| 179 | pthread_attr_init(&thread_attr); |
| 180 | gettimeofday(&start, NULL); |
| 181 | |
| 182 | create_threads(worker, thread_attr); |
| 183 | pthread_attr_destroy(&thread_attr); |
| 184 | |
| 185 | pthread_mutex_lock(&thread_lock); |
| 186 | while (threads_starting) |
| 187 | pthread_cond_wait(&thread_parent, &thread_lock); |
| 188 | pthread_cond_broadcast(&thread_worker); |
| 189 | pthread_mutex_unlock(&thread_lock); |
| 190 | |
| 191 | sleep(nsecs); |
| 192 | toggle_done(0, NULL, NULL); |
| 193 | |
| 194 | for (i = 0; i < nthreads; i++) { |
| 195 | ret = pthread_join(worker[i].thread, NULL); |
| 196 | if (ret) |
| 197 | err(EXIT_FAILURE, "pthread_join"); |
| 198 | } |
| 199 | |
| 200 | /* cleanup & report results */ |
| 201 | pthread_cond_destroy(&thread_parent); |
| 202 | pthread_cond_destroy(&thread_worker); |
| 203 | pthread_mutex_destroy(&thread_lock); |
| 204 | |
| 205 | for (i = 0; i < nthreads; i++) { |
| 206 | unsigned long t = worker[i].ops/runtime.tv_sec; |
| 207 | |
| 208 | update_stats(&throughput_stats, t); |
| 209 | if (!silent) |
| 210 | printf("[thread %3d] futex: %p [ %ld ops/sec ]\n", |
| 211 | worker[i].tid, worker[i].futex, t); |
| 212 | |
| 213 | if (multi) |
| 214 | free(worker[i].futex); |
| 215 | } |
| 216 | |
| 217 | print_summary(); |
| 218 | |
| 219 | free(worker); |
| 220 | return ret; |
| 221 | err: |
| 222 | usage_with_options(bench_futex_lock_pi_usage, options); |
| 223 | exit(EXIT_FAILURE); |
| 224 | } |