xref: /openbmc/linux/tools/perf/util/hist.h (revision ed1666f6)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_HIST_H
3 #define __PERF_HIST_H
4 
5 #include <linux/rbtree.h>
6 #include <linux/types.h>
7 #include <pthread.h>
8 #include "evsel.h"
9 #include "header.h"
10 #include "color.h"
11 #include "ui/progress.h"
12 
13 struct hist_entry;
14 struct hist_entry_ops;
15 struct addr_location;
16 struct map_symbol;
17 struct mem_info;
18 struct branch_info;
19 struct symbol;
20 
21 enum hist_filter {
22 	HIST_FILTER__DSO,
23 	HIST_FILTER__THREAD,
24 	HIST_FILTER__PARENT,
25 	HIST_FILTER__SYMBOL,
26 	HIST_FILTER__GUEST,
27 	HIST_FILTER__HOST,
28 	HIST_FILTER__SOCKET,
29 	HIST_FILTER__C2C,
30 };
31 
32 enum hist_column {
33 	HISTC_SYMBOL,
34 	HISTC_DSO,
35 	HISTC_THREAD,
36 	HISTC_COMM,
37 	HISTC_CGROUP_ID,
38 	HISTC_PARENT,
39 	HISTC_CPU,
40 	HISTC_SOCKET,
41 	HISTC_SRCLINE,
42 	HISTC_SRCFILE,
43 	HISTC_MISPREDICT,
44 	HISTC_IN_TX,
45 	HISTC_ABORT,
46 	HISTC_SYMBOL_FROM,
47 	HISTC_SYMBOL_TO,
48 	HISTC_DSO_FROM,
49 	HISTC_DSO_TO,
50 	HISTC_LOCAL_WEIGHT,
51 	HISTC_GLOBAL_WEIGHT,
52 	HISTC_MEM_DADDR_SYMBOL,
53 	HISTC_MEM_DADDR_DSO,
54 	HISTC_MEM_PHYS_DADDR,
55 	HISTC_MEM_LOCKED,
56 	HISTC_MEM_TLB,
57 	HISTC_MEM_LVL,
58 	HISTC_MEM_SNOOP,
59 	HISTC_MEM_DCACHELINE,
60 	HISTC_MEM_IADDR_SYMBOL,
61 	HISTC_TRANSACTION,
62 	HISTC_CYCLES,
63 	HISTC_SRCLINE_FROM,
64 	HISTC_SRCLINE_TO,
65 	HISTC_TRACE,
66 	HISTC_SYM_SIZE,
67 	HISTC_DSO_SIZE,
68 	HISTC_SYMBOL_IPC,
69 	HISTC_NR_COLS, /* Last entry */
70 };
71 
72 struct thread;
73 struct dso;
74 
75 struct hists {
76 	struct rb_root_cached	entries_in_array[2];
77 	struct rb_root_cached	*entries_in;
78 	struct rb_root_cached	entries;
79 	struct rb_root_cached	entries_collapsed;
80 	u64			nr_entries;
81 	u64			nr_non_filtered_entries;
82 	u64			callchain_period;
83 	u64			callchain_non_filtered_period;
84 	struct thread		*thread_filter;
85 	const struct dso	*dso_filter;
86 	const char		*uid_filter_str;
87 	const char		*symbol_filter_str;
88 	pthread_mutex_t		lock;
89 	struct events_stats	stats;
90 	u64			event_stream;
91 	u16			col_len[HISTC_NR_COLS];
92 	bool			has_callchains;
93 	int			socket_filter;
94 	struct perf_hpp_list	*hpp_list;
95 	struct list_head	hpp_formats;
96 	int			nr_hpp_node;
97 };
98 
99 #define hists__has(__h, __f) (__h)->hpp_list->__f
100 
101 struct hist_entry_iter;
102 
103 struct hist_iter_ops {
104 	int (*prepare_entry)(struct hist_entry_iter *, struct addr_location *);
105 	int (*add_single_entry)(struct hist_entry_iter *, struct addr_location *);
106 	int (*next_entry)(struct hist_entry_iter *, struct addr_location *);
107 	int (*add_next_entry)(struct hist_entry_iter *, struct addr_location *);
108 	int (*finish_entry)(struct hist_entry_iter *, struct addr_location *);
109 };
110 
111 struct hist_entry_iter {
112 	int total;
113 	int curr;
114 
115 	bool hide_unresolved;
116 
117 	struct perf_evsel *evsel;
118 	struct perf_sample *sample;
119 	struct hist_entry *he;
120 	struct symbol *parent;
121 	void *priv;
122 
123 	const struct hist_iter_ops *ops;
124 	/* user-defined callback function (optional) */
125 	int (*add_entry_cb)(struct hist_entry_iter *iter,
126 			    struct addr_location *al, bool single, void *arg);
127 };
128 
129 extern const struct hist_iter_ops hist_iter_normal;
130 extern const struct hist_iter_ops hist_iter_branch;
131 extern const struct hist_iter_ops hist_iter_mem;
132 extern const struct hist_iter_ops hist_iter_cumulative;
133 
134 struct hist_entry *hists__add_entry(struct hists *hists,
135 				    struct addr_location *al,
136 				    struct symbol *parent,
137 				    struct branch_info *bi,
138 				    struct mem_info *mi,
139 				    struct perf_sample *sample,
140 				    bool sample_self);
141 
142 struct hist_entry *hists__add_entry_ops(struct hists *hists,
143 					struct hist_entry_ops *ops,
144 					struct addr_location *al,
145 					struct symbol *sym_parent,
146 					struct branch_info *bi,
147 					struct mem_info *mi,
148 					struct perf_sample *sample,
149 					bool sample_self);
150 
151 int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
152 			 int max_stack_depth, void *arg);
153 
154 struct perf_hpp;
155 struct perf_hpp_fmt;
156 
157 int64_t hist_entry__cmp(struct hist_entry *left, struct hist_entry *right);
158 int64_t hist_entry__collapse(struct hist_entry *left, struct hist_entry *right);
159 int hist_entry__transaction_len(void);
160 int hist_entry__sort_snprintf(struct hist_entry *he, char *bf, size_t size,
161 			      struct hists *hists);
162 int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
163 				   struct perf_hpp_fmt *fmt, int printed);
164 void hist_entry__delete(struct hist_entry *he);
165 
166 typedef int (*hists__resort_cb_t)(struct hist_entry *he, void *arg);
167 
168 void perf_evsel__output_resort_cb(struct perf_evsel *evsel, struct ui_progress *prog,
169 				  hists__resort_cb_t cb, void *cb_arg);
170 void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog);
171 void hists__output_resort(struct hists *hists, struct ui_progress *prog);
172 void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
173 			     hists__resort_cb_t cb);
174 int hists__collapse_resort(struct hists *hists, struct ui_progress *prog);
175 
176 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel);
177 void hists__delete_entries(struct hists *hists);
178 void hists__output_recalc_col_len(struct hists *hists, int max_rows);
179 
180 u64 hists__total_period(struct hists *hists);
181 void hists__reset_stats(struct hists *hists);
182 void hists__inc_stats(struct hists *hists, struct hist_entry *h);
183 void hists__inc_nr_events(struct hists *hists, u32 type);
184 void hists__inc_nr_samples(struct hists *hists, bool filtered);
185 void events_stats__inc(struct events_stats *stats, u32 type);
186 size_t events_stats__fprintf(struct events_stats *stats, FILE *fp);
187 
188 size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
189 		      int max_cols, float min_pcnt, FILE *fp,
190 		      bool ignore_callchains);
191 size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp);
192 
193 void hists__filter_by_dso(struct hists *hists);
194 void hists__filter_by_thread(struct hists *hists);
195 void hists__filter_by_symbol(struct hists *hists);
196 void hists__filter_by_socket(struct hists *hists);
197 
198 static inline bool hists__has_filter(struct hists *hists)
199 {
200 	return hists->thread_filter || hists->dso_filter ||
201 		hists->symbol_filter_str || (hists->socket_filter > -1);
202 }
203 
204 u16 hists__col_len(struct hists *hists, enum hist_column col);
205 void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len);
206 bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len);
207 void hists__reset_col_len(struct hists *hists);
208 void hists__calc_col_len(struct hists *hists, struct hist_entry *he);
209 
210 void hists__match(struct hists *leader, struct hists *other);
211 int hists__link(struct hists *leader, struct hists *other);
212 
213 struct hists_evsel {
214 	struct perf_evsel evsel;
215 	struct hists	  hists;
216 };
217 
218 static inline struct perf_evsel *hists_to_evsel(struct hists *hists)
219 {
220 	struct hists_evsel *hevsel = container_of(hists, struct hists_evsel, hists);
221 	return &hevsel->evsel;
222 }
223 
224 static inline struct hists *evsel__hists(struct perf_evsel *evsel)
225 {
226 	struct hists_evsel *hevsel = (struct hists_evsel *)evsel;
227 	return &hevsel->hists;
228 }
229 
230 static __pure inline bool hists__has_callchains(struct hists *hists)
231 {
232 	return hists->has_callchains;
233 }
234 
235 int hists__init(void);
236 int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list);
237 
238 struct rb_root_cached *hists__get_rotate_entries_in(struct hists *hists);
239 
240 struct perf_hpp {
241 	char *buf;
242 	size_t size;
243 	const char *sep;
244 	void *ptr;
245 };
246 
247 struct perf_hpp_fmt {
248 	const char *name;
249 	int (*header)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
250 		      struct hists *hists, int line, int *span);
251 	int (*width)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
252 		     struct hists *hists);
253 	int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
254 		     struct hist_entry *he);
255 	int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
256 		     struct hist_entry *he);
257 	int64_t (*cmp)(struct perf_hpp_fmt *fmt,
258 		       struct hist_entry *a, struct hist_entry *b);
259 	int64_t (*collapse)(struct perf_hpp_fmt *fmt,
260 			    struct hist_entry *a, struct hist_entry *b);
261 	int64_t (*sort)(struct perf_hpp_fmt *fmt,
262 			struct hist_entry *a, struct hist_entry *b);
263 	bool (*equal)(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b);
264 	void (*free)(struct perf_hpp_fmt *fmt);
265 
266 	struct list_head list;
267 	struct list_head sort_list;
268 	bool elide;
269 	int len;
270 	int user_len;
271 	int idx;
272 	int level;
273 };
274 
275 struct perf_hpp_list {
276 	struct list_head fields;
277 	struct list_head sorts;
278 
279 	int nr_header_lines;
280 	int need_collapse;
281 	int parent;
282 	int sym;
283 	int dso;
284 	int socket;
285 	int thread;
286 	int comm;
287 };
288 
289 extern struct perf_hpp_list perf_hpp_list;
290 
291 struct perf_hpp_list_node {
292 	struct list_head	list;
293 	struct perf_hpp_list	hpp;
294 	int			level;
295 	bool			skip;
296 };
297 
298 void perf_hpp_list__column_register(struct perf_hpp_list *list,
299 				    struct perf_hpp_fmt *format);
300 void perf_hpp_list__register_sort_field(struct perf_hpp_list *list,
301 					struct perf_hpp_fmt *format);
302 void perf_hpp_list__prepend_sort_field(struct perf_hpp_list *list,
303 				       struct perf_hpp_fmt *format);
304 
305 static inline void perf_hpp__column_register(struct perf_hpp_fmt *format)
306 {
307 	perf_hpp_list__column_register(&perf_hpp_list, format);
308 }
309 
310 static inline void perf_hpp__register_sort_field(struct perf_hpp_fmt *format)
311 {
312 	perf_hpp_list__register_sort_field(&perf_hpp_list, format);
313 }
314 
315 static inline void perf_hpp__prepend_sort_field(struct perf_hpp_fmt *format)
316 {
317 	perf_hpp_list__prepend_sort_field(&perf_hpp_list, format);
318 }
319 
320 #define perf_hpp_list__for_each_format(_list, format) \
321 	list_for_each_entry(format, &(_list)->fields, list)
322 
323 #define perf_hpp_list__for_each_format_safe(_list, format, tmp)	\
324 	list_for_each_entry_safe(format, tmp, &(_list)->fields, list)
325 
326 #define perf_hpp_list__for_each_sort_list(_list, format) \
327 	list_for_each_entry(format, &(_list)->sorts, sort_list)
328 
329 #define perf_hpp_list__for_each_sort_list_safe(_list, format, tmp)	\
330 	list_for_each_entry_safe(format, tmp, &(_list)->sorts, sort_list)
331 
332 #define hists__for_each_format(hists, format) \
333 	perf_hpp_list__for_each_format((hists)->hpp_list, fmt)
334 
335 #define hists__for_each_sort_list(hists, format) \
336 	perf_hpp_list__for_each_sort_list((hists)->hpp_list, fmt)
337 
338 extern struct perf_hpp_fmt perf_hpp__format[];
339 
340 enum {
341 	/* Matches perf_hpp__format array. */
342 	PERF_HPP__OVERHEAD,
343 	PERF_HPP__OVERHEAD_SYS,
344 	PERF_HPP__OVERHEAD_US,
345 	PERF_HPP__OVERHEAD_GUEST_SYS,
346 	PERF_HPP__OVERHEAD_GUEST_US,
347 	PERF_HPP__OVERHEAD_ACC,
348 	PERF_HPP__SAMPLES,
349 	PERF_HPP__PERIOD,
350 
351 	PERF_HPP__MAX_INDEX
352 };
353 
354 void perf_hpp__init(void);
355 void perf_hpp__column_unregister(struct perf_hpp_fmt *format);
356 void perf_hpp__cancel_cumulate(void);
357 void perf_hpp__setup_output_field(struct perf_hpp_list *list);
358 void perf_hpp__reset_output_field(struct perf_hpp_list *list);
359 void perf_hpp__append_sort_keys(struct perf_hpp_list *list);
360 int perf_hpp__setup_hists_formats(struct perf_hpp_list *list,
361 				  struct perf_evlist *evlist);
362 
363 
364 bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format);
365 bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *format);
366 bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists);
367 bool perf_hpp__is_trace_entry(struct perf_hpp_fmt *fmt);
368 bool perf_hpp__is_srcline_entry(struct perf_hpp_fmt *fmt);
369 bool perf_hpp__is_srcfile_entry(struct perf_hpp_fmt *fmt);
370 bool perf_hpp__is_thread_entry(struct perf_hpp_fmt *fmt);
371 bool perf_hpp__is_comm_entry(struct perf_hpp_fmt *fmt);
372 bool perf_hpp__is_dso_entry(struct perf_hpp_fmt *fmt);
373 bool perf_hpp__is_sym_entry(struct perf_hpp_fmt *fmt);
374 
375 struct perf_hpp_fmt *perf_hpp_fmt__dup(struct perf_hpp_fmt *fmt);
376 
377 int hist_entry__filter(struct hist_entry *he, int type, const void *arg);
378 
379 static inline bool perf_hpp__should_skip(struct perf_hpp_fmt *format,
380 					 struct hists *hists)
381 {
382 	if (format->elide)
383 		return true;
384 
385 	if (perf_hpp__is_dynamic_entry(format) &&
386 	    !perf_hpp__defined_dynamic_entry(format, hists))
387 		return true;
388 
389 	return false;
390 }
391 
392 void perf_hpp__reset_width(struct perf_hpp_fmt *fmt, struct hists *hists);
393 void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists);
394 void perf_hpp__set_user_width(const char *width_list_str);
395 void hists__reset_column_width(struct hists *hists);
396 
397 typedef u64 (*hpp_field_fn)(struct hist_entry *he);
398 typedef int (*hpp_callback_fn)(struct perf_hpp *hpp, bool front);
399 typedef int (*hpp_snprint_fn)(struct perf_hpp *hpp, const char *fmt, ...);
400 
401 int hpp__fmt(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
402 	     struct hist_entry *he, hpp_field_fn get_field,
403 	     const char *fmtstr, hpp_snprint_fn print_fn, bool fmt_percent);
404 int hpp__fmt_acc(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
405 		 struct hist_entry *he, hpp_field_fn get_field,
406 		 const char *fmtstr, hpp_snprint_fn print_fn, bool fmt_percent);
407 
408 static inline void advance_hpp(struct perf_hpp *hpp, int inc)
409 {
410 	hpp->buf  += inc;
411 	hpp->size -= inc;
412 }
413 
414 static inline size_t perf_hpp__use_color(void)
415 {
416 	return !symbol_conf.field_sep;
417 }
418 
419 static inline size_t perf_hpp__color_overhead(void)
420 {
421 	return perf_hpp__use_color() ?
422 	       (COLOR_MAXLEN + sizeof(PERF_COLOR_RESET)) * PERF_HPP__MAX_INDEX
423 	       : 0;
424 }
425 
426 struct perf_evlist;
427 
428 struct hist_browser_timer {
429 	void (*timer)(void *arg);
430 	void *arg;
431 	int refresh;
432 };
433 
434 struct annotation_options;
435 
436 #ifdef HAVE_SLANG_SUPPORT
437 #include "../ui/keysyms.h"
438 int map_symbol__tui_annotate(struct map_symbol *ms, struct perf_evsel *evsel,
439 			     struct hist_browser_timer *hbt,
440 			     struct annotation_options *annotation_opts);
441 
442 int hist_entry__tui_annotate(struct hist_entry *he, struct perf_evsel *evsel,
443 			     struct hist_browser_timer *hbt,
444 			     struct annotation_options *annotation_opts);
445 
446 int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help,
447 				  struct hist_browser_timer *hbt,
448 				  float min_pcnt,
449 				  struct perf_env *env,
450 				  bool warn_lost_event,
451 				  struct annotation_options *annotation_options);
452 int script_browse(const char *script_opt);
453 #else
454 static inline
455 int perf_evlist__tui_browse_hists(struct perf_evlist *evlist __maybe_unused,
456 				  const char *help __maybe_unused,
457 				  struct hist_browser_timer *hbt __maybe_unused,
458 				  float min_pcnt __maybe_unused,
459 				  struct perf_env *env __maybe_unused,
460 				  bool warn_lost_event __maybe_unused,
461 				  struct annotation_options *annotation_options __maybe_unused)
462 {
463 	return 0;
464 }
465 static inline int map_symbol__tui_annotate(struct map_symbol *ms __maybe_unused,
466 					   struct perf_evsel *evsel __maybe_unused,
467 					   struct hist_browser_timer *hbt __maybe_unused,
468 					   struct annotation_options *annotation_options __maybe_unused)
469 {
470 	return 0;
471 }
472 
473 static inline int hist_entry__tui_annotate(struct hist_entry *he __maybe_unused,
474 					   struct perf_evsel *evsel __maybe_unused,
475 					   struct hist_browser_timer *hbt __maybe_unused,
476 					   struct annotation_options *annotation_opts __maybe_unused)
477 {
478 	return 0;
479 }
480 
481 static inline int script_browse(const char *script_opt __maybe_unused)
482 {
483 	return 0;
484 }
485 
486 #define K_LEFT  -1000
487 #define K_RIGHT -2000
488 #define K_SWITCH_INPUT_DATA -3000
489 #endif
490 
491 unsigned int hists__sort_list_width(struct hists *hists);
492 unsigned int hists__overhead_width(struct hists *hists);
493 
494 void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
495 			  struct perf_sample *sample, bool nonany_branch_mode);
496 
497 struct option;
498 int parse_filter_percentage(const struct option *opt, const char *arg, int unset);
499 int perf_hist_config(const char *var, const char *value);
500 
501 void perf_hpp_list__init(struct perf_hpp_list *list);
502 
503 enum hierarchy_move_dir {
504 	HMD_NORMAL,
505 	HMD_FORCE_SIBLING,
506 	HMD_FORCE_CHILD,
507 };
508 
509 struct rb_node *rb_hierarchy_last(struct rb_node *node);
510 struct rb_node *__rb_hierarchy_next(struct rb_node *node,
511 				    enum hierarchy_move_dir hmd);
512 struct rb_node *rb_hierarchy_prev(struct rb_node *node);
513 
514 static inline struct rb_node *rb_hierarchy_next(struct rb_node *node)
515 {
516 	return __rb_hierarchy_next(node, HMD_NORMAL);
517 }
518 
519 #define HIERARCHY_INDENT  3
520 
521 bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit);
522 int hpp_color_scnprintf(struct perf_hpp *hpp, const char *fmt, ...);
523 int __hpp__slsmg_color_printf(struct perf_hpp *hpp, const char *fmt, ...);
524 int __hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp,
525 			   struct perf_hpp_list *hpp_list);
526 int hists__fprintf_headers(struct hists *hists, FILE *fp);
527 int __hists__scnprintf_title(struct hists *hists, char *bf, size_t size, bool show_freq);
528 
529 static inline int hists__scnprintf_title(struct hists *hists, char *bf, size_t size)
530 {
531 	return __hists__scnprintf_title(hists, bf, size, true);
532 }
533 
534 #endif	/* __PERF_HIST_H */
535