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 #include "hashmap.h" 16 #include "disasm.h" 17 #include "branch.h" 18 #include "evsel.h" 19 20 struct hist_browser_timer; 21 struct hist_entry; 22 struct map; 23 struct map_symbol; 24 struct addr_map_symbol; 25 struct option; 26 struct perf_sample; 27 struct symbol; 28 struct annotated_data_type; 29 30 #define ANNOTATION__IPC_WIDTH 6 31 #define ANNOTATION__CYCLES_WIDTH 6 32 #define ANNOTATION__MINMAX_CYCLES_WIDTH 19 33 #define ANNOTATION__AVG_IPC_WIDTH 36 34 #define ANNOTATION__BR_CNTR_WIDTH 30 35 #define ANNOTATION_DUMMY_LEN 256 36 37 enum perf_disassembler { 38 PERF_DISASM_UNKNOWN = 0, 39 PERF_DISASM_LLVM, 40 PERF_DISASM_CAPSTONE, 41 PERF_DISASM_OBJDUMP, 42 }; 43 #define MAX_DISASSEMBLERS (PERF_DISASM_OBJDUMP + 1) 44 45 struct annotation_options { 46 bool hide_src_code, 47 use_offset, 48 jump_arrows, 49 print_lines, 50 full_path, 51 show_linenr, 52 show_fileloc, 53 show_nr_jumps, 54 show_minmax_cycle, 55 show_asm_raw, 56 show_br_cntr, 57 annotate_src, 58 code_with_type, 59 full_addr; 60 u8 offset_level; 61 u8 disassemblers[MAX_DISASSEMBLERS]; 62 u8 disassembler_used; 63 int min_pcnt; 64 int max_lines; 65 int context; 66 char *objdump_path; 67 char *disassembler_style; 68 const char *prefix; 69 const char *prefix_strip; 70 unsigned int percent_type; 71 }; 72 73 extern struct annotation_options annotate_opts; 74 75 enum { 76 ANNOTATION__OFFSET_JUMP_TARGETS = 1, 77 ANNOTATION__OFFSET_CALL, 78 ANNOTATION__MAX_OFFSET_LEVEL, 79 }; 80 81 #define ANNOTATION__MIN_OFFSET_LEVEL ANNOTATION__OFFSET_JUMP_TARGETS 82 83 struct annotation; 84 85 struct sym_hist_entry { 86 u64 nr_samples; 87 u64 period; 88 }; 89 90 enum { 91 PERCENT_HITS_LOCAL, 92 PERCENT_HITS_GLOBAL, 93 PERCENT_PERIOD_LOCAL, 94 PERCENT_PERIOD_GLOBAL, 95 PERCENT_MAX, 96 }; 97 98 struct annotation_data { 99 double percent[PERCENT_MAX]; 100 double percent_sum; 101 struct sym_hist_entry he; 102 }; 103 104 struct cycles_info { 105 float ipc; 106 u64 avg; 107 u64 max; 108 u64 min; 109 }; 110 111 struct annotation_line { 112 struct list_head node; 113 struct rb_node rb_node; 114 s64 offset; 115 char *line; 116 int line_nr; 117 char *fileloc; 118 char *path; 119 struct cycles_info *cycles; 120 int num_aggr; 121 int br_cntr_nr; 122 u64 *br_cntr; 123 struct evsel *evsel; 124 int jump_sources; 125 u32 idx; 126 int idx_asm; 127 int data_nr; 128 struct annotation_data data[]; 129 }; 130 131 struct disasm_line { 132 struct ins ins; 133 struct ins_operands ops; 134 union { 135 u8 bytes[4]; 136 u32 raw_insn; 137 } raw; 138 /* This needs to be at the end. */ 139 struct annotation_line al; 140 }; 141 142 extern const char * const perf_disassembler__strs[]; 143 144 void annotation_line__add(struct annotation_line *al, struct list_head *head); 145 146 static inline double annotation_data__percent(struct annotation_data *data, 147 unsigned int which) 148 { 149 return which < PERCENT_MAX ? data->percent[which] : -1; 150 } 151 152 static inline const char *percent_type_str(unsigned int type) 153 { 154 static const char *str[PERCENT_MAX] = { 155 "local hits", 156 "global hits", 157 "local period", 158 "global period", 159 }; 160 161 if (WARN_ON(type >= PERCENT_MAX)) 162 return "N/A"; 163 164 return str[type]; 165 } 166 167 static inline struct disasm_line *disasm_line(struct annotation_line *al) 168 { 169 return al ? container_of(al, struct disasm_line, al) : NULL; 170 } 171 172 /* 173 * Is this offset in the same function as the line it is used? 174 * asm functions jump to other functions, for instance. 175 */ 176 static inline bool disasm_line__has_local_offset(const struct disasm_line *dl) 177 { 178 return dl->ops.target.offset_avail && !dl->ops.target.outside; 179 } 180 181 /* 182 * Can we draw an arrow from the jump to its target, for instance? I.e. 183 * is the jump and its target in the same function? 184 */ 185 bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym); 186 187 struct annotation_line * 188 annotation_line__next(struct annotation_line *pos, struct list_head *head); 189 190 struct annotation_write_ops { 191 bool first_line, current_entry, change_color; 192 int width; 193 void *obj; 194 int (*set_color)(void *obj, int color); 195 void (*set_percent_color)(void *obj, double percent, bool current); 196 int (*set_jumps_percent_color)(void *obj, int nr, bool current); 197 void (*printf)(void *obj, const char *fmt, ...); 198 void (*write_graph)(void *obj, int graph); 199 }; 200 201 void annotation_line__write(struct annotation_line *al, struct annotation *notes, 202 struct annotation_write_ops *ops); 203 204 int __annotation__scnprintf_samples_period(struct annotation *notes, 205 char *bf, size_t size, 206 struct evsel *evsel, 207 bool show_freq); 208 209 size_t disasm__fprintf(struct list_head *head, FILE *fp); 210 void symbol__calc_percent(struct symbol *sym, struct evsel *evsel); 211 212 /** 213 * struct sym_hist - symbol histogram information for an event 214 * 215 * @nr_samples: Total number of samples. 216 * @period: Sum of sample periods. 217 */ 218 struct sym_hist { 219 u64 nr_samples; 220 u64 period; 221 }; 222 223 /** 224 * struct cyc_hist - (CPU) cycle histogram for a basic block 225 * 226 * @start: Start address of current block (if known). 227 * @cycles: Sum of cycles for the longest basic block. 228 * @cycles_aggr: Total cycles for this address. 229 * @cycles_max: Max cycles for this address. 230 * @cycles_min: Min cycles for this address. 231 * @cycles_spark: History of cycles for the longest basic block. 232 * @num: Number of samples for the longest basic block. 233 * @num_aggr: Total number of samples for this address. 234 * @have_start: Whether the current branch info has a start address. 235 * @reset: Number of resets due to a different start address. 236 * 237 * If sample has branch_stack and cycles info, it can construct basic blocks 238 * between two adjacent branches. It'd have start and end addresses but 239 * sometimes the start address may not be available. So the cycles are 240 * accounted at the end address. If multiple basic blocks end at the same 241 * address, it will take the longest one. 242 * 243 * The @start, @cycles, @cycles_spark and @num fields are used for the longest 244 * block only. Other fields are used for all cases. 245 * 246 * See __symbol__account_cycles(). 247 */ 248 struct cyc_hist { 249 u64 start; 250 u64 cycles; 251 u64 cycles_aggr; 252 u64 cycles_max; 253 u64 cycles_min; 254 s64 cycles_spark[NUM_SPARKS]; 255 u32 num; 256 u32 num_aggr; 257 u8 have_start; 258 /* 1 byte padding */ 259 u16 reset; 260 }; 261 262 /** 263 * struct annotated_source - symbols with hits have this attached as in annotation 264 * 265 * @source: List head for annotated_line (embeded in disasm_line). 266 * @histograms: Array of symbol histograms per event to maintain the total number 267 * of samples and period. 268 * @nr_histograms: This may not be the same as evsel->evlist->core.nr_entries if 269 * we have more than a group in a evlist, where we will want 270 * to see each group separately, that is why symbol__annotate2() 271 * sets src->nr_histograms to evsel->nr_members. 272 * @samples: Hash map of sym_hist_entry. Keyed by event index and offset in symbol. 273 * @nr_events: Number of events in the current output. 274 * @nr_entries: Number of annotated_line in the source list. 275 * @nr_asm_entries: Number of annotated_line with actual asm instruction in the 276 * source list. 277 * @max_jump_sources: Maximum number of jump instructions targeting to the same 278 * instruction. 279 * @widths: Precalculated width of each column in the TUI output. 280 * 281 * disasm_lines are allocated, percentages calculated and all sorted by percentage 282 * when the annotation is about to be presented, so the percentages are for 283 * one of the entries in the histogram array, i.e. for the event/counter being 284 * presented. It is deallocated right after symbol__{tui,tty,etc}_annotate 285 * returns. 286 */ 287 struct annotated_source { 288 struct list_head source; 289 struct sym_hist *histograms; 290 struct hashmap *samples; 291 int nr_histograms; 292 int nr_events; 293 int nr_entries; 294 int nr_asm_entries; 295 int max_jump_sources; 296 u64 start; 297 struct { 298 u8 addr; 299 u8 jumps; 300 u8 target; 301 u8 min_addr; 302 u8 max_addr; 303 u8 max_ins_name; 304 u16 max_line_len; 305 } widths; 306 }; 307 308 struct annotation_line *annotated_source__get_line(struct annotated_source *src, 309 s64 offset); 310 311 /* A branch counter once saturated */ 312 #define ANNOTATION__BR_CNTR_SATURATED_FLAG (1ULL << 63) 313 314 /** 315 * struct annotated_branch - basic block and IPC information for a symbol. 316 * 317 * @hit_cycles: Total executed cycles. 318 * @hit_insn: Total number of instructions executed. 319 * @total_insn: Number of instructions in the function. 320 * @cover_insn: Number of distinct, actually executed instructions. 321 * @cycles_hist: Array of cyc_hist for each instruction. 322 * @max_coverage: Maximum number of covered basic block (used for block-range). 323 * @br_cntr: Array of the occurrences of events (branch counters) during a block. 324 * 325 * This struct is used by two different codes when the sample has branch stack 326 * and cycles information. annotation__compute_ipc() calculates average IPC 327 * using @hit_insn / @hit_cycles. The actual coverage can be calculated using 328 * @cover_insn / @total_insn. The @cycles_hist can give IPC for each (longest) 329 * basic block ends at the given address. 330 * process_basic_block() calculates coverage of instructions (or basic blocks) 331 * in the function. 332 */ 333 struct annotated_branch { 334 u64 hit_cycles; 335 u64 hit_insn; 336 unsigned int total_insn; 337 unsigned int cover_insn; 338 struct cyc_hist *cycles_hist; 339 u64 max_coverage; 340 u64 *br_cntr; 341 }; 342 343 struct LOCKABLE annotation { 344 struct annotated_source *src; 345 struct annotated_branch *branch; 346 }; 347 348 static inline void annotation__init(struct annotation *notes __maybe_unused) 349 { 350 } 351 void annotation__exit(struct annotation *notes); 352 353 void annotation__lock(struct annotation *notes) EXCLUSIVE_LOCK_FUNCTION(*notes); 354 void annotation__unlock(struct annotation *notes) UNLOCK_FUNCTION(*notes); 355 bool annotation__trylock(struct annotation *notes) EXCLUSIVE_TRYLOCK_FUNCTION(true, *notes); 356 357 static inline int annotation__cycles_width(struct annotation *notes) 358 { 359 if (notes->branch && annotate_opts.show_minmax_cycle) 360 return ANNOTATION__IPC_WIDTH + ANNOTATION__MINMAX_CYCLES_WIDTH; 361 362 return notes->branch ? ANNOTATION__IPC_WIDTH + ANNOTATION__CYCLES_WIDTH : 0; 363 } 364 365 static inline int annotation__pcnt_width(struct annotation *notes) 366 { 367 return (symbol_conf.show_total_period ? 12 : 8) * notes->src->nr_events; 368 } 369 370 static inline bool annotation_line__filter(struct annotation_line *al) 371 { 372 return annotate_opts.hide_src_code && al->offset == -1; 373 } 374 375 static inline u8 annotation__br_cntr_width(void) 376 { 377 return annotate_opts.show_br_cntr ? ANNOTATION__BR_CNTR_WIDTH : 0; 378 } 379 380 void annotation__update_column_widths(struct annotation *notes); 381 void annotation__toggle_full_addr(struct annotation *notes, struct map_symbol *ms); 382 383 static inline struct sym_hist *annotated_source__histogram(struct annotated_source *src, 384 const struct evsel *evsel) 385 { 386 return &src->histograms[evsel->core.idx]; 387 } 388 389 static inline struct sym_hist *annotation__histogram(struct annotation *notes, 390 const struct evsel *evsel) 391 { 392 return annotated_source__histogram(notes->src, evsel); 393 } 394 395 static inline struct sym_hist_entry * 396 annotated_source__hist_entry(struct annotated_source *src, const struct evsel *evsel, u64 offset) 397 { 398 struct sym_hist_entry *entry; 399 long key = offset << 16 | evsel->core.idx; 400 401 if (!hashmap__find(src->samples, key, &entry)) 402 return NULL; 403 return entry; 404 } 405 406 static inline struct annotation *symbol__annotation(struct symbol *sym) 407 { 408 return (void *)sym - symbol_conf.priv_size; 409 } 410 411 int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample, 412 struct evsel *evsel); 413 414 struct annotated_branch *annotation__get_branch(struct annotation *notes); 415 416 int addr_map_symbol__account_cycles(struct addr_map_symbol *ams, 417 struct addr_map_symbol *start, 418 unsigned cycles, 419 struct evsel *evsel, 420 u64 br_cntr); 421 422 int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample, 423 struct evsel *evsel, u64 addr); 424 425 struct annotated_source *symbol__hists(struct symbol *sym, int nr_hists); 426 void symbol__annotate_zero_histograms(struct symbol *sym); 427 428 int symbol__annotate(struct map_symbol *ms, 429 struct evsel *evsel, 430 struct arch **parch); 431 int symbol__annotate2(struct map_symbol *ms, 432 struct evsel *evsel, 433 struct arch **parch); 434 435 enum symbol_disassemble_errno { 436 SYMBOL_ANNOTATE_ERRNO__SUCCESS = 0, 437 438 /* 439 * Choose an arbitrary negative big number not to clash with standard 440 * errno since SUS requires the errno has distinct positive values. 441 * See 'Issue 6' in the link below. 442 * 443 * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html 444 */ 445 __SYMBOL_ANNOTATE_ERRNO__START = -10000, 446 447 SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX = __SYMBOL_ANNOTATE_ERRNO__START, 448 SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF, 449 SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING, 450 SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP, 451 SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE, 452 SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF, 453 SYMBOL_ANNOTATE_ERRNO__COULDNT_DETERMINE_FILE_TYPE, 454 455 __SYMBOL_ANNOTATE_ERRNO__END, 456 }; 457 458 int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, size_t buflen); 459 460 void symbol__annotate_zero_histogram(struct symbol *sym, struct evsel *evsel); 461 void symbol__annotate_decay_histogram(struct symbol *sym, struct evsel *evsel); 462 void annotated_source__purge(struct annotated_source *as); 463 464 int map_symbol__annotation_dump(struct map_symbol *ms, struct evsel *evsel); 465 466 bool ui__has_annotation(void); 467 468 int hist_entry__annotate_printf(struct hist_entry *he, struct evsel *evsel); 469 int hist_entry__tty_annotate(struct hist_entry *he, struct evsel *evsel); 470 int hist_entry__tty_annotate2(struct hist_entry *he, struct evsel *evsel); 471 472 #ifdef HAVE_SLANG_SUPPORT 473 int symbol__tui_annotate(struct map_symbol *ms, struct evsel *evsel, 474 struct hist_browser_timer *hbt); 475 #else 476 static inline int symbol__tui_annotate(struct map_symbol *ms __maybe_unused, 477 struct evsel *evsel __maybe_unused, 478 struct hist_browser_timer *hbt __maybe_unused) 479 { 480 return 0; 481 } 482 #endif 483 484 void annotation_options__init(void); 485 void annotation_options__exit(void); 486 487 void annotation_config__init(void); 488 489 int annotate_parse_percent_type(const struct option *opt, const char *_str, 490 int unset); 491 492 int annotate_check_args(void); 493 494 /** 495 * struct annotated_op_loc - Location info of instruction operand 496 * @reg1: First register in the operand 497 * @reg2: Second register in the operand 498 * @offset: Memory access offset in the operand 499 * @segment: Segment selector register 500 * @mem_ref: Whether the operand accesses memory 501 * @multi_regs: Whether the second register is used 502 * @imm: Whether the operand is an immediate value (in offset) 503 */ 504 struct annotated_op_loc { 505 int reg1; 506 int reg2; 507 int offset; 508 u8 segment; 509 bool mem_ref; 510 bool multi_regs; 511 bool imm; 512 }; 513 514 enum annotated_insn_ops { 515 INSN_OP_SOURCE = 0, 516 INSN_OP_TARGET = 1, 517 518 INSN_OP_MAX, 519 }; 520 521 enum annotated_x86_segment { 522 INSN_SEG_NONE = 0, 523 524 INSN_SEG_X86_CS, 525 INSN_SEG_X86_DS, 526 INSN_SEG_X86_ES, 527 INSN_SEG_X86_FS, 528 INSN_SEG_X86_GS, 529 INSN_SEG_X86_SS, 530 }; 531 532 /** 533 * struct annotated_insn_loc - Location info of instruction 534 * @ops: Array of location info for source and target operands 535 */ 536 struct annotated_insn_loc { 537 struct annotated_op_loc ops[INSN_OP_MAX]; 538 }; 539 540 #define for_each_insn_op_loc(insn_loc, i, op_loc) \ 541 for (i = INSN_OP_SOURCE, op_loc = &(insn_loc)->ops[i]; \ 542 i < INSN_OP_MAX; \ 543 i++, op_loc++) 544 545 /* Get detailed location info in the instruction */ 546 int annotate_get_insn_location(struct arch *arch, struct disasm_line *dl, 547 struct annotated_insn_loc *loc); 548 549 /* Returns a data type from the sample instruction (if any) */ 550 struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he); 551 552 struct annotated_item_stat { 553 struct list_head list; 554 char *name; 555 int good; 556 int bad; 557 }; 558 extern struct list_head ann_insn_stat; 559 560 /* Calculate PC-relative address */ 561 u64 annotate_calc_pcrel(struct map_symbol *ms, u64 ip, int offset, 562 struct disasm_line *dl); 563 564 /** 565 * struct annotated_basic_block - Basic block of instructions 566 * @list: List node 567 * @begin: start instruction in the block 568 * @end: end instruction in the block 569 */ 570 struct annotated_basic_block { 571 struct list_head list; 572 struct disasm_line *begin; 573 struct disasm_line *end; 574 }; 575 576 /* Get a list of basic blocks from src to dst addresses */ 577 int annotate_get_basic_blocks(struct symbol *sym, s64 src, s64 dst, 578 struct list_head *head); 579 580 void debuginfo_cache__delete(void); 581 582 int annotation_br_cntr_entry(char **str, int br_cntr_nr, u64 *br_cntr, 583 int num_aggr, struct evsel *evsel); 584 int annotation_br_cntr_abbr_list(char **str, struct evsel *evsel, bool header); 585 #endif /* __PERF_ANNOTATE_H */ 586