top.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
  3. *
  4. * Parts came from builtin-{top,stat,record}.c, see those files for further
  5. * copyright notes.
  6. *
  7. * Released under the GPL v2. (and only v2, not any later version)
  8. */
  9. #include "../browser.h"
  10. #include "../../annotate.h"
  11. #include "../helpline.h"
  12. #include "../libslang.h"
  13. #include "../util.h"
  14. #include "../../evlist.h"
  15. #include "../../hist.h"
  16. #include "../../sort.h"
  17. #include "../../symbol.h"
  18. #include "../../top.h"
  19. struct perf_top_browser {
  20. struct ui_browser b;
  21. struct rb_root root;
  22. struct sym_entry *selection;
  23. float sum_ksamples;
  24. int dso_width;
  25. int dso_short_width;
  26. int sym_width;
  27. };
  28. static void perf_top_browser__write(struct ui_browser *browser, void *entry, int row)
  29. {
  30. struct perf_top_browser *top_browser = container_of(browser, struct perf_top_browser, b);
  31. struct sym_entry *syme = rb_entry(entry, struct sym_entry, rb_node);
  32. bool current_entry = ui_browser__is_current_entry(browser, row);
  33. struct symbol *symbol = sym_entry__symbol(syme);
  34. struct perf_top *top = browser->priv;
  35. int width = browser->width;
  36. double pcnt;
  37. pcnt = 100.0 - (100.0 * ((top_browser->sum_ksamples - syme->snap_count) /
  38. top_browser->sum_ksamples));
  39. ui_browser__set_percent_color(browser, pcnt, current_entry);
  40. if (top->evlist->nr_entries == 1 || !top->display_weighted) {
  41. slsmg_printf("%20.2f ", syme->weight);
  42. width -= 24;
  43. } else {
  44. slsmg_printf("%9.1f %10ld ", syme->weight, syme->snap_count);
  45. width -= 23;
  46. }
  47. slsmg_printf("%4.1f%%", pcnt);
  48. width -= 7;
  49. if (verbose) {
  50. slsmg_printf(" %016" PRIx64, symbol->start);
  51. width -= 17;
  52. }
  53. slsmg_printf(" %-*.*s ", top_browser->sym_width, top_browser->sym_width,
  54. symbol->name);
  55. width -= top_browser->sym_width;
  56. slsmg_write_nstring(width >= syme->map->dso->long_name_len ?
  57. syme->map->dso->long_name :
  58. syme->map->dso->short_name, width);
  59. if (current_entry)
  60. top_browser->selection = syme;
  61. }
  62. static void perf_top_browser__update_rb_tree(struct perf_top_browser *browser)
  63. {
  64. struct perf_top *top = browser->b.priv;
  65. u64 top_idx = browser->b.top_idx;
  66. browser->root = RB_ROOT;
  67. browser->b.top = NULL;
  68. browser->sum_ksamples = perf_top__decay_samples(top, &browser->root);
  69. /*
  70. * No active symbols
  71. */
  72. if (top->rb_entries == 0)
  73. return;
  74. perf_top__find_widths(top, &browser->root, &browser->dso_width,
  75. &browser->dso_short_width,
  76. &browser->sym_width);
  77. if (browser->sym_width + browser->dso_width > browser->b.width - 29) {
  78. browser->dso_width = browser->dso_short_width;
  79. if (browser->sym_width + browser->dso_width > browser->b.width - 29)
  80. browser->sym_width = browser->b.width - browser->dso_width - 29;
  81. }
  82. /*
  83. * Adjust the ui_browser indexes since the entries in the browser->root
  84. * rb_tree may have changed, then seek it from start, so that we get a
  85. * possible new top of the screen.
  86. */
  87. browser->b.nr_entries = top->rb_entries;
  88. if (top_idx >= browser->b.nr_entries) {
  89. if (browser->b.height >= browser->b.nr_entries)
  90. top_idx = browser->b.nr_entries - browser->b.height;
  91. else
  92. top_idx = 0;
  93. }
  94. if (browser->b.index >= top_idx + browser->b.height)
  95. browser->b.index = top_idx + browser->b.index - browser->b.top_idx;
  96. if (browser->b.index >= browser->b.nr_entries)
  97. browser->b.index = browser->b.nr_entries - 1;
  98. browser->b.top_idx = top_idx;
  99. browser->b.seek(&browser->b, top_idx, SEEK_SET);
  100. }
  101. static void perf_top_browser__annotate(struct perf_top_browser *browser)
  102. {
  103. struct sym_entry *syme = browser->selection;
  104. struct symbol *sym = sym_entry__symbol(syme);
  105. struct annotation *notes = symbol__annotation(sym);
  106. struct perf_top *top = browser->b.priv;
  107. if (notes->src != NULL)
  108. goto do_annotation;
  109. pthread_mutex_lock(&notes->lock);
  110. top->sym_filter_entry = NULL;
  111. if (symbol__alloc_hist(sym, top->evlist->nr_entries) < 0) {
  112. pr_err("Not enough memory for annotating '%s' symbol!\n",
  113. sym->name);
  114. pthread_mutex_unlock(&notes->lock);
  115. return;
  116. }
  117. top->sym_filter_entry = syme;
  118. pthread_mutex_unlock(&notes->lock);
  119. do_annotation:
  120. symbol__tui_annotate(sym, syme->map, 0, top->delay_secs * 1000);
  121. }
  122. static int perf_top_browser__run(struct perf_top_browser *browser)
  123. {
  124. int key;
  125. char title[160];
  126. struct perf_top *top = browser->b.priv;
  127. int delay_msecs = top->delay_secs * 1000;
  128. int exit_keys[] = { 'a', NEWT_KEY_ENTER, NEWT_KEY_RIGHT, 0, };
  129. perf_top_browser__update_rb_tree(browser);
  130. perf_top__header_snprintf(top, title, sizeof(title));
  131. perf_top__reset_sample_counters(top);
  132. if (ui_browser__show(&browser->b, title,
  133. "ESC: exit, ENTER|->|a: Live Annotate") < 0)
  134. return -1;
  135. newtFormSetTimer(browser->b.form, delay_msecs);
  136. ui_browser__add_exit_keys(&browser->b, exit_keys);
  137. while (1) {
  138. key = ui_browser__run(&browser->b);
  139. switch (key) {
  140. case -1:
  141. /* FIXME we need to check if it was es.reason == NEWT_EXIT_TIMER */
  142. perf_top_browser__update_rb_tree(browser);
  143. perf_top__header_snprintf(top, title, sizeof(title));
  144. perf_top__reset_sample_counters(top);
  145. ui_browser__set_color(&browser->b, NEWT_COLORSET_ROOT);
  146. SLsmg_gotorc(0, 0);
  147. slsmg_write_nstring(title, browser->b.width);
  148. break;
  149. case 'a':
  150. case NEWT_KEY_RIGHT:
  151. case NEWT_KEY_ENTER:
  152. if (browser->selection)
  153. perf_top_browser__annotate(browser);
  154. break;
  155. case NEWT_KEY_LEFT:
  156. continue;
  157. case NEWT_KEY_ESCAPE:
  158. if (!ui__dialog_yesno("Do you really want to exit?"))
  159. continue;
  160. /* Fall thru */
  161. default:
  162. goto out;
  163. }
  164. }
  165. out:
  166. ui_browser__hide(&browser->b);
  167. return key;
  168. }
  169. int perf_top__tui_browser(struct perf_top *top)
  170. {
  171. struct perf_top_browser browser = {
  172. .b = {
  173. .entries = &browser.root,
  174. .refresh = ui_browser__rb_tree_refresh,
  175. .seek = ui_browser__rb_tree_seek,
  176. .write = perf_top_browser__write,
  177. .priv = top,
  178. },
  179. };
  180. ui_helpline__push("Press <- or ESC to exit");
  181. return perf_top_browser__run(&browser);
  182. }