hist.h 16 KB

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