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