annotate.h 10 KB

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