annotate.h 5.4 KB

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