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/annotate.h"
13 #include "util/color.h"
14 #include <linux/list.h>
15 #include "util/cache.h"
16 #include <linux/rbtree.h>
17 #include "util/symbol.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";
36 static bool force
, use_tui
, use_stdio
;
37 static bool hide_unresolved
;
38 static bool dont_use_callchains
;
40 static bool show_threads
;
41 static struct perf_read_values show_threads_values
;
43 static const char default_pretty_printing_style
[] = "normal";
44 static const char *pretty_printing_style
= default_pretty_printing_style
;
46 static char callchain_default_opt
[] = "fractal,0.5";
48 static struct hists
*perf_session__hists_findnew(struct perf_session
*self
,
49 u64 event_stream
, u32 type
,
52 struct rb_node
**p
= &self
->hists_tree
.rb_node
;
53 struct rb_node
*parent
= NULL
;
54 struct hists
*iter
, *new;
58 iter
= rb_entry(parent
, struct hists
, rb_node
);
59 if (iter
->config
== config
)
63 if (config
> iter
->config
)
69 new = malloc(sizeof(struct hists
));
72 memset(new, 0, sizeof(struct hists
));
73 new->event_stream
= event_stream
;
76 rb_link_node(&new->rb_node
, parent
, p
);
77 rb_insert_color(&new->rb_node
, &self
->hists_tree
);
81 static int perf_session__add_hist_entry(struct perf_session
*session
,
82 struct addr_location
*al
,
83 struct perf_sample
*sample
)
85 struct symbol
*parent
= NULL
;
87 struct hist_entry
*he
;
89 struct perf_event_attr
*attr
;
91 if ((sort__has_parent
|| symbol_conf
.use_callchain
) && sample
->callchain
) {
92 err
= perf_session__resolve_callchain(session
, al
->thread
,
93 sample
->callchain
, &parent
);
98 attr
= perf_header__find_attr(sample
->id
, &session
->header
);
100 hists
= perf_session__hists_findnew(session
, sample
->id
, attr
->type
, attr
->config
);
102 hists
= perf_session__hists_findnew(session
, sample
->id
, 0, 0);
106 he
= __hists__add_entry(hists
, al
, parent
, sample
->period
);
110 if (symbol_conf
.use_callchain
) {
111 err
= callchain_append(he
->callchain
, &session
->callchain_cursor
,
117 * Only in the newt browser we are doing integrated annotation,
118 * so we don't allocated the extra space needed because the stdio
119 * code will not use it.
122 err
= hist_entry__inc_addr_samples(he
, al
->addr
);
127 static int add_event_total(struct perf_session
*session
,
128 struct perf_sample
*sample
,
129 struct perf_event_attr
*attr
)
134 hists
= perf_session__hists_findnew(session
, sample
->id
,
135 attr
->type
, attr
->config
);
137 hists
= perf_session__hists_findnew(session
, sample
->id
, 0, 0);
142 hists
->stats
.total_period
+= sample
->period
;
144 * FIXME: add_event_total should be moved from here to
145 * perf_session__process_event so that the proper hist is passed to
146 * the event_op methods.
148 hists__inc_nr_events(hists
, PERF_RECORD_SAMPLE
);
149 session
->hists
.stats
.total_period
+= sample
->period
;
153 static int process_sample_event(union perf_event
*event
,
154 struct perf_sample
*sample
,
155 struct perf_session
*session
)
157 struct addr_location al
;
158 struct perf_event_attr
*attr
;
160 if (perf_event__preprocess_sample(event
, session
, &al
, sample
, NULL
) < 0) {
161 fprintf(stderr
, "problem processing %d event, skipping it.\n",
166 if (al
.filtered
|| (hide_unresolved
&& al
.sym
== NULL
))
169 if (perf_session__add_hist_entry(session
, &al
, sample
)) {
170 pr_debug("problem incrementing symbol period, skipping event\n");
174 attr
= perf_header__find_attr(sample
->id
, &session
->header
);
176 if (add_event_total(session
, sample
, attr
)) {
177 pr_debug("problem adding event period\n");
184 static int process_read_event(union perf_event
*event
,
185 struct perf_sample
*sample __used
,
186 struct perf_session
*session __used
)
188 struct perf_event_attr
*attr
;
190 attr
= perf_header__find_attr(event
->read
.id
, &session
->header
);
193 const char *name
= attr
? __event_name(attr
->type
, attr
->config
)
195 perf_read_values_add_value(&show_threads_values
,
196 event
->read
.pid
, event
->read
.tid
,
202 dump_printf(": %d %d %s %" PRIu64
"\n", event
->read
.pid
, event
->read
.tid
,
203 attr
? __event_name(attr
->type
, attr
->config
) : "FAIL",
209 static int perf_session__setup_sample_type(struct perf_session
*self
)
211 if (!(self
->sample_type
& PERF_SAMPLE_CALLCHAIN
)) {
212 if (sort__has_parent
) {
213 fprintf(stderr
, "selected --sort parent, but no"
214 " callchain data. Did you call"
215 " perf record without -g?\n");
218 if (symbol_conf
.use_callchain
) {
219 fprintf(stderr
, "selected -g but no callchain data."
220 " Did you call perf record without"
224 } else if (!dont_use_callchains
&& callchain_param
.mode
!= CHAIN_NONE
&&
225 !symbol_conf
.use_callchain
) {
226 symbol_conf
.use_callchain
= true;
227 if (callchain_register_param(&callchain_param
) < 0) {
228 fprintf(stderr
, "Can't register callchain"
237 static struct perf_event_ops event_ops
= {
238 .sample
= process_sample_event
,
239 .mmap
= perf_event__process_mmap
,
240 .comm
= perf_event__process_comm
,
241 .exit
= perf_event__process_task
,
242 .fork
= perf_event__process_task
,
243 .lost
= perf_event__process_lost
,
244 .read
= process_read_event
,
245 .attr
= perf_event__process_attr
,
246 .event_type
= perf_event__process_event_type
,
247 .tracing_data
= perf_event__process_tracing_data
,
248 .build_id
= perf_event__process_build_id
,
249 .ordered_samples
= true,
250 .ordering_requires_timestamps
= true,
253 extern volatile int session_done
;
255 static void sig_handler(int sig __used
)
260 static size_t hists__fprintf_nr_sample_events(struct hists
*self
,
261 const char *evname
, FILE *fp
)
265 unsigned long nr_events
= self
->stats
.nr_events
[PERF_RECORD_SAMPLE
];
267 nr_events
= convert_unit(nr_events
, &unit
);
268 ret
= fprintf(fp
, "# Events: %lu%c", nr_events
, unit
);
270 ret
+= fprintf(fp
, " %s", evname
);
271 return ret
+ fprintf(fp
, "\n#\n");
274 static int hists__tty_browse_tree(struct rb_root
*tree
, const char *help
)
276 struct rb_node
*next
= rb_first(tree
);
279 struct hists
*hists
= rb_entry(next
, struct hists
, rb_node
);
280 const char *evname
= NULL
;
282 if (rb_first(&hists
->entries
) != rb_last(&hists
->entries
))
283 evname
= __event_name(hists
->type
, hists
->config
);
285 hists__fprintf_nr_sample_events(hists
, evname
, stdout
);
286 hists__fprintf(hists
, NULL
, false, stdout
);
287 fprintf(stdout
, "\n\n");
288 next
= rb_next(&hists
->rb_node
);
291 if (sort_order
== default_sort_order
&&
292 parent_pattern
== default_parent_pattern
) {
293 fprintf(stdout
, "#\n# (%s)\n#\n", help
);
296 bool style
= !strcmp(pretty_printing_style
, "raw");
297 perf_read_values_display(stdout
, &show_threads_values
,
299 perf_read_values_destroy(&show_threads_values
);
306 static int __cmd_report(void)
309 struct perf_session
*session
;
310 struct rb_node
*next
;
311 const char *help
= "For a higher level overview, try: perf report --sort comm,dso";
313 signal(SIGINT
, sig_handler
);
315 session
= perf_session__new(input_name
, O_RDONLY
, force
, false, &event_ops
);
320 perf_read_values_init(&show_threads_values
);
322 ret
= perf_session__setup_sample_type(session
);
326 ret
= perf_session__process_events(session
, &event_ops
);
331 perf_session__fprintf_nr_events(session
, stdout
);
336 perf_session__fprintf(session
, stdout
);
339 perf_session__fprintf_dsos(session
, stdout
);
341 next
= rb_first(&session
->hists_tree
);
345 hists
= rb_entry(next
, struct hists
, rb_node
);
346 hists__collapse_resort(hists
);
347 hists__output_resort(hists
);
348 next
= rb_next(&hists
->rb_node
);
352 hists__tui_browse_tree(&session
->hists_tree
, help
);
354 hists__tty_browse_tree(&session
->hists_tree
, help
);
358 * Speed up the exit process, for large files this can
359 * take quite a while.
361 * XXX Enable this when using valgrind or if we ever
362 * librarize this command.
364 * Also experiment with obstacks to see how much speed
367 * perf_session__delete(session);
373 parse_callchain_opt(const struct option
*opt __used
, const char *arg
,
383 dont_use_callchains
= true;
387 symbol_conf
.use_callchain
= true;
392 tok
= strtok((char *)arg
, ",");
396 /* get the output mode */
397 if (!strncmp(tok
, "graph", strlen(arg
)))
398 callchain_param
.mode
= CHAIN_GRAPH_ABS
;
400 else if (!strncmp(tok
, "flat", strlen(arg
)))
401 callchain_param
.mode
= CHAIN_FLAT
;
403 else if (!strncmp(tok
, "fractal", strlen(arg
)))
404 callchain_param
.mode
= CHAIN_GRAPH_REL
;
406 else if (!strncmp(tok
, "none", strlen(arg
))) {
407 callchain_param
.mode
= CHAIN_NONE
;
408 symbol_conf
.use_callchain
= false;
416 /* get the min percentage */
417 tok
= strtok(NULL
, ",");
421 tok2
= strtok(NULL
, ",");
422 callchain_param
.min_percent
= strtod(tok
, &endptr
);
427 callchain_param
.print_limit
= strtod(tok2
, &endptr
);
429 if (callchain_register_param(&callchain_param
) < 0) {
430 fprintf(stderr
, "Can't register callchain params\n");
436 static const char * const report_usage
[] = {
437 "perf report [<options>] <command>",
441 static const struct option options
[] = {
442 OPT_STRING('i', "input", &input_name
, "file",
444 OPT_INCR('v', "verbose", &verbose
,
445 "be more verbose (show symbol address, etc)"),
446 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
447 "dump raw trace in ASCII"),
448 OPT_STRING('k', "vmlinux", &symbol_conf
.vmlinux_name
,
449 "file", "vmlinux pathname"),
450 OPT_STRING(0, "kallsyms", &symbol_conf
.kallsyms_name
,
451 "file", "kallsyms pathname"),
452 OPT_BOOLEAN('f', "force", &force
, "don't complain, do it"),
453 OPT_BOOLEAN('m', "modules", &symbol_conf
.use_modules
,
454 "load module symbols - WARNING: use only with -k and LIVE kernel"),
455 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf
.show_nr_samples
,
456 "Show a column with the number of samples"),
457 OPT_BOOLEAN('T', "threads", &show_threads
,
458 "Show per-thread event counters"),
459 OPT_STRING(0, "pretty", &pretty_printing_style
, "key",
460 "pretty printing style key: normal raw"),
461 OPT_BOOLEAN(0, "tui", &use_tui
, "Use the TUI interface"),
462 OPT_BOOLEAN(0, "stdio", &use_stdio
, "Use the stdio interface"),
463 OPT_STRING('s', "sort", &sort_order
, "key[,key2...]",
464 "sort by key(s): pid, comm, dso, symbol, parent"),
465 OPT_BOOLEAN(0, "showcpuutilization", &symbol_conf
.show_cpu_utilization
,
466 "Show sample percentage for different cpu modes"),
467 OPT_STRING('p', "parent", &parent_pattern
, "regex",
468 "regex filter to identify parent, see: '--sort parent'"),
469 OPT_BOOLEAN('x', "exclude-other", &symbol_conf
.exclude_other
,
470 "Only display entries with parent-match"),
471 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL
, "output_type,min_percent",
472 "Display callchains using output_type (graph, flat, fractal, or none) and min percent threshold. "
473 "Default: fractal,0.5", &parse_callchain_opt
, callchain_default_opt
),
474 OPT_STRING('d', "dsos", &symbol_conf
.dso_list_str
, "dso[,dso...]",
475 "only consider symbols in these dsos"),
476 OPT_STRING('C', "comms", &symbol_conf
.comm_list_str
, "comm[,comm...]",
477 "only consider symbols in these comms"),
478 OPT_STRING('S', "symbols", &symbol_conf
.sym_list_str
, "symbol[,symbol...]",
479 "only consider these symbols"),
480 OPT_STRING('w', "column-widths", &symbol_conf
.col_width_list_str
,
482 "don't try to adjust column width, use these fixed values"),
483 OPT_STRING('t', "field-separator", &symbol_conf
.field_sep
, "separator",
484 "separator for columns, no spaces will be added between "
485 "columns '.' is reserved."),
486 OPT_BOOLEAN('U', "hide-unresolved", &hide_unresolved
,
487 "Only display entries resolved to a symbol"),
488 OPT_STRING(0, "symfs", &symbol_conf
.symfs
, "directory",
489 "Look for files with symbols relative to this directory"),
493 int cmd_report(int argc
, const char **argv
, const char *prefix __used
)
495 argc
= parse_options(argc
, argv
, options
, report_usage
, 0);
502 if (strcmp(input_name
, "-") != 0)
507 * Only in the newt browser we are doing integrated annotation,
508 * so don't allocate extra space that won't be used in the stdio
511 if (use_browser
> 0) {
512 symbol_conf
.priv_size
= sizeof(struct annotation
);
514 * For searching by name on the "Browse map details".
515 * providing it only in verbose mode not to bloat too
516 * much struct symbol.
520 * XXX: Need to provide a less kludgy way to ask for
521 * more space per symbol, the u32 is for the index on
523 * See symbol__browser_index.
525 symbol_conf
.priv_size
+= sizeof(u32
);
526 symbol_conf
.sort_by_name
= true;
530 if (symbol__init() < 0)
533 setup_sorting(report_usage
, options
);
535 if (parent_pattern
!= default_parent_pattern
) {
536 if (sort_dimension__add("parent") < 0)
538 sort_parent
.elide
= 1;
540 symbol_conf
.exclude_other
= false;
543 * Any (unrecognized) arguments left?
546 usage_with_options(report_usage
, options
);
548 sort_entry__setup_elide(&sort_dso
, symbol_conf
.dso_list
, "dso", stdout
);
549 sort_entry__setup_elide(&sort_comm
, symbol_conf
.comm_list
, "comm", stdout
);
550 sort_entry__setup_elide(&sort_sym
, symbol_conf
.sym_list
, "symbol", stdout
);
552 return __cmd_report();