blob: 17cd5d274fe500b64b07387e3ad639adbcc950c3 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02002#ifndef __PERF_ANNOTATE_H
3#define __PERF_ANNOTATE_H
4
5#include <stdbool.h>
Arnaldo Carvalho de Melofb29fa52012-04-25 14:16:03 -03006#include <stdint.h>
Borislav Petkovd944c4e2014-04-25 21:31:02 +02007#include <linux/types.h>
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02008#include "symbol.h"
Namhyung Kim9783adf2012-11-02 14:50:05 +09009#include "hist.h"
Namhyung Kim2b676bf2013-02-07 18:02:08 +090010#include "sort.h"
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020011#include <linux/list.h>
12#include <linux/rbtree.h>
Irina Tirdea27683dc2012-09-08 03:43:19 +030013#include <pthread.h>
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020014
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -030015struct ins_ops;
16
17struct ins {
18 const char *name;
19 struct ins_ops *ops;
20};
Arnaldo Carvalho de Melo28548d72012-04-19 10:16:27 -030021
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -030022struct ins_operands {
23 char *raw;
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -030024 struct {
Arnaldo Carvalho de Melo6de783b2012-05-11 16:48:49 -030025 char *raw;
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -030026 char *name;
Arnaldo Carvalho de Melo696703a2018-03-02 11:59:36 -030027 struct symbol *sym;
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -030028 u64 addr;
Ravi Bangoriae2168742016-12-05 21:26:47 +053029 s64 offset;
30 bool offset_avail;
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -030031 } target;
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -030032 union {
33 struct {
34 char *raw;
35 char *name;
36 u64 addr;
37 } source;
38 struct {
Arnaldo Carvalho de Melo75b49202016-11-24 11:16:06 -030039 struct ins ins;
Arnaldo Carvalho de Melo7a997fe2012-05-12 13:15:34 -030040 struct ins_operands *ops;
41 } locked;
42 };
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -030043};
44
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -030045struct arch;
46
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -030047struct ins_ops {
Arnaldo Carvalho de Meloc46219ac2012-05-12 13:26:20 -030048 void (*free)(struct ins_operands *ops);
Arnaldo Carvalho de Melo786c1b52016-11-16 15:39:50 -030049 int (*parse)(struct arch *arch, struct ins_operands *ops, struct map *map);
Arnaldo Carvalho de Melo28548d72012-04-19 10:16:27 -030050 int (*scnprintf)(struct ins *ins, char *bf, size_t size,
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -030051 struct ins_operands *ops);
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -030052};
53
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -030054bool ins__is_jump(const struct ins *ins);
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -030055bool ins__is_call(const struct ins *ins);
Naveen N. Rao6ef94922016-06-24 17:23:58 +053056bool ins__is_ret(const struct ins *ins);
Jin Yao7e63a132017-07-07 13:06:35 +080057bool ins__is_lock(const struct ins *ins);
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -030058int ins__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops);
Jin Yao69fb09f2017-07-07 13:06:34 +080059bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2);
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -030060
Arnaldo Carvalho de Meloc426e582018-03-15 10:26:17 -030061#define ANNOTATION__IPC_WIDTH 6
62#define ANNOTATION__CYCLES_WIDTH 6
63
Arnaldo Carvalho de Melo98bc80b2018-03-15 10:03:34 -030064struct 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
Namhyung Kime64aa752013-03-05 14:53:30 +090074struct annotation;
75
Jiri Olsa7e304552017-10-11 17:01:39 +020076struct sym_hist_entry {
77 u64 nr_samples;
78 u64 period;
79};
80
81struct annotation_data {
82 double percent;
Jiri Olsa8b4c74d2017-10-11 17:01:41 +020083 double percent_sum;
Jiri Olsa7e304552017-10-11 17:01:39 +020084 struct sym_hist_entry he;
85};
86
Jiri Olsaa17c4ca2017-10-11 17:01:25 +020087struct annotation_line {
88 struct list_head node;
Jiri Olsa5b12adc2017-10-11 17:01:36 +020089 struct rb_node rb_node;
Jiri Olsad5490b92017-10-11 17:01:26 +020090 s64 offset;
91 char *line;
92 int line_nr;
Jiri Olsa37236d52017-10-11 17:01:27 +020093 float ipc;
94 u64 cycles;
Jiri Olsac835e192017-10-11 17:01:37 +020095 size_t privsize;
Jiri Olsa8b4c74d2017-10-11 17:01:41 +020096 char *path;
Jiri Olsa7e304552017-10-11 17:01:39 +020097 int samples_nr;
98 struct annotation_data samples[0];
Jiri Olsaa17c4ca2017-10-11 17:01:25 +020099};
100
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300101struct disasm_line {
Jiri Olsaa17c4ca2017-10-11 17:01:25 +0200102 struct ins ins;
Jiri Olsaa17c4ca2017-10-11 17:01:25 +0200103 struct ins_operands ops;
Jiri Olsac835e192017-10-11 17:01:37 +0200104
105 /* This needs to be at the end. */
106 struct annotation_line al;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200107};
108
Jiri Olsac835e192017-10-11 17:01:37 +0200109static inline struct disasm_line *disasm_line(struct annotation_line *al)
110{
111 return al ? container_of(al, struct disasm_line, al) : NULL;
112}
113
Arnaldo Carvalho de Melofb29fa52012-04-25 14:16:03 -0300114static inline bool disasm_line__has_offset(const struct disasm_line *dl)
115{
Ravi Bangoriae2168742016-12-05 21:26:47 +0530116 return dl->ops.target.offset_avail;
Arnaldo Carvalho de Melofb29fa52012-04-25 14:16:03 -0300117}
118
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300119void disasm_line__free(struct disasm_line *dl);
Jiri Olsac4c72432017-10-11 17:01:34 +0200120struct annotation_line *
121annotation_line__next(struct annotation_line *pos, struct list_head *head);
Arnaldo Carvalho de Melo54170722012-05-07 18:54:16 -0300122int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw);
Arnaldo Carvalho de Melo51454182012-04-15 15:52:18 -0300123size_t disasm__fprintf(struct list_head *head, FILE *fp);
Jiri Olsa9e4e0a92017-11-15 12:05:59 +0100124void symbol__calc_percent(struct symbol *sym, struct perf_evsel *evsel);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200125
126struct sym_hist {
Taeung Song8158683d2017-07-20 06:36:51 +0900127 u64 nr_samples;
Taeung Song461c17f2017-07-20 17:18:05 -0300128 u64 period;
Taeung Song896bccd2017-07-20 06:36:45 +0900129 struct sym_hist_entry addr[0];
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200130};
131
Andi Kleend4957632015-07-18 08:24:48 -0700132struct 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
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200143/** struct annotated_source - symbols with hits have this attached as in sannotation
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200144 *
145 * @histogram: Array of addr hit histograms per event being monitored
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200146 * @lines: If 'print_lines' is specified, per source code line percentages
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300147 * @source: source parsed from a disassembler like objdump -dS
Andi Kleend4957632015-07-18 08:24:48 -0700148 * @cyc_hist: Average cycles per basic block
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200149 *
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200150 * lines is allocated, percentages calculated and all sorted by percentage
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200151 * 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 */
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200156struct annotated_source {
157 struct list_head source;
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200158 int nr_histograms;
Jiri Olsa5ec45022015-10-05 20:06:03 +0200159 size_t sizeof_sym_hist;
Andi Kleend4957632015-07-18 08:24:48 -0700160 struct cyc_hist *cycles_hist;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200161 struct sym_hist histograms[0];
162};
163
164struct annotation {
165 pthread_mutex_t lock;
Peter Zijlstra70fbe052016-09-05 16:08:12 -0300166 u64 max_coverage;
Arnaldo Carvalho de Melo16932d72018-03-15 12:33:56 -0300167 struct annotation_options *options;
Arnaldo Carvalho de Melo9d6bb412018-03-15 10:47:54 -0300168 struct annotation_line **offsets;
Arnaldo Carvalho de Melo0553e832018-03-15 11:52:32 -0300169 int nr_events;
Arnaldo Carvalho de Melo0e83a7e2018-03-15 10:35:04 -0300170 bool have_cycles;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200171 struct annotated_source *src;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200172};
173
Arnaldo Carvalho de Melo0e83a7e2018-03-15 10:35:04 -0300174static inline int annotation__cycles_width(struct annotation *notes)
175{
176 return notes->have_cycles ? ANNOTATION__IPC_WIDTH + ANNOTATION__CYCLES_WIDTH : 0;
177}
178
Arnaldo Carvalho de Melo6af612d2018-03-15 12:41:39 -0300179static inline int annotation__pcnt_width(struct annotation *notes)
180{
181 return (notes->options->show_total_period ? 12 : 7) * notes->nr_events;
182}
183
Arnaldo Carvalho de Melof56c0832018-03-15 11:46:23 -0300184void annotation__compute_ipc(struct annotation *notes, size_t size);
185
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200186static inline struct sym_hist *annotation__histogram(struct annotation *notes, int idx)
187{
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200188 return (((void *)&notes->src->histograms) +
189 (notes->src->sizeof_sym_hist * idx));
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200190}
191
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200192static inline struct annotation *symbol__annotation(struct symbol *sym)
193{
Namhyung Kim813ccd12015-01-14 20:18:05 +0900194 return (void *)sym - symbol_conf.priv_size;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200195}
196
Taeung Songbab89f62017-07-20 16:28:53 -0300197int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
198 int evidx);
Arnaldo Carvalho de Melo0f4e7a22013-12-18 16:48:29 -0300199
Andi Kleend4957632015-07-18 08:24:48 -0700200int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
201 struct addr_map_symbol *start,
202 unsigned cycles);
203
Taeung Songbab89f62017-07-20 16:28:53 -0300204int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
205 int evidx, u64 addr);
Arnaldo Carvalho de Melof626adf2013-12-18 17:10:15 -0300206
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200207int symbol__alloc_hist(struct symbol *sym);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200208void symbol__annotate_zero_histograms(struct symbol *sym);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200209
Jiri Olsac34df252017-10-11 17:01:28 +0200210int symbol__annotate(struct symbol *sym, struct map *map,
Jiri Olsad03a6862017-10-11 17:01:33 +0200211 struct perf_evsel *evsel, size_t privsize,
Arnaldo Carvalho de Melo5449f132017-12-11 12:46:11 -0300212 struct arch **parch);
Arnaldo Carvalho de Melof626adf2013-12-18 17:10:15 -0300213
Arnaldo Carvalho de Meloee51d852016-07-29 16:27:18 -0300214enum 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
Namhyung Kimdb8fd072013-03-05 14:53:21 +0900234int 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);
Arnaldo Carvalho de Melo36532462011-02-06 14:54:44 -0200237void symbol__annotate_zero_histogram(struct symbol *sym, int evidx);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200238void symbol__annotate_decay_histogram(struct symbol *sym, int evidx);
Jiri Olsaf8eb37b2017-10-11 17:01:38 +0200239void annotated_source__purge(struct annotated_source *as);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200240
Namhyung Kim48c65bd2014-02-20 10:32:53 +0900241bool ui__has_annotation(void);
242
Namhyung Kimdb8fd072013-03-05 14:53:21 +0900243int 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);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200246
Ingo Molnar89fe8082013-09-30 12:07:11 +0200247#ifdef HAVE_SLANG_SUPPORT
Namhyung Kimdb8fd072013-03-05 14:53:21 +0900248int symbol__tui_annotate(struct symbol *sym, struct map *map,
249 struct perf_evsel *evsel,
Namhyung Kim9783adf2012-11-02 14:50:05 +0900250 struct hist_browser_timer *hbt);
Namhyung Kim1254b51e2012-09-28 18:32:02 +0900251#else
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300252static inline int symbol__tui_annotate(struct symbol *sym __maybe_unused,
Namhyung Kimdb8fd072013-03-05 14:53:21 +0900253 struct map *map __maybe_unused,
254 struct perf_evsel *evsel __maybe_unused,
255 struct hist_browser_timer *hbt
256 __maybe_unused)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200257{
258 return 0;
259}
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200260#endif
261
Andi Kleenf69b64f2011-09-15 14:31:41 -0700262extern const char *disassembler_style;
263
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200264#endif /* __PERF_ANNOTATE_H */