hist.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 "evsel.h"
  7. #include "header.h"
  8. #include "color.h"
  9. #include "ui/progress.h"
  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. enum hist_column {
  22. HISTC_SYMBOL,
  23. HISTC_DSO,
  24. HISTC_THREAD,
  25. HISTC_COMM,
  26. HISTC_PARENT,
  27. HISTC_CPU,
  28. HISTC_SRCLINE,
  29. HISTC_MISPREDICT,
  30. HISTC_IN_TX,
  31. HISTC_ABORT,
  32. HISTC_SYMBOL_FROM,
  33. HISTC_SYMBOL_TO,
  34. HISTC_DSO_FROM,
  35. HISTC_DSO_TO,
  36. HISTC_LOCAL_WEIGHT,
  37. HISTC_GLOBAL_WEIGHT,
  38. HISTC_MEM_DADDR_SYMBOL,
  39. HISTC_MEM_DADDR_DSO,
  40. HISTC_MEM_LOCKED,
  41. HISTC_MEM_TLB,
  42. HISTC_MEM_LVL,
  43. HISTC_MEM_SNOOP,
  44. HISTC_MEM_DCACHELINE,
  45. HISTC_TRANSACTION,
  46. HISTC_NR_COLS, /* Last entry */
  47. };
  48. struct thread;
  49. struct dso;
  50. struct hists {
  51. struct rb_root entries_in_array[2];
  52. struct rb_root *entries_in;
  53. struct rb_root entries;
  54. struct rb_root entries_collapsed;
  55. u64 nr_entries;
  56. u64 nr_non_filtered_entries;
  57. const struct thread *thread_filter;
  58. const struct dso *dso_filter;
  59. const char *uid_filter_str;
  60. const char *symbol_filter_str;
  61. pthread_mutex_t lock;
  62. struct events_stats stats;
  63. u64 event_stream;
  64. u16 col_len[HISTC_NR_COLS];
  65. };
  66. struct hist_entry_iter;
  67. struct hist_iter_ops {
  68. int (*prepare_entry)(struct hist_entry_iter *, struct addr_location *);
  69. int (*add_single_entry)(struct hist_entry_iter *, struct addr_location *);
  70. int (*next_entry)(struct hist_entry_iter *, struct addr_location *);
  71. int (*add_next_entry)(struct hist_entry_iter *, struct addr_location *);
  72. int (*finish_entry)(struct hist_entry_iter *, struct addr_location *);
  73. };
  74. struct hist_entry_iter {
  75. int total;
  76. int curr;
  77. bool hide_unresolved;
  78. struct perf_evsel *evsel;
  79. struct perf_sample *sample;
  80. struct hist_entry *he;
  81. struct symbol *parent;
  82. void *priv;
  83. const struct hist_iter_ops *ops;
  84. /* user-defined callback function (optional) */
  85. int (*add_entry_cb)(struct hist_entry_iter *iter,
  86. struct addr_location *al, bool single, void *arg);
  87. };
  88. extern const struct hist_iter_ops hist_iter_normal;
  89. extern const struct hist_iter_ops hist_iter_branch;
  90. extern const struct hist_iter_ops hist_iter_mem;
  91. extern const struct hist_iter_ops hist_iter_cumulative;
  92. struct hist_entry *__hists__add_entry(struct hists *hists,
  93. struct addr_location *al,
  94. struct symbol *parent,
  95. struct branch_info *bi,
  96. struct mem_info *mi, u64 period,
  97. u64 weight, u64 transaction,
  98. bool sample_self);
  99. int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
  100. struct perf_evsel *evsel, struct perf_sample *sample,
  101. int max_stack_depth, void *arg);
  102. int64_t hist_entry__cmp(struct hist_entry *left, struct hist_entry *right);
  103. int64_t hist_entry__collapse(struct hist_entry *left, struct hist_entry *right);
  104. int hist_entry__transaction_len(void);
  105. int hist_entry__sort_snprintf(struct hist_entry *he, char *bf, size_t size,
  106. struct hists *hists);
  107. void hist_entry__delete(struct hist_entry *he);
  108. void hists__output_resort(struct hists *hists, struct ui_progress *prog);
  109. void hists__collapse_resort(struct hists *hists, struct ui_progress *prog);
  110. void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel);
  111. void hists__delete_entries(struct hists *hists);
  112. void hists__output_recalc_col_len(struct hists *hists, int max_rows);
  113. u64 hists__total_period(struct hists *hists);
  114. void hists__reset_stats(struct hists *hists);
  115. void hists__inc_stats(struct hists *hists, struct hist_entry *h);
  116. void hists__inc_nr_events(struct hists *hists, u32 type);
  117. void hists__inc_nr_samples(struct hists *hists, bool filtered);
  118. void events_stats__inc(struct events_stats *stats, u32 type);
  119. size_t events_stats__fprintf(struct events_stats *stats, FILE *fp);
  120. size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
  121. int max_cols, float min_pcnt, FILE *fp);
  122. size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp);
  123. void hists__filter_by_dso(struct hists *hists);
  124. void hists__filter_by_thread(struct hists *hists);
  125. void hists__filter_by_symbol(struct hists *hists);
  126. static inline bool hists__has_filter(struct hists *hists)
  127. {
  128. return hists->thread_filter || hists->dso_filter ||
  129. hists->symbol_filter_str;
  130. }
  131. u16 hists__col_len(struct hists *hists, enum hist_column col);
  132. void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len);
  133. bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len);
  134. void hists__reset_col_len(struct hists *hists);
  135. void hists__calc_col_len(struct hists *hists, struct hist_entry *he);
  136. void hists__match(struct hists *leader, struct hists *other);
  137. int hists__link(struct hists *leader, struct hists *other);
  138. struct hists_evsel {
  139. struct perf_evsel evsel;
  140. struct hists hists;
  141. };
  142. static inline struct perf_evsel *hists_to_evsel(struct hists *hists)
  143. {
  144. struct hists_evsel *hevsel = container_of(hists, struct hists_evsel, hists);
  145. return &hevsel->evsel;
  146. }
  147. static inline struct hists *evsel__hists(struct perf_evsel *evsel)
  148. {
  149. struct hists_evsel *hevsel = (struct hists_evsel *)evsel;
  150. return &hevsel->hists;
  151. }
  152. int hists__init(void);
  153. struct perf_hpp {
  154. char *buf;
  155. size_t size;
  156. const char *sep;
  157. void *ptr;
  158. };
  159. struct perf_hpp_fmt {
  160. const char *name;
  161. int (*header)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  162. struct perf_evsel *evsel);
  163. int (*width)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  164. struct perf_evsel *evsel);
  165. int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  166. struct hist_entry *he);
  167. int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  168. struct hist_entry *he);
  169. int64_t (*cmp)(struct perf_hpp_fmt *fmt,
  170. struct hist_entry *a, struct hist_entry *b);
  171. int64_t (*collapse)(struct perf_hpp_fmt *fmt,
  172. struct hist_entry *a, struct hist_entry *b);
  173. int64_t (*sort)(struct perf_hpp_fmt *fmt,
  174. struct hist_entry *a, struct hist_entry *b);
  175. struct list_head list;
  176. struct list_head sort_list;
  177. bool elide;
  178. int len;
  179. int user_len;
  180. };
  181. extern struct list_head perf_hpp__list;
  182. extern struct list_head perf_hpp__sort_list;
  183. #define perf_hpp__for_each_format(format) \
  184. list_for_each_entry(format, &perf_hpp__list, list)
  185. #define perf_hpp__for_each_format_safe(format, tmp) \
  186. list_for_each_entry_safe(format, tmp, &perf_hpp__list, list)
  187. #define perf_hpp__for_each_sort_list(format) \
  188. list_for_each_entry(format, &perf_hpp__sort_list, sort_list)
  189. #define perf_hpp__for_each_sort_list_safe(format, tmp) \
  190. list_for_each_entry_safe(format, tmp, &perf_hpp__sort_list, sort_list)
  191. extern struct perf_hpp_fmt perf_hpp__format[];
  192. enum {
  193. /* Matches perf_hpp__format array. */
  194. PERF_HPP__OVERHEAD,
  195. PERF_HPP__OVERHEAD_SYS,
  196. PERF_HPP__OVERHEAD_US,
  197. PERF_HPP__OVERHEAD_GUEST_SYS,
  198. PERF_HPP__OVERHEAD_GUEST_US,
  199. PERF_HPP__OVERHEAD_ACC,
  200. PERF_HPP__SAMPLES,
  201. PERF_HPP__PERIOD,
  202. PERF_HPP__MAX_INDEX
  203. };
  204. void perf_hpp__init(void);
  205. void perf_hpp__column_register(struct perf_hpp_fmt *format);
  206. void perf_hpp__column_unregister(struct perf_hpp_fmt *format);
  207. void perf_hpp__column_enable(unsigned col);
  208. void perf_hpp__column_disable(unsigned col);
  209. void perf_hpp__cancel_cumulate(void);
  210. void perf_hpp__register_sort_field(struct perf_hpp_fmt *format);
  211. void perf_hpp__setup_output_field(void);
  212. void perf_hpp__reset_output_field(void);
  213. void perf_hpp__append_sort_keys(void);
  214. bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format);
  215. bool perf_hpp__same_sort_entry(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b);
  216. static inline bool perf_hpp__should_skip(struct perf_hpp_fmt *format)
  217. {
  218. return format->elide;
  219. }
  220. void perf_hpp__reset_width(struct perf_hpp_fmt *fmt, struct hists *hists);
  221. void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists);
  222. void perf_hpp__set_user_width(const char *width_list_str);
  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_fmt *fmt, struct perf_hpp *hpp,
  227. struct hist_entry *he, hpp_field_fn get_field,
  228. const char *fmtstr, hpp_snprint_fn print_fn, bool fmt_percent);
  229. int hpp__fmt_acc(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  230. struct hist_entry *he, hpp_field_fn get_field,
  231. const char *fmtstr, 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 */