callchain.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. char *srcline;
  64. struct list_head list;
  65. };
  66. /*
  67. * A callchain cursor is a single linked list that
  68. * let one feed a callchain progressively.
  69. * It keeps persistent allocated entries to minimize
  70. * allocations.
  71. */
  72. struct callchain_cursor_node {
  73. u64 ip;
  74. struct map *map;
  75. struct symbol *sym;
  76. struct callchain_cursor_node *next;
  77. };
  78. struct callchain_cursor {
  79. u64 nr;
  80. struct callchain_cursor_node *first;
  81. struct callchain_cursor_node **last;
  82. u64 pos;
  83. struct callchain_cursor_node *curr;
  84. };
  85. extern __thread struct callchain_cursor callchain_cursor;
  86. static inline void callchain_init(struct callchain_root *root)
  87. {
  88. INIT_LIST_HEAD(&root->node.val);
  89. root->node.parent = NULL;
  90. root->node.hit = 0;
  91. root->node.children_hit = 0;
  92. root->node.rb_root_in = RB_ROOT;
  93. root->max_depth = 0;
  94. }
  95. static inline u64 callchain_cumul_hits(struct callchain_node *node)
  96. {
  97. return node->hit + node->children_hit;
  98. }
  99. int callchain_register_param(struct callchain_param *param);
  100. int callchain_append(struct callchain_root *root,
  101. struct callchain_cursor *cursor,
  102. u64 period);
  103. int callchain_merge(struct callchain_cursor *cursor,
  104. struct callchain_root *dst, struct callchain_root *src);
  105. /*
  106. * Initialize a cursor before adding entries inside, but keep
  107. * the previously allocated entries as a cache.
  108. */
  109. static inline void callchain_cursor_reset(struct callchain_cursor *cursor)
  110. {
  111. cursor->nr = 0;
  112. cursor->last = &cursor->first;
  113. }
  114. int callchain_cursor_append(struct callchain_cursor *cursor, u64 ip,
  115. struct map *map, struct symbol *sym);
  116. /* Close a cursor writing session. Initialize for the reader */
  117. static inline void callchain_cursor_commit(struct callchain_cursor *cursor)
  118. {
  119. cursor->curr = cursor->first;
  120. cursor->pos = 0;
  121. }
  122. /* Cursor reading iteration helpers */
  123. static inline struct callchain_cursor_node *
  124. callchain_cursor_current(struct callchain_cursor *cursor)
  125. {
  126. if (cursor->pos == cursor->nr)
  127. return NULL;
  128. return cursor->curr;
  129. }
  130. static inline void callchain_cursor_advance(struct callchain_cursor *cursor)
  131. {
  132. cursor->curr = cursor->curr->next;
  133. cursor->pos++;
  134. }
  135. struct option;
  136. struct hist_entry;
  137. int record_parse_callchain_opt(const struct option *opt, const char *arg, int unset);
  138. int record_callchain_opt(const struct option *opt, const char *arg, int unset);
  139. int sample__resolve_callchain(struct perf_sample *sample, struct symbol **parent,
  140. struct perf_evsel *evsel, struct addr_location *al,
  141. int max_stack);
  142. int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample);
  143. int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
  144. bool hide_unresolved);
  145. extern const char record_callchain_help[];
  146. int parse_callchain_record_opt(const char *arg);
  147. int parse_callchain_report_opt(const char *arg);
  148. int perf_callchain_config(const char *var, const char *value);
  149. static inline void callchain_cursor_snapshot(struct callchain_cursor *dest,
  150. struct callchain_cursor *src)
  151. {
  152. *dest = *src;
  153. dest->first = src->curr;
  154. dest->nr -= src->pos;
  155. }
  156. #ifdef HAVE_SKIP_CALLCHAIN_IDX
  157. extern int arch_skip_callchain_idx(struct thread *thread, struct ip_callchain *chain);
  158. #else
  159. static inline int arch_skip_callchain_idx(struct thread *thread __maybe_unused,
  160. struct ip_callchain *chain __maybe_unused)
  161. {
  162. return -1;
  163. }
  164. #endif
  165. char *callchain_list__sym_name(struct callchain_list *cl,
  166. char *bf, size_t bfsize, bool show_dso);
  167. void free_callchain(struct callchain_root *root);
  168. #endif /* __PERF_CALLCHAIN_H */