xref: /openbmc/linux/tools/perf/util/annotate.h (revision 0317cd52)
1 #ifndef __PERF_ANNOTATE_H
2 #define __PERF_ANNOTATE_H
3 
4 #include <stdbool.h>
5 #include <stdint.h>
6 #include <linux/types.h>
7 #include "symbol.h"
8 #include "hist.h"
9 #include "sort.h"
10 #include <linux/list.h>
11 #include <linux/rbtree.h>
12 #include <pthread.h>
13 
14 struct ins;
15 
16 struct ins_operands {
17 	char	*raw;
18 	struct {
19 		char	*raw;
20 		char	*name;
21 		u64	addr;
22 		u64	offset;
23 	} target;
24 	union {
25 		struct {
26 			char	*raw;
27 			char	*name;
28 			u64	addr;
29 		} source;
30 		struct {
31 			struct ins *ins;
32 			struct ins_operands *ops;
33 		} locked;
34 	};
35 };
36 
37 struct ins_ops {
38 	void (*free)(struct ins_operands *ops);
39 	int (*parse)(struct ins_operands *ops);
40 	int (*scnprintf)(struct ins *ins, char *bf, size_t size,
41 			 struct ins_operands *ops);
42 };
43 
44 struct ins {
45 	const char     *name;
46 	struct ins_ops *ops;
47 };
48 
49 bool ins__is_jump(const struct ins *ins);
50 bool ins__is_call(const struct ins *ins);
51 bool ins__is_ret(const struct ins *ins);
52 int ins__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops);
53 
54 struct annotation;
55 
56 struct disasm_line {
57 	struct list_head    node;
58 	s64		    offset;
59 	char		    *line;
60 	char		    *name;
61 	struct ins	    *ins;
62 	int		    line_nr;
63 	float		    ipc;
64 	u64		    cycles;
65 	struct ins_operands ops;
66 };
67 
68 static inline bool disasm_line__has_offset(const struct disasm_line *dl)
69 {
70 	return dl->ops.target.offset != UINT64_MAX;
71 }
72 
73 void disasm_line__free(struct disasm_line *dl);
74 struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos);
75 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw);
76 size_t disasm__fprintf(struct list_head *head, FILE *fp);
77 double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset,
78 			    s64 end, const char **path, u64 *nr_samples);
79 
80 struct sym_hist {
81 	u64		sum;
82 	u64		addr[0];
83 };
84 
85 struct cyc_hist {
86 	u64	start;
87 	u64	cycles;
88 	u64	cycles_aggr;
89 	u32	num;
90 	u32	num_aggr;
91 	u8	have_start;
92 	/* 1 byte padding */
93 	u16	reset;
94 };
95 
96 struct source_line_samples {
97 	double		percent;
98 	double		percent_sum;
99 	double          nr;
100 };
101 
102 struct source_line {
103 	struct rb_node	node;
104 	char		*path;
105 	int		nr_pcnt;
106 	struct source_line_samples samples[1];
107 };
108 
109 /** struct annotated_source - symbols with hits have this attached as in sannotation
110  *
111  * @histogram: Array of addr hit histograms per event being monitored
112  * @lines: If 'print_lines' is specified, per source code line percentages
113  * @source: source parsed from a disassembler like objdump -dS
114  * @cyc_hist: Average cycles per basic block
115  *
116  * lines is allocated, percentages calculated and all sorted by percentage
117  * when the annotation is about to be presented, so the percentages are for
118  * one of the entries in the histogram array, i.e. for the event/counter being
119  * presented. It is deallocated right after symbol__{tui,tty,etc}_annotate
120  * returns.
121  */
122 struct annotated_source {
123 	struct list_head   source;
124 	struct source_line *lines;
125 	int    		   nr_histograms;
126 	size_t		   sizeof_sym_hist;
127 	struct cyc_hist	   *cycles_hist;
128 	struct sym_hist	   histograms[0];
129 };
130 
131 struct annotation {
132 	pthread_mutex_t		lock;
133 	struct annotated_source *src;
134 };
135 
136 static inline struct sym_hist *annotation__histogram(struct annotation *notes, int idx)
137 {
138 	return (((void *)&notes->src->histograms) +
139 	 	(notes->src->sizeof_sym_hist * idx));
140 }
141 
142 static inline struct annotation *symbol__annotation(struct symbol *sym)
143 {
144 	return (void *)sym - symbol_conf.priv_size;
145 }
146 
147 int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, int evidx);
148 
149 int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
150 				    struct addr_map_symbol *start,
151 				    unsigned cycles);
152 
153 int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 addr);
154 
155 int symbol__alloc_hist(struct symbol *sym);
156 void symbol__annotate_zero_histograms(struct symbol *sym);
157 
158 int symbol__disassemble(struct symbol *sym, struct map *map, size_t privsize);
159 
160 enum symbol_disassemble_errno {
161 	SYMBOL_ANNOTATE_ERRNO__SUCCESS		= 0,
162 
163 	/*
164 	 * Choose an arbitrary negative big number not to clash with standard
165 	 * errno since SUS requires the errno has distinct positive values.
166 	 * See 'Issue 6' in the link below.
167 	 *
168 	 * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html
169 	 */
170 	__SYMBOL_ANNOTATE_ERRNO__START		= -10000,
171 
172 	SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX	= __SYMBOL_ANNOTATE_ERRNO__START,
173 
174 	__SYMBOL_ANNOTATE_ERRNO__END,
175 };
176 
177 int symbol__strerror_disassemble(struct symbol *sym, struct map *map,
178 				 int errnum, char *buf, size_t buflen);
179 
180 int symbol__annotate_init(struct map *map, struct symbol *sym);
181 int symbol__annotate_printf(struct symbol *sym, struct map *map,
182 			    struct perf_evsel *evsel, bool full_paths,
183 			    int min_pcnt, int max_lines, int context);
184 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx);
185 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx);
186 void disasm__purge(struct list_head *head);
187 
188 bool ui__has_annotation(void);
189 
190 int symbol__tty_annotate(struct symbol *sym, struct map *map,
191 			 struct perf_evsel *evsel, bool print_lines,
192 			 bool full_paths, int min_pcnt, int max_lines);
193 
194 #ifdef HAVE_SLANG_SUPPORT
195 int symbol__tui_annotate(struct symbol *sym, struct map *map,
196 			 struct perf_evsel *evsel,
197 			 struct hist_browser_timer *hbt);
198 #else
199 static inline int symbol__tui_annotate(struct symbol *sym __maybe_unused,
200 				struct map *map __maybe_unused,
201 				struct perf_evsel *evsel  __maybe_unused,
202 				struct hist_browser_timer *hbt
203 				__maybe_unused)
204 {
205 	return 0;
206 }
207 #endif
208 
209 extern const char	*disassembler_style;
210 
211 #endif	/* __PERF_ANNOTATE_H */
212