xref: /openbmc/linux/tools/perf/util/sort.h (revision 9bb94643)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_SORT_H
3 #define __PERF_SORT_H
4 #include "../builtin.h"
5 
6 #include <regex.h>
7 
8 #include "color.h"
9 #include <linux/list.h>
10 #include "cache.h"
11 #include <linux/rbtree.h>
12 #include "map_symbol.h"
13 #include "symbol_conf.h"
14 #include "string.h"
15 #include "callchain.h"
16 #include "values.h"
17 
18 #include "../perf.h"
19 #include "debug.h"
20 #include "header.h"
21 
22 #include <subcmd/parse-options.h>
23 #include "parse-events.h"
24 #include "hist.h"
25 #include "srcline.h"
26 
27 struct thread;
28 
29 extern regex_t parent_regex;
30 extern const char *sort_order;
31 extern const char *field_order;
32 extern const char default_parent_pattern[];
33 extern const char *parent_pattern;
34 extern const char *default_sort_order;
35 extern regex_t ignore_callees_regex;
36 extern int have_ignore_callees;
37 extern enum sort_mode sort__mode;
38 extern struct sort_entry sort_comm;
39 extern struct sort_entry sort_dso;
40 extern struct sort_entry sort_sym;
41 extern struct sort_entry sort_parent;
42 extern struct sort_entry sort_dso_from;
43 extern struct sort_entry sort_dso_to;
44 extern struct sort_entry sort_sym_from;
45 extern struct sort_entry sort_sym_to;
46 extern struct sort_entry sort_srcline;
47 extern enum sort_type sort__first_dimension;
48 extern const char default_mem_sort_order[];
49 
50 struct he_stat {
51 	u64			period;
52 	u64			period_sys;
53 	u64			period_us;
54 	u64			period_guest_sys;
55 	u64			period_guest_us;
56 	u64			weight;
57 	u32			nr_events;
58 };
59 
60 struct namespace_id {
61 	u64			dev;
62 	u64			ino;
63 };
64 
65 struct hist_entry_diff {
66 	bool	computed;
67 	union {
68 		/* PERF_HPP__DELTA */
69 		double	period_ratio_delta;
70 
71 		/* PERF_HPP__RATIO */
72 		double	period_ratio;
73 
74 		/* HISTC_WEIGHTED_DIFF */
75 		s64	wdiff;
76 	};
77 };
78 
79 struct hist_entry_ops {
80 	void	*(*new)(size_t size);
81 	void	(*free)(void *ptr);
82 };
83 
84 /**
85  * struct hist_entry - histogram entry
86  *
87  * @row_offset - offset from the first callchain expanded to appear on screen
88  * @nr_rows - rows expanded in callchain, recalculated on folding/unfolding
89  */
90 struct hist_entry {
91 	struct rb_node		rb_node_in;
92 	struct rb_node		rb_node;
93 	union {
94 		struct list_head node;
95 		struct list_head head;
96 	} pairs;
97 	struct he_stat		stat;
98 	struct he_stat		*stat_acc;
99 	struct map_symbol	ms;
100 	struct thread		*thread;
101 	struct comm		*comm;
102 	struct namespace_id	cgroup_id;
103 	u64			ip;
104 	u64			transaction;
105 	s32			socket;
106 	s32			cpu;
107 	u8			cpumode;
108 	u8			depth;
109 
110 	/* We are added by hists__add_dummy_entry. */
111 	bool			dummy;
112 	bool			leaf;
113 
114 	char			level;
115 	u8			filtered;
116 
117 	u16			callchain_size;
118 	union {
119 		/*
120 		 * Since perf diff only supports the stdio output, TUI
121 		 * fields are only accessed from perf report (or perf
122 		 * top).  So make it a union to reduce memory usage.
123 		 */
124 		struct hist_entry_diff	diff;
125 		struct /* for TUI */ {
126 			u16	row_offset;
127 			u16	nr_rows;
128 			bool	init_have_children;
129 			bool	unfolded;
130 			bool	has_children;
131 			bool	has_no_entry;
132 		};
133 	};
134 	char			*srcline;
135 	char			*srcfile;
136 	struct symbol		*parent;
137 	struct branch_info	*branch_info;
138 	struct hists		*hists;
139 	struct mem_info		*mem_info;
140 	void			*raw_data;
141 	u32			raw_size;
142 	void			*trace_output;
143 	struct perf_hpp_list	*hpp_list;
144 	struct hist_entry	*parent_he;
145 	struct hist_entry_ops	*ops;
146 	union {
147 		/* this is for hierarchical entry structure */
148 		struct {
149 			struct rb_root_cached	hroot_in;
150 			struct rb_root_cached   hroot_out;
151 		};				/* non-leaf entries */
152 		struct rb_root	sorted_chain;	/* leaf entry has callchains */
153 	};
154 	struct callchain_root	callchain[0]; /* must be last member */
155 };
156 
157 static __pure inline bool hist_entry__has_callchains(struct hist_entry *he)
158 {
159 	return he->callchain_size != 0;
160 }
161 
162 static inline bool hist_entry__has_pairs(struct hist_entry *he)
163 {
164 	return !list_empty(&he->pairs.node);
165 }
166 
167 static inline struct hist_entry *hist_entry__next_pair(struct hist_entry *he)
168 {
169 	if (hist_entry__has_pairs(he))
170 		return list_entry(he->pairs.node.next, struct hist_entry, pairs.node);
171 	return NULL;
172 }
173 
174 static inline void hist_entry__add_pair(struct hist_entry *pair,
175 					struct hist_entry *he)
176 {
177 	list_add_tail(&pair->pairs.node, &he->pairs.head);
178 }
179 
180 static inline float hist_entry__get_percent_limit(struct hist_entry *he)
181 {
182 	u64 period = he->stat.period;
183 	u64 total_period = hists__total_period(he->hists);
184 
185 	if (unlikely(total_period == 0))
186 		return 0;
187 
188 	if (symbol_conf.cumulate_callchain)
189 		period = he->stat_acc->period;
190 
191 	return period * 100.0 / total_period;
192 }
193 
194 static inline u64 cl_address(u64 address)
195 {
196 	/* return the cacheline of the address */
197 	return (address & ~(cacheline_size() - 1));
198 }
199 
200 static inline u64 cl_offset(u64 address)
201 {
202 	/* return the cacheline of the address */
203 	return (address & (cacheline_size() - 1));
204 }
205 
206 enum sort_mode {
207 	SORT_MODE__NORMAL,
208 	SORT_MODE__BRANCH,
209 	SORT_MODE__MEMORY,
210 	SORT_MODE__TOP,
211 	SORT_MODE__DIFF,
212 	SORT_MODE__TRACEPOINT,
213 };
214 
215 enum sort_type {
216 	/* common sort keys */
217 	SORT_PID,
218 	SORT_COMM,
219 	SORT_DSO,
220 	SORT_SYM,
221 	SORT_PARENT,
222 	SORT_CPU,
223 	SORT_SOCKET,
224 	SORT_SRCLINE,
225 	SORT_SRCFILE,
226 	SORT_LOCAL_WEIGHT,
227 	SORT_GLOBAL_WEIGHT,
228 	SORT_TRANSACTION,
229 	SORT_TRACE,
230 	SORT_SYM_SIZE,
231 	SORT_DSO_SIZE,
232 	SORT_CGROUP_ID,
233 	SORT_SYM_IPC_NULL,
234 
235 	/* branch stack specific sort keys */
236 	__SORT_BRANCH_STACK,
237 	SORT_DSO_FROM = __SORT_BRANCH_STACK,
238 	SORT_DSO_TO,
239 	SORT_SYM_FROM,
240 	SORT_SYM_TO,
241 	SORT_MISPREDICT,
242 	SORT_ABORT,
243 	SORT_IN_TX,
244 	SORT_CYCLES,
245 	SORT_SRCLINE_FROM,
246 	SORT_SRCLINE_TO,
247 	SORT_SYM_IPC,
248 
249 	/* memory mode specific sort keys */
250 	__SORT_MEMORY_MODE,
251 	SORT_MEM_DADDR_SYMBOL = __SORT_MEMORY_MODE,
252 	SORT_MEM_DADDR_DSO,
253 	SORT_MEM_LOCKED,
254 	SORT_MEM_TLB,
255 	SORT_MEM_LVL,
256 	SORT_MEM_SNOOP,
257 	SORT_MEM_DCACHELINE,
258 	SORT_MEM_IADDR_SYMBOL,
259 	SORT_MEM_PHYS_DADDR,
260 };
261 
262 /*
263  * configurable sorting bits
264  */
265 
266 struct sort_entry {
267 	const char *se_header;
268 
269 	int64_t (*se_cmp)(struct hist_entry *, struct hist_entry *);
270 	int64_t (*se_collapse)(struct hist_entry *, struct hist_entry *);
271 	int64_t	(*se_sort)(struct hist_entry *, struct hist_entry *);
272 	int	(*se_snprintf)(struct hist_entry *he, char *bf, size_t size,
273 			       unsigned int width);
274 	int	(*se_filter)(struct hist_entry *he, int type, const void *arg);
275 	u8	se_width_idx;
276 };
277 
278 extern struct sort_entry sort_thread;
279 extern struct list_head hist_entry__sort_list;
280 
281 struct perf_evlist;
282 struct tep_handle;
283 int setup_sorting(struct perf_evlist *evlist);
284 int setup_output_field(void);
285 void reset_output_field(void);
286 void sort__setup_elide(FILE *fp);
287 void perf_hpp__set_elide(int idx, bool elide);
288 
289 int report_parse_ignore_callees_opt(const struct option *opt, const char *arg, int unset);
290 
291 bool is_strict_order(const char *order);
292 
293 int hpp_dimension__add_output(unsigned col);
294 void reset_dimensions(void);
295 int sort_dimension__add(struct perf_hpp_list *list, const char *tok,
296 			struct perf_evlist *evlist,
297 			int level);
298 int output_field_add(struct perf_hpp_list *list, char *tok);
299 int64_t
300 sort__iaddr_cmp(struct hist_entry *left, struct hist_entry *right);
301 int64_t
302 sort__daddr_cmp(struct hist_entry *left, struct hist_entry *right);
303 int64_t
304 sort__dcacheline_cmp(struct hist_entry *left, struct hist_entry *right);
305 char *hist_entry__srcline(struct hist_entry *he);
306 #endif	/* __PERF_SORT_H */
307