hist.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. const char *name;
  169. int (*header)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  170. struct perf_evsel *evsel);
  171. int (*width)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  172. struct perf_evsel *evsel);
  173. int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  174. struct hist_entry *he);
  175. int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  176. struct hist_entry *he);
  177. int64_t (*cmp)(struct hist_entry *a, struct hist_entry *b);
  178. int64_t (*collapse)(struct hist_entry *a, struct hist_entry *b);
  179. int64_t (*sort)(struct hist_entry *a, struct hist_entry *b);
  180. struct list_head list;
  181. struct list_head sort_list;
  182. bool elide;
  183. int len;
  184. int user_len;
  185. };
  186. extern struct list_head perf_hpp__list;
  187. extern struct list_head perf_hpp__sort_list;
  188. #define perf_hpp__for_each_format(format) \
  189. list_for_each_entry(format, &perf_hpp__list, list)
  190. #define perf_hpp__for_each_format_safe(format, tmp) \
  191. list_for_each_entry_safe(format, tmp, &perf_hpp__list, list)
  192. #define perf_hpp__for_each_sort_list(format) \
  193. list_for_each_entry(format, &perf_hpp__sort_list, sort_list)
  194. #define perf_hpp__for_each_sort_list_safe(format, tmp) \
  195. list_for_each_entry_safe(format, tmp, &perf_hpp__sort_list, sort_list)
  196. extern struct perf_hpp_fmt perf_hpp__format[];
  197. enum {
  198. /* Matches perf_hpp__format array. */
  199. PERF_HPP__OVERHEAD,
  200. PERF_HPP__OVERHEAD_SYS,
  201. PERF_HPP__OVERHEAD_US,
  202. PERF_HPP__OVERHEAD_GUEST_SYS,
  203. PERF_HPP__OVERHEAD_GUEST_US,
  204. PERF_HPP__OVERHEAD_ACC,
  205. PERF_HPP__SAMPLES,
  206. PERF_HPP__PERIOD,
  207. PERF_HPP__MAX_INDEX
  208. };
  209. void perf_hpp__init(void);
  210. void perf_hpp__column_register(struct perf_hpp_fmt *format);
  211. void perf_hpp__column_unregister(struct perf_hpp_fmt *format);
  212. void perf_hpp__column_enable(unsigned col);
  213. void perf_hpp__column_disable(unsigned col);
  214. void perf_hpp__cancel_cumulate(void);
  215. void perf_hpp__register_sort_field(struct perf_hpp_fmt *format);
  216. void perf_hpp__setup_output_field(void);
  217. void perf_hpp__reset_output_field(void);
  218. void perf_hpp__append_sort_keys(void);
  219. bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format);
  220. bool perf_hpp__same_sort_entry(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b);
  221. static inline bool perf_hpp__should_skip(struct perf_hpp_fmt *format)
  222. {
  223. return format->elide;
  224. }
  225. void perf_hpp__reset_width(struct perf_hpp_fmt *fmt, struct hists *hists);
  226. void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists);
  227. void perf_hpp__set_user_width(const char *width_list_str);
  228. typedef u64 (*hpp_field_fn)(struct hist_entry *he);
  229. typedef int (*hpp_callback_fn)(struct perf_hpp *hpp, bool front);
  230. typedef int (*hpp_snprint_fn)(struct perf_hpp *hpp, const char *fmt, ...);
  231. int hpp__fmt(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  232. struct hist_entry *he, hpp_field_fn get_field,
  233. const char *fmtstr, hpp_snprint_fn print_fn, bool fmt_percent);
  234. int hpp__fmt_acc(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  235. struct hist_entry *he, hpp_field_fn get_field,
  236. const char *fmtstr, hpp_snprint_fn print_fn, bool fmt_percent);
  237. static inline void advance_hpp(struct perf_hpp *hpp, int inc)
  238. {
  239. hpp->buf += inc;
  240. hpp->size -= inc;
  241. }
  242. static inline size_t perf_hpp__use_color(void)
  243. {
  244. return !symbol_conf.field_sep;
  245. }
  246. static inline size_t perf_hpp__color_overhead(void)
  247. {
  248. return perf_hpp__use_color() ?
  249. (COLOR_MAXLEN + sizeof(PERF_COLOR_RESET)) * PERF_HPP__MAX_INDEX
  250. : 0;
  251. }
  252. struct perf_evlist;
  253. struct hist_browser_timer {
  254. void (*timer)(void *arg);
  255. void *arg;
  256. int refresh;
  257. };
  258. #ifdef HAVE_SLANG_SUPPORT
  259. #include "../ui/keysyms.h"
  260. int hist_entry__tui_annotate(struct hist_entry *he, struct perf_evsel *evsel,
  261. struct hist_browser_timer *hbt);
  262. int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help,
  263. struct hist_browser_timer *hbt,
  264. float min_pcnt,
  265. struct perf_session_env *env);
  266. int script_browse(const char *script_opt);
  267. #else
  268. static inline
  269. int perf_evlist__tui_browse_hists(struct perf_evlist *evlist __maybe_unused,
  270. const char *help __maybe_unused,
  271. struct hist_browser_timer *hbt __maybe_unused,
  272. float min_pcnt __maybe_unused,
  273. struct perf_session_env *env __maybe_unused)
  274. {
  275. return 0;
  276. }
  277. static inline int hist_entry__tui_annotate(struct hist_entry *he __maybe_unused,
  278. struct perf_evsel *evsel __maybe_unused,
  279. struct hist_browser_timer *hbt __maybe_unused)
  280. {
  281. return 0;
  282. }
  283. static inline int script_browse(const char *script_opt __maybe_unused)
  284. {
  285. return 0;
  286. }
  287. #define K_LEFT -1000
  288. #define K_RIGHT -2000
  289. #define K_SWITCH_INPUT_DATA -3000
  290. #endif
  291. unsigned int hists__sort_list_width(struct hists *hists);
  292. struct option;
  293. int parse_filter_percentage(const struct option *opt __maybe_unused,
  294. const char *arg, int unset __maybe_unused);
  295. int perf_hist_config(const char *var, const char *value);
  296. #endif /* __PERF_HIST_H */