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