newt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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, const char *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. char seq[] = ".";
  336. int rows;
  337. newtGetScreenSize(NULL, &rows);
  338. if (symbol_conf.use_callchain)
  339. self->tree = newtCheckboxTreeMulti(0, 0, rows - 5, seq,
  340. NEWT_FLAG_SCROLL);
  341. else
  342. self->tree = newtListbox(0, 0, rows - 5,
  343. (NEWT_FLAG_SCROLL |
  344. NEWT_FLAG_RETURNEXIT));
  345. newtComponentAddCallback(self->tree, hist_browser__selection,
  346. &self->selection);
  347. }
  348. return self;
  349. }
  350. static void hist_browser__delete(struct hist_browser *self)
  351. {
  352. newtFormDestroy(self->form);
  353. newtPopWindow();
  354. free(self);
  355. }
  356. static int hist_browser__populate(struct hist_browser *self, struct rb_root *hists,
  357. u64 nr_hists, u64 session_total)
  358. {
  359. int max_len = 0, idx, cols, rows;
  360. struct ui_progress *progress;
  361. struct rb_node *nd;
  362. u64 curr_hist = 0;
  363. progress = ui_progress__new("Adding entries to the browser...", nr_hists);
  364. if (progress == NULL)
  365. return -1;
  366. idx = 0;
  367. for (nd = rb_first(hists); nd; nd = rb_next(nd)) {
  368. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  369. int len = hist_entry__append_browser(h, self->tree, session_total);
  370. if (len > max_len)
  371. max_len = len;
  372. if (symbol_conf.use_callchain)
  373. hist_entry__append_callchain_browser(h, self->tree,
  374. session_total, idx++);
  375. ++curr_hist;
  376. if (curr_hist % 5)
  377. ui_progress__update(progress, curr_hist);
  378. }
  379. ui_progress__delete(progress);
  380. newtGetScreenSize(&cols, &rows);
  381. if (max_len > cols)
  382. max_len = cols - 3;
  383. if (!symbol_conf.use_callchain)
  384. newtListboxSetWidth(self->tree, max_len);
  385. newtCenteredWindow(max_len + (symbol_conf.use_callchain ? 5 : 0),
  386. rows - 5, "Report");
  387. self->form = newt_form__new();
  388. newtFormAddHotKey(self->form, 'A');
  389. newtFormAddHotKey(self->form, 'a');
  390. newtFormAddHotKey(self->form, NEWT_KEY_RIGHT);
  391. newtFormAddComponents(self->form, self->tree, NULL);
  392. self->selection = newt__symbol_tree_get_current(self->tree);
  393. return 0;
  394. }
  395. int perf_session__browse_hists(struct rb_root *hists, u64 nr_hists,
  396. u64 session_total, const char *helpline,
  397. const char *input_name)
  398. {
  399. struct newtExitStruct es;
  400. char str[1024];
  401. int err = -1;
  402. struct hist_browser *browser = hist_browser__new();
  403. if (browser == NULL)
  404. return -1;
  405. snprintf(str, sizeof(str), "Samples: %Ld", session_total);
  406. newtDrawRootText(0, 0, str);
  407. newtPushHelpLine(helpline);
  408. if (hist_browser__populate(browser, hists, nr_hists, session_total) < 0)
  409. goto out;
  410. while (1) {
  411. char annotate[512];
  412. const char *options[2];
  413. int nr_options = 0, choice = 0;
  414. newtFormRun(browser->form, &es);
  415. if (es.reason == NEWT_EXIT_HOTKEY) {
  416. if (toupper(es.u.key) == 'A')
  417. goto do_annotate;
  418. if (es.u.key == NEWT_KEY_ESCAPE ||
  419. toupper(es.u.key) == 'Q' ||
  420. es.u.key == CTRL('c')) {
  421. if (dialog_yesno("Do you really want to exit?"))
  422. break;
  423. else
  424. continue;
  425. }
  426. }
  427. if (browser->selection->sym != NULL) {
  428. snprintf(annotate, sizeof(annotate),
  429. "Annotate %s", browser->selection->sym->name);
  430. options[nr_options++] = annotate;
  431. }
  432. options[nr_options++] = "Exit";
  433. choice = popup_menu(nr_options, options);
  434. if (choice == nr_options - 1)
  435. break;
  436. do_annotate:
  437. if (browser->selection->sym != NULL && choice >= 0) {
  438. if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
  439. newtPopHelpLine();
  440. newtPushHelpLine("No vmlinux file found, can't "
  441. "annotate with just a "
  442. "kallsyms file");
  443. continue;
  444. }
  445. map_symbol__annotate_browser(browser->selection,
  446. input_name);
  447. }
  448. }
  449. err = 0;
  450. out:
  451. hist_browser__delete(browser);
  452. return err;
  453. }
  454. void setup_browser(void)
  455. {
  456. if (!isatty(1))
  457. return;
  458. use_browser = true;
  459. newtInit();
  460. newtCls();
  461. newtPushHelpLine(" ");
  462. }
  463. void exit_browser(bool wait_for_ok)
  464. {
  465. if (use_browser) {
  466. if (wait_for_ok) {
  467. char title[] = "Fatal Error", ok[] = "Ok";
  468. newtWinMessage(title, ok, browser__last_msg);
  469. }
  470. newtFinished();
  471. }
  472. }