callchain.h 6.9 KB

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