xref: /openbmc/linux/tools/perf/util/sort.h (revision d8a0f800)
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;
11229750821SNamhyung Kim 		};
11329750821SNamhyung Kim 	};
114409a8be6SArnaldo Carvalho de Melo 	char			*srcline;
115439d473bSArnaldo Carvalho de Melo 	struct symbol		*parent;
116dd68ada2SJohn Kacur 	struct rb_root		sorted_chain;
117b5387528SRoberto Agostino Vitillo 	struct branch_info	*branch_info;
118ae359f19SJiri Olsa 	struct hists		*hists;
11998a3b32cSStephane Eranian 	struct mem_info		*mem_info;
12098a3b32cSStephane Eranian 	struct callchain_root	callchain[0]; /* must be last member */
12186a9eee0SArnaldo Carvalho de Melo };
122dd68ada2SJohn Kacur 
123b821c732SArnaldo Carvalho de Melo static inline bool hist_entry__has_pairs(struct hist_entry *he)
124b821c732SArnaldo Carvalho de Melo {
125b821c732SArnaldo Carvalho de Melo 	return !list_empty(&he->pairs.node);
126b821c732SArnaldo Carvalho de Melo }
127b821c732SArnaldo Carvalho de Melo 
128b821c732SArnaldo Carvalho de Melo static inline struct hist_entry *hist_entry__next_pair(struct hist_entry *he)
129b821c732SArnaldo Carvalho de Melo {
130b821c732SArnaldo Carvalho de Melo 	if (hist_entry__has_pairs(he))
131b821c732SArnaldo Carvalho de Melo 		return list_entry(he->pairs.node.next, struct hist_entry, pairs.node);
132b821c732SArnaldo Carvalho de Melo 	return NULL;
133b821c732SArnaldo Carvalho de Melo }
134b821c732SArnaldo Carvalho de Melo 
1354d23322aSJiri Olsa static inline void hist_entry__add_pair(struct hist_entry *pair,
1364d23322aSJiri Olsa 					struct hist_entry *he)
137b821c732SArnaldo Carvalho de Melo {
1384d23322aSJiri Olsa 	list_add_tail(&pair->pairs.node, &he->pairs.head);
139b821c732SArnaldo Carvalho de Melo }
140b821c732SArnaldo Carvalho de Melo 
14114135663SNamhyung Kim static inline float hist_entry__get_percent_limit(struct hist_entry *he)
14214135663SNamhyung Kim {
14314135663SNamhyung Kim 	u64 period = he->stat.period;
14414135663SNamhyung Kim 	u64 total_period = hists__total_period(he->hists);
14514135663SNamhyung Kim 
14614135663SNamhyung Kim 	if (unlikely(total_period == 0))
14714135663SNamhyung Kim 		return 0;
14814135663SNamhyung Kim 
14914135663SNamhyung Kim 	if (symbol_conf.cumulate_callchain)
15014135663SNamhyung Kim 		period = he->stat_acc->period;
15114135663SNamhyung Kim 
15214135663SNamhyung Kim 	return period * 100.0 / total_period;
15314135663SNamhyung Kim }
15414135663SNamhyung Kim 
15514135663SNamhyung Kim 
15655369fc1SNamhyung Kim enum sort_mode {
15755369fc1SNamhyung Kim 	SORT_MODE__NORMAL,
15855369fc1SNamhyung Kim 	SORT_MODE__BRANCH,
15955369fc1SNamhyung Kim 	SORT_MODE__MEMORY,
160512ae1bdSNamhyung Kim 	SORT_MODE__TOP,
161512ae1bdSNamhyung Kim 	SORT_MODE__DIFF,
16255369fc1SNamhyung Kim };
16355369fc1SNamhyung Kim 
164a4fb581bSFrederic Weisbecker enum sort_type {
165fc5871edSNamhyung Kim 	/* common sort keys */
166a4fb581bSFrederic Weisbecker 	SORT_PID,
167a4fb581bSFrederic Weisbecker 	SORT_COMM,
168a4fb581bSFrederic Weisbecker 	SORT_DSO,
169a4fb581bSFrederic Weisbecker 	SORT_SYM,
170f60f3593SArun Sharma 	SORT_PARENT,
171f60f3593SArun Sharma 	SORT_CPU,
172fc5871edSNamhyung Kim 	SORT_SRCLINE,
173f9ea55d0SAndi Kleen 	SORT_LOCAL_WEIGHT,
174f9ea55d0SAndi Kleen 	SORT_GLOBAL_WEIGHT,
175475eeab9SAndi Kleen 	SORT_TRANSACTION,
176fc5871edSNamhyung Kim 
177fc5871edSNamhyung Kim 	/* branch stack specific sort keys */
178fc5871edSNamhyung Kim 	__SORT_BRANCH_STACK,
179fc5871edSNamhyung Kim 	SORT_DSO_FROM = __SORT_BRANCH_STACK,
180b5387528SRoberto Agostino Vitillo 	SORT_DSO_TO,
181b5387528SRoberto Agostino Vitillo 	SORT_SYM_FROM,
182b5387528SRoberto Agostino Vitillo 	SORT_SYM_TO,
183b5387528SRoberto Agostino Vitillo 	SORT_MISPREDICT,
184f5d05bceSAndi Kleen 	SORT_ABORT,
185f5d05bceSAndi Kleen 	SORT_IN_TX,
186afab87b9SNamhyung Kim 
187afab87b9SNamhyung Kim 	/* memory mode specific sort keys */
188afab87b9SNamhyung Kim 	__SORT_MEMORY_MODE,
189f9ea55d0SAndi Kleen 	SORT_MEM_DADDR_SYMBOL = __SORT_MEMORY_MODE,
190afab87b9SNamhyung Kim 	SORT_MEM_DADDR_DSO,
191afab87b9SNamhyung Kim 	SORT_MEM_LOCKED,
192afab87b9SNamhyung Kim 	SORT_MEM_TLB,
193afab87b9SNamhyung Kim 	SORT_MEM_LVL,
194afab87b9SNamhyung Kim 	SORT_MEM_SNOOP,
1959b32ba71SDon Zickus 	SORT_MEM_DCACHELINE,
196a4fb581bSFrederic Weisbecker };
197a4fb581bSFrederic Weisbecker 
198dd68ada2SJohn Kacur /*
199dd68ada2SJohn Kacur  * configurable sorting bits
200dd68ada2SJohn Kacur  */
201dd68ada2SJohn Kacur 
202dd68ada2SJohn Kacur struct sort_entry {
203dd68ada2SJohn Kacur 	struct list_head list;
204dd68ada2SJohn Kacur 
205fcd14984SFrederic Weisbecker 	const char *se_header;
206dd68ada2SJohn Kacur 
207fcd14984SFrederic Weisbecker 	int64_t (*se_cmp)(struct hist_entry *, struct hist_entry *);
208fcd14984SFrederic Weisbecker 	int64_t (*se_collapse)(struct hist_entry *, struct hist_entry *);
209202e7a6dSNamhyung Kim 	int64_t	(*se_sort)(struct hist_entry *, struct hist_entry *);
210316c7136SArnaldo Carvalho de Melo 	int	(*se_snprintf)(struct hist_entry *he, char *bf, size_t size,
211a4e3b956SArnaldo Carvalho de Melo 			       unsigned int width);
2128a6c5b26SArnaldo Carvalho de Melo 	u8	se_width_idx;
213dd68ada2SJohn Kacur };
214dd68ada2SJohn Kacur 
215dd68ada2SJohn Kacur extern struct sort_entry sort_thread;
216dd68ada2SJohn Kacur extern struct list_head hist_entry__sort_list;
217dd68ada2SJohn Kacur 
21855309985SNamhyung Kim int setup_sorting(void);
219a7d945bcSNamhyung Kim int setup_output_field(void);
2201c89fe9bSNamhyung Kim void reset_output_field(void);
221dd68ada2SJohn Kacur extern int sort_dimension__add(const char *);
22208e71542SNamhyung Kim void sort__setup_elide(FILE *fp);
223f2998422SJiri Olsa void perf_hpp__set_elide(int idx, bool elide);
224dd68ada2SJohn Kacur 
225b21484f1SGreg Price int report_parse_ignore_callees_opt(const struct option *opt, const char *arg, int unset);
226b21484f1SGreg Price 
2272f3f9bcfSJiri Olsa bool is_strict_order(const char *order);
228dd68ada2SJohn Kacur #endif	/* __PERF_SORT_H */
229