1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_ANNOTATE_H
3 #define __PERF_ANNOTATE_H
8 #include <linux/types.h>
9 #include <linux/list.h>
10 #include <linux/rbtree.h>
12 #include "symbol_conf.h"
18 struct hist_browser_timer;
22 struct addr_map_symbol;
27 struct annotated_data_type;
29 #define ANNOTATION__IPC_WIDTH 6
30 #define ANNOTATION__CYCLES_WIDTH 6
31 #define ANNOTATION__MINMAX_CYCLES_WIDTH 19
32 #define ANNOTATION__AVG_IPC_WIDTH 36
33 #define ANNOTATION_DUMMY_LEN 256
35 struct annotation_options {
53 char *disassembler_style;
55 const char *prefix_strip;
56 unsigned int percent_type;
59 extern struct annotation_options annotate_opts;
62 ANNOTATION__OFFSET_JUMP_TARGETS = 1,
63 ANNOTATION__OFFSET_CALL,
64 ANNOTATION__MAX_OFFSET_LEVEL,
67 #define ANNOTATION__MIN_OFFSET_LEVEL ANNOTATION__OFFSET_JUMP_TARGETS
71 struct sym_hist_entry {
80 PERCENT_PERIOD_GLOBAL,
84 struct annotation_data {
85 double percent[PERCENT_MAX];
87 struct sym_hist_entry he;
97 struct annotation_line {
98 struct list_head node;
99 struct rb_node rb_node;
105 struct cycles_info *cycles;
110 struct annotation_data data[];
115 struct ins_operands ops;
117 /* This needs to be at the end. */
118 struct annotation_line al;
121 void annotation_line__add(struct annotation_line *al, struct list_head *head);
123 static inline double annotation_data__percent(struct annotation_data *data,
126 return which < PERCENT_MAX ? data->percent[which] : -1;
129 static inline const char *percent_type_str(unsigned int type)
131 static const char *str[PERCENT_MAX] = {
138 if (WARN_ON(type >= PERCENT_MAX))
144 static inline struct disasm_line *disasm_line(struct annotation_line *al)
146 return al ? container_of(al, struct disasm_line, al) : NULL;
150 * Is this offset in the same function as the line it is used?
151 * asm functions jump to other functions, for instance.
153 static inline bool disasm_line__has_local_offset(const struct disasm_line *dl)
155 return dl->ops.target.offset_avail && !dl->ops.target.outside;
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?
162 bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym);
164 struct annotation_line *
165 annotation_line__next(struct annotation_line *pos, struct list_head *head);
167 struct annotation_write_ops {
168 bool first_line, current_entry, change_color;
171 int (*set_color)(void *obj, int color);
172 void (*set_percent_color)(void *obj, double percent, bool current);
173 int (*set_jumps_percent_color)(void *obj, int nr, bool current);
174 void (*printf)(void *obj, const char *fmt, ...);
175 void (*write_graph)(void *obj, int graph);
178 void annotation_line__write(struct annotation_line *al, struct annotation *notes,
179 struct annotation_write_ops *ops);
181 int __annotation__scnprintf_samples_period(struct annotation *notes,
182 char *bf, size_t size,
186 size_t disasm__fprintf(struct list_head *head, FILE *fp);
187 void symbol__calc_percent(struct symbol *sym, struct evsel *evsel);
190 * struct sym_hist - symbol histogram information for an event
192 * @nr_samples: Total number of samples.
193 * @period: Sum of sample periods.
201 * struct cyc_hist - (CPU) cycle histogram for a basic block
203 * @start: Start address of current block (if known).
204 * @cycles: Sum of cycles for the longest basic block.
205 * @cycles_aggr: Total cycles for this address.
206 * @cycles_max: Max cycles for this address.
207 * @cycles_min: Min cycles for this address.
208 * @cycles_spark: History of cycles for the longest basic block.
209 * @num: Number of samples for the longest basic block.
210 * @num_aggr: Total number of samples for this address.
211 * @have_start: Whether the current branch info has a start address.
212 * @reset: Number of resets due to a different start address.
214 * If sample has branch_stack and cycles info, it can construct basic blocks
215 * between two adjacent branches. It'd have start and end addresses but
216 * sometimes the start address may not be available. So the cycles are
217 * accounted at the end address. If multiple basic blocks end at the same
218 * address, it will take the longest one.
220 * The @start, @cycles, @cycles_spark and @num fields are used for the longest
221 * block only. Other fields are used for all cases.
223 * See __symbol__account_cycles().
231 s64 cycles_spark[NUM_SPARKS];
240 * struct annotated_source - symbols with hits have this attached as in annotation
242 * @source: List head for annotated_line (embeded in disasm_line).
243 * @histograms: Array of symbol histograms per event to maintain the total number
244 * of samples and period.
245 * @nr_histograms: This may not be the same as evsel->evlist->core.nr_entries if
246 * we have more than a group in a evlist, where we will want
247 * to see each group separately, that is why symbol__annotate2()
248 * sets src->nr_histograms to evsel->nr_members.
249 * @samples: Hash map of sym_hist_entry. Keyed by event index and offset in symbol.
250 * @nr_events: Number of events in the current output.
251 * @nr_entries: Number of annotated_line in the source list.
252 * @nr_asm_entries: Number of annotated_line with actual asm instruction in the
254 * @max_jump_sources: Maximum number of jump instructions targeting to the same
256 * @widths: Precalculated width of each column in the TUI output.
258 * disasm_lines are allocated, percentages calculated and all sorted by percentage
259 * when the annotation is about to be presented, so the percentages are for
260 * one of the entries in the histogram array, i.e. for the event/counter being
261 * presented. It is deallocated right after symbol__{tui,tty,etc}_annotate
264 struct annotated_source {
265 struct list_head source;
266 struct sym_hist *histograms;
267 struct hashmap *samples;
272 int max_jump_sources;
285 struct annotation_line *annotated_source__get_line(struct annotated_source *src,
289 * struct annotated_branch - basic block and IPC information for a symbol.
291 * @hit_cycles: Total executed cycles.
292 * @hit_insn: Total number of instructions executed.
293 * @total_insn: Number of instructions in the function.
294 * @cover_insn: Number of distinct, actually executed instructions.
295 * @cycles_hist: Array of cyc_hist for each instruction.
296 * @max_coverage: Maximum number of covered basic block (used for block-range).
298 * This struct is used by two different codes when the sample has branch stack
299 * and cycles information. annotation__compute_ipc() calculates average IPC
300 * using @hit_insn / @hit_cycles. The actual coverage can be calculated using
301 * @cover_insn / @total_insn. The @cycles_hist can give IPC for each (longest)
302 * basic block ends at the given address.
303 * process_basic_block() calculates coverage of instructions (or basic blocks)
306 struct annotated_branch {
309 unsigned int total_insn;
310 unsigned int cover_insn;
311 struct cyc_hist *cycles_hist;
315 struct LOCKABLE annotation {
316 struct annotated_source *src;
317 struct annotated_branch *branch;
320 static inline void annotation__init(struct annotation *notes __maybe_unused)
323 void annotation__exit(struct annotation *notes);
325 void annotation__lock(struct annotation *notes) EXCLUSIVE_LOCK_FUNCTION(*notes);
326 void annotation__unlock(struct annotation *notes) UNLOCK_FUNCTION(*notes);
327 bool annotation__trylock(struct annotation *notes) EXCLUSIVE_TRYLOCK_FUNCTION(true, *notes);
329 static inline int annotation__cycles_width(struct annotation *notes)
331 if (notes->branch && annotate_opts.show_minmax_cycle)
332 return ANNOTATION__IPC_WIDTH + ANNOTATION__MINMAX_CYCLES_WIDTH;
334 return notes->branch ? ANNOTATION__IPC_WIDTH + ANNOTATION__CYCLES_WIDTH : 0;
337 static inline int annotation__pcnt_width(struct annotation *notes)
339 return (symbol_conf.show_total_period ? 12 : 7) * notes->src->nr_events;
342 static inline bool annotation_line__filter(struct annotation_line *al)
344 return annotate_opts.hide_src_code && al->offset == -1;
347 void annotation__update_column_widths(struct annotation *notes);
348 void annotation__toggle_full_addr(struct annotation *notes, struct map_symbol *ms);
350 static inline struct sym_hist *annotated_source__histogram(struct annotated_source *src, int idx)
352 return &src->histograms[idx];
355 static inline struct sym_hist *annotation__histogram(struct annotation *notes, int idx)
357 return annotated_source__histogram(notes->src, idx);
360 static inline struct sym_hist_entry *
361 annotated_source__hist_entry(struct annotated_source *src, int idx, u64 offset)
363 struct sym_hist_entry *entry;
364 long key = offset << 16 | idx;
366 if (!hashmap__find(src->samples, key, &entry))
371 static inline struct annotation *symbol__annotation(struct symbol *sym)
373 return (void *)sym - symbol_conf.priv_size;
376 int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
377 struct evsel *evsel);
379 struct annotated_branch *annotation__get_branch(struct annotation *notes);
381 int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
382 struct addr_map_symbol *start,
385 int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
386 struct evsel *evsel, u64 addr);
388 struct annotated_source *symbol__hists(struct symbol *sym, int nr_hists);
389 void symbol__annotate_zero_histograms(struct symbol *sym);
391 int symbol__annotate(struct map_symbol *ms,
393 struct arch **parch);
394 int symbol__annotate2(struct map_symbol *ms,
396 struct arch **parch);
398 enum symbol_disassemble_errno {
399 SYMBOL_ANNOTATE_ERRNO__SUCCESS = 0,
402 * Choose an arbitrary negative big number not to clash with standard
403 * errno since SUS requires the errno has distinct positive values.
404 * See 'Issue 6' in the link below.
406 * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html
408 __SYMBOL_ANNOTATE_ERRNO__START = -10000,
410 SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX = __SYMBOL_ANNOTATE_ERRNO__START,
411 SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF,
412 SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING,
413 SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP,
414 SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE,
415 SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF,
417 __SYMBOL_ANNOTATE_ERRNO__END,
420 int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, size_t buflen);
422 int symbol__annotate_printf(struct map_symbol *ms, struct evsel *evsel);
423 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx);
424 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx);
425 void annotated_source__purge(struct annotated_source *as);
427 int map_symbol__annotation_dump(struct map_symbol *ms, struct evsel *evsel);
429 bool ui__has_annotation(void);
431 int symbol__tty_annotate(struct map_symbol *ms, struct evsel *evsel);
433 int symbol__tty_annotate2(struct map_symbol *ms, struct evsel *evsel);
435 #ifdef HAVE_SLANG_SUPPORT
436 int symbol__tui_annotate(struct map_symbol *ms, struct evsel *evsel,
437 struct hist_browser_timer *hbt);
439 static inline int symbol__tui_annotate(struct map_symbol *ms __maybe_unused,
440 struct evsel *evsel __maybe_unused,
441 struct hist_browser_timer *hbt __maybe_unused)
447 void annotation_options__init(void);
448 void annotation_options__exit(void);
450 void annotation_config__init(void);
452 int annotate_parse_percent_type(const struct option *opt, const char *_str,
455 int annotate_check_args(void);
458 * struct annotated_op_loc - Location info of instruction operand
459 * @reg1: First register in the operand
460 * @reg2: Second register in the operand
461 * @offset: Memory access offset in the operand
462 * @segment: Segment selector register
463 * @mem_ref: Whether the operand accesses memory
464 * @multi_regs: Whether the second register is used
465 * @imm: Whether the operand is an immediate value (in offset)
467 struct annotated_op_loc {
477 enum annotated_insn_ops {
484 enum annotated_x86_segment {
496 * struct annotated_insn_loc - Location info of instruction
497 * @ops: Array of location info for source and target operands
499 struct annotated_insn_loc {
500 struct annotated_op_loc ops[INSN_OP_MAX];
503 #define for_each_insn_op_loc(insn_loc, i, op_loc) \
504 for (i = INSN_OP_SOURCE, op_loc = &(insn_loc)->ops[i]; \
508 /* Get detailed location info in the instruction */
509 int annotate_get_insn_location(struct arch *arch, struct disasm_line *dl,
510 struct annotated_insn_loc *loc);
512 /* Returns a data type from the sample instruction (if any) */
513 struct annotated_data_type *hist_entry__get_data_type(struct hist_entry *he);
515 struct annotated_item_stat {
516 struct list_head list;
521 extern struct list_head ann_insn_stat;
523 /* Calculate PC-relative address */
524 u64 annotate_calc_pcrel(struct map_symbol *ms, u64 ip, int offset,
525 struct disasm_line *dl);
528 * struct annotated_basic_block - Basic block of instructions
530 * @begin: start instruction in the block
531 * @end: end instruction in the block
533 struct annotated_basic_block {
534 struct list_head list;
535 struct disasm_line *begin;
536 struct disasm_line *end;
539 /* Get a list of basic blocks from src to dst addresses */
540 int annotate_get_basic_blocks(struct symbol *sym, s64 src, s64 dst,
541 struct list_head *head);
543 #endif /* __PERF_ANNOTATE_H */