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; |
4e4f06e4 | 37 | static bool use_callchain; |
8fa66bdc | 38 | |
e3d7e183 | 39 | static int show_nr_samples; |
97b07b69 | 40 | |
8d513270 BG |
41 | static int show_threads; |
42 | static struct perf_read_values show_threads_values; | |
43 | ||
9f866697 BG |
44 | static char default_pretty_printing_style[] = "normal"; |
45 | static char *pretty_printing_style = default_pretty_printing_style; | |
46 | ||
b8e6d829 | 47 | static int exclude_other = 1; |
be903885 | 48 | |
805d127d FW |
49 | static char callchain_default_opt[] = "fractal,0.5"; |
50 | ||
a4fb581b FW |
51 | static size_t |
52 | callchain__fprintf_left_margin(FILE *fp, int left_margin) | |
53 | { | |
54 | int i; | |
55 | int ret; | |
56 | ||
57 | ret = fprintf(fp, " "); | |
58 | ||
59 | for (i = 0; i < left_margin; i++) | |
60 | ret += fprintf(fp, " "); | |
61 | ||
62 | return ret; | |
63 | } | |
64 | ||
65 | static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask, | |
66 | int left_margin) | |
4eb3e478 FW |
67 | { |
68 | int i; | |
69 | size_t ret = 0; | |
70 | ||
a4fb581b | 71 | ret += callchain__fprintf_left_margin(fp, left_margin); |
4eb3e478 FW |
72 | |
73 | for (i = 0; i < depth; i++) | |
74 | if (depth_mask & (1 << i)) | |
75 | ret += fprintf(fp, "| "); | |
76 | else | |
77 | ret += fprintf(fp, " "); | |
78 | ||
79 | ret += fprintf(fp, "\n"); | |
80 | ||
81 | return ret; | |
82 | } | |
f55c5552 | 83 | static size_t |
4eb3e478 FW |
84 | ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth, |
85 | int depth_mask, int count, u64 total_samples, | |
a4fb581b | 86 | int hits, int left_margin) |
4eb3e478 FW |
87 | { |
88 | int i; | |
89 | size_t ret = 0; | |
90 | ||
a4fb581b | 91 | ret += callchain__fprintf_left_margin(fp, left_margin); |
4eb3e478 FW |
92 | for (i = 0; i < depth; i++) { |
93 | if (depth_mask & (1 << i)) | |
94 | ret += fprintf(fp, "|"); | |
95 | else | |
96 | ret += fprintf(fp, " "); | |
97 | if (!count && i == depth - 1) { | |
98 | double percent; | |
99 | ||
100 | percent = hits * 100.0 / total_samples; | |
24b57c69 | 101 | ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent); |
4eb3e478 FW |
102 | } else |
103 | ret += fprintf(fp, "%s", " "); | |
104 | } | |
105 | if (chain->sym) | |
106 | ret += fprintf(fp, "%s\n", chain->sym->name); | |
107 | else | |
108 | ret += fprintf(fp, "%p\n", (void *)(long)chain->ip); | |
109 | ||
110 | return ret; | |
111 | } | |
112 | ||
25446036 FW |
113 | static struct symbol *rem_sq_bracket; |
114 | static struct callchain_list rem_hits; | |
115 | ||
116 | static void init_rem_hits(void) | |
117 | { | |
118 | rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6); | |
119 | if (!rem_sq_bracket) { | |
120 | fprintf(stderr, "Not enough memory to display remaining hits\n"); | |
121 | return; | |
122 | } | |
123 | ||
124 | strcpy(rem_sq_bracket->name, "[...]"); | |
125 | rem_hits.sym = rem_sq_bracket; | |
126 | } | |
127 | ||
4eb3e478 | 128 | static size_t |
af0a6fa4 | 129 | __callchain__fprintf_graph(FILE *fp, struct callchain_node *self, |
a4fb581b FW |
130 | u64 total_samples, int depth, int depth_mask, |
131 | int left_margin) | |
4eb3e478 FW |
132 | { |
133 | struct rb_node *node, *next; | |
134 | struct callchain_node *child; | |
135 | struct callchain_list *chain; | |
136 | int new_depth_mask = depth_mask; | |
805d127d | 137 | u64 new_total; |
25446036 | 138 | u64 remaining; |
4eb3e478 FW |
139 | size_t ret = 0; |
140 | int i; | |
141 | ||
805d127d | 142 | if (callchain_param.mode == CHAIN_GRAPH_REL) |
1953287b | 143 | new_total = self->children_hit; |
805d127d FW |
144 | else |
145 | new_total = total_samples; | |
146 | ||
25446036 FW |
147 | remaining = new_total; |
148 | ||
4eb3e478 FW |
149 | node = rb_first(&self->rb_root); |
150 | while (node) { | |
25446036 FW |
151 | u64 cumul; |
152 | ||
4eb3e478 | 153 | child = rb_entry(node, struct callchain_node, rb_node); |
25446036 FW |
154 | cumul = cumul_hits(child); |
155 | remaining -= cumul; | |
4eb3e478 FW |
156 | |
157 | /* | |
158 | * The depth mask manages the output of pipes that show | |
159 | * the depth. We don't want to keep the pipes of the current | |
25446036 FW |
160 | * level for the last child of this depth. |
161 | * Except if we have remaining filtered hits. They will | |
162 | * supersede the last child | |
4eb3e478 FW |
163 | */ |
164 | next = rb_next(node); | |
25446036 | 165 | if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining)) |
4eb3e478 FW |
166 | new_depth_mask &= ~(1 << (depth - 1)); |
167 | ||
168 | /* | |
169 | * But we keep the older depth mask for the line seperator | |
170 | * to keep the level link until we reach the last child | |
171 | */ | |
a4fb581b FW |
172 | ret += ipchain__fprintf_graph_line(fp, depth, depth_mask, |
173 | left_margin); | |
4eb3e478 FW |
174 | i = 0; |
175 | list_for_each_entry(chain, &child->val, list) { | |
176 | if (chain->ip >= PERF_CONTEXT_MAX) | |
177 | continue; | |
178 | ret += ipchain__fprintf_graph(fp, chain, depth, | |
179 | new_depth_mask, i++, | |
805d127d | 180 | new_total, |
a4fb581b FW |
181 | cumul, |
182 | left_margin); | |
4eb3e478 | 183 | } |
af0a6fa4 FW |
184 | ret += __callchain__fprintf_graph(fp, child, new_total, |
185 | depth + 1, | |
a4fb581b FW |
186 | new_depth_mask | (1 << depth), |
187 | left_margin); | |
4eb3e478 FW |
188 | node = next; |
189 | } | |
190 | ||
25446036 FW |
191 | if (callchain_param.mode == CHAIN_GRAPH_REL && |
192 | remaining && remaining != new_total) { | |
193 | ||
194 | if (!rem_sq_bracket) | |
195 | return ret; | |
196 | ||
197 | new_depth_mask &= ~(1 << (depth - 1)); | |
198 | ||
199 | ret += ipchain__fprintf_graph(fp, &rem_hits, depth, | |
200 | new_depth_mask, 0, new_total, | |
a4fb581b | 201 | remaining, left_margin); |
25446036 FW |
202 | } |
203 | ||
4eb3e478 FW |
204 | return ret; |
205 | } | |
206 | ||
a4fb581b | 207 | |
af0a6fa4 FW |
208 | static size_t |
209 | callchain__fprintf_graph(FILE *fp, struct callchain_node *self, | |
a4fb581b | 210 | u64 total_samples, int left_margin) |
af0a6fa4 FW |
211 | { |
212 | struct callchain_list *chain; | |
a4fb581b | 213 | bool printed = false; |
af0a6fa4 FW |
214 | int i = 0; |
215 | int ret = 0; | |
216 | ||
217 | list_for_each_entry(chain, &self->val, list) { | |
218 | if (chain->ip >= PERF_CONTEXT_MAX) | |
219 | continue; | |
220 | ||
a4fb581b | 221 | if (!i++ && sort__first_dimension == SORT_SYM) |
af0a6fa4 FW |
222 | continue; |
223 | ||
a4fb581b FW |
224 | if (!printed) { |
225 | ret += callchain__fprintf_left_margin(fp, left_margin); | |
226 | ret += fprintf(fp, "|\n"); | |
227 | ret += callchain__fprintf_left_margin(fp, left_margin); | |
228 | ret += fprintf(fp, "---"); | |
229 | ||
230 | left_margin += 3; | |
231 | printed = true; | |
232 | } else | |
233 | ret += callchain__fprintf_left_margin(fp, left_margin); | |
234 | ||
af0a6fa4 | 235 | if (chain->sym) |
a4fb581b | 236 | ret += fprintf(fp, " %s\n", chain->sym->name); |
af0a6fa4 | 237 | else |
a4fb581b | 238 | ret += fprintf(fp, " %p\n", (void *)(long)chain->ip); |
af0a6fa4 FW |
239 | } |
240 | ||
a4fb581b | 241 | ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin); |
af0a6fa4 FW |
242 | |
243 | return ret; | |
244 | } | |
245 | ||
4eb3e478 FW |
246 | static size_t |
247 | callchain__fprintf_flat(FILE *fp, struct callchain_node *self, | |
248 | u64 total_samples) | |
f55c5552 FW |
249 | { |
250 | struct callchain_list *chain; | |
251 | size_t ret = 0; | |
252 | ||
253 | if (!self) | |
254 | return 0; | |
255 | ||
4eb3e478 | 256 | ret += callchain__fprintf_flat(fp, self->parent, total_samples); |
f55c5552 FW |
257 | |
258 | ||
4424961a FW |
259 | list_for_each_entry(chain, &self->val, list) { |
260 | if (chain->ip >= PERF_CONTEXT_MAX) | |
261 | continue; | |
262 | if (chain->sym) | |
263 | ret += fprintf(fp, " %s\n", chain->sym->name); | |
264 | else | |
265 | ret += fprintf(fp, " %p\n", | |
f37a291c | 266 | (void *)(long)chain->ip); |
4424961a | 267 | } |
f55c5552 FW |
268 | |
269 | return ret; | |
270 | } | |
271 | ||
272 | static size_t | |
273 | hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self, | |
a4fb581b | 274 | u64 total_samples, int left_margin) |
f55c5552 FW |
275 | { |
276 | struct rb_node *rb_node; | |
277 | struct callchain_node *chain; | |
278 | size_t ret = 0; | |
279 | ||
280 | rb_node = rb_first(&self->sorted_chain); | |
281 | while (rb_node) { | |
282 | double percent; | |
283 | ||
284 | chain = rb_entry(rb_node, struct callchain_node, rb_node); | |
285 | percent = chain->hit * 100.0 / total_samples; | |
805d127d FW |
286 | switch (callchain_param.mode) { |
287 | case CHAIN_FLAT: | |
24b57c69 FW |
288 | ret += percent_color_fprintf(fp, " %6.2f%%\n", |
289 | percent); | |
4eb3e478 | 290 | ret += callchain__fprintf_flat(fp, chain, total_samples); |
805d127d FW |
291 | break; |
292 | case CHAIN_GRAPH_ABS: /* Falldown */ | |
293 | case CHAIN_GRAPH_REL: | |
a4fb581b FW |
294 | ret += callchain__fprintf_graph(fp, chain, total_samples, |
295 | left_margin); | |
83a0944f | 296 | case CHAIN_NONE: |
805d127d FW |
297 | default: |
298 | break; | |
4eb3e478 | 299 | } |
f55c5552 FW |
300 | ret += fprintf(fp, "\n"); |
301 | rb_node = rb_next(rb_node); | |
302 | } | |
303 | ||
304 | return ret; | |
305 | } | |
306 | ||
4e4f06e4 ACM |
307 | static size_t hist_entry__fprintf(FILE *fp, struct hist_entry *self, |
308 | struct perf_session *session, | |
309 | u64 total_samples) | |
1aa16738 PZ |
310 | { |
311 | struct sort_entry *se; | |
312 | size_t ret; | |
313 | ||
b8e6d829 IM |
314 | if (exclude_other && !self->parent) |
315 | return 0; | |
316 | ||
1e11fd82 | 317 | if (total_samples) |
52d422de ACM |
318 | ret = percent_color_fprintf(fp, |
319 | field_sep ? "%.2f" : " %6.2f%%", | |
320 | (self->count * 100.0) / total_samples); | |
1e11fd82 | 321 | else |
52d422de | 322 | ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count); |
1aa16738 | 323 | |
e3d7e183 ACM |
324 | if (show_nr_samples) { |
325 | if (field_sep) | |
326 | fprintf(fp, "%c%lld", *field_sep, self->count); | |
327 | else | |
328 | fprintf(fp, "%11lld", self->count); | |
329 | } | |
1aa16738 | 330 | |
71dd8945 | 331 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
021191b3 | 332 | if (se->elide) |
b8e6d829 IM |
333 | continue; |
334 | ||
52d422de ACM |
335 | fprintf(fp, "%s", field_sep ?: " "); |
336 | ret += se->print(fp, self, se->width ? *se->width : 0); | |
71dd8945 | 337 | } |
1aa16738 PZ |
338 | |
339 | ret += fprintf(fp, "\n"); | |
340 | ||
4e4f06e4 | 341 | if (session->use_callchain) { |
a4fb581b FW |
342 | int left_margin = 0; |
343 | ||
344 | if (sort__first_dimension == SORT_COMM) { | |
345 | se = list_first_entry(&hist_entry__sort_list, typeof(*se), | |
346 | list); | |
347 | left_margin = se->width ? *se->width : 0; | |
348 | left_margin -= thread__comm_len(self->thread); | |
349 | } | |
350 | ||
351 | hist_entry_callchain__fprintf(fp, self, total_samples, | |
352 | left_margin); | |
353 | } | |
f55c5552 | 354 | |
1aa16738 PZ |
355 | return ret; |
356 | } | |
357 | ||
6e7d6fdc PZ |
358 | /* |
359 | * | |
360 | */ | |
361 | ||
52d422de ACM |
362 | static void dso__calc_col_width(struct dso *self) |
363 | { | |
655000e7 ACM |
364 | if (!symbol_conf.col_width_list_str && !field_sep && |
365 | (!symbol_conf.dso_list || | |
366 | strlist__has_entry(symbol_conf.dso_list, self->name))) { | |
52d422de ACM |
367 | unsigned int slen = strlen(self->name); |
368 | if (slen > dsos__col_width) | |
369 | dsos__col_width = slen; | |
370 | } | |
371 | ||
372 | self->slen_calculated = 1; | |
373 | } | |
374 | ||
5b447a6a | 375 | static void thread__comm_adjust(struct thread *self) |
4273b005 | 376 | { |
5b447a6a | 377 | char *comm = self->comm; |
4273b005 | 378 | |
655000e7 ACM |
379 | if (!symbol_conf.col_width_list_str && !field_sep && |
380 | (!symbol_conf.comm_list || | |
381 | strlist__has_entry(symbol_conf.comm_list, comm))) { | |
4273b005 FW |
382 | unsigned int slen = strlen(comm); |
383 | ||
384 | if (slen > comms__col_width) { | |
385 | comms__col_width = slen; | |
386 | threads__col_width = slen + 6; | |
387 | } | |
388 | } | |
5b447a6a FW |
389 | } |
390 | ||
391 | static int thread__set_comm_adjust(struct thread *self, const char *comm) | |
392 | { | |
393 | int ret = thread__set_comm(self, comm); | |
394 | ||
395 | if (ret) | |
396 | return ret; | |
397 | ||
398 | thread__comm_adjust(self); | |
4273b005 FW |
399 | |
400 | return 0; | |
401 | } | |
402 | ||
1aa16738 PZ |
403 | /* |
404 | * collect histogram counts | |
405 | */ | |
406 | ||
4e4f06e4 ACM |
407 | static int perf_session__add_hist_entry(struct perf_session *self, |
408 | struct addr_location *al, | |
409 | struct ip_callchain *chain, u64 count) | |
8fa66bdc | 410 | { |
9735abf1 ACM |
411 | struct symbol **syms = NULL, *parent = NULL; |
412 | bool hit; | |
e7fb08b1 | 413 | struct hist_entry *he; |
e7fb08b1 | 414 | |
4e4f06e4 | 415 | if ((sort__has_parent || self->use_callchain) && chain) |
a328626b ACM |
416 | syms = perf_session__resolve_callchain(self, al->thread, |
417 | chain, &parent); | |
4e4f06e4 | 418 | he = __perf_session__add_hist_entry(self, al, parent, count, &hit); |
9735abf1 ACM |
419 | if (he == NULL) |
420 | return -ENOMEM; | |
e7fb08b1 | 421 | |
9735abf1 ACM |
422 | if (hit) |
423 | he->count += count; | |
e7fb08b1 | 424 | |
4e4f06e4 | 425 | if (self->use_callchain) { |
9735abf1 ACM |
426 | if (!hit) |
427 | callchain_init(&he->callchain); | |
4424961a FW |
428 | append_chain(&he->callchain, chain, syms); |
429 | free(syms); | |
f55c5552 | 430 | } |
e7fb08b1 PZ |
431 | |
432 | return 0; | |
8fa66bdc ACM |
433 | } |
434 | ||
4e4f06e4 ACM |
435 | static size_t perf_session__fprintf_hist_entries(struct perf_session *self, |
436 | u64 total_samples, FILE *fp) | |
3a4b8cc7 | 437 | { |
e7fb08b1 | 438 | struct hist_entry *pos; |
2d65537e | 439 | struct sort_entry *se; |
3a4b8cc7 ACM |
440 | struct rb_node *nd; |
441 | size_t ret = 0; | |
52d422de | 442 | unsigned int width; |
655000e7 | 443 | char *col_width = symbol_conf.col_width_list_str; |
9f866697 BG |
444 | int raw_printing_style; |
445 | ||
446 | raw_printing_style = !strcmp(pretty_printing_style, "raw"); | |
3a4b8cc7 | 447 | |
25446036 FW |
448 | init_rem_hits(); |
449 | ||
021191b3 | 450 | fprintf(fp, "# Samples: %Ld\n", (u64)total_samples); |
ca8cdeef PZ |
451 | fprintf(fp, "#\n"); |
452 | ||
453 | fprintf(fp, "# Overhead"); | |
e3d7e183 ACM |
454 | if (show_nr_samples) { |
455 | if (field_sep) | |
456 | fprintf(fp, "%cSamples", *field_sep); | |
457 | else | |
458 | fputs(" Samples ", fp); | |
459 | } | |
b8e6d829 | 460 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
021191b3 | 461 | if (se->elide) |
b8e6d829 | 462 | continue; |
52d422de ACM |
463 | if (field_sep) { |
464 | fprintf(fp, "%c%s", *field_sep, se->header); | |
b8e6d829 | 465 | continue; |
52d422de ACM |
466 | } |
467 | width = strlen(se->header); | |
468 | if (se->width) { | |
655000e7 | 469 | if (symbol_conf.col_width_list_str) { |
52d422de ACM |
470 | if (col_width) { |
471 | *se->width = atoi(col_width); | |
472 | col_width = strchr(col_width, ','); | |
473 | if (col_width) | |
474 | ++col_width; | |
475 | } | |
476 | } | |
477 | width = *se->width = max(*se->width, width); | |
478 | } | |
479 | fprintf(fp, " %*s", width, se->header); | |
b8e6d829 | 480 | } |
ca8cdeef PZ |
481 | fprintf(fp, "\n"); |
482 | ||
52d422de ACM |
483 | if (field_sep) |
484 | goto print_entries; | |
485 | ||
ca8cdeef | 486 | fprintf(fp, "# ........"); |
e3d7e183 ACM |
487 | if (show_nr_samples) |
488 | fprintf(fp, " .........."); | |
2d65537e | 489 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
f37a291c | 490 | unsigned int i; |
ca8cdeef | 491 | |
021191b3 | 492 | if (se->elide) |
b8e6d829 IM |
493 | continue; |
494 | ||
4593bba8 | 495 | fprintf(fp, " "); |
52d422de ACM |
496 | if (se->width) |
497 | width = *se->width; | |
498 | else | |
499 | width = strlen(se->header); | |
500 | for (i = 0; i < width; i++) | |
ca8cdeef | 501 | fprintf(fp, "."); |
2d65537e | 502 | } |
ca8cdeef PZ |
503 | fprintf(fp, "\n"); |
504 | ||
505 | fprintf(fp, "#\n"); | |
2d65537e | 506 | |
52d422de | 507 | print_entries: |
4e4f06e4 | 508 | for (nd = rb_first(&self->hists); nd; nd = rb_next(nd)) { |
e7fb08b1 | 509 | pos = rb_entry(nd, struct hist_entry, rb_node); |
4e4f06e4 | 510 | ret += hist_entry__fprintf(fp, pos, self, total_samples); |
3a4b8cc7 ACM |
511 | } |
512 | ||
b8e6d829 IM |
513 | if (sort_order == default_sort_order && |
514 | parent_pattern == default_parent_pattern) { | |
bd74137e | 515 | fprintf(fp, "#\n"); |
114cfab2 | 516 | fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n"); |
bd74137e IM |
517 | fprintf(fp, "#\n"); |
518 | } | |
71dd8945 | 519 | fprintf(fp, "\n"); |
bd74137e | 520 | |
25446036 FW |
521 | free(rem_sq_bracket); |
522 | ||
8d513270 | 523 | if (show_threads) |
9f866697 BG |
524 | perf_read_values_display(fp, &show_threads_values, |
525 | raw_printing_style); | |
8d513270 | 526 | |
3a4b8cc7 ACM |
527 | return ret; |
528 | } | |
529 | ||
2a0a50fe | 530 | static int validate_chain(struct ip_callchain *chain, event_t *event) |
7522060c IM |
531 | { |
532 | unsigned int chain_size; | |
533 | ||
7522060c IM |
534 | chain_size = event->header.size; |
535 | chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event; | |
536 | ||
9cffa8d5 | 537 | if (chain->nr*sizeof(u64) > chain_size) |
7522060c IM |
538 | return -1; |
539 | ||
540 | return 0; | |
541 | } | |
542 | ||
b3165f41 | 543 | static int process_sample_event(event_t *event, struct perf_session *session) |
75051724 | 544 | { |
180f95e2 | 545 | struct sample_data data; |
d8db1b57 | 546 | int cpumode; |
1ed091c4 | 547 | struct addr_location al; |
180f95e2 | 548 | struct thread *thread; |
6baa0a5a | 549 | |
180f95e2 OH |
550 | memset(&data, 0, sizeof(data)); |
551 | data.period = 1; | |
552 | ||
c019879b | 553 | event__parse_sample(event, session->sample_type, &data); |
ea1900e5 | 554 | |
62daacb5 | 555 | dump_printf("(IP, %d): %d/%d: %p period: %Ld\n", |
75051724 | 556 | event->header.misc, |
180f95e2 OH |
557 | data.pid, data.tid, |
558 | (void *)(long)data.ip, | |
559 | (long long)data.period); | |
75051724 | 560 | |
c019879b | 561 | if (session->sample_type & PERF_SAMPLE_CALLCHAIN) { |
f37a291c | 562 | unsigned int i; |
3efa1cc9 | 563 | |
180f95e2 | 564 | dump_printf("... chain: nr:%Lu\n", data.callchain->nr); |
3efa1cc9 | 565 | |
180f95e2 | 566 | if (validate_chain(data.callchain, event) < 0) { |
6beba7ad ACM |
567 | pr_debug("call-chain problem with event, " |
568 | "skipping it.\n"); | |
7522060c IM |
569 | return 0; |
570 | } | |
571 | ||
572 | if (dump_trace) { | |
180f95e2 OH |
573 | for (i = 0; i < data.callchain->nr; i++) |
574 | dump_printf("..... %2d: %016Lx\n", | |
575 | i, data.callchain->ips[i]); | |
3efa1cc9 IM |
576 | } |
577 | } | |
578 | ||
b3165f41 | 579 | thread = perf_session__findnew(session, data.pid); |
75051724 | 580 | if (thread == NULL) { |
6beba7ad | 581 | pr_debug("problem processing %d event, skipping it.\n", |
75051724 IM |
582 | event->header.type); |
583 | return -1; | |
584 | } | |
e7fb08b1 | 585 | |
f39cdf25 JL |
586 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); |
587 | ||
655000e7 ACM |
588 | if (symbol_conf.comm_list && |
589 | !strlist__has_entry(symbol_conf.comm_list, thread->comm)) | |
cc8b88b1 ACM |
590 | return 0; |
591 | ||
cdd6c482 | 592 | cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
d8db1b57 | 593 | |
4aa65636 | 594 | thread__find_addr_location(thread, session, cpumode, |
180f95e2 | 595 | MAP__FUNCTION, data.ip, &al, NULL); |
1ed091c4 ACM |
596 | /* |
597 | * We have to do this here as we may have a dso with no symbol hit that | |
598 | * has a name longer than the ones with symbols sampled. | |
599 | */ | |
600 | if (al.map && !sort_dso.elide && !al.map->dso->slen_calculated) | |
601 | dso__calc_col_width(al.map->dso); | |
8fa66bdc | 602 | |
655000e7 | 603 | if (symbol_conf.dso_list && |
1ed091c4 | 604 | (!al.map || !al.map->dso || |
655000e7 | 605 | !(strlist__has_entry(symbol_conf.dso_list, al.map->dso->short_name) || |
1ed091c4 | 606 | (al.map->dso->short_name != al.map->dso->long_name && |
655000e7 | 607 | strlist__has_entry(symbol_conf.dso_list, al.map->dso->long_name))))) |
ec218fc4 | 608 | return 0; |
25903407 | 609 | |
655000e7 ACM |
610 | if (symbol_conf.sym_list && al.sym && |
611 | !strlist__has_entry(symbol_conf.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), |
655000e7 | 828 | OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]", |
25903407 | 829 | "only consider symbols in these dsos"), |
655000e7 | 830 | OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]", |
cc8b88b1 | 831 | "only consider symbols in these comms"), |
655000e7 | 832 | OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]", |
7bec7a91 | 833 | "only consider these symbols"), |
655000e7 | 834 | OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str, |
52d422de ACM |
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 | ||
655000e7 ACM |
843 | static void sort_entry__setup_elide(struct sort_entry *self, |
844 | struct strlist *list, | |
845 | const char *list_name, FILE *fp) | |
cc8b88b1 | 846 | { |
655000e7 ACM |
847 | if (list && strlist__nr_entries(list) == 1) { |
848 | fprintf(fp, "# %s: %s\n", list_name, strlist__entry(list, 0)->s); | |
849 | self->elide = true; | |
cc8b88b1 ACM |
850 | } |
851 | } | |
852 | ||
f37a291c | 853 | int cmd_report(int argc, const char **argv, const char *prefix __used) |
53cb8bc2 | 854 | { |
655000e7 ACM |
855 | argc = parse_options(argc, argv, options, report_usage, 0); |
856 | ||
857 | setup_pager(); | |
858 | ||
75be6cf4 | 859 | if (symbol__init() < 0) |
b32d133a | 860 | return -1; |
53cb8bc2 | 861 | |
c8829c7a | 862 | setup_sorting(report_usage, options); |
1aa16738 | 863 | |
021191b3 | 864 | if (parent_pattern != default_parent_pattern) { |
b8e6d829 | 865 | sort_dimension__add("parent"); |
021191b3 ACM |
866 | sort_parent.elide = 1; |
867 | } else | |
b8e6d829 IM |
868 | exclude_other = 0; |
869 | ||
edc52dea IM |
870 | /* |
871 | * Any (unrecognized) arguments left? | |
872 | */ | |
873 | if (argc) | |
874 | usage_with_options(report_usage, options); | |
875 | ||
655000e7 ACM |
876 | sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout); |
877 | sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout); | |
878 | sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout); | |
25903407 | 879 | |
52d422de ACM |
880 | if (field_sep && *field_sep == '.') { |
881 | fputs("'.' is the only non valid --field-separator argument\n", | |
882 | stderr); | |
883 | exit(129); | |
884 | } | |
885 | ||
53cb8bc2 IM |
886 | return __cmd_report(); |
887 | } |