3 #include "../../util/util.h"
4 #include "../../util/hist.h"
5 #include "../../util/sort.h"
6 #include "../../util/evsel.h"
7 #include "../../util/srcline.h"
8 #include "../../util/string2.h"
9 #include "../../util/thread.h"
10 #include "../../util/sane_ctype.h"
12 static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
15 int ret = fprintf(fp, " ");
17 for (i = 0; i < left_margin; i++)
18 ret += fprintf(fp, " ");
23 static size_t inline__fprintf(struct map *map, u64 ip, int left_margin,
24 int depth, int depth_mask, FILE *fp)
27 struct inline_node *node;
28 struct inline_list *ilist;
38 if (dso->kernel != DSO_TYPE_USER)
41 node = dso__parse_addr_inlines(dso,
42 map__rip_2objdump(map, ip));
46 list_for_each_entry(ilist, &node->val, list) {
47 if ((ilist->filename != NULL) || (ilist->funcname != NULL)) {
48 ret += callchain__fprintf_left_margin(fp, left_margin);
50 for (i = 0; i < depth; i++) {
51 if (depth_mask & (1 << i))
52 ret += fprintf(fp, "|");
54 ret += fprintf(fp, " ");
55 ret += fprintf(fp, " ");
58 if (callchain_param.key == CCKEY_ADDRESS ||
59 callchain_param.key == CCKEY_SRCLINE) {
60 if (ilist->filename != NULL)
61 ret += fprintf(fp, "%s:%d (inline)",
65 ret += fprintf(fp, "??");
66 } else if (ilist->funcname != NULL)
67 ret += fprintf(fp, "%s (inline)",
69 else if (ilist->filename != NULL)
70 ret += fprintf(fp, "%s:%d (inline)",
74 ret += fprintf(fp, "??");
76 ret += fprintf(fp, "\n");
80 inline_node__delete(node);
84 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
88 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
90 for (i = 0; i < depth; i++)
91 if (depth_mask & (1 << i))
92 ret += fprintf(fp, "| ");
94 ret += fprintf(fp, " ");
96 ret += fprintf(fp, "\n");
101 static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_node *node,
102 struct callchain_list *chain,
103 int depth, int depth_mask, int period,
104 u64 total_samples, int left_margin)
108 char bf[1024], *alloc_str = NULL;
112 ret += callchain__fprintf_left_margin(fp, left_margin);
113 for (i = 0; i < depth; i++) {
114 if (depth_mask & (1 << i))
115 ret += fprintf(fp, "|");
117 ret += fprintf(fp, " ");
118 if (!period && i == depth - 1) {
119 ret += fprintf(fp, "--");
120 ret += callchain_node__fprintf_value(node, fp, total_samples);
121 ret += fprintf(fp, "--");
123 ret += fprintf(fp, "%s", " ");
126 str = callchain_list__sym_name(chain, bf, sizeof(bf), false);
128 if (symbol_conf.show_branchflag_count) {
130 callchain_list_counts__printf_value(node, chain, NULL,
133 callchain_list_counts__printf_value(NULL, chain, NULL,
136 if (asprintf(&alloc_str, "%s%s", str, buf) < 0)
137 str = "Not enough memory!";
146 if (symbol_conf.inline_name)
147 ret += inline__fprintf(chain->ms.map, chain->ip,
148 left_margin, depth, depth_mask, fp);
152 static struct symbol *rem_sq_bracket;
153 static struct callchain_list rem_hits;
155 static void init_rem_hits(void)
157 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
158 if (!rem_sq_bracket) {
159 fprintf(stderr, "Not enough memory to display remaining hits\n");
163 strcpy(rem_sq_bracket->name, "[...]");
164 rem_hits.ms.sym = rem_sq_bracket;
167 static size_t __callchain__fprintf_graph(FILE *fp, struct rb_root *root,
168 u64 total_samples, int depth,
169 int depth_mask, int left_margin)
171 struct rb_node *node, *next;
172 struct callchain_node *child = NULL;
173 struct callchain_list *chain;
174 int new_depth_mask = depth_mask;
178 uint entries_printed = 0;
181 remaining = total_samples;
183 node = rb_first(root);
188 child = rb_entry(node, struct callchain_node, rb_node);
189 cumul = callchain_cumul_hits(child);
191 cumul_count += callchain_cumul_counts(child);
194 * The depth mask manages the output of pipes that show
195 * the depth. We don't want to keep the pipes of the current
196 * level for the last child of this depth.
197 * Except if we have remaining filtered hits. They will
198 * supersede the last child
200 next = rb_next(node);
201 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
202 new_depth_mask &= ~(1 << (depth - 1));
205 * But we keep the older depth mask for the line separator
206 * to keep the level link until we reach the last child
208 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
211 list_for_each_entry(chain, &child->val, list) {
212 ret += ipchain__fprintf_graph(fp, child, chain, depth,
218 if (callchain_param.mode == CHAIN_GRAPH_REL)
219 new_total = child->children_hit;
221 new_total = total_samples;
223 ret += __callchain__fprintf_graph(fp, &child->rb_root, new_total,
225 new_depth_mask | (1 << depth),
228 if (++entries_printed == callchain_param.print_limit)
232 if (callchain_param.mode == CHAIN_GRAPH_REL &&
233 remaining && remaining != total_samples) {
234 struct callchain_node rem_node = {
241 if (callchain_param.value == CCVAL_COUNT && child && child->parent) {
242 rem_node.count = child->parent->children_count - cumul_count;
243 if (rem_node.count <= 0)
247 new_depth_mask &= ~(1 << (depth - 1));
248 ret += ipchain__fprintf_graph(fp, &rem_node, &rem_hits, depth,
249 new_depth_mask, 0, total_samples,
257 * If have one single callchain root, don't bother printing
258 * its percentage (100 % in fractal mode and the same percentage
259 * than the hist in graph mode). This also avoid one level of column.
261 * However when percent-limit applied, it's possible that single callchain
262 * node have different (non-100% in fractal mode) percentage.
264 static bool need_percent_display(struct rb_node *node, u64 parent_samples)
266 struct callchain_node *cnode;
271 cnode = rb_entry(node, struct callchain_node, rb_node);
272 return callchain_cumul_hits(cnode) != parent_samples;
275 static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root,
276 u64 total_samples, u64 parent_samples,
279 struct callchain_node *cnode;
280 struct callchain_list *chain;
281 u32 entries_printed = 0;
282 bool printed = false;
283 struct rb_node *node;
288 node = rb_first(root);
289 if (node && !need_percent_display(node, parent_samples)) {
290 cnode = rb_entry(node, struct callchain_node, rb_node);
291 list_for_each_entry(chain, &cnode->val, list) {
293 * If we sort by symbol, the first entry is the same than
294 * the symbol. No need to print it otherwise it appears as
297 if (!i++ && field_order == NULL &&
298 sort_order && !prefixcmp(sort_order, "sym"))
302 ret += callchain__fprintf_left_margin(fp, left_margin);
303 ret += fprintf(fp, "|\n");
304 ret += callchain__fprintf_left_margin(fp, left_margin);
305 ret += fprintf(fp, "---");
309 ret += callchain__fprintf_left_margin(fp, left_margin);
311 ret += fprintf(fp, "%s",
312 callchain_list__sym_name(chain, bf,
316 if (symbol_conf.show_branchflag_count)
317 ret += callchain_list_counts__printf_value(
318 NULL, chain, fp, NULL, 0);
319 ret += fprintf(fp, "\n");
321 if (++entries_printed == callchain_param.print_limit)
324 if (symbol_conf.inline_name)
325 ret += inline__fprintf(chain->ms.map,
331 root = &cnode->rb_root;
334 if (callchain_param.mode == CHAIN_GRAPH_REL)
335 total_samples = parent_samples;
337 ret += __callchain__fprintf_graph(fp, root, total_samples,
340 /* do not add a blank line if it printed nothing */
341 ret += fprintf(fp, "\n");
347 static size_t __callchain__fprintf_flat(FILE *fp, struct callchain_node *node,
350 struct callchain_list *chain;
357 ret += __callchain__fprintf_flat(fp, node->parent, total_samples);
360 list_for_each_entry(chain, &node->val, list) {
361 if (chain->ip >= PERF_CONTEXT_MAX)
363 ret += fprintf(fp, " %s\n", callchain_list__sym_name(chain,
364 bf, sizeof(bf), false));
370 static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *tree,
374 u32 entries_printed = 0;
375 struct callchain_node *chain;
376 struct rb_node *rb_node = rb_first(tree);
379 chain = rb_entry(rb_node, struct callchain_node, rb_node);
381 ret += fprintf(fp, " ");
382 ret += callchain_node__fprintf_value(chain, fp, total_samples);
383 ret += fprintf(fp, "\n");
384 ret += __callchain__fprintf_flat(fp, chain, total_samples);
385 ret += fprintf(fp, "\n");
386 if (++entries_printed == callchain_param.print_limit)
389 rb_node = rb_next(rb_node);
395 static size_t __callchain__fprintf_folded(FILE *fp, struct callchain_node *node)
397 const char *sep = symbol_conf.field_sep ?: ";";
398 struct callchain_list *chain;
406 ret += __callchain__fprintf_folded(fp, node->parent);
409 list_for_each_entry(chain, &node->val, list) {
410 if (chain->ip >= PERF_CONTEXT_MAX)
412 ret += fprintf(fp, "%s%s", first ? "" : sep,
413 callchain_list__sym_name(chain,
414 bf, sizeof(bf), false));
421 static size_t callchain__fprintf_folded(FILE *fp, struct rb_root *tree,
425 u32 entries_printed = 0;
426 struct callchain_node *chain;
427 struct rb_node *rb_node = rb_first(tree);
431 chain = rb_entry(rb_node, struct callchain_node, rb_node);
433 ret += callchain_node__fprintf_value(chain, fp, total_samples);
434 ret += fprintf(fp, " ");
435 ret += __callchain__fprintf_folded(fp, chain);
436 ret += fprintf(fp, "\n");
437 if (++entries_printed == callchain_param.print_limit)
440 rb_node = rb_next(rb_node);
446 static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
447 u64 total_samples, int left_margin,
450 u64 parent_samples = he->stat.period;
452 if (symbol_conf.cumulate_callchain)
453 parent_samples = he->stat_acc->period;
455 switch (callchain_param.mode) {
456 case CHAIN_GRAPH_REL:
457 return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
458 parent_samples, left_margin);
460 case CHAIN_GRAPH_ABS:
461 return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
462 parent_samples, left_margin);
465 return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples);
468 return callchain__fprintf_folded(fp, &he->sorted_chain, total_samples);
473 pr_err("Bad callchain mode\n");
479 int __hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp,
480 struct perf_hpp_list *hpp_list)
482 const char *sep = symbol_conf.field_sep;
483 struct perf_hpp_fmt *fmt;
484 char *start = hpp->buf;
488 if (symbol_conf.exclude_other && !he->parent)
491 perf_hpp_list__for_each_format(hpp_list, fmt) {
492 if (perf_hpp__should_skip(fmt, he->hists))
496 * If there's no field_sep, we still need
497 * to display initial ' '.
499 if (!sep || !first) {
500 ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
501 advance_hpp(hpp, ret);
505 if (perf_hpp__use_color() && fmt->color)
506 ret = fmt->color(fmt, hpp, he);
508 ret = fmt->entry(fmt, hpp, he);
510 ret = hist_entry__snprintf_alignment(he, hpp, fmt, ret);
511 advance_hpp(hpp, ret);
514 return hpp->buf - start;
517 static int hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp)
519 return __hist_entry__snprintf(he, hpp, he->hists->hpp_list);
522 static int hist_entry__hierarchy_fprintf(struct hist_entry *he,
523 struct perf_hpp *hpp,
527 const char *sep = symbol_conf.field_sep;
528 struct perf_hpp_fmt *fmt;
529 struct perf_hpp_list_node *fmt_node;
530 char *buf = hpp->buf;
531 size_t size = hpp->size;
532 int ret, printed = 0;
535 if (symbol_conf.exclude_other && !he->parent)
538 ret = scnprintf(hpp->buf, hpp->size, "%*s", he->depth * HIERARCHY_INDENT, "");
539 advance_hpp(hpp, ret);
541 /* the first hpp_list_node is for overhead columns */
542 fmt_node = list_first_entry(&hists->hpp_formats,
543 struct perf_hpp_list_node, list);
544 perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
546 * If there's no field_sep, we still need
547 * to display initial ' '.
549 if (!sep || !first) {
550 ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
551 advance_hpp(hpp, ret);
555 if (perf_hpp__use_color() && fmt->color)
556 ret = fmt->color(fmt, hpp, he);
558 ret = fmt->entry(fmt, hpp, he);
560 ret = hist_entry__snprintf_alignment(he, hpp, fmt, ret);
561 advance_hpp(hpp, ret);
565 ret = scnprintf(hpp->buf, hpp->size, "%*s",
566 (hists->nr_hpp_node - 2) * HIERARCHY_INDENT, "");
567 advance_hpp(hpp, ret);
569 printed += fprintf(fp, "%s", buf);
571 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
576 * No need to call hist_entry__snprintf_alignment() since this
577 * fmt is always the last column in the hierarchy mode.
579 if (perf_hpp__use_color() && fmt->color)
580 fmt->color(fmt, hpp, he);
582 fmt->entry(fmt, hpp, he);
585 * dynamic entries are right-aligned but we want left-aligned
586 * in the hierarchy mode
588 printed += fprintf(fp, "%s%s", sep ?: " ", ltrim(buf));
590 printed += putc('\n', fp);
592 if (symbol_conf.use_callchain && he->leaf) {
593 u64 total = hists__total_period(hists);
595 printed += hist_entry_callchain__fprintf(he, total, 0, fp);
603 static int hist_entry__fprintf(struct hist_entry *he, size_t size,
604 char *bf, size_t bfsz, FILE *fp,
608 int callchain_ret = 0;
610 struct perf_hpp hpp = {
614 struct hists *hists = he->hists;
615 u64 total_period = hists->stats.total_period;
617 if (size == 0 || size > bfsz)
618 size = hpp.size = bfsz;
620 if (symbol_conf.report_hierarchy)
621 return hist_entry__hierarchy_fprintf(he, &hpp, hists, fp);
623 hist_entry__snprintf(he, &hpp);
625 ret = fprintf(fp, "%s\n", bf);
628 callchain_ret = hist_entry_callchain__fprintf(he, total_period,
631 if (callchain_ret == 0 && symbol_conf.inline_name) {
632 inline_ret = inline__fprintf(he->ms.map, he->ip, 0, 0, 0, fp);
635 ret += fprintf(fp, "\n");
637 ret += callchain_ret;
642 static int print_hierarchy_indent(const char *sep, int indent,
643 const char *line, FILE *fp)
645 if (sep != NULL || indent < 2)
648 return fprintf(fp, "%-.*s", (indent - 2) * HIERARCHY_INDENT, line);
651 static int hists__fprintf_hierarchy_headers(struct hists *hists,
652 struct perf_hpp *hpp, FILE *fp)
654 bool first_node, first_col;
658 unsigned header_width = 0;
659 struct perf_hpp_fmt *fmt;
660 struct perf_hpp_list_node *fmt_node;
661 const char *sep = symbol_conf.field_sep;
663 indent = hists->nr_hpp_node;
665 /* preserve max indent depth for column headers */
666 print_hierarchy_indent(sep, indent, spaces, fp);
668 /* the first hpp_list_node is for overhead columns */
669 fmt_node = list_first_entry(&hists->hpp_formats,
670 struct perf_hpp_list_node, list);
672 perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
673 fmt->header(fmt, hpp, hists, 0, NULL);
674 fprintf(fp, "%s%s", hpp->buf, sep ?: " ");
677 /* combine sort headers with ' / ' */
679 list_for_each_entry_continue(fmt_node, &hists->hpp_formats, list) {
681 header_width += fprintf(fp, " / ");
685 perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
686 if (perf_hpp__should_skip(fmt, hists))
690 header_width += fprintf(fp, "+");
693 fmt->header(fmt, hpp, hists, 0, NULL);
695 header_width += fprintf(fp, "%s", trim(hpp->buf));
701 /* preserve max indent depth for initial dots */
702 print_hierarchy_indent(sep, indent, dots, fp);
704 /* the first hpp_list_node is for overhead columns */
705 fmt_node = list_first_entry(&hists->hpp_formats,
706 struct perf_hpp_list_node, list);
709 perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
711 fprintf(fp, "%s", sep ?: "..");
714 width = fmt->width(fmt, hpp, hists);
715 fprintf(fp, "%.*s", width, dots);
719 list_for_each_entry_continue(fmt_node, &hists->hpp_formats, list) {
721 width = depth * HIERARCHY_INDENT;
723 perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
724 if (perf_hpp__should_skip(fmt, hists))
728 width++; /* for '+' sign between column header */
731 width += fmt->width(fmt, hpp, hists);
734 if (width > header_width)
735 header_width = width;
740 fprintf(fp, "%s%-.*s", sep ?: " ", header_width, dots);
742 fprintf(fp, "\n#\n");
747 static void fprintf_line(struct hists *hists, struct perf_hpp *hpp,
750 struct perf_hpp_fmt *fmt;
751 const char *sep = symbol_conf.field_sep;
755 hists__for_each_format(hists, fmt) {
756 if (perf_hpp__should_skip(fmt, hists))
760 fprintf(fp, "%s", sep ?: " ");
764 fmt->header(fmt, hpp, hists, line, &span);
767 fprintf(fp, "%s", hpp->buf);
772 hists__fprintf_standard_headers(struct hists *hists,
773 struct perf_hpp *hpp,
776 struct perf_hpp_list *hpp_list = hists->hpp_list;
777 struct perf_hpp_fmt *fmt;
779 const char *sep = symbol_conf.field_sep;
783 for (line = 0; line < hpp_list->nr_header_lines; line++) {
784 /* first # is displayed one level up */
787 fprintf_line(hists, hpp, line, fp);
792 return hpp_list->nr_header_lines;
798 hists__for_each_format(hists, fmt) {
801 if (perf_hpp__should_skip(fmt, hists))
805 fprintf(fp, "%s", sep ?: " ");
809 width = fmt->width(fmt, hpp, hists);
810 for (i = 0; i < width; i++)
816 return hpp_list->nr_header_lines + 2;
819 int hists__fprintf_headers(struct hists *hists, FILE *fp)
822 struct perf_hpp dummy_hpp = {
829 if (symbol_conf.report_hierarchy)
830 return hists__fprintf_hierarchy_headers(hists, &dummy_hpp, fp);
832 return hists__fprintf_standard_headers(hists, &dummy_hpp, fp);
836 size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
837 int max_cols, float min_pcnt, FILE *fp,
842 const char *sep = symbol_conf.field_sep;
850 hists__reset_column_width(hists);
852 if (symbol_conf.col_width_list_str)
853 perf_hpp__set_user_width(symbol_conf.col_width_list_str);
856 nr_rows += hists__fprintf_headers(hists, fp);
858 if (max_rows && nr_rows >= max_rows)
861 linesz = hists__sort_list_width(hists) + 3 + 1;
862 linesz += perf_hpp__color_overhead();
863 line = malloc(linesz);
869 indent = hists__overhead_width(hists) + 4;
871 for (nd = rb_first(&hists->entries); nd; nd = __rb_hierarchy_next(nd, HMD_FORCE_CHILD)) {
872 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
878 percent = hist_entry__get_percent_limit(h);
879 if (percent < min_pcnt)
882 ret += hist_entry__fprintf(h, max_cols, line, linesz, fp, use_callchain);
884 if (max_rows && ++nr_rows >= max_rows)
888 * If all children are filtered out or percent-limited,
889 * display "no entry >= x.xx%" message.
891 if (!h->leaf && !hist_entry__has_hierarchy_children(h, min_pcnt)) {
892 int depth = hists->nr_hpp_node + h->depth + 1;
894 print_hierarchy_indent(sep, depth, spaces, fp);
895 fprintf(fp, "%*sno entry >= %.2f%%\n", indent, "", min_pcnt);
897 if (max_rows && ++nr_rows >= max_rows)
901 if (h->ms.map == NULL && verbose > 1) {
902 __map_groups__fprintf_maps(h->thread->mg,
904 fprintf(fp, "%.10s end\n", graph_dotted_line);
910 zfree(&rem_sq_bracket);
915 size_t events_stats__fprintf(struct events_stats *stats, FILE *fp)
920 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
923 if (stats->nr_events[i] == 0)
926 name = perf_event__name(i);
927 if (!strcmp(name, "UNKNOWN"))
930 ret += fprintf(fp, "%16s events: %10d\n", name,
931 stats->nr_events[i]);