top.c 5.6 KB

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