]> git.ipfire.org Git - thirdparty/linux.git/blame - tools/perf/util/annotate.h
perf tools: Ditch the symbol_conf.nr_events global
[thirdparty/linux.git] / tools / perf / util / annotate.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
78f7defe
ACM
2#ifndef __PERF_ANNOTATE_H
3#define __PERF_ANNOTATE_H
4
5#include <stdbool.h>
fb29fa58 6#include <stdint.h>
d944c4ee 7#include <linux/types.h>
78f7defe 8#include "symbol.h"
9783adf7 9#include "hist.h"
2b676bf0 10#include "sort.h"
78f7defe
ACM
11#include <linux/list.h>
12#include <linux/rbtree.h>
27683dc5 13#include <pthread.h>
78f7defe 14
75b49202
ACM
15struct ins_ops;
16
17struct ins {
18 const char *name;
19 struct ins_ops *ops;
20};
28548d78 21
c7e6ead7
ACM
22struct ins_operands {
23 char *raw;
44d1a3ed 24 struct {
6de783b6 25 char *raw;
44d1a3ed 26 char *name;
696703af 27 struct symbol *sym;
44d1a3ed 28 u64 addr;
e216874c
RB
29 s64 offset;
30 bool offset_avail;
751b1783 31 bool outside;
44d1a3ed 32 } target;
7a997fe4
ACM
33 union {
34 struct {
35 char *raw;
36 char *name;
37 u64 addr;
38 } source;
39 struct {
75b49202 40 struct ins ins;
7a997fe4
ACM
41 struct ins_operands *ops;
42 } locked;
43 };
c7e6ead7
ACM
44};
45
786c1b51
ACM
46struct arch;
47
4f9d0325 48struct ins_ops {
c46219ac 49 void (*free)(struct ins_operands *ops);
85a84e4f 50 int (*parse)(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms);
28548d78 51 int (*scnprintf)(struct ins *ins, char *bf, size_t size,
5417072b 52 struct ins_operands *ops);
4f9d0325
ACM
53};
54
4f9d0325 55bool ins__is_jump(const struct ins *ins);
d86b0597 56bool ins__is_call(const struct ins *ins);
6ef94929 57bool ins__is_ret(const struct ins *ins);
7e63a13a 58bool ins__is_lock(const struct ins *ins);
5417072b 59int ins__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops);
69fb09f6 60bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2);
4f9d0325 61
c426e584
ACM
62#define ANNOTATION__IPC_WIDTH 6
63#define ANNOTATION__CYCLES_WIDTH 6
3e71fc03 64#define ANNOTATION__MINMAX_CYCLES_WIDTH 19
c426e584 65
98bc80b0
ACM
66struct annotation_options {
67 bool hide_src_code,
68 use_offset,
69 jump_arrows,
70 show_linenr,
71 show_nr_jumps,
72 show_nr_samples,
3e71fc03
JY
73 show_total_period,
74 show_minmax_cycle;
592c10e2 75 u8 offset_level;
98bc80b0
ACM
76};
77
592c10e2
ACM
78enum {
79 ANNOTATION__OFFSET_JUMP_TARGETS = 1,
80 ANNOTATION__OFFSET_CALL,
81 ANNOTATION__MAX_OFFSET_LEVEL,
82};
83
84#define ANNOTATION__MIN_OFFSET_LEVEL ANNOTATION__OFFSET_JUMP_TARGETS
85
7f0b6fde
ACM
86extern struct annotation_options annotation__default_options;
87
e64aa75b
NK
88struct annotation;
89
7e304557
JO
90struct sym_hist_entry {
91 u64 nr_samples;
92 u64 period;
93};
94
95struct annotation_data {
96 double percent;
8b4c74dc 97 double percent_sum;
7e304557
JO
98 struct sym_hist_entry he;
99};
100
a17c4ca0
JO
101struct annotation_line {
102 struct list_head node;
5b12adc8 103 struct rb_node rb_node;
d5490b96
JO
104 s64 offset;
105 char *line;
106 int line_nr;
0db45bcf 107 int jump_sources;
37236d5e
JO
108 float ipc;
109 u64 cycles;
48659ebf
JY
110 u64 cycles_max;
111 u64 cycles_min;
c835e191 112 size_t privsize;
8b4c74dc 113 char *path;
4850c92e
ACM
114 u32 idx;
115 int idx_asm;
7e304557
JO
116 int samples_nr;
117 struct annotation_data samples[0];
a17c4ca0
JO
118};
119
29ed6e76 120struct disasm_line {
a17c4ca0 121 struct ins ins;
a17c4ca0 122 struct ins_operands ops;
c835e191
JO
123
124 /* This needs to be at the end. */
125 struct annotation_line al;
78f7defe
ACM
126};
127
c835e191
JO
128static inline struct disasm_line *disasm_line(struct annotation_line *al)
129{
130 return al ? container_of(al, struct disasm_line, al) : NULL;
131}
132
2eff0611
ACM
133/*
134 * Is this offset in the same function as the line it is used?
135 * asm functions jump to other functions, for instance.
136 */
137static inline bool disasm_line__has_local_offset(const struct disasm_line *dl)
fb29fa58 138{
2eff0611 139 return dl->ops.target.offset_avail && !dl->ops.target.outside;
fb29fa58
ACM
140}
141
2eff0611
ACM
142/*
143 * Can we draw an arrow from the jump to its target, for instance? I.e.
144 * is the jump and its target in the same function?
145 */
146bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym);
0db45bcf 147
29ed6e76 148void disasm_line__free(struct disasm_line *dl);
c4c72436
JO
149struct annotation_line *
150annotation_line__next(struct annotation_line *pos, struct list_head *head);
2f025ea0 151
c298304b
ACM
152struct annotation_write_ops {
153 bool first_line, current_entry, change_color;
154 int width;
155 void *obj;
156 int (*set_color)(void *obj, int color);
157 void (*set_percent_color)(void *obj, double percent, bool current);
158 int (*set_jumps_percent_color)(void *obj, int nr, bool current);
159 void (*printf)(void *obj, const char *fmt, ...);
160 void (*write_graph)(void *obj, int graph);
161};
162
2f025ea0 163double annotation_line__max_percent(struct annotation_line *al, struct annotation *notes);
a1e9b74c 164void annotation_line__write(struct annotation_line *al, struct annotation *notes,
c298304b 165 struct annotation_write_ops *ops);
2f025ea0 166
b213eac2
ACM
167int __annotation__scnprintf_samples_period(struct annotation *notes,
168 char *bf, size_t size,
169 struct perf_evsel *evsel,
170 bool show_freq);
171
172static inline int annotation__scnprintf_samples_period(struct annotation *notes,
173 char *bf, size_t size,
174 struct perf_evsel *evsel)
175{
176 return __annotation__scnprintf_samples_period(notes, bf, size, evsel, true);
177}
178
5417072b 179int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw);
5145418b 180size_t disasm__fprintf(struct list_head *head, FILE *fp);
9e4e0a9d 181void symbol__calc_percent(struct symbol *sym, struct perf_evsel *evsel);
78f7defe
ACM
182
183struct sym_hist {
8158683d 184 u64 nr_samples;
461c17f0 185 u64 period;
896bccd3 186 struct sym_hist_entry addr[0];
78f7defe
ACM
187};
188
d4957633
AK
189struct cyc_hist {
190 u64 start;
191 u64 cycles;
192 u64 cycles_aggr;
48659ebf
JY
193 u64 cycles_max;
194 u64 cycles_min;
d4957633
AK
195 u32 num;
196 u32 num_aggr;
197 u8 have_start;
198 /* 1 byte padding */
199 u16 reset;
200};
201
ce6f4fab 202/** struct annotated_source - symbols with hits have this attached as in sannotation
2f525d01 203 *
116c626b 204 * @histograms: Array of addr hit histograms per event being monitored
ce6f4fab 205 * @lines: If 'print_lines' is specified, per source code line percentages
29ed6e76 206 * @source: source parsed from a disassembler like objdump -dS
d4957633 207 * @cyc_hist: Average cycles per basic block
2f525d01 208 *
ce6f4fab 209 * lines is allocated, percentages calculated and all sorted by percentage
2f525d01
ACM
210 * when the annotation is about to be presented, so the percentages are for
211 * one of the entries in the histogram array, i.e. for the event/counter being
212 * presented. It is deallocated right after symbol__{tui,tty,etc}_annotate
213 * returns.
214 */
ce6f4fab
ACM
215struct annotated_source {
216 struct list_head source;
36532461 217 int nr_histograms;
5ec4502d 218 size_t sizeof_sym_hist;
d4957633 219 struct cyc_hist *cycles_hist;
116c626b 220 struct sym_hist *histograms;
ce6f4fab
ACM
221};
222
223struct annotation {
224 pthread_mutex_t lock;
70fbe057 225 u64 max_coverage;
0ca693b3 226 u64 start;
16932d77 227 struct annotation_options *options;
9d6bb41d 228 struct annotation_line **offsets;
0553e83d 229 int nr_events;
6dcd57e8 230 int nr_jumps;
bc1c0f3d 231 int max_jump_sources;
1cf5f98a
ACM
232 int nr_entries;
233 int nr_asm_entries;
5bc49f61 234 u16 max_line_len;
9761e86e
ACM
235 struct {
236 u8 addr;
237 u8 jumps;
238 u8 target;
239 u8 min_addr;
240 u8 max_addr;
241 } widths;
0e83a7e9 242 bool have_cycles;
ce6f4fab 243 struct annotated_source *src;
78f7defe
ACM
244};
245
0e83a7e9
ACM
246static inline int annotation__cycles_width(struct annotation *notes)
247{
3e71fc03
JY
248 if (notes->have_cycles && notes->options->show_minmax_cycle)
249 return ANNOTATION__IPC_WIDTH + ANNOTATION__MINMAX_CYCLES_WIDTH;
250
0e83a7e9
ACM
251 return notes->have_cycles ? ANNOTATION__IPC_WIDTH + ANNOTATION__CYCLES_WIDTH : 0;
252}
253
6af612d2
ACM
254static inline int annotation__pcnt_width(struct annotation *notes)
255{
256 return (notes->options->show_total_period ? 12 : 7) * notes->nr_events;
257}
258
9b80d1f9
ACM
259static inline bool annotation_line__filter(struct annotation_line *al, struct annotation *notes)
260{
261 return notes->options->hide_src_code && al->offset == -1;
262}
5bc49f61
ACM
263
264void annotation__set_offsets(struct annotation *notes, s64 size);
f56c083b 265void annotation__compute_ipc(struct annotation *notes, size_t size);
0db45bcf 266void annotation__mark_jump_targets(struct annotation *notes, struct symbol *sym);
7232bf7a 267void annotation__update_column_widths(struct annotation *notes);
b8b0d819 268void annotation__init_column_widths(struct annotation *notes, struct symbol *sym);
f56c083b 269
e1a91a83
ACM
270static inline struct sym_hist *annotated_source__histogram(struct annotated_source *src, int idx)
271{
272 return ((void *)src->histograms) + (src->sizeof_sym_hist * idx);
273}
274
2f525d01
ACM
275static inline struct sym_hist *annotation__histogram(struct annotation *notes, int idx)
276{
e1a91a83 277 return annotated_source__histogram(notes->src, idx);
2f525d01
ACM
278}
279
78f7defe
ACM
280static inline struct annotation *symbol__annotation(struct symbol *sym)
281{
813ccd15 282 return (void *)sym - symbol_conf.priv_size;
78f7defe
ACM
283}
284
bab89f6a 285int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
e345f3bd 286 struct perf_evsel *evsel);
0f4e7a24 287
d4957633
AK
288int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
289 struct addr_map_symbol *start,
290 unsigned cycles);
291
bab89f6a 292int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
e345f3bd 293 struct perf_evsel *evsel, u64 addr);
f626adff 294
14c8dde1 295struct annotated_source *symbol__hists(struct symbol *sym, int nr_hists);
36532461 296void symbol__annotate_zero_histograms(struct symbol *sym);
78f7defe 297
c34df25b 298int symbol__annotate(struct symbol *sym, struct map *map,
d03a686e 299 struct perf_evsel *evsel, size_t privsize,
5449f13c 300 struct arch **parch);
ecda45bd
ACM
301int symbol__annotate2(struct symbol *sym, struct map *map,
302 struct perf_evsel *evsel,
303 struct annotation_options *options,
304 struct arch **parch);
f626adff 305
ee51d851
ACM
306enum symbol_disassemble_errno {
307 SYMBOL_ANNOTATE_ERRNO__SUCCESS = 0,
308
309 /*
310 * Choose an arbitrary negative big number not to clash with standard
311 * errno since SUS requires the errno has distinct positive values.
312 * See 'Issue 6' in the link below.
313 *
314 * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html
315 */
316 __SYMBOL_ANNOTATE_ERRNO__START = -10000,
317
318 SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX = __SYMBOL_ANNOTATE_ERRNO__START,
319
320 __SYMBOL_ANNOTATE_ERRNO__END,
321};
322
323int symbol__strerror_disassemble(struct symbol *sym, struct map *map,
324 int errnum, char *buf, size_t buflen);
325
db8fd07a
NK
326int symbol__annotate_printf(struct symbol *sym, struct map *map,
327 struct perf_evsel *evsel, bool full_paths,
328 int min_pcnt, int max_lines, int context);
befd2a38 329int symbol__annotate_fprintf2(struct symbol *sym, FILE *fp);
36532461 330void symbol__annotate_zero_histogram(struct symbol *sym, int evidx);
ce6f4fab 331void symbol__annotate_decay_histogram(struct symbol *sym, int evidx);
f8eb37bd 332void annotated_source__purge(struct annotated_source *as);
78f7defe 333
d9bd7665
ACM
334int map_symbol__annotation_dump(struct map_symbol *ms, struct perf_evsel *evsel);
335
48c65bda
NK
336bool ui__has_annotation(void);
337
db8fd07a
NK
338int symbol__tty_annotate(struct symbol *sym, struct map *map,
339 struct perf_evsel *evsel, bool print_lines,
340 bool full_paths, int min_pcnt, int max_lines);
78f7defe 341
befd2a38
ACM
342int symbol__tty_annotate2(struct symbol *sym, struct map *map,
343 struct perf_evsel *evsel, bool print_lines,
344 bool full_paths);
345
89fe808a 346#ifdef HAVE_SLANG_SUPPORT
db8fd07a
NK
347int symbol__tui_annotate(struct symbol *sym, struct map *map,
348 struct perf_evsel *evsel,
9783adf7 349 struct hist_browser_timer *hbt);
1254b51e 350#else
1d037ca1 351static inline int symbol__tui_annotate(struct symbol *sym __maybe_unused,
db8fd07a
NK
352 struct map *map __maybe_unused,
353 struct perf_evsel *evsel __maybe_unused,
354 struct hist_browser_timer *hbt
355 __maybe_unused)
78f7defe
ACM
356{
357 return 0;
358}
78f7defe
ACM
359#endif
360
f69b64f7
AK
361extern const char *disassembler_style;
362
7f0b6fde
ACM
363void annotation_config__init(void);
364
78f7defe 365#endif /* __PERF_ANNOTATE_H */