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