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" |
53cb8bc2 IM |
25 | |
26 | #include "util/parse-options.h" | |
27 | #include "util/parse-events.h" | |
28 | ||
016e92fb | 29 | #include "util/data_map.h" |
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 | |
52d422de ACM |
36 | static char *dso_list_str, *comm_list_str, *sym_list_str, |
37 | *col_width_list_str; | |
7bec7a91 | 38 | static struct strlist *dso_list, *comm_list, *sym_list; |
bd74137e | 39 | |
fa6963b2 | 40 | static int force; |
8fa66bdc | 41 | |
b78c07d4 | 42 | static int full_paths; |
e3d7e183 | 43 | static int show_nr_samples; |
97b07b69 | 44 | |
8d513270 BG |
45 | static int show_threads; |
46 | static struct perf_read_values show_threads_values; | |
47 | ||
9f866697 BG |
48 | static char default_pretty_printing_style[] = "normal"; |
49 | static char *pretty_printing_style = default_pretty_printing_style; | |
50 | ||
b8e6d829 | 51 | static int exclude_other = 1; |
be903885 | 52 | |
805d127d FW |
53 | static char callchain_default_opt[] = "fractal,0.5"; |
54 | ||
0d3a5c88 FW |
55 | static struct perf_header *header; |
56 | ||
e6e18ec7 PZ |
57 | static u64 sample_type; |
58 | ||
b32d133a ACM |
59 | struct symbol_conf symbol_conf; |
60 | ||
a4fb581b FW |
61 | |
62 | static size_t | |
63 | callchain__fprintf_left_margin(FILE *fp, int left_margin) | |
64 | { | |
65 | int i; | |
66 | int ret; | |
67 | ||
68 | ret = fprintf(fp, " "); | |
69 | ||
70 | for (i = 0; i < left_margin; i++) | |
71 | ret += fprintf(fp, " "); | |
72 | ||
73 | return ret; | |
74 | } | |
75 | ||
76 | static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask, | |
77 | int left_margin) | |
4eb3e478 FW |
78 | { |
79 | int i; | |
80 | size_t ret = 0; | |
81 | ||
a4fb581b | 82 | ret += callchain__fprintf_left_margin(fp, left_margin); |
4eb3e478 FW |
83 | |
84 | for (i = 0; i < depth; i++) | |
85 | if (depth_mask & (1 << i)) | |
86 | ret += fprintf(fp, "| "); | |
87 | else | |
88 | ret += fprintf(fp, " "); | |
89 | ||
90 | ret += fprintf(fp, "\n"); | |
91 | ||
92 | return ret; | |
93 | } | |
f55c5552 | 94 | static size_t |
4eb3e478 FW |
95 | ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth, |
96 | int depth_mask, int count, u64 total_samples, | |
a4fb581b | 97 | int hits, int left_margin) |
4eb3e478 FW |
98 | { |
99 | int i; | |
100 | size_t ret = 0; | |
101 | ||
a4fb581b | 102 | ret += callchain__fprintf_left_margin(fp, left_margin); |
4eb3e478 FW |
103 | for (i = 0; i < depth; i++) { |
104 | if (depth_mask & (1 << i)) | |
105 | ret += fprintf(fp, "|"); | |
106 | else | |
107 | ret += fprintf(fp, " "); | |
108 | if (!count && i == depth - 1) { | |
109 | double percent; | |
110 | ||
111 | percent = hits * 100.0 / total_samples; | |
24b57c69 | 112 | ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent); |
4eb3e478 FW |
113 | } else |
114 | ret += fprintf(fp, "%s", " "); | |
115 | } | |
116 | if (chain->sym) | |
117 | ret += fprintf(fp, "%s\n", chain->sym->name); | |
118 | else | |
119 | ret += fprintf(fp, "%p\n", (void *)(long)chain->ip); | |
120 | ||
121 | return ret; | |
122 | } | |
123 | ||
25446036 FW |
124 | static struct symbol *rem_sq_bracket; |
125 | static struct callchain_list rem_hits; | |
126 | ||
127 | static void init_rem_hits(void) | |
128 | { | |
129 | rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6); | |
130 | if (!rem_sq_bracket) { | |
131 | fprintf(stderr, "Not enough memory to display remaining hits\n"); | |
132 | return; | |
133 | } | |
134 | ||
135 | strcpy(rem_sq_bracket->name, "[...]"); | |
136 | rem_hits.sym = rem_sq_bracket; | |
137 | } | |
138 | ||
4eb3e478 | 139 | static size_t |
af0a6fa4 | 140 | __callchain__fprintf_graph(FILE *fp, struct callchain_node *self, |
a4fb581b FW |
141 | u64 total_samples, int depth, int depth_mask, |
142 | int left_margin) | |
4eb3e478 FW |
143 | { |
144 | struct rb_node *node, *next; | |
145 | struct callchain_node *child; | |
146 | struct callchain_list *chain; | |
147 | int new_depth_mask = depth_mask; | |
805d127d | 148 | u64 new_total; |
25446036 | 149 | u64 remaining; |
4eb3e478 FW |
150 | size_t ret = 0; |
151 | int i; | |
152 | ||
805d127d | 153 | if (callchain_param.mode == CHAIN_GRAPH_REL) |
1953287b | 154 | new_total = self->children_hit; |
805d127d FW |
155 | else |
156 | new_total = total_samples; | |
157 | ||
25446036 FW |
158 | remaining = new_total; |
159 | ||
4eb3e478 FW |
160 | node = rb_first(&self->rb_root); |
161 | while (node) { | |
25446036 FW |
162 | u64 cumul; |
163 | ||
4eb3e478 | 164 | child = rb_entry(node, struct callchain_node, rb_node); |
25446036 FW |
165 | cumul = cumul_hits(child); |
166 | remaining -= cumul; | |
4eb3e478 FW |
167 | |
168 | /* | |
169 | * The depth mask manages the output of pipes that show | |
170 | * the depth. We don't want to keep the pipes of the current | |
25446036 FW |
171 | * level for the last child of this depth. |
172 | * Except if we have remaining filtered hits. They will | |
173 | * supersede the last child | |
4eb3e478 FW |
174 | */ |
175 | next = rb_next(node); | |
25446036 | 176 | if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining)) |
4eb3e478 FW |
177 | new_depth_mask &= ~(1 << (depth - 1)); |
178 | ||
179 | /* | |
180 | * But we keep the older depth mask for the line seperator | |
181 | * to keep the level link until we reach the last child | |
182 | */ | |
a4fb581b FW |
183 | ret += ipchain__fprintf_graph_line(fp, depth, depth_mask, |
184 | left_margin); | |
4eb3e478 FW |
185 | i = 0; |
186 | list_for_each_entry(chain, &child->val, list) { | |
187 | if (chain->ip >= PERF_CONTEXT_MAX) | |
188 | continue; | |
189 | ret += ipchain__fprintf_graph(fp, chain, depth, | |
190 | new_depth_mask, i++, | |
805d127d | 191 | new_total, |
a4fb581b FW |
192 | cumul, |
193 | left_margin); | |
4eb3e478 | 194 | } |
af0a6fa4 FW |
195 | ret += __callchain__fprintf_graph(fp, child, new_total, |
196 | depth + 1, | |
a4fb581b FW |
197 | new_depth_mask | (1 << depth), |
198 | left_margin); | |
4eb3e478 FW |
199 | node = next; |
200 | } | |
201 | ||
25446036 FW |
202 | if (callchain_param.mode == CHAIN_GRAPH_REL && |
203 | remaining && remaining != new_total) { | |
204 | ||
205 | if (!rem_sq_bracket) | |
206 | return ret; | |
207 | ||
208 | new_depth_mask &= ~(1 << (depth - 1)); | |
209 | ||
210 | ret += ipchain__fprintf_graph(fp, &rem_hits, depth, | |
211 | new_depth_mask, 0, new_total, | |
a4fb581b | 212 | remaining, left_margin); |
25446036 FW |
213 | } |
214 | ||
4eb3e478 FW |
215 | return ret; |
216 | } | |
217 | ||
a4fb581b | 218 | |
af0a6fa4 FW |
219 | static size_t |
220 | callchain__fprintf_graph(FILE *fp, struct callchain_node *self, | |
a4fb581b | 221 | u64 total_samples, int left_margin) |
af0a6fa4 FW |
222 | { |
223 | struct callchain_list *chain; | |
a4fb581b | 224 | bool printed = false; |
af0a6fa4 FW |
225 | int i = 0; |
226 | int ret = 0; | |
227 | ||
228 | list_for_each_entry(chain, &self->val, list) { | |
229 | if (chain->ip >= PERF_CONTEXT_MAX) | |
230 | continue; | |
231 | ||
a4fb581b | 232 | if (!i++ && sort__first_dimension == SORT_SYM) |
af0a6fa4 FW |
233 | continue; |
234 | ||
a4fb581b FW |
235 | if (!printed) { |
236 | ret += callchain__fprintf_left_margin(fp, left_margin); | |
237 | ret += fprintf(fp, "|\n"); | |
238 | ret += callchain__fprintf_left_margin(fp, left_margin); | |
239 | ret += fprintf(fp, "---"); | |
240 | ||
241 | left_margin += 3; | |
242 | printed = true; | |
243 | } else | |
244 | ret += callchain__fprintf_left_margin(fp, left_margin); | |
245 | ||
af0a6fa4 | 246 | if (chain->sym) |
a4fb581b | 247 | ret += fprintf(fp, " %s\n", chain->sym->name); |
af0a6fa4 | 248 | else |
a4fb581b | 249 | ret += fprintf(fp, " %p\n", (void *)(long)chain->ip); |
af0a6fa4 FW |
250 | } |
251 | ||
a4fb581b | 252 | ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin); |
af0a6fa4 FW |
253 | |
254 | return ret; | |
255 | } | |
256 | ||
4eb3e478 FW |
257 | static size_t |
258 | callchain__fprintf_flat(FILE *fp, struct callchain_node *self, | |
259 | u64 total_samples) | |
f55c5552 FW |
260 | { |
261 | struct callchain_list *chain; | |
262 | size_t ret = 0; | |
263 | ||
264 | if (!self) | |
265 | return 0; | |
266 | ||
4eb3e478 | 267 | ret += callchain__fprintf_flat(fp, self->parent, total_samples); |
f55c5552 FW |
268 | |
269 | ||
4424961a FW |
270 | list_for_each_entry(chain, &self->val, list) { |
271 | if (chain->ip >= PERF_CONTEXT_MAX) | |
272 | continue; | |
273 | if (chain->sym) | |
274 | ret += fprintf(fp, " %s\n", chain->sym->name); | |
275 | else | |
276 | ret += fprintf(fp, " %p\n", | |
f37a291c | 277 | (void *)(long)chain->ip); |
4424961a | 278 | } |
f55c5552 FW |
279 | |
280 | return ret; | |
281 | } | |
282 | ||
283 | static size_t | |
284 | hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self, | |
a4fb581b | 285 | u64 total_samples, int left_margin) |
f55c5552 FW |
286 | { |
287 | struct rb_node *rb_node; | |
288 | struct callchain_node *chain; | |
289 | size_t ret = 0; | |
290 | ||
291 | rb_node = rb_first(&self->sorted_chain); | |
292 | while (rb_node) { | |
293 | double percent; | |
294 | ||
295 | chain = rb_entry(rb_node, struct callchain_node, rb_node); | |
296 | percent = chain->hit * 100.0 / total_samples; | |
805d127d FW |
297 | switch (callchain_param.mode) { |
298 | case CHAIN_FLAT: | |
24b57c69 FW |
299 | ret += percent_color_fprintf(fp, " %6.2f%%\n", |
300 | percent); | |
4eb3e478 | 301 | ret += callchain__fprintf_flat(fp, chain, total_samples); |
805d127d FW |
302 | break; |
303 | case CHAIN_GRAPH_ABS: /* Falldown */ | |
304 | case CHAIN_GRAPH_REL: | |
a4fb581b FW |
305 | ret += callchain__fprintf_graph(fp, chain, total_samples, |
306 | left_margin); | |
83a0944f | 307 | case CHAIN_NONE: |
805d127d FW |
308 | default: |
309 | break; | |
4eb3e478 | 310 | } |
f55c5552 FW |
311 | ret += fprintf(fp, "\n"); |
312 | rb_node = rb_next(rb_node); | |
313 | } | |
314 | ||
315 | return ret; | |
316 | } | |
317 | ||
1aa16738 | 318 | static size_t |
9cffa8d5 | 319 | hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples) |
1aa16738 PZ |
320 | { |
321 | struct sort_entry *se; | |
322 | size_t ret; | |
323 | ||
b8e6d829 IM |
324 | if (exclude_other && !self->parent) |
325 | return 0; | |
326 | ||
1e11fd82 | 327 | if (total_samples) |
52d422de ACM |
328 | ret = percent_color_fprintf(fp, |
329 | field_sep ? "%.2f" : " %6.2f%%", | |
330 | (self->count * 100.0) / total_samples); | |
1e11fd82 | 331 | else |
52d422de | 332 | ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count); |
1aa16738 | 333 | |
e3d7e183 ACM |
334 | if (show_nr_samples) { |
335 | if (field_sep) | |
336 | fprintf(fp, "%c%lld", *field_sep, self->count); | |
337 | else | |
338 | fprintf(fp, "%11lld", self->count); | |
339 | } | |
1aa16738 | 340 | |
71dd8945 | 341 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
021191b3 | 342 | if (se->elide) |
b8e6d829 IM |
343 | continue; |
344 | ||
52d422de ACM |
345 | fprintf(fp, "%s", field_sep ?: " "); |
346 | ret += se->print(fp, self, se->width ? *se->width : 0); | |
71dd8945 | 347 | } |
1aa16738 PZ |
348 | |
349 | ret += fprintf(fp, "\n"); | |
350 | ||
a4fb581b FW |
351 | if (callchain) { |
352 | int left_margin = 0; | |
353 | ||
354 | if (sort__first_dimension == SORT_COMM) { | |
355 | se = list_first_entry(&hist_entry__sort_list, typeof(*se), | |
356 | list); | |
357 | left_margin = se->width ? *se->width : 0; | |
358 | left_margin -= thread__comm_len(self->thread); | |
359 | } | |
360 | ||
361 | hist_entry_callchain__fprintf(fp, self, total_samples, | |
362 | left_margin); | |
363 | } | |
f55c5552 | 364 | |
1aa16738 PZ |
365 | return ret; |
366 | } | |
367 | ||
6e7d6fdc PZ |
368 | /* |
369 | * | |
370 | */ | |
371 | ||
52d422de ACM |
372 | static void dso__calc_col_width(struct dso *self) |
373 | { | |
374 | if (!col_width_list_str && !field_sep && | |
375 | (!dso_list || strlist__has_entry(dso_list, self->name))) { | |
376 | unsigned int slen = strlen(self->name); | |
377 | if (slen > dsos__col_width) | |
378 | dsos__col_width = slen; | |
379 | } | |
380 | ||
381 | self->slen_calculated = 1; | |
382 | } | |
383 | ||
5b447a6a | 384 | static void thread__comm_adjust(struct thread *self) |
4273b005 | 385 | { |
5b447a6a | 386 | char *comm = self->comm; |
4273b005 FW |
387 | |
388 | if (!col_width_list_str && !field_sep && | |
389 | (!comm_list || strlist__has_entry(comm_list, comm))) { | |
390 | unsigned int slen = strlen(comm); | |
391 | ||
392 | if (slen > comms__col_width) { | |
393 | comms__col_width = slen; | |
394 | threads__col_width = slen + 6; | |
395 | } | |
396 | } | |
5b447a6a FW |
397 | } |
398 | ||
399 | static int thread__set_comm_adjust(struct thread *self, const char *comm) | |
400 | { | |
401 | int ret = thread__set_comm(self, comm); | |
402 | ||
403 | if (ret) | |
404 | return ret; | |
405 | ||
406 | thread__comm_adjust(self); | |
4273b005 FW |
407 | |
408 | return 0; | |
409 | } | |
410 | ||
2a0a50fe | 411 | static int call__match(struct symbol *sym) |
6e7d6fdc | 412 | { |
b25bcf2f | 413 | if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0)) |
2a0a50fe | 414 | return 1; |
6e7d6fdc | 415 | |
2a0a50fe | 416 | return 0; |
6e7d6fdc PZ |
417 | } |
418 | ||
50e5095a | 419 | static struct symbol **resolve_callchain(struct thread *thread, |
9735abf1 ACM |
420 | struct ip_callchain *chain, |
421 | struct symbol **parent) | |
4424961a | 422 | { |
1ed091c4 | 423 | u8 cpumode = PERF_RECORD_MISC_USER; |
029e5b16 | 424 | struct symbol **syms = NULL; |
f37a291c | 425 | unsigned int i; |
4424961a FW |
426 | |
427 | if (callchain) { | |
428 | syms = calloc(chain->nr, sizeof(*syms)); | |
429 | if (!syms) { | |
430 | fprintf(stderr, "Can't allocate memory for symbols\n"); | |
431 | exit(-1); | |
432 | } | |
433 | } | |
434 | ||
435 | for (i = 0; i < chain->nr; i++) { | |
436 | u64 ip = chain->ips[i]; | |
1ed091c4 | 437 | struct addr_location al; |
4424961a FW |
438 | |
439 | if (ip >= PERF_CONTEXT_MAX) { | |
1ed091c4 ACM |
440 | switch (ip) { |
441 | case PERF_CONTEXT_HV: | |
442 | cpumode = PERF_RECORD_MISC_HYPERVISOR; break; | |
443 | case PERF_CONTEXT_KERNEL: | |
444 | cpumode = PERF_RECORD_MISC_KERNEL; break; | |
445 | case PERF_CONTEXT_USER: | |
446 | cpumode = PERF_RECORD_MISC_USER; break; | |
447 | default: | |
448 | break; | |
449 | } | |
4424961a FW |
450 | continue; |
451 | } | |
452 | ||
1ed091c4 ACM |
453 | thread__find_addr_location(thread, cpumode, MAP__FUNCTION, |
454 | ip, &al, NULL); | |
455 | if (al.sym != NULL) { | |
456 | if (sort__has_parent && !*parent && | |
457 | call__match(al.sym)) | |
458 | *parent = al.sym; | |
4424961a FW |
459 | if (!callchain) |
460 | break; | |
1ed091c4 | 461 | syms[i] = al.sym; |
4424961a FW |
462 | } |
463 | } | |
464 | ||
465 | return syms; | |
466 | } | |
467 | ||
1aa16738 PZ |
468 | /* |
469 | * collect histogram counts | |
470 | */ | |
471 | ||
1ed091c4 ACM |
472 | static int hist_entry__add(struct addr_location *al, |
473 | struct ip_callchain *chain, u64 count) | |
8fa66bdc | 474 | { |
9735abf1 ACM |
475 | struct symbol **syms = NULL, *parent = NULL; |
476 | bool hit; | |
e7fb08b1 | 477 | struct hist_entry *he; |
e7fb08b1 | 478 | |
4424961a | 479 | if ((sort__has_parent || callchain) && chain) |
1ed091c4 | 480 | syms = resolve_callchain(al->thread, chain, &parent); |
e7fb08b1 | 481 | |
1ed091c4 | 482 | he = __hist_entry__add(al, parent, count, &hit); |
9735abf1 ACM |
483 | if (he == NULL) |
484 | return -ENOMEM; | |
e7fb08b1 | 485 | |
9735abf1 ACM |
486 | if (hit) |
487 | he->count += count; | |
e7fb08b1 | 488 | |
f55c5552 | 489 | if (callchain) { |
9735abf1 ACM |
490 | if (!hit) |
491 | callchain_init(&he->callchain); | |
4424961a FW |
492 | append_chain(&he->callchain, chain, syms); |
493 | free(syms); | |
f55c5552 | 494 | } |
e7fb08b1 PZ |
495 | |
496 | return 0; | |
8fa66bdc ACM |
497 | } |
498 | ||
9cffa8d5 | 499 | static size_t output__fprintf(FILE *fp, u64 total_samples) |
3a4b8cc7 | 500 | { |
e7fb08b1 | 501 | struct hist_entry *pos; |
2d65537e | 502 | struct sort_entry *se; |
3a4b8cc7 ACM |
503 | struct rb_node *nd; |
504 | size_t ret = 0; | |
52d422de ACM |
505 | unsigned int width; |
506 | char *col_width = col_width_list_str; | |
9f866697 BG |
507 | int raw_printing_style; |
508 | ||
509 | raw_printing_style = !strcmp(pretty_printing_style, "raw"); | |
3a4b8cc7 | 510 | |
25446036 FW |
511 | init_rem_hits(); |
512 | ||
021191b3 | 513 | fprintf(fp, "# Samples: %Ld\n", (u64)total_samples); |
ca8cdeef PZ |
514 | fprintf(fp, "#\n"); |
515 | ||
516 | fprintf(fp, "# Overhead"); | |
e3d7e183 ACM |
517 | if (show_nr_samples) { |
518 | if (field_sep) | |
519 | fprintf(fp, "%cSamples", *field_sep); | |
520 | else | |
521 | fputs(" Samples ", fp); | |
522 | } | |
b8e6d829 | 523 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
021191b3 | 524 | if (se->elide) |
b8e6d829 | 525 | continue; |
52d422de ACM |
526 | if (field_sep) { |
527 | fprintf(fp, "%c%s", *field_sep, se->header); | |
b8e6d829 | 528 | continue; |
52d422de ACM |
529 | } |
530 | width = strlen(se->header); | |
531 | if (se->width) { | |
532 | if (col_width_list_str) { | |
533 | if (col_width) { | |
534 | *se->width = atoi(col_width); | |
535 | col_width = strchr(col_width, ','); | |
536 | if (col_width) | |
537 | ++col_width; | |
538 | } | |
539 | } | |
540 | width = *se->width = max(*se->width, width); | |
541 | } | |
542 | fprintf(fp, " %*s", width, se->header); | |
b8e6d829 | 543 | } |
ca8cdeef PZ |
544 | fprintf(fp, "\n"); |
545 | ||
52d422de ACM |
546 | if (field_sep) |
547 | goto print_entries; | |
548 | ||
ca8cdeef | 549 | fprintf(fp, "# ........"); |
e3d7e183 ACM |
550 | if (show_nr_samples) |
551 | fprintf(fp, " .........."); | |
2d65537e | 552 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
f37a291c | 553 | unsigned int i; |
ca8cdeef | 554 | |
021191b3 | 555 | if (se->elide) |
b8e6d829 IM |
556 | continue; |
557 | ||
4593bba8 | 558 | fprintf(fp, " "); |
52d422de ACM |
559 | if (se->width) |
560 | width = *se->width; | |
561 | else | |
562 | width = strlen(se->header); | |
563 | for (i = 0; i < width; i++) | |
ca8cdeef | 564 | fprintf(fp, "."); |
2d65537e | 565 | } |
ca8cdeef PZ |
566 | fprintf(fp, "\n"); |
567 | ||
568 | fprintf(fp, "#\n"); | |
2d65537e | 569 | |
52d422de | 570 | print_entries: |
e7fb08b1 PZ |
571 | for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) { |
572 | pos = rb_entry(nd, struct hist_entry, rb_node); | |
573 | ret += hist_entry__fprintf(fp, pos, total_samples); | |
3a4b8cc7 ACM |
574 | } |
575 | ||
b8e6d829 IM |
576 | if (sort_order == default_sort_order && |
577 | parent_pattern == default_parent_pattern) { | |
bd74137e | 578 | fprintf(fp, "#\n"); |
114cfab2 | 579 | fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n"); |
bd74137e IM |
580 | fprintf(fp, "#\n"); |
581 | } | |
71dd8945 | 582 | fprintf(fp, "\n"); |
bd74137e | 583 | |
25446036 FW |
584 | free(rem_sq_bracket); |
585 | ||
8d513270 | 586 | if (show_threads) |
9f866697 BG |
587 | perf_read_values_display(fp, &show_threads_values, |
588 | raw_printing_style); | |
8d513270 | 589 | |
3a4b8cc7 ACM |
590 | return ret; |
591 | } | |
592 | ||
2a0a50fe | 593 | static int validate_chain(struct ip_callchain *chain, event_t *event) |
7522060c IM |
594 | { |
595 | unsigned int chain_size; | |
596 | ||
7522060c IM |
597 | chain_size = event->header.size; |
598 | chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event; | |
599 | ||
9cffa8d5 | 600 | if (chain->nr*sizeof(u64) > chain_size) |
7522060c IM |
601 | return -1; |
602 | ||
603 | return 0; | |
604 | } | |
605 | ||
62daacb5 | 606 | static int process_sample_event(event_t *event) |
75051724 | 607 | { |
180f95e2 | 608 | struct sample_data data; |
d8db1b57 | 609 | int cpumode; |
1ed091c4 | 610 | struct addr_location al; |
180f95e2 | 611 | struct thread *thread; |
6baa0a5a | 612 | |
180f95e2 OH |
613 | memset(&data, 0, sizeof(data)); |
614 | data.period = 1; | |
615 | ||
616 | event__parse_sample(event, sample_type, &data); | |
ea1900e5 | 617 | |
62daacb5 | 618 | dump_printf("(IP, %d): %d/%d: %p period: %Ld\n", |
75051724 | 619 | event->header.misc, |
180f95e2 OH |
620 | data.pid, data.tid, |
621 | (void *)(long)data.ip, | |
622 | (long long)data.period); | |
75051724 | 623 | |
e6e18ec7 | 624 | if (sample_type & PERF_SAMPLE_CALLCHAIN) { |
f37a291c | 625 | unsigned int i; |
3efa1cc9 | 626 | |
180f95e2 | 627 | dump_printf("... chain: nr:%Lu\n", data.callchain->nr); |
3efa1cc9 | 628 | |
180f95e2 | 629 | if (validate_chain(data.callchain, event) < 0) { |
6beba7ad ACM |
630 | pr_debug("call-chain problem with event, " |
631 | "skipping it.\n"); | |
7522060c IM |
632 | return 0; |
633 | } | |
634 | ||
635 | if (dump_trace) { | |
180f95e2 OH |
636 | for (i = 0; i < data.callchain->nr; i++) |
637 | dump_printf("..... %2d: %016Lx\n", | |
638 | i, data.callchain->ips[i]); | |
3efa1cc9 IM |
639 | } |
640 | } | |
641 | ||
180f95e2 | 642 | thread = threads__findnew(data.pid); |
75051724 | 643 | if (thread == NULL) { |
6beba7ad | 644 | pr_debug("problem processing %d event, skipping it.\n", |
75051724 IM |
645 | event->header.type); |
646 | return -1; | |
647 | } | |
e7fb08b1 | 648 | |
f39cdf25 JL |
649 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); |
650 | ||
cc8b88b1 ACM |
651 | if (comm_list && !strlist__has_entry(comm_list, thread->comm)) |
652 | return 0; | |
653 | ||
cdd6c482 | 654 | cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
d8db1b57 | 655 | |
1ed091c4 | 656 | thread__find_addr_location(thread, cpumode, |
180f95e2 | 657 | MAP__FUNCTION, data.ip, &al, NULL); |
1ed091c4 ACM |
658 | /* |
659 | * We have to do this here as we may have a dso with no symbol hit that | |
660 | * has a name longer than the ones with symbols sampled. | |
661 | */ | |
662 | if (al.map && !sort_dso.elide && !al.map->dso->slen_calculated) | |
663 | dso__calc_col_width(al.map->dso); | |
8fa66bdc | 664 | |
ec218fc4 | 665 | if (dso_list && |
1ed091c4 ACM |
666 | (!al.map || !al.map->dso || |
667 | !(strlist__has_entry(dso_list, al.map->dso->short_name) || | |
668 | (al.map->dso->short_name != al.map->dso->long_name && | |
669 | strlist__has_entry(dso_list, al.map->dso->long_name))))) | |
ec218fc4 | 670 | return 0; |
25903407 | 671 | |
1ed091c4 | 672 | if (sym_list && al.sym && !strlist__has_entry(sym_list, al.sym->name)) |
ec218fc4 | 673 | return 0; |
7bec7a91 | 674 | |
180f95e2 | 675 | if (hist_entry__add(&al, data.callchain, data.period)) { |
6beba7ad | 676 | pr_debug("problem incrementing symbol count, skipping event\n"); |
ec218fc4 | 677 | return -1; |
8fa66bdc | 678 | } |
ec218fc4 | 679 | |
180f95e2 | 680 | event__stats.total += data.period; |
8fa66bdc | 681 | |
75051724 IM |
682 | return 0; |
683 | } | |
3502973d | 684 | |
62daacb5 | 685 | static int process_comm_event(event_t *event) |
75051724 | 686 | { |
d5b889f2 | 687 | struct thread *thread = threads__findnew(event->comm.pid); |
75051724 | 688 | |
62daacb5 | 689 | dump_printf(": %s:%d\n", event->comm.comm, event->comm.pid); |
75051724 IM |
690 | |
691 | if (thread == NULL || | |
4273b005 | 692 | thread__set_comm_adjust(thread, event->comm.comm)) { |
cdd6c482 | 693 | dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n"); |
75051724 | 694 | return -1; |
8fa66bdc | 695 | } |
9d91a6f7 PZ |
696 | |
697 | return 0; | |
698 | } | |
699 | ||
62daacb5 | 700 | static int process_read_event(event_t *event) |
e9ea2fde | 701 | { |
cdd6c482 | 702 | struct perf_event_attr *attr; |
0d3a5c88 FW |
703 | |
704 | attr = perf_header__find_attr(event->read.id, header); | |
8f18aec5 | 705 | |
8d513270 | 706 | if (show_threads) { |
83a0944f | 707 | const char *name = attr ? __event_name(attr->type, attr->config) |
8d513270 BG |
708 | : "unknown"; |
709 | perf_read_values_add_value(&show_threads_values, | |
710 | event->read.pid, event->read.tid, | |
711 | event->read.id, | |
712 | name, | |
713 | event->read.value); | |
714 | } | |
715 | ||
62daacb5 ACM |
716 | dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid, |
717 | attr ? __event_name(attr->type, attr->config) : "FAIL", | |
718 | event->read.value); | |
e9ea2fde PZ |
719 | |
720 | return 0; | |
721 | } | |
722 | ||
016e92fb | 723 | static int sample_type_check(u64 type) |
d80d338d | 724 | { |
016e92fb | 725 | sample_type = type; |
e6e18ec7 | 726 | |
91b4eaea FW |
727 | if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) { |
728 | if (sort__has_parent) { | |
729 | fprintf(stderr, "selected --sort parent, but no" | |
730 | " callchain data. Did you call" | |
731 | " perf record without -g?\n"); | |
016e92fb | 732 | return -1; |
91b4eaea FW |
733 | } |
734 | if (callchain) { | |
6ede59c4 | 735 | fprintf(stderr, "selected -g but no callchain data." |
91b4eaea FW |
736 | " Did you call perf record without" |
737 | " -g?\n"); | |
016e92fb | 738 | return -1; |
91b4eaea | 739 | } |
b1a88349 FW |
740 | } else if (callchain_param.mode != CHAIN_NONE && !callchain) { |
741 | callchain = 1; | |
742 | if (register_callchain_param(&callchain_param) < 0) { | |
743 | fprintf(stderr, "Can't register callchain" | |
744 | " params\n"); | |
016e92fb | 745 | return -1; |
b1a88349 | 746 | } |
f5970550 PZ |
747 | } |
748 | ||
016e92fb FW |
749 | return 0; |
750 | } | |
6142f9ec | 751 | |
016e92fb FW |
752 | static struct perf_file_handler file_handler = { |
753 | .process_sample_event = process_sample_event, | |
62daacb5 | 754 | .process_mmap_event = event__process_mmap, |
016e92fb | 755 | .process_comm_event = process_comm_event, |
62daacb5 ACM |
756 | .process_exit_event = event__process_task, |
757 | .process_fork_event = event__process_task, | |
758 | .process_lost_event = event__process_lost, | |
016e92fb FW |
759 | .process_read_event = process_read_event, |
760 | .sample_type_check = sample_type_check, | |
761 | }; | |
6142f9ec | 762 | |
6142f9ec | 763 | |
016e92fb FW |
764 | static int __cmd_report(void) |
765 | { | |
766 | struct thread *idle; | |
767 | int ret; | |
8fa66bdc | 768 | |
d5b889f2 | 769 | idle = register_idle_thread(); |
016e92fb | 770 | thread__comm_adjust(idle); |
f49515b1 | 771 | |
016e92fb FW |
772 | if (show_threads) |
773 | perf_read_values_init(&show_threads_values); | |
f5970550 | 774 | |
016e92fb | 775 | register_perf_file_handler(&file_handler); |
8fa66bdc | 776 | |
b32d133a | 777 | ret = mmap_dispatch_perf_file(&header, input_name, force, |
62daacb5 | 778 | full_paths, &event__cwdlen, &event__cwd); |
016e92fb FW |
779 | if (ret) |
780 | return ret; | |
97b07b69 | 781 | |
62daacb5 ACM |
782 | if (dump_trace) { |
783 | event__print_totals(); | |
97b07b69 | 784 | return 0; |
62daacb5 | 785 | } |
97b07b69 | 786 | |
da21d1b5 | 787 | if (verbose > 3) |
d5b889f2 | 788 | threads__fprintf(stdout); |
9ac99545 | 789 | |
da21d1b5 | 790 | if (verbose > 2) |
16f762a2 | 791 | dsos__fprintf(stdout); |
16f762a2 | 792 | |
8229289b | 793 | collapse__resort(); |
62daacb5 ACM |
794 | output__resort(event__stats.total); |
795 | output__fprintf(stdout, event__stats.total); | |
8fa66bdc | 796 | |
8d513270 BG |
797 | if (show_threads) |
798 | perf_read_values_destroy(&show_threads_values); | |
799 | ||
016e92fb | 800 | return ret; |
8fa66bdc ACM |
801 | } |
802 | ||
4eb3e478 FW |
803 | static int |
804 | parse_callchain_opt(const struct option *opt __used, const char *arg, | |
805 | int unset __used) | |
806 | { | |
c20ab37e FW |
807 | char *tok; |
808 | char *endptr; | |
809 | ||
4eb3e478 FW |
810 | callchain = 1; |
811 | ||
812 | if (!arg) | |
813 | return 0; | |
814 | ||
c20ab37e FW |
815 | tok = strtok((char *)arg, ","); |
816 | if (!tok) | |
817 | return -1; | |
818 | ||
819 | /* get the output mode */ | |
820 | if (!strncmp(tok, "graph", strlen(arg))) | |
805d127d | 821 | callchain_param.mode = CHAIN_GRAPH_ABS; |
4eb3e478 | 822 | |
c20ab37e | 823 | else if (!strncmp(tok, "flat", strlen(arg))) |
805d127d FW |
824 | callchain_param.mode = CHAIN_FLAT; |
825 | ||
826 | else if (!strncmp(tok, "fractal", strlen(arg))) | |
827 | callchain_param.mode = CHAIN_GRAPH_REL; | |
828 | ||
b1a88349 FW |
829 | else if (!strncmp(tok, "none", strlen(arg))) { |
830 | callchain_param.mode = CHAIN_NONE; | |
831 | callchain = 0; | |
832 | ||
833 | return 0; | |
834 | } | |
835 | ||
4eb3e478 FW |
836 | else |
837 | return -1; | |
838 | ||
c20ab37e FW |
839 | /* get the min percentage */ |
840 | tok = strtok(NULL, ","); | |
841 | if (!tok) | |
805d127d | 842 | goto setup; |
c20ab37e | 843 | |
805d127d | 844 | callchain_param.min_percent = strtod(tok, &endptr); |
c20ab37e FW |
845 | if (tok == endptr) |
846 | return -1; | |
847 | ||
805d127d FW |
848 | setup: |
849 | if (register_callchain_param(&callchain_param) < 0) { | |
850 | fprintf(stderr, "Can't register callchain params\n"); | |
851 | return -1; | |
852 | } | |
4eb3e478 FW |
853 | return 0; |
854 | } | |
855 | ||
dd68ada2 JK |
856 | //static const char * const report_usage[] = { |
857 | const char * const report_usage[] = { | |
53cb8bc2 IM |
858 | "perf report [<options>] <command>", |
859 | NULL | |
860 | }; | |
861 | ||
862 | static const struct option options[] = { | |
863 | OPT_STRING('i', "input", &input_name, "file", | |
864 | "input file name"), | |
815e777f ACM |
865 | OPT_BOOLEAN('v', "verbose", &verbose, |
866 | "be more verbose (show symbol address, etc)"), | |
97b07b69 IM |
867 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
868 | "dump raw trace in ASCII"), | |
b32d133a ACM |
869 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
870 | "file", "vmlinux pathname"), | |
fa6963b2 | 871 | OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), |
b32d133a | 872 | OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, |
42976487 | 873 | "load module symbols - WARNING: use only with -k and LIVE kernel"), |
e3d7e183 ACM |
874 | OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples, |
875 | "Show a column with the number of samples"), | |
8d513270 BG |
876 | OPT_BOOLEAN('T', "threads", &show_threads, |
877 | "Show per-thread event counters"), | |
9f866697 BG |
878 | OPT_STRING(0, "pretty", &pretty_printing_style, "key", |
879 | "pretty printing style key: normal raw"), | |
63299f05 | 880 | OPT_STRING('s', "sort", &sort_order, "key[,key2...]", |
b25bcf2f | 881 | "sort by key(s): pid, comm, dso, symbol, parent"), |
b78c07d4 ACM |
882 | OPT_BOOLEAN('P', "full-paths", &full_paths, |
883 | "Don't shorten the pathnames taking into account the cwd"), | |
b25bcf2f IM |
884 | OPT_STRING('p', "parent", &parent_pattern, "regex", |
885 | "regex filter to identify parent, see: '--sort parent'"), | |
b8e6d829 IM |
886 | OPT_BOOLEAN('x', "exclude-other", &exclude_other, |
887 | "Only display entries with parent-match"), | |
1483b19f | 888 | OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent", |
c20ab37e | 889 | "Display callchains using output_type and min percent threshold. " |
1483b19f | 890 | "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt), |
25903407 ACM |
891 | OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]", |
892 | "only consider symbols in these dsos"), | |
cc8b88b1 ACM |
893 | OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]", |
894 | "only consider symbols in these comms"), | |
7bec7a91 ACM |
895 | OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]", |
896 | "only consider these symbols"), | |
52d422de ACM |
897 | OPT_STRING('w', "column-widths", &col_width_list_str, |
898 | "width[,width...]", | |
899 | "don't try to adjust column width, use these fixed values"), | |
900 | OPT_STRING('t', "field-separator", &field_sep, "separator", | |
901 | "separator for columns, no spaces will be added between " | |
902 | "columns '.' is reserved."), | |
53cb8bc2 IM |
903 | OPT_END() |
904 | }; | |
905 | ||
5352f35d IM |
906 | static void setup_sorting(void) |
907 | { | |
908 | char *tmp, *tok, *str = strdup(sort_order); | |
909 | ||
910 | for (tok = strtok_r(str, ", ", &tmp); | |
911 | tok; tok = strtok_r(NULL, ", ", &tmp)) { | |
912 | if (sort_dimension__add(tok) < 0) { | |
913 | error("Unknown --sort key: `%s'", tok); | |
914 | usage_with_options(report_usage, options); | |
915 | } | |
916 | } | |
917 | ||
918 | free(str); | |
919 | } | |
920 | ||
cc8b88b1 | 921 | static void setup_list(struct strlist **list, const char *list_str, |
021191b3 ACM |
922 | struct sort_entry *se, const char *list_name, |
923 | FILE *fp) | |
cc8b88b1 ACM |
924 | { |
925 | if (list_str) { | |
926 | *list = strlist__new(true, list_str); | |
927 | if (!*list) { | |
928 | fprintf(stderr, "problems parsing %s list\n", | |
929 | list_name); | |
930 | exit(129); | |
931 | } | |
021191b3 ACM |
932 | if (strlist__nr_entries(*list) == 1) { |
933 | fprintf(fp, "# %s: %s\n", list_name, | |
934 | strlist__entry(*list, 0)->s); | |
935 | se->elide = true; | |
936 | } | |
cc8b88b1 ACM |
937 | } |
938 | } | |
939 | ||
f37a291c | 940 | int cmd_report(int argc, const char **argv, const char *prefix __used) |
53cb8bc2 | 941 | { |
b32d133a ACM |
942 | if (symbol__init(&symbol_conf) < 0) |
943 | return -1; | |
53cb8bc2 | 944 | |
edc52dea | 945 | argc = parse_options(argc, argv, options, report_usage, 0); |
53cb8bc2 | 946 | |
1aa16738 PZ |
947 | setup_sorting(); |
948 | ||
021191b3 | 949 | if (parent_pattern != default_parent_pattern) { |
b8e6d829 | 950 | sort_dimension__add("parent"); |
021191b3 ACM |
951 | sort_parent.elide = 1; |
952 | } else | |
b8e6d829 IM |
953 | exclude_other = 0; |
954 | ||
edc52dea IM |
955 | /* |
956 | * Any (unrecognized) arguments left? | |
957 | */ | |
958 | if (argc) | |
959 | usage_with_options(report_usage, options); | |
960 | ||
a930d2c0 IM |
961 | setup_pager(); |
962 | ||
021191b3 ACM |
963 | setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout); |
964 | setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout); | |
965 | setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout); | |
25903407 | 966 | |
52d422de ACM |
967 | if (field_sep && *field_sep == '.') { |
968 | fputs("'.' is the only non valid --field-separator argument\n", | |
969 | stderr); | |
970 | exit(129); | |
971 | } | |
972 | ||
53cb8bc2 IM |
973 | return __cmd_report(); |
974 | } |