annotate.h 5.5 KB

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