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/callchain.h"
18 #include "util/strlist.h"
19 #include "util/values.h"
22 #include "util/debug.h"
23 #include "util/header.h"
24 #include "util/session.h"
26 #include "util/parse-options.h"
27 #include "util/parse-events.h"
29 #include "util/thread.h"
30 #include "util/sort.h"
31 #include "util/hist.h"
33 static char const *input_name
= "perf.data";
36 static bool hide_unresolved
;
37 static bool dont_use_callchains
;
39 static bool show_threads
;
40 static struct perf_read_values show_threads_values
;
42 static char default_pretty_printing_style
[] = "normal";
43 static char *pretty_printing_style
= default_pretty_printing_style
;
45 static char callchain_default_opt
[] = "fractal,0.5";
47 static struct event_stat_id
*get_stats(struct perf_session
*self
,
48 u64 event_stream
, u32 type
, u64 config
)
50 struct rb_node
**p
= &self
->stats_by_id
.rb_node
;
51 struct rb_node
*parent
= NULL
;
52 struct event_stat_id
*iter
, *new;
56 iter
= rb_entry(parent
, struct event_stat_id
, rb_node
);
57 if (iter
->config
== config
)
61 if (config
> iter
->config
)
67 new = malloc(sizeof(struct event_stat_id
));
70 memset(new, 0, sizeof(struct event_stat_id
));
71 new->event_stream
= event_stream
;
74 rb_link_node(&new->rb_node
, parent
, p
);
75 rb_insert_color(&new->rb_node
, &self
->stats_by_id
);
79 static int perf_session__add_hist_entry(struct perf_session
*self
,
80 struct addr_location
*al
,
81 struct sample_data
*data
)
83 struct map_symbol
*syms
= NULL
;
84 struct symbol
*parent
= NULL
;
87 struct hist_entry
*he
;
88 struct event_stat_id
*stats
;
89 struct perf_event_attr
*attr
;
91 if ((sort__has_parent
|| symbol_conf
.use_callchain
) && data
->callchain
) {
92 syms
= perf_session__resolve_callchain(self
, al
->thread
,
93 data
->callchain
, &parent
);
98 attr
= perf_header__find_attr(data
->id
, &self
->header
);
100 stats
= get_stats(self
, data
->id
, attr
->type
, attr
->config
);
102 stats
= get_stats(self
, data
->id
, 0, 0);
105 he
= __perf_session__add_hist_entry(&stats
->hists
, al
, parent
,
111 __perf_session__add_count(he
, al
, data
->period
);
113 if (symbol_conf
.use_callchain
) {
115 callchain_init(he
->callchain
);
116 err
= append_chain(he
->callchain
, data
->callchain
, syms
);
126 static int add_event_total(struct perf_session
*session
,
127 struct sample_data
*data
,
128 struct perf_event_attr
*attr
)
130 struct event_stat_id
*stats
;
133 stats
= get_stats(session
, data
->id
, attr
->type
, attr
->config
);
135 stats
= get_stats(session
, data
->id
, 0, 0);
140 stats
->stats
.total
+= data
->period
;
141 session
->events_stats
.total
+= data
->period
;
145 static int process_sample_event(event_t
*event
, struct perf_session
*session
)
147 struct sample_data data
= { .period
= 1, };
148 struct addr_location al
;
149 struct perf_event_attr
*attr
;
151 event__parse_sample(event
, session
->sample_type
, &data
);
153 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event
->header
.misc
,
154 data
.pid
, data
.tid
, data
.ip
, data
.period
);
156 if (session
->sample_type
& PERF_SAMPLE_CALLCHAIN
) {
159 dump_printf("... chain: nr:%Lu\n", data
.callchain
->nr
);
161 if (!ip_callchain__valid(data
.callchain
, event
)) {
162 pr_debug("call-chain problem with event, "
168 for (i
= 0; i
< data
.callchain
->nr
; i
++)
169 dump_printf("..... %2d: %016Lx\n",
170 i
, data
.callchain
->ips
[i
]);
174 if (event__preprocess_sample(event
, session
, &al
, NULL
) < 0) {
175 fprintf(stderr
, "problem processing %d event, skipping it.\n",
180 if (al
.filtered
|| (hide_unresolved
&& al
.sym
== NULL
))
183 if (perf_session__add_hist_entry(session
, &al
, &data
)) {
184 pr_debug("problem incrementing symbol count, skipping event\n");
188 attr
= perf_header__find_attr(data
.id
, &session
->header
);
190 if (add_event_total(session
, &data
, attr
)) {
191 pr_debug("problem adding event count\n");
198 static int process_read_event(event_t
*event
, struct perf_session
*session __used
)
200 struct perf_event_attr
*attr
;
202 attr
= perf_header__find_attr(event
->read
.id
, &session
->header
);
205 const char *name
= attr
? __event_name(attr
->type
, attr
->config
)
207 perf_read_values_add_value(&show_threads_values
,
208 event
->read
.pid
, event
->read
.tid
,
214 dump_printf(": %d %d %s %Lu\n", event
->read
.pid
, event
->read
.tid
,
215 attr
? __event_name(attr
->type
, attr
->config
) : "FAIL",
221 static int perf_session__setup_sample_type(struct perf_session
*self
)
223 if (!(self
->sample_type
& PERF_SAMPLE_CALLCHAIN
)) {
224 if (sort__has_parent
) {
225 fprintf(stderr
, "selected --sort parent, but no"
226 " callchain data. Did you call"
227 " perf record without -g?\n");
230 if (symbol_conf
.use_callchain
) {
231 fprintf(stderr
, "selected -g but no callchain data."
232 " Did you call perf record without"
236 } else if (!dont_use_callchains
&& callchain_param
.mode
!= CHAIN_NONE
&&
237 !symbol_conf
.use_callchain
) {
238 symbol_conf
.use_callchain
= true;
239 if (register_callchain_param(&callchain_param
) < 0) {
240 fprintf(stderr
, "Can't register callchain"
249 static struct perf_event_ops event_ops
= {
250 .sample
= process_sample_event
,
251 .mmap
= event__process_mmap
,
252 .comm
= event__process_comm
,
253 .exit
= event__process_task
,
254 .fork
= event__process_task
,
255 .lost
= event__process_lost
,
256 .read
= process_read_event
,
257 .attr
= event__process_attr
,
258 .event_type
= event__process_event_type
,
259 .tracing_data
= event__process_tracing_data
,
260 .build_id
= event__process_build_id
,
263 extern volatile int session_done
;
265 static void sig_handler(int sig
__attribute__((__unused__
)))
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 signal(SIGINT
, sig_handler
);
279 session
= perf_session__new(input_name
, O_RDONLY
, force
, false);
284 perf_read_values_init(&show_threads_values
);
286 ret
= perf_session__setup_sample_type(session
);
290 ret
= perf_session__process_events(session
, &event_ops
);
295 event__print_totals();
300 perf_session__fprintf(session
, stdout
);
303 perf_session__fprintf_dsos(session
, stdout
);
305 next
= rb_first(&session
->stats_by_id
);
307 struct event_stat_id
*stats
;
310 stats
= rb_entry(next
, struct event_stat_id
, rb_node
);
311 perf_session__collapse_resort(&stats
->hists
);
312 nr_hists
= perf_session__output_resort(&stats
->hists
,
315 perf_session__browse_hists(&stats
->hists
, nr_hists
,
316 stats
->stats
.total
, help
,
319 if (rb_first(&session
->stats_by_id
) ==
320 rb_last(&session
->stats_by_id
))
321 fprintf(stdout
, "# Samples: %Ld\n#\n",
324 fprintf(stdout
, "# Samples: %Ld %s\n#\n",
326 __event_name(stats
->type
, stats
->config
));
328 perf_session__fprintf_hists(&stats
->hists
, NULL
, false, stdout
,
330 fprintf(stdout
, "\n\n");
333 next
= rb_next(&stats
->rb_node
);
336 if (!use_browser
&& sort_order
== default_sort_order
&&
337 parent_pattern
== default_parent_pattern
) {
338 fprintf(stdout
, "#\n# (%s)\n#\n", help
);
341 bool style
= !strcmp(pretty_printing_style
, "raw");
342 perf_read_values_display(stdout
, &show_threads_values
,
344 perf_read_values_destroy(&show_threads_values
);
348 perf_session__delete(session
);
353 parse_callchain_opt(const struct option
*opt __used
, const char *arg
,
363 dont_use_callchains
= true;
367 symbol_conf
.use_callchain
= true;
372 tok
= strtok((char *)arg
, ",");
376 /* get the output mode */
377 if (!strncmp(tok
, "graph", strlen(arg
)))
378 callchain_param
.mode
= CHAIN_GRAPH_ABS
;
380 else if (!strncmp(tok
, "flat", strlen(arg
)))
381 callchain_param
.mode
= CHAIN_FLAT
;
383 else if (!strncmp(tok
, "fractal", strlen(arg
)))
384 callchain_param
.mode
= CHAIN_GRAPH_REL
;
386 else if (!strncmp(tok
, "none", strlen(arg
))) {
387 callchain_param
.mode
= CHAIN_NONE
;
388 symbol_conf
.use_callchain
= false;
396 /* get the min percentage */
397 tok
= strtok(NULL
, ",");
401 callchain_param
.min_percent
= strtod(tok
, &endptr
);
406 if (register_callchain_param(&callchain_param
) < 0) {
407 fprintf(stderr
, "Can't register callchain params\n");
413 static const char * const report_usage
[] = {
414 "perf report [<options>] <command>",
418 static const struct option options
[] = {
419 OPT_STRING('i', "input", &input_name
, "file",
421 OPT_INCR('v', "verbose", &verbose
,
422 "be more verbose (show symbol address, etc)"),
423 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
424 "dump raw trace in ASCII"),
425 OPT_STRING('k', "vmlinux", &symbol_conf
.vmlinux_name
,
426 "file", "vmlinux pathname"),
427 OPT_BOOLEAN('f', "force", &force
, "don't complain, do it"),
428 OPT_BOOLEAN('m', "modules", &symbol_conf
.use_modules
,
429 "load module symbols - WARNING: use only with -k and LIVE kernel"),
430 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf
.show_nr_samples
,
431 "Show a column with the number of samples"),
432 OPT_BOOLEAN('T', "threads", &show_threads
,
433 "Show per-thread event counters"),
434 OPT_STRING(0, "pretty", &pretty_printing_style
, "key",
435 "pretty printing style key: normal raw"),
436 OPT_STRING('s', "sort", &sort_order
, "key[,key2...]",
437 "sort by key(s): pid, comm, dso, symbol, parent"),
438 OPT_BOOLEAN('P', "full-paths", &symbol_conf
.full_paths
,
439 "Don't shorten the pathnames taking into account the cwd"),
440 OPT_BOOLEAN(0, "showcpuutilization", &symbol_conf
.show_cpu_utilization
,
441 "Show sample percentage for different cpu modes"),
442 OPT_STRING('p', "parent", &parent_pattern
, "regex",
443 "regex filter to identify parent, see: '--sort parent'"),
444 OPT_BOOLEAN('x', "exclude-other", &symbol_conf
.exclude_other
,
445 "Only display entries with parent-match"),
446 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL
, "output_type,min_percent",
447 "Display callchains using output_type (graph, flat, fractal, or none) and min percent threshold. "
448 "Default: fractal,0.5", &parse_callchain_opt
, callchain_default_opt
),
449 OPT_STRING('d', "dsos", &symbol_conf
.dso_list_str
, "dso[,dso...]",
450 "only consider symbols in these dsos"),
451 OPT_STRING('C', "comms", &symbol_conf
.comm_list_str
, "comm[,comm...]",
452 "only consider symbols in these comms"),
453 OPT_STRING('S', "symbols", &symbol_conf
.sym_list_str
, "symbol[,symbol...]",
454 "only consider these symbols"),
455 OPT_STRING('w', "column-widths", &symbol_conf
.col_width_list_str
,
457 "don't try to adjust column width, use these fixed values"),
458 OPT_STRING('t', "field-separator", &symbol_conf
.field_sep
, "separator",
459 "separator for columns, no spaces will be added between "
460 "columns '.' is reserved."),
461 OPT_BOOLEAN('U', "hide-unresolved", &hide_unresolved
,
462 "Only display entries resolved to a symbol"),
466 int cmd_report(int argc
, const char **argv
, const char *prefix __used
)
468 argc
= parse_options(argc
, argv
, options
, report_usage
, 0);
470 if (strcmp(input_name
, "-") != 0)
473 if (symbol__init() < 0)
476 setup_sorting(report_usage
, options
);
478 if (parent_pattern
!= default_parent_pattern
) {
479 if (sort_dimension__add("parent") < 0)
481 sort_parent
.elide
= 1;
483 symbol_conf
.exclude_other
= false;
486 * Any (unrecognized) arguments left?
489 usage_with_options(report_usage
, options
);
491 sort_entry__setup_elide(&sort_dso
, symbol_conf
.dso_list
, "dso", stdout
);
492 sort_entry__setup_elide(&sort_comm
, symbol_conf
.comm_list
, "comm", stdout
);
493 sort_entry__setup_elide(&sort_sym
, symbol_conf
.sym_list
, "symbol", stdout
);
495 return __cmd_report();