xref: /openbmc/linux/tools/perf/util/sort.h (revision 7365be55)
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 
21dd68ada2SJohn Kacur #include "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;
31edb7c60eSArnaldo Carvalho de Melo extern const char default_sort_order[];
32b21484f1SGreg Price extern regex_t ignore_callees_regex;
33b21484f1SGreg Price extern int have_ignore_callees;
34dd68ada2SJohn Kacur extern int sort__need_collapse;
35dd68ada2SJohn Kacur extern int sort__has_parent;
361af55640SNamhyung Kim extern int sort__has_sym;
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;
46a4fb581bSFrederic Weisbecker extern enum sort_type sort__first_dimension;
47dd68ada2SJohn Kacur 
48b24c28f7SNamhyung Kim struct he_stat {
49b24c28f7SNamhyung Kim 	u64			period;
50b24c28f7SNamhyung Kim 	u64			period_sys;
51b24c28f7SNamhyung Kim 	u64			period_us;
52b24c28f7SNamhyung Kim 	u64			period_guest_sys;
53b24c28f7SNamhyung Kim 	u64			period_guest_us;
5405484298SAndi Kleen 	u64			weight;
55b24c28f7SNamhyung Kim 	u32			nr_events;
56b24c28f7SNamhyung Kim };
57b24c28f7SNamhyung Kim 
5896c47f19SJiri Olsa struct hist_entry_diff {
5996c47f19SJiri Olsa 	bool	computed;
6096c47f19SJiri Olsa 
6196c47f19SJiri Olsa 	/* PERF_HPP__DELTA */
6296c47f19SJiri Olsa 	double	period_ratio_delta;
6396c47f19SJiri Olsa 
6496c47f19SJiri Olsa 	/* PERF_HPP__RATIO */
6596c47f19SJiri Olsa 	double	period_ratio;
6681d5f958SJiri Olsa 
6781d5f958SJiri Olsa 	/* HISTC_WEIGHTED_DIFF */
6881d5f958SJiri Olsa 	s64	wdiff;
6996c47f19SJiri Olsa };
7096c47f19SJiri Olsa 
710f0cbf7aSArnaldo Carvalho de Melo /**
720f0cbf7aSArnaldo Carvalho de Melo  * struct hist_entry - histogram entry
730f0cbf7aSArnaldo Carvalho de Melo  *
740f0cbf7aSArnaldo Carvalho de Melo  * @row_offset - offset from the first callchain expanded to appear on screen
750f0cbf7aSArnaldo Carvalho de Melo  * @nr_rows - rows expanded in callchain, recalculated on folding/unfolding
760f0cbf7aSArnaldo Carvalho de Melo  */
77dd68ada2SJohn Kacur struct hist_entry {
781980c2ebSArnaldo Carvalho de Melo 	struct rb_node		rb_node_in;
79dd68ada2SJohn Kacur 	struct rb_node		rb_node;
80b821c732SArnaldo Carvalho de Melo 	union {
81b821c732SArnaldo Carvalho de Melo 		struct list_head node;
82b821c732SArnaldo Carvalho de Melo 		struct list_head head;
83b821c732SArnaldo Carvalho de Melo 	} pairs;
84b24c28f7SNamhyung Kim 	struct he_stat		stat;
85f8be1c8cSNamhyung Kim 	struct he_stat		*stat_acc;
8659fd5306SArnaldo Carvalho de Melo 	struct map_symbol	ms;
87a5e29acaSArnaldo Carvalho de Melo 	struct thread		*thread;
884dfced35SNamhyung Kim 	struct comm		*comm;
89dd68ada2SJohn Kacur 	u64			ip;
90475eeab9SAndi Kleen 	u64			transaction;
91f60f3593SArun Sharma 	s32			cpu;
927365be55SDon Zickus 	u8			cpumode;
930f0cbf7aSArnaldo Carvalho de Melo 
9496c47f19SJiri Olsa 	struct hist_entry_diff	diff;
9596c47f19SJiri Olsa 
96e0af43d2SJiri Olsa 	/* We are added by hists__add_dummy_entry. */
97e0af43d2SJiri Olsa 	bool			dummy;
98e0af43d2SJiri Olsa 
990f0cbf7aSArnaldo Carvalho de Melo 	/* XXX These two should move to some tree widget lib */
1000f0cbf7aSArnaldo Carvalho de Melo 	u16			row_offset;
1010f0cbf7aSArnaldo Carvalho de Melo 	u16			nr_rows;
1020f0cbf7aSArnaldo Carvalho de Melo 
1030f0cbf7aSArnaldo Carvalho de Melo 	bool			init_have_children;
104dd68ada2SJohn Kacur 	char			level;
105df71d95fSArnaldo Carvalho de Melo 	bool			used;
106a5e29acaSArnaldo Carvalho de Melo 	u8			filtered;
107409a8be6SArnaldo Carvalho de Melo 	char			*srcline;
108439d473bSArnaldo Carvalho de Melo 	struct symbol		*parent;
10986a9eee0SArnaldo Carvalho de Melo 	unsigned long		position;
110dd68ada2SJohn Kacur 	struct rb_root		sorted_chain;
111b5387528SRoberto Agostino Vitillo 	struct branch_info	*branch_info;
112ae359f19SJiri Olsa 	struct hists		*hists;
11398a3b32cSStephane Eranian 	struct mem_info		*mem_info;
11498a3b32cSStephane Eranian 	struct callchain_root	callchain[0]; /* must be last member */
11586a9eee0SArnaldo Carvalho de Melo };
116dd68ada2SJohn Kacur 
117b821c732SArnaldo Carvalho de Melo static inline bool hist_entry__has_pairs(struct hist_entry *he)
118b821c732SArnaldo Carvalho de Melo {
119b821c732SArnaldo Carvalho de Melo 	return !list_empty(&he->pairs.node);
120b821c732SArnaldo Carvalho de Melo }
121b821c732SArnaldo Carvalho de Melo 
122b821c732SArnaldo Carvalho de Melo static inline struct hist_entry *hist_entry__next_pair(struct hist_entry *he)
123b821c732SArnaldo Carvalho de Melo {
124b821c732SArnaldo Carvalho de Melo 	if (hist_entry__has_pairs(he))
125b821c732SArnaldo Carvalho de Melo 		return list_entry(he->pairs.node.next, struct hist_entry, pairs.node);
126b821c732SArnaldo Carvalho de Melo 	return NULL;
127b821c732SArnaldo Carvalho de Melo }
128b821c732SArnaldo Carvalho de Melo 
1294d23322aSJiri Olsa static inline void hist_entry__add_pair(struct hist_entry *pair,
1304d23322aSJiri Olsa 					struct hist_entry *he)
131b821c732SArnaldo Carvalho de Melo {
1324d23322aSJiri Olsa 	list_add_tail(&pair->pairs.node, &he->pairs.head);
133b821c732SArnaldo Carvalho de Melo }
134b821c732SArnaldo Carvalho de Melo 
13514135663SNamhyung Kim static inline float hist_entry__get_percent_limit(struct hist_entry *he)
13614135663SNamhyung Kim {
13714135663SNamhyung Kim 	u64 period = he->stat.period;
13814135663SNamhyung Kim 	u64 total_period = hists__total_period(he->hists);
13914135663SNamhyung Kim 
14014135663SNamhyung Kim 	if (unlikely(total_period == 0))
14114135663SNamhyung Kim 		return 0;
14214135663SNamhyung Kim 
14314135663SNamhyung Kim 	if (symbol_conf.cumulate_callchain)
14414135663SNamhyung Kim 		period = he->stat_acc->period;
14514135663SNamhyung Kim 
14614135663SNamhyung Kim 	return period * 100.0 / total_period;
14714135663SNamhyung Kim }
14814135663SNamhyung Kim 
14914135663SNamhyung Kim 
15055369fc1SNamhyung Kim enum sort_mode {
15155369fc1SNamhyung Kim 	SORT_MODE__NORMAL,
15255369fc1SNamhyung Kim 	SORT_MODE__BRANCH,
15355369fc1SNamhyung Kim 	SORT_MODE__MEMORY,
154512ae1bdSNamhyung Kim 	SORT_MODE__TOP,
155512ae1bdSNamhyung Kim 	SORT_MODE__DIFF,
15655369fc1SNamhyung Kim };
15755369fc1SNamhyung Kim 
158a4fb581bSFrederic Weisbecker enum sort_type {
159fc5871edSNamhyung Kim 	/* common sort keys */
160a4fb581bSFrederic Weisbecker 	SORT_PID,
161a4fb581bSFrederic Weisbecker 	SORT_COMM,
162a4fb581bSFrederic Weisbecker 	SORT_DSO,
163a4fb581bSFrederic Weisbecker 	SORT_SYM,
164f60f3593SArun Sharma 	SORT_PARENT,
165f60f3593SArun Sharma 	SORT_CPU,
166fc5871edSNamhyung Kim 	SORT_SRCLINE,
167f9ea55d0SAndi Kleen 	SORT_LOCAL_WEIGHT,
168f9ea55d0SAndi Kleen 	SORT_GLOBAL_WEIGHT,
169475eeab9SAndi Kleen 	SORT_TRANSACTION,
170fc5871edSNamhyung Kim 
171fc5871edSNamhyung Kim 	/* branch stack specific sort keys */
172fc5871edSNamhyung Kim 	__SORT_BRANCH_STACK,
173fc5871edSNamhyung Kim 	SORT_DSO_FROM = __SORT_BRANCH_STACK,
174b5387528SRoberto Agostino Vitillo 	SORT_DSO_TO,
175b5387528SRoberto Agostino Vitillo 	SORT_SYM_FROM,
176b5387528SRoberto Agostino Vitillo 	SORT_SYM_TO,
177b5387528SRoberto Agostino Vitillo 	SORT_MISPREDICT,
178f5d05bceSAndi Kleen 	SORT_ABORT,
179f5d05bceSAndi Kleen 	SORT_IN_TX,
180afab87b9SNamhyung Kim 
181afab87b9SNamhyung Kim 	/* memory mode specific sort keys */
182afab87b9SNamhyung Kim 	__SORT_MEMORY_MODE,
183f9ea55d0SAndi Kleen 	SORT_MEM_DADDR_SYMBOL = __SORT_MEMORY_MODE,
184afab87b9SNamhyung Kim 	SORT_MEM_DADDR_DSO,
185afab87b9SNamhyung Kim 	SORT_MEM_LOCKED,
186afab87b9SNamhyung Kim 	SORT_MEM_TLB,
187afab87b9SNamhyung Kim 	SORT_MEM_LVL,
188afab87b9SNamhyung Kim 	SORT_MEM_SNOOP,
189a4fb581bSFrederic Weisbecker };
190a4fb581bSFrederic Weisbecker 
191dd68ada2SJohn Kacur /*
192dd68ada2SJohn Kacur  * configurable sorting bits
193dd68ada2SJohn Kacur  */
194dd68ada2SJohn Kacur 
195dd68ada2SJohn Kacur struct sort_entry {
196dd68ada2SJohn Kacur 	struct list_head list;
197dd68ada2SJohn Kacur 
198fcd14984SFrederic Weisbecker 	const char *se_header;
199dd68ada2SJohn Kacur 
200fcd14984SFrederic Weisbecker 	int64_t (*se_cmp)(struct hist_entry *, struct hist_entry *);
201fcd14984SFrederic Weisbecker 	int64_t (*se_collapse)(struct hist_entry *, struct hist_entry *);
202202e7a6dSNamhyung Kim 	int64_t	(*se_sort)(struct hist_entry *, struct hist_entry *);
203316c7136SArnaldo Carvalho de Melo 	int	(*se_snprintf)(struct hist_entry *he, char *bf, size_t size,
204a4e3b956SArnaldo Carvalho de Melo 			       unsigned int width);
2058a6c5b26SArnaldo Carvalho de Melo 	u8	se_width_idx;
206dd68ada2SJohn Kacur };
207dd68ada2SJohn Kacur 
208dd68ada2SJohn Kacur extern struct sort_entry sort_thread;
209dd68ada2SJohn Kacur extern struct list_head hist_entry__sort_list;
210dd68ada2SJohn Kacur 
21155309985SNamhyung Kim int setup_sorting(void);
212a7d945bcSNamhyung Kim int setup_output_field(void);
2131c89fe9bSNamhyung Kim void reset_output_field(void);
214dd68ada2SJohn Kacur extern int sort_dimension__add(const char *);
21508e71542SNamhyung Kim void sort__setup_elide(FILE *fp);
216f2998422SJiri Olsa void perf_hpp__set_elide(int idx, bool elide);
217dd68ada2SJohn Kacur 
218b21484f1SGreg Price int report_parse_ignore_callees_opt(const struct option *opt, const char *arg, int unset);
219b21484f1SGreg Price 
220dd68ada2SJohn Kacur #endif	/* __PERF_SORT_H */
221