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