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