annotate.h 10 KB

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