]> git.ipfire.org Git - thirdparty/linux.git/blame - tools/perf/util/annotate.h
perf annotate tui: Add browser__annotation() helper
[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;
44d1a3ed 31 } target;
7a997fe4
ACM
32 union {
33 struct {
34 char *raw;
35 char *name;
36 u64 addr;
37 } source;
38 struct {
75b49202 39 struct ins ins;
7a997fe4
ACM
40 struct ins_operands *ops;
41 } locked;
42 };
c7e6ead7
ACM
43};
44
786c1b51
ACM
45struct arch;
46
4f9d0325 47struct ins_ops {
c46219ac 48 void (*free)(struct ins_operands *ops);
786c1b51 49 int (*parse)(struct arch *arch, struct ins_operands *ops, struct map *map);
28548d78 50 int (*scnprintf)(struct ins *ins, char *bf, size_t size,
5417072b 51 struct ins_operands *ops);
4f9d0325
ACM
52};
53
4f9d0325 54bool ins__is_jump(const struct ins *ins);
d86b0597 55bool ins__is_call(const struct ins *ins);
6ef94929 56bool ins__is_ret(const struct ins *ins);
7e63a13a 57bool ins__is_lock(const struct ins *ins);
5417072b 58int ins__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops);
69fb09f6 59bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2);
4f9d0325 60
c426e584
ACM
61#define ANNOTATION__IPC_WIDTH 6
62#define ANNOTATION__CYCLES_WIDTH 6
63
98bc80b0
ACM
64struct annotation_options {
65 bool hide_src_code,
66 use_offset,
67 jump_arrows,
68 show_linenr,
69 show_nr_jumps,
70 show_nr_samples,
71 show_total_period;
72};
73
e64aa75b
NK
74struct annotation;
75
7e304557
JO
76struct sym_hist_entry {
77 u64 nr_samples;
78 u64 period;
79};
80
81struct annotation_data {
82 double percent;
8b4c74dc 83 double percent_sum;
7e304557
JO
84 struct sym_hist_entry he;
85};
86
a17c4ca0
JO
87struct annotation_line {
88 struct list_head node;
5b12adc8 89 struct rb_node rb_node;
d5490b96
JO
90 s64 offset;
91 char *line;
92 int line_nr;
37236d5e
JO
93 float ipc;
94 u64 cycles;
c835e191 95 size_t privsize;
8b4c74dc 96 char *path;
7e304557
JO
97 int samples_nr;
98 struct annotation_data samples[0];
a17c4ca0
JO
99};
100
29ed6e76 101struct disasm_line {
a17c4ca0 102 struct ins ins;
a17c4ca0 103 struct ins_operands ops;
c835e191
JO
104
105 /* This needs to be at the end. */
106 struct annotation_line al;
78f7defe
ACM
107};
108
c835e191
JO
109static inline struct disasm_line *disasm_line(struct annotation_line *al)
110{
111 return al ? container_of(al, struct disasm_line, al) : NULL;
112}
113
fb29fa58
ACM
114static inline bool disasm_line__has_offset(const struct disasm_line *dl)
115{
e216874c 116 return dl->ops.target.offset_avail;
fb29fa58
ACM
117}
118
29ed6e76 119void disasm_line__free(struct disasm_line *dl);
c4c72436
JO
120struct annotation_line *
121annotation_line__next(struct annotation_line *pos, struct list_head *head);
5417072b 122int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw);
5145418b 123size_t disasm__fprintf(struct list_head *head, FILE *fp);
9e4e0a9d 124void symbol__calc_percent(struct symbol *sym, struct perf_evsel *evsel);
78f7defe
ACM
125
126struct sym_hist {
8158683d 127 u64 nr_samples;
461c17f0 128 u64 period;
896bccd3 129 struct sym_hist_entry addr[0];
78f7defe
ACM
130};
131
d4957633
AK
132struct cyc_hist {
133 u64 start;
134 u64 cycles;
135 u64 cycles_aggr;
136 u32 num;
137 u32 num_aggr;
138 u8 have_start;
139 /* 1 byte padding */
140 u16 reset;
141};
142
ce6f4fab 143/** struct annotated_source - symbols with hits have this attached as in sannotation
2f525d01
ACM
144 *
145 * @histogram: Array of addr hit histograms per event being monitored
ce6f4fab 146 * @lines: If 'print_lines' is specified, per source code line percentages
29ed6e76 147 * @source: source parsed from a disassembler like objdump -dS
d4957633 148 * @cyc_hist: Average cycles per basic block
2f525d01 149 *
ce6f4fab 150 * lines is allocated, percentages calculated and all sorted by percentage
2f525d01
ACM
151 * when the annotation is about to be presented, so the percentages are for
152 * one of the entries in the histogram array, i.e. for the event/counter being
153 * presented. It is deallocated right after symbol__{tui,tty,etc}_annotate
154 * returns.
155 */
ce6f4fab
ACM
156struct annotated_source {
157 struct list_head source;
36532461 158 int nr_histograms;
5ec4502d 159 size_t sizeof_sym_hist;
d4957633 160 struct cyc_hist *cycles_hist;
ce6f4fab
ACM
161 struct sym_hist histograms[0];
162};
163
164struct annotation {
165 pthread_mutex_t lock;
70fbe057 166 u64 max_coverage;
16932d77 167 struct annotation_options *options;
9d6bb41d 168 struct annotation_line **offsets;
0553e83d 169 int nr_events;
0e83a7e9 170 bool have_cycles;
ce6f4fab 171 struct annotated_source *src;
78f7defe
ACM
172};
173
0e83a7e9
ACM
174static inline int annotation__cycles_width(struct annotation *notes)
175{
176 return notes->have_cycles ? ANNOTATION__IPC_WIDTH + ANNOTATION__CYCLES_WIDTH : 0;
177}
178
6af612d2
ACM
179static inline int annotation__pcnt_width(struct annotation *notes)
180{
181 return (notes->options->show_total_period ? 12 : 7) * notes->nr_events;
182}
183
f56c083b
ACM
184void annotation__compute_ipc(struct annotation *notes, size_t size);
185
2f525d01
ACM
186static inline struct sym_hist *annotation__histogram(struct annotation *notes, int idx)
187{
ce6f4fab
ACM
188 return (((void *)&notes->src->histograms) +
189 (notes->src->sizeof_sym_hist * idx));
2f525d01
ACM
190}
191
78f7defe
ACM
192static inline struct annotation *symbol__annotation(struct symbol *sym)
193{
813ccd15 194 return (void *)sym - symbol_conf.priv_size;
78f7defe
ACM
195}
196
bab89f6a
TS
197int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
198 int evidx);
0f4e7a24 199
d4957633
AK
200int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
201 struct addr_map_symbol *start,
202 unsigned cycles);
203
bab89f6a
TS
204int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
205 int evidx, u64 addr);
f626adff 206
d04b35f8 207int symbol__alloc_hist(struct symbol *sym);
36532461 208void symbol__annotate_zero_histograms(struct symbol *sym);
78f7defe 209
c34df25b 210int symbol__annotate(struct symbol *sym, struct map *map,
d03a686e 211 struct perf_evsel *evsel, size_t privsize,
5449f13c 212 struct arch **parch);
f626adff 213
ee51d851
ACM
214enum symbol_disassemble_errno {
215 SYMBOL_ANNOTATE_ERRNO__SUCCESS = 0,
216
217 /*
218 * Choose an arbitrary negative big number not to clash with standard
219 * errno since SUS requires the errno has distinct positive values.
220 * See 'Issue 6' in the link below.
221 *
222 * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html
223 */
224 __SYMBOL_ANNOTATE_ERRNO__START = -10000,
225
226 SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX = __SYMBOL_ANNOTATE_ERRNO__START,
227
228 __SYMBOL_ANNOTATE_ERRNO__END,
229};
230
231int symbol__strerror_disassemble(struct symbol *sym, struct map *map,
232 int errnum, char *buf, size_t buflen);
233
db8fd07a
NK
234int symbol__annotate_printf(struct symbol *sym, struct map *map,
235 struct perf_evsel *evsel, bool full_paths,
236 int min_pcnt, int max_lines, int context);
36532461 237void symbol__annotate_zero_histogram(struct symbol *sym, int evidx);
ce6f4fab 238void symbol__annotate_decay_histogram(struct symbol *sym, int evidx);
f8eb37bd 239void annotated_source__purge(struct annotated_source *as);
78f7defe 240
48c65bda
NK
241bool ui__has_annotation(void);
242
db8fd07a
NK
243int symbol__tty_annotate(struct symbol *sym, struct map *map,
244 struct perf_evsel *evsel, bool print_lines,
245 bool full_paths, int min_pcnt, int max_lines);
78f7defe 246
89fe808a 247#ifdef HAVE_SLANG_SUPPORT
db8fd07a
NK
248int symbol__tui_annotate(struct symbol *sym, struct map *map,
249 struct perf_evsel *evsel,
9783adf7 250 struct hist_browser_timer *hbt);
1254b51e 251#else
1d037ca1 252static inline int symbol__tui_annotate(struct symbol *sym __maybe_unused,
db8fd07a
NK
253 struct map *map __maybe_unused,
254 struct perf_evsel *evsel __maybe_unused,
255 struct hist_browser_timer *hbt
256 __maybe_unused)
78f7defe
ACM
257{
258 return 0;
259}
78f7defe
ACM
260#endif
261
f69b64f7
AK
262extern const char *disassembler_style;
263
78f7defe 264#endif /* __PERF_ANNOTATE_H */