Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux-block.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>
3e0d7953 14#include <asm/bug.h>
78f7defe 15
75b49202
ACM
16struct ins_ops;
17
18struct ins {
19 const char *name;
20 struct ins_ops *ops;
21};
28548d78 22
c7e6ead7
ACM
23struct ins_operands {
24 char *raw;
4e67b2a5 25 char *raw_comment;
44d1a3ed 26 struct {
6de783b6 27 char *raw;
44d1a3ed 28 char *name;
696703af 29 struct symbol *sym;
44d1a3ed 30 u64 addr;
e216874c
RB
31 s64 offset;
32 bool offset_avail;
751b1783 33 bool outside;
44d1a3ed 34 } target;
7a997fe4
ACM
35 union {
36 struct {
37 char *raw;
38 char *name;
39 u64 addr;
40 } source;
41 struct {
75b49202 42 struct ins ins;
7a997fe4
ACM
43 struct ins_operands *ops;
44 } locked;
45 };
c7e6ead7
ACM
46};
47
786c1b51
ACM
48struct arch;
49
4f9d0325 50struct ins_ops {
c46219ac 51 void (*free)(struct ins_operands *ops);
85a84e4f 52 int (*parse)(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms);
28548d78 53 int (*scnprintf)(struct ins *ins, char *bf, size_t size,
5417072b 54 struct ins_operands *ops);
4f9d0325
ACM
55};
56
4f9d0325 57bool ins__is_jump(const struct ins *ins);
d86b0597 58bool ins__is_call(const struct ins *ins);
6ef94929 59bool ins__is_ret(const struct ins *ins);
7e63a13a 60bool ins__is_lock(const struct ins *ins);
5417072b 61int ins__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops);
69fb09f6 62bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2);
4f9d0325 63
c426e584
ACM
64#define ANNOTATION__IPC_WIDTH 6
65#define ANNOTATION__CYCLES_WIDTH 6
3e71fc03 66#define ANNOTATION__MINMAX_CYCLES_WIDTH 19
c426e584 67
98bc80b0
ACM
68struct annotation_options {
69 bool hide_src_code,
70 use_offset,
71 jump_arrows,
982d410b
ACM
72 print_lines,
73 full_path,
98bc80b0
ACM
74 show_linenr,
75 show_nr_jumps,
76 show_nr_samples,
3e71fc03 77 show_total_period,
1eddd9e4
ACM
78 show_minmax_cycle,
79 show_asm_raw,
80 annotate_src;
592c10e2 81 u8 offset_level;
982d410b
ACM
82 int min_pcnt;
83 int max_lines;
84 int context;
f178fd2d 85 const char *objdump_path;
a47e843e 86 const char *disassembler_style;
796ca33d 87 unsigned int percent_type;
98bc80b0
ACM
88};
89
592c10e2
ACM
90enum {
91 ANNOTATION__OFFSET_JUMP_TARGETS = 1,
92 ANNOTATION__OFFSET_CALL,
93 ANNOTATION__MAX_OFFSET_LEVEL,
94};
95
96#define ANNOTATION__MIN_OFFSET_LEVEL ANNOTATION__OFFSET_JUMP_TARGETS
97
7f0b6fde
ACM
98extern struct annotation_options annotation__default_options;
99
e64aa75b
NK
100struct annotation;
101
7e304557
JO
102struct sym_hist_entry {
103 u64 nr_samples;
104 u64 period;
105};
106
6d9f0c2d
JO
107enum {
108 PERCENT_HITS_LOCAL,
75a8c1ff 109 PERCENT_HITS_GLOBAL,
ab371169 110 PERCENT_PERIOD_LOCAL,
e58684df 111 PERCENT_PERIOD_GLOBAL,
6d9f0c2d
JO
112 PERCENT_MAX,
113};
114
7e304557 115struct annotation_data {
6d9f0c2d 116 double percent[PERCENT_MAX];
8b4c74dc 117 double percent_sum;
7e304557
JO
118 struct sym_hist_entry he;
119};
120
a17c4ca0
JO
121struct annotation_line {
122 struct list_head node;
5b12adc8 123 struct rb_node rb_node;
d5490b96
JO
124 s64 offset;
125 char *line;
126 int line_nr;
0db45bcf 127 int jump_sources;
37236d5e
JO
128 float ipc;
129 u64 cycles;
48659ebf
JY
130 u64 cycles_max;
131 u64 cycles_min;
c835e191 132 size_t privsize;
8b4c74dc 133 char *path;
4850c92e
ACM
134 u32 idx;
135 int idx_asm;
c2f938ba
JO
136 int data_nr;
137 struct annotation_data data[0];
a17c4ca0
JO
138};
139
29ed6e76 140struct disasm_line {
a17c4ca0 141 struct ins ins;
a17c4ca0 142 struct ins_operands ops;
c835e191
JO
143
144 /* This needs to be at the end. */
145 struct annotation_line al;
78f7defe
ACM
146};
147
6d9f0c2d
JO
148static inline double annotation_data__percent(struct annotation_data *data,
149 unsigned int which)
150{
151 return which < PERCENT_MAX ? data->percent[which] : -1;
152}
153
3e0d7953
JO
154static inline const char *percent_type_str(unsigned int type)
155{
156 static const char *str[PERCENT_MAX] = {
157 "local hits",
158 "global hits",
159 "local period",
160 "global period",
161 };
162
163 if (WARN_ON(type >= PERCENT_MAX))
164 return "N/A";
165
166 return str[type];
167}
168
c835e191
JO
169static inline struct disasm_line *disasm_line(struct annotation_line *al)
170{
171 return al ? container_of(al, struct disasm_line, al) : NULL;
172}
173
2eff0611
ACM
174/*
175 * Is this offset in the same function as the line it is used?
176 * asm functions jump to other functions, for instance.
177 */
178static inline bool disasm_line__has_local_offset(const struct disasm_line *dl)
fb29fa58 179{
2eff0611 180 return dl->ops.target.offset_avail && !dl->ops.target.outside;
fb29fa58
ACM
181}
182
2eff0611
ACM
183/*
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?
186 */
187bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym);
0db45bcf 188
29ed6e76 189void disasm_line__free(struct disasm_line *dl);
c4c72436
JO
190struct annotation_line *
191annotation_line__next(struct annotation_line *pos, struct list_head *head);
2f025ea0 192
c298304b
ACM
193struct annotation_write_ops {
194 bool first_line, current_entry, change_color;
195 int width;
196 void *obj;
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);
202};
203
a1e9b74c 204void annotation_line__write(struct annotation_line *al, struct annotation *notes,
4c650ddc
JO
205 struct annotation_write_ops *ops,
206 struct annotation_options *opts);
2f025ea0 207
b213eac2
ACM
208int __annotation__scnprintf_samples_period(struct annotation *notes,
209 char *bf, size_t size,
210 struct perf_evsel *evsel,
211 bool show_freq);
212
5417072b 213int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw);
5145418b 214size_t disasm__fprintf(struct list_head *head, FILE *fp);
9e4e0a9d 215void symbol__calc_percent(struct symbol *sym, struct perf_evsel *evsel);
78f7defe
ACM
216
217struct sym_hist {
8158683d 218 u64 nr_samples;
461c17f0 219 u64 period;
896bccd3 220 struct sym_hist_entry addr[0];
78f7defe
ACM
221};
222
d4957633
AK
223struct cyc_hist {
224 u64 start;
225 u64 cycles;
226 u64 cycles_aggr;
48659ebf
JY
227 u64 cycles_max;
228 u64 cycles_min;
d4957633
AK
229 u32 num;
230 u32 num_aggr;
231 u8 have_start;
232 /* 1 byte padding */
233 u16 reset;
234};
235
ce6f4fab 236/** struct annotated_source - symbols with hits have this attached as in sannotation
2f525d01 237 *
116c626b 238 * @histograms: Array of addr hit histograms per event being monitored
9132d3d9
ACM
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.
ce6f4fab 243 * @lines: If 'print_lines' is specified, per source code line percentages
29ed6e76 244 * @source: source parsed from a disassembler like objdump -dS
d4957633 245 * @cyc_hist: Average cycles per basic block
2f525d01 246 *
ce6f4fab 247 * lines is allocated, percentages calculated and all sorted by percentage
2f525d01
ACM
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
251 * returns.
252 */
ce6f4fab
ACM
253struct annotated_source {
254 struct list_head source;
36532461 255 int nr_histograms;
5ec4502d 256 size_t sizeof_sym_hist;
d4957633 257 struct cyc_hist *cycles_hist;
116c626b 258 struct sym_hist *histograms;
ce6f4fab
ACM
259};
260
261struct annotation {
262 pthread_mutex_t lock;
70fbe057 263 u64 max_coverage;
0ca693b3 264 u64 start;
16932d77 265 struct annotation_options *options;
9d6bb41d 266 struct annotation_line **offsets;
0553e83d 267 int nr_events;
6dcd57e8 268 int nr_jumps;
bc1c0f3d 269 int max_jump_sources;
1cf5f98a
ACM
270 int nr_entries;
271 int nr_asm_entries;
5bc49f61 272 u16 max_line_len;
9761e86e
ACM
273 struct {
274 u8 addr;
275 u8 jumps;
276 u8 target;
277 u8 min_addr;
278 u8 max_addr;
279 } widths;
0e83a7e9 280 bool have_cycles;
ce6f4fab 281 struct annotated_source *src;
78f7defe
ACM
282};
283
0e83a7e9
ACM
284static inline int annotation__cycles_width(struct annotation *notes)
285{
3e71fc03
JY
286 if (notes->have_cycles && notes->options->show_minmax_cycle)
287 return ANNOTATION__IPC_WIDTH + ANNOTATION__MINMAX_CYCLES_WIDTH;
288
0e83a7e9
ACM
289 return notes->have_cycles ? ANNOTATION__IPC_WIDTH + ANNOTATION__CYCLES_WIDTH : 0;
290}
291
6af612d2
ACM
292static inline int annotation__pcnt_width(struct annotation *notes)
293{
294 return (notes->options->show_total_period ? 12 : 7) * notes->nr_events;
295}
296
9b80d1f9
ACM
297static inline bool annotation_line__filter(struct annotation_line *al, struct annotation *notes)
298{
299 return notes->options->hide_src_code && al->offset == -1;
300}
5bc49f61
ACM
301
302void annotation__set_offsets(struct annotation *notes, s64 size);
f56c083b 303void annotation__compute_ipc(struct annotation *notes, size_t size);
0db45bcf 304void annotation__mark_jump_targets(struct annotation *notes, struct symbol *sym);
7232bf7a 305void annotation__update_column_widths(struct annotation *notes);
b8b0d819 306void annotation__init_column_widths(struct annotation *notes, struct symbol *sym);
f56c083b 307
e1a91a83
ACM
308static inline struct sym_hist *annotated_source__histogram(struct annotated_source *src, int idx)
309{
310 return ((void *)src->histograms) + (src->sizeof_sym_hist * idx);
311}
312
2f525d01
ACM
313static inline struct sym_hist *annotation__histogram(struct annotation *notes, int idx)
314{
e1a91a83 315 return annotated_source__histogram(notes->src, idx);
2f525d01
ACM
316}
317
78f7defe
ACM
318static inline struct annotation *symbol__annotation(struct symbol *sym)
319{
813ccd15 320 return (void *)sym - symbol_conf.priv_size;
78f7defe
ACM
321}
322
bab89f6a 323int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
e345f3bd 324 struct perf_evsel *evsel);
0f4e7a24 325
d4957633
AK
326int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
327 struct addr_map_symbol *start,
328 unsigned cycles);
329
bab89f6a 330int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
e345f3bd 331 struct perf_evsel *evsel, u64 addr);
f626adff 332
14c8dde1 333struct annotated_source *symbol__hists(struct symbol *sym, int nr_hists);
36532461 334void symbol__annotate_zero_histograms(struct symbol *sym);
78f7defe 335
c34df25b 336int symbol__annotate(struct symbol *sym, struct map *map,
d03a686e 337 struct perf_evsel *evsel, size_t privsize,
380195e2 338 struct annotation_options *options,
5449f13c 339 struct arch **parch);
ecda45bd
ACM
340int symbol__annotate2(struct symbol *sym, struct map *map,
341 struct perf_evsel *evsel,
342 struct annotation_options *options,
343 struct arch **parch);
f626adff 344
ee51d851
ACM
345enum symbol_disassemble_errno {
346 SYMBOL_ANNOTATE_ERRNO__SUCCESS = 0,
347
348 /*
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.
352 *
353 * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html
354 */
355 __SYMBOL_ANNOTATE_ERRNO__START = -10000,
356
357 SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX = __SYMBOL_ANNOTATE_ERRNO__START,
358
359 __SYMBOL_ANNOTATE_ERRNO__END,
360};
361
362int symbol__strerror_disassemble(struct symbol *sym, struct map *map,
363 int errnum, char *buf, size_t buflen);
364
db8fd07a 365int symbol__annotate_printf(struct symbol *sym, struct map *map,
982d410b
ACM
366 struct perf_evsel *evsel,
367 struct annotation_options *options);
36532461 368void symbol__annotate_zero_histogram(struct symbol *sym, int evidx);
ce6f4fab 369void symbol__annotate_decay_histogram(struct symbol *sym, int evidx);
f8eb37bd 370void annotated_source__purge(struct annotated_source *as);
78f7defe 371
4c650ddc
JO
372int map_symbol__annotation_dump(struct map_symbol *ms, struct perf_evsel *evsel,
373 struct annotation_options *opts);
d9bd7665 374
48c65bda
NK
375bool ui__has_annotation(void);
376
db8fd07a 377int symbol__tty_annotate(struct symbol *sym, struct map *map,
982d410b 378 struct perf_evsel *evsel, struct annotation_options *opts);
78f7defe 379
befd2a38 380int symbol__tty_annotate2(struct symbol *sym, struct map *map,
982d410b 381 struct perf_evsel *evsel, struct annotation_options *opts);
befd2a38 382
89fe808a 383#ifdef HAVE_SLANG_SUPPORT
db8fd07a
NK
384int symbol__tui_annotate(struct symbol *sym, struct map *map,
385 struct perf_evsel *evsel,
cd0cccba
ACM
386 struct hist_browser_timer *hbt,
387 struct annotation_options *opts);
1254b51e 388#else
1d037ca1 389static inline int symbol__tui_annotate(struct symbol *sym __maybe_unused,
db8fd07a
NK
390 struct map *map __maybe_unused,
391 struct perf_evsel *evsel __maybe_unused,
cd0cccba
ACM
392 struct hist_browser_timer *hbt __maybe_unused,
393 struct annotation_options *opts __maybe_unused)
78f7defe
ACM
394{
395 return 0;
396}
78f7defe
ACM
397#endif
398
7f0b6fde
ACM
399void annotation_config__init(void);
400
88c21190
JO
401int annotate_parse_percent_type(const struct option *opt, const char *_str,
402 int unset);
78f7defe 403#endif /* __PERF_ANNOTATE_H */