newt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. static void newt_form__set_exit_keys(newtComponent self)
  13. {
  14. newtFormAddHotKey(self, NEWT_KEY_ESCAPE);
  15. newtFormAddHotKey(self, 'Q');
  16. newtFormAddHotKey(self, 'q');
  17. newtFormAddHotKey(self, CTRL('c'));
  18. }
  19. static newtComponent newt_form__new(void)
  20. {
  21. newtComponent self = newtForm(NULL, NULL, 0);
  22. if (self)
  23. newt_form__set_exit_keys(self);
  24. return self;
  25. }
  26. static int popup_menu(int argc, const char *argv[])
  27. {
  28. struct newtExitStruct es;
  29. int i, rc = -1, max_len = 5;
  30. newtComponent listbox, form = newt_form__new();
  31. if (form == NULL)
  32. return -1;
  33. listbox = newtListbox(0, 0, argc, NEWT_FLAG_RETURNEXIT);
  34. if (listbox == NULL)
  35. goto out_destroy_form;
  36. newtFormAddComponents(form, listbox, NULL);
  37. for (i = 0; i < argc; ++i) {
  38. int len = strlen(argv[i]);
  39. if (len > max_len)
  40. max_len = len;
  41. if (newtListboxAddEntry(listbox, argv[i], (void *)(long)i))
  42. goto out_destroy_form;
  43. }
  44. newtCenteredWindow(max_len, argc, NULL);
  45. newtFormRun(form, &es);
  46. rc = newtListboxGetCurrent(listbox) - NULL;
  47. if (es.reason == NEWT_EXIT_HOTKEY)
  48. rc = -1;
  49. newtPopWindow();
  50. out_destroy_form:
  51. newtFormDestroy(form);
  52. return rc;
  53. }
  54. static bool dialog_yesno(const char *msg)
  55. {
  56. /* newtWinChoice should really be accepting const char pointers... */
  57. char yes[] = "Yes", no[] = "No";
  58. return newtWinChoice(NULL, no, yes, (char *)msg) == 2;
  59. }
  60. /*
  61. * When debugging newt problems it was useful to be able to "unroll"
  62. * the calls to newtCheckBoxTreeAdd{Array,Item}, so that we can generate
  63. * a source file with the sequence of calls to these methods, to then
  64. * tweak the arrays to get the intended results, so I'm keeping this code
  65. * here, may be useful again in the future.
  66. */
  67. #undef NEWT_DEBUG
  68. static void newt_checkbox_tree__add(newtComponent tree, const char *str,
  69. void *priv, int *indexes)
  70. {
  71. #ifdef NEWT_DEBUG
  72. /* Print the newtCheckboxTreeAddArray to tinker with its index arrays */
  73. int i = 0, len = 40 - strlen(str);
  74. fprintf(stderr,
  75. "\tnewtCheckboxTreeAddItem(tree, %*.*s\"%s\", (void *)%p, 0, ",
  76. len, len, " ", str, priv);
  77. while (indexes[i] != NEWT_ARG_LAST) {
  78. if (indexes[i] != NEWT_ARG_APPEND)
  79. fprintf(stderr, " %d,", indexes[i]);
  80. else
  81. fprintf(stderr, " %s,", "NEWT_ARG_APPEND");
  82. ++i;
  83. }
  84. fprintf(stderr, " %s", " NEWT_ARG_LAST);\n");
  85. fflush(stderr);
  86. #endif
  87. newtCheckboxTreeAddArray(tree, str, priv, 0, indexes);
  88. }
  89. static char *callchain_list__sym_name(struct callchain_list *self,
  90. char *bf, size_t bfsize)
  91. {
  92. if (self->sym)
  93. return self->sym->name;
  94. snprintf(bf, bfsize, "%#Lx", self->ip);
  95. return bf;
  96. }
  97. static void __callchain__append_graph_browser(struct callchain_node *self,
  98. newtComponent tree, u64 total,
  99. int *indexes, int depth)
  100. {
  101. struct rb_node *node;
  102. u64 new_total, remaining;
  103. int idx = 0;
  104. if (callchain_param.mode == CHAIN_GRAPH_REL)
  105. new_total = self->children_hit;
  106. else
  107. new_total = total;
  108. remaining = new_total;
  109. node = rb_first(&self->rb_root);
  110. while (node) {
  111. struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
  112. struct rb_node *next = rb_next(node);
  113. u64 cumul = cumul_hits(child);
  114. struct callchain_list *chain;
  115. int first = true, printed = 0;
  116. int chain_idx = -1;
  117. remaining -= cumul;
  118. indexes[depth] = NEWT_ARG_APPEND;
  119. indexes[depth + 1] = NEWT_ARG_LAST;
  120. list_for_each_entry(chain, &child->val, list) {
  121. char ipstr[BITS_PER_LONG / 4 + 1],
  122. *alloc_str = NULL;
  123. const char *str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
  124. if (first) {
  125. double percent = cumul * 100.0 / new_total;
  126. first = false;
  127. if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
  128. str = "Not enough memory!";
  129. else
  130. str = alloc_str;
  131. } else {
  132. indexes[depth] = idx;
  133. indexes[depth + 1] = NEWT_ARG_APPEND;
  134. indexes[depth + 2] = NEWT_ARG_LAST;
  135. ++chain_idx;
  136. }
  137. newt_checkbox_tree__add(tree, str, chain->sym, indexes);
  138. free(alloc_str);
  139. ++printed;
  140. }
  141. indexes[depth] = idx;
  142. if (chain_idx != -1)
  143. indexes[depth + 1] = chain_idx;
  144. if (printed != 0)
  145. ++idx;
  146. __callchain__append_graph_browser(child, tree, new_total, indexes,
  147. depth + (chain_idx != -1 ? 2 : 1));
  148. node = next;
  149. }
  150. }
  151. static void callchain__append_graph_browser(struct callchain_node *self,
  152. newtComponent tree, u64 total,
  153. int *indexes, int parent_idx)
  154. {
  155. struct callchain_list *chain;
  156. int i = 0;
  157. indexes[1] = NEWT_ARG_APPEND;
  158. indexes[2] = NEWT_ARG_LAST;
  159. list_for_each_entry(chain, &self->val, list) {
  160. char ipstr[BITS_PER_LONG / 4 + 1], *str;
  161. if (chain->ip >= PERF_CONTEXT_MAX)
  162. continue;
  163. if (!i++ && sort__first_dimension == SORT_SYM)
  164. continue;
  165. str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
  166. newt_checkbox_tree__add(tree, str, chain->sym, indexes);
  167. }
  168. indexes[1] = parent_idx;
  169. indexes[2] = NEWT_ARG_APPEND;
  170. indexes[3] = NEWT_ARG_LAST;
  171. __callchain__append_graph_browser(self, tree, total, indexes, 2);
  172. }
  173. static void hist_entry__append_callchain_browser(struct hist_entry *self,
  174. newtComponent tree, u64 total, int parent_idx)
  175. {
  176. struct rb_node *rb_node;
  177. int indexes[1024] = { [0] = parent_idx, };
  178. int idx = 0;
  179. struct callchain_node *chain;
  180. rb_node = rb_first(&self->sorted_chain);
  181. while (rb_node) {
  182. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  183. switch (callchain_param.mode) {
  184. case CHAIN_FLAT:
  185. break;
  186. case CHAIN_GRAPH_ABS: /* falldown */
  187. case CHAIN_GRAPH_REL:
  188. callchain__append_graph_browser(chain, tree, total, indexes, idx++);
  189. break;
  190. case CHAIN_NONE:
  191. default:
  192. break;
  193. }
  194. rb_node = rb_next(rb_node);
  195. }
  196. }
  197. /*
  198. * FIXME: get lib/string.c linked with perf somehow
  199. */
  200. static char *skip_spaces(const char *str)
  201. {
  202. while (isspace(*str))
  203. ++str;
  204. return (char *)str;
  205. }
  206. static char *strim(char *s)
  207. {
  208. size_t size;
  209. char *end;
  210. s = skip_spaces(s);
  211. size = strlen(s);
  212. if (!size)
  213. return s;
  214. end = s + size - 1;
  215. while (end >= s && isspace(*end))
  216. end--;
  217. *(end + 1) = '\0';
  218. return s;
  219. }
  220. static size_t hist_entry__append_browser(struct hist_entry *self,
  221. newtComponent tree, u64 total)
  222. {
  223. char bf[1024], *s;
  224. FILE *fp;
  225. if (symbol_conf.exclude_other && !self->parent)
  226. return 0;
  227. fp = fmemopen(bf, sizeof(bf), "w");
  228. if (fp == NULL)
  229. return 0;
  230. hist_entry__fprintf(self, NULL, false, 0, fp, total);
  231. fclose(fp);
  232. /*
  233. * FIXME: We shouldn't need to trim, as the printing routines shouldn't
  234. * add spaces it in the first place, the stdio output routines should
  235. * call a __snprintf method instead of the current __print (that
  236. * actually is a __fprintf) one, but get the raw string and _then_ add
  237. * the newline, as this is a detail of stdio printing, not needed in
  238. * other UIs, e.g. newt.
  239. */
  240. s = strim(bf);
  241. if (symbol_conf.use_callchain) {
  242. int indexes[2];
  243. indexes[0] = NEWT_ARG_APPEND;
  244. indexes[1] = NEWT_ARG_LAST;
  245. newt_checkbox_tree__add(tree, s, self->sym, indexes);
  246. } else
  247. newtListboxAppendEntry(tree, s, self->sym);
  248. return strlen(s);
  249. }
  250. static void symbol__annotate_browser(const struct symbol *self)
  251. {
  252. FILE *fp;
  253. int cols, rows;
  254. newtComponent form, tree;
  255. struct newtExitStruct es;
  256. char *str;
  257. size_t line_len, max_line_len = 0;
  258. size_t max_usable_width;
  259. char *line = NULL;
  260. if (self == NULL)
  261. return;
  262. if (asprintf(&str, "perf annotate %s 2>&1 | expand", self->name) < 0)
  263. return;
  264. fp = popen(str, "r");
  265. if (fp == NULL)
  266. goto out_free_str;
  267. newtPushHelpLine("Press ESC to exit");
  268. newtGetScreenSize(&cols, &rows);
  269. tree = newtListbox(0, 0, rows - 5, NEWT_FLAG_SCROLL);
  270. while (!feof(fp)) {
  271. if (getline(&line, &line_len, fp) < 0 || !line_len)
  272. break;
  273. while (line_len != 0 && isspace(line[line_len - 1]))
  274. line[--line_len] = '\0';
  275. if (line_len > max_line_len)
  276. max_line_len = line_len;
  277. newtListboxAppendEntry(tree, line, NULL);
  278. }
  279. fclose(fp);
  280. free(line);
  281. max_usable_width = cols - 22;
  282. if (max_line_len > max_usable_width)
  283. max_line_len = max_usable_width;
  284. newtListboxSetWidth(tree, max_line_len);
  285. newtCenteredWindow(max_line_len + 2, rows - 5, self->name);
  286. form = newt_form__new();
  287. newtFormAddComponents(form, tree, NULL);
  288. newtFormRun(form, &es);
  289. newtFormDestroy(form);
  290. newtPopWindow();
  291. newtPopHelpLine();
  292. out_free_str:
  293. free(str);
  294. }
  295. static const void *newt__symbol_tree_get_current(newtComponent self)
  296. {
  297. if (symbol_conf.use_callchain)
  298. return newtCheckboxTreeGetCurrent(self);
  299. return newtListboxGetCurrent(self);
  300. }
  301. static void perf_session__selection(newtComponent self, void *data)
  302. {
  303. const struct symbol **symbol_ptr = data;
  304. *symbol_ptr = newt__symbol_tree_get_current(self);
  305. }
  306. void perf_session__browse_hists(struct rb_root *hists, u64 session_total,
  307. const char *helpline)
  308. {
  309. struct sort_entry *se;
  310. struct rb_node *nd;
  311. char seq[] = ".";
  312. unsigned int width;
  313. char *col_width = symbol_conf.col_width_list_str;
  314. int rows, cols, idx;
  315. int max_len = 0;
  316. char str[1024];
  317. newtComponent form, tree;
  318. struct newtExitStruct es;
  319. const struct symbol *selection;
  320. snprintf(str, sizeof(str), "Samples: %Ld", session_total);
  321. newtDrawRootText(0, 0, str);
  322. newtPushHelpLine(helpline);
  323. newtGetScreenSize(&cols, &rows);
  324. if (symbol_conf.use_callchain)
  325. tree = newtCheckboxTreeMulti(0, 0, rows - 5, seq,
  326. NEWT_FLAG_SCROLL);
  327. else
  328. tree = newtListbox(0, 0, rows - 5, (NEWT_FLAG_SCROLL |
  329. NEWT_FLAG_RETURNEXIT));
  330. newtComponentAddCallback(tree, perf_session__selection, &selection);
  331. list_for_each_entry(se, &hist_entry__sort_list, list) {
  332. if (se->elide)
  333. continue;
  334. width = strlen(se->header);
  335. if (se->width) {
  336. if (symbol_conf.col_width_list_str) {
  337. if (col_width) {
  338. *se->width = atoi(col_width);
  339. col_width = strchr(col_width, ',');
  340. if (col_width)
  341. ++col_width;
  342. }
  343. }
  344. *se->width = max(*se->width, width);
  345. }
  346. }
  347. idx = 0;
  348. for (nd = rb_first(hists); nd; nd = rb_next(nd)) {
  349. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  350. int len = hist_entry__append_browser(h, tree, session_total);
  351. if (len > max_len)
  352. max_len = len;
  353. if (symbol_conf.use_callchain) {
  354. hist_entry__append_callchain_browser(h, tree, session_total, idx++);
  355. if (idx > 3300)
  356. break;
  357. }
  358. }
  359. if (max_len > cols)
  360. max_len = cols - 3;
  361. if (!symbol_conf.use_callchain)
  362. newtListboxSetWidth(tree, max_len);
  363. newtCenteredWindow(max_len + (symbol_conf.use_callchain ? 5 : 0),
  364. rows - 5, "Report");
  365. form = newt_form__new();
  366. newtFormAddHotKey(form, 'A');
  367. newtFormAddHotKey(form, 'a');
  368. newtFormAddHotKey(form, NEWT_KEY_RIGHT);
  369. newtFormAddComponents(form, tree, NULL);
  370. selection = newt__symbol_tree_get_current(tree);
  371. while (1) {
  372. char annotate[512];
  373. const char *options[2];
  374. int nr_options = 0, choice;
  375. newtFormRun(form, &es);
  376. if (es.reason == NEWT_EXIT_HOTKEY) {
  377. if (toupper(es.u.key) == 'A') {
  378. symbol__annotate_browser(selection);
  379. continue;
  380. }
  381. if (es.u.key == NEWT_KEY_ESCAPE ||
  382. toupper(es.u.key) == 'Q' ||
  383. es.u.key == CTRL('c')) {
  384. if (dialog_yesno("Do you really want to exit?"))
  385. break;
  386. else
  387. continue;
  388. }
  389. }
  390. if (selection != NULL) {
  391. snprintf(annotate, sizeof(annotate),
  392. "Annotate %s", selection->name);
  393. options[nr_options++] = annotate;
  394. }
  395. options[nr_options++] = "Exit";
  396. choice = popup_menu(nr_options, options);
  397. if (choice == nr_options - 1)
  398. break;
  399. else if (selection != NULL && choice >= 0)
  400. symbol__annotate_browser(selection);
  401. }
  402. newtFormDestroy(form);
  403. newtPopWindow();
  404. }
  405. static char browser__last_msg[1024];
  406. int browser__show_help(const char *format, va_list ap)
  407. {
  408. int ret;
  409. static int backlog;
  410. ret = vsnprintf(browser__last_msg + backlog,
  411. sizeof(browser__last_msg) - backlog, format, ap);
  412. backlog += ret;
  413. if (browser__last_msg[backlog - 1] == '\n') {
  414. newtPopHelpLine();
  415. newtPushHelpLine(browser__last_msg);
  416. newtRefresh();
  417. backlog = 0;
  418. }
  419. return ret;
  420. }
  421. void setup_browser(void)
  422. {
  423. if (!isatty(1))
  424. return;
  425. use_browser = true;
  426. newtInit();
  427. newtCls();
  428. newtPushHelpLine(" ");
  429. }
  430. void exit_browser(bool wait_for_ok)
  431. {
  432. if (use_browser) {
  433. if (wait_for_ok) {
  434. char title[] = "Fatal Error", ok[] = "Ok";
  435. newtWinMessage(title, ok, browser__last_msg);
  436. }
  437. newtFinished();
  438. }
  439. }