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