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