callchain.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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|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. 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. };
  39. enum chain_order {
  40. ORDER_CALLER,
  41. ORDER_CALLEE
  42. };
  43. struct callchain_node {
  44. struct callchain_node *parent;
  45. struct list_head 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. u64 hit;
  52. u64 children_hit;
  53. };
  54. struct callchain_root {
  55. u64 max_depth;
  56. struct callchain_node node;
  57. };
  58. struct callchain_param;
  59. typedef void (*sort_chain_func_t)(struct rb_root *, struct callchain_root *,
  60. u64, struct callchain_param *);
  61. enum chain_key {
  62. CCKEY_FUNCTION,
  63. CCKEY_ADDRESS
  64. };
  65. struct callchain_param {
  66. bool enabled;
  67. enum perf_call_graph_mode record_mode;
  68. u32 dump_size;
  69. enum chain_mode mode;
  70. u32 print_limit;
  71. double min_percent;
  72. sort_chain_func_t sort;
  73. enum chain_order order;
  74. bool order_set;
  75. enum chain_key key;
  76. bool branch_callstack;
  77. };
  78. extern struct callchain_param callchain_param;
  79. struct callchain_list {
  80. u64 ip;
  81. struct map_symbol ms;
  82. struct /* for TUI */ {
  83. bool unfolded;
  84. bool has_children;
  85. };
  86. char *srcline;
  87. struct list_head list;
  88. };
  89. /*
  90. * A callchain cursor is a single linked list that
  91. * let one feed a callchain progressively.
  92. * It keeps persistent allocated entries to minimize
  93. * allocations.
  94. */
  95. struct callchain_cursor_node {
  96. u64 ip;
  97. struct map *map;
  98. struct symbol *sym;
  99. struct callchain_cursor_node *next;
  100. };
  101. struct callchain_cursor {
  102. u64 nr;
  103. struct callchain_cursor_node *first;
  104. struct callchain_cursor_node **last;
  105. u64 pos;
  106. struct callchain_cursor_node *curr;
  107. };
  108. extern __thread struct callchain_cursor callchain_cursor;
  109. static inline void callchain_init(struct callchain_root *root)
  110. {
  111. INIT_LIST_HEAD(&root->node.val);
  112. root->node.parent = NULL;
  113. root->node.hit = 0;
  114. root->node.children_hit = 0;
  115. root->node.rb_root_in = RB_ROOT;
  116. root->max_depth = 0;
  117. }
  118. static inline u64 callchain_cumul_hits(struct callchain_node *node)
  119. {
  120. return node->hit + node->children_hit;
  121. }
  122. int callchain_register_param(struct callchain_param *param);
  123. int callchain_append(struct callchain_root *root,
  124. struct callchain_cursor *cursor,
  125. u64 period);
  126. int callchain_merge(struct callchain_cursor *cursor,
  127. struct callchain_root *dst, struct callchain_root *src);
  128. /*
  129. * Initialize a cursor before adding entries inside, but keep
  130. * the previously allocated entries as a cache.
  131. */
  132. static inline void callchain_cursor_reset(struct callchain_cursor *cursor)
  133. {
  134. cursor->nr = 0;
  135. cursor->last = &cursor->first;
  136. }
  137. int callchain_cursor_append(struct callchain_cursor *cursor, u64 ip,
  138. struct map *map, struct symbol *sym);
  139. /* Close a cursor writing session. Initialize for the reader */
  140. static inline void callchain_cursor_commit(struct callchain_cursor *cursor)
  141. {
  142. cursor->curr = cursor->first;
  143. cursor->pos = 0;
  144. }
  145. /* Cursor reading iteration helpers */
  146. static inline struct callchain_cursor_node *
  147. callchain_cursor_current(struct callchain_cursor *cursor)
  148. {
  149. if (cursor->pos == cursor->nr)
  150. return NULL;
  151. return cursor->curr;
  152. }
  153. static inline void callchain_cursor_advance(struct callchain_cursor *cursor)
  154. {
  155. cursor->curr = cursor->curr->next;
  156. cursor->pos++;
  157. }
  158. struct option;
  159. struct hist_entry;
  160. int record_parse_callchain_opt(const struct option *opt, const char *arg, int unset);
  161. int record_callchain_opt(const struct option *opt, const char *arg, int unset);
  162. int sample__resolve_callchain(struct perf_sample *sample, struct symbol **parent,
  163. struct perf_evsel *evsel, struct addr_location *al,
  164. int max_stack);
  165. int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample);
  166. int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
  167. bool hide_unresolved);
  168. extern const char record_callchain_help[];
  169. extern int parse_callchain_record(const char *arg, struct callchain_param *param);
  170. int parse_callchain_record_opt(const char *arg, struct callchain_param *param);
  171. int parse_callchain_report_opt(const char *arg);
  172. int parse_callchain_top_opt(const char *arg);
  173. int perf_callchain_config(const char *var, const char *value);
  174. static inline void callchain_cursor_snapshot(struct callchain_cursor *dest,
  175. struct callchain_cursor *src)
  176. {
  177. *dest = *src;
  178. dest->first = src->curr;
  179. dest->nr -= src->pos;
  180. }
  181. #ifdef HAVE_SKIP_CALLCHAIN_IDX
  182. extern int arch_skip_callchain_idx(struct thread *thread, struct ip_callchain *chain);
  183. #else
  184. static inline int arch_skip_callchain_idx(struct thread *thread __maybe_unused,
  185. struct ip_callchain *chain __maybe_unused)
  186. {
  187. return -1;
  188. }
  189. #endif
  190. char *callchain_list__sym_name(struct callchain_list *cl,
  191. char *bf, size_t bfsize, bool show_dso);
  192. void free_callchain(struct callchain_root *root);
  193. #endif /* __PERF_CALLCHAIN_H */