1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_ANNOTATE_H
3 #define __PERF_ANNOTATE_H
7 #include <linux/types.h>
11 #include <linux/list.h>
12 #include <linux/rbtree.h>
43 struct ins_operands *ops;
51 void (*free)(struct ins_operands *ops);
52 int (*parse)(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms);
53 int (*scnprintf)(struct ins *ins, char *bf, size_t size,
54 struct ins_operands *ops);
57 bool ins__is_jump(const struct ins *ins);
58 bool ins__is_call(const struct ins *ins);
59 bool ins__is_ret(const struct ins *ins);
60 bool ins__is_lock(const struct ins *ins);
61 int ins__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops);
62 bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2);
64 #define ANNOTATION__IPC_WIDTH 6
65 #define ANNOTATION__CYCLES_WIDTH 6
66 #define ANNOTATION__MINMAX_CYCLES_WIDTH 19
68 struct annotation_options {
85 const char *objdump_path;
86 const char *disassembler_style;
87 unsigned int percent_type;
91 ANNOTATION__OFFSET_JUMP_TARGETS = 1,
92 ANNOTATION__OFFSET_CALL,
93 ANNOTATION__MAX_OFFSET_LEVEL,
96 #define ANNOTATION__MIN_OFFSET_LEVEL ANNOTATION__OFFSET_JUMP_TARGETS
98 extern struct annotation_options annotation__default_options;
102 struct sym_hist_entry {
110 PERCENT_PERIOD_LOCAL,
111 PERCENT_PERIOD_GLOBAL,
115 struct annotation_data {
116 double percent[PERCENT_MAX];
118 struct sym_hist_entry he;
121 struct annotation_line {
122 struct list_head node;
123 struct rb_node rb_node;
137 struct annotation_data data[0];
142 struct ins_operands ops;
144 /* This needs to be at the end. */
145 struct annotation_line al;
148 static inline double annotation_data__percent(struct annotation_data *data,
151 return which < PERCENT_MAX ? data->percent[which] : -1;
154 static inline const char *percent_type_str(unsigned int type)
156 static const char *str[PERCENT_MAX] = {
163 if (WARN_ON(type >= PERCENT_MAX))
169 static inline struct disasm_line *disasm_line(struct annotation_line *al)
171 return al ? container_of(al, struct disasm_line, al) : NULL;
175 * Is this offset in the same function as the line it is used?
176 * asm functions jump to other functions, for instance.
178 static inline bool disasm_line__has_local_offset(const struct disasm_line *dl)
180 return dl->ops.target.offset_avail && !dl->ops.target.outside;
184 * Can we draw an arrow from the jump to its target, for instance? I.e.
185 * is the jump and its target in the same function?
187 bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym);
189 void disasm_line__free(struct disasm_line *dl);
190 struct annotation_line *
191 annotation_line__next(struct annotation_line *pos, struct list_head *head);
193 struct annotation_write_ops {
194 bool first_line, current_entry, change_color;
197 int (*set_color)(void *obj, int color);
198 void (*set_percent_color)(void *obj, double percent, bool current);
199 int (*set_jumps_percent_color)(void *obj, int nr, bool current);
200 void (*printf)(void *obj, const char *fmt, ...);
201 void (*write_graph)(void *obj, int graph);
204 void annotation_line__write(struct annotation_line *al, struct annotation *notes,
205 struct annotation_write_ops *ops,
206 struct annotation_options *opts);
208 int __annotation__scnprintf_samples_period(struct annotation *notes,
209 char *bf, size_t size,
210 struct perf_evsel *evsel,
213 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw);
214 size_t disasm__fprintf(struct list_head *head, FILE *fp);
215 void symbol__calc_percent(struct symbol *sym, struct perf_evsel *evsel);
220 struct sym_hist_entry addr[0];
236 /** struct annotated_source - symbols with hits have this attached as in sannotation
238 * @histograms: Array of addr hit histograms per event being monitored
239 * nr_histograms: This may not be the same as evsel->evlist->nr_entries if
240 * we have more than a group in a evlist, where we will want
241 * to see each group separately, that is why symbol__annotate2()
242 * sets src->nr_histograms to evsel->nr_members.
243 * @lines: If 'print_lines' is specified, per source code line percentages
244 * @source: source parsed from a disassembler like objdump -dS
245 * @cyc_hist: Average cycles per basic block
247 * lines is allocated, percentages calculated and all sorted by percentage
248 * when the annotation is about to be presented, so the percentages are for
249 * one of the entries in the histogram array, i.e. for the event/counter being
250 * presented. It is deallocated right after symbol__{tui,tty,etc}_annotate
253 struct annotated_source {
254 struct list_head source;
256 size_t sizeof_sym_hist;
257 struct cyc_hist *cycles_hist;
258 struct sym_hist *histograms;
262 pthread_mutex_t lock;
265 struct annotation_options *options;
266 struct annotation_line **offsets;
269 int max_jump_sources;
281 struct annotated_source *src;
284 static inline int annotation__cycles_width(struct annotation *notes)
286 if (notes->have_cycles && notes->options->show_minmax_cycle)
287 return ANNOTATION__IPC_WIDTH + ANNOTATION__MINMAX_CYCLES_WIDTH;
289 return notes->have_cycles ? ANNOTATION__IPC_WIDTH + ANNOTATION__CYCLES_WIDTH : 0;
292 static inline int annotation__pcnt_width(struct annotation *notes)
294 return (notes->options->show_total_period ? 12 : 7) * notes->nr_events;
297 static inline bool annotation_line__filter(struct annotation_line *al, struct annotation *notes)
299 return notes->options->hide_src_code && al->offset == -1;
302 void annotation__set_offsets(struct annotation *notes, s64 size);
303 void annotation__compute_ipc(struct annotation *notes, size_t size);
304 void annotation__mark_jump_targets(struct annotation *notes, struct symbol *sym);
305 void annotation__update_column_widths(struct annotation *notes);
306 void annotation__init_column_widths(struct annotation *notes, struct symbol *sym);
308 static inline struct sym_hist *annotated_source__histogram(struct annotated_source *src, int idx)
310 return ((void *)src->histograms) + (src->sizeof_sym_hist * idx);
313 static inline struct sym_hist *annotation__histogram(struct annotation *notes, int idx)
315 return annotated_source__histogram(notes->src, idx);
318 static inline struct annotation *symbol__annotation(struct symbol *sym)
320 return (void *)sym - symbol_conf.priv_size;
323 int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
324 struct perf_evsel *evsel);
326 int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
327 struct addr_map_symbol *start,
330 int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
331 struct perf_evsel *evsel, u64 addr);
333 struct annotated_source *symbol__hists(struct symbol *sym, int nr_hists);
334 void symbol__annotate_zero_histograms(struct symbol *sym);
336 int symbol__annotate(struct symbol *sym, struct map *map,
337 struct perf_evsel *evsel, size_t privsize,
338 struct annotation_options *options,
339 struct arch **parch);
340 int symbol__annotate2(struct symbol *sym, struct map *map,
341 struct perf_evsel *evsel,
342 struct annotation_options *options,
343 struct arch **parch);
345 enum symbol_disassemble_errno {
346 SYMBOL_ANNOTATE_ERRNO__SUCCESS = 0,
349 * Choose an arbitrary negative big number not to clash with standard
350 * errno since SUS requires the errno has distinct positive values.
351 * See 'Issue 6' in the link below.
353 * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html
355 __SYMBOL_ANNOTATE_ERRNO__START = -10000,
357 SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX = __SYMBOL_ANNOTATE_ERRNO__START,
359 __SYMBOL_ANNOTATE_ERRNO__END,
362 int symbol__strerror_disassemble(struct symbol *sym, struct map *map,
363 int errnum, char *buf, size_t buflen);
365 int symbol__annotate_printf(struct symbol *sym, struct map *map,
366 struct perf_evsel *evsel,
367 struct annotation_options *options);
368 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx);
369 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx);
370 void annotated_source__purge(struct annotated_source *as);
372 int map_symbol__annotation_dump(struct map_symbol *ms, struct perf_evsel *evsel,
373 struct annotation_options *opts);
375 bool ui__has_annotation(void);
377 int symbol__tty_annotate(struct symbol *sym, struct map *map,
378 struct perf_evsel *evsel, struct annotation_options *opts);
380 int symbol__tty_annotate2(struct symbol *sym, struct map *map,
381 struct perf_evsel *evsel, struct annotation_options *opts);
383 #ifdef HAVE_SLANG_SUPPORT
384 int symbol__tui_annotate(struct symbol *sym, struct map *map,
385 struct perf_evsel *evsel,
386 struct hist_browser_timer *hbt,
387 struct annotation_options *opts);
389 static inline int symbol__tui_annotate(struct symbol *sym __maybe_unused,
390 struct map *map __maybe_unused,
391 struct perf_evsel *evsel __maybe_unused,
392 struct hist_browser_timer *hbt __maybe_unused,
393 struct annotation_options *opts __maybe_unused)
399 void annotation_config__init(void);
401 int annotate_parse_percent_type(const struct option *opt, const char *_str,
403 #endif /* __PERF_ANNOTATE_H */