annotate.h 9.3 KB

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