hist.h 15 KB

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