Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-2.6-block.git] / tools / perf / ui / stdio / hist.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
2333cfa9 2#include <limits.h>
7ccf4f90 3#include <stdio.h>
f2a39fe8 4#include <stdlib.h>
8e99b6d4 5#include <linux/string.h>
7ccf4f90 6
b10ba7f1 7#include "../../util/callchain.h"
185bcb92 8#include "../../util/debug.h"
ea49e01c 9#include "../../util/event.h"
7ccf4f90 10#include "../../util/hist.h"
7b644f9a 11#include "../../util/map.h"
c54d241b 12#include "../../util/maps.h"
daecf9e0 13#include "../../util/symbol.h"
7ccf4f90 14#include "../../util/sort.h"
5b9e2146 15#include "../../util/evsel.h"
632a5cab 16#include "../../util/srcline.h"
a067558e 17#include "../../util/string2.h"
e7ff8920 18#include "../../util/thread.h"
0b49f836 19#include "../../util/block-info.h"
3052ba56 20#include <linux/ctype.h>
7f7c536f 21#include <linux/zalloc.h>
7ccf4f90
NK
22
23static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
24{
25 int i;
26 int ret = fprintf(fp, " ");
27
2333cfa9
ACM
28 if (left_margin > USHRT_MAX)
29 left_margin = USHRT_MAX;
30
7ccf4f90
NK
31 for (i = 0; i < left_margin; i++)
32 ret += fprintf(fp, " ");
33
34 return ret;
35}
36
37static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
38 int left_margin)
39{
40 int i;
41 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
42
43 for (i = 0; i < depth; i++)
44 if (depth_mask & (1 << i))
45 ret += fprintf(fp, "| ");
46 else
47 ret += fprintf(fp, " ");
48
49 ret += fprintf(fp, "\n");
50
51 return ret;
52}
53
5ab250ca
NK
54static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_node *node,
55 struct callchain_list *chain,
7ccf4f90 56 int depth, int depth_mask, int period,
5ab250ca 57 u64 total_samples, int left_margin)
7ccf4f90
NK
58{
59 int i;
60 size_t ret = 0;
8577ae6b
JY
61 char bf[1024], *alloc_str = NULL;
62 char buf[64];
63 const char *str;
7ccf4f90
NK
64
65 ret += callchain__fprintf_left_margin(fp, left_margin);
66 for (i = 0; i < depth; i++) {
67 if (depth_mask & (1 << i))
68 ret += fprintf(fp, "|");
69 else
70 ret += fprintf(fp, " ");
71 if (!period && i == depth - 1) {
5ab250ca
NK
72 ret += fprintf(fp, "--");
73 ret += callchain_node__fprintf_value(node, fp, total_samples);
74 ret += fprintf(fp, "--");
7ccf4f90
NK
75 } else
76 ret += fprintf(fp, "%s", " ");
77 }
8577ae6b
JY
78
79 str = callchain_list__sym_name(chain, bf, sizeof(bf), false);
80
81 if (symbol_conf.show_branchflag_count) {
c4ee0625
JY
82 callchain_list_counts__printf_value(chain, NULL,
83 buf, sizeof(buf));
8577ae6b
JY
84
85 if (asprintf(&alloc_str, "%s%s", str, buf) < 0)
86 str = "Not enough memory!";
87 else
88 str = alloc_str;
89 }
90
91 fputs(str, fp);
2989ccaa 92 fputc('\n', fp);
8577ae6b 93 free(alloc_str);
0db64dd0 94
7ccf4f90
NK
95 return ret;
96}
97
98static struct symbol *rem_sq_bracket;
99static struct callchain_list rem_hits;
100
101static void init_rem_hits(void)
102{
103 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
104 if (!rem_sq_bracket) {
105 fprintf(stderr, "Not enough memory to display remaining hits\n");
106 return;
107 }
108
109 strcpy(rem_sq_bracket->name, "[...]");
110 rem_hits.ms.sym = rem_sq_bracket;
111}
112
113static size_t __callchain__fprintf_graph(FILE *fp, struct rb_root *root,
114 u64 total_samples, int depth,
115 int depth_mask, int left_margin)
116{
117 struct rb_node *node, *next;
f2af0086 118 struct callchain_node *child = NULL;
7ccf4f90
NK
119 struct callchain_list *chain;
120 int new_depth_mask = depth_mask;
121 u64 remaining;
122 size_t ret = 0;
123 int i;
124 uint entries_printed = 0;
f2af0086 125 int cumul_count = 0;
7ccf4f90
NK
126
127 remaining = total_samples;
128
129 node = rb_first(root);
130 while (node) {
131 u64 new_total;
132 u64 cumul;
133
134 child = rb_entry(node, struct callchain_node, rb_node);
135 cumul = callchain_cumul_hits(child);
136 remaining -= cumul;
f2af0086 137 cumul_count += callchain_cumul_counts(child);
7ccf4f90
NK
138
139 /*
140 * The depth mask manages the output of pipes that show
141 * the depth. We don't want to keep the pipes of the current
142 * level for the last child of this depth.
143 * Except if we have remaining filtered hits. They will
144 * supersede the last child
145 */
146 next = rb_next(node);
147 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
148 new_depth_mask &= ~(1 << (depth - 1));
149
150 /*
151 * But we keep the older depth mask for the line separator
152 * to keep the level link until we reach the last child
153 */
154 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
155 left_margin);
156 i = 0;
157 list_for_each_entry(chain, &child->val, list) {
5ab250ca 158 ret += ipchain__fprintf_graph(fp, child, chain, depth,
7ccf4f90
NK
159 new_depth_mask, i++,
160 total_samples,
7ccf4f90
NK
161 left_margin);
162 }
163
164 if (callchain_param.mode == CHAIN_GRAPH_REL)
165 new_total = child->children_hit;
166 else
167 new_total = total_samples;
168
169 ret += __callchain__fprintf_graph(fp, &child->rb_root, new_total,
170 depth + 1,
171 new_depth_mask | (1 << depth),
172 left_margin);
173 node = next;
174 if (++entries_printed == callchain_param.print_limit)
175 break;
176 }
177
178 if (callchain_param.mode == CHAIN_GRAPH_REL &&
179 remaining && remaining != total_samples) {
5ab250ca
NK
180 struct callchain_node rem_node = {
181 .hit = remaining,
182 };
7ccf4f90
NK
183
184 if (!rem_sq_bracket)
185 return ret;
186
f2af0086
NK
187 if (callchain_param.value == CCVAL_COUNT && child && child->parent) {
188 rem_node.count = child->parent->children_count - cumul_count;
189 if (rem_node.count <= 0)
190 return ret;
191 }
192
7ccf4f90 193 new_depth_mask &= ~(1 << (depth - 1));
5ab250ca 194 ret += ipchain__fprintf_graph(fp, &rem_node, &rem_hits, depth,
7ccf4f90 195 new_depth_mask, 0, total_samples,
5ab250ca 196 left_margin);
7ccf4f90
NK
197 }
198
199 return ret;
200}
201
7ed5d6e2
NK
202/*
203 * If have one single callchain root, don't bother printing
204 * its percentage (100 % in fractal mode and the same percentage
205 * than the hist in graph mode). This also avoid one level of column.
206 *
207 * However when percent-limit applied, it's possible that single callchain
208 * node have different (non-100% in fractal mode) percentage.
209 */
210static bool need_percent_display(struct rb_node *node, u64 parent_samples)
211{
212 struct callchain_node *cnode;
213
214 if (rb_next(node))
215 return true;
216
217 cnode = rb_entry(node, struct callchain_node, rb_node);
218 return callchain_cumul_hits(cnode) != parent_samples;
219}
220
7ccf4f90 221static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root,
54d27b31
NK
222 u64 total_samples, u64 parent_samples,
223 int left_margin)
7ccf4f90
NK
224{
225 struct callchain_node *cnode;
226 struct callchain_list *chain;
227 u32 entries_printed = 0;
228 bool printed = false;
229 struct rb_node *node;
230 int i = 0;
231 int ret = 0;
2989ccaa 232 char bf[1024];
7ccf4f90 233
7ccf4f90 234 node = rb_first(root);
7ed5d6e2 235 if (node && !need_percent_display(node, parent_samples)) {
7ccf4f90
NK
236 cnode = rb_entry(node, struct callchain_node, rb_node);
237 list_for_each_entry(chain, &cnode->val, list) {
238 /*
239 * If we sort by symbol, the first entry is the same than
240 * the symbol. No need to print it otherwise it appears as
241 * displayed twice.
242 */
cfaa154b 243 if (!i++ && field_order == NULL &&
8e99b6d4 244 sort_order && strstarts(sort_order, "sym"))
7ccf4f90 245 continue;
0db64dd0 246
7ccf4f90
NK
247 if (!printed) {
248 ret += callchain__fprintf_left_margin(fp, left_margin);
249 ret += fprintf(fp, "|\n");
250 ret += callchain__fprintf_left_margin(fp, left_margin);
251 ret += fprintf(fp, "---");
252 left_margin += 3;
253 printed = true;
254 } else
255 ret += callchain__fprintf_left_margin(fp, left_margin);
256
8577ae6b
JY
257 ret += fprintf(fp, "%s",
258 callchain_list__sym_name(chain, bf,
259 sizeof(bf),
260 false));
261
262 if (symbol_conf.show_branchflag_count)
263 ret += callchain_list_counts__printf_value(
c4ee0625 264 chain, fp, NULL, 0);
8577ae6b 265 ret += fprintf(fp, "\n");
7ccf4f90
NK
266
267 if (++entries_printed == callchain_param.print_limit)
268 break;
269 }
270 root = &cnode->rb_root;
271 }
272
54d27b31
NK
273 if (callchain_param.mode == CHAIN_GRAPH_REL)
274 total_samples = parent_samples;
275
7ccf4f90
NK
276 ret += __callchain__fprintf_graph(fp, root, total_samples,
277 1, 1, left_margin);
3848c23b
NK
278 if (ret) {
279 /* do not add a blank line if it printed nothing */
280 ret += fprintf(fp, "\n");
281 }
7ccf4f90
NK
282
283 return ret;
284}
285
316c7136 286static size_t __callchain__fprintf_flat(FILE *fp, struct callchain_node *node,
7ccf4f90
NK
287 u64 total_samples)
288{
289 struct callchain_list *chain;
290 size_t ret = 0;
2989ccaa 291 char bf[1024];
7ccf4f90 292
316c7136 293 if (!node)
7ccf4f90
NK
294 return 0;
295
316c7136 296 ret += __callchain__fprintf_flat(fp, node->parent, total_samples);
7ccf4f90
NK
297
298
316c7136 299 list_for_each_entry(chain, &node->val, list) {
7ccf4f90
NK
300 if (chain->ip >= PERF_CONTEXT_MAX)
301 continue;
2989ccaa
AK
302 ret += fprintf(fp, " %s\n", callchain_list__sym_name(chain,
303 bf, sizeof(bf), false));
7ccf4f90
NK
304 }
305
306 return ret;
307}
308
316c7136 309static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *tree,
7ccf4f90
NK
310 u64 total_samples)
311{
312 size_t ret = 0;
313 u32 entries_printed = 0;
7ccf4f90 314 struct callchain_node *chain;
316c7136 315 struct rb_node *rb_node = rb_first(tree);
7ccf4f90 316
7ccf4f90 317 while (rb_node) {
7ccf4f90 318 chain = rb_entry(rb_node, struct callchain_node, rb_node);
7ccf4f90 319
5ab250ca
NK
320 ret += fprintf(fp, " ");
321 ret += callchain_node__fprintf_value(chain, fp, total_samples);
322 ret += fprintf(fp, "\n");
7ccf4f90
NK
323 ret += __callchain__fprintf_flat(fp, chain, total_samples);
324 ret += fprintf(fp, "\n");
325 if (++entries_printed == callchain_param.print_limit)
326 break;
327
328 rb_node = rb_next(rb_node);
329 }
330
331 return ret;
332}
333
26e77924
NK
334static size_t __callchain__fprintf_folded(FILE *fp, struct callchain_node *node)
335{
336 const char *sep = symbol_conf.field_sep ?: ";";
337 struct callchain_list *chain;
338 size_t ret = 0;
339 char bf[1024];
340 bool first;
341
342 if (!node)
343 return 0;
344
345 ret += __callchain__fprintf_folded(fp, node->parent);
346
347 first = (ret == 0);
348 list_for_each_entry(chain, &node->val, list) {
349 if (chain->ip >= PERF_CONTEXT_MAX)
350 continue;
351 ret += fprintf(fp, "%s%s", first ? "" : sep,
352 callchain_list__sym_name(chain,
353 bf, sizeof(bf), false));
354 first = false;
355 }
356
357 return ret;
358}
359
360static size_t callchain__fprintf_folded(FILE *fp, struct rb_root *tree,
361 u64 total_samples)
362{
363 size_t ret = 0;
364 u32 entries_printed = 0;
365 struct callchain_node *chain;
366 struct rb_node *rb_node = rb_first(tree);
367
368 while (rb_node) {
26e77924
NK
369
370 chain = rb_entry(rb_node, struct callchain_node, rb_node);
26e77924 371
5ab250ca
NK
372 ret += callchain_node__fprintf_value(chain, fp, total_samples);
373 ret += fprintf(fp, " ");
26e77924
NK
374 ret += __callchain__fprintf_folded(fp, chain);
375 ret += fprintf(fp, "\n");
376 if (++entries_printed == callchain_param.print_limit)
377 break;
378
379 rb_node = rb_next(rb_node);
380 }
381
382 return ret;
383}
384
7ccf4f90
NK
385static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
386 u64 total_samples, int left_margin,
387 FILE *fp)
388{
54d27b31
NK
389 u64 parent_samples = he->stat.period;
390
391 if (symbol_conf.cumulate_callchain)
392 parent_samples = he->stat_acc->period;
393
7ccf4f90
NK
394 switch (callchain_param.mode) {
395 case CHAIN_GRAPH_REL:
54d27b31
NK
396 return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
397 parent_samples, left_margin);
7ccf4f90
NK
398 break;
399 case CHAIN_GRAPH_ABS:
400 return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
54d27b31 401 parent_samples, left_margin);
7ccf4f90
NK
402 break;
403 case CHAIN_FLAT:
404 return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples);
405 break;
26e77924
NK
406 case CHAIN_FOLDED:
407 return callchain__fprintf_folded(fp, &he->sorted_chain, total_samples);
408 break;
7ccf4f90
NK
409 case CHAIN_NONE:
410 break;
411 default:
412 pr_err("Bad callchain mode\n");
413 }
414
415 return 0;
416}
417
bd28d0c5
JO
418int __hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp,
419 struct perf_hpp_list *hpp_list)
be0e6d10
JO
420{
421 const char *sep = symbol_conf.field_sep;
422 struct perf_hpp_fmt *fmt;
423 char *start = hpp->buf;
424 int ret;
425 bool first = true;
426
427 if (symbol_conf.exclude_other && !he->parent)
428 return 0;
429
9da44db1 430 perf_hpp_list__for_each_format(hpp_list, fmt) {
361459f1 431 if (perf_hpp__should_skip(fmt, he->hists))
e67d49a7
NK
432 continue;
433
be0e6d10
JO
434 /*
435 * If there's no field_sep, we still need
436 * to display initial ' '.
437 */
438 if (!sep || !first) {
439 ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
440 advance_hpp(hpp, ret);
441 } else
442 first = false;
443
9754c4f9 444 if (perf_hpp__use_color() && fmt->color)
be0e6d10
JO
445 ret = fmt->color(fmt, hpp, he);
446 else
447 ret = fmt->entry(fmt, hpp, he);
448
89fee709 449 ret = hist_entry__snprintf_alignment(he, hpp, fmt, ret);
be0e6d10
JO
450 advance_hpp(hpp, ret);
451 }
452
453 return hpp->buf - start;
454}
455
9da44db1
JO
456static int hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp)
457{
458 return __hist_entry__snprintf(he, hpp, he->hists->hpp_list);
459}
460
ef86d68a
NK
461static int hist_entry__hierarchy_fprintf(struct hist_entry *he,
462 struct perf_hpp *hpp,
2dbbe9f2 463 struct hists *hists,
ef86d68a
NK
464 FILE *fp)
465{
466 const char *sep = symbol_conf.field_sep;
467 struct perf_hpp_fmt *fmt;
f58c95e3 468 struct perf_hpp_list_node *fmt_node;
ef86d68a 469 char *buf = hpp->buf;
cb1fab91 470 size_t size = hpp->size;
ef86d68a
NK
471 int ret, printed = 0;
472 bool first = true;
473
474 if (symbol_conf.exclude_other && !he->parent)
475 return 0;
476
477 ret = scnprintf(hpp->buf, hpp->size, "%*s", he->depth * HIERARCHY_INDENT, "");
478 advance_hpp(hpp, ret);
479
f58c95e3
NK
480 /* the first hpp_list_node is for overhead columns */
481 fmt_node = list_first_entry(&hists->hpp_formats,
482 struct perf_hpp_list_node, list);
483 perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
ef86d68a
NK
484 /*
485 * If there's no field_sep, we still need
486 * to display initial ' '.
487 */
488 if (!sep || !first) {
489 ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
490 advance_hpp(hpp, ret);
491 } else
492 first = false;
493
494 if (perf_hpp__use_color() && fmt->color)
495 ret = fmt->color(fmt, hpp, he);
496 else
497 ret = fmt->entry(fmt, hpp, he);
498
499 ret = hist_entry__snprintf_alignment(he, hpp, fmt, ret);
500 advance_hpp(hpp, ret);
501 }
502
1b2dbbf4 503 if (!sep)
ef86d68a 504 ret = scnprintf(hpp->buf, hpp->size, "%*s",
2dbbe9f2 505 (hists->nr_hpp_node - 2) * HIERARCHY_INDENT, "");
ef86d68a
NK
506 advance_hpp(hpp, ret);
507
cb1fab91
NK
508 printed += fprintf(fp, "%s", buf);
509
1b2dbbf4
NK
510 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
511 hpp->buf = buf;
512 hpp->size = size;
513
514 /*
515 * No need to call hist_entry__snprintf_alignment() since this
516 * fmt is always the last column in the hierarchy mode.
517 */
518 if (perf_hpp__use_color() && fmt->color)
519 fmt->color(fmt, hpp, he);
520 else
521 fmt->entry(fmt, hpp, he);
522
523 /*
524 * dynamic entries are right-aligned but we want left-aligned
525 * in the hierarchy mode
526 */
32858480 527 printed += fprintf(fp, "%s%s", sep ?: " ", skip_spaces(buf));
1b2dbbf4
NK
528 }
529 printed += putc('\n', fp);
ef86d68a 530
fabd37b8 531 if (he->leaf && hist_entry__has_callchains(he) && symbol_conf.use_callchain) {
ef86d68a
NK
532 u64 total = hists__total_period(hists);
533
534 printed += hist_entry_callchain__fprintf(he, total, 0, fp);
535 goto out;
536 }
537
538out:
539 return printed;
540}
541
b10c78c5
JY
542static int hist_entry__block_fprintf(struct hist_entry *he,
543 char *bf, size_t size,
544 FILE *fp)
545{
546 struct block_hist *bh = container_of(he, struct block_hist, he);
547 int ret = 0;
548
549 for (unsigned int i = 0; i < bh->block_hists.nr_entries; i++) {
550 struct perf_hpp hpp = {
551 .buf = bf,
552 .size = size,
553 .skip = false,
554 };
555
556 bh->block_idx = i;
557 hist_entry__snprintf(he, &hpp);
558
559 if (!hpp.skip)
560 ret += fprintf(fp, "%s\n", bf);
561 }
562
563 return ret;
564}
565
6f7164fa
JY
566static int hist_entry__individual_block_fprintf(struct hist_entry *he,
567 char *bf, size_t size,
568 FILE *fp)
569{
570 int ret = 0;
571
572 struct perf_hpp hpp = {
573 .buf = bf,
574 .size = size,
575 .skip = false,
576 };
577
578 hist_entry__snprintf(he, &hpp);
579 if (!hpp.skip)
580 ret += fprintf(fp, "%s\n", bf);
581
582 return ret;
583}
584
7ccf4f90 585static int hist_entry__fprintf(struct hist_entry *he, size_t size,
d05e3aae 586 char *bf, size_t bfsz, FILE *fp,
e9de7e2f 587 bool ignore_callchains)
7ccf4f90 588{
7ccf4f90 589 int ret;
0db64dd0 590 int callchain_ret = 0;
ea251d51
NK
591 struct perf_hpp hpp = {
592 .buf = bf,
593 .size = size,
ea251d51 594 };
8f1d1b44 595 struct hists *hists = he->hists;
7e597d32 596 u64 total_period = hists->stats.total_period;
7ccf4f90 597
99cf666c
ACM
598 if (size == 0 || size > bfsz)
599 size = hpp.size = bfsz;
7ccf4f90 600
2dbbe9f2
NK
601 if (symbol_conf.report_hierarchy)
602 return hist_entry__hierarchy_fprintf(he, &hpp, hists, fp);
ef86d68a 603
b10c78c5
JY
604 if (symbol_conf.report_block)
605 return hist_entry__block_fprintf(he, bf, size, fp);
606
6f7164fa
JY
607 if (symbol_conf.report_individual_block)
608 return hist_entry__individual_block_fprintf(he, bf, size, fp);
609
26d8b338 610 hist_entry__snprintf(he, &hpp);
7ccf4f90 611
000078bc 612 ret = fprintf(fp, "%s\n", bf);
7ccf4f90 613
e9de7e2f 614 if (hist_entry__has_callchains(he) && !ignore_callchains)
0db64dd0
JY
615 callchain_ret = hist_entry_callchain__fprintf(he, total_period,
616 0, fp);
617
2a704fc8 618 ret += callchain_ret;
7ccf4f90 619
000078bc 620 return ret;
7ccf4f90
NK
621}
622
2dbbe9f2 623static int print_hierarchy_indent(const char *sep, int indent,
8e2fc44f
NK
624 const char *line, FILE *fp)
625{
b598c34f
ACM
626 int width;
627
2dbbe9f2 628 if (sep != NULL || indent < 2)
8e2fc44f
NK
629 return 0;
630
b598c34f
ACM
631 width = (indent - 2) * HIERARCHY_INDENT;
632
633 return fprintf(fp, "%-*.*s", width, width, line);
8e2fc44f
NK
634}
635
195bc0f8
NK
636static int hists__fprintf_hierarchy_headers(struct hists *hists,
637 struct perf_hpp *hpp, FILE *fp)
8e2fc44f 638{
f58c95e3 639 bool first_node, first_col;
2dbbe9f2 640 int indent;
cb1fab91 641 int depth;
8e2fc44f
NK
642 unsigned width = 0;
643 unsigned header_width = 0;
644 struct perf_hpp_fmt *fmt;
f58c95e3 645 struct perf_hpp_list_node *fmt_node;
29e6392e 646 struct perf_hpp_list *hpp_list = hists->hpp_list;
195bc0f8 647 const char *sep = symbol_conf.field_sep;
8e2fc44f 648
2dbbe9f2 649 indent = hists->nr_hpp_node;
8e2fc44f 650
f58c95e3
NK
651 /* the first hpp_list_node is for overhead columns */
652 fmt_node = list_first_entry(&hists->hpp_formats,
653 struct perf_hpp_list_node, list);
8e2fc44f 654
29e6392e
NK
655 for (int line = 0; line < hpp_list->nr_header_lines; line++) {
656 /* first # is displayed one level up */
657 if (line)
658 fprintf(fp, "# ");
8e2fc44f 659
29e6392e
NK
660 /* preserve max indent depth for column headers */
661 print_hierarchy_indent(sep, indent, " ", fp);
8e2fc44f 662
f58c95e3 663 perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
29e6392e
NK
664 fmt->header(fmt, hpp, hists, line, NULL);
665 fprintf(fp, "%s%s", hpp->buf, sep ?: " ");
666 }
f58c95e3 667
29e6392e
NK
668 if (line < hpp_list->nr_header_lines - 1)
669 goto next_line;
f58c95e3 670
29e6392e
NK
671 /* combine sort headers with ' / ' */
672 first_node = true;
673 list_for_each_entry_continue(fmt_node, &hists->hpp_formats, list) {
674 if (!first_node)
675 header_width += fprintf(fp, " / ");
676 first_node = false;
8e2fc44f 677
29e6392e
NK
678 first_col = true;
679 perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
680 if (perf_hpp__should_skip(fmt, hists))
681 continue;
682
683 if (!first_col)
684 header_width += fprintf(fp, "+");
685 first_col = false;
686
687 fmt->header(fmt, hpp, hists, line, NULL);
688
689 header_width += fprintf(fp, "%s", strim(hpp->buf));
690 }
f58c95e3 691 }
29e6392e
NK
692
693next_line:
694 fprintf(fp, "\n");
8e2fc44f
NK
695 }
696
29e6392e 697 fprintf(fp, "# ");
8e2fc44f
NK
698
699 /* preserve max indent depth for initial dots */
2dbbe9f2 700 print_hierarchy_indent(sep, indent, dots, fp);
8e2fc44f 701
f58c95e3
NK
702 /* the first hpp_list_node is for overhead columns */
703 fmt_node = list_first_entry(&hists->hpp_formats,
704 struct perf_hpp_list_node, list);
8e2fc44f 705
f58c95e3
NK
706 first_col = true;
707 perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
708 if (!first_col)
709 fprintf(fp, "%s", sep ?: "..");
710 first_col = false;
8e2fc44f 711
da1b0407 712 width = fmt->width(fmt, hpp, hists);
8e2fc44f
NK
713 fprintf(fp, "%.*s", width, dots);
714 }
715
cb1fab91 716 depth = 0;
f58c95e3
NK
717 list_for_each_entry_continue(fmt_node, &hists->hpp_formats, list) {
718 first_col = true;
719 width = depth * HIERARCHY_INDENT;
8e2fc44f 720
f58c95e3
NK
721 perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
722 if (perf_hpp__should_skip(fmt, hists))
723 continue;
724
725 if (!first_col)
726 width++; /* for '+' sign between column header */
727 first_col = false;
728
da1b0407 729 width += fmt->width(fmt, hpp, hists);
f58c95e3 730 }
cb1fab91 731
8e2fc44f
NK
732 if (width > header_width)
733 header_width = width;
cb1fab91
NK
734
735 depth++;
8e2fc44f
NK
736 }
737
738 fprintf(fp, "%s%-.*s", sep ?: " ", header_width, dots);
739
8e2fc44f
NK
740 fprintf(fp, "\n#\n");
741
742 return 2;
743}
744
f3705b06
JO
745static void fprintf_line(struct hists *hists, struct perf_hpp *hpp,
746 int line, FILE *fp)
7ccf4f90 747{
1240005e 748 struct perf_hpp_fmt *fmt;
7ccf4f90 749 const char *sep = symbol_conf.field_sep;
5395a048 750 bool first = true;
29659ab4 751 int span = 0;
26d8b338 752
f0786af5 753 hists__for_each_format(hists, fmt) {
361459f1 754 if (perf_hpp__should_skip(fmt, hists))
e67d49a7
NK
755 continue;
756
29659ab4 757 if (!first && !span)
26d8b338
NK
758 fprintf(fp, "%s", sep ?: " ");
759 else
760 first = false;
761
29659ab4
JO
762 fmt->header(fmt, hpp, hists, line, &span);
763
764 if (!span)
765 fprintf(fp, "%s", hpp->buf);
7ccf4f90 766 }
f3705b06 767}
7ccf4f90 768
f3705b06
JO
769static int
770hists__fprintf_standard_headers(struct hists *hists,
771 struct perf_hpp *hpp,
772 FILE *fp)
773{
774 struct perf_hpp_list *hpp_list = hists->hpp_list;
775 struct perf_hpp_fmt *fmt;
776 unsigned int width;
777 const char *sep = symbol_conf.field_sep;
778 bool first = true;
779 int line;
780
781 for (line = 0; line < hpp_list->nr_header_lines; line++) {
782 /* first # is displayed one level up */
783 if (line)
784 fprintf(fp, "# ");
785 fprintf_line(hists, hpp, line, fp);
786 fprintf(fp, "\n");
787 }
7ccf4f90
NK
788
789 if (sep)
f3705b06 790 return hpp_list->nr_header_lines;
7ccf4f90 791
5395a048
JO
792 first = true;
793
ea251d51 794 fprintf(fp, "# ");
ea251d51 795
f0786af5 796 hists__for_each_format(hists, fmt) {
1240005e 797 unsigned int i;
ea251d51 798
361459f1 799 if (perf_hpp__should_skip(fmt, hists))
e67d49a7
NK
800 continue;
801
5395a048 802 if (!first)
ea251d51 803 fprintf(fp, "%s", sep ?: " ");
5395a048
JO
804 else
805 first = false;
ea251d51 806
da1b0407 807 width = fmt->width(fmt, hpp, hists);
ea251d51
NK
808 for (i = 0; i < width; i++)
809 fprintf(fp, ".");
7ccf4f90 810 }
ea251d51 811
7ccf4f90 812 fprintf(fp, "\n");
7ccf4f90 813 fprintf(fp, "#\n");
f3705b06 814 return hpp_list->nr_header_lines + 2;
36592ebb
JO
815}
816
2d831454 817int hists__fprintf_headers(struct hists *hists, FILE *fp)
7a72a2e5 818{
d5278220 819 char bf[1024];
7a72a2e5
JO
820 struct perf_hpp dummy_hpp = {
821 .buf = bf,
822 .size = sizeof(bf),
823 };
824
825 fprintf(fp, "# ");
826
827 if (symbol_conf.report_hierarchy)
828 return hists__fprintf_hierarchy_headers(hists, &dummy_hpp, fp);
829 else
830 return hists__fprintf_standard_headers(hists, &dummy_hpp, fp);
831
832}
833
36592ebb 834size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
d05e3aae 835 int max_cols, float min_pcnt, FILE *fp,
e9de7e2f 836 bool ignore_callchains)
36592ebb 837{
36592ebb
JO
838 struct rb_node *nd;
839 size_t ret = 0;
840 const char *sep = symbol_conf.field_sep;
841 int nr_rows = 0;
842 size_t linesz;
843 char *line = NULL;
844 unsigned indent;
845
846 init_rem_hits();
847
e3b60bc9 848 hists__reset_column_width(hists);
36592ebb
JO
849
850 if (symbol_conf.col_width_list_str)
851 perf_hpp__set_user_width(symbol_conf.col_width_list_str);
852
853 if (show_header)
854 nr_rows += hists__fprintf_headers(hists, fp);
855
856 if (max_rows && nr_rows >= max_rows)
7ccf4f90
NK
857 goto out;
858
99cf666c 859 linesz = hists__sort_list_width(hists) + 3 + 1;
9754c4f9 860 linesz += perf_hpp__color_overhead();
99cf666c
ACM
861 line = malloc(linesz);
862 if (line == NULL) {
863 ret = -1;
864 goto out;
865 }
866
bd4abd39
NK
867 indent = hists__overhead_width(hists) + 4;
868
2eb3d689
DB
869 for (nd = rb_first_cached(&hists->entries); nd;
870 nd = __rb_hierarchy_next(nd, HMD_FORCE_CHILD)) {
7ccf4f90 871 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
14135663 872 float percent;
7ccf4f90
NK
873
874 if (h->filtered)
875 continue;
876
0b49f836
JY
877 if (symbol_conf.report_individual_block)
878 percent = block_info__total_cycles_percent(h);
879 else
880 percent = hist_entry__get_percent_limit(h);
881
064f1981
NK
882 if (percent < min_pcnt)
883 continue;
884
e9de7e2f 885 ret += hist_entry__fprintf(h, max_cols, line, linesz, fp, ignore_callchains);
7ccf4f90 886
7ccf4f90 887 if (max_rows && ++nr_rows >= max_rows)
99cf666c 888 break;
7ccf4f90 889
bd4abd39
NK
890 /*
891 * If all children are filtered out or percent-limited,
892 * display "no entry >= x.xx%" message.
893 */
894 if (!h->leaf && !hist_entry__has_hierarchy_children(h, min_pcnt)) {
f58c95e3 895 int depth = hists->nr_hpp_node + h->depth + 1;
bd4abd39 896
b598c34f 897 print_hierarchy_indent(sep, depth, " ", fp);
bd4abd39
NK
898 fprintf(fp, "%*sno entry >= %.2f%%\n", indent, "", min_pcnt);
899
900 if (max_rows && ++nr_rows >= max_rows)
901 break;
902 }
903
7ccf4f90 904 if (h->ms.map == NULL && verbose > 1) {
ee84a303 905 maps__fprintf(thread__maps(h->thread), fp);
7ccf4f90
NK
906 fprintf(fp, "%.10s end\n", graph_dotted_line);
907 }
908 }
99cf666c
ACM
909
910 free(line);
7ccf4f90 911out:
74cf249d 912 zfree(&rem_sq_bracket);
7ccf4f90
NK
913
914 return ret;
915}
916
411ee135 917size_t events_stats__fprintf(struct events_stats *stats, FILE *fp)
7ccf4f90
NK
918{
919 int i;
920 size_t ret = 0;
462f57db 921 u32 total = stats->nr_events[0];
7ccf4f90
NK
922
923 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
924 const char *name;
925
7ccf4f90
NK
926 name = perf_event__name(i);
927 if (!strcmp(name, "UNKNOWN"))
928 continue;
411ee135 929 if (symbol_conf.skip_empty && !stats->nr_events[i])
2775de0b 930 continue;
7ccf4f90 931
462f57db 932 if (i && total) {
1a5474a7 933 ret += fprintf(fp, "%20s events: %10d (%4.1f%%)\n",
462f57db
NK
934 name, stats->nr_events[i],
935 100.0 * stats->nr_events[i] / total);
936 } else {
1a5474a7 937 ret += fprintf(fp, "%20s events: %10d\n",
462f57db
NK
938 name, stats->nr_events[i]);
939 }
7ccf4f90
NK
940 }
941
942 return ret;
943}