diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2014-03-31 14:13:25 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2014-03-31 14:13:25 -0400 |
commit | 8c292f11744297dfb3a69f4a0bccbe4a6417b50d (patch) | |
tree | f1a89560de25a69b697d459a9b5cf2e738038d9f /tools/perf/bench | |
parent | d31605dc8a63f1df28443ddb3560b1079417af92 (diff) | |
parent | 538592ff0b008237ae88f5ce5fb1247127dc3ce5 (diff) |
Merge branch 'perf-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull perf changes from Ingo Molnar:
"Main changes:
Kernel side changes:
- Add SNB/IVB/HSW client uncore memory controller support (Stephane
Eranian)
- Fix various x86/P4 PMU driver bugs (Don Zickus)
Tooling, user visible changes:
- Add several futex 'perf bench' microbenchmarks (Davidlohr Bueso)
- Speed up thread map generation (Don Zickus)
- Introduce 'perf kvm --list-cmds' command line option for use by
scripts (Ramkumar Ramachandra)
- Print the evsel name in the annotate stdio output, prep to fix
support outputting annotation for multiple events, not just for the
first one (Arnaldo Carvalho de Melo)
- Allow setting preferred callchain method in .perfconfig (Jiri Olsa)
- Show in what binaries/modules 'perf probe's are set (Masami
Hiramatsu)
- Support distro-style debuginfo for uprobe in 'perf probe' (Masami
Hiramatsu)
Tooling, internal changes and fixes:
- Use tid in mmap/mmap2 events to find maps (Don Zickus)
- Record the reason for filtering an address_location (Namhyung Kim)
- Apply all filters to an addr_location (Namhyung Kim)
- Merge al->filtered with hist_entry->filtered in report/hists
(Namhyung Kim)
- Fix memory leak when synthesizing thread records (Namhyung Kim)
- Use ui__has_annotation() in 'report' (Namhyung Kim)
- hists browser refactorings to reuse code accross UIs (Namhyung Kim)
- Add support for the new DWARF unwinder library in elfutils (Jiri
Olsa)
- Fix build race in the generation of bison files (Jiri Olsa)
- Further streamline the feature detection display, trimming it a bit
to show just the libraries detected, using VF=1 gets a more verbose
output, showing the less interesting feature checks as well (Jiri
Olsa).
- Check compatible symtab type before loading dso (Namhyung Kim)
- Check return value of filename__read_debuglink() (Stephane Eranian)
- Move some hashing and fs related code from tools/perf/util/ to
tools/lib/ so that it can be used by more tools/ living utilities
(Borislav Petkov)
- Prepare DWARF unwinding code for using an elfutils alternative
unwinding library (Jiri Olsa)
- Fix DWARF unwind max_stack processing (Jiri Olsa)
- Add dwarf unwind 'perf test' entry (Jiri Olsa)
- 'perf probe' improvements including memory leak fixes, sharing the
intlist class with other tools, uprobes/kprobes code sharing and
use of ref_reloc_sym (Masami Hiramatsu)
- Shorten sample symbol resolving by adding cpumode to struct
addr_location (Arnaldo Carvalho de Melo)
- Fix synthesizing mmaps for threads (Don Zickus)
- Fix invalid output on event group stdio report (Namhyung Kim)
- Fixup header alignment in 'perf sched latency' output (Ramkumar
Ramachandra)
- Fix off-by-one error in 'perf timechart record' argv handling
(Ramkumar Ramachandra)
Tooling, cleanups:
- Remove unused thread__find_map function (Jiri Olsa)
- Remove unused simple_strtoul() function (Ramkumar Ramachandra)
Tooling, documentation updates:
- Update function names in debug messages (Ramkumar Ramachandra)
- Update some code references in design.txt (Ramkumar Ramachandra)
- Clarify load-latency information in the 'perf mem' docs (Andi
Kleen)
- Clarify x86 register naming in 'perf probe' docs (Andi Kleen)"
* 'perf-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (96 commits)
perf tools: Remove unused simple_strtoul() function
perf tools: Update some code references in design.txt
perf evsel: Update function names in debug messages
perf tools: Remove thread__find_map function
perf annotate: Print the evsel name in the stdio output
perf report: Use ui__has_annotation()
perf tools: Fix memory leak when synthesizing thread records
perf tools: Use tid in mmap/mmap2 events to find maps
perf report: Merge al->filtered with hist_entry->filtered
perf symbols: Apply all filters to an addr_location
perf symbols: Record the reason for filtering an address_location
perf sched: Fixup header alignment in 'latency' output
perf timechart: Fix off-by-one error in 'record' argv handling
perf machine: Factor machine__find_thread to take tid argument
perf tools: Speed up thread map generation
perf kvm: introduce --list-cmds for use by scripts
perf ui hists: Pass evsel to hpp->header/width functions explicitly
perf symbols: Introduce thread__find_cpumode_addr_location
perf session: Change header.misc dump from decimal to hex
perf ui/tui: Reuse generic __hpp__fmt() code
...
Diffstat (limited to 'tools/perf/bench')
-rw-r--r-- | tools/perf/bench/bench.h | 3 | ||||
-rw-r--r-- | tools/perf/bench/futex-hash.c | 212 | ||||
-rw-r--r-- | tools/perf/bench/futex-requeue.c | 211 | ||||
-rw-r--r-- | tools/perf/bench/futex-wake.c | 201 | ||||
-rw-r--r-- | tools/perf/bench/futex.h | 71 |
5 files changed, 698 insertions, 0 deletions
diff --git a/tools/perf/bench/bench.h b/tools/perf/bench/bench.h index 0fdc85269c4d..eba46709b279 100644 --- a/tools/perf/bench/bench.h +++ b/tools/perf/bench/bench.h | |||
@@ -31,6 +31,9 @@ extern int bench_sched_pipe(int argc, const char **argv, const char *prefix); | |||
31 | extern int bench_mem_memcpy(int argc, const char **argv, | 31 | extern int bench_mem_memcpy(int argc, const char **argv, |
32 | const char *prefix __maybe_unused); | 32 | const char *prefix __maybe_unused); |
33 | extern int bench_mem_memset(int argc, const char **argv, const char *prefix); | 33 | extern int bench_mem_memset(int argc, const char **argv, const char *prefix); |
34 | extern int bench_futex_hash(int argc, const char **argv, const char *prefix); | ||
35 | extern int bench_futex_wake(int argc, const char **argv, const char *prefix); | ||
36 | extern int bench_futex_requeue(int argc, const char **argv, const char *prefix); | ||
34 | 37 | ||
35 | #define BENCH_FORMAT_DEFAULT_STR "default" | 38 | #define BENCH_FORMAT_DEFAULT_STR "default" |
36 | #define BENCH_FORMAT_DEFAULT 0 | 39 | #define BENCH_FORMAT_DEFAULT 0 |
diff --git a/tools/perf/bench/futex-hash.c b/tools/perf/bench/futex-hash.c new file mode 100644 index 000000000000..a84206e9c4aa --- /dev/null +++ b/tools/perf/bench/futex-hash.c | |||
@@ -0,0 +1,212 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2013 Davidlohr Bueso <davidlohr@hp.com> | ||
3 | * | ||
4 | * futex-hash: Stress the hell out of the Linux kernel futex uaddr hashing. | ||
5 | * | ||
6 | * This program is particularly useful for measuring the kernel's futex hash | ||
7 | * table/function implementation. In order for it to make sense, use with as | ||
8 | * many threads and futexes as possible. | ||
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 | |||
24 | static unsigned int nthreads = 0; | ||
25 | static unsigned int nsecs = 10; | ||
26 | /* amount of futexes per thread */ | ||
27 | static unsigned int nfutexes = 1024; | ||
28 | static bool fshared = false, done = false, silent = false; | ||
29 | |||
30 | struct timeval start, end, runtime; | ||
31 | static pthread_mutex_t thread_lock; | ||
32 | static unsigned int threads_starting; | ||
33 | static struct stats throughput_stats; | ||
34 | static pthread_cond_t thread_parent, thread_worker; | ||
35 | |||
36 | struct worker { | ||
37 | int tid; | ||
38 | u_int32_t *futex; | ||
39 | pthread_t thread; | ||
40 | unsigned long ops; | ||
41 | }; | ||
42 | |||
43 | static const struct option options[] = { | ||
44 | OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"), | ||
45 | OPT_UINTEGER('r', "runtime", &nsecs, "Specify runtime (in seconds)"), | ||
46 | OPT_UINTEGER('f', "futexes", &nfutexes, "Specify amount of futexes per threads"), | ||
47 | OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"), | ||
48 | OPT_BOOLEAN( 'S', "shared", &fshared, "Use shared futexes instead of private ones"), | ||
49 | OPT_END() | ||
50 | }; | ||
51 | |||
52 | static const char * const bench_futex_hash_usage[] = { | ||
53 | "perf bench futex hash <options>", | ||
54 | NULL | ||
55 | }; | ||
56 | |||
57 | static void *workerfn(void *arg) | ||
58 | { | ||
59 | int ret; | ||
60 | unsigned int i; | ||
61 | struct worker *w = (struct worker *) arg; | ||
62 | |||
63 | pthread_mutex_lock(&thread_lock); | ||
64 | threads_starting--; | ||
65 | if (!threads_starting) | ||
66 | pthread_cond_signal(&thread_parent); | ||
67 | pthread_cond_wait(&thread_worker, &thread_lock); | ||
68 | pthread_mutex_unlock(&thread_lock); | ||
69 | |||
70 | do { | ||
71 | for (i = 0; i < nfutexes; i++, w->ops++) { | ||
72 | /* | ||
73 | * We want the futex calls to fail in order to stress | ||
74 | * the hashing of uaddr and not measure other steps, | ||
75 | * such as internal waitqueue handling, thus enlarging | ||
76 | * the critical region protected by hb->lock. | ||
77 | */ | ||
78 | ret = futex_wait(&w->futex[i], 1234, NULL, | ||
79 | fshared ? 0 : FUTEX_PRIVATE_FLAG); | ||
80 | if (!silent && | ||
81 | (!ret || errno != EAGAIN || errno != EWOULDBLOCK)) | ||
82 | warn("Non-expected futex return call"); | ||
83 | } | ||
84 | } while (!done); | ||
85 | |||
86 | return NULL; | ||
87 | } | ||
88 | |||
89 | static void toggle_done(int sig __maybe_unused, | ||
90 | siginfo_t *info __maybe_unused, | ||
91 | void *uc __maybe_unused) | ||
92 | { | ||
93 | /* inform all threads that we're done for the day */ | ||
94 | done = true; | ||
95 | gettimeofday(&end, NULL); | ||
96 | timersub(&end, &start, &runtime); | ||
97 | } | ||
98 | |||
99 | static void print_summary(void) | ||
100 | { | ||
101 | unsigned long avg = avg_stats(&throughput_stats); | ||
102 | double stddev = stddev_stats(&throughput_stats); | ||
103 | |||
104 | printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n", | ||
105 | !silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg), | ||
106 | (int) runtime.tv_sec); | ||
107 | } | ||
108 | |||
109 | int bench_futex_hash(int argc, const char **argv, | ||
110 | const char *prefix __maybe_unused) | ||
111 | { | ||
112 | int ret = 0; | ||
113 | cpu_set_t cpu; | ||
114 | struct sigaction act; | ||
115 | unsigned int i, ncpus; | ||
116 | pthread_attr_t thread_attr; | ||
117 | struct worker *worker = NULL; | ||
118 | |||
119 | argc = parse_options(argc, argv, options, bench_futex_hash_usage, 0); | ||
120 | if (argc) { | ||
121 | usage_with_options(bench_futex_hash_usage, options); | ||
122 | exit(EXIT_FAILURE); | ||
123 | } | ||
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) /* default to the number of CPUs */ | ||
132 | nthreads = ncpus; | ||
133 | |||
134 | worker = calloc(nthreads, sizeof(*worker)); | ||
135 | if (!worker) | ||
136 | goto errmem; | ||
137 | |||
138 | printf("Run summary [PID %d]: %d threads, each operating on %d [%s] futexes for %d secs.\n\n", | ||
139 | getpid(), nthreads, nfutexes, fshared ? "shared":"private", nsecs); | ||
140 | |||
141 | init_stats(&throughput_stats); | ||
142 | pthread_mutex_init(&thread_lock, NULL); | ||
143 | pthread_cond_init(&thread_parent, NULL); | ||
144 | pthread_cond_init(&thread_worker, NULL); | ||
145 | |||
146 | threads_starting = nthreads; | ||
147 | pthread_attr_init(&thread_attr); | ||
148 | gettimeofday(&start, NULL); | ||
149 | for (i = 0; i < nthreads; i++) { | ||
150 | worker[i].tid = i; | ||
151 | worker[i].futex = calloc(nfutexes, sizeof(*worker[i].futex)); | ||
152 | if (!worker[i].futex) | ||
153 | goto errmem; | ||
154 | |||
155 | CPU_ZERO(&cpu); | ||
156 | CPU_SET(i % ncpus, &cpu); | ||
157 | |||
158 | ret = pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu); | ||
159 | if (ret) | ||
160 | err(EXIT_FAILURE, "pthread_attr_setaffinity_np"); | ||
161 | |||
162 | ret = pthread_create(&worker[i].thread, &thread_attr, workerfn, | ||
163 | (void *)(struct worker *) &worker[i]); | ||
164 | if (ret) | ||
165 | err(EXIT_FAILURE, "pthread_create"); | ||
166 | |||
167 | } | ||
168 | pthread_attr_destroy(&thread_attr); | ||
169 | |||
170 | pthread_mutex_lock(&thread_lock); | ||
171 | while (threads_starting) | ||
172 | pthread_cond_wait(&thread_parent, &thread_lock); | ||
173 | pthread_cond_broadcast(&thread_worker); | ||
174 | pthread_mutex_unlock(&thread_lock); | ||
175 | |||
176 | sleep(nsecs); | ||
177 | toggle_done(0, NULL, NULL); | ||
178 | |||
179 | for (i = 0; i < nthreads; i++) { | ||
180 | ret = pthread_join(worker[i].thread, NULL); | ||
181 | if (ret) | ||
182 | err(EXIT_FAILURE, "pthread_join"); | ||
183 | } | ||
184 | |||
185 | /* cleanup & report results */ | ||
186 | pthread_cond_destroy(&thread_parent); | ||
187 | pthread_cond_destroy(&thread_worker); | ||
188 | pthread_mutex_destroy(&thread_lock); | ||
189 | |||
190 | for (i = 0; i < nthreads; i++) { | ||
191 | unsigned long t = worker[i].ops/runtime.tv_sec; | ||
192 | update_stats(&throughput_stats, t); | ||
193 | if (!silent) { | ||
194 | if (nfutexes == 1) | ||
195 | printf("[thread %2d] futex: %p [ %ld ops/sec ]\n", | ||
196 | worker[i].tid, &worker[i].futex[0], t); | ||
197 | else | ||
198 | printf("[thread %2d] futexes: %p ... %p [ %ld ops/sec ]\n", | ||
199 | worker[i].tid, &worker[i].futex[0], | ||
200 | &worker[i].futex[nfutexes-1], t); | ||
201 | } | ||
202 | |||
203 | free(worker[i].futex); | ||
204 | } | ||
205 | |||
206 | print_summary(); | ||
207 | |||
208 | free(worker); | ||
209 | return ret; | ||
210 | errmem: | ||
211 | err(EXIT_FAILURE, "calloc"); | ||
212 | } | ||
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 | |||
24 | static 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 | */ | ||
30 | static 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 | */ | ||
37 | static unsigned int repeat = 10; | ||
38 | |||
39 | static pthread_t *worker; | ||
40 | static bool done = 0, silent = 0; | ||
41 | static pthread_mutex_t thread_lock; | ||
42 | static pthread_cond_t thread_parent, thread_worker; | ||
43 | static struct stats requeuetime_stats, requeued_stats; | ||
44 | static unsigned int ncpus, threads_starting, nthreads = 0; | ||
45 | |||
46 | static 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 | |||
54 | static const char * const bench_futex_requeue_usage[] = { | ||
55 | "perf bench futex requeue <options>", | ||
56 | NULL | ||
57 | }; | ||
58 | |||
59 | static 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 | |||
72 | static 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 | |||
85 | static 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 | |||
106 | static void toggle_done(int sig __maybe_unused, | ||
107 | siginfo_t *info __maybe_unused, | ||
108 | void *uc __maybe_unused) | ||
109 | { | ||
110 | done = true; | ||
111 | } | ||
112 | |||
113 | int 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; | ||
208 | err: | ||
209 | usage_with_options(bench_futex_requeue_usage, options); | ||
210 | exit(EXIT_FAILURE); | ||
211 | } | ||
diff --git a/tools/perf/bench/futex-wake.c b/tools/perf/bench/futex-wake.c new file mode 100644 index 000000000000..d096169b161e --- /dev/null +++ b/tools/perf/bench/futex-wake.c | |||
@@ -0,0 +1,201 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2013 Davidlohr Bueso <davidlohr@hp.com> | ||
3 | * | ||
4 | * futex-wake: Block a bunch of threads on a futex and wake'em up, N at a time. | ||
5 | * | ||
6 | * This program is particularly useful to measure the latency of nthread wakeups | ||
7 | * in non-error situations: all waiters are queued and all wake calls wakeup | ||
8 | * one or more tasks, and thus the waitqueue is never empty. | ||
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 | |||
24 | /* all threads will block on the same futex */ | ||
25 | static u_int32_t futex1 = 0; | ||
26 | |||
27 | /* | ||
28 | * How many wakeups to do at a time. | ||
29 | * Default to 1 in order to make the kernel work more. | ||
30 | */ | ||
31 | static unsigned int nwakes = 1; | ||
32 | |||
33 | /* | ||
34 | * There can be significant variance from run to run, | ||
35 | * the more repeats, the more exact the overall avg and | ||
36 | * the better idea of the futex latency. | ||
37 | */ | ||
38 | static unsigned int repeat = 10; | ||
39 | |||
40 | pthread_t *worker; | ||
41 | static bool done = 0, silent = 0; | ||
42 | static pthread_mutex_t thread_lock; | ||
43 | static pthread_cond_t thread_parent, thread_worker; | ||
44 | static struct stats waketime_stats, wakeup_stats; | ||
45 | static unsigned int ncpus, threads_starting, nthreads = 0; | ||
46 | |||
47 | static const struct option options[] = { | ||
48 | OPT_UINTEGER('t', "threads", &nthreads, "Specify amount of threads"), | ||
49 | OPT_UINTEGER('w', "nwakes", &nwakes, "Specify amount of threads to wake at once"), | ||
50 | OPT_UINTEGER('r', "repeat", &repeat, "Specify amount of times to repeat the run"), | ||
51 | OPT_BOOLEAN( 's', "silent", &silent, "Silent mode: do not display data/details"), | ||
52 | OPT_END() | ||
53 | }; | ||
54 | |||
55 | static const char * const bench_futex_wake_usage[] = { | ||
56 | "perf bench futex wake <options>", | ||
57 | NULL | ||
58 | }; | ||
59 | |||
60 | static void *workerfn(void *arg __maybe_unused) | ||
61 | { | ||
62 | pthread_mutex_lock(&thread_lock); | ||
63 | threads_starting--; | ||
64 | if (!threads_starting) | ||
65 | pthread_cond_signal(&thread_parent); | ||
66 | pthread_cond_wait(&thread_worker, &thread_lock); | ||
67 | pthread_mutex_unlock(&thread_lock); | ||
68 | |||
69 | futex_wait(&futex1, 0, NULL, FUTEX_PRIVATE_FLAG); | ||
70 | return NULL; | ||
71 | } | ||
72 | |||
73 | static void print_summary(void) | ||
74 | { | ||
75 | double waketime_avg = avg_stats(&waketime_stats); | ||
76 | double waketime_stddev = stddev_stats(&waketime_stats); | ||
77 | unsigned int wakeup_avg = avg_stats(&wakeup_stats); | ||
78 | |||
79 | printf("Wokeup %d of %d threads in %.4f ms (+-%.2f%%)\n", | ||
80 | wakeup_avg, | ||
81 | nthreads, | ||
82 | waketime_avg/1e3, | ||
83 | rel_stddev_stats(waketime_stddev, waketime_avg)); | ||
84 | } | ||
85 | |||
86 | static void block_threads(pthread_t *w, | ||
87 | pthread_attr_t thread_attr) | ||
88 | { | ||
89 | cpu_set_t cpu; | ||
90 | unsigned int i; | ||
91 | |||
92 | threads_starting = nthreads; | ||
93 | |||
94 | /* create and block all threads */ | ||
95 | for (i = 0; i < nthreads; i++) { | ||
96 | CPU_ZERO(&cpu); | ||
97 | CPU_SET(i % ncpus, &cpu); | ||
98 | |||
99 | if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpu)) | ||
100 | err(EXIT_FAILURE, "pthread_attr_setaffinity_np"); | ||
101 | |||
102 | if (pthread_create(&w[i], &thread_attr, workerfn, NULL)) | ||
103 | err(EXIT_FAILURE, "pthread_create"); | ||
104 | } | ||
105 | } | ||
106 | |||
107 | static void toggle_done(int sig __maybe_unused, | ||
108 | siginfo_t *info __maybe_unused, | ||
109 | void *uc __maybe_unused) | ||
110 | { | ||
111 | done = true; | ||
112 | } | ||
113 | |||
114 | int bench_futex_wake(int argc, const char **argv, | ||
115 | const char *prefix __maybe_unused) | ||
116 | { | ||
117 | int ret = 0; | ||
118 | unsigned int i, j; | ||
119 | struct sigaction act; | ||
120 | pthread_attr_t thread_attr; | ||
121 | |||
122 | argc = parse_options(argc, argv, options, bench_futex_wake_usage, 0); | ||
123 | if (argc) { | ||
124 | usage_with_options(bench_futex_wake_usage, options); | ||
125 | exit(EXIT_FAILURE); | ||
126 | } | ||
127 | |||
128 | ncpus = sysconf(_SC_NPROCESSORS_ONLN); | ||
129 | |||
130 | sigfillset(&act.sa_mask); | ||
131 | act.sa_sigaction = toggle_done; | ||
132 | sigaction(SIGINT, &act, NULL); | ||
133 | |||
134 | if (!nthreads) | ||
135 | nthreads = ncpus; | ||
136 | |||
137 | worker = calloc(nthreads, sizeof(*worker)); | ||
138 | if (!worker) | ||
139 | err(EXIT_FAILURE, "calloc"); | ||
140 | |||
141 | printf("Run summary [PID %d]: blocking on %d threads (at futex %p), " | ||
142 | "waking up %d at a time.\n\n", | ||
143 | getpid(), nthreads, &futex1, nwakes); | ||
144 | |||
145 | init_stats(&wakeup_stats); | ||
146 | init_stats(&waketime_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 | |||
152 | for (j = 0; j < repeat && !done; j++) { | ||
153 | unsigned int nwoken = 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 waking folks up */ | ||
169 | gettimeofday(&start, NULL); | ||
170 | while (nwoken != nthreads) | ||
171 | nwoken += futex_wake(&futex1, nwakes, FUTEX_PRIVATE_FLAG); | ||
172 | gettimeofday(&end, NULL); | ||
173 | timersub(&end, &start, &runtime); | ||
174 | |||
175 | update_stats(&wakeup_stats, nwoken); | ||
176 | update_stats(&waketime_stats, runtime.tv_usec); | ||
177 | |||
178 | if (!silent) { | ||
179 | printf("[Run %d]: Wokeup %d of %d threads in %.4f ms\n", | ||
180 | j + 1, nwoken, nthreads, runtime.tv_usec/1e3); | ||
181 | } | ||
182 | |||
183 | for (i = 0; i < nthreads; i++) { | ||
184 | ret = pthread_join(worker[i], NULL); | ||
185 | if (ret) | ||
186 | err(EXIT_FAILURE, "pthread_join"); | ||
187 | } | ||
188 | |||
189 | } | ||
190 | |||
191 | /* cleanup & report results */ | ||
192 | pthread_cond_destroy(&thread_parent); | ||
193 | pthread_cond_destroy(&thread_worker); | ||
194 | pthread_mutex_destroy(&thread_lock); | ||
195 | pthread_attr_destroy(&thread_attr); | ||
196 | |||
197 | print_summary(); | ||
198 | |||
199 | free(worker); | ||
200 | return ret; | ||
201 | } | ||
diff --git a/tools/perf/bench/futex.h b/tools/perf/bench/futex.h new file mode 100644 index 000000000000..71f2844cf97f --- /dev/null +++ b/tools/perf/bench/futex.h | |||
@@ -0,0 +1,71 @@ | |||
1 | /* | ||
2 | * Glibc independent futex library for testing kernel functionality. | ||
3 | * Shamelessly stolen from Darren Hart <dvhltc@us.ibm.com> | ||
4 | * http://git.kernel.org/cgit/linux/kernel/git/dvhart/futextest.git/ | ||
5 | */ | ||
6 | |||
7 | #ifndef _FUTEX_H | ||
8 | #define _FUTEX_H | ||
9 | |||
10 | #include <unistd.h> | ||
11 | #include <sys/syscall.h> | ||
12 | #include <sys/types.h> | ||
13 | #include <linux/futex.h> | ||
14 | |||
15 | /** | ||
16 | * futex() - SYS_futex syscall wrapper | ||
17 | * @uaddr: address of first futex | ||
18 | * @op: futex op code | ||
19 | * @val: typically expected value of uaddr, but varies by op | ||
20 | * @timeout: typically an absolute struct timespec (except where noted | ||
21 | * otherwise). Overloaded by some ops | ||
22 | * @uaddr2: address of second futex for some ops\ | ||
23 | * @val3: varies by op | ||
24 | * @opflags: flags to be bitwise OR'd with op, such as FUTEX_PRIVATE_FLAG | ||
25 | * | ||
26 | * futex() is used by all the following futex op wrappers. It can also be | ||
27 | * used for misuse and abuse testing. Generally, the specific op wrappers | ||
28 | * should be used instead. It is a macro instead of an static inline function as | ||
29 | * some of the types over overloaded (timeout is used for nr_requeue for | ||
30 | * example). | ||
31 | * | ||
32 | * These argument descriptions are the defaults for all | ||
33 | * like-named arguments in the following wrappers except where noted below. | ||
34 | */ | ||
35 | #define futex(uaddr, op, val, timeout, uaddr2, val3, opflags) \ | ||
36 | syscall(SYS_futex, uaddr, op | opflags, val, timeout, uaddr2, val3) | ||
37 | |||
38 | /** | ||
39 | * futex_wait() - block on uaddr with optional timeout | ||
40 | * @timeout: relative timeout | ||
41 | */ | ||
42 | static inline int | ||
43 | futex_wait(u_int32_t *uaddr, u_int32_t val, struct timespec *timeout, int opflags) | ||
44 | { | ||
45 | return futex(uaddr, FUTEX_WAIT, val, timeout, NULL, 0, opflags); | ||
46 | } | ||
47 | |||
48 | /** | ||
49 | * futex_wake() - wake one or more tasks blocked on uaddr | ||
50 | * @nr_wake: wake up to this many tasks | ||
51 | */ | ||
52 | static inline int | ||
53 | futex_wake(u_int32_t *uaddr, int nr_wake, int opflags) | ||
54 | { | ||
55 | return futex(uaddr, FUTEX_WAKE, nr_wake, NULL, NULL, 0, opflags); | ||
56 | } | ||
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 | */ | ||
63 | static inline int | ||
64 | futex_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 | |||
71 | #endif /* _FUTEX_H */ | ||