Commit | Line | Data |
---|---|---|
bf9e1876 IM |
1 | /* |
2 | * builtin-report.c | |
3 | * | |
4 | * Builtin report command: Analyze the perf.data input file, | |
5 | * look up and read DSOs and symbol information and display | |
6 | * a histogram of results, along various sorting keys. | |
7 | */ | |
16f762a2 | 8 | #include "builtin.h" |
53cb8bc2 | 9 | |
bf9e1876 IM |
10 | #include "util/util.h" |
11 | ||
8fc0321f | 12 | #include "util/color.h" |
5da50258 | 13 | #include <linux/list.h> |
a930d2c0 | 14 | #include "util/cache.h" |
43cbcd8a | 15 | #include <linux/rbtree.h> |
a2928c42 | 16 | #include "util/symbol.h" |
f55c5552 | 17 | #include "util/callchain.h" |
25903407 | 18 | #include "util/strlist.h" |
8d513270 | 19 | #include "util/values.h" |
8fa66bdc | 20 | |
53cb8bc2 | 21 | #include "perf.h" |
8f28827a | 22 | #include "util/debug.h" |
7c6a1c65 | 23 | #include "util/header.h" |
94c744b6 | 24 | #include "util/session.h" |
53cb8bc2 IM |
25 | |
26 | #include "util/parse-options.h" | |
27 | #include "util/parse-events.h" | |
28 | ||
6baa0a5a | 29 | #include "util/thread.h" |
dd68ada2 | 30 | #include "util/sort.h" |
3d1d07ec | 31 | #include "util/hist.h" |
6baa0a5a | 32 | |
23ac9cbe | 33 | static char const *input_name = "perf.data"; |
bd74137e | 34 | |
c0555642 | 35 | static bool force; |
71289be7 | 36 | static bool hide_unresolved; |
b9a63b9b | 37 | static bool dont_use_callchains; |
97b07b69 | 38 | |
c0555642 | 39 | static bool show_threads; |
8d513270 BG |
40 | static struct perf_read_values show_threads_values; |
41 | ||
9f866697 BG |
42 | static char default_pretty_printing_style[] = "normal"; |
43 | static char *pretty_printing_style = default_pretty_printing_style; | |
44 | ||
805d127d FW |
45 | static char callchain_default_opt[] = "fractal,0.5"; |
46 | ||
cbbc79a5 EM |
47 | static struct event_stat_id *get_stats(struct perf_session *self, |
48 | u64 event_stream, u32 type, u64 config) | |
49 | { | |
50 | struct rb_node **p = &self->stats_by_id.rb_node; | |
51 | struct rb_node *parent = NULL; | |
52 | struct event_stat_id *iter, *new; | |
53 | ||
54 | while (*p != NULL) { | |
55 | parent = *p; | |
56 | iter = rb_entry(parent, struct event_stat_id, rb_node); | |
57 | if (iter->config == config) | |
58 | return iter; | |
59 | ||
60 | ||
61 | if (config > iter->config) | |
62 | p = &(*p)->rb_right; | |
63 | else | |
64 | p = &(*p)->rb_left; | |
65 | } | |
66 | ||
67 | new = malloc(sizeof(struct event_stat_id)); | |
68 | if (new == NULL) | |
69 | return NULL; | |
70 | memset(new, 0, sizeof(struct event_stat_id)); | |
71 | new->event_stream = event_stream; | |
72 | new->config = config; | |
73 | new->type = type; | |
74 | rb_link_node(&new->rb_node, parent, p); | |
75 | rb_insert_color(&new->rb_node, &self->stats_by_id); | |
76 | return new; | |
77 | } | |
78 | ||
4e4f06e4 ACM |
79 | static int perf_session__add_hist_entry(struct perf_session *self, |
80 | struct addr_location *al, | |
cbbc79a5 | 81 | struct sample_data *data) |
8fa66bdc | 82 | { |
b3c9ac08 ACM |
83 | struct map_symbol *syms = NULL; |
84 | struct symbol *parent = NULL; | |
9735abf1 | 85 | bool hit; |
301fde27 | 86 | int err; |
e7fb08b1 | 87 | struct hist_entry *he; |
cbbc79a5 EM |
88 | struct event_stat_id *stats; |
89 | struct perf_event_attr *attr; | |
e7fb08b1 | 90 | |
ad5b217b | 91 | if ((sort__has_parent || symbol_conf.use_callchain) && data->callchain) { |
a328626b | 92 | syms = perf_session__resolve_callchain(self, al->thread, |
cbbc79a5 | 93 | data->callchain, &parent); |
ad5b217b ACM |
94 | if (syms == NULL) |
95 | return -ENOMEM; | |
96 | } | |
cbbc79a5 EM |
97 | |
98 | attr = perf_header__find_attr(data->id, &self->header); | |
99 | if (attr) | |
100 | stats = get_stats(self, data->id, attr->type, attr->config); | |
101 | else | |
102 | stats = get_stats(self, data->id, 0, 0); | |
103 | if (stats == NULL) | |
104 | return -ENOMEM; | |
105 | he = __perf_session__add_hist_entry(&stats->hists, al, parent, | |
106 | data->period, &hit); | |
9735abf1 ACM |
107 | if (he == NULL) |
108 | return -ENOMEM; | |
e7fb08b1 | 109 | |
9735abf1 | 110 | if (hit) |
cbbc79a5 | 111 | he->count += data->period; |
e7fb08b1 | 112 | |
d599db3f | 113 | if (symbol_conf.use_callchain) { |
9735abf1 | 114 | if (!hit) |
b9fb9304 ACM |
115 | callchain_init(he->callchain); |
116 | err = append_chain(he->callchain, data->callchain, syms); | |
4424961a | 117 | free(syms); |
301fde27 FW |
118 | |
119 | if (err) | |
120 | return err; | |
f55c5552 | 121 | } |
e7fb08b1 PZ |
122 | |
123 | return 0; | |
8fa66bdc ACM |
124 | } |
125 | ||
2a0a50fe | 126 | static int validate_chain(struct ip_callchain *chain, event_t *event) |
7522060c IM |
127 | { |
128 | unsigned int chain_size; | |
129 | ||
7522060c IM |
130 | chain_size = event->header.size; |
131 | chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event; | |
132 | ||
9cffa8d5 | 133 | if (chain->nr*sizeof(u64) > chain_size) |
7522060c IM |
134 | return -1; |
135 | ||
136 | return 0; | |
137 | } | |
138 | ||
cbbc79a5 EM |
139 | static int add_event_total(struct perf_session *session, |
140 | struct sample_data *data, | |
141 | struct perf_event_attr *attr) | |
142 | { | |
143 | struct event_stat_id *stats; | |
144 | ||
145 | if (attr) | |
146 | stats = get_stats(session, data->id, attr->type, attr->config); | |
147 | else | |
148 | stats = get_stats(session, data->id, 0, 0); | |
149 | ||
150 | if (!stats) | |
151 | return -ENOMEM; | |
152 | ||
153 | stats->stats.total += data->period; | |
154 | session->events_stats.total += data->period; | |
155 | return 0; | |
156 | } | |
157 | ||
b3165f41 | 158 | static int process_sample_event(event_t *event, struct perf_session *session) |
75051724 | 159 | { |
c410a338 | 160 | struct sample_data data = { .period = 1, }; |
1ed091c4 | 161 | struct addr_location al; |
cbbc79a5 | 162 | struct perf_event_attr *attr; |
180f95e2 | 163 | |
c019879b | 164 | event__parse_sample(event, session->sample_type, &data); |
ea1900e5 | 165 | |
0d755034 ACM |
166 | dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc, |
167 | data.pid, data.tid, data.ip, data.period); | |
75051724 | 168 | |
c019879b | 169 | if (session->sample_type & PERF_SAMPLE_CALLCHAIN) { |
f37a291c | 170 | unsigned int i; |
3efa1cc9 | 171 | |
180f95e2 | 172 | dump_printf("... chain: nr:%Lu\n", data.callchain->nr); |
3efa1cc9 | 173 | |
180f95e2 | 174 | if (validate_chain(data.callchain, event) < 0) { |
6beba7ad ACM |
175 | pr_debug("call-chain problem with event, " |
176 | "skipping it.\n"); | |
7522060c IM |
177 | return 0; |
178 | } | |
179 | ||
180 | if (dump_trace) { | |
180f95e2 OH |
181 | for (i = 0; i < data.callchain->nr; i++) |
182 | dump_printf("..... %2d: %016Lx\n", | |
183 | i, data.callchain->ips[i]); | |
3efa1cc9 IM |
184 | } |
185 | } | |
186 | ||
c410a338 ACM |
187 | if (event__preprocess_sample(event, session, &al, NULL) < 0) { |
188 | fprintf(stderr, "problem processing %d event, skipping it.\n", | |
75051724 IM |
189 | event->header.type); |
190 | return -1; | |
191 | } | |
e7fb08b1 | 192 | |
71289be7 | 193 | if (al.filtered || (hide_unresolved && al.sym == NULL)) |
ec218fc4 | 194 | return 0; |
7bec7a91 | 195 | |
cbbc79a5 | 196 | if (perf_session__add_hist_entry(session, &al, &data)) { |
6beba7ad | 197 | pr_debug("problem incrementing symbol count, skipping event\n"); |
ec218fc4 | 198 | return -1; |
8fa66bdc | 199 | } |
ec218fc4 | 200 | |
cbbc79a5 EM |
201 | attr = perf_header__find_attr(data.id, &session->header); |
202 | ||
203 | if (add_event_total(session, &data, attr)) { | |
204 | pr_debug("problem adding event count\n"); | |
205 | return -1; | |
206 | } | |
207 | ||
75051724 IM |
208 | return 0; |
209 | } | |
3502973d | 210 | |
d8f66248 | 211 | static int process_read_event(event_t *event, struct perf_session *session __used) |
e9ea2fde | 212 | { |
cdd6c482 | 213 | struct perf_event_attr *attr; |
0d3a5c88 | 214 | |
94c744b6 | 215 | attr = perf_header__find_attr(event->read.id, &session->header); |
8f18aec5 | 216 | |
8d513270 | 217 | if (show_threads) { |
83a0944f | 218 | const char *name = attr ? __event_name(attr->type, attr->config) |
8d513270 BG |
219 | : "unknown"; |
220 | perf_read_values_add_value(&show_threads_values, | |
221 | event->read.pid, event->read.tid, | |
222 | event->read.id, | |
223 | name, | |
224 | event->read.value); | |
225 | } | |
226 | ||
62daacb5 ACM |
227 | dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid, |
228 | attr ? __event_name(attr->type, attr->config) : "FAIL", | |
229 | event->read.value); | |
e9ea2fde PZ |
230 | |
231 | return 0; | |
232 | } | |
233 | ||
d549c769 | 234 | static int perf_session__setup_sample_type(struct perf_session *self) |
d80d338d | 235 | { |
d549c769 | 236 | if (!(self->sample_type & PERF_SAMPLE_CALLCHAIN)) { |
91b4eaea FW |
237 | if (sort__has_parent) { |
238 | fprintf(stderr, "selected --sort parent, but no" | |
239 | " callchain data. Did you call" | |
240 | " perf record without -g?\n"); | |
d549c769 | 241 | return -EINVAL; |
91b4eaea | 242 | } |
d599db3f | 243 | if (symbol_conf.use_callchain) { |
6ede59c4 | 244 | fprintf(stderr, "selected -g but no callchain data." |
91b4eaea FW |
245 | " Did you call perf record without" |
246 | " -g?\n"); | |
016e92fb | 247 | return -1; |
91b4eaea | 248 | } |
b9a63b9b ACM |
249 | } else if (!dont_use_callchains && callchain_param.mode != CHAIN_NONE && |
250 | !symbol_conf.use_callchain) { | |
d599db3f | 251 | symbol_conf.use_callchain = true; |
b1a88349 FW |
252 | if (register_callchain_param(&callchain_param) < 0) { |
253 | fprintf(stderr, "Can't register callchain" | |
254 | " params\n"); | |
d549c769 | 255 | return -EINVAL; |
b1a88349 | 256 | } |
f5970550 PZ |
257 | } |
258 | ||
016e92fb FW |
259 | return 0; |
260 | } | |
6142f9ec | 261 | |
301a0b02 | 262 | static struct perf_event_ops event_ops = { |
55aa640f ACM |
263 | .sample = process_sample_event, |
264 | .mmap = event__process_mmap, | |
265 | .comm = event__process_comm, | |
266 | .exit = event__process_task, | |
267 | .fork = event__process_task, | |
268 | .lost = event__process_lost, | |
269 | .read = process_read_event, | |
016e92fb | 270 | }; |
6142f9ec | 271 | |
016e92fb FW |
272 | static int __cmd_report(void) |
273 | { | |
d549c769 | 274 | int ret = -EINVAL; |
d8f66248 | 275 | struct perf_session *session; |
cbbc79a5 | 276 | struct rb_node *next; |
f9224c5c | 277 | const char *help = "For a higher level overview, try: perf report --sort comm,dso"; |
8fa66bdc | 278 | |
75be6cf4 | 279 | session = perf_session__new(input_name, O_RDONLY, force); |
94c744b6 ACM |
280 | if (session == NULL) |
281 | return -ENOMEM; | |
282 | ||
016e92fb FW |
283 | if (show_threads) |
284 | perf_read_values_init(&show_threads_values); | |
f5970550 | 285 | |
d549c769 ACM |
286 | ret = perf_session__setup_sample_type(session); |
287 | if (ret) | |
288 | goto out_delete; | |
289 | ||
ec913369 | 290 | ret = perf_session__process_events(session, &event_ops); |
016e92fb | 291 | if (ret) |
94c744b6 | 292 | goto out_delete; |
97b07b69 | 293 | |
62daacb5 ACM |
294 | if (dump_trace) { |
295 | event__print_totals(); | |
94c744b6 | 296 | goto out_delete; |
62daacb5 | 297 | } |
97b07b69 | 298 | |
da21d1b5 | 299 | if (verbose > 3) |
b3165f41 | 300 | perf_session__fprintf(session, stdout); |
9ac99545 | 301 | |
da21d1b5 | 302 | if (verbose > 2) |
16f762a2 | 303 | dsos__fprintf(stdout); |
16f762a2 | 304 | |
cbbc79a5 EM |
305 | next = rb_first(&session->stats_by_id); |
306 | while (next) { | |
307 | struct event_stat_id *stats; | |
5f4d3f88 | 308 | u64 nr_hists; |
cbbc79a5 EM |
309 | |
310 | stats = rb_entry(next, struct event_stat_id, rb_node); | |
311 | perf_session__collapse_resort(&stats->hists); | |
5f4d3f88 ACM |
312 | nr_hists = perf_session__output_resort(&stats->hists, |
313 | stats->stats.total); | |
f9224c5c | 314 | if (use_browser) |
5f4d3f88 | 315 | perf_session__browse_hists(&stats->hists, nr_hists, |
533c46c3 ACM |
316 | stats->stats.total, help, |
317 | input_name); | |
f9224c5c ACM |
318 | else { |
319 | if (rb_first(&session->stats_by_id) == | |
320 | rb_last(&session->stats_by_id)) | |
321 | fprintf(stdout, "# Samples: %Ld\n#\n", | |
322 | stats->stats.total); | |
323 | else | |
324 | fprintf(stdout, "# Samples: %Ld %s\n#\n", | |
325 | stats->stats.total, | |
326 | __event_name(stats->type, stats->config)); | |
327 | ||
328 | perf_session__fprintf_hists(&stats->hists, NULL, false, stdout, | |
cbbc79a5 | 329 | stats->stats.total); |
f9224c5c ACM |
330 | fprintf(stdout, "\n\n"); |
331 | } | |
332 | ||
cbbc79a5 EM |
333 | next = rb_next(&stats->rb_node); |
334 | } | |
335 | ||
f9224c5c ACM |
336 | if (!use_browser && sort_order == default_sort_order && |
337 | parent_pattern == default_parent_pattern) { | |
338 | fprintf(stdout, "#\n# (%s)\n#\n", help); | |
3e6055ab | 339 | |
f9224c5c ACM |
340 | if (show_threads) { |
341 | bool style = !strcmp(pretty_printing_style, "raw"); | |
342 | perf_read_values_display(stdout, &show_threads_values, | |
343 | style); | |
344 | perf_read_values_destroy(&show_threads_values); | |
345 | } | |
d599db3f | 346 | } |
94c744b6 ACM |
347 | out_delete: |
348 | perf_session__delete(session); | |
016e92fb | 349 | return ret; |
8fa66bdc ACM |
350 | } |
351 | ||
4eb3e478 FW |
352 | static int |
353 | parse_callchain_opt(const struct option *opt __used, const char *arg, | |
b9a63b9b | 354 | int unset) |
4eb3e478 | 355 | { |
c20ab37e FW |
356 | char *tok; |
357 | char *endptr; | |
358 | ||
b9a63b9b ACM |
359 | /* |
360 | * --no-call-graph | |
361 | */ | |
362 | if (unset) { | |
363 | dont_use_callchains = true; | |
364 | return 0; | |
365 | } | |
366 | ||
d599db3f | 367 | symbol_conf.use_callchain = true; |
4eb3e478 FW |
368 | |
369 | if (!arg) | |
370 | return 0; | |
371 | ||
c20ab37e FW |
372 | tok = strtok((char *)arg, ","); |
373 | if (!tok) | |
374 | return -1; | |
375 | ||
376 | /* get the output mode */ | |
377 | if (!strncmp(tok, "graph", strlen(arg))) | |
805d127d | 378 | callchain_param.mode = CHAIN_GRAPH_ABS; |
4eb3e478 | 379 | |
c20ab37e | 380 | else if (!strncmp(tok, "flat", strlen(arg))) |
805d127d FW |
381 | callchain_param.mode = CHAIN_FLAT; |
382 | ||
383 | else if (!strncmp(tok, "fractal", strlen(arg))) | |
384 | callchain_param.mode = CHAIN_GRAPH_REL; | |
385 | ||
b1a88349 FW |
386 | else if (!strncmp(tok, "none", strlen(arg))) { |
387 | callchain_param.mode = CHAIN_NONE; | |
b7ae11b3 | 388 | symbol_conf.use_callchain = false; |
b1a88349 FW |
389 | |
390 | return 0; | |
391 | } | |
392 | ||
4eb3e478 FW |
393 | else |
394 | return -1; | |
395 | ||
c20ab37e FW |
396 | /* get the min percentage */ |
397 | tok = strtok(NULL, ","); | |
398 | if (!tok) | |
805d127d | 399 | goto setup; |
c20ab37e | 400 | |
805d127d | 401 | callchain_param.min_percent = strtod(tok, &endptr); |
c20ab37e FW |
402 | if (tok == endptr) |
403 | return -1; | |
404 | ||
805d127d FW |
405 | setup: |
406 | if (register_callchain_param(&callchain_param) < 0) { | |
407 | fprintf(stderr, "Can't register callchain params\n"); | |
408 | return -1; | |
409 | } | |
4eb3e478 FW |
410 | return 0; |
411 | } | |
412 | ||
0422a4fc | 413 | static const char * const report_usage[] = { |
53cb8bc2 IM |
414 | "perf report [<options>] <command>", |
415 | NULL | |
416 | }; | |
417 | ||
418 | static const struct option options[] = { | |
419 | OPT_STRING('i', "input", &input_name, "file", | |
420 | "input file name"), | |
c0555642 | 421 | OPT_INCR('v', "verbose", &verbose, |
815e777f | 422 | "be more verbose (show symbol address, etc)"), |
97b07b69 IM |
423 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
424 | "dump raw trace in ASCII"), | |
b32d133a ACM |
425 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
426 | "file", "vmlinux pathname"), | |
fa6963b2 | 427 | OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), |
b32d133a | 428 | OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, |
42976487 | 429 | "load module symbols - WARNING: use only with -k and LIVE kernel"), |
d599db3f | 430 | OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples, |
e3d7e183 | 431 | "Show a column with the number of samples"), |
8d513270 BG |
432 | OPT_BOOLEAN('T', "threads", &show_threads, |
433 | "Show per-thread event counters"), | |
9f866697 BG |
434 | OPT_STRING(0, "pretty", &pretty_printing_style, "key", |
435 | "pretty printing style key: normal raw"), | |
63299f05 | 436 | OPT_STRING('s', "sort", &sort_order, "key[,key2...]", |
b25bcf2f | 437 | "sort by key(s): pid, comm, dso, symbol, parent"), |
f7d87444 | 438 | OPT_BOOLEAN('P', "full-paths", &symbol_conf.full_paths, |
b78c07d4 | 439 | "Don't shorten the pathnames taking into account the cwd"), |
b25bcf2f IM |
440 | OPT_STRING('p', "parent", &parent_pattern, "regex", |
441 | "regex filter to identify parent, see: '--sort parent'"), | |
d599db3f | 442 | OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other, |
b8e6d829 | 443 | "Only display entries with parent-match"), |
1483b19f | 444 | OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent", |
c20ab37e | 445 | "Display callchains using output_type and min percent threshold. " |
1483b19f | 446 | "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt), |
655000e7 | 447 | OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]", |
25903407 | 448 | "only consider symbols in these dsos"), |
655000e7 | 449 | OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]", |
cc8b88b1 | 450 | "only consider symbols in these comms"), |
655000e7 | 451 | OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]", |
7bec7a91 | 452 | "only consider these symbols"), |
655000e7 | 453 | OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str, |
52d422de ACM |
454 | "width[,width...]", |
455 | "don't try to adjust column width, use these fixed values"), | |
c410a338 | 456 | OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator", |
52d422de ACM |
457 | "separator for columns, no spaces will be added between " |
458 | "columns '.' is reserved."), | |
71289be7 ACM |
459 | OPT_BOOLEAN('U', "hide-unresolved", &hide_unresolved, |
460 | "Only display entries resolved to a symbol"), | |
53cb8bc2 IM |
461 | OPT_END() |
462 | }; | |
463 | ||
f37a291c | 464 | int cmd_report(int argc, const char **argv, const char *prefix __used) |
53cb8bc2 | 465 | { |
655000e7 ACM |
466 | argc = parse_options(argc, argv, options, report_usage, 0); |
467 | ||
f9224c5c | 468 | setup_browser(); |
655000e7 | 469 | |
75be6cf4 | 470 | if (symbol__init() < 0) |
b32d133a | 471 | return -1; |
53cb8bc2 | 472 | |
c8829c7a | 473 | setup_sorting(report_usage, options); |
1aa16738 | 474 | |
021191b3 | 475 | if (parent_pattern != default_parent_pattern) { |
2aefa4f7 ACM |
476 | if (sort_dimension__add("parent") < 0) |
477 | return -1; | |
021191b3 ACM |
478 | sort_parent.elide = 1; |
479 | } else | |
d599db3f | 480 | symbol_conf.exclude_other = false; |
b8e6d829 | 481 | |
edc52dea IM |
482 | /* |
483 | * Any (unrecognized) arguments left? | |
484 | */ | |
485 | if (argc) | |
486 | usage_with_options(report_usage, options); | |
487 | ||
655000e7 ACM |
488 | sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout); |
489 | sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout); | |
490 | sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout); | |
25903407 | 491 | |
53cb8bc2 IM |
492 | return __cmd_report(); |
493 | } |