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