callchain.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. enum chain_mode mode;
  48. u32 print_limit;
  49. double min_percent;
  50. sort_chain_func_t sort;
  51. enum chain_order order;
  52. enum chain_key key;
  53. };
  54. struct callchain_list {
  55. u64 ip;
  56. struct map_symbol ms;
  57. struct list_head list;
  58. };
  59. /*
  60. * A callchain cursor is a single linked list that
  61. * let one feed a callchain progressively.
  62. * It keeps persistent allocated entries to minimize
  63. * allocations.
  64. */
  65. struct callchain_cursor_node {
  66. u64 ip;
  67. struct map *map;
  68. struct symbol *sym;
  69. struct callchain_cursor_node *next;
  70. };
  71. struct callchain_cursor {
  72. u64 nr;
  73. struct callchain_cursor_node *first;
  74. struct callchain_cursor_node **last;
  75. u64 pos;
  76. struct callchain_cursor_node *curr;
  77. };
  78. extern __thread struct callchain_cursor callchain_cursor;
  79. static inline void callchain_init(struct callchain_root *root)
  80. {
  81. INIT_LIST_HEAD(&root->node.val);
  82. root->node.parent = NULL;
  83. root->node.hit = 0;
  84. root->node.children_hit = 0;
  85. root->node.rb_root_in = RB_ROOT;
  86. root->max_depth = 0;
  87. }
  88. static inline u64 callchain_cumul_hits(struct callchain_node *node)
  89. {
  90. return node->hit + node->children_hit;
  91. }
  92. int callchain_register_param(struct callchain_param *param);
  93. int callchain_append(struct callchain_root *root,
  94. struct callchain_cursor *cursor,
  95. u64 period);
  96. int callchain_merge(struct callchain_cursor *cursor,
  97. struct callchain_root *dst, struct callchain_root *src);
  98. /*
  99. * Initialize a cursor before adding entries inside, but keep
  100. * the previously allocated entries as a cache.
  101. */
  102. static inline void callchain_cursor_reset(struct callchain_cursor *cursor)
  103. {
  104. cursor->nr = 0;
  105. cursor->last = &cursor->first;
  106. }
  107. int callchain_cursor_append(struct callchain_cursor *cursor, u64 ip,
  108. struct map *map, struct symbol *sym);
  109. /* Close a cursor writing session. Initialize for the reader */
  110. static inline void callchain_cursor_commit(struct callchain_cursor *cursor)
  111. {
  112. cursor->curr = cursor->first;
  113. cursor->pos = 0;
  114. }
  115. /* Cursor reading iteration helpers */
  116. static inline struct callchain_cursor_node *
  117. callchain_cursor_current(struct callchain_cursor *cursor)
  118. {
  119. if (cursor->pos == cursor->nr)
  120. return NULL;
  121. return cursor->curr;
  122. }
  123. static inline void callchain_cursor_advance(struct callchain_cursor *cursor)
  124. {
  125. cursor->curr = cursor->curr->next;
  126. cursor->pos++;
  127. }
  128. struct option;
  129. struct hist_entry;
  130. int record_parse_callchain(const char *arg, struct record_opts *opts);
  131. int record_parse_callchain_opt(const struct option *opt, const char *arg, int unset);
  132. int record_callchain_opt(const struct option *opt, const char *arg, int unset);
  133. int sample__resolve_callchain(struct perf_sample *sample, struct symbol **parent,
  134. struct perf_evsel *evsel, struct addr_location *al,
  135. int max_stack);
  136. int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample);
  137. int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
  138. bool hide_unresolved);
  139. extern const char record_callchain_help[];
  140. int parse_callchain_report_opt(const char *arg);
  141. static inline void callchain_cursor_snapshot(struct callchain_cursor *dest,
  142. struct callchain_cursor *src)
  143. {
  144. *dest = *src;
  145. dest->first = src->curr;
  146. dest->nr -= src->pos;
  147. }
  148. #endif /* __PERF_CALLCHAIN_H */