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/sane_ctype.h"
10 static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
13 int ret = fprintf(fp, " ");
15 for (i = 0; i < left_margin; i++)
16 ret += fprintf(fp, " ");
21 static size_t inline__fprintf(struct map *map, u64 ip, int left_margin,
22 int depth, int depth_mask, FILE *fp)
25 struct inline_node *node;
26 struct inline_list *ilist;
36 if (dso->kernel != DSO_TYPE_USER)
39 node = dso__parse_addr_inlines(dso,
40 map__rip_2objdump(map, ip));
44 list_for_each_entry(ilist, &node->val, list) {
45 if ((ilist->filename != NULL) || (ilist->funcname != NULL)) {
46 ret += callchain__fprintf_left_margin(fp, left_margin);
48 for (i = 0; i < depth; i++) {
49 if (depth_mask & (1 << i))
50 ret += fprintf(fp, "|");
52 ret += fprintf(fp, " ");
53 ret += fprintf(fp, " ");
56 if (callchain_param.key == CCKEY_ADDRESS ||
57 callchain_param.key == CCKEY_SRCLINE) {
58 if (ilist->filename != NULL)
59 ret += fprintf(fp, "%s:%d (inline)",
63 ret += fprintf(fp, "??");
64 } else if (ilist->funcname != NULL)
65 ret += fprintf(fp, "%s (inline)",
67 else if (ilist->filename != NULL)
68 ret += fprintf(fp, "%s:%d (inline)",
72 ret += fprintf(fp, "??");
74 ret += fprintf(fp, "\n");
78 inline_node__delete(node);
82 static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
86 size_t ret = callchain__fprintf_left_margin(fp, left_margin);
88 for (i = 0; i < depth; i++)
89 if (depth_mask & (1 << i))
90 ret += fprintf(fp, "| ");
92 ret += fprintf(fp, " ");
94 ret += fprintf(fp, "\n");
99 static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_node *node,
100 struct callchain_list *chain,
101 int depth, int depth_mask, int period,
102 u64 total_samples, int left_margin)
106 char bf[1024], *alloc_str = NULL;
110 ret += callchain__fprintf_left_margin(fp, left_margin);
111 for (i = 0; i < depth; i++) {
112 if (depth_mask & (1 << i))
113 ret += fprintf(fp, "|");
115 ret += fprintf(fp, " ");
116 if (!period && i == depth - 1) {
117 ret += fprintf(fp, "--");
118 ret += callchain_node__fprintf_value(node, fp, total_samples);
119 ret += fprintf(fp, "--");
121 ret += fprintf(fp, "%s", " ");
124 str = callchain_list__sym_name(chain, bf, sizeof(bf), false);
126 if (symbol_conf.show_branchflag_count) {
128 callchain_list_counts__printf_value(node, chain, NULL,
131 callchain_list_counts__printf_value(NULL, chain, NULL,
134 if (asprintf(&alloc_str, "%s%s", str, buf) < 0)
135 str = "Not enough memory!";
144 if (symbol_conf.inline_name)
145 ret += inline__fprintf(chain->ms.map, chain->ip,
146 left_margin, depth, depth_mask, fp);
150 static struct symbol *rem_sq_bracket;
151 static struct callchain_list rem_hits;
153 static void init_rem_hits(void)
155 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
156 if (!rem_sq_bracket) {
157 fprintf(stderr, "Not enough memory to display remaining hits\n");
161 strcpy(rem_sq_bracket->name, "[...]");
162 rem_hits.ms.sym = rem_sq_bracket;
165 static size_t __callchain__fprintf_graph(FILE *fp, struct rb_root *root,
166 u64 total_samples, int depth,
167 int depth_mask, int left_margin)
169 struct rb_node *node, *next;
170 struct callchain_node *child = NULL;
171 struct callchain_list *chain;
172 int new_depth_mask = depth_mask;
176 uint entries_printed = 0;
179 remaining = total_samples;
181 node = rb_first(root);
186 child = rb_entry(node, struct callchain_node, rb_node);
187 cumul = callchain_cumul_hits(child);
189 cumul_count += callchain_cumul_counts(child);
192 * The depth mask manages the output of pipes that show
193 * the depth. We don't want to keep the pipes of the current
194 * level for the last child of this depth.
195 * Except if we have remaining filtered hits. They will
196 * supersede the last child
198 next = rb_next(node);
199 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
200 new_depth_mask &= ~(1 << (depth - 1));
203 * But we keep the older depth mask for the line separator
204 * to keep the level link until we reach the last child
206 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
209 list_for_each_entry(chain, &child->val, list) {
210 ret += ipchain__fprintf_graph(fp, child, chain, depth,
216 if (callchain_param.mode == CHAIN_GRAPH_REL)
217 new_total = child->children_hit;
219 new_total = total_samples;
221 ret += __callchain__fprintf_graph(fp, &child->rb_root, new_total,
223 new_depth_mask | (1 << depth),
226 if (++entries_printed == callchain_param.print_limit)
230 if (callchain_param.mode == CHAIN_GRAPH_REL &&
231 remaining && remaining != total_samples) {
232 struct callchain_node rem_node = {
239 if (callchain_param.value == CCVAL_COUNT && child && child->parent) {
240 rem_node.count = child->parent->children_count - cumul_count;
241 if (rem_node.count <= 0)
245 new_depth_mask &= ~(1 << (depth - 1));
246 ret += ipchain__fprintf_graph(fp, &rem_node, &rem_hits, depth,
247 new_depth_mask, 0, total_samples,
255 * If have one single callchain root, don't bother printing
256 * its percentage (100 % in fractal mode and the same percentage
257 * than the hist in graph mode). This also avoid one level of column.
259 * However when percent-limit applied, it's possible that single callchain
260 * node have different (non-100% in fractal mode) percentage.
262 static bool need_percent_display(struct rb_node *node, u64 parent_samples)
264 struct callchain_node *cnode;
269 cnode = rb_entry(node, struct callchain_node, rb_node);
270 return callchain_cumul_hits(cnode) != parent_samples;
273 static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root,
274 u64 total_samples, u64 parent_samples,
277 struct callchain_node *cnode;
278 struct callchain_list *chain;
279 u32 entries_printed = 0;
280 bool printed = false;
281 struct rb_node *node;
286 node = rb_first(root);
287 if (node && !need_percent_display(node, parent_samples)) {
288 cnode = rb_entry(node, struct callchain_node, rb_node);
289 list_for_each_entry(chain, &cnode->val, list) {
291 * If we sort by symbol, the first entry is the same than
292 * the symbol. No need to print it otherwise it appears as
295 if (!i++ && field_order == NULL &&
296 sort_order && !prefixcmp(sort_order, "sym"))
300 ret += callchain__fprintf_left_margin(fp, left_margin);
301 ret += fprintf(fp, "|\n");
302 ret += callchain__fprintf_left_margin(fp, left_margin);
303 ret += fprintf(fp, "---");
307 ret += callchain__fprintf_left_margin(fp, left_margin);
309 ret += fprintf(fp, "%s",
310 callchain_list__sym_name(chain, bf,
314 if (symbol_conf.show_branchflag_count)
315 ret += callchain_list_counts__printf_value(
316 NULL, chain, fp, NULL, 0);
317 ret += fprintf(fp, "\n");
319 if (++entries_printed == callchain_param.print_limit)
322 if (symbol_conf.inline_name)
323 ret += inline__fprintf(chain->ms.map,
329 root = &cnode->rb_root;
332 if (callchain_param.mode == CHAIN_GRAPH_REL)
333 total_samples = parent_samples;
335 ret += __callchain__fprintf_graph(fp, root, total_samples,
338 /* do not add a blank line if it printed nothing */
339 ret += fprintf(fp, "\n");
345 static size_t __callchain__fprintf_flat(FILE *fp, struct callchain_node *node,
348 struct callchain_list *chain;
355 ret += __callchain__fprintf_flat(fp, node->parent, total_samples);
358 list_for_each_entry(chain, &node->val, list) {
359 if (chain->ip >= PERF_CONTEXT_MAX)
361 ret += fprintf(fp, " %s\n", callchain_list__sym_name(chain,
362 bf, sizeof(bf), false));
368 static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *tree,
372 u32 entries_printed = 0;
373 struct callchain_node *chain;
374 struct rb_node *rb_node = rb_first(tree);
377 chain = rb_entry(rb_node, struct callchain_node, rb_node);
379 ret += fprintf(fp, " ");
380 ret += callchain_node__fprintf_value(chain, fp, total_samples);
381 ret += fprintf(fp, "\n");
382 ret += __callchain__fprintf_flat(fp, chain, total_samples);
383 ret += fprintf(fp, "\n");
384 if (++entries_printed == callchain_param.print_limit)
387 rb_node = rb_next(rb_node);
393 static size_t __callchain__fprintf_folded(FILE *fp, struct callchain_node *node)
395 const char *sep = symbol_conf.field_sep ?: ";";
396 struct callchain_list *chain;
404 ret += __callchain__fprintf_folded(fp, node->parent);
407 list_for_each_entry(chain, &node->val, list) {
408 if (chain->ip >= PERF_CONTEXT_MAX)
410 ret += fprintf(fp, "%s%s", first ? "" : sep,
411 callchain_list__sym_name(chain,
412 bf, sizeof(bf), false));
419 static size_t callchain__fprintf_folded(FILE *fp, struct rb_root *tree,
423 u32 entries_printed = 0;
424 struct callchain_node *chain;
425 struct rb_node *rb_node = rb_first(tree);
429 chain = rb_entry(rb_node, struct callchain_node, rb_node);
431 ret += callchain_node__fprintf_value(chain, fp, total_samples);
432 ret += fprintf(fp, " ");
433 ret += __callchain__fprintf_folded(fp, chain);
434 ret += fprintf(fp, "\n");
435 if (++entries_printed == callchain_param.print_limit)
438 rb_node = rb_next(rb_node);
444 static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
445 u64 total_samples, int left_margin,
448 u64 parent_samples = he->stat.period;
450 if (symbol_conf.cumulate_callchain)
451 parent_samples = he->stat_acc->period;
453 switch (callchain_param.mode) {
454 case CHAIN_GRAPH_REL:
455 return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
456 parent_samples, left_margin);
458 case CHAIN_GRAPH_ABS:
459 return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
460 parent_samples, left_margin);
463 return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples);
466 return callchain__fprintf_folded(fp, &he->sorted_chain, total_samples);
471 pr_err("Bad callchain mode\n");
477 int __hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp,
478 struct perf_hpp_list *hpp_list)
480 const char *sep = symbol_conf.field_sep;
481 struct perf_hpp_fmt *fmt;
482 char *start = hpp->buf;
486 if (symbol_conf.exclude_other && !he->parent)
489 perf_hpp_list__for_each_format(hpp_list, fmt) {
490 if (perf_hpp__should_skip(fmt, he->hists))
494 * If there's no field_sep, we still need
495 * to display initial ' '.
497 if (!sep || !first) {
498 ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
499 advance_hpp(hpp, ret);
503 if (perf_hpp__use_color() && fmt->color)
504 ret = fmt->color(fmt, hpp, he);
506 ret = fmt->entry(fmt, hpp, he);
508 ret = hist_entry__snprintf_alignment(he, hpp, fmt, ret);
509 advance_hpp(hpp, ret);
512 return hpp->buf - start;
515 static int hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp)
517 return __hist_entry__snprintf(he, hpp, he->hists->hpp_list);
520 static int hist_entry__hierarchy_fprintf(struct hist_entry *he,
521 struct perf_hpp *hpp,
525 const char *sep = symbol_conf.field_sep;
526 struct perf_hpp_fmt *fmt;
527 struct perf_hpp_list_node *fmt_node;
528 char *buf = hpp->buf;
529 size_t size = hpp->size;
530 int ret, printed = 0;
533 if (symbol_conf.exclude_other && !he->parent)
536 ret = scnprintf(hpp->buf, hpp->size, "%*s", he->depth * HIERARCHY_INDENT, "");
537 advance_hpp(hpp, ret);
539 /* the first hpp_list_node is for overhead columns */
540 fmt_node = list_first_entry(&hists->hpp_formats,
541 struct perf_hpp_list_node, list);
542 perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
544 * If there's no field_sep, we still need
545 * to display initial ' '.
547 if (!sep || !first) {
548 ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
549 advance_hpp(hpp, ret);
553 if (perf_hpp__use_color() && fmt->color)
554 ret = fmt->color(fmt, hpp, he);
556 ret = fmt->entry(fmt, hpp, he);
558 ret = hist_entry__snprintf_alignment(he, hpp, fmt, ret);
559 advance_hpp(hpp, ret);
563 ret = scnprintf(hpp->buf, hpp->size, "%*s",
564 (hists->nr_hpp_node - 2) * HIERARCHY_INDENT, "");
565 advance_hpp(hpp, ret);
567 printed += fprintf(fp, "%s", buf);
569 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
574 * No need to call hist_entry__snprintf_alignment() since this
575 * fmt is always the last column in the hierarchy mode.
577 if (perf_hpp__use_color() && fmt->color)
578 fmt->color(fmt, hpp, he);
580 fmt->entry(fmt, hpp, he);
583 * dynamic entries are right-aligned but we want left-aligned
584 * in the hierarchy mode
586 printed += fprintf(fp, "%s%s", sep ?: " ", ltrim(buf));
588 printed += putc('\n', fp);
590 if (symbol_conf.use_callchain && he->leaf) {
591 u64 total = hists__total_period(hists);
593 printed += hist_entry_callchain__fprintf(he, total, 0, fp);
601 static int hist_entry__fprintf(struct hist_entry *he, size_t size,
602 char *bf, size_t bfsz, FILE *fp,
606 int callchain_ret = 0;
608 struct perf_hpp hpp = {
612 struct hists *hists = he->hists;
613 u64 total_period = hists->stats.total_period;
615 if (size == 0 || size > bfsz)
616 size = hpp.size = bfsz;
618 if (symbol_conf.report_hierarchy)
619 return hist_entry__hierarchy_fprintf(he, &hpp, hists, fp);
621 hist_entry__snprintf(he, &hpp);
623 ret = fprintf(fp, "%s\n", bf);
626 callchain_ret = hist_entry_callchain__fprintf(he, total_period,
629 if (callchain_ret == 0 && symbol_conf.inline_name) {
630 inline_ret = inline__fprintf(he->ms.map, he->ip, 0, 0, 0, fp);
633 ret += fprintf(fp, "\n");
635 ret += callchain_ret;
640 static int print_hierarchy_indent(const char *sep, int indent,
641 const char *line, FILE *fp)
643 if (sep != NULL || indent < 2)
646 return fprintf(fp, "%-.*s", (indent - 2) * HIERARCHY_INDENT, line);
649 static int hists__fprintf_hierarchy_headers(struct hists *hists,
650 struct perf_hpp *hpp, FILE *fp)
652 bool first_node, first_col;
656 unsigned header_width = 0;
657 struct perf_hpp_fmt *fmt;
658 struct perf_hpp_list_node *fmt_node;
659 const char *sep = symbol_conf.field_sep;
661 indent = hists->nr_hpp_node;
663 /* preserve max indent depth for column headers */
664 print_hierarchy_indent(sep, indent, spaces, fp);
666 /* the first hpp_list_node is for overhead columns */
667 fmt_node = list_first_entry(&hists->hpp_formats,
668 struct perf_hpp_list_node, list);
670 perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
671 fmt->header(fmt, hpp, hists, 0, NULL);
672 fprintf(fp, "%s%s", hpp->buf, sep ?: " ");
675 /* combine sort headers with ' / ' */
677 list_for_each_entry_continue(fmt_node, &hists->hpp_formats, list) {
679 header_width += fprintf(fp, " / ");
683 perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
684 if (perf_hpp__should_skip(fmt, hists))
688 header_width += fprintf(fp, "+");
691 fmt->header(fmt, hpp, hists, 0, NULL);
693 header_width += fprintf(fp, "%s", trim(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,
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(&hists->entries); nd; nd = __rb_hierarchy_next(nd, HMD_FORCE_CHILD)) {
870 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
876 percent = hist_entry__get_percent_limit(h);
877 if (percent < min_pcnt)
880 ret += hist_entry__fprintf(h, max_cols, line, linesz, fp, use_callchain);
882 if (max_rows && ++nr_rows >= max_rows)
886 * If all children are filtered out or percent-limited,
887 * display "no entry >= x.xx%" message.
889 if (!h->leaf && !hist_entry__has_hierarchy_children(h, min_pcnt)) {
890 int depth = hists->nr_hpp_node + h->depth + 1;
892 print_hierarchy_indent(sep, depth, spaces, fp);
893 fprintf(fp, "%*sno entry >= %.2f%%\n", indent, "", min_pcnt);
895 if (max_rows && ++nr_rows >= max_rows)
899 if (h->ms.map == NULL && verbose > 1) {
900 __map_groups__fprintf_maps(h->thread->mg,
902 fprintf(fp, "%.10s end\n", graph_dotted_line);
908 zfree(&rem_sq_bracket);
913 size_t events_stats__fprintf(struct events_stats *stats, FILE *fp)
918 for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
921 if (stats->nr_events[i] == 0)
924 name = perf_event__name(i);
925 if (!strcmp(name, "UNKNOWN"))
928 ret += fprintf(fp, "%16s events: %10d\n", name,
929 stats->nr_events[i]);