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 int perf_session__add_hist_entry(struct perf_session
*self
,
49 struct addr_location
*al
,
50 struct ip_callchain
*chain
, u64 count
)
52 struct symbol
**syms
= NULL
, *parent
= NULL
;
54 struct hist_entry
*he
;
56 if ((sort__has_parent
|| symbol_conf
.use_callchain
) && chain
)
57 syms
= perf_session__resolve_callchain(self
, al
->thread
,
59 he
= __perf_session__add_hist_entry(self
, al
, parent
, count
, &hit
);
66 if (symbol_conf
.use_callchain
) {
68 callchain_init(&he
->callchain
);
69 append_chain(&he
->callchain
, chain
, syms
);
76 static int validate_chain(struct ip_callchain
*chain
, event_t
*event
)
78 unsigned int chain_size
;
80 chain_size
= event
->header
.size
;
81 chain_size
-= (unsigned long)&event
->ip
.__more_data
- (unsigned long)event
;
83 if (chain
->nr
*sizeof(u64
) > chain_size
)
89 static int process_sample_event(event_t
*event
, struct perf_session
*session
)
91 struct sample_data data
= { .period
= 1, };
92 struct addr_location al
;
94 event__parse_sample(event
, session
->sample_type
, &data
);
96 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event
->header
.misc
,
97 data
.pid
, data
.tid
, data
.ip
, data
.period
);
99 if (session
->sample_type
& PERF_SAMPLE_CALLCHAIN
) {
102 dump_printf("... chain: nr:%Lu\n", data
.callchain
->nr
);
104 if (validate_chain(data
.callchain
, event
) < 0) {
105 pr_debug("call-chain problem with event, "
111 for (i
= 0; i
< data
.callchain
->nr
; i
++)
112 dump_printf("..... %2d: %016Lx\n",
113 i
, data
.callchain
->ips
[i
]);
117 if (event__preprocess_sample(event
, session
, &al
, NULL
) < 0) {
118 fprintf(stderr
, "problem processing %d event, skipping it.\n",
123 if (al
.filtered
|| (hide_unresolved
&& al
.sym
== NULL
))
126 if (perf_session__add_hist_entry(session
, &al
, data
.callchain
, data
.period
)) {
127 pr_debug("problem incrementing symbol count, skipping event\n");
131 session
->events_stats
.total
+= data
.period
;
135 static int process_read_event(event_t
*event
, struct perf_session
*session __used
)
137 struct perf_event_attr
*attr
;
139 attr
= perf_header__find_attr(event
->read
.id
, &session
->header
);
142 const char *name
= attr
? __event_name(attr
->type
, attr
->config
)
144 perf_read_values_add_value(&show_threads_values
,
145 event
->read
.pid
, event
->read
.tid
,
151 dump_printf(": %d %d %s %Lu\n", event
->read
.pid
, event
->read
.tid
,
152 attr
? __event_name(attr
->type
, attr
->config
) : "FAIL",
158 static int perf_session__setup_sample_type(struct perf_session
*self
)
160 if (!(self
->sample_type
& PERF_SAMPLE_CALLCHAIN
)) {
161 if (sort__has_parent
) {
162 fprintf(stderr
, "selected --sort parent, but no"
163 " callchain data. Did you call"
164 " perf record without -g?\n");
167 if (symbol_conf
.use_callchain
) {
168 fprintf(stderr
, "selected -g but no callchain data."
169 " Did you call perf record without"
173 } else if (!dont_use_callchains
&& callchain_param
.mode
!= CHAIN_NONE
&&
174 !symbol_conf
.use_callchain
) {
175 symbol_conf
.use_callchain
= true;
176 if (register_callchain_param(&callchain_param
) < 0) {
177 fprintf(stderr
, "Can't register callchain"
186 static struct perf_event_ops event_ops
= {
187 .sample
= process_sample_event
,
188 .mmap
= event__process_mmap
,
189 .comm
= event__process_comm
,
190 .exit
= event__process_task
,
191 .fork
= event__process_task
,
192 .lost
= event__process_lost
,
193 .read
= process_read_event
,
196 static int __cmd_report(void)
199 struct perf_session
*session
;
201 session
= perf_session__new(input_name
, O_RDONLY
, force
);
206 perf_read_values_init(&show_threads_values
);
208 ret
= perf_session__setup_sample_type(session
);
212 ret
= perf_session__process_events(session
, &event_ops
);
217 event__print_totals();
222 perf_session__fprintf(session
, stdout
);
225 dsos__fprintf(stdout
);
227 perf_session__collapse_resort(session
);
228 perf_session__output_resort(session
, session
->events_stats
.total
);
229 fprintf(stdout
, "# Samples: %Ld\n#\n", session
->events_stats
.total
);
230 perf_session__fprintf_hists(session
, NULL
, false, stdout
);
231 if (sort_order
== default_sort_order
&&
232 parent_pattern
== default_parent_pattern
)
233 fprintf(stdout
, "#\n# (For a higher level overview, try: perf report --sort comm,dso)\n#\n");
236 bool raw_printing_style
= !strcmp(pretty_printing_style
, "raw");
237 perf_read_values_display(stdout
, &show_threads_values
,
239 perf_read_values_destroy(&show_threads_values
);
242 perf_session__delete(session
);
247 parse_callchain_opt(const struct option
*opt __used
, const char *arg
,
257 dont_use_callchains
= true;
261 symbol_conf
.use_callchain
= true;
266 tok
= strtok((char *)arg
, ",");
270 /* get the output mode */
271 if (!strncmp(tok
, "graph", strlen(arg
)))
272 callchain_param
.mode
= CHAIN_GRAPH_ABS
;
274 else if (!strncmp(tok
, "flat", strlen(arg
)))
275 callchain_param
.mode
= CHAIN_FLAT
;
277 else if (!strncmp(tok
, "fractal", strlen(arg
)))
278 callchain_param
.mode
= CHAIN_GRAPH_REL
;
280 else if (!strncmp(tok
, "none", strlen(arg
))) {
281 callchain_param
.mode
= CHAIN_NONE
;
282 symbol_conf
.use_callchain
= false;
290 /* get the min percentage */
291 tok
= strtok(NULL
, ",");
295 callchain_param
.min_percent
= strtod(tok
, &endptr
);
300 if (register_callchain_param(&callchain_param
) < 0) {
301 fprintf(stderr
, "Can't register callchain params\n");
307 static const char * const report_usage
[] = {
308 "perf report [<options>] <command>",
312 static const struct option options
[] = {
313 OPT_STRING('i', "input", &input_name
, "file",
315 OPT_BOOLEAN('v', "verbose", &verbose
,
316 "be more verbose (show symbol address, etc)"),
317 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace
,
318 "dump raw trace in ASCII"),
319 OPT_STRING('k', "vmlinux", &symbol_conf
.vmlinux_name
,
320 "file", "vmlinux pathname"),
321 OPT_BOOLEAN('f', "force", &force
, "don't complain, do it"),
322 OPT_BOOLEAN('m', "modules", &symbol_conf
.use_modules
,
323 "load module symbols - WARNING: use only with -k and LIVE kernel"),
324 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf
.show_nr_samples
,
325 "Show a column with the number of samples"),
326 OPT_BOOLEAN('T', "threads", &show_threads
,
327 "Show per-thread event counters"),
328 OPT_STRING(0, "pretty", &pretty_printing_style
, "key",
329 "pretty printing style key: normal raw"),
330 OPT_STRING('s', "sort", &sort_order
, "key[,key2...]",
331 "sort by key(s): pid, comm, dso, symbol, parent"),
332 OPT_BOOLEAN('P', "full-paths", &symbol_conf
.full_paths
,
333 "Don't shorten the pathnames taking into account the cwd"),
334 OPT_STRING('p', "parent", &parent_pattern
, "regex",
335 "regex filter to identify parent, see: '--sort parent'"),
336 OPT_BOOLEAN('x', "exclude-other", &symbol_conf
.exclude_other
,
337 "Only display entries with parent-match"),
338 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL
, "output_type,min_percent",
339 "Display callchains using output_type and min percent threshold. "
340 "Default: fractal,0.5", &parse_callchain_opt
, callchain_default_opt
),
341 OPT_STRING('d', "dsos", &symbol_conf
.dso_list_str
, "dso[,dso...]",
342 "only consider symbols in these dsos"),
343 OPT_STRING('C', "comms", &symbol_conf
.comm_list_str
, "comm[,comm...]",
344 "only consider symbols in these comms"),
345 OPT_STRING('S', "symbols", &symbol_conf
.sym_list_str
, "symbol[,symbol...]",
346 "only consider these symbols"),
347 OPT_STRING('w', "column-widths", &symbol_conf
.col_width_list_str
,
349 "don't try to adjust column width, use these fixed values"),
350 OPT_STRING('t', "field-separator", &symbol_conf
.field_sep
, "separator",
351 "separator for columns, no spaces will be added between "
352 "columns '.' is reserved."),
353 OPT_BOOLEAN('U', "hide-unresolved", &hide_unresolved
,
354 "Only display entries resolved to a symbol"),
358 int cmd_report(int argc
, const char **argv
, const char *prefix __used
)
360 argc
= parse_options(argc
, argv
, options
, report_usage
, 0);
364 if (symbol__init() < 0)
367 setup_sorting(report_usage
, options
);
369 if (parent_pattern
!= default_parent_pattern
) {
370 sort_dimension__add("parent");
371 sort_parent
.elide
= 1;
373 symbol_conf
.exclude_other
= false;
376 * Any (unrecognized) arguments left?
379 usage_with_options(report_usage
, options
);
381 sort_entry__setup_elide(&sort_dso
, symbol_conf
.dso_list
, "dso", stdout
);
382 sort_entry__setup_elide(&sort_comm
, symbol_conf
.comm_list
, "comm", stdout
);
383 sort_entry__setup_elide(&sort_sym
, symbol_conf
.sym_list
, "symbol", stdout
);
385 return __cmd_report();