annotate.h 10 KB

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