callchain.h 3.2 KB

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