xref: /openbmc/linux/tools/perf/util/sort.h (revision 2e7ea3ab)
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;
372e7ea3abSKan Liang extern int sort__has_socket;
3855369fc1SNamhyung Kim extern enum sort_mode sort__mode;
39dd68ada2SJohn Kacur extern struct sort_entry sort_comm;
40dd68ada2SJohn Kacur extern struct sort_entry sort_dso;
41dd68ada2SJohn Kacur extern struct sort_entry sort_sym;
42dd68ada2SJohn Kacur extern struct sort_entry sort_parent;
43a68c2c58SStephane Eranian extern struct sort_entry sort_dso_from;
44a68c2c58SStephane Eranian extern struct sort_entry sort_dso_to;
45a68c2c58SStephane Eranian extern struct sort_entry sort_sym_from;
46a68c2c58SStephane Eranian extern struct sort_entry sort_sym_to;
47a4fb581bSFrederic Weisbecker extern enum sort_type sort__first_dimension;
48228f14f2SYunlong Song extern const char default_mem_sort_order[];
49dd68ada2SJohn Kacur 
50b24c28f7SNamhyung Kim struct he_stat {
51b24c28f7SNamhyung Kim 	u64			period;
52b24c28f7SNamhyung Kim 	u64			period_sys;
53b24c28f7SNamhyung Kim 	u64			period_us;
54b24c28f7SNamhyung Kim 	u64			period_guest_sys;
55b24c28f7SNamhyung Kim 	u64			period_guest_us;
5605484298SAndi Kleen 	u64			weight;
57b24c28f7SNamhyung Kim 	u32			nr_events;
58b24c28f7SNamhyung Kim };
59b24c28f7SNamhyung Kim 
6096c47f19SJiri Olsa struct hist_entry_diff {
6196c47f19SJiri Olsa 	bool	computed;
62a0b404f4SNamhyung Kim 	union {
6396c47f19SJiri Olsa 		/* PERF_HPP__DELTA */
6496c47f19SJiri Olsa 		double	period_ratio_delta;
6596c47f19SJiri Olsa 
6696c47f19SJiri Olsa 		/* PERF_HPP__RATIO */
6796c47f19SJiri Olsa 		double	period_ratio;
6881d5f958SJiri Olsa 
6981d5f958SJiri Olsa 		/* HISTC_WEIGHTED_DIFF */
7081d5f958SJiri Olsa 		s64	wdiff;
7196c47f19SJiri Olsa 	};
72a0b404f4SNamhyung Kim };
7396c47f19SJiri Olsa 
740f0cbf7aSArnaldo Carvalho de Melo /**
750f0cbf7aSArnaldo Carvalho de Melo  * struct hist_entry - histogram entry
760f0cbf7aSArnaldo Carvalho de Melo  *
770f0cbf7aSArnaldo Carvalho de Melo  * @row_offset - offset from the first callchain expanded to appear on screen
780f0cbf7aSArnaldo Carvalho de Melo  * @nr_rows - rows expanded in callchain, recalculated on folding/unfolding
790f0cbf7aSArnaldo Carvalho de Melo  */
80dd68ada2SJohn Kacur struct hist_entry {
811980c2ebSArnaldo Carvalho de Melo 	struct rb_node		rb_node_in;
82dd68ada2SJohn Kacur 	struct rb_node		rb_node;
83b821c732SArnaldo Carvalho de Melo 	union {
84b821c732SArnaldo Carvalho de Melo 		struct list_head node;
85b821c732SArnaldo Carvalho de Melo 		struct list_head head;
86b821c732SArnaldo Carvalho de Melo 	} pairs;
87b24c28f7SNamhyung Kim 	struct he_stat		stat;
88f8be1c8cSNamhyung Kim 	struct he_stat		*stat_acc;
8959fd5306SArnaldo Carvalho de Melo 	struct map_symbol	ms;
90a5e29acaSArnaldo Carvalho de Melo 	struct thread		*thread;
914dfced35SNamhyung Kim 	struct comm		*comm;
92dd68ada2SJohn Kacur 	u64			ip;
93475eeab9SAndi Kleen 	u64			transaction;
940c4c4debSKan Liang 	s32			socket;
95f60f3593SArun Sharma 	s32			cpu;
967365be55SDon Zickus 	u8			cpumode;
970f0cbf7aSArnaldo Carvalho de Melo 
98e0af43d2SJiri Olsa 	/* We are added by hists__add_dummy_entry. */
99e0af43d2SJiri Olsa 	bool			dummy;
100e0af43d2SJiri Olsa 
101dd68ada2SJohn Kacur 	char			level;
102a5e29acaSArnaldo Carvalho de Melo 	u8			filtered;
10329750821SNamhyung Kim 	union {
10429750821SNamhyung Kim 		/*
10529750821SNamhyung Kim 		 * Since perf diff only supports the stdio output, TUI
10629750821SNamhyung Kim 		 * fields are only accessed from perf report (or perf
10729750821SNamhyung Kim 		 * top).  So make it an union to reduce memory usage.
10829750821SNamhyung Kim 		 */
10929750821SNamhyung Kim 		struct hist_entry_diff	diff;
11029750821SNamhyung Kim 		struct /* for TUI */ {
11129750821SNamhyung Kim 			u16	row_offset;
11229750821SNamhyung Kim 			u16	nr_rows;
113d8a0f800SNamhyung Kim 			bool	init_have_children;
1143698dab1SNamhyung Kim 			bool	unfolded;
1153698dab1SNamhyung Kim 			bool	has_children;
11629750821SNamhyung Kim 		};
11729750821SNamhyung Kim 	};
118409a8be6SArnaldo Carvalho de Melo 	char			*srcline;
11931191a85SAndi Kleen 	char			*srcfile;
120439d473bSArnaldo Carvalho de Melo 	struct symbol		*parent;
121dd68ada2SJohn Kacur 	struct rb_root		sorted_chain;
122b5387528SRoberto Agostino Vitillo 	struct branch_info	*branch_info;
123ae359f19SJiri Olsa 	struct hists		*hists;
12498a3b32cSStephane Eranian 	struct mem_info		*mem_info;
12598a3b32cSStephane Eranian 	struct callchain_root	callchain[0]; /* must be last member */
12686a9eee0SArnaldo Carvalho de Melo };
127dd68ada2SJohn Kacur 
128b821c732SArnaldo Carvalho de Melo static inline bool hist_entry__has_pairs(struct hist_entry *he)
129b821c732SArnaldo Carvalho de Melo {
130b821c732SArnaldo Carvalho de Melo 	return !list_empty(&he->pairs.node);
131b821c732SArnaldo Carvalho de Melo }
132b821c732SArnaldo Carvalho de Melo 
133b821c732SArnaldo Carvalho de Melo static inline struct hist_entry *hist_entry__next_pair(struct hist_entry *he)
134b821c732SArnaldo Carvalho de Melo {
135b821c732SArnaldo Carvalho de Melo 	if (hist_entry__has_pairs(he))
136b821c732SArnaldo Carvalho de Melo 		return list_entry(he->pairs.node.next, struct hist_entry, pairs.node);
137b821c732SArnaldo Carvalho de Melo 	return NULL;
138b821c732SArnaldo Carvalho de Melo }
139b821c732SArnaldo Carvalho de Melo 
1404d23322aSJiri Olsa static inline void hist_entry__add_pair(struct hist_entry *pair,
1414d23322aSJiri Olsa 					struct hist_entry *he)
142b821c732SArnaldo Carvalho de Melo {
1434d23322aSJiri Olsa 	list_add_tail(&pair->pairs.node, &he->pairs.head);
144b821c732SArnaldo Carvalho de Melo }
145b821c732SArnaldo Carvalho de Melo 
14614135663SNamhyung Kim static inline float hist_entry__get_percent_limit(struct hist_entry *he)
14714135663SNamhyung Kim {
14814135663SNamhyung Kim 	u64 period = he->stat.period;
14914135663SNamhyung Kim 	u64 total_period = hists__total_period(he->hists);
15014135663SNamhyung Kim 
15114135663SNamhyung Kim 	if (unlikely(total_period == 0))
15214135663SNamhyung Kim 		return 0;
15314135663SNamhyung Kim 
15414135663SNamhyung Kim 	if (symbol_conf.cumulate_callchain)
15514135663SNamhyung Kim 		period = he->stat_acc->period;
15614135663SNamhyung Kim 
15714135663SNamhyung Kim 	return period * 100.0 / total_period;
15814135663SNamhyung Kim }
15914135663SNamhyung Kim 
16014135663SNamhyung Kim 
16155369fc1SNamhyung Kim enum sort_mode {
16255369fc1SNamhyung Kim 	SORT_MODE__NORMAL,
16355369fc1SNamhyung Kim 	SORT_MODE__BRANCH,
16455369fc1SNamhyung Kim 	SORT_MODE__MEMORY,
165512ae1bdSNamhyung Kim 	SORT_MODE__TOP,
166512ae1bdSNamhyung Kim 	SORT_MODE__DIFF,
16755369fc1SNamhyung Kim };
16855369fc1SNamhyung Kim 
169a4fb581bSFrederic Weisbecker enum sort_type {
170fc5871edSNamhyung Kim 	/* common sort keys */
171a4fb581bSFrederic Weisbecker 	SORT_PID,
172a4fb581bSFrederic Weisbecker 	SORT_COMM,
173a4fb581bSFrederic Weisbecker 	SORT_DSO,
174a4fb581bSFrederic Weisbecker 	SORT_SYM,
175f60f3593SArun Sharma 	SORT_PARENT,
176f60f3593SArun Sharma 	SORT_CPU,
1772e7ea3abSKan Liang 	SORT_SOCKET,
178fc5871edSNamhyung Kim 	SORT_SRCLINE,
17931191a85SAndi Kleen 	SORT_SRCFILE,
180f9ea55d0SAndi Kleen 	SORT_LOCAL_WEIGHT,
181f9ea55d0SAndi Kleen 	SORT_GLOBAL_WEIGHT,
182475eeab9SAndi Kleen 	SORT_TRANSACTION,
183fc5871edSNamhyung Kim 
184fc5871edSNamhyung Kim 	/* branch stack specific sort keys */
185fc5871edSNamhyung Kim 	__SORT_BRANCH_STACK,
186fc5871edSNamhyung Kim 	SORT_DSO_FROM = __SORT_BRANCH_STACK,
187b5387528SRoberto Agostino Vitillo 	SORT_DSO_TO,
188b5387528SRoberto Agostino Vitillo 	SORT_SYM_FROM,
189b5387528SRoberto Agostino Vitillo 	SORT_SYM_TO,
190b5387528SRoberto Agostino Vitillo 	SORT_MISPREDICT,
191f5d05bceSAndi Kleen 	SORT_ABORT,
192f5d05bceSAndi Kleen 	SORT_IN_TX,
1930e332f03SAndi Kleen 	SORT_CYCLES,
194afab87b9SNamhyung Kim 
195afab87b9SNamhyung Kim 	/* memory mode specific sort keys */
196afab87b9SNamhyung Kim 	__SORT_MEMORY_MODE,
197f9ea55d0SAndi Kleen 	SORT_MEM_DADDR_SYMBOL = __SORT_MEMORY_MODE,
198afab87b9SNamhyung Kim 	SORT_MEM_DADDR_DSO,
199afab87b9SNamhyung Kim 	SORT_MEM_LOCKED,
200afab87b9SNamhyung Kim 	SORT_MEM_TLB,
201afab87b9SNamhyung Kim 	SORT_MEM_LVL,
202afab87b9SNamhyung Kim 	SORT_MEM_SNOOP,
2039b32ba71SDon Zickus 	SORT_MEM_DCACHELINE,
204a4fb581bSFrederic Weisbecker };
205a4fb581bSFrederic Weisbecker 
206dd68ada2SJohn Kacur /*
207dd68ada2SJohn Kacur  * configurable sorting bits
208dd68ada2SJohn Kacur  */
209dd68ada2SJohn Kacur 
210dd68ada2SJohn Kacur struct sort_entry {
211dd68ada2SJohn Kacur 	struct list_head list;
212dd68ada2SJohn Kacur 
213fcd14984SFrederic Weisbecker 	const char *se_header;
214dd68ada2SJohn Kacur 
215fcd14984SFrederic Weisbecker 	int64_t (*se_cmp)(struct hist_entry *, struct hist_entry *);
216fcd14984SFrederic Weisbecker 	int64_t (*se_collapse)(struct hist_entry *, struct hist_entry *);
217202e7a6dSNamhyung Kim 	int64_t	(*se_sort)(struct hist_entry *, struct hist_entry *);
218316c7136SArnaldo Carvalho de Melo 	int	(*se_snprintf)(struct hist_entry *he, char *bf, size_t size,
219a4e3b956SArnaldo Carvalho de Melo 			       unsigned int width);
2208a6c5b26SArnaldo Carvalho de Melo 	u8	se_width_idx;
221dd68ada2SJohn Kacur };
222dd68ada2SJohn Kacur 
223dd68ada2SJohn Kacur extern struct sort_entry sort_thread;
224dd68ada2SJohn Kacur extern struct list_head hist_entry__sort_list;
225dd68ada2SJohn Kacur 
22655309985SNamhyung Kim int setup_sorting(void);
227a7d945bcSNamhyung Kim int setup_output_field(void);
2281c89fe9bSNamhyung Kim void reset_output_field(void);
229dd68ada2SJohn Kacur extern int sort_dimension__add(const char *);
23008e71542SNamhyung Kim void sort__setup_elide(FILE *fp);
231f2998422SJiri Olsa void perf_hpp__set_elide(int idx, bool elide);
232dd68ada2SJohn Kacur 
233b21484f1SGreg Price int report_parse_ignore_callees_opt(const struct option *opt, const char *arg, int unset);
234b21484f1SGreg Price 
2352f3f9bcfSJiri Olsa bool is_strict_order(const char *order);
236dd68ada2SJohn Kacur #endif	/* __PERF_SORT_H */
237