hist.h 15 KB

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