callchain.h 4.8 KB

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