xref: /openbmc/linux/tools/perf/ui/hist.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
3ea251d51SNamhyung Kim #include <math.h>
4f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h>
5f2a39fe8SArnaldo Carvalho de Melo #include <string.h>
62c5d4b4aSJiri Olsa #include <linux/compiler.h>
7ea251d51SNamhyung Kim 
8b10ba7f1SArnaldo Carvalho de Melo #include "../util/callchain.h"
9b4209025SArnaldo Carvalho de Melo #include "../util/debug.h"
10ea251d51SNamhyung Kim #include "../util/hist.h"
11ea251d51SNamhyung Kim #include "../util/sort.h"
124fb71074SNamhyung Kim #include "../util/evsel.h"
13c3bc0c43SNamhyung Kim #include "../util/evlist.h"
14*ea0c5239SIan Rogers #include "../util/thread.h"
15ea251d51SNamhyung Kim #include "../util/util.h"
16ea251d51SNamhyung Kim 
17ea251d51SNamhyung Kim /* hist period print (hpp) functions */
18a0088adcSNamhyung Kim 
19a0088adcSNamhyung Kim #define hpp__call_print_fn(hpp, fn, fmt, ...)			\
20a0088adcSNamhyung Kim ({								\
21a0088adcSNamhyung Kim 	int __ret = fn(hpp, fmt, ##__VA_ARGS__);		\
22a0088adcSNamhyung Kim 	advance_hpp(hpp, __ret);				\
23a0088adcSNamhyung Kim 	__ret;							\
24a0088adcSNamhyung Kim })
255b591669SNamhyung Kim 
__hpp__fmt(struct perf_hpp * hpp,struct hist_entry * he,hpp_field_fn get_field,const char * fmt,int len,hpp_snprint_fn print_fn,bool fmt_percent)26d675107cSNamhyung Kim static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he,
27fb821c9eSNamhyung Kim 		      hpp_field_fn get_field, const char *fmt, int len,
28ea251d51SNamhyung Kim 		      hpp_snprint_fn print_fn, bool fmt_percent)
29fb821c9eSNamhyung Kim {
30b5ff71c3SJiri Olsa 	int ret;
3132dcd021SJiri Olsa 	struct hists *hists = he->hists;
32a0088adcSNamhyung Kim 	struct evsel *evsel = hists_to_evsel(hists);
33a0088adcSNamhyung Kim 	char *buf = hpp->buf;
34ea251d51SNamhyung Kim 	size_t size = hpp->size;
350c5268bfSJiri Olsa 
360c5268bfSJiri Olsa 	if (fmt_percent) {
37f2148330SNamhyung Kim 		double percent = 0.0;
380c5268bfSJiri Olsa 		u64 total = hists__total_period(hists);
39f2148330SNamhyung Kim 
40f2148330SNamhyung Kim 		if (total)
414fb71074SNamhyung Kim 			percent = 100.0 * get_field(he) / total;
42d675107cSNamhyung Kim 
430c5268bfSJiri Olsa 		ret = hpp__call_print_fn(hpp, print_fn, fmt, len, percent);
44d675107cSNamhyung Kim 	} else
455b9e2146SNamhyung Kim 		ret = hpp__call_print_fn(hpp, print_fn, fmt, len, get_field(he));
46c754c382SArnaldo Carvalho de Melo 
475b9e2146SNamhyung Kim 	if (evsel__is_group_event(evsel)) {
485b9e2146SNamhyung Kim 		int prev_idx, idx_delta;
495643b1a5SJiri Olsa 		struct hist_entry *pair;
505b9e2146SNamhyung Kim 		int nr_members = evsel->core.nr_members;
512bb72dbbSArnaldo Carvalho de Melo 
525b9e2146SNamhyung Kim 		prev_idx = evsel__group_idx(evsel);
535b9e2146SNamhyung Kim 
545b9e2146SNamhyung Kim 		list_for_each_entry(pair, &he->pairs.head, pairs.node) {
55f2148330SNamhyung Kim 			u64 period = get_field(pair);
565b9e2146SNamhyung Kim 			u64 total = hists__total_period(pair->hists);
575b9e2146SNamhyung Kim 
585b9e2146SNamhyung Kim 			if (!total)
595b9e2146SNamhyung Kim 				continue;
605b9e2146SNamhyung Kim 
612bb72dbbSArnaldo Carvalho de Melo 			evsel = hists_to_evsel(pair->hists);
625b9e2146SNamhyung Kim 			idx_delta = evsel__group_idx(evsel) - prev_idx - 1;
635b9e2146SNamhyung Kim 
645b9e2146SNamhyung Kim 			while (idx_delta--) {
655b9e2146SNamhyung Kim 				/*
665b9e2146SNamhyung Kim 				 * zero-fill group members in the middle which
675b9e2146SNamhyung Kim 				 * have no sample
689b0d2fb8SNamhyung Kim 				 */
69a0088adcSNamhyung Kim 				if (fmt_percent) {
70d675107cSNamhyung Kim 					ret += hpp__call_print_fn(hpp, print_fn,
719b0d2fb8SNamhyung Kim 								  fmt, len, 0.0);
72a0088adcSNamhyung Kim 				} else {
73d675107cSNamhyung Kim 					ret += hpp__call_print_fn(hpp, print_fn,
749b0d2fb8SNamhyung Kim 								  fmt, len, 0ULL);
755b9e2146SNamhyung Kim 				}
765b9e2146SNamhyung Kim 			}
77a0088adcSNamhyung Kim 
78d675107cSNamhyung Kim 			if (fmt_percent) {
79a0088adcSNamhyung Kim 				ret += hpp__call_print_fn(hpp, print_fn, fmt, len,
80a0088adcSNamhyung Kim 							  100.0 * period / total);
81a0088adcSNamhyung Kim 			} else {
82d675107cSNamhyung Kim 				ret += hpp__call_print_fn(hpp, print_fn, fmt,
83a0088adcSNamhyung Kim 							  len, period);
845b9e2146SNamhyung Kim 			}
852bb72dbbSArnaldo Carvalho de Melo 
865b9e2146SNamhyung Kim 			prev_idx = evsel__group_idx(evsel);
875b9e2146SNamhyung Kim 		}
885b9e2146SNamhyung Kim 
895b9e2146SNamhyung Kim 		idx_delta = nr_members - prev_idx - 1;
905b9e2146SNamhyung Kim 
915b9e2146SNamhyung Kim 		while (idx_delta--) {
925b9e2146SNamhyung Kim 			/*
935b9e2146SNamhyung Kim 			 * zero-fill group members at last which have no sample
949b0d2fb8SNamhyung Kim 			 */
95a0088adcSNamhyung Kim 			if (fmt_percent) {
96d675107cSNamhyung Kim 				ret += hpp__call_print_fn(hpp, print_fn,
979b0d2fb8SNamhyung Kim 							  fmt, len, 0.0);
98a0088adcSNamhyung Kim 			} else {
99d675107cSNamhyung Kim 				ret += hpp__call_print_fn(hpp, print_fn,
1009b0d2fb8SNamhyung Kim 							  fmt, len, 0ULL);
1015b9e2146SNamhyung Kim 			}
1025b9e2146SNamhyung Kim 		}
103a0088adcSNamhyung Kim 	}
104a0088adcSNamhyung Kim 
105a0088adcSNamhyung Kim 	/*
106a0088adcSNamhyung Kim 	 * Restore original buf and size as it's where caller expects
107a0088adcSNamhyung Kim 	 * the result will be saved.
108a0088adcSNamhyung Kim 	 */
109a0088adcSNamhyung Kim 	hpp->buf = buf;
110a0088adcSNamhyung Kim 	hpp->size = size;
1114fb71074SNamhyung Kim 
112ea251d51SNamhyung Kim 	return ret;
113ea251d51SNamhyung Kim }
1145b591669SNamhyung Kim 
hpp__fmt(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he,hpp_field_fn get_field,const char * fmtstr,hpp_snprint_fn print_fn,bool fmt_percent)1155b591669SNamhyung Kim int hpp__fmt(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1165b591669SNamhyung Kim 	     struct hist_entry *he, hpp_field_fn get_field,
117594dcbf3SNamhyung Kim 	     const char *fmtstr, hpp_snprint_fn print_fn, bool fmt_percent)
1185b591669SNamhyung Kim {
1195b591669SNamhyung Kim 	int len = fmt->user_len ?: fmt->len;
1205b591669SNamhyung Kim 
1215b591669SNamhyung Kim 	if (symbol_conf.field_sep) {
1225b591669SNamhyung Kim 		return __hpp__fmt(hpp, he, get_field, fmtstr, 1,
123594dcbf3SNamhyung Kim 				  print_fn, fmt_percent);
124594dcbf3SNamhyung Kim 	}
1255b591669SNamhyung Kim 
1265b591669SNamhyung Kim 	if (fmt_percent)
1275b591669SNamhyung Kim 		len -= 2; /* 2 for a space and a % sign */
1285b591669SNamhyung Kim 	else
1295b591669SNamhyung Kim 		len -= 1;
1305b591669SNamhyung Kim 
1315b591669SNamhyung Kim 	return  __hpp__fmt(hpp, he, get_field, fmtstr, len, print_fn, fmt_percent);
1325b591669SNamhyung Kim }
1335b591669SNamhyung Kim 
hpp__fmt_acc(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hist_entry * he,hpp_field_fn get_field,const char * fmtstr,hpp_snprint_fn print_fn,bool fmt_percent)1345b591669SNamhyung Kim int hpp__fmt_acc(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1355b591669SNamhyung Kim 		 struct hist_entry *he, hpp_field_fn get_field,
1365b591669SNamhyung Kim 		 const char *fmtstr, hpp_snprint_fn print_fn, bool fmt_percent)
1375b591669SNamhyung Kim {
1385b591669SNamhyung Kim 	if (!symbol_conf.cumulate_callchain) {
1395b591669SNamhyung Kim 		int len = fmt->user_len ?: fmt->len;
1405b591669SNamhyung Kim 		return snprintf(hpp->buf, hpp->size, " %*s", len - 1, "N/A");
1415b591669SNamhyung Kim 	}
1425b591669SNamhyung Kim 
143594dcbf3SNamhyung Kim 	return hpp__fmt(fmt, hpp, he, get_field, fmtstr, print_fn, fmt_percent);
144594dcbf3SNamhyung Kim }
145f156d84eSNamhyung Kim 
field_cmp(u64 field_a,u64 field_b)146f156d84eSNamhyung Kim static int field_cmp(u64 field_a, u64 field_b)
147f156d84eSNamhyung Kim {
148f156d84eSNamhyung Kim 	if (field_a > field_b)
149f156d84eSNamhyung Kim 		return 1;
150f156d84eSNamhyung Kim 	if (field_a < field_b)
151f156d84eSNamhyung Kim 		return -1;
152f156d84eSNamhyung Kim 	return 0;
153f156d84eSNamhyung Kim }
154429a5f9dSJin Yao 
hist_entry__new_pair(struct hist_entry * a,struct hist_entry * b,hpp_field_fn get_field,int nr_members,u64 ** fields_a,u64 ** fields_b)155429a5f9dSJin Yao static int hist_entry__new_pair(struct hist_entry *a, struct hist_entry *b,
156429a5f9dSJin Yao 				hpp_field_fn get_field, int nr_members,
157429a5f9dSJin Yao 				u64 **fields_a, u64 **fields_b)
158429a5f9dSJin Yao {
159429a5f9dSJin Yao 	u64 *fa = calloc(nr_members, sizeof(*fa)),
160429a5f9dSJin Yao 	    *fb = calloc(nr_members, sizeof(*fb));
161429a5f9dSJin Yao 	struct hist_entry *pair;
162429a5f9dSJin Yao 
163429a5f9dSJin Yao 	if (!fa || !fb)
164429a5f9dSJin Yao 		goto out_free;
165429a5f9dSJin Yao 
166429a5f9dSJin Yao 	list_for_each_entry(pair, &a->pairs.head, pairs.node) {
1672bb72dbbSArnaldo Carvalho de Melo 		struct evsel *evsel = hists_to_evsel(pair->hists);
168429a5f9dSJin Yao 		fa[evsel__group_idx(evsel)] = get_field(pair);
169429a5f9dSJin Yao 	}
170429a5f9dSJin Yao 
171429a5f9dSJin Yao 	list_for_each_entry(pair, &b->pairs.head, pairs.node) {
1722bb72dbbSArnaldo Carvalho de Melo 		struct evsel *evsel = hists_to_evsel(pair->hists);
173429a5f9dSJin Yao 		fb[evsel__group_idx(evsel)] = get_field(pair);
174429a5f9dSJin Yao 	}
175429a5f9dSJin Yao 
176429a5f9dSJin Yao 	*fields_a = fa;
177429a5f9dSJin Yao 	*fields_b = fb;
178429a5f9dSJin Yao 	return 0;
179429a5f9dSJin Yao out_free:
180429a5f9dSJin Yao 	free(fa);
181429a5f9dSJin Yao 	free(fb);
182429a5f9dSJin Yao 	*fields_a = *fields_b = NULL;
183429a5f9dSJin Yao 	return -1;
184429a5f9dSJin Yao }
185429a5f9dSJin Yao 
__hpp__group_sort_idx(struct hist_entry * a,struct hist_entry * b,hpp_field_fn get_field,int idx)186429a5f9dSJin Yao static int __hpp__group_sort_idx(struct hist_entry *a, struct hist_entry *b,
187429a5f9dSJin Yao 				 hpp_field_fn get_field, int idx)
188429a5f9dSJin Yao {
189429a5f9dSJin Yao 	struct evsel *evsel = hists_to_evsel(a->hists);
190429a5f9dSJin Yao 	u64 *fields_a, *fields_b;
191429a5f9dSJin Yao 	int cmp, nr_members, ret, i;
192429a5f9dSJin Yao 
193c754c382SArnaldo Carvalho de Melo 	cmp = field_cmp(get_field(a), get_field(b));
194429a5f9dSJin Yao 	if (!evsel__is_group_event(evsel))
195429a5f9dSJin Yao 		return cmp;
196429a5f9dSJin Yao 
197429a5f9dSJin Yao 	nr_members = evsel->core.nr_members;
198429a5f9dSJin Yao 	if (idx < 1 || idx >= nr_members)
199429a5f9dSJin Yao 		return cmp;
200429a5f9dSJin Yao 
201429a5f9dSJin Yao 	ret = hist_entry__new_pair(a, b, get_field, nr_members, &fields_a, &fields_b);
202429a5f9dSJin Yao 	if (ret) {
203429a5f9dSJin Yao 		ret = cmp;
204429a5f9dSJin Yao 		goto out;
205429a5f9dSJin Yao 	}
206429a5f9dSJin Yao 
207429a5f9dSJin Yao 	ret = field_cmp(fields_a[idx], fields_b[idx]);
208429a5f9dSJin Yao 	if (ret)
209429a5f9dSJin Yao 		goto out;
210429a5f9dSJin Yao 
211429a5f9dSJin Yao 	for (i = 1; i < nr_members; i++) {
212429a5f9dSJin Yao 		if (i != idx) {
213429a5f9dSJin Yao 			ret = field_cmp(fields_a[i], fields_b[i]);
214429a5f9dSJin Yao 			if (ret)
215429a5f9dSJin Yao 				goto out;
216429a5f9dSJin Yao 		}
217429a5f9dSJin Yao 	}
218429a5f9dSJin Yao 
219429a5f9dSJin Yao out:
220429a5f9dSJin Yao 	free(fields_a);
221429a5f9dSJin Yao 	free(fields_b);
222429a5f9dSJin Yao 
223429a5f9dSJin Yao 	return ret;
224429a5f9dSJin Yao }
225f156d84eSNamhyung Kim 
__hpp__sort(struct hist_entry * a,struct hist_entry * b,hpp_field_fn get_field)226f156d84eSNamhyung Kim static int __hpp__sort(struct hist_entry *a, struct hist_entry *b,
227f156d84eSNamhyung Kim 		       hpp_field_fn get_field)
228f156d84eSNamhyung Kim {
229f156d84eSNamhyung Kim 	s64 ret;
23032dcd021SJiri Olsa 	int i, nr_members;
231f156d84eSNamhyung Kim 	struct evsel *evsel;
232f156d84eSNamhyung Kim 	u64 *fields_a, *fields_b;
233429a5f9dSJin Yao 
234429a5f9dSJin Yao 	if (symbol_conf.group_sort_idx && symbol_conf.event_group) {
235429a5f9dSJin Yao 		return __hpp__group_sort_idx(a, b, get_field,
236429a5f9dSJin Yao 					     symbol_conf.group_sort_idx);
237429a5f9dSJin Yao 	}
238f156d84eSNamhyung Kim 
239f156d84eSNamhyung Kim 	ret = field_cmp(get_field(a), get_field(b));
240f156d84eSNamhyung Kim 	if (ret || !symbol_conf.event_group)
241f156d84eSNamhyung Kim 		return ret;
242f156d84eSNamhyung Kim 
243c754c382SArnaldo Carvalho de Melo 	evsel = hists_to_evsel(a->hists);
244f156d84eSNamhyung Kim 	if (!evsel__is_group_event(evsel))
245f156d84eSNamhyung Kim 		return ret;
2465643b1a5SJiri Olsa 
247429a5f9dSJin Yao 	nr_members = evsel->core.nr_members;
248429a5f9dSJin Yao 	i = hist_entry__new_pair(a, b, get_field, nr_members, &fields_a, &fields_b);
249f156d84eSNamhyung Kim 	if (i)
250f156d84eSNamhyung Kim 		goto out;
251f156d84eSNamhyung Kim 
252f156d84eSNamhyung Kim 	for (i = 1; i < nr_members; i++) {
253f156d84eSNamhyung Kim 		ret = field_cmp(fields_a[i], fields_b[i]);
254f156d84eSNamhyung Kim 		if (ret)
255f156d84eSNamhyung Kim 			break;
256f156d84eSNamhyung Kim 	}
257f156d84eSNamhyung Kim 
258f156d84eSNamhyung Kim out:
259f156d84eSNamhyung Kim 	free(fields_a);
260f156d84eSNamhyung Kim 	free(fields_b);
261f156d84eSNamhyung Kim 
262f156d84eSNamhyung Kim 	return ret;
263f156d84eSNamhyung Kim }
264594dcbf3SNamhyung Kim 
__hpp__sort_acc(struct hist_entry * a,struct hist_entry * b,hpp_field_fn get_field)265594dcbf3SNamhyung Kim static int __hpp__sort_acc(struct hist_entry *a, struct hist_entry *b,
266594dcbf3SNamhyung Kim 			   hpp_field_fn get_field)
267594dcbf3SNamhyung Kim {
268594dcbf3SNamhyung Kim 	s64 ret = 0;
269594dcbf3SNamhyung Kim 
270594dcbf3SNamhyung Kim 	if (symbol_conf.cumulate_callchain) {
271594dcbf3SNamhyung Kim 		/*
272594dcbf3SNamhyung Kim 		 * Put caller above callee when they have equal period.
273594dcbf3SNamhyung Kim 		 */
274594dcbf3SNamhyung Kim 		ret = field_cmp(get_field(a), get_field(b));
275594dcbf3SNamhyung Kim 		if (ret)
276594dcbf3SNamhyung Kim 			return ret;
277fabd37b8SArnaldo Carvalho de Melo 
2785ca82710SNamhyung Kim 		if ((a->thread == NULL ? NULL : RC_CHK_ACCESS(a->thread)) !=
2795ca82710SNamhyung Kim 		    (b->thread == NULL ? NULL : RC_CHK_ACCESS(b->thread)) ||
280594dcbf3SNamhyung Kim 		    !hist_entry__has_callchains(a) || !symbol_conf.use_callchain)
2817111ffffSNamhyung Kim 			return 0;
2827111ffffSNamhyung Kim 
283594dcbf3SNamhyung Kim 		ret = b->callchain->max_depth - a->callchain->max_depth;
284594dcbf3SNamhyung Kim 		if (callchain_param.order == ORDER_CALLER)
285594dcbf3SNamhyung Kim 			ret = -ret;
286594dcbf3SNamhyung Kim 	}
2871ecd4453SNamhyung Kim 	return ret;
2881ecd4453SNamhyung Kim }
289da1b0407SJiri Olsa 
hpp__width_fn(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp __maybe_unused,struct hists * hists)2901ecd4453SNamhyung Kim static int hpp__width_fn(struct perf_hpp_fmt *fmt,
2911ecd4453SNamhyung Kim 			 struct perf_hpp *hpp __maybe_unused,
29232dcd021SJiri Olsa 			 struct hists *hists)
2931ecd4453SNamhyung Kim {
2941ecd4453SNamhyung Kim 	int len = fmt->user_len ?: fmt->len;
2955643b1a5SJiri Olsa 	struct evsel *evsel = hists_to_evsel(hists);
2961ecd4453SNamhyung Kim 
2971ecd4453SNamhyung Kim 	if (symbol_conf.event_group)
2981ecd4453SNamhyung Kim 		len = max(len, evsel->core.nr_members * fmt->len);
2991ecd4453SNamhyung Kim 
3001ecd4453SNamhyung Kim 	if (len < (int)strlen(fmt->name))
301ea251d51SNamhyung Kim 		len = strlen(fmt->name);
302ea251d51SNamhyung Kim 
3031ecd4453SNamhyung Kim 	return len;
30429659ab4SJiri Olsa }
30529659ab4SJiri Olsa 
hpp__header_fn(struct perf_hpp_fmt * fmt,struct perf_hpp * hpp,struct hists * hists,int line __maybe_unused,int * span __maybe_unused)3061ecd4453SNamhyung Kim static int hpp__header_fn(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
307da1b0407SJiri Olsa 			  struct hists *hists, int line __maybe_unused,
3081ecd4453SNamhyung Kim 			  int *span __maybe_unused)
309e0d66c74SNamhyung Kim {
310e0d66c74SNamhyung Kim 	int len = hpp__width_fn(fmt, hpp, hists);
31198ba1609SJiri Olsa 	return scnprintf(hpp->buf, hpp->size, "%*s", len, fmt->name);
312a0088adcSNamhyung Kim }
313a0088adcSNamhyung Kim 
hpp_color_scnprintf(struct perf_hpp * hpp,const char * fmt,...)314a0088adcSNamhyung Kim int hpp_color_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
315a0088adcSNamhyung Kim {
316d675107cSNamhyung Kim 	va_list args;
317a0088adcSNamhyung Kim 	ssize_t ssize = hpp->size;
318a0088adcSNamhyung Kim 	double percent;
319d675107cSNamhyung Kim 	int ret, len;
320a0088adcSNamhyung Kim 
321d675107cSNamhyung Kim 	va_start(args, fmt);
322a0088adcSNamhyung Kim 	len = va_arg(args, int);
323a0088adcSNamhyung Kim 	percent = va_arg(args, double);
324a0088adcSNamhyung Kim 	ret = percent_color_len_snprintf(hpp->buf, hpp->size, fmt, len, percent);
325a0088adcSNamhyung Kim 	va_end(args);
326a0088adcSNamhyung Kim 
327a0088adcSNamhyung Kim 	return (ret >= ssize) ? (ssize - 1) : ret;
328a0088adcSNamhyung Kim }
329a0088adcSNamhyung Kim 
hpp_entry_scnprintf(struct perf_hpp * hpp,const char * fmt,...)330a0088adcSNamhyung Kim static int hpp_entry_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
331a0088adcSNamhyung Kim {
332a0088adcSNamhyung Kim 	va_list args;
333a0088adcSNamhyung Kim 	ssize_t ssize = hpp->size;
334a0088adcSNamhyung Kim 	int ret;
335a0088adcSNamhyung Kim 
336a0088adcSNamhyung Kim 	va_start(args, fmt);
337a0088adcSNamhyung Kim 	ret = vsnprintf(hpp->buf, hpp->size, fmt, args);
338a0088adcSNamhyung Kim 	va_end(args);
339a0088adcSNamhyung Kim 
3404fb71074SNamhyung Kim 	return (ret >= ssize) ? (ssize - 1) : ret;
3414fb71074SNamhyung Kim }
3424fb71074SNamhyung Kim 
3434fb71074SNamhyung Kim #define __HPP_COLOR_PERCENT_FN(_type, _field)					\
3444fb71074SNamhyung Kim static u64 he_get_##_field(struct hist_entry *he)				\
3454fb71074SNamhyung Kim {										\
346e0d66c74SNamhyung Kim 	return he->stat._field;							\
3472c5d4b4aSJiri Olsa }										\
3484fb71074SNamhyung Kim 										\
3495b591669SNamhyung Kim static int hpp__color_##_type(struct perf_hpp_fmt *fmt,				\
350a0088adcSNamhyung Kim 			      struct perf_hpp *hpp, struct hist_entry *he) 	\
351ea251d51SNamhyung Kim {										\
352ea251d51SNamhyung Kim 	return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%",		\
3534fb71074SNamhyung Kim 			hpp_color_scnprintf, true);				\
354e0d66c74SNamhyung Kim }
3552c5d4b4aSJiri Olsa 
3564fb71074SNamhyung Kim #define __HPP_ENTRY_PERCENT_FN(_type, _field)					\
3575b591669SNamhyung Kim static int hpp__entry_##_type(struct perf_hpp_fmt *fmt,				\
358a0088adcSNamhyung Kim 			      struct perf_hpp *hpp, struct hist_entry *he) 	\
359ea251d51SNamhyung Kim {										\
360ea251d51SNamhyung Kim 	return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%",		\
361bc18b7f2SNamhyung Kim 			hpp_entry_scnprintf, true);				\
36287bbdf76SNamhyung Kim }
36387bbdf76SNamhyung Kim 
364bc18b7f2SNamhyung Kim #define __HPP_SORT_FN(_type, _field)						\
365f156d84eSNamhyung Kim static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, 	\
366bc18b7f2SNamhyung Kim 				 struct hist_entry *a, struct hist_entry *b) 	\
367bc18b7f2SNamhyung Kim {										\
368594dcbf3SNamhyung Kim 	return __hpp__sort(a, b, he_get_##_field);				\
369594dcbf3SNamhyung Kim }
370594dcbf3SNamhyung Kim 
371594dcbf3SNamhyung Kim #define __HPP_COLOR_ACC_PERCENT_FN(_type, _field)				\
372594dcbf3SNamhyung Kim static u64 he_get_acc_##_field(struct hist_entry *he)				\
373594dcbf3SNamhyung Kim {										\
374e0d66c74SNamhyung Kim 	return he->stat_acc->_field;						\
375594dcbf3SNamhyung Kim }										\
376594dcbf3SNamhyung Kim 										\
3775b591669SNamhyung Kim static int hpp__color_##_type(struct perf_hpp_fmt *fmt,				\
378594dcbf3SNamhyung Kim 			      struct perf_hpp *hpp, struct hist_entry *he) 	\
379594dcbf3SNamhyung Kim {										\
380594dcbf3SNamhyung Kim 	return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", 	\
381594dcbf3SNamhyung Kim 			    hpp_color_scnprintf, true);				\
382e0d66c74SNamhyung Kim }
383594dcbf3SNamhyung Kim 
384594dcbf3SNamhyung Kim #define __HPP_ENTRY_ACC_PERCENT_FN(_type, _field)				\
3852bfa1528SNamhyung Kim static int hpp__entry_##_type(struct perf_hpp_fmt *fmt,				\
386594dcbf3SNamhyung Kim 			      struct perf_hpp *hpp, struct hist_entry *he) 	\
387594dcbf3SNamhyung Kim {										\
388594dcbf3SNamhyung Kim 	return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%",	\
389594dcbf3SNamhyung Kim 			    hpp_entry_scnprintf, true);				\
39087bbdf76SNamhyung Kim }
39187bbdf76SNamhyung Kim 
392594dcbf3SNamhyung Kim #define __HPP_SORT_ACC_FN(_type, _field)					\
393594dcbf3SNamhyung Kim static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, 	\
394594dcbf3SNamhyung Kim 				 struct hist_entry *a, struct hist_entry *b) 	\
395594dcbf3SNamhyung Kim {										\
3964fb71074SNamhyung Kim 	return __hpp__sort_acc(a, b, he_get_acc_##_field);			\
3974fb71074SNamhyung Kim }
3984fb71074SNamhyung Kim 
3994fb71074SNamhyung Kim #define __HPP_ENTRY_RAW_FN(_type, _field)					\
4004fb71074SNamhyung Kim static u64 he_get_raw_##_field(struct hist_entry *he)				\
4014fb71074SNamhyung Kim {										\
402e0d66c74SNamhyung Kim 	return he->stat._field;							\
4032c5d4b4aSJiri Olsa }										\
4044fb71074SNamhyung Kim 										\
4055b591669SNamhyung Kim static int hpp__entry_##_type(struct perf_hpp_fmt *fmt,				\
406a0088adcSNamhyung Kim 			      struct perf_hpp *hpp, struct hist_entry *he) 	\
407ea251d51SNamhyung Kim {										\
408ea251d51SNamhyung Kim 	return hpp__fmt(fmt, hpp, he, he_get_raw_##_field, " %*"PRIu64, 	\
409bc18b7f2SNamhyung Kim 			hpp_entry_scnprintf, false);				\
41087bbdf76SNamhyung Kim }
41187bbdf76SNamhyung Kim 
412bc18b7f2SNamhyung Kim #define __HPP_SORT_RAW_FN(_type, _field)					\
413f156d84eSNamhyung Kim static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, 	\
414bc18b7f2SNamhyung Kim 				 struct hist_entry *a, struct hist_entry *b) 	\
415bc18b7f2SNamhyung Kim {										\
416bc18b7f2SNamhyung Kim 	return __hpp__sort(a, b, he_get_raw_##_field);				\
4171ecd4453SNamhyung Kim }
4184fb71074SNamhyung Kim 
419bc18b7f2SNamhyung Kim 
420bc18b7f2SNamhyung Kim #define HPP_PERCENT_FNS(_type, _field)					\
421ea251d51SNamhyung Kim __HPP_COLOR_PERCENT_FN(_type, _field)					\
4221ecd4453SNamhyung Kim __HPP_ENTRY_PERCENT_FN(_type, _field)					\
423594dcbf3SNamhyung Kim __HPP_SORT_FN(_type, _field)
424594dcbf3SNamhyung Kim 
425594dcbf3SNamhyung Kim #define HPP_PERCENT_ACC_FNS(_type, _field)				\
426594dcbf3SNamhyung Kim __HPP_COLOR_ACC_PERCENT_FN(_type, _field)				\
4271ecd4453SNamhyung Kim __HPP_ENTRY_ACC_PERCENT_FN(_type, _field)				\
428bc18b7f2SNamhyung Kim __HPP_SORT_ACC_FN(_type, _field)
429bc18b7f2SNamhyung Kim 
430b5ff71c3SJiri Olsa #define HPP_RAW_FNS(_type, _field)					\
4311ecd4453SNamhyung Kim __HPP_ENTRY_RAW_FN(_type, _field)					\
4321ecd4453SNamhyung Kim __HPP_SORT_RAW_FN(_type, _field)
4331ecd4453SNamhyung Kim 
HPP_PERCENT_FNS(overhead,period)4341ecd4453SNamhyung Kim HPP_PERCENT_FNS(overhead, period)
4351ecd4453SNamhyung Kim HPP_PERCENT_FNS(overhead_sys, period_sys)
4361ecd4453SNamhyung Kim HPP_PERCENT_FNS(overhead_us, period_us)
437ea251d51SNamhyung Kim HPP_PERCENT_FNS(overhead_guest_sys, period_guest_sys)
4381ecd4453SNamhyung Kim HPP_PERCENT_FNS(overhead_guest_us, period_guest_us)
4391ecd4453SNamhyung Kim HPP_PERCENT_ACC_FNS(overhead_acc, period)
440ea251d51SNamhyung Kim 
44187bbdf76SNamhyung Kim HPP_RAW_FNS(samples, nr_events)
44287bbdf76SNamhyung Kim HPP_RAW_FNS(period, period)
443bc18b7f2SNamhyung Kim 
444bc18b7f2SNamhyung Kim static int64_t hpp__nop_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
445bc18b7f2SNamhyung Kim 			    struct hist_entry *a __maybe_unused,
446bc18b7f2SNamhyung Kim 			    struct hist_entry *b __maybe_unused)
447bc18b7f2SNamhyung Kim {
448c0020efaSJiri Olsa 	return 0;
449c0020efaSJiri Olsa }
450c0020efaSJiri Olsa 
perf_hpp__is_hpp_entry(struct perf_hpp_fmt * a)451c0020efaSJiri Olsa static bool perf_hpp__is_hpp_entry(struct perf_hpp_fmt *a)
452c0020efaSJiri Olsa {
453c0020efaSJiri Olsa 	return a->header == hpp__header_fn;
454c0020efaSJiri Olsa }
455c0020efaSJiri Olsa 
hpp__equal(struct perf_hpp_fmt * a,struct perf_hpp_fmt * b)456c0020efaSJiri Olsa static bool hpp__equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
457c0020efaSJiri Olsa {
458c0020efaSJiri Olsa 	if (!perf_hpp__is_hpp_entry(a) || !perf_hpp__is_hpp_entry(b))
459c0020efaSJiri Olsa 		return false;
460c0020efaSJiri Olsa 
461b21a763eSJiri Olsa 	return a->idx == b->idx;
4621240005eSJiri Olsa }
4631ecd4453SNamhyung Kim 
4641ecd4453SNamhyung Kim #define HPP__COLOR_PRINT_FNS(_name, _fn, _idx)		\
4651ecd4453SNamhyung Kim 	{						\
4661ecd4453SNamhyung Kim 		.name   = _name,			\
4671ecd4453SNamhyung Kim 		.header	= hpp__header_fn,		\
468bc18b7f2SNamhyung Kim 		.width	= hpp__width_fn,		\
469bc18b7f2SNamhyung Kim 		.color	= hpp__color_ ## _fn,		\
4701ecd4453SNamhyung Kim 		.entry	= hpp__entry_ ## _fn,		\
471b21a763eSJiri Olsa 		.cmp	= hpp__nop_cmp,			\
472c0020efaSJiri Olsa 		.collapse = hpp__nop_cmp,		\
4731240005eSJiri Olsa 		.sort	= hpp__sort_ ## _fn,		\
474ea251d51SNamhyung Kim 		.idx	= PERF_HPP__ ## _idx,		\
475b21a763eSJiri Olsa 		.equal	= hpp__equal,			\
476594dcbf3SNamhyung Kim 	}
4771ecd4453SNamhyung Kim 
4781ecd4453SNamhyung Kim #define HPP__COLOR_ACC_PRINT_FNS(_name, _fn, _idx)	\
4791ecd4453SNamhyung Kim 	{						\
4801ecd4453SNamhyung Kim 		.name   = _name,			\
4811ecd4453SNamhyung Kim 		.header	= hpp__header_fn,		\
482594dcbf3SNamhyung Kim 		.width	= hpp__width_fn,		\
483594dcbf3SNamhyung Kim 		.color	= hpp__color_ ## _fn,		\
4841ecd4453SNamhyung Kim 		.entry	= hpp__entry_ ## _fn,		\
485b21a763eSJiri Olsa 		.cmp	= hpp__nop_cmp,			\
486c0020efaSJiri Olsa 		.collapse = hpp__nop_cmp,		\
487594dcbf3SNamhyung Kim 		.sort	= hpp__sort_ ## _fn,		\
488594dcbf3SNamhyung Kim 		.idx	= PERF_HPP__ ## _idx,		\
489b21a763eSJiri Olsa 		.equal	= hpp__equal,			\
4901240005eSJiri Olsa 	}
4911ecd4453SNamhyung Kim 
4921ecd4453SNamhyung Kim #define HPP__PRINT_FNS(_name, _fn, _idx)		\
4931ecd4453SNamhyung Kim 	{						\
4941ecd4453SNamhyung Kim 		.name   = _name,			\
495bc18b7f2SNamhyung Kim 		.header	= hpp__header_fn,		\
496bc18b7f2SNamhyung Kim 		.width	= hpp__width_fn,		\
4971ecd4453SNamhyung Kim 		.entry	= hpp__entry_ ## _fn,		\
498b21a763eSJiri Olsa 		.cmp	= hpp__nop_cmp,			\
499c0020efaSJiri Olsa 		.collapse = hpp__nop_cmp,		\
5001240005eSJiri Olsa 		.sort	= hpp__sort_ ## _fn,		\
501ea251d51SNamhyung Kim 		.idx	= PERF_HPP__ ## _idx,		\
502ea251d51SNamhyung Kim 		.equal	= hpp__equal,			\
503b21a763eSJiri Olsa 	}
504b21a763eSJiri Olsa 
505b21a763eSJiri Olsa struct perf_hpp_fmt perf_hpp__format[] = {
506b21a763eSJiri Olsa 	HPP__COLOR_PRINT_FNS("Overhead", overhead, OVERHEAD),
507b21a763eSJiri Olsa 	HPP__COLOR_PRINT_FNS("sys", overhead_sys, OVERHEAD_SYS),
508b21a763eSJiri Olsa 	HPP__COLOR_PRINT_FNS("usr", overhead_us, OVERHEAD_US),
509b21a763eSJiri Olsa 	HPP__COLOR_PRINT_FNS("guest sys", overhead_guest_sys, OVERHEAD_GUEST_SYS),
510b21a763eSJiri Olsa 	HPP__COLOR_PRINT_FNS("guest usr", overhead_guest_us, OVERHEAD_GUEST_US),
511ea251d51SNamhyung Kim 	HPP__COLOR_ACC_PRINT_FNS("Children", overhead_acc, OVERHEAD_ACC),
512ea251d51SNamhyung Kim 	HPP__PRINT_FNS("Samples", samples, SAMPLES),
5137c31e102SJiri Olsa 	HPP__PRINT_FNS("Period", period, PERIOD)
5147c31e102SJiri Olsa };
5157c31e102SJiri Olsa 
516f8e6710dSJiri Olsa struct perf_hpp_list perf_hpp_list = {
5177c31e102SJiri Olsa 	.fields	= LIST_HEAD_INIT(perf_hpp_list.fields),
5184fb71074SNamhyung Kim 	.sorts	= LIST_HEAD_INIT(perf_hpp_list.sorts),
519ea251d51SNamhyung Kim 	.nr_header_lines = 1,
520594dcbf3SNamhyung Kim };
521ea251d51SNamhyung Kim 
522ea251d51SNamhyung Kim #undef HPP__COLOR_PRINT_FNS
5234fb71074SNamhyung Kim #undef HPP__COLOR_ACC_PRINT_FNS
524594dcbf3SNamhyung Kim #undef HPP__PRINT_FNS
5254fb71074SNamhyung Kim 
5264fb71074SNamhyung Kim #undef HPP_PERCENT_FNS
5274fb71074SNamhyung Kim #undef HPP_PERCENT_ACC_FNS
5284fb71074SNamhyung Kim #undef HPP_RAW_FNS
5294fb71074SNamhyung Kim 
5304fb71074SNamhyung Kim #undef __HPP_HEADER_FN
531594dcbf3SNamhyung Kim #undef __HPP_WIDTH_FN
532594dcbf3SNamhyung Kim #undef __HPP_COLOR_PERCENT_FN
5334fb71074SNamhyung Kim #undef __HPP_ENTRY_PERCENT_FN
534594dcbf3SNamhyung Kim #undef __HPP_COLOR_ACC_PERCENT_FN
535594dcbf3SNamhyung Kim #undef __HPP_ENTRY_ACC_PERCENT_FN
536594dcbf3SNamhyung Kim #undef __HPP_ENTRY_RAW_FN
5374fb71074SNamhyung Kim #undef __HPP_SORT_FN
5380ca1f534SIan Rogers #undef __HPP_SORT_ACC_FN
5390ca1f534SIan Rogers #undef __HPP_SORT_RAW_FN
5400ca1f534SIan Rogers 
fmt_free(struct perf_hpp_fmt * fmt)5410ca1f534SIan Rogers static void fmt_free(struct perf_hpp_fmt *fmt)
5420ca1f534SIan Rogers {
5430ca1f534SIan Rogers 	/*
5440ca1f534SIan Rogers 	 * At this point fmt should be completely
5450ca1f534SIan Rogers 	 * unhooked, if not it's a bug.
5460ca1f534SIan Rogers 	 */
5470ca1f534SIan Rogers 	BUG_ON(!list_empty(&fmt->list));
5480ca1f534SIan Rogers 	BUG_ON(!list_empty(&fmt->sort_list));
5490ca1f534SIan Rogers 
5504fb71074SNamhyung Kim 	if (fmt->free)
5511d77822eSJiri Olsa 		fmt->free(fmt);
552ea251d51SNamhyung Kim }
55326d8b338SNamhyung Kim 
perf_hpp__init(void)55426d8b338SNamhyung Kim void perf_hpp__init(void)
55526d8b338SNamhyung Kim {
556a2ce067eSNamhyung Kim 	int i;
557a2ce067eSNamhyung Kim 
558a2ce067eSNamhyung Kim 	for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
559a2ce067eSNamhyung Kim 		struct perf_hpp_fmt *fmt = &perf_hpp__format[i];
560a2ce067eSNamhyung Kim 
561a2ce067eSNamhyung Kim 		INIT_LIST_HEAD(&fmt->list);
562a2ce067eSNamhyung Kim 
56326d8b338SNamhyung Kim 		/* sort_list may be linked by setup_sorting() */
56426d8b338SNamhyung Kim 		if (fmt->sort_list.next == NULL)
565a7d945bcSNamhyung Kim 			INIT_LIST_HEAD(&fmt->sort_list);
566a7d945bcSNamhyung Kim 	}
567a7d945bcSNamhyung Kim 
5682f3f9bcfSJiri Olsa 	/*
569a7d945bcSNamhyung Kim 	 * If user specified field order, no need to setup default fields.
570a7d945bcSNamhyung Kim 	 */
571594dcbf3SNamhyung Kim 	if (is_strict_order(field_order))
5721178bfd4SJiri Olsa 		return;
5731ecd4453SNamhyung Kim 
574594dcbf3SNamhyung Kim 	if (symbol_conf.cumulate_callchain) {
575594dcbf3SNamhyung Kim 		hpp_dimension__add_output(PERF_HPP__OVERHEAD_ACC);
5761178bfd4SJiri Olsa 		perf_hpp__format[PERF_HPP__OVERHEAD].name = "Self";
5772b8bfa6bSJiri Olsa 	}
578ea251d51SNamhyung Kim 
5791178bfd4SJiri Olsa 	hpp_dimension__add_output(PERF_HPP__OVERHEAD);
5801178bfd4SJiri Olsa 
581ea251d51SNamhyung Kim 	if (symbol_conf.show_cpu_utilization) {
582ea251d51SNamhyung Kim 		hpp_dimension__add_output(PERF_HPP__OVERHEAD_SYS);
5831178bfd4SJiri Olsa 		hpp_dimension__add_output(PERF_HPP__OVERHEAD_US);
5841178bfd4SJiri Olsa 
585ea251d51SNamhyung Kim 		if (perf_guest) {
586ea251d51SNamhyung Kim 			hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_SYS);
587ea251d51SNamhyung Kim 			hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_US);
588ea251d51SNamhyung Kim 		}
5891178bfd4SJiri Olsa 	}
590ea251d51SNamhyung Kim 
591ea251d51SNamhyung Kim 	if (symbol_conf.show_nr_samples)
5921178bfd4SJiri Olsa 		hpp_dimension__add_output(PERF_HPP__SAMPLES);
593ea251d51SNamhyung Kim 
5941d77822eSJiri Olsa 	if (symbol_conf.show_total_period)
595ebdd98e0SJiri Olsa 		hpp_dimension__add_output(PERF_HPP__PERIOD);
596ebdd98e0SJiri Olsa }
5971240005eSJiri Olsa 
perf_hpp_list__column_register(struct perf_hpp_list * list,struct perf_hpp_fmt * format)598ebdd98e0SJiri Olsa void perf_hpp_list__column_register(struct perf_hpp_list *list,
599ebdd98e0SJiri Olsa 				    struct perf_hpp_fmt *format)
600ebdd98e0SJiri Olsa {
601ebdd98e0SJiri Olsa 	list_add_tail(&format->list, &list->fields);
602ebdd98e0SJiri Olsa }
603ebdd98e0SJiri Olsa 
perf_hpp_list__register_sort_field(struct perf_hpp_list * list,struct perf_hpp_fmt * format)604ebdd98e0SJiri Olsa void perf_hpp_list__register_sort_field(struct perf_hpp_list *list,
6051240005eSJiri Olsa 					struct perf_hpp_fmt *format)
6061240005eSJiri Olsa {
607a1c9f97fSNamhyung Kim 	list_add_tail(&format->sort_list, &list->sorts);
608a1c9f97fSNamhyung Kim }
609a1c9f97fSNamhyung Kim 
perf_hpp_list__prepend_sort_field(struct perf_hpp_list * list,struct perf_hpp_fmt * format)610a1c9f97fSNamhyung Kim void perf_hpp_list__prepend_sort_field(struct perf_hpp_list *list,
611a1c9f97fSNamhyung Kim 				       struct perf_hpp_fmt *format)
612a1c9f97fSNamhyung Kim {
6130ca1f534SIan Rogers 	list_add(&format->sort_list, &list->sorts);
61477284de3SNamhyung Kim }
61570b01dfdSJiri Olsa 
perf_hpp__column_unregister(struct perf_hpp_fmt * format)6160ca1f534SIan Rogers static void perf_hpp__column_unregister(struct perf_hpp_fmt *format)
61777284de3SNamhyung Kim {
61877284de3SNamhyung Kim 	list_del_init(&format->list);
61977284de3SNamhyung Kim 	fmt_free(format);
62077284de3SNamhyung Kim }
6211945c3e7SJiri Olsa 
perf_hpp__cancel_cumulate(void)6221945c3e7SJiri Olsa void perf_hpp__cancel_cumulate(void)
6232f3f9bcfSJiri Olsa {
6242bf1a123SNamhyung Kim 	struct perf_hpp_fmt *fmt, *acc, *ovh, *tmp;
6252bf1a123SNamhyung Kim 
6261945c3e7SJiri Olsa 	if (is_strict_order(field_order))
6271945c3e7SJiri Olsa 		return;
6281945c3e7SJiri Olsa 
6297a1799e0SJiri Olsa 	ovh = &perf_hpp__format[PERF_HPP__OVERHEAD];
6301945c3e7SJiri Olsa 	acc = &perf_hpp__format[PERF_HPP__OVERHEAD_ACC];
6311945c3e7SJiri Olsa 
6321945c3e7SJiri Olsa 	perf_hpp_list__for_each_format_safe(&perf_hpp_list, fmt, tmp) {
6331945c3e7SJiri Olsa 		if (acc->equal(acc, fmt)) {
6341945c3e7SJiri Olsa 			perf_hpp__column_unregister(fmt);
6351945c3e7SJiri Olsa 			continue;
6361945c3e7SJiri Olsa 		}
6371945c3e7SJiri Olsa 
63877284de3SNamhyung Kim 		if (ovh->equal(ovh, fmt))
63977284de3SNamhyung Kim 			fmt->name = "Overhead";
64097358084SJiri Olsa 	}
64197358084SJiri Olsa }
64297358084SJiri Olsa 
fmt_equal(struct perf_hpp_fmt * a,struct perf_hpp_fmt * b)64397358084SJiri Olsa static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
64497358084SJiri Olsa {
64543e0a68fSJiri Olsa 	return a->equal && a->equal(a, b);
64626d8b338SNamhyung Kim }
64726d8b338SNamhyung Kim 
perf_hpp__setup_output_field(struct perf_hpp_list * list)64826d8b338SNamhyung Kim void perf_hpp__setup_output_field(struct perf_hpp_list *list)
64926d8b338SNamhyung Kim {
65043e0a68fSJiri Olsa 	struct perf_hpp_fmt *fmt;
651a7d945bcSNamhyung Kim 
652a7d945bcSNamhyung Kim 	/* append sort keys to output field */
6538381cdd0SNamhyung Kim 	perf_hpp_list__for_each_sort_list(list, fmt) {
6548381cdd0SNamhyung Kim 		struct perf_hpp_fmt *pos;
6558381cdd0SNamhyung Kim 
6568381cdd0SNamhyung Kim 		/* skip sort-only fields ("sort_compute" in perf diff) */
65743e0a68fSJiri Olsa 		if (!fmt->entry && !fmt->color)
65897358084SJiri Olsa 			continue;
659a7d945bcSNamhyung Kim 
660a7d945bcSNamhyung Kim 		perf_hpp_list__for_each_format(list, pos) {
661a7d945bcSNamhyung Kim 			if (fmt_equal(fmt, pos))
66226d8b338SNamhyung Kim 				goto next;
663a7d945bcSNamhyung Kim 		}
664a7d945bcSNamhyung Kim 
665a7d945bcSNamhyung Kim 		perf_hpp__column_register(fmt);
666a7d945bcSNamhyung Kim next:
667a7d945bcSNamhyung Kim 		continue;
66843e0a68fSJiri Olsa 	}
669a7d945bcSNamhyung Kim }
670a7d945bcSNamhyung Kim 
perf_hpp__append_sort_keys(struct perf_hpp_list * list)671a7d945bcSNamhyung Kim void perf_hpp__append_sort_keys(struct perf_hpp_list *list)
672a7d945bcSNamhyung Kim {
67343e0a68fSJiri Olsa 	struct perf_hpp_fmt *fmt;
674a7d945bcSNamhyung Kim 
675a7d945bcSNamhyung Kim 	/* append output fields to sort keys */
67643e0a68fSJiri Olsa 	perf_hpp_list__for_each_format(list, fmt) {
67797358084SJiri Olsa 		struct perf_hpp_fmt *pos;
678a7d945bcSNamhyung Kim 
679a7d945bcSNamhyung Kim 		perf_hpp_list__for_each_sort_list(list, pos) {
680a7d945bcSNamhyung Kim 			if (fmt_equal(fmt, pos))
681a7d945bcSNamhyung Kim 				goto next;
682a7d945bcSNamhyung Kim 		}
683a7d945bcSNamhyung Kim 
68426d8b338SNamhyung Kim 		perf_hpp__register_sort_field(fmt);
68526d8b338SNamhyung Kim next:
68626d8b338SNamhyung Kim 		continue;
68743e0a68fSJiri Olsa 	}
68843e0a68fSJiri Olsa }
6891c89fe9bSNamhyung Kim 
6901c89fe9bSNamhyung Kim 
perf_hpp__reset_output_field(struct perf_hpp_list * list)6911c89fe9bSNamhyung Kim void perf_hpp__reset_output_field(struct perf_hpp_list *list)
6921c89fe9bSNamhyung Kim {
69343e0a68fSJiri Olsa 	struct perf_hpp_fmt *fmt, *tmp;
6941c89fe9bSNamhyung Kim 
6951c89fe9bSNamhyung Kim 	/* reset output fields */
696564132f3SJiri Olsa 	perf_hpp_list__for_each_format_safe(list, fmt, tmp) {
6971c89fe9bSNamhyung Kim 		list_del_init(&fmt->list);
6981c89fe9bSNamhyung Kim 		list_del_init(&fmt->sort_list);
6991c89fe9bSNamhyung Kim 		fmt_free(fmt);
70043e0a68fSJiri Olsa 	}
7011c89fe9bSNamhyung Kim 
7021c89fe9bSNamhyung Kim 	/* reset sort keys */
703564132f3SJiri Olsa 	perf_hpp_list__for_each_sort_list_safe(list, fmt, tmp) {
7041c89fe9bSNamhyung Kim 		list_del_init(&fmt->list);
7051c89fe9bSNamhyung Kim 		list_del_init(&fmt->sort_list);
7061c89fe9bSNamhyung Kim 		fmt_free(fmt);
7077e62ef44SNamhyung Kim 	}
7087e62ef44SNamhyung Kim }
7097e62ef44SNamhyung Kim 
7107e62ef44SNamhyung Kim /*
7117e62ef44SNamhyung Kim  * See hists__fprintf to match the column widths
7121240005eSJiri Olsa  */
hists__sort_list_width(struct hists * hists)713cfaa154bSNamhyung Kim unsigned int hists__sort_list_width(struct hists *hists)
714cfaa154bSNamhyung Kim {
71594a0793dSNamhyung Kim 	struct perf_hpp_fmt *fmt;
7167e62ef44SNamhyung Kim 	int ret = 0;
717f0786af5SJiri Olsa 	bool first = true;
718361459f1SNamhyung Kim 	struct perf_hpp dummy_hpp;
719cfaa154bSNamhyung Kim 
720cfaa154bSNamhyung Kim 	hists__for_each_format(hists, fmt) {
721cfaa154bSNamhyung Kim 		if (perf_hpp__should_skip(fmt, hists))
722cfaa154bSNamhyung Kim 			continue;
723cfaa154bSNamhyung Kim 
7247e62ef44SNamhyung Kim 		if (first)
7257e62ef44SNamhyung Kim 			first = false;
726da1b0407SJiri Olsa 		else
7277e62ef44SNamhyung Kim 			ret += 2;
7287e62ef44SNamhyung Kim 
729bb963e16SNamhyung Kim 		ret += fmt->width(fmt, &dummy_hpp, hists);
7307e62ef44SNamhyung Kim 	}
7317e62ef44SNamhyung Kim 
7327e62ef44SNamhyung Kim 	if (verbose > 0 && hists__has(hists, sym)) /* Addr + origin */
7337e62ef44SNamhyung Kim 		ret += 3 + BITS_PER_LONG / 4;
734e0d66c74SNamhyung Kim 
735a7b5895bSNamhyung Kim 	return ret;
736a7b5895bSNamhyung Kim }
737a7b5895bSNamhyung Kim 
hists__overhead_width(struct hists * hists)738a7b5895bSNamhyung Kim unsigned int hists__overhead_width(struct hists *hists)
739a7b5895bSNamhyung Kim {
740a7b5895bSNamhyung Kim 	struct perf_hpp_fmt *fmt;
741a7b5895bSNamhyung Kim 	int ret = 0;
742a7b5895bSNamhyung Kim 	bool first = true;
743a7b5895bSNamhyung Kim 	struct perf_hpp dummy_hpp;
744a7b5895bSNamhyung Kim 
745a7b5895bSNamhyung Kim 	hists__for_each_format(hists, fmt) {
746a7b5895bSNamhyung Kim 		if (perf_hpp__is_sort_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
747a7b5895bSNamhyung Kim 			break;
748a7b5895bSNamhyung Kim 
749a7b5895bSNamhyung Kim 		if (first)
750a7b5895bSNamhyung Kim 			first = false;
751da1b0407SJiri Olsa 		else
752a7b5895bSNamhyung Kim 			ret += 2;
753a7b5895bSNamhyung Kim 
754a7b5895bSNamhyung Kim 		ret += fmt->width(fmt, &dummy_hpp, hists);
755a7b5895bSNamhyung Kim 	}
756a7b5895bSNamhyung Kim 
757e0d66c74SNamhyung Kim 	return ret;
758e0d66c74SNamhyung Kim }
759e0d66c74SNamhyung Kim 
perf_hpp__reset_width(struct perf_hpp_fmt * fmt,struct hists * hists)760e0d66c74SNamhyung Kim void perf_hpp__reset_width(struct perf_hpp_fmt *fmt, struct hists *hists)
761e0d66c74SNamhyung Kim {
762dd42baf1SNamhyung Kim 	if (perf_hpp__is_sort_entry(fmt))
763dd42baf1SNamhyung Kim 		return perf_hpp__reset_sort_width(fmt, hists);
764dd42baf1SNamhyung Kim 
7652e8b79e7SJiri Olsa 	if (perf_hpp__is_dynamic_entry(fmt))
766e0d66c74SNamhyung Kim 		return;
7672e8b79e7SJiri Olsa 
768e0d66c74SNamhyung Kim 	BUG_ON(fmt->idx >= PERF_HPP__MAX_INDEX);
769e0d66c74SNamhyung Kim 
770e0d66c74SNamhyung Kim 	switch (fmt->idx) {
771e0d66c74SNamhyung Kim 	case PERF_HPP__OVERHEAD:
772e0d66c74SNamhyung Kim 	case PERF_HPP__OVERHEAD_SYS:
773e0d66c74SNamhyung Kim 	case PERF_HPP__OVERHEAD_US:
774e0d66c74SNamhyung Kim 	case PERF_HPP__OVERHEAD_ACC:
775e0d66c74SNamhyung Kim 		fmt->len = 8;
776e0d66c74SNamhyung Kim 		break;
777e0d66c74SNamhyung Kim 
778e0d66c74SNamhyung Kim 	case PERF_HPP__OVERHEAD_GUEST_SYS:
779e0d66c74SNamhyung Kim 	case PERF_HPP__OVERHEAD_GUEST_US:
780e0d66c74SNamhyung Kim 		fmt->len = 9;
781e0d66c74SNamhyung Kim 		break;
782e0d66c74SNamhyung Kim 
783e0d66c74SNamhyung Kim 	case PERF_HPP__SAMPLES:
784e0d66c74SNamhyung Kim 	case PERF_HPP__PERIOD:
785e0d66c74SNamhyung Kim 		fmt->len = 12;
786e0d66c74SNamhyung Kim 		break;
787e0d66c74SNamhyung Kim 
788e0d66c74SNamhyung Kim 	default:
7895b591669SNamhyung Kim 		break;
790e3b60bc9SNamhyung Kim 	}
791e3b60bc9SNamhyung Kim }
792e3b60bc9SNamhyung Kim 
hists__reset_column_width(struct hists * hists)793e3b60bc9SNamhyung Kim void hists__reset_column_width(struct hists *hists)
794e3b60bc9SNamhyung Kim {
795e3b60bc9SNamhyung Kim 	struct perf_hpp_fmt *fmt;
796e3b60bc9SNamhyung Kim 	struct perf_hpp_list_node *node;
797e3b60bc9SNamhyung Kim 
798e3b60bc9SNamhyung Kim 	hists__for_each_format(hists, fmt)
799e3b60bc9SNamhyung Kim 		perf_hpp__reset_width(fmt, hists);
800e3b60bc9SNamhyung Kim 
801e3b60bc9SNamhyung Kim 	/* hierarchy entries have their own hpp list */
802e3b60bc9SNamhyung Kim 	list_for_each_entry(node, &hists->hpp_formats, list) {
803e3b60bc9SNamhyung Kim 		perf_hpp_list__for_each_format(&node->hpp, fmt)
804e3b60bc9SNamhyung Kim 			perf_hpp__reset_width(fmt, hists);
8055b591669SNamhyung Kim 	}
8065b591669SNamhyung Kim }
8075b591669SNamhyung Kim 
perf_hpp__set_user_width(const char * width_list_str)8085b591669SNamhyung Kim void perf_hpp__set_user_width(const char *width_list_str)
8095b591669SNamhyung Kim {
810cf094045SJiri Olsa 	struct perf_hpp_fmt *fmt;
8115b591669SNamhyung Kim 	const char *ptr = width_list_str;
8125b591669SNamhyung Kim 
8135b591669SNamhyung Kim 	perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
8145b591669SNamhyung Kim 		char *p;
8155b591669SNamhyung Kim 
8165b591669SNamhyung Kim 		int len = strtol(ptr, &p, 10);
8175b591669SNamhyung Kim 		fmt->user_len = len;
8185b591669SNamhyung Kim 
8195b591669SNamhyung Kim 		if (*p == ',')
8205b591669SNamhyung Kim 			ptr = p + 1;
8215b591669SNamhyung Kim 		else
822c3bc0c43SNamhyung Kim 			break;
823c3bc0c43SNamhyung Kim 	}
824c3bc0c43SNamhyung Kim }
825c3bc0c43SNamhyung Kim 
add_hierarchy_fmt(struct hists * hists,struct perf_hpp_fmt * fmt)826c3bc0c43SNamhyung Kim static int add_hierarchy_fmt(struct hists *hists, struct perf_hpp_fmt *fmt)
827c3bc0c43SNamhyung Kim {
8281b2dbbf4SNamhyung Kim 	struct perf_hpp_list_node *node = NULL;
829c3bc0c43SNamhyung Kim 	struct perf_hpp_fmt *fmt_copy;
830c3bc0c43SNamhyung Kim 	bool found = false;
831c3bc0c43SNamhyung Kim 	bool skip = perf_hpp__should_skip(fmt, hists);
832c3bc0c43SNamhyung Kim 
833c3bc0c43SNamhyung Kim 	list_for_each_entry(node, &hists->hpp_formats, list) {
834c3bc0c43SNamhyung Kim 		if (node->level == fmt->level) {
835c3bc0c43SNamhyung Kim 			found = true;
836c3bc0c43SNamhyung Kim 			break;
837c3bc0c43SNamhyung Kim 		}
838c3bc0c43SNamhyung Kim 	}
839c3bc0c43SNamhyung Kim 
840c3bc0c43SNamhyung Kim 	if (!found) {
841c3bc0c43SNamhyung Kim 		node = malloc(sizeof(*node));
8421b2dbbf4SNamhyung Kim 		if (node == NULL)
843c3bc0c43SNamhyung Kim 			return -1;
844c3bc0c43SNamhyung Kim 
845c3bc0c43SNamhyung Kim 		node->skip = skip;
8462dbbe9f2SNamhyung Kim 		node->level = fmt->level;
847c3bc0c43SNamhyung Kim 		perf_hpp_list__init(&node->hpp);
848c3bc0c43SNamhyung Kim 
849c3bc0c43SNamhyung Kim 		hists->nr_hpp_node++;
850c3bc0c43SNamhyung Kim 		list_add_tail(&node->list, &hists->hpp_formats);
851c3bc0c43SNamhyung Kim 	}
852c3bc0c43SNamhyung Kim 
853c3bc0c43SNamhyung Kim 	fmt_copy = perf_hpp_fmt__dup(fmt);
8541b2dbbf4SNamhyung Kim 	if (fmt_copy == NULL)
8551b2dbbf4SNamhyung Kim 		return -1;
8561b2dbbf4SNamhyung Kim 
857c3bc0c43SNamhyung Kim 	if (!skip)
858c3bc0c43SNamhyung Kim 		node->skip = false;
859c3bc0c43SNamhyung Kim 
860c3bc0c43SNamhyung Kim 	list_add_tail(&fmt_copy->list, &node->hpp.fields);
861c3bc0c43SNamhyung Kim 	list_add_tail(&fmt_copy->sort_list, &node->hpp.sorts);
862c3bc0c43SNamhyung Kim 
863c3bc0c43SNamhyung Kim 	return 0;
86463503dbaSJiri Olsa }
865c3bc0c43SNamhyung Kim 
perf_hpp__setup_hists_formats(struct perf_hpp_list * list,struct evlist * evlist)86632dcd021SJiri Olsa int perf_hpp__setup_hists_formats(struct perf_hpp_list *list,
867c3bc0c43SNamhyung Kim 				  struct evlist *evlist)
868c3bc0c43SNamhyung Kim {
869c3bc0c43SNamhyung Kim 	struct evsel *evsel;
870c3bc0c43SNamhyung Kim 	struct perf_hpp_fmt *fmt;
871c3bc0c43SNamhyung Kim 	struct hists *hists;
872c3bc0c43SNamhyung Kim 	int ret;
873c3bc0c43SNamhyung Kim 
874e5cadb93SArnaldo Carvalho de Melo 	if (!symbol_conf.report_hierarchy)
875c3bc0c43SNamhyung Kim 		return 0;
876c3bc0c43SNamhyung Kim 
877c3bc0c43SNamhyung Kim 	evlist__for_each_entry(evlist, evsel) {
878c3bc0c43SNamhyung Kim 		hists = evsel__hists(evsel);
879c3bc0c43SNamhyung Kim 
880c3bc0c43SNamhyung Kim 		perf_hpp_list__for_each_sort_list(list, fmt) {
881c3bc0c43SNamhyung Kim 			if (perf_hpp__is_dynamic_entry(fmt) &&
882c3bc0c43SNamhyung Kim 			    !perf_hpp__defined_dynamic_entry(fmt, hists))
883c3bc0c43SNamhyung Kim 				continue;
884c3bc0c43SNamhyung Kim 
885c3bc0c43SNamhyung Kim 			ret = add_hierarchy_fmt(hists, fmt);
886c3bc0c43SNamhyung Kim 			if (ret < 0)
887c3bc0c43SNamhyung Kim 				return ret;
888c3bc0c43SNamhyung Kim 		}
889c3bc0c43SNamhyung Kim 	}
890 
891 	return 0;
892 }
893