annotate.h 8.2 KB

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