callchain.h 8.1 KB

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