annotate.h 11 KB

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