1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/rbtree.h>
6 #include <linux/types.h>
9 #include "events_stats.h"
11 #include "map_symbol.h"
12 #include "mem-events.h"
35 HIST_FILTER__PARALLELISM,
38 typedef u16 filter_mask_t;
65 HISTC_MEM_DADDR_SYMBOL,
68 HISTC_MEM_DATA_PAGE_SIZE,
74 HISTC_MEM_IADDR_SYMBOL,
86 HISTC_LOCAL_P_STAGE_CYC,
87 HISTC_GLOBAL_P_STAGE_CYC,
96 HISTC_CALLCHAIN_BRANCH_PREDICTED,
97 HISTC_CALLCHAIN_BRANCH_ABORT,
98 HISTC_CALLCHAIN_BRANCH_CYCLES,
99 HISTC_NR_COLS, /* Last entry */
105 #define MEM_STAT_LEN 8
108 /* meaning of entries depends on enum mem_stat_type */
109 u64 entries[MEM_STAT_LEN];
113 struct rb_root_cached entries_in_array[2];
114 struct rb_root_cached *entries_in;
115 struct rb_root_cached entries;
116 struct rb_root_cached entries_collapsed;
118 u64 nr_non_filtered_entries;
119 u64 callchain_period;
120 u64 callchain_non_filtered_period;
121 u64 callchain_latency;
122 u64 callchain_non_filtered_latency;
123 struct thread *thread_filter;
124 const struct dso *dso_filter;
125 const char *uid_filter_str;
126 const char *symbol_filter_str;
127 unsigned long *parallelism_filter;
129 struct hists_stats stats;
131 u16 col_len[HISTC_NR_COLS];
134 struct perf_hpp_list *hpp_list;
135 struct list_head hpp_formats;
138 enum mem_stat_type *mem_stat_types;
139 struct he_mem_stat *mem_stat_total;
142 #define hists__has(__h, __f) (__h)->hpp_list->__f
144 struct hist_entry_iter;
146 struct hist_iter_ops {
147 int (*prepare_entry)(struct hist_entry_iter *, struct addr_location *);
148 int (*add_single_entry)(struct hist_entry_iter *, struct addr_location *);
149 int (*next_entry)(struct hist_entry_iter *, struct addr_location *);
150 int (*add_next_entry)(struct hist_entry_iter *, struct addr_location *);
151 int (*finish_entry)(struct hist_entry_iter *, struct addr_location *);
154 struct hist_entry_iter {
159 struct perf_sample *sample;
160 struct hist_entry *he;
161 struct symbol *parent;
164 struct branch_info *bi;
165 struct hist_entry **he_cache;
167 const struct hist_iter_ops *ops;
168 /* user-defined callback function (optional) */
169 int (*add_entry_cb)(struct hist_entry_iter *iter,
170 struct addr_location *al, bool single, void *arg);
171 bool hide_unresolved;
174 extern const struct hist_iter_ops hist_iter_normal;
175 extern const struct hist_iter_ops hist_iter_branch;
176 extern const struct hist_iter_ops hist_iter_mem;
177 extern const struct hist_iter_ops hist_iter_cumulative;
188 * Period re-scaled from CPU time to wall-clock time (divided by the
189 * parallelism at the time of the sample). This represents effect of
190 * the event on latency rather than CPU consumption.
195 u64 period_guest_sys;
203 struct namespace_id {
208 struct hist_entry_diff {
211 /* PERF_HPP__DELTA */
212 double period_ratio_delta;
214 /* PERF_HPP__RATIO */
217 /* HISTC_WEIGHTED_DIFF */
220 /* PERF_HPP_DIFF__CYCLES */
224 unsigned long svals[NUM_SPARKS];
227 struct hist_entry_ops {
228 void *(*new)(size_t size);
229 void (*free)(void *ptr);
233 * struct hist_entry - histogram entry
235 * @row_offset - offset from the first callchain expanded to appear on screen
236 * @nr_rows - rows expanded in callchain, recalculated on folding/unfolding
239 struct rb_node rb_node_in;
240 struct rb_node rb_node;
242 struct list_head node;
243 struct list_head head;
246 struct he_stat *stat_acc;
247 struct he_mem_stat *mem_stat;
248 struct map_symbol ms;
249 struct thread *thread;
251 struct namespace_id cgroup_id;
265 struct simd_flags simd_flags;
267 /* We are added by hists__add_dummy_entry. */
272 filter_mask_t filtered;
277 * Since perf diff only supports the stdio output, TUI
278 * fields are only accessed from perf report (or perf
279 * top). So make it a union to reduce memory usage.
281 struct hist_entry_diff diff;
282 struct /* for TUI */ {
285 bool init_have_children;
293 struct symbol *parent;
294 struct branch_info *branch_info;
297 struct mem_info *mem_info;
298 struct block_info *block_info;
299 struct kvm_info *kvm_info;
303 struct res_sample *res_samples;
305 struct perf_hpp_list *hpp_list;
306 struct hist_entry *parent_he;
307 struct hist_entry_ops *ops;
308 struct annotated_data_type *mem_type;
310 /* this is for hierarchical entry structure */
312 struct rb_root_cached hroot_in;
313 struct rb_root_cached hroot_out;
314 }; /* non-leaf entries */
315 struct rb_root sorted_chain; /* leaf entry has callchains */
317 struct callchain_root callchain[0]; /* must be last member */
320 static __pure inline bool hist_entry__has_callchains(struct hist_entry *he)
322 return he->callchain_size != 0;
325 static inline bool hist_entry__has_pairs(struct hist_entry *he)
327 return !list_empty(&he->pairs.node);
330 static inline struct hist_entry *hist_entry__next_pair(struct hist_entry *he)
332 if (hist_entry__has_pairs(he))
333 return list_entry(he->pairs.node.next, struct hist_entry, pairs.node);
337 static inline void hist_entry__add_pair(struct hist_entry *pair,
338 struct hist_entry *he)
340 list_add_tail(&pair->pairs.node, &he->pairs.head);
343 struct hist_entry *hists__add_entry(struct hists *hists,
344 struct addr_location *al,
345 struct symbol *parent,
346 struct branch_info *bi,
349 struct perf_sample *sample,
352 struct hist_entry *hists__add_entry_ops(struct hists *hists,
353 struct hist_entry_ops *ops,
354 struct addr_location *al,
355 struct symbol *sym_parent,
356 struct branch_info *bi,
359 struct perf_sample *sample,
362 struct hist_entry *hists__add_entry_block(struct hists *hists,
363 struct addr_location *al,
364 struct block_info *bi);
366 int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
367 int max_stack_depth, void *arg);
372 int hist_entry__transaction_len(void);
373 int hist_entry__sort_snprintf(struct hist_entry *he, char *bf, size_t size,
374 struct hists *hists);
375 int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
376 struct perf_hpp_fmt *fmt, int printed);
377 int hist_entry__sym_snprintf(struct hist_entry *he, char *bf, size_t size,
379 void hist_entry__delete(struct hist_entry *he);
381 typedef int (*hists__resort_cb_t)(struct hist_entry *he, void *arg);
383 void evsel__output_resort_cb(struct evsel *evsel, struct ui_progress *prog,
384 hists__resort_cb_t cb, void *cb_arg);
385 void evsel__output_resort(struct evsel *evsel, struct ui_progress *prog);
386 void hists__output_resort(struct hists *hists, struct ui_progress *prog);
387 void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
388 hists__resort_cb_t cb);
389 int hists__collapse_resort(struct hists *hists, struct ui_progress *prog);
391 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel);
392 void hists__delete_entries(struct hists *hists);
393 void hists__output_recalc_col_len(struct hists *hists, int max_rows);
395 struct hist_entry *hists__get_entry(struct hists *hists, int idx);
397 u64 hists__total_period(struct hists *hists);
398 u64 hists__total_latency(struct hists *hists);
399 void hists__reset_stats(struct hists *hists);
400 void hists__inc_stats(struct hists *hists, struct hist_entry *h);
401 void hists__inc_nr_events(struct hists *hists);
402 void hists__inc_nr_samples(struct hists *hists, bool filtered);
403 void hists__inc_nr_lost_samples(struct hists *hists, u32 lost);
404 void hists__inc_nr_dropped_samples(struct hists *hists, u32 lost);
406 size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
407 int max_cols, float min_pcnt, FILE *fp,
408 bool ignore_callchains);
409 size_t evlist__fprintf_nr_events(struct evlist *evlist, FILE *fp);
411 void hists__filter_by_dso(struct hists *hists);
412 void hists__filter_by_thread(struct hists *hists);
413 void hists__filter_by_symbol(struct hists *hists);
414 void hists__filter_by_socket(struct hists *hists);
415 void hists__filter_by_parallelism(struct hists *hists);
417 static inline bool hists__has_filter(struct hists *hists)
419 return hists->thread_filter || hists->dso_filter ||
420 hists->symbol_filter_str || (hists->socket_filter > -1) ||
421 hists->parallelism_filter;
424 u16 hists__col_len(struct hists *hists, enum hist_column col);
425 void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len);
426 bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len);
427 void hists__reset_col_len(struct hists *hists);
428 void hists__calc_col_len(struct hists *hists, struct hist_entry *he);
430 void hists__match(struct hists *leader, struct hists *other);
431 int hists__link(struct hists *leader, struct hists *other);
432 int hists__unlink(struct hists *hists);
434 static inline float hist_entry__get_percent_limit(struct hist_entry *he)
436 u64 period = he->stat.period;
437 u64 total_period = hists__total_period(he->hists);
439 if (unlikely(total_period == 0))
442 if (symbol_conf.cumulate_callchain)
443 period = he->stat_acc->period;
445 return period * 100.0 / total_period;
453 static inline struct evsel *hists_to_evsel(struct hists *hists)
455 struct hists_evsel *hevsel = container_of(hists, struct hists_evsel, hists);
456 return &hevsel->evsel;
459 static inline struct hists *evsel__hists(struct evsel *evsel)
461 struct hists_evsel *hevsel = (struct hists_evsel *)evsel;
462 return &hevsel->hists;
465 static __pure inline bool hists__has_callchains(struct hists *hists)
467 return hists->has_callchains;
470 int hists__init(void);
471 int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list);
473 struct rb_root_cached *hists__get_rotate_entries_in(struct hists *hists);
483 typedef int64_t (*perf_hpp_fmt_cmp_t)(
484 struct perf_hpp_fmt *, struct hist_entry *, struct hist_entry *);
486 struct perf_hpp_fmt {
488 int (*header)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
489 struct hists *hists, int line, int *span);
490 int (*width)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
491 struct hists *hists);
492 void (*init)(struct perf_hpp_fmt *fmt, struct hist_entry *he);
493 int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
494 struct hist_entry *he);
495 int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
496 struct hist_entry *he);
497 perf_hpp_fmt_cmp_t cmp;
498 perf_hpp_fmt_cmp_t collapse;
499 perf_hpp_fmt_cmp_t sort;
500 bool (*equal)(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b);
501 void (*free)(struct perf_hpp_fmt *fmt);
503 struct list_head list;
504 struct list_head sort_list;
512 struct perf_hpp_list {
513 struct list_head fields;
514 struct list_head sorts;
526 extern struct perf_hpp_list perf_hpp_list;
528 struct perf_hpp_list_node {
529 struct list_head list;
530 struct perf_hpp_list hpp;
535 void perf_hpp_list__column_register(struct perf_hpp_list *list,
536 struct perf_hpp_fmt *format);
537 void perf_hpp_list__register_sort_field(struct perf_hpp_list *list,
538 struct perf_hpp_fmt *format);
539 void perf_hpp_list__prepend_sort_field(struct perf_hpp_list *list,
540 struct perf_hpp_fmt *format);
542 static inline void perf_hpp__column_register(struct perf_hpp_fmt *format)
544 perf_hpp_list__column_register(&perf_hpp_list, format);
547 static inline void perf_hpp__register_sort_field(struct perf_hpp_fmt *format)
549 perf_hpp_list__register_sort_field(&perf_hpp_list, format);
552 static inline void perf_hpp__prepend_sort_field(struct perf_hpp_fmt *format)
554 perf_hpp_list__prepend_sort_field(&perf_hpp_list, format);
557 #define perf_hpp_list__for_each_format(_list, format) \
558 list_for_each_entry(format, &(_list)->fields, list)
560 #define perf_hpp_list__for_each_format_safe(_list, format, tmp) \
561 list_for_each_entry_safe(format, tmp, &(_list)->fields, list)
563 #define perf_hpp_list__for_each_sort_list(_list, format) \
564 list_for_each_entry(format, &(_list)->sorts, sort_list)
566 #define perf_hpp_list__for_each_sort_list_safe(_list, format, tmp) \
567 list_for_each_entry_safe(format, tmp, &(_list)->sorts, sort_list)
569 #define hists__for_each_format(hists, format) \
570 perf_hpp_list__for_each_format((hists)->hpp_list, format)
572 #define hists__for_each_sort_list(hists, format) \
573 perf_hpp_list__for_each_sort_list((hists)->hpp_list, format)
575 extern struct perf_hpp_fmt perf_hpp__format[];
578 /* Matches perf_hpp__format array. */
581 PERF_HPP__OVERHEAD_SYS,
582 PERF_HPP__OVERHEAD_US,
583 PERF_HPP__OVERHEAD_GUEST_SYS,
584 PERF_HPP__OVERHEAD_GUEST_US,
585 PERF_HPP__OVERHEAD_ACC,
586 PERF_HPP__LATENCY_ACC,
592 PERF_HPP__MEM_STAT_OP,
593 PERF_HPP__MEM_STAT_CACHE,
594 PERF_HPP__MEM_STAT_MEMORY,
595 PERF_HPP__MEM_STAT_SNOOP,
596 PERF_HPP__MEM_STAT_DTLB,
601 void perf_hpp__init(void);
602 void perf_hpp__cancel_cumulate(struct evlist *evlist);
603 void perf_hpp__cancel_latency(struct evlist *evlist);
604 void perf_hpp__setup_output_field(struct perf_hpp_list *list);
605 void perf_hpp__reset_output_field(struct perf_hpp_list *list);
606 void perf_hpp__append_sort_keys(struct perf_hpp_list *list);
607 int perf_hpp__setup_hists_formats(struct perf_hpp_list *list,
608 struct evlist *evlist);
609 int perf_hpp__alloc_mem_stats(struct perf_hpp_list *list,
610 struct evlist *evlist);
613 bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format);
614 bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *format);
615 bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists);
616 bool perf_hpp__is_trace_entry(struct perf_hpp_fmt *fmt);
617 bool perf_hpp__is_srcline_entry(struct perf_hpp_fmt *fmt);
618 bool perf_hpp__is_srcfile_entry(struct perf_hpp_fmt *fmt);
619 bool perf_hpp__is_thread_entry(struct perf_hpp_fmt *fmt);
620 bool perf_hpp__is_comm_entry(struct perf_hpp_fmt *fmt);
621 bool perf_hpp__is_dso_entry(struct perf_hpp_fmt *fmt);
622 bool perf_hpp__is_sym_entry(struct perf_hpp_fmt *fmt);
623 bool perf_hpp__is_parallelism_entry(struct perf_hpp_fmt *fmt);
625 struct perf_hpp_fmt *perf_hpp_fmt__dup(struct perf_hpp_fmt *fmt);
627 int hist_entry__filter(struct hist_entry *he, int type, const void *arg);
629 static inline bool perf_hpp__should_skip(struct perf_hpp_fmt *format,
635 if (perf_hpp__is_dynamic_entry(format) &&
636 !perf_hpp__defined_dynamic_entry(format, hists))
642 void perf_hpp__reset_width(struct perf_hpp_fmt *fmt, struct hists *hists);
643 void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists);
644 void perf_hpp__set_user_width(const char *width_list_str);
645 void hists__reset_column_width(struct hists *hists);
647 enum perf_hpp_fmt_type {
648 PERF_HPP_FMT_TYPE__RAW,
649 PERF_HPP_FMT_TYPE__PERCENT,
650 PERF_HPP_FMT_TYPE__LATENCY,
651 PERF_HPP_FMT_TYPE__AVERAGE,
654 typedef u64 (*hpp_field_fn)(struct hist_entry *he);
655 typedef int (*hpp_callback_fn)(struct perf_hpp *hpp, bool front);
656 typedef int (*hpp_snprint_fn)(struct perf_hpp *hpp, const char *fmt, ...);
658 int hpp__fmt(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
659 struct hist_entry *he, hpp_field_fn get_field,
660 const char *fmtstr, hpp_snprint_fn print_fn,
661 enum perf_hpp_fmt_type fmtype);
662 int hpp__fmt_acc(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
663 struct hist_entry *he, hpp_field_fn get_field,
664 const char *fmtstr, hpp_snprint_fn print_fn,
665 enum perf_hpp_fmt_type fmtype);
666 int hpp__fmt_mem_stat(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
667 struct hist_entry *he, enum mem_stat_type mst,
668 const char *fmtstr, hpp_snprint_fn print_fn);
670 static inline void advance_hpp(struct perf_hpp *hpp, int inc)
676 static inline size_t perf_hpp__use_color(void)
678 return !symbol_conf.field_sep;
681 static inline size_t perf_hpp__color_overhead(void)
683 return perf_hpp__use_color() ?
684 (COLOR_MAXLEN + sizeof(PERF_COLOR_RESET)) * PERF_HPP__MAX_INDEX
690 struct hist_browser_timer {
691 void (*timer)(void *arg);
703 struct hists block_hists;
704 struct perf_hpp_list block_list;
705 struct perf_hpp_fmt block_fmt;
708 struct hist_entry he;
711 #ifdef HAVE_SLANG_SUPPORT
712 #include "../ui/keysyms.h"
713 void attr_to_script(char *buf, struct perf_event_attr *attr);
715 int map_symbol__tui_annotate(struct map_symbol *ms, struct evsel *evsel,
716 struct hist_browser_timer *hbt);
718 int hist_entry__tui_annotate(struct hist_entry *he, struct evsel *evsel,
719 struct hist_browser_timer *hbt);
721 int evlist__tui_browse_hists(struct evlist *evlist, const char *help, struct hist_browser_timer *hbt,
722 float min_pcnt, struct perf_env *env, bool warn_lost_event);
724 int script_browse(const char *script_opt, struct evsel *evsel);
726 void run_script(char *cmd);
727 int res_sample_browse(struct res_sample *res_samples, int num_res,
728 struct evsel *evsel, enum rstype rstype);
729 void res_sample_init(void);
731 int block_hists_tui_browse(struct block_hist *bh, struct evsel *evsel,
732 float min_percent, struct perf_env *env);
735 int evlist__tui_browse_hists(struct evlist *evlist __maybe_unused,
736 const char *help __maybe_unused,
737 struct hist_browser_timer *hbt __maybe_unused,
738 float min_pcnt __maybe_unused,
739 struct perf_env *env __maybe_unused,
740 bool warn_lost_event __maybe_unused)
744 static inline int map_symbol__tui_annotate(struct map_symbol *ms __maybe_unused,
745 struct evsel *evsel __maybe_unused,
746 struct hist_browser_timer *hbt __maybe_unused)
751 static inline int hist_entry__tui_annotate(struct hist_entry *he __maybe_unused,
752 struct evsel *evsel __maybe_unused,
753 struct hist_browser_timer *hbt __maybe_unused)
758 static inline int script_browse(const char *script_opt __maybe_unused,
759 struct evsel *evsel __maybe_unused)
764 static inline int res_sample_browse(struct res_sample *res_samples __maybe_unused,
765 int num_res __maybe_unused,
766 struct evsel *evsel __maybe_unused,
767 enum rstype rstype __maybe_unused)
772 static inline void res_sample_init(void) {}
774 static inline int block_hists_tui_browse(struct block_hist *bh __maybe_unused,
775 struct evsel *evsel __maybe_unused,
776 float min_percent __maybe_unused,
777 struct perf_env *env __maybe_unused)
783 #define K_RIGHT -2000
784 #define K_SWITCH_INPUT_DATA -3000
785 #define K_RELOAD -4000
788 unsigned int hists__sort_list_width(struct hists *hists);
789 unsigned int hists__overhead_width(struct hists *hists);
791 void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
792 struct perf_sample *sample, bool nonany_branch_mode,
793 u64 *total_cycles, struct evsel *evsel);
796 int parse_filter_percentage(const struct option *opt, const char *arg, int unset);
797 int perf_hist_config(const char *var, const char *value);
799 void perf_hpp_list__init(struct perf_hpp_list *list);
801 enum hierarchy_move_dir {
807 struct rb_node *rb_hierarchy_last(struct rb_node *node);
808 struct rb_node *__rb_hierarchy_next(struct rb_node *node,
809 enum hierarchy_move_dir hmd);
810 struct rb_node *rb_hierarchy_prev(struct rb_node *node);
812 static inline struct rb_node *rb_hierarchy_next(struct rb_node *node)
814 return __rb_hierarchy_next(node, HMD_NORMAL);
817 #define HIERARCHY_INDENT 3
819 bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit);
820 int hpp_color_scnprintf(struct perf_hpp *hpp, const char *fmt, ...);
821 int __hpp__slsmg_color_printf(struct perf_hpp *hpp, const char *fmt, ...);
822 int __hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp,
823 struct perf_hpp_list *hpp_list);
824 int hists__fprintf_headers(struct hists *hists, FILE *fp);
825 int __hists__scnprintf_title(struct hists *hists, char *bf, size_t size, bool show_freq);
827 static inline int hists__scnprintf_title(struct hists *hists, char *bf, size_t size)
829 return __hists__scnprintf_title(hists, bf, size, true);
832 #endif /* __PERF_HIST_H */