annotate.h 10 KB

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