xref: /openbmc/linux/tools/perf/util/sort.h (revision 0c4c4deb)
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;
47228f14f2SYunlong Song extern const char default_mem_sort_order[];
48dd68ada2SJohn Kacur 
49b24c28f7SNamhyung Kim struct he_stat {
50b24c28f7SNamhyung Kim 	u64			period;
51b24c28f7SNamhyung Kim 	u64			period_sys;
52b24c28f7SNamhyung Kim 	u64			period_us;
53b24c28f7SNamhyung Kim 	u64			period_guest_sys;
54b24c28f7SNamhyung Kim 	u64			period_guest_us;
5505484298SAndi Kleen 	u64			weight;
56b24c28f7SNamhyung Kim 	u32			nr_events;
57b24c28f7SNamhyung Kim };
58b24c28f7SNamhyung Kim 
5996c47f19SJiri Olsa struct hist_entry_diff {
6096c47f19SJiri Olsa 	bool	computed;
61a0b404f4SNamhyung Kim 	union {
6296c47f19SJiri Olsa 		/* PERF_HPP__DELTA */
6396c47f19SJiri Olsa 		double	period_ratio_delta;
6496c47f19SJiri Olsa 
6596c47f19SJiri Olsa 		/* PERF_HPP__RATIO */
6696c47f19SJiri Olsa 		double	period_ratio;
6781d5f958SJiri Olsa 
6881d5f958SJiri Olsa 		/* HISTC_WEIGHTED_DIFF */
6981d5f958SJiri Olsa 		s64	wdiff;
7096c47f19SJiri Olsa 	};
71a0b404f4SNamhyung Kim };
7296c47f19SJiri Olsa 
730f0cbf7aSArnaldo Carvalho de Melo /**
740f0cbf7aSArnaldo Carvalho de Melo  * struct hist_entry - histogram entry
750f0cbf7aSArnaldo Carvalho de Melo  *
760f0cbf7aSArnaldo Carvalho de Melo  * @row_offset - offset from the first callchain expanded to appear on screen
770f0cbf7aSArnaldo Carvalho de Melo  * @nr_rows - rows expanded in callchain, recalculated on folding/unfolding
780f0cbf7aSArnaldo Carvalho de Melo  */
79dd68ada2SJohn Kacur struct hist_entry {
801980c2ebSArnaldo Carvalho de Melo 	struct rb_node		rb_node_in;
81dd68ada2SJohn Kacur 	struct rb_node		rb_node;
82b821c732SArnaldo Carvalho de Melo 	union {
83b821c732SArnaldo Carvalho de Melo 		struct list_head node;
84b821c732SArnaldo Carvalho de Melo 		struct list_head head;
85b821c732SArnaldo Carvalho de Melo 	} pairs;
86b24c28f7SNamhyung Kim 	struct he_stat		stat;
87f8be1c8cSNamhyung Kim 	struct he_stat		*stat_acc;
8859fd5306SArnaldo Carvalho de Melo 	struct map_symbol	ms;
89a5e29acaSArnaldo Carvalho de Melo 	struct thread		*thread;
904dfced35SNamhyung Kim 	struct comm		*comm;
91dd68ada2SJohn Kacur 	u64			ip;
92475eeab9SAndi Kleen 	u64			transaction;
930c4c4debSKan Liang 	s32			socket;
94f60f3593SArun Sharma 	s32			cpu;
957365be55SDon Zickus 	u8			cpumode;
960f0cbf7aSArnaldo Carvalho de Melo 
97e0af43d2SJiri Olsa 	/* We are added by hists__add_dummy_entry. */
98e0af43d2SJiri Olsa 	bool			dummy;
99e0af43d2SJiri Olsa 
100dd68ada2SJohn Kacur 	char			level;
101a5e29acaSArnaldo Carvalho de Melo 	u8			filtered;
10229750821SNamhyung Kim 	union {
10329750821SNamhyung Kim 		/*
10429750821SNamhyung Kim 		 * Since perf diff only supports the stdio output, TUI
10529750821SNamhyung Kim 		 * fields are only accessed from perf report (or perf
10629750821SNamhyung Kim 		 * top).  So make it an union to reduce memory usage.
10729750821SNamhyung Kim 		 */
10829750821SNamhyung Kim 		struct hist_entry_diff	diff;
10929750821SNamhyung Kim 		struct /* for TUI */ {
11029750821SNamhyung Kim 			u16	row_offset;
11129750821SNamhyung Kim 			u16	nr_rows;
112d8a0f800SNamhyung Kim 			bool	init_have_children;
1133698dab1SNamhyung Kim 			bool	unfolded;
1143698dab1SNamhyung Kim 			bool	has_children;
11529750821SNamhyung Kim 		};
11629750821SNamhyung Kim 	};
117409a8be6SArnaldo Carvalho de Melo 	char			*srcline;
11831191a85SAndi Kleen 	char			*srcfile;
119439d473bSArnaldo Carvalho de Melo 	struct symbol		*parent;
120dd68ada2SJohn Kacur 	struct rb_root		sorted_chain;
121b5387528SRoberto Agostino Vitillo 	struct branch_info	*branch_info;
122ae359f19SJiri Olsa 	struct hists		*hists;
12398a3b32cSStephane Eranian 	struct mem_info		*mem_info;
12498a3b32cSStephane Eranian 	struct callchain_root	callchain[0]; /* must be last member */
12586a9eee0SArnaldo Carvalho de Melo };
126dd68ada2SJohn Kacur 
127b821c732SArnaldo Carvalho de Melo static inline bool hist_entry__has_pairs(struct hist_entry *he)
128b821c732SArnaldo Carvalho de Melo {
129b821c732SArnaldo Carvalho de Melo 	return !list_empty(&he->pairs.node);
130b821c732SArnaldo Carvalho de Melo }
131b821c732SArnaldo Carvalho de Melo 
132b821c732SArnaldo Carvalho de Melo static inline struct hist_entry *hist_entry__next_pair(struct hist_entry *he)
133b821c732SArnaldo Carvalho de Melo {
134b821c732SArnaldo Carvalho de Melo 	if (hist_entry__has_pairs(he))
135b821c732SArnaldo Carvalho de Melo 		return list_entry(he->pairs.node.next, struct hist_entry, pairs.node);
136b821c732SArnaldo Carvalho de Melo 	return NULL;
137b821c732SArnaldo Carvalho de Melo }
138b821c732SArnaldo Carvalho de Melo 
1394d23322aSJiri Olsa static inline void hist_entry__add_pair(struct hist_entry *pair,
1404d23322aSJiri Olsa 					struct hist_entry *he)
141b821c732SArnaldo Carvalho de Melo {
1424d23322aSJiri Olsa 	list_add_tail(&pair->pairs.node, &he->pairs.head);
143b821c732SArnaldo Carvalho de Melo }
144b821c732SArnaldo Carvalho de Melo 
14514135663SNamhyung Kim static inline float hist_entry__get_percent_limit(struct hist_entry *he)
14614135663SNamhyung Kim {
14714135663SNamhyung Kim 	u64 period = he->stat.period;
14814135663SNamhyung Kim 	u64 total_period = hists__total_period(he->hists);
14914135663SNamhyung Kim 
15014135663SNamhyung Kim 	if (unlikely(total_period == 0))
15114135663SNamhyung Kim 		return 0;
15214135663SNamhyung Kim 
15314135663SNamhyung Kim 	if (symbol_conf.cumulate_callchain)
15414135663SNamhyung Kim 		period = he->stat_acc->period;
15514135663SNamhyung Kim 
15614135663SNamhyung Kim 	return period * 100.0 / total_period;
15714135663SNamhyung Kim }
15814135663SNamhyung Kim 
15914135663SNamhyung Kim 
16055369fc1SNamhyung Kim enum sort_mode {
16155369fc1SNamhyung Kim 	SORT_MODE__NORMAL,
16255369fc1SNamhyung Kim 	SORT_MODE__BRANCH,
16355369fc1SNamhyung Kim 	SORT_MODE__MEMORY,
164512ae1bdSNamhyung Kim 	SORT_MODE__TOP,
165512ae1bdSNamhyung Kim 	SORT_MODE__DIFF,
16655369fc1SNamhyung Kim };
16755369fc1SNamhyung Kim 
168a4fb581bSFrederic Weisbecker enum sort_type {
169fc5871edSNamhyung Kim 	/* common sort keys */
170a4fb581bSFrederic Weisbecker 	SORT_PID,
171a4fb581bSFrederic Weisbecker 	SORT_COMM,
172a4fb581bSFrederic Weisbecker 	SORT_DSO,
173a4fb581bSFrederic Weisbecker 	SORT_SYM,
174f60f3593SArun Sharma 	SORT_PARENT,
175f60f3593SArun Sharma 	SORT_CPU,
176fc5871edSNamhyung Kim 	SORT_SRCLINE,
17731191a85SAndi Kleen 	SORT_SRCFILE,
178f9ea55d0SAndi Kleen 	SORT_LOCAL_WEIGHT,
179f9ea55d0SAndi Kleen 	SORT_GLOBAL_WEIGHT,
180475eeab9SAndi Kleen 	SORT_TRANSACTION,
181fc5871edSNamhyung Kim 
182fc5871edSNamhyung Kim 	/* branch stack specific sort keys */
183fc5871edSNamhyung Kim 	__SORT_BRANCH_STACK,
184fc5871edSNamhyung Kim 	SORT_DSO_FROM = __SORT_BRANCH_STACK,
185b5387528SRoberto Agostino Vitillo 	SORT_DSO_TO,
186b5387528SRoberto Agostino Vitillo 	SORT_SYM_FROM,
187b5387528SRoberto Agostino Vitillo 	SORT_SYM_TO,
188b5387528SRoberto Agostino Vitillo 	SORT_MISPREDICT,
189f5d05bceSAndi Kleen 	SORT_ABORT,
190f5d05bceSAndi Kleen 	SORT_IN_TX,
1910e332f03SAndi Kleen 	SORT_CYCLES,
192afab87b9SNamhyung Kim 
193afab87b9SNamhyung Kim 	/* memory mode specific sort keys */
194afab87b9SNamhyung Kim 	__SORT_MEMORY_MODE,
195f9ea55d0SAndi Kleen 	SORT_MEM_DADDR_SYMBOL = __SORT_MEMORY_MODE,
196afab87b9SNamhyung Kim 	SORT_MEM_DADDR_DSO,
197afab87b9SNamhyung Kim 	SORT_MEM_LOCKED,
198afab87b9SNamhyung Kim 	SORT_MEM_TLB,
199afab87b9SNamhyung Kim 	SORT_MEM_LVL,
200afab87b9SNamhyung Kim 	SORT_MEM_SNOOP,
2019b32ba71SDon Zickus 	SORT_MEM_DCACHELINE,
202a4fb581bSFrederic Weisbecker };
203a4fb581bSFrederic Weisbecker 
204dd68ada2SJohn Kacur /*
205dd68ada2SJohn Kacur  * configurable sorting bits
206dd68ada2SJohn Kacur  */
207dd68ada2SJohn Kacur 
208dd68ada2SJohn Kacur struct sort_entry {
209dd68ada2SJohn Kacur 	struct list_head list;
210dd68ada2SJohn Kacur 
211fcd14984SFrederic Weisbecker 	const char *se_header;
212dd68ada2SJohn Kacur 
213fcd14984SFrederic Weisbecker 	int64_t (*se_cmp)(struct hist_entry *, struct hist_entry *);
214fcd14984SFrederic Weisbecker 	int64_t (*se_collapse)(struct hist_entry *, struct hist_entry *);
215202e7a6dSNamhyung Kim 	int64_t	(*se_sort)(struct hist_entry *, struct hist_entry *);
216316c7136SArnaldo Carvalho de Melo 	int	(*se_snprintf)(struct hist_entry *he, char *bf, size_t size,
217a4e3b956SArnaldo Carvalho de Melo 			       unsigned int width);
2188a6c5b26SArnaldo Carvalho de Melo 	u8	se_width_idx;
219dd68ada2SJohn Kacur };
220dd68ada2SJohn Kacur 
221dd68ada2SJohn Kacur extern struct sort_entry sort_thread;
222dd68ada2SJohn Kacur extern struct list_head hist_entry__sort_list;
223dd68ada2SJohn Kacur 
22455309985SNamhyung Kim int setup_sorting(void);
225a7d945bcSNamhyung Kim int setup_output_field(void);
2261c89fe9bSNamhyung Kim void reset_output_field(void);
227dd68ada2SJohn Kacur extern int sort_dimension__add(const char *);
22808e71542SNamhyung Kim void sort__setup_elide(FILE *fp);
229f2998422SJiri Olsa void perf_hpp__set_elide(int idx, bool elide);
230dd68ada2SJohn Kacur 
231b21484f1SGreg Price int report_parse_ignore_callees_opt(const struct option *opt, const char *arg, int unset);
232b21484f1SGreg Price 
2332f3f9bcfSJiri Olsa bool is_strict_order(const char *order);
234dd68ada2SJohn Kacur #endif	/* __PERF_SORT_H */
235