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