callchain.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. struct callchain_node {
  15. struct callchain_node *parent;
  16. struct list_head siblings;
  17. struct list_head children;
  18. struct list_head val;
  19. struct rb_node rb_node; /* to sort nodes in an rbtree */
  20. struct rb_root rb_root; /* sorted tree of children */
  21. unsigned int val_nr;
  22. u64 hit;
  23. u64 children_hit;
  24. };
  25. struct callchain_root {
  26. u64 max_depth;
  27. struct callchain_node node;
  28. };
  29. struct callchain_param;
  30. typedef void (*sort_chain_func_t)(struct rb_root *, struct callchain_root *,
  31. u64, struct callchain_param *);
  32. struct callchain_param {
  33. enum chain_mode mode;
  34. u32 print_limit;
  35. double min_percent;
  36. sort_chain_func_t sort;
  37. };
  38. struct callchain_list {
  39. u64 ip;
  40. struct map_symbol ms;
  41. struct list_head list;
  42. };
  43. /*
  44. * A callchain cursor is a single linked list that
  45. * let one feed a callchain progressively.
  46. * It keeps persitent allocated entries to minimize
  47. * allocations.
  48. */
  49. struct callchain_cursor_node {
  50. u64 ip;
  51. struct map *map;
  52. struct symbol *sym;
  53. struct callchain_cursor_node *next;
  54. };
  55. struct callchain_cursor {
  56. u64 nr;
  57. struct callchain_cursor_node *first;
  58. struct callchain_cursor_node **last;
  59. u64 pos;
  60. struct callchain_cursor_node *curr;
  61. };
  62. static inline void callchain_init(struct callchain_root *root)
  63. {
  64. INIT_LIST_HEAD(&root->node.siblings);
  65. INIT_LIST_HEAD(&root->node.children);
  66. INIT_LIST_HEAD(&root->node.val);
  67. root->node.parent = NULL;
  68. root->node.hit = 0;
  69. root->node.children_hit = 0;
  70. root->max_depth = 0;
  71. }
  72. static inline u64 callchain_cumul_hits(struct callchain_node *node)
  73. {
  74. return node->hit + node->children_hit;
  75. }
  76. int callchain_register_param(struct callchain_param *param);
  77. int callchain_append(struct callchain_root *root,
  78. struct callchain_cursor *cursor,
  79. u64 period);
  80. int callchain_merge(struct callchain_cursor *cursor,
  81. struct callchain_root *dst, struct callchain_root *src);
  82. bool ip_callchain__valid(struct ip_callchain *chain,
  83. const union perf_event *event);
  84. /*
  85. * Initialize a cursor before adding entries inside, but keep
  86. * the previously allocated entries as a cache.
  87. */
  88. static inline void callchain_cursor_reset(struct callchain_cursor *cursor)
  89. {
  90. cursor->nr = 0;
  91. cursor->last = &cursor->first;
  92. }
  93. int callchain_cursor_append(struct callchain_cursor *cursor, u64 ip,
  94. struct map *map, struct symbol *sym);
  95. /* Close a cursor writing session. Initialize for the reader */
  96. static inline void callchain_cursor_commit(struct callchain_cursor *cursor)
  97. {
  98. cursor->curr = cursor->first;
  99. cursor->pos = 0;
  100. }
  101. /* Cursor reading iteration helpers */
  102. static inline struct callchain_cursor_node *
  103. callchain_cursor_current(struct callchain_cursor *cursor)
  104. {
  105. if (cursor->pos == cursor->nr)
  106. return NULL;
  107. return cursor->curr;
  108. }
  109. static inline void callchain_cursor_advance(struct callchain_cursor *cursor)
  110. {
  111. cursor->curr = cursor->curr->next;
  112. cursor->pos++;
  113. }
  114. #endif /* __PERF_CALLCHAIN_H */