annotate.h 17 KB

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