annotate.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. u64 addr;
  24. s64 offset;
  25. bool offset_avail;
  26. } target;
  27. union {
  28. struct {
  29. char *raw;
  30. char *name;
  31. u64 addr;
  32. } source;
  33. struct {
  34. struct ins ins;
  35. struct ins_operands *ops;
  36. } locked;
  37. };
  38. };
  39. struct arch;
  40. struct ins_ops {
  41. void (*free)(struct ins_operands *ops);
  42. int (*parse)(struct arch *arch, struct ins_operands *ops, struct map *map);
  43. int (*scnprintf)(struct ins *ins, char *bf, size_t size,
  44. struct ins_operands *ops);
  45. };
  46. bool ins__is_jump(const struct ins *ins);
  47. bool ins__is_call(const struct ins *ins);
  48. bool ins__is_ret(const struct ins *ins);
  49. bool ins__is_lock(const struct ins *ins);
  50. int ins__scnprintf(struct ins *ins, char *bf, size_t size, struct ins_operands *ops);
  51. bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2);
  52. struct annotation;
  53. struct sym_hist_entry {
  54. u64 nr_samples;
  55. u64 period;
  56. };
  57. struct annotation_data {
  58. double percent;
  59. struct sym_hist_entry he;
  60. };
  61. struct annotation_line {
  62. struct list_head node;
  63. struct rb_node rb_node;
  64. s64 offset;
  65. char *line;
  66. int line_nr;
  67. float ipc;
  68. u64 cycles;
  69. size_t privsize;
  70. int samples_nr;
  71. struct annotation_data samples[0];
  72. };
  73. struct disasm_line {
  74. struct ins ins;
  75. struct ins_operands ops;
  76. /* This needs to be at the end. */
  77. struct annotation_line al;
  78. };
  79. static inline struct disasm_line *disasm_line(struct annotation_line *al)
  80. {
  81. return al ? container_of(al, struct disasm_line, al) : NULL;
  82. }
  83. static inline bool disasm_line__has_offset(const struct disasm_line *dl)
  84. {
  85. return dl->ops.target.offset_avail;
  86. }
  87. void disasm_line__free(struct disasm_line *dl);
  88. struct annotation_line *
  89. annotation_line__next(struct annotation_line *pos, struct list_head *head);
  90. int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw);
  91. size_t disasm__fprintf(struct list_head *head, FILE *fp);
  92. double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset,
  93. s64 end, const char **path, struct sym_hist_entry *sample);
  94. struct sym_hist {
  95. u64 nr_samples;
  96. u64 period;
  97. struct sym_hist_entry addr[0];
  98. };
  99. struct cyc_hist {
  100. u64 start;
  101. u64 cycles;
  102. u64 cycles_aggr;
  103. u32 num;
  104. u32 num_aggr;
  105. u8 have_start;
  106. /* 1 byte padding */
  107. u16 reset;
  108. };
  109. struct source_line_samples {
  110. double percent;
  111. double percent_sum;
  112. u64 nr;
  113. };
  114. struct source_line {
  115. struct rb_node node;
  116. char *path;
  117. int nr_pcnt;
  118. struct source_line_samples samples[1];
  119. };
  120. /** struct annotated_source - symbols with hits have this attached as in sannotation
  121. *
  122. * @histogram: Array of addr hit histograms per event being monitored
  123. * @lines: If 'print_lines' is specified, per source code line percentages
  124. * @source: source parsed from a disassembler like objdump -dS
  125. * @cyc_hist: Average cycles per basic block
  126. *
  127. * lines is allocated, percentages calculated and all sorted by percentage
  128. * when the annotation is about to be presented, so the percentages are for
  129. * one of the entries in the histogram array, i.e. for the event/counter being
  130. * presented. It is deallocated right after symbol__{tui,tty,etc}_annotate
  131. * returns.
  132. */
  133. struct annotated_source {
  134. struct list_head source;
  135. struct source_line *lines;
  136. int nr_histograms;
  137. size_t sizeof_sym_hist;
  138. struct cyc_hist *cycles_hist;
  139. struct sym_hist histograms[0];
  140. };
  141. struct annotation {
  142. pthread_mutex_t lock;
  143. u64 max_coverage;
  144. struct annotated_source *src;
  145. };
  146. static inline struct sym_hist *annotation__histogram(struct annotation *notes, int idx)
  147. {
  148. return (((void *)&notes->src->histograms) +
  149. (notes->src->sizeof_sym_hist * idx));
  150. }
  151. static inline struct annotation *symbol__annotation(struct symbol *sym)
  152. {
  153. return (void *)sym - symbol_conf.priv_size;
  154. }
  155. int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
  156. int evidx);
  157. int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
  158. struct addr_map_symbol *start,
  159. unsigned cycles);
  160. int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
  161. int evidx, u64 addr);
  162. int symbol__alloc_hist(struct symbol *sym);
  163. void symbol__annotate_zero_histograms(struct symbol *sym);
  164. int symbol__annotate(struct symbol *sym, struct map *map,
  165. struct perf_evsel *evsel, size_t privsize,
  166. struct arch **parch, char *cpuid);
  167. enum symbol_disassemble_errno {
  168. SYMBOL_ANNOTATE_ERRNO__SUCCESS = 0,
  169. /*
  170. * Choose an arbitrary negative big number not to clash with standard
  171. * errno since SUS requires the errno has distinct positive values.
  172. * See 'Issue 6' in the link below.
  173. *
  174. * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html
  175. */
  176. __SYMBOL_ANNOTATE_ERRNO__START = -10000,
  177. SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX = __SYMBOL_ANNOTATE_ERRNO__START,
  178. __SYMBOL_ANNOTATE_ERRNO__END,
  179. };
  180. int symbol__strerror_disassemble(struct symbol *sym, struct map *map,
  181. int errnum, char *buf, size_t buflen);
  182. int symbol__annotate_printf(struct symbol *sym, struct map *map,
  183. struct perf_evsel *evsel, bool full_paths,
  184. int min_pcnt, int max_lines, int context);
  185. void symbol__annotate_zero_histogram(struct symbol *sym, int evidx);
  186. void symbol__annotate_decay_histogram(struct symbol *sym, int evidx);
  187. void annotated_source__purge(struct annotated_source *as);
  188. bool ui__has_annotation(void);
  189. int symbol__tty_annotate(struct symbol *sym, struct map *map,
  190. struct perf_evsel *evsel, bool print_lines,
  191. bool full_paths, int min_pcnt, int max_lines);
  192. #ifdef HAVE_SLANG_SUPPORT
  193. int symbol__tui_annotate(struct symbol *sym, struct map *map,
  194. struct perf_evsel *evsel,
  195. struct hist_browser_timer *hbt);
  196. #else
  197. static inline int symbol__tui_annotate(struct symbol *sym __maybe_unused,
  198. struct map *map __maybe_unused,
  199. struct perf_evsel *evsel __maybe_unused,
  200. struct hist_browser_timer *hbt
  201. __maybe_unused)
  202. {
  203. return 0;
  204. }
  205. #endif
  206. extern const char *disassembler_style;
  207. #endif /* __PERF_ANNOTATE_H */