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