xref: /openbmc/linux/tools/perf/ui/hist.c (revision f6005caf)
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*f6005cafSIan Rogers #include "../util/thread.h"
15ea0c5239SIan Rogers #include "../util/util.h"
16ea251d51SNamhyung Kim 
17ea251d51SNamhyung Kim /* hist period print (hpp) functions */
18ea251d51SNamhyung 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 })
25a0088adcSNamhyung 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)265b591669SNamhyung Kim static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he,
27d675107cSNamhyung Kim 		      hpp_field_fn get_field, const char *fmt, int len,
28fb821c9eSNamhyung Kim 		      hpp_snprint_fn print_fn, bool fmt_percent)
29ea251d51SNamhyung Kim {
30fb821c9eSNamhyung Kim 	int ret;
31b5ff71c3SJiri Olsa 	struct hists *hists = he->hists;
3232dcd021SJiri Olsa 	struct evsel *evsel = hists_to_evsel(hists);
33a0088adcSNamhyung Kim 	char *buf = hpp->buf;
34a0088adcSNamhyung Kim 	size_t size = hpp->size;
35ea251d51SNamhyung Kim 
360c5268bfSJiri Olsa 	if (fmt_percent) {
370c5268bfSJiri Olsa 		double percent = 0.0;
38f2148330SNamhyung Kim 		u64 total = hists__total_period(hists);
390c5268bfSJiri Olsa 
40f2148330SNamhyung Kim 		if (total)
41f2148330SNamhyung Kim 			percent = 100.0 * get_field(he) / total;
424fb71074SNamhyung Kim 
43d675107cSNamhyung Kim 		ret = hpp__call_print_fn(hpp, print_fn, fmt, len, percent);
440c5268bfSJiri Olsa 	} else
45d675107cSNamhyung Kim 		ret = hpp__call_print_fn(hpp, print_fn, fmt, len, get_field(he));
465b9e2146SNamhyung Kim 
47c754c382SArnaldo Carvalho de Melo 	if (evsel__is_group_event(evsel)) {
485b9e2146SNamhyung Kim 		int prev_idx, idx_delta;
495b9e2146SNamhyung Kim 		struct hist_entry *pair;
505643b1a5SJiri Olsa 		int nr_members = evsel->core.nr_members;
515b9e2146SNamhyung Kim 
522bb72dbbSArnaldo Carvalho de Melo 		prev_idx = evsel__group_idx(evsel);
535b9e2146SNamhyung Kim 
545b9e2146SNamhyung Kim 		list_for_each_entry(pair, &he->pairs.head, pairs.node) {
555b9e2146SNamhyung Kim 			u64 period = get_field(pair);
56f2148330SNamhyung Kim 			u64 total = hists__total_period(pair->hists);
575b9e2146SNamhyung Kim 
585b9e2146SNamhyung Kim 			if (!total)
595b9e2146SNamhyung Kim 				continue;
605b9e2146SNamhyung Kim 
615b9e2146SNamhyung Kim 			evsel = hists_to_evsel(pair->hists);
622bb72dbbSArnaldo Carvalho de Melo 			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
685b9e2146SNamhyung Kim 				 */
699b0d2fb8SNamhyung Kim 				if (fmt_percent) {
70a0088adcSNamhyung Kim 					ret += hpp__call_print_fn(hpp, print_fn,
71d675107cSNamhyung Kim 								  fmt, len, 0.0);
729b0d2fb8SNamhyung Kim 				} else {
73a0088adcSNamhyung Kim 					ret += hpp__call_print_fn(hpp, print_fn,
74d675107cSNamhyung Kim 								  fmt, len, 0ULL);
759b0d2fb8SNamhyung Kim 				}
765b9e2146SNamhyung Kim 			}
775b9e2146SNamhyung Kim 
78a0088adcSNamhyung Kim 			if (fmt_percent) {
79d675107cSNamhyung Kim 				ret += hpp__call_print_fn(hpp, print_fn, fmt, len,
80a0088adcSNamhyung Kim 							  100.0 * period / total);
81a0088adcSNamhyung Kim 			} else {
82a0088adcSNamhyung Kim 				ret += hpp__call_print_fn(hpp, print_fn, fmt,
83d675107cSNamhyung Kim 							  len, period);
84a0088adcSNamhyung Kim 			}
855b9e2146SNamhyung Kim 
862bb72dbbSArnaldo Carvalho de Melo 			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
945b9e2146SNamhyung Kim 			 */
959b0d2fb8SNamhyung Kim 			if (fmt_percent) {
96a0088adcSNamhyung Kim 				ret += hpp__call_print_fn(hpp, print_fn,
97d675107cSNamhyung Kim 							  fmt, len, 0.0);
989b0d2fb8SNamhyung Kim 			} else {
99a0088adcSNamhyung Kim 				ret += hpp__call_print_fn(hpp, print_fn,
100d675107cSNamhyung Kim 							  fmt, len, 0ULL);
1019b0d2fb8SNamhyung Kim 			}
1025b9e2146SNamhyung Kim 		}
1035b9e2146SNamhyung 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;
111a0088adcSNamhyung Kim 
1124fb71074SNamhyung Kim 	return ret;
113ea251d51SNamhyung Kim }
114ea251d51SNamhyung 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,
1175b591669SNamhyung Kim 	     const char *fmtstr, hpp_snprint_fn print_fn, bool fmt_percent)
118594dcbf3SNamhyung 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,
1235b591669SNamhyung Kim 				  print_fn, fmt_percent);
124594dcbf3SNamhyung Kim 	}
125594dcbf3SNamhyung 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 
1435b591669SNamhyung Kim 	return hpp__fmt(fmt, hpp, he, get_field, fmtstr, print_fn, fmt_percent);
144594dcbf3SNamhyung Kim }
145594dcbf3SNamhyung 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 }
154f156d84eSNamhyung Kim 
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) {
167429a5f9dSJin Yao 		struct evsel *evsel = hists_to_evsel(pair->hists);
1682bb72dbbSArnaldo Carvalho de Melo 		fa[evsel__group_idx(evsel)] = get_field(pair);
169429a5f9dSJin Yao 	}
170429a5f9dSJin Yao 
171429a5f9dSJin Yao 	list_for_each_entry(pair, &b->pairs.head, pairs.node) {
172429a5f9dSJin Yao 		struct evsel *evsel = hists_to_evsel(pair->hists);
1732bb72dbbSArnaldo Carvalho de Melo 		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 
193429a5f9dSJin Yao 	cmp = field_cmp(get_field(a), get_field(b));
194c754c382SArnaldo Carvalho de Melo 	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 }
225429a5f9dSJin Yao 
__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;
230f156d84eSNamhyung Kim 	int i, nr_members;
23132dcd021SJiri Olsa 	struct evsel *evsel;
232f156d84eSNamhyung Kim 	u64 *fields_a, *fields_b;
233f156d84eSNamhyung Kim 
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 	}
238429a5f9dSJin Yao 
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 
243f156d84eSNamhyung Kim 	evsel = hists_to_evsel(a->hists);
244c754c382SArnaldo Carvalho de Melo 	if (!evsel__is_group_event(evsel))
245f156d84eSNamhyung Kim 		return ret;
246f156d84eSNamhyung Kim 
2475643b1a5SJiri Olsa 	nr_members = evsel->core.nr_members;
248429a5f9dSJin Yao 	i = hist_entry__new_pair(a, b, get_field, nr_members, &fields_a, &fields_b);
249429a5f9dSJin Yao 	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 }
264f156d84eSNamhyung 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;
277594dcbf3SNamhyung Kim 
278*f6005cafSIan Rogers 		if ((a->thread == NULL ? NULL : RC_CHK_ACCESS(a->thread)) !=
279*f6005cafSIan Rogers 		    (b->thread == NULL ? NULL : RC_CHK_ACCESS(b->thread)) ||
280*f6005cafSIan Rogers 		    !hist_entry__has_callchains(a) || !symbol_conf.use_callchain)
2815ca82710SNamhyung Kim 			return 0;
2825ca82710SNamhyung Kim 
283594dcbf3SNamhyung Kim 		ret = b->callchain->max_depth - a->callchain->max_depth;
2847111ffffSNamhyung Kim 		if (callchain_param.order == ORDER_CALLER)
2857111ffffSNamhyung Kim 			ret = -ret;
286594dcbf3SNamhyung Kim 	}
287594dcbf3SNamhyung Kim 	return ret;
288594dcbf3SNamhyung Kim }
289594dcbf3SNamhyung Kim 
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,
292da1b0407SJiri Olsa 			 struct hists *hists)
2931ecd4453SNamhyung Kim {
2941ecd4453SNamhyung Kim 	int len = fmt->user_len ?: fmt->len;
29532dcd021SJiri Olsa 	struct evsel *evsel = hists_to_evsel(hists);
2961ecd4453SNamhyung Kim 
2971ecd4453SNamhyung Kim 	if (symbol_conf.event_group)
2985643b1a5SJiri Olsa 		len = max(len, evsel->core.nr_members * fmt->len);
2991ecd4453SNamhyung Kim 
3001ecd4453SNamhyung Kim 	if (len < (int)strlen(fmt->name))
3011ecd4453SNamhyung Kim 		len = strlen(fmt->name);
3021ecd4453SNamhyung Kim 
3031ecd4453SNamhyung Kim 	return len;
304ea251d51SNamhyung Kim }
305ea251d51SNamhyung Kim 
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,
30729659ab4SJiri Olsa 			  struct hists *hists, int line __maybe_unused,
30829659ab4SJiri Olsa 			  int *span __maybe_unused)
3091ecd4453SNamhyung Kim {
310da1b0407SJiri Olsa 	int len = hpp__width_fn(fmt, hpp, hists);
3111ecd4453SNamhyung Kim 	return scnprintf(hpp->buf, hpp->size, "%*s", len, fmt->name);
312e0d66c74SNamhyung Kim }
313e0d66c74SNamhyung Kim 
hpp_color_scnprintf(struct perf_hpp * hpp,const char * fmt,...)31498ba1609SJiri Olsa int hpp_color_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
315a0088adcSNamhyung Kim {
316a0088adcSNamhyung Kim 	va_list args;
317a0088adcSNamhyung Kim 	ssize_t ssize = hpp->size;
318a0088adcSNamhyung Kim 	double percent;
319d675107cSNamhyung Kim 	int ret, len;
320a0088adcSNamhyung Kim 
321a0088adcSNamhyung Kim 	va_start(args, fmt);
322d675107cSNamhyung Kim 	len = va_arg(args, int);
323a0088adcSNamhyung Kim 	percent = va_arg(args, double);
324d675107cSNamhyung 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 
340a0088adcSNamhyung Kim 	return (ret >= ssize) ? (ssize - 1) : ret;
341a0088adcSNamhyung Kim }
342a0088adcSNamhyung Kim 
3434fb71074SNamhyung Kim #define __HPP_COLOR_PERCENT_FN(_type, _field)					\
3444fb71074SNamhyung Kim static u64 he_get_##_field(struct hist_entry *he)				\
3454fb71074SNamhyung Kim {										\
3464fb71074SNamhyung Kim 	return he->stat._field;							\
3474fb71074SNamhyung Kim }										\
3484fb71074SNamhyung Kim 										\
349e0d66c74SNamhyung Kim static int hpp__color_##_type(struct perf_hpp_fmt *fmt,				\
3502c5d4b4aSJiri Olsa 			      struct perf_hpp *hpp, struct hist_entry *he) 	\
3514fb71074SNamhyung Kim {										\
3525b591669SNamhyung Kim 	return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%",		\
353a0088adcSNamhyung Kim 			hpp_color_scnprintf, true);				\
354ea251d51SNamhyung Kim }
355ea251d51SNamhyung Kim 
3564fb71074SNamhyung Kim #define __HPP_ENTRY_PERCENT_FN(_type, _field)					\
357e0d66c74SNamhyung Kim static int hpp__entry_##_type(struct perf_hpp_fmt *fmt,				\
3582c5d4b4aSJiri Olsa 			      struct perf_hpp *hpp, struct hist_entry *he) 	\
3594fb71074SNamhyung Kim {										\
3605b591669SNamhyung Kim 	return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%",		\
361a0088adcSNamhyung Kim 			hpp_entry_scnprintf, true);				\
362ea251d51SNamhyung Kim }
363ea251d51SNamhyung Kim 
364bc18b7f2SNamhyung Kim #define __HPP_SORT_FN(_type, _field)						\
36587bbdf76SNamhyung Kim static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, 	\
36687bbdf76SNamhyung Kim 				 struct hist_entry *a, struct hist_entry *b) 	\
367bc18b7f2SNamhyung Kim {										\
368f156d84eSNamhyung Kim 	return __hpp__sort(a, b, he_get_##_field);				\
369bc18b7f2SNamhyung Kim }
370bc18b7f2SNamhyung 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 {										\
374594dcbf3SNamhyung Kim 	return he->stat_acc->_field;						\
375594dcbf3SNamhyung Kim }										\
376594dcbf3SNamhyung Kim 										\
377e0d66c74SNamhyung Kim static int hpp__color_##_type(struct perf_hpp_fmt *fmt,				\
378594dcbf3SNamhyung Kim 			      struct perf_hpp *hpp, struct hist_entry *he) 	\
379594dcbf3SNamhyung Kim {										\
3805b591669SNamhyung Kim 	return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", 	\
381594dcbf3SNamhyung Kim 			    hpp_color_scnprintf, true);				\
382594dcbf3SNamhyung Kim }
383594dcbf3SNamhyung Kim 
384594dcbf3SNamhyung Kim #define __HPP_ENTRY_ACC_PERCENT_FN(_type, _field)				\
385e0d66c74SNamhyung Kim static int hpp__entry_##_type(struct perf_hpp_fmt *fmt,				\
386594dcbf3SNamhyung Kim 			      struct perf_hpp *hpp, struct hist_entry *he) 	\
387594dcbf3SNamhyung Kim {										\
3882bfa1528SNamhyung Kim 	return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%",	\
389594dcbf3SNamhyung Kim 			    hpp_entry_scnprintf, true);				\
390594dcbf3SNamhyung Kim }
391594dcbf3SNamhyung Kim 
392594dcbf3SNamhyung Kim #define __HPP_SORT_ACC_FN(_type, _field)					\
39387bbdf76SNamhyung Kim static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, 	\
39487bbdf76SNamhyung Kim 				 struct hist_entry *a, struct hist_entry *b) 	\
395594dcbf3SNamhyung Kim {										\
396594dcbf3SNamhyung Kim 	return __hpp__sort_acc(a, b, he_get_acc_##_field);			\
397594dcbf3SNamhyung Kim }
398594dcbf3SNamhyung Kim 
3994fb71074SNamhyung Kim #define __HPP_ENTRY_RAW_FN(_type, _field)					\
4004fb71074SNamhyung Kim static u64 he_get_raw_##_field(struct hist_entry *he)				\
4014fb71074SNamhyung Kim {										\
4024fb71074SNamhyung Kim 	return he->stat._field;							\
4034fb71074SNamhyung Kim }										\
4044fb71074SNamhyung Kim 										\
405e0d66c74SNamhyung Kim static int hpp__entry_##_type(struct perf_hpp_fmt *fmt,				\
4062c5d4b4aSJiri Olsa 			      struct perf_hpp *hpp, struct hist_entry *he) 	\
4074fb71074SNamhyung Kim {										\
4085b591669SNamhyung Kim 	return hpp__fmt(fmt, hpp, he, he_get_raw_##_field, " %*"PRIu64, 	\
409a0088adcSNamhyung Kim 			hpp_entry_scnprintf, false);				\
410ea251d51SNamhyung Kim }
411ea251d51SNamhyung Kim 
412bc18b7f2SNamhyung Kim #define __HPP_SORT_RAW_FN(_type, _field)					\
41387bbdf76SNamhyung Kim static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, 	\
41487bbdf76SNamhyung Kim 				 struct hist_entry *a, struct hist_entry *b) 	\
415bc18b7f2SNamhyung Kim {										\
416f156d84eSNamhyung Kim 	return __hpp__sort(a, b, he_get_raw_##_field);				\
417bc18b7f2SNamhyung Kim }
418bc18b7f2SNamhyung Kim 
419bc18b7f2SNamhyung Kim 
4201ecd4453SNamhyung Kim #define HPP_PERCENT_FNS(_type, _field)					\
4214fb71074SNamhyung Kim __HPP_COLOR_PERCENT_FN(_type, _field)					\
422bc18b7f2SNamhyung Kim __HPP_ENTRY_PERCENT_FN(_type, _field)					\
423bc18b7f2SNamhyung Kim __HPP_SORT_FN(_type, _field)
424ea251d51SNamhyung Kim 
4251ecd4453SNamhyung Kim #define HPP_PERCENT_ACC_FNS(_type, _field)				\
426594dcbf3SNamhyung Kim __HPP_COLOR_ACC_PERCENT_FN(_type, _field)				\
427594dcbf3SNamhyung Kim __HPP_ENTRY_ACC_PERCENT_FN(_type, _field)				\
428594dcbf3SNamhyung Kim __HPP_SORT_ACC_FN(_type, _field)
429594dcbf3SNamhyung Kim 
4301ecd4453SNamhyung Kim #define HPP_RAW_FNS(_type, _field)					\
431bc18b7f2SNamhyung Kim __HPP_ENTRY_RAW_FN(_type, _field)					\
432bc18b7f2SNamhyung Kim __HPP_SORT_RAW_FN(_type, _field)
433b5ff71c3SJiri Olsa 
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)
4371ecd4453SNamhyung 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 
4411ecd4453SNamhyung Kim HPP_RAW_FNS(samples, nr_events)
4421ecd4453SNamhyung Kim HPP_RAW_FNS(period, period)
443ea251d51SNamhyung Kim 
44487bbdf76SNamhyung Kim static int64_t hpp__nop_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
44587bbdf76SNamhyung Kim 			    struct hist_entry *a __maybe_unused,
446bc18b7f2SNamhyung Kim 			    struct hist_entry *b __maybe_unused)
447bc18b7f2SNamhyung Kim {
448bc18b7f2SNamhyung Kim 	return 0;
449bc18b7f2SNamhyung Kim }
450bc18b7f2SNamhyung Kim 
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 
461c0020efaSJiri Olsa 	return a->idx == b->idx;
462c0020efaSJiri Olsa }
463c0020efaSJiri Olsa 
464b21a763eSJiri Olsa #define HPP__COLOR_PRINT_FNS(_name, _fn, _idx)		\
4651240005eSJiri Olsa 	{						\
4661ecd4453SNamhyung Kim 		.name   = _name,			\
4671ecd4453SNamhyung Kim 		.header	= hpp__header_fn,		\
4681ecd4453SNamhyung Kim 		.width	= hpp__width_fn,		\
4691ecd4453SNamhyung Kim 		.color	= hpp__color_ ## _fn,		\
4701ecd4453SNamhyung Kim 		.entry	= hpp__entry_ ## _fn,		\
471bc18b7f2SNamhyung Kim 		.cmp	= hpp__nop_cmp,			\
472bc18b7f2SNamhyung Kim 		.collapse = hpp__nop_cmp,		\
4731ecd4453SNamhyung Kim 		.sort	= hpp__sort_ ## _fn,		\
474b21a763eSJiri Olsa 		.idx	= PERF_HPP__ ## _idx,		\
475c0020efaSJiri Olsa 		.equal	= hpp__equal,			\
4761240005eSJiri Olsa 	}
477ea251d51SNamhyung Kim 
478b21a763eSJiri Olsa #define HPP__COLOR_ACC_PRINT_FNS(_name, _fn, _idx)	\
479594dcbf3SNamhyung Kim 	{						\
4801ecd4453SNamhyung Kim 		.name   = _name,			\
4811ecd4453SNamhyung Kim 		.header	= hpp__header_fn,		\
4821ecd4453SNamhyung Kim 		.width	= hpp__width_fn,		\
4831ecd4453SNamhyung Kim 		.color	= hpp__color_ ## _fn,		\
4841ecd4453SNamhyung Kim 		.entry	= hpp__entry_ ## _fn,		\
485594dcbf3SNamhyung Kim 		.cmp	= hpp__nop_cmp,			\
486594dcbf3SNamhyung Kim 		.collapse = hpp__nop_cmp,		\
4871ecd4453SNamhyung Kim 		.sort	= hpp__sort_ ## _fn,		\
488b21a763eSJiri Olsa 		.idx	= PERF_HPP__ ## _idx,		\
489c0020efaSJiri Olsa 		.equal	= hpp__equal,			\
490594dcbf3SNamhyung Kim 	}
491594dcbf3SNamhyung Kim 
492b21a763eSJiri Olsa #define HPP__PRINT_FNS(_name, _fn, _idx)		\
4931240005eSJiri Olsa 	{						\
4941ecd4453SNamhyung Kim 		.name   = _name,			\
4951ecd4453SNamhyung Kim 		.header	= hpp__header_fn,		\
4961ecd4453SNamhyung Kim 		.width	= hpp__width_fn,		\
4971ecd4453SNamhyung Kim 		.entry	= hpp__entry_ ## _fn,		\
498bc18b7f2SNamhyung Kim 		.cmp	= hpp__nop_cmp,			\
499bc18b7f2SNamhyung Kim 		.collapse = hpp__nop_cmp,		\
5001ecd4453SNamhyung Kim 		.sort	= hpp__sort_ ## _fn,		\
501b21a763eSJiri Olsa 		.idx	= PERF_HPP__ ## _idx,		\
502c0020efaSJiri Olsa 		.equal	= hpp__equal,			\
5031240005eSJiri Olsa 	}
504ea251d51SNamhyung Kim 
505ea251d51SNamhyung Kim 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),
511b21a763eSJiri Olsa 	HPP__COLOR_ACC_PRINT_FNS("Children", overhead_acc, OVERHEAD_ACC),
512b21a763eSJiri Olsa 	HPP__PRINT_FNS("Samples", samples, SAMPLES),
513b21a763eSJiri Olsa 	HPP__PRINT_FNS("Period", period, PERIOD)
514ea251d51SNamhyung Kim };
515ea251d51SNamhyung Kim 
5167c31e102SJiri Olsa struct perf_hpp_list perf_hpp_list = {
5177c31e102SJiri Olsa 	.fields	= LIST_HEAD_INIT(perf_hpp_list.fields),
5187c31e102SJiri Olsa 	.sorts	= LIST_HEAD_INIT(perf_hpp_list.sorts),
519f8e6710dSJiri Olsa 	.nr_header_lines = 1,
5207c31e102SJiri Olsa };
5214fb71074SNamhyung Kim 
522ea251d51SNamhyung Kim #undef HPP__COLOR_PRINT_FNS
523594dcbf3SNamhyung Kim #undef HPP__COLOR_ACC_PRINT_FNS
524ea251d51SNamhyung Kim #undef HPP__PRINT_FNS
525ea251d51SNamhyung Kim 
5264fb71074SNamhyung Kim #undef HPP_PERCENT_FNS
527594dcbf3SNamhyung Kim #undef HPP_PERCENT_ACC_FNS
5284fb71074SNamhyung Kim #undef HPP_RAW_FNS
5294fb71074SNamhyung Kim 
5304fb71074SNamhyung Kim #undef __HPP_HEADER_FN
5314fb71074SNamhyung Kim #undef __HPP_WIDTH_FN
5324fb71074SNamhyung 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
5364fb71074SNamhyung Kim #undef __HPP_ENTRY_RAW_FN
537594dcbf3SNamhyung Kim #undef __HPP_SORT_FN
538594dcbf3SNamhyung Kim #undef __HPP_SORT_ACC_FN
539594dcbf3SNamhyung Kim #undef __HPP_SORT_RAW_FN
5404fb71074SNamhyung Kim 
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 
5500ca1f534SIan Rogers 	if (fmt->free)
5510ca1f534SIan Rogers 		fmt->free(fmt);
5520ca1f534SIan Rogers }
5534fb71074SNamhyung Kim 
perf_hpp__init(void)5541d77822eSJiri Olsa void perf_hpp__init(void)
555ea251d51SNamhyung Kim {
55626d8b338SNamhyung Kim 	int i;
55726d8b338SNamhyung Kim 
55826d8b338SNamhyung 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 
563a2ce067eSNamhyung Kim 		/* sort_list may be linked by setup_sorting() */
564a2ce067eSNamhyung Kim 		if (fmt->sort_list.next == NULL)
565a2ce067eSNamhyung Kim 			INIT_LIST_HEAD(&fmt->sort_list);
56626d8b338SNamhyung Kim 	}
56726d8b338SNamhyung Kim 
568a7d945bcSNamhyung Kim 	/*
569a7d945bcSNamhyung Kim 	 * If user specified field order, no need to setup default fields.
570a7d945bcSNamhyung Kim 	 */
5712f3f9bcfSJiri Olsa 	if (is_strict_order(field_order))
572a7d945bcSNamhyung Kim 		return;
573a7d945bcSNamhyung Kim 
574594dcbf3SNamhyung Kim 	if (symbol_conf.cumulate_callchain) {
5751178bfd4SJiri Olsa 		hpp_dimension__add_output(PERF_HPP__OVERHEAD_ACC);
5761ecd4453SNamhyung Kim 		perf_hpp__format[PERF_HPP__OVERHEAD].name = "Self";
577594dcbf3SNamhyung Kim 	}
578594dcbf3SNamhyung Kim 
5791178bfd4SJiri Olsa 	hpp_dimension__add_output(PERF_HPP__OVERHEAD);
5802b8bfa6bSJiri Olsa 
581ea251d51SNamhyung Kim 	if (symbol_conf.show_cpu_utilization) {
5821178bfd4SJiri Olsa 		hpp_dimension__add_output(PERF_HPP__OVERHEAD_SYS);
5831178bfd4SJiri Olsa 		hpp_dimension__add_output(PERF_HPP__OVERHEAD_US);
584ea251d51SNamhyung Kim 
585ea251d51SNamhyung Kim 		if (perf_guest) {
5861178bfd4SJiri Olsa 			hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_SYS);
5871178bfd4SJiri Olsa 			hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_US);
588ea251d51SNamhyung Kim 		}
589ea251d51SNamhyung Kim 	}
590ea251d51SNamhyung Kim 
591ea251d51SNamhyung Kim 	if (symbol_conf.show_nr_samples)
5921178bfd4SJiri Olsa 		hpp_dimension__add_output(PERF_HPP__SAMPLES);
593ea251d51SNamhyung Kim 
594ea251d51SNamhyung Kim 	if (symbol_conf.show_total_period)
5951178bfd4SJiri Olsa 		hpp_dimension__add_output(PERF_HPP__PERIOD);
596ea251d51SNamhyung Kim }
5971d77822eSJiri 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)
6001240005eSJiri 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,
605ebdd98e0SJiri Olsa 					struct perf_hpp_fmt *format)
606ebdd98e0SJiri Olsa {
607ebdd98e0SJiri Olsa 	list_add_tail(&format->sort_list, &list->sorts);
6081240005eSJiri Olsa }
6091240005eSJiri Olsa 
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 {
613a1c9f97fSNamhyung Kim 	list_add(&format->sort_list, &list->sorts);
614a1c9f97fSNamhyung Kim }
615a1c9f97fSNamhyung Kim 
perf_hpp__column_unregister(struct perf_hpp_fmt * format)6160ca1f534SIan Rogers static void perf_hpp__column_unregister(struct perf_hpp_fmt *format)
61777284de3SNamhyung Kim {
61870b01dfdSJiri Olsa 	list_del_init(&format->list);
6190ca1f534SIan Rogers 	fmt_free(format);
62077284de3SNamhyung Kim }
62177284de3SNamhyung Kim 
perf_hpp__cancel_cumulate(void)62277284de3SNamhyung Kim void perf_hpp__cancel_cumulate(void)
62377284de3SNamhyung Kim {
6241945c3e7SJiri Olsa 	struct perf_hpp_fmt *fmt, *acc, *ovh, *tmp;
6251945c3e7SJiri Olsa 
6262f3f9bcfSJiri Olsa 	if (is_strict_order(field_order))
6272bf1a123SNamhyung Kim 		return;
6282bf1a123SNamhyung Kim 
6291945c3e7SJiri Olsa 	ovh = &perf_hpp__format[PERF_HPP__OVERHEAD];
6301945c3e7SJiri Olsa 	acc = &perf_hpp__format[PERF_HPP__OVERHEAD_ACC];
6311945c3e7SJiri Olsa 
6327a1799e0SJiri 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 
6381945c3e7SJiri Olsa 		if (ovh->equal(ovh, fmt))
6391945c3e7SJiri Olsa 			fmt->name = "Overhead";
6401945c3e7SJiri Olsa 	}
64177284de3SNamhyung Kim }
64277284de3SNamhyung Kim 
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 {
64597358084SJiri Olsa 	return a->equal && a->equal(a, b);
64697358084SJiri Olsa }
64797358084SJiri Olsa 
perf_hpp__setup_output_field(struct perf_hpp_list * list)64843e0a68fSJiri Olsa void perf_hpp__setup_output_field(struct perf_hpp_list *list)
64926d8b338SNamhyung Kim {
65026d8b338SNamhyung Kim 	struct perf_hpp_fmt *fmt;
65126d8b338SNamhyung Kim 
65226d8b338SNamhyung Kim 	/* append sort keys to output field */
65343e0a68fSJiri Olsa 	perf_hpp_list__for_each_sort_list(list, fmt) {
654a7d945bcSNamhyung Kim 		struct perf_hpp_fmt *pos;
655a7d945bcSNamhyung Kim 
6568381cdd0SNamhyung Kim 		/* skip sort-only fields ("sort_compute" in perf diff) */
6578381cdd0SNamhyung Kim 		if (!fmt->entry && !fmt->color)
6588381cdd0SNamhyung Kim 			continue;
6598381cdd0SNamhyung Kim 
66043e0a68fSJiri Olsa 		perf_hpp_list__for_each_format(list, pos) {
66197358084SJiri Olsa 			if (fmt_equal(fmt, pos))
662a7d945bcSNamhyung Kim 				goto next;
663a7d945bcSNamhyung Kim 		}
664a7d945bcSNamhyung Kim 
66526d8b338SNamhyung Kim 		perf_hpp__column_register(fmt);
666a7d945bcSNamhyung Kim next:
667a7d945bcSNamhyung Kim 		continue;
668a7d945bcSNamhyung Kim 	}
669a7d945bcSNamhyung Kim }
670a7d945bcSNamhyung Kim 
perf_hpp__append_sort_keys(struct perf_hpp_list * list)67143e0a68fSJiri Olsa void perf_hpp__append_sort_keys(struct perf_hpp_list *list)
672a7d945bcSNamhyung Kim {
673a7d945bcSNamhyung Kim 	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) {
677a7d945bcSNamhyung Kim 		struct perf_hpp_fmt *pos;
678a7d945bcSNamhyung Kim 
67943e0a68fSJiri Olsa 		perf_hpp_list__for_each_sort_list(list, pos) {
68097358084SJiri Olsa 			if (fmt_equal(fmt, pos))
681a7d945bcSNamhyung Kim 				goto next;
682a7d945bcSNamhyung Kim 		}
683a7d945bcSNamhyung Kim 
684a7d945bcSNamhyung Kim 		perf_hpp__register_sort_field(fmt);
685a7d945bcSNamhyung Kim next:
686a7d945bcSNamhyung Kim 		continue;
68726d8b338SNamhyung Kim 	}
68826d8b338SNamhyung Kim }
68926d8b338SNamhyung Kim 
69043e0a68fSJiri Olsa 
perf_hpp__reset_output_field(struct perf_hpp_list * list)69143e0a68fSJiri Olsa void perf_hpp__reset_output_field(struct perf_hpp_list *list)
6921c89fe9bSNamhyung Kim {
6931c89fe9bSNamhyung Kim 	struct perf_hpp_fmt *fmt, *tmp;
6941c89fe9bSNamhyung Kim 
6951c89fe9bSNamhyung Kim 	/* reset output fields */
69643e0a68fSJiri 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);
699564132f3SJiri Olsa 		fmt_free(fmt);
7001c89fe9bSNamhyung Kim 	}
7011c89fe9bSNamhyung Kim 
7021c89fe9bSNamhyung Kim 	/* reset sort keys */
70343e0a68fSJiri 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);
706564132f3SJiri Olsa 		fmt_free(fmt);
7071c89fe9bSNamhyung Kim 	}
7081c89fe9bSNamhyung Kim }
7091c89fe9bSNamhyung Kim 
7107e62ef44SNamhyung Kim /*
7117e62ef44SNamhyung Kim  * See hists__fprintf to match the column widths
7127e62ef44SNamhyung Kim  */
hists__sort_list_width(struct hists * hists)7137e62ef44SNamhyung Kim unsigned int hists__sort_list_width(struct hists *hists)
7147e62ef44SNamhyung Kim {
7151240005eSJiri Olsa 	struct perf_hpp_fmt *fmt;
716cfaa154bSNamhyung Kim 	int ret = 0;
717cfaa154bSNamhyung Kim 	bool first = true;
71894a0793dSNamhyung Kim 	struct perf_hpp dummy_hpp;
7197e62ef44SNamhyung Kim 
720f0786af5SJiri Olsa 	hists__for_each_format(hists, fmt) {
721361459f1SNamhyung Kim 		if (perf_hpp__should_skip(fmt, hists))
722cfaa154bSNamhyung Kim 			continue;
723cfaa154bSNamhyung Kim 
724cfaa154bSNamhyung Kim 		if (first)
725cfaa154bSNamhyung Kim 			first = false;
726cfaa154bSNamhyung Kim 		else
7277e62ef44SNamhyung Kim 			ret += 2;
7287e62ef44SNamhyung Kim 
729da1b0407SJiri Olsa 		ret += fmt->width(fmt, &dummy_hpp, hists);
7307e62ef44SNamhyung Kim 	}
7317e62ef44SNamhyung Kim 
732bb963e16SNamhyung Kim 	if (verbose > 0 && hists__has(hists, sym)) /* Addr + origin */
7337e62ef44SNamhyung Kim 		ret += 3 + BITS_PER_LONG / 4;
7347e62ef44SNamhyung Kim 
7357e62ef44SNamhyung Kim 	return ret;
7367e62ef44SNamhyung Kim }
737e0d66c74SNamhyung 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;
751a7b5895bSNamhyung Kim 		else
752a7b5895bSNamhyung Kim 			ret += 2;
753a7b5895bSNamhyung Kim 
754da1b0407SJiri Olsa 		ret += fmt->width(fmt, &dummy_hpp, hists);
755a7b5895bSNamhyung Kim 	}
756a7b5895bSNamhyung Kim 
757a7b5895bSNamhyung Kim 	return ret;
758a7b5895bSNamhyung Kim }
759a7b5895bSNamhyung 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 {
762e0d66c74SNamhyung Kim 	if (perf_hpp__is_sort_entry(fmt))
763e0d66c74SNamhyung Kim 		return perf_hpp__reset_sort_width(fmt, hists);
764e0d66c74SNamhyung Kim 
765dd42baf1SNamhyung Kim 	if (perf_hpp__is_dynamic_entry(fmt))
766dd42baf1SNamhyung Kim 		return;
767dd42baf1SNamhyung Kim 
7682e8b79e7SJiri Olsa 	BUG_ON(fmt->idx >= PERF_HPP__MAX_INDEX);
769e0d66c74SNamhyung Kim 
7702e8b79e7SJiri Olsa 	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:
789e0d66c74SNamhyung Kim 		break;
790e0d66c74SNamhyung Kim 	}
791e0d66c74SNamhyung Kim }
7925b591669SNamhyung 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);
805e3b60bc9SNamhyung Kim 	}
806e3b60bc9SNamhyung Kim }
807e3b60bc9SNamhyung 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 {
8105b591669SNamhyung Kim 	struct perf_hpp_fmt *fmt;
8115b591669SNamhyung Kim 	const char *ptr = width_list_str;
8125b591669SNamhyung Kim 
813cf094045SJiri Olsa 	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
8225b591669SNamhyung Kim 			break;
8235b591669SNamhyung Kim 	}
8245b591669SNamhyung 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 {
828c3bc0c43SNamhyung Kim 	struct perf_hpp_list_node *node = NULL;
829c3bc0c43SNamhyung Kim 	struct perf_hpp_fmt *fmt_copy;
830c3bc0c43SNamhyung Kim 	bool found = false;
8311b2dbbf4SNamhyung 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));
842c3bc0c43SNamhyung Kim 		if (node == NULL)
843c3bc0c43SNamhyung Kim 			return -1;
844c3bc0c43SNamhyung Kim 
8451b2dbbf4SNamhyung Kim 		node->skip = skip;
846c3bc0c43SNamhyung Kim 		node->level = fmt->level;
847c3bc0c43SNamhyung Kim 		perf_hpp_list__init(&node->hpp);
848c3bc0c43SNamhyung Kim 
8492dbbe9f2SNamhyung 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);
854c3bc0c43SNamhyung Kim 	if (fmt_copy == NULL)
855c3bc0c43SNamhyung Kim 		return -1;
856c3bc0c43SNamhyung Kim 
8571b2dbbf4SNamhyung Kim 	if (!skip)
8581b2dbbf4SNamhyung Kim 		node->skip = false;
8591b2dbbf4SNamhyung 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;
864c3bc0c43SNamhyung Kim }
865c3bc0c43SNamhyung Kim 
perf_hpp__setup_hists_formats(struct perf_hpp_list * list,struct evlist * evlist)866c3bc0c43SNamhyung Kim int perf_hpp__setup_hists_formats(struct perf_hpp_list *list,
86763503dbaSJiri Olsa 				  struct evlist *evlist)
868c3bc0c43SNamhyung Kim {
86932dcd021SJiri Olsa 	struct evsel *evsel;
870c3bc0c43SNamhyung Kim 	struct perf_hpp_fmt *fmt;
871c3bc0c43SNamhyung Kim 	struct hists *hists;
872c3bc0c43SNamhyung Kim 	int ret;
873c3bc0c43SNamhyung Kim 
874c3bc0c43SNamhyung Kim 	if (!symbol_conf.report_hierarchy)
875c3bc0c43SNamhyung Kim 		return 0;
876c3bc0c43SNamhyung Kim 
877e5cadb93SArnaldo Carvalho de Melo 	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 	}
890c3bc0c43SNamhyung Kim 
891c3bc0c43SNamhyung Kim 	return 0;
892c3bc0c43SNamhyung Kim }
893