callchain.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 predicted_count;
  102. u64 abort_count;
  103. u64 cycles_count;
  104. u64 iter_count;
  105. u64 iter_cycles;
  106. struct branch_type_stat brtype_stat;
  107. const char *srcline;
  108. struct list_head list;
  109. };
  110. /*
  111. * A callchain cursor is a single linked list that
  112. * let one feed a callchain progressively.
  113. * It keeps persistent allocated entries to minimize
  114. * allocations.
  115. */
  116. struct callchain_cursor_node {
  117. u64 ip;
  118. struct map *map;
  119. struct symbol *sym;
  120. const char *srcline;
  121. bool branch;
  122. struct branch_flags branch_flags;
  123. u64 branch_from;
  124. int nr_loop_iter;
  125. u64 iter_cycles;
  126. struct callchain_cursor_node *next;
  127. };
  128. struct callchain_cursor {
  129. u64 nr;
  130. struct callchain_cursor_node *first;
  131. struct callchain_cursor_node **last;
  132. u64 pos;
  133. struct callchain_cursor_node *curr;
  134. };
  135. extern __thread struct callchain_cursor callchain_cursor;
  136. static inline void callchain_init(struct callchain_root *root)
  137. {
  138. INIT_LIST_HEAD(&root->node.val);
  139. INIT_LIST_HEAD(&root->node.parent_val);
  140. root->node.parent = NULL;
  141. root->node.hit = 0;
  142. root->node.children_hit = 0;
  143. root->node.rb_root_in = RB_ROOT;
  144. root->max_depth = 0;
  145. }
  146. static inline u64 callchain_cumul_hits(struct callchain_node *node)
  147. {
  148. return node->hit + node->children_hit;
  149. }
  150. static inline unsigned callchain_cumul_counts(struct callchain_node *node)
  151. {
  152. return node->count + node->children_count;
  153. }
  154. int callchain_register_param(struct callchain_param *param);
  155. int callchain_append(struct callchain_root *root,
  156. struct callchain_cursor *cursor,
  157. u64 period);
  158. int callchain_merge(struct callchain_cursor *cursor,
  159. struct callchain_root *dst, struct callchain_root *src);
  160. /*
  161. * Initialize a cursor before adding entries inside, but keep
  162. * the previously allocated entries as a cache.
  163. */
  164. static inline void callchain_cursor_reset(struct callchain_cursor *cursor)
  165. {
  166. struct callchain_cursor_node *node;
  167. cursor->nr = 0;
  168. cursor->last = &cursor->first;
  169. for (node = cursor->first; node != NULL; node = node->next)
  170. map__zput(node->map);
  171. }
  172. int callchain_cursor_append(struct callchain_cursor *cursor, u64 ip,
  173. struct map *map, struct symbol *sym,
  174. bool branch, struct branch_flags *flags,
  175. int nr_loop_iter, u64 iter_cycles, u64 branch_from,
  176. const char *srcline);
  177. /* Close a cursor writing session. Initialize for the reader */
  178. static inline void callchain_cursor_commit(struct callchain_cursor *cursor)
  179. {
  180. cursor->curr = cursor->first;
  181. cursor->pos = 0;
  182. }
  183. /* Cursor reading iteration helpers */
  184. static inline struct callchain_cursor_node *
  185. callchain_cursor_current(struct callchain_cursor *cursor)
  186. {
  187. if (cursor->pos == cursor->nr)
  188. return NULL;
  189. return cursor->curr;
  190. }
  191. static inline void callchain_cursor_advance(struct callchain_cursor *cursor)
  192. {
  193. cursor->curr = cursor->curr->next;
  194. cursor->pos++;
  195. }
  196. int callchain_cursor__copy(struct callchain_cursor *dst,
  197. struct callchain_cursor *src);
  198. struct option;
  199. struct hist_entry;
  200. int record_parse_callchain_opt(const struct option *opt, const char *arg, int unset);
  201. int record_callchain_opt(const struct option *opt, const char *arg, int unset);
  202. struct record_opts;
  203. int record_opts__parse_callchain(struct record_opts *record,
  204. struct callchain_param *callchain,
  205. const char *arg, bool unset);
  206. int sample__resolve_callchain(struct perf_sample *sample,
  207. struct callchain_cursor *cursor, struct symbol **parent,
  208. struct perf_evsel *evsel, struct addr_location *al,
  209. int max_stack);
  210. int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample);
  211. int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
  212. bool hide_unresolved);
  213. extern const char record_callchain_help[];
  214. int parse_callchain_record(const char *arg, struct callchain_param *param);
  215. int parse_callchain_record_opt(const char *arg, struct callchain_param *param);
  216. int parse_callchain_report_opt(const char *arg);
  217. int parse_callchain_top_opt(const char *arg);
  218. int perf_callchain_config(const char *var, const char *value);
  219. static inline void callchain_cursor_snapshot(struct callchain_cursor *dest,
  220. struct callchain_cursor *src)
  221. {
  222. *dest = *src;
  223. dest->first = src->curr;
  224. dest->nr -= src->pos;
  225. }
  226. #ifdef HAVE_SKIP_CALLCHAIN_IDX
  227. int arch_skip_callchain_idx(struct thread *thread, struct ip_callchain *chain);
  228. #else
  229. static inline int arch_skip_callchain_idx(struct thread *thread __maybe_unused,
  230. struct ip_callchain *chain __maybe_unused)
  231. {
  232. return -1;
  233. }
  234. #endif
  235. char *callchain_list__sym_name(struct callchain_list *cl,
  236. char *bf, size_t bfsize, bool show_dso);
  237. char *callchain_node__scnprintf_value(struct callchain_node *node,
  238. char *bf, size_t bfsize, u64 total);
  239. int callchain_node__fprintf_value(struct callchain_node *node,
  240. FILE *fp, u64 total);
  241. int callchain_list_counts__printf_value(struct callchain_list *clist,
  242. FILE *fp, char *bf, int bfsize);
  243. void free_callchain(struct callchain_root *root);
  244. void decay_callchain(struct callchain_root *root);
  245. int callchain_node__make_parent_list(struct callchain_node *node);
  246. int callchain_branch_counts(struct callchain_root *root,
  247. u64 *branch_count, u64 *predicted_count,
  248. u64 *abort_count, u64 *cycles_count);
  249. #endif /* __PERF_CALLCHAIN_H */