annotate.h 11 KB

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