callchain.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. enum perf_call_graph_mode {
  9. CALLCHAIN_NONE,
  10. CALLCHAIN_FP,
  11. CALLCHAIN_DWARF,
  12. CALLCHAIN_LBR,
  13. CALLCHAIN_MAX
  14. };
  15. enum chain_mode {
  16. CHAIN_NONE,
  17. CHAIN_FLAT,
  18. CHAIN_GRAPH_ABS,
  19. CHAIN_GRAPH_REL
  20. };
  21. enum chain_order {
  22. ORDER_CALLER,
  23. ORDER_CALLEE
  24. };
  25. struct callchain_node {
  26. struct callchain_node *parent;
  27. struct list_head val;
  28. struct rb_node rb_node_in; /* to insert nodes in an rbtree */
  29. struct rb_node rb_node; /* to sort nodes in an output tree */
  30. struct rb_root rb_root_in; /* input tree of children */
  31. struct rb_root rb_root; /* sorted output tree of children */
  32. unsigned int val_nr;
  33. u64 hit;
  34. u64 children_hit;
  35. };
  36. struct callchain_root {
  37. u64 max_depth;
  38. struct callchain_node node;
  39. };
  40. struct callchain_param;
  41. typedef void (*sort_chain_func_t)(struct rb_root *, struct callchain_root *,
  42. u64, struct callchain_param *);
  43. enum chain_key {
  44. CCKEY_FUNCTION,
  45. CCKEY_ADDRESS
  46. };
  47. struct callchain_param {
  48. bool enabled;
  49. enum perf_call_graph_mode record_mode;
  50. u32 dump_size;
  51. enum chain_mode mode;
  52. u32 print_limit;
  53. double min_percent;
  54. sort_chain_func_t sort;
  55. enum chain_order order;
  56. enum chain_key key;
  57. bool branch_callstack;
  58. };
  59. extern struct callchain_param callchain_param;
  60. struct callchain_list {
  61. u64 ip;
  62. struct map_symbol ms;
  63. struct /* for TUI */ {
  64. bool unfolded;
  65. bool has_children;
  66. };
  67. char *srcline;
  68. struct list_head list;
  69. };
  70. /*
  71. * A callchain cursor is a single linked list that
  72. * let one feed a callchain progressively.
  73. * It keeps persistent allocated entries to minimize
  74. * allocations.
  75. */
  76. struct callchain_cursor_node {
  77. u64 ip;
  78. struct map *map;
  79. struct symbol *sym;
  80. struct callchain_cursor_node *next;
  81. };
  82. struct callchain_cursor {
  83. u64 nr;
  84. struct callchain_cursor_node *first;
  85. struct callchain_cursor_node **last;
  86. u64 pos;
  87. struct callchain_cursor_node *curr;
  88. };
  89. extern __thread struct callchain_cursor callchain_cursor;
  90. static inline void callchain_init(struct callchain_root *root)
  91. {
  92. INIT_LIST_HEAD(&root->node.val);
  93. root->node.parent = NULL;
  94. root->node.hit = 0;
  95. root->node.children_hit = 0;
  96. root->node.rb_root_in = RB_ROOT;
  97. root->max_depth = 0;
  98. }
  99. static inline u64 callchain_cumul_hits(struct callchain_node *node)
  100. {
  101. return node->hit + node->children_hit;
  102. }
  103. int callchain_register_param(struct callchain_param *param);
  104. int callchain_append(struct callchain_root *root,
  105. struct callchain_cursor *cursor,
  106. u64 period);
  107. int callchain_merge(struct callchain_cursor *cursor,
  108. struct callchain_root *dst, struct callchain_root *src);
  109. /*
  110. * Initialize a cursor before adding entries inside, but keep
  111. * the previously allocated entries as a cache.
  112. */
  113. static inline void callchain_cursor_reset(struct callchain_cursor *cursor)
  114. {
  115. cursor->nr = 0;
  116. cursor->last = &cursor->first;
  117. }
  118. int callchain_cursor_append(struct callchain_cursor *cursor, u64 ip,
  119. struct map *map, struct symbol *sym);
  120. /* Close a cursor writing session. Initialize for the reader */
  121. static inline void callchain_cursor_commit(struct callchain_cursor *cursor)
  122. {
  123. cursor->curr = cursor->first;
  124. cursor->pos = 0;
  125. }
  126. /* Cursor reading iteration helpers */
  127. static inline struct callchain_cursor_node *
  128. callchain_cursor_current(struct callchain_cursor *cursor)
  129. {
  130. if (cursor->pos == cursor->nr)
  131. return NULL;
  132. return cursor->curr;
  133. }
  134. static inline void callchain_cursor_advance(struct callchain_cursor *cursor)
  135. {
  136. cursor->curr = cursor->curr->next;
  137. cursor->pos++;
  138. }
  139. struct option;
  140. struct hist_entry;
  141. int record_parse_callchain_opt(const struct option *opt, const char *arg, int unset);
  142. int record_callchain_opt(const struct option *opt, const char *arg, int unset);
  143. int sample__resolve_callchain(struct perf_sample *sample, struct symbol **parent,
  144. struct perf_evsel *evsel, struct addr_location *al,
  145. int max_stack);
  146. int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample);
  147. int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
  148. bool hide_unresolved);
  149. extern const char record_callchain_help[];
  150. extern int parse_callchain_record(const char *arg, struct callchain_param *param);
  151. int parse_callchain_record_opt(const char *arg, struct callchain_param *param);
  152. int parse_callchain_report_opt(const char *arg);
  153. int perf_callchain_config(const char *var, const char *value);
  154. static inline void callchain_cursor_snapshot(struct callchain_cursor *dest,
  155. struct callchain_cursor *src)
  156. {
  157. *dest = *src;
  158. dest->first = src->curr;
  159. dest->nr -= src->pos;
  160. }
  161. #ifdef HAVE_SKIP_CALLCHAIN_IDX
  162. extern int arch_skip_callchain_idx(struct thread *thread, struct ip_callchain *chain);
  163. #else
  164. static inline int arch_skip_callchain_idx(struct thread *thread __maybe_unused,
  165. struct ip_callchain *chain __maybe_unused)
  166. {
  167. return -1;
  168. }
  169. #endif
  170. char *callchain_list__sym_name(struct callchain_list *cl,
  171. char *bf, size_t bfsize, bool show_dso);
  172. void free_callchain(struct callchain_root *root);
  173. #endif /* __PERF_CALLCHAIN_H */