hist.h 17 KB

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