aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tools/perf/Documentation/perf-mem.txt4
-rw-r--r--tools/perf/Documentation/perf-probe.txt2
-rw-r--r--tools/perf/Makefile.perf3
-rw-r--r--tools/perf/bench/bench.h3
-rw-r--r--tools/perf/bench/futex-hash.c212
-rw-r--r--tools/perf/bench/futex-requeue.c211
-rw-r--r--tools/perf/bench/futex-wake.c201
-rw-r--r--tools/perf/bench/futex.h71
-rw-r--r--tools/perf/builtin-bench.c12
-rw-r--r--tools/perf/builtin-diff.c7
-rw-r--r--tools/perf/builtin-kvm.c12
-rw-r--r--tools/perf/perf-completion.sh2
-rw-r--r--tools/perf/perf.h6
-rw-r--r--tools/perf/tests/dwarf-unwind.c2
-rw-r--r--tools/perf/ui/browsers/hists.c122
-rw-r--r--tools/perf/ui/gtk/hists.c78
-rw-r--r--tools/perf/ui/hist.c138
-rw-r--r--tools/perf/ui/stdio/hist.c11
-rw-r--r--tools/perf/util/event.c156
-rw-r--r--tools/perf/util/hist.h20
-rw-r--r--tools/perf/util/machine.c46
-rw-r--r--tools/perf/util/machine.h3
-rw-r--r--tools/perf/util/parse-options.c37
-rw-r--r--tools/perf/util/parse-options.h8
-rw-r--r--tools/perf/util/session.c2
-rw-r--r--tools/perf/util/thread.c21
-rw-r--r--tools/perf/util/thread.h5
27 files changed, 1094 insertions, 301 deletions
diff --git a/tools/perf/Documentation/perf-mem.txt b/tools/perf/Documentation/perf-mem.txt
index 888d51137fbe..1d78a4064da4 100644
--- a/tools/perf/Documentation/perf-mem.txt
+++ b/tools/perf/Documentation/perf-mem.txt
@@ -18,6 +18,10 @@ from it, into perf.data. Perf record options are accepted and are passed through
18"perf mem -t <TYPE> report" displays the result. It invokes perf report with the 18"perf mem -t <TYPE> report" displays the result. It invokes perf report with the
19right set of options to display a memory access profile. 19right set of options to display a memory access profile.
20 20
21Note that on Intel systems the memory latency reported is the use-latency,
22not the pure load (or store latency). Use latency includes any pipeline
23queueing delays in addition to the memory subsystem latency.
24
21OPTIONS 25OPTIONS
22------- 26-------
23<command>...:: 27<command>...::
diff --git a/tools/perf/Documentation/perf-probe.txt b/tools/perf/Documentation/perf-probe.txt
index b715cb71592b..1513935c399b 100644
--- a/tools/perf/Documentation/perf-probe.txt
+++ b/tools/perf/Documentation/perf-probe.txt
@@ -136,6 +136,8 @@ Each probe argument follows below syntax.
136'NAME' specifies the name of this argument (optional). You can use the name of local variable, local data structure member (e.g. var->field, var.field2), local array with fixed index (e.g. array[1], var->array[0], var->pointer[2]), or kprobe-tracer argument format (e.g. $retval, %ax, etc). Note that the name of this argument will be set as the last member name if you specify a local data structure member (e.g. field2 for 'var->field1.field2'.) 136'NAME' specifies the name of this argument (optional). You can use the name of local variable, local data structure member (e.g. var->field, var.field2), local array with fixed index (e.g. array[1], var->array[0], var->pointer[2]), or kprobe-tracer argument format (e.g. $retval, %ax, etc). Note that the name of this argument will be set as the last member name if you specify a local data structure member (e.g. field2 for 'var->field1.field2'.)
137'TYPE' casts the type of this argument (optional). If omitted, perf probe automatically set the type based on debuginfo. You can specify 'string' type only for the local variable or structure member which is an array of or a pointer to 'char' or 'unsigned char' type. 137'TYPE' casts the type of this argument (optional). If omitted, perf probe automatically set the type based on debuginfo. You can specify 'string' type only for the local variable or structure member which is an array of or a pointer to 'char' or 'unsigned char' type.
138 138
139On x86 systems %REG is always the short form of the register: for example %AX. %RAX or %EAX is not valid.
140
139LINE SYNTAX 141LINE SYNTAX
140----------- 142-----------
141Line range is described by following syntax. 143Line range is described by following syntax.
diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
index 1f7ec48ac959..50d875d970c4 100644
--- a/tools/perf/Makefile.perf
+++ b/tools/perf/Makefile.perf
@@ -426,6 +426,9 @@ BUILTIN_OBJS += $(OUTPUT)bench/mem-memset-x86-64-asm.o
426endif 426endif
427BUILTIN_OBJS += $(OUTPUT)bench/mem-memcpy.o 427BUILTIN_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
430BUILTIN_OBJS += $(OUTPUT)bench/futex-wake.o
431BUILTIN_OBJS += $(OUTPUT)bench/futex-requeue.o
429 432
430BUILTIN_OBJS += $(OUTPUT)builtin-diff.o 433BUILTIN_OBJS += $(OUTPUT)builtin-diff.o
431BUILTIN_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 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);
31extern int bench_mem_memcpy(int argc, const char **argv, 31extern int bench_mem_memcpy(int argc, const char **argv,
32 const char *prefix __maybe_unused); 32 const char *prefix __maybe_unused);
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);
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);
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
24static unsigned int nthreads = 0;
25static unsigned int nsecs = 10;
26/* amount of futexes per thread */
27static unsigned int nfutexes = 1024;
28static bool fshared = false, done = false, silent = false;
29
30struct timeval start, end, runtime;
31static pthread_mutex_t thread_lock;
32static unsigned int threads_starting;
33static struct stats throughput_stats;
34static pthread_cond_t thread_parent, thread_worker;
35
36struct worker {
37 int tid;
38 u_int32_t *futex;
39 pthread_t thread;
40 unsigned long ops;
41};
42
43static 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
52static const char * const bench_futex_hash_usage[] = {
53 "perf bench futex hash <options>",
54 NULL
55};
56
57static 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
89static 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
99static 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
109int 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;
210errmem:
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
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-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 */
25static 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 */
31static 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 */
38static unsigned int repeat = 10;
39
40pthread_t *worker;
41static bool done = 0, silent = 0;
42static pthread_mutex_t thread_lock;
43static pthread_cond_t thread_parent, thread_worker;
44static struct stats waketime_stats, wakeup_stats;
45static unsigned int ncpus, threads_starting, nthreads = 0;
46
47static 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
55static const char * const bench_futex_wake_usage[] = {
56 "perf bench futex wake <options>",
57 NULL
58};
59
60static 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
73static 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
86static 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
107static void toggle_done(int sig __maybe_unused,
108 siginfo_t *info __maybe_unused,
109 void *uc __maybe_unused)
110{
111 done = true;
112}
113
114int 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 */
42static inline int
43futex_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 */
52static inline int
53futex_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*/
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
71#endif /* _FUTEX_H */
diff --git a/tools/perf/builtin-bench.c b/tools/perf/builtin-bench.c
index e47f90cc7b98..f600b74216da 100644
--- a/tools/perf/builtin-bench.c
+++ b/tools/perf/builtin-bench.c
@@ -12,6 +12,7 @@
12 * sched ... scheduler and IPC performance 12 * sched ... scheduler and IPC performance
13 * mem ... memory access performance 13 * mem ... memory access performance
14 * numa ... NUMA scheduling and MM performance 14 * numa ... NUMA scheduling and MM performance
15 * futex ... Futex performance
15 */ 16 */
16#include "perf.h" 17#include "perf.h"
17#include "util/util.h" 18#include "util/util.h"
@@ -54,6 +55,14 @@ static struct bench mem_benchmarks[] = {
54 { NULL, NULL, NULL } 55 { NULL, NULL, NULL }
55}; 56};
56 57
58static struct bench futex_benchmarks[] = {
59 { "hash", "Benchmark for futex hash table", bench_futex_hash },
60 { "wake", "Benchmark for futex wake calls", bench_futex_wake },
61 { "requeue", "Benchmark for futex requeue calls", bench_futex_requeue },
62 { "all", "Test all futex benchmarks", NULL },
63 { NULL, NULL, NULL }
64};
65
57struct collection { 66struct collection {
58 const char *name; 67 const char *name;
59 const char *summary; 68 const char *summary;
@@ -61,11 +70,12 @@ struct collection {
61}; 70};
62 71
63static struct collection collections[] = { 72static struct collection collections[] = {
64 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks }, 73 { "sched", "Scheduler and IPC benchmarks", sched_benchmarks },
65 { "mem", "Memory access benchmarks", mem_benchmarks }, 74 { "mem", "Memory access benchmarks", mem_benchmarks },
66#ifdef HAVE_LIBNUMA_SUPPORT 75#ifdef HAVE_LIBNUMA_SUPPORT
67 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks }, 76 { "numa", "NUMA scheduling and MM benchmarks", numa_benchmarks },
68#endif 77#endif
78 {"futex", "Futex stressing benchmarks", futex_benchmarks },
69 { "all", "All benchmarks", NULL }, 79 { "all", "All benchmarks", NULL },
70 { NULL, NULL, NULL } 80 { NULL, NULL, NULL }
71}; 81};
diff --git a/tools/perf/builtin-diff.c b/tools/perf/builtin-diff.c
index a77e31246c00..204fffe22532 100644
--- a/tools/perf/builtin-diff.c
+++ b/tools/perf/builtin-diff.c
@@ -952,8 +952,8 @@ static int hpp__entry_global(struct perf_hpp_fmt *_fmt, struct perf_hpp *hpp,
952 dfmt->header_width, buf); 952 dfmt->header_width, buf);
953} 953}
954 954
955static int hpp__header(struct perf_hpp_fmt *fmt, 955static int hpp__header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
956 struct perf_hpp *hpp) 956 struct perf_evsel *evsel __maybe_unused)
957{ 957{
958 struct diff_hpp_fmt *dfmt = 958 struct diff_hpp_fmt *dfmt =
959 container_of(fmt, struct diff_hpp_fmt, fmt); 959 container_of(fmt, struct diff_hpp_fmt, fmt);
@@ -963,7 +963,8 @@ static int hpp__header(struct perf_hpp_fmt *fmt,
963} 963}
964 964
965static int hpp__width(struct perf_hpp_fmt *fmt, 965static int hpp__width(struct perf_hpp_fmt *fmt,
966 struct perf_hpp *hpp __maybe_unused) 966 struct perf_hpp *hpp __maybe_unused,
967 struct perf_evsel *evsel __maybe_unused)
967{ 968{
968 struct diff_hpp_fmt *dfmt = 969 struct diff_hpp_fmt *dfmt =
969 container_of(fmt, struct diff_hpp_fmt, fmt); 970 container_of(fmt, struct diff_hpp_fmt, fmt);
diff --git a/tools/perf/builtin-kvm.c b/tools/perf/builtin-kvm.c
index a7350519c63f..21c164b8f9db 100644
--- a/tools/perf/builtin-kvm.c
+++ b/tools/perf/builtin-kvm.c
@@ -1691,17 +1691,15 @@ int cmd_kvm(int argc, const char **argv, const char *prefix __maybe_unused)
1691 OPT_END() 1691 OPT_END()
1692 }; 1692 };
1693 1693
1694 1694 const char *const kvm_subcommands[] = { "top", "record", "report", "diff",
1695 const char * const kvm_usage[] = { 1695 "buildid-list", "stat", NULL };
1696 "perf kvm [<options>] {top|record|report|diff|buildid-list|stat}", 1696 const char *kvm_usage[] = { NULL, NULL };
1697 NULL
1698 };
1699 1697
1700 perf_host = 0; 1698 perf_host = 0;
1701 perf_guest = 1; 1699 perf_guest = 1;
1702 1700
1703 argc = parse_options(argc, argv, kvm_options, kvm_usage, 1701 argc = parse_options_subcommand(argc, argv, kvm_options, kvm_subcommands, kvm_usage,
1704 PARSE_OPT_STOP_AT_NON_OPTION); 1702 PARSE_OPT_STOP_AT_NON_OPTION);
1705 if (!argc) 1703 if (!argc)
1706 usage_with_options(kvm_usage, kvm_options); 1704 usage_with_options(kvm_usage, kvm_options);
1707 1705
diff --git a/tools/perf/perf-completion.sh b/tools/perf/perf-completion.sh
index 496e2abb5482..ae3a57694b6b 100644
--- a/tools/perf/perf-completion.sh
+++ b/tools/perf/perf-completion.sh
@@ -123,7 +123,7 @@ __perf_main ()
123 __perfcomp_colon "$evts" "$cur" 123 __perfcomp_colon "$evts" "$cur"
124 # List subcommands for 'perf kvm' 124 # List subcommands for 'perf kvm'
125 elif [[ $prev == "kvm" ]]; then 125 elif [[ $prev == "kvm" ]]; then
126 subcmds="top record report diff buildid-list stat" 126 subcmds=$($cmd $prev --list-cmds)
127 __perfcomp_colon "$subcmds" "$cur" 127 __perfcomp_colon "$subcmds" "$cur"
128 # List long option names 128 # List long option names
129 elif [[ $cur == --* ]]; then 129 elif [[ $cur == --* ]]; then
diff --git a/tools/perf/perf.h b/tools/perf/perf.h
index 6898ad0e199f..e18a8b5e6953 100644
--- a/tools/perf/perf.h
+++ b/tools/perf/perf.h
@@ -12,6 +12,9 @@
12#ifndef __NR_perf_event_open 12#ifndef __NR_perf_event_open
13# define __NR_perf_event_open 336 13# define __NR_perf_event_open 336
14#endif 14#endif
15#ifndef __NR_futex
16# define __NR_futex 240
17#endif
15#endif 18#endif
16 19
17#if defined(__x86_64__) 20#if defined(__x86_64__)
@@ -23,6 +26,9 @@
23#ifndef __NR_perf_event_open 26#ifndef __NR_perf_event_open
24# define __NR_perf_event_open 298 27# define __NR_perf_event_open 298
25#endif 28#endif
29#ifndef __NR_futex
30# define __NR_futex 202
31#endif
26#endif 32#endif
27 33
28#ifdef __powerpc__ 34#ifdef __powerpc__
diff --git a/tools/perf/tests/dwarf-unwind.c b/tools/perf/tests/dwarf-unwind.c
index f16ea2808a75..c059ee81c038 100644
--- a/tools/perf/tests/dwarf-unwind.c
+++ b/tools/perf/tests/dwarf-unwind.c
@@ -128,7 +128,7 @@ int test__dwarf_unwind(void)
128 if (verbose > 1) 128 if (verbose > 1)
129 machine__fprintf(machine, stderr); 129 machine__fprintf(machine, stderr);
130 130
131 thread = machine__find_thread(machine, getpid()); 131 thread = machine__find_thread(machine, getpid(), getpid());
132 if (!thread) { 132 if (!thread) {
133 pr_err("Could not get thread\n"); 133 pr_err("Could not get thread\n");
134 goto out; 134 goto out;
diff --git a/tools/perf/ui/browsers/hists.c b/tools/perf/ui/browsers/hists.c
index b720b92eba6e..7ec871af3f6f 100644
--- a/tools/perf/ui/browsers/hists.c
+++ b/tools/perf/ui/browsers/hists.c
@@ -587,95 +587,52 @@ struct hpp_arg {
587 bool current_entry; 587 bool current_entry;
588}; 588};
589 589
590static int __hpp__color_callchain(struct hpp_arg *arg) 590static int __hpp__overhead_callback(struct perf_hpp *hpp, bool front)
591{ 591{
592 if (!symbol_conf.use_callchain)
593 return 0;
594
595 slsmg_printf("%c ", arg->folded_sign);
596 return 2;
597}
598
599static int __hpp__color_fmt(struct perf_hpp *hpp, struct hist_entry *he,
600 u64 (*get_field)(struct hist_entry *),
601 int (*callchain_cb)(struct hpp_arg *))
602{
603 int ret = 0;
604 double percent = 0.0;
605 struct hists *hists = he->hists;
606 struct hpp_arg *arg = hpp->ptr; 592 struct hpp_arg *arg = hpp->ptr;
607 593
608 if (hists->stats.total_period) 594 if (arg->current_entry && arg->b->navkeypressed)
609 percent = 100.0 * get_field(he) / hists->stats.total_period; 595 ui_browser__set_color(arg->b, HE_COLORSET_SELECTED);
610 596 else
611 ui_browser__set_percent_color(arg->b, percent, arg->current_entry); 597 ui_browser__set_color(arg->b, HE_COLORSET_NORMAL);
612
613 if (callchain_cb)
614 ret += callchain_cb(arg);
615
616 ret += scnprintf(hpp->buf, hpp->size, "%6.2f%%", percent);
617 slsmg_printf("%s", hpp->buf);
618
619 if (symbol_conf.event_group) {
620 int prev_idx, idx_delta;
621 struct perf_evsel *evsel = hists_to_evsel(hists);
622 struct hist_entry *pair;
623 int nr_members = evsel->nr_members;
624
625 if (nr_members <= 1)
626 goto out;
627 598
628 prev_idx = perf_evsel__group_idx(evsel); 599 if (front) {
600 if (!symbol_conf.use_callchain)
601 return 0;
629 602
630 list_for_each_entry(pair, &he->pairs.head, pairs.node) { 603 slsmg_printf("%c ", arg->folded_sign);
631 u64 period = get_field(pair); 604 return 2;
632 u64 total = pair->hists->stats.total_period; 605 }
633 606
634 if (!total) 607 return 0;
635 continue; 608}
636 609
637 evsel = hists_to_evsel(pair->hists); 610static int __hpp__color_callback(struct perf_hpp *hpp, bool front __maybe_unused)
638 idx_delta = perf_evsel__group_idx(evsel) - prev_idx - 1; 611{
612 struct hpp_arg *arg = hpp->ptr;
639 613
640 while (idx_delta--) { 614 if (!arg->current_entry || !arg->b->navkeypressed)
641 /* 615 ui_browser__set_color(arg->b, HE_COLORSET_NORMAL);
642 * zero-fill group members in the middle which 616 return 0;
643 * have no sample 617}
644 */
645 ui_browser__set_percent_color(arg->b, 0.0,
646 arg->current_entry);
647 ret += scnprintf(hpp->buf, hpp->size,
648 " %6.2f%%", 0.0);
649 slsmg_printf("%s", hpp->buf);
650 }
651 618
652 percent = 100.0 * period / total; 619static int __hpp__slsmg_color_printf(struct perf_hpp *hpp, const char *fmt, ...)
653 ui_browser__set_percent_color(arg->b, percent, 620{
654 arg->current_entry); 621 struct hpp_arg *arg = hpp->ptr;
655 ret += scnprintf(hpp->buf, hpp->size, 622 int ret;
656 " %6.2f%%", percent); 623 va_list args;
657 slsmg_printf("%s", hpp->buf); 624 double percent;
658 625
659 prev_idx = perf_evsel__group_idx(evsel); 626 va_start(args, fmt);
660 } 627 percent = va_arg(args, double);
628 va_end(args);
661 629
662 idx_delta = nr_members - prev_idx - 1; 630 ui_browser__set_percent_color(arg->b, percent, arg->current_entry);
663 631
664 while (idx_delta--) { 632 ret = scnprintf(hpp->buf, hpp->size, fmt, percent);
665 /* 633 slsmg_printf("%s", hpp->buf);
666 * zero-fill group members at last which have no sample
667 */
668 ui_browser__set_percent_color(arg->b, 0.0,
669 arg->current_entry);
670 ret += scnprintf(hpp->buf, hpp->size,
671 " %6.2f%%", 0.0);
672 slsmg_printf("%s", hpp->buf);
673 }
674 }
675out:
676 if (!arg->current_entry || !arg->b->navkeypressed)
677 ui_browser__set_color(arg->b, HE_COLORSET_NORMAL);
678 634
635 advance_hpp(hpp, ret);
679 return ret; 636 return ret;
680} 637}
681 638
@@ -690,14 +647,15 @@ hist_browser__hpp_color_##_type(struct perf_hpp_fmt *fmt __maybe_unused,\
690 struct perf_hpp *hpp, \ 647 struct perf_hpp *hpp, \
691 struct hist_entry *he) \ 648 struct hist_entry *he) \
692{ \ 649{ \
693 return __hpp__color_fmt(hpp, he, __hpp_get_##_field, _cb); \ 650 return __hpp__fmt(hpp, he, __hpp_get_##_field, _cb, " %6.2f%%", \
651 __hpp__slsmg_color_printf, true); \
694} 652}
695 653
696__HPP_COLOR_PERCENT_FN(overhead, period, __hpp__color_callchain) 654__HPP_COLOR_PERCENT_FN(overhead, period, __hpp__overhead_callback)
697__HPP_COLOR_PERCENT_FN(overhead_sys, period_sys, NULL) 655__HPP_COLOR_PERCENT_FN(overhead_sys, period_sys, __hpp__color_callback)
698__HPP_COLOR_PERCENT_FN(overhead_us, period_us, NULL) 656__HPP_COLOR_PERCENT_FN(overhead_us, period_us, __hpp__color_callback)
699__HPP_COLOR_PERCENT_FN(overhead_guest_sys, period_guest_sys, NULL) 657__HPP_COLOR_PERCENT_FN(overhead_guest_sys, period_guest_sys, __hpp__color_callback)
700__HPP_COLOR_PERCENT_FN(overhead_guest_us, period_guest_us, NULL) 658__HPP_COLOR_PERCENT_FN(overhead_guest_us, period_guest_us, __hpp__color_callback)
701 659
702#undef __HPP_COLOR_PERCENT_FN 660#undef __HPP_COLOR_PERCENT_FN
703 661
diff --git a/tools/perf/ui/gtk/hists.c b/tools/perf/ui/gtk/hists.c
index 5b95c44f3435..e395ef9b0ae0 100644
--- a/tools/perf/ui/gtk/hists.c
+++ b/tools/perf/ui/gtk/hists.c
@@ -8,16 +8,24 @@
8 8
9#define MAX_COLUMNS 32 9#define MAX_COLUMNS 32
10 10
11static int __percent_color_snprintf(char *buf, size_t size, double percent) 11static int __percent_color_snprintf(struct perf_hpp *hpp, const char *fmt, ...)
12{ 12{
13 int ret = 0; 13 int ret = 0;
14 va_list args;
15 double percent;
14 const char *markup; 16 const char *markup;
17 char *buf = hpp->buf;
18 size_t size = hpp->size;
19
20 va_start(args, fmt);
21 percent = va_arg(args, double);
22 va_end(args);
15 23
16 markup = perf_gtk__get_percent_color(percent); 24 markup = perf_gtk__get_percent_color(percent);
17 if (markup) 25 if (markup)
18 ret += scnprintf(buf, size, markup); 26 ret += scnprintf(buf, size, markup);
19 27
20 ret += scnprintf(buf + ret, size - ret, " %6.2f%%", percent); 28 ret += scnprintf(buf + ret, size - ret, fmt, percent);
21 29
22 if (markup) 30 if (markup)
23 ret += scnprintf(buf + ret, size - ret, "</span>"); 31 ret += scnprintf(buf + ret, size - ret, "</span>");
@@ -25,66 +33,6 @@ static int __percent_color_snprintf(char *buf, size_t size, double percent)
25 return ret; 33 return ret;
26} 34}
27 35
28
29static int __hpp__color_fmt(struct perf_hpp *hpp, struct hist_entry *he,
30 u64 (*get_field)(struct hist_entry *))
31{
32 int ret;
33 double percent = 0.0;
34 struct hists *hists = he->hists;
35 struct perf_evsel *evsel = hists_to_evsel(hists);
36
37 if (hists->stats.total_period)
38 percent = 100.0 * get_field(he) / hists->stats.total_period;
39
40 ret = __percent_color_snprintf(hpp->buf, hpp->size, percent);
41
42 if (perf_evsel__is_group_event(evsel)) {
43 int prev_idx, idx_delta;
44 struct hist_entry *pair;
45 int nr_members = evsel->nr_members;
46
47 prev_idx = perf_evsel__group_idx(evsel);
48
49 list_for_each_entry(pair, &he->pairs.head, pairs.node) {
50 u64 period = get_field(pair);
51 u64 total = pair->hists->stats.total_period;
52
53 evsel = hists_to_evsel(pair->hists);
54 idx_delta = perf_evsel__group_idx(evsel) - prev_idx - 1;
55
56 while (idx_delta--) {
57 /*
58 * zero-fill group members in the middle which
59 * have no sample
60 */
61 ret += __percent_color_snprintf(hpp->buf + ret,
62 hpp->size - ret,
63 0.0);
64 }
65
66 percent = 100.0 * period / total;
67 ret += __percent_color_snprintf(hpp->buf + ret,
68 hpp->size - ret,
69 percent);
70
71 prev_idx = perf_evsel__group_idx(evsel);
72 }
73
74 idx_delta = nr_members - prev_idx - 1;
75
76 while (idx_delta--) {
77 /*
78 * zero-fill group members at last which have no sample
79 */
80 ret += __percent_color_snprintf(hpp->buf + ret,
81 hpp->size - ret,
82 0.0);
83 }
84 }
85 return ret;
86}
87
88#define __HPP_COLOR_PERCENT_FN(_type, _field) \ 36#define __HPP_COLOR_PERCENT_FN(_type, _field) \
89static u64 he_get_##_field(struct hist_entry *he) \ 37static u64 he_get_##_field(struct hist_entry *he) \
90{ \ 38{ \
@@ -95,7 +43,8 @@ static int perf_gtk__hpp_color_##_type(struct perf_hpp_fmt *fmt __maybe_unused,
95 struct perf_hpp *hpp, \ 43 struct perf_hpp *hpp, \
96 struct hist_entry *he) \ 44 struct hist_entry *he) \
97{ \ 45{ \
98 return __hpp__color_fmt(hpp, he, he_get_##_field); \ 46 return __hpp__fmt(hpp, he, he_get_##_field, NULL, " %6.2f%%", \
47 __percent_color_snprintf, true); \
99} 48}
100 49
101__HPP_COLOR_PERCENT_FN(overhead, period) 50__HPP_COLOR_PERCENT_FN(overhead, period)
@@ -216,7 +165,6 @@ static void perf_gtk__show_hists(GtkWidget *window, struct hists *hists,
216 struct perf_hpp hpp = { 165 struct perf_hpp hpp = {
217 .buf = s, 166 .buf = s,
218 .size = sizeof(s), 167 .size = sizeof(s),
219 .ptr = hists_to_evsel(hists),
220 }; 168 };
221 169
222 nr_cols = 0; 170 nr_cols = 0;
@@ -243,7 +191,7 @@ static void perf_gtk__show_hists(GtkWidget *window, struct hists *hists,
243 col_idx = 0; 191 col_idx = 0;
244 192
245 perf_hpp__for_each_format(fmt) { 193 perf_hpp__for_each_format(fmt) {
246 fmt->header(fmt, &hpp); 194 fmt->header(fmt, &hpp, hists_to_evsel(hists));
247 195
248 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), 196 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
249 -1, ltrim(s), 197 -1, ltrim(s),
diff --git a/tools/perf/ui/hist.c b/tools/perf/ui/hist.c
index 78f4c92e9b73..0f403b83e9d1 100644
--- a/tools/perf/ui/hist.c
+++ b/tools/perf/ui/hist.c
@@ -8,16 +8,27 @@
8 8
9/* hist period print (hpp) functions */ 9/* hist period print (hpp) functions */
10 10
11typedef int (*hpp_snprint_fn)(char *buf, size_t size, const char *fmt, ...); 11#define hpp__call_print_fn(hpp, fn, fmt, ...) \
12 12({ \
13static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he, 13 int __ret = fn(hpp, fmt, ##__VA_ARGS__); \
14 u64 (*get_field)(struct hist_entry *), 14 advance_hpp(hpp, __ret); \
15 const char *fmt, hpp_snprint_fn print_fn, 15 __ret; \
16 bool fmt_percent) 16})
17
18int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he,
19 hpp_field_fn get_field, hpp_callback_fn callback,
20 const char *fmt, hpp_snprint_fn print_fn, bool fmt_percent)
17{ 21{
18 int ret; 22 int ret = 0;
19 struct hists *hists = he->hists; 23 struct hists *hists = he->hists;
20 struct perf_evsel *evsel = hists_to_evsel(hists); 24 struct perf_evsel *evsel = hists_to_evsel(hists);
25 char *buf = hpp->buf;
26 size_t size = hpp->size;
27
28 if (callback) {
29 ret = callback(hpp, true);
30 advance_hpp(hpp, ret);
31 }
21 32
22 if (fmt_percent) { 33 if (fmt_percent) {
23 double percent = 0.0; 34 double percent = 0.0;
@@ -26,9 +37,9 @@ static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he,
26 percent = 100.0 * get_field(he) / 37 percent = 100.0 * get_field(he) /
27 hists->stats.total_period; 38 hists->stats.total_period;
28 39
29 ret = print_fn(hpp->buf, hpp->size, fmt, percent); 40 ret += hpp__call_print_fn(hpp, print_fn, fmt, percent);
30 } else 41 } else
31 ret = print_fn(hpp->buf, hpp->size, fmt, get_field(he)); 42 ret += hpp__call_print_fn(hpp, print_fn, fmt, get_field(he));
32 43
33 if (perf_evsel__is_group_event(evsel)) { 44 if (perf_evsel__is_group_event(evsel)) {
34 int prev_idx, idx_delta; 45 int prev_idx, idx_delta;
@@ -52,16 +63,22 @@ static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he,
52 * zero-fill group members in the middle which 63 * zero-fill group members in the middle which
53 * have no sample 64 * have no sample
54 */ 65 */
55 ret += print_fn(hpp->buf + ret, hpp->size - ret, 66 if (fmt_percent) {
56 fmt, 0); 67 ret += hpp__call_print_fn(hpp, print_fn,
68 fmt, 0.0);
69 } else {
70 ret += hpp__call_print_fn(hpp, print_fn,
71 fmt, 0ULL);
72 }
57 } 73 }
58 74
59 if (fmt_percent) 75 if (fmt_percent) {
60 ret += print_fn(hpp->buf + ret, hpp->size - ret, 76 ret += hpp__call_print_fn(hpp, print_fn, fmt,
61 fmt, 100.0 * period / total); 77 100.0 * period / total);
62 else 78 } else {
63 ret += print_fn(hpp->buf + ret, hpp->size - ret, 79 ret += hpp__call_print_fn(hpp, print_fn, fmt,
64 fmt, period); 80 period);
81 }
65 82
66 prev_idx = perf_evsel__group_idx(evsel); 83 prev_idx = perf_evsel__group_idx(evsel);
67 } 84 }
@@ -72,41 +89,87 @@ static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he,
72 /* 89 /*
73 * zero-fill group members at last which have no sample 90 * zero-fill group members at last which have no sample
74 */ 91 */
75 ret += print_fn(hpp->buf + ret, hpp->size - ret, 92 if (fmt_percent) {
76 fmt, 0); 93 ret += hpp__call_print_fn(hpp, print_fn,
94 fmt, 0.0);
95 } else {
96 ret += hpp__call_print_fn(hpp, print_fn,
97 fmt, 0ULL);
98 }
77 } 99 }
78 } 100 }
101
102 if (callback) {
103 int __ret = callback(hpp, false);
104
105 advance_hpp(hpp, __ret);
106 ret += __ret;
107 }
108
109 /*
110 * Restore original buf and size as it's where caller expects
111 * the result will be saved.
112 */
113 hpp->buf = buf;
114 hpp->size = size;
115
79 return ret; 116 return ret;
80} 117}
81 118
82#define __HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \ 119#define __HPP_HEADER_FN(_type, _str, _min_width, _unit_width) \
83static int hpp__header_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \ 120static int hpp__header_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
84 struct perf_hpp *hpp) \ 121 struct perf_hpp *hpp, \
122 struct perf_evsel *evsel) \
85{ \ 123{ \
86 int len = _min_width; \ 124 int len = _min_width; \
87 \ 125 \
88 if (symbol_conf.event_group) { \ 126 if (symbol_conf.event_group) \
89 struct perf_evsel *evsel = hpp->ptr; \
90 \
91 len = max(len, evsel->nr_members * _unit_width); \ 127 len = max(len, evsel->nr_members * _unit_width); \
92 } \ 128 \
93 return scnprintf(hpp->buf, hpp->size, "%*s", len, _str); \ 129 return scnprintf(hpp->buf, hpp->size, "%*s", len, _str); \
94} 130}
95 131
96#define __HPP_WIDTH_FN(_type, _min_width, _unit_width) \ 132#define __HPP_WIDTH_FN(_type, _min_width, _unit_width) \
97static int hpp__width_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \ 133static int hpp__width_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
98 struct perf_hpp *hpp __maybe_unused) \ 134 struct perf_hpp *hpp __maybe_unused, \
135 struct perf_evsel *evsel) \
99{ \ 136{ \
100 int len = _min_width; \ 137 int len = _min_width; \
101 \ 138 \
102 if (symbol_conf.event_group) { \ 139 if (symbol_conf.event_group) \
103 struct perf_evsel *evsel = hpp->ptr; \
104 \
105 len = max(len, evsel->nr_members * _unit_width); \ 140 len = max(len, evsel->nr_members * _unit_width); \
106 } \ 141 \
107 return len; \ 142 return len; \
108} 143}
109 144
145static int hpp_color_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
146{
147 va_list args;
148 ssize_t ssize = hpp->size;
149 double percent;
150 int ret;
151
152 va_start(args, fmt);
153 percent = va_arg(args, double);
154 ret = value_color_snprintf(hpp->buf, hpp->size, fmt, percent);
155 va_end(args);
156
157 return (ret >= ssize) ? (ssize - 1) : ret;
158}
159
160static int hpp_entry_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
161{
162 va_list args;
163 ssize_t ssize = hpp->size;
164 int ret;
165
166 va_start(args, fmt);
167 ret = vsnprintf(hpp->buf, hpp->size, fmt, args);
168 va_end(args);
169
170 return (ret >= ssize) ? (ssize - 1) : ret;
171}
172
110#define __HPP_COLOR_PERCENT_FN(_type, _field) \ 173#define __HPP_COLOR_PERCENT_FN(_type, _field) \
111static u64 he_get_##_field(struct hist_entry *he) \ 174static u64 he_get_##_field(struct hist_entry *he) \
112{ \ 175{ \
@@ -116,8 +179,8 @@ static u64 he_get_##_field(struct hist_entry *he) \
116static int hpp__color_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \ 179static int hpp__color_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
117 struct perf_hpp *hpp, struct hist_entry *he) \ 180 struct perf_hpp *hpp, struct hist_entry *he) \
118{ \ 181{ \
119 return __hpp__fmt(hpp, he, he_get_##_field, " %6.2f%%", \ 182 return __hpp__fmt(hpp, he, he_get_##_field, NULL, " %6.2f%%", \
120 percent_color_snprintf, true); \ 183 hpp_color_scnprintf, true); \
121} 184}
122 185
123#define __HPP_ENTRY_PERCENT_FN(_type, _field) \ 186#define __HPP_ENTRY_PERCENT_FN(_type, _field) \
@@ -125,8 +188,8 @@ static int hpp__entry_##_type(struct perf_hpp_fmt *_fmt __maybe_unused, \
125 struct perf_hpp *hpp, struct hist_entry *he) \ 188 struct perf_hpp *hpp, struct hist_entry *he) \
126{ \ 189{ \
127 const char *fmt = symbol_conf.field_sep ? " %.2f" : " %6.2f%%"; \ 190 const char *fmt = symbol_conf.field_sep ? " %.2f" : " %6.2f%%"; \
128 return __hpp__fmt(hpp, he, he_get_##_field, fmt, \ 191 return __hpp__fmt(hpp, he, he_get_##_field, NULL, fmt, \
129 scnprintf, true); \ 192 hpp_entry_scnprintf, true); \
130} 193}
131 194
132#define __HPP_ENTRY_RAW_FN(_type, _field) \ 195#define __HPP_ENTRY_RAW_FN(_type, _field) \
@@ -139,7 +202,8 @@ static int hpp__entry_##_type(struct perf_hpp_fmt *_fmt __maybe_unused, \
139 struct perf_hpp *hpp, struct hist_entry *he) \ 202 struct perf_hpp *hpp, struct hist_entry *he) \
140{ \ 203{ \
141 const char *fmt = symbol_conf.field_sep ? " %"PRIu64 : " %11"PRIu64; \ 204 const char *fmt = symbol_conf.field_sep ? " %"PRIu64 : " %11"PRIu64; \
142 return __hpp__fmt(hpp, he, he_get_raw_##_field, fmt, scnprintf, false); \ 205 return __hpp__fmt(hpp, he, he_get_raw_##_field, NULL, fmt, \
206 hpp_entry_scnprintf, false); \
143} 207}
144 208
145#define HPP_PERCENT_FNS(_type, _str, _field, _min_width, _unit_width) \ 209#define HPP_PERCENT_FNS(_type, _str, _field, _min_width, _unit_width) \
@@ -263,15 +327,13 @@ unsigned int hists__sort_list_width(struct hists *hists)
263 struct perf_hpp_fmt *fmt; 327 struct perf_hpp_fmt *fmt;
264 struct sort_entry *se; 328 struct sort_entry *se;
265 int i = 0, ret = 0; 329 int i = 0, ret = 0;
266 struct perf_hpp dummy_hpp = { 330 struct perf_hpp dummy_hpp;
267 .ptr = hists_to_evsel(hists),
268 };
269 331
270 perf_hpp__for_each_format(fmt) { 332 perf_hpp__for_each_format(fmt) {
271 if (i) 333 if (i)
272 ret += 2; 334 ret += 2;
273 335
274 ret += fmt->width(fmt, &dummy_hpp); 336 ret += fmt->width(fmt, &dummy_hpp, hists_to_evsel(hists));
275 } 337 }
276 338
277 list_for_each_entry(se, &hist_entry__sort_list, list) 339 list_for_each_entry(se, &hist_entry__sort_list, list)
diff --git a/tools/perf/ui/stdio/hist.c b/tools/perf/ui/stdio/hist.c
index 831fbb77d1ff..d59893edf031 100644
--- a/tools/perf/ui/stdio/hist.c
+++ b/tools/perf/ui/stdio/hist.c
@@ -306,12 +306,6 @@ static size_t hist_entry__callchain_fprintf(struct hist_entry *he,
306 return hist_entry_callchain__fprintf(he, total_period, left_margin, fp); 306 return hist_entry_callchain__fprintf(he, total_period, left_margin, fp);
307} 307}
308 308
309static inline void advance_hpp(struct perf_hpp *hpp, int inc)
310{
311 hpp->buf += inc;
312 hpp->size -= inc;
313}
314
315static int hist_entry__period_snprintf(struct perf_hpp *hpp, 309static int hist_entry__period_snprintf(struct perf_hpp *hpp,
316 struct hist_entry *he) 310 struct hist_entry *he)
317{ 311{
@@ -385,7 +379,6 @@ size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
385 struct perf_hpp dummy_hpp = { 379 struct perf_hpp dummy_hpp = {
386 .buf = bf, 380 .buf = bf,
387 .size = sizeof(bf), 381 .size = sizeof(bf),
388 .ptr = hists_to_evsel(hists),
389 }; 382 };
390 bool first = true; 383 bool first = true;
391 size_t linesz; 384 size_t linesz;
@@ -404,7 +397,7 @@ size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
404 else 397 else
405 first = false; 398 first = false;
406 399
407 fmt->header(fmt, &dummy_hpp); 400 fmt->header(fmt, &dummy_hpp, hists_to_evsel(hists));
408 fprintf(fp, "%s", bf); 401 fprintf(fp, "%s", bf);
409 } 402 }
410 403
@@ -449,7 +442,7 @@ size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
449 else 442 else
450 first = false; 443 first = false;
451 444
452 width = fmt->width(fmt, &dummy_hpp); 445 width = fmt->width(fmt, &dummy_hpp, hists_to_evsel(hists));
453 for (i = 0; i < width; i++) 446 for (i = 0; i < width; i++)
454 fprintf(fp, "."); 447 fprintf(fp, ".");
455 } 448 }
diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
index b0f3ca850e9e..3e580be0f6fb 100644
--- a/tools/perf/util/event.c
+++ b/tools/perf/util/event.c
@@ -94,14 +94,10 @@ static pid_t perf_event__get_comm_tgid(pid_t pid, char *comm, size_t len)
94 94
95static pid_t perf_event__synthesize_comm(struct perf_tool *tool, 95static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
96 union perf_event *event, pid_t pid, 96 union perf_event *event, pid_t pid,
97 int full,
98 perf_event__handler_t process, 97 perf_event__handler_t process,
99 struct machine *machine) 98 struct machine *machine)
100{ 99{
101 char filename[PATH_MAX];
102 size_t size; 100 size_t size;
103 DIR *tasks;
104 struct dirent dirent, *next;
105 pid_t tgid; 101 pid_t tgid;
106 102
107 memset(&event->comm, 0, sizeof(event->comm)); 103 memset(&event->comm, 0, sizeof(event->comm));
@@ -124,55 +120,35 @@ static pid_t perf_event__synthesize_comm(struct perf_tool *tool,
124 event->comm.header.size = (sizeof(event->comm) - 120 event->comm.header.size = (sizeof(event->comm) -
125 (sizeof(event->comm.comm) - size) + 121 (sizeof(event->comm.comm) - size) +
126 machine->id_hdr_size); 122 machine->id_hdr_size);
127 if (!full) { 123 event->comm.tid = pid;
128 event->comm.tid = pid;
129
130 if (process(tool, event, &synth_sample, machine) != 0)
131 return -1;
132 124
133 goto out; 125 if (process(tool, event, &synth_sample, machine) != 0)
134 } 126 return -1;
135
136 if (machine__is_default_guest(machine))
137 return 0;
138
139 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
140 machine->root_dir, pid);
141
142 tasks = opendir(filename);
143 if (tasks == NULL) {
144 pr_debug("couldn't open %s\n", filename);
145 return 0;
146 }
147 127
148 while (!readdir_r(tasks, &dirent, &next) && next) { 128out:
149 char *end; 129 return tgid;
150 pid = strtol(dirent.d_name, &end, 10); 130}
151 if (*end)
152 continue;
153 131
154 /* already have tgid; jut want to update the comm */ 132static int perf_event__synthesize_fork(struct perf_tool *tool,
155 (void) perf_event__get_comm_tgid(pid, event->comm.comm, 133 union perf_event *event, pid_t pid,
156 sizeof(event->comm.comm)); 134 pid_t tgid, perf_event__handler_t process,
135 struct machine *machine)
136{
137 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
157 138
158 size = strlen(event->comm.comm) + 1; 139 /* this is really a clone event but we use fork to synthesize it */
159 size = PERF_ALIGN(size, sizeof(u64)); 140 event->fork.ppid = tgid;
160 memset(event->comm.comm + size, 0, machine->id_hdr_size); 141 event->fork.ptid = tgid;
161 event->comm.header.size = (sizeof(event->comm) - 142 event->fork.pid = tgid;
162 (sizeof(event->comm.comm) - size) + 143 event->fork.tid = pid;
163 machine->id_hdr_size); 144 event->fork.header.type = PERF_RECORD_FORK;
164 145
165 event->comm.tid = pid; 146 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
166 147
167 if (process(tool, event, &synth_sample, machine) != 0) { 148 if (process(tool, event, &synth_sample, machine) != 0)
168 tgid = -1; 149 return -1;
169 break;
170 }
171 }
172 150
173 closedir(tasks); 151 return 0;
174out:
175 return tgid;
176} 152}
177 153
178int perf_event__synthesize_mmap_events(struct perf_tool *tool, 154int perf_event__synthesize_mmap_events(struct perf_tool *tool,
@@ -324,17 +300,71 @@ int perf_event__synthesize_modules(struct perf_tool *tool,
324 300
325static int __event__synthesize_thread(union perf_event *comm_event, 301static int __event__synthesize_thread(union perf_event *comm_event,
326 union perf_event *mmap_event, 302 union perf_event *mmap_event,
303 union perf_event *fork_event,
327 pid_t pid, int full, 304 pid_t pid, int full,
328 perf_event__handler_t process, 305 perf_event__handler_t process,
329 struct perf_tool *tool, 306 struct perf_tool *tool,
330 struct machine *machine, bool mmap_data) 307 struct machine *machine, bool mmap_data)
331{ 308{
332 pid_t tgid = perf_event__synthesize_comm(tool, comm_event, pid, full, 309 char filename[PATH_MAX];
310 DIR *tasks;
311 struct dirent dirent, *next;
312 pid_t tgid;
313
314 /* special case: only send one comm event using passed in pid */
315 if (!full) {
316 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
317 process, machine);
318
319 if (tgid == -1)
320 return -1;
321
322 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
323 process, machine, mmap_data);
324 }
325
326 if (machine__is_default_guest(machine))
327 return 0;
328
329 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
330 machine->root_dir, pid);
331
332 tasks = opendir(filename);
333 if (tasks == NULL) {
334 pr_debug("couldn't open %s\n", filename);
335 return 0;
336 }
337
338 while (!readdir_r(tasks, &dirent, &next) && next) {
339 char *end;
340 int rc = 0;
341 pid_t _pid;
342
343 _pid = strtol(dirent.d_name, &end, 10);
344 if (*end)
345 continue;
346
347 tgid = perf_event__synthesize_comm(tool, comm_event, _pid,
348 process, machine);
349 if (tgid == -1)
350 return -1;
351
352 if (_pid == pid) {
353 /* process the parent's maps too */
354 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
355 process, machine, mmap_data);
356 } else {
357 /* only fork the tid's map, to save time */
358 rc = perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
333 process, machine); 359 process, machine);
334 if (tgid == -1) 360 }
335 return -1; 361
336 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid, 362 if (rc)
337 process, machine, mmap_data); 363 return rc;
364 }
365
366 closedir(tasks);
367 return 0;
338} 368}
339 369
340int perf_event__synthesize_thread_map(struct perf_tool *tool, 370int perf_event__synthesize_thread_map(struct perf_tool *tool,
@@ -343,7 +373,7 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool,
343 struct machine *machine, 373 struct machine *machine,
344 bool mmap_data) 374 bool mmap_data)
345{ 375{
346 union perf_event *comm_event, *mmap_event; 376 union perf_event *comm_event, *mmap_event, *fork_event;
347 int err = -1, thread, j; 377 int err = -1, thread, j;
348 378
349 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size); 379 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
@@ -354,9 +384,14 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool,
354 if (mmap_event == NULL) 384 if (mmap_event == NULL)
355 goto out_free_comm; 385 goto out_free_comm;
356 386
387 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
388 if (fork_event == NULL)
389 goto out_free_mmap;
390
357 err = 0; 391 err = 0;
358 for (thread = 0; thread < threads->nr; ++thread) { 392 for (thread = 0; thread < threads->nr; ++thread) {
359 if (__event__synthesize_thread(comm_event, mmap_event, 393 if (__event__synthesize_thread(comm_event, mmap_event,
394 fork_event,
360 threads->map[thread], 0, 395 threads->map[thread], 0,
361 process, tool, machine, 396 process, tool, machine,
362 mmap_data)) { 397 mmap_data)) {
@@ -382,6 +417,7 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool,
382 /* if not, generate events for it */ 417 /* if not, generate events for it */
383 if (need_leader && 418 if (need_leader &&
384 __event__synthesize_thread(comm_event, mmap_event, 419 __event__synthesize_thread(comm_event, mmap_event,
420 fork_event,
385 comm_event->comm.pid, 0, 421 comm_event->comm.pid, 0,
386 process, tool, machine, 422 process, tool, machine,
387 mmap_data)) { 423 mmap_data)) {
@@ -390,6 +426,8 @@ int perf_event__synthesize_thread_map(struct perf_tool *tool,
390 } 426 }
391 } 427 }
392 } 428 }
429 free(fork_event);
430out_free_mmap:
393 free(mmap_event); 431 free(mmap_event);
394out_free_comm: 432out_free_comm:
395 free(comm_event); 433 free(comm_event);
@@ -404,7 +442,7 @@ int perf_event__synthesize_threads(struct perf_tool *tool,
404 DIR *proc; 442 DIR *proc;
405 char proc_path[PATH_MAX]; 443 char proc_path[PATH_MAX];
406 struct dirent dirent, *next; 444 struct dirent dirent, *next;
407 union perf_event *comm_event, *mmap_event; 445 union perf_event *comm_event, *mmap_event, *fork_event;
408 int err = -1; 446 int err = -1;
409 447
410 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size); 448 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
@@ -415,6 +453,10 @@ int perf_event__synthesize_threads(struct perf_tool *tool,
415 if (mmap_event == NULL) 453 if (mmap_event == NULL)
416 goto out_free_comm; 454 goto out_free_comm;
417 455
456 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
457 if (fork_event == NULL)
458 goto out_free_mmap;
459
418 if (machine__is_default_guest(machine)) 460 if (machine__is_default_guest(machine))
419 return 0; 461 return 0;
420 462
@@ -422,7 +464,7 @@ int perf_event__synthesize_threads(struct perf_tool *tool,
422 proc = opendir(proc_path); 464 proc = opendir(proc_path);
423 465
424 if (proc == NULL) 466 if (proc == NULL)
425 goto out_free_mmap; 467 goto out_free_fork;
426 468
427 while (!readdir_r(proc, &dirent, &next) && next) { 469 while (!readdir_r(proc, &dirent, &next) && next) {
428 char *end; 470 char *end;
@@ -434,12 +476,14 @@ int perf_event__synthesize_threads(struct perf_tool *tool,
434 * We may race with exiting thread, so don't stop just because 476 * We may race with exiting thread, so don't stop just because
435 * one thread couldn't be synthesized. 477 * one thread couldn't be synthesized.
436 */ 478 */
437 __event__synthesize_thread(comm_event, mmap_event, pid, 1, 479 __event__synthesize_thread(comm_event, mmap_event, fork_event, pid,
438 process, tool, machine, mmap_data); 480 1, process, tool, machine, mmap_data);
439 } 481 }
440 482
441 err = 0; 483 err = 0;
442 closedir(proc); 484 closedir(proc);
485out_free_fork:
486 free(fork_event);
443out_free_mmap: 487out_free_mmap:
444 free(mmap_event); 488 free(mmap_event);
445out_free_comm: 489out_free_comm:
diff --git a/tools/perf/util/hist.h b/tools/perf/util/hist.h
index a59743fa3ef7..0c76bf972736 100644
--- a/tools/perf/util/hist.h
+++ b/tools/perf/util/hist.h
@@ -132,8 +132,10 @@ struct perf_hpp {
132}; 132};
133 133
134struct perf_hpp_fmt { 134struct perf_hpp_fmt {
135 int (*header)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp); 135 int (*header)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
136 int (*width)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp); 136 struct perf_evsel *evsel);
137 int (*width)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
138 struct perf_evsel *evsel);
137 int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 139 int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
138 struct hist_entry *he); 140 struct hist_entry *he);
139 int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 141 int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
@@ -166,6 +168,20 @@ void perf_hpp__init(void);
166void perf_hpp__column_register(struct perf_hpp_fmt *format); 168void perf_hpp__column_register(struct perf_hpp_fmt *format);
167void perf_hpp__column_enable(unsigned col); 169void perf_hpp__column_enable(unsigned col);
168 170
171typedef u64 (*hpp_field_fn)(struct hist_entry *he);
172typedef int (*hpp_callback_fn)(struct perf_hpp *hpp, bool front);
173typedef int (*hpp_snprint_fn)(struct perf_hpp *hpp, const char *fmt, ...);
174
175int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he,
176 hpp_field_fn get_field, hpp_callback_fn callback,
177 const char *fmt, hpp_snprint_fn print_fn, bool fmt_percent);
178
179static inline void advance_hpp(struct perf_hpp *hpp, int inc)
180{
181 hpp->buf += inc;
182 hpp->size -= inc;
183}
184
169static inline size_t perf_hpp__use_color(void) 185static inline size_t perf_hpp__use_color(void)
170{ 186{
171 return !symbol_conf.field_sep; 187 return !symbol_conf.field_sep;
diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c
index 813e94e7cf29..5cecd98c1bc0 100644
--- a/tools/perf/util/machine.c
+++ b/tools/perf/util/machine.c
@@ -327,9 +327,10 @@ struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
327 return __machine__findnew_thread(machine, pid, tid, true); 327 return __machine__findnew_thread(machine, pid, tid, true);
328} 328}
329 329
330struct thread *machine__find_thread(struct machine *machine, pid_t tid) 330struct thread *machine__find_thread(struct machine *machine, pid_t pid,
331 pid_t tid)
331{ 332{
332 return __machine__findnew_thread(machine, 0, tid, false); 333 return __machine__findnew_thread(machine, pid, tid, false);
333} 334}
334 335
335int machine__process_comm_event(struct machine *machine, union perf_event *event, 336int machine__process_comm_event(struct machine *machine, union perf_event *event,
@@ -1114,7 +1115,9 @@ static void machine__remove_thread(struct machine *machine, struct thread *th)
1114int machine__process_fork_event(struct machine *machine, union perf_event *event, 1115int machine__process_fork_event(struct machine *machine, union perf_event *event,
1115 struct perf_sample *sample) 1116 struct perf_sample *sample)
1116{ 1117{
1117 struct thread *thread = machine__find_thread(machine, event->fork.tid); 1118 struct thread *thread = machine__find_thread(machine,
1119 event->fork.pid,
1120 event->fork.tid);
1118 struct thread *parent = machine__findnew_thread(machine, 1121 struct thread *parent = machine__findnew_thread(machine,
1119 event->fork.ppid, 1122 event->fork.ppid,
1120 event->fork.ptid); 1123 event->fork.ptid);
@@ -1140,7 +1143,9 @@ int machine__process_fork_event(struct machine *machine, union perf_event *event
1140int machine__process_exit_event(struct machine *machine, union perf_event *event, 1143int machine__process_exit_event(struct machine *machine, union perf_event *event,
1141 struct perf_sample *sample __maybe_unused) 1144 struct perf_sample *sample __maybe_unused)
1142{ 1145{
1143 struct thread *thread = machine__find_thread(machine, event->fork.tid); 1146 struct thread *thread = machine__find_thread(machine,
1147 event->fork.pid,
1148 event->fork.tid);
1144 1149
1145 if (dump_trace) 1150 if (dump_trace)
1146 perf_event__fprintf_task(event, stdout); 1151 perf_event__fprintf_task(event, stdout);
@@ -1184,39 +1189,22 @@ static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
1184 return 0; 1189 return 0;
1185} 1190}
1186 1191
1187static const u8 cpumodes[] = {
1188 PERF_RECORD_MISC_USER,
1189 PERF_RECORD_MISC_KERNEL,
1190 PERF_RECORD_MISC_GUEST_USER,
1191 PERF_RECORD_MISC_GUEST_KERNEL
1192};
1193#define NCPUMODES (sizeof(cpumodes)/sizeof(u8))
1194
1195static void ip__resolve_ams(struct machine *machine, struct thread *thread, 1192static void ip__resolve_ams(struct machine *machine, struct thread *thread,
1196 struct addr_map_symbol *ams, 1193 struct addr_map_symbol *ams,
1197 u64 ip) 1194 u64 ip)
1198{ 1195{
1199 struct addr_location al; 1196 struct addr_location al;
1200 size_t i;
1201 u8 m;
1202 1197
1203 memset(&al, 0, sizeof(al)); 1198 memset(&al, 0, sizeof(al));
1199 /*
1200 * We cannot use the header.misc hint to determine whether a
1201 * branch stack address is user, kernel, guest, hypervisor.
1202 * Branches may straddle the kernel/user/hypervisor boundaries.
1203 * Thus, we have to try consecutively until we find a match
1204 * or else, the symbol is unknown
1205 */
1206 thread__find_cpumode_addr_location(thread, machine, MAP__FUNCTION, ip, &al);
1204 1207
1205 for (i = 0; i < NCPUMODES; i++) {
1206 m = cpumodes[i];
1207 /*
1208 * We cannot use the header.misc hint to determine whether a
1209 * branch stack address is user, kernel, guest, hypervisor.
1210 * Branches may straddle the kernel/user/hypervisor boundaries.
1211 * Thus, we have to try consecutively until we find a match
1212 * or else, the symbol is unknown
1213 */
1214 thread__find_addr_location(thread, machine, m, MAP__FUNCTION,
1215 ip, &al);
1216 if (al.map)
1217 goto found;
1218 }
1219found:
1220 ams->addr = ip; 1208 ams->addr = ip;
1221 ams->al_addr = al.addr; 1209 ams->al_addr = al.addr;
1222 ams->sym = al.sym; 1210 ams->sym = al.sym;
diff --git a/tools/perf/util/machine.h b/tools/perf/util/machine.h
index 2e6c248c870f..c8c74a119398 100644
--- a/tools/perf/util/machine.h
+++ b/tools/perf/util/machine.h
@@ -41,7 +41,8 @@ struct map *machine__kernel_map(struct machine *machine, enum map_type type)
41 return machine->vmlinux_maps[type]; 41 return machine->vmlinux_maps[type];
42} 42}
43 43
44struct thread *machine__find_thread(struct machine *machine, pid_t tid); 44struct thread *machine__find_thread(struct machine *machine, pid_t pid,
45 pid_t tid);
45 46
46int machine__process_comm_event(struct machine *machine, union perf_event *event, 47int machine__process_comm_event(struct machine *machine, union perf_event *event,
47 struct perf_sample *sample); 48 struct perf_sample *sample);
diff --git a/tools/perf/util/parse-options.c b/tools/perf/util/parse-options.c
index d22e3f8017dc..bf48092983c6 100644
--- a/tools/perf/util/parse-options.c
+++ b/tools/perf/util/parse-options.c
@@ -407,7 +407,9 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
407 if (internal_help && !strcmp(arg + 2, "help")) 407 if (internal_help && !strcmp(arg + 2, "help"))
408 return usage_with_options_internal(usagestr, options, 0); 408 return usage_with_options_internal(usagestr, options, 0);
409 if (!strcmp(arg + 2, "list-opts")) 409 if (!strcmp(arg + 2, "list-opts"))
410 return PARSE_OPT_LIST; 410 return PARSE_OPT_LIST_OPTS;
411 if (!strcmp(arg + 2, "list-cmds"))
412 return PARSE_OPT_LIST_SUBCMDS;
411 switch (parse_long_opt(ctx, arg + 2, options)) { 413 switch (parse_long_opt(ctx, arg + 2, options)) {
412 case -1: 414 case -1:
413 return parse_options_usage(usagestr, options, arg + 2, 0); 415 return parse_options_usage(usagestr, options, arg + 2, 0);
@@ -433,25 +435,45 @@ int parse_options_end(struct parse_opt_ctx_t *ctx)
433 return ctx->cpidx + ctx->argc; 435 return ctx->cpidx + ctx->argc;
434} 436}
435 437
436int parse_options(int argc, const char **argv, const struct option *options, 438int parse_options_subcommand(int argc, const char **argv, const struct option *options,
437 const char * const usagestr[], int flags) 439 const char *const subcommands[], const char *usagestr[], int flags)
438{ 440{
439 struct parse_opt_ctx_t ctx; 441 struct parse_opt_ctx_t ctx;
440 442
441 perf_header__set_cmdline(argc, argv); 443 perf_header__set_cmdline(argc, argv);
442 444
445 /* build usage string if it's not provided */
446 if (subcommands && !usagestr[0]) {
447 struct strbuf buf = STRBUF_INIT;
448
449 strbuf_addf(&buf, "perf %s [<options>] {", argv[0]);
450 for (int i = 0; subcommands[i]; i++) {
451 if (i)
452 strbuf_addstr(&buf, "|");
453 strbuf_addstr(&buf, subcommands[i]);
454 }
455 strbuf_addstr(&buf, "}");
456
457 usagestr[0] = strdup(buf.buf);
458 strbuf_release(&buf);
459 }
460
443 parse_options_start(&ctx, argc, argv, flags); 461 parse_options_start(&ctx, argc, argv, flags);
444 switch (parse_options_step(&ctx, options, usagestr)) { 462 switch (parse_options_step(&ctx, options, usagestr)) {
445 case PARSE_OPT_HELP: 463 case PARSE_OPT_HELP:
446 exit(129); 464 exit(129);
447 case PARSE_OPT_DONE: 465 case PARSE_OPT_DONE:
448 break; 466 break;
449 case PARSE_OPT_LIST: 467 case PARSE_OPT_LIST_OPTS:
450 while (options->type != OPTION_END) { 468 while (options->type != OPTION_END) {
451 printf("--%s ", options->long_name); 469 printf("--%s ", options->long_name);
452 options++; 470 options++;
453 } 471 }
454 exit(130); 472 exit(130);
473 case PARSE_OPT_LIST_SUBCMDS:
474 for (int i = 0; subcommands[i]; i++)
475 printf("%s ", subcommands[i]);
476 exit(130);
455 default: /* PARSE_OPT_UNKNOWN */ 477 default: /* PARSE_OPT_UNKNOWN */
456 if (ctx.argv[0][1] == '-') { 478 if (ctx.argv[0][1] == '-') {
457 error("unknown option `%s'", ctx.argv[0] + 2); 479 error("unknown option `%s'", ctx.argv[0] + 2);
@@ -464,6 +486,13 @@ int parse_options(int argc, const char **argv, const struct option *options,
464 return parse_options_end(&ctx); 486 return parse_options_end(&ctx);
465} 487}
466 488
489int parse_options(int argc, const char **argv, const struct option *options,
490 const char * const usagestr[], int flags)
491{
492 return parse_options_subcommand(argc, argv, options, NULL,
493 (const char **) usagestr, flags);
494}
495
467#define USAGE_OPTS_WIDTH 24 496#define USAGE_OPTS_WIDTH 24
468#define USAGE_GAP 2 497#define USAGE_GAP 2
469 498
diff --git a/tools/perf/util/parse-options.h b/tools/perf/util/parse-options.h
index cbf0149cf221..d8dac8ac5f37 100644
--- a/tools/perf/util/parse-options.h
+++ b/tools/perf/util/parse-options.h
@@ -140,6 +140,11 @@ extern int parse_options(int argc, const char **argv,
140 const struct option *options, 140 const struct option *options,
141 const char * const usagestr[], int flags); 141 const char * const usagestr[], int flags);
142 142
143extern int parse_options_subcommand(int argc, const char **argv,
144 const struct option *options,
145 const char *const subcommands[],
146 const char *usagestr[], int flags);
147
143extern NORETURN void usage_with_options(const char * const *usagestr, 148extern NORETURN void usage_with_options(const char * const *usagestr,
144 const struct option *options); 149 const struct option *options);
145 150
@@ -148,7 +153,8 @@ extern NORETURN void usage_with_options(const char * const *usagestr,
148enum { 153enum {
149 PARSE_OPT_HELP = -1, 154 PARSE_OPT_HELP = -1,
150 PARSE_OPT_DONE, 155 PARSE_OPT_DONE,
151 PARSE_OPT_LIST, 156 PARSE_OPT_LIST_OPTS,
157 PARSE_OPT_LIST_SUBCMDS,
152 PARSE_OPT_UNKNOWN, 158 PARSE_OPT_UNKNOWN,
153}; 159};
154 160
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 1d555d652f58..55960f22233c 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -794,7 +794,7 @@ static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
794 if (!dump_trace) 794 if (!dump_trace)
795 return; 795 return;
796 796
797 printf("(IP, %d): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n", 797 printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
798 event->header.misc, sample->pid, sample->tid, sample->ip, 798 event->header.misc, sample->pid, sample->tid, sample->ip,
799 sample->period, sample->addr); 799 sample->period, sample->addr);
800 800
diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index 0358882c8910..3ce0498bdae6 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -142,3 +142,24 @@ int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
142 142
143 return 0; 143 return 0;
144} 144}
145
146void thread__find_cpumode_addr_location(struct thread *thread,
147 struct machine *machine,
148 enum map_type type, u64 addr,
149 struct addr_location *al)
150{
151 size_t i;
152 const u8 const cpumodes[] = {
153 PERF_RECORD_MISC_USER,
154 PERF_RECORD_MISC_KERNEL,
155 PERF_RECORD_MISC_GUEST_USER,
156 PERF_RECORD_MISC_GUEST_KERNEL
157 };
158
159 for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
160 thread__find_addr_location(thread, machine, cpumodes[i], type,
161 addr, al);
162 if (al->map)
163 break;
164 }
165}
diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h
index 5b856bf942e1..9a070743270c 100644
--- a/tools/perf/util/thread.h
+++ b/tools/perf/util/thread.h
@@ -58,6 +58,11 @@ void thread__find_addr_location(struct thread *thread, struct machine *machine,
58 u8 cpumode, enum map_type type, u64 addr, 58 u8 cpumode, enum map_type type, u64 addr,
59 struct addr_location *al); 59 struct addr_location *al);
60 60
61void thread__find_cpumode_addr_location(struct thread *thread,
62 struct machine *machine,
63 enum map_type type, u64 addr,
64 struct addr_location *al);
65
61static inline void *thread__priv(struct thread *thread) 66static inline void *thread__priv(struct thread *thread)
62{ 67{
63 return thread->priv; 68 return thread->priv;