xref: /linux/tools/perf/ui/stdio/hist.c (revision f2a39fe84901df2b3d1bec3459b65cee3e8db57c)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
27ccf4f90SNamhyung Kim #include <stdio.h>
3*f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h>
48e99b6d4SArnaldo Carvalho de Melo #include <linux/string.h>
57ccf4f90SNamhyung Kim 
6b10ba7f1SArnaldo Carvalho de Melo #include "../../util/callchain.h"
7185bcb92SArnaldo Carvalho de Melo #include "../../util/debug.h"
87ccf4f90SNamhyung Kim #include "../../util/hist.h"
97b644f9aSArnaldo Carvalho de Melo #include "../../util/map.h"
1041f30914SArnaldo Carvalho de Melo #include "../../util/map_groups.h"
11daecf9e0SArnaldo Carvalho de Melo #include "../../util/symbol.h"
127ccf4f90SNamhyung Kim #include "../../util/sort.h"
135b9e2146SNamhyung Kim #include "../../util/evsel.h"
14632a5cabSArnaldo Carvalho de Melo #include "../../util/srcline.h"
15a067558eSArnaldo Carvalho de Melo #include "../../util/string2.h"
16e7ff8920SArnaldo Carvalho de Melo #include "../../util/thread.h"
173052ba56SArnaldo Carvalho de Melo #include <linux/ctype.h>
187f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
197ccf4f90SNamhyung Kim 
207ccf4f90SNamhyung Kim static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
217ccf4f90SNamhyung Kim {
227ccf4f90SNamhyung Kim 	int i;
237ccf4f90SNamhyung Kim 	int ret = fprintf(fp, "            ");
247ccf4f90SNamhyung Kim 
257ccf4f90SNamhyung Kim 	for (i = 0; i < left_margin; i++)
267ccf4f90SNamhyung Kim 		ret += fprintf(fp, " ");
277ccf4f90SNamhyung Kim 
287ccf4f90SNamhyung Kim 	return ret;
297ccf4f90SNamhyung Kim }
307ccf4f90SNamhyung Kim 
317ccf4f90SNamhyung Kim static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
327ccf4f90SNamhyung Kim 					  int left_margin)
337ccf4f90SNamhyung Kim {
347ccf4f90SNamhyung Kim 	int i;
357ccf4f90SNamhyung Kim 	size_t ret = callchain__fprintf_left_margin(fp, left_margin);
367ccf4f90SNamhyung Kim 
377ccf4f90SNamhyung Kim 	for (i = 0; i < depth; i++)
387ccf4f90SNamhyung Kim 		if (depth_mask & (1 << i))
397ccf4f90SNamhyung Kim 			ret += fprintf(fp, "|          ");
407ccf4f90SNamhyung Kim 		else
417ccf4f90SNamhyung Kim 			ret += fprintf(fp, "           ");
427ccf4f90SNamhyung Kim 
437ccf4f90SNamhyung Kim 	ret += fprintf(fp, "\n");
447ccf4f90SNamhyung Kim 
457ccf4f90SNamhyung Kim 	return ret;
467ccf4f90SNamhyung Kim }
477ccf4f90SNamhyung Kim 
485ab250caSNamhyung Kim static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_node *node,
495ab250caSNamhyung Kim 				     struct callchain_list *chain,
507ccf4f90SNamhyung Kim 				     int depth, int depth_mask, int period,
515ab250caSNamhyung Kim 				     u64 total_samples, int left_margin)
527ccf4f90SNamhyung Kim {
537ccf4f90SNamhyung Kim 	int i;
547ccf4f90SNamhyung Kim 	size_t ret = 0;
558577ae6bSJin Yao 	char bf[1024], *alloc_str = NULL;
568577ae6bSJin Yao 	char buf[64];
578577ae6bSJin Yao 	const char *str;
587ccf4f90SNamhyung Kim 
597ccf4f90SNamhyung Kim 	ret += callchain__fprintf_left_margin(fp, left_margin);
607ccf4f90SNamhyung Kim 	for (i = 0; i < depth; i++) {
617ccf4f90SNamhyung Kim 		if (depth_mask & (1 << i))
627ccf4f90SNamhyung Kim 			ret += fprintf(fp, "|");
637ccf4f90SNamhyung Kim 		else
647ccf4f90SNamhyung Kim 			ret += fprintf(fp, " ");
657ccf4f90SNamhyung Kim 		if (!period && i == depth - 1) {
665ab250caSNamhyung Kim 			ret += fprintf(fp, "--");
675ab250caSNamhyung Kim 			ret += callchain_node__fprintf_value(node, fp, total_samples);
685ab250caSNamhyung Kim 			ret += fprintf(fp, "--");
697ccf4f90SNamhyung Kim 		} else
707ccf4f90SNamhyung Kim 			ret += fprintf(fp, "%s", "          ");
717ccf4f90SNamhyung Kim 	}
728577ae6bSJin Yao 
738577ae6bSJin Yao 	str = callchain_list__sym_name(chain, bf, sizeof(bf), false);
748577ae6bSJin Yao 
758577ae6bSJin Yao 	if (symbol_conf.show_branchflag_count) {
76c4ee0625SJin Yao 		callchain_list_counts__printf_value(chain, NULL,
778577ae6bSJin Yao 						    buf, sizeof(buf));
788577ae6bSJin Yao 
798577ae6bSJin Yao 		if (asprintf(&alloc_str, "%s%s", str, buf) < 0)
808577ae6bSJin Yao 			str = "Not enough memory!";
818577ae6bSJin Yao 		else
828577ae6bSJin Yao 			str = alloc_str;
838577ae6bSJin Yao 	}
848577ae6bSJin Yao 
858577ae6bSJin Yao 	fputs(str, fp);
862989ccaaSAndi Kleen 	fputc('\n', fp);
878577ae6bSJin Yao 	free(alloc_str);
880db64dd0SJin Yao 
897ccf4f90SNamhyung Kim 	return ret;
907ccf4f90SNamhyung Kim }
917ccf4f90SNamhyung Kim 
927ccf4f90SNamhyung Kim static struct symbol *rem_sq_bracket;
937ccf4f90SNamhyung Kim static struct callchain_list rem_hits;
947ccf4f90SNamhyung Kim 
957ccf4f90SNamhyung Kim static void init_rem_hits(void)
967ccf4f90SNamhyung Kim {
977ccf4f90SNamhyung Kim 	rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
987ccf4f90SNamhyung Kim 	if (!rem_sq_bracket) {
997ccf4f90SNamhyung Kim 		fprintf(stderr, "Not enough memory to display remaining hits\n");
1007ccf4f90SNamhyung Kim 		return;
1017ccf4f90SNamhyung Kim 	}
1027ccf4f90SNamhyung Kim 
1037ccf4f90SNamhyung Kim 	strcpy(rem_sq_bracket->name, "[...]");
1047ccf4f90SNamhyung Kim 	rem_hits.ms.sym = rem_sq_bracket;
1057ccf4f90SNamhyung Kim }
1067ccf4f90SNamhyung Kim 
1077ccf4f90SNamhyung Kim static size_t __callchain__fprintf_graph(FILE *fp, struct rb_root *root,
1087ccf4f90SNamhyung Kim 					 u64 total_samples, int depth,
1097ccf4f90SNamhyung Kim 					 int depth_mask, int left_margin)
1107ccf4f90SNamhyung Kim {
1117ccf4f90SNamhyung Kim 	struct rb_node *node, *next;
112f2af0086SNamhyung Kim 	struct callchain_node *child = NULL;
1137ccf4f90SNamhyung Kim 	struct callchain_list *chain;
1147ccf4f90SNamhyung Kim 	int new_depth_mask = depth_mask;
1157ccf4f90SNamhyung Kim 	u64 remaining;
1167ccf4f90SNamhyung Kim 	size_t ret = 0;
1177ccf4f90SNamhyung Kim 	int i;
1187ccf4f90SNamhyung Kim 	uint entries_printed = 0;
119f2af0086SNamhyung Kim 	int cumul_count = 0;
1207ccf4f90SNamhyung Kim 
1217ccf4f90SNamhyung Kim 	remaining = total_samples;
1227ccf4f90SNamhyung Kim 
1237ccf4f90SNamhyung Kim 	node = rb_first(root);
1247ccf4f90SNamhyung Kim 	while (node) {
1257ccf4f90SNamhyung Kim 		u64 new_total;
1267ccf4f90SNamhyung Kim 		u64 cumul;
1277ccf4f90SNamhyung Kim 
1287ccf4f90SNamhyung Kim 		child = rb_entry(node, struct callchain_node, rb_node);
1297ccf4f90SNamhyung Kim 		cumul = callchain_cumul_hits(child);
1307ccf4f90SNamhyung Kim 		remaining -= cumul;
131f2af0086SNamhyung Kim 		cumul_count += callchain_cumul_counts(child);
1327ccf4f90SNamhyung Kim 
1337ccf4f90SNamhyung Kim 		/*
1347ccf4f90SNamhyung Kim 		 * The depth mask manages the output of pipes that show
1357ccf4f90SNamhyung Kim 		 * the depth. We don't want to keep the pipes of the current
1367ccf4f90SNamhyung Kim 		 * level for the last child of this depth.
1377ccf4f90SNamhyung Kim 		 * Except if we have remaining filtered hits. They will
1387ccf4f90SNamhyung Kim 		 * supersede the last child
1397ccf4f90SNamhyung Kim 		 */
1407ccf4f90SNamhyung Kim 		next = rb_next(node);
1417ccf4f90SNamhyung Kim 		if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
1427ccf4f90SNamhyung Kim 			new_depth_mask &= ~(1 << (depth - 1));
1437ccf4f90SNamhyung Kim 
1447ccf4f90SNamhyung Kim 		/*
1457ccf4f90SNamhyung Kim 		 * But we keep the older depth mask for the line separator
1467ccf4f90SNamhyung Kim 		 * to keep the level link until we reach the last child
1477ccf4f90SNamhyung Kim 		 */
1487ccf4f90SNamhyung Kim 		ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
1497ccf4f90SNamhyung Kim 						   left_margin);
1507ccf4f90SNamhyung Kim 		i = 0;
1517ccf4f90SNamhyung Kim 		list_for_each_entry(chain, &child->val, list) {
1525ab250caSNamhyung Kim 			ret += ipchain__fprintf_graph(fp, child, chain, depth,
1537ccf4f90SNamhyung Kim 						      new_depth_mask, i++,
1547ccf4f90SNamhyung Kim 						      total_samples,
1557ccf4f90SNamhyung Kim 						      left_margin);
1567ccf4f90SNamhyung Kim 		}
1577ccf4f90SNamhyung Kim 
1587ccf4f90SNamhyung Kim 		if (callchain_param.mode == CHAIN_GRAPH_REL)
1597ccf4f90SNamhyung Kim 			new_total = child->children_hit;
1607ccf4f90SNamhyung Kim 		else
1617ccf4f90SNamhyung Kim 			new_total = total_samples;
1627ccf4f90SNamhyung Kim 
1637ccf4f90SNamhyung Kim 		ret += __callchain__fprintf_graph(fp, &child->rb_root, new_total,
1647ccf4f90SNamhyung Kim 						  depth + 1,
1657ccf4f90SNamhyung Kim 						  new_depth_mask | (1 << depth),
1667ccf4f90SNamhyung Kim 						  left_margin);
1677ccf4f90SNamhyung Kim 		node = next;
1687ccf4f90SNamhyung Kim 		if (++entries_printed == callchain_param.print_limit)
1697ccf4f90SNamhyung Kim 			break;
1707ccf4f90SNamhyung Kim 	}
1717ccf4f90SNamhyung Kim 
1727ccf4f90SNamhyung Kim 	if (callchain_param.mode == CHAIN_GRAPH_REL &&
1737ccf4f90SNamhyung Kim 		remaining && remaining != total_samples) {
1745ab250caSNamhyung Kim 		struct callchain_node rem_node = {
1755ab250caSNamhyung Kim 			.hit = remaining,
1765ab250caSNamhyung Kim 		};
1777ccf4f90SNamhyung Kim 
1787ccf4f90SNamhyung Kim 		if (!rem_sq_bracket)
1797ccf4f90SNamhyung Kim 			return ret;
1807ccf4f90SNamhyung Kim 
181f2af0086SNamhyung Kim 		if (callchain_param.value == CCVAL_COUNT && child && child->parent) {
182f2af0086SNamhyung Kim 			rem_node.count = child->parent->children_count - cumul_count;
183f2af0086SNamhyung Kim 			if (rem_node.count <= 0)
184f2af0086SNamhyung Kim 				return ret;
185f2af0086SNamhyung Kim 		}
186f2af0086SNamhyung Kim 
1877ccf4f90SNamhyung Kim 		new_depth_mask &= ~(1 << (depth - 1));
1885ab250caSNamhyung Kim 		ret += ipchain__fprintf_graph(fp, &rem_node, &rem_hits, depth,
1897ccf4f90SNamhyung Kim 					      new_depth_mask, 0, total_samples,
1905ab250caSNamhyung Kim 					      left_margin);
1917ccf4f90SNamhyung Kim 	}
1927ccf4f90SNamhyung Kim 
1937ccf4f90SNamhyung Kim 	return ret;
1947ccf4f90SNamhyung Kim }
1957ccf4f90SNamhyung Kim 
1967ed5d6e2SNamhyung Kim /*
1977ed5d6e2SNamhyung Kim  * If have one single callchain root, don't bother printing
1987ed5d6e2SNamhyung Kim  * its percentage (100 % in fractal mode and the same percentage
1997ed5d6e2SNamhyung Kim  * than the hist in graph mode). This also avoid one level of column.
2007ed5d6e2SNamhyung Kim  *
2017ed5d6e2SNamhyung Kim  * However when percent-limit applied, it's possible that single callchain
2027ed5d6e2SNamhyung Kim  * node have different (non-100% in fractal mode) percentage.
2037ed5d6e2SNamhyung Kim  */
2047ed5d6e2SNamhyung Kim static bool need_percent_display(struct rb_node *node, u64 parent_samples)
2057ed5d6e2SNamhyung Kim {
2067ed5d6e2SNamhyung Kim 	struct callchain_node *cnode;
2077ed5d6e2SNamhyung Kim 
2087ed5d6e2SNamhyung Kim 	if (rb_next(node))
2097ed5d6e2SNamhyung Kim 		return true;
2107ed5d6e2SNamhyung Kim 
2117ed5d6e2SNamhyung Kim 	cnode = rb_entry(node, struct callchain_node, rb_node);
2127ed5d6e2SNamhyung Kim 	return callchain_cumul_hits(cnode) != parent_samples;
2137ed5d6e2SNamhyung Kim }
2147ed5d6e2SNamhyung Kim 
2157ccf4f90SNamhyung Kim static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root,
21654d27b31SNamhyung Kim 				       u64 total_samples, u64 parent_samples,
21754d27b31SNamhyung Kim 				       int left_margin)
2187ccf4f90SNamhyung Kim {
2197ccf4f90SNamhyung Kim 	struct callchain_node *cnode;
2207ccf4f90SNamhyung Kim 	struct callchain_list *chain;
2217ccf4f90SNamhyung Kim 	u32 entries_printed = 0;
2227ccf4f90SNamhyung Kim 	bool printed = false;
2237ccf4f90SNamhyung Kim 	struct rb_node *node;
2247ccf4f90SNamhyung Kim 	int i = 0;
2257ccf4f90SNamhyung Kim 	int ret = 0;
2262989ccaaSAndi Kleen 	char bf[1024];
2277ccf4f90SNamhyung Kim 
2287ccf4f90SNamhyung Kim 	node = rb_first(root);
2297ed5d6e2SNamhyung Kim 	if (node && !need_percent_display(node, parent_samples)) {
2307ccf4f90SNamhyung Kim 		cnode = rb_entry(node, struct callchain_node, rb_node);
2317ccf4f90SNamhyung Kim 		list_for_each_entry(chain, &cnode->val, list) {
2327ccf4f90SNamhyung Kim 			/*
2337ccf4f90SNamhyung Kim 			 * If we sort by symbol, the first entry is the same than
2347ccf4f90SNamhyung Kim 			 * the symbol. No need to print it otherwise it appears as
2357ccf4f90SNamhyung Kim 			 * displayed twice.
2367ccf4f90SNamhyung Kim 			 */
237cfaa154bSNamhyung Kim 			if (!i++ && field_order == NULL &&
2388e99b6d4SArnaldo Carvalho de Melo 			    sort_order && strstarts(sort_order, "sym"))
2397ccf4f90SNamhyung Kim 				continue;
2400db64dd0SJin Yao 
2417ccf4f90SNamhyung Kim 			if (!printed) {
2427ccf4f90SNamhyung Kim 				ret += callchain__fprintf_left_margin(fp, left_margin);
2437ccf4f90SNamhyung Kim 				ret += fprintf(fp, "|\n");
2447ccf4f90SNamhyung Kim 				ret += callchain__fprintf_left_margin(fp, left_margin);
2457ccf4f90SNamhyung Kim 				ret += fprintf(fp, "---");
2467ccf4f90SNamhyung Kim 				left_margin += 3;
2477ccf4f90SNamhyung Kim 				printed = true;
2487ccf4f90SNamhyung Kim 			} else
2497ccf4f90SNamhyung Kim 				ret += callchain__fprintf_left_margin(fp, left_margin);
2507ccf4f90SNamhyung Kim 
2518577ae6bSJin Yao 			ret += fprintf(fp, "%s",
2528577ae6bSJin Yao 				       callchain_list__sym_name(chain, bf,
2538577ae6bSJin Yao 								sizeof(bf),
2542989ccaaSAndi Kleen 								false));
2557ccf4f90SNamhyung Kim 
2568577ae6bSJin Yao 			if (symbol_conf.show_branchflag_count)
2578577ae6bSJin Yao 				ret += callchain_list_counts__printf_value(
258c4ee0625SJin Yao 						chain, fp, NULL, 0);
2598577ae6bSJin Yao 			ret += fprintf(fp, "\n");
2608577ae6bSJin Yao 
2617ccf4f90SNamhyung Kim 			if (++entries_printed == callchain_param.print_limit)
2627ccf4f90SNamhyung Kim 				break;
2637ccf4f90SNamhyung Kim 		}
2647ccf4f90SNamhyung Kim 		root = &cnode->rb_root;
2657ccf4f90SNamhyung Kim 	}
2667ccf4f90SNamhyung Kim 
26754d27b31SNamhyung Kim 	if (callchain_param.mode == CHAIN_GRAPH_REL)
26854d27b31SNamhyung Kim 		total_samples = parent_samples;
26954d27b31SNamhyung Kim 
2707ccf4f90SNamhyung Kim 	ret += __callchain__fprintf_graph(fp, root, total_samples,
2717ccf4f90SNamhyung Kim 					  1, 1, left_margin);
2723848c23bSNamhyung Kim 	if (ret) {
2733848c23bSNamhyung Kim 		/* do not add a blank line if it printed nothing */
2747ccf4f90SNamhyung Kim 		ret += fprintf(fp, "\n");
2753848c23bSNamhyung Kim 	}
2767ccf4f90SNamhyung Kim 
2777ccf4f90SNamhyung Kim 	return ret;
2787ccf4f90SNamhyung Kim }
2797ccf4f90SNamhyung Kim 
280316c7136SArnaldo Carvalho de Melo static size_t __callchain__fprintf_flat(FILE *fp, struct callchain_node *node,
2817ccf4f90SNamhyung Kim 					u64 total_samples)
2827ccf4f90SNamhyung Kim {
2837ccf4f90SNamhyung Kim 	struct callchain_list *chain;
2847ccf4f90SNamhyung Kim 	size_t ret = 0;
2852989ccaaSAndi Kleen 	char bf[1024];
2867ccf4f90SNamhyung Kim 
287316c7136SArnaldo Carvalho de Melo 	if (!node)
2887ccf4f90SNamhyung Kim 		return 0;
2897ccf4f90SNamhyung Kim 
290316c7136SArnaldo Carvalho de Melo 	ret += __callchain__fprintf_flat(fp, node->parent, total_samples);
2917ccf4f90SNamhyung Kim 
2927ccf4f90SNamhyung Kim 
293316c7136SArnaldo Carvalho de Melo 	list_for_each_entry(chain, &node->val, list) {
2947ccf4f90SNamhyung Kim 		if (chain->ip >= PERF_CONTEXT_MAX)
2957ccf4f90SNamhyung Kim 			continue;
2962989ccaaSAndi Kleen 		ret += fprintf(fp, "                %s\n", callchain_list__sym_name(chain,
2972989ccaaSAndi Kleen 					bf, sizeof(bf), false));
2987ccf4f90SNamhyung Kim 	}
2997ccf4f90SNamhyung Kim 
3007ccf4f90SNamhyung Kim 	return ret;
3017ccf4f90SNamhyung Kim }
3027ccf4f90SNamhyung Kim 
303316c7136SArnaldo Carvalho de Melo static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *tree,
3047ccf4f90SNamhyung Kim 				      u64 total_samples)
3057ccf4f90SNamhyung Kim {
3067ccf4f90SNamhyung Kim 	size_t ret = 0;
3077ccf4f90SNamhyung Kim 	u32 entries_printed = 0;
3087ccf4f90SNamhyung Kim 	struct callchain_node *chain;
309316c7136SArnaldo Carvalho de Melo 	struct rb_node *rb_node = rb_first(tree);
3107ccf4f90SNamhyung Kim 
3117ccf4f90SNamhyung Kim 	while (rb_node) {
3127ccf4f90SNamhyung Kim 		chain = rb_entry(rb_node, struct callchain_node, rb_node);
3137ccf4f90SNamhyung Kim 
3145ab250caSNamhyung Kim 		ret += fprintf(fp, "           ");
3155ab250caSNamhyung Kim 		ret += callchain_node__fprintf_value(chain, fp, total_samples);
3165ab250caSNamhyung Kim 		ret += fprintf(fp, "\n");
3177ccf4f90SNamhyung Kim 		ret += __callchain__fprintf_flat(fp, chain, total_samples);
3187ccf4f90SNamhyung Kim 		ret += fprintf(fp, "\n");
3197ccf4f90SNamhyung Kim 		if (++entries_printed == callchain_param.print_limit)
3207ccf4f90SNamhyung Kim 			break;
3217ccf4f90SNamhyung Kim 
3227ccf4f90SNamhyung Kim 		rb_node = rb_next(rb_node);
3237ccf4f90SNamhyung Kim 	}
3247ccf4f90SNamhyung Kim 
3257ccf4f90SNamhyung Kim 	return ret;
3267ccf4f90SNamhyung Kim }
3277ccf4f90SNamhyung Kim 
32826e77924SNamhyung Kim static size_t __callchain__fprintf_folded(FILE *fp, struct callchain_node *node)
32926e77924SNamhyung Kim {
33026e77924SNamhyung Kim 	const char *sep = symbol_conf.field_sep ?: ";";
33126e77924SNamhyung Kim 	struct callchain_list *chain;
33226e77924SNamhyung Kim 	size_t ret = 0;
33326e77924SNamhyung Kim 	char bf[1024];
33426e77924SNamhyung Kim 	bool first;
33526e77924SNamhyung Kim 
33626e77924SNamhyung Kim 	if (!node)
33726e77924SNamhyung Kim 		return 0;
33826e77924SNamhyung Kim 
33926e77924SNamhyung Kim 	ret += __callchain__fprintf_folded(fp, node->parent);
34026e77924SNamhyung Kim 
34126e77924SNamhyung Kim 	first = (ret == 0);
34226e77924SNamhyung Kim 	list_for_each_entry(chain, &node->val, list) {
34326e77924SNamhyung Kim 		if (chain->ip >= PERF_CONTEXT_MAX)
34426e77924SNamhyung Kim 			continue;
34526e77924SNamhyung Kim 		ret += fprintf(fp, "%s%s", first ? "" : sep,
34626e77924SNamhyung Kim 			       callchain_list__sym_name(chain,
34726e77924SNamhyung Kim 						bf, sizeof(bf), false));
34826e77924SNamhyung Kim 		first = false;
34926e77924SNamhyung Kim 	}
35026e77924SNamhyung Kim 
35126e77924SNamhyung Kim 	return ret;
35226e77924SNamhyung Kim }
35326e77924SNamhyung Kim 
35426e77924SNamhyung Kim static size_t callchain__fprintf_folded(FILE *fp, struct rb_root *tree,
35526e77924SNamhyung Kim 					u64 total_samples)
35626e77924SNamhyung Kim {
35726e77924SNamhyung Kim 	size_t ret = 0;
35826e77924SNamhyung Kim 	u32 entries_printed = 0;
35926e77924SNamhyung Kim 	struct callchain_node *chain;
36026e77924SNamhyung Kim 	struct rb_node *rb_node = rb_first(tree);
36126e77924SNamhyung Kim 
36226e77924SNamhyung Kim 	while (rb_node) {
36326e77924SNamhyung Kim 
36426e77924SNamhyung Kim 		chain = rb_entry(rb_node, struct callchain_node, rb_node);
36526e77924SNamhyung Kim 
3665ab250caSNamhyung Kim 		ret += callchain_node__fprintf_value(chain, fp, total_samples);
3675ab250caSNamhyung Kim 		ret += fprintf(fp, " ");
36826e77924SNamhyung Kim 		ret += __callchain__fprintf_folded(fp, chain);
36926e77924SNamhyung Kim 		ret += fprintf(fp, "\n");
37026e77924SNamhyung Kim 		if (++entries_printed == callchain_param.print_limit)
37126e77924SNamhyung Kim 			break;
37226e77924SNamhyung Kim 
37326e77924SNamhyung Kim 		rb_node = rb_next(rb_node);
37426e77924SNamhyung Kim 	}
37526e77924SNamhyung Kim 
37626e77924SNamhyung Kim 	return ret;
37726e77924SNamhyung Kim }
37826e77924SNamhyung Kim 
3797ccf4f90SNamhyung Kim static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
3807ccf4f90SNamhyung Kim 					    u64 total_samples, int left_margin,
3817ccf4f90SNamhyung Kim 					    FILE *fp)
3827ccf4f90SNamhyung Kim {
38354d27b31SNamhyung Kim 	u64 parent_samples = he->stat.period;
38454d27b31SNamhyung Kim 
38554d27b31SNamhyung Kim 	if (symbol_conf.cumulate_callchain)
38654d27b31SNamhyung Kim 		parent_samples = he->stat_acc->period;
38754d27b31SNamhyung Kim 
3887ccf4f90SNamhyung Kim 	switch (callchain_param.mode) {
3897ccf4f90SNamhyung Kim 	case CHAIN_GRAPH_REL:
39054d27b31SNamhyung Kim 		return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
39154d27b31SNamhyung Kim 						parent_samples, left_margin);
3927ccf4f90SNamhyung Kim 		break;
3937ccf4f90SNamhyung Kim 	case CHAIN_GRAPH_ABS:
3947ccf4f90SNamhyung Kim 		return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
39554d27b31SNamhyung Kim 						parent_samples, left_margin);
3967ccf4f90SNamhyung Kim 		break;
3977ccf4f90SNamhyung Kim 	case CHAIN_FLAT:
3987ccf4f90SNamhyung Kim 		return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples);
3997ccf4f90SNamhyung Kim 		break;
40026e77924SNamhyung Kim 	case CHAIN_FOLDED:
40126e77924SNamhyung Kim 		return callchain__fprintf_folded(fp, &he->sorted_chain, total_samples);
40226e77924SNamhyung Kim 		break;
4037ccf4f90SNamhyung Kim 	case CHAIN_NONE:
4047ccf4f90SNamhyung Kim 		break;
4057ccf4f90SNamhyung Kim 	default:
4067ccf4f90SNamhyung Kim 		pr_err("Bad callchain mode\n");
4077ccf4f90SNamhyung Kim 	}
4087ccf4f90SNamhyung Kim 
4097ccf4f90SNamhyung Kim 	return 0;
4107ccf4f90SNamhyung Kim }
4117ccf4f90SNamhyung Kim 
412bd28d0c5SJiri Olsa int __hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp,
4139da44db1SJiri Olsa 			   struct perf_hpp_list *hpp_list)
414be0e6d10SJiri Olsa {
415be0e6d10SJiri Olsa 	const char *sep = symbol_conf.field_sep;
416be0e6d10SJiri Olsa 	struct perf_hpp_fmt *fmt;
417be0e6d10SJiri Olsa 	char *start = hpp->buf;
418be0e6d10SJiri Olsa 	int ret;
419be0e6d10SJiri Olsa 	bool first = true;
420be0e6d10SJiri Olsa 
421be0e6d10SJiri Olsa 	if (symbol_conf.exclude_other && !he->parent)
422be0e6d10SJiri Olsa 		return 0;
423be0e6d10SJiri Olsa 
4249da44db1SJiri Olsa 	perf_hpp_list__for_each_format(hpp_list, fmt) {
425361459f1SNamhyung Kim 		if (perf_hpp__should_skip(fmt, he->hists))
426e67d49a7SNamhyung Kim 			continue;
427e67d49a7SNamhyung Kim 
428be0e6d10SJiri Olsa 		/*
429be0e6d10SJiri Olsa 		 * If there's no field_sep, we still need
430be0e6d10SJiri Olsa 		 * to display initial '  '.
431be0e6d10SJiri Olsa 		 */
432be0e6d10SJiri Olsa 		if (!sep || !first) {
433be0e6d10SJiri Olsa 			ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: "  ");
434be0e6d10SJiri Olsa 			advance_hpp(hpp, ret);
435be0e6d10SJiri Olsa 		} else
436be0e6d10SJiri Olsa 			first = false;
437be0e6d10SJiri Olsa 
4389754c4f9SJiri Olsa 		if (perf_hpp__use_color() && fmt->color)
439be0e6d10SJiri Olsa 			ret = fmt->color(fmt, hpp, he);
440be0e6d10SJiri Olsa 		else
441be0e6d10SJiri Olsa 			ret = fmt->entry(fmt, hpp, he);
442be0e6d10SJiri Olsa 
44389fee709SArnaldo Carvalho de Melo 		ret = hist_entry__snprintf_alignment(he, hpp, fmt, ret);
444be0e6d10SJiri Olsa 		advance_hpp(hpp, ret);
445be0e6d10SJiri Olsa 	}
446be0e6d10SJiri Olsa 
447be0e6d10SJiri Olsa 	return hpp->buf - start;
448be0e6d10SJiri Olsa }
449be0e6d10SJiri Olsa 
4509da44db1SJiri Olsa static int hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp)
4519da44db1SJiri Olsa {
4529da44db1SJiri Olsa 	return __hist_entry__snprintf(he, hpp, he->hists->hpp_list);
4539da44db1SJiri Olsa }
4549da44db1SJiri Olsa 
455ef86d68aSNamhyung Kim static int hist_entry__hierarchy_fprintf(struct hist_entry *he,
456ef86d68aSNamhyung Kim 					 struct perf_hpp *hpp,
4572dbbe9f2SNamhyung Kim 					 struct hists *hists,
458ef86d68aSNamhyung Kim 					 FILE *fp)
459ef86d68aSNamhyung Kim {
460ef86d68aSNamhyung Kim 	const char *sep = symbol_conf.field_sep;
461ef86d68aSNamhyung Kim 	struct perf_hpp_fmt *fmt;
462f58c95e3SNamhyung Kim 	struct perf_hpp_list_node *fmt_node;
463ef86d68aSNamhyung Kim 	char *buf = hpp->buf;
464cb1fab91SNamhyung Kim 	size_t size = hpp->size;
465ef86d68aSNamhyung Kim 	int ret, printed = 0;
466ef86d68aSNamhyung Kim 	bool first = true;
467ef86d68aSNamhyung Kim 
468ef86d68aSNamhyung Kim 	if (symbol_conf.exclude_other && !he->parent)
469ef86d68aSNamhyung Kim 		return 0;
470ef86d68aSNamhyung Kim 
471ef86d68aSNamhyung Kim 	ret = scnprintf(hpp->buf, hpp->size, "%*s", he->depth * HIERARCHY_INDENT, "");
472ef86d68aSNamhyung Kim 	advance_hpp(hpp, ret);
473ef86d68aSNamhyung Kim 
474f58c95e3SNamhyung Kim 	/* the first hpp_list_node is for overhead columns */
475f58c95e3SNamhyung Kim 	fmt_node = list_first_entry(&hists->hpp_formats,
476f58c95e3SNamhyung Kim 				    struct perf_hpp_list_node, list);
477f58c95e3SNamhyung Kim 	perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
478ef86d68aSNamhyung Kim 		/*
479ef86d68aSNamhyung Kim 		 * If there's no field_sep, we still need
480ef86d68aSNamhyung Kim 		 * to display initial '  '.
481ef86d68aSNamhyung Kim 		 */
482ef86d68aSNamhyung Kim 		if (!sep || !first) {
483ef86d68aSNamhyung Kim 			ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: "  ");
484ef86d68aSNamhyung Kim 			advance_hpp(hpp, ret);
485ef86d68aSNamhyung Kim 		} else
486ef86d68aSNamhyung Kim 			first = false;
487ef86d68aSNamhyung Kim 
488ef86d68aSNamhyung Kim 		if (perf_hpp__use_color() && fmt->color)
489ef86d68aSNamhyung Kim 			ret = fmt->color(fmt, hpp, he);
490ef86d68aSNamhyung Kim 		else
491ef86d68aSNamhyung Kim 			ret = fmt->entry(fmt, hpp, he);
492ef86d68aSNamhyung Kim 
493ef86d68aSNamhyung Kim 		ret = hist_entry__snprintf_alignment(he, hpp, fmt, ret);
494ef86d68aSNamhyung Kim 		advance_hpp(hpp, ret);
495ef86d68aSNamhyung Kim 	}
496ef86d68aSNamhyung Kim 
4971b2dbbf4SNamhyung Kim 	if (!sep)
498ef86d68aSNamhyung Kim 		ret = scnprintf(hpp->buf, hpp->size, "%*s",
4992dbbe9f2SNamhyung Kim 				(hists->nr_hpp_node - 2) * HIERARCHY_INDENT, "");
500ef86d68aSNamhyung Kim 	advance_hpp(hpp, ret);
501ef86d68aSNamhyung Kim 
502cb1fab91SNamhyung Kim 	printed += fprintf(fp, "%s", buf);
503cb1fab91SNamhyung Kim 
5041b2dbbf4SNamhyung Kim 	perf_hpp_list__for_each_format(he->hpp_list, fmt) {
505cb1fab91SNamhyung Kim 		hpp->buf  = buf;
506cb1fab91SNamhyung Kim 		hpp->size = size;
507cb1fab91SNamhyung Kim 
508ef86d68aSNamhyung Kim 		/*
509ef86d68aSNamhyung Kim 		 * No need to call hist_entry__snprintf_alignment() since this
510ef86d68aSNamhyung Kim 		 * fmt is always the last column in the hierarchy mode.
511ef86d68aSNamhyung Kim 		 */
512ef86d68aSNamhyung Kim 		if (perf_hpp__use_color() && fmt->color)
513ef86d68aSNamhyung Kim 			fmt->color(fmt, hpp, he);
514ef86d68aSNamhyung Kim 		else
515ef86d68aSNamhyung Kim 			fmt->entry(fmt, hpp, he);
516ef86d68aSNamhyung Kim 
517cb1fab91SNamhyung Kim 		/*
518cb1fab91SNamhyung Kim 		 * dynamic entries are right-aligned but we want left-aligned
519cb1fab91SNamhyung Kim 		 * in the hierarchy mode
520cb1fab91SNamhyung Kim 		 */
52132858480SArnaldo Carvalho de Melo 		printed += fprintf(fp, "%s%s", sep ?: "  ", skip_spaces(buf));
5221b2dbbf4SNamhyung Kim 	}
5231b2dbbf4SNamhyung Kim 	printed += putc('\n', fp);
524ef86d68aSNamhyung Kim 
525fabd37b8SArnaldo Carvalho de Melo 	if (he->leaf && hist_entry__has_callchains(he) && symbol_conf.use_callchain) {
526ef86d68aSNamhyung Kim 		u64 total = hists__total_period(hists);
527ef86d68aSNamhyung Kim 
528ef86d68aSNamhyung Kim 		printed += hist_entry_callchain__fprintf(he, total, 0, fp);
529ef86d68aSNamhyung Kim 		goto out;
530ef86d68aSNamhyung Kim 	}
531ef86d68aSNamhyung Kim 
532ef86d68aSNamhyung Kim out:
533ef86d68aSNamhyung Kim 	return printed;
534ef86d68aSNamhyung Kim }
535ef86d68aSNamhyung Kim 
536b10c78c5SJin Yao static int hist_entry__block_fprintf(struct hist_entry *he,
537b10c78c5SJin Yao 				     char *bf, size_t size,
538b10c78c5SJin Yao 				     FILE *fp)
539b10c78c5SJin Yao {
540b10c78c5SJin Yao 	struct block_hist *bh = container_of(he, struct block_hist, he);
541b10c78c5SJin Yao 	int ret = 0;
542b10c78c5SJin Yao 
543b10c78c5SJin Yao 	for (unsigned int i = 0; i < bh->block_hists.nr_entries; i++) {
544b10c78c5SJin Yao 		struct perf_hpp hpp = {
545b10c78c5SJin Yao 			.buf		= bf,
546b10c78c5SJin Yao 			.size		= size,
547b10c78c5SJin Yao 			.skip		= false,
548b10c78c5SJin Yao 		};
549b10c78c5SJin Yao 
550b10c78c5SJin Yao 		bh->block_idx = i;
551b10c78c5SJin Yao 		hist_entry__snprintf(he, &hpp);
552b10c78c5SJin Yao 
553b10c78c5SJin Yao 		if (!hpp.skip)
554b10c78c5SJin Yao 			ret += fprintf(fp, "%s\n", bf);
555b10c78c5SJin Yao 	}
556b10c78c5SJin Yao 
557b10c78c5SJin Yao 	return ret;
558b10c78c5SJin Yao }
559b10c78c5SJin Yao 
560000078bcSNamhyung Kim static int hist_entry__fprintf(struct hist_entry *he, size_t size,
561d05e3aaeSJiri Olsa 			       char *bf, size_t bfsz, FILE *fp,
562e9de7e2fSArnaldo Carvalho de Melo 			       bool ignore_callchains)
563000078bcSNamhyung Kim {
564000078bcSNamhyung Kim 	int ret;
5650db64dd0SJin Yao 	int callchain_ret = 0;
566ea251d51SNamhyung Kim 	struct perf_hpp hpp = {
567ea251d51SNamhyung Kim 		.buf		= bf,
568ea251d51SNamhyung Kim 		.size		= size,
569ea251d51SNamhyung Kim 	};
5708f1d1b44SJiri Olsa 	struct hists *hists = he->hists;
5717e597d32SNamhyung Kim 	u64 total_period = hists->stats.total_period;
572000078bcSNamhyung Kim 
57399cf666cSArnaldo Carvalho de Melo 	if (size == 0 || size > bfsz)
57499cf666cSArnaldo Carvalho de Melo 		size = hpp.size = bfsz;
575000078bcSNamhyung Kim 
5762dbbe9f2SNamhyung Kim 	if (symbol_conf.report_hierarchy)
5772dbbe9f2SNamhyung Kim 		return hist_entry__hierarchy_fprintf(he, &hpp, hists, fp);
578ef86d68aSNamhyung Kim 
579b10c78c5SJin Yao 	if (symbol_conf.report_block)
580b10c78c5SJin Yao 		return hist_entry__block_fprintf(he, bf, size, fp);
581b10c78c5SJin Yao 
58226d8b338SNamhyung Kim 	hist_entry__snprintf(he, &hpp);
583000078bcSNamhyung Kim 
584000078bcSNamhyung Kim 	ret = fprintf(fp, "%s\n", bf);
585000078bcSNamhyung Kim 
586e9de7e2fSArnaldo Carvalho de Melo 	if (hist_entry__has_callchains(he) && !ignore_callchains)
5870db64dd0SJin Yao 		callchain_ret = hist_entry_callchain__fprintf(he, total_period,
5880db64dd0SJin Yao 							      0, fp);
5890db64dd0SJin Yao 
5900db64dd0SJin Yao 	ret += callchain_ret;
591000078bcSNamhyung Kim 
592000078bcSNamhyung Kim 	return ret;
593000078bcSNamhyung Kim }
594000078bcSNamhyung Kim 
5952dbbe9f2SNamhyung Kim static int print_hierarchy_indent(const char *sep, int indent,
5968e2fc44fSNamhyung Kim 				  const char *line, FILE *fp)
5978e2fc44fSNamhyung Kim {
598b598c34fSArnaldo Carvalho de Melo 	int width;
599b598c34fSArnaldo Carvalho de Melo 
6002dbbe9f2SNamhyung Kim 	if (sep != NULL || indent < 2)
6018e2fc44fSNamhyung Kim 		return 0;
6028e2fc44fSNamhyung Kim 
603b598c34fSArnaldo Carvalho de Melo 	width = (indent - 2) * HIERARCHY_INDENT;
604b598c34fSArnaldo Carvalho de Melo 
605b598c34fSArnaldo Carvalho de Melo 	return fprintf(fp, "%-*.*s", width, width, line);
6068e2fc44fSNamhyung Kim }
6078e2fc44fSNamhyung Kim 
608195bc0f8SNamhyung Kim static int hists__fprintf_hierarchy_headers(struct hists *hists,
609195bc0f8SNamhyung Kim 					    struct perf_hpp *hpp, FILE *fp)
6108e2fc44fSNamhyung Kim {
611f58c95e3SNamhyung Kim 	bool first_node, first_col;
6122dbbe9f2SNamhyung Kim 	int indent;
613cb1fab91SNamhyung Kim 	int depth;
6148e2fc44fSNamhyung Kim 	unsigned width = 0;
6158e2fc44fSNamhyung Kim 	unsigned header_width = 0;
6168e2fc44fSNamhyung Kim 	struct perf_hpp_fmt *fmt;
617f58c95e3SNamhyung Kim 	struct perf_hpp_list_node *fmt_node;
618195bc0f8SNamhyung Kim 	const char *sep = symbol_conf.field_sep;
6198e2fc44fSNamhyung Kim 
6202dbbe9f2SNamhyung Kim 	indent = hists->nr_hpp_node;
6218e2fc44fSNamhyung Kim 
6228e2fc44fSNamhyung Kim 	/* preserve max indent depth for column headers */
623b598c34fSArnaldo Carvalho de Melo 	print_hierarchy_indent(sep, indent, " ", fp);
6248e2fc44fSNamhyung Kim 
625f58c95e3SNamhyung Kim 	/* the first hpp_list_node is for overhead columns */
626f58c95e3SNamhyung Kim 	fmt_node = list_first_entry(&hists->hpp_formats,
627f58c95e3SNamhyung Kim 				    struct perf_hpp_list_node, list);
6288e2fc44fSNamhyung Kim 
629f58c95e3SNamhyung Kim 	perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
63029659ab4SJiri Olsa 		fmt->header(fmt, hpp, hists, 0, NULL);
631f58c95e3SNamhyung Kim 		fprintf(fp, "%s%s", hpp->buf, sep ?: "  ");
6328e2fc44fSNamhyung Kim 	}
6338e2fc44fSNamhyung Kim 
6348e2fc44fSNamhyung Kim 	/* combine sort headers with ' / ' */
635f58c95e3SNamhyung Kim 	first_node = true;
636f58c95e3SNamhyung Kim 	list_for_each_entry_continue(fmt_node, &hists->hpp_formats, list) {
637f58c95e3SNamhyung Kim 		if (!first_node)
638f58c95e3SNamhyung Kim 			header_width += fprintf(fp, " / ");
639f58c95e3SNamhyung Kim 		first_node = false;
640f58c95e3SNamhyung Kim 
641f58c95e3SNamhyung Kim 		first_col = true;
642f58c95e3SNamhyung Kim 		perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
6438e2fc44fSNamhyung Kim 			if (perf_hpp__should_skip(fmt, hists))
6448e2fc44fSNamhyung Kim 				continue;
6458e2fc44fSNamhyung Kim 
646f58c95e3SNamhyung Kim 			if (!first_col)
647f58c95e3SNamhyung Kim 				header_width += fprintf(fp, "+");
648f58c95e3SNamhyung Kim 			first_col = false;
6498e2fc44fSNamhyung Kim 
65029659ab4SJiri Olsa 			fmt->header(fmt, hpp, hists, 0, NULL);
6518e2fc44fSNamhyung Kim 
6523ca43b60SArnaldo Carvalho de Melo 			header_width += fprintf(fp, "%s", strim(hpp->buf));
6538e2fc44fSNamhyung Kim 		}
654f58c95e3SNamhyung Kim 	}
6558e2fc44fSNamhyung Kim 
6568e2fc44fSNamhyung Kim 	fprintf(fp, "\n# ");
6578e2fc44fSNamhyung Kim 
6588e2fc44fSNamhyung Kim 	/* preserve max indent depth for initial dots */
6592dbbe9f2SNamhyung Kim 	print_hierarchy_indent(sep, indent, dots, fp);
6608e2fc44fSNamhyung Kim 
661f58c95e3SNamhyung Kim 	/* the first hpp_list_node is for overhead columns */
662f58c95e3SNamhyung Kim 	fmt_node = list_first_entry(&hists->hpp_formats,
663f58c95e3SNamhyung Kim 				    struct perf_hpp_list_node, list);
6648e2fc44fSNamhyung Kim 
665f58c95e3SNamhyung Kim 	first_col = true;
666f58c95e3SNamhyung Kim 	perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
667f58c95e3SNamhyung Kim 		if (!first_col)
668f58c95e3SNamhyung Kim 			fprintf(fp, "%s", sep ?: "..");
669f58c95e3SNamhyung Kim 		first_col = false;
6708e2fc44fSNamhyung Kim 
671da1b0407SJiri Olsa 		width = fmt->width(fmt, hpp, hists);
6728e2fc44fSNamhyung Kim 		fprintf(fp, "%.*s", width, dots);
6738e2fc44fSNamhyung Kim 	}
6748e2fc44fSNamhyung Kim 
675cb1fab91SNamhyung Kim 	depth = 0;
676f58c95e3SNamhyung Kim 	list_for_each_entry_continue(fmt_node, &hists->hpp_formats, list) {
677f58c95e3SNamhyung Kim 		first_col = true;
678f58c95e3SNamhyung Kim 		width = depth * HIERARCHY_INDENT;
679f58c95e3SNamhyung Kim 
680f58c95e3SNamhyung Kim 		perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
6818e2fc44fSNamhyung Kim 			if (perf_hpp__should_skip(fmt, hists))
6828e2fc44fSNamhyung Kim 				continue;
6838e2fc44fSNamhyung Kim 
684f58c95e3SNamhyung Kim 			if (!first_col)
685f58c95e3SNamhyung Kim 				width++;  /* for '+' sign between column header */
686f58c95e3SNamhyung Kim 			first_col = false;
687f58c95e3SNamhyung Kim 
688da1b0407SJiri Olsa 			width += fmt->width(fmt, hpp, hists);
689f58c95e3SNamhyung Kim 		}
690cb1fab91SNamhyung Kim 
6918e2fc44fSNamhyung Kim 		if (width > header_width)
6928e2fc44fSNamhyung Kim 			header_width = width;
693cb1fab91SNamhyung Kim 
694cb1fab91SNamhyung Kim 		depth++;
6958e2fc44fSNamhyung Kim 	}
6968e2fc44fSNamhyung Kim 
6978e2fc44fSNamhyung Kim 	fprintf(fp, "%s%-.*s", sep ?: "  ", header_width, dots);
6988e2fc44fSNamhyung Kim 
6998e2fc44fSNamhyung Kim 	fprintf(fp, "\n#\n");
7008e2fc44fSNamhyung Kim 
7018e2fc44fSNamhyung Kim 	return 2;
7028e2fc44fSNamhyung Kim }
7038e2fc44fSNamhyung Kim 
704f3705b06SJiri Olsa static void fprintf_line(struct hists *hists, struct perf_hpp *hpp,
705f3705b06SJiri Olsa 			 int line, FILE *fp)
7067ccf4f90SNamhyung Kim {
7071240005eSJiri Olsa 	struct perf_hpp_fmt *fmt;
7087ccf4f90SNamhyung Kim 	const char *sep = symbol_conf.field_sep;
7095395a048SJiri Olsa 	bool first = true;
71029659ab4SJiri Olsa 	int span = 0;
7117ccf4f90SNamhyung Kim 
712f0786af5SJiri Olsa 	hists__for_each_format(hists, fmt) {
713361459f1SNamhyung Kim 		if (perf_hpp__should_skip(fmt, hists))
714e67d49a7SNamhyung Kim 			continue;
715e67d49a7SNamhyung Kim 
71629659ab4SJiri Olsa 		if (!first && !span)
717ea251d51SNamhyung Kim 			fprintf(fp, "%s", sep ?: "  ");
7185395a048SJiri Olsa 		else
7195395a048SJiri Olsa 			first = false;
7207ccf4f90SNamhyung Kim 
72129659ab4SJiri Olsa 		fmt->header(fmt, hpp, hists, line, &span);
72229659ab4SJiri Olsa 
72329659ab4SJiri Olsa 		if (!span)
7247a72a2e5SJiri Olsa 			fprintf(fp, "%s", hpp->buf);
7257ccf4f90SNamhyung Kim 	}
726f3705b06SJiri Olsa }
7277ccf4f90SNamhyung Kim 
728f3705b06SJiri Olsa static int
729f3705b06SJiri Olsa hists__fprintf_standard_headers(struct hists *hists,
730f3705b06SJiri Olsa 				struct perf_hpp *hpp,
731f3705b06SJiri Olsa 				FILE *fp)
732f3705b06SJiri Olsa {
733f3705b06SJiri Olsa 	struct perf_hpp_list *hpp_list = hists->hpp_list;
734f3705b06SJiri Olsa 	struct perf_hpp_fmt *fmt;
735f3705b06SJiri Olsa 	unsigned int width;
736f3705b06SJiri Olsa 	const char *sep = symbol_conf.field_sep;
737f3705b06SJiri Olsa 	bool first = true;
738f3705b06SJiri Olsa 	int line;
739f3705b06SJiri Olsa 
740f3705b06SJiri Olsa 	for (line = 0; line < hpp_list->nr_header_lines; line++) {
741f3705b06SJiri Olsa 		/* first # is displayed one level up */
742f3705b06SJiri Olsa 		if (line)
743f3705b06SJiri Olsa 			fprintf(fp, "# ");
744f3705b06SJiri Olsa 		fprintf_line(hists, hpp, line, fp);
7457ccf4f90SNamhyung Kim 		fprintf(fp, "\n");
746f3705b06SJiri Olsa 	}
7477ccf4f90SNamhyung Kim 
7487ccf4f90SNamhyung Kim 	if (sep)
749f3705b06SJiri Olsa 		return hpp_list->nr_header_lines;
7507ccf4f90SNamhyung Kim 
7515395a048SJiri Olsa 	first = true;
7525395a048SJiri Olsa 
753ea251d51SNamhyung Kim 	fprintf(fp, "# ");
754ea251d51SNamhyung Kim 
755f0786af5SJiri Olsa 	hists__for_each_format(hists, fmt) {
7561240005eSJiri Olsa 		unsigned int i;
757ea251d51SNamhyung Kim 
758361459f1SNamhyung Kim 		if (perf_hpp__should_skip(fmt, hists))
759e67d49a7SNamhyung Kim 			continue;
760e67d49a7SNamhyung Kim 
7615395a048SJiri Olsa 		if (!first)
762ea251d51SNamhyung Kim 			fprintf(fp, "%s", sep ?: "  ");
7635395a048SJiri Olsa 		else
7645395a048SJiri Olsa 			first = false;
765ea251d51SNamhyung Kim 
766da1b0407SJiri Olsa 		width = fmt->width(fmt, hpp, hists);
767ea251d51SNamhyung Kim 		for (i = 0; i < width; i++)
768ea251d51SNamhyung Kim 			fprintf(fp, ".");
7697ccf4f90SNamhyung Kim 	}
770ea251d51SNamhyung Kim 
7717ccf4f90SNamhyung Kim 	fprintf(fp, "\n");
7727ccf4f90SNamhyung Kim 	fprintf(fp, "#\n");
773f3705b06SJiri Olsa 	return hpp_list->nr_header_lines + 2;
77436592ebbSJiri Olsa }
77536592ebbSJiri Olsa 
7762d831454SJiri Olsa int hists__fprintf_headers(struct hists *hists, FILE *fp)
7777a72a2e5SJiri Olsa {
778d5278220SJiri Olsa 	char bf[1024];
7797a72a2e5SJiri Olsa 	struct perf_hpp dummy_hpp = {
7807a72a2e5SJiri Olsa 		.buf	= bf,
7817a72a2e5SJiri Olsa 		.size	= sizeof(bf),
7827a72a2e5SJiri Olsa 	};
7837a72a2e5SJiri Olsa 
7847a72a2e5SJiri Olsa 	fprintf(fp, "# ");
7857a72a2e5SJiri Olsa 
7867a72a2e5SJiri Olsa 	if (symbol_conf.report_hierarchy)
7877a72a2e5SJiri Olsa 		return hists__fprintf_hierarchy_headers(hists, &dummy_hpp, fp);
7887a72a2e5SJiri Olsa 	else
7897a72a2e5SJiri Olsa 		return hists__fprintf_standard_headers(hists, &dummy_hpp, fp);
7907a72a2e5SJiri Olsa 
7917a72a2e5SJiri Olsa }
7927a72a2e5SJiri Olsa 
79336592ebbSJiri Olsa size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
794d05e3aaeSJiri Olsa 		      int max_cols, float min_pcnt, FILE *fp,
795e9de7e2fSArnaldo Carvalho de Melo 		      bool ignore_callchains)
79636592ebbSJiri Olsa {
79736592ebbSJiri Olsa 	struct rb_node *nd;
79836592ebbSJiri Olsa 	size_t ret = 0;
79936592ebbSJiri Olsa 	const char *sep = symbol_conf.field_sep;
80036592ebbSJiri Olsa 	int nr_rows = 0;
80136592ebbSJiri Olsa 	size_t linesz;
80236592ebbSJiri Olsa 	char *line = NULL;
80336592ebbSJiri Olsa 	unsigned indent;
80436592ebbSJiri Olsa 
80536592ebbSJiri Olsa 	init_rem_hits();
80636592ebbSJiri Olsa 
807e3b60bc9SNamhyung Kim 	hists__reset_column_width(hists);
80836592ebbSJiri Olsa 
80936592ebbSJiri Olsa 	if (symbol_conf.col_width_list_str)
81036592ebbSJiri Olsa 		perf_hpp__set_user_width(symbol_conf.col_width_list_str);
81136592ebbSJiri Olsa 
81236592ebbSJiri Olsa 	if (show_header)
81336592ebbSJiri Olsa 		nr_rows += hists__fprintf_headers(hists, fp);
81436592ebbSJiri Olsa 
81536592ebbSJiri Olsa 	if (max_rows && nr_rows >= max_rows)
8167ccf4f90SNamhyung Kim 		goto out;
8177ccf4f90SNamhyung Kim 
81899cf666cSArnaldo Carvalho de Melo 	linesz = hists__sort_list_width(hists) + 3 + 1;
8199754c4f9SJiri Olsa 	linesz += perf_hpp__color_overhead();
82099cf666cSArnaldo Carvalho de Melo 	line = malloc(linesz);
82199cf666cSArnaldo Carvalho de Melo 	if (line == NULL) {
82299cf666cSArnaldo Carvalho de Melo 		ret = -1;
82399cf666cSArnaldo Carvalho de Melo 		goto out;
82499cf666cSArnaldo Carvalho de Melo 	}
82599cf666cSArnaldo Carvalho de Melo 
826bd4abd39SNamhyung Kim 	indent = hists__overhead_width(hists) + 4;
827bd4abd39SNamhyung Kim 
8282eb3d689SDavidlohr Bueso 	for (nd = rb_first_cached(&hists->entries); nd;
8292eb3d689SDavidlohr Bueso 	     nd = __rb_hierarchy_next(nd, HMD_FORCE_CHILD)) {
8307ccf4f90SNamhyung Kim 		struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
83114135663SNamhyung Kim 		float percent;
8327ccf4f90SNamhyung Kim 
8337ccf4f90SNamhyung Kim 		if (h->filtered)
8347ccf4f90SNamhyung Kim 			continue;
8357ccf4f90SNamhyung Kim 
83614135663SNamhyung Kim 		percent = hist_entry__get_percent_limit(h);
837064f1981SNamhyung Kim 		if (percent < min_pcnt)
838064f1981SNamhyung Kim 			continue;
839064f1981SNamhyung Kim 
840e9de7e2fSArnaldo Carvalho de Melo 		ret += hist_entry__fprintf(h, max_cols, line, linesz, fp, ignore_callchains);
8417ccf4f90SNamhyung Kim 
8427ccf4f90SNamhyung Kim 		if (max_rows && ++nr_rows >= max_rows)
84399cf666cSArnaldo Carvalho de Melo 			break;
8447ccf4f90SNamhyung Kim 
845bd4abd39SNamhyung Kim 		/*
846bd4abd39SNamhyung Kim 		 * If all children are filtered out or percent-limited,
847bd4abd39SNamhyung Kim 		 * display "no entry >= x.xx%" message.
848bd4abd39SNamhyung Kim 		 */
849bd4abd39SNamhyung Kim 		if (!h->leaf && !hist_entry__has_hierarchy_children(h, min_pcnt)) {
850f58c95e3SNamhyung Kim 			int depth = hists->nr_hpp_node + h->depth + 1;
851bd4abd39SNamhyung Kim 
852b598c34fSArnaldo Carvalho de Melo 			print_hierarchy_indent(sep, depth, " ", fp);
853bd4abd39SNamhyung Kim 			fprintf(fp, "%*sno entry >= %.2f%%\n", indent, "", min_pcnt);
854bd4abd39SNamhyung Kim 
855bd4abd39SNamhyung Kim 			if (max_rows && ++nr_rows >= max_rows)
856bd4abd39SNamhyung Kim 				break;
857bd4abd39SNamhyung Kim 		}
858bd4abd39SNamhyung Kim 
8597ccf4f90SNamhyung Kim 		if (h->ms.map == NULL && verbose > 1) {
860b0867f0cSArnaldo Carvalho de Melo 			map_groups__fprintf(h->thread->mg, fp);
8617ccf4f90SNamhyung Kim 			fprintf(fp, "%.10s end\n", graph_dotted_line);
8627ccf4f90SNamhyung Kim 		}
8637ccf4f90SNamhyung Kim 	}
86499cf666cSArnaldo Carvalho de Melo 
86599cf666cSArnaldo Carvalho de Melo 	free(line);
8667ccf4f90SNamhyung Kim out:
86774cf249dSArnaldo Carvalho de Melo 	zfree(&rem_sq_bracket);
8687ccf4f90SNamhyung Kim 
8697ccf4f90SNamhyung Kim 	return ret;
8707ccf4f90SNamhyung Kim }
8717ccf4f90SNamhyung Kim 
87252168eeaSArnaldo Carvalho de Melo size_t events_stats__fprintf(struct events_stats *stats, FILE *fp)
8737ccf4f90SNamhyung Kim {
8747ccf4f90SNamhyung Kim 	int i;
8757ccf4f90SNamhyung Kim 	size_t ret = 0;
8767ccf4f90SNamhyung Kim 
8777ccf4f90SNamhyung Kim 	for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
8787ccf4f90SNamhyung Kim 		const char *name;
8797ccf4f90SNamhyung Kim 
8807ccf4f90SNamhyung Kim 		name = perf_event__name(i);
8817ccf4f90SNamhyung Kim 		if (!strcmp(name, "UNKNOWN"))
8827ccf4f90SNamhyung Kim 			continue;
8837ccf4f90SNamhyung Kim 
88439ce7fb3SIngo Molnar 		ret += fprintf(fp, "%16s events: %10d\n", name, stats->nr_events[i]);
8857ccf4f90SNamhyung Kim 	}
8867ccf4f90SNamhyung Kim 
8877ccf4f90SNamhyung Kim 	return ret;
8887ccf4f90SNamhyung Kim }
889