annotate.h 6.0 KB

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