annotate.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. double annotation_line__max_percent(struct annotation_line *al, struct annotation *notes);
  109. int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw);
  110. size_t disasm__fprintf(struct list_head *head, FILE *fp);
  111. void symbol__calc_percent(struct symbol *sym, struct perf_evsel *evsel);
  112. struct sym_hist {
  113. u64 nr_samples;
  114. u64 period;
  115. struct sym_hist_entry addr[0];
  116. };
  117. struct cyc_hist {
  118. u64 start;
  119. u64 cycles;
  120. u64 cycles_aggr;
  121. u32 num;
  122. u32 num_aggr;
  123. u8 have_start;
  124. /* 1 byte padding */
  125. u16 reset;
  126. };
  127. /** struct annotated_source - symbols with hits have this attached as in sannotation
  128. *
  129. * @histogram: Array of addr hit histograms per event being monitored
  130. * @lines: If 'print_lines' is specified, per source code line percentages
  131. * @source: source parsed from a disassembler like objdump -dS
  132. * @cyc_hist: Average cycles per basic block
  133. *
  134. * lines is allocated, percentages calculated and all sorted by percentage
  135. * when the annotation is about to be presented, so the percentages are for
  136. * one of the entries in the histogram array, i.e. for the event/counter being
  137. * presented. It is deallocated right after symbol__{tui,tty,etc}_annotate
  138. * returns.
  139. */
  140. struct annotated_source {
  141. struct list_head source;
  142. int nr_histograms;
  143. size_t sizeof_sym_hist;
  144. struct cyc_hist *cycles_hist;
  145. struct sym_hist histograms[0];
  146. };
  147. struct annotation {
  148. pthread_mutex_t lock;
  149. u64 max_coverage;
  150. u64 start;
  151. struct annotation_options *options;
  152. struct annotation_line **offsets;
  153. int nr_events;
  154. int nr_jumps;
  155. int max_jump_sources;
  156. int nr_entries;
  157. int nr_asm_entries;
  158. u16 max_line_len;
  159. struct {
  160. u8 addr;
  161. u8 jumps;
  162. u8 target;
  163. u8 min_addr;
  164. u8 max_addr;
  165. } widths;
  166. bool have_cycles;
  167. struct annotated_source *src;
  168. };
  169. static inline int annotation__cycles_width(struct annotation *notes)
  170. {
  171. return notes->have_cycles ? ANNOTATION__IPC_WIDTH + ANNOTATION__CYCLES_WIDTH : 0;
  172. }
  173. static inline int annotation__pcnt_width(struct annotation *notes)
  174. {
  175. return (notes->options->show_total_period ? 12 : 7) * notes->nr_events;
  176. }
  177. void annotation__set_offsets(struct annotation *notes, s64 size);
  178. void annotation__compute_ipc(struct annotation *notes, size_t size);
  179. void annotation__mark_jump_targets(struct annotation *notes, struct symbol *sym);
  180. void annotation__update_column_widths(struct annotation *notes);
  181. void annotation__init_column_widths(struct annotation *notes, struct symbol *sym);
  182. static inline struct sym_hist *annotation__histogram(struct annotation *notes, int idx)
  183. {
  184. return (((void *)&notes->src->histograms) +
  185. (notes->src->sizeof_sym_hist * idx));
  186. }
  187. static inline struct annotation *symbol__annotation(struct symbol *sym)
  188. {
  189. return (void *)sym - symbol_conf.priv_size;
  190. }
  191. int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
  192. int evidx);
  193. int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
  194. struct addr_map_symbol *start,
  195. unsigned cycles);
  196. int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
  197. int evidx, u64 addr);
  198. int symbol__alloc_hist(struct symbol *sym);
  199. void symbol__annotate_zero_histograms(struct symbol *sym);
  200. int symbol__annotate(struct symbol *sym, struct map *map,
  201. struct perf_evsel *evsel, size_t privsize,
  202. struct arch **parch);
  203. int symbol__annotate2(struct symbol *sym, struct map *map,
  204. struct perf_evsel *evsel,
  205. struct annotation_options *options,
  206. struct arch **parch);
  207. enum symbol_disassemble_errno {
  208. SYMBOL_ANNOTATE_ERRNO__SUCCESS = 0,
  209. /*
  210. * Choose an arbitrary negative big number not to clash with standard
  211. * errno since SUS requires the errno has distinct positive values.
  212. * See 'Issue 6' in the link below.
  213. *
  214. * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html
  215. */
  216. __SYMBOL_ANNOTATE_ERRNO__START = -10000,
  217. SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX = __SYMBOL_ANNOTATE_ERRNO__START,
  218. __SYMBOL_ANNOTATE_ERRNO__END,
  219. };
  220. int symbol__strerror_disassemble(struct symbol *sym, struct map *map,
  221. int errnum, char *buf, size_t buflen);
  222. int symbol__annotate_printf(struct symbol *sym, struct map *map,
  223. struct perf_evsel *evsel, bool full_paths,
  224. int min_pcnt, int max_lines, int context);
  225. void symbol__annotate_zero_histogram(struct symbol *sym, int evidx);
  226. void symbol__annotate_decay_histogram(struct symbol *sym, int evidx);
  227. void annotated_source__purge(struct annotated_source *as);
  228. bool ui__has_annotation(void);
  229. int symbol__tty_annotate(struct symbol *sym, struct map *map,
  230. struct perf_evsel *evsel, bool print_lines,
  231. bool full_paths, int min_pcnt, int max_lines);
  232. #ifdef HAVE_SLANG_SUPPORT
  233. int symbol__tui_annotate(struct symbol *sym, struct map *map,
  234. struct perf_evsel *evsel,
  235. struct hist_browser_timer *hbt);
  236. #else
  237. static inline int symbol__tui_annotate(struct symbol *sym __maybe_unused,
  238. struct map *map __maybe_unused,
  239. struct perf_evsel *evsel __maybe_unused,
  240. struct hist_browser_timer *hbt
  241. __maybe_unused)
  242. {
  243. return 0;
  244. }
  245. #endif
  246. extern const char *disassembler_style;
  247. #endif /* __PERF_ANNOTATE_H */