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