hist.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. HIST_FILTER__SOCKET,
  21. };
  22. enum hist_column {
  23. HISTC_SYMBOL,
  24. HISTC_DSO,
  25. HISTC_THREAD,
  26. HISTC_COMM,
  27. HISTC_PARENT,
  28. HISTC_CPU,
  29. HISTC_SOCKET,
  30. HISTC_SRCLINE,
  31. HISTC_SRCFILE,
  32. HISTC_MISPREDICT,
  33. HISTC_IN_TX,
  34. HISTC_ABORT,
  35. HISTC_SYMBOL_FROM,
  36. HISTC_SYMBOL_TO,
  37. HISTC_DSO_FROM,
  38. HISTC_DSO_TO,
  39. HISTC_LOCAL_WEIGHT,
  40. HISTC_GLOBAL_WEIGHT,
  41. HISTC_MEM_DADDR_SYMBOL,
  42. HISTC_MEM_DADDR_DSO,
  43. HISTC_MEM_LOCKED,
  44. HISTC_MEM_TLB,
  45. HISTC_MEM_LVL,
  46. HISTC_MEM_SNOOP,
  47. HISTC_MEM_DCACHELINE,
  48. HISTC_MEM_IADDR_SYMBOL,
  49. HISTC_TRANSACTION,
  50. HISTC_CYCLES,
  51. HISTC_TRACE,
  52. HISTC_NR_COLS, /* Last entry */
  53. };
  54. struct thread;
  55. struct dso;
  56. struct hists {
  57. struct rb_root entries_in_array[2];
  58. struct rb_root *entries_in;
  59. struct rb_root entries;
  60. struct rb_root entries_collapsed;
  61. u64 nr_entries;
  62. u64 nr_non_filtered_entries;
  63. u64 callchain_period;
  64. u64 callchain_non_filtered_period;
  65. struct thread *thread_filter;
  66. const struct dso *dso_filter;
  67. const char *uid_filter_str;
  68. const char *symbol_filter_str;
  69. pthread_mutex_t lock;
  70. struct events_stats stats;
  71. u64 event_stream;
  72. u16 col_len[HISTC_NR_COLS];
  73. int socket_filter;
  74. struct perf_hpp_list *hpp_list;
  75. struct list_head hpp_formats;
  76. int nr_hpp_node;
  77. };
  78. struct hist_entry_iter;
  79. struct hist_iter_ops {
  80. int (*prepare_entry)(struct hist_entry_iter *, struct addr_location *);
  81. int (*add_single_entry)(struct hist_entry_iter *, struct addr_location *);
  82. int (*next_entry)(struct hist_entry_iter *, struct addr_location *);
  83. int (*add_next_entry)(struct hist_entry_iter *, struct addr_location *);
  84. int (*finish_entry)(struct hist_entry_iter *, struct addr_location *);
  85. };
  86. struct hist_entry_iter {
  87. int total;
  88. int curr;
  89. bool hide_unresolved;
  90. int max_stack;
  91. struct perf_evsel *evsel;
  92. struct perf_sample *sample;
  93. struct hist_entry *he;
  94. struct symbol *parent;
  95. void *priv;
  96. const struct hist_iter_ops *ops;
  97. /* user-defined callback function (optional) */
  98. int (*add_entry_cb)(struct hist_entry_iter *iter,
  99. struct addr_location *al, bool single, void *arg);
  100. };
  101. extern const struct hist_iter_ops hist_iter_normal;
  102. extern const struct hist_iter_ops hist_iter_branch;
  103. extern const struct hist_iter_ops hist_iter_mem;
  104. extern const struct hist_iter_ops hist_iter_cumulative;
  105. struct hist_entry *__hists__add_entry(struct hists *hists,
  106. struct addr_location *al,
  107. struct symbol *parent,
  108. struct branch_info *bi,
  109. struct mem_info *mi,
  110. struct perf_sample *sample,
  111. bool sample_self);
  112. int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
  113. int max_stack_depth, void *arg);
  114. struct perf_hpp;
  115. struct perf_hpp_fmt;
  116. int64_t hist_entry__cmp(struct hist_entry *left, struct hist_entry *right);
  117. int64_t hist_entry__collapse(struct hist_entry *left, struct hist_entry *right);
  118. int hist_entry__transaction_len(void);
  119. int hist_entry__sort_snprintf(struct hist_entry *he, char *bf, size_t size,
  120. struct hists *hists);
  121. int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
  122. struct perf_hpp_fmt *fmt, int printed);
  123. void hist_entry__delete(struct hist_entry *he);
  124. void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog);
  125. void hists__output_resort(struct hists *hists, struct ui_progress *prog);
  126. int hists__collapse_resort(struct hists *hists, struct ui_progress *prog);
  127. void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel);
  128. void hists__delete_entries(struct hists *hists);
  129. void hists__output_recalc_col_len(struct hists *hists, int max_rows);
  130. u64 hists__total_period(struct hists *hists);
  131. void hists__reset_stats(struct hists *hists);
  132. void hists__inc_stats(struct hists *hists, struct hist_entry *h);
  133. void hists__inc_nr_events(struct hists *hists, u32 type);
  134. void hists__inc_nr_samples(struct hists *hists, bool filtered);
  135. void events_stats__inc(struct events_stats *stats, u32 type);
  136. size_t events_stats__fprintf(struct events_stats *stats, FILE *fp);
  137. size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
  138. int max_cols, float min_pcnt, FILE *fp);
  139. size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp);
  140. void hists__filter_by_dso(struct hists *hists);
  141. void hists__filter_by_thread(struct hists *hists);
  142. void hists__filter_by_symbol(struct hists *hists);
  143. void hists__filter_by_socket(struct hists *hists);
  144. static inline bool hists__has_filter(struct hists *hists)
  145. {
  146. return hists->thread_filter || hists->dso_filter ||
  147. hists->symbol_filter_str || (hists->socket_filter > -1);
  148. }
  149. u16 hists__col_len(struct hists *hists, enum hist_column col);
  150. void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len);
  151. bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len);
  152. void hists__reset_col_len(struct hists *hists);
  153. void hists__calc_col_len(struct hists *hists, struct hist_entry *he);
  154. void hists__match(struct hists *leader, struct hists *other);
  155. int hists__link(struct hists *leader, struct hists *other);
  156. struct hists_evsel {
  157. struct perf_evsel evsel;
  158. struct hists hists;
  159. };
  160. static inline struct perf_evsel *hists_to_evsel(struct hists *hists)
  161. {
  162. struct hists_evsel *hevsel = container_of(hists, struct hists_evsel, hists);
  163. return &hevsel->evsel;
  164. }
  165. static inline struct hists *evsel__hists(struct perf_evsel *evsel)
  166. {
  167. struct hists_evsel *hevsel = (struct hists_evsel *)evsel;
  168. return &hevsel->hists;
  169. }
  170. int hists__init(void);
  171. int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list);
  172. struct rb_root *hists__get_rotate_entries_in(struct hists *hists);
  173. int hists__collapse_insert_entry(struct hists *hists,
  174. struct rb_root *root, struct hist_entry *he);
  175. struct perf_hpp {
  176. char *buf;
  177. size_t size;
  178. const char *sep;
  179. void *ptr;
  180. };
  181. struct perf_hpp_fmt {
  182. const char *name;
  183. int (*header)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  184. struct perf_evsel *evsel);
  185. int (*width)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  186. struct perf_evsel *evsel);
  187. int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  188. struct hist_entry *he);
  189. int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  190. struct hist_entry *he);
  191. int64_t (*cmp)(struct perf_hpp_fmt *fmt,
  192. struct hist_entry *a, struct hist_entry *b);
  193. int64_t (*collapse)(struct perf_hpp_fmt *fmt,
  194. struct hist_entry *a, struct hist_entry *b);
  195. int64_t (*sort)(struct perf_hpp_fmt *fmt,
  196. struct hist_entry *a, struct hist_entry *b);
  197. bool (*equal)(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b);
  198. void (*free)(struct perf_hpp_fmt *fmt);
  199. struct list_head list;
  200. struct list_head sort_list;
  201. bool elide;
  202. int len;
  203. int user_len;
  204. int idx;
  205. int level;
  206. };
  207. struct perf_hpp_list {
  208. struct list_head fields;
  209. struct list_head sorts;
  210. };
  211. extern struct perf_hpp_list perf_hpp_list;
  212. struct perf_hpp_list_node {
  213. struct list_head list;
  214. struct perf_hpp_list hpp;
  215. int level;
  216. bool skip;
  217. };
  218. void perf_hpp_list__column_register(struct perf_hpp_list *list,
  219. struct perf_hpp_fmt *format);
  220. void perf_hpp_list__register_sort_field(struct perf_hpp_list *list,
  221. struct perf_hpp_fmt *format);
  222. static inline void perf_hpp__column_register(struct perf_hpp_fmt *format)
  223. {
  224. perf_hpp_list__column_register(&perf_hpp_list, format);
  225. }
  226. static inline void perf_hpp__register_sort_field(struct perf_hpp_fmt *format)
  227. {
  228. perf_hpp_list__register_sort_field(&perf_hpp_list, format);
  229. }
  230. #define perf_hpp_list__for_each_format(_list, format) \
  231. list_for_each_entry(format, &(_list)->fields, list)
  232. #define perf_hpp_list__for_each_format_safe(_list, format, tmp) \
  233. list_for_each_entry_safe(format, tmp, &(_list)->fields, list)
  234. #define perf_hpp_list__for_each_sort_list(_list, format) \
  235. list_for_each_entry(format, &(_list)->sorts, sort_list)
  236. #define perf_hpp_list__for_each_sort_list_safe(_list, format, tmp) \
  237. list_for_each_entry_safe(format, tmp, &(_list)->sorts, sort_list)
  238. #define hists__for_each_format(hists, format) \
  239. perf_hpp_list__for_each_format((hists)->hpp_list, fmt)
  240. #define hists__for_each_sort_list(hists, format) \
  241. perf_hpp_list__for_each_sort_list((hists)->hpp_list, fmt)
  242. extern struct perf_hpp_fmt perf_hpp__format[];
  243. enum {
  244. /* Matches perf_hpp__format array. */
  245. PERF_HPP__OVERHEAD,
  246. PERF_HPP__OVERHEAD_SYS,
  247. PERF_HPP__OVERHEAD_US,
  248. PERF_HPP__OVERHEAD_GUEST_SYS,
  249. PERF_HPP__OVERHEAD_GUEST_US,
  250. PERF_HPP__OVERHEAD_ACC,
  251. PERF_HPP__SAMPLES,
  252. PERF_HPP__PERIOD,
  253. PERF_HPP__MAX_INDEX
  254. };
  255. void perf_hpp__init(void);
  256. void perf_hpp__column_unregister(struct perf_hpp_fmt *format);
  257. void perf_hpp__cancel_cumulate(void);
  258. void perf_hpp__setup_output_field(struct perf_hpp_list *list);
  259. void perf_hpp__reset_output_field(struct perf_hpp_list *list);
  260. void perf_hpp__append_sort_keys(struct perf_hpp_list *list);
  261. int perf_hpp__setup_hists_formats(struct perf_hpp_list *list,
  262. struct perf_evlist *evlist);
  263. bool perf_hpp__is_sort_entry(struct perf_hpp_fmt *format);
  264. bool perf_hpp__is_dynamic_entry(struct perf_hpp_fmt *format);
  265. bool perf_hpp__defined_dynamic_entry(struct perf_hpp_fmt *fmt, struct hists *hists);
  266. bool perf_hpp__is_trace_entry(struct perf_hpp_fmt *fmt);
  267. bool perf_hpp__is_srcline_entry(struct perf_hpp_fmt *fmt);
  268. bool perf_hpp__is_srcfile_entry(struct perf_hpp_fmt *fmt);
  269. bool perf_hpp__is_thread_entry(struct perf_hpp_fmt *fmt);
  270. bool perf_hpp__is_comm_entry(struct perf_hpp_fmt *fmt);
  271. bool perf_hpp__is_dso_entry(struct perf_hpp_fmt *fmt);
  272. bool perf_hpp__is_sym_entry(struct perf_hpp_fmt *fmt);
  273. struct perf_hpp_fmt *perf_hpp_fmt__dup(struct perf_hpp_fmt *fmt);
  274. int hist_entry__filter(struct hist_entry *he, int type, const void *arg);
  275. static inline bool perf_hpp__should_skip(struct perf_hpp_fmt *format,
  276. struct hists *hists)
  277. {
  278. if (format->elide)
  279. return true;
  280. if (perf_hpp__is_dynamic_entry(format) &&
  281. !perf_hpp__defined_dynamic_entry(format, hists))
  282. return true;
  283. return false;
  284. }
  285. void perf_hpp__reset_width(struct perf_hpp_fmt *fmt, struct hists *hists);
  286. void perf_hpp__reset_sort_width(struct perf_hpp_fmt *fmt, struct hists *hists);
  287. void perf_hpp__set_user_width(const char *width_list_str);
  288. typedef u64 (*hpp_field_fn)(struct hist_entry *he);
  289. typedef int (*hpp_callback_fn)(struct perf_hpp *hpp, bool front);
  290. typedef int (*hpp_snprint_fn)(struct perf_hpp *hpp, const char *fmt, ...);
  291. int hpp__fmt(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  292. struct hist_entry *he, hpp_field_fn get_field,
  293. const char *fmtstr, hpp_snprint_fn print_fn, bool fmt_percent);
  294. int hpp__fmt_acc(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  295. struct hist_entry *he, hpp_field_fn get_field,
  296. const char *fmtstr, hpp_snprint_fn print_fn, bool fmt_percent);
  297. static inline void advance_hpp(struct perf_hpp *hpp, int inc)
  298. {
  299. hpp->buf += inc;
  300. hpp->size -= inc;
  301. }
  302. static inline size_t perf_hpp__use_color(void)
  303. {
  304. return !symbol_conf.field_sep;
  305. }
  306. static inline size_t perf_hpp__color_overhead(void)
  307. {
  308. return perf_hpp__use_color() ?
  309. (COLOR_MAXLEN + sizeof(PERF_COLOR_RESET)) * PERF_HPP__MAX_INDEX
  310. : 0;
  311. }
  312. struct perf_evlist;
  313. struct hist_browser_timer {
  314. void (*timer)(void *arg);
  315. void *arg;
  316. int refresh;
  317. };
  318. #ifdef HAVE_SLANG_SUPPORT
  319. #include "../ui/keysyms.h"
  320. int map_symbol__tui_annotate(struct map_symbol *ms, struct perf_evsel *evsel,
  321. struct hist_browser_timer *hbt);
  322. int hist_entry__tui_annotate(struct hist_entry *he, struct perf_evsel *evsel,
  323. struct hist_browser_timer *hbt);
  324. int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help,
  325. struct hist_browser_timer *hbt,
  326. float min_pcnt,
  327. struct perf_env *env);
  328. int script_browse(const char *script_opt);
  329. #else
  330. static inline
  331. int perf_evlist__tui_browse_hists(struct perf_evlist *evlist __maybe_unused,
  332. const char *help __maybe_unused,
  333. struct hist_browser_timer *hbt __maybe_unused,
  334. float min_pcnt __maybe_unused,
  335. struct perf_env *env __maybe_unused)
  336. {
  337. return 0;
  338. }
  339. static inline int map_symbol__tui_annotate(struct map_symbol *ms __maybe_unused,
  340. struct perf_evsel *evsel __maybe_unused,
  341. struct hist_browser_timer *hbt __maybe_unused)
  342. {
  343. return 0;
  344. }
  345. static inline int hist_entry__tui_annotate(struct hist_entry *he __maybe_unused,
  346. struct perf_evsel *evsel __maybe_unused,
  347. struct hist_browser_timer *hbt __maybe_unused)
  348. {
  349. return 0;
  350. }
  351. static inline int script_browse(const char *script_opt __maybe_unused)
  352. {
  353. return 0;
  354. }
  355. #define K_LEFT -1000
  356. #define K_RIGHT -2000
  357. #define K_SWITCH_INPUT_DATA -3000
  358. #endif
  359. unsigned int hists__sort_list_width(struct hists *hists);
  360. unsigned int hists__overhead_width(struct hists *hists);
  361. void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
  362. struct perf_sample *sample, bool nonany_branch_mode);
  363. struct option;
  364. int parse_filter_percentage(const struct option *opt, const char *arg, int unset);
  365. int perf_hist_config(const char *var, const char *value);
  366. void perf_hpp_list__init(struct perf_hpp_list *list);
  367. enum hierarchy_move_dir {
  368. HMD_NORMAL,
  369. HMD_FORCE_SIBLING,
  370. HMD_FORCE_CHILD,
  371. };
  372. struct rb_node *rb_hierarchy_last(struct rb_node *node);
  373. struct rb_node *__rb_hierarchy_next(struct rb_node *node,
  374. enum hierarchy_move_dir hmd);
  375. struct rb_node *rb_hierarchy_prev(struct rb_node *node);
  376. static inline struct rb_node *rb_hierarchy_next(struct rb_node *node)
  377. {
  378. return __rb_hierarchy_next(node, HMD_NORMAL);
  379. }
  380. #define HIERARCHY_INDENT 3
  381. bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit);
  382. #endif /* __PERF_HIST_H */