xref: /openbmc/linux/tools/perf/util/sort.h (revision 5fe7b9b4)
1dd68ada2SJohn Kacur #ifndef __PERF_SORT_H
2dd68ada2SJohn Kacur #define __PERF_SORT_H
3dd68ada2SJohn Kacur #include "../builtin.h"
4dd68ada2SJohn Kacur 
5dd68ada2SJohn Kacur #include "util.h"
6dd68ada2SJohn Kacur 
7dd68ada2SJohn Kacur #include "color.h"
8dd68ada2SJohn Kacur #include <linux/list.h>
9dd68ada2SJohn Kacur #include "cache.h"
10dd68ada2SJohn Kacur #include <linux/rbtree.h>
11dd68ada2SJohn Kacur #include "symbol.h"
12dd68ada2SJohn Kacur #include "string.h"
13dd68ada2SJohn Kacur #include "callchain.h"
14dd68ada2SJohn Kacur #include "strlist.h"
15dd68ada2SJohn Kacur #include "values.h"
16dd68ada2SJohn Kacur 
17dd68ada2SJohn Kacur #include "../perf.h"
18dd68ada2SJohn Kacur #include "debug.h"
19dd68ada2SJohn Kacur #include "header.h"
20dd68ada2SJohn Kacur 
214b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
22dd68ada2SJohn Kacur #include "parse-events.h"
2314135663SNamhyung Kim #include "hist.h"
24dd68ada2SJohn Kacur #include "thread.h"
25dd68ada2SJohn Kacur 
26dd68ada2SJohn Kacur extern regex_t parent_regex;
27edb7c60eSArnaldo Carvalho de Melo extern const char *sort_order;
28a7d945bcSNamhyung Kim extern const char *field_order;
29edb7c60eSArnaldo Carvalho de Melo extern const char default_parent_pattern[];
30edb7c60eSArnaldo Carvalho de Melo extern const char *parent_pattern;
31fa1f4565SArnaldo Carvalho de Melo extern const char *default_sort_order;
32b21484f1SGreg Price extern regex_t ignore_callees_regex;
33b21484f1SGreg Price extern int have_ignore_callees;
3455369fc1SNamhyung Kim extern enum sort_mode sort__mode;
35dd68ada2SJohn Kacur extern struct sort_entry sort_comm;
36dd68ada2SJohn Kacur extern struct sort_entry sort_dso;
37dd68ada2SJohn Kacur extern struct sort_entry sort_sym;
38dd68ada2SJohn Kacur extern struct sort_entry sort_parent;
39a68c2c58SStephane Eranian extern struct sort_entry sort_dso_from;
40a68c2c58SStephane Eranian extern struct sort_entry sort_dso_to;
41a68c2c58SStephane Eranian extern struct sort_entry sort_sym_from;
42a68c2c58SStephane Eranian extern struct sort_entry sort_sym_to;
43f666ac0dSJiri Olsa extern struct sort_entry sort_srcline;
44a4fb581bSFrederic Weisbecker extern enum sort_type sort__first_dimension;
45228f14f2SYunlong Song extern const char default_mem_sort_order[];
46dd68ada2SJohn Kacur 
47b24c28f7SNamhyung Kim struct he_stat {
48b24c28f7SNamhyung Kim 	u64			period;
49b24c28f7SNamhyung Kim 	u64			period_sys;
50b24c28f7SNamhyung Kim 	u64			period_us;
51b24c28f7SNamhyung Kim 	u64			period_guest_sys;
52b24c28f7SNamhyung Kim 	u64			period_guest_us;
5305484298SAndi Kleen 	u64			weight;
54b24c28f7SNamhyung Kim 	u32			nr_events;
55b24c28f7SNamhyung Kim };
56b24c28f7SNamhyung Kim 
5796c47f19SJiri Olsa struct hist_entry_diff {
5896c47f19SJiri Olsa 	bool	computed;
59a0b404f4SNamhyung Kim 	union {
6096c47f19SJiri Olsa 		/* PERF_HPP__DELTA */
6196c47f19SJiri Olsa 		double	period_ratio_delta;
6296c47f19SJiri Olsa 
6396c47f19SJiri Olsa 		/* PERF_HPP__RATIO */
6496c47f19SJiri Olsa 		double	period_ratio;
6581d5f958SJiri Olsa 
6681d5f958SJiri Olsa 		/* HISTC_WEIGHTED_DIFF */
6781d5f958SJiri Olsa 		s64	wdiff;
6896c47f19SJiri Olsa 	};
69a0b404f4SNamhyung Kim };
7096c47f19SJiri Olsa 
71f542e767SJiri Olsa struct hist_entry_ops {
72f542e767SJiri Olsa 	void	*(*new)(size_t size);
73f542e767SJiri Olsa 	void	(*free)(void *ptr);
74f542e767SJiri Olsa };
75f542e767SJiri Olsa 
760f0cbf7aSArnaldo Carvalho de Melo /**
770f0cbf7aSArnaldo Carvalho de Melo  * struct hist_entry - histogram entry
780f0cbf7aSArnaldo Carvalho de Melo  *
790f0cbf7aSArnaldo Carvalho de Melo  * @row_offset - offset from the first callchain expanded to appear on screen
800f0cbf7aSArnaldo Carvalho de Melo  * @nr_rows - rows expanded in callchain, recalculated on folding/unfolding
810f0cbf7aSArnaldo Carvalho de Melo  */
82dd68ada2SJohn Kacur struct hist_entry {
831980c2ebSArnaldo Carvalho de Melo 	struct rb_node		rb_node_in;
84dd68ada2SJohn Kacur 	struct rb_node		rb_node;
85b821c732SArnaldo Carvalho de Melo 	union {
86b821c732SArnaldo Carvalho de Melo 		struct list_head node;
87b821c732SArnaldo Carvalho de Melo 		struct list_head head;
88b821c732SArnaldo Carvalho de Melo 	} pairs;
89b24c28f7SNamhyung Kim 	struct he_stat		stat;
90f8be1c8cSNamhyung Kim 	struct he_stat		*stat_acc;
9159fd5306SArnaldo Carvalho de Melo 	struct map_symbol	ms;
92a5e29acaSArnaldo Carvalho de Melo 	struct thread		*thread;
934dfced35SNamhyung Kim 	struct comm		*comm;
94dd68ada2SJohn Kacur 	u64			ip;
95475eeab9SAndi Kleen 	u64			transaction;
960c4c4debSKan Liang 	s32			socket;
97f60f3593SArun Sharma 	s32			cpu;
987365be55SDon Zickus 	u8			cpumode;
99aef810ecSNamhyung Kim 	u8			depth;
1000f0cbf7aSArnaldo Carvalho de Melo 
101e0af43d2SJiri Olsa 	/* We are added by hists__add_dummy_entry. */
102e0af43d2SJiri Olsa 	bool			dummy;
103aef810ecSNamhyung Kim 	bool			leaf;
104e0af43d2SJiri Olsa 
105dd68ada2SJohn Kacur 	char			level;
106a5e29acaSArnaldo Carvalho de Melo 	u8			filtered;
10729750821SNamhyung Kim 	union {
10829750821SNamhyung Kim 		/*
10929750821SNamhyung Kim 		 * Since perf diff only supports the stdio output, TUI
11029750821SNamhyung Kim 		 * fields are only accessed from perf report (or perf
11129750821SNamhyung Kim 		 * top).  So make it an union to reduce memory usage.
11229750821SNamhyung Kim 		 */
11329750821SNamhyung Kim 		struct hist_entry_diff	diff;
11429750821SNamhyung Kim 		struct /* for TUI */ {
11529750821SNamhyung Kim 			u16	row_offset;
11629750821SNamhyung Kim 			u16	nr_rows;
117d8a0f800SNamhyung Kim 			bool	init_have_children;
1183698dab1SNamhyung Kim 			bool	unfolded;
1193698dab1SNamhyung Kim 			bool	has_children;
12079dded87SNamhyung Kim 			bool	has_no_entry;
12129750821SNamhyung Kim 		};
12229750821SNamhyung Kim 	};
123409a8be6SArnaldo Carvalho de Melo 	char			*srcline;
12431191a85SAndi Kleen 	char			*srcfile;
125439d473bSArnaldo Carvalho de Melo 	struct symbol		*parent;
126b5387528SRoberto Agostino Vitillo 	struct branch_info	*branch_info;
127ae359f19SJiri Olsa 	struct hists		*hists;
12898a3b32cSStephane Eranian 	struct mem_info		*mem_info;
12972392834SNamhyung Kim 	void			*raw_data;
13072392834SNamhyung Kim 	u32			raw_size;
13160517d28SNamhyung Kim 	void			*trace_output;
1321b2dbbf4SNamhyung Kim 	struct perf_hpp_list	*hpp_list;
133aef810ecSNamhyung Kim 	struct hist_entry	*parent_he;
134f542e767SJiri Olsa 	struct hist_entry_ops	*ops;
135aef810ecSNamhyung Kim 	union {
136aef810ecSNamhyung Kim 		/* this is for hierarchical entry structure */
137aef810ecSNamhyung Kim 		struct {
138aef810ecSNamhyung Kim 			struct rb_root	hroot_in;
139aef810ecSNamhyung Kim 			struct rb_root  hroot_out;
140aef810ecSNamhyung Kim 		};				/* non-leaf entries */
141aef810ecSNamhyung Kim 		struct rb_root	sorted_chain;	/* leaf entry has callchains */
142aef810ecSNamhyung Kim 	};
14398a3b32cSStephane Eranian 	struct callchain_root	callchain[0]; /* must be last member */
14486a9eee0SArnaldo Carvalho de Melo };
145dd68ada2SJohn Kacur 
146b821c732SArnaldo Carvalho de Melo static inline bool hist_entry__has_pairs(struct hist_entry *he)
147b821c732SArnaldo Carvalho de Melo {
148b821c732SArnaldo Carvalho de Melo 	return !list_empty(&he->pairs.node);
149b821c732SArnaldo Carvalho de Melo }
150b821c732SArnaldo Carvalho de Melo 
151b821c732SArnaldo Carvalho de Melo static inline struct hist_entry *hist_entry__next_pair(struct hist_entry *he)
152b821c732SArnaldo Carvalho de Melo {
153b821c732SArnaldo Carvalho de Melo 	if (hist_entry__has_pairs(he))
154b821c732SArnaldo Carvalho de Melo 		return list_entry(he->pairs.node.next, struct hist_entry, pairs.node);
155b821c732SArnaldo Carvalho de Melo 	return NULL;
156b821c732SArnaldo Carvalho de Melo }
157b821c732SArnaldo Carvalho de Melo 
1584d23322aSJiri Olsa static inline void hist_entry__add_pair(struct hist_entry *pair,
1594d23322aSJiri Olsa 					struct hist_entry *he)
160b821c732SArnaldo Carvalho de Melo {
1614d23322aSJiri Olsa 	list_add_tail(&pair->pairs.node, &he->pairs.head);
162b821c732SArnaldo Carvalho de Melo }
163b821c732SArnaldo Carvalho de Melo 
16414135663SNamhyung Kim static inline float hist_entry__get_percent_limit(struct hist_entry *he)
16514135663SNamhyung Kim {
16614135663SNamhyung Kim 	u64 period = he->stat.period;
16714135663SNamhyung Kim 	u64 total_period = hists__total_period(he->hists);
16814135663SNamhyung Kim 
16914135663SNamhyung Kim 	if (unlikely(total_period == 0))
17014135663SNamhyung Kim 		return 0;
17114135663SNamhyung Kim 
17214135663SNamhyung Kim 	if (symbol_conf.cumulate_callchain)
17314135663SNamhyung Kim 		period = he->stat_acc->period;
17414135663SNamhyung Kim 
17514135663SNamhyung Kim 	return period * 100.0 / total_period;
17614135663SNamhyung Kim }
17714135663SNamhyung Kim 
178e95cf700SJiri Olsa static inline u64 cl_address(u64 address)
179e95cf700SJiri Olsa {
180e95cf700SJiri Olsa 	/* return the cacheline of the address */
181e95cf700SJiri Olsa 	return (address & ~(cacheline_size - 1));
182e95cf700SJiri Olsa }
18314135663SNamhyung Kim 
184d3927110SJiri Olsa static inline u64 cl_offset(u64 address)
185d3927110SJiri Olsa {
186d3927110SJiri Olsa 	/* return the cacheline of the address */
187d3927110SJiri Olsa 	return (address & (cacheline_size - 1));
188d3927110SJiri Olsa }
189d3927110SJiri Olsa 
19055369fc1SNamhyung Kim enum sort_mode {
19155369fc1SNamhyung Kim 	SORT_MODE__NORMAL,
19255369fc1SNamhyung Kim 	SORT_MODE__BRANCH,
19355369fc1SNamhyung Kim 	SORT_MODE__MEMORY,
194512ae1bdSNamhyung Kim 	SORT_MODE__TOP,
195512ae1bdSNamhyung Kim 	SORT_MODE__DIFF,
196d49dadeaSNamhyung Kim 	SORT_MODE__TRACEPOINT,
19755369fc1SNamhyung Kim };
19855369fc1SNamhyung Kim 
199a4fb581bSFrederic Weisbecker enum sort_type {
200fc5871edSNamhyung Kim 	/* common sort keys */
201a4fb581bSFrederic Weisbecker 	SORT_PID,
202a4fb581bSFrederic Weisbecker 	SORT_COMM,
203a4fb581bSFrederic Weisbecker 	SORT_DSO,
204a4fb581bSFrederic Weisbecker 	SORT_SYM,
205f60f3593SArun Sharma 	SORT_PARENT,
206f60f3593SArun Sharma 	SORT_CPU,
2072e7ea3abSKan Liang 	SORT_SOCKET,
208fc5871edSNamhyung Kim 	SORT_SRCLINE,
20931191a85SAndi Kleen 	SORT_SRCFILE,
210f9ea55d0SAndi Kleen 	SORT_LOCAL_WEIGHT,
211f9ea55d0SAndi Kleen 	SORT_GLOBAL_WEIGHT,
212475eeab9SAndi Kleen 	SORT_TRANSACTION,
213a34bb6a0SNamhyung Kim 	SORT_TRACE,
214fc5871edSNamhyung Kim 
215fc5871edSNamhyung Kim 	/* branch stack specific sort keys */
216fc5871edSNamhyung Kim 	__SORT_BRANCH_STACK,
217fc5871edSNamhyung Kim 	SORT_DSO_FROM = __SORT_BRANCH_STACK,
218b5387528SRoberto Agostino Vitillo 	SORT_DSO_TO,
219b5387528SRoberto Agostino Vitillo 	SORT_SYM_FROM,
220b5387528SRoberto Agostino Vitillo 	SORT_SYM_TO,
221b5387528SRoberto Agostino Vitillo 	SORT_MISPREDICT,
222f5d05bceSAndi Kleen 	SORT_ABORT,
223f5d05bceSAndi Kleen 	SORT_IN_TX,
2240e332f03SAndi Kleen 	SORT_CYCLES,
225508be0dfSAndi Kleen 	SORT_SRCLINE_FROM,
226508be0dfSAndi Kleen 	SORT_SRCLINE_TO,
227afab87b9SNamhyung Kim 
228afab87b9SNamhyung Kim 	/* memory mode specific sort keys */
229afab87b9SNamhyung Kim 	__SORT_MEMORY_MODE,
230f9ea55d0SAndi Kleen 	SORT_MEM_DADDR_SYMBOL = __SORT_MEMORY_MODE,
231afab87b9SNamhyung Kim 	SORT_MEM_DADDR_DSO,
232afab87b9SNamhyung Kim 	SORT_MEM_LOCKED,
233afab87b9SNamhyung Kim 	SORT_MEM_TLB,
234afab87b9SNamhyung Kim 	SORT_MEM_LVL,
235afab87b9SNamhyung Kim 	SORT_MEM_SNOOP,
2369b32ba71SDon Zickus 	SORT_MEM_DCACHELINE,
23728e6db20SDon Zickus 	SORT_MEM_IADDR_SYMBOL,
238a4fb581bSFrederic Weisbecker };
239a4fb581bSFrederic Weisbecker 
240dd68ada2SJohn Kacur /*
241dd68ada2SJohn Kacur  * configurable sorting bits
242dd68ada2SJohn Kacur  */
243dd68ada2SJohn Kacur 
244dd68ada2SJohn Kacur struct sort_entry {
245fcd14984SFrederic Weisbecker 	const char *se_header;
246dd68ada2SJohn Kacur 
247fcd14984SFrederic Weisbecker 	int64_t (*se_cmp)(struct hist_entry *, struct hist_entry *);
248fcd14984SFrederic Weisbecker 	int64_t (*se_collapse)(struct hist_entry *, struct hist_entry *);
249202e7a6dSNamhyung Kim 	int64_t	(*se_sort)(struct hist_entry *, struct hist_entry *);
250316c7136SArnaldo Carvalho de Melo 	int	(*se_snprintf)(struct hist_entry *he, char *bf, size_t size,
251a4e3b956SArnaldo Carvalho de Melo 			       unsigned int width);
25254430101SNamhyung Kim 	int	(*se_filter)(struct hist_entry *he, int type, const void *arg);
2538a6c5b26SArnaldo Carvalho de Melo 	u8	se_width_idx;
254dd68ada2SJohn Kacur };
255dd68ada2SJohn Kacur 
256dd68ada2SJohn Kacur extern struct sort_entry sort_thread;
257dd68ada2SJohn Kacur extern struct list_head hist_entry__sort_list;
258dd68ada2SJohn Kacur 
25940184c46SNamhyung Kim struct perf_evlist;
26040184c46SNamhyung Kim struct pevent;
26140184c46SNamhyung Kim int setup_sorting(struct perf_evlist *evlist);
262a7d945bcSNamhyung Kim int setup_output_field(void);
2631c89fe9bSNamhyung Kim void reset_output_field(void);
26408e71542SNamhyung Kim void sort__setup_elide(FILE *fp);
265f2998422SJiri Olsa void perf_hpp__set_elide(int idx, bool elide);
266dd68ada2SJohn Kacur 
267b21484f1SGreg Price int report_parse_ignore_callees_opt(const struct option *opt, const char *arg, int unset);
268b21484f1SGreg Price 
2692f3f9bcfSJiri Olsa bool is_strict_order(const char *order);
270beeaaeb3SJiri Olsa 
271beeaaeb3SJiri Olsa int hpp_dimension__add_output(unsigned col);
272bcf98740SJiri Olsa void reset_dimensions(void);
273a76490e4SJiri Olsa int sort_dimension__add(struct perf_hpp_list *list, const char *tok,
274a76490e4SJiri Olsa 			struct perf_evlist *evlist,
275a76490e4SJiri Olsa 			int level);
276a76490e4SJiri Olsa int output_field_add(struct perf_hpp_list *list, char *tok);
2775fe7b9b4SJiri Olsa int64_t
2785fe7b9b4SJiri Olsa sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right);
2795fe7b9b4SJiri Olsa int64_t
2805fe7b9b4SJiri Olsa sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right);
2815fe7b9b4SJiri Olsa int64_t
2825fe7b9b4SJiri Olsa sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right);
283dd68ada2SJohn Kacur #endif	/* __PERF_SORT_H */
284