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