callchain.h 7.7 KB

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