newt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #undef _GNU_SOURCE
  4. #include <stdlib.h>
  5. #include <newt.h>
  6. #include <sys/ttydefaults.h>
  7. #include "cache.h"
  8. #include "hist.h"
  9. #include "session.h"
  10. #include "sort.h"
  11. #include "symbol.h"
  12. struct ui_progress {
  13. newtComponent form, scale;
  14. };
  15. struct ui_progress *ui_progress__new(const char *title, u64 total)
  16. {
  17. struct ui_progress *self = malloc(sizeof(*self));
  18. if (self != NULL) {
  19. int cols;
  20. newtGetScreenSize(&cols, NULL);
  21. cols -= 4;
  22. newtCenteredWindow(cols, 1, title);
  23. self->form = newtForm(NULL, NULL, 0);
  24. if (self->form == NULL)
  25. goto out_free_self;
  26. self->scale = newtScale(0, 0, cols, total);
  27. if (self->scale == NULL)
  28. goto out_free_form;
  29. newtFormAddComponents(self->form, self->scale, NULL);
  30. newtRefresh();
  31. }
  32. return self;
  33. out_free_form:
  34. newtFormDestroy(self->form);
  35. out_free_self:
  36. free(self);
  37. return NULL;
  38. }
  39. void ui_progress__update(struct ui_progress *self, u64 curr)
  40. {
  41. newtScaleSet(self->scale, curr);
  42. newtRefresh();
  43. }
  44. void ui_progress__delete(struct ui_progress *self)
  45. {
  46. newtFormDestroy(self->form);
  47. newtPopWindow();
  48. free(self);
  49. }
  50. static char browser__last_msg[1024];
  51. int browser__show_help(const char *format, va_list ap)
  52. {
  53. int ret;
  54. static int backlog;
  55. ret = vsnprintf(browser__last_msg + backlog,
  56. sizeof(browser__last_msg) - backlog, format, ap);
  57. backlog += ret;
  58. if (browser__last_msg[backlog - 1] == '\n') {
  59. newtPopHelpLine();
  60. newtPushHelpLine(browser__last_msg);
  61. newtRefresh();
  62. backlog = 0;
  63. }
  64. return ret;
  65. }
  66. static void newt_form__set_exit_keys(newtComponent self)
  67. {
  68. newtFormAddHotKey(self, NEWT_KEY_ESCAPE);
  69. newtFormAddHotKey(self, 'Q');
  70. newtFormAddHotKey(self, 'q');
  71. newtFormAddHotKey(self, CTRL('c'));
  72. }
  73. static newtComponent newt_form__new(void)
  74. {
  75. newtComponent self = newtForm(NULL, NULL, 0);
  76. if (self)
  77. newt_form__set_exit_keys(self);
  78. return self;
  79. }
  80. static int popup_menu(int argc, char * const argv[])
  81. {
  82. struct newtExitStruct es;
  83. int i, rc = -1, max_len = 5;
  84. newtComponent listbox, form = newt_form__new();
  85. if (form == NULL)
  86. return -1;
  87. listbox = newtListbox(0, 0, argc, NEWT_FLAG_RETURNEXIT);
  88. if (listbox == NULL)
  89. goto out_destroy_form;
  90. newtFormAddComponents(form, listbox, NULL);
  91. for (i = 0; i < argc; ++i) {
  92. int len = strlen(argv[i]);
  93. if (len > max_len)
  94. max_len = len;
  95. if (newtListboxAddEntry(listbox, argv[i], (void *)(long)i))
  96. goto out_destroy_form;
  97. }
  98. newtCenteredWindow(max_len, argc, NULL);
  99. newtFormRun(form, &es);
  100. rc = newtListboxGetCurrent(listbox) - NULL;
  101. if (es.reason == NEWT_EXIT_HOTKEY)
  102. rc = -1;
  103. newtPopWindow();
  104. out_destroy_form:
  105. newtFormDestroy(form);
  106. return rc;
  107. }
  108. static bool dialog_yesno(const char *msg)
  109. {
  110. /* newtWinChoice should really be accepting const char pointers... */
  111. char yes[] = "Yes", no[] = "No";
  112. return newtWinChoice(NULL, no, yes, (char *)msg) == 2;
  113. }
  114. /*
  115. * When debugging newt problems it was useful to be able to "unroll"
  116. * the calls to newtCheckBoxTreeAdd{Array,Item}, so that we can generate
  117. * a source file with the sequence of calls to these methods, to then
  118. * tweak the arrays to get the intended results, so I'm keeping this code
  119. * here, may be useful again in the future.
  120. */
  121. #undef NEWT_DEBUG
  122. static void newt_checkbox_tree__add(newtComponent tree, const char *str,
  123. void *priv, int *indexes)
  124. {
  125. #ifdef NEWT_DEBUG
  126. /* Print the newtCheckboxTreeAddArray to tinker with its index arrays */
  127. int i = 0, len = 40 - strlen(str);
  128. fprintf(stderr,
  129. "\tnewtCheckboxTreeAddItem(tree, %*.*s\"%s\", (void *)%p, 0, ",
  130. len, len, " ", str, priv);
  131. while (indexes[i] != NEWT_ARG_LAST) {
  132. if (indexes[i] != NEWT_ARG_APPEND)
  133. fprintf(stderr, " %d,", indexes[i]);
  134. else
  135. fprintf(stderr, " %s,", "NEWT_ARG_APPEND");
  136. ++i;
  137. }
  138. fprintf(stderr, " %s", " NEWT_ARG_LAST);\n");
  139. fflush(stderr);
  140. #endif
  141. newtCheckboxTreeAddArray(tree, str, priv, 0, indexes);
  142. }
  143. static char *callchain_list__sym_name(struct callchain_list *self,
  144. char *bf, size_t bfsize)
  145. {
  146. if (self->ms.sym)
  147. return self->ms.sym->name;
  148. snprintf(bf, bfsize, "%#Lx", self->ip);
  149. return bf;
  150. }
  151. static void __callchain__append_graph_browser(struct callchain_node *self,
  152. newtComponent tree, u64 total,
  153. int *indexes, int depth)
  154. {
  155. struct rb_node *node;
  156. u64 new_total, remaining;
  157. int idx = 0;
  158. if (callchain_param.mode == CHAIN_GRAPH_REL)
  159. new_total = self->children_hit;
  160. else
  161. new_total = total;
  162. remaining = new_total;
  163. node = rb_first(&self->rb_root);
  164. while (node) {
  165. struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
  166. struct rb_node *next = rb_next(node);
  167. u64 cumul = cumul_hits(child);
  168. struct callchain_list *chain;
  169. int first = true, printed = 0;
  170. int chain_idx = -1;
  171. remaining -= cumul;
  172. indexes[depth] = NEWT_ARG_APPEND;
  173. indexes[depth + 1] = NEWT_ARG_LAST;
  174. list_for_each_entry(chain, &child->val, list) {
  175. char ipstr[BITS_PER_LONG / 4 + 1],
  176. *alloc_str = NULL;
  177. const char *str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
  178. if (first) {
  179. double percent = cumul * 100.0 / new_total;
  180. first = false;
  181. if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
  182. str = "Not enough memory!";
  183. else
  184. str = alloc_str;
  185. } else {
  186. indexes[depth] = idx;
  187. indexes[depth + 1] = NEWT_ARG_APPEND;
  188. indexes[depth + 2] = NEWT_ARG_LAST;
  189. ++chain_idx;
  190. }
  191. newt_checkbox_tree__add(tree, str, &chain->ms, indexes);
  192. free(alloc_str);
  193. ++printed;
  194. }
  195. indexes[depth] = idx;
  196. if (chain_idx != -1)
  197. indexes[depth + 1] = chain_idx;
  198. if (printed != 0)
  199. ++idx;
  200. __callchain__append_graph_browser(child, tree, new_total, indexes,
  201. depth + (chain_idx != -1 ? 2 : 1));
  202. node = next;
  203. }
  204. }
  205. static void callchain__append_graph_browser(struct callchain_node *self,
  206. newtComponent tree, u64 total,
  207. int *indexes, int parent_idx)
  208. {
  209. struct callchain_list *chain;
  210. int i = 0;
  211. indexes[1] = NEWT_ARG_APPEND;
  212. indexes[2] = NEWT_ARG_LAST;
  213. list_for_each_entry(chain, &self->val, list) {
  214. char ipstr[BITS_PER_LONG / 4 + 1], *str;
  215. if (chain->ip >= PERF_CONTEXT_MAX)
  216. continue;
  217. if (!i++ && sort__first_dimension == SORT_SYM)
  218. continue;
  219. str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
  220. newt_checkbox_tree__add(tree, str, &chain->ms, indexes);
  221. }
  222. indexes[1] = parent_idx;
  223. indexes[2] = NEWT_ARG_APPEND;
  224. indexes[3] = NEWT_ARG_LAST;
  225. __callchain__append_graph_browser(self, tree, total, indexes, 2);
  226. }
  227. static void hist_entry__append_callchain_browser(struct hist_entry *self,
  228. newtComponent tree, u64 total, int parent_idx)
  229. {
  230. struct rb_node *rb_node;
  231. int indexes[1024] = { [0] = parent_idx, };
  232. int idx = 0;
  233. struct callchain_node *chain;
  234. rb_node = rb_first(&self->sorted_chain);
  235. while (rb_node) {
  236. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  237. switch (callchain_param.mode) {
  238. case CHAIN_FLAT:
  239. break;
  240. case CHAIN_GRAPH_ABS: /* falldown */
  241. case CHAIN_GRAPH_REL:
  242. callchain__append_graph_browser(chain, tree, total, indexes, idx++);
  243. break;
  244. case CHAIN_NONE:
  245. default:
  246. break;
  247. }
  248. rb_node = rb_next(rb_node);
  249. }
  250. }
  251. static size_t hist_entry__append_browser(struct hist_entry *self,
  252. newtComponent tree, u64 total)
  253. {
  254. char s[256];
  255. size_t ret;
  256. if (symbol_conf.exclude_other && !self->parent)
  257. return 0;
  258. ret = hist_entry__snprintf(self, s, sizeof(s), NULL,
  259. false, 0, false, total);
  260. if (symbol_conf.use_callchain) {
  261. int indexes[2];
  262. indexes[0] = NEWT_ARG_APPEND;
  263. indexes[1] = NEWT_ARG_LAST;
  264. newt_checkbox_tree__add(tree, s, &self->ms, indexes);
  265. } else
  266. newtListboxAppendEntry(tree, s, &self->ms);
  267. return ret;
  268. }
  269. static void map_symbol__annotate_browser(const struct map_symbol *self,
  270. const char *input_name)
  271. {
  272. FILE *fp;
  273. int cols, rows;
  274. newtComponent form, tree;
  275. struct newtExitStruct es;
  276. char *str;
  277. size_t line_len, max_line_len = 0;
  278. size_t max_usable_width;
  279. char *line = NULL;
  280. if (self->sym == NULL)
  281. return;
  282. if (asprintf(&str, "perf annotate -i \"%s\" -d \"%s\" %s 2>&1 | expand",
  283. input_name, self->map->dso->name, self->sym->name) < 0)
  284. return;
  285. fp = popen(str, "r");
  286. if (fp == NULL)
  287. goto out_free_str;
  288. newtPushHelpLine("Press ESC to exit");
  289. newtGetScreenSize(&cols, &rows);
  290. tree = newtListbox(0, 0, rows - 5, NEWT_FLAG_SCROLL);
  291. while (!feof(fp)) {
  292. if (getline(&line, &line_len, fp) < 0 || !line_len)
  293. break;
  294. while (line_len != 0 && isspace(line[line_len - 1]))
  295. line[--line_len] = '\0';
  296. if (line_len > max_line_len)
  297. max_line_len = line_len;
  298. newtListboxAppendEntry(tree, line, NULL);
  299. }
  300. fclose(fp);
  301. free(line);
  302. max_usable_width = cols - 22;
  303. if (max_line_len > max_usable_width)
  304. max_line_len = max_usable_width;
  305. newtListboxSetWidth(tree, max_line_len);
  306. newtCenteredWindow(max_line_len + 2, rows - 5, self->sym->name);
  307. form = newt_form__new();
  308. newtFormAddComponents(form, tree, NULL);
  309. newtFormRun(form, &es);
  310. newtFormDestroy(form);
  311. newtPopWindow();
  312. newtPopHelpLine();
  313. out_free_str:
  314. free(str);
  315. }
  316. static const void *newt__symbol_tree_get_current(newtComponent self)
  317. {
  318. if (symbol_conf.use_callchain)
  319. return newtCheckboxTreeGetCurrent(self);
  320. return newtListboxGetCurrent(self);
  321. }
  322. static void hist_browser__selection(newtComponent self, void *data)
  323. {
  324. const struct map_symbol **symbol_ptr = data;
  325. *symbol_ptr = newt__symbol_tree_get_current(self);
  326. }
  327. struct hist_browser {
  328. newtComponent form, tree;
  329. const struct map_symbol *selection;
  330. };
  331. static struct hist_browser *hist_browser__new(void)
  332. {
  333. struct hist_browser *self = malloc(sizeof(*self));
  334. if (self != NULL)
  335. self->form = NULL;
  336. return self;
  337. }
  338. static void hist_browser__delete(struct hist_browser *self)
  339. {
  340. newtFormDestroy(self->form);
  341. newtPopWindow();
  342. free(self);
  343. }
  344. static int hist_browser__populate(struct hist_browser *self, struct rb_root *hists,
  345. u64 nr_hists, u64 session_total)
  346. {
  347. int max_len = 0, idx, cols, rows;
  348. struct ui_progress *progress;
  349. struct rb_node *nd;
  350. u64 curr_hist = 0;
  351. char seq[] = ".";
  352. char str[256];
  353. if (self->form) {
  354. newtFormDestroy(self->form);
  355. newtPopWindow();
  356. }
  357. snprintf(str, sizeof(str), "Samples: %Ld ",
  358. session_total);
  359. newtDrawRootText(0, 0, str);
  360. newtGetScreenSize(NULL, &rows);
  361. if (symbol_conf.use_callchain)
  362. self->tree = newtCheckboxTreeMulti(0, 0, rows - 5, seq,
  363. NEWT_FLAG_SCROLL);
  364. else
  365. self->tree = newtListbox(0, 0, rows - 5,
  366. (NEWT_FLAG_SCROLL |
  367. NEWT_FLAG_RETURNEXIT));
  368. newtComponentAddCallback(self->tree, hist_browser__selection,
  369. &self->selection);
  370. progress = ui_progress__new("Adding entries to the browser...", nr_hists);
  371. if (progress == NULL)
  372. return -1;
  373. idx = 0;
  374. for (nd = rb_first(hists); nd; nd = rb_next(nd)) {
  375. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  376. int len;
  377. if (h->filtered)
  378. continue;
  379. len = hist_entry__append_browser(h, self->tree, session_total);
  380. if (len > max_len)
  381. max_len = len;
  382. if (symbol_conf.use_callchain)
  383. hist_entry__append_callchain_browser(h, self->tree,
  384. session_total, idx++);
  385. ++curr_hist;
  386. if (curr_hist % 5)
  387. ui_progress__update(progress, curr_hist);
  388. }
  389. ui_progress__delete(progress);
  390. newtGetScreenSize(&cols, &rows);
  391. if (max_len > cols)
  392. max_len = cols - 3;
  393. if (!symbol_conf.use_callchain)
  394. newtListboxSetWidth(self->tree, max_len);
  395. newtCenteredWindow(max_len + (symbol_conf.use_callchain ? 5 : 0),
  396. rows - 5, "Report");
  397. self->form = newt_form__new();
  398. if (self->form == NULL)
  399. return -1;
  400. newtFormAddHotKey(self->form, 'A');
  401. newtFormAddHotKey(self->form, 'a');
  402. newtFormAddHotKey(self->form, NEWT_KEY_RIGHT);
  403. newtFormAddComponents(self->form, self->tree, NULL);
  404. self->selection = newt__symbol_tree_get_current(self->tree);
  405. return 0;
  406. }
  407. static u64 hists__filter_by_dso(struct rb_root *hists, struct dso *dso,
  408. u64 *session_total)
  409. {
  410. struct rb_node *nd;
  411. u64 nr_hists = 0;
  412. *session_total = 0;
  413. for (nd = rb_first(hists); nd; nd = rb_next(nd)) {
  414. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  415. if (dso != NULL && (h->ms.map == NULL || h->ms.map->dso != dso)) {
  416. h->filtered = true;
  417. continue;
  418. }
  419. h->filtered = false;
  420. ++nr_hists;
  421. *session_total += h->count;
  422. }
  423. return nr_hists;
  424. }
  425. int perf_session__browse_hists(struct rb_root *hists, u64 nr_hists,
  426. u64 session_total, const char *helpline,
  427. const char *input_name)
  428. {
  429. struct newtExitStruct es;
  430. bool dso_filtered = false;
  431. int err = -1;
  432. struct hist_browser *browser = hist_browser__new();
  433. if (browser == NULL)
  434. return -1;
  435. newtPushHelpLine(helpline);
  436. if (hist_browser__populate(browser, hists, nr_hists, session_total) < 0)
  437. goto out;
  438. while (1) {
  439. char *options[16];
  440. int nr_options = 0, choice = 0, i,
  441. annotate = -2, zoom_dso = -2;
  442. newtFormRun(browser->form, &es);
  443. if (es.reason == NEWT_EXIT_HOTKEY) {
  444. if (toupper(es.u.key) == 'A')
  445. goto do_annotate;
  446. if (es.u.key == NEWT_KEY_ESCAPE ||
  447. toupper(es.u.key) == 'Q' ||
  448. es.u.key == CTRL('c')) {
  449. if (dialog_yesno("Do you really want to exit?"))
  450. break;
  451. else
  452. continue;
  453. }
  454. }
  455. if (browser->selection->sym != NULL &&
  456. asprintf(&options[nr_options], "Annotate %s",
  457. browser->selection->sym->name) > 0)
  458. annotate = nr_options++;
  459. if (browser->selection->map != NULL &&
  460. asprintf(&options[nr_options], "Zoom %s %s DSO",
  461. dso_filtered ? "out of" : "into",
  462. (browser->selection->map->dso->kernel ? "the Kernel" :
  463. browser->selection->map->dso->short_name)) > 0)
  464. zoom_dso = nr_options++;
  465. options[nr_options++] = (char *)"Exit";
  466. choice = popup_menu(nr_options, options);
  467. for (i = 0; i < nr_options - 1; ++i)
  468. free(options[i]);
  469. if (choice == nr_options - 1)
  470. break;
  471. do_annotate:
  472. if (choice == annotate) {
  473. if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
  474. newtPopHelpLine();
  475. newtPushHelpLine("No vmlinux file found, can't "
  476. "annotate with just a "
  477. "kallsyms file");
  478. continue;
  479. }
  480. map_symbol__annotate_browser(browser->selection,
  481. input_name);
  482. } if (choice == zoom_dso) {
  483. hists__filter_by_dso(hists,
  484. dso_filtered ? NULL : browser->selection->map->dso,
  485. &session_total);
  486. dso_filtered = !dso_filtered;
  487. if (hist_browser__populate(browser, hists, nr_hists, session_total) < 0)
  488. goto out;
  489. }
  490. }
  491. err = 0;
  492. out:
  493. hist_browser__delete(browser);
  494. return err;
  495. }
  496. void setup_browser(void)
  497. {
  498. if (!isatty(1))
  499. return;
  500. use_browser = true;
  501. newtInit();
  502. newtCls();
  503. newtPushHelpLine(" ");
  504. }
  505. void exit_browser(bool wait_for_ok)
  506. {
  507. if (use_browser) {
  508. if (wait_for_ok) {
  509. char title[] = "Fatal Error", ok[] = "Ok";
  510. newtWinMessage(title, ok, browser__last_msg);
  511. }
  512. newtFinished();
  513. }
  514. }