newt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. /*
  252. * FIXME: get lib/string.c linked with perf somehow
  253. */
  254. static char *skip_spaces(const char *str)
  255. {
  256. while (isspace(*str))
  257. ++str;
  258. return (char *)str;
  259. }
  260. static char *strim(char *s)
  261. {
  262. size_t size;
  263. char *end;
  264. s = skip_spaces(s);
  265. size = strlen(s);
  266. if (!size)
  267. return s;
  268. end = s + size - 1;
  269. while (end >= s && isspace(*end))
  270. end--;
  271. *(end + 1) = '\0';
  272. return s;
  273. }
  274. static size_t hist_entry__append_browser(struct hist_entry *self,
  275. newtComponent tree, u64 total)
  276. {
  277. char bf[1024], *s;
  278. FILE *fp;
  279. if (symbol_conf.exclude_other && !self->parent)
  280. return 0;
  281. fp = fmemopen(bf, sizeof(bf), "w");
  282. if (fp == NULL)
  283. return 0;
  284. hist_entry__fprintf(self, NULL, false, 0, fp, total);
  285. fclose(fp);
  286. /*
  287. * FIXME: We shouldn't need to trim, as the printing routines shouldn't
  288. * add spaces it in the first place, the stdio output routines should
  289. * call a __snprintf method instead of the current __print (that
  290. * actually is a __fprintf) one, but get the raw string and _then_ add
  291. * the newline, as this is a detail of stdio printing, not needed in
  292. * other UIs, e.g. newt.
  293. */
  294. s = strim(bf);
  295. if (symbol_conf.use_callchain) {
  296. int indexes[2];
  297. indexes[0] = NEWT_ARG_APPEND;
  298. indexes[1] = NEWT_ARG_LAST;
  299. newt_checkbox_tree__add(tree, s, &self->ms, indexes);
  300. } else
  301. newtListboxAppendEntry(tree, s, &self->ms);
  302. return strlen(s);
  303. }
  304. static void map_symbol__annotate_browser(const struct map_symbol *self)
  305. {
  306. FILE *fp;
  307. int cols, rows;
  308. newtComponent form, tree;
  309. struct newtExitStruct es;
  310. char *str;
  311. size_t line_len, max_line_len = 0;
  312. size_t max_usable_width;
  313. char *line = NULL;
  314. if (self->sym == NULL)
  315. return;
  316. if (asprintf(&str, "perf annotate -d \"%s\" %s 2>&1 | expand",
  317. self->map->dso->name, self->sym->name) < 0)
  318. return;
  319. fp = popen(str, "r");
  320. if (fp == NULL)
  321. goto out_free_str;
  322. newtPushHelpLine("Press ESC to exit");
  323. newtGetScreenSize(&cols, &rows);
  324. tree = newtListbox(0, 0, rows - 5, NEWT_FLAG_SCROLL);
  325. while (!feof(fp)) {
  326. if (getline(&line, &line_len, fp) < 0 || !line_len)
  327. break;
  328. while (line_len != 0 && isspace(line[line_len - 1]))
  329. line[--line_len] = '\0';
  330. if (line_len > max_line_len)
  331. max_line_len = line_len;
  332. newtListboxAppendEntry(tree, line, NULL);
  333. }
  334. fclose(fp);
  335. free(line);
  336. max_usable_width = cols - 22;
  337. if (max_line_len > max_usable_width)
  338. max_line_len = max_usable_width;
  339. newtListboxSetWidth(tree, max_line_len);
  340. newtCenteredWindow(max_line_len + 2, rows - 5, self->sym->name);
  341. form = newt_form__new();
  342. newtFormAddComponents(form, tree, NULL);
  343. newtFormRun(form, &es);
  344. newtFormDestroy(form);
  345. newtPopWindow();
  346. newtPopHelpLine();
  347. out_free_str:
  348. free(str);
  349. }
  350. static const void *newt__symbol_tree_get_current(newtComponent self)
  351. {
  352. if (symbol_conf.use_callchain)
  353. return newtCheckboxTreeGetCurrent(self);
  354. return newtListboxGetCurrent(self);
  355. }
  356. static void perf_session__selection(newtComponent self, void *data)
  357. {
  358. const struct map_symbol **symbol_ptr = data;
  359. *symbol_ptr = newt__symbol_tree_get_current(self);
  360. }
  361. int perf_session__browse_hists(struct rb_root *hists, u64 nr_hists,
  362. u64 session_total, const char *helpline)
  363. {
  364. struct sort_entry *se;
  365. struct rb_node *nd;
  366. char seq[] = ".";
  367. unsigned int width;
  368. char *col_width = symbol_conf.col_width_list_str;
  369. int rows, cols, idx;
  370. int max_len = 0;
  371. char str[1024];
  372. newtComponent form, tree;
  373. struct newtExitStruct es;
  374. const struct map_symbol *selection;
  375. u64 curr_hist = 0;
  376. struct ui_progress *progress;
  377. progress = ui_progress__new("Adding entries to the browser...", nr_hists);
  378. if (progress == NULL)
  379. return -1;
  380. snprintf(str, sizeof(str), "Samples: %Ld", session_total);
  381. newtDrawRootText(0, 0, str);
  382. newtPushHelpLine(helpline);
  383. newtGetScreenSize(&cols, &rows);
  384. if (symbol_conf.use_callchain)
  385. tree = newtCheckboxTreeMulti(0, 0, rows - 5, seq,
  386. NEWT_FLAG_SCROLL);
  387. else
  388. tree = newtListbox(0, 0, rows - 5, (NEWT_FLAG_SCROLL |
  389. NEWT_FLAG_RETURNEXIT));
  390. newtComponentAddCallback(tree, perf_session__selection, &selection);
  391. list_for_each_entry(se, &hist_entry__sort_list, list) {
  392. if (se->elide)
  393. continue;
  394. width = strlen(se->header);
  395. if (se->width) {
  396. if (symbol_conf.col_width_list_str) {
  397. if (col_width) {
  398. *se->width = atoi(col_width);
  399. col_width = strchr(col_width, ',');
  400. if (col_width)
  401. ++col_width;
  402. }
  403. }
  404. *se->width = max(*se->width, width);
  405. }
  406. }
  407. idx = 0;
  408. for (nd = rb_first(hists); nd; nd = rb_next(nd)) {
  409. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  410. int len = hist_entry__append_browser(h, tree, session_total);
  411. if (len > max_len)
  412. max_len = len;
  413. if (symbol_conf.use_callchain)
  414. hist_entry__append_callchain_browser(h, tree, session_total, idx++);
  415. ++curr_hist;
  416. if (curr_hist % 5)
  417. ui_progress__update(progress, curr_hist);
  418. }
  419. ui_progress__delete(progress);
  420. if (max_len > cols)
  421. max_len = cols - 3;
  422. if (!symbol_conf.use_callchain)
  423. newtListboxSetWidth(tree, max_len);
  424. newtCenteredWindow(max_len + (symbol_conf.use_callchain ? 5 : 0),
  425. rows - 5, "Report");
  426. form = newt_form__new();
  427. newtFormAddHotKey(form, 'A');
  428. newtFormAddHotKey(form, 'a');
  429. newtFormAddHotKey(form, NEWT_KEY_RIGHT);
  430. newtFormAddComponents(form, tree, NULL);
  431. selection = newt__symbol_tree_get_current(tree);
  432. while (1) {
  433. char annotate[512];
  434. const char *options[2];
  435. int nr_options = 0, choice = 0;
  436. newtFormRun(form, &es);
  437. if (es.reason == NEWT_EXIT_HOTKEY) {
  438. if (toupper(es.u.key) == 'A')
  439. goto do_annotate;
  440. if (es.u.key == NEWT_KEY_ESCAPE ||
  441. toupper(es.u.key) == 'Q' ||
  442. es.u.key == CTRL('c')) {
  443. if (dialog_yesno("Do you really want to exit?"))
  444. break;
  445. else
  446. continue;
  447. }
  448. }
  449. if (selection->sym != NULL) {
  450. snprintf(annotate, sizeof(annotate),
  451. "Annotate %s", selection->sym->name);
  452. options[nr_options++] = annotate;
  453. }
  454. options[nr_options++] = "Exit";
  455. choice = popup_menu(nr_options, options);
  456. if (choice == nr_options - 1)
  457. break;
  458. do_annotate:
  459. if (selection->sym != NULL && choice >= 0) {
  460. if (selection->map->dso->origin == DSO__ORIG_KERNEL) {
  461. newtPopHelpLine();
  462. newtPushHelpLine("No vmlinux file found, can't "
  463. "annotate with just a "
  464. "kallsyms file");
  465. continue;
  466. }
  467. map_symbol__annotate_browser(selection);
  468. }
  469. }
  470. newtFormDestroy(form);
  471. newtPopWindow();
  472. return 0;
  473. }
  474. void setup_browser(void)
  475. {
  476. if (!isatty(1))
  477. return;
  478. use_browser = true;
  479. newtInit();
  480. newtCls();
  481. newtPushHelpLine(" ");
  482. }
  483. void exit_browser(bool wait_for_ok)
  484. {
  485. if (use_browser) {
  486. if (wait_for_ok) {
  487. char title[] = "Fatal Error", ok[] = "Ok";
  488. newtWinMessage(title, ok, browser__last_msg);
  489. }
  490. newtFinished();
  491. }
  492. }