1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __PERF_ANNOTATE_H 3 #define __PERF_ANNOTATE_H 4 5 #include <stdbool.h> 6 #include <stdint.h> 7 #include <stdio.h> 8 #include <linux/types.h> 9 #include <linux/list.h> 10 #include <linux/rbtree.h> 11 #include <asm/bug.h> 12 #include "symbol_conf.h" 13 #include "mutex.h" 14 #include "spark.h" 15 16 struct hist_browser_timer; 17 struct hist_entry; 18 struct ins_ops; 19 struct map; 20 struct map_symbol; 21 struct addr_map_symbol; 22 struct option; 23 struct perf_sample; 24 struct evsel; 25 struct symbol; 26 27 struct ins { 28 const char *name; 29 struct ins_ops *ops; 30 }; 31 32 struct ins_operands { 33 char *raw; 34 struct { 35 char *raw; 36 char *name; 37 struct symbol *sym; 38 u64 addr; 39 s64 offset; 40 bool offset_avail; 41 bool outside; 42 bool multi_regs; 43 } target; 44 union { 45 struct { 46 char *raw; 47 char *name; 48 u64 addr; 49 bool multi_regs; 50 } source; 51 struct { 52 struct ins ins; 53 struct ins_operands *ops; 54 } locked; 55 struct { 56 char *raw_comment; 57 char *raw_func_start; 58 } jump; 59 }; 60 }; 61 62 struct arch; 63 64 struct ins_ops { 65 void (*free)(struct ins_operands *ops); 66 int (*parse)(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms); 67 int (*scnprintf)(struct ins *ins, char *bf, size_t size, 68 struct ins_operands *ops, int max_ins_name); 69 }; 70 71 bool ins__is_jump(const struct ins *ins); 72 bool ins__is_call(const struct ins *ins); 73 bool ins__is_ret(const struct ins *ins); 74 bool ins__is_lock(const struct ins *ins); 75 int ins__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops, int max_ins_name); 76 bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2); 77 78 #define ANNOTATION__IPC_WIDTH 6 79 #define ANNOTATION__CYCLES_WIDTH 6 80 #define ANNOTATION__MINMAX_CYCLES_WIDTH 19 81 #define ANNOTATION__AVG_IPC_WIDTH 36 82 #define ANNOTATION_DUMMY_LEN 256 83 84 struct annotation_options { 85 bool hide_src_code, 86 use_offset, 87 jump_arrows, 88 print_lines, 89 full_path, 90 show_linenr, 91 show_fileloc, 92 show_nr_jumps, 93 show_minmax_cycle, 94 show_asm_raw, 95 annotate_src, 96 full_addr; 97 u8 offset_level; 98 int min_pcnt; 99 int max_lines; 100 int context; 101 char *objdump_path; 102 char *disassembler_style; 103 const char *prefix; 104 const char *prefix_strip; 105 unsigned int percent_type; 106 }; 107 108 enum { 109 ANNOTATION__OFFSET_JUMP_TARGETS = 1, 110 ANNOTATION__OFFSET_CALL, 111 ANNOTATION__MAX_OFFSET_LEVEL, 112 }; 113 114 #define ANNOTATION__MIN_OFFSET_LEVEL ANNOTATION__OFFSET_JUMP_TARGETS 115 116 struct annotation; 117 118 struct sym_hist_entry { 119 u64 nr_samples; 120 u64 period; 121 }; 122 123 enum { 124 PERCENT_HITS_LOCAL, 125 PERCENT_HITS_GLOBAL, 126 PERCENT_PERIOD_LOCAL, 127 PERCENT_PERIOD_GLOBAL, 128 PERCENT_MAX, 129 }; 130 131 struct annotation_data { 132 double percent[PERCENT_MAX]; 133 double percent_sum; 134 struct sym_hist_entry he; 135 }; 136 137 struct cycles_info { 138 float ipc; 139 u64 avg; 140 u64 max; 141 u64 min; 142 }; 143 144 struct annotation_line { 145 struct list_head node; 146 struct rb_node rb_node; 147 s64 offset; 148 char *line; 149 int line_nr; 150 char *fileloc; 151 char *path; 152 struct cycles_info *cycles; 153 int jump_sources; 154 u32 idx; 155 int idx_asm; 156 int data_nr; 157 struct annotation_data data[]; 158 }; 159 160 struct disasm_line { 161 struct ins ins; 162 struct ins_operands ops; 163 164 /* This needs to be at the end. */ 165 struct annotation_line al; 166 }; 167 168 static inline double annotation_data__percent(struct annotation_data *data, 169 unsigned int which) 170 { 171 return which < PERCENT_MAX ? data->percent[which] : -1; 172 } 173 174 static inline const char *percent_type_str(unsigned int type) 175 { 176 static const char *str[PERCENT_MAX] = { 177 "local hits", 178 "global hits", 179 "local period", 180 "global period", 181 }; 182 183 if (WARN_ON(type >= PERCENT_MAX)) 184 return "N/A"; 185 186 return str[type]; 187 } 188 189 static inline struct disasm_line *disasm_line(struct annotation_line *al) 190 { 191 return al ? container_of(al, struct disasm_line, al) : NULL; 192 } 193 194 /* 195 * Is this offset in the same function as the line it is used? 196 * asm functions jump to other functions, for instance. 197 */ 198 static inline bool disasm_line__has_local_offset(const struct disasm_line *dl) 199 { 200 return dl->ops.target.offset_avail && !dl->ops.target.outside; 201 } 202 203 /* 204 * Can we draw an arrow from the jump to its target, for instance? I.e. 205 * is the jump and its target in the same function? 206 */ 207 bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym); 208 209 void disasm_line__free(struct disasm_line *dl); 210 struct annotation_line * 211 annotation_line__next(struct annotation_line *pos, struct list_head *head); 212 213 struct annotation_write_ops { 214 bool first_line, current_entry, change_color; 215 int width; 216 void *obj; 217 int (*set_color)(void *obj, int color); 218 void (*set_percent_color)(void *obj, double percent, bool current); 219 int (*set_jumps_percent_color)(void *obj, int nr, bool current); 220 void (*printf)(void *obj, const char *fmt, ...); 221 void (*write_graph)(void *obj, int graph); 222 }; 223 224 void annotation_line__write(struct annotation_line *al, struct annotation *notes, 225 struct annotation_write_ops *ops, 226 struct annotation_options *opts); 227 228 int __annotation__scnprintf_samples_period(struct annotation *notes, 229 char *bf, size_t size, 230 struct evsel *evsel, 231 bool show_freq); 232 233 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw, int max_ins_name); 234 size_t disasm__fprintf(struct list_head *head, FILE *fp); 235 void symbol__calc_percent(struct symbol *sym, struct evsel *evsel); 236 237 struct sym_hist { 238 u64 nr_samples; 239 u64 period; 240 struct sym_hist_entry addr[]; 241 }; 242 243 struct cyc_hist { 244 u64 start; 245 u64 cycles; 246 u64 cycles_aggr; 247 u64 cycles_max; 248 u64 cycles_min; 249 s64 cycles_spark[NUM_SPARKS]; 250 u32 num; 251 u32 num_aggr; 252 u8 have_start; 253 /* 1 byte padding */ 254 u16 reset; 255 }; 256 257 /** struct annotated_source - symbols with hits have this attached as in sannotation 258 * 259 * @histograms: Array of addr hit histograms per event being monitored 260 * nr_histograms: This may not be the same as evsel->evlist->core.nr_entries if 261 * we have more than a group in a evlist, where we will want 262 * to see each group separately, that is why symbol__annotate2() 263 * sets src->nr_histograms to evsel->nr_members. 264 * @lines: If 'print_lines' is specified, per source code line percentages 265 * @source: source parsed from a disassembler like objdump -dS 266 * @cyc_hist: Average cycles per basic block 267 * 268 * lines is allocated, percentages calculated and all sorted by percentage 269 * when the annotation is about to be presented, so the percentages are for 270 * one of the entries in the histogram array, i.e. for the event/counter being 271 * presented. It is deallocated right after symbol__{tui,tty,etc}_annotate 272 * returns. 273 */ 274 struct annotated_source { 275 struct list_head source; 276 size_t sizeof_sym_hist; 277 struct sym_hist *histograms; 278 struct annotation_line **offsets; 279 int nr_histograms; 280 int nr_entries; 281 int nr_asm_entries; 282 u16 max_line_len; 283 }; 284 285 struct annotated_branch { 286 u64 hit_cycles; 287 u64 hit_insn; 288 unsigned int total_insn; 289 unsigned int cover_insn; 290 struct cyc_hist *cycles_hist; 291 u64 max_coverage; 292 }; 293 294 struct LOCKABLE annotation { 295 u64 start; 296 struct annotation_options *options; 297 int nr_events; 298 int max_jump_sources; 299 struct { 300 u8 addr; 301 u8 jumps; 302 u8 target; 303 u8 min_addr; 304 u8 max_addr; 305 u8 max_ins_name; 306 } widths; 307 struct annotated_source *src; 308 struct annotated_branch *branch; 309 }; 310 311 static inline void annotation__init(struct annotation *notes __maybe_unused) 312 { 313 } 314 void annotation__exit(struct annotation *notes); 315 316 void annotation__lock(struct annotation *notes) EXCLUSIVE_LOCK_FUNCTION(*notes); 317 void annotation__unlock(struct annotation *notes) UNLOCK_FUNCTION(*notes); 318 bool annotation__trylock(struct annotation *notes) EXCLUSIVE_TRYLOCK_FUNCTION(true, *notes); 319 320 static inline int annotation__cycles_width(struct annotation *notes) 321 { 322 if (notes->branch && notes->options->show_minmax_cycle) 323 return ANNOTATION__IPC_WIDTH + ANNOTATION__MINMAX_CYCLES_WIDTH; 324 325 return notes->branch ? ANNOTATION__IPC_WIDTH + ANNOTATION__CYCLES_WIDTH : 0; 326 } 327 328 static inline int annotation__pcnt_width(struct annotation *notes) 329 { 330 return (symbol_conf.show_total_period ? 12 : 7) * notes->nr_events; 331 } 332 333 static inline bool annotation_line__filter(struct annotation_line *al, struct annotation *notes) 334 { 335 return notes->options->hide_src_code && al->offset == -1; 336 } 337 338 void annotation__set_offsets(struct annotation *notes, s64 size); 339 void annotation__mark_jump_targets(struct annotation *notes, struct symbol *sym); 340 void annotation__update_column_widths(struct annotation *notes); 341 void annotation__init_column_widths(struct annotation *notes, struct symbol *sym); 342 void annotation__toggle_full_addr(struct annotation *notes, struct map_symbol *ms); 343 344 static inline struct sym_hist *annotated_source__histogram(struct annotated_source *src, int idx) 345 { 346 return ((void *)src->histograms) + (src->sizeof_sym_hist * idx); 347 } 348 349 static inline struct sym_hist *annotation__histogram(struct annotation *notes, int idx) 350 { 351 return annotated_source__histogram(notes->src, idx); 352 } 353 354 static inline struct annotation *symbol__annotation(struct symbol *sym) 355 { 356 return (void *)sym - symbol_conf.priv_size; 357 } 358 359 int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample, 360 struct evsel *evsel); 361 362 struct annotated_branch *annotation__get_branch(struct annotation *notes); 363 364 int addr_map_symbol__account_cycles(struct addr_map_symbol *ams, 365 struct addr_map_symbol *start, 366 unsigned cycles); 367 368 int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample, 369 struct evsel *evsel, u64 addr); 370 371 struct annotated_source *symbol__hists(struct symbol *sym, int nr_hists); 372 void symbol__annotate_zero_histograms(struct symbol *sym); 373 374 int symbol__annotate(struct map_symbol *ms, 375 struct evsel *evsel, 376 struct annotation_options *options, 377 struct arch **parch); 378 int symbol__annotate2(struct map_symbol *ms, 379 struct evsel *evsel, 380 struct annotation_options *options, 381 struct arch **parch); 382 383 enum symbol_disassemble_errno { 384 SYMBOL_ANNOTATE_ERRNO__SUCCESS = 0, 385 386 /* 387 * Choose an arbitrary negative big number not to clash with standard 388 * errno since SUS requires the errno has distinct positive values. 389 * See 'Issue 6' in the link below. 390 * 391 * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html 392 */ 393 __SYMBOL_ANNOTATE_ERRNO__START = -10000, 394 395 SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX = __SYMBOL_ANNOTATE_ERRNO__START, 396 SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF, 397 SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING, 398 SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP, 399 SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE, 400 SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF, 401 402 __SYMBOL_ANNOTATE_ERRNO__END, 403 }; 404 405 int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, size_t buflen); 406 407 int symbol__annotate_printf(struct map_symbol *ms, struct evsel *evsel, 408 struct annotation_options *options); 409 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx); 410 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx); 411 void annotated_source__purge(struct annotated_source *as); 412 413 int map_symbol__annotation_dump(struct map_symbol *ms, struct evsel *evsel, 414 struct annotation_options *opts); 415 416 bool ui__has_annotation(void); 417 418 int symbol__tty_annotate(struct map_symbol *ms, struct evsel *evsel, struct annotation_options *opts); 419 420 int symbol__tty_annotate2(struct map_symbol *ms, struct evsel *evsel, struct annotation_options *opts); 421 422 #ifdef HAVE_SLANG_SUPPORT 423 int symbol__tui_annotate(struct map_symbol *ms, struct evsel *evsel, 424 struct hist_browser_timer *hbt, 425 struct annotation_options *opts); 426 #else 427 static inline int symbol__tui_annotate(struct map_symbol *ms __maybe_unused, 428 struct evsel *evsel __maybe_unused, 429 struct hist_browser_timer *hbt __maybe_unused, 430 struct annotation_options *opts __maybe_unused) 431 { 432 return 0; 433 } 434 #endif 435 436 void annotation_options__init(struct annotation_options *opt); 437 void annotation_options__exit(struct annotation_options *opt); 438 439 void annotation_config__init(struct annotation_options *opt); 440 441 int annotate_parse_percent_type(const struct option *opt, const char *_str, 442 int unset); 443 444 int annotate_check_args(struct annotation_options *args); 445 446 #endif /* __PERF_ANNOTATE_H */ 447