diff options
-rw-r--r-- | tools/perf/Makefile | 3 | ||||
-rw-r--r-- | tools/perf/ui/stdio/hist.c | 648 | ||||
-rw-r--r-- | tools/perf/util/hist.c | 677 | ||||
-rw-r--r-- | tools/perf/util/hist.h | 2 |
4 files changed, 669 insertions, 661 deletions
diff --git a/tools/perf/Makefile b/tools/perf/Makefile index 77e7ae3ef1b4..6bd888d04b6e 100644 --- a/tools/perf/Makefile +++ b/tools/perf/Makefile | |||
@@ -404,11 +404,10 @@ LIB_OBJS += $(OUTPUT)util/target.o | |||
404 | LIB_OBJS += $(OUTPUT)util/rblist.o | 404 | LIB_OBJS += $(OUTPUT)util/rblist.o |
405 | LIB_OBJS += $(OUTPUT)util/intlist.o | 405 | LIB_OBJS += $(OUTPUT)util/intlist.o |
406 | LIB_OBJS += $(OUTPUT)ui/helpline.o | 406 | LIB_OBJS += $(OUTPUT)ui/helpline.o |
407 | LIB_OBJS += $(OUTPUT)ui/stdio/hist.o | ||
407 | 408 | ||
408 | BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o | 409 | BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o |
409 | |||
410 | BUILTIN_OBJS += $(OUTPUT)builtin-bench.o | 410 | BUILTIN_OBJS += $(OUTPUT)builtin-bench.o |
411 | |||
412 | # Benchmark modules | 411 | # Benchmark modules |
413 | BUILTIN_OBJS += $(OUTPUT)bench/sched-messaging.o | 412 | BUILTIN_OBJS += $(OUTPUT)bench/sched-messaging.o |
414 | BUILTIN_OBJS += $(OUTPUT)bench/sched-pipe.o | 413 | BUILTIN_OBJS += $(OUTPUT)bench/sched-pipe.o |
diff --git a/tools/perf/ui/stdio/hist.c b/tools/perf/ui/stdio/hist.c new file mode 100644 index 000000000000..7881d625e17a --- /dev/null +++ b/tools/perf/ui/stdio/hist.c | |||
@@ -0,0 +1,648 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <math.h> | ||
3 | |||
4 | #include "../../util/util.h" | ||
5 | #include "../../util/hist.h" | ||
6 | #include "../../util/sort.h" | ||
7 | |||
8 | |||
9 | static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin) | ||
10 | { | ||
11 | int i; | ||
12 | int ret = fprintf(fp, " "); | ||
13 | |||
14 | for (i = 0; i < left_margin; i++) | ||
15 | ret += fprintf(fp, " "); | ||
16 | |||
17 | return ret; | ||
18 | } | ||
19 | |||
20 | static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask, | ||
21 | int left_margin) | ||
22 | { | ||
23 | int i; | ||
24 | size_t ret = callchain__fprintf_left_margin(fp, left_margin); | ||
25 | |||
26 | for (i = 0; i < depth; i++) | ||
27 | if (depth_mask & (1 << i)) | ||
28 | ret += fprintf(fp, "| "); | ||
29 | else | ||
30 | ret += fprintf(fp, " "); | ||
31 | |||
32 | ret += fprintf(fp, "\n"); | ||
33 | |||
34 | return ret; | ||
35 | } | ||
36 | |||
37 | static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, | ||
38 | int depth, int depth_mask, int period, | ||
39 | u64 total_samples, u64 hits, | ||
40 | int left_margin) | ||
41 | { | ||
42 | int i; | ||
43 | size_t ret = 0; | ||
44 | |||
45 | ret += callchain__fprintf_left_margin(fp, left_margin); | ||
46 | for (i = 0; i < depth; i++) { | ||
47 | if (depth_mask & (1 << i)) | ||
48 | ret += fprintf(fp, "|"); | ||
49 | else | ||
50 | ret += fprintf(fp, " "); | ||
51 | if (!period && i == depth - 1) { | ||
52 | double percent; | ||
53 | |||
54 | percent = hits * 100.0 / total_samples; | ||
55 | ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent); | ||
56 | } else | ||
57 | ret += fprintf(fp, "%s", " "); | ||
58 | } | ||
59 | if (chain->ms.sym) | ||
60 | ret += fprintf(fp, "%s\n", chain->ms.sym->name); | ||
61 | else | ||
62 | ret += fprintf(fp, "0x%0" PRIx64 "\n", chain->ip); | ||
63 | |||
64 | return ret; | ||
65 | } | ||
66 | |||
67 | static struct symbol *rem_sq_bracket; | ||
68 | static struct callchain_list rem_hits; | ||
69 | |||
70 | static void init_rem_hits(void) | ||
71 | { | ||
72 | rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6); | ||
73 | if (!rem_sq_bracket) { | ||
74 | fprintf(stderr, "Not enough memory to display remaining hits\n"); | ||
75 | return; | ||
76 | } | ||
77 | |||
78 | strcpy(rem_sq_bracket->name, "[...]"); | ||
79 | rem_hits.ms.sym = rem_sq_bracket; | ||
80 | } | ||
81 | |||
82 | static size_t __callchain__fprintf_graph(FILE *fp, struct rb_root *root, | ||
83 | u64 total_samples, int depth, | ||
84 | int depth_mask, int left_margin) | ||
85 | { | ||
86 | struct rb_node *node, *next; | ||
87 | struct callchain_node *child; | ||
88 | struct callchain_list *chain; | ||
89 | int new_depth_mask = depth_mask; | ||
90 | u64 remaining; | ||
91 | size_t ret = 0; | ||
92 | int i; | ||
93 | uint entries_printed = 0; | ||
94 | |||
95 | remaining = total_samples; | ||
96 | |||
97 | node = rb_first(root); | ||
98 | while (node) { | ||
99 | u64 new_total; | ||
100 | u64 cumul; | ||
101 | |||
102 | child = rb_entry(node, struct callchain_node, rb_node); | ||
103 | cumul = callchain_cumul_hits(child); | ||
104 | remaining -= cumul; | ||
105 | |||
106 | /* | ||
107 | * The depth mask manages the output of pipes that show | ||
108 | * the depth. We don't want to keep the pipes of the current | ||
109 | * level for the last child of this depth. | ||
110 | * Except if we have remaining filtered hits. They will | ||
111 | * supersede the last child | ||
112 | */ | ||
113 | next = rb_next(node); | ||
114 | if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining)) | ||
115 | new_depth_mask &= ~(1 << (depth - 1)); | ||
116 | |||
117 | /* | ||
118 | * But we keep the older depth mask for the line separator | ||
119 | * to keep the level link until we reach the last child | ||
120 | */ | ||
121 | ret += ipchain__fprintf_graph_line(fp, depth, depth_mask, | ||
122 | left_margin); | ||
123 | i = 0; | ||
124 | list_for_each_entry(chain, &child->val, list) { | ||
125 | ret += ipchain__fprintf_graph(fp, chain, depth, | ||
126 | new_depth_mask, i++, | ||
127 | total_samples, | ||
128 | cumul, | ||
129 | left_margin); | ||
130 | } | ||
131 | |||
132 | if (callchain_param.mode == CHAIN_GRAPH_REL) | ||
133 | new_total = child->children_hit; | ||
134 | else | ||
135 | new_total = total_samples; | ||
136 | |||
137 | ret += __callchain__fprintf_graph(fp, &child->rb_root, new_total, | ||
138 | depth + 1, | ||
139 | new_depth_mask | (1 << depth), | ||
140 | left_margin); | ||
141 | node = next; | ||
142 | if (++entries_printed == callchain_param.print_limit) | ||
143 | break; | ||
144 | } | ||
145 | |||
146 | if (callchain_param.mode == CHAIN_GRAPH_REL && | ||
147 | remaining && remaining != total_samples) { | ||
148 | |||
149 | if (!rem_sq_bracket) | ||
150 | return ret; | ||
151 | |||
152 | new_depth_mask &= ~(1 << (depth - 1)); | ||
153 | ret += ipchain__fprintf_graph(fp, &rem_hits, depth, | ||
154 | new_depth_mask, 0, total_samples, | ||
155 | remaining, left_margin); | ||
156 | } | ||
157 | |||
158 | return ret; | ||
159 | } | ||
160 | |||
161 | static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root, | ||
162 | u64 total_samples, int left_margin) | ||
163 | { | ||
164 | struct callchain_node *cnode; | ||
165 | struct callchain_list *chain; | ||
166 | u32 entries_printed = 0; | ||
167 | bool printed = false; | ||
168 | struct rb_node *node; | ||
169 | int i = 0; | ||
170 | int ret = 0; | ||
171 | |||
172 | /* | ||
173 | * If have one single callchain root, don't bother printing | ||
174 | * its percentage (100 % in fractal mode and the same percentage | ||
175 | * than the hist in graph mode). This also avoid one level of column. | ||
176 | */ | ||
177 | node = rb_first(root); | ||
178 | if (node && !rb_next(node)) { | ||
179 | cnode = rb_entry(node, struct callchain_node, rb_node); | ||
180 | list_for_each_entry(chain, &cnode->val, list) { | ||
181 | /* | ||
182 | * If we sort by symbol, the first entry is the same than | ||
183 | * the symbol. No need to print it otherwise it appears as | ||
184 | * displayed twice. | ||
185 | */ | ||
186 | if (!i++ && sort__first_dimension == SORT_SYM) | ||
187 | continue; | ||
188 | if (!printed) { | ||
189 | ret += callchain__fprintf_left_margin(fp, left_margin); | ||
190 | ret += fprintf(fp, "|\n"); | ||
191 | ret += callchain__fprintf_left_margin(fp, left_margin); | ||
192 | ret += fprintf(fp, "---"); | ||
193 | left_margin += 3; | ||
194 | printed = true; | ||
195 | } else | ||
196 | ret += callchain__fprintf_left_margin(fp, left_margin); | ||
197 | |||
198 | if (chain->ms.sym) | ||
199 | ret += fprintf(fp, " %s\n", chain->ms.sym->name); | ||
200 | else | ||
201 | ret += fprintf(fp, " %p\n", (void *)(long)chain->ip); | ||
202 | |||
203 | if (++entries_printed == callchain_param.print_limit) | ||
204 | break; | ||
205 | } | ||
206 | root = &cnode->rb_root; | ||
207 | } | ||
208 | |||
209 | ret += __callchain__fprintf_graph(fp, root, total_samples, | ||
210 | 1, 1, left_margin); | ||
211 | ret += fprintf(fp, "\n"); | ||
212 | |||
213 | return ret; | ||
214 | } | ||
215 | |||
216 | static size_t __callchain__fprintf_flat(FILE *fp, | ||
217 | struct callchain_node *self, | ||
218 | u64 total_samples) | ||
219 | { | ||
220 | struct callchain_list *chain; | ||
221 | size_t ret = 0; | ||
222 | |||
223 | if (!self) | ||
224 | return 0; | ||
225 | |||
226 | ret += __callchain__fprintf_flat(fp, self->parent, total_samples); | ||
227 | |||
228 | |||
229 | list_for_each_entry(chain, &self->val, list) { | ||
230 | if (chain->ip >= PERF_CONTEXT_MAX) | ||
231 | continue; | ||
232 | if (chain->ms.sym) | ||
233 | ret += fprintf(fp, " %s\n", chain->ms.sym->name); | ||
234 | else | ||
235 | ret += fprintf(fp, " %p\n", | ||
236 | (void *)(long)chain->ip); | ||
237 | } | ||
238 | |||
239 | return ret; | ||
240 | } | ||
241 | |||
242 | static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *self, | ||
243 | u64 total_samples) | ||
244 | { | ||
245 | size_t ret = 0; | ||
246 | u32 entries_printed = 0; | ||
247 | struct rb_node *rb_node; | ||
248 | struct callchain_node *chain; | ||
249 | |||
250 | rb_node = rb_first(self); | ||
251 | while (rb_node) { | ||
252 | double percent; | ||
253 | |||
254 | chain = rb_entry(rb_node, struct callchain_node, rb_node); | ||
255 | percent = chain->hit * 100.0 / total_samples; | ||
256 | |||
257 | ret = percent_color_fprintf(fp, " %6.2f%%\n", percent); | ||
258 | ret += __callchain__fprintf_flat(fp, chain, total_samples); | ||
259 | ret += fprintf(fp, "\n"); | ||
260 | if (++entries_printed == callchain_param.print_limit) | ||
261 | break; | ||
262 | |||
263 | rb_node = rb_next(rb_node); | ||
264 | } | ||
265 | |||
266 | return ret; | ||
267 | } | ||
268 | |||
269 | static size_t hist_entry_callchain__fprintf(struct hist_entry *he, | ||
270 | u64 total_samples, int left_margin, | ||
271 | FILE *fp) | ||
272 | { | ||
273 | switch (callchain_param.mode) { | ||
274 | case CHAIN_GRAPH_REL: | ||
275 | return callchain__fprintf_graph(fp, &he->sorted_chain, he->period, | ||
276 | left_margin); | ||
277 | break; | ||
278 | case CHAIN_GRAPH_ABS: | ||
279 | return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples, | ||
280 | left_margin); | ||
281 | break; | ||
282 | case CHAIN_FLAT: | ||
283 | return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples); | ||
284 | break; | ||
285 | case CHAIN_NONE: | ||
286 | break; | ||
287 | default: | ||
288 | pr_err("Bad callchain mode\n"); | ||
289 | } | ||
290 | |||
291 | return 0; | ||
292 | } | ||
293 | |||
294 | static int hist_entry__pcnt_snprintf(struct hist_entry *he, char *s, | ||
295 | size_t size, struct hists *pair_hists, | ||
296 | bool show_displacement, long displacement, | ||
297 | bool color, u64 total_period) | ||
298 | { | ||
299 | u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us; | ||
300 | u64 nr_events; | ||
301 | const char *sep = symbol_conf.field_sep; | ||
302 | int ret; | ||
303 | |||
304 | if (symbol_conf.exclude_other && !he->parent) | ||
305 | return 0; | ||
306 | |||
307 | if (pair_hists) { | ||
308 | period = he->pair ? he->pair->period : 0; | ||
309 | nr_events = he->pair ? he->pair->nr_events : 0; | ||
310 | total = pair_hists->stats.total_period; | ||
311 | period_sys = he->pair ? he->pair->period_sys : 0; | ||
312 | period_us = he->pair ? he->pair->period_us : 0; | ||
313 | period_guest_sys = he->pair ? he->pair->period_guest_sys : 0; | ||
314 | period_guest_us = he->pair ? he->pair->period_guest_us : 0; | ||
315 | } else { | ||
316 | period = he->period; | ||
317 | nr_events = he->nr_events; | ||
318 | total = total_period; | ||
319 | period_sys = he->period_sys; | ||
320 | period_us = he->period_us; | ||
321 | period_guest_sys = he->period_guest_sys; | ||
322 | period_guest_us = he->period_guest_us; | ||
323 | } | ||
324 | |||
325 | if (total) { | ||
326 | if (color) | ||
327 | ret = percent_color_snprintf(s, size, | ||
328 | sep ? "%.2f" : " %6.2f%%", | ||
329 | (period * 100.0) / total); | ||
330 | else | ||
331 | ret = scnprintf(s, size, sep ? "%.2f" : " %6.2f%%", | ||
332 | (period * 100.0) / total); | ||
333 | if (symbol_conf.show_cpu_utilization) { | ||
334 | ret += percent_color_snprintf(s + ret, size - ret, | ||
335 | sep ? "%.2f" : " %6.2f%%", | ||
336 | (period_sys * 100.0) / total); | ||
337 | ret += percent_color_snprintf(s + ret, size - ret, | ||
338 | sep ? "%.2f" : " %6.2f%%", | ||
339 | (period_us * 100.0) / total); | ||
340 | if (perf_guest) { | ||
341 | ret += percent_color_snprintf(s + ret, | ||
342 | size - ret, | ||
343 | sep ? "%.2f" : " %6.2f%%", | ||
344 | (period_guest_sys * 100.0) / | ||
345 | total); | ||
346 | ret += percent_color_snprintf(s + ret, | ||
347 | size - ret, | ||
348 | sep ? "%.2f" : " %6.2f%%", | ||
349 | (period_guest_us * 100.0) / | ||
350 | total); | ||
351 | } | ||
352 | } | ||
353 | } else | ||
354 | ret = scnprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period); | ||
355 | |||
356 | if (symbol_conf.show_nr_samples) { | ||
357 | if (sep) | ||
358 | ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events); | ||
359 | else | ||
360 | ret += scnprintf(s + ret, size - ret, "%11" PRIu64, nr_events); | ||
361 | } | ||
362 | |||
363 | if (symbol_conf.show_total_period) { | ||
364 | if (sep) | ||
365 | ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period); | ||
366 | else | ||
367 | ret += scnprintf(s + ret, size - ret, " %12" PRIu64, period); | ||
368 | } | ||
369 | |||
370 | if (pair_hists) { | ||
371 | char bf[32]; | ||
372 | double old_percent = 0, new_percent = 0, diff; | ||
373 | |||
374 | if (total > 0) | ||
375 | old_percent = (period * 100.0) / total; | ||
376 | if (total_period > 0) | ||
377 | new_percent = (he->period * 100.0) / total_period; | ||
378 | |||
379 | diff = new_percent - old_percent; | ||
380 | |||
381 | if (fabs(diff) >= 0.01) | ||
382 | scnprintf(bf, sizeof(bf), "%+4.2F%%", diff); | ||
383 | else | ||
384 | scnprintf(bf, sizeof(bf), " "); | ||
385 | |||
386 | if (sep) | ||
387 | ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf); | ||
388 | else | ||
389 | ret += scnprintf(s + ret, size - ret, "%11.11s", bf); | ||
390 | |||
391 | if (show_displacement) { | ||
392 | if (displacement) | ||
393 | scnprintf(bf, sizeof(bf), "%+4ld", displacement); | ||
394 | else | ||
395 | scnprintf(bf, sizeof(bf), " "); | ||
396 | |||
397 | if (sep) | ||
398 | ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf); | ||
399 | else | ||
400 | ret += scnprintf(s + ret, size - ret, "%6.6s", bf); | ||
401 | } | ||
402 | } | ||
403 | |||
404 | return ret; | ||
405 | } | ||
406 | |||
407 | int hist_entry__snprintf(struct hist_entry *he, char *s, size_t size, | ||
408 | struct hists *hists) | ||
409 | { | ||
410 | const char *sep = symbol_conf.field_sep; | ||
411 | struct sort_entry *se; | ||
412 | int ret = 0; | ||
413 | |||
414 | list_for_each_entry(se, &hist_entry__sort_list, list) { | ||
415 | if (se->elide) | ||
416 | continue; | ||
417 | |||
418 | ret += scnprintf(s + ret, size - ret, "%s", sep ?: " "); | ||
419 | ret += se->se_snprintf(he, s + ret, size - ret, | ||
420 | hists__col_len(hists, se->se_width_idx)); | ||
421 | } | ||
422 | |||
423 | return ret; | ||
424 | } | ||
425 | |||
426 | static int hist_entry__fprintf(struct hist_entry *he, size_t size, | ||
427 | struct hists *hists, struct hists *pair_hists, | ||
428 | bool show_displacement, long displacement, | ||
429 | u64 total_period, FILE *fp) | ||
430 | { | ||
431 | char bf[512]; | ||
432 | int ret; | ||
433 | |||
434 | if (size == 0 || size > sizeof(bf)) | ||
435 | size = sizeof(bf); | ||
436 | |||
437 | ret = hist_entry__pcnt_snprintf(he, bf, size, pair_hists, | ||
438 | show_displacement, displacement, | ||
439 | true, total_period); | ||
440 | hist_entry__snprintf(he, bf + ret, size - ret, hists); | ||
441 | return fprintf(fp, "%s\n", bf); | ||
442 | } | ||
443 | |||
444 | static size_t hist_entry__fprintf_callchain(struct hist_entry *he, | ||
445 | struct hists *hists, | ||
446 | u64 total_period, FILE *fp) | ||
447 | { | ||
448 | int left_margin = 0; | ||
449 | |||
450 | if (sort__first_dimension == SORT_COMM) { | ||
451 | struct sort_entry *se = list_first_entry(&hist_entry__sort_list, | ||
452 | typeof(*se), list); | ||
453 | left_margin = hists__col_len(hists, se->se_width_idx); | ||
454 | left_margin -= thread__comm_len(he->thread); | ||
455 | } | ||
456 | |||
457 | return hist_entry_callchain__fprintf(he, total_period, left_margin, fp); | ||
458 | } | ||
459 | |||
460 | size_t hists__fprintf(struct hists *hists, struct hists *pair, | ||
461 | bool show_displacement, bool show_header, int max_rows, | ||
462 | int max_cols, FILE *fp) | ||
463 | { | ||
464 | struct sort_entry *se; | ||
465 | struct rb_node *nd; | ||
466 | size_t ret = 0; | ||
467 | u64 total_period; | ||
468 | unsigned long position = 1; | ||
469 | long displacement = 0; | ||
470 | unsigned int width; | ||
471 | const char *sep = symbol_conf.field_sep; | ||
472 | const char *col_width = symbol_conf.col_width_list_str; | ||
473 | int nr_rows = 0; | ||
474 | |||
475 | init_rem_hits(); | ||
476 | |||
477 | if (!show_header) | ||
478 | goto print_entries; | ||
479 | |||
480 | fprintf(fp, "# %s", pair ? "Baseline" : "Overhead"); | ||
481 | |||
482 | if (symbol_conf.show_cpu_utilization) { | ||
483 | if (sep) { | ||
484 | ret += fprintf(fp, "%csys", *sep); | ||
485 | ret += fprintf(fp, "%cus", *sep); | ||
486 | if (perf_guest) { | ||
487 | ret += fprintf(fp, "%cguest sys", *sep); | ||
488 | ret += fprintf(fp, "%cguest us", *sep); | ||
489 | } | ||
490 | } else { | ||
491 | ret += fprintf(fp, " sys "); | ||
492 | ret += fprintf(fp, " us "); | ||
493 | if (perf_guest) { | ||
494 | ret += fprintf(fp, " guest sys "); | ||
495 | ret += fprintf(fp, " guest us "); | ||
496 | } | ||
497 | } | ||
498 | } | ||
499 | |||
500 | if (symbol_conf.show_nr_samples) { | ||
501 | if (sep) | ||
502 | fprintf(fp, "%cSamples", *sep); | ||
503 | else | ||
504 | fputs(" Samples ", fp); | ||
505 | } | ||
506 | |||
507 | if (symbol_conf.show_total_period) { | ||
508 | if (sep) | ||
509 | ret += fprintf(fp, "%cPeriod", *sep); | ||
510 | else | ||
511 | ret += fprintf(fp, " Period "); | ||
512 | } | ||
513 | |||
514 | if (pair) { | ||
515 | if (sep) | ||
516 | ret += fprintf(fp, "%cDelta", *sep); | ||
517 | else | ||
518 | ret += fprintf(fp, " Delta "); | ||
519 | |||
520 | if (show_displacement) { | ||
521 | if (sep) | ||
522 | ret += fprintf(fp, "%cDisplacement", *sep); | ||
523 | else | ||
524 | ret += fprintf(fp, " Displ"); | ||
525 | } | ||
526 | } | ||
527 | |||
528 | list_for_each_entry(se, &hist_entry__sort_list, list) { | ||
529 | if (se->elide) | ||
530 | continue; | ||
531 | if (sep) { | ||
532 | fprintf(fp, "%c%s", *sep, se->se_header); | ||
533 | continue; | ||
534 | } | ||
535 | width = strlen(se->se_header); | ||
536 | if (symbol_conf.col_width_list_str) { | ||
537 | if (col_width) { | ||
538 | hists__set_col_len(hists, se->se_width_idx, | ||
539 | atoi(col_width)); | ||
540 | col_width = strchr(col_width, ','); | ||
541 | if (col_width) | ||
542 | ++col_width; | ||
543 | } | ||
544 | } | ||
545 | if (!hists__new_col_len(hists, se->se_width_idx, width)) | ||
546 | width = hists__col_len(hists, se->se_width_idx); | ||
547 | fprintf(fp, " %*s", width, se->se_header); | ||
548 | } | ||
549 | |||
550 | fprintf(fp, "\n"); | ||
551 | if (max_rows && ++nr_rows >= max_rows) | ||
552 | goto out; | ||
553 | |||
554 | if (sep) | ||
555 | goto print_entries; | ||
556 | |||
557 | fprintf(fp, "# ........"); | ||
558 | if (symbol_conf.show_cpu_utilization) | ||
559 | fprintf(fp, " ....... ......."); | ||
560 | if (symbol_conf.show_nr_samples) | ||
561 | fprintf(fp, " .........."); | ||
562 | if (symbol_conf.show_total_period) | ||
563 | fprintf(fp, " ............"); | ||
564 | if (pair) { | ||
565 | fprintf(fp, " .........."); | ||
566 | if (show_displacement) | ||
567 | fprintf(fp, " ....."); | ||
568 | } | ||
569 | list_for_each_entry(se, &hist_entry__sort_list, list) { | ||
570 | unsigned int i; | ||
571 | |||
572 | if (se->elide) | ||
573 | continue; | ||
574 | |||
575 | fprintf(fp, " "); | ||
576 | width = hists__col_len(hists, se->se_width_idx); | ||
577 | if (width == 0) | ||
578 | width = strlen(se->se_header); | ||
579 | for (i = 0; i < width; i++) | ||
580 | fprintf(fp, "."); | ||
581 | } | ||
582 | |||
583 | fprintf(fp, "\n"); | ||
584 | if (max_rows && ++nr_rows >= max_rows) | ||
585 | goto out; | ||
586 | |||
587 | fprintf(fp, "#\n"); | ||
588 | if (max_rows && ++nr_rows >= max_rows) | ||
589 | goto out; | ||
590 | |||
591 | print_entries: | ||
592 | total_period = hists->stats.total_period; | ||
593 | |||
594 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { | ||
595 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); | ||
596 | |||
597 | if (h->filtered) | ||
598 | continue; | ||
599 | |||
600 | if (show_displacement) { | ||
601 | if (h->pair != NULL) | ||
602 | displacement = ((long)h->pair->position - | ||
603 | (long)position); | ||
604 | else | ||
605 | displacement = 0; | ||
606 | ++position; | ||
607 | } | ||
608 | ret += hist_entry__fprintf(h, max_cols, hists, pair, show_displacement, | ||
609 | displacement, total_period, fp); | ||
610 | |||
611 | if (symbol_conf.use_callchain) | ||
612 | ret += hist_entry__fprintf_callchain(h, hists, total_period, fp); | ||
613 | if (max_rows && ++nr_rows >= max_rows) | ||
614 | goto out; | ||
615 | |||
616 | if (h->ms.map == NULL && verbose > 1) { | ||
617 | __map_groups__fprintf_maps(&h->thread->mg, | ||
618 | MAP__FUNCTION, verbose, fp); | ||
619 | fprintf(fp, "%.10s end\n", graph_dotted_line); | ||
620 | } | ||
621 | } | ||
622 | out: | ||
623 | free(rem_sq_bracket); | ||
624 | |||
625 | return ret; | ||
626 | } | ||
627 | |||
628 | size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp) | ||
629 | { | ||
630 | int i; | ||
631 | size_t ret = 0; | ||
632 | |||
633 | for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) { | ||
634 | const char *name; | ||
635 | |||
636 | if (hists->stats.nr_events[i] == 0) | ||
637 | continue; | ||
638 | |||
639 | name = perf_event__name(i); | ||
640 | if (!strcmp(name, "UNKNOWN")) | ||
641 | continue; | ||
642 | |||
643 | ret += fprintf(fp, "%16s events: %10d\n", name, | ||
644 | hists->stats.nr_events[i]); | ||
645 | } | ||
646 | |||
647 | return ret; | ||
648 | } | ||
diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c index f247ef2789a4..b1817f15bb87 100644 --- a/tools/perf/util/hist.c +++ b/tools/perf/util/hist.c | |||
@@ -45,7 +45,7 @@ bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len) | |||
45 | return false; | 45 | return false; |
46 | } | 46 | } |
47 | 47 | ||
48 | static void hists__reset_col_len(struct hists *hists) | 48 | void hists__reset_col_len(struct hists *hists) |
49 | { | 49 | { |
50 | enum hist_column col; | 50 | enum hist_column col; |
51 | 51 | ||
@@ -63,7 +63,7 @@ static void hists__set_unres_dso_col_len(struct hists *hists, int dso) | |||
63 | hists__set_col_len(hists, dso, unresolved_col_width); | 63 | hists__set_col_len(hists, dso, unresolved_col_width); |
64 | } | 64 | } |
65 | 65 | ||
66 | static void hists__calc_col_len(struct hists *hists, struct hist_entry *h) | 66 | void hists__calc_col_len(struct hists *hists, struct hist_entry *h) |
67 | { | 67 | { |
68 | const unsigned int unresolved_col_width = BITS_PER_LONG / 4; | 68 | const unsigned int unresolved_col_width = BITS_PER_LONG / 4; |
69 | u16 len; | 69 | u16 len; |
@@ -114,6 +114,22 @@ static void hists__calc_col_len(struct hists *hists, struct hist_entry *h) | |||
114 | } | 114 | } |
115 | } | 115 | } |
116 | 116 | ||
117 | void hists__output_recalc_col_len(struct hists *hists, int max_rows) | ||
118 | { | ||
119 | struct rb_node *next = rb_first(&hists->entries); | ||
120 | struct hist_entry *n; | ||
121 | int row = 0; | ||
122 | |||
123 | hists__reset_col_len(hists); | ||
124 | |||
125 | while (next && row++ < max_rows) { | ||
126 | n = rb_entry(next, struct hist_entry, rb_node); | ||
127 | if (!n->filtered) | ||
128 | hists__calc_col_len(hists, n); | ||
129 | next = rb_next(&n->rb_node); | ||
130 | } | ||
131 | } | ||
132 | |||
117 | static void hist_entry__add_cpumode_period(struct hist_entry *he, | 133 | static void hist_entry__add_cpumode_period(struct hist_entry *he, |
118 | unsigned int cpumode, u64 period) | 134 | unsigned int cpumode, u64 period) |
119 | { | 135 | { |
@@ -547,641 +563,6 @@ void hists__output_resort_threaded(struct hists *hists) | |||
547 | return __hists__output_resort(hists, true); | 563 | return __hists__output_resort(hists, true); |
548 | } | 564 | } |
549 | 565 | ||
550 | static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin) | ||
551 | { | ||
552 | int i; | ||
553 | int ret = fprintf(fp, " "); | ||
554 | |||
555 | for (i = 0; i < left_margin; i++) | ||
556 | ret += fprintf(fp, " "); | ||
557 | |||
558 | return ret; | ||
559 | } | ||
560 | |||
561 | static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask, | ||
562 | int left_margin) | ||
563 | { | ||
564 | int i; | ||
565 | size_t ret = callchain__fprintf_left_margin(fp, left_margin); | ||
566 | |||
567 | for (i = 0; i < depth; i++) | ||
568 | if (depth_mask & (1 << i)) | ||
569 | ret += fprintf(fp, "| "); | ||
570 | else | ||
571 | ret += fprintf(fp, " "); | ||
572 | |||
573 | ret += fprintf(fp, "\n"); | ||
574 | |||
575 | return ret; | ||
576 | } | ||
577 | |||
578 | static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, | ||
579 | int depth, int depth_mask, int period, | ||
580 | u64 total_samples, u64 hits, | ||
581 | int left_margin) | ||
582 | { | ||
583 | int i; | ||
584 | size_t ret = 0; | ||
585 | |||
586 | ret += callchain__fprintf_left_margin(fp, left_margin); | ||
587 | for (i = 0; i < depth; i++) { | ||
588 | if (depth_mask & (1 << i)) | ||
589 | ret += fprintf(fp, "|"); | ||
590 | else | ||
591 | ret += fprintf(fp, " "); | ||
592 | if (!period && i == depth - 1) { | ||
593 | double percent; | ||
594 | |||
595 | percent = hits * 100.0 / total_samples; | ||
596 | ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent); | ||
597 | } else | ||
598 | ret += fprintf(fp, "%s", " "); | ||
599 | } | ||
600 | if (chain->ms.sym) | ||
601 | ret += fprintf(fp, "%s\n", chain->ms.sym->name); | ||
602 | else | ||
603 | ret += fprintf(fp, "0x%0" PRIx64 "\n", chain->ip); | ||
604 | |||
605 | return ret; | ||
606 | } | ||
607 | |||
608 | static struct symbol *rem_sq_bracket; | ||
609 | static struct callchain_list rem_hits; | ||
610 | |||
611 | static void init_rem_hits(void) | ||
612 | { | ||
613 | rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6); | ||
614 | if (!rem_sq_bracket) { | ||
615 | fprintf(stderr, "Not enough memory to display remaining hits\n"); | ||
616 | return; | ||
617 | } | ||
618 | |||
619 | strcpy(rem_sq_bracket->name, "[...]"); | ||
620 | rem_hits.ms.sym = rem_sq_bracket; | ||
621 | } | ||
622 | |||
623 | static size_t __callchain__fprintf_graph(FILE *fp, struct rb_root *root, | ||
624 | u64 total_samples, int depth, | ||
625 | int depth_mask, int left_margin) | ||
626 | { | ||
627 | struct rb_node *node, *next; | ||
628 | struct callchain_node *child; | ||
629 | struct callchain_list *chain; | ||
630 | int new_depth_mask = depth_mask; | ||
631 | u64 remaining; | ||
632 | size_t ret = 0; | ||
633 | int i; | ||
634 | uint entries_printed = 0; | ||
635 | |||
636 | remaining = total_samples; | ||
637 | |||
638 | node = rb_first(root); | ||
639 | while (node) { | ||
640 | u64 new_total; | ||
641 | u64 cumul; | ||
642 | |||
643 | child = rb_entry(node, struct callchain_node, rb_node); | ||
644 | cumul = callchain_cumul_hits(child); | ||
645 | remaining -= cumul; | ||
646 | |||
647 | /* | ||
648 | * The depth mask manages the output of pipes that show | ||
649 | * the depth. We don't want to keep the pipes of the current | ||
650 | * level for the last child of this depth. | ||
651 | * Except if we have remaining filtered hits. They will | ||
652 | * supersede the last child | ||
653 | */ | ||
654 | next = rb_next(node); | ||
655 | if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining)) | ||
656 | new_depth_mask &= ~(1 << (depth - 1)); | ||
657 | |||
658 | /* | ||
659 | * But we keep the older depth mask for the line separator | ||
660 | * to keep the level link until we reach the last child | ||
661 | */ | ||
662 | ret += ipchain__fprintf_graph_line(fp, depth, depth_mask, | ||
663 | left_margin); | ||
664 | i = 0; | ||
665 | list_for_each_entry(chain, &child->val, list) { | ||
666 | ret += ipchain__fprintf_graph(fp, chain, depth, | ||
667 | new_depth_mask, i++, | ||
668 | total_samples, | ||
669 | cumul, | ||
670 | left_margin); | ||
671 | } | ||
672 | |||
673 | if (callchain_param.mode == CHAIN_GRAPH_REL) | ||
674 | new_total = child->children_hit; | ||
675 | else | ||
676 | new_total = total_samples; | ||
677 | |||
678 | ret += __callchain__fprintf_graph(fp, &child->rb_root, new_total, | ||
679 | depth + 1, | ||
680 | new_depth_mask | (1 << depth), | ||
681 | left_margin); | ||
682 | node = next; | ||
683 | if (++entries_printed == callchain_param.print_limit) | ||
684 | break; | ||
685 | } | ||
686 | |||
687 | if (callchain_param.mode == CHAIN_GRAPH_REL && | ||
688 | remaining && remaining != total_samples) { | ||
689 | |||
690 | if (!rem_sq_bracket) | ||
691 | return ret; | ||
692 | |||
693 | new_depth_mask &= ~(1 << (depth - 1)); | ||
694 | ret += ipchain__fprintf_graph(fp, &rem_hits, depth, | ||
695 | new_depth_mask, 0, total_samples, | ||
696 | remaining, left_margin); | ||
697 | } | ||
698 | |||
699 | return ret; | ||
700 | } | ||
701 | |||
702 | static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root, | ||
703 | u64 total_samples, int left_margin) | ||
704 | { | ||
705 | struct callchain_node *cnode; | ||
706 | struct callchain_list *chain; | ||
707 | u32 entries_printed = 0; | ||
708 | bool printed = false; | ||
709 | struct rb_node *node; | ||
710 | int i = 0; | ||
711 | int ret = 0; | ||
712 | |||
713 | /* | ||
714 | * If have one single callchain root, don't bother printing | ||
715 | * its percentage (100 % in fractal mode and the same percentage | ||
716 | * than the hist in graph mode). This also avoid one level of column. | ||
717 | */ | ||
718 | node = rb_first(root); | ||
719 | if (node && !rb_next(node)) { | ||
720 | cnode = rb_entry(node, struct callchain_node, rb_node); | ||
721 | list_for_each_entry(chain, &cnode->val, list) { | ||
722 | /* | ||
723 | * If we sort by symbol, the first entry is the same than | ||
724 | * the symbol. No need to print it otherwise it appears as | ||
725 | * displayed twice. | ||
726 | */ | ||
727 | if (!i++ && sort__first_dimension == SORT_SYM) | ||
728 | continue; | ||
729 | if (!printed) { | ||
730 | ret += callchain__fprintf_left_margin(fp, left_margin); | ||
731 | ret += fprintf(fp, "|\n"); | ||
732 | ret += callchain__fprintf_left_margin(fp, left_margin); | ||
733 | ret += fprintf(fp, "---"); | ||
734 | left_margin += 3; | ||
735 | printed = true; | ||
736 | } else | ||
737 | ret += callchain__fprintf_left_margin(fp, left_margin); | ||
738 | |||
739 | if (chain->ms.sym) | ||
740 | ret += fprintf(fp, " %s\n", chain->ms.sym->name); | ||
741 | else | ||
742 | ret += fprintf(fp, " %p\n", (void *)(long)chain->ip); | ||
743 | |||
744 | if (++entries_printed == callchain_param.print_limit) | ||
745 | break; | ||
746 | } | ||
747 | root = &cnode->rb_root; | ||
748 | } | ||
749 | |||
750 | ret += __callchain__fprintf_graph(fp, root, total_samples, | ||
751 | 1, 1, left_margin); | ||
752 | ret += fprintf(fp, "\n"); | ||
753 | |||
754 | return ret; | ||
755 | } | ||
756 | |||
757 | static size_t __callchain__fprintf_flat(FILE *fp, | ||
758 | struct callchain_node *self, | ||
759 | u64 total_samples) | ||
760 | { | ||
761 | struct callchain_list *chain; | ||
762 | size_t ret = 0; | ||
763 | |||
764 | if (!self) | ||
765 | return 0; | ||
766 | |||
767 | ret += __callchain__fprintf_flat(fp, self->parent, total_samples); | ||
768 | |||
769 | |||
770 | list_for_each_entry(chain, &self->val, list) { | ||
771 | if (chain->ip >= PERF_CONTEXT_MAX) | ||
772 | continue; | ||
773 | if (chain->ms.sym) | ||
774 | ret += fprintf(fp, " %s\n", chain->ms.sym->name); | ||
775 | else | ||
776 | ret += fprintf(fp, " %p\n", | ||
777 | (void *)(long)chain->ip); | ||
778 | } | ||
779 | |||
780 | return ret; | ||
781 | } | ||
782 | |||
783 | static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *self, | ||
784 | u64 total_samples) | ||
785 | { | ||
786 | size_t ret = 0; | ||
787 | u32 entries_printed = 0; | ||
788 | struct rb_node *rb_node; | ||
789 | struct callchain_node *chain; | ||
790 | |||
791 | rb_node = rb_first(self); | ||
792 | while (rb_node) { | ||
793 | double percent; | ||
794 | |||
795 | chain = rb_entry(rb_node, struct callchain_node, rb_node); | ||
796 | percent = chain->hit * 100.0 / total_samples; | ||
797 | |||
798 | ret = percent_color_fprintf(fp, " %6.2f%%\n", percent); | ||
799 | ret += __callchain__fprintf_flat(fp, chain, total_samples); | ||
800 | ret += fprintf(fp, "\n"); | ||
801 | if (++entries_printed == callchain_param.print_limit) | ||
802 | break; | ||
803 | |||
804 | rb_node = rb_next(rb_node); | ||
805 | } | ||
806 | |||
807 | return ret; | ||
808 | } | ||
809 | |||
810 | static size_t hist_entry_callchain__fprintf(struct hist_entry *he, | ||
811 | u64 total_samples, int left_margin, | ||
812 | FILE *fp) | ||
813 | { | ||
814 | switch (callchain_param.mode) { | ||
815 | case CHAIN_GRAPH_REL: | ||
816 | return callchain__fprintf_graph(fp, &he->sorted_chain, he->period, | ||
817 | left_margin); | ||
818 | break; | ||
819 | case CHAIN_GRAPH_ABS: | ||
820 | return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples, | ||
821 | left_margin); | ||
822 | break; | ||
823 | case CHAIN_FLAT: | ||
824 | return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples); | ||
825 | break; | ||
826 | case CHAIN_NONE: | ||
827 | break; | ||
828 | default: | ||
829 | pr_err("Bad callchain mode\n"); | ||
830 | } | ||
831 | |||
832 | return 0; | ||
833 | } | ||
834 | |||
835 | void hists__output_recalc_col_len(struct hists *hists, int max_rows) | ||
836 | { | ||
837 | struct rb_node *next = rb_first(&hists->entries); | ||
838 | struct hist_entry *n; | ||
839 | int row = 0; | ||
840 | |||
841 | hists__reset_col_len(hists); | ||
842 | |||
843 | while (next && row++ < max_rows) { | ||
844 | n = rb_entry(next, struct hist_entry, rb_node); | ||
845 | if (!n->filtered) | ||
846 | hists__calc_col_len(hists, n); | ||
847 | next = rb_next(&n->rb_node); | ||
848 | } | ||
849 | } | ||
850 | |||
851 | static int hist_entry__pcnt_snprintf(struct hist_entry *he, char *s, | ||
852 | size_t size, struct hists *pair_hists, | ||
853 | bool show_displacement, long displacement, | ||
854 | bool color, u64 total_period) | ||
855 | { | ||
856 | u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us; | ||
857 | u64 nr_events; | ||
858 | const char *sep = symbol_conf.field_sep; | ||
859 | int ret; | ||
860 | |||
861 | if (symbol_conf.exclude_other && !he->parent) | ||
862 | return 0; | ||
863 | |||
864 | if (pair_hists) { | ||
865 | period = he->pair ? he->pair->period : 0; | ||
866 | nr_events = he->pair ? he->pair->nr_events : 0; | ||
867 | total = pair_hists->stats.total_period; | ||
868 | period_sys = he->pair ? he->pair->period_sys : 0; | ||
869 | period_us = he->pair ? he->pair->period_us : 0; | ||
870 | period_guest_sys = he->pair ? he->pair->period_guest_sys : 0; | ||
871 | period_guest_us = he->pair ? he->pair->period_guest_us : 0; | ||
872 | } else { | ||
873 | period = he->period; | ||
874 | nr_events = he->nr_events; | ||
875 | total = total_period; | ||
876 | period_sys = he->period_sys; | ||
877 | period_us = he->period_us; | ||
878 | period_guest_sys = he->period_guest_sys; | ||
879 | period_guest_us = he->period_guest_us; | ||
880 | } | ||
881 | |||
882 | if (total) { | ||
883 | if (color) | ||
884 | ret = percent_color_snprintf(s, size, | ||
885 | sep ? "%.2f" : " %6.2f%%", | ||
886 | (period * 100.0) / total); | ||
887 | else | ||
888 | ret = scnprintf(s, size, sep ? "%.2f" : " %6.2f%%", | ||
889 | (period * 100.0) / total); | ||
890 | if (symbol_conf.show_cpu_utilization) { | ||
891 | ret += percent_color_snprintf(s + ret, size - ret, | ||
892 | sep ? "%.2f" : " %6.2f%%", | ||
893 | (period_sys * 100.0) / total); | ||
894 | ret += percent_color_snprintf(s + ret, size - ret, | ||
895 | sep ? "%.2f" : " %6.2f%%", | ||
896 | (period_us * 100.0) / total); | ||
897 | if (perf_guest) { | ||
898 | ret += percent_color_snprintf(s + ret, | ||
899 | size - ret, | ||
900 | sep ? "%.2f" : " %6.2f%%", | ||
901 | (period_guest_sys * 100.0) / | ||
902 | total); | ||
903 | ret += percent_color_snprintf(s + ret, | ||
904 | size - ret, | ||
905 | sep ? "%.2f" : " %6.2f%%", | ||
906 | (period_guest_us * 100.0) / | ||
907 | total); | ||
908 | } | ||
909 | } | ||
910 | } else | ||
911 | ret = scnprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period); | ||
912 | |||
913 | if (symbol_conf.show_nr_samples) { | ||
914 | if (sep) | ||
915 | ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events); | ||
916 | else | ||
917 | ret += scnprintf(s + ret, size - ret, "%11" PRIu64, nr_events); | ||
918 | } | ||
919 | |||
920 | if (symbol_conf.show_total_period) { | ||
921 | if (sep) | ||
922 | ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period); | ||
923 | else | ||
924 | ret += scnprintf(s + ret, size - ret, " %12" PRIu64, period); | ||
925 | } | ||
926 | |||
927 | if (pair_hists) { | ||
928 | char bf[32]; | ||
929 | double old_percent = 0, new_percent = 0, diff; | ||
930 | |||
931 | if (total > 0) | ||
932 | old_percent = (period * 100.0) / total; | ||
933 | if (total_period > 0) | ||
934 | new_percent = (he->period * 100.0) / total_period; | ||
935 | |||
936 | diff = new_percent - old_percent; | ||
937 | |||
938 | if (fabs(diff) >= 0.01) | ||
939 | scnprintf(bf, sizeof(bf), "%+4.2F%%", diff); | ||
940 | else | ||
941 | scnprintf(bf, sizeof(bf), " "); | ||
942 | |||
943 | if (sep) | ||
944 | ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf); | ||
945 | else | ||
946 | ret += scnprintf(s + ret, size - ret, "%11.11s", bf); | ||
947 | |||
948 | if (show_displacement) { | ||
949 | if (displacement) | ||
950 | scnprintf(bf, sizeof(bf), "%+4ld", displacement); | ||
951 | else | ||
952 | scnprintf(bf, sizeof(bf), " "); | ||
953 | |||
954 | if (sep) | ||
955 | ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf); | ||
956 | else | ||
957 | ret += scnprintf(s + ret, size - ret, "%6.6s", bf); | ||
958 | } | ||
959 | } | ||
960 | |||
961 | return ret; | ||
962 | } | ||
963 | |||
964 | int hist_entry__snprintf(struct hist_entry *he, char *s, size_t size, | ||
965 | struct hists *hists) | ||
966 | { | ||
967 | const char *sep = symbol_conf.field_sep; | ||
968 | struct sort_entry *se; | ||
969 | int ret = 0; | ||
970 | |||
971 | list_for_each_entry(se, &hist_entry__sort_list, list) { | ||
972 | if (se->elide) | ||
973 | continue; | ||
974 | |||
975 | ret += scnprintf(s + ret, size - ret, "%s", sep ?: " "); | ||
976 | ret += se->se_snprintf(he, s + ret, size - ret, | ||
977 | hists__col_len(hists, se->se_width_idx)); | ||
978 | } | ||
979 | |||
980 | return ret; | ||
981 | } | ||
982 | |||
983 | static int hist_entry__fprintf(struct hist_entry *he, size_t size, | ||
984 | struct hists *hists, struct hists *pair_hists, | ||
985 | bool show_displacement, long displacement, | ||
986 | u64 total_period, FILE *fp) | ||
987 | { | ||
988 | char bf[512]; | ||
989 | int ret; | ||
990 | |||
991 | if (size == 0 || size > sizeof(bf)) | ||
992 | size = sizeof(bf); | ||
993 | |||
994 | ret = hist_entry__pcnt_snprintf(he, bf, size, pair_hists, | ||
995 | show_displacement, displacement, | ||
996 | true, total_period); | ||
997 | hist_entry__snprintf(he, bf + ret, size - ret, hists); | ||
998 | return fprintf(fp, "%s\n", bf); | ||
999 | } | ||
1000 | |||
1001 | static size_t hist_entry__fprintf_callchain(struct hist_entry *he, | ||
1002 | struct hists *hists, | ||
1003 | u64 total_period, FILE *fp) | ||
1004 | { | ||
1005 | int left_margin = 0; | ||
1006 | |||
1007 | if (sort__first_dimension == SORT_COMM) { | ||
1008 | struct sort_entry *se = list_first_entry(&hist_entry__sort_list, | ||
1009 | typeof(*se), list); | ||
1010 | left_margin = hists__col_len(hists, se->se_width_idx); | ||
1011 | left_margin -= thread__comm_len(he->thread); | ||
1012 | } | ||
1013 | |||
1014 | return hist_entry_callchain__fprintf(he, total_period, left_margin, fp); | ||
1015 | } | ||
1016 | |||
1017 | size_t hists__fprintf(struct hists *hists, struct hists *pair, | ||
1018 | bool show_displacement, bool show_header, int max_rows, | ||
1019 | int max_cols, FILE *fp) | ||
1020 | { | ||
1021 | struct sort_entry *se; | ||
1022 | struct rb_node *nd; | ||
1023 | size_t ret = 0; | ||
1024 | u64 total_period; | ||
1025 | unsigned long position = 1; | ||
1026 | long displacement = 0; | ||
1027 | unsigned int width; | ||
1028 | const char *sep = symbol_conf.field_sep; | ||
1029 | const char *col_width = symbol_conf.col_width_list_str; | ||
1030 | int nr_rows = 0; | ||
1031 | |||
1032 | init_rem_hits(); | ||
1033 | |||
1034 | if (!show_header) | ||
1035 | goto print_entries; | ||
1036 | |||
1037 | fprintf(fp, "# %s", pair ? "Baseline" : "Overhead"); | ||
1038 | |||
1039 | if (symbol_conf.show_cpu_utilization) { | ||
1040 | if (sep) { | ||
1041 | ret += fprintf(fp, "%csys", *sep); | ||
1042 | ret += fprintf(fp, "%cus", *sep); | ||
1043 | if (perf_guest) { | ||
1044 | ret += fprintf(fp, "%cguest sys", *sep); | ||
1045 | ret += fprintf(fp, "%cguest us", *sep); | ||
1046 | } | ||
1047 | } else { | ||
1048 | ret += fprintf(fp, " sys "); | ||
1049 | ret += fprintf(fp, " us "); | ||
1050 | if (perf_guest) { | ||
1051 | ret += fprintf(fp, " guest sys "); | ||
1052 | ret += fprintf(fp, " guest us "); | ||
1053 | } | ||
1054 | } | ||
1055 | } | ||
1056 | |||
1057 | if (symbol_conf.show_nr_samples) { | ||
1058 | if (sep) | ||
1059 | fprintf(fp, "%cSamples", *sep); | ||
1060 | else | ||
1061 | fputs(" Samples ", fp); | ||
1062 | } | ||
1063 | |||
1064 | if (symbol_conf.show_total_period) { | ||
1065 | if (sep) | ||
1066 | ret += fprintf(fp, "%cPeriod", *sep); | ||
1067 | else | ||
1068 | ret += fprintf(fp, " Period "); | ||
1069 | } | ||
1070 | |||
1071 | if (pair) { | ||
1072 | if (sep) | ||
1073 | ret += fprintf(fp, "%cDelta", *sep); | ||
1074 | else | ||
1075 | ret += fprintf(fp, " Delta "); | ||
1076 | |||
1077 | if (show_displacement) { | ||
1078 | if (sep) | ||
1079 | ret += fprintf(fp, "%cDisplacement", *sep); | ||
1080 | else | ||
1081 | ret += fprintf(fp, " Displ"); | ||
1082 | } | ||
1083 | } | ||
1084 | |||
1085 | list_for_each_entry(se, &hist_entry__sort_list, list) { | ||
1086 | if (se->elide) | ||
1087 | continue; | ||
1088 | if (sep) { | ||
1089 | fprintf(fp, "%c%s", *sep, se->se_header); | ||
1090 | continue; | ||
1091 | } | ||
1092 | width = strlen(se->se_header); | ||
1093 | if (symbol_conf.col_width_list_str) { | ||
1094 | if (col_width) { | ||
1095 | hists__set_col_len(hists, se->se_width_idx, | ||
1096 | atoi(col_width)); | ||
1097 | col_width = strchr(col_width, ','); | ||
1098 | if (col_width) | ||
1099 | ++col_width; | ||
1100 | } | ||
1101 | } | ||
1102 | if (!hists__new_col_len(hists, se->se_width_idx, width)) | ||
1103 | width = hists__col_len(hists, se->se_width_idx); | ||
1104 | fprintf(fp, " %*s", width, se->se_header); | ||
1105 | } | ||
1106 | |||
1107 | fprintf(fp, "\n"); | ||
1108 | if (max_rows && ++nr_rows >= max_rows) | ||
1109 | goto out; | ||
1110 | |||
1111 | if (sep) | ||
1112 | goto print_entries; | ||
1113 | |||
1114 | fprintf(fp, "# ........"); | ||
1115 | if (symbol_conf.show_cpu_utilization) | ||
1116 | fprintf(fp, " ....... ......."); | ||
1117 | if (symbol_conf.show_nr_samples) | ||
1118 | fprintf(fp, " .........."); | ||
1119 | if (symbol_conf.show_total_period) | ||
1120 | fprintf(fp, " ............"); | ||
1121 | if (pair) { | ||
1122 | fprintf(fp, " .........."); | ||
1123 | if (show_displacement) | ||
1124 | fprintf(fp, " ....."); | ||
1125 | } | ||
1126 | list_for_each_entry(se, &hist_entry__sort_list, list) { | ||
1127 | unsigned int i; | ||
1128 | |||
1129 | if (se->elide) | ||
1130 | continue; | ||
1131 | |||
1132 | fprintf(fp, " "); | ||
1133 | width = hists__col_len(hists, se->se_width_idx); | ||
1134 | if (width == 0) | ||
1135 | width = strlen(se->se_header); | ||
1136 | for (i = 0; i < width; i++) | ||
1137 | fprintf(fp, "."); | ||
1138 | } | ||
1139 | |||
1140 | fprintf(fp, "\n"); | ||
1141 | if (max_rows && ++nr_rows >= max_rows) | ||
1142 | goto out; | ||
1143 | |||
1144 | fprintf(fp, "#\n"); | ||
1145 | if (max_rows && ++nr_rows >= max_rows) | ||
1146 | goto out; | ||
1147 | |||
1148 | print_entries: | ||
1149 | total_period = hists->stats.total_period; | ||
1150 | |||
1151 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { | ||
1152 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); | ||
1153 | |||
1154 | if (h->filtered) | ||
1155 | continue; | ||
1156 | |||
1157 | if (show_displacement) { | ||
1158 | if (h->pair != NULL) | ||
1159 | displacement = ((long)h->pair->position - | ||
1160 | (long)position); | ||
1161 | else | ||
1162 | displacement = 0; | ||
1163 | ++position; | ||
1164 | } | ||
1165 | ret += hist_entry__fprintf(h, max_cols, hists, pair, show_displacement, | ||
1166 | displacement, total_period, fp); | ||
1167 | |||
1168 | if (symbol_conf.use_callchain) | ||
1169 | ret += hist_entry__fprintf_callchain(h, hists, total_period, fp); | ||
1170 | if (max_rows && ++nr_rows >= max_rows) | ||
1171 | goto out; | ||
1172 | |||
1173 | if (h->ms.map == NULL && verbose > 1) { | ||
1174 | __map_groups__fprintf_maps(&h->thread->mg, | ||
1175 | MAP__FUNCTION, verbose, fp); | ||
1176 | fprintf(fp, "%.10s end\n", graph_dotted_line); | ||
1177 | } | ||
1178 | } | ||
1179 | out: | ||
1180 | free(rem_sq_bracket); | ||
1181 | |||
1182 | return ret; | ||
1183 | } | ||
1184 | |||
1185 | /* | 566 | /* |
1186 | * See hists__fprintf to match the column widths | 567 | * See hists__fprintf to match the column widths |
1187 | */ | 568 | */ |
@@ -1342,25 +723,3 @@ void hists__inc_nr_events(struct hists *hists, u32 type) | |||
1342 | ++hists->stats.nr_events[0]; | 723 | ++hists->stats.nr_events[0]; |
1343 | ++hists->stats.nr_events[type]; | 724 | ++hists->stats.nr_events[type]; |
1344 | } | 725 | } |
1345 | |||
1346 | size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp) | ||
1347 | { | ||
1348 | int i; | ||
1349 | size_t ret = 0; | ||
1350 | |||
1351 | for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) { | ||
1352 | const char *name; | ||
1353 | |||
1354 | if (hists->stats.nr_events[i] == 0) | ||
1355 | continue; | ||
1356 | |||
1357 | name = perf_event__name(i); | ||
1358 | if (!strcmp(name, "UNKNOWN")) | ||
1359 | continue; | ||
1360 | |||
1361 | ret += fprintf(fp, "%16s events: %10d\n", name, | ||
1362 | hists->stats.nr_events[i]); | ||
1363 | } | ||
1364 | |||
1365 | return ret; | ||
1366 | } | ||
diff --git a/tools/perf/util/hist.h b/tools/perf/util/hist.h index 0b096c27a419..69fab7d9abcd 100644 --- a/tools/perf/util/hist.h +++ b/tools/perf/util/hist.h | |||
@@ -112,6 +112,8 @@ void hists__filter_by_symbol(struct hists *hists); | |||
112 | u16 hists__col_len(struct hists *self, enum hist_column col); | 112 | u16 hists__col_len(struct hists *self, enum hist_column col); |
113 | void hists__set_col_len(struct hists *self, enum hist_column col, u16 len); | 113 | void hists__set_col_len(struct hists *self, enum hist_column col, u16 len); |
114 | bool hists__new_col_len(struct hists *self, enum hist_column col, u16 len); | 114 | bool hists__new_col_len(struct hists *self, enum hist_column col, u16 len); |
115 | void hists__reset_col_len(struct hists *hists); | ||
116 | void hists__calc_col_len(struct hists *hists, struct hist_entry *he); | ||
115 | 117 | ||
116 | struct perf_evlist; | 118 | struct perf_evlist; |
117 | 119 | ||