Commit | Line | Data |
---|---|---|
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 | 34 | static char const *input_name = "perf.data"; |
bd74137e | 35 | |
fa6963b2 | 36 | static int force; |
71289be7 | 37 | static bool hide_unresolved; |
b9a63b9b | 38 | static bool dont_use_callchains; |
97b07b69 | 39 | |
8d513270 BG |
40 | static int show_threads; |
41 | static struct perf_read_values show_threads_values; | |
42 | ||
9f866697 BG |
43 | static char default_pretty_printing_style[] = "normal"; |
44 | static char *pretty_printing_style = default_pretty_printing_style; | |
45 | ||
805d127d FW |
46 | static char callchain_default_opt[] = "fractal,0.5"; |
47 | ||
4e4f06e4 ACM |
48 | static int perf_session__add_hist_entry(struct perf_session *self, |
49 | struct addr_location *al, | |
50 | struct ip_callchain *chain, u64 count) | |
8fa66bdc | 51 | { |
9735abf1 ACM |
52 | struct symbol **syms = NULL, *parent = NULL; |
53 | bool hit; | |
e7fb08b1 | 54 | struct hist_entry *he; |
e7fb08b1 | 55 | |
d599db3f | 56 | if ((sort__has_parent || symbol_conf.use_callchain) && chain) |
a328626b ACM |
57 | syms = perf_session__resolve_callchain(self, al->thread, |
58 | chain, &parent); | |
4e4f06e4 | 59 | he = __perf_session__add_hist_entry(self, al, parent, count, &hit); |
9735abf1 ACM |
60 | if (he == NULL) |
61 | return -ENOMEM; | |
e7fb08b1 | 62 | |
9735abf1 ACM |
63 | if (hit) |
64 | he->count += count; | |
e7fb08b1 | 65 | |
d599db3f | 66 | if (symbol_conf.use_callchain) { |
9735abf1 ACM |
67 | if (!hit) |
68 | callchain_init(&he->callchain); | |
4424961a FW |
69 | append_chain(&he->callchain, chain, syms); |
70 | free(syms); | |
f55c5552 | 71 | } |
e7fb08b1 PZ |
72 | |
73 | return 0; | |
8fa66bdc ACM |
74 | } |
75 | ||
2a0a50fe | 76 | static int validate_chain(struct ip_callchain *chain, event_t *event) |
7522060c IM |
77 | { |
78 | unsigned int chain_size; | |
79 | ||
7522060c IM |
80 | chain_size = event->header.size; |
81 | chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event; | |
82 | ||
9cffa8d5 | 83 | if (chain->nr*sizeof(u64) > chain_size) |
7522060c IM |
84 | return -1; |
85 | ||
86 | return 0; | |
87 | } | |
88 | ||
b3165f41 | 89 | static int process_sample_event(event_t *event, struct perf_session *session) |
75051724 | 90 | { |
c410a338 | 91 | struct sample_data data = { .period = 1, }; |
1ed091c4 | 92 | struct addr_location al; |
180f95e2 | 93 | |
c019879b | 94 | event__parse_sample(event, session->sample_type, &data); |
ea1900e5 | 95 | |
0d755034 ACM |
96 | dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc, |
97 | data.pid, data.tid, data.ip, data.period); | |
75051724 | 98 | |
c019879b | 99 | if (session->sample_type & PERF_SAMPLE_CALLCHAIN) { |
f37a291c | 100 | unsigned int i; |
3efa1cc9 | 101 | |
180f95e2 | 102 | dump_printf("... chain: nr:%Lu\n", data.callchain->nr); |
3efa1cc9 | 103 | |
180f95e2 | 104 | if (validate_chain(data.callchain, event) < 0) { |
6beba7ad ACM |
105 | pr_debug("call-chain problem with event, " |
106 | "skipping it.\n"); | |
7522060c IM |
107 | return 0; |
108 | } | |
109 | ||
110 | if (dump_trace) { | |
180f95e2 OH |
111 | for (i = 0; i < data.callchain->nr; i++) |
112 | dump_printf("..... %2d: %016Lx\n", | |
113 | i, data.callchain->ips[i]); | |
3efa1cc9 IM |
114 | } |
115 | } | |
116 | ||
c410a338 ACM |
117 | if (event__preprocess_sample(event, session, &al, NULL) < 0) { |
118 | fprintf(stderr, "problem processing %d event, skipping it.\n", | |
75051724 IM |
119 | event->header.type); |
120 | return -1; | |
121 | } | |
e7fb08b1 | 122 | |
71289be7 | 123 | if (al.filtered || (hide_unresolved && al.sym == NULL)) |
ec218fc4 | 124 | return 0; |
7bec7a91 | 125 | |
4e4f06e4 | 126 | if (perf_session__add_hist_entry(session, &al, data.callchain, data.period)) { |
6beba7ad | 127 | pr_debug("problem incrementing symbol count, skipping event\n"); |
ec218fc4 | 128 | return -1; |
8fa66bdc | 129 | } |
ec218fc4 | 130 | |
f823e441 | 131 | session->events_stats.total += data.period; |
75051724 IM |
132 | return 0; |
133 | } | |
3502973d | 134 | |
d8f66248 | 135 | static int process_read_event(event_t *event, struct perf_session *session __used) |
e9ea2fde | 136 | { |
cdd6c482 | 137 | struct perf_event_attr *attr; |
0d3a5c88 | 138 | |
94c744b6 | 139 | attr = perf_header__find_attr(event->read.id, &session->header); |
8f18aec5 | 140 | |
8d513270 | 141 | if (show_threads) { |
83a0944f | 142 | const char *name = attr ? __event_name(attr->type, attr->config) |
8d513270 BG |
143 | : "unknown"; |
144 | perf_read_values_add_value(&show_threads_values, | |
145 | event->read.pid, event->read.tid, | |
146 | event->read.id, | |
147 | name, | |
148 | event->read.value); | |
149 | } | |
150 | ||
62daacb5 ACM |
151 | dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid, |
152 | attr ? __event_name(attr->type, attr->config) : "FAIL", | |
153 | event->read.value); | |
e9ea2fde PZ |
154 | |
155 | return 0; | |
156 | } | |
157 | ||
d549c769 | 158 | static int perf_session__setup_sample_type(struct perf_session *self) |
d80d338d | 159 | { |
d549c769 | 160 | if (!(self->sample_type & PERF_SAMPLE_CALLCHAIN)) { |
91b4eaea FW |
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"); | |
d549c769 | 165 | return -EINVAL; |
91b4eaea | 166 | } |
d599db3f | 167 | if (symbol_conf.use_callchain) { |
6ede59c4 | 168 | fprintf(stderr, "selected -g but no callchain data." |
91b4eaea FW |
169 | " Did you call perf record without" |
170 | " -g?\n"); | |
016e92fb | 171 | return -1; |
91b4eaea | 172 | } |
b9a63b9b ACM |
173 | } else if (!dont_use_callchains && callchain_param.mode != CHAIN_NONE && |
174 | !symbol_conf.use_callchain) { | |
d599db3f | 175 | symbol_conf.use_callchain = true; |
b1a88349 FW |
176 | if (register_callchain_param(&callchain_param) < 0) { |
177 | fprintf(stderr, "Can't register callchain" | |
178 | " params\n"); | |
d549c769 | 179 | return -EINVAL; |
b1a88349 | 180 | } |
f5970550 PZ |
181 | } |
182 | ||
016e92fb FW |
183 | return 0; |
184 | } | |
6142f9ec | 185 | |
301a0b02 | 186 | static struct perf_event_ops event_ops = { |
55aa640f ACM |
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, | |
016e92fb | 194 | }; |
6142f9ec | 195 | |
016e92fb FW |
196 | static int __cmd_report(void) |
197 | { | |
d549c769 | 198 | int ret = -EINVAL; |
d8f66248 | 199 | struct perf_session *session; |
8fa66bdc | 200 | |
75be6cf4 | 201 | session = perf_session__new(input_name, O_RDONLY, force); |
94c744b6 ACM |
202 | if (session == NULL) |
203 | return -ENOMEM; | |
204 | ||
016e92fb FW |
205 | if (show_threads) |
206 | perf_read_values_init(&show_threads_values); | |
f5970550 | 207 | |
d549c769 ACM |
208 | ret = perf_session__setup_sample_type(session); |
209 | if (ret) | |
210 | goto out_delete; | |
211 | ||
ec913369 | 212 | ret = perf_session__process_events(session, &event_ops); |
016e92fb | 213 | if (ret) |
94c744b6 | 214 | goto out_delete; |
97b07b69 | 215 | |
62daacb5 ACM |
216 | if (dump_trace) { |
217 | event__print_totals(); | |
94c744b6 | 218 | goto out_delete; |
62daacb5 | 219 | } |
97b07b69 | 220 | |
da21d1b5 | 221 | if (verbose > 3) |
b3165f41 | 222 | perf_session__fprintf(session, stdout); |
9ac99545 | 223 | |
da21d1b5 | 224 | if (verbose > 2) |
16f762a2 | 225 | dsos__fprintf(stdout); |
16f762a2 | 226 | |
4e4f06e4 | 227 | perf_session__collapse_resort(session); |
f823e441 | 228 | perf_session__output_resort(session, session->events_stats.total); |
b5b60fda | 229 | fprintf(stdout, "# Samples: %Ld\n#\n", session->events_stats.total); |
c351c281 | 230 | perf_session__fprintf_hists(session, NULL, false, stdout); |
3e6055ab ACM |
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"); | |
234 | ||
d599db3f ACM |
235 | if (show_threads) { |
236 | bool raw_printing_style = !strcmp(pretty_printing_style, "raw"); | |
237 | perf_read_values_display(stdout, &show_threads_values, | |
238 | raw_printing_style); | |
8d513270 | 239 | perf_read_values_destroy(&show_threads_values); |
d599db3f | 240 | } |
94c744b6 ACM |
241 | out_delete: |
242 | perf_session__delete(session); | |
016e92fb | 243 | return ret; |
8fa66bdc ACM |
244 | } |
245 | ||
4eb3e478 FW |
246 | static int |
247 | parse_callchain_opt(const struct option *opt __used, const char *arg, | |
b9a63b9b | 248 | int unset) |
4eb3e478 | 249 | { |
c20ab37e FW |
250 | char *tok; |
251 | char *endptr; | |
252 | ||
b9a63b9b ACM |
253 | /* |
254 | * --no-call-graph | |
255 | */ | |
256 | if (unset) { | |
257 | dont_use_callchains = true; | |
258 | return 0; | |
259 | } | |
260 | ||
d599db3f | 261 | symbol_conf.use_callchain = true; |
4eb3e478 FW |
262 | |
263 | if (!arg) | |
264 | return 0; | |
265 | ||
c20ab37e FW |
266 | tok = strtok((char *)arg, ","); |
267 | if (!tok) | |
268 | return -1; | |
269 | ||
270 | /* get the output mode */ | |
271 | if (!strncmp(tok, "graph", strlen(arg))) | |
805d127d | 272 | callchain_param.mode = CHAIN_GRAPH_ABS; |
4eb3e478 | 273 | |
c20ab37e | 274 | else if (!strncmp(tok, "flat", strlen(arg))) |
805d127d FW |
275 | callchain_param.mode = CHAIN_FLAT; |
276 | ||
277 | else if (!strncmp(tok, "fractal", strlen(arg))) | |
278 | callchain_param.mode = CHAIN_GRAPH_REL; | |
279 | ||
b1a88349 FW |
280 | else if (!strncmp(tok, "none", strlen(arg))) { |
281 | callchain_param.mode = CHAIN_NONE; | |
b7ae11b3 | 282 | symbol_conf.use_callchain = false; |
b1a88349 FW |
283 | |
284 | return 0; | |
285 | } | |
286 | ||
4eb3e478 FW |
287 | else |
288 | return -1; | |
289 | ||
c20ab37e FW |
290 | /* get the min percentage */ |
291 | tok = strtok(NULL, ","); | |
292 | if (!tok) | |
805d127d | 293 | goto setup; |
c20ab37e | 294 | |
805d127d | 295 | callchain_param.min_percent = strtod(tok, &endptr); |
c20ab37e FW |
296 | if (tok == endptr) |
297 | return -1; | |
298 | ||
805d127d FW |
299 | setup: |
300 | if (register_callchain_param(&callchain_param) < 0) { | |
301 | fprintf(stderr, "Can't register callchain params\n"); | |
302 | return -1; | |
303 | } | |
4eb3e478 FW |
304 | return 0; |
305 | } | |
306 | ||
0422a4fc | 307 | static const char * const report_usage[] = { |
53cb8bc2 IM |
308 | "perf report [<options>] <command>", |
309 | NULL | |
310 | }; | |
311 | ||
312 | static const struct option options[] = { | |
313 | OPT_STRING('i', "input", &input_name, "file", | |
314 | "input file name"), | |
815e777f ACM |
315 | OPT_BOOLEAN('v', "verbose", &verbose, |
316 | "be more verbose (show symbol address, etc)"), | |
97b07b69 IM |
317 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
318 | "dump raw trace in ASCII"), | |
b32d133a ACM |
319 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
320 | "file", "vmlinux pathname"), | |
fa6963b2 | 321 | OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), |
b32d133a | 322 | OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, |
42976487 | 323 | "load module symbols - WARNING: use only with -k and LIVE kernel"), |
d599db3f | 324 | OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples, |
e3d7e183 | 325 | "Show a column with the number of samples"), |
8d513270 BG |
326 | OPT_BOOLEAN('T', "threads", &show_threads, |
327 | "Show per-thread event counters"), | |
9f866697 BG |
328 | OPT_STRING(0, "pretty", &pretty_printing_style, "key", |
329 | "pretty printing style key: normal raw"), | |
63299f05 | 330 | OPT_STRING('s', "sort", &sort_order, "key[,key2...]", |
b25bcf2f | 331 | "sort by key(s): pid, comm, dso, symbol, parent"), |
f7d87444 | 332 | OPT_BOOLEAN('P', "full-paths", &symbol_conf.full_paths, |
b78c07d4 | 333 | "Don't shorten the pathnames taking into account the cwd"), |
b25bcf2f IM |
334 | OPT_STRING('p', "parent", &parent_pattern, "regex", |
335 | "regex filter to identify parent, see: '--sort parent'"), | |
d599db3f | 336 | OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other, |
b8e6d829 | 337 | "Only display entries with parent-match"), |
1483b19f | 338 | OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent", |
c20ab37e | 339 | "Display callchains using output_type and min percent threshold. " |
1483b19f | 340 | "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt), |
655000e7 | 341 | OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]", |
25903407 | 342 | "only consider symbols in these dsos"), |
655000e7 | 343 | OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]", |
cc8b88b1 | 344 | "only consider symbols in these comms"), |
655000e7 | 345 | OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]", |
7bec7a91 | 346 | "only consider these symbols"), |
655000e7 | 347 | OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str, |
52d422de ACM |
348 | "width[,width...]", |
349 | "don't try to adjust column width, use these fixed values"), | |
c410a338 | 350 | OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator", |
52d422de ACM |
351 | "separator for columns, no spaces will be added between " |
352 | "columns '.' is reserved."), | |
71289be7 ACM |
353 | OPT_BOOLEAN('U', "hide-unresolved", &hide_unresolved, |
354 | "Only display entries resolved to a symbol"), | |
53cb8bc2 IM |
355 | OPT_END() |
356 | }; | |
357 | ||
f37a291c | 358 | int cmd_report(int argc, const char **argv, const char *prefix __used) |
53cb8bc2 | 359 | { |
655000e7 ACM |
360 | argc = parse_options(argc, argv, options, report_usage, 0); |
361 | ||
362 | setup_pager(); | |
363 | ||
75be6cf4 | 364 | if (symbol__init() < 0) |
b32d133a | 365 | return -1; |
53cb8bc2 | 366 | |
c8829c7a | 367 | setup_sorting(report_usage, options); |
1aa16738 | 368 | |
021191b3 | 369 | if (parent_pattern != default_parent_pattern) { |
b8e6d829 | 370 | sort_dimension__add("parent"); |
021191b3 ACM |
371 | sort_parent.elide = 1; |
372 | } else | |
d599db3f | 373 | symbol_conf.exclude_other = false; |
b8e6d829 | 374 | |
edc52dea IM |
375 | /* |
376 | * Any (unrecognized) arguments left? | |
377 | */ | |
378 | if (argc) | |
379 | usage_with_options(report_usage, options); | |
380 | ||
655000e7 ACM |
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); | |
25903407 | 384 | |
53cb8bc2 IM |
385 | return __cmd_report(); |
386 | } |