annotate.h 9.6 KB

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