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.
10 #include "util/util.h"
12 #include "util/color.h"
13 #include <linux/list.h>
14 #include "util/cache.h"
15 #include <linux/rbtree.h>
16 #include "util/symbol.h"
17 #include "util/string.h"
18 #include "util/callchain.h"
19 #include "util/strlist.h"
20 #include "util/values.h"
23 #include "util/debug.h"
24 #include "util/header.h"
25 #include "util/session.h"
27 #include "util/parse-options.h"
28 #include "util/parse-events.h"
30 #include "util/thread.h"
31 #include "util/sort.h"
32 #include "util/hist.h"
34 static char const *input_name = "perf.data";
37 static bool hide_unresolved;
38 static bool dont_use_callchains;
40 static int show_threads;
41 static struct perf_read_values show_threads_values;
43 static char default_pretty_printing_style[] = "normal";
44 static char *pretty_printing_style = default_pretty_printing_style;
46 static char callchain_default_opt[] = "fractal,0.5";
48 static struct event_stat_id *get_stats(struct perf_session *self,
49 u64 event_stream, u32 type, u64 config)
51 struct rb_node **p = &self->stats_by_id.rb_node;
52 struct rb_node *parent = NULL;
53 struct event_stat_id *iter, *new;
57 iter = rb_entry(parent, struct event_stat_id, rb_node);
58 if (iter->config == config)
62 if (config > iter->config)
68 new = malloc(sizeof(struct event_stat_id));
71 memset(new, 0, sizeof(struct event_stat_id));
72 new->event_stream = event_stream;
75 rb_link_node(&new->rb_node, parent, p);
76 rb_insert_color(&new->rb_node, &self->stats_by_id);
80 static int perf_session__add_hist_entry(struct perf_session *self,
81 struct addr_location *al,
82 struct sample_data *data)
84 struct map_symbol *syms = NULL;
85 struct symbol *parent = NULL;
88 struct hist_entry *he;
89 struct event_stat_id *stats;
90 struct perf_event_attr *attr;
92 if ((sort__has_parent || symbol_conf.use_callchain) && data->callchain)
93 syms = perf_session__resolve_callchain(self, al->thread,
94 data->callchain, &parent);
96 attr = perf_header__find_attr(data->id, &self->header);
98 stats = get_stats(self, data->id, attr->type, attr->config);
100 stats = get_stats(self, data->id, 0, 0);
103 he = __perf_session__add_hist_entry(&stats->hists, al, parent,
109 he->count += data->period;
111 if (symbol_conf.use_callchain) {
113 callchain_init(&he->callchain);
114 err = append_chain(&he->callchain, data->callchain, syms);
124 static int validate_chain(struct ip_callchain *chain, event_t *event)
126 unsigned int chain_size;
128 chain_size = event->header.size;
129 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
131 if (chain->nr*sizeof(u64) > chain_size)
137 static int add_event_total(struct perf_session *session,
138 struct sample_data *data,
139 struct perf_event_attr *attr)
141 struct event_stat_id *stats;
144 stats = get_stats(session, data->id, attr->type, attr->config);
146 stats = get_stats(session, data->id, 0, 0);
151 stats->stats.total += data->period;
152 session->events_stats.total += data->period;
156 static int process_sample_event(event_t *event, struct perf_session *session)
158 struct sample_data data = { .period = 1, };
159 struct addr_location al;
160 struct perf_event_attr *attr;
162 event__parse_sample(event, session->sample_type, &data);
164 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
165 data.pid, data.tid, data.ip, data.period);
167 if (session->sample_type & PERF_SAMPLE_CALLCHAIN) {
170 dump_printf("... chain: nr:%Lu\n", data.callchain->nr);
172 if (validate_chain(data.callchain, event) < 0) {
173 pr_debug("call-chain problem with event, "
179 for (i = 0; i < data.callchain->nr; i++)
180 dump_printf("..... %2d: %016Lx\n",
181 i, data.callchain->ips[i]);
185 if (event__preprocess_sample(event, session, &al, NULL) < 0) {
186 fprintf(stderr, "problem processing %d event, skipping it.\n",
191 if (al.filtered || (hide_unresolved && al.sym == NULL))
194 if (perf_session__add_hist_entry(session, &al, &data)) {
195 pr_debug("problem incrementing symbol count, skipping event\n");
199 attr = perf_header__find_attr(data.id, &session->header);
201 if (add_event_total(session, &data, attr)) {
202 pr_debug("problem adding event count\n");
209 static int process_read_event(event_t *event, struct perf_session *session __used)
211 struct perf_event_attr *attr;
213 attr = perf_header__find_attr(event->read.id, &session->header);
216 const char *name = attr ? __event_name(attr->type, attr->config)
218 perf_read_values_add_value(&show_threads_values,
219 event->read.pid, event->read.tid,
225 dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid,
226 attr ? __event_name(attr->type, attr->config) : "FAIL",
232 static int perf_session__setup_sample_type(struct perf_session *self)
234 if (!(self->sample_type & PERF_SAMPLE_CALLCHAIN)) {
235 if (sort__has_parent) {
236 fprintf(stderr, "selected --sort parent, but no"
237 " callchain data. Did you call"
238 " perf record without -g?\n");
241 if (symbol_conf.use_callchain) {
242 fprintf(stderr, "selected -g but no callchain data."
243 " Did you call perf record without"
247 } else if (!dont_use_callchains && callchain_param.mode != CHAIN_NONE &&
248 !symbol_conf.use_callchain) {
249 symbol_conf.use_callchain = true;
250 if (register_callchain_param(&callchain_param) < 0) {
251 fprintf(stderr, "Can't register callchain"
260 static struct perf_event_ops event_ops = {
261 .sample = process_sample_event,
262 .mmap = event__process_mmap,
263 .comm = event__process_comm,
264 .exit = event__process_task,
265 .fork = event__process_task,
266 .lost = event__process_lost,
267 .read = process_read_event,
270 static int __cmd_report(void)
273 struct perf_session *session;
274 struct rb_node *next;
275 const char *help = "For a higher level overview, try: perf report --sort comm,dso";
277 session = perf_session__new(input_name, O_RDONLY, force);
282 perf_read_values_init(&show_threads_values);
284 ret = perf_session__setup_sample_type(session);
288 ret = perf_session__process_events(session, &event_ops);
293 event__print_totals();
298 perf_session__fprintf(session, stdout);
301 dsos__fprintf(stdout);
303 next = rb_first(&session->stats_by_id);
305 struct event_stat_id *stats;
307 stats = rb_entry(next, struct event_stat_id, rb_node);
308 perf_session__collapse_resort(&stats->hists);
309 perf_session__output_resort(&stats->hists, stats->stats.total);
312 perf_session__browse_hists(&stats->hists,
313 stats->stats.total, help);
315 if (rb_first(&session->stats_by_id) ==
316 rb_last(&session->stats_by_id))
317 fprintf(stdout, "# Samples: %Ld\n#\n",
320 fprintf(stdout, "# Samples: %Ld %s\n#\n",
322 __event_name(stats->type, stats->config));
324 perf_session__fprintf_hists(&stats->hists, NULL, false, stdout,
326 fprintf(stdout, "\n\n");
329 next = rb_next(&stats->rb_node);
332 if (!use_browser && sort_order == default_sort_order &&
333 parent_pattern == default_parent_pattern) {
334 fprintf(stdout, "#\n# (%s)\n#\n", help);
337 bool style = !strcmp(pretty_printing_style, "raw");
338 perf_read_values_display(stdout, &show_threads_values,
340 perf_read_values_destroy(&show_threads_values);
344 perf_session__delete(session);
349 parse_callchain_opt(const struct option *opt __used, const char *arg,
359 dont_use_callchains = true;
363 symbol_conf.use_callchain = true;
368 tok = strtok((char *)arg, ",");
372 /* get the output mode */
373 if (!strncmp(tok, "graph", strlen(arg)))
374 callchain_param.mode = CHAIN_GRAPH_ABS;
376 else if (!strncmp(tok, "flat", strlen(arg)))
377 callchain_param.mode = CHAIN_FLAT;
379 else if (!strncmp(tok, "fractal", strlen(arg)))
380 callchain_param.mode = CHAIN_GRAPH_REL;
382 else if (!strncmp(tok, "none", strlen(arg))) {
383 callchain_param.mode = CHAIN_NONE;
384 symbol_conf.use_callchain = false;
392 /* get the min percentage */
393 tok = strtok(NULL, ",");
397 callchain_param.min_percent = strtod(tok, &endptr);
402 if (register_callchain_param(&callchain_param) < 0) {
403 fprintf(stderr, "Can't register callchain params\n");
409 static const char * const report_usage[] = {
410 "perf report [<options>] <command>",
414 static const struct option options[] = {
415 OPT_STRING('i', "input", &input_name, "file",
417 OPT_BOOLEAN('v', "verbose", &verbose,
418 "be more verbose (show symbol address, etc)"),
419 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
420 "dump raw trace in ASCII"),
421 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
422 "file", "vmlinux pathname"),
423 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
424 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
425 "load module symbols - WARNING: use only with -k and LIVE kernel"),
426 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
427 "Show a column with the number of samples"),
428 OPT_BOOLEAN('T', "threads", &show_threads,
429 "Show per-thread event counters"),
430 OPT_STRING(0, "pretty", &pretty_printing_style, "key",
431 "pretty printing style key: normal raw"),
432 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
433 "sort by key(s): pid, comm, dso, symbol, parent"),
434 OPT_BOOLEAN('P', "full-paths", &symbol_conf.full_paths,
435 "Don't shorten the pathnames taking into account the cwd"),
436 OPT_STRING('p', "parent", &parent_pattern, "regex",
437 "regex filter to identify parent, see: '--sort parent'"),
438 OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
439 "Only display entries with parent-match"),
440 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
441 "Display callchains using output_type and min percent threshold. "
442 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
443 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
444 "only consider symbols in these dsos"),
445 OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
446 "only consider symbols in these comms"),
447 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
448 "only consider these symbols"),
449 OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
451 "don't try to adjust column width, use these fixed values"),
452 OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
453 "separator for columns, no spaces will be added between "
454 "columns '.' is reserved."),
455 OPT_BOOLEAN('U', "hide-unresolved", &hide_unresolved,
456 "Only display entries resolved to a symbol"),
460 int cmd_report(int argc, const char **argv, const char *prefix __used)
462 argc = parse_options(argc, argv, options, report_usage, 0);
466 if (symbol__init() < 0)
469 setup_sorting(report_usage, options);
471 if (parent_pattern != default_parent_pattern) {
472 sort_dimension__add("parent");
473 sort_parent.elide = 1;
475 symbol_conf.exclude_other = false;
478 * Any (unrecognized) arguments left?
481 usage_with_options(report_usage, options);
483 sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout);
484 sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout);
485 sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout);
487 return __cmd_report();