1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __PERF_CALLCHAIN_H 3 #define __PERF_CALLCHAIN_H 4 5 #include <linux/list.h> 6 #include <linux/rbtree.h> 7 #include "map_symbol.h" 8 #include "branch.h" 9 10 struct addr_location; 11 struct evsel; 12 struct hist_entry; 13 struct hists; 14 struct ip_callchain; 15 struct map; 16 struct perf_sample; 17 struct record_opts; 18 struct thread; 19 20 #define HELP_PAD "\t\t\t\t" 21 22 #define CALLCHAIN_HELP "setup and enables call-graph (stack chain/backtrace):\n\n" 23 24 # define RECORD_MODE_HELP HELP_PAD "record_mode:\tcall graph recording mode (fp|dwarf|lbr)\n" 25 26 #define RECORD_SIZE_HELP \ 27 HELP_PAD "record_size:\tif record_mode is 'dwarf', max size of stack recording (<bytes>)\n" \ 28 HELP_PAD "\t\tdefault: 8192 (bytes)\n" 29 30 #define CALLCHAIN_RECORD_HELP CALLCHAIN_HELP RECORD_MODE_HELP RECORD_SIZE_HELP 31 32 #define CALLCHAIN_REPORT_HELP \ 33 HELP_PAD "print_type:\tcall graph printing style (graph|flat|fractal|folded|none)\n" \ 34 HELP_PAD "threshold:\tminimum call graph inclusion threshold (<percent>)\n" \ 35 HELP_PAD "print_limit:\tmaximum number of call graph entry (<number>)\n" \ 36 HELP_PAD "order:\t\tcall graph order (caller|callee)\n" \ 37 HELP_PAD "sort_key:\tcall graph sort key (function|address)\n" \ 38 HELP_PAD "branch:\t\tinclude last branch info to call graph (branch)\n" \ 39 HELP_PAD "value:\t\tcall graph value (percent|period|count)\n" 40 41 enum perf_call_graph_mode { 42 CALLCHAIN_NONE, 43 CALLCHAIN_FP, 44 CALLCHAIN_DWARF, 45 CALLCHAIN_LBR, 46 CALLCHAIN_MAX 47 }; 48 49 enum chain_mode { 50 CHAIN_NONE, 51 CHAIN_FLAT, 52 CHAIN_GRAPH_ABS, 53 CHAIN_GRAPH_REL, 54 CHAIN_FOLDED, 55 }; 56 57 enum chain_order { 58 ORDER_CALLER, 59 ORDER_CALLEE 60 }; 61 62 struct callchain_node { 63 struct callchain_node *parent; 64 struct list_head val; 65 struct list_head parent_val; 66 struct rb_node rb_node_in; /* to insert nodes in an rbtree */ 67 struct rb_node rb_node; /* to sort nodes in an output tree */ 68 struct rb_root rb_root_in; /* input tree of children */ 69 struct rb_root rb_root; /* sorted output tree of children */ 70 unsigned int val_nr; 71 unsigned int count; 72 unsigned int children_count; 73 u64 hit; 74 u64 children_hit; 75 }; 76 77 struct callchain_root { 78 u64 max_depth; 79 struct callchain_node node; 80 }; 81 82 struct callchain_param; 83 84 typedef void (*sort_chain_func_t)(struct rb_root *, struct callchain_root *, 85 u64, struct callchain_param *); 86 87 enum chain_key { 88 CCKEY_FUNCTION, 89 CCKEY_ADDRESS, 90 CCKEY_SRCLINE 91 }; 92 93 enum chain_value { 94 CCVAL_PERCENT, 95 CCVAL_PERIOD, 96 CCVAL_COUNT, 97 }; 98 99 extern bool dwarf_callchain_users; 100 101 struct callchain_param { 102 bool enabled; 103 bool defer; 104 enum perf_call_graph_mode record_mode; 105 u32 dump_size; 106 enum chain_mode mode; 107 u16 max_stack; 108 u32 print_limit; 109 double min_percent; 110 sort_chain_func_t sort; 111 enum chain_order order; 112 bool order_set; 113 enum chain_key key; 114 bool branch_callstack; 115 enum chain_value value; 116 }; 117 118 extern struct callchain_param callchain_param; 119 extern struct callchain_param callchain_param_default; 120 121 struct callchain_list { 122 struct list_head list; 123 u64 ip; 124 struct map_symbol ms; 125 const char *srcline; 126 u64 branch_count; 127 u64 from_count; 128 u64 cycles_count; 129 u64 iter_count; 130 u64 iter_cycles; 131 struct branch_type_stat *brtype_stat; 132 u64 predicted_count; 133 u64 abort_count; 134 struct /* for TUI */ { 135 bool unfolded; 136 bool has_children; 137 }; 138 }; 139 140 /* 141 * A callchain cursor is a single linked list that 142 * let one feed a callchain progressively. 143 * It keeps persistent allocated entries to minimize 144 * allocations. 145 */ 146 struct callchain_cursor_node { 147 u64 ip; 148 struct map_symbol ms; 149 const char *srcline; 150 /* Indicate valid cursor node for LBR stitch */ 151 bool valid; 152 153 bool branch; 154 struct branch_flags branch_flags; 155 u64 branch_from; 156 int nr_loop_iter; 157 u64 iter_cycles; 158 struct callchain_cursor_node *next; 159 }; 160 161 struct stitch_list { 162 struct list_head node; 163 struct callchain_cursor_node cursor; 164 }; 165 166 struct callchain_cursor { 167 u64 nr; 168 struct callchain_cursor_node *first; 169 struct callchain_cursor_node **last; 170 u64 pos; 171 struct callchain_cursor_node *curr; 172 }; 173 174 static inline void callchain_init(struct callchain_root *root) 175 { 176 INIT_LIST_HEAD(&root->node.val); 177 INIT_LIST_HEAD(&root->node.parent_val); 178 179 root->node.parent = NULL; 180 root->node.hit = 0; 181 root->node.children_hit = 0; 182 root->node.rb_root_in = RB_ROOT; 183 root->max_depth = 0; 184 } 185 186 static inline u64 callchain_cumul_hits(struct callchain_node *node) 187 { 188 return node->hit + node->children_hit; 189 } 190 191 static inline unsigned callchain_cumul_counts(struct callchain_node *node) 192 { 193 return node->count + node->children_count; 194 } 195 196 int callchain_register_param(struct callchain_param *param); 197 int callchain_append(struct callchain_root *root, 198 struct callchain_cursor *cursor, 199 u64 period); 200 201 int callchain_merge(struct callchain_cursor *cursor, 202 struct callchain_root *dst, struct callchain_root *src); 203 204 void callchain_cursor_reset(struct callchain_cursor *cursor); 205 206 int callchain_cursor_append(struct callchain_cursor *cursor, u64 ip, 207 struct map_symbol *ms, 208 bool branch, struct branch_flags *flags, 209 int nr_loop_iter, u64 iter_cycles, u64 branch_from, 210 const char *srcline); 211 212 /* Close a cursor writing session. Initialize for the reader */ 213 static inline void callchain_cursor_commit(struct callchain_cursor *cursor) 214 { 215 if (cursor == NULL) 216 return; 217 cursor->curr = cursor->first; 218 cursor->pos = 0; 219 } 220 221 /* Cursor reading iteration helpers */ 222 static inline struct callchain_cursor_node * 223 callchain_cursor_current(struct callchain_cursor *cursor) 224 { 225 if (cursor == NULL || cursor->pos == cursor->nr) 226 return NULL; 227 228 return cursor->curr; 229 } 230 231 static inline void callchain_cursor_advance(struct callchain_cursor *cursor) 232 { 233 cursor->curr = cursor->curr->next; 234 cursor->pos++; 235 } 236 237 struct callchain_cursor *get_tls_callchain_cursor(void); 238 239 int callchain_cursor__copy(struct callchain_cursor *dst, 240 struct callchain_cursor *src); 241 242 int record_opts__parse_callchain(struct record_opts *record, 243 struct callchain_param *callchain, 244 const char *arg, bool unset); 245 246 int sample__resolve_callchain(struct perf_sample *sample, 247 struct callchain_cursor *cursor, struct symbol **parent, 248 struct evsel *evsel, struct addr_location *al, 249 int max_stack); 250 int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample); 251 int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node, 252 bool hide_unresolved); 253 254 extern const char record_callchain_help[]; 255 int parse_callchain_record(const char *arg, struct callchain_param *param); 256 int parse_callchain_record_opt(const char *arg, struct callchain_param *param); 257 int parse_callchain_report_opt(const char *arg); 258 int parse_callchain_top_opt(const char *arg); 259 int perf_callchain_config(const char *var, const char *value); 260 261 static inline void callchain_cursor_snapshot(struct callchain_cursor *dest, 262 struct callchain_cursor *src) 263 { 264 *dest = *src; 265 266 dest->first = src->curr; 267 dest->nr -= src->pos; 268 } 269 270 #ifdef HAVE_SKIP_CALLCHAIN_IDX 271 int arch_skip_callchain_idx(struct thread *thread, struct ip_callchain *chain); 272 #else 273 static inline int arch_skip_callchain_idx(struct thread *thread __maybe_unused, 274 struct ip_callchain *chain __maybe_unused) 275 { 276 return -1; 277 } 278 #endif 279 280 void arch__add_leaf_frame_record_opts(struct record_opts *opts); 281 282 char *callchain_list__sym_name(struct callchain_list *cl, 283 char *bf, size_t bfsize, bool show_dso); 284 char *callchain_node__scnprintf_value(struct callchain_node *node, 285 char *bf, size_t bfsize, u64 total); 286 int callchain_node__fprintf_value(struct callchain_node *node, 287 FILE *fp, u64 total); 288 289 int callchain_list_counts__printf_value(struct callchain_list *clist, 290 FILE *fp, char *bf, int bfsize); 291 292 void free_callchain(struct callchain_root *root); 293 void decay_callchain(struct callchain_root *root); 294 int callchain_node__make_parent_list(struct callchain_node *node); 295 296 int callchain_branch_counts(struct callchain_root *root, 297 u64 *branch_count, u64 *predicted_count, 298 u64 *abort_count, u64 *cycles_count); 299 300 void callchain_param_setup(u64 sample_type, uint16_t e_machine); 301 302 bool callchain_cnode_matched(struct callchain_node *base_cnode, 303 struct callchain_node *pair_cnode); 304 305 u64 callchain_total_hits(struct hists *hists); 306 307 s64 callchain_avg_cycles(struct callchain_node *cnode); 308 309 typedef int (*callchain_iter_fn)(struct callchain_cursor_node *node, void *data); 310 311 int sample__for_each_callchain_node(struct thread *thread, struct evsel *evsel, 312 struct perf_sample *sample, int max_stack, 313 bool symbols, callchain_iter_fn cb, void *data); 314 315 int sample__merge_deferred_callchain(struct perf_sample *sample_orig, 316 struct perf_sample *sample_callchain); 317 318 #endif /* __PERF_CALLCHAIN_H */ 319