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