aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorDavidlohr Bueso <davidlohr@hp.com>2013-12-14 23:31:57 -0500
committerArnaldo Carvalho de Melo <acme@redhat.com>2014-03-14 10:20:44 -0400
commit0fb298cf95c0d8119557b7d4657724a146e0622e (patch)
tree373a4bd7d4117fd6e49d0750fb709368631594e5 /tools
parent27db78307481dbba68c5f3563c6cb694b25521d9 (diff)
perf bench: Add futex-requeue microbenchmark
Block a bunch of threads on a futex and requeue them on another, N at a time. This program is particularly useful to measure the latency of nthread requeues without waking up any tasks -- thus mimicking a regular futex_wait. An example run: $ perf bench futex requeue -r 100 -t 64 Run summary [PID 151011]: Requeuing 64 threads (from 0x7d15c4 to 0x7d15c8), 1 at a time. [Run 1]: Requeued 64 of 64 threads in 0.0400 ms [Run 2]: Requeued 64 of 64 threads in 0.0390 ms [Run 3]: Requeued 64 of 64 threads in 0.0400 ms ... [Run 100]: Requeued 64 of 64 threads in 0.0390 ms Requeued 64 of 64 threads in 0.0399 ms (+-0.37%) Signed-off-by: Davidlohr Bueso <davidlohr@hp.com> Acked-by: Darren Hart <dvhart@linux.intel.com> Cc: Aswin Chandramouleeswaran <aswin@hp.com> Cc: Darren Hart <dvhart@linux.intel.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Jason Low <jason.low2@hp.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Scott J Norton <scott.norton@hp.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Waiman Long <Waiman.Long@hp.com> Link: http://lkml.kernel.org/r/1387081917-9102-4-git-send-email-davidlohr@hp.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/perf/Makefile.perf1
-rw-r--r--tools/perf/bench/bench.h1
-rw-r--r--tools/perf/bench/futex-requeue.c211
-rw-r--r--tools/perf/bench/futex.h13
-rw-r--r--tools/perf/builtin-bench.c1
5 files changed, 227 insertions, 0 deletions
diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
index 6fa5d8b74635..50d875d970c4 100644
--- a/tools/perf/Makefile.perf
+++ b/tools/perf/Makefile.perf
@@ -428,6 +428,7 @@ BUILTIN_OBJS += $(OUTPUT)bench/mem-memcpy.o
428BUILTIN_OBJS += $(OUTPUT)bench/mem-memset.o 428BUILTIN_OBJS += $(OUTPUT)bench/mem-memset.o
429BUILTIN_OBJS += $(OUTPUT)bench/futex-hash.o 429BUILTIN_OBJS += $(OUTPUT)bench/futex-hash.o
430BUILTIN_OBJS += $(OUTPUT)bench/futex-wake.o 430BUILTIN_OBJS += $(OUTPUT)bench/futex-wake.o
431BUILTIN_OBJS += $(OUTPUT)bench/futex-requeue.o
431 432
432BUILTIN_OBJS += $(OUTPUT)builtin-diff.o 433BUILTIN_OBJS += $(OUTPUT)builtin-diff.o
433BUILTIN_OBJS += $(OUTPUT)builtin-evlist.o 434BUILTIN_OBJS += $(OUTPUT)builtin-evlist.o
diff --git a/tools/perf/bench/bench.h b/tools/perf/bench/bench.h
index 6ac3f1d083cc..eba46709b279 100644
--- a/tools/perf/bench/bench.h
+++ b/tools/perf/bench/bench.h
@@ -33,6 +33,7 @@ extern int bench_mem_memcpy(int argc, const char **argv,
33extern int bench_mem_memset(int argc, const char **argv, const char *prefix); 33extern int bench_mem_memset(int argc, const char **argv, const char *prefix);
34extern int bench_futex_hash(int argc, const char **argv, const char *prefix); 34extern int bench_futex_hash(int argc, const char **argv, const char *prefix);
35extern int bench_futex_wake(int argc, const char **argv, const char *prefix); 35extern int bench_futex_wake(int argc, const char **argv, const char *prefix);
36extern int bench_futex_requeue(int argc, const char **argv, const char *prefix);
36 37
37#define BENCH_FORMAT_DEFAULT_STR "default" 38#define BENCH_FORMAT_DEFAULT_STR "default"
38#define BENCH_FORMAT_DEFAULT 0 39#define BENCH_FORMAT_DEFAULT 0
diff --git a/tools/perf/bench/futex-requeue.c b/tools/perf/bench/futex-requeue.c
new file mode 100644
index 000000000000..a16255876f1d
--- /dev/null
+++ b/tools/perf/bench/futex-requeue.c
@@ -0,0 +1,211 @@
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
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
32/*
33 * There can be significant variance from run to run,
34 * the more repeats, the more exact the overall avg and
35 * the better idea of the futex latency.
36 */
37static unsigned int repeat = 10;
38
39static pthread_t *worker;
40static bool done = 0, silent = 0;
41static pthread_mutex_t thread_lock;
42static pthread_cond_t thread_parent, thread_worker;
43static struct stats requeuetime_stats, requeued_stats;
44static unsigned int ncpus, threads_starting, nthreads = 0;
45
46static const struct option options[] = {
47 OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"),
48 OPT_UINTEGER('q', "nrequeue", &nrequeue, "Specify amount of threads to requeue at once"),
49 OPT_UINTEGER('r', "repeat", &repeat, "Specify amount of times to repeat the run"),
50 OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"),
51 OPT_END()
52};
53
54static const char * const bench_futex_requeue_usage[] = {
55 "perf bench futex requeue <options>",
56 NULL
57};
58
59static void print_summary(void)
60{
61 double requeuetime_avg = avg_stats(&requeuetime_stats);
62 double requeuetime_stddev = stddev_stats(&requeuetime_stats);
63 unsigned int requeued_avg = avg_stats(&requeued_stats);
64
65 printf("Requeued %d of %d threads in %.4f ms (+-%.2f%%)\n",
66 requeued_avg,
67 nthreads,
68 requeuetime_avg/1e3,
69 rel_stddev_stats(requeuetime_stddev, requeuetime_avg));
70}
71
72static void *workerfn(void *arg __maybe_unused)
73{
74 pthread_mutex_lock(&thread_lock);
75 threads_starting--;
76 if (!threads_starting)
77 pthread_cond_signal(&thread_parent);
78 pthread_cond_wait(&thread_worker, &thread_lock);
79 pthread_mutex_unlock(&thread_lock);
80
81 futex_wait(&futex1, 0, NULL, FUTEX_PRIVATE_FLAG);
82 return NULL;
83}
84
85static void block_threads(pthread_t *w,
86 pthread_attr_t thread_attr)
87{
88 cpu_set_t cpu;
89 unsigned int i;
90
91 threads_starting = nthreads;
92
93 /* create and block all threads */
94 for (i = 0; i < nthreads; i++) {
95 CPU_ZERO(&cpu);
96 CPU_SET(i % ncpus, &cpu);
97
98 if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu))
99 err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
100
101 if (pthread_create(&w[i], &thread_attr, workerfn, NULL))
102 err(EXIT_FAILURE, "pthread_create");
103 }
104}
105
106static void toggle_done(int sig __maybe_unused,
107 siginfo_t *info __maybe_unused,
108 void *uc __maybe_unused)
109{
110 done = true;
111}
112
113int bench_futex_requeue(int argc, const char **argv,
114 const char *prefix __maybe_unused)
115{
116 int ret = 0;
117 unsigned int i, j;
118 struct sigaction act;
119 pthread_attr_t thread_attr;
120
121 argc = parse_options(argc, argv, options, bench_futex_requeue_usage, 0);
122 if (argc)
123 goto err;
124
125 ncpus = sysconf(_SC_NPROCESSORS_ONLN);
126
127 sigfillset(&act.sa_mask);
128 act.sa_sigaction = toggle_done;
129 sigaction(SIGINT, &act, NULL);
130
131 if (!nthreads)
132 nthreads = ncpus;
133
134 worker = calloc(nthreads, sizeof(*worker));
135 if (!worker)
136 err(EXIT_FAILURE, "calloc");
137
138 printf("Run summary [PID %d]: Requeuing %d threads (from %p to %p), "
139 "%d at a time.\n\n",
140 getpid(), nthreads, &futex1, &futex2, nrequeue);
141
142 init_stats(&requeued_stats);
143 init_stats(&requeuetime_stats);
144 pthread_attr_init(&thread_attr);
145 pthread_mutex_init(&thread_lock, NULL);
146 pthread_cond_init(&thread_parent, NULL);
147 pthread_cond_init(&thread_worker, NULL);
148
149 for (j = 0; j < repeat && !done; j++) {
150 unsigned int nrequeued = 0;
151 struct timeval start, end, runtime;
152
153 /* create, launch & block all threads */
154 block_threads(worker, thread_attr);
155
156 /* make sure all threads are already blocked */
157 pthread_mutex_lock(&thread_lock);
158 while (threads_starting)
159 pthread_cond_wait(&thread_parent, &thread_lock);
160 pthread_cond_broadcast(&thread_worker);
161 pthread_mutex_unlock(&thread_lock);
162
163 usleep(100000);
164
165 /* Ok, all threads are patiently blocked, start requeueing */
166 gettimeofday(&start, NULL);
167 for (nrequeued = 0; nrequeued < nthreads; nrequeued += nrequeue)
168 /*
169 * Do not wakeup any tasks blocked on futex1, allowing
170 * us to really measure futex_wait functionality.
171 */
172 futex_cmp_requeue(&futex1, 0, &futex2, 0, nrequeue,
173 FUTEX_PRIVATE_FLAG);
174 gettimeofday(&end, NULL);
175 timersub(&end, &start, &runtime);
176
177 update_stats(&requeued_stats, nrequeued);
178 update_stats(&requeuetime_stats, runtime.tv_usec);
179
180 if (!silent) {
181 printf("[Run %d]: Requeued %d of %d threads in %.4f ms\n",
182 j + 1, nrequeued, nthreads, runtime.tv_usec/1e3);
183 }
184
185 /* everybody should be blocked on futex2, wake'em up */
186 nrequeued = futex_wake(&futex2, nthreads, FUTEX_PRIVATE_FLAG);
187 if (nthreads != nrequeued)
188 warnx("couldn't wakeup all tasks (%d/%d)", nrequeued, nthreads);
189
190 for (i = 0; i < nthreads; i++) {
191 ret = pthread_join(worker[i], NULL);
192 if (ret)
193 err(EXIT_FAILURE, "pthread_join");
194 }
195
196 }
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}
diff --git a/tools/perf/bench/futex.h b/tools/perf/bench/futex.h
index 6ac45093fac4..71f2844cf97f 100644
--- a/tools/perf/bench/futex.h
+++ b/tools/perf/bench/futex.h
@@ -55,4 +55,17 @@ futex_wake(u_int32_t *uaddr, int nr_wake, int opflags)
55 return futex(uaddr, FUTEX_WAKE, nr_wake, NULL, NULL, 0, opflags); 55 return futex(uaddr, FUTEX_WAKE, nr_wake, NULL, NULL, 0, opflags);
56} 56}
57 57
58/**
59* futex_cmp_requeue() - requeue tasks from uaddr to uaddr2
60* @nr_wake: wake up to this many tasks
61* @nr_requeue: requeue up to this many tasks
62*/
63static inline int
64futex_cmp_requeue(u_int32_t *uaddr, u_int32_t val, u_int32_t *uaddr2, int nr_wake,
65 int nr_requeue, int opflags)
66{
67 return futex(uaddr, FUTEX_CMP_REQUEUE, nr_wake, nr_requeue, uaddr2,
68 val, opflags);
69}
70
58#endif /* _FUTEX_H */ 71#endif /* _FUTEX_H */
diff --git a/tools/perf/builtin-bench.c b/tools/perf/builtin-bench.c
index 743a30a8baa0..f600b74216da 100644
--- a/tools/perf/builtin-bench.c
+++ b/tools/perf/builtin-bench.c
@@ -58,6 +58,7 @@ static struct bench mem_benchmarks[] = {
58static struct bench futex_benchmarks[] = { 58static struct bench futex_benchmarks[] = {
59 { "hash", "Benchmark for futex hash table", bench_futex_hash }, 59 { "hash", "Benchmark for futex hash table", bench_futex_hash },
60 { "wake", "Benchmark for futex wake calls", bench_futex_wake }, 60 { "wake", "Benchmark for futex wake calls", bench_futex_wake },
61 { "requeue", "Benchmark for futex requeue calls", bench_futex_requeue },
61 { "all", "Test all futex benchmarks", NULL }, 62 { "all", "Test all futex benchmarks", NULL },
62 { NULL, NULL, NULL } 63 { NULL, NULL, NULL }
63}; 64};