xref: /linux/tools/perf/ui/stdio/hist.c (revision 000078bc3ee69efb1124b8478c7527389a826074)
17ccf4f90SNamhyung Kim #include <stdio.h>
27ccf4f90SNamhyung Kim #include <math.h>
37ccf4f90SNamhyung Kim 
47ccf4f90SNamhyung Kim #include "../../util/util.h"
57ccf4f90SNamhyung Kim #include "../../util/hist.h"
67ccf4f90SNamhyung Kim #include "../../util/sort.h"
77ccf4f90SNamhyung Kim 
87ccf4f90SNamhyung Kim 
97ccf4f90SNamhyung Kim static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
107ccf4f90SNamhyung Kim {
117ccf4f90SNamhyung Kim 	int i;
127ccf4f90SNamhyung Kim 	int ret = fprintf(fp, "            ");
137ccf4f90SNamhyung Kim 
147ccf4f90SNamhyung Kim 	for (i = 0; i < left_margin; i++)
157ccf4f90SNamhyung Kim 		ret += fprintf(fp, " ");
167ccf4f90SNamhyung Kim 
177ccf4f90SNamhyung Kim 	return ret;
187ccf4f90SNamhyung Kim }
197ccf4f90SNamhyung Kim 
207ccf4f90SNamhyung Kim static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
217ccf4f90SNamhyung Kim 					  int left_margin)
227ccf4f90SNamhyung Kim {
237ccf4f90SNamhyung Kim 	int i;
247ccf4f90SNamhyung Kim 	size_t ret = callchain__fprintf_left_margin(fp, left_margin);
257ccf4f90SNamhyung Kim 
267ccf4f90SNamhyung Kim 	for (i = 0; i < depth; i++)
277ccf4f90SNamhyung Kim 		if (depth_mask & (1 << i))
287ccf4f90SNamhyung Kim 			ret += fprintf(fp, "|          ");
297ccf4f90SNamhyung Kim 		else
307ccf4f90SNamhyung Kim 			ret += fprintf(fp, "           ");
317ccf4f90SNamhyung Kim 
327ccf4f90SNamhyung Kim 	ret += fprintf(fp, "\n");
337ccf4f90SNamhyung Kim 
347ccf4f90SNamhyung Kim 	return ret;
357ccf4f90SNamhyung Kim }
367ccf4f90SNamhyung Kim 
377ccf4f90SNamhyung Kim static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
387ccf4f90SNamhyung Kim 				     int depth, int depth_mask, int period,
397ccf4f90SNamhyung Kim 				     u64 total_samples, u64 hits,
407ccf4f90SNamhyung Kim 				     int left_margin)
417ccf4f90SNamhyung Kim {
427ccf4f90SNamhyung Kim 	int i;
437ccf4f90SNamhyung Kim 	size_t ret = 0;
447ccf4f90SNamhyung Kim 
457ccf4f90SNamhyung Kim 	ret += callchain__fprintf_left_margin(fp, left_margin);
467ccf4f90SNamhyung Kim 	for (i = 0; i < depth; i++) {
477ccf4f90SNamhyung Kim 		if (depth_mask & (1 << i))
487ccf4f90SNamhyung Kim 			ret += fprintf(fp, "|");
497ccf4f90SNamhyung Kim 		else
507ccf4f90SNamhyung Kim 			ret += fprintf(fp, " ");
517ccf4f90SNamhyung Kim 		if (!period && i == depth - 1) {
527ccf4f90SNamhyung Kim 			double percent;
537ccf4f90SNamhyung Kim 
547ccf4f90SNamhyung Kim 			percent = hits * 100.0 / total_samples;
557ccf4f90SNamhyung Kim 			ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
567ccf4f90SNamhyung Kim 		} else
577ccf4f90SNamhyung Kim 			ret += fprintf(fp, "%s", "          ");
587ccf4f90SNamhyung Kim 	}
597ccf4f90SNamhyung Kim 	if (chain->ms.sym)
607ccf4f90SNamhyung Kim 		ret += fprintf(fp, "%s\n", chain->ms.sym->name);
617ccf4f90SNamhyung Kim 	else
627ccf4f90SNamhyung Kim 		ret += fprintf(fp, "0x%0" PRIx64 "\n", chain->ip);
637ccf4f90SNamhyung Kim 
647ccf4f90SNamhyung Kim 	return ret;
657ccf4f90SNamhyung Kim }
667ccf4f90SNamhyung Kim 
677ccf4f90SNamhyung Kim static struct symbol *rem_sq_bracket;
687ccf4f90SNamhyung Kim static struct callchain_list rem_hits;
697ccf4f90SNamhyung Kim 
707ccf4f90SNamhyung Kim static void init_rem_hits(void)
717ccf4f90SNamhyung Kim {
727ccf4f90SNamhyung Kim 	rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
737ccf4f90SNamhyung Kim 	if (!rem_sq_bracket) {
747ccf4f90SNamhyung Kim 		fprintf(stderr, "Not enough memory to display remaining hits\n");
757ccf4f90SNamhyung Kim 		return;
767ccf4f90SNamhyung Kim 	}
777ccf4f90SNamhyung Kim 
787ccf4f90SNamhyung Kim 	strcpy(rem_sq_bracket->name, "[...]");
797ccf4f90SNamhyung Kim 	rem_hits.ms.sym = rem_sq_bracket;
807ccf4f90SNamhyung Kim }
817ccf4f90SNamhyung Kim 
827ccf4f90SNamhyung Kim static size_t __callchain__fprintf_graph(FILE *fp, struct rb_root *root,
837ccf4f90SNamhyung Kim 					 u64 total_samples, int depth,
847ccf4f90SNamhyung Kim 					 int depth_mask, int left_margin)
857ccf4f90SNamhyung Kim {
867ccf4f90SNamhyung Kim 	struct rb_node *node, *next;
877ccf4f90SNamhyung Kim 	struct callchain_node *child;
887ccf4f90SNamhyung Kim 	struct callchain_list *chain;
897ccf4f90SNamhyung Kim 	int new_depth_mask = depth_mask;
907ccf4f90SNamhyung Kim 	u64 remaining;
917ccf4f90SNamhyung Kim 	size_t ret = 0;
927ccf4f90SNamhyung Kim 	int i;
937ccf4f90SNamhyung Kim 	uint entries_printed = 0;
947ccf4f90SNamhyung Kim 
957ccf4f90SNamhyung Kim 	remaining = total_samples;
967ccf4f90SNamhyung Kim 
977ccf4f90SNamhyung Kim 	node = rb_first(root);
987ccf4f90SNamhyung Kim 	while (node) {
997ccf4f90SNamhyung Kim 		u64 new_total;
1007ccf4f90SNamhyung Kim 		u64 cumul;
1017ccf4f90SNamhyung Kim 
1027ccf4f90SNamhyung Kim 		child = rb_entry(node, struct callchain_node, rb_node);
1037ccf4f90SNamhyung Kim 		cumul = callchain_cumul_hits(child);
1047ccf4f90SNamhyung Kim 		remaining -= cumul;
1057ccf4f90SNamhyung Kim 
1067ccf4f90SNamhyung Kim 		/*
1077ccf4f90SNamhyung Kim 		 * The depth mask manages the output of pipes that show
1087ccf4f90SNamhyung Kim 		 * the depth. We don't want to keep the pipes of the current
1097ccf4f90SNamhyung Kim 		 * level for the last child of this depth.
1107ccf4f90SNamhyung Kim 		 * Except if we have remaining filtered hits. They will
1117ccf4f90SNamhyung Kim 		 * supersede the last child
1127ccf4f90SNamhyung Kim 		 */
1137ccf4f90SNamhyung Kim 		next = rb_next(node);
1147ccf4f90SNamhyung Kim 		if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
1157ccf4f90SNamhyung Kim 			new_depth_mask &= ~(1 << (depth - 1));
1167ccf4f90SNamhyung Kim 
1177ccf4f90SNamhyung Kim 		/*
1187ccf4f90SNamhyung Kim 		 * But we keep the older depth mask for the line separator
1197ccf4f90SNamhyung Kim 		 * to keep the level link until we reach the last child
1207ccf4f90SNamhyung Kim 		 */
1217ccf4f90SNamhyung Kim 		ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
1227ccf4f90SNamhyung Kim 						   left_margin);
1237ccf4f90SNamhyung Kim 		i = 0;
1247ccf4f90SNamhyung Kim 		list_for_each_entry(chain, &child->val, list) {
1257ccf4f90SNamhyung Kim 			ret += ipchain__fprintf_graph(fp, chain, depth,
1267ccf4f90SNamhyung Kim 						      new_depth_mask, i++,
1277ccf4f90SNamhyung Kim 						      total_samples,
1287ccf4f90SNamhyung Kim 						      cumul,
1297ccf4f90SNamhyung Kim 						      left_margin);
1307ccf4f90SNamhyung Kim 		}
1317ccf4f90SNamhyung Kim 
1327ccf4f90SNamhyung Kim 		if (callchain_param.mode == CHAIN_GRAPH_REL)
1337ccf4f90SNamhyung Kim 			new_total = child->children_hit;
1347ccf4f90SNamhyung Kim 		else
1357ccf4f90SNamhyung Kim 			new_total = total_samples;
1367ccf4f90SNamhyung Kim 
1377ccf4f90SNamhyung Kim 		ret += __callchain__fprintf_graph(fp, &child->rb_root, new_total,
1387ccf4f90SNamhyung Kim 						  depth + 1,
1397ccf4f90SNamhyung Kim 						  new_depth_mask | (1 << depth),
1407ccf4f90SNamhyung Kim 						  left_margin);
1417ccf4f90SNamhyung Kim 		node = next;
1427ccf4f90SNamhyung Kim 		if (++entries_printed == callchain_param.print_limit)
1437ccf4f90SNamhyung Kim 			break;
1447ccf4f90SNamhyung Kim 	}
1457ccf4f90SNamhyung Kim 
1467ccf4f90SNamhyung Kim 	if (callchain_param.mode == CHAIN_GRAPH_REL &&
1477ccf4f90SNamhyung Kim 		remaining && remaining != total_samples) {
1487ccf4f90SNamhyung Kim 
1497ccf4f90SNamhyung Kim 		if (!rem_sq_bracket)
1507ccf4f90SNamhyung Kim 			return ret;
1517ccf4f90SNamhyung Kim 
1527ccf4f90SNamhyung Kim 		new_depth_mask &= ~(1 << (depth - 1));
1537ccf4f90SNamhyung Kim 		ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
1547ccf4f90SNamhyung Kim 					      new_depth_mask, 0, total_samples,
1557ccf4f90SNamhyung Kim 					      remaining, left_margin);
1567ccf4f90SNamhyung Kim 	}
1577ccf4f90SNamhyung Kim 
1587ccf4f90SNamhyung Kim 	return ret;
1597ccf4f90SNamhyung Kim }
1607ccf4f90SNamhyung Kim 
1617ccf4f90SNamhyung Kim static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root,
1627ccf4f90SNamhyung Kim 				       u64 total_samples, int left_margin)
1637ccf4f90SNamhyung Kim {
1647ccf4f90SNamhyung Kim 	struct callchain_node *cnode;
1657ccf4f90SNamhyung Kim 	struct callchain_list *chain;
1667ccf4f90SNamhyung Kim 	u32 entries_printed = 0;
1677ccf4f90SNamhyung Kim 	bool printed = false;
1687ccf4f90SNamhyung Kim 	struct rb_node *node;
1697ccf4f90SNamhyung Kim 	int i = 0;
1707ccf4f90SNamhyung Kim 	int ret = 0;
1717ccf4f90SNamhyung Kim 
1727ccf4f90SNamhyung Kim 	/*
1737ccf4f90SNamhyung Kim 	 * If have one single callchain root, don't bother printing
1747ccf4f90SNamhyung Kim 	 * its percentage (100 % in fractal mode and the same percentage
1757ccf4f90SNamhyung Kim 	 * than the hist in graph mode). This also avoid one level of column.
1767ccf4f90SNamhyung Kim 	 */
1777ccf4f90SNamhyung Kim 	node = rb_first(root);
1787ccf4f90SNamhyung Kim 	if (node && !rb_next(node)) {
1797ccf4f90SNamhyung Kim 		cnode = rb_entry(node, struct callchain_node, rb_node);
1807ccf4f90SNamhyung Kim 		list_for_each_entry(chain, &cnode->val, list) {
1817ccf4f90SNamhyung Kim 			/*
1827ccf4f90SNamhyung Kim 			 * If we sort by symbol, the first entry is the same than
1837ccf4f90SNamhyung Kim 			 * the symbol. No need to print it otherwise it appears as
1847ccf4f90SNamhyung Kim 			 * displayed twice.
1857ccf4f90SNamhyung Kim 			 */
1867ccf4f90SNamhyung Kim 			if (!i++ && sort__first_dimension == SORT_SYM)
1877ccf4f90SNamhyung Kim 				continue;
1887ccf4f90SNamhyung Kim 			if (!printed) {
1897ccf4f90SNamhyung Kim 				ret += callchain__fprintf_left_margin(fp, left_margin);
1907ccf4f90SNamhyung Kim 				ret += fprintf(fp, "|\n");
1917ccf4f90SNamhyung Kim 				ret += callchain__fprintf_left_margin(fp, left_margin);
1927ccf4f90SNamhyung Kim 				ret += fprintf(fp, "---");
1937ccf4f90SNamhyung Kim 				left_margin += 3;
1947ccf4f90SNamhyung Kim 				printed = true;
1957ccf4f90SNamhyung Kim 			} else
1967ccf4f90SNamhyung Kim 				ret += callchain__fprintf_left_margin(fp, left_margin);
1977ccf4f90SNamhyung Kim 
1987ccf4f90SNamhyung Kim 			if (chain->ms.sym)
1997ccf4f90SNamhyung Kim 				ret += fprintf(fp, " %s\n", chain->ms.sym->name);
2007ccf4f90SNamhyung Kim 			else
2017ccf4f90SNamhyung Kim 				ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
2027ccf4f90SNamhyung Kim 
2037ccf4f90SNamhyung Kim 			if (++entries_printed == callchain_param.print_limit)
2047ccf4f90SNamhyung Kim 				break;
2057ccf4f90SNamhyung Kim 		}
2067ccf4f90SNamhyung Kim 		root = &cnode->rb_root;
2077ccf4f90SNamhyung Kim 	}
2087ccf4f90SNamhyung Kim 
2097ccf4f90SNamhyung Kim 	ret += __callchain__fprintf_graph(fp, root, total_samples,
2107ccf4f90SNamhyung Kim 					  1, 1, left_margin);
2117ccf4f90SNamhyung Kim 	ret += fprintf(fp, "\n");
2127ccf4f90SNamhyung Kim 
2137ccf4f90SNamhyung Kim 	return ret;
2147ccf4f90SNamhyung Kim }
2157ccf4f90SNamhyung Kim 
2167ccf4f90SNamhyung Kim static size_t __callchain__fprintf_flat(FILE *fp,
2177ccf4f90SNamhyung Kim 					struct callchain_node *self,
2187ccf4f90SNamhyung Kim 					u64 total_samples)
2197ccf4f90SNamhyung Kim {
2207ccf4f90SNamhyung Kim 	struct callchain_list *chain;
2217ccf4f90SNamhyung Kim 	size_t ret = 0;
2227ccf4f90SNamhyung Kim 
2237ccf4f90SNamhyung Kim 	if (!self)
2247ccf4f90SNamhyung Kim 		return 0;
2257ccf4f90SNamhyung Kim 
2267ccf4f90SNamhyung Kim 	ret += __callchain__fprintf_flat(fp, self->parent, total_samples);
2277ccf4f90SNamhyung Kim 
2287ccf4f90SNamhyung Kim 
2297ccf4f90SNamhyung Kim 	list_for_each_entry(chain, &self->val, list) {
2307ccf4f90SNamhyung Kim 		if (chain->ip >= PERF_CONTEXT_MAX)
2317ccf4f90SNamhyung Kim 			continue;
2327ccf4f90SNamhyung Kim 		if (chain->ms.sym)
2337ccf4f90SNamhyung Kim 			ret += fprintf(fp, "                %s\n", chain->ms.sym->name);
2347ccf4f90SNamhyung Kim 		else
2357ccf4f90SNamhyung Kim 			ret += fprintf(fp, "                %p\n",
2367ccf4f90SNamhyung Kim 					(void *)(long)chain->ip);
2377ccf4f90SNamhyung Kim 	}
2387ccf4f90SNamhyung Kim 
2397ccf4f90SNamhyung Kim 	return ret;
2407ccf4f90SNamhyung Kim }
2417ccf4f90SNamhyung Kim 
2427ccf4f90SNamhyung Kim static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *self,
2437ccf4f90SNamhyung Kim 				      u64 total_samples)
2447ccf4f90SNamhyung Kim {
2457ccf4f90SNamhyung Kim 	size_t ret = 0;
2467ccf4f90SNamhyung Kim 	u32 entries_printed = 0;
2477ccf4f90SNamhyung Kim 	struct rb_node *rb_node;
2487ccf4f90SNamhyung Kim 	struct callchain_node *chain;
2497ccf4f90SNamhyung Kim 
2507ccf4f90SNamhyung Kim 	rb_node = rb_first(self);
2517ccf4f90SNamhyung Kim 	while (rb_node) {
2527ccf4f90SNamhyung Kim 		double percent;
2537ccf4f90SNamhyung Kim 
2547ccf4f90SNamhyung Kim 		chain = rb_entry(rb_node, struct callchain_node, rb_node);
2557ccf4f90SNamhyung Kim 		percent = chain->hit * 100.0 / total_samples;
2567ccf4f90SNamhyung Kim 
2577ccf4f90SNamhyung Kim 		ret = percent_color_fprintf(fp, "           %6.2f%%\n", percent);
2587ccf4f90SNamhyung Kim 		ret += __callchain__fprintf_flat(fp, chain, total_samples);
2597ccf4f90SNamhyung Kim 		ret += fprintf(fp, "\n");
2607ccf4f90SNamhyung Kim 		if (++entries_printed == callchain_param.print_limit)
2617ccf4f90SNamhyung Kim 			break;
2627ccf4f90SNamhyung Kim 
2637ccf4f90SNamhyung Kim 		rb_node = rb_next(rb_node);
2647ccf4f90SNamhyung Kim 	}
2657ccf4f90SNamhyung Kim 
2667ccf4f90SNamhyung Kim 	return ret;
2677ccf4f90SNamhyung Kim }
2687ccf4f90SNamhyung Kim 
2697ccf4f90SNamhyung Kim static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
2707ccf4f90SNamhyung Kim 					    u64 total_samples, int left_margin,
2717ccf4f90SNamhyung Kim 					    FILE *fp)
2727ccf4f90SNamhyung Kim {
2737ccf4f90SNamhyung Kim 	switch (callchain_param.mode) {
2747ccf4f90SNamhyung Kim 	case CHAIN_GRAPH_REL:
2757ccf4f90SNamhyung Kim 		return callchain__fprintf_graph(fp, &he->sorted_chain, he->period,
2767ccf4f90SNamhyung Kim 						left_margin);
2777ccf4f90SNamhyung Kim 		break;
2787ccf4f90SNamhyung Kim 	case CHAIN_GRAPH_ABS:
2797ccf4f90SNamhyung Kim 		return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
2807ccf4f90SNamhyung Kim 						left_margin);
2817ccf4f90SNamhyung Kim 		break;
2827ccf4f90SNamhyung Kim 	case CHAIN_FLAT:
2837ccf4f90SNamhyung Kim 		return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples);
2847ccf4f90SNamhyung Kim 		break;
2857ccf4f90SNamhyung Kim 	case CHAIN_NONE:
2867ccf4f90SNamhyung Kim 		break;
2877ccf4f90SNamhyung Kim 	default:
2887ccf4f90SNamhyung Kim 		pr_err("Bad callchain mode\n");
2897ccf4f90SNamhyung Kim 	}
2907ccf4f90SNamhyung Kim 
2917ccf4f90SNamhyung Kim 	return 0;
2927ccf4f90SNamhyung Kim }
2937ccf4f90SNamhyung Kim 
294*000078bcSNamhyung Kim static int hist_entry__period_snprintf(struct hist_entry *he, char *s,
2957ccf4f90SNamhyung Kim 				     size_t size, struct hists *pair_hists,
2967ccf4f90SNamhyung Kim 				     bool show_displacement, long displacement,
2977ccf4f90SNamhyung Kim 				     bool color, u64 total_period)
2987ccf4f90SNamhyung Kim {
2997ccf4f90SNamhyung Kim 	u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
3007ccf4f90SNamhyung Kim 	u64 nr_events;
3017ccf4f90SNamhyung Kim 	const char *sep = symbol_conf.field_sep;
3027ccf4f90SNamhyung Kim 	int ret;
3037ccf4f90SNamhyung Kim 
3047ccf4f90SNamhyung Kim 	if (symbol_conf.exclude_other && !he->parent)
3057ccf4f90SNamhyung Kim 		return 0;
3067ccf4f90SNamhyung Kim 
3077ccf4f90SNamhyung Kim 	if (pair_hists) {
3087ccf4f90SNamhyung Kim 		period = he->pair ? he->pair->period : 0;
3097ccf4f90SNamhyung Kim 		nr_events = he->pair ? he->pair->nr_events : 0;
3107ccf4f90SNamhyung Kim 		total = pair_hists->stats.total_period;
3117ccf4f90SNamhyung Kim 		period_sys = he->pair ? he->pair->period_sys : 0;
3127ccf4f90SNamhyung Kim 		period_us = he->pair ? he->pair->period_us : 0;
3137ccf4f90SNamhyung Kim 		period_guest_sys = he->pair ? he->pair->period_guest_sys : 0;
3147ccf4f90SNamhyung Kim 		period_guest_us = he->pair ? he->pair->period_guest_us : 0;
3157ccf4f90SNamhyung Kim 	} else {
3167ccf4f90SNamhyung Kim 		period = he->period;
3177ccf4f90SNamhyung Kim 		nr_events = he->nr_events;
3187ccf4f90SNamhyung Kim 		total = total_period;
3197ccf4f90SNamhyung Kim 		period_sys = he->period_sys;
3207ccf4f90SNamhyung Kim 		period_us = he->period_us;
3217ccf4f90SNamhyung Kim 		period_guest_sys = he->period_guest_sys;
3227ccf4f90SNamhyung Kim 		period_guest_us = he->period_guest_us;
3237ccf4f90SNamhyung Kim 	}
3247ccf4f90SNamhyung Kim 
3257ccf4f90SNamhyung Kim 	if (total) {
3267ccf4f90SNamhyung Kim 		if (color)
3277ccf4f90SNamhyung Kim 			ret = percent_color_snprintf(s, size,
3287ccf4f90SNamhyung Kim 						     sep ? "%.2f" : "   %6.2f%%",
3297ccf4f90SNamhyung Kim 						     (period * 100.0) / total);
3307ccf4f90SNamhyung Kim 		else
3317ccf4f90SNamhyung Kim 			ret = scnprintf(s, size, sep ? "%.2f" : "   %6.2f%%",
3327ccf4f90SNamhyung Kim 				       (period * 100.0) / total);
3337ccf4f90SNamhyung Kim 		if (symbol_conf.show_cpu_utilization) {
3347ccf4f90SNamhyung Kim 			ret += percent_color_snprintf(s + ret, size - ret,
3357ccf4f90SNamhyung Kim 					sep ? "%.2f" : "   %6.2f%%",
3367ccf4f90SNamhyung Kim 					(period_sys * 100.0) / total);
3377ccf4f90SNamhyung Kim 			ret += percent_color_snprintf(s + ret, size - ret,
3387ccf4f90SNamhyung Kim 					sep ? "%.2f" : "   %6.2f%%",
3397ccf4f90SNamhyung Kim 					(period_us * 100.0) / total);
3407ccf4f90SNamhyung Kim 			if (perf_guest) {
3417ccf4f90SNamhyung Kim 				ret += percent_color_snprintf(s + ret,
3427ccf4f90SNamhyung Kim 						size - ret,
3437ccf4f90SNamhyung Kim 						sep ? "%.2f" : "   %6.2f%%",
3447ccf4f90SNamhyung Kim 						(period_guest_sys * 100.0) /
3457ccf4f90SNamhyung Kim 								total);
3467ccf4f90SNamhyung Kim 				ret += percent_color_snprintf(s + ret,
3477ccf4f90SNamhyung Kim 						size - ret,
3487ccf4f90SNamhyung Kim 						sep ? "%.2f" : "   %6.2f%%",
3497ccf4f90SNamhyung Kim 						(period_guest_us * 100.0) /
3507ccf4f90SNamhyung Kim 								total);
3517ccf4f90SNamhyung Kim 			}
3527ccf4f90SNamhyung Kim 		}
3537ccf4f90SNamhyung Kim 	} else
3547ccf4f90SNamhyung Kim 		ret = scnprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
3557ccf4f90SNamhyung Kim 
3567ccf4f90SNamhyung Kim 	if (symbol_conf.show_nr_samples) {
3577ccf4f90SNamhyung Kim 		if (sep)
3587ccf4f90SNamhyung Kim 			ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
3597ccf4f90SNamhyung Kim 		else
3607ccf4f90SNamhyung Kim 			ret += scnprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
3617ccf4f90SNamhyung Kim 	}
3627ccf4f90SNamhyung Kim 
3637ccf4f90SNamhyung Kim 	if (symbol_conf.show_total_period) {
3647ccf4f90SNamhyung Kim 		if (sep)
3657ccf4f90SNamhyung Kim 			ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
3667ccf4f90SNamhyung Kim 		else
3677ccf4f90SNamhyung Kim 			ret += scnprintf(s + ret, size - ret, " %12" PRIu64, period);
3687ccf4f90SNamhyung Kim 	}
3697ccf4f90SNamhyung Kim 
3707ccf4f90SNamhyung Kim 	if (pair_hists) {
3717ccf4f90SNamhyung Kim 		char bf[32];
3727ccf4f90SNamhyung Kim 		double old_percent = 0, new_percent = 0, diff;
3737ccf4f90SNamhyung Kim 
3747ccf4f90SNamhyung Kim 		if (total > 0)
3757ccf4f90SNamhyung Kim 			old_percent = (period * 100.0) / total;
3767ccf4f90SNamhyung Kim 		if (total_period > 0)
3777ccf4f90SNamhyung Kim 			new_percent = (he->period * 100.0) / total_period;
3787ccf4f90SNamhyung Kim 
3797ccf4f90SNamhyung Kim 		diff = new_percent - old_percent;
3807ccf4f90SNamhyung Kim 
3817ccf4f90SNamhyung Kim 		if (fabs(diff) >= 0.01)
3827ccf4f90SNamhyung Kim 			scnprintf(bf, sizeof(bf), "%+4.2F%%", diff);
3837ccf4f90SNamhyung Kim 		else
3847ccf4f90SNamhyung Kim 			scnprintf(bf, sizeof(bf), " ");
3857ccf4f90SNamhyung Kim 
3867ccf4f90SNamhyung Kim 		if (sep)
3877ccf4f90SNamhyung Kim 			ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
3887ccf4f90SNamhyung Kim 		else
3897ccf4f90SNamhyung Kim 			ret += scnprintf(s + ret, size - ret, "%11.11s", bf);
3907ccf4f90SNamhyung Kim 
3917ccf4f90SNamhyung Kim 		if (show_displacement) {
3927ccf4f90SNamhyung Kim 			if (displacement)
3937ccf4f90SNamhyung Kim 				scnprintf(bf, sizeof(bf), "%+4ld", displacement);
3947ccf4f90SNamhyung Kim 			else
3957ccf4f90SNamhyung Kim 				scnprintf(bf, sizeof(bf), " ");
3967ccf4f90SNamhyung Kim 
3977ccf4f90SNamhyung Kim 			if (sep)
3987ccf4f90SNamhyung Kim 				ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
3997ccf4f90SNamhyung Kim 			else
4007ccf4f90SNamhyung Kim 				ret += scnprintf(s + ret, size - ret, "%6.6s", bf);
4017ccf4f90SNamhyung Kim 		}
4027ccf4f90SNamhyung Kim 	}
4037ccf4f90SNamhyung Kim 
4047ccf4f90SNamhyung Kim 	return ret;
4057ccf4f90SNamhyung Kim }
4067ccf4f90SNamhyung Kim 
407*000078bcSNamhyung Kim int hist_entry__sort_snprintf(struct hist_entry *he, char *s, size_t size,
4087ccf4f90SNamhyung Kim 			      struct hists *hists)
4097ccf4f90SNamhyung Kim {
4107ccf4f90SNamhyung Kim 	const char *sep = symbol_conf.field_sep;
4117ccf4f90SNamhyung Kim 	struct sort_entry *se;
4127ccf4f90SNamhyung Kim 	int ret = 0;
4137ccf4f90SNamhyung Kim 
4147ccf4f90SNamhyung Kim 	list_for_each_entry(se, &hist_entry__sort_list, list) {
4157ccf4f90SNamhyung Kim 		if (se->elide)
4167ccf4f90SNamhyung Kim 			continue;
4177ccf4f90SNamhyung Kim 
4187ccf4f90SNamhyung Kim 		ret += scnprintf(s + ret, size - ret, "%s", sep ?: "  ");
4197ccf4f90SNamhyung Kim 		ret += se->se_snprintf(he, s + ret, size - ret,
4207ccf4f90SNamhyung Kim 				       hists__col_len(hists, se->se_width_idx));
4217ccf4f90SNamhyung Kim 	}
4227ccf4f90SNamhyung Kim 
4237ccf4f90SNamhyung Kim 	return ret;
4247ccf4f90SNamhyung Kim }
4257ccf4f90SNamhyung Kim 
426*000078bcSNamhyung Kim static size_t hist_entry__callchain_fprintf(struct hist_entry *he,
4277ccf4f90SNamhyung Kim 					    struct hists *hists,
4287ccf4f90SNamhyung Kim 					    u64 total_period, FILE *fp)
4297ccf4f90SNamhyung Kim {
4307ccf4f90SNamhyung Kim 	int left_margin = 0;
4317ccf4f90SNamhyung Kim 
4327ccf4f90SNamhyung Kim 	if (sort__first_dimension == SORT_COMM) {
4337ccf4f90SNamhyung Kim 		struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
4347ccf4f90SNamhyung Kim 							 typeof(*se), list);
4357ccf4f90SNamhyung Kim 		left_margin = hists__col_len(hists, se->se_width_idx);
4367ccf4f90SNamhyung Kim 		left_margin -= thread__comm_len(he->thread);
4377ccf4f90SNamhyung Kim 	}
4387ccf4f90SNamhyung Kim 
4397ccf4f90SNamhyung Kim 	return hist_entry_callchain__fprintf(he, total_period, left_margin, fp);
4407ccf4f90SNamhyung Kim }
4417ccf4f90SNamhyung Kim 
442*000078bcSNamhyung Kim static int hist_entry__fprintf(struct hist_entry *he, size_t size,
443*000078bcSNamhyung Kim 			       struct hists *hists, struct hists *pair_hists,
444*000078bcSNamhyung Kim 			       bool show_displacement, long displacement,
445*000078bcSNamhyung Kim 			       u64 total_period, FILE *fp)
446*000078bcSNamhyung Kim {
447*000078bcSNamhyung Kim 	char bf[512];
448*000078bcSNamhyung Kim 	int ret;
449*000078bcSNamhyung Kim 
450*000078bcSNamhyung Kim 	if (size == 0 || size > sizeof(bf))
451*000078bcSNamhyung Kim 		size = sizeof(bf);
452*000078bcSNamhyung Kim 
453*000078bcSNamhyung Kim 	ret = hist_entry__period_snprintf(he, bf, size, pair_hists,
454*000078bcSNamhyung Kim 					  show_displacement, displacement,
455*000078bcSNamhyung Kim 					  true, total_period);
456*000078bcSNamhyung Kim 	hist_entry__sort_snprintf(he, bf + ret, size - ret, hists);
457*000078bcSNamhyung Kim 
458*000078bcSNamhyung Kim 	ret = fprintf(fp, "%s\n", bf);
459*000078bcSNamhyung Kim 
460*000078bcSNamhyung Kim 	if (symbol_conf.use_callchain)
461*000078bcSNamhyung Kim 		ret += hist_entry__callchain_fprintf(he, hists,
462*000078bcSNamhyung Kim 						     total_period, fp);
463*000078bcSNamhyung Kim 
464*000078bcSNamhyung Kim 	return ret;
465*000078bcSNamhyung Kim }
466*000078bcSNamhyung Kim 
4677ccf4f90SNamhyung Kim size_t hists__fprintf(struct hists *hists, struct hists *pair,
4687ccf4f90SNamhyung Kim 		      bool show_displacement, bool show_header, int max_rows,
4697ccf4f90SNamhyung Kim 		      int max_cols, FILE *fp)
4707ccf4f90SNamhyung Kim {
4717ccf4f90SNamhyung Kim 	struct sort_entry *se;
4727ccf4f90SNamhyung Kim 	struct rb_node *nd;
4737ccf4f90SNamhyung Kim 	size_t ret = 0;
4747ccf4f90SNamhyung Kim 	u64 total_period;
4757ccf4f90SNamhyung Kim 	unsigned long position = 1;
4767ccf4f90SNamhyung Kim 	long displacement = 0;
4777ccf4f90SNamhyung Kim 	unsigned int width;
4787ccf4f90SNamhyung Kim 	const char *sep = symbol_conf.field_sep;
4797ccf4f90SNamhyung Kim 	const char *col_width = symbol_conf.col_width_list_str;
4807ccf4f90SNamhyung Kim 	int nr_rows = 0;
4817ccf4f90SNamhyung Kim 
4827ccf4f90SNamhyung Kim 	init_rem_hits();
4837ccf4f90SNamhyung Kim 
4847ccf4f90SNamhyung Kim 	if (!show_header)
4857ccf4f90SNamhyung Kim 		goto print_entries;
4867ccf4f90SNamhyung Kim 
4877ccf4f90SNamhyung Kim 	fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
4887ccf4f90SNamhyung Kim 
4897ccf4f90SNamhyung Kim 	if (symbol_conf.show_cpu_utilization) {
4907ccf4f90SNamhyung Kim 		if (sep) {
4917ccf4f90SNamhyung Kim 			ret += fprintf(fp, "%csys", *sep);
4927ccf4f90SNamhyung Kim 			ret += fprintf(fp, "%cus", *sep);
4937ccf4f90SNamhyung Kim 			if (perf_guest) {
4947ccf4f90SNamhyung Kim 				ret += fprintf(fp, "%cguest sys", *sep);
4957ccf4f90SNamhyung Kim 				ret += fprintf(fp, "%cguest us", *sep);
4967ccf4f90SNamhyung Kim 			}
4977ccf4f90SNamhyung Kim 		} else {
4987ccf4f90SNamhyung Kim 			ret += fprintf(fp, "     sys  ");
4997ccf4f90SNamhyung Kim 			ret += fprintf(fp, "      us  ");
5007ccf4f90SNamhyung Kim 			if (perf_guest) {
5017ccf4f90SNamhyung Kim 				ret += fprintf(fp, "  guest sys  ");
5027ccf4f90SNamhyung Kim 				ret += fprintf(fp, "  guest us  ");
5037ccf4f90SNamhyung Kim 			}
5047ccf4f90SNamhyung Kim 		}
5057ccf4f90SNamhyung Kim 	}
5067ccf4f90SNamhyung Kim 
5077ccf4f90SNamhyung Kim 	if (symbol_conf.show_nr_samples) {
5087ccf4f90SNamhyung Kim 		if (sep)
5097ccf4f90SNamhyung Kim 			fprintf(fp, "%cSamples", *sep);
5107ccf4f90SNamhyung Kim 		else
5117ccf4f90SNamhyung Kim 			fputs("  Samples  ", fp);
5127ccf4f90SNamhyung Kim 	}
5137ccf4f90SNamhyung Kim 
5147ccf4f90SNamhyung Kim 	if (symbol_conf.show_total_period) {
5157ccf4f90SNamhyung Kim 		if (sep)
5167ccf4f90SNamhyung Kim 			ret += fprintf(fp, "%cPeriod", *sep);
5177ccf4f90SNamhyung Kim 		else
5187ccf4f90SNamhyung Kim 			ret += fprintf(fp, "   Period    ");
5197ccf4f90SNamhyung Kim 	}
5207ccf4f90SNamhyung Kim 
5217ccf4f90SNamhyung Kim 	if (pair) {
5227ccf4f90SNamhyung Kim 		if (sep)
5237ccf4f90SNamhyung Kim 			ret += fprintf(fp, "%cDelta", *sep);
5247ccf4f90SNamhyung Kim 		else
5257ccf4f90SNamhyung Kim 			ret += fprintf(fp, "  Delta    ");
5267ccf4f90SNamhyung Kim 
5277ccf4f90SNamhyung Kim 		if (show_displacement) {
5287ccf4f90SNamhyung Kim 			if (sep)
5297ccf4f90SNamhyung Kim 				ret += fprintf(fp, "%cDisplacement", *sep);
5307ccf4f90SNamhyung Kim 			else
5317ccf4f90SNamhyung Kim 				ret += fprintf(fp, " Displ");
5327ccf4f90SNamhyung Kim 		}
5337ccf4f90SNamhyung Kim 	}
5347ccf4f90SNamhyung Kim 
5357ccf4f90SNamhyung Kim 	list_for_each_entry(se, &hist_entry__sort_list, list) {
5367ccf4f90SNamhyung Kim 		if (se->elide)
5377ccf4f90SNamhyung Kim 			continue;
5387ccf4f90SNamhyung Kim 		if (sep) {
5397ccf4f90SNamhyung Kim 			fprintf(fp, "%c%s", *sep, se->se_header);
5407ccf4f90SNamhyung Kim 			continue;
5417ccf4f90SNamhyung Kim 		}
5427ccf4f90SNamhyung Kim 		width = strlen(se->se_header);
5437ccf4f90SNamhyung Kim 		if (symbol_conf.col_width_list_str) {
5447ccf4f90SNamhyung Kim 			if (col_width) {
5457ccf4f90SNamhyung Kim 				hists__set_col_len(hists, se->se_width_idx,
5467ccf4f90SNamhyung Kim 						   atoi(col_width));
5477ccf4f90SNamhyung Kim 				col_width = strchr(col_width, ',');
5487ccf4f90SNamhyung Kim 				if (col_width)
5497ccf4f90SNamhyung Kim 					++col_width;
5507ccf4f90SNamhyung Kim 			}
5517ccf4f90SNamhyung Kim 		}
5527ccf4f90SNamhyung Kim 		if (!hists__new_col_len(hists, se->se_width_idx, width))
5537ccf4f90SNamhyung Kim 			width = hists__col_len(hists, se->se_width_idx);
5547ccf4f90SNamhyung Kim 		fprintf(fp, "  %*s", width, se->se_header);
5557ccf4f90SNamhyung Kim 	}
5567ccf4f90SNamhyung Kim 
5577ccf4f90SNamhyung Kim 	fprintf(fp, "\n");
5587ccf4f90SNamhyung Kim 	if (max_rows && ++nr_rows >= max_rows)
5597ccf4f90SNamhyung Kim 		goto out;
5607ccf4f90SNamhyung Kim 
5617ccf4f90SNamhyung Kim 	if (sep)
5627ccf4f90SNamhyung Kim 		goto print_entries;
5637ccf4f90SNamhyung Kim 
5647ccf4f90SNamhyung Kim 	fprintf(fp, "# ........");
5657ccf4f90SNamhyung Kim 	if (symbol_conf.show_cpu_utilization)
5667ccf4f90SNamhyung Kim 		fprintf(fp, "   .......   .......");
5677ccf4f90SNamhyung Kim 	if (symbol_conf.show_nr_samples)
5687ccf4f90SNamhyung Kim 		fprintf(fp, " ..........");
5697ccf4f90SNamhyung Kim 	if (symbol_conf.show_total_period)
5707ccf4f90SNamhyung Kim 		fprintf(fp, " ............");
5717ccf4f90SNamhyung Kim 	if (pair) {
5727ccf4f90SNamhyung Kim 		fprintf(fp, " ..........");
5737ccf4f90SNamhyung Kim 		if (show_displacement)
5747ccf4f90SNamhyung Kim 			fprintf(fp, " .....");
5757ccf4f90SNamhyung Kim 	}
5767ccf4f90SNamhyung Kim 	list_for_each_entry(se, &hist_entry__sort_list, list) {
5777ccf4f90SNamhyung Kim 		unsigned int i;
5787ccf4f90SNamhyung Kim 
5797ccf4f90SNamhyung Kim 		if (se->elide)
5807ccf4f90SNamhyung Kim 			continue;
5817ccf4f90SNamhyung Kim 
5827ccf4f90SNamhyung Kim 		fprintf(fp, "  ");
5837ccf4f90SNamhyung Kim 		width = hists__col_len(hists, se->se_width_idx);
5847ccf4f90SNamhyung Kim 		if (width == 0)
5857ccf4f90SNamhyung Kim 			width = strlen(se->se_header);
5867ccf4f90SNamhyung Kim 		for (i = 0; i < width; i++)
5877ccf4f90SNamhyung Kim 			fprintf(fp, ".");
5887ccf4f90SNamhyung Kim 	}
5897ccf4f90SNamhyung Kim 
5907ccf4f90SNamhyung Kim 	fprintf(fp, "\n");
5917ccf4f90SNamhyung Kim 	if (max_rows && ++nr_rows >= max_rows)
5927ccf4f90SNamhyung Kim 		goto out;
5937ccf4f90SNamhyung Kim 
5947ccf4f90SNamhyung Kim 	fprintf(fp, "#\n");
5957ccf4f90SNamhyung Kim 	if (max_rows && ++nr_rows >= max_rows)
5967ccf4f90SNamhyung Kim 		goto out;
5977ccf4f90SNamhyung Kim 
5987ccf4f90SNamhyung Kim print_entries:
5997ccf4f90SNamhyung Kim 	total_period = hists->stats.total_period;
6007ccf4f90SNamhyung Kim 
6017ccf4f90SNamhyung Kim 	for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
6027ccf4f90SNamhyung Kim 		struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
6037ccf4f90SNamhyung Kim 
6047ccf4f90SNamhyung Kim 		if (h->filtered)
6057ccf4f90SNamhyung Kim 			continue;
6067ccf4f90SNamhyung Kim 
6077ccf4f90SNamhyung Kim 		if (show_displacement) {
6087ccf4f90SNamhyung Kim 			if (h->pair != NULL)
6097ccf4f90SNamhyung Kim 				displacement = ((long)h->pair->position -
6107ccf4f90SNamhyung Kim 					        (long)position);
6117ccf4f90SNamhyung Kim 			else
6127ccf4f90SNamhyung Kim 				displacement = 0;
6137ccf4f90SNamhyung Kim 			++position;
6147ccf4f90SNamhyung Kim 		}
6157ccf4f90SNamhyung Kim 		ret += hist_entry__fprintf(h, max_cols, hists, pair, show_displacement,
6167ccf4f90SNamhyung Kim 					   displacement, total_period, fp);
6177ccf4f90SNamhyung Kim 
6187ccf4f90SNamhyung Kim 		if (max_rows && ++nr_rows >= max_rows)
6197ccf4f90SNamhyung Kim 			goto out;
6207ccf4f90SNamhyung Kim 
6217ccf4f90SNamhyung Kim 		if (h->ms.map == NULL && verbose > 1) {
6227ccf4f90SNamhyung Kim 			__map_groups__fprintf_maps(&h->thread->mg,
6237ccf4f90SNamhyung Kim 						   MAP__FUNCTION, verbose, fp);
6247ccf4f90SNamhyung Kim 			fprintf(fp, "%.10s end\n", graph_dotted_line);
6257ccf4f90SNamhyung Kim 		}
6267ccf4f90SNamhyung Kim 	}
6277ccf4f90SNamhyung Kim out:
6287ccf4f90SNamhyung Kim 	free(rem_sq_bracket);
6297ccf4f90SNamhyung Kim 
6307ccf4f90SNamhyung Kim 	return ret;
6317ccf4f90SNamhyung Kim }
6327ccf4f90SNamhyung Kim 
6337ccf4f90SNamhyung Kim size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp)
6347ccf4f90SNamhyung Kim {
6357ccf4f90SNamhyung Kim 	int i;
6367ccf4f90SNamhyung Kim 	size_t ret = 0;
6377ccf4f90SNamhyung Kim 
6387ccf4f90SNamhyung Kim 	for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
6397ccf4f90SNamhyung Kim 		const char *name;
6407ccf4f90SNamhyung Kim 
6417ccf4f90SNamhyung Kim 		if (hists->stats.nr_events[i] == 0)
6427ccf4f90SNamhyung Kim 			continue;
6437ccf4f90SNamhyung Kim 
6447ccf4f90SNamhyung Kim 		name = perf_event__name(i);
6457ccf4f90SNamhyung Kim 		if (!strcmp(name, "UNKNOWN"))
6467ccf4f90SNamhyung Kim 			continue;
6477ccf4f90SNamhyung Kim 
6487ccf4f90SNamhyung Kim 		ret += fprintf(fp, "%16s events: %10d\n", name,
6497ccf4f90SNamhyung Kim 			       hists->stats.nr_events[i]);
6507ccf4f90SNamhyung Kim 	}
6517ccf4f90SNamhyung Kim 
6527ccf4f90SNamhyung Kim 	return ret;
6537ccf4f90SNamhyung Kim }
654