xref: /openbmc/linux/tools/perf/util/sort.h (revision 4968ac8f)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2dd68ada2SJohn Kacur #ifndef __PERF_SORT_H
3dd68ada2SJohn Kacur #define __PERF_SORT_H
4dd68ada2SJohn Kacur #include "../builtin.h"
5dd68ada2SJohn Kacur 
61eae20c1SArnaldo Carvalho de Melo #include <regex.h>
7dd68ada2SJohn Kacur 
8dd68ada2SJohn Kacur #include "color.h"
9dd68ada2SJohn Kacur #include <linux/list.h>
10dd68ada2SJohn Kacur #include "cache.h"
11dd68ada2SJohn Kacur #include <linux/rbtree.h>
129f4e8ff2SArnaldo Carvalho de Melo #include "map_symbol.h"
139f4e8ff2SArnaldo Carvalho de Melo #include "symbol_conf.h"
14dd68ada2SJohn Kacur #include "string.h"
15dd68ada2SJohn Kacur #include "callchain.h"
16dd68ada2SJohn Kacur #include "values.h"
17dd68ada2SJohn Kacur 
18dd68ada2SJohn Kacur #include "../perf.h"
19dd68ada2SJohn Kacur #include "debug.h"
20dd68ada2SJohn Kacur #include "header.h"
21dd68ada2SJohn Kacur 
224b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
23dd68ada2SJohn Kacur #include "parse-events.h"
2414135663SNamhyung Kim #include "hist.h"
25632a5cabSArnaldo Carvalho de Melo #include "srcline.h"
26e7ff8920SArnaldo Carvalho de Melo 
27e7ff8920SArnaldo Carvalho de Melo struct thread;
28dd68ada2SJohn Kacur 
29dd68ada2SJohn Kacur extern regex_t parent_regex;
30edb7c60eSArnaldo Carvalho de Melo extern const char *sort_order;
31a7d945bcSNamhyung Kim extern const char *field_order;
32edb7c60eSArnaldo Carvalho de Melo extern const char default_parent_pattern[];
33edb7c60eSArnaldo Carvalho de Melo extern const char *parent_pattern;
34fa1f4565SArnaldo Carvalho de Melo extern const char *default_sort_order;
35b21484f1SGreg Price extern regex_t ignore_callees_regex;
36b21484f1SGreg Price extern int have_ignore_callees;
3755369fc1SNamhyung Kim extern enum sort_mode sort__mode;
38dd68ada2SJohn Kacur extern struct sort_entry sort_comm;
39dd68ada2SJohn Kacur extern struct sort_entry sort_dso;
40dd68ada2SJohn Kacur extern struct sort_entry sort_sym;
41dd68ada2SJohn Kacur extern struct sort_entry sort_parent;
42a68c2c58SStephane Eranian extern struct sort_entry sort_dso_from;
43a68c2c58SStephane Eranian extern struct sort_entry sort_dso_to;
44a68c2c58SStephane Eranian extern struct sort_entry sort_sym_from;
45a68c2c58SStephane Eranian extern struct sort_entry sort_sym_to;
46f666ac0dSJiri Olsa extern struct sort_entry sort_srcline;
47a4fb581bSFrederic Weisbecker extern enum sort_type sort__first_dimension;
48228f14f2SYunlong Song extern const char default_mem_sort_order[];
49dd68ada2SJohn Kacur 
504968ac8fSAndi Kleen struct res_sample {
514968ac8fSAndi Kleen 	u64 time;
524968ac8fSAndi Kleen 	int cpu;
534968ac8fSAndi Kleen 	int tid;
544968ac8fSAndi Kleen };
554968ac8fSAndi Kleen 
56b24c28f7SNamhyung Kim struct he_stat {
57b24c28f7SNamhyung Kim 	u64			period;
58b24c28f7SNamhyung Kim 	u64			period_sys;
59b24c28f7SNamhyung Kim 	u64			period_us;
60b24c28f7SNamhyung Kim 	u64			period_guest_sys;
61b24c28f7SNamhyung Kim 	u64			period_guest_us;
6205484298SAndi Kleen 	u64			weight;
63b24c28f7SNamhyung Kim 	u32			nr_events;
64b24c28f7SNamhyung Kim };
65b24c28f7SNamhyung Kim 
66d890a98cSHari Bathini struct namespace_id {
67d890a98cSHari Bathini 	u64			dev;
68d890a98cSHari Bathini 	u64			ino;
69d890a98cSHari Bathini };
70d890a98cSHari Bathini 
7196c47f19SJiri Olsa struct hist_entry_diff {
7296c47f19SJiri Olsa 	bool	computed;
73a0b404f4SNamhyung Kim 	union {
7496c47f19SJiri Olsa 		/* PERF_HPP__DELTA */
7596c47f19SJiri Olsa 		double	period_ratio_delta;
7696c47f19SJiri Olsa 
7796c47f19SJiri Olsa 		/* PERF_HPP__RATIO */
7896c47f19SJiri Olsa 		double	period_ratio;
7981d5f958SJiri Olsa 
8081d5f958SJiri Olsa 		/* HISTC_WEIGHTED_DIFF */
8181d5f958SJiri Olsa 		s64	wdiff;
8296c47f19SJiri Olsa 	};
83a0b404f4SNamhyung Kim };
8496c47f19SJiri Olsa 
85f542e767SJiri Olsa struct hist_entry_ops {
86f542e767SJiri Olsa 	void	*(*new)(size_t size);
87f542e767SJiri Olsa 	void	(*free)(void *ptr);
88f542e767SJiri Olsa };
89f542e767SJiri Olsa 
900f0cbf7aSArnaldo Carvalho de Melo /**
910f0cbf7aSArnaldo Carvalho de Melo  * struct hist_entry - histogram entry
920f0cbf7aSArnaldo Carvalho de Melo  *
930f0cbf7aSArnaldo Carvalho de Melo  * @row_offset - offset from the first callchain expanded to appear on screen
940f0cbf7aSArnaldo Carvalho de Melo  * @nr_rows - rows expanded in callchain, recalculated on folding/unfolding
950f0cbf7aSArnaldo Carvalho de Melo  */
96dd68ada2SJohn Kacur struct hist_entry {
971980c2ebSArnaldo Carvalho de Melo 	struct rb_node		rb_node_in;
98dd68ada2SJohn Kacur 	struct rb_node		rb_node;
99b821c732SArnaldo Carvalho de Melo 	union {
100b821c732SArnaldo Carvalho de Melo 		struct list_head node;
101b821c732SArnaldo Carvalho de Melo 		struct list_head head;
102b821c732SArnaldo Carvalho de Melo 	} pairs;
103b24c28f7SNamhyung Kim 	struct he_stat		stat;
104f8be1c8cSNamhyung Kim 	struct he_stat		*stat_acc;
10559fd5306SArnaldo Carvalho de Melo 	struct map_symbol	ms;
106a5e29acaSArnaldo Carvalho de Melo 	struct thread		*thread;
1074dfced35SNamhyung Kim 	struct comm		*comm;
108d890a98cSHari Bathini 	struct namespace_id	cgroup_id;
109dd68ada2SJohn Kacur 	u64			ip;
110475eeab9SAndi Kleen 	u64			transaction;
1110c4c4debSKan Liang 	s32			socket;
112f60f3593SArun Sharma 	s32			cpu;
1137365be55SDon Zickus 	u8			cpumode;
114aef810ecSNamhyung Kim 	u8			depth;
1150f0cbf7aSArnaldo Carvalho de Melo 
116e0af43d2SJiri Olsa 	/* We are added by hists__add_dummy_entry. */
117e0af43d2SJiri Olsa 	bool			dummy;
118aef810ecSNamhyung Kim 	bool			leaf;
119e0af43d2SJiri Olsa 
120dd68ada2SJohn Kacur 	char			level;
121a5e29acaSArnaldo Carvalho de Melo 	u8			filtered;
12241477acfSArnaldo Carvalho de Melo 
12341477acfSArnaldo Carvalho de Melo 	u16			callchain_size;
12429750821SNamhyung Kim 	union {
12529750821SNamhyung Kim 		/*
12629750821SNamhyung Kim 		 * Since perf diff only supports the stdio output, TUI
12729750821SNamhyung Kim 		 * fields are only accessed from perf report (or perf
12803440c4eSMasahiro Yamada 		 * top).  So make it a union to reduce memory usage.
12929750821SNamhyung Kim 		 */
13029750821SNamhyung Kim 		struct hist_entry_diff	diff;
13129750821SNamhyung Kim 		struct /* for TUI */ {
13229750821SNamhyung Kim 			u16	row_offset;
13329750821SNamhyung Kim 			u16	nr_rows;
134d8a0f800SNamhyung Kim 			bool	init_have_children;
1353698dab1SNamhyung Kim 			bool	unfolded;
1363698dab1SNamhyung Kim 			bool	has_children;
13779dded87SNamhyung Kim 			bool	has_no_entry;
13829750821SNamhyung Kim 		};
13929750821SNamhyung Kim 	};
140409a8be6SArnaldo Carvalho de Melo 	char			*srcline;
14131191a85SAndi Kleen 	char			*srcfile;
142439d473bSArnaldo Carvalho de Melo 	struct symbol		*parent;
143b5387528SRoberto Agostino Vitillo 	struct branch_info	*branch_info;
1443723908dSAndi Kleen 	long			time;
145ae359f19SJiri Olsa 	struct hists		*hists;
14698a3b32cSStephane Eranian 	struct mem_info		*mem_info;
14772392834SNamhyung Kim 	void			*raw_data;
14872392834SNamhyung Kim 	u32			raw_size;
1494968ac8fSAndi Kleen 	int			num_res;
1504968ac8fSAndi Kleen 	struct res_sample	*res_samples;
15160517d28SNamhyung Kim 	void			*trace_output;
1521b2dbbf4SNamhyung Kim 	struct perf_hpp_list	*hpp_list;
153aef810ecSNamhyung Kim 	struct hist_entry	*parent_he;
154f542e767SJiri Olsa 	struct hist_entry_ops	*ops;
155aef810ecSNamhyung Kim 	union {
156aef810ecSNamhyung Kim 		/* this is for hierarchical entry structure */
157aef810ecSNamhyung Kim 		struct {
1582eb3d689SDavidlohr Bueso 			struct rb_root_cached	hroot_in;
1592eb3d689SDavidlohr Bueso 			struct rb_root_cached   hroot_out;
160aef810ecSNamhyung Kim 		};				/* non-leaf entries */
161aef810ecSNamhyung Kim 		struct rb_root	sorted_chain;	/* leaf entry has callchains */
162aef810ecSNamhyung Kim 	};
16398a3b32cSStephane Eranian 	struct callchain_root	callchain[0]; /* must be last member */
16486a9eee0SArnaldo Carvalho de Melo };
165dd68ada2SJohn Kacur 
1660b5d6eceSArnaldo Carvalho de Melo static __pure inline bool hist_entry__has_callchains(struct hist_entry *he)
1670b5d6eceSArnaldo Carvalho de Melo {
168e5654455SArnaldo Carvalho de Melo 	return he->callchain_size != 0;
1690b5d6eceSArnaldo Carvalho de Melo }
1700b5d6eceSArnaldo Carvalho de Melo 
171b821c732SArnaldo Carvalho de Melo static inline bool hist_entry__has_pairs(struct hist_entry *he)
172b821c732SArnaldo Carvalho de Melo {
173b821c732SArnaldo Carvalho de Melo 	return !list_empty(&he->pairs.node);
174b821c732SArnaldo Carvalho de Melo }
175b821c732SArnaldo Carvalho de Melo 
176b821c732SArnaldo Carvalho de Melo static inline struct hist_entry *hist_entry__next_pair(struct hist_entry *he)
177b821c732SArnaldo Carvalho de Melo {
178b821c732SArnaldo Carvalho de Melo 	if (hist_entry__has_pairs(he))
179b821c732SArnaldo Carvalho de Melo 		return list_entry(he->pairs.node.next, struct hist_entry, pairs.node);
180b821c732SArnaldo Carvalho de Melo 	return NULL;
181b821c732SArnaldo Carvalho de Melo }
182b821c732SArnaldo Carvalho de Melo 
1834d23322aSJiri Olsa static inline void hist_entry__add_pair(struct hist_entry *pair,
1844d23322aSJiri Olsa 					struct hist_entry *he)
185b821c732SArnaldo Carvalho de Melo {
1864d23322aSJiri Olsa 	list_add_tail(&pair->pairs.node, &he->pairs.head);
187b821c732SArnaldo Carvalho de Melo }
188b821c732SArnaldo Carvalho de Melo 
18914135663SNamhyung Kim static inline float hist_entry__get_percent_limit(struct hist_entry *he)
19014135663SNamhyung Kim {
19114135663SNamhyung Kim 	u64 period = he->stat.period;
19214135663SNamhyung Kim 	u64 total_period = hists__total_period(he->hists);
19314135663SNamhyung Kim 
19414135663SNamhyung Kim 	if (unlikely(total_period == 0))
19514135663SNamhyung Kim 		return 0;
19614135663SNamhyung Kim 
19714135663SNamhyung Kim 	if (symbol_conf.cumulate_callchain)
19814135663SNamhyung Kim 		period = he->stat_acc->period;
19914135663SNamhyung Kim 
20014135663SNamhyung Kim 	return period * 100.0 / total_period;
20114135663SNamhyung Kim }
20214135663SNamhyung Kim 
203e95cf700SJiri Olsa static inline u64 cl_address(u64 address)
204e95cf700SJiri Olsa {
205e95cf700SJiri Olsa 	/* return the cacheline of the address */
2069ac94e31SArnaldo Carvalho de Melo 	return (address & ~(cacheline_size() - 1));
207e95cf700SJiri Olsa }
20814135663SNamhyung Kim 
209d3927110SJiri Olsa static inline u64 cl_offset(u64 address)
210d3927110SJiri Olsa {
211d3927110SJiri Olsa 	/* return the cacheline of the address */
2129ac94e31SArnaldo Carvalho de Melo 	return (address & (cacheline_size() - 1));
213d3927110SJiri Olsa }
214d3927110SJiri Olsa 
21555369fc1SNamhyung Kim enum sort_mode {
21655369fc1SNamhyung Kim 	SORT_MODE__NORMAL,
21755369fc1SNamhyung Kim 	SORT_MODE__BRANCH,
21855369fc1SNamhyung Kim 	SORT_MODE__MEMORY,
219512ae1bdSNamhyung Kim 	SORT_MODE__TOP,
220512ae1bdSNamhyung Kim 	SORT_MODE__DIFF,
221d49dadeaSNamhyung Kim 	SORT_MODE__TRACEPOINT,
22255369fc1SNamhyung Kim };
22355369fc1SNamhyung Kim 
224a4fb581bSFrederic Weisbecker enum sort_type {
225fc5871edSNamhyung Kim 	/* common sort keys */
226a4fb581bSFrederic Weisbecker 	SORT_PID,
227a4fb581bSFrederic Weisbecker 	SORT_COMM,
228a4fb581bSFrederic Weisbecker 	SORT_DSO,
229a4fb581bSFrederic Weisbecker 	SORT_SYM,
230f60f3593SArun Sharma 	SORT_PARENT,
231f60f3593SArun Sharma 	SORT_CPU,
2322e7ea3abSKan Liang 	SORT_SOCKET,
233fc5871edSNamhyung Kim 	SORT_SRCLINE,
23431191a85SAndi Kleen 	SORT_SRCFILE,
235f9ea55d0SAndi Kleen 	SORT_LOCAL_WEIGHT,
236f9ea55d0SAndi Kleen 	SORT_GLOBAL_WEIGHT,
237475eeab9SAndi Kleen 	SORT_TRANSACTION,
238a34bb6a0SNamhyung Kim 	SORT_TRACE,
2397768f8daSCharles Baylis 	SORT_SYM_SIZE,
240b74d12d5SKim Phillips 	SORT_DSO_SIZE,
241d890a98cSHari Bathini 	SORT_CGROUP_ID,
242ec6ae74fSJin Yao 	SORT_SYM_IPC_NULL,
2433723908dSAndi Kleen 	SORT_TIME,
244fc5871edSNamhyung Kim 
245fc5871edSNamhyung Kim 	/* branch stack specific sort keys */
246fc5871edSNamhyung Kim 	__SORT_BRANCH_STACK,
247fc5871edSNamhyung Kim 	SORT_DSO_FROM = __SORT_BRANCH_STACK,
248b5387528SRoberto Agostino Vitillo 	SORT_DSO_TO,
249b5387528SRoberto Agostino Vitillo 	SORT_SYM_FROM,
250b5387528SRoberto Agostino Vitillo 	SORT_SYM_TO,
251b5387528SRoberto Agostino Vitillo 	SORT_MISPREDICT,
252f5d05bceSAndi Kleen 	SORT_ABORT,
253f5d05bceSAndi Kleen 	SORT_IN_TX,
2540e332f03SAndi Kleen 	SORT_CYCLES,
255508be0dfSAndi Kleen 	SORT_SRCLINE_FROM,
256508be0dfSAndi Kleen 	SORT_SRCLINE_TO,
257ec6ae74fSJin Yao 	SORT_SYM_IPC,
258afab87b9SNamhyung Kim 
259afab87b9SNamhyung Kim 	/* memory mode specific sort keys */
260afab87b9SNamhyung Kim 	__SORT_MEMORY_MODE,
261f9ea55d0SAndi Kleen 	SORT_MEM_DADDR_SYMBOL = __SORT_MEMORY_MODE,
262afab87b9SNamhyung Kim 	SORT_MEM_DADDR_DSO,
263afab87b9SNamhyung Kim 	SORT_MEM_LOCKED,
264afab87b9SNamhyung Kim 	SORT_MEM_TLB,
265afab87b9SNamhyung Kim 	SORT_MEM_LVL,
266afab87b9SNamhyung Kim 	SORT_MEM_SNOOP,
2679b32ba71SDon Zickus 	SORT_MEM_DCACHELINE,
26828e6db20SDon Zickus 	SORT_MEM_IADDR_SYMBOL,
2698780fb25SKan Liang 	SORT_MEM_PHYS_DADDR,
270a4fb581bSFrederic Weisbecker };
271a4fb581bSFrederic Weisbecker 
272dd68ada2SJohn Kacur /*
273dd68ada2SJohn Kacur  * configurable sorting bits
274dd68ada2SJohn Kacur  */
275dd68ada2SJohn Kacur 
276dd68ada2SJohn Kacur struct sort_entry {
277fcd14984SFrederic Weisbecker 	const char *se_header;
278dd68ada2SJohn Kacur 
279fcd14984SFrederic Weisbecker 	int64_t (*se_cmp)(struct hist_entry *, struct hist_entry *);
280fcd14984SFrederic Weisbecker 	int64_t (*se_collapse)(struct hist_entry *, struct hist_entry *);
281202e7a6dSNamhyung Kim 	int64_t	(*se_sort)(struct hist_entry *, struct hist_entry *);
282316c7136SArnaldo Carvalho de Melo 	int	(*se_snprintf)(struct hist_entry *he, char *bf, size_t size,
283a4e3b956SArnaldo Carvalho de Melo 			       unsigned int width);
28454430101SNamhyung Kim 	int	(*se_filter)(struct hist_entry *he, int type, const void *arg);
2858a6c5b26SArnaldo Carvalho de Melo 	u8	se_width_idx;
286dd68ada2SJohn Kacur };
287dd68ada2SJohn Kacur 
288dd68ada2SJohn Kacur extern struct sort_entry sort_thread;
289dd68ada2SJohn Kacur extern struct list_head hist_entry__sort_list;
290dd68ada2SJohn Kacur 
29140184c46SNamhyung Kim struct perf_evlist;
292096177a8STzvetomir Stoyanov (VMware) struct tep_handle;
29340184c46SNamhyung Kim int setup_sorting(struct perf_evlist *evlist);
294a7d945bcSNamhyung Kim int setup_output_field(void);
2951c89fe9bSNamhyung Kim void reset_output_field(void);
29608e71542SNamhyung Kim void sort__setup_elide(FILE *fp);
297f2998422SJiri Olsa void perf_hpp__set_elide(int idx, bool elide);
298dd68ada2SJohn Kacur 
299b21484f1SGreg Price int report_parse_ignore_callees_opt(const struct option *opt, const char *arg, int unset);
300b21484f1SGreg Price 
3012f3f9bcfSJiri Olsa bool is_strict_order(const char *order);
302beeaaeb3SJiri Olsa 
303beeaaeb3SJiri Olsa int hpp_dimension__add_output(unsigned col);
304bcf98740SJiri Olsa void reset_dimensions(void);
305a76490e4SJiri Olsa int sort_dimension__add(struct perf_hpp_list *list, const char *tok,
306a76490e4SJiri Olsa 			struct perf_evlist *evlist,
307a76490e4SJiri Olsa 			int level);
308a76490e4SJiri Olsa int output_field_add(struct perf_hpp_list *list, char *tok);
3095fe7b9b4SJiri Olsa int64_t
3105fe7b9b4SJiri Olsa sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right);
3115fe7b9b4SJiri Olsa int64_t
3125fe7b9b4SJiri Olsa sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right);
3135fe7b9b4SJiri Olsa int64_t
3145fe7b9b4SJiri Olsa sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right);
3156a53da05SArnaldo Carvalho de Melo char *hist_entry__srcline(struct hist_entry *he);
316dd68ada2SJohn Kacur #endif	/* __PERF_SORT_H */
317