xref: /openbmc/linux/tools/perf/util/sort.h (revision 31191a85)
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;
93f60f3593SArun Sharma 	s32			cpu;
947365be55SDon Zickus 	u8			cpumode;
950f0cbf7aSArnaldo Carvalho de Melo 
96e0af43d2SJiri Olsa 	/* We are added by hists__add_dummy_entry. */
97e0af43d2SJiri Olsa 	bool			dummy;
98e0af43d2SJiri Olsa 
99dd68ada2SJohn Kacur 	char			level;
100a5e29acaSArnaldo Carvalho de Melo 	u8			filtered;
10129750821SNamhyung Kim 	union {
10229750821SNamhyung Kim 		/*
10329750821SNamhyung Kim 		 * Since perf diff only supports the stdio output, TUI
10429750821SNamhyung Kim 		 * fields are only accessed from perf report (or perf
10529750821SNamhyung Kim 		 * top).  So make it an union to reduce memory usage.
10629750821SNamhyung Kim 		 */
10729750821SNamhyung Kim 		struct hist_entry_diff	diff;
10829750821SNamhyung Kim 		struct /* for TUI */ {
10929750821SNamhyung Kim 			u16	row_offset;
11029750821SNamhyung Kim 			u16	nr_rows;
111d8a0f800SNamhyung Kim 			bool	init_have_children;
1123698dab1SNamhyung Kim 			bool	unfolded;
1133698dab1SNamhyung Kim 			bool	has_children;
11429750821SNamhyung Kim 		};
11529750821SNamhyung Kim 	};
116409a8be6SArnaldo Carvalho de Melo 	char			*srcline;
11731191a85SAndi Kleen 	char			*srcfile;
118439d473bSArnaldo Carvalho de Melo 	struct symbol		*parent;
119dd68ada2SJohn Kacur 	struct rb_root		sorted_chain;
120b5387528SRoberto Agostino Vitillo 	struct branch_info	*branch_info;
121ae359f19SJiri Olsa 	struct hists		*hists;
12298a3b32cSStephane Eranian 	struct mem_info		*mem_info;
12398a3b32cSStephane Eranian 	struct callchain_root	callchain[0]; /* must be last member */
12486a9eee0SArnaldo Carvalho de Melo };
125dd68ada2SJohn Kacur 
126b821c732SArnaldo Carvalho de Melo static inline bool hist_entry__has_pairs(struct hist_entry *he)
127b821c732SArnaldo Carvalho de Melo {
128b821c732SArnaldo Carvalho de Melo 	return !list_empty(&he->pairs.node);
129b821c732SArnaldo Carvalho de Melo }
130b821c732SArnaldo Carvalho de Melo 
131b821c732SArnaldo Carvalho de Melo static inline struct hist_entry *hist_entry__next_pair(struct hist_entry *he)
132b821c732SArnaldo Carvalho de Melo {
133b821c732SArnaldo Carvalho de Melo 	if (hist_entry__has_pairs(he))
134b821c732SArnaldo Carvalho de Melo 		return list_entry(he->pairs.node.next, struct hist_entry, pairs.node);
135b821c732SArnaldo Carvalho de Melo 	return NULL;
136b821c732SArnaldo Carvalho de Melo }
137b821c732SArnaldo Carvalho de Melo 
1384d23322aSJiri Olsa static inline void hist_entry__add_pair(struct hist_entry *pair,
1394d23322aSJiri Olsa 					struct hist_entry *he)
140b821c732SArnaldo Carvalho de Melo {
1414d23322aSJiri Olsa 	list_add_tail(&pair->pairs.node, &he->pairs.head);
142b821c732SArnaldo Carvalho de Melo }
143b821c732SArnaldo Carvalho de Melo 
14414135663SNamhyung Kim static inline float hist_entry__get_percent_limit(struct hist_entry *he)
14514135663SNamhyung Kim {
14614135663SNamhyung Kim 	u64 period = he->stat.period;
14714135663SNamhyung Kim 	u64 total_period = hists__total_period(he->hists);
14814135663SNamhyung Kim 
14914135663SNamhyung Kim 	if (unlikely(total_period == 0))
15014135663SNamhyung Kim 		return 0;
15114135663SNamhyung Kim 
15214135663SNamhyung Kim 	if (symbol_conf.cumulate_callchain)
15314135663SNamhyung Kim 		period = he->stat_acc->period;
15414135663SNamhyung Kim 
15514135663SNamhyung Kim 	return period * 100.0 / total_period;
15614135663SNamhyung Kim }
15714135663SNamhyung Kim 
15814135663SNamhyung Kim 
15955369fc1SNamhyung Kim enum sort_mode {
16055369fc1SNamhyung Kim 	SORT_MODE__NORMAL,
16155369fc1SNamhyung Kim 	SORT_MODE__BRANCH,
16255369fc1SNamhyung Kim 	SORT_MODE__MEMORY,
163512ae1bdSNamhyung Kim 	SORT_MODE__TOP,
164512ae1bdSNamhyung Kim 	SORT_MODE__DIFF,
16555369fc1SNamhyung Kim };
16655369fc1SNamhyung Kim 
167a4fb581bSFrederic Weisbecker enum sort_type {
168fc5871edSNamhyung Kim 	/* common sort keys */
169a4fb581bSFrederic Weisbecker 	SORT_PID,
170a4fb581bSFrederic Weisbecker 	SORT_COMM,
171a4fb581bSFrederic Weisbecker 	SORT_DSO,
172a4fb581bSFrederic Weisbecker 	SORT_SYM,
173f60f3593SArun Sharma 	SORT_PARENT,
174f60f3593SArun Sharma 	SORT_CPU,
175fc5871edSNamhyung Kim 	SORT_SRCLINE,
17631191a85SAndi Kleen 	SORT_SRCFILE,
177f9ea55d0SAndi Kleen 	SORT_LOCAL_WEIGHT,
178f9ea55d0SAndi Kleen 	SORT_GLOBAL_WEIGHT,
179475eeab9SAndi Kleen 	SORT_TRANSACTION,
180fc5871edSNamhyung Kim 
181fc5871edSNamhyung Kim 	/* branch stack specific sort keys */
182fc5871edSNamhyung Kim 	__SORT_BRANCH_STACK,
183fc5871edSNamhyung Kim 	SORT_DSO_FROM = __SORT_BRANCH_STACK,
184b5387528SRoberto Agostino Vitillo 	SORT_DSO_TO,
185b5387528SRoberto Agostino Vitillo 	SORT_SYM_FROM,
186b5387528SRoberto Agostino Vitillo 	SORT_SYM_TO,
187b5387528SRoberto Agostino Vitillo 	SORT_MISPREDICT,
188f5d05bceSAndi Kleen 	SORT_ABORT,
189f5d05bceSAndi Kleen 	SORT_IN_TX,
1900e332f03SAndi Kleen 	SORT_CYCLES,
191afab87b9SNamhyung Kim 
192afab87b9SNamhyung Kim 	/* memory mode specific sort keys */
193afab87b9SNamhyung Kim 	__SORT_MEMORY_MODE,
194f9ea55d0SAndi Kleen 	SORT_MEM_DADDR_SYMBOL = __SORT_MEMORY_MODE,
195afab87b9SNamhyung Kim 	SORT_MEM_DADDR_DSO,
196afab87b9SNamhyung Kim 	SORT_MEM_LOCKED,
197afab87b9SNamhyung Kim 	SORT_MEM_TLB,
198afab87b9SNamhyung Kim 	SORT_MEM_LVL,
199afab87b9SNamhyung Kim 	SORT_MEM_SNOOP,
2009b32ba71SDon Zickus 	SORT_MEM_DCACHELINE,
201a4fb581bSFrederic Weisbecker };
202a4fb581bSFrederic Weisbecker 
203dd68ada2SJohn Kacur /*
204dd68ada2SJohn Kacur  * configurable sorting bits
205dd68ada2SJohn Kacur  */
206dd68ada2SJohn Kacur 
207dd68ada2SJohn Kacur struct sort_entry {
208dd68ada2SJohn Kacur 	struct list_head list;
209dd68ada2SJohn Kacur 
210fcd14984SFrederic Weisbecker 	const char *se_header;
211dd68ada2SJohn Kacur 
212fcd14984SFrederic Weisbecker 	int64_t (*se_cmp)(struct hist_entry *, struct hist_entry *);
213fcd14984SFrederic Weisbecker 	int64_t (*se_collapse)(struct hist_entry *, struct hist_entry *);
214202e7a6dSNamhyung Kim 	int64_t	(*se_sort)(struct hist_entry *, struct hist_entry *);
215316c7136SArnaldo Carvalho de Melo 	int	(*se_snprintf)(struct hist_entry *he, char *bf, size_t size,
216a4e3b956SArnaldo Carvalho de Melo 			       unsigned int width);
2178a6c5b26SArnaldo Carvalho de Melo 	u8	se_width_idx;
218dd68ada2SJohn Kacur };
219dd68ada2SJohn Kacur 
220dd68ada2SJohn Kacur extern struct sort_entry sort_thread;
221dd68ada2SJohn Kacur extern struct list_head hist_entry__sort_list;
222dd68ada2SJohn Kacur 
22355309985SNamhyung Kim int setup_sorting(void);
224a7d945bcSNamhyung Kim int setup_output_field(void);
2251c89fe9bSNamhyung Kim void reset_output_field(void);
226dd68ada2SJohn Kacur extern int sort_dimension__add(const char *);
22708e71542SNamhyung Kim void sort__setup_elide(FILE *fp);
228f2998422SJiri Olsa void perf_hpp__set_elide(int idx, bool elide);
229dd68ada2SJohn Kacur 
230b21484f1SGreg Price int report_parse_ignore_callees_opt(const struct option *opt, const char *arg, int unset);
231b21484f1SGreg Price 
2322f3f9bcfSJiri Olsa bool is_strict_order(const char *order);
233dd68ada2SJohn Kacur #endif	/* __PERF_SORT_H */
234