annotate.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #include "../browser.h"
  2. #include "../helpline.h"
  3. #include "../libslang.h"
  4. #include "../../hist.h"
  5. #include "../../sort.h"
  6. #include "../../symbol.h"
  7. static void ui__error_window(const char *fmt, ...)
  8. {
  9. va_list ap;
  10. va_start(ap, fmt);
  11. newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap);
  12. va_end(ap);
  13. }
  14. struct annotate_browser {
  15. struct ui_browser b;
  16. struct rb_root entries;
  17. struct rb_node *curr_hot;
  18. };
  19. struct objdump_line_rb_node {
  20. struct rb_node rb_node;
  21. double percent;
  22. u32 idx;
  23. };
  24. static inline
  25. struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self)
  26. {
  27. return (struct objdump_line_rb_node *)(self + 1);
  28. }
  29. static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
  30. {
  31. struct objdump_line *ol = rb_entry(entry, struct objdump_line, node);
  32. bool current_entry = ui_browser__is_current_entry(self, row);
  33. int width = self->width;
  34. if (ol->offset != -1) {
  35. struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
  36. int color = ui_browser__percent_color(olrb->percent, current_entry);
  37. SLsmg_set_color(color);
  38. slsmg_printf(" %7.2f ", olrb->percent);
  39. if (!current_entry)
  40. SLsmg_set_color(HE_COLORSET_CODE);
  41. } else {
  42. int color = ui_browser__percent_color(0, current_entry);
  43. SLsmg_set_color(color);
  44. slsmg_write_nstring(" ", 9);
  45. }
  46. SLsmg_write_char(':');
  47. slsmg_write_nstring(" ", 8);
  48. if (!*ol->line)
  49. slsmg_write_nstring(" ", width - 18);
  50. else
  51. slsmg_write_nstring(ol->line, width - 18);
  52. }
  53. static double objdump_line__calc_percent(struct objdump_line *self,
  54. struct list_head *head,
  55. struct symbol *sym)
  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 sym_priv *priv = symbol__priv(sym);
  62. struct sym_ext *sym_ext = priv->ext;
  63. struct sym_hist *h = priv->hist;
  64. s64 offset = self->offset;
  65. struct objdump_line *next = objdump__get_next_ip_line(head, self);
  66. while (offset < (s64)len &&
  67. (next == NULL || offset < next->offset)) {
  68. if (sym_ext) {
  69. percent += sym_ext[offset].percent;
  70. } else
  71. hits += h->ip[offset];
  72. ++offset;
  73. }
  74. if (sym_ext == NULL && h->sum)
  75. percent = 100.0 * hits / h->sum;
  76. }
  77. return percent;
  78. }
  79. static void objdump__insert_line(struct rb_root *self,
  80. struct objdump_line_rb_node *line)
  81. {
  82. struct rb_node **p = &self->rb_node;
  83. struct rb_node *parent = NULL;
  84. struct objdump_line_rb_node *l;
  85. while (*p != NULL) {
  86. parent = *p;
  87. l = rb_entry(parent, struct objdump_line_rb_node, rb_node);
  88. if (line->percent < l->percent)
  89. p = &(*p)->rb_left;
  90. else
  91. p = &(*p)->rb_right;
  92. }
  93. rb_link_node(&line->rb_node, parent, p);
  94. rb_insert_color(&line->rb_node, self);
  95. }
  96. static void annotate_browser__set_top(struct annotate_browser *self,
  97. struct rb_node *nd)
  98. {
  99. struct objdump_line_rb_node *rbpos;
  100. struct objdump_line *pos;
  101. unsigned back;
  102. ui_browser__refresh_dimensions(&self->b);
  103. back = self->b.height / 2;
  104. rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node);
  105. pos = ((struct objdump_line *)rbpos) - 1;
  106. self->b.top_idx = self->b.index = rbpos->idx;
  107. while (self->b.top_idx != 0 && back != 0) {
  108. pos = list_entry(pos->node.prev, struct objdump_line, node);
  109. --self->b.top_idx;
  110. --back;
  111. }
  112. self->b.top = pos;
  113. self->curr_hot = nd;
  114. }
  115. static int annotate_browser__run(struct annotate_browser *self,
  116. struct newtExitStruct *es)
  117. {
  118. struct rb_node *nd;
  119. struct hist_entry *he = self->b.priv;
  120. if (ui_browser__show(&self->b, he->ms.sym->name,
  121. "<- or ESC: exit, TAB/shift+TAB: cycle thru samples") < 0)
  122. return -1;
  123. newtFormAddHotKey(self->b.form, NEWT_KEY_LEFT);
  124. newtFormAddHotKey(self->b.form, NEWT_KEY_RIGHT);
  125. nd = self->curr_hot;
  126. if (nd) {
  127. newtFormAddHotKey(self->b.form, NEWT_KEY_TAB);
  128. newtFormAddHotKey(self->b.form, NEWT_KEY_UNTAB);
  129. }
  130. while (1) {
  131. ui_browser__run(&self->b, es);
  132. if (es->reason != NEWT_EXIT_HOTKEY)
  133. break;
  134. switch (es->u.key) {
  135. case NEWT_KEY_TAB:
  136. nd = rb_prev(nd);
  137. if (nd == NULL)
  138. nd = rb_last(&self->entries);
  139. annotate_browser__set_top(self, nd);
  140. break;
  141. case NEWT_KEY_UNTAB:
  142. nd = rb_next(nd);
  143. if (nd == NULL)
  144. nd = rb_first(&self->entries);
  145. annotate_browser__set_top(self, nd);
  146. break;
  147. default:
  148. goto out;
  149. }
  150. }
  151. out:
  152. ui_browser__hide(&self->b);
  153. return es->u.key;
  154. }
  155. int hist_entry__tui_annotate(struct hist_entry *self)
  156. {
  157. struct newtExitStruct es;
  158. struct objdump_line *pos, *n;
  159. struct objdump_line_rb_node *rbpos;
  160. LIST_HEAD(head);
  161. struct annotate_browser browser = {
  162. .b = {
  163. .entries = &head,
  164. .refresh = ui_browser__list_head_refresh,
  165. .seek = ui_browser__list_head_seek,
  166. .write = annotate_browser__write,
  167. .priv = self,
  168. },
  169. };
  170. int ret;
  171. if (self->ms.sym == NULL)
  172. return -1;
  173. if (self->ms.map->dso->annotate_warned)
  174. return -1;
  175. if (hist_entry__annotate(self, &head, sizeof(*rbpos)) < 0) {
  176. ui__error_window(ui_helpline__last_msg);
  177. return -1;
  178. }
  179. ui_helpline__push("Press <- or ESC to exit");
  180. list_for_each_entry(pos, &head, node) {
  181. size_t line_len = strlen(pos->line);
  182. if (browser.b.width < line_len)
  183. browser.b.width = line_len;
  184. rbpos = objdump_line__rb(pos);
  185. rbpos->idx = browser.b.nr_entries++;
  186. rbpos->percent = objdump_line__calc_percent(pos, &head, self->ms.sym);
  187. if (rbpos->percent < 0.01)
  188. continue;
  189. objdump__insert_line(&browser.entries, rbpos);
  190. }
  191. /*
  192. * Position the browser at the hottest line.
  193. */
  194. browser.curr_hot = rb_last(&browser.entries);
  195. if (browser.curr_hot)
  196. annotate_browser__set_top(&browser, browser.curr_hot);
  197. browser.b.width += 18; /* Percentage */
  198. ret = annotate_browser__run(&browser, &es);
  199. list_for_each_entry_safe(pos, n, &head, node) {
  200. list_del(&pos->node);
  201. objdump_line__free(pos);
  202. }
  203. return ret;
  204. }