18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <inttypes.h>
38c2ecf20Sopenharmony_ci#include <math.h>
48c2ecf20Sopenharmony_ci#include <stdlib.h>
58c2ecf20Sopenharmony_ci#include <string.h>
68c2ecf20Sopenharmony_ci#include <linux/compiler.h>
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include "../util/callchain.h"
98c2ecf20Sopenharmony_ci#include "../util/debug.h"
108c2ecf20Sopenharmony_ci#include "../util/hist.h"
118c2ecf20Sopenharmony_ci#include "../util/sort.h"
128c2ecf20Sopenharmony_ci#include "../util/evsel.h"
138c2ecf20Sopenharmony_ci#include "../util/evlist.h"
148c2ecf20Sopenharmony_ci#include "../perf.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/* hist period print (hpp) functions */
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define hpp__call_print_fn(hpp, fn, fmt, ...)			\
198c2ecf20Sopenharmony_ci({								\
208c2ecf20Sopenharmony_ci	int __ret = fn(hpp, fmt, ##__VA_ARGS__);		\
218c2ecf20Sopenharmony_ci	advance_hpp(hpp, __ret);				\
228c2ecf20Sopenharmony_ci	__ret;							\
238c2ecf20Sopenharmony_ci})
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he,
268c2ecf20Sopenharmony_ci		      hpp_field_fn get_field, const char *fmt, int len,
278c2ecf20Sopenharmony_ci		      hpp_snprint_fn print_fn, bool fmt_percent)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	int ret;
308c2ecf20Sopenharmony_ci	struct hists *hists = he->hists;
318c2ecf20Sopenharmony_ci	struct evsel *evsel = hists_to_evsel(hists);
328c2ecf20Sopenharmony_ci	char *buf = hpp->buf;
338c2ecf20Sopenharmony_ci	size_t size = hpp->size;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	if (fmt_percent) {
368c2ecf20Sopenharmony_ci		double percent = 0.0;
378c2ecf20Sopenharmony_ci		u64 total = hists__total_period(hists);
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci		if (total)
408c2ecf20Sopenharmony_ci			percent = 100.0 * get_field(he) / total;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci		ret = hpp__call_print_fn(hpp, print_fn, fmt, len, percent);
438c2ecf20Sopenharmony_ci	} else
448c2ecf20Sopenharmony_ci		ret = hpp__call_print_fn(hpp, print_fn, fmt, len, get_field(he));
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	if (evsel__is_group_event(evsel)) {
478c2ecf20Sopenharmony_ci		int prev_idx, idx_delta;
488c2ecf20Sopenharmony_ci		struct hist_entry *pair;
498c2ecf20Sopenharmony_ci		int nr_members = evsel->core.nr_members;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci		prev_idx = evsel__group_idx(evsel);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci		list_for_each_entry(pair, &he->pairs.head, pairs.node) {
548c2ecf20Sopenharmony_ci			u64 period = get_field(pair);
558c2ecf20Sopenharmony_ci			u64 total = hists__total_period(pair->hists);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci			if (!total)
588c2ecf20Sopenharmony_ci				continue;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci			evsel = hists_to_evsel(pair->hists);
618c2ecf20Sopenharmony_ci			idx_delta = evsel__group_idx(evsel) - prev_idx - 1;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci			while (idx_delta--) {
648c2ecf20Sopenharmony_ci				/*
658c2ecf20Sopenharmony_ci				 * zero-fill group members in the middle which
668c2ecf20Sopenharmony_ci				 * have no sample
678c2ecf20Sopenharmony_ci				 */
688c2ecf20Sopenharmony_ci				if (fmt_percent) {
698c2ecf20Sopenharmony_ci					ret += hpp__call_print_fn(hpp, print_fn,
708c2ecf20Sopenharmony_ci								  fmt, len, 0.0);
718c2ecf20Sopenharmony_ci				} else {
728c2ecf20Sopenharmony_ci					ret += hpp__call_print_fn(hpp, print_fn,
738c2ecf20Sopenharmony_ci								  fmt, len, 0ULL);
748c2ecf20Sopenharmony_ci				}
758c2ecf20Sopenharmony_ci			}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci			if (fmt_percent) {
788c2ecf20Sopenharmony_ci				ret += hpp__call_print_fn(hpp, print_fn, fmt, len,
798c2ecf20Sopenharmony_ci							  100.0 * period / total);
808c2ecf20Sopenharmony_ci			} else {
818c2ecf20Sopenharmony_ci				ret += hpp__call_print_fn(hpp, print_fn, fmt,
828c2ecf20Sopenharmony_ci							  len, period);
838c2ecf20Sopenharmony_ci			}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci			prev_idx = evsel__group_idx(evsel);
868c2ecf20Sopenharmony_ci		}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci		idx_delta = nr_members - prev_idx - 1;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci		while (idx_delta--) {
918c2ecf20Sopenharmony_ci			/*
928c2ecf20Sopenharmony_ci			 * zero-fill group members at last which have no sample
938c2ecf20Sopenharmony_ci			 */
948c2ecf20Sopenharmony_ci			if (fmt_percent) {
958c2ecf20Sopenharmony_ci				ret += hpp__call_print_fn(hpp, print_fn,
968c2ecf20Sopenharmony_ci							  fmt, len, 0.0);
978c2ecf20Sopenharmony_ci			} else {
988c2ecf20Sopenharmony_ci				ret += hpp__call_print_fn(hpp, print_fn,
998c2ecf20Sopenharmony_ci							  fmt, len, 0ULL);
1008c2ecf20Sopenharmony_ci			}
1018c2ecf20Sopenharmony_ci		}
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	/*
1058c2ecf20Sopenharmony_ci	 * Restore original buf and size as it's where caller expects
1068c2ecf20Sopenharmony_ci	 * the result will be saved.
1078c2ecf20Sopenharmony_ci	 */
1088c2ecf20Sopenharmony_ci	hpp->buf = buf;
1098c2ecf20Sopenharmony_ci	hpp->size = size;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	return ret;
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ciint hpp__fmt(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1158c2ecf20Sopenharmony_ci	     struct hist_entry *he, hpp_field_fn get_field,
1168c2ecf20Sopenharmony_ci	     const char *fmtstr, hpp_snprint_fn print_fn, bool fmt_percent)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	int len = fmt->user_len ?: fmt->len;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	if (symbol_conf.field_sep) {
1218c2ecf20Sopenharmony_ci		return __hpp__fmt(hpp, he, get_field, fmtstr, 1,
1228c2ecf20Sopenharmony_ci				  print_fn, fmt_percent);
1238c2ecf20Sopenharmony_ci	}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	if (fmt_percent)
1268c2ecf20Sopenharmony_ci		len -= 2; /* 2 for a space and a % sign */
1278c2ecf20Sopenharmony_ci	else
1288c2ecf20Sopenharmony_ci		len -= 1;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	return  __hpp__fmt(hpp, he, get_field, fmtstr, len, print_fn, fmt_percent);
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ciint hpp__fmt_acc(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
1348c2ecf20Sopenharmony_ci		 struct hist_entry *he, hpp_field_fn get_field,
1358c2ecf20Sopenharmony_ci		 const char *fmtstr, hpp_snprint_fn print_fn, bool fmt_percent)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	if (!symbol_conf.cumulate_callchain) {
1388c2ecf20Sopenharmony_ci		int len = fmt->user_len ?: fmt->len;
1398c2ecf20Sopenharmony_ci		return snprintf(hpp->buf, hpp->size, " %*s", len - 1, "N/A");
1408c2ecf20Sopenharmony_ci	}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	return hpp__fmt(fmt, hpp, he, get_field, fmtstr, print_fn, fmt_percent);
1438c2ecf20Sopenharmony_ci}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cistatic int field_cmp(u64 field_a, u64 field_b)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	if (field_a > field_b)
1488c2ecf20Sopenharmony_ci		return 1;
1498c2ecf20Sopenharmony_ci	if (field_a < field_b)
1508c2ecf20Sopenharmony_ci		return -1;
1518c2ecf20Sopenharmony_ci	return 0;
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic int hist_entry__new_pair(struct hist_entry *a, struct hist_entry *b,
1558c2ecf20Sopenharmony_ci				hpp_field_fn get_field, int nr_members,
1568c2ecf20Sopenharmony_ci				u64 **fields_a, u64 **fields_b)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	u64 *fa = calloc(nr_members, sizeof(*fa)),
1598c2ecf20Sopenharmony_ci	    *fb = calloc(nr_members, sizeof(*fb));
1608c2ecf20Sopenharmony_ci	struct hist_entry *pair;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	if (!fa || !fb)
1638c2ecf20Sopenharmony_ci		goto out_free;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	list_for_each_entry(pair, &a->pairs.head, pairs.node) {
1668c2ecf20Sopenharmony_ci		struct evsel *evsel = hists_to_evsel(pair->hists);
1678c2ecf20Sopenharmony_ci		fa[evsel__group_idx(evsel)] = get_field(pair);
1688c2ecf20Sopenharmony_ci	}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	list_for_each_entry(pair, &b->pairs.head, pairs.node) {
1718c2ecf20Sopenharmony_ci		struct evsel *evsel = hists_to_evsel(pair->hists);
1728c2ecf20Sopenharmony_ci		fb[evsel__group_idx(evsel)] = get_field(pair);
1738c2ecf20Sopenharmony_ci	}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	*fields_a = fa;
1768c2ecf20Sopenharmony_ci	*fields_b = fb;
1778c2ecf20Sopenharmony_ci	return 0;
1788c2ecf20Sopenharmony_ciout_free:
1798c2ecf20Sopenharmony_ci	free(fa);
1808c2ecf20Sopenharmony_ci	free(fb);
1818c2ecf20Sopenharmony_ci	*fields_a = *fields_b = NULL;
1828c2ecf20Sopenharmony_ci	return -1;
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistatic int __hpp__group_sort_idx(struct hist_entry *a, struct hist_entry *b,
1868c2ecf20Sopenharmony_ci				 hpp_field_fn get_field, int idx)
1878c2ecf20Sopenharmony_ci{
1888c2ecf20Sopenharmony_ci	struct evsel *evsel = hists_to_evsel(a->hists);
1898c2ecf20Sopenharmony_ci	u64 *fields_a, *fields_b;
1908c2ecf20Sopenharmony_ci	int cmp, nr_members, ret, i;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	cmp = field_cmp(get_field(a), get_field(b));
1938c2ecf20Sopenharmony_ci	if (!evsel__is_group_event(evsel))
1948c2ecf20Sopenharmony_ci		return cmp;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	nr_members = evsel->core.nr_members;
1978c2ecf20Sopenharmony_ci	if (idx < 1 || idx >= nr_members)
1988c2ecf20Sopenharmony_ci		return cmp;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	ret = hist_entry__new_pair(a, b, get_field, nr_members, &fields_a, &fields_b);
2018c2ecf20Sopenharmony_ci	if (ret) {
2028c2ecf20Sopenharmony_ci		ret = cmp;
2038c2ecf20Sopenharmony_ci		goto out;
2048c2ecf20Sopenharmony_ci	}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	ret = field_cmp(fields_a[idx], fields_b[idx]);
2078c2ecf20Sopenharmony_ci	if (ret)
2088c2ecf20Sopenharmony_ci		goto out;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	for (i = 1; i < nr_members; i++) {
2118c2ecf20Sopenharmony_ci		if (i != idx) {
2128c2ecf20Sopenharmony_ci			ret = field_cmp(fields_a[i], fields_b[i]);
2138c2ecf20Sopenharmony_ci			if (ret)
2148c2ecf20Sopenharmony_ci				goto out;
2158c2ecf20Sopenharmony_ci		}
2168c2ecf20Sopenharmony_ci	}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ciout:
2198c2ecf20Sopenharmony_ci	free(fields_a);
2208c2ecf20Sopenharmony_ci	free(fields_b);
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	return ret;
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_cistatic int __hpp__sort(struct hist_entry *a, struct hist_entry *b,
2268c2ecf20Sopenharmony_ci		       hpp_field_fn get_field)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	s64 ret;
2298c2ecf20Sopenharmony_ci	int i, nr_members;
2308c2ecf20Sopenharmony_ci	struct evsel *evsel;
2318c2ecf20Sopenharmony_ci	u64 *fields_a, *fields_b;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	if (symbol_conf.group_sort_idx && symbol_conf.event_group) {
2348c2ecf20Sopenharmony_ci		return __hpp__group_sort_idx(a, b, get_field,
2358c2ecf20Sopenharmony_ci					     symbol_conf.group_sort_idx);
2368c2ecf20Sopenharmony_ci	}
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	ret = field_cmp(get_field(a), get_field(b));
2398c2ecf20Sopenharmony_ci	if (ret || !symbol_conf.event_group)
2408c2ecf20Sopenharmony_ci		return ret;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	evsel = hists_to_evsel(a->hists);
2438c2ecf20Sopenharmony_ci	if (!evsel__is_group_event(evsel))
2448c2ecf20Sopenharmony_ci		return ret;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	nr_members = evsel->core.nr_members;
2478c2ecf20Sopenharmony_ci	i = hist_entry__new_pair(a, b, get_field, nr_members, &fields_a, &fields_b);
2488c2ecf20Sopenharmony_ci	if (i)
2498c2ecf20Sopenharmony_ci		goto out;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	for (i = 1; i < nr_members; i++) {
2528c2ecf20Sopenharmony_ci		ret = field_cmp(fields_a[i], fields_b[i]);
2538c2ecf20Sopenharmony_ci		if (ret)
2548c2ecf20Sopenharmony_ci			break;
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ciout:
2588c2ecf20Sopenharmony_ci	free(fields_a);
2598c2ecf20Sopenharmony_ci	free(fields_b);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	return ret;
2628c2ecf20Sopenharmony_ci}
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_cistatic int __hpp__sort_acc(struct hist_entry *a, struct hist_entry *b,
2658c2ecf20Sopenharmony_ci			   hpp_field_fn get_field)
2668c2ecf20Sopenharmony_ci{
2678c2ecf20Sopenharmony_ci	s64 ret = 0;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	if (symbol_conf.cumulate_callchain) {
2708c2ecf20Sopenharmony_ci		/*
2718c2ecf20Sopenharmony_ci		 * Put caller above callee when they have equal period.
2728c2ecf20Sopenharmony_ci		 */
2738c2ecf20Sopenharmony_ci		ret = field_cmp(get_field(a), get_field(b));
2748c2ecf20Sopenharmony_ci		if (ret)
2758c2ecf20Sopenharmony_ci			return ret;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci		if (a->thread != b->thread || !hist_entry__has_callchains(a) || !symbol_conf.use_callchain)
2788c2ecf20Sopenharmony_ci			return 0;
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci		ret = b->callchain->max_depth - a->callchain->max_depth;
2818c2ecf20Sopenharmony_ci		if (callchain_param.order == ORDER_CALLER)
2828c2ecf20Sopenharmony_ci			ret = -ret;
2838c2ecf20Sopenharmony_ci	}
2848c2ecf20Sopenharmony_ci	return ret;
2858c2ecf20Sopenharmony_ci}
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_cistatic int hpp__width_fn(struct perf_hpp_fmt *fmt,
2888c2ecf20Sopenharmony_ci			 struct perf_hpp *hpp __maybe_unused,
2898c2ecf20Sopenharmony_ci			 struct hists *hists)
2908c2ecf20Sopenharmony_ci{
2918c2ecf20Sopenharmony_ci	int len = fmt->user_len ?: fmt->len;
2928c2ecf20Sopenharmony_ci	struct evsel *evsel = hists_to_evsel(hists);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	if (symbol_conf.event_group)
2958c2ecf20Sopenharmony_ci		len = max(len, evsel->core.nr_members * fmt->len);
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	if (len < (int)strlen(fmt->name))
2988c2ecf20Sopenharmony_ci		len = strlen(fmt->name);
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	return len;
3018c2ecf20Sopenharmony_ci}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_cistatic int hpp__header_fn(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
3048c2ecf20Sopenharmony_ci			  struct hists *hists, int line __maybe_unused,
3058c2ecf20Sopenharmony_ci			  int *span __maybe_unused)
3068c2ecf20Sopenharmony_ci{
3078c2ecf20Sopenharmony_ci	int len = hpp__width_fn(fmt, hpp, hists);
3088c2ecf20Sopenharmony_ci	return scnprintf(hpp->buf, hpp->size, "%*s", len, fmt->name);
3098c2ecf20Sopenharmony_ci}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ciint hpp_color_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
3128c2ecf20Sopenharmony_ci{
3138c2ecf20Sopenharmony_ci	va_list args;
3148c2ecf20Sopenharmony_ci	ssize_t ssize = hpp->size;
3158c2ecf20Sopenharmony_ci	double percent;
3168c2ecf20Sopenharmony_ci	int ret, len;
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	va_start(args, fmt);
3198c2ecf20Sopenharmony_ci	len = va_arg(args, int);
3208c2ecf20Sopenharmony_ci	percent = va_arg(args, double);
3218c2ecf20Sopenharmony_ci	ret = percent_color_len_snprintf(hpp->buf, hpp->size, fmt, len, percent);
3228c2ecf20Sopenharmony_ci	va_end(args);
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	return (ret >= ssize) ? (ssize - 1) : ret;
3258c2ecf20Sopenharmony_ci}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_cistatic int hpp_entry_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	va_list args;
3308c2ecf20Sopenharmony_ci	ssize_t ssize = hpp->size;
3318c2ecf20Sopenharmony_ci	int ret;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	va_start(args, fmt);
3348c2ecf20Sopenharmony_ci	ret = vsnprintf(hpp->buf, hpp->size, fmt, args);
3358c2ecf20Sopenharmony_ci	va_end(args);
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	return (ret >= ssize) ? (ssize - 1) : ret;
3388c2ecf20Sopenharmony_ci}
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci#define __HPP_COLOR_PERCENT_FN(_type, _field)					\
3418c2ecf20Sopenharmony_cistatic u64 he_get_##_field(struct hist_entry *he)				\
3428c2ecf20Sopenharmony_ci{										\
3438c2ecf20Sopenharmony_ci	return he->stat._field;							\
3448c2ecf20Sopenharmony_ci}										\
3458c2ecf20Sopenharmony_ci										\
3468c2ecf20Sopenharmony_cistatic int hpp__color_##_type(struct perf_hpp_fmt *fmt,				\
3478c2ecf20Sopenharmony_ci			      struct perf_hpp *hpp, struct hist_entry *he) 	\
3488c2ecf20Sopenharmony_ci{										\
3498c2ecf20Sopenharmony_ci	return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%",		\
3508c2ecf20Sopenharmony_ci			hpp_color_scnprintf, true);				\
3518c2ecf20Sopenharmony_ci}
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci#define __HPP_ENTRY_PERCENT_FN(_type, _field)					\
3548c2ecf20Sopenharmony_cistatic int hpp__entry_##_type(struct perf_hpp_fmt *fmt,				\
3558c2ecf20Sopenharmony_ci			      struct perf_hpp *hpp, struct hist_entry *he) 	\
3568c2ecf20Sopenharmony_ci{										\
3578c2ecf20Sopenharmony_ci	return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%",		\
3588c2ecf20Sopenharmony_ci			hpp_entry_scnprintf, true);				\
3598c2ecf20Sopenharmony_ci}
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci#define __HPP_SORT_FN(_type, _field)						\
3628c2ecf20Sopenharmony_cistatic int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, 	\
3638c2ecf20Sopenharmony_ci				 struct hist_entry *a, struct hist_entry *b) 	\
3648c2ecf20Sopenharmony_ci{										\
3658c2ecf20Sopenharmony_ci	return __hpp__sort(a, b, he_get_##_field);				\
3668c2ecf20Sopenharmony_ci}
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci#define __HPP_COLOR_ACC_PERCENT_FN(_type, _field)				\
3698c2ecf20Sopenharmony_cistatic u64 he_get_acc_##_field(struct hist_entry *he)				\
3708c2ecf20Sopenharmony_ci{										\
3718c2ecf20Sopenharmony_ci	return he->stat_acc->_field;						\
3728c2ecf20Sopenharmony_ci}										\
3738c2ecf20Sopenharmony_ci										\
3748c2ecf20Sopenharmony_cistatic int hpp__color_##_type(struct perf_hpp_fmt *fmt,				\
3758c2ecf20Sopenharmony_ci			      struct perf_hpp *hpp, struct hist_entry *he) 	\
3768c2ecf20Sopenharmony_ci{										\
3778c2ecf20Sopenharmony_ci	return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", 	\
3788c2ecf20Sopenharmony_ci			    hpp_color_scnprintf, true);				\
3798c2ecf20Sopenharmony_ci}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci#define __HPP_ENTRY_ACC_PERCENT_FN(_type, _field)				\
3828c2ecf20Sopenharmony_cistatic int hpp__entry_##_type(struct perf_hpp_fmt *fmt,				\
3838c2ecf20Sopenharmony_ci			      struct perf_hpp *hpp, struct hist_entry *he) 	\
3848c2ecf20Sopenharmony_ci{										\
3858c2ecf20Sopenharmony_ci	return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%",	\
3868c2ecf20Sopenharmony_ci			    hpp_entry_scnprintf, true);				\
3878c2ecf20Sopenharmony_ci}
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci#define __HPP_SORT_ACC_FN(_type, _field)					\
3908c2ecf20Sopenharmony_cistatic int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, 	\
3918c2ecf20Sopenharmony_ci				 struct hist_entry *a, struct hist_entry *b) 	\
3928c2ecf20Sopenharmony_ci{										\
3938c2ecf20Sopenharmony_ci	return __hpp__sort_acc(a, b, he_get_acc_##_field);			\
3948c2ecf20Sopenharmony_ci}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci#define __HPP_ENTRY_RAW_FN(_type, _field)					\
3978c2ecf20Sopenharmony_cistatic u64 he_get_raw_##_field(struct hist_entry *he)				\
3988c2ecf20Sopenharmony_ci{										\
3998c2ecf20Sopenharmony_ci	return he->stat._field;							\
4008c2ecf20Sopenharmony_ci}										\
4018c2ecf20Sopenharmony_ci										\
4028c2ecf20Sopenharmony_cistatic int hpp__entry_##_type(struct perf_hpp_fmt *fmt,				\
4038c2ecf20Sopenharmony_ci			      struct perf_hpp *hpp, struct hist_entry *he) 	\
4048c2ecf20Sopenharmony_ci{										\
4058c2ecf20Sopenharmony_ci	return hpp__fmt(fmt, hpp, he, he_get_raw_##_field, " %*"PRIu64, 	\
4068c2ecf20Sopenharmony_ci			hpp_entry_scnprintf, false);				\
4078c2ecf20Sopenharmony_ci}
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci#define __HPP_SORT_RAW_FN(_type, _field)					\
4108c2ecf20Sopenharmony_cistatic int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, 	\
4118c2ecf20Sopenharmony_ci				 struct hist_entry *a, struct hist_entry *b) 	\
4128c2ecf20Sopenharmony_ci{										\
4138c2ecf20Sopenharmony_ci	return __hpp__sort(a, b, he_get_raw_##_field);				\
4148c2ecf20Sopenharmony_ci}
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci#define HPP_PERCENT_FNS(_type, _field)					\
4188c2ecf20Sopenharmony_ci__HPP_COLOR_PERCENT_FN(_type, _field)					\
4198c2ecf20Sopenharmony_ci__HPP_ENTRY_PERCENT_FN(_type, _field)					\
4208c2ecf20Sopenharmony_ci__HPP_SORT_FN(_type, _field)
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci#define HPP_PERCENT_ACC_FNS(_type, _field)				\
4238c2ecf20Sopenharmony_ci__HPP_COLOR_ACC_PERCENT_FN(_type, _field)				\
4248c2ecf20Sopenharmony_ci__HPP_ENTRY_ACC_PERCENT_FN(_type, _field)				\
4258c2ecf20Sopenharmony_ci__HPP_SORT_ACC_FN(_type, _field)
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci#define HPP_RAW_FNS(_type, _field)					\
4288c2ecf20Sopenharmony_ci__HPP_ENTRY_RAW_FN(_type, _field)					\
4298c2ecf20Sopenharmony_ci__HPP_SORT_RAW_FN(_type, _field)
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ciHPP_PERCENT_FNS(overhead, period)
4328c2ecf20Sopenharmony_ciHPP_PERCENT_FNS(overhead_sys, period_sys)
4338c2ecf20Sopenharmony_ciHPP_PERCENT_FNS(overhead_us, period_us)
4348c2ecf20Sopenharmony_ciHPP_PERCENT_FNS(overhead_guest_sys, period_guest_sys)
4358c2ecf20Sopenharmony_ciHPP_PERCENT_FNS(overhead_guest_us, period_guest_us)
4368c2ecf20Sopenharmony_ciHPP_PERCENT_ACC_FNS(overhead_acc, period)
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ciHPP_RAW_FNS(samples, nr_events)
4398c2ecf20Sopenharmony_ciHPP_RAW_FNS(period, period)
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_cistatic int64_t hpp__nop_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
4428c2ecf20Sopenharmony_ci			    struct hist_entry *a __maybe_unused,
4438c2ecf20Sopenharmony_ci			    struct hist_entry *b __maybe_unused)
4448c2ecf20Sopenharmony_ci{
4458c2ecf20Sopenharmony_ci	return 0;
4468c2ecf20Sopenharmony_ci}
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_cistatic bool perf_hpp__is_hpp_entry(struct perf_hpp_fmt *a)
4498c2ecf20Sopenharmony_ci{
4508c2ecf20Sopenharmony_ci	return a->header == hpp__header_fn;
4518c2ecf20Sopenharmony_ci}
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_cistatic bool hpp__equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
4548c2ecf20Sopenharmony_ci{
4558c2ecf20Sopenharmony_ci	if (!perf_hpp__is_hpp_entry(a) || !perf_hpp__is_hpp_entry(b))
4568c2ecf20Sopenharmony_ci		return false;
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	return a->idx == b->idx;
4598c2ecf20Sopenharmony_ci}
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci#define HPP__COLOR_PRINT_FNS(_name, _fn, _idx)		\
4628c2ecf20Sopenharmony_ci	{						\
4638c2ecf20Sopenharmony_ci		.name   = _name,			\
4648c2ecf20Sopenharmony_ci		.header	= hpp__header_fn,		\
4658c2ecf20Sopenharmony_ci		.width	= hpp__width_fn,		\
4668c2ecf20Sopenharmony_ci		.color	= hpp__color_ ## _fn,		\
4678c2ecf20Sopenharmony_ci		.entry	= hpp__entry_ ## _fn,		\
4688c2ecf20Sopenharmony_ci		.cmp	= hpp__nop_cmp,			\
4698c2ecf20Sopenharmony_ci		.collapse = hpp__nop_cmp,		\
4708c2ecf20Sopenharmony_ci		.sort	= hpp__sort_ ## _fn,		\
4718c2ecf20Sopenharmony_ci		.idx	= PERF_HPP__ ## _idx,		\
4728c2ecf20Sopenharmony_ci		.equal	= hpp__equal,			\
4738c2ecf20Sopenharmony_ci	}
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci#define HPP__COLOR_ACC_PRINT_FNS(_name, _fn, _idx)	\
4768c2ecf20Sopenharmony_ci	{						\
4778c2ecf20Sopenharmony_ci		.name   = _name,			\
4788c2ecf20Sopenharmony_ci		.header	= hpp__header_fn,		\
4798c2ecf20Sopenharmony_ci		.width	= hpp__width_fn,		\
4808c2ecf20Sopenharmony_ci		.color	= hpp__color_ ## _fn,		\
4818c2ecf20Sopenharmony_ci		.entry	= hpp__entry_ ## _fn,		\
4828c2ecf20Sopenharmony_ci		.cmp	= hpp__nop_cmp,			\
4838c2ecf20Sopenharmony_ci		.collapse = hpp__nop_cmp,		\
4848c2ecf20Sopenharmony_ci		.sort	= hpp__sort_ ## _fn,		\
4858c2ecf20Sopenharmony_ci		.idx	= PERF_HPP__ ## _idx,		\
4868c2ecf20Sopenharmony_ci		.equal	= hpp__equal,			\
4878c2ecf20Sopenharmony_ci	}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci#define HPP__PRINT_FNS(_name, _fn, _idx)		\
4908c2ecf20Sopenharmony_ci	{						\
4918c2ecf20Sopenharmony_ci		.name   = _name,			\
4928c2ecf20Sopenharmony_ci		.header	= hpp__header_fn,		\
4938c2ecf20Sopenharmony_ci		.width	= hpp__width_fn,		\
4948c2ecf20Sopenharmony_ci		.entry	= hpp__entry_ ## _fn,		\
4958c2ecf20Sopenharmony_ci		.cmp	= hpp__nop_cmp,			\
4968c2ecf20Sopenharmony_ci		.collapse = hpp__nop_cmp,		\
4978c2ecf20Sopenharmony_ci		.sort	= hpp__sort_ ## _fn,		\
4988c2ecf20Sopenharmony_ci		.idx	= PERF_HPP__ ## _idx,		\
4998c2ecf20Sopenharmony_ci		.equal	= hpp__equal,			\
5008c2ecf20Sopenharmony_ci	}
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_cistruct perf_hpp_fmt perf_hpp__format[] = {
5038c2ecf20Sopenharmony_ci	HPP__COLOR_PRINT_FNS("Overhead", overhead, OVERHEAD),
5048c2ecf20Sopenharmony_ci	HPP__COLOR_PRINT_FNS("sys", overhead_sys, OVERHEAD_SYS),
5058c2ecf20Sopenharmony_ci	HPP__COLOR_PRINT_FNS("usr", overhead_us, OVERHEAD_US),
5068c2ecf20Sopenharmony_ci	HPP__COLOR_PRINT_FNS("guest sys", overhead_guest_sys, OVERHEAD_GUEST_SYS),
5078c2ecf20Sopenharmony_ci	HPP__COLOR_PRINT_FNS("guest usr", overhead_guest_us, OVERHEAD_GUEST_US),
5088c2ecf20Sopenharmony_ci	HPP__COLOR_ACC_PRINT_FNS("Children", overhead_acc, OVERHEAD_ACC),
5098c2ecf20Sopenharmony_ci	HPP__PRINT_FNS("Samples", samples, SAMPLES),
5108c2ecf20Sopenharmony_ci	HPP__PRINT_FNS("Period", period, PERIOD)
5118c2ecf20Sopenharmony_ci};
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_cistruct perf_hpp_list perf_hpp_list = {
5148c2ecf20Sopenharmony_ci	.fields	= LIST_HEAD_INIT(perf_hpp_list.fields),
5158c2ecf20Sopenharmony_ci	.sorts	= LIST_HEAD_INIT(perf_hpp_list.sorts),
5168c2ecf20Sopenharmony_ci	.nr_header_lines = 1,
5178c2ecf20Sopenharmony_ci};
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci#undef HPP__COLOR_PRINT_FNS
5208c2ecf20Sopenharmony_ci#undef HPP__COLOR_ACC_PRINT_FNS
5218c2ecf20Sopenharmony_ci#undef HPP__PRINT_FNS
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci#undef HPP_PERCENT_FNS
5248c2ecf20Sopenharmony_ci#undef HPP_PERCENT_ACC_FNS
5258c2ecf20Sopenharmony_ci#undef HPP_RAW_FNS
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci#undef __HPP_HEADER_FN
5288c2ecf20Sopenharmony_ci#undef __HPP_WIDTH_FN
5298c2ecf20Sopenharmony_ci#undef __HPP_COLOR_PERCENT_FN
5308c2ecf20Sopenharmony_ci#undef __HPP_ENTRY_PERCENT_FN
5318c2ecf20Sopenharmony_ci#undef __HPP_COLOR_ACC_PERCENT_FN
5328c2ecf20Sopenharmony_ci#undef __HPP_ENTRY_ACC_PERCENT_FN
5338c2ecf20Sopenharmony_ci#undef __HPP_ENTRY_RAW_FN
5348c2ecf20Sopenharmony_ci#undef __HPP_SORT_FN
5358c2ecf20Sopenharmony_ci#undef __HPP_SORT_ACC_FN
5368c2ecf20Sopenharmony_ci#undef __HPP_SORT_RAW_FN
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_cistatic void fmt_free(struct perf_hpp_fmt *fmt)
5398c2ecf20Sopenharmony_ci{
5408c2ecf20Sopenharmony_ci	/*
5418c2ecf20Sopenharmony_ci	 * At this point fmt should be completely
5428c2ecf20Sopenharmony_ci	 * unhooked, if not it's a bug.
5438c2ecf20Sopenharmony_ci	 */
5448c2ecf20Sopenharmony_ci	BUG_ON(!list_empty(&fmt->list));
5458c2ecf20Sopenharmony_ci	BUG_ON(!list_empty(&fmt->sort_list));
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	if (fmt->free)
5488c2ecf20Sopenharmony_ci		fmt->free(fmt);
5498c2ecf20Sopenharmony_ci}
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_civoid perf_hpp__init(void)
5528c2ecf20Sopenharmony_ci{
5538c2ecf20Sopenharmony_ci	int i;
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
5568c2ecf20Sopenharmony_ci		struct perf_hpp_fmt *fmt = &perf_hpp__format[i];
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci		INIT_LIST_HEAD(&fmt->list);
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci		/* sort_list may be linked by setup_sorting() */
5618c2ecf20Sopenharmony_ci		if (fmt->sort_list.next == NULL)
5628c2ecf20Sopenharmony_ci			INIT_LIST_HEAD(&fmt->sort_list);
5638c2ecf20Sopenharmony_ci	}
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	/*
5668c2ecf20Sopenharmony_ci	 * If user specified field order, no need to setup default fields.
5678c2ecf20Sopenharmony_ci	 */
5688c2ecf20Sopenharmony_ci	if (is_strict_order(field_order))
5698c2ecf20Sopenharmony_ci		return;
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	if (symbol_conf.cumulate_callchain) {
5728c2ecf20Sopenharmony_ci		hpp_dimension__add_output(PERF_HPP__OVERHEAD_ACC);
5738c2ecf20Sopenharmony_ci		perf_hpp__format[PERF_HPP__OVERHEAD].name = "Self";
5748c2ecf20Sopenharmony_ci	}
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	hpp_dimension__add_output(PERF_HPP__OVERHEAD);
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci	if (symbol_conf.show_cpu_utilization) {
5798c2ecf20Sopenharmony_ci		hpp_dimension__add_output(PERF_HPP__OVERHEAD_SYS);
5808c2ecf20Sopenharmony_ci		hpp_dimension__add_output(PERF_HPP__OVERHEAD_US);
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci		if (perf_guest) {
5838c2ecf20Sopenharmony_ci			hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_SYS);
5848c2ecf20Sopenharmony_ci			hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_US);
5858c2ecf20Sopenharmony_ci		}
5868c2ecf20Sopenharmony_ci	}
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	if (symbol_conf.show_nr_samples)
5898c2ecf20Sopenharmony_ci		hpp_dimension__add_output(PERF_HPP__SAMPLES);
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	if (symbol_conf.show_total_period)
5928c2ecf20Sopenharmony_ci		hpp_dimension__add_output(PERF_HPP__PERIOD);
5938c2ecf20Sopenharmony_ci}
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_civoid perf_hpp_list__column_register(struct perf_hpp_list *list,
5968c2ecf20Sopenharmony_ci				    struct perf_hpp_fmt *format)
5978c2ecf20Sopenharmony_ci{
5988c2ecf20Sopenharmony_ci	list_add_tail(&format->list, &list->fields);
5998c2ecf20Sopenharmony_ci}
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_civoid perf_hpp_list__register_sort_field(struct perf_hpp_list *list,
6028c2ecf20Sopenharmony_ci					struct perf_hpp_fmt *format)
6038c2ecf20Sopenharmony_ci{
6048c2ecf20Sopenharmony_ci	list_add_tail(&format->sort_list, &list->sorts);
6058c2ecf20Sopenharmony_ci}
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_civoid perf_hpp_list__prepend_sort_field(struct perf_hpp_list *list,
6088c2ecf20Sopenharmony_ci				       struct perf_hpp_fmt *format)
6098c2ecf20Sopenharmony_ci{
6108c2ecf20Sopenharmony_ci	list_add(&format->sort_list, &list->sorts);
6118c2ecf20Sopenharmony_ci}
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_cistatic void perf_hpp__column_unregister(struct perf_hpp_fmt *format)
6148c2ecf20Sopenharmony_ci{
6158c2ecf20Sopenharmony_ci	list_del_init(&format->list);
6168c2ecf20Sopenharmony_ci	fmt_free(format);
6178c2ecf20Sopenharmony_ci}
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_civoid perf_hpp__cancel_cumulate(void)
6208c2ecf20Sopenharmony_ci{
6218c2ecf20Sopenharmony_ci	struct perf_hpp_fmt *fmt, *acc, *ovh, *tmp;
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	if (is_strict_order(field_order))
6248c2ecf20Sopenharmony_ci		return;
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	ovh = &perf_hpp__format[PERF_HPP__OVERHEAD];
6278c2ecf20Sopenharmony_ci	acc = &perf_hpp__format[PERF_HPP__OVERHEAD_ACC];
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	perf_hpp_list__for_each_format_safe(&perf_hpp_list, fmt, tmp) {
6308c2ecf20Sopenharmony_ci		if (acc->equal(acc, fmt)) {
6318c2ecf20Sopenharmony_ci			perf_hpp__column_unregister(fmt);
6328c2ecf20Sopenharmony_ci			continue;
6338c2ecf20Sopenharmony_ci		}
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci		if (ovh->equal(ovh, fmt))
6368c2ecf20Sopenharmony_ci			fmt->name = "Overhead";
6378c2ecf20Sopenharmony_ci	}
6388c2ecf20Sopenharmony_ci}
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_cistatic bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
6418c2ecf20Sopenharmony_ci{
6428c2ecf20Sopenharmony_ci	return a->equal && a->equal(a, b);
6438c2ecf20Sopenharmony_ci}
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_civoid perf_hpp__setup_output_field(struct perf_hpp_list *list)
6468c2ecf20Sopenharmony_ci{
6478c2ecf20Sopenharmony_ci	struct perf_hpp_fmt *fmt;
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	/* append sort keys to output field */
6508c2ecf20Sopenharmony_ci	perf_hpp_list__for_each_sort_list(list, fmt) {
6518c2ecf20Sopenharmony_ci		struct perf_hpp_fmt *pos;
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci		/* skip sort-only fields ("sort_compute" in perf diff) */
6548c2ecf20Sopenharmony_ci		if (!fmt->entry && !fmt->color)
6558c2ecf20Sopenharmony_ci			continue;
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci		perf_hpp_list__for_each_format(list, pos) {
6588c2ecf20Sopenharmony_ci			if (fmt_equal(fmt, pos))
6598c2ecf20Sopenharmony_ci				goto next;
6608c2ecf20Sopenharmony_ci		}
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci		perf_hpp__column_register(fmt);
6638c2ecf20Sopenharmony_cinext:
6648c2ecf20Sopenharmony_ci		continue;
6658c2ecf20Sopenharmony_ci	}
6668c2ecf20Sopenharmony_ci}
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_civoid perf_hpp__append_sort_keys(struct perf_hpp_list *list)
6698c2ecf20Sopenharmony_ci{
6708c2ecf20Sopenharmony_ci	struct perf_hpp_fmt *fmt;
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci	/* append output fields to sort keys */
6738c2ecf20Sopenharmony_ci	perf_hpp_list__for_each_format(list, fmt) {
6748c2ecf20Sopenharmony_ci		struct perf_hpp_fmt *pos;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci		perf_hpp_list__for_each_sort_list(list, pos) {
6778c2ecf20Sopenharmony_ci			if (fmt_equal(fmt, pos))
6788c2ecf20Sopenharmony_ci				goto next;
6798c2ecf20Sopenharmony_ci		}
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci		perf_hpp__register_sort_field(fmt);
6828c2ecf20Sopenharmony_cinext:
6838c2ecf20Sopenharmony_ci		continue;
6848c2ecf20Sopenharmony_ci	}
6858c2ecf20Sopenharmony_ci}
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_civoid perf_hpp__reset_output_field(struct perf_hpp_list *list)
6898c2ecf20Sopenharmony_ci{
6908c2ecf20Sopenharmony_ci	struct perf_hpp_fmt *fmt, *tmp;
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci	/* reset output fields */
6938c2ecf20Sopenharmony_ci	perf_hpp_list__for_each_format_safe(list, fmt, tmp) {
6948c2ecf20Sopenharmony_ci		list_del_init(&fmt->list);
6958c2ecf20Sopenharmony_ci		list_del_init(&fmt->sort_list);
6968c2ecf20Sopenharmony_ci		fmt_free(fmt);
6978c2ecf20Sopenharmony_ci	}
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	/* reset sort keys */
7008c2ecf20Sopenharmony_ci	perf_hpp_list__for_each_sort_list_safe(list, fmt, tmp) {
7018c2ecf20Sopenharmony_ci		list_del_init(&fmt->list);
7028c2ecf20Sopenharmony_ci		list_del_init(&fmt->sort_list);
7038c2ecf20Sopenharmony_ci		fmt_free(fmt);
7048c2ecf20Sopenharmony_ci	}
7058c2ecf20Sopenharmony_ci}
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci/*
7088c2ecf20Sopenharmony_ci * See hists__fprintf to match the column widths
7098c2ecf20Sopenharmony_ci */
7108c2ecf20Sopenharmony_ciunsigned int hists__sort_list_width(struct hists *hists)
7118c2ecf20Sopenharmony_ci{
7128c2ecf20Sopenharmony_ci	struct perf_hpp_fmt *fmt;
7138c2ecf20Sopenharmony_ci	int ret = 0;
7148c2ecf20Sopenharmony_ci	bool first = true;
7158c2ecf20Sopenharmony_ci	struct perf_hpp dummy_hpp;
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	hists__for_each_format(hists, fmt) {
7188c2ecf20Sopenharmony_ci		if (perf_hpp__should_skip(fmt, hists))
7198c2ecf20Sopenharmony_ci			continue;
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci		if (first)
7228c2ecf20Sopenharmony_ci			first = false;
7238c2ecf20Sopenharmony_ci		else
7248c2ecf20Sopenharmony_ci			ret += 2;
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci		ret += fmt->width(fmt, &dummy_hpp, hists);
7278c2ecf20Sopenharmony_ci	}
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	if (verbose > 0 && hists__has(hists, sym)) /* Addr + origin */
7308c2ecf20Sopenharmony_ci		ret += 3 + BITS_PER_LONG / 4;
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci	return ret;
7338c2ecf20Sopenharmony_ci}
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ciunsigned int hists__overhead_width(struct hists *hists)
7368c2ecf20Sopenharmony_ci{
7378c2ecf20Sopenharmony_ci	struct perf_hpp_fmt *fmt;
7388c2ecf20Sopenharmony_ci	int ret = 0;
7398c2ecf20Sopenharmony_ci	bool first = true;
7408c2ecf20Sopenharmony_ci	struct perf_hpp dummy_hpp;
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	hists__for_each_format(hists, fmt) {
7438c2ecf20Sopenharmony_ci		if (perf_hpp__is_sort_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
7448c2ecf20Sopenharmony_ci			break;
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci		if (first)
7478c2ecf20Sopenharmony_ci			first = false;
7488c2ecf20Sopenharmony_ci		else
7498c2ecf20Sopenharmony_ci			ret += 2;
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci		ret += fmt->width(fmt, &dummy_hpp, hists);
7528c2ecf20Sopenharmony_ci	}
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	return ret;
7558c2ecf20Sopenharmony_ci}
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_civoid perf_hpp__reset_width(struct perf_hpp_fmt *fmt, struct hists *hists)
7588c2ecf20Sopenharmony_ci{
7598c2ecf20Sopenharmony_ci	if (perf_hpp__is_sort_entry(fmt))
7608c2ecf20Sopenharmony_ci		return perf_hpp__reset_sort_width(fmt, hists);
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci	if (perf_hpp__is_dynamic_entry(fmt))
7638c2ecf20Sopenharmony_ci		return;
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci	BUG_ON(fmt->idx >= PERF_HPP__MAX_INDEX);
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	switch (fmt->idx) {
7688c2ecf20Sopenharmony_ci	case PERF_HPP__OVERHEAD:
7698c2ecf20Sopenharmony_ci	case PERF_HPP__OVERHEAD_SYS:
7708c2ecf20Sopenharmony_ci	case PERF_HPP__OVERHEAD_US:
7718c2ecf20Sopenharmony_ci	case PERF_HPP__OVERHEAD_ACC:
7728c2ecf20Sopenharmony_ci		fmt->len = 8;
7738c2ecf20Sopenharmony_ci		break;
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci	case PERF_HPP__OVERHEAD_GUEST_SYS:
7768c2ecf20Sopenharmony_ci	case PERF_HPP__OVERHEAD_GUEST_US:
7778c2ecf20Sopenharmony_ci		fmt->len = 9;
7788c2ecf20Sopenharmony_ci		break;
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	case PERF_HPP__SAMPLES:
7818c2ecf20Sopenharmony_ci	case PERF_HPP__PERIOD:
7828c2ecf20Sopenharmony_ci		fmt->len = 12;
7838c2ecf20Sopenharmony_ci		break;
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	default:
7868c2ecf20Sopenharmony_ci		break;
7878c2ecf20Sopenharmony_ci	}
7888c2ecf20Sopenharmony_ci}
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_civoid hists__reset_column_width(struct hists *hists)
7918c2ecf20Sopenharmony_ci{
7928c2ecf20Sopenharmony_ci	struct perf_hpp_fmt *fmt;
7938c2ecf20Sopenharmony_ci	struct perf_hpp_list_node *node;
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci	hists__for_each_format(hists, fmt)
7968c2ecf20Sopenharmony_ci		perf_hpp__reset_width(fmt, hists);
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	/* hierarchy entries have their own hpp list */
7998c2ecf20Sopenharmony_ci	list_for_each_entry(node, &hists->hpp_formats, list) {
8008c2ecf20Sopenharmony_ci		perf_hpp_list__for_each_format(&node->hpp, fmt)
8018c2ecf20Sopenharmony_ci			perf_hpp__reset_width(fmt, hists);
8028c2ecf20Sopenharmony_ci	}
8038c2ecf20Sopenharmony_ci}
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_civoid perf_hpp__set_user_width(const char *width_list_str)
8068c2ecf20Sopenharmony_ci{
8078c2ecf20Sopenharmony_ci	struct perf_hpp_fmt *fmt;
8088c2ecf20Sopenharmony_ci	const char *ptr = width_list_str;
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci	perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
8118c2ecf20Sopenharmony_ci		char *p;
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci		int len = strtol(ptr, &p, 10);
8148c2ecf20Sopenharmony_ci		fmt->user_len = len;
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci		if (*p == ',')
8178c2ecf20Sopenharmony_ci			ptr = p + 1;
8188c2ecf20Sopenharmony_ci		else
8198c2ecf20Sopenharmony_ci			break;
8208c2ecf20Sopenharmony_ci	}
8218c2ecf20Sopenharmony_ci}
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_cistatic int add_hierarchy_fmt(struct hists *hists, struct perf_hpp_fmt *fmt)
8248c2ecf20Sopenharmony_ci{
8258c2ecf20Sopenharmony_ci	struct perf_hpp_list_node *node = NULL;
8268c2ecf20Sopenharmony_ci	struct perf_hpp_fmt *fmt_copy;
8278c2ecf20Sopenharmony_ci	bool found = false;
8288c2ecf20Sopenharmony_ci	bool skip = perf_hpp__should_skip(fmt, hists);
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	list_for_each_entry(node, &hists->hpp_formats, list) {
8318c2ecf20Sopenharmony_ci		if (node->level == fmt->level) {
8328c2ecf20Sopenharmony_ci			found = true;
8338c2ecf20Sopenharmony_ci			break;
8348c2ecf20Sopenharmony_ci		}
8358c2ecf20Sopenharmony_ci	}
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	if (!found) {
8388c2ecf20Sopenharmony_ci		node = malloc(sizeof(*node));
8398c2ecf20Sopenharmony_ci		if (node == NULL)
8408c2ecf20Sopenharmony_ci			return -1;
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci		node->skip = skip;
8438c2ecf20Sopenharmony_ci		node->level = fmt->level;
8448c2ecf20Sopenharmony_ci		perf_hpp_list__init(&node->hpp);
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci		hists->nr_hpp_node++;
8478c2ecf20Sopenharmony_ci		list_add_tail(&node->list, &hists->hpp_formats);
8488c2ecf20Sopenharmony_ci	}
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci	fmt_copy = perf_hpp_fmt__dup(fmt);
8518c2ecf20Sopenharmony_ci	if (fmt_copy == NULL)
8528c2ecf20Sopenharmony_ci		return -1;
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci	if (!skip)
8558c2ecf20Sopenharmony_ci		node->skip = false;
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	list_add_tail(&fmt_copy->list, &node->hpp.fields);
8588c2ecf20Sopenharmony_ci	list_add_tail(&fmt_copy->sort_list, &node->hpp.sorts);
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci	return 0;
8618c2ecf20Sopenharmony_ci}
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ciint perf_hpp__setup_hists_formats(struct perf_hpp_list *list,
8648c2ecf20Sopenharmony_ci				  struct evlist *evlist)
8658c2ecf20Sopenharmony_ci{
8668c2ecf20Sopenharmony_ci	struct evsel *evsel;
8678c2ecf20Sopenharmony_ci	struct perf_hpp_fmt *fmt;
8688c2ecf20Sopenharmony_ci	struct hists *hists;
8698c2ecf20Sopenharmony_ci	int ret;
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci	if (!symbol_conf.report_hierarchy)
8728c2ecf20Sopenharmony_ci		return 0;
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci	evlist__for_each_entry(evlist, evsel) {
8758c2ecf20Sopenharmony_ci		hists = evsel__hists(evsel);
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci		perf_hpp_list__for_each_sort_list(list, fmt) {
8788c2ecf20Sopenharmony_ci			if (perf_hpp__is_dynamic_entry(fmt) &&
8798c2ecf20Sopenharmony_ci			    !perf_hpp__defined_dynamic_entry(fmt, hists))
8808c2ecf20Sopenharmony_ci				continue;
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci			ret = add_hierarchy_fmt(hists, fmt);
8838c2ecf20Sopenharmony_ci			if (ret < 0)
8848c2ecf20Sopenharmony_ci				return ret;
8858c2ecf20Sopenharmony_ci		}
8868c2ecf20Sopenharmony_ci	}
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci	return 0;
8898c2ecf20Sopenharmony_ci}
890