callchain.h 7.8 KB

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