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