annotate.h 5.7 KB

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