xref: /openbmc/linux/tools/perf/util/sort.h (revision fa1f4565)
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;
43a4fb581bSFrederic Weisbecker extern enum sort_type sort__first_dimension;
44228f14f2SYunlong Song extern const char default_mem_sort_order[];
45dd68ada2SJohn Kacur 
46b24c28f7SNamhyung Kim struct he_stat {
47b24c28f7SNamhyung Kim 	u64			period;
48b24c28f7SNamhyung Kim 	u64			period_sys;
49b24c28f7SNamhyung Kim 	u64			period_us;
50b24c28f7SNamhyung Kim 	u64			period_guest_sys;
51b24c28f7SNamhyung Kim 	u64			period_guest_us;
5205484298SAndi Kleen 	u64			weight;
53b24c28f7SNamhyung Kim 	u32			nr_events;
54b24c28f7SNamhyung Kim };
55b24c28f7SNamhyung Kim 
5696c47f19SJiri Olsa struct hist_entry_diff {
5796c47f19SJiri Olsa 	bool	computed;
58a0b404f4SNamhyung Kim 	union {
5996c47f19SJiri Olsa 		/* PERF_HPP__DELTA */
6096c47f19SJiri Olsa 		double	period_ratio_delta;
6196c47f19SJiri Olsa 
6296c47f19SJiri Olsa 		/* PERF_HPP__RATIO */
6396c47f19SJiri Olsa 		double	period_ratio;
6481d5f958SJiri Olsa 
6581d5f958SJiri Olsa 		/* HISTC_WEIGHTED_DIFF */
6681d5f958SJiri Olsa 		s64	wdiff;
6796c47f19SJiri Olsa 	};
68a0b404f4SNamhyung Kim };
6996c47f19SJiri Olsa 
70f542e767SJiri Olsa struct hist_entry_ops {
71f542e767SJiri Olsa 	void	*(*new)(size_t size);
72f542e767SJiri Olsa 	void	(*free)(void *ptr);
73f542e767SJiri Olsa };
74f542e767SJiri Olsa 
750f0cbf7aSArnaldo Carvalho de Melo /**
760f0cbf7aSArnaldo Carvalho de Melo  * struct hist_entry - histogram entry
770f0cbf7aSArnaldo Carvalho de Melo  *
780f0cbf7aSArnaldo Carvalho de Melo  * @row_offset - offset from the first callchain expanded to appear on screen
790f0cbf7aSArnaldo Carvalho de Melo  * @nr_rows - rows expanded in callchain, recalculated on folding/unfolding
800f0cbf7aSArnaldo Carvalho de Melo  */
81dd68ada2SJohn Kacur struct hist_entry {
821980c2ebSArnaldo Carvalho de Melo 	struct rb_node		rb_node_in;
83dd68ada2SJohn Kacur 	struct rb_node		rb_node;
84b821c732SArnaldo Carvalho de Melo 	union {
85b821c732SArnaldo Carvalho de Melo 		struct list_head node;
86b821c732SArnaldo Carvalho de Melo 		struct list_head head;
87b821c732SArnaldo Carvalho de Melo 	} pairs;
88b24c28f7SNamhyung Kim 	struct he_stat		stat;
89f8be1c8cSNamhyung Kim 	struct he_stat		*stat_acc;
9059fd5306SArnaldo Carvalho de Melo 	struct map_symbol	ms;
91a5e29acaSArnaldo Carvalho de Melo 	struct thread		*thread;
924dfced35SNamhyung Kim 	struct comm		*comm;
93dd68ada2SJohn Kacur 	u64			ip;
94475eeab9SAndi Kleen 	u64			transaction;
950c4c4debSKan Liang 	s32			socket;
96f60f3593SArun Sharma 	s32			cpu;
977365be55SDon Zickus 	u8			cpumode;
98aef810ecSNamhyung Kim 	u8			depth;
990f0cbf7aSArnaldo Carvalho de Melo 
100e0af43d2SJiri Olsa 	/* We are added by hists__add_dummy_entry. */
101e0af43d2SJiri Olsa 	bool			dummy;
102aef810ecSNamhyung Kim 	bool			leaf;
103e0af43d2SJiri Olsa 
104dd68ada2SJohn Kacur 	char			level;
105a5e29acaSArnaldo Carvalho de Melo 	u8			filtered;
10629750821SNamhyung Kim 	union {
10729750821SNamhyung Kim 		/*
10829750821SNamhyung Kim 		 * Since perf diff only supports the stdio output, TUI
10929750821SNamhyung Kim 		 * fields are only accessed from perf report (or perf
11029750821SNamhyung Kim 		 * top).  So make it an union to reduce memory usage.
11129750821SNamhyung Kim 		 */
11229750821SNamhyung Kim 		struct hist_entry_diff	diff;
11329750821SNamhyung Kim 		struct /* for TUI */ {
11429750821SNamhyung Kim 			u16	row_offset;
11529750821SNamhyung Kim 			u16	nr_rows;
116d8a0f800SNamhyung Kim 			bool	init_have_children;
1173698dab1SNamhyung Kim 			bool	unfolded;
1183698dab1SNamhyung Kim 			bool	has_children;
11979dded87SNamhyung Kim 			bool	has_no_entry;
12029750821SNamhyung Kim 		};
12129750821SNamhyung Kim 	};
122409a8be6SArnaldo Carvalho de Melo 	char			*srcline;
12331191a85SAndi Kleen 	char			*srcfile;
124439d473bSArnaldo Carvalho de Melo 	struct symbol		*parent;
125b5387528SRoberto Agostino Vitillo 	struct branch_info	*branch_info;
126ae359f19SJiri Olsa 	struct hists		*hists;
12798a3b32cSStephane Eranian 	struct mem_info		*mem_info;
12872392834SNamhyung Kim 	void			*raw_data;
12972392834SNamhyung Kim 	u32			raw_size;
13060517d28SNamhyung Kim 	void			*trace_output;
1311b2dbbf4SNamhyung Kim 	struct perf_hpp_list	*hpp_list;
132aef810ecSNamhyung Kim 	struct hist_entry	*parent_he;
133f542e767SJiri Olsa 	struct hist_entry_ops	*ops;
134aef810ecSNamhyung Kim 	union {
135aef810ecSNamhyung Kim 		/* this is for hierarchical entry structure */
136aef810ecSNamhyung Kim 		struct {
137aef810ecSNamhyung Kim 			struct rb_root	hroot_in;
138aef810ecSNamhyung Kim 			struct rb_root  hroot_out;
139aef810ecSNamhyung Kim 		};				/* non-leaf entries */
140aef810ecSNamhyung Kim 		struct rb_root	sorted_chain;	/* leaf entry has callchains */
141aef810ecSNamhyung Kim 	};
14298a3b32cSStephane Eranian 	struct callchain_root	callchain[0]; /* must be last member */
14386a9eee0SArnaldo Carvalho de Melo };
144dd68ada2SJohn Kacur 
145b821c732SArnaldo Carvalho de Melo static inline bool hist_entry__has_pairs(struct hist_entry *he)
146b821c732SArnaldo Carvalho de Melo {
147b821c732SArnaldo Carvalho de Melo 	return !list_empty(&he->pairs.node);
148b821c732SArnaldo Carvalho de Melo }
149b821c732SArnaldo Carvalho de Melo 
150b821c732SArnaldo Carvalho de Melo static inline struct hist_entry *hist_entry__next_pair(struct hist_entry *he)
151b821c732SArnaldo Carvalho de Melo {
152b821c732SArnaldo Carvalho de Melo 	if (hist_entry__has_pairs(he))
153b821c732SArnaldo Carvalho de Melo 		return list_entry(he->pairs.node.next, struct hist_entry, pairs.node);
154b821c732SArnaldo Carvalho de Melo 	return NULL;
155b821c732SArnaldo Carvalho de Melo }
156b821c732SArnaldo Carvalho de Melo 
1574d23322aSJiri Olsa static inline void hist_entry__add_pair(struct hist_entry *pair,
1584d23322aSJiri Olsa 					struct hist_entry *he)
159b821c732SArnaldo Carvalho de Melo {
1604d23322aSJiri Olsa 	list_add_tail(&pair->pairs.node, &he->pairs.head);
161b821c732SArnaldo Carvalho de Melo }
162b821c732SArnaldo Carvalho de Melo 
16314135663SNamhyung Kim static inline float hist_entry__get_percent_limit(struct hist_entry *he)
16414135663SNamhyung Kim {
16514135663SNamhyung Kim 	u64 period = he->stat.period;
16614135663SNamhyung Kim 	u64 total_period = hists__total_period(he->hists);
16714135663SNamhyung Kim 
16814135663SNamhyung Kim 	if (unlikely(total_period == 0))
16914135663SNamhyung Kim 		return 0;
17014135663SNamhyung Kim 
17114135663SNamhyung Kim 	if (symbol_conf.cumulate_callchain)
17214135663SNamhyung Kim 		period = he->stat_acc->period;
17314135663SNamhyung Kim 
17414135663SNamhyung Kim 	return period * 100.0 / total_period;
17514135663SNamhyung Kim }
17614135663SNamhyung Kim 
177e95cf700SJiri Olsa static inline u64 cl_address(u64 address)
178e95cf700SJiri Olsa {
179e95cf700SJiri Olsa 	/* return the cacheline of the address */
180e95cf700SJiri Olsa 	return (address & ~(cacheline_size - 1));
181e95cf700SJiri Olsa }
18214135663SNamhyung Kim 
183d3927110SJiri Olsa static inline u64 cl_offset(u64 address)
184d3927110SJiri Olsa {
185d3927110SJiri Olsa 	/* return the cacheline of the address */
186d3927110SJiri Olsa 	return (address & (cacheline_size - 1));
187d3927110SJiri Olsa }
188d3927110SJiri Olsa 
18955369fc1SNamhyung Kim enum sort_mode {
19055369fc1SNamhyung Kim 	SORT_MODE__NORMAL,
19155369fc1SNamhyung Kim 	SORT_MODE__BRANCH,
19255369fc1SNamhyung Kim 	SORT_MODE__MEMORY,
193512ae1bdSNamhyung Kim 	SORT_MODE__TOP,
194512ae1bdSNamhyung Kim 	SORT_MODE__DIFF,
195d49dadeaSNamhyung Kim 	SORT_MODE__TRACEPOINT,
19655369fc1SNamhyung Kim };
19755369fc1SNamhyung Kim 
198a4fb581bSFrederic Weisbecker enum sort_type {
199fc5871edSNamhyung Kim 	/* common sort keys */
200a4fb581bSFrederic Weisbecker 	SORT_PID,
201a4fb581bSFrederic Weisbecker 	SORT_COMM,
202a4fb581bSFrederic Weisbecker 	SORT_DSO,
203a4fb581bSFrederic Weisbecker 	SORT_SYM,
204f60f3593SArun Sharma 	SORT_PARENT,
205f60f3593SArun Sharma 	SORT_CPU,
2062e7ea3abSKan Liang 	SORT_SOCKET,
207fc5871edSNamhyung Kim 	SORT_SRCLINE,
20831191a85SAndi Kleen 	SORT_SRCFILE,
209f9ea55d0SAndi Kleen 	SORT_LOCAL_WEIGHT,
210f9ea55d0SAndi Kleen 	SORT_GLOBAL_WEIGHT,
211475eeab9SAndi Kleen 	SORT_TRANSACTION,
212a34bb6a0SNamhyung Kim 	SORT_TRACE,
213fc5871edSNamhyung Kim 
214fc5871edSNamhyung Kim 	/* branch stack specific sort keys */
215fc5871edSNamhyung Kim 	__SORT_BRANCH_STACK,
216fc5871edSNamhyung Kim 	SORT_DSO_FROM = __SORT_BRANCH_STACK,
217b5387528SRoberto Agostino Vitillo 	SORT_DSO_TO,
218b5387528SRoberto Agostino Vitillo 	SORT_SYM_FROM,
219b5387528SRoberto Agostino Vitillo 	SORT_SYM_TO,
220b5387528SRoberto Agostino Vitillo 	SORT_MISPREDICT,
221f5d05bceSAndi Kleen 	SORT_ABORT,
222f5d05bceSAndi Kleen 	SORT_IN_TX,
2230e332f03SAndi Kleen 	SORT_CYCLES,
224508be0dfSAndi Kleen 	SORT_SRCLINE_FROM,
225508be0dfSAndi Kleen 	SORT_SRCLINE_TO,
226afab87b9SNamhyung Kim 
227afab87b9SNamhyung Kim 	/* memory mode specific sort keys */
228afab87b9SNamhyung Kim 	__SORT_MEMORY_MODE,
229f9ea55d0SAndi Kleen 	SORT_MEM_DADDR_SYMBOL = __SORT_MEMORY_MODE,
230afab87b9SNamhyung Kim 	SORT_MEM_DADDR_DSO,
231afab87b9SNamhyung Kim 	SORT_MEM_LOCKED,
232afab87b9SNamhyung Kim 	SORT_MEM_TLB,
233afab87b9SNamhyung Kim 	SORT_MEM_LVL,
234afab87b9SNamhyung Kim 	SORT_MEM_SNOOP,
2359b32ba71SDon Zickus 	SORT_MEM_DCACHELINE,
23628e6db20SDon Zickus 	SORT_MEM_IADDR_SYMBOL,
237a4fb581bSFrederic Weisbecker };
238a4fb581bSFrederic Weisbecker 
239dd68ada2SJohn Kacur /*
240dd68ada2SJohn Kacur  * configurable sorting bits
241dd68ada2SJohn Kacur  */
242dd68ada2SJohn Kacur 
243dd68ada2SJohn Kacur struct sort_entry {
244fcd14984SFrederic Weisbecker 	const char *se_header;
245dd68ada2SJohn Kacur 
246fcd14984SFrederic Weisbecker 	int64_t (*se_cmp)(struct hist_entry *, struct hist_entry *);
247fcd14984SFrederic Weisbecker 	int64_t (*se_collapse)(struct hist_entry *, struct hist_entry *);
248202e7a6dSNamhyung Kim 	int64_t	(*se_sort)(struct hist_entry *, struct hist_entry *);
249316c7136SArnaldo Carvalho de Melo 	int	(*se_snprintf)(struct hist_entry *he, char *bf, size_t size,
250a4e3b956SArnaldo Carvalho de Melo 			       unsigned int width);
25154430101SNamhyung Kim 	int	(*se_filter)(struct hist_entry *he, int type, const void *arg);
2528a6c5b26SArnaldo Carvalho de Melo 	u8	se_width_idx;
253dd68ada2SJohn Kacur };
254dd68ada2SJohn Kacur 
255dd68ada2SJohn Kacur extern struct sort_entry sort_thread;
256dd68ada2SJohn Kacur extern struct list_head hist_entry__sort_list;
257dd68ada2SJohn Kacur 
25840184c46SNamhyung Kim struct perf_evlist;
25940184c46SNamhyung Kim struct pevent;
26040184c46SNamhyung Kim int setup_sorting(struct perf_evlist *evlist);
261a7d945bcSNamhyung Kim int setup_output_field(void);
2621c89fe9bSNamhyung Kim void reset_output_field(void);
26308e71542SNamhyung Kim void sort__setup_elide(FILE *fp);
264f2998422SJiri Olsa void perf_hpp__set_elide(int idx, bool elide);
265dd68ada2SJohn Kacur 
266b21484f1SGreg Price int report_parse_ignore_callees_opt(const struct option *opt, const char *arg, int unset);
267b21484f1SGreg Price 
2682f3f9bcfSJiri Olsa bool is_strict_order(const char *order);
269beeaaeb3SJiri Olsa 
270beeaaeb3SJiri Olsa int hpp_dimension__add_output(unsigned col);
271dd68ada2SJohn Kacur #endif	/* __PERF_SORT_H */
272