1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __PERF_ANNOTATE_H 3 #define __PERF_ANNOTATE_H 4 5 #include <stdbool.h> 6 #include <stdint.h> 7 #include <linux/types.h> 8 #include "symbol.h" 9 #include "hist.h" 10 #include "sort.h" 11 #include <linux/list.h> 12 #include <linux/rbtree.h> 13 #include <pthread.h> 14 #include <asm/bug.h> 15 16 struct ins_ops; 17 18 struct ins { 19 const char *name; 20 struct ins_ops *ops; 21 }; 22 23 struct ins_operands { 24 char *raw; 25 char *raw_comment; 26 struct { 27 char *raw; 28 char *name; 29 struct symbol *sym; 30 u64 addr; 31 s64 offset; 32 bool offset_avail; 33 bool outside; 34 } target; 35 union { 36 struct { 37 char *raw; 38 char *name; 39 u64 addr; 40 } source; 41 struct { 42 struct ins ins; 43 struct ins_operands *ops; 44 } locked; 45 }; 46 }; 47 48 struct arch; 49 50 struct ins_ops { 51 void (*free)(struct ins_operands *ops); 52 int (*parse)(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms); 53 int (*scnprintf)(struct ins *ins, char *bf, size_t size, 54 struct ins_operands *ops); 55 }; 56 57 bool ins__is_jump(const struct ins *ins); 58 bool ins__is_call(const struct ins *ins); 59 bool ins__is_ret(const struct ins *ins); 60 bool ins__is_lock(const struct ins *ins); 61 int ins__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops); 62 bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2); 63 64 #define ANNOTATION__IPC_WIDTH 6 65 #define ANNOTATION__CYCLES_WIDTH 6 66 #define ANNOTATION__MINMAX_CYCLES_WIDTH 19 67 68 struct annotation_options { 69 bool hide_src_code, 70 use_offset, 71 jump_arrows, 72 print_lines, 73 full_path, 74 show_linenr, 75 show_nr_jumps, 76 show_nr_samples, 77 show_total_period, 78 show_minmax_cycle, 79 show_asm_raw, 80 annotate_src; 81 u8 offset_level; 82 int min_pcnt; 83 int max_lines; 84 int context; 85 const char *objdump_path; 86 const char *disassembler_style; 87 unsigned int percent_type; 88 }; 89 90 enum { 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 98 extern struct annotation_options annotation__default_options; 99 100 struct annotation; 101 102 struct sym_hist_entry { 103 u64 nr_samples; 104 u64 period; 105 }; 106 107 enum { 108 PERCENT_HITS_LOCAL, 109 PERCENT_HITS_GLOBAL, 110 PERCENT_PERIOD_LOCAL, 111 PERCENT_PERIOD_GLOBAL, 112 PERCENT_MAX, 113 }; 114 115 struct annotation_data { 116 double percent[PERCENT_MAX]; 117 double percent_sum; 118 struct sym_hist_entry he; 119 }; 120 121 struct annotation_line { 122 struct list_head node; 123 struct rb_node rb_node; 124 s64 offset; 125 char *line; 126 int line_nr; 127 int jump_sources; 128 float ipc; 129 u64 cycles; 130 u64 cycles_max; 131 u64 cycles_min; 132 size_t privsize; 133 char *path; 134 u32 idx; 135 int idx_asm; 136 int data_nr; 137 struct annotation_data data[0]; 138 }; 139 140 struct disasm_line { 141 struct ins ins; 142 struct ins_operands ops; 143 144 /* This needs to be at the end. */ 145 struct annotation_line al; 146 }; 147 148 static 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 154 static 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 169 static inline struct disasm_line *disasm_line(struct annotation_line *al) 170 { 171 return al ? container_of(al, struct disasm_line, al) : NULL; 172 } 173 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 */ 178 static inline bool disasm_line__has_local_offset(const struct disasm_line *dl) 179 { 180 return dl->ops.target.offset_avail && !dl->ops.target.outside; 181 } 182 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 */ 187 bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym); 188 189 void disasm_line__free(struct disasm_line *dl); 190 struct annotation_line * 191 annotation_line__next(struct annotation_line *pos, struct list_head *head); 192 193 struct 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 204 void annotation_line__write(struct annotation_line *al, struct annotation *notes, 205 struct annotation_write_ops *ops, 206 struct annotation_options *opts); 207 208 int __annotation__scnprintf_samples_period(struct annotation *notes, 209 char *bf, size_t size, 210 struct perf_evsel *evsel, 211 bool show_freq); 212 213 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw); 214 size_t disasm__fprintf(struct list_head *head, FILE *fp); 215 void symbol__calc_percent(struct symbol *sym, struct perf_evsel *evsel); 216 217 struct sym_hist { 218 u64 nr_samples; 219 u64 period; 220 struct sym_hist_entry addr[0]; 221 }; 222 223 struct cyc_hist { 224 u64 start; 225 u64 cycles; 226 u64 cycles_aggr; 227 u64 cycles_max; 228 u64 cycles_min; 229 u32 num; 230 u32 num_aggr; 231 u8 have_start; 232 /* 1 byte padding */ 233 u16 reset; 234 }; 235 236 /** struct annotated_source - symbols with hits have this attached as in sannotation 237 * 238 * @histograms: Array of addr hit histograms per event being monitored 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. 243 * @lines: If 'print_lines' is specified, per source code line percentages 244 * @source: source parsed from a disassembler like objdump -dS 245 * @cyc_hist: Average cycles per basic block 246 * 247 * lines is allocated, percentages calculated and all sorted by percentage 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 */ 253 struct annotated_source { 254 struct list_head source; 255 int nr_histograms; 256 size_t sizeof_sym_hist; 257 struct cyc_hist *cycles_hist; 258 struct sym_hist *histograms; 259 }; 260 261 struct annotation { 262 pthread_mutex_t lock; 263 u64 max_coverage; 264 u64 start; 265 struct annotation_options *options; 266 struct annotation_line **offsets; 267 int nr_events; 268 int nr_jumps; 269 int max_jump_sources; 270 int nr_entries; 271 int nr_asm_entries; 272 u16 max_line_len; 273 struct { 274 u8 addr; 275 u8 jumps; 276 u8 target; 277 u8 min_addr; 278 u8 max_addr; 279 } widths; 280 bool have_cycles; 281 struct annotated_source *src; 282 }; 283 284 static inline int annotation__cycles_width(struct annotation *notes) 285 { 286 if (notes->have_cycles && notes->options->show_minmax_cycle) 287 return ANNOTATION__IPC_WIDTH + ANNOTATION__MINMAX_CYCLES_WIDTH; 288 289 return notes->have_cycles ? ANNOTATION__IPC_WIDTH + ANNOTATION__CYCLES_WIDTH : 0; 290 } 291 292 static inline int annotation__pcnt_width(struct annotation *notes) 293 { 294 return (notes->options->show_total_period ? 12 : 7) * notes->nr_events; 295 } 296 297 static inline bool annotation_line__filter(struct annotation_line *al, struct annotation *notes) 298 { 299 return notes->options->hide_src_code && al->offset == -1; 300 } 301 302 void annotation__set_offsets(struct annotation *notes, s64 size); 303 void annotation__compute_ipc(struct annotation *notes, size_t size); 304 void annotation__mark_jump_targets(struct annotation *notes, struct symbol *sym); 305 void annotation__update_column_widths(struct annotation *notes); 306 void annotation__init_column_widths(struct annotation *notes, struct symbol *sym); 307 308 static 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 313 static inline struct sym_hist *annotation__histogram(struct annotation *notes, int idx) 314 { 315 return annotated_source__histogram(notes->src, idx); 316 } 317 318 static inline struct annotation *symbol__annotation(struct symbol *sym) 319 { 320 return (void *)sym - symbol_conf.priv_size; 321 } 322 323 int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample, 324 struct perf_evsel *evsel); 325 326 int addr_map_symbol__account_cycles(struct addr_map_symbol *ams, 327 struct addr_map_symbol *start, 328 unsigned cycles); 329 330 int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample, 331 struct perf_evsel *evsel, u64 addr); 332 333 struct annotated_source *symbol__hists(struct symbol *sym, int nr_hists); 334 void symbol__annotate_zero_histograms(struct symbol *sym); 335 336 int symbol__annotate(struct symbol *sym, struct map *map, 337 struct perf_evsel *evsel, size_t privsize, 338 struct annotation_options *options, 339 struct arch **parch); 340 int symbol__annotate2(struct symbol *sym, struct map *map, 341 struct perf_evsel *evsel, 342 struct annotation_options *options, 343 struct arch **parch); 344 345 enum 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 362 int symbol__strerror_disassemble(struct symbol *sym, struct map *map, 363 int errnum, char *buf, size_t buflen); 364 365 int symbol__annotate_printf(struct symbol *sym, struct map *map, 366 struct perf_evsel *evsel, 367 struct annotation_options *options); 368 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx); 369 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx); 370 void annotated_source__purge(struct annotated_source *as); 371 372 int map_symbol__annotation_dump(struct map_symbol *ms, struct perf_evsel *evsel, 373 struct annotation_options *opts); 374 375 bool ui__has_annotation(void); 376 377 int symbol__tty_annotate(struct symbol *sym, struct map *map, 378 struct perf_evsel *evsel, struct annotation_options *opts); 379 380 int symbol__tty_annotate2(struct symbol *sym, struct map *map, 381 struct perf_evsel *evsel, struct annotation_options *opts); 382 383 #ifdef HAVE_SLANG_SUPPORT 384 int symbol__tui_annotate(struct symbol *sym, struct map *map, 385 struct perf_evsel *evsel, 386 struct hist_browser_timer *hbt, 387 struct annotation_options *opts); 388 #else 389 static inline int symbol__tui_annotate(struct symbol *sym __maybe_unused, 390 struct map *map __maybe_unused, 391 struct perf_evsel *evsel __maybe_unused, 392 struct hist_browser_timer *hbt __maybe_unused, 393 struct annotation_options *opts __maybe_unused) 394 { 395 return 0; 396 } 397 #endif 398 399 void annotation_config__init(void); 400 401 int annotate_parse_percent_type(const struct option *opt, const char *_str, 402 int unset); 403 #endif /* __PERF_ANNOTATE_H */ 404