annotate.h 9.5 KB

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