xref: /linux/tools/perf/ui/stdio/hist.c (revision c4ee06251d4212a0d55e2371f2db464f6a1e0901)
17ccf4f90SNamhyung Kim #include <stdio.h>
28e99b6d4SArnaldo Carvalho de Melo #include <linux/string.h>
37ccf4f90SNamhyung Kim 
47ccf4f90SNamhyung Kim #include "../../util/util.h"
57ccf4f90SNamhyung Kim #include "../../util/hist.h"
67ccf4f90SNamhyung Kim #include "../../util/sort.h"
75b9e2146SNamhyung Kim #include "../../util/evsel.h"
8632a5cabSArnaldo Carvalho de Melo #include "../../util/srcline.h"
9a067558eSArnaldo Carvalho de Melo #include "../../util/string2.h"
10e7ff8920SArnaldo Carvalho de Melo #include "../../util/thread.h"
113d689ed6SArnaldo Carvalho de Melo #include "../../util/sane_ctype.h"
127ccf4f90SNamhyung Kim 
137ccf4f90SNamhyung Kim static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
147ccf4f90SNamhyung Kim {
157ccf4f90SNamhyung Kim 	int i;
167ccf4f90SNamhyung Kim 	int ret = fprintf(fp, "            ");
177ccf4f90SNamhyung Kim 
187ccf4f90SNamhyung Kim 	for (i = 0; i < left_margin; i++)
197ccf4f90SNamhyung Kim 		ret += fprintf(fp, " ");
207ccf4f90SNamhyung Kim 
217ccf4f90SNamhyung Kim 	return ret;
227ccf4f90SNamhyung Kim }
237ccf4f90SNamhyung Kim 
240db64dd0SJin Yao static size_t inline__fprintf(struct map *map, u64 ip, int left_margin,
250db64dd0SJin Yao 			      int depth, int depth_mask, FILE *fp)
260db64dd0SJin Yao {
270db64dd0SJin Yao 	struct dso *dso;
280db64dd0SJin Yao 	struct inline_node *node;
290db64dd0SJin Yao 	struct inline_list *ilist;
300db64dd0SJin Yao 	int ret = 0, i;
310db64dd0SJin Yao 
320db64dd0SJin Yao 	if (map == NULL)
330db64dd0SJin Yao 		return 0;
340db64dd0SJin Yao 
350db64dd0SJin Yao 	dso = map->dso;
360db64dd0SJin Yao 	if (dso == NULL)
370db64dd0SJin Yao 		return 0;
380db64dd0SJin Yao 
390db64dd0SJin Yao 	node = dso__parse_addr_inlines(dso,
400db64dd0SJin Yao 				       map__rip_2objdump(map, ip));
410db64dd0SJin Yao 	if (node == NULL)
420db64dd0SJin Yao 		return 0;
430db64dd0SJin Yao 
440db64dd0SJin Yao 	list_for_each_entry(ilist, &node->val, list) {
450db64dd0SJin Yao 		if ((ilist->filename != NULL) || (ilist->funcname != NULL)) {
460db64dd0SJin Yao 			ret += callchain__fprintf_left_margin(fp, left_margin);
470db64dd0SJin Yao 
480db64dd0SJin Yao 			for (i = 0; i < depth; i++) {
490db64dd0SJin Yao 				if (depth_mask & (1 << i))
500db64dd0SJin Yao 					ret += fprintf(fp, "|");
510db64dd0SJin Yao 				else
520db64dd0SJin Yao 					ret += fprintf(fp, " ");
530db64dd0SJin Yao 				ret += fprintf(fp, "          ");
540db64dd0SJin Yao 			}
550db64dd0SJin Yao 
565dfa210eSMilian Wolff 			if (callchain_param.key == CCKEY_ADDRESS ||
575dfa210eSMilian Wolff 			    callchain_param.key == CCKEY_SRCLINE) {
580db64dd0SJin Yao 				if (ilist->filename != NULL)
590db64dd0SJin Yao 					ret += fprintf(fp, "%s:%d (inline)",
600db64dd0SJin Yao 						       ilist->filename,
610db64dd0SJin Yao 						       ilist->line_nr);
620db64dd0SJin Yao 				else
630db64dd0SJin Yao 					ret += fprintf(fp, "??");
640db64dd0SJin Yao 			} else if (ilist->funcname != NULL)
650db64dd0SJin Yao 				ret += fprintf(fp, "%s (inline)",
660db64dd0SJin Yao 					       ilist->funcname);
670db64dd0SJin Yao 			else if (ilist->filename != NULL)
680db64dd0SJin Yao 				ret += fprintf(fp, "%s:%d (inline)",
690db64dd0SJin Yao 					       ilist->filename,
700db64dd0SJin Yao 					       ilist->line_nr);
710db64dd0SJin Yao 			else
720db64dd0SJin Yao 				ret += fprintf(fp, "??");
730db64dd0SJin Yao 
740db64dd0SJin Yao 			ret += fprintf(fp, "\n");
750db64dd0SJin Yao 		}
760db64dd0SJin Yao 	}
770db64dd0SJin Yao 
780db64dd0SJin Yao 	inline_node__delete(node);
790db64dd0SJin Yao 	return ret;
800db64dd0SJin Yao }
810db64dd0SJin Yao 
827ccf4f90SNamhyung Kim static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
837ccf4f90SNamhyung Kim 					  int left_margin)
847ccf4f90SNamhyung Kim {
857ccf4f90SNamhyung Kim 	int i;
867ccf4f90SNamhyung Kim 	size_t ret = callchain__fprintf_left_margin(fp, left_margin);
877ccf4f90SNamhyung Kim 
887ccf4f90SNamhyung Kim 	for (i = 0; i < depth; i++)
897ccf4f90SNamhyung Kim 		if (depth_mask & (1 << i))
907ccf4f90SNamhyung Kim 			ret += fprintf(fp, "|          ");
917ccf4f90SNamhyung Kim 		else
927ccf4f90SNamhyung Kim 			ret += fprintf(fp, "           ");
937ccf4f90SNamhyung Kim 
947ccf4f90SNamhyung Kim 	ret += fprintf(fp, "\n");
957ccf4f90SNamhyung Kim 
967ccf4f90SNamhyung Kim 	return ret;
977ccf4f90SNamhyung Kim }
987ccf4f90SNamhyung Kim 
995ab250caSNamhyung Kim static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_node *node,
1005ab250caSNamhyung Kim 				     struct callchain_list *chain,
1017ccf4f90SNamhyung Kim 				     int depth, int depth_mask, int period,
1025ab250caSNamhyung Kim 				     u64 total_samples, int left_margin)
1037ccf4f90SNamhyung Kim {
1047ccf4f90SNamhyung Kim 	int i;
1057ccf4f90SNamhyung Kim 	size_t ret = 0;
1068577ae6bSJin Yao 	char bf[1024], *alloc_str = NULL;
1078577ae6bSJin Yao 	char buf[64];
1088577ae6bSJin Yao 	const char *str;
1097ccf4f90SNamhyung Kim 
1107ccf4f90SNamhyung Kim 	ret += callchain__fprintf_left_margin(fp, left_margin);
1117ccf4f90SNamhyung Kim 	for (i = 0; i < depth; i++) {
1127ccf4f90SNamhyung Kim 		if (depth_mask & (1 << i))
1137ccf4f90SNamhyung Kim 			ret += fprintf(fp, "|");
1147ccf4f90SNamhyung Kim 		else
1157ccf4f90SNamhyung Kim 			ret += fprintf(fp, " ");
1167ccf4f90SNamhyung Kim 		if (!period && i == depth - 1) {
1175ab250caSNamhyung Kim 			ret += fprintf(fp, "--");
1185ab250caSNamhyung Kim 			ret += callchain_node__fprintf_value(node, fp, total_samples);
1195ab250caSNamhyung Kim 			ret += fprintf(fp, "--");
1207ccf4f90SNamhyung Kim 		} else
1217ccf4f90SNamhyung Kim 			ret += fprintf(fp, "%s", "          ");
1227ccf4f90SNamhyung Kim 	}
1238577ae6bSJin Yao 
1248577ae6bSJin Yao 	str = callchain_list__sym_name(chain, bf, sizeof(bf), false);
1258577ae6bSJin Yao 
1268577ae6bSJin Yao 	if (symbol_conf.show_branchflag_count) {
127*c4ee0625SJin Yao 		callchain_list_counts__printf_value(chain, NULL,
1288577ae6bSJin Yao 						    buf, sizeof(buf));
1298577ae6bSJin Yao 
1308577ae6bSJin Yao 		if (asprintf(&alloc_str, "%s%s", str, buf) < 0)
1318577ae6bSJin Yao 			str = "Not enough memory!";
1328577ae6bSJin Yao 		else
1338577ae6bSJin Yao 			str = alloc_str;
1348577ae6bSJin Yao 	}
1358577ae6bSJin Yao 
1368577ae6bSJin Yao 	fputs(str, fp);
1372989ccaaSAndi Kleen 	fputc('\n', fp);
1388577ae6bSJin Yao 	free(alloc_str);
1390db64dd0SJin Yao 
1400db64dd0SJin Yao 	if (symbol_conf.inline_name)
1410db64dd0SJin Yao 		ret += inline__fprintf(chain->ms.map, chain->ip,
1420db64dd0SJin Yao 				       left_margin, depth, depth_mask, fp);
1437ccf4f90SNamhyung Kim 	return ret;
1447ccf4f90SNamhyung Kim }
1457ccf4f90SNamhyung Kim 
1467ccf4f90SNamhyung Kim static struct symbol *rem_sq_bracket;
1477ccf4f90SNamhyung Kim static struct callchain_list rem_hits;
1487ccf4f90SNamhyung Kim 
1497ccf4f90SNamhyung Kim static void init_rem_hits(void)
1507ccf4f90SNamhyung Kim {
1517ccf4f90SNamhyung Kim 	rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
1527ccf4f90SNamhyung Kim 	if (!rem_sq_bracket) {
1537ccf4f90SNamhyung Kim 		fprintf(stderr, "Not enough memory to display remaining hits\n");
1547ccf4f90SNamhyung Kim 		return;
1557ccf4f90SNamhyung Kim 	}
1567ccf4f90SNamhyung Kim 
1577ccf4f90SNamhyung Kim 	strcpy(rem_sq_bracket->name, "[...]");
1587ccf4f90SNamhyung Kim 	rem_hits.ms.sym = rem_sq_bracket;
1597ccf4f90SNamhyung Kim }
1607ccf4f90SNamhyung Kim 
1617ccf4f90SNamhyung Kim static size_t __callchain__fprintf_graph(FILE *fp, struct rb_root *root,
1627ccf4f90SNamhyung Kim 					 u64 total_samples, int depth,
1637ccf4f90SNamhyung Kim 					 int depth_mask, int left_margin)
1647ccf4f90SNamhyung Kim {
1657ccf4f90SNamhyung Kim 	struct rb_node *node, *next;
166f2af0086SNamhyung Kim 	struct callchain_node *child = NULL;
1677ccf4f90SNamhyung Kim 	struct callchain_list *chain;
1687ccf4f90SNamhyung Kim 	int new_depth_mask = depth_mask;
1697ccf4f90SNamhyung Kim 	u64 remaining;
1707ccf4f90SNamhyung Kim 	size_t ret = 0;
1717ccf4f90SNamhyung Kim 	int i;
1727ccf4f90SNamhyung Kim 	uint entries_printed = 0;
173f2af0086SNamhyung Kim 	int cumul_count = 0;
1747ccf4f90SNamhyung Kim 
1757ccf4f90SNamhyung Kim 	remaining = total_samples;
1767ccf4f90SNamhyung Kim 
1777ccf4f90SNamhyung Kim 	node = rb_first(root);
1787ccf4f90SNamhyung Kim 	while (node) {
1797ccf4f90SNamhyung Kim 		u64 new_total;
1807ccf4f90SNamhyung Kim 		u64 cumul;
1817ccf4f90SNamhyung Kim 
1827ccf4f90SNamhyung Kim 		child = rb_entry(node, struct callchain_node, rb_node);
1837ccf4f90SNamhyung Kim 		cumul = callchain_cumul_hits(child);
1847ccf4f90SNamhyung Kim 		remaining -= cumul;
185f2af0086SNamhyung Kim 		cumul_count += callchain_cumul_counts(child);
1867ccf4f90SNamhyung Kim 
1877ccf4f90SNamhyung Kim 		/*
1887ccf4f90SNamhyung Kim 		 * The depth mask manages the output of pipes that show
1897ccf4f90SNamhyung Kim 		 * the depth. We don't want to keep the pipes of the current
1907ccf4f90SNamhyung Kim 		 * level for the last child of this depth.
1917ccf4f90SNamhyung Kim 		 * Except if we have remaining filtered hits. They will
1927ccf4f90SNamhyung Kim 		 * supersede the last child
1937ccf4f90SNamhyung Kim 		 */
1947ccf4f90SNamhyung Kim 		next = rb_next(node);
1957ccf4f90SNamhyung Kim 		if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
1967ccf4f90SNamhyung Kim 			new_depth_mask &= ~(1 << (depth - 1));
1977ccf4f90SNamhyung Kim 
1987ccf4f90SNamhyung Kim 		/*
1997ccf4f90SNamhyung Kim 		 * But we keep the older depth mask for the line separator
2007ccf4f90SNamhyung Kim 		 * to keep the level link until we reach the last child
2017ccf4f90SNamhyung Kim 		 */
2027ccf4f90SNamhyung Kim 		ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
2037ccf4f90SNamhyung Kim 						   left_margin);
2047ccf4f90SNamhyung Kim 		i = 0;
2057ccf4f90SNamhyung Kim 		list_for_each_entry(chain, &child->val, list) {
2065ab250caSNamhyung Kim 			ret += ipchain__fprintf_graph(fp, child, chain, depth,
2077ccf4f90SNamhyung Kim 						      new_depth_mask, i++,
2087ccf4f90SNamhyung Kim 						      total_samples,
2097ccf4f90SNamhyung Kim 						      left_margin);
2107ccf4f90SNamhyung Kim 		}
2117ccf4f90SNamhyung Kim 
2127ccf4f90SNamhyung Kim 		if (callchain_param.mode == CHAIN_GRAPH_REL)
2137ccf4f90SNamhyung Kim 			new_total = child->children_hit;
2147ccf4f90SNamhyung Kim 		else
2157ccf4f90SNamhyung Kim 			new_total = total_samples;
2167ccf4f90SNamhyung Kim 
2177ccf4f90SNamhyung Kim 		ret += __callchain__fprintf_graph(fp, &child->rb_root, new_total,
2187ccf4f90SNamhyung Kim 						  depth + 1,
2197ccf4f90SNamhyung Kim 						  new_depth_mask | (1 << depth),
2207ccf4f90SNamhyung Kim 						  left_margin);
2217ccf4f90SNamhyung Kim 		node = next;
2227ccf4f90SNamhyung Kim 		if (++entries_printed == callchain_param.print_limit)
2237ccf4f90SNamhyung Kim 			break;
2247ccf4f90SNamhyung Kim 	}
2257ccf4f90SNamhyung Kim 
2267ccf4f90SNamhyung Kim 	if (callchain_param.mode == CHAIN_GRAPH_REL &&
2277ccf4f90SNamhyung Kim 		remaining && remaining != total_samples) {
2285ab250caSNamhyung Kim 		struct callchain_node rem_node = {
2295ab250caSNamhyung Kim 			.hit = remaining,
2305ab250caSNamhyung Kim 		};
2317ccf4f90SNamhyung Kim 
2327ccf4f90SNamhyung Kim 		if (!rem_sq_bracket)
2337ccf4f90SNamhyung Kim 			return ret;
2347ccf4f90SNamhyung Kim 
235f2af0086SNamhyung Kim 		if (callchain_param.value == CCVAL_COUNT && child && child->parent) {
236f2af0086SNamhyung Kim 			rem_node.count = child->parent->children_count - cumul_count;
237f2af0086SNamhyung Kim 			if (rem_node.count <= 0)
238f2af0086SNamhyung Kim 				return ret;
239f2af0086SNamhyung Kim 		}
240f2af0086SNamhyung Kim 
2417ccf4f90SNamhyung Kim 		new_depth_mask &= ~(1 << (depth - 1));
2425ab250caSNamhyung Kim 		ret += ipchain__fprintf_graph(fp, &rem_node, &rem_hits, depth,
2437ccf4f90SNamhyung Kim 					      new_depth_mask, 0, total_samples,
2445ab250caSNamhyung Kim 					      left_margin);
2457ccf4f90SNamhyung Kim 	}
2467ccf4f90SNamhyung Kim 
2477ccf4f90SNamhyung Kim 	return ret;
2487ccf4f90SNamhyung Kim }
2497ccf4f90SNamhyung Kim 
2507ed5d6e2SNamhyung Kim /*
2517ed5d6e2SNamhyung Kim  * If have one single callchain root, don't bother printing
2527ed5d6e2SNamhyung Kim  * its percentage (100 % in fractal mode and the same percentage
2537ed5d6e2SNamhyung Kim  * than the hist in graph mode). This also avoid one level of column.
2547ed5d6e2SNamhyung Kim  *
2557ed5d6e2SNamhyung Kim  * However when percent-limit applied, it's possible that single callchain
2567ed5d6e2SNamhyung Kim  * node have different (non-100% in fractal mode) percentage.
2577ed5d6e2SNamhyung Kim  */
2587ed5d6e2SNamhyung Kim static bool need_percent_display(struct rb_node *node, u64 parent_samples)
2597ed5d6e2SNamhyung Kim {
2607ed5d6e2SNamhyung Kim 	struct callchain_node *cnode;
2617ed5d6e2SNamhyung Kim 
2627ed5d6e2SNamhyung Kim 	if (rb_next(node))
2637ed5d6e2SNamhyung Kim 		return true;
2647ed5d6e2SNamhyung Kim 
2657ed5d6e2SNamhyung Kim 	cnode = rb_entry(node, struct callchain_node, rb_node);
2667ed5d6e2SNamhyung Kim 	return callchain_cumul_hits(cnode) != parent_samples;
2677ed5d6e2SNamhyung Kim }
2687ed5d6e2SNamhyung Kim 
2697ccf4f90SNamhyung Kim static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root,
27054d27b31SNamhyung Kim 				       u64 total_samples, u64 parent_samples,
27154d27b31SNamhyung Kim 				       int left_margin)
2727ccf4f90SNamhyung Kim {
2737ccf4f90SNamhyung Kim 	struct callchain_node *cnode;
2747ccf4f90SNamhyung Kim 	struct callchain_list *chain;
2757ccf4f90SNamhyung Kim 	u32 entries_printed = 0;
2767ccf4f90SNamhyung Kim 	bool printed = false;
2777ccf4f90SNamhyung Kim 	struct rb_node *node;
2787ccf4f90SNamhyung Kim 	int i = 0;
2797ccf4f90SNamhyung Kim 	int ret = 0;
2802989ccaaSAndi Kleen 	char bf[1024];
2817ccf4f90SNamhyung Kim 
2827ccf4f90SNamhyung Kim 	node = rb_first(root);
2837ed5d6e2SNamhyung Kim 	if (node && !need_percent_display(node, parent_samples)) {
2847ccf4f90SNamhyung Kim 		cnode = rb_entry(node, struct callchain_node, rb_node);
2857ccf4f90SNamhyung Kim 		list_for_each_entry(chain, &cnode->val, list) {
2867ccf4f90SNamhyung Kim 			/*
2877ccf4f90SNamhyung Kim 			 * If we sort by symbol, the first entry is the same than
2887ccf4f90SNamhyung Kim 			 * the symbol. No need to print it otherwise it appears as
2897ccf4f90SNamhyung Kim 			 * displayed twice.
2907ccf4f90SNamhyung Kim 			 */
291cfaa154bSNamhyung Kim 			if (!i++ && field_order == NULL &&
2928e99b6d4SArnaldo Carvalho de Melo 			    sort_order && strstarts(sort_order, "sym"))
2937ccf4f90SNamhyung Kim 				continue;
2940db64dd0SJin Yao 
2957ccf4f90SNamhyung Kim 			if (!printed) {
2967ccf4f90SNamhyung Kim 				ret += callchain__fprintf_left_margin(fp, left_margin);
2977ccf4f90SNamhyung Kim 				ret += fprintf(fp, "|\n");
2987ccf4f90SNamhyung Kim 				ret += callchain__fprintf_left_margin(fp, left_margin);
2997ccf4f90SNamhyung Kim 				ret += fprintf(fp, "---");
3007ccf4f90SNamhyung Kim 				left_margin += 3;
3017ccf4f90SNamhyung Kim 				printed = true;
3027ccf4f90SNamhyung Kim 			} else
3037ccf4f90SNamhyung Kim 				ret += callchain__fprintf_left_margin(fp, left_margin);
3047ccf4f90SNamhyung Kim 
3058577ae6bSJin Yao 			ret += fprintf(fp, "%s",
3068577ae6bSJin Yao 				       callchain_list__sym_name(chain, bf,
3078577ae6bSJin Yao 								sizeof(bf),
3082989ccaaSAndi Kleen 								false));
3097ccf4f90SNamhyung Kim 
3108577ae6bSJin Yao 			if (symbol_conf.show_branchflag_count)
3118577ae6bSJin Yao 				ret += callchain_list_counts__printf_value(
312*c4ee0625SJin Yao 						chain, fp, NULL, 0);
3138577ae6bSJin Yao 			ret += fprintf(fp, "\n");
3148577ae6bSJin Yao 
3157ccf4f90SNamhyung Kim 			if (++entries_printed == callchain_param.print_limit)
3167ccf4f90SNamhyung Kim 				break;
3170db64dd0SJin Yao 
3180db64dd0SJin Yao 			if (symbol_conf.inline_name)
3190db64dd0SJin Yao 				ret += inline__fprintf(chain->ms.map,
3200db64dd0SJin Yao 						       chain->ip,
3210db64dd0SJin Yao 						       left_margin,
3220db64dd0SJin Yao 						       0, 0,
3230db64dd0SJin Yao 						       fp);
3247ccf4f90SNamhyung Kim 		}
3257ccf4f90SNamhyung Kim 		root = &cnode->rb_root;
3267ccf4f90SNamhyung Kim 	}
3277ccf4f90SNamhyung Kim 
32854d27b31SNamhyung Kim 	if (callchain_param.mode == CHAIN_GRAPH_REL)
32954d27b31SNamhyung Kim 		total_samples = parent_samples;
33054d27b31SNamhyung Kim 
3317ccf4f90SNamhyung Kim 	ret += __callchain__fprintf_graph(fp, root, total_samples,
3327ccf4f90SNamhyung Kim 					  1, 1, left_margin);
3333848c23bSNamhyung Kim 	if (ret) {
3343848c23bSNamhyung Kim 		/* do not add a blank line if it printed nothing */
3357ccf4f90SNamhyung Kim 		ret += fprintf(fp, "\n");
3363848c23bSNamhyung Kim 	}
3377ccf4f90SNamhyung Kim 
3387ccf4f90SNamhyung Kim 	return ret;
3397ccf4f90SNamhyung Kim }
3407ccf4f90SNamhyung Kim 
341316c7136SArnaldo Carvalho de Melo static size_t __callchain__fprintf_flat(FILE *fp, struct callchain_node *node,
3427ccf4f90SNamhyung Kim 					u64 total_samples)
3437ccf4f90SNamhyung Kim {
3447ccf4f90SNamhyung Kim 	struct callchain_list *chain;
3457ccf4f90SNamhyung Kim 	size_t ret = 0;
3462989ccaaSAndi Kleen 	char bf[1024];
3477ccf4f90SNamhyung Kim 
348316c7136SArnaldo Carvalho de Melo 	if (!node)
3497ccf4f90SNamhyung Kim 		return 0;
3507ccf4f90SNamhyung Kim 
351316c7136SArnaldo Carvalho de Melo 	ret += __callchain__fprintf_flat(fp, node->parent, total_samples);
3527ccf4f90SNamhyung Kim 
3537ccf4f90SNamhyung Kim 
354316c7136SArnaldo Carvalho de Melo 	list_for_each_entry(chain, &node->val, list) {
3557ccf4f90SNamhyung Kim 		if (chain->ip >= PERF_CONTEXT_MAX)
3567ccf4f90SNamhyung Kim 			continue;
3572989ccaaSAndi Kleen 		ret += fprintf(fp, "                %s\n", callchain_list__sym_name(chain,
3582989ccaaSAndi Kleen 					bf, sizeof(bf), false));
3597ccf4f90SNamhyung Kim 	}
3607ccf4f90SNamhyung Kim 
3617ccf4f90SNamhyung Kim 	return ret;
3627ccf4f90SNamhyung Kim }
3637ccf4f90SNamhyung Kim 
364316c7136SArnaldo Carvalho de Melo static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *tree,
3657ccf4f90SNamhyung Kim 				      u64 total_samples)
3667ccf4f90SNamhyung Kim {
3677ccf4f90SNamhyung Kim 	size_t ret = 0;
3687ccf4f90SNamhyung Kim 	u32 entries_printed = 0;
3697ccf4f90SNamhyung Kim 	struct callchain_node *chain;
370316c7136SArnaldo Carvalho de Melo 	struct rb_node *rb_node = rb_first(tree);
3717ccf4f90SNamhyung Kim 
3727ccf4f90SNamhyung Kim 	while (rb_node) {
3737ccf4f90SNamhyung Kim 		chain = rb_entry(rb_node, struct callchain_node, rb_node);
3747ccf4f90SNamhyung Kim 
3755ab250caSNamhyung Kim 		ret += fprintf(fp, "           ");
3765ab250caSNamhyung Kim 		ret += callchain_node__fprintf_value(chain, fp, total_samples);
3775ab250caSNamhyung Kim 		ret += fprintf(fp, "\n");
3787ccf4f90SNamhyung Kim 		ret += __callchain__fprintf_flat(fp, chain, total_samples);
3797ccf4f90SNamhyung Kim 		ret += fprintf(fp, "\n");
3807ccf4f90SNamhyung Kim 		if (++entries_printed == callchain_param.print_limit)
3817ccf4f90SNamhyung Kim 			break;
3827ccf4f90SNamhyung Kim 
3837ccf4f90SNamhyung Kim 		rb_node = rb_next(rb_node);
3847ccf4f90SNamhyung Kim 	}
3857ccf4f90SNamhyung Kim 
3867ccf4f90SNamhyung Kim 	return ret;
3877ccf4f90SNamhyung Kim }
3887ccf4f90SNamhyung Kim 
38926e77924SNamhyung Kim static size_t __callchain__fprintf_folded(FILE *fp, struct callchain_node *node)
39026e77924SNamhyung Kim {
39126e77924SNamhyung Kim 	const char *sep = symbol_conf.field_sep ?: ";";
39226e77924SNamhyung Kim 	struct callchain_list *chain;
39326e77924SNamhyung Kim 	size_t ret = 0;
39426e77924SNamhyung Kim 	char bf[1024];
39526e77924SNamhyung Kim 	bool first;
39626e77924SNamhyung Kim 
39726e77924SNamhyung Kim 	if (!node)
39826e77924SNamhyung Kim 		return 0;
39926e77924SNamhyung Kim 
40026e77924SNamhyung Kim 	ret += __callchain__fprintf_folded(fp, node->parent);
40126e77924SNamhyung Kim 
40226e77924SNamhyung Kim 	first = (ret == 0);
40326e77924SNamhyung Kim 	list_for_each_entry(chain, &node->val, list) {
40426e77924SNamhyung Kim 		if (chain->ip >= PERF_CONTEXT_MAX)
40526e77924SNamhyung Kim 			continue;
40626e77924SNamhyung Kim 		ret += fprintf(fp, "%s%s", first ? "" : sep,
40726e77924SNamhyung Kim 			       callchain_list__sym_name(chain,
40826e77924SNamhyung Kim 						bf, sizeof(bf), false));
40926e77924SNamhyung Kim 		first = false;
41026e77924SNamhyung Kim 	}
41126e77924SNamhyung Kim 
41226e77924SNamhyung Kim 	return ret;
41326e77924SNamhyung Kim }
41426e77924SNamhyung Kim 
41526e77924SNamhyung Kim static size_t callchain__fprintf_folded(FILE *fp, struct rb_root *tree,
41626e77924SNamhyung Kim 					u64 total_samples)
41726e77924SNamhyung Kim {
41826e77924SNamhyung Kim 	size_t ret = 0;
41926e77924SNamhyung Kim 	u32 entries_printed = 0;
42026e77924SNamhyung Kim 	struct callchain_node *chain;
42126e77924SNamhyung Kim 	struct rb_node *rb_node = rb_first(tree);
42226e77924SNamhyung Kim 
42326e77924SNamhyung Kim 	while (rb_node) {
42426e77924SNamhyung Kim 
42526e77924SNamhyung Kim 		chain = rb_entry(rb_node, struct callchain_node, rb_node);
42626e77924SNamhyung Kim 
4275ab250caSNamhyung Kim 		ret += callchain_node__fprintf_value(chain, fp, total_samples);
4285ab250caSNamhyung Kim 		ret += fprintf(fp, " ");
42926e77924SNamhyung Kim 		ret += __callchain__fprintf_folded(fp, chain);
43026e77924SNamhyung Kim 		ret += fprintf(fp, "\n");
43126e77924SNamhyung Kim 		if (++entries_printed == callchain_param.print_limit)
43226e77924SNamhyung Kim 			break;
43326e77924SNamhyung Kim 
43426e77924SNamhyung Kim 		rb_node = rb_next(rb_node);
43526e77924SNamhyung Kim 	}
43626e77924SNamhyung Kim 
43726e77924SNamhyung Kim 	return ret;
43826e77924SNamhyung Kim }
43926e77924SNamhyung Kim 
4407ccf4f90SNamhyung Kim static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
4417ccf4f90SNamhyung Kim 					    u64 total_samples, int left_margin,
4427ccf4f90SNamhyung Kim 					    FILE *fp)
4437ccf4f90SNamhyung Kim {
44454d27b31SNamhyung Kim 	u64 parent_samples = he->stat.period;
44554d27b31SNamhyung Kim 
44654d27b31SNamhyung Kim 	if (symbol_conf.cumulate_callchain)
44754d27b31SNamhyung Kim 		parent_samples = he->stat_acc->period;
44854d27b31SNamhyung Kim 
4497ccf4f90SNamhyung Kim 	switch (callchain_param.mode) {
4507ccf4f90SNamhyung Kim 	case CHAIN_GRAPH_REL:
45154d27b31SNamhyung Kim 		return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
45254d27b31SNamhyung Kim 						parent_samples, left_margin);
4537ccf4f90SNamhyung Kim 		break;
4547ccf4f90SNamhyung Kim 	case CHAIN_GRAPH_ABS:
4557ccf4f90SNamhyung Kim 		return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
45654d27b31SNamhyung Kim 						parent_samples, left_margin);
4577ccf4f90SNamhyung Kim 		break;
4587ccf4f90SNamhyung Kim 	case CHAIN_FLAT:
4597ccf4f90SNamhyung Kim 		return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples);
4607ccf4f90SNamhyung Kim 		break;
46126e77924SNamhyung Kim 	case CHAIN_FOLDED:
46226e77924SNamhyung Kim 		return callchain__fprintf_folded(fp, &he->sorted_chain, total_samples);
46326e77924SNamhyung Kim 		break;
4647ccf4f90SNamhyung Kim 	case CHAIN_NONE:
4657ccf4f90SNamhyung Kim 		break;
4667ccf4f90SNamhyung Kim 	default:
4677ccf4f90SNamhyung Kim 		pr_err("Bad callchain mode\n");
4687ccf4f90SNamhyung Kim 	}
4697ccf4f90SNamhyung Kim 
4707ccf4f90SNamhyung Kim 	return 0;
4717ccf4f90SNamhyung Kim }
4727ccf4f90SNamhyung Kim 
473bd28d0c5SJiri Olsa int __hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp,
4749da44db1SJiri Olsa 			   struct perf_hpp_list *hpp_list)
475be0e6d10SJiri Olsa {
476be0e6d10SJiri Olsa 	const char *sep = symbol_conf.field_sep;
477be0e6d10SJiri Olsa 	struct perf_hpp_fmt *fmt;
478be0e6d10SJiri Olsa 	char *start = hpp->buf;
479be0e6d10SJiri Olsa 	int ret;
480be0e6d10SJiri Olsa 	bool first = true;
481be0e6d10SJiri Olsa 
482be0e6d10SJiri Olsa 	if (symbol_conf.exclude_other && !he->parent)
483be0e6d10SJiri Olsa 		return 0;
484be0e6d10SJiri Olsa 
4859da44db1SJiri Olsa 	perf_hpp_list__for_each_format(hpp_list, fmt) {
486361459f1SNamhyung Kim 		if (perf_hpp__should_skip(fmt, he->hists))
487e67d49a7SNamhyung Kim 			continue;
488e67d49a7SNamhyung Kim 
489be0e6d10SJiri Olsa 		/*
490be0e6d10SJiri Olsa 		 * If there's no field_sep, we still need
491be0e6d10SJiri Olsa 		 * to display initial '  '.
492be0e6d10SJiri Olsa 		 */
493be0e6d10SJiri Olsa 		if (!sep || !first) {
494be0e6d10SJiri Olsa 			ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: "  ");
495be0e6d10SJiri Olsa 			advance_hpp(hpp, ret);
496be0e6d10SJiri Olsa 		} else
497be0e6d10SJiri Olsa 			first = false;
498be0e6d10SJiri Olsa 
4999754c4f9SJiri Olsa 		if (perf_hpp__use_color() && fmt->color)
500be0e6d10SJiri Olsa 			ret = fmt->color(fmt, hpp, he);
501be0e6d10SJiri Olsa 		else
502be0e6d10SJiri Olsa 			ret = fmt->entry(fmt, hpp, he);
503be0e6d10SJiri Olsa 
50489fee709SArnaldo Carvalho de Melo 		ret = hist_entry__snprintf_alignment(he, hpp, fmt, ret);
505be0e6d10SJiri Olsa 		advance_hpp(hpp, ret);
506be0e6d10SJiri Olsa 	}
507be0e6d10SJiri Olsa 
508be0e6d10SJiri Olsa 	return hpp->buf - start;
509be0e6d10SJiri Olsa }
510be0e6d10SJiri Olsa 
5119da44db1SJiri Olsa static int hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp)
5129da44db1SJiri Olsa {
5139da44db1SJiri Olsa 	return __hist_entry__snprintf(he, hpp, he->hists->hpp_list);
5149da44db1SJiri Olsa }
5159da44db1SJiri Olsa 
516ef86d68aSNamhyung Kim static int hist_entry__hierarchy_fprintf(struct hist_entry *he,
517ef86d68aSNamhyung Kim 					 struct perf_hpp *hpp,
5182dbbe9f2SNamhyung Kim 					 struct hists *hists,
519ef86d68aSNamhyung Kim 					 FILE *fp)
520ef86d68aSNamhyung Kim {
521ef86d68aSNamhyung Kim 	const char *sep = symbol_conf.field_sep;
522ef86d68aSNamhyung Kim 	struct perf_hpp_fmt *fmt;
523f58c95e3SNamhyung Kim 	struct perf_hpp_list_node *fmt_node;
524ef86d68aSNamhyung Kim 	char *buf = hpp->buf;
525cb1fab91SNamhyung Kim 	size_t size = hpp->size;
526ef86d68aSNamhyung Kim 	int ret, printed = 0;
527ef86d68aSNamhyung Kim 	bool first = true;
528ef86d68aSNamhyung Kim 
529ef86d68aSNamhyung Kim 	if (symbol_conf.exclude_other && !he->parent)
530ef86d68aSNamhyung Kim 		return 0;
531ef86d68aSNamhyung Kim 
532ef86d68aSNamhyung Kim 	ret = scnprintf(hpp->buf, hpp->size, "%*s", he->depth * HIERARCHY_INDENT, "");
533ef86d68aSNamhyung Kim 	advance_hpp(hpp, ret);
534ef86d68aSNamhyung Kim 
535f58c95e3SNamhyung Kim 	/* the first hpp_list_node is for overhead columns */
536f58c95e3SNamhyung Kim 	fmt_node = list_first_entry(&hists->hpp_formats,
537f58c95e3SNamhyung Kim 				    struct perf_hpp_list_node, list);
538f58c95e3SNamhyung Kim 	perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
539ef86d68aSNamhyung Kim 		/*
540ef86d68aSNamhyung Kim 		 * If there's no field_sep, we still need
541ef86d68aSNamhyung Kim 		 * to display initial '  '.
542ef86d68aSNamhyung Kim 		 */
543ef86d68aSNamhyung Kim 		if (!sep || !first) {
544ef86d68aSNamhyung Kim 			ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: "  ");
545ef86d68aSNamhyung Kim 			advance_hpp(hpp, ret);
546ef86d68aSNamhyung Kim 		} else
547ef86d68aSNamhyung Kim 			first = false;
548ef86d68aSNamhyung Kim 
549ef86d68aSNamhyung Kim 		if (perf_hpp__use_color() && fmt->color)
550ef86d68aSNamhyung Kim 			ret = fmt->color(fmt, hpp, he);
551ef86d68aSNamhyung Kim 		else
552ef86d68aSNamhyung Kim 			ret = fmt->entry(fmt, hpp, he);
553ef86d68aSNamhyung Kim 
554ef86d68aSNamhyung Kim 		ret = hist_entry__snprintf_alignment(he, hpp, fmt, ret);
555ef86d68aSNamhyung Kim 		advance_hpp(hpp, ret);
556ef86d68aSNamhyung Kim 	}
557ef86d68aSNamhyung Kim 
5581b2dbbf4SNamhyung Kim 	if (!sep)
559ef86d68aSNamhyung Kim 		ret = scnprintf(hpp->buf, hpp->size, "%*s",
5602dbbe9f2SNamhyung Kim 				(hists->nr_hpp_node - 2) * HIERARCHY_INDENT, "");
561ef86d68aSNamhyung Kim 	advance_hpp(hpp, ret);
562ef86d68aSNamhyung Kim 
563cb1fab91SNamhyung Kim 	printed += fprintf(fp, "%s", buf);
564cb1fab91SNamhyung Kim 
5651b2dbbf4SNamhyung Kim 	perf_hpp_list__for_each_format(he->hpp_list, fmt) {
566cb1fab91SNamhyung Kim 		hpp->buf  = buf;
567cb1fab91SNamhyung Kim 		hpp->size = size;
568cb1fab91SNamhyung Kim 
569ef86d68aSNamhyung Kim 		/*
570ef86d68aSNamhyung Kim 		 * No need to call hist_entry__snprintf_alignment() since this
571ef86d68aSNamhyung Kim 		 * fmt is always the last column in the hierarchy mode.
572ef86d68aSNamhyung Kim 		 */
573ef86d68aSNamhyung Kim 		if (perf_hpp__use_color() && fmt->color)
574ef86d68aSNamhyung Kim 			fmt->color(fmt, hpp, he);
575ef86d68aSNamhyung Kim 		else
576ef86d68aSNamhyung Kim 			fmt->entry(fmt, hpp, he);
577ef86d68aSNamhyung Kim 
578cb1fab91SNamhyung Kim 		/*
579cb1fab91SNamhyung Kim 		 * dynamic entries are right-aligned but we want left-aligned
580cb1fab91SNamhyung Kim 		 * in the hierarchy mode
581cb1fab91SNamhyung Kim 		 */
5821b2dbbf4SNamhyung Kim 		printed += fprintf(fp, "%s%s", sep ?: "  ", ltrim(buf));
5831b2dbbf4SNamhyung Kim 	}
5841b2dbbf4SNamhyung Kim 	printed += putc('\n', fp);
585ef86d68aSNamhyung Kim 
586ef86d68aSNamhyung Kim 	if (symbol_conf.use_callchain && he->leaf) {
587ef86d68aSNamhyung Kim 		u64 total = hists__total_period(hists);
588ef86d68aSNamhyung Kim 
589ef86d68aSNamhyung Kim 		printed += hist_entry_callchain__fprintf(he, total, 0, fp);
590ef86d68aSNamhyung Kim 		goto out;
591ef86d68aSNamhyung Kim 	}
592ef86d68aSNamhyung Kim 
593ef86d68aSNamhyung Kim out:
594ef86d68aSNamhyung Kim 	return printed;
595ef86d68aSNamhyung Kim }
596ef86d68aSNamhyung Kim 
597000078bcSNamhyung Kim static int hist_entry__fprintf(struct hist_entry *he, size_t size,
598d05e3aaeSJiri Olsa 			       char *bf, size_t bfsz, FILE *fp,
599d05e3aaeSJiri Olsa 			       bool use_callchain)
600000078bcSNamhyung Kim {
601000078bcSNamhyung Kim 	int ret;
6020db64dd0SJin Yao 	int callchain_ret = 0;
6030db64dd0SJin Yao 	int inline_ret = 0;
604ea251d51SNamhyung Kim 	struct perf_hpp hpp = {
605ea251d51SNamhyung Kim 		.buf		= bf,
606ea251d51SNamhyung Kim 		.size		= size,
607ea251d51SNamhyung Kim 	};
6088f1d1b44SJiri Olsa 	struct hists *hists = he->hists;
6097e597d32SNamhyung Kim 	u64 total_period = hists->stats.total_period;
610000078bcSNamhyung Kim 
61199cf666cSArnaldo Carvalho de Melo 	if (size == 0 || size > bfsz)
61299cf666cSArnaldo Carvalho de Melo 		size = hpp.size = bfsz;
613000078bcSNamhyung Kim 
6142dbbe9f2SNamhyung Kim 	if (symbol_conf.report_hierarchy)
6152dbbe9f2SNamhyung Kim 		return hist_entry__hierarchy_fprintf(he, &hpp, hists, fp);
616ef86d68aSNamhyung Kim 
61726d8b338SNamhyung Kim 	hist_entry__snprintf(he, &hpp);
618000078bcSNamhyung Kim 
619000078bcSNamhyung Kim 	ret = fprintf(fp, "%s\n", bf);
620000078bcSNamhyung Kim 
621d05e3aaeSJiri Olsa 	if (use_callchain)
6220db64dd0SJin Yao 		callchain_ret = hist_entry_callchain__fprintf(he, total_period,
6230db64dd0SJin Yao 							      0, fp);
6240db64dd0SJin Yao 
6250db64dd0SJin Yao 	if (callchain_ret == 0 && symbol_conf.inline_name) {
6260db64dd0SJin Yao 		inline_ret = inline__fprintf(he->ms.map, he->ip, 0, 0, 0, fp);
6270db64dd0SJin Yao 		ret += inline_ret;
6280db64dd0SJin Yao 		if (inline_ret > 0)
6290db64dd0SJin Yao 			ret += fprintf(fp, "\n");
6300db64dd0SJin Yao 	} else
6310db64dd0SJin Yao 		ret += callchain_ret;
632000078bcSNamhyung Kim 
633000078bcSNamhyung Kim 	return ret;
634000078bcSNamhyung Kim }
635000078bcSNamhyung Kim 
6362dbbe9f2SNamhyung Kim static int print_hierarchy_indent(const char *sep, int indent,
6378e2fc44fSNamhyung Kim 				  const char *line, FILE *fp)
6388e2fc44fSNamhyung Kim {
6392dbbe9f2SNamhyung Kim 	if (sep != NULL || indent < 2)
6408e2fc44fSNamhyung Kim 		return 0;
6418e2fc44fSNamhyung Kim 
6422dbbe9f2SNamhyung Kim 	return fprintf(fp, "%-.*s", (indent - 2) * HIERARCHY_INDENT, line);
6438e2fc44fSNamhyung Kim }
6448e2fc44fSNamhyung Kim 
645195bc0f8SNamhyung Kim static int hists__fprintf_hierarchy_headers(struct hists *hists,
646195bc0f8SNamhyung Kim 					    struct perf_hpp *hpp, FILE *fp)
6478e2fc44fSNamhyung Kim {
648f58c95e3SNamhyung Kim 	bool first_node, first_col;
6492dbbe9f2SNamhyung Kim 	int indent;
650cb1fab91SNamhyung Kim 	int depth;
6518e2fc44fSNamhyung Kim 	unsigned width = 0;
6528e2fc44fSNamhyung Kim 	unsigned header_width = 0;
6538e2fc44fSNamhyung Kim 	struct perf_hpp_fmt *fmt;
654f58c95e3SNamhyung Kim 	struct perf_hpp_list_node *fmt_node;
655195bc0f8SNamhyung Kim 	const char *sep = symbol_conf.field_sep;
6568e2fc44fSNamhyung Kim 
6572dbbe9f2SNamhyung Kim 	indent = hists->nr_hpp_node;
6588e2fc44fSNamhyung Kim 
6598e2fc44fSNamhyung Kim 	/* preserve max indent depth for column headers */
6602dbbe9f2SNamhyung Kim 	print_hierarchy_indent(sep, indent, spaces, fp);
6618e2fc44fSNamhyung Kim 
662f58c95e3SNamhyung Kim 	/* the first hpp_list_node is for overhead columns */
663f58c95e3SNamhyung Kim 	fmt_node = list_first_entry(&hists->hpp_formats,
664f58c95e3SNamhyung Kim 				    struct perf_hpp_list_node, list);
6658e2fc44fSNamhyung Kim 
666f58c95e3SNamhyung Kim 	perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
66729659ab4SJiri Olsa 		fmt->header(fmt, hpp, hists, 0, NULL);
668f58c95e3SNamhyung Kim 		fprintf(fp, "%s%s", hpp->buf, sep ?: "  ");
6698e2fc44fSNamhyung Kim 	}
6708e2fc44fSNamhyung Kim 
6718e2fc44fSNamhyung Kim 	/* combine sort headers with ' / ' */
672f58c95e3SNamhyung Kim 	first_node = true;
673f58c95e3SNamhyung Kim 	list_for_each_entry_continue(fmt_node, &hists->hpp_formats, list) {
674f58c95e3SNamhyung Kim 		if (!first_node)
675f58c95e3SNamhyung Kim 			header_width += fprintf(fp, " / ");
676f58c95e3SNamhyung Kim 		first_node = false;
677f58c95e3SNamhyung Kim 
678f58c95e3SNamhyung Kim 		first_col = true;
679f58c95e3SNamhyung Kim 		perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
6808e2fc44fSNamhyung Kim 			if (perf_hpp__should_skip(fmt, hists))
6818e2fc44fSNamhyung Kim 				continue;
6828e2fc44fSNamhyung Kim 
683f58c95e3SNamhyung Kim 			if (!first_col)
684f58c95e3SNamhyung Kim 				header_width += fprintf(fp, "+");
685f58c95e3SNamhyung Kim 			first_col = false;
6868e2fc44fSNamhyung Kim 
68729659ab4SJiri Olsa 			fmt->header(fmt, hpp, hists, 0, NULL);
6888e2fc44fSNamhyung Kim 
6897d6a7e78SJiri Olsa 			header_width += fprintf(fp, "%s", trim(hpp->buf));
6908e2fc44fSNamhyung Kim 		}
691f58c95e3SNamhyung Kim 	}
6928e2fc44fSNamhyung Kim 
6938e2fc44fSNamhyung Kim 	fprintf(fp, "\n# ");
6948e2fc44fSNamhyung Kim 
6958e2fc44fSNamhyung Kim 	/* preserve max indent depth for initial dots */
6962dbbe9f2SNamhyung Kim 	print_hierarchy_indent(sep, indent, dots, fp);
6978e2fc44fSNamhyung Kim 
698f58c95e3SNamhyung Kim 	/* the first hpp_list_node is for overhead columns */
699f58c95e3SNamhyung Kim 	fmt_node = list_first_entry(&hists->hpp_formats,
700f58c95e3SNamhyung Kim 				    struct perf_hpp_list_node, list);
7018e2fc44fSNamhyung Kim 
702f58c95e3SNamhyung Kim 	first_col = true;
703f58c95e3SNamhyung Kim 	perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
704f58c95e3SNamhyung Kim 		if (!first_col)
705f58c95e3SNamhyung Kim 			fprintf(fp, "%s", sep ?: "..");
706f58c95e3SNamhyung Kim 		first_col = false;
7078e2fc44fSNamhyung Kim 
708da1b0407SJiri Olsa 		width = fmt->width(fmt, hpp, hists);
7098e2fc44fSNamhyung Kim 		fprintf(fp, "%.*s", width, dots);
7108e2fc44fSNamhyung Kim 	}
7118e2fc44fSNamhyung Kim 
712cb1fab91SNamhyung Kim 	depth = 0;
713f58c95e3SNamhyung Kim 	list_for_each_entry_continue(fmt_node, &hists->hpp_formats, list) {
714f58c95e3SNamhyung Kim 		first_col = true;
715f58c95e3SNamhyung Kim 		width = depth * HIERARCHY_INDENT;
716f58c95e3SNamhyung Kim 
717f58c95e3SNamhyung Kim 		perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
7188e2fc44fSNamhyung Kim 			if (perf_hpp__should_skip(fmt, hists))
7198e2fc44fSNamhyung Kim 				continue;
7208e2fc44fSNamhyung Kim 
721f58c95e3SNamhyung Kim 			if (!first_col)
722f58c95e3SNamhyung Kim 				width++;  /* for '+' sign between column header */
723f58c95e3SNamhyung Kim 			first_col = false;
724f58c95e3SNamhyung Kim 
725da1b0407SJiri Olsa 			width += fmt->width(fmt, hpp, hists);
726f58c95e3SNamhyung Kim 		}
727cb1fab91SNamhyung Kim 
7288e2fc44fSNamhyung Kim 		if (width > header_width)
7298e2fc44fSNamhyung Kim 			header_width = width;
730cb1fab91SNamhyung Kim 
731cb1fab91SNamhyung Kim 		depth++;
7328e2fc44fSNamhyung Kim 	}
7338e2fc44fSNamhyung Kim 
7348e2fc44fSNamhyung Kim 	fprintf(fp, "%s%-.*s", sep ?: "  ", header_width, dots);
7358e2fc44fSNamhyung Kim 
7368e2fc44fSNamhyung Kim 	fprintf(fp, "\n#\n");
7378e2fc44fSNamhyung Kim 
7388e2fc44fSNamhyung Kim 	return 2;
7398e2fc44fSNamhyung Kim }
7408e2fc44fSNamhyung Kim 
741f3705b06SJiri Olsa static void fprintf_line(struct hists *hists, struct perf_hpp *hpp,
742f3705b06SJiri Olsa 			 int line, FILE *fp)
7437ccf4f90SNamhyung Kim {
7441240005eSJiri Olsa 	struct perf_hpp_fmt *fmt;
7457ccf4f90SNamhyung Kim 	const char *sep = symbol_conf.field_sep;
7465395a048SJiri Olsa 	bool first = true;
74729659ab4SJiri Olsa 	int span = 0;
7487ccf4f90SNamhyung Kim 
749f0786af5SJiri Olsa 	hists__for_each_format(hists, fmt) {
750361459f1SNamhyung Kim 		if (perf_hpp__should_skip(fmt, hists))
751e67d49a7SNamhyung Kim 			continue;
752e67d49a7SNamhyung Kim 
75329659ab4SJiri Olsa 		if (!first && !span)
754ea251d51SNamhyung Kim 			fprintf(fp, "%s", sep ?: "  ");
7555395a048SJiri Olsa 		else
7565395a048SJiri Olsa 			first = false;
7577ccf4f90SNamhyung Kim 
75829659ab4SJiri Olsa 		fmt->header(fmt, hpp, hists, line, &span);
75929659ab4SJiri Olsa 
76029659ab4SJiri Olsa 		if (!span)
7617a72a2e5SJiri Olsa 			fprintf(fp, "%s", hpp->buf);
7627ccf4f90SNamhyung Kim 	}
763f3705b06SJiri Olsa }
7647ccf4f90SNamhyung Kim 
765f3705b06SJiri Olsa static int
766f3705b06SJiri Olsa hists__fprintf_standard_headers(struct hists *hists,
767f3705b06SJiri Olsa 				struct perf_hpp *hpp,
768f3705b06SJiri Olsa 				FILE *fp)
769f3705b06SJiri Olsa {
770f3705b06SJiri Olsa 	struct perf_hpp_list *hpp_list = hists->hpp_list;
771f3705b06SJiri Olsa 	struct perf_hpp_fmt *fmt;
772f3705b06SJiri Olsa 	unsigned int width;
773f3705b06SJiri Olsa 	const char *sep = symbol_conf.field_sep;
774f3705b06SJiri Olsa 	bool first = true;
775f3705b06SJiri Olsa 	int line;
776f3705b06SJiri Olsa 
777f3705b06SJiri Olsa 	for (line = 0; line < hpp_list->nr_header_lines; line++) {
778f3705b06SJiri Olsa 		/* first # is displayed one level up */
779f3705b06SJiri Olsa 		if (line)
780f3705b06SJiri Olsa 			fprintf(fp, "# ");
781f3705b06SJiri Olsa 		fprintf_line(hists, hpp, line, fp);
7827ccf4f90SNamhyung Kim 		fprintf(fp, "\n");
783f3705b06SJiri Olsa 	}
7847ccf4f90SNamhyung Kim 
7857ccf4f90SNamhyung Kim 	if (sep)
786f3705b06SJiri Olsa 		return hpp_list->nr_header_lines;
7877ccf4f90SNamhyung Kim 
7885395a048SJiri Olsa 	first = true;
7895395a048SJiri Olsa 
790ea251d51SNamhyung Kim 	fprintf(fp, "# ");
791ea251d51SNamhyung Kim 
792f0786af5SJiri Olsa 	hists__for_each_format(hists, fmt) {
7931240005eSJiri Olsa 		unsigned int i;
794ea251d51SNamhyung Kim 
795361459f1SNamhyung Kim 		if (perf_hpp__should_skip(fmt, hists))
796e67d49a7SNamhyung Kim 			continue;
797e67d49a7SNamhyung Kim 
7985395a048SJiri Olsa 		if (!first)
799ea251d51SNamhyung Kim 			fprintf(fp, "%s", sep ?: "  ");
8005395a048SJiri Olsa 		else
8015395a048SJiri Olsa 			first = false;
802ea251d51SNamhyung Kim 
803da1b0407SJiri Olsa 		width = fmt->width(fmt, hpp, hists);
804ea251d51SNamhyung Kim 		for (i = 0; i < width; i++)
805ea251d51SNamhyung Kim 			fprintf(fp, ".");
8067ccf4f90SNamhyung Kim 	}
807ea251d51SNamhyung Kim 
8087ccf4f90SNamhyung Kim 	fprintf(fp, "\n");
8097ccf4f90SNamhyung Kim 	fprintf(fp, "#\n");
810f3705b06SJiri Olsa 	return hpp_list->nr_header_lines + 2;
81136592ebbSJiri Olsa }
81236592ebbSJiri Olsa 
8132d831454SJiri Olsa int hists__fprintf_headers(struct hists *hists, FILE *fp)
8147a72a2e5SJiri Olsa {
815d5278220SJiri Olsa 	char bf[1024];
8167a72a2e5SJiri Olsa 	struct perf_hpp dummy_hpp = {
8177a72a2e5SJiri Olsa 		.buf	= bf,
8187a72a2e5SJiri Olsa 		.size	= sizeof(bf),
8197a72a2e5SJiri Olsa 	};
8207a72a2e5SJiri Olsa 
8217a72a2e5SJiri Olsa 	fprintf(fp, "# ");
8227a72a2e5SJiri Olsa 
8237a72a2e5SJiri Olsa 	if (symbol_conf.report_hierarchy)
8247a72a2e5SJiri Olsa 		return hists__fprintf_hierarchy_headers(hists, &dummy_hpp, fp);
8257a72a2e5SJiri Olsa 	else
8267a72a2e5SJiri Olsa 		return hists__fprintf_standard_headers(hists, &dummy_hpp, fp);
8277a72a2e5SJiri Olsa 
8287a72a2e5SJiri Olsa }
8297a72a2e5SJiri Olsa 
83036592ebbSJiri Olsa size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
831d05e3aaeSJiri Olsa 		      int max_cols, float min_pcnt, FILE *fp,
832d05e3aaeSJiri Olsa 		      bool use_callchain)
83336592ebbSJiri Olsa {
83436592ebbSJiri Olsa 	struct rb_node *nd;
83536592ebbSJiri Olsa 	size_t ret = 0;
83636592ebbSJiri Olsa 	const char *sep = symbol_conf.field_sep;
83736592ebbSJiri Olsa 	int nr_rows = 0;
83836592ebbSJiri Olsa 	size_t linesz;
83936592ebbSJiri Olsa 	char *line = NULL;
84036592ebbSJiri Olsa 	unsigned indent;
84136592ebbSJiri Olsa 
84236592ebbSJiri Olsa 	init_rem_hits();
84336592ebbSJiri Olsa 
844e3b60bc9SNamhyung Kim 	hists__reset_column_width(hists);
84536592ebbSJiri Olsa 
84636592ebbSJiri Olsa 	if (symbol_conf.col_width_list_str)
84736592ebbSJiri Olsa 		perf_hpp__set_user_width(symbol_conf.col_width_list_str);
84836592ebbSJiri Olsa 
84936592ebbSJiri Olsa 	if (show_header)
85036592ebbSJiri Olsa 		nr_rows += hists__fprintf_headers(hists, fp);
85136592ebbSJiri Olsa 
85236592ebbSJiri Olsa 	if (max_rows && nr_rows >= max_rows)
8537ccf4f90SNamhyung Kim 		goto out;
8547ccf4f90SNamhyung Kim 
85599cf666cSArnaldo Carvalho de Melo 	linesz = hists__sort_list_width(hists) + 3 + 1;
8569754c4f9SJiri Olsa 	linesz += perf_hpp__color_overhead();
85799cf666cSArnaldo Carvalho de Melo 	line = malloc(linesz);
85899cf666cSArnaldo Carvalho de Melo 	if (line == NULL) {
85999cf666cSArnaldo Carvalho de Melo 		ret = -1;
86099cf666cSArnaldo Carvalho de Melo 		goto out;
86199cf666cSArnaldo Carvalho de Melo 	}
86299cf666cSArnaldo Carvalho de Melo 
863bd4abd39SNamhyung Kim 	indent = hists__overhead_width(hists) + 4;
864bd4abd39SNamhyung Kim 
865ef86d68aSNamhyung Kim 	for (nd = rb_first(&hists->entries); nd; nd = __rb_hierarchy_next(nd, HMD_FORCE_CHILD)) {
8667ccf4f90SNamhyung Kim 		struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
86714135663SNamhyung Kim 		float percent;
8687ccf4f90SNamhyung Kim 
8697ccf4f90SNamhyung Kim 		if (h->filtered)
8707ccf4f90SNamhyung Kim 			continue;
8717ccf4f90SNamhyung Kim 
87214135663SNamhyung Kim 		percent = hist_entry__get_percent_limit(h);
873064f1981SNamhyung Kim 		if (percent < min_pcnt)
874064f1981SNamhyung Kim 			continue;
875064f1981SNamhyung Kim 
876d05e3aaeSJiri Olsa 		ret += hist_entry__fprintf(h, max_cols, line, linesz, fp, use_callchain);
8777ccf4f90SNamhyung Kim 
8787ccf4f90SNamhyung Kim 		if (max_rows && ++nr_rows >= max_rows)
87999cf666cSArnaldo Carvalho de Melo 			break;
8807ccf4f90SNamhyung Kim 
881bd4abd39SNamhyung Kim 		/*
882bd4abd39SNamhyung Kim 		 * If all children are filtered out or percent-limited,
883bd4abd39SNamhyung Kim 		 * display "no entry >= x.xx%" message.
884bd4abd39SNamhyung Kim 		 */
885bd4abd39SNamhyung Kim 		if (!h->leaf && !hist_entry__has_hierarchy_children(h, min_pcnt)) {
886f58c95e3SNamhyung Kim 			int depth = hists->nr_hpp_node + h->depth + 1;
887bd4abd39SNamhyung Kim 
888f58c95e3SNamhyung Kim 			print_hierarchy_indent(sep, depth, spaces, fp);
889bd4abd39SNamhyung Kim 			fprintf(fp, "%*sno entry >= %.2f%%\n", indent, "", min_pcnt);
890bd4abd39SNamhyung Kim 
891bd4abd39SNamhyung Kim 			if (max_rows && ++nr_rows >= max_rows)
892bd4abd39SNamhyung Kim 				break;
893bd4abd39SNamhyung Kim 		}
894bd4abd39SNamhyung Kim 
8957ccf4f90SNamhyung Kim 		if (h->ms.map == NULL && verbose > 1) {
89693d5731dSArnaldo Carvalho de Melo 			__map_groups__fprintf_maps(h->thread->mg,
897acebd408SJiri Olsa 						   MAP__FUNCTION, fp);
8987ccf4f90SNamhyung Kim 			fprintf(fp, "%.10s end\n", graph_dotted_line);
8997ccf4f90SNamhyung Kim 		}
9007ccf4f90SNamhyung Kim 	}
90199cf666cSArnaldo Carvalho de Melo 
90299cf666cSArnaldo Carvalho de Melo 	free(line);
9037ccf4f90SNamhyung Kim out:
90474cf249dSArnaldo Carvalho de Melo 	zfree(&rem_sq_bracket);
9057ccf4f90SNamhyung Kim 
9067ccf4f90SNamhyung Kim 	return ret;
9077ccf4f90SNamhyung Kim }
9087ccf4f90SNamhyung Kim 
90952168eeaSArnaldo Carvalho de Melo size_t events_stats__fprintf(struct events_stats *stats, FILE *fp)
9107ccf4f90SNamhyung Kim {
9117ccf4f90SNamhyung Kim 	int i;
9127ccf4f90SNamhyung Kim 	size_t ret = 0;
9137ccf4f90SNamhyung Kim 
9147ccf4f90SNamhyung Kim 	for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
9157ccf4f90SNamhyung Kim 		const char *name;
9167ccf4f90SNamhyung Kim 
91752168eeaSArnaldo Carvalho de Melo 		if (stats->nr_events[i] == 0)
9187ccf4f90SNamhyung Kim 			continue;
9197ccf4f90SNamhyung Kim 
9207ccf4f90SNamhyung Kim 		name = perf_event__name(i);
9217ccf4f90SNamhyung Kim 		if (!strcmp(name, "UNKNOWN"))
9227ccf4f90SNamhyung Kim 			continue;
9237ccf4f90SNamhyung Kim 
9247ccf4f90SNamhyung Kim 		ret += fprintf(fp, "%16s events: %10d\n", name,
92552168eeaSArnaldo Carvalho de Melo 			       stats->nr_events[i]);
9267ccf4f90SNamhyung Kim 	}
9277ccf4f90SNamhyung Kim 
9287ccf4f90SNamhyung Kim 	return ret;
9297ccf4f90SNamhyung Kim }
930