annotate.h 11 KB

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