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