top.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "../ui.h"
  15. #include "../../evlist.h"
  16. #include "../../hist.h"
  17. #include "../../sort.h"
  18. #include "../../symbol.h"
  19. #include "../../session.h"
  20. #include "../../top.h"
  21. struct perf_top_browser {
  22. struct ui_browser b;
  23. struct rb_root root;
  24. struct sym_entry *selection;
  25. float sum_ksamples;
  26. int dso_width;
  27. int dso_short_width;
  28. int sym_width;
  29. };
  30. static void perf_top_browser__write(struct ui_browser *browser, void *entry, int row)
  31. {
  32. struct perf_top_browser *top_browser = container_of(browser, struct perf_top_browser, b);
  33. struct sym_entry *syme = rb_entry(entry, struct sym_entry, rb_node);
  34. bool current_entry = ui_browser__is_current_entry(browser, row);
  35. struct symbol *symbol = sym_entry__symbol(syme);
  36. struct perf_top *top = browser->priv;
  37. int width = browser->width;
  38. double pcnt;
  39. pcnt = 100.0 - (100.0 * ((top_browser->sum_ksamples - syme->snap_count) /
  40. top_browser->sum_ksamples));
  41. ui_browser__set_percent_color(browser, pcnt, current_entry);
  42. if (top->evlist->nr_entries == 1 || !top->display_weighted) {
  43. slsmg_printf("%20.2f ", syme->weight);
  44. width -= 21;
  45. } else {
  46. slsmg_printf("%9.1f %10ld ", syme->weight, syme->snap_count);
  47. width -= 20;
  48. }
  49. slsmg_printf("%4.1f%%", pcnt);
  50. width -= 7;
  51. if (verbose) {
  52. slsmg_printf(" %016" PRIx64, symbol->start);
  53. width -= 17;
  54. }
  55. slsmg_printf(" %-*.*s ", top_browser->sym_width, top_browser->sym_width,
  56. symbol->name);
  57. width -= top_browser->sym_width;
  58. slsmg_write_nstring(width >= syme->map->dso->long_name_len ?
  59. syme->map->dso->long_name :
  60. syme->map->dso->short_name, width);
  61. if (current_entry)
  62. top_browser->selection = syme;
  63. }
  64. static void perf_top_browser__update_rb_tree(struct perf_top_browser *browser)
  65. {
  66. struct perf_top *top = browser->b.priv;
  67. u64 top_idx = browser->b.top_idx;
  68. browser->root = RB_ROOT;
  69. browser->b.top = NULL;
  70. browser->sum_ksamples = perf_top__decay_samples(top, &browser->root);
  71. /*
  72. * No active symbols
  73. */
  74. if (top->rb_entries == 0)
  75. return;
  76. perf_top__find_widths(top, &browser->root, &browser->dso_width,
  77. &browser->dso_short_width,
  78. &browser->sym_width);
  79. if (browser->sym_width + browser->dso_width > browser->b.width - 29) {
  80. browser->dso_width = browser->dso_short_width;
  81. if (browser->sym_width + browser->dso_width > browser->b.width - 29)
  82. browser->sym_width = browser->b.width - browser->dso_width - 29;
  83. }
  84. /*
  85. * Adjust the ui_browser indexes since the entries in the browser->root
  86. * rb_tree may have changed, then seek it from start, so that we get a
  87. * possible new top of the screen.
  88. */
  89. browser->b.nr_entries = top->rb_entries;
  90. if (top_idx >= browser->b.nr_entries) {
  91. if (browser->b.height >= browser->b.nr_entries)
  92. top_idx = browser->b.nr_entries - browser->b.height;
  93. else
  94. top_idx = 0;
  95. }
  96. if (browser->b.index >= top_idx + browser->b.height)
  97. browser->b.index = top_idx + browser->b.index - browser->b.top_idx;
  98. if (browser->b.index >= browser->b.nr_entries)
  99. browser->b.index = browser->b.nr_entries - 1;
  100. browser->b.top_idx = top_idx;
  101. browser->b.seek(&browser->b, top_idx, SEEK_SET);
  102. }
  103. static void perf_top_browser__annotate(struct perf_top_browser *browser)
  104. {
  105. struct sym_entry *syme = browser->selection;
  106. struct symbol *sym = sym_entry__symbol(syme);
  107. struct annotation *notes = symbol__annotation(sym);
  108. struct perf_top *top = browser->b.priv;
  109. if (notes->src != NULL)
  110. goto do_annotation;
  111. pthread_mutex_lock(&notes->lock);
  112. top->sym_filter_entry = NULL;
  113. if (symbol__alloc_hist(sym, top->evlist->nr_entries) < 0) {
  114. pr_err("Not enough memory for annotating '%s' symbol!\n",
  115. sym->name);
  116. pthread_mutex_unlock(&notes->lock);
  117. return;
  118. }
  119. top->sym_filter_entry = syme;
  120. pthread_mutex_unlock(&notes->lock);
  121. do_annotation:
  122. symbol__tui_annotate(sym, syme->map, 0, top->delay_secs * 1000);
  123. }
  124. static void perf_top_browser__warn_lost(struct perf_top_browser *browser)
  125. {
  126. struct perf_top *top = browser->b.priv;
  127. char msg[128];
  128. int len;
  129. top->total_lost_warned = top->session->hists.stats.total_lost;
  130. pthread_mutex_lock(&ui__lock);
  131. ui_browser__set_color(&browser->b, HE_COLORSET_TOP);
  132. len = snprintf(msg, sizeof(msg),
  133. " WARNING: LOST %" PRIu64 " events, Check IO/CPU overload",
  134. top->total_lost_warned);
  135. if (len > browser->b.width)
  136. len = browser->b.width;
  137. SLsmg_gotorc(0, browser->b.width - len);
  138. slsmg_write_nstring(msg, len);
  139. pthread_mutex_unlock(&ui__lock);
  140. }
  141. static int perf_top_browser__run(struct perf_top_browser *browser)
  142. {
  143. int key;
  144. char title[160];
  145. struct perf_top *top = browser->b.priv;
  146. int delay_msecs = top->delay_secs * 1000;
  147. int exit_keys[] = { 'a', NEWT_KEY_ENTER, NEWT_KEY_RIGHT, 0, };
  148. perf_top_browser__update_rb_tree(browser);
  149. perf_top__header_snprintf(top, title, sizeof(title));
  150. perf_top__reset_sample_counters(top);
  151. if (ui_browser__show(&browser->b, title,
  152. "ESC: exit, ENTER|->|a: Live Annotate") < 0)
  153. return -1;
  154. newtFormSetTimer(browser->b.form, delay_msecs);
  155. ui_browser__add_exit_keys(&browser->b, exit_keys);
  156. while (1) {
  157. key = ui_browser__run(&browser->b);
  158. switch (key) {
  159. case -1:
  160. /* FIXME we need to check if it was es.reason == NEWT_EXIT_TIMER */
  161. perf_top_browser__update_rb_tree(browser);
  162. perf_top__header_snprintf(top, title, sizeof(title));
  163. perf_top__reset_sample_counters(top);
  164. ui_browser__set_color(&browser->b, NEWT_COLORSET_ROOT);
  165. SLsmg_gotorc(0, 0);
  166. slsmg_write_nstring(title, browser->b.width);
  167. if (top->total_lost_warned != top->session->hists.stats.total_lost)
  168. perf_top_browser__warn_lost(browser);
  169. break;
  170. case 'a':
  171. case NEWT_KEY_RIGHT:
  172. case NEWT_KEY_ENTER:
  173. if (browser->selection)
  174. perf_top_browser__annotate(browser);
  175. break;
  176. case NEWT_KEY_LEFT:
  177. continue;
  178. case NEWT_KEY_ESCAPE:
  179. if (!ui__dialog_yesno("Do you really want to exit?"))
  180. continue;
  181. /* Fall thru */
  182. default:
  183. goto out;
  184. }
  185. }
  186. out:
  187. ui_browser__hide(&browser->b);
  188. return key;
  189. }
  190. int perf_top__tui_browser(struct perf_top *top)
  191. {
  192. struct perf_top_browser browser = {
  193. .b = {
  194. .entries = &browser.root,
  195. .refresh = ui_browser__rb_tree_refresh,
  196. .seek = ui_browser__rb_tree_seek,
  197. .write = perf_top_browser__write,
  198. .priv = top,
  199. },
  200. };
  201. return perf_top_browser__run(&browser);
  202. }