annotate.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. ui_browser__set_percent_color(self, olrb->percent, current_entry);
  37. slsmg_printf(" %7.2f ", olrb->percent);
  38. if (!current_entry)
  39. ui_browser__set_color(self, HE_COLORSET_CODE);
  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. }
  51. static double objdump_line__calc_percent(struct objdump_line *self,
  52. struct list_head *head,
  53. struct symbol *sym)
  54. {
  55. double percent = 0.0;
  56. if (self->offset != -1) {
  57. int len = sym->end - sym->start;
  58. unsigned int hits = 0;
  59. struct sym_priv *priv = symbol__priv(sym);
  60. struct sym_ext *sym_ext = priv->ext;
  61. struct sym_hist *h = priv->hist;
  62. s64 offset = self->offset;
  63. struct objdump_line *next = objdump__get_next_ip_line(head, self);
  64. while (offset < (s64)len &&
  65. (next == NULL || offset < next->offset)) {
  66. if (sym_ext) {
  67. percent += sym_ext[offset].percent;
  68. } else
  69. hits += h->ip[offset];
  70. ++offset;
  71. }
  72. if (sym_ext == NULL && h->sum)
  73. percent = 100.0 * hits / h->sum;
  74. }
  75. return percent;
  76. }
  77. static void objdump__insert_line(struct rb_root *self,
  78. struct objdump_line_rb_node *line)
  79. {
  80. struct rb_node **p = &self->rb_node;
  81. struct rb_node *parent = NULL;
  82. struct objdump_line_rb_node *l;
  83. while (*p != NULL) {
  84. parent = *p;
  85. l = rb_entry(parent, struct objdump_line_rb_node, rb_node);
  86. if (line->percent < l->percent)
  87. p = &(*p)->rb_left;
  88. else
  89. p = &(*p)->rb_right;
  90. }
  91. rb_link_node(&line->rb_node, parent, p);
  92. rb_insert_color(&line->rb_node, self);
  93. }
  94. static void annotate_browser__set_top(struct annotate_browser *self,
  95. struct rb_node *nd)
  96. {
  97. struct objdump_line_rb_node *rbpos;
  98. struct objdump_line *pos;
  99. unsigned back;
  100. ui_browser__refresh_dimensions(&self->b);
  101. back = self->b.height / 2;
  102. rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node);
  103. pos = ((struct objdump_line *)rbpos) - 1;
  104. self->b.top_idx = self->b.index = rbpos->idx;
  105. while (self->b.top_idx != 0 && back != 0) {
  106. pos = list_entry(pos->node.prev, struct objdump_line, node);
  107. --self->b.top_idx;
  108. --back;
  109. }
  110. self->b.top = pos;
  111. self->curr_hot = nd;
  112. }
  113. static int annotate_browser__run(struct annotate_browser *self,
  114. struct newtExitStruct *es)
  115. {
  116. struct rb_node *nd;
  117. struct hist_entry *he = self->b.priv;
  118. if (ui_browser__show(&self->b, he->ms.sym->name,
  119. "<- or ESC: exit, TAB/shift+TAB: cycle thru samples") < 0)
  120. return -1;
  121. newtFormAddHotKey(self->b.form, NEWT_KEY_LEFT);
  122. newtFormAddHotKey(self->b.form, NEWT_KEY_RIGHT);
  123. nd = self->curr_hot;
  124. if (nd) {
  125. newtFormAddHotKey(self->b.form, NEWT_KEY_TAB);
  126. newtFormAddHotKey(self->b.form, NEWT_KEY_UNTAB);
  127. }
  128. while (1) {
  129. ui_browser__run(&self->b, es);
  130. if (es->reason != NEWT_EXIT_HOTKEY)
  131. break;
  132. switch (es->u.key) {
  133. case NEWT_KEY_TAB:
  134. nd = rb_prev(nd);
  135. if (nd == NULL)
  136. nd = rb_last(&self->entries);
  137. annotate_browser__set_top(self, nd);
  138. break;
  139. case NEWT_KEY_UNTAB:
  140. nd = rb_next(nd);
  141. if (nd == NULL)
  142. nd = rb_first(&self->entries);
  143. annotate_browser__set_top(self, nd);
  144. break;
  145. default:
  146. goto out;
  147. }
  148. }
  149. out:
  150. ui_browser__hide(&self->b);
  151. return es->u.key;
  152. }
  153. int hist_entry__tui_annotate(struct hist_entry *self)
  154. {
  155. struct newtExitStruct es;
  156. struct objdump_line *pos, *n;
  157. struct objdump_line_rb_node *rbpos;
  158. LIST_HEAD(head);
  159. struct annotate_browser browser = {
  160. .b = {
  161. .entries = &head,
  162. .refresh = ui_browser__list_head_refresh,
  163. .seek = ui_browser__list_head_seek,
  164. .write = annotate_browser__write,
  165. .priv = self,
  166. },
  167. };
  168. int ret;
  169. if (self->ms.sym == NULL)
  170. return -1;
  171. if (self->ms.map->dso->annotate_warned)
  172. return -1;
  173. if (hist_entry__annotate(self, &head, sizeof(*rbpos)) < 0) {
  174. ui__error_window(ui_helpline__last_msg);
  175. return -1;
  176. }
  177. ui_helpline__push("Press <- or ESC to exit");
  178. list_for_each_entry(pos, &head, node) {
  179. size_t line_len = strlen(pos->line);
  180. if (browser.b.width < line_len)
  181. browser.b.width = line_len;
  182. rbpos = objdump_line__rb(pos);
  183. rbpos->idx = browser.b.nr_entries++;
  184. rbpos->percent = objdump_line__calc_percent(pos, &head, self->ms.sym);
  185. if (rbpos->percent < 0.01)
  186. continue;
  187. objdump__insert_line(&browser.entries, rbpos);
  188. }
  189. /*
  190. * Position the browser at the hottest line.
  191. */
  192. browser.curr_hot = rb_last(&browser.entries);
  193. if (browser.curr_hot)
  194. annotate_browser__set_top(&browser, browser.curr_hot);
  195. browser.b.width += 18; /* Percentage */
  196. ret = annotate_browser__run(&browser, &es);
  197. list_for_each_entry_safe(pos, n, &head, node) {
  198. list_del(&pos->node);
  199. objdump_line__free(pos);
  200. }
  201. return ret;
  202. }