0001
0002 #ifndef __PERF_HIST_H
0003 #define __PERF_HIST_H
0004
0005 #include <linux/rbtree.h>
0006 #include <linux/types.h>
0007 #include <pthread.h>
0008 #include "evsel.h"
0009 #include "color.h"
0010 #include "events_stats.h"
0011
0012 struct hist_entry;
0013 struct hist_entry_ops;
0014 struct addr_location;
0015 struct map_symbol;
0016 struct mem_info;
0017 struct branch_info;
0018 struct branch_stack;
0019 struct block_info;
0020 struct symbol;
0021 struct ui_progress;
0022
0023 enum hist_filter {
0024 HIST_FILTER__DSO,
0025 HIST_FILTER__THREAD,
0026 HIST_FILTER__PARENT,
0027 HIST_FILTER__SYMBOL,
0028 HIST_FILTER__GUEST,
0029 HIST_FILTER__HOST,
0030 HIST_FILTER__SOCKET,
0031 HIST_FILTER__C2C,
0032 };
0033
0034 enum hist_column {
0035 HISTC_SYMBOL,
0036 HISTC_TIME,
0037 HISTC_DSO,
0038 HISTC_THREAD,
0039 HISTC_COMM,
0040 HISTC_CGROUP_ID,
0041 HISTC_CGROUP,
0042 HISTC_PARENT,
0043 HISTC_CPU,
0044 HISTC_SOCKET,
0045 HISTC_SRCLINE,
0046 HISTC_SRCFILE,
0047 HISTC_MISPREDICT,
0048 HISTC_IN_TX,
0049 HISTC_ABORT,
0050 HISTC_SYMBOL_FROM,
0051 HISTC_SYMBOL_TO,
0052 HISTC_DSO_FROM,
0053 HISTC_DSO_TO,
0054 HISTC_LOCAL_WEIGHT,
0055 HISTC_GLOBAL_WEIGHT,
0056 HISTC_CODE_PAGE_SIZE,
0057 HISTC_MEM_DADDR_SYMBOL,
0058 HISTC_MEM_DADDR_DSO,
0059 HISTC_MEM_PHYS_DADDR,
0060 HISTC_MEM_DATA_PAGE_SIZE,
0061 HISTC_MEM_LOCKED,
0062 HISTC_MEM_TLB,
0063 HISTC_MEM_LVL,
0064 HISTC_MEM_SNOOP,
0065 HISTC_MEM_DCACHELINE,
0066 HISTC_MEM_IADDR_SYMBOL,
0067 HISTC_TRANSACTION,
0068 HISTC_CYCLES,
0069 HISTC_SRCLINE_FROM,
0070 HISTC_SRCLINE_TO,
0071 HISTC_TRACE,
0072 HISTC_SYM_SIZE,
0073 HISTC_DSO_SIZE,
0074 HISTC_SYMBOL_IPC,
0075 HISTC_MEM_BLOCKED,
0076 HISTC_LOCAL_INS_LAT,
0077 HISTC_GLOBAL_INS_LAT,
0078 HISTC_LOCAL_P_STAGE_CYC,
0079 HISTC_GLOBAL_P_STAGE_CYC,
0080 HISTC_ADDR_FROM,
0081 HISTC_ADDR_TO,
0082 HISTC_NR_COLS,
0083 };
0084
0085 struct thread;
0086 struct dso;
0087
0088 struct hists {
0089 struct rb_root_cached entries_in_array[2];
0090 struct rb_root_cached *entries_in;
0091 struct rb_root_cached entries;
0092 struct rb_root_cached entries_collapsed;
0093 u64 nr_entries;
0094 u64 nr_non_filtered_entries;
0095 u64 callchain_period;
0096 u64 callchain_non_filtered_period;
0097 struct thread *thread_filter;
0098 const struct dso *dso_filter;
0099 const char *uid_filter_str;
0100 const char *symbol_filter_str;
0101 pthread_mutex_t lock;
0102 struct hists_stats stats;
0103 u64 event_stream;
0104 u16 col_len[HISTC_NR_COLS];
0105 bool has_callchains;
0106 int socket_filter;
0107 struct perf_hpp_list *hpp_list;
0108 struct list_head hpp_formats;
0109 int nr_hpp_node;
0110 };
0111
0112 #define hists__has(__h, __f) (__h)->hpp_list->__f
0113
0114 struct hist_entry_iter;
0115
0116 struct hist_iter_ops {
0117 int (*prepare_entry)(struct hist_entry_iter *, struct addr_location *);
0118 int (*add_single_entry)(struct hist_entry_iter *, struct addr_location *);
0119 int (*next_entry)(struct hist_entry_iter *, struct addr_location *);
0120 int (*add_next_entry)(struct hist_entry_iter *, struct addr_location *);
0121 int (*finish_entry)(struct hist_entry_iter *, struct addr_location *);
0122 };
0123
0124 struct hist_entry_iter {
0125 int total;
0126 int curr;
0127
0128 bool hide_unresolved;
0129
0130 struct evsel *evsel;
0131 struct perf_sample *sample;
0132 struct hist_entry *he;
0133 struct symbol *parent;
0134 void *priv;
0135
0136 const struct hist_iter_ops *ops;
0137
0138 int (*add_entry_cb)(struct hist_entry_iter *iter,
0139 struct addr_location *al, bool single, void *arg);
0140 };
0141
0142 extern const struct hist_iter_ops hist_iter_normal;
0143 extern const struct hist_iter_ops hist_iter_branch;
0144 extern const struct hist_iter_ops hist_iter_mem;
0145 extern const struct hist_iter_ops hist_iter_cumulative;
0146
0147 struct hist_entry *hists__add_entry(struct hists *hists,
0148 struct addr_location *al,
0149 struct symbol *parent,
0150 struct branch_info *bi,
0151 struct mem_info *mi,
0152 struct perf_sample *sample,
0153 bool sample_self);
0154
0155 struct hist_entry *hists__add_entry_ops(struct hists *hists,
0156 struct hist_entry_ops *ops,
0157 struct addr_location *al,
0158 struct symbol *sym_parent,
0159 struct branch_info *bi,
0160 struct mem_info *mi,
0161 struct perf_sample *sample,
0162 bool sample_self);
0163
0164 struct hist_entry *hists__add_entry_block(struct hists *hists,
0165 struct addr_location *al,
0166 struct block_info *bi);
0167
0168 int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
0169 int max_stack_depth, void *arg);
0170
0171 struct perf_hpp;
0172 struct perf_hpp_fmt;
0173
0174 int64_t hist_entry__cmp(struct hist_entry *left, struct hist_entry *right);
0175 int64_t hist_entry__collapse(struct hist_entry *left, struct hist_entry *right);
0176 int hist_entry__transaction_len(void);
0177 int hist_entry__sort_snprintf(struct hist_entry *he, char *bf, size_t size,
0178 struct hists *hists);
0179 int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
0180 struct perf_hpp_fmt *fmt, int printed);
0181 void hist_entry__delete(struct hist_entry *he);
0182
0183 typedef int (*hists__resort_cb_t)(struct hist_entry *he, void *arg);
0184
0185 void evsel__output_resort_cb(struct evsel *evsel, struct ui_progress *prog,
0186 hists__resort_cb_t cb, void *cb_arg);
0187 void evsel__output_resort(struct evsel *evsel, struct ui_progress *prog);
0188 void hists__output_resort(struct hists *hists, struct ui_progress *prog);
0189 void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
0190 hists__resort_cb_t cb);
0191 int hists__collapse_resort(struct hists *hists, struct ui_progress *prog);
0192
0193 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel);
0194 void hists__delete_entries(struct hists *hists);
0195 void hists__output_recalc_col_len(struct hists *hists, int max_rows);
0196
0197 struct hist_entry *hists__get_entry(struct hists *hists, int idx);
0198
0199 u64 hists__total_period(struct hists *hists);
0200 void hists__reset_stats(struct hists *hists);
0201 void hists__inc_stats(struct hists *hists, struct hist_entry *h);
0202 void hists__inc_nr_events(struct hists *hists);
0203 void hists__inc_nr_samples(struct hists *hists, bool filtered);
0204
0205 size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
0206 int max_cols, float min_pcnt, FILE *fp,
0207 bool ignore_callchains);
0208 size_t evlist__fprintf_nr_events(struct evlist *evlist, FILE *fp,
0209 bool skip_empty);
0210
0211 void hists__filter_by_dso(struct hists *hists);
0212 void hists__filter_by_thread(struct hists *hists);
0213 void hists__filter_by_symbol(struct hists *hists);
0214 void hists__filter_by_socket(struct hists *hists);
0215
0216 static inline bool hists__has_filter(struct hists *hists)
0217 {
0218 return hists->thread_filter || hists->dso_filter ||
0219 hists->symbol_filter_str || (hists->socket_filter > -1);
0220 }
0221
0222 u16 hists__col_len(struct hists *hists, enum hist_column col);
0223 void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len);
0224 bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len);
0225 void hists__reset_col_len(struct hists *hists);
0226 void hists__calc_col_len(struct hists *hists, struct hist_entry *he);
0227
0228 void hists__match(struct hists *leader, struct hists *other);
0229 int hists__link(struct hists *leader, struct hists *other);
0230 int hists__unlink(struct hists *hists);
0231
0232 struct hists_evsel {
0233 struct evsel evsel;
0234 struct hists hists;
0235 };
0236
0237 static inline struct evsel *hists_to_evsel(struct hists *hists)
0238 {
0239 struct hists_evsel *hevsel = container_of(hists, struct hists_evsel, hists);
0240 return &hevsel->evsel;
0241 }
0242
0243 static inline struct hists *evsel__hists(struct evsel *evsel)
0244 {
0245 struct hists_evsel *hevsel = (struct hists_evsel *)evsel;
0246 return &hevsel->hists;
0247 }
0248
0249 static __pure inline bool hists__has_callchains(struct hists *hists)
0250 {
0251 return hists->has_callchains;
0252 }
0253
0254 int hists__init(void);
0255 int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list);
0256
0257 struct rb_root_cached *hists__get_rotate_entries_in(struct hists *hists);
0258
0259 struct perf_hpp {
0260 char *buf;
0261 size_t size;
0262 const char *sep;
0263 void *ptr;
0264 bool skip;
0265 };
0266
0267 struct perf_hpp_fmt {
0268 const char *name;
0269 int (*header)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
0270 struct hists *hists, int line, int *span);
0271 int (*width)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
0272 struct hists *hists);
0273 int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
0274 struct hist_entry *he);
0275 int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
0276 struct hist_entry *he);
0277 int64_t (*cmp)(struct perf_hpp_fmt *fmt,
0278 struct hist_entry *a, struct hist_entry *b);
0279 int64_t (*collapse)(struct perf_hpp_fmt *fmt,
0280 struct hist_entry *a, struct hist_entry *b);
0281 int64_t (*sort)(struct perf_hpp_fmt *fmt,
0282 struct hist_entry *a, struct hist_entry *b);
0283 bool (*equal)(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b);
0284 void (*free)(struct perf_hpp_fmt *fmt);
0285
0286 struct list_head list;
0287 struct list_head sort_list;
0288 bool elide;
0289 int len;
0290 int user_len;
0291 int idx;
0292 int level;
0293 };
0294
0295 struct perf_hpp_list {
0296 struct list_head fields;
0297 struct list_head sorts;
0298
0299 int nr_header_lines;
0300 int need_collapse;
0301 int parent;
0302 int sym;
0303 int dso;
0304 int socket;
0305 int thread;
0306 int comm;
0307 };
0308
0309 extern struct perf_hpp_list perf_hpp_list;
0310
0311 struct perf_hpp_list_node {
0312 struct list_head list;
0313 struct perf_hpp_list hpp;
0314 int level;
0315 bool skip;
0316 };
0317
0318 void perf_hpp_list__column_register(struct perf_hpp_list *list,
0319 struct perf_hpp_fmt *format);
0320 void perf_hpp_list__register_sort_field(struct perf_hpp_list *list,
0321 struct perf_hpp_fmt *format);
0322 void perf_hpp_list__prepend_sort_field(struct perf_hpp_list *list,
0323 struct perf_hpp_fmt *format);
0324
0325 static inline void perf_hpp__column_register(struct perf_hpp_fmt *format)
0326 {
0327 perf_hpp_list__column_register(&perf_hpp_list, format);
0328 }
0329
0330 static inline void perf_hpp__register_sort_field(struct perf_hpp_fmt *format)
0331 {
0332 perf_hpp_list__register_sort_field(&perf_hpp_list, format);
0333 }
0334
0335 static inline void perf_hpp__prepend_sort_field(struct perf_hpp_fmt *format)
0336 {
0337 perf_hpp_list__prepend_sort_field(&perf_hpp_list, format);
0338 }
0339
0340 #define perf_hpp_list__for_each_format(_list, format) \
0341 list_for_each_entry(format, &(_list)->fields, list)
0342
0343 #define perf_hpp_list__for_each_format_safe(_list, format, tmp) \
0344 list_for_each_entry_safe(format, tmp, &(_list)->fields, list)
0345
0346 #define perf_hpp_list__for_each_sort_list(_list, format) \
0347 list_for_each_entry(format, &(_list)->sorts, sort_list)
0348
0349 #define perf_hpp_list__for_each_sort_list_safe(_list, format, tmp) \
0350 list_for_each_entry_safe(format, tmp, &(_list)->sorts, sort_list)
0351
0352 #define hists__for_each_format(hists, format) \
0353 perf_hpp_list__for_each_format((hists)->hpp_list, format)
0354
0355 #define hists__for_each_sort_list(hists, format) \
0356 perf_hpp_list__for_each_sort_list((hists)->hpp_list, format)
0357
0358 extern struct perf_hpp_fmt perf_hpp__format[];
0359
0360 enum {
0361
0362 PERF_HPP__OVERHEAD,
0363 PERF_HPP__OVERHEAD_SYS,
0364 PERF_HPP__OVERHEAD_US,
0365 PERF_HPP__OVERHEAD_GUEST_SYS,
0366 PERF_HPP__OVERHEAD_GUEST_US,
0367 PERF_HPP__OVERHEAD_ACC,
0368 PERF_HPP__SAMPLES,
0369 PERF_HPP__PERIOD,
0370
0371 PERF_HPP__MAX_INDEX
0372 };
0373
0374 void perf_hpp__init(void);
0375 void perf_hpp__cancel_cumulate(void);
0376 void perf_hpp__setup_output_field(struct perf_hpp_list *list);
0377 void perf_hpp__reset_output_field(struct perf_hpp_list *list);
0378 void perf_hpp__append_sort_keys(struct perf_hpp_list *list);
0379 int perf_hpp__setup_hists_formats(struct perf_hpp_list *list,
0380 struct evlist *evlist);
0381
0382
0383 bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format);
0384 bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *format);
0385 bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists);
0386 bool perf_hpp__is_trace_entry(struct perf_hpp_fmt *fmt);
0387 bool perf_hpp__is_srcline_entry(struct perf_hpp_fmt *fmt);
0388 bool perf_hpp__is_srcfile_entry(struct perf_hpp_fmt *fmt);
0389 bool perf_hpp__is_thread_entry(struct perf_hpp_fmt *fmt);
0390 bool perf_hpp__is_comm_entry(struct perf_hpp_fmt *fmt);
0391 bool perf_hpp__is_dso_entry(struct perf_hpp_fmt *fmt);
0392 bool perf_hpp__is_sym_entry(struct perf_hpp_fmt *fmt);
0393
0394 struct perf_hpp_fmt *perf_hpp_fmt__dup(struct perf_hpp_fmt *fmt);
0395
0396 int hist_entry__filter(struct hist_entry *he, int type, const void *arg);
0397
0398 static inline bool perf_hpp__should_skip(struct perf_hpp_fmt *format,
0399 struct hists *hists)
0400 {
0401 if (format->elide)
0402 return true;
0403
0404 if (perf_hpp__is_dynamic_entry(format) &&
0405 !perf_hpp__defined_dynamic_entry(format, hists))
0406 return true;
0407
0408 return false;
0409 }
0410
0411 void perf_hpp__reset_width(struct perf_hpp_fmt *fmt, struct hists *hists);
0412 void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists);
0413 void perf_hpp__set_user_width(const char *width_list_str);
0414 void hists__reset_column_width(struct hists *hists);
0415
0416 typedef u64 (*hpp_field_fn)(struct hist_entry *he);
0417 typedef int (*hpp_callback_fn)(struct perf_hpp *hpp, bool front);
0418 typedef int (*hpp_snprint_fn)(struct perf_hpp *hpp, const char *fmt, ...);
0419
0420 int hpp__fmt(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
0421 struct hist_entry *he, hpp_field_fn get_field,
0422 const char *fmtstr, hpp_snprint_fn print_fn, bool fmt_percent);
0423 int hpp__fmt_acc(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
0424 struct hist_entry *he, hpp_field_fn get_field,
0425 const char *fmtstr, hpp_snprint_fn print_fn, bool fmt_percent);
0426
0427 static inline void advance_hpp(struct perf_hpp *hpp, int inc)
0428 {
0429 hpp->buf += inc;
0430 hpp->size -= inc;
0431 }
0432
0433 static inline size_t perf_hpp__use_color(void)
0434 {
0435 return !symbol_conf.field_sep;
0436 }
0437
0438 static inline size_t perf_hpp__color_overhead(void)
0439 {
0440 return perf_hpp__use_color() ?
0441 (COLOR_MAXLEN + sizeof(PERF_COLOR_RESET)) * PERF_HPP__MAX_INDEX
0442 : 0;
0443 }
0444
0445 struct evlist;
0446
0447 struct hist_browser_timer {
0448 void (*timer)(void *arg);
0449 void *arg;
0450 int refresh;
0451 };
0452
0453 struct annotation_options;
0454 struct res_sample;
0455
0456 enum rstype {
0457 A_NORMAL,
0458 A_ASM,
0459 A_SOURCE
0460 };
0461
0462 struct block_hist;
0463
0464 #ifdef HAVE_SLANG_SUPPORT
0465 #include "../ui/keysyms.h"
0466 void attr_to_script(char *buf, struct perf_event_attr *attr);
0467
0468 int map_symbol__tui_annotate(struct map_symbol *ms, struct evsel *evsel,
0469 struct hist_browser_timer *hbt,
0470 struct annotation_options *annotation_opts);
0471
0472 int hist_entry__tui_annotate(struct hist_entry *he, struct evsel *evsel,
0473 struct hist_browser_timer *hbt,
0474 struct annotation_options *annotation_opts);
0475
0476 int evlist__tui_browse_hists(struct evlist *evlist, const char *help, struct hist_browser_timer *hbt,
0477 float min_pcnt, struct perf_env *env, bool warn_lost_event,
0478 struct annotation_options *annotation_options);
0479
0480 int script_browse(const char *script_opt, struct evsel *evsel);
0481
0482 void run_script(char *cmd);
0483 int res_sample_browse(struct res_sample *res_samples, int num_res,
0484 struct evsel *evsel, enum rstype rstype);
0485 void res_sample_init(void);
0486
0487 int block_hists_tui_browse(struct block_hist *bh, struct evsel *evsel,
0488 float min_percent, struct perf_env *env,
0489 struct annotation_options *annotation_opts);
0490 #else
0491 static inline
0492 int evlist__tui_browse_hists(struct evlist *evlist __maybe_unused,
0493 const char *help __maybe_unused,
0494 struct hist_browser_timer *hbt __maybe_unused,
0495 float min_pcnt __maybe_unused,
0496 struct perf_env *env __maybe_unused,
0497 bool warn_lost_event __maybe_unused,
0498 struct annotation_options *annotation_options __maybe_unused)
0499 {
0500 return 0;
0501 }
0502 static inline int map_symbol__tui_annotate(struct map_symbol *ms __maybe_unused,
0503 struct evsel *evsel __maybe_unused,
0504 struct hist_browser_timer *hbt __maybe_unused,
0505 struct annotation_options *annotation_options __maybe_unused)
0506 {
0507 return 0;
0508 }
0509
0510 static inline int hist_entry__tui_annotate(struct hist_entry *he __maybe_unused,
0511 struct evsel *evsel __maybe_unused,
0512 struct hist_browser_timer *hbt __maybe_unused,
0513 struct annotation_options *annotation_opts __maybe_unused)
0514 {
0515 return 0;
0516 }
0517
0518 static inline int script_browse(const char *script_opt __maybe_unused,
0519 struct evsel *evsel __maybe_unused)
0520 {
0521 return 0;
0522 }
0523
0524 static inline int res_sample_browse(struct res_sample *res_samples __maybe_unused,
0525 int num_res __maybe_unused,
0526 struct evsel *evsel __maybe_unused,
0527 enum rstype rstype __maybe_unused)
0528 {
0529 return 0;
0530 }
0531
0532 static inline void res_sample_init(void) {}
0533
0534 static inline int block_hists_tui_browse(struct block_hist *bh __maybe_unused,
0535 struct evsel *evsel __maybe_unused,
0536 float min_percent __maybe_unused,
0537 struct perf_env *env __maybe_unused,
0538 struct annotation_options *annotation_opts __maybe_unused)
0539 {
0540 return 0;
0541 }
0542
0543 #define K_LEFT -1000
0544 #define K_RIGHT -2000
0545 #define K_SWITCH_INPUT_DATA -3000
0546 #define K_RELOAD -4000
0547 #endif
0548
0549 unsigned int hists__sort_list_width(struct hists *hists);
0550 unsigned int hists__overhead_width(struct hists *hists);
0551
0552 void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
0553 struct perf_sample *sample, bool nonany_branch_mode,
0554 u64 *total_cycles);
0555
0556 struct option;
0557 int parse_filter_percentage(const struct option *opt, const char *arg, int unset);
0558 int perf_hist_config(const char *var, const char *value);
0559
0560 void perf_hpp_list__init(struct perf_hpp_list *list);
0561
0562 enum hierarchy_move_dir {
0563 HMD_NORMAL,
0564 HMD_FORCE_SIBLING,
0565 HMD_FORCE_CHILD,
0566 };
0567
0568 struct rb_node *rb_hierarchy_last(struct rb_node *node);
0569 struct rb_node *__rb_hierarchy_next(struct rb_node *node,
0570 enum hierarchy_move_dir hmd);
0571 struct rb_node *rb_hierarchy_prev(struct rb_node *node);
0572
0573 static inline struct rb_node *rb_hierarchy_next(struct rb_node *node)
0574 {
0575 return __rb_hierarchy_next(node, HMD_NORMAL);
0576 }
0577
0578 #define HIERARCHY_INDENT 3
0579
0580 bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit);
0581 int hpp_color_scnprintf(struct perf_hpp *hpp, const char *fmt, ...);
0582 int __hpp__slsmg_color_printf(struct perf_hpp *hpp, const char *fmt, ...);
0583 int __hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp,
0584 struct perf_hpp_list *hpp_list);
0585 int hists__fprintf_headers(struct hists *hists, FILE *fp);
0586 int __hists__scnprintf_title(struct hists *hists, char *bf, size_t size, bool show_freq);
0587
0588 static inline int hists__scnprintf_title(struct hists *hists, char *bf, size_t size)
0589 {
0590 return __hists__scnprintf_title(hists, bf, size, true);
0591 }
0592
0593 #endif