annotate.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __PERF_ANNOTATE_H
  3. #define __PERF_ANNOTATE_H
  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. struct ins_ops;
  14. struct ins {
  15. const char *name;
  16. struct ins_ops *ops;
  17. };
  18. struct ins_operands {
  19. char *raw;
  20. struct {
  21. char *raw;
  22. char *name;
  23. struct symbol *sym;
  24. u64 addr;
  25. s64 offset;
  26. bool offset_avail;
  27. bool outside;
  28. } target;
  29. union {
  30. struct {
  31. char *raw;
  32. char *name;
  33. u64 addr;
  34. } source;
  35. struct {
  36. struct ins ins;
  37. struct ins_operands *ops;
  38. } locked;
  39. };
  40. };
  41. struct arch;
  42. struct ins_ops {
  43. void (*free)(struct ins_operands *ops);
  44. int (*parse)(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms);
  45. int (*scnprintf)(struct ins *ins, char *bf, size_t size,
  46. struct ins_operands *ops);
  47. };
  48. bool ins__is_jump(const struct ins *ins);
  49. bool ins__is_call(const struct ins *ins);
  50. bool ins__is_ret(const struct ins *ins);
  51. bool ins__is_lock(const struct ins *ins);
  52. int ins__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops);
  53. bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2);
  54. #define ANNOTATION__IPC_WIDTH 6
  55. #define ANNOTATION__CYCLES_WIDTH 6
  56. #define ANNOTATION__MINMAX_CYCLES_WIDTH 19
  57. struct annotation_options {
  58. bool hide_src_code,
  59. use_offset,
  60. jump_arrows,
  61. show_linenr,
  62. show_nr_jumps,
  63. show_nr_samples,
  64. show_total_period,
  65. show_minmax_cycle;
  66. u8 offset_level;
  67. };
  68. enum {
  69. ANNOTATION__OFFSET_JUMP_TARGETS = 1,
  70. ANNOTATION__OFFSET_CALL,
  71. ANNOTATION__MAX_OFFSET_LEVEL,
  72. };
  73. #define ANNOTATION__MIN_OFFSET_LEVEL ANNOTATION__OFFSET_JUMP_TARGETS
  74. extern struct annotation_options annotation__default_options;
  75. struct annotation;
  76. struct sym_hist_entry {
  77. u64 nr_samples;
  78. u64 period;
  79. };
  80. struct annotation_data {
  81. double percent;
  82. double percent_sum;
  83. struct sym_hist_entry he;
  84. };
  85. struct annotation_line {
  86. struct list_head node;
  87. struct rb_node rb_node;
  88. s64 offset;
  89. char *line;
  90. int line_nr;
  91. int jump_sources;
  92. float ipc;
  93. u64 cycles;
  94. u64 cycles_max;
  95. u64 cycles_min;
  96. size_t privsize;
  97. char *path;
  98. u32 idx;
  99. int idx_asm;
  100. int samples_nr;
  101. struct annotation_data samples[0];
  102. };
  103. struct disasm_line {
  104. struct ins ins;
  105. struct ins_operands ops;
  106. /* This needs to be at the end. */
  107. struct annotation_line al;
  108. };
  109. static inline struct disasm_line *disasm_line(struct annotation_line *al)
  110. {
  111. return al ? container_of(al, struct disasm_line, al) : NULL;
  112. }
  113. /*
  114. * Is this offset in the same function as the line it is used?
  115. * asm functions jump to other functions, for instance.
  116. */
  117. static inline bool disasm_line__has_local_offset(const struct disasm_line *dl)
  118. {
  119. return dl->ops.target.offset_avail && !dl->ops.target.outside;
  120. }
  121. /*
  122. * Can we draw an arrow from the jump to its target, for instance? I.e.
  123. * is the jump and its target in the same function?
  124. */
  125. bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym);
  126. void disasm_line__free(struct disasm_line *dl);
  127. struct annotation_line *
  128. annotation_line__next(struct annotation_line *pos, struct list_head *head);
  129. struct annotation_write_ops {
  130. bool first_line, current_entry, change_color;
  131. int width;
  132. void *obj;
  133. int (*set_color)(void *obj, int color);
  134. void (*set_percent_color)(void *obj, double percent, bool current);
  135. int (*set_jumps_percent_color)(void *obj, int nr, bool current);
  136. void (*printf)(void *obj, const char *fmt, ...);
  137. void (*write_graph)(void *obj, int graph);
  138. };
  139. double annotation_line__max_percent(struct annotation_line *al, struct annotation *notes);
  140. void annotation_line__write(struct annotation_line *al, struct annotation *notes,
  141. struct annotation_write_ops *ops);
  142. int __annotation__scnprintf_samples_period(struct annotation *notes,
  143. char *bf, size_t size,
  144. struct perf_evsel *evsel,
  145. bool show_freq);
  146. static inline int annotation__scnprintf_samples_period(struct annotation *notes,
  147. char *bf, size_t size,
  148. struct perf_evsel *evsel)
  149. {
  150. return __annotation__scnprintf_samples_period(notes, bf, size, evsel, true);
  151. }
  152. int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw);
  153. size_t disasm__fprintf(struct list_head *head, FILE *fp);
  154. void symbol__calc_percent(struct symbol *sym, struct perf_evsel *evsel);
  155. struct sym_hist {
  156. u64 nr_samples;
  157. u64 period;
  158. struct sym_hist_entry addr[0];
  159. };
  160. struct cyc_hist {
  161. u64 start;
  162. u64 cycles;
  163. u64 cycles_aggr;
  164. u64 cycles_max;
  165. u64 cycles_min;
  166. u32 num;
  167. u32 num_aggr;
  168. u8 have_start;
  169. /* 1 byte padding */
  170. u16 reset;
  171. };
  172. /** struct annotated_source - symbols with hits have this attached as in sannotation
  173. *
  174. * @histograms: Array of addr hit histograms per event being monitored
  175. * @lines: If 'print_lines' is specified, per source code line percentages
  176. * @source: source parsed from a disassembler like objdump -dS
  177. * @cyc_hist: Average cycles per basic block
  178. *
  179. * lines is allocated, percentages calculated and all sorted by percentage
  180. * when the annotation is about to be presented, so the percentages are for
  181. * one of the entries in the histogram array, i.e. for the event/counter being
  182. * presented. It is deallocated right after symbol__{tui,tty,etc}_annotate
  183. * returns.
  184. */
  185. struct annotated_source {
  186. struct list_head source;
  187. int nr_histograms;
  188. size_t sizeof_sym_hist;
  189. struct cyc_hist *cycles_hist;
  190. struct sym_hist *histograms;
  191. };
  192. struct annotation {
  193. pthread_mutex_t lock;
  194. u64 max_coverage;
  195. u64 start;
  196. struct annotation_options *options;
  197. struct annotation_line **offsets;
  198. int nr_events;
  199. int nr_jumps;
  200. int max_jump_sources;
  201. int nr_entries;
  202. int nr_asm_entries;
  203. u16 max_line_len;
  204. struct {
  205. u8 addr;
  206. u8 jumps;
  207. u8 target;
  208. u8 min_addr;
  209. u8 max_addr;
  210. } widths;
  211. bool have_cycles;
  212. struct annotated_source *src;
  213. };
  214. static inline int annotation__cycles_width(struct annotation *notes)
  215. {
  216. if (notes->have_cycles && notes->options->show_minmax_cycle)
  217. return ANNOTATION__IPC_WIDTH + ANNOTATION__MINMAX_CYCLES_WIDTH;
  218. return notes->have_cycles ? ANNOTATION__IPC_WIDTH + ANNOTATION__CYCLES_WIDTH : 0;
  219. }
  220. static inline int annotation__pcnt_width(struct annotation *notes)
  221. {
  222. return (notes->options->show_total_period ? 12 : 7) * notes->nr_events;
  223. }
  224. static inline bool annotation_line__filter(struct annotation_line *al, struct annotation *notes)
  225. {
  226. return notes->options->hide_src_code && al->offset == -1;
  227. }
  228. void annotation__set_offsets(struct annotation *notes, s64 size);
  229. void annotation__compute_ipc(struct annotation *notes, size_t size);
  230. void annotation__mark_jump_targets(struct annotation *notes, struct symbol *sym);
  231. void annotation__update_column_widths(struct annotation *notes);
  232. void annotation__init_column_widths(struct annotation *notes, struct symbol *sym);
  233. static inline struct sym_hist *annotated_source__histogram(struct annotated_source *src, int idx)
  234. {
  235. return ((void *)src->histograms) + (src->sizeof_sym_hist * idx);
  236. }
  237. static inline struct sym_hist *annotation__histogram(struct annotation *notes, int idx)
  238. {
  239. return annotated_source__histogram(notes->src, idx);
  240. }
  241. static inline struct annotation *symbol__annotation(struct symbol *sym)
  242. {
  243. return (void *)sym - symbol_conf.priv_size;
  244. }
  245. int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
  246. struct perf_evsel *evsel);
  247. int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
  248. struct addr_map_symbol *start,
  249. unsigned cycles);
  250. int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
  251. struct perf_evsel *evsel, u64 addr);
  252. int symbol__alloc_hist(struct symbol *sym);
  253. void symbol__annotate_zero_histograms(struct symbol *sym);
  254. int symbol__annotate(struct symbol *sym, struct map *map,
  255. struct perf_evsel *evsel, size_t privsize,
  256. struct arch **parch);
  257. int symbol__annotate2(struct symbol *sym, struct map *map,
  258. struct perf_evsel *evsel,
  259. struct annotation_options *options,
  260. struct arch **parch);
  261. enum symbol_disassemble_errno {
  262. SYMBOL_ANNOTATE_ERRNO__SUCCESS = 0,
  263. /*
  264. * Choose an arbitrary negative big number not to clash with standard
  265. * errno since SUS requires the errno has distinct positive values.
  266. * See 'Issue 6' in the link below.
  267. *
  268. * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html
  269. */
  270. __SYMBOL_ANNOTATE_ERRNO__START = -10000,
  271. SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX = __SYMBOL_ANNOTATE_ERRNO__START,
  272. __SYMBOL_ANNOTATE_ERRNO__END,
  273. };
  274. int symbol__strerror_disassemble(struct symbol *sym, struct map *map,
  275. int errnum, char *buf, size_t buflen);
  276. int symbol__annotate_printf(struct symbol *sym, struct map *map,
  277. struct perf_evsel *evsel, bool full_paths,
  278. int min_pcnt, int max_lines, int context);
  279. int symbol__annotate_fprintf2(struct symbol *sym, FILE *fp);
  280. void symbol__annotate_zero_histogram(struct symbol *sym, int evidx);
  281. void symbol__annotate_decay_histogram(struct symbol *sym, int evidx);
  282. void annotated_source__purge(struct annotated_source *as);
  283. int map_symbol__annotation_dump(struct map_symbol *ms, struct perf_evsel *evsel);
  284. bool ui__has_annotation(void);
  285. int symbol__tty_annotate(struct symbol *sym, struct map *map,
  286. struct perf_evsel *evsel, bool print_lines,
  287. bool full_paths, int min_pcnt, int max_lines);
  288. int symbol__tty_annotate2(struct symbol *sym, struct map *map,
  289. struct perf_evsel *evsel, bool print_lines,
  290. bool full_paths);
  291. #ifdef HAVE_SLANG_SUPPORT
  292. int symbol__tui_annotate(struct symbol *sym, struct map *map,
  293. struct perf_evsel *evsel,
  294. struct hist_browser_timer *hbt);
  295. #else
  296. static inline int symbol__tui_annotate(struct symbol *sym __maybe_unused,
  297. struct map *map __maybe_unused,
  298. struct perf_evsel *evsel __maybe_unused,
  299. struct hist_browser_timer *hbt
  300. __maybe_unused)
  301. {
  302. return 0;
  303. }
  304. #endif
  305. extern const char *disassembler_style;
  306. void annotation_config__init(void);
  307. #endif /* __PERF_ANNOTATE_H */