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