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