callchain.h 7.2 KB

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