hist.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #ifndef __PERF_HIST_H
  2. #define __PERF_HIST_H
  3. #include <linux/types.h>
  4. #include <pthread.h>
  5. #include "callchain.h"
  6. #include "header.h"
  7. #include "color.h"
  8. #include "ui/progress.h"
  9. extern struct callchain_param callchain_param;
  10. struct hist_entry;
  11. struct addr_location;
  12. struct symbol;
  13. enum hist_filter {
  14. HIST_FILTER__DSO,
  15. HIST_FILTER__THREAD,
  16. HIST_FILTER__PARENT,
  17. HIST_FILTER__SYMBOL,
  18. HIST_FILTER__GUEST,
  19. HIST_FILTER__HOST,
  20. };
  21. /*
  22. * The kernel collects the number of events it couldn't send in a stretch and
  23. * when possible sends this number in a PERF_RECORD_LOST event. The number of
  24. * such "chunks" of lost events is stored in .nr_events[PERF_EVENT_LOST] while
  25. * total_lost tells exactly how many events the kernel in fact lost, i.e. it is
  26. * the sum of all struct lost_event.lost fields reported.
  27. *
  28. * The total_period is needed because by default auto-freq is used, so
  29. * multipling nr_events[PERF_EVENT_SAMPLE] by a frequency isn't possible to get
  30. * the total number of low level events, it is necessary to to sum all struct
  31. * sample_event.period and stash the result in total_period.
  32. */
  33. struct events_stats {
  34. u64 total_period;
  35. u64 total_non_filtered_period;
  36. u64 total_lost;
  37. u64 total_invalid_chains;
  38. u32 nr_events[PERF_RECORD_HEADER_MAX];
  39. u32 nr_non_filtered_samples;
  40. u32 nr_lost_warned;
  41. u32 nr_unknown_events;
  42. u32 nr_invalid_chains;
  43. u32 nr_unknown_id;
  44. u32 nr_unprocessable_samples;
  45. };
  46. enum hist_column {
  47. HISTC_SYMBOL,
  48. HISTC_DSO,
  49. HISTC_THREAD,
  50. HISTC_COMM,
  51. HISTC_PARENT,
  52. HISTC_CPU,
  53. HISTC_SRCLINE,
  54. HISTC_MISPREDICT,
  55. HISTC_IN_TX,
  56. HISTC_ABORT,
  57. HISTC_SYMBOL_FROM,
  58. HISTC_SYMBOL_TO,
  59. HISTC_DSO_FROM,
  60. HISTC_DSO_TO,
  61. HISTC_LOCAL_WEIGHT,
  62. HISTC_GLOBAL_WEIGHT,
  63. HISTC_MEM_DADDR_SYMBOL,
  64. HISTC_MEM_DADDR_DSO,
  65. HISTC_MEM_LOCKED,
  66. HISTC_MEM_TLB,
  67. HISTC_MEM_LVL,
  68. HISTC_MEM_SNOOP,
  69. HISTC_MEM_DCACHELINE,
  70. HISTC_TRANSACTION,
  71. HISTC_NR_COLS, /* Last entry */
  72. };
  73. struct thread;
  74. struct dso;
  75. struct hists {
  76. struct rb_root entries_in_array[2];
  77. struct rb_root *entries_in;
  78. struct rb_root entries;
  79. struct rb_root entries_collapsed;
  80. u64 nr_entries;
  81. u64 nr_non_filtered_entries;
  82. const struct thread *thread_filter;
  83. const struct dso *dso_filter;
  84. const char *uid_filter_str;
  85. const char *symbol_filter_str;
  86. pthread_mutex_t lock;
  87. struct events_stats stats;
  88. u64 event_stream;
  89. u16 col_len[HISTC_NR_COLS];
  90. };
  91. struct hist_entry_iter;
  92. struct hist_iter_ops {
  93. int (*prepare_entry)(struct hist_entry_iter *, struct addr_location *);
  94. int (*add_single_entry)(struct hist_entry_iter *, struct addr_location *);
  95. int (*next_entry)(struct hist_entry_iter *, struct addr_location *);
  96. int (*add_next_entry)(struct hist_entry_iter *, struct addr_location *);
  97. int (*finish_entry)(struct hist_entry_iter *, struct addr_location *);
  98. };
  99. struct hist_entry_iter {
  100. int total;
  101. int curr;
  102. bool hide_unresolved;
  103. struct perf_evsel *evsel;
  104. struct perf_sample *sample;
  105. struct hist_entry *he;
  106. struct symbol *parent;
  107. void *priv;
  108. const struct hist_iter_ops *ops;
  109. /* user-defined callback function (optional) */
  110. int (*add_entry_cb)(struct hist_entry_iter *iter,
  111. struct addr_location *al, bool single, void *arg);
  112. };
  113. extern const struct hist_iter_ops hist_iter_normal;
  114. extern const struct hist_iter_ops hist_iter_branch;
  115. extern const struct hist_iter_ops hist_iter_mem;
  116. extern const struct hist_iter_ops hist_iter_cumulative;
  117. struct hist_entry *__hists__add_entry(struct hists *hists,
  118. struct addr_location *al,
  119. struct symbol *parent,
  120. struct branch_info *bi,
  121. struct mem_info *mi, u64 period,
  122. u64 weight, u64 transaction,
  123. bool sample_self);
  124. int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
  125. struct perf_evsel *evsel, struct perf_sample *sample,
  126. int max_stack_depth, void *arg);
  127. int64_t hist_entry__cmp(struct hist_entry *left, struct hist_entry *right);
  128. int64_t hist_entry__collapse(struct hist_entry *left, struct hist_entry *right);
  129. int hist_entry__transaction_len(void);
  130. int hist_entry__sort_snprintf(struct hist_entry *he, char *bf, size_t size,
  131. struct hists *hists);
  132. void hist_entry__free(struct hist_entry *);
  133. void hists__output_resort(struct hists *hists);
  134. void hists__collapse_resort(struct hists *hists, struct ui_progress *prog);
  135. void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel);
  136. void hists__output_recalc_col_len(struct hists *hists, int max_rows);
  137. u64 hists__total_period(struct hists *hists);
  138. void hists__reset_stats(struct hists *hists);
  139. void hists__inc_stats(struct hists *hists, struct hist_entry *h);
  140. void hists__inc_nr_events(struct hists *hists, u32 type);
  141. void hists__inc_nr_samples(struct hists *hists, bool filtered);
  142. void events_stats__inc(struct events_stats *stats, u32 type);
  143. size_t events_stats__fprintf(struct events_stats *stats, FILE *fp);
  144. size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
  145. int max_cols, float min_pcnt, FILE *fp);
  146. void hists__filter_by_dso(struct hists *hists);
  147. void hists__filter_by_thread(struct hists *hists);
  148. void hists__filter_by_symbol(struct hists *hists);
  149. static inline bool hists__has_filter(struct hists *hists)
  150. {
  151. return hists->thread_filter || hists->dso_filter ||
  152. hists->symbol_filter_str;
  153. }
  154. u16 hists__col_len(struct hists *hists, enum hist_column col);
  155. void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len);
  156. bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len);
  157. void hists__reset_col_len(struct hists *hists);
  158. void hists__calc_col_len(struct hists *hists, struct hist_entry *he);
  159. void hists__match(struct hists *leader, struct hists *other);
  160. int hists__link(struct hists *leader, struct hists *other);
  161. struct perf_hpp {
  162. char *buf;
  163. size_t size;
  164. const char *sep;
  165. void *ptr;
  166. };
  167. struct perf_hpp_fmt {
  168. int (*header)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  169. struct perf_evsel *evsel);
  170. int (*width)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  171. struct perf_evsel *evsel);
  172. int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  173. struct hist_entry *he);
  174. int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  175. struct hist_entry *he);
  176. int64_t (*cmp)(struct hist_entry *a, struct hist_entry *b);
  177. int64_t (*collapse)(struct hist_entry *a, struct hist_entry *b);
  178. int64_t (*sort)(struct hist_entry *a, struct hist_entry *b);
  179. struct list_head list;
  180. struct list_head sort_list;
  181. bool elide;
  182. };
  183. extern struct list_head perf_hpp__list;
  184. extern struct list_head perf_hpp__sort_list;
  185. #define perf_hpp__for_each_format(format) \
  186. list_for_each_entry(format, &perf_hpp__list, list)
  187. #define perf_hpp__for_each_format_safe(format, tmp) \
  188. list_for_each_entry_safe(format, tmp, &perf_hpp__list, list)
  189. #define perf_hpp__for_each_sort_list(format) \
  190. list_for_each_entry(format, &perf_hpp__sort_list, sort_list)
  191. #define perf_hpp__for_each_sort_list_safe(format, tmp) \
  192. list_for_each_entry_safe(format, tmp, &perf_hpp__sort_list, sort_list)
  193. extern struct perf_hpp_fmt perf_hpp__format[];
  194. enum {
  195. /* Matches perf_hpp__format array. */
  196. PERF_HPP__OVERHEAD,
  197. PERF_HPP__OVERHEAD_SYS,
  198. PERF_HPP__OVERHEAD_US,
  199. PERF_HPP__OVERHEAD_GUEST_SYS,
  200. PERF_HPP__OVERHEAD_GUEST_US,
  201. PERF_HPP__OVERHEAD_ACC,
  202. PERF_HPP__SAMPLES,
  203. PERF_HPP__PERIOD,
  204. PERF_HPP__MAX_INDEX
  205. };
  206. void perf_hpp__init(void);
  207. void perf_hpp__column_register(struct perf_hpp_fmt *format);
  208. void perf_hpp__column_unregister(struct perf_hpp_fmt *format);
  209. void perf_hpp__column_enable(unsigned col);
  210. void perf_hpp__column_disable(unsigned col);
  211. void perf_hpp__cancel_cumulate(void);
  212. void perf_hpp__register_sort_field(struct perf_hpp_fmt *format);
  213. void perf_hpp__setup_output_field(void);
  214. void perf_hpp__reset_output_field(void);
  215. void perf_hpp__append_sort_keys(void);
  216. bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format);
  217. bool perf_hpp__same_sort_entry(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b);
  218. static inline bool perf_hpp__should_skip(struct perf_hpp_fmt *format)
  219. {
  220. return format->elide;
  221. }
  222. void perf_hpp__reset_width(struct perf_hpp_fmt *fmt, struct hists *hists);
  223. typedef u64 (*hpp_field_fn)(struct hist_entry *he);
  224. typedef int (*hpp_callback_fn)(struct perf_hpp *hpp, bool front);
  225. typedef int (*hpp_snprint_fn)(struct perf_hpp *hpp, const char *fmt, ...);
  226. int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he,
  227. hpp_field_fn get_field, const char *fmt,
  228. hpp_snprint_fn print_fn, bool fmt_percent);
  229. int __hpp__fmt_acc(struct perf_hpp *hpp, struct hist_entry *he,
  230. hpp_field_fn get_field, const char *fmt,
  231. hpp_snprint_fn print_fn, bool fmt_percent);
  232. static inline void advance_hpp(struct perf_hpp *hpp, int inc)
  233. {
  234. hpp->buf += inc;
  235. hpp->size -= inc;
  236. }
  237. static inline size_t perf_hpp__use_color(void)
  238. {
  239. return !symbol_conf.field_sep;
  240. }
  241. static inline size_t perf_hpp__color_overhead(void)
  242. {
  243. return perf_hpp__use_color() ?
  244. (COLOR_MAXLEN + sizeof(PERF_COLOR_RESET)) * PERF_HPP__MAX_INDEX
  245. : 0;
  246. }
  247. struct perf_evlist;
  248. struct hist_browser_timer {
  249. void (*timer)(void *arg);
  250. void *arg;
  251. int refresh;
  252. };
  253. #ifdef HAVE_SLANG_SUPPORT
  254. #include "../ui/keysyms.h"
  255. int hist_entry__tui_annotate(struct hist_entry *he, struct perf_evsel *evsel,
  256. struct hist_browser_timer *hbt);
  257. int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help,
  258. struct hist_browser_timer *hbt,
  259. float min_pcnt,
  260. struct perf_session_env *env);
  261. int script_browse(const char *script_opt);
  262. #else
  263. static inline
  264. int perf_evlist__tui_browse_hists(struct perf_evlist *evlist __maybe_unused,
  265. const char *help __maybe_unused,
  266. struct hist_browser_timer *hbt __maybe_unused,
  267. float min_pcnt __maybe_unused,
  268. struct perf_session_env *env __maybe_unused)
  269. {
  270. return 0;
  271. }
  272. static inline int hist_entry__tui_annotate(struct hist_entry *he __maybe_unused,
  273. struct perf_evsel *evsel __maybe_unused,
  274. struct hist_browser_timer *hbt __maybe_unused)
  275. {
  276. return 0;
  277. }
  278. static inline int script_browse(const char *script_opt __maybe_unused)
  279. {
  280. return 0;
  281. }
  282. #define K_LEFT -1000
  283. #define K_RIGHT -2000
  284. #define K_SWITCH_INPUT_DATA -3000
  285. #endif
  286. unsigned int hists__sort_list_width(struct hists *hists);
  287. struct option;
  288. int parse_filter_percentage(const struct option *opt __maybe_unused,
  289. const char *arg, int unset __maybe_unused);
  290. int perf_hist_config(const char *var, const char *value);
  291. #endif /* __PERF_HIST_H */