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