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