callchain.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __PERF_CALLCHAIN_H
  3. #define __PERF_CALLCHAIN_H
  4. #include <linux/list.h>
  5. #include <linux/rbtree.h>
  6. #include "map_symbol.h"
  7. #include "branch.h"
  8. struct addr_location;
  9. struct evsel;
  10. struct ip_callchain;
  11. struct map;
  12. struct perf_sample;
  13. struct thread;
  14. struct hists;
  15. #define HELP_PAD "\t\t\t\t"
  16. #define CALLCHAIN_HELP "setup and enables call-graph (stack chain/backtrace):\n\n"
  17. # define RECORD_MODE_HELP HELP_PAD "record_mode:\tcall graph recording mode (fp|dwarf|lbr)\n"
  18. #define RECORD_SIZE_HELP \
  19. HELP_PAD "record_size:\tif record_mode is 'dwarf', max size of stack recording (<bytes>)\n" \
  20. HELP_PAD "\t\tdefault: 8192 (bytes)\n"
  21. #define CALLCHAIN_RECORD_HELP CALLCHAIN_HELP RECORD_MODE_HELP RECORD_SIZE_HELP
  22. #define CALLCHAIN_REPORT_HELP \
  23. HELP_PAD "print_type:\tcall graph printing style (graph|flat|fractal|folded|none)\n" \
  24. HELP_PAD "threshold:\tminimum call graph inclusion threshold (<percent>)\n" \
  25. HELP_PAD "print_limit:\tmaximum number of call graph entry (<number>)\n" \
  26. HELP_PAD "order:\t\tcall graph order (caller|callee)\n" \
  27. HELP_PAD "sort_key:\tcall graph sort key (function|address)\n" \
  28. HELP_PAD "branch:\t\tinclude last branch info to call graph (branch)\n" \
  29. HELP_PAD "value:\t\tcall graph value (percent|period|count)\n"
  30. enum perf_call_graph_mode {
  31. CALLCHAIN_NONE,
  32. CALLCHAIN_FP,
  33. CALLCHAIN_DWARF,
  34. CALLCHAIN_LBR,
  35. CALLCHAIN_MAX
  36. };
  37. enum chain_mode {
  38. CHAIN_NONE,
  39. CHAIN_FLAT,
  40. CHAIN_GRAPH_ABS,
  41. CHAIN_GRAPH_REL,
  42. CHAIN_FOLDED,
  43. };
  44. enum chain_order {
  45. ORDER_CALLER,
  46. ORDER_CALLEE
  47. };
  48. struct callchain_node {
  49. struct callchain_node *parent;
  50. struct list_head val;
  51. struct list_head parent_val;
  52. struct rb_node rb_node_in; /* to insert nodes in an rbtree */
  53. struct rb_node rb_node; /* to sort nodes in an output tree */
  54. struct rb_root rb_root_in; /* input tree of children */
  55. struct rb_root rb_root; /* sorted output tree of children */
  56. unsigned int val_nr;
  57. unsigned int count;
  58. unsigned int children_count;
  59. u64 hit;
  60. u64 children_hit;
  61. };
  62. struct callchain_root {
  63. u64 max_depth;
  64. struct callchain_node node;
  65. };
  66. struct callchain_param;
  67. typedef void (*sort_chain_func_t)(struct rb_root *, struct callchain_root *,
  68. u64, struct callchain_param *);
  69. enum chain_key {
  70. CCKEY_FUNCTION,
  71. CCKEY_ADDRESS,
  72. CCKEY_SRCLINE
  73. };
  74. enum chain_value {
  75. CCVAL_PERCENT,
  76. CCVAL_PERIOD,
  77. CCVAL_COUNT,
  78. };
  79. extern bool dwarf_callchain_users;
  80. struct callchain_param {
  81. bool enabled;
  82. enum perf_call_graph_mode record_mode;
  83. u32 dump_size;
  84. enum chain_mode mode;
  85. u16 max_stack;
  86. u32 print_limit;
  87. double min_percent;
  88. sort_chain_func_t sort;
  89. enum chain_order order;
  90. bool order_set;
  91. enum chain_key key;
  92. bool branch_callstack;
  93. enum chain_value value;
  94. };
  95. extern struct callchain_param callchain_param;
  96. extern struct callchain_param callchain_param_default;
  97. struct callchain_list {
  98. u64 ip;
  99. struct map_symbol ms;
  100. struct /* for TUI */ {
  101. bool unfolded;
  102. bool has_children;
  103. };
  104. u64 branch_count;
  105. u64 from_count;
  106. u64 predicted_count;
  107. u64 abort_count;
  108. u64 cycles_count;
  109. u64 iter_count;
  110. u64 iter_cycles;
  111. struct branch_type_stat brtype_stat;
  112. const char *srcline;
  113. struct list_head list;
  114. };
  115. /*
  116. * A callchain cursor is a single linked list that
  117. * let one feed a callchain progressively.
  118. * It keeps persistent allocated entries to minimize
  119. * allocations.
  120. */
  121. struct callchain_cursor_node {
  122. u64 ip;
  123. struct map_symbol ms;
  124. const char *srcline;
  125. /* Indicate valid cursor node for LBR stitch */
  126. bool valid;
  127. bool branch;
  128. struct branch_flags branch_flags;
  129. u64 branch_from;
  130. int nr_loop_iter;
  131. u64 iter_cycles;
  132. struct callchain_cursor_node *next;
  133. };
  134. struct stitch_list {
  135. struct list_head node;
  136. struct callchain_cursor_node cursor;
  137. };
  138. struct callchain_cursor {
  139. u64 nr;
  140. struct callchain_cursor_node *first;
  141. struct callchain_cursor_node **last;
  142. u64 pos;
  143. struct callchain_cursor_node *curr;
  144. };
  145. extern __thread struct callchain_cursor callchain_cursor;
  146. static inline void callchain_init(struct callchain_root *root)
  147. {
  148. INIT_LIST_HEAD(&root->node.val);
  149. INIT_LIST_HEAD(&root->node.parent_val);
  150. root->node.parent = NULL;
  151. root->node.hit = 0;
  152. root->node.children_hit = 0;
  153. root->node.rb_root_in = RB_ROOT;
  154. root->max_depth = 0;
  155. }
  156. static inline u64 callchain_cumul_hits(struct callchain_node *node)
  157. {
  158. return node->hit + node->children_hit;
  159. }
  160. static inline unsigned callchain_cumul_counts(struct callchain_node *node)
  161. {
  162. return node->count + node->children_count;
  163. }
  164. int callchain_register_param(struct callchain_param *param);
  165. int callchain_append(struct callchain_root *root,
  166. struct callchain_cursor *cursor,
  167. u64 period);
  168. int callchain_merge(struct callchain_cursor *cursor,
  169. struct callchain_root *dst, struct callchain_root *src);
  170. void callchain_cursor_reset(struct callchain_cursor *cursor);
  171. int callchain_cursor_append(struct callchain_cursor *cursor, u64 ip,
  172. struct map_symbol *ms,
  173. bool branch, struct branch_flags *flags,
  174. int nr_loop_iter, u64 iter_cycles, u64 branch_from,
  175. const char *srcline);
  176. /* Close a cursor writing session. Initialize for the reader */
  177. static inline void callchain_cursor_commit(struct callchain_cursor *cursor)
  178. {
  179. cursor->curr = cursor->first;
  180. cursor->pos = 0;
  181. }
  182. /* Cursor reading iteration helpers */
  183. static inline struct callchain_cursor_node *
  184. callchain_cursor_current(struct callchain_cursor *cursor)
  185. {
  186. if (cursor->pos == cursor->nr)
  187. return NULL;
  188. return cursor->curr;
  189. }
  190. static inline void callchain_cursor_advance(struct callchain_cursor *cursor)
  191. {
  192. cursor->curr = cursor->curr->next;
  193. cursor->pos++;
  194. }
  195. int callchain_cursor__copy(struct callchain_cursor *dst,
  196. struct callchain_cursor *src);
  197. struct option;
  198. struct hist_entry;
  199. int record_parse_callchain_opt(const struct option *opt, const char *arg, int unset);
  200. int record_callchain_opt(const struct option *opt, const char *arg, int unset);
  201. struct record_opts;
  202. int record_opts__parse_callchain(struct record_opts *record,
  203. struct callchain_param *callchain,
  204. const char *arg, bool unset);
  205. int sample__resolve_callchain(struct perf_sample *sample,
  206. struct callchain_cursor *cursor, struct symbol **parent,
  207. struct evsel *evsel, struct addr_location *al,
  208. int max_stack);
  209. int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample);
  210. int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
  211. bool hide_unresolved);
  212. extern const char record_callchain_help[];
  213. int parse_callchain_record(const char *arg, struct callchain_param *param);
  214. int parse_callchain_record_opt(const char *arg, struct callchain_param *param);
  215. int parse_callchain_report_opt(const char *arg);
  216. int parse_callchain_top_opt(const char *arg);
  217. int perf_callchain_config(const char *var, const char *value);
  218. static inline void callchain_cursor_snapshot(struct callchain_cursor *dest,
  219. struct callchain_cursor *src)
  220. {
  221. *dest = *src;
  222. dest->first = src->curr;
  223. dest->nr -= src->pos;
  224. }
  225. #ifdef HAVE_SKIP_CALLCHAIN_IDX
  226. int arch_skip_callchain_idx(struct thread *thread, struct ip_callchain *chain);
  227. #else
  228. static inline int arch_skip_callchain_idx(struct thread *thread __maybe_unused,
  229. struct ip_callchain *chain __maybe_unused)
  230. {
  231. return -1;
  232. }
  233. #endif
  234. char *callchain_list__sym_name(struct callchain_list *cl,
  235. char *bf, size_t bfsize, bool show_dso);
  236. char *callchain_node__scnprintf_value(struct callchain_node *node,
  237. char *bf, size_t bfsize, u64 total);
  238. int callchain_node__fprintf_value(struct callchain_node *node,
  239. FILE *fp, u64 total);
  240. int callchain_list_counts__printf_value(struct callchain_list *clist,
  241. FILE *fp, char *bf, int bfsize);
  242. void free_callchain(struct callchain_root *root);
  243. void decay_callchain(struct callchain_root *root);
  244. int callchain_node__make_parent_list(struct callchain_node *node);
  245. int callchain_branch_counts(struct callchain_root *root,
  246. u64 *branch_count, u64 *predicted_count,
  247. u64 *abort_count, u64 *cycles_count);
  248. void callchain_param_setup(u64 sample_type);
  249. bool callchain_cnode_matched(struct callchain_node *base_cnode,
  250. struct callchain_node *pair_cnode);
  251. u64 callchain_total_hits(struct hists *hists);
  252. s64 callchain_avg_cycles(struct callchain_node *cnode);
  253. #endif /* __PERF_CALLCHAIN_H */