callchain.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 chain_mode {
  9. CHAIN_NONE,
  10. CHAIN_FLAT,
  11. CHAIN_GRAPH_ABS,
  12. CHAIN_GRAPH_REL
  13. };
  14. enum chain_order {
  15. ORDER_CALLER,
  16. ORDER_CALLEE
  17. };
  18. struct callchain_node {
  19. struct callchain_node *parent;
  20. struct list_head val;
  21. struct rb_node rb_node_in; /* to insert nodes in an rbtree */
  22. struct rb_node rb_node; /* to sort nodes in an output tree */
  23. struct rb_root rb_root_in; /* input tree of children */
  24. struct rb_root rb_root; /* sorted output tree of children */
  25. unsigned int val_nr;
  26. u64 hit;
  27. u64 children_hit;
  28. };
  29. struct callchain_root {
  30. u64 max_depth;
  31. struct callchain_node node;
  32. };
  33. struct callchain_param;
  34. typedef void (*sort_chain_func_t)(struct rb_root *, struct callchain_root *,
  35. u64, struct callchain_param *);
  36. enum chain_key {
  37. CCKEY_FUNCTION,
  38. CCKEY_ADDRESS
  39. };
  40. struct callchain_param {
  41. enum chain_mode mode;
  42. u32 print_limit;
  43. double min_percent;
  44. sort_chain_func_t sort;
  45. enum chain_order order;
  46. enum chain_key key;
  47. };
  48. struct callchain_list {
  49. u64 ip;
  50. struct map_symbol ms;
  51. struct list_head list;
  52. };
  53. /*
  54. * A callchain cursor is a single linked list that
  55. * let one feed a callchain progressively.
  56. * It keeps persistent allocated entries to minimize
  57. * allocations.
  58. */
  59. struct callchain_cursor_node {
  60. u64 ip;
  61. struct map *map;
  62. struct symbol *sym;
  63. struct callchain_cursor_node *next;
  64. };
  65. struct callchain_cursor {
  66. u64 nr;
  67. struct callchain_cursor_node *first;
  68. struct callchain_cursor_node **last;
  69. u64 pos;
  70. struct callchain_cursor_node *curr;
  71. };
  72. extern __thread struct callchain_cursor callchain_cursor;
  73. static inline void callchain_init(struct callchain_root *root)
  74. {
  75. INIT_LIST_HEAD(&root->node.val);
  76. root->node.parent = NULL;
  77. root->node.hit = 0;
  78. root->node.children_hit = 0;
  79. root->node.rb_root_in = RB_ROOT;
  80. root->max_depth = 0;
  81. }
  82. static inline u64 callchain_cumul_hits(struct callchain_node *node)
  83. {
  84. return node->hit + node->children_hit;
  85. }
  86. int callchain_register_param(struct callchain_param *param);
  87. int callchain_append(struct callchain_root *root,
  88. struct callchain_cursor *cursor,
  89. u64 period);
  90. int callchain_merge(struct callchain_cursor *cursor,
  91. struct callchain_root *dst, struct callchain_root *src);
  92. /*
  93. * Initialize a cursor before adding entries inside, but keep
  94. * the previously allocated entries as a cache.
  95. */
  96. static inline void callchain_cursor_reset(struct callchain_cursor *cursor)
  97. {
  98. cursor->nr = 0;
  99. cursor->last = &cursor->first;
  100. }
  101. int callchain_cursor_append(struct callchain_cursor *cursor, u64 ip,
  102. struct map *map, struct symbol *sym);
  103. /* Close a cursor writing session. Initialize for the reader */
  104. static inline void callchain_cursor_commit(struct callchain_cursor *cursor)
  105. {
  106. cursor->curr = cursor->first;
  107. cursor->pos = 0;
  108. }
  109. /* Cursor reading iteration helpers */
  110. static inline struct callchain_cursor_node *
  111. callchain_cursor_current(struct callchain_cursor *cursor)
  112. {
  113. if (cursor->pos == cursor->nr)
  114. return NULL;
  115. return cursor->curr;
  116. }
  117. static inline void callchain_cursor_advance(struct callchain_cursor *cursor)
  118. {
  119. cursor->curr = cursor->curr->next;
  120. cursor->pos++;
  121. }
  122. struct option;
  123. int record_parse_callchain(const char *arg, struct perf_record_opts *opts);
  124. int record_parse_callchain_opt(const struct option *opt, const char *arg, int unset);
  125. int record_callchain_opt(const struct option *opt, const char *arg, int unset);
  126. extern const char record_callchain_help[];
  127. #endif /* __PERF_CALLCHAIN_H */