annotate.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #include "../browser.h"
  2. #include "../helpline.h"
  3. #include "../libslang.h"
  4. #include "../../annotate.h"
  5. #include "../../hist.h"
  6. #include "../../sort.h"
  7. #include "../../symbol.h"
  8. #include "../../annotate.h"
  9. #include <pthread.h>
  10. static void ui__error_window(const char *fmt, ...)
  11. {
  12. va_list ap;
  13. va_start(ap, fmt);
  14. newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap);
  15. va_end(ap);
  16. }
  17. struct annotate_browser {
  18. struct ui_browser b;
  19. struct rb_root entries;
  20. struct rb_node *curr_hot;
  21. };
  22. struct objdump_line_rb_node {
  23. struct rb_node rb_node;
  24. double percent;
  25. u32 idx;
  26. };
  27. static inline
  28. struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self)
  29. {
  30. return (struct objdump_line_rb_node *)(self + 1);
  31. }
  32. static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
  33. {
  34. struct objdump_line *ol = rb_entry(entry, struct objdump_line, node);
  35. bool current_entry = ui_browser__is_current_entry(self, row);
  36. int width = self->width;
  37. if (ol->offset != -1) {
  38. struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
  39. ui_browser__set_percent_color(self, olrb->percent, current_entry);
  40. slsmg_printf(" %7.2f ", olrb->percent);
  41. } else {
  42. ui_browser__set_percent_color(self, 0, current_entry);
  43. slsmg_write_nstring(" ", 9);
  44. }
  45. SLsmg_write_char(':');
  46. slsmg_write_nstring(" ", 8);
  47. if (!*ol->line)
  48. slsmg_write_nstring(" ", width - 18);
  49. else
  50. slsmg_write_nstring(ol->line, width - 18);
  51. if (!current_entry)
  52. ui_browser__set_color(self, HE_COLORSET_CODE);
  53. }
  54. static double objdump_line__calc_percent(struct objdump_line *self,
  55. struct symbol *sym, int evidx)
  56. {
  57. double percent = 0.0;
  58. if (self->offset != -1) {
  59. int len = sym->end - sym->start;
  60. unsigned int hits = 0;
  61. struct annotation *notes = symbol__annotation(sym);
  62. struct source_line *src_line = notes->src->lines;
  63. struct sym_hist *h = annotation__histogram(notes, evidx);
  64. s64 offset = self->offset;
  65. struct objdump_line *next;
  66. next = objdump__get_next_ip_line(&notes->src->source, self);
  67. while (offset < (s64)len &&
  68. (next == NULL || offset < next->offset)) {
  69. if (src_line) {
  70. percent += src_line[offset].percent;
  71. } else
  72. hits += h->addr[offset];
  73. ++offset;
  74. }
  75. /*
  76. * If the percentage wasn't already calculated in
  77. * symbol__get_source_line, do it now:
  78. */
  79. if (src_line == NULL && h->sum)
  80. percent = 100.0 * hits / h->sum;
  81. }
  82. return percent;
  83. }
  84. static void objdump__insert_line(struct rb_root *self,
  85. struct objdump_line_rb_node *line)
  86. {
  87. struct rb_node **p = &self->rb_node;
  88. struct rb_node *parent = NULL;
  89. struct objdump_line_rb_node *l;
  90. while (*p != NULL) {
  91. parent = *p;
  92. l = rb_entry(parent, struct objdump_line_rb_node, rb_node);
  93. if (line->percent < l->percent)
  94. p = &(*p)->rb_left;
  95. else
  96. p = &(*p)->rb_right;
  97. }
  98. rb_link_node(&line->rb_node, parent, p);
  99. rb_insert_color(&line->rb_node, self);
  100. }
  101. static void annotate_browser__set_top(struct annotate_browser *self,
  102. struct rb_node *nd)
  103. {
  104. struct objdump_line_rb_node *rbpos;
  105. struct objdump_line *pos;
  106. unsigned back;
  107. ui_browser__refresh_dimensions(&self->b);
  108. back = self->b.height / 2;
  109. rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node);
  110. pos = ((struct objdump_line *)rbpos) - 1;
  111. self->b.top_idx = self->b.index = rbpos->idx;
  112. while (self->b.top_idx != 0 && back != 0) {
  113. pos = list_entry(pos->node.prev, struct objdump_line, node);
  114. --self->b.top_idx;
  115. --back;
  116. }
  117. self->b.top = pos;
  118. self->curr_hot = nd;
  119. }
  120. static void annotate_browser__calc_percent(struct annotate_browser *browser,
  121. int evidx)
  122. {
  123. struct symbol *sym = browser->b.priv;
  124. struct annotation *notes = symbol__annotation(sym);
  125. struct objdump_line *pos;
  126. browser->entries = RB_ROOT;
  127. pthread_mutex_lock(&notes->lock);
  128. list_for_each_entry(pos, &notes->src->source, node) {
  129. struct objdump_line_rb_node *rbpos = objdump_line__rb(pos);
  130. rbpos->percent = objdump_line__calc_percent(pos, sym, evidx);
  131. if (rbpos->percent < 0.01) {
  132. RB_CLEAR_NODE(&rbpos->rb_node);
  133. continue;
  134. }
  135. objdump__insert_line(&browser->entries, rbpos);
  136. }
  137. pthread_mutex_unlock(&notes->lock);
  138. browser->curr_hot = rb_last(&browser->entries);
  139. }
  140. static int annotate_browser__run(struct annotate_browser *self, int evidx,
  141. int refresh)
  142. {
  143. struct rb_node *nd = NULL;
  144. struct symbol *sym = self->b.priv;
  145. /*
  146. * RIGHT To allow builtin-annotate to cycle thru multiple symbols by
  147. * examining the exit key for this function.
  148. */
  149. int exit_keys[] = { 'H', NEWT_KEY_TAB, NEWT_KEY_UNTAB,
  150. NEWT_KEY_RIGHT, 0 };
  151. int key;
  152. if (ui_browser__show(&self->b, sym->name,
  153. "<-, -> or ESC: exit, TAB/shift+TAB: "
  154. "cycle hottest lines, H: Hottest") < 0)
  155. return -1;
  156. ui_browser__add_exit_keys(&self->b, exit_keys);
  157. annotate_browser__calc_percent(self, evidx);
  158. if (self->curr_hot)
  159. annotate_browser__set_top(self, self->curr_hot);
  160. nd = self->curr_hot;
  161. if (refresh != 0)
  162. newtFormSetTimer(self->b.form, refresh);
  163. while (1) {
  164. key = ui_browser__run(&self->b);
  165. if (refresh != 0) {
  166. annotate_browser__calc_percent(self, evidx);
  167. /*
  168. * Current line focus got out of the list of most active
  169. * lines, NULL it so that if TAB|UNTAB is pressed, we
  170. * move to curr_hot (current hottest line).
  171. */
  172. if (nd != NULL && RB_EMPTY_NODE(nd))
  173. nd = NULL;
  174. }
  175. switch (key) {
  176. case -1:
  177. /*
  178. * FIXME we need to check if it was
  179. * es.reason == NEWT_EXIT_TIMER
  180. */
  181. if (refresh != 0)
  182. symbol__annotate_decay_histogram(sym, evidx);
  183. continue;
  184. case NEWT_KEY_TAB:
  185. if (nd != NULL) {
  186. nd = rb_prev(nd);
  187. if (nd == NULL)
  188. nd = rb_last(&self->entries);
  189. } else
  190. nd = self->curr_hot;
  191. break;
  192. case NEWT_KEY_UNTAB:
  193. if (nd != NULL)
  194. nd = rb_next(nd);
  195. if (nd == NULL)
  196. nd = rb_first(&self->entries);
  197. else
  198. nd = self->curr_hot;
  199. break;
  200. case 'H':
  201. nd = self->curr_hot;
  202. break;
  203. default:
  204. goto out;
  205. }
  206. if (nd != NULL)
  207. annotate_browser__set_top(self, nd);
  208. }
  209. out:
  210. ui_browser__hide(&self->b);
  211. return key;
  212. }
  213. int hist_entry__tui_annotate(struct hist_entry *he, int evidx)
  214. {
  215. return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx, 0);
  216. }
  217. int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
  218. int refresh)
  219. {
  220. struct objdump_line *pos, *n;
  221. struct annotation *notes;
  222. struct annotate_browser browser = {
  223. .b = {
  224. .refresh = ui_browser__list_head_refresh,
  225. .seek = ui_browser__list_head_seek,
  226. .write = annotate_browser__write,
  227. .priv = sym,
  228. },
  229. };
  230. int ret;
  231. if (sym == NULL)
  232. return -1;
  233. if (map->dso->annotate_warned)
  234. return -1;
  235. if (symbol__annotate(sym, map, sizeof(struct objdump_line_rb_node)) < 0) {
  236. ui__error_window(ui_helpline__last_msg);
  237. return -1;
  238. }
  239. ui_helpline__push("Press <- or ESC to exit");
  240. notes = symbol__annotation(sym);
  241. list_for_each_entry(pos, &notes->src->source, node) {
  242. struct objdump_line_rb_node *rbpos;
  243. size_t line_len = strlen(pos->line);
  244. if (browser.b.width < line_len)
  245. browser.b.width = line_len;
  246. rbpos = objdump_line__rb(pos);
  247. rbpos->idx = browser.b.nr_entries++;
  248. }
  249. browser.b.entries = &notes->src->source,
  250. browser.b.width += 18; /* Percentage */
  251. ret = annotate_browser__run(&browser, evidx, refresh);
  252. list_for_each_entry_safe(pos, n, &notes->src->source, node) {
  253. list_del(&pos->node);
  254. objdump_line__free(pos);
  255. }
  256. return ret;
  257. }