1 // SPDX-License-Identifier: GPL-2.0
5 #include <linux/string.h>
7 #include "../../util/callchain.h"
8 #include "../../util/debug.h"
9 #include "../../util/event.h"
10 #include "../../util/hist.h"
11 #include "../../util/map.h"
12 #include "../../util/maps.h"
13 #include "../../util/symbol.h"
14 #include "../../util/sort.h"
15 #include "../../util/evsel.h"
16 #include "../../util/srcline.h"
17 #include "../../util/string2.h"
18 #include "../../util/thread.h"
19 #include "../../util/block-info.h"
20 #include <linux/ctype.h>
21 #include <linux/zalloc.h>
23 static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
26 int ret = fprintf(fp, " ");
28 if (left_margin > USHRT_MAX)
29 left_margin = USHRT_MAX;
31 for (i = 0; i < left_margin; i++)
32 ret += fprintf(fp, " ");
37 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
41 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
43 for (i = 0; i < depth; i++)
44 if (depth_mask & (1 << i))
45 ret += fprintf(fp, "| ");
47 ret += fprintf(fp, " ");
49 ret += fprintf(fp, "\n");
54 static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_node *node,
55 struct callchain_list *chain,
56 int depth, int depth_mask, int period,
57 u64 total_samples, int left_margin)
61 char bf[1024], *alloc_str = NULL;
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, "|");
70 ret += fprintf(fp, " ");
71 if (!period && i == depth - 1) {
72 ret += fprintf(fp, "--");
73 ret += callchain_node__fprintf_value(node, fp, total_samples);
74 ret += fprintf(fp, "--");
76 ret += fprintf(fp, "%s", " ");
79 str = callchain_list__sym_name(chain, bf, sizeof(bf), false);
81 if (symbol_conf.show_branchflag_count) {
82 callchain_list_counts__printf_value(chain, NULL,
85 if (asprintf(&alloc_str, "%s%s", str, buf) < 0)
86 str = "Not enough memory!";
98 static struct symbol *rem_sq_bracket;
99 static struct callchain_list rem_hits;
101 static void init_rem_hits(void)
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");
109 strcpy(rem_sq_bracket->name, "[...]");
110 rem_hits.ms.sym = rem_sq_bracket;
113 static size_t __callchain__fprintf_graph(FILE *fp, struct rb_root *root,
114 u64 total_samples, int depth,
115 int depth_mask, int left_margin)
117 struct rb_node *node, *next;
118 struct callchain_node *child = NULL;
119 struct callchain_list *chain;
120 int new_depth_mask = depth_mask;
124 uint entries_printed = 0;
127 remaining = total_samples;
129 node = rb_first(root);
134 child = rb_entry(node, struct callchain_node, rb_node);
135 cumul = callchain_cumul_hits(child);
137 cumul_count += callchain_cumul_counts(child);
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
146 next = rb_next(node);
147 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
148 new_depth_mask &= ~(1 << (depth - 1));
151 * But we keep the older depth mask for the line separator
152 * to keep the level link until we reach the last child
154 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
157 list_for_each_entry(chain, &child->val, list) {
158 ret += ipchain__fprintf_graph(fp, child, chain, depth,
164 if (callchain_param.mode == CHAIN_GRAPH_REL)
165 new_total = child->children_hit;
167 new_total = total_samples;
169 ret += __callchain__fprintf_graph(fp, &child->rb_root, new_total,
171 new_depth_mask | (1 << depth),
174 if (++entries_printed == callchain_param.print_limit)
178 if (callchain_param.mode == CHAIN_GRAPH_REL &&
179 remaining && remaining != total_samples) {
180 struct callchain_node rem_node = {
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)
193 new_depth_mask &= ~(1 << (depth - 1));
194 ret += ipchain__fprintf_graph(fp, &rem_node, &rem_hits, depth,
195 new_depth_mask, 0, total_samples,
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.
207 * However when percent-limit applied, it's possible that single callchain
208 * node have different (non-100% in fractal mode) percentage.
210 static bool need_percent_display(struct rb_node *node, u64 parent_samples)
212 struct callchain_node *cnode;
217 cnode = rb_entry(node, struct callchain_node, rb_node);
218 return callchain_cumul_hits(cnode) != parent_samples;
221 static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root,
222 u64 total_samples, u64 parent_samples,
225 struct callchain_node *cnode;
226 struct callchain_list *chain;
227 u32 entries_printed = 0;
228 bool printed = false;
229 struct rb_node *node;
234 node = rb_first(root);
235 if (node && !need_percent_display(node, parent_samples)) {
236 cnode = rb_entry(node, struct callchain_node, rb_node);
237 list_for_each_entry(chain, &cnode->val, list) {
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
243 if (!i++ && field_order == NULL &&
244 sort_order && strstarts(sort_order, "sym"))
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, "---");
255 ret += callchain__fprintf_left_margin(fp, left_margin);
257 ret += fprintf(fp, "%s",
258 callchain_list__sym_name(chain, bf,
262 if (symbol_conf.show_branchflag_count)
263 ret += callchain_list_counts__printf_value(
265 ret += fprintf(fp, "\n");
267 if (++entries_printed == callchain_param.print_limit)
270 root = &cnode->rb_root;
273 if (callchain_param.mode == CHAIN_GRAPH_REL)
274 total_samples = parent_samples;
276 ret += __callchain__fprintf_graph(fp, root, total_samples,
279 /* do not add a blank line if it printed nothing */
280 ret += fprintf(fp, "\n");
286 static size_t __callchain__fprintf_flat(FILE *fp, struct callchain_node *node,
289 struct callchain_list *chain;
296 ret += __callchain__fprintf_flat(fp, node->parent, total_samples);
299 list_for_each_entry(chain, &node->val, list) {
300 if (chain->ip >= PERF_CONTEXT_MAX)
302 ret += fprintf(fp, " %s\n", callchain_list__sym_name(chain,
303 bf, sizeof(bf), false));
309 static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *tree,
313 u32 entries_printed = 0;
314 struct callchain_node *chain;
315 struct rb_node *rb_node = rb_first(tree);
318 chain = rb_entry(rb_node, struct callchain_node, rb_node);
320 ret += fprintf(fp, " ");
321 ret += callchain_node__fprintf_value(chain, fp, total_samples);
322 ret += fprintf(fp, "\n");
323 ret += __callchain__fprintf_flat(fp, chain, total_samples);
324 ret += fprintf(fp, "\n");
325 if (++entries_printed == callchain_param.print_limit)
328 rb_node = rb_next(rb_node);
334 static size_t __callchain__fprintf_folded(FILE *fp, struct callchain_node *node)
336 const char *sep = symbol_conf.field_sep ?: ";";
337 struct callchain_list *chain;
345 ret += __callchain__fprintf_folded(fp, node->parent);
348 list_for_each_entry(chain, &node->val, list) {
349 if (chain->ip >= PERF_CONTEXT_MAX)
351 ret += fprintf(fp, "%s%s", first ? "" : sep,
352 callchain_list__sym_name(chain,
353 bf, sizeof(bf), false));
360 static size_t callchain__fprintf_folded(FILE *fp, struct rb_root *tree,
364 u32 entries_printed = 0;
365 struct callchain_node *chain;
366 struct rb_node *rb_node = rb_first(tree);
370 chain = rb_entry(rb_node, struct callchain_node, rb_node);
372 ret += callchain_node__fprintf_value(chain, fp, total_samples);
373 ret += fprintf(fp, " ");
374 ret += __callchain__fprintf_folded(fp, chain);
375 ret += fprintf(fp, "\n");
376 if (++entries_printed == callchain_param.print_limit)
379 rb_node = rb_next(rb_node);
385 static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
386 u64 total_samples, int left_margin,
389 u64 parent_samples = he->stat.period;
391 if (symbol_conf.cumulate_callchain)
392 parent_samples = he->stat_acc->period;
394 switch (callchain_param.mode) {
395 case CHAIN_GRAPH_REL:
396 return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
397 parent_samples, left_margin);
399 case CHAIN_GRAPH_ABS:
400 return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
401 parent_samples, left_margin);
404 return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples);
407 return callchain__fprintf_folded(fp, &he->sorted_chain, total_samples);
412 pr_err("Bad callchain mode\n");
418 int __hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp,
419 struct perf_hpp_list *hpp_list)
421 const char *sep = symbol_conf.field_sep;
422 struct perf_hpp_fmt *fmt;
423 char *start = hpp->buf;
427 if (symbol_conf.exclude_other && !he->parent)
430 perf_hpp_list__for_each_format(hpp_list, fmt) {
431 if (perf_hpp__should_skip(fmt, he->hists))
435 * If there's no field_sep, we still need
436 * to display initial ' '.
438 if (!sep || !first) {
439 ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
440 advance_hpp(hpp, ret);
444 if (perf_hpp__use_color() && fmt->color)
445 ret = fmt->color(fmt, hpp, he);
447 ret = fmt->entry(fmt, hpp, he);
449 ret = hist_entry__snprintf_alignment(he, hpp, fmt, ret);
450 advance_hpp(hpp, ret);
453 return hpp->buf - start;
456 static int hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp)
458 return __hist_entry__snprintf(he, hpp, he->hists->hpp_list);
461 static int hist_entry__hierarchy_fprintf(struct hist_entry *he,
462 struct perf_hpp *hpp,
466 const char *sep = symbol_conf.field_sep;
467 struct perf_hpp_fmt *fmt;
468 struct perf_hpp_list_node *fmt_node;
469 char *buf = hpp->buf;
470 size_t size = hpp->size;
471 int ret, printed = 0;
474 if (symbol_conf.exclude_other && !he->parent)
477 ret = scnprintf(hpp->buf, hpp->size, "%*s", he->depth * HIERARCHY_INDENT, "");
478 advance_hpp(hpp, ret);
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) {
485 * If there's no field_sep, we still need
486 * to display initial ' '.
488 if (!sep || !first) {
489 ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
490 advance_hpp(hpp, ret);
494 if (perf_hpp__use_color() && fmt->color)
495 ret = fmt->color(fmt, hpp, he);
497 ret = fmt->entry(fmt, hpp, he);
499 ret = hist_entry__snprintf_alignment(he, hpp, fmt, ret);
500 advance_hpp(hpp, ret);
504 ret = scnprintf(hpp->buf, hpp->size, "%*s",
505 (hists->nr_hpp_node - 2) * HIERARCHY_INDENT, "");
506 advance_hpp(hpp, ret);
508 printed += fprintf(fp, "%s", buf);
510 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
515 * No need to call hist_entry__snprintf_alignment() since this
516 * fmt is always the last column in the hierarchy mode.
518 if (perf_hpp__use_color() && fmt->color)
519 fmt->color(fmt, hpp, he);
521 fmt->entry(fmt, hpp, he);
524 * dynamic entries are right-aligned but we want left-aligned
525 * in the hierarchy mode
527 printed += fprintf(fp, "%s%s", sep ?: " ", skip_spaces(buf));
529 printed += putc('\n', fp);
531 if (he->leaf && hist_entry__has_callchains(he) && symbol_conf.use_callchain) {
532 u64 total = hists__total_period(hists);
534 printed += hist_entry_callchain__fprintf(he, total, 0, fp);
542 static int hist_entry__block_fprintf(struct hist_entry *he,
543 char *bf, size_t size,
546 struct block_hist *bh = container_of(he, struct block_hist, he);
549 for (unsigned int i = 0; i < bh->block_hists.nr_entries; i++) {
550 struct perf_hpp hpp = {
557 hist_entry__snprintf(he, &hpp);
560 ret += fprintf(fp, "%s\n", bf);
566 static int hist_entry__individual_block_fprintf(struct hist_entry *he,
567 char *bf, size_t size,
572 struct perf_hpp hpp = {
578 hist_entry__snprintf(he, &hpp);
580 ret += fprintf(fp, "%s\n", bf);
585 static int hist_entry__fprintf(struct hist_entry *he, size_t size,
586 char *bf, size_t bfsz, FILE *fp,
587 bool ignore_callchains)
590 int callchain_ret = 0;
591 struct perf_hpp hpp = {
595 struct hists *hists = he->hists;
596 u64 total_period = hists->stats.total_period;
598 if (size == 0 || size > bfsz)
599 size = hpp.size = bfsz;
601 if (symbol_conf.report_hierarchy)
602 return hist_entry__hierarchy_fprintf(he, &hpp, hists, fp);
604 if (symbol_conf.report_block)
605 return hist_entry__block_fprintf(he, bf, size, fp);
607 if (symbol_conf.report_individual_block)
608 return hist_entry__individual_block_fprintf(he, bf, size, fp);
610 hist_entry__snprintf(he, &hpp);
612 ret = fprintf(fp, "%s\n", bf);
614 if (hist_entry__has_callchains(he) && !ignore_callchains)
615 callchain_ret = hist_entry_callchain__fprintf(he, total_period,
618 ret += callchain_ret;
623 static int print_hierarchy_indent(const char *sep, int indent,
624 const char *line, FILE *fp)
628 if (sep != NULL || indent < 2)
631 width = (indent - 2) * HIERARCHY_INDENT;
633 return fprintf(fp, "%-*.*s", width, width, line);
636 static int hists__fprintf_hierarchy_headers(struct hists *hists,
637 struct perf_hpp *hpp, FILE *fp)
639 bool first_node, first_col;
643 unsigned header_width = 0;
644 struct perf_hpp_fmt *fmt;
645 struct perf_hpp_list_node *fmt_node;
646 struct perf_hpp_list *hpp_list = hists->hpp_list;
647 const char *sep = symbol_conf.field_sep;
649 indent = hists->nr_hpp_node;
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);
655 for (int line = 0; line < hpp_list->nr_header_lines; line++) {
656 /* first # is displayed one level up */
660 /* preserve max indent depth for column headers */
661 print_hierarchy_indent(sep, indent, " ", fp);
663 perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
664 fmt->header(fmt, hpp, hists, line, NULL);
665 fprintf(fp, "%s%s", hpp->buf, sep ?: " ");
668 if (line < hpp_list->nr_header_lines - 1)
671 /* combine sort headers with ' / ' */
673 list_for_each_entry_continue(fmt_node, &hists->hpp_formats, list) {
675 header_width += fprintf(fp, " / ");
679 perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
680 if (perf_hpp__should_skip(fmt, hists))
684 header_width += fprintf(fp, "+");
687 fmt->header(fmt, hpp, hists, line, NULL);
689 header_width += fprintf(fp, "%s", strim(hpp->buf));
699 /* preserve max indent depth for initial dots */
700 print_hierarchy_indent(sep, indent, dots, fp);
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);
707 perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
709 fprintf(fp, "%s", sep ?: "..");
712 width = fmt->width(fmt, hpp, hists);
713 fprintf(fp, "%.*s", width, dots);
717 list_for_each_entry_continue(fmt_node, &hists->hpp_formats, list) {
719 width = depth * HIERARCHY_INDENT;
721 perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
722 if (perf_hpp__should_skip(fmt, hists))
726 width++; /* for '+' sign between column header */
729 width += fmt->width(fmt, hpp, hists);
732 if (width > header_width)
733 header_width = width;
738 fprintf(fp, "%s%-.*s", sep ?: " ", header_width, dots);
740 fprintf(fp, "\n#\n");
745 static void fprintf_line(struct hists *hists, struct perf_hpp *hpp,
748 struct perf_hpp_fmt *fmt;
749 const char *sep = symbol_conf.field_sep;
753 hists__for_each_format(hists, fmt) {
754 if (perf_hpp__should_skip(fmt, hists))
758 fprintf(fp, "%s", sep ?: " ");
762 fmt->header(fmt, hpp, hists, line, &span);
765 fprintf(fp, "%s", hpp->buf);
770 hists__fprintf_standard_headers(struct hists *hists,
771 struct perf_hpp *hpp,
774 struct perf_hpp_list *hpp_list = hists->hpp_list;
775 struct perf_hpp_fmt *fmt;
777 const char *sep = symbol_conf.field_sep;
781 for (line = 0; line < hpp_list->nr_header_lines; line++) {
782 /* first # is displayed one level up */
785 fprintf_line(hists, hpp, line, fp);
790 return hpp_list->nr_header_lines;
796 hists__for_each_format(hists, fmt) {
799 if (perf_hpp__should_skip(fmt, hists))
803 fprintf(fp, "%s", sep ?: " ");
807 width = fmt->width(fmt, hpp, hists);
808 for (i = 0; i < width; i++)
814 return hpp_list->nr_header_lines + 2;
817 int hists__fprintf_headers(struct hists *hists, FILE *fp)
820 struct perf_hpp dummy_hpp = {
827 if (symbol_conf.report_hierarchy)
828 return hists__fprintf_hierarchy_headers(hists, &dummy_hpp, fp);
830 return hists__fprintf_standard_headers(hists, &dummy_hpp, fp);
834 size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
835 int max_cols, float min_pcnt, FILE *fp,
836 bool ignore_callchains)
840 const char *sep = symbol_conf.field_sep;
848 hists__reset_column_width(hists);
850 if (symbol_conf.col_width_list_str)
851 perf_hpp__set_user_width(symbol_conf.col_width_list_str);
854 nr_rows += hists__fprintf_headers(hists, fp);
856 if (max_rows && nr_rows >= max_rows)
859 linesz = hists__sort_list_width(hists) + 3 + 1;
860 linesz += perf_hpp__color_overhead();
861 line = malloc(linesz);
867 indent = hists__overhead_width(hists) + 4;
869 for (nd = rb_first_cached(&hists->entries); nd;
870 nd = __rb_hierarchy_next(nd, HMD_FORCE_CHILD)) {
871 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
877 if (symbol_conf.report_individual_block)
878 percent = block_info__total_cycles_percent(h);
880 percent = hist_entry__get_percent_limit(h);
882 if (percent < min_pcnt)
885 ret += hist_entry__fprintf(h, max_cols, line, linesz, fp, ignore_callchains);
887 if (max_rows && ++nr_rows >= max_rows)
891 * If all children are filtered out or percent-limited,
892 * display "no entry >= x.xx%" message.
894 if (!h->leaf && !hist_entry__has_hierarchy_children(h, min_pcnt)) {
895 int depth = hists->nr_hpp_node + h->depth + 1;
897 print_hierarchy_indent(sep, depth, " ", fp);
898 fprintf(fp, "%*sno entry >= %.2f%%\n", indent, "", min_pcnt);
900 if (max_rows && ++nr_rows >= max_rows)
904 if (h->ms.map == NULL && verbose > 1) {
905 maps__fprintf(thread__maps(h->thread), fp);
906 fprintf(fp, "%.10s end\n", graph_dotted_line);
912 zfree(&rem_sq_bracket);
917 size_t events_stats__fprintf(struct events_stats *stats, FILE *fp)
921 u32 total = stats->nr_events[0];
923 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
926 name = perf_event__name(i);
927 if (!strcmp(name, "UNKNOWN"))
929 if (symbol_conf.skip_empty && !stats->nr_events[i])
933 ret += fprintf(fp, "%20s events: %10d (%4.1f%%)\n",
934 name, stats->nr_events[i],
935 100.0 * stats->nr_events[i] / total);
937 ret += fprintf(fp, "%20s events: %10d\n",
938 name, stats->nr_events[i]);