newt.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #undef _GNU_SOURCE
  4. #include "ui/libslang.h"
  5. #include <signal.h>
  6. #include <stdlib.h>
  7. #include <elf.h>
  8. #include <newt.h>
  9. #include <sys/ttydefaults.h>
  10. #include "cache.h"
  11. #include "hist.h"
  12. #include "pstack.h"
  13. #include "session.h"
  14. #include "sort.h"
  15. #include "symbol.h"
  16. #include "ui/browser.h"
  17. #include "ui/helpline.h"
  18. #include "ui/browsers/map.h"
  19. newtComponent newt_form__new(void);
  20. char browser__last_msg[1024];
  21. int browser__show_help(const char *format, va_list ap)
  22. {
  23. int ret;
  24. static int backlog;
  25. ret = vsnprintf(browser__last_msg + backlog,
  26. sizeof(browser__last_msg) - backlog, format, ap);
  27. backlog += ret;
  28. if (browser__last_msg[backlog - 1] == '\n') {
  29. ui_helpline__puts(browser__last_msg);
  30. newtRefresh();
  31. backlog = 0;
  32. }
  33. return ret;
  34. }
  35. static void newt_form__set_exit_keys(newtComponent self)
  36. {
  37. newtFormAddHotKey(self, NEWT_KEY_LEFT);
  38. newtFormAddHotKey(self, NEWT_KEY_ESCAPE);
  39. newtFormAddHotKey(self, 'Q');
  40. newtFormAddHotKey(self, 'q');
  41. newtFormAddHotKey(self, CTRL('c'));
  42. }
  43. newtComponent newt_form__new(void)
  44. {
  45. newtComponent self = newtForm(NULL, NULL, 0);
  46. if (self)
  47. newt_form__set_exit_keys(self);
  48. return self;
  49. }
  50. static int popup_menu(int argc, char * const argv[])
  51. {
  52. struct newtExitStruct es;
  53. int i, rc = -1, max_len = 5;
  54. newtComponent listbox, form = newt_form__new();
  55. if (form == NULL)
  56. return -1;
  57. listbox = newtListbox(0, 0, argc, NEWT_FLAG_RETURNEXIT);
  58. if (listbox == NULL)
  59. goto out_destroy_form;
  60. newtFormAddComponent(form, listbox);
  61. for (i = 0; i < argc; ++i) {
  62. int len = strlen(argv[i]);
  63. if (len > max_len)
  64. max_len = len;
  65. if (newtListboxAddEntry(listbox, argv[i], (void *)(long)i))
  66. goto out_destroy_form;
  67. }
  68. newtCenteredWindow(max_len, argc, NULL);
  69. newtFormRun(form, &es);
  70. rc = newtListboxGetCurrent(listbox) - NULL;
  71. if (es.reason == NEWT_EXIT_HOTKEY)
  72. rc = -1;
  73. newtPopWindow();
  74. out_destroy_form:
  75. newtFormDestroy(form);
  76. return rc;
  77. }
  78. static int ui__help_window(const char *text)
  79. {
  80. struct newtExitStruct es;
  81. newtComponent tb, form = newt_form__new();
  82. int rc = -1;
  83. int max_len = 0, nr_lines = 0;
  84. const char *t;
  85. if (form == NULL)
  86. return -1;
  87. t = text;
  88. while (1) {
  89. const char *sep = strchr(t, '\n');
  90. int len;
  91. if (sep == NULL)
  92. sep = strchr(t, '\0');
  93. len = sep - t;
  94. if (max_len < len)
  95. max_len = len;
  96. ++nr_lines;
  97. if (*sep == '\0')
  98. break;
  99. t = sep + 1;
  100. }
  101. tb = newtTextbox(0, 0, max_len, nr_lines, 0);
  102. if (tb == NULL)
  103. goto out_destroy_form;
  104. newtTextboxSetText(tb, text);
  105. newtFormAddComponent(form, tb);
  106. newtCenteredWindow(max_len, nr_lines, NULL);
  107. newtFormRun(form, &es);
  108. newtPopWindow();
  109. rc = 0;
  110. out_destroy_form:
  111. newtFormDestroy(form);
  112. return rc;
  113. }
  114. static bool dialog_yesno(const char *msg)
  115. {
  116. /* newtWinChoice should really be accepting const char pointers... */
  117. char yes[] = "Yes", no[] = "No";
  118. return newtWinChoice(NULL, yes, no, (char *)msg) == 1;
  119. }
  120. static char *callchain_list__sym_name(struct callchain_list *self,
  121. char *bf, size_t bfsize)
  122. {
  123. if (self->ms.sym)
  124. return self->ms.sym->name;
  125. snprintf(bf, bfsize, "%#Lx", self->ip);
  126. return bf;
  127. }
  128. struct hist_browser {
  129. struct ui_browser b;
  130. struct hists *hists;
  131. struct hist_entry *he_selection;
  132. struct map_symbol *selection;
  133. };
  134. static void hist_browser__reset(struct hist_browser *self);
  135. static int hist_browser__run(struct hist_browser *self, const char *title,
  136. struct newtExitStruct *es);
  137. static unsigned int hist_browser__refresh(struct ui_browser *self);
  138. static void ui_browser__hists_seek(struct ui_browser *self,
  139. off_t offset, int whence);
  140. static struct hist_browser *hist_browser__new(struct hists *hists)
  141. {
  142. struct hist_browser *self = zalloc(sizeof(*self));
  143. if (self) {
  144. self->hists = hists;
  145. self->b.refresh = hist_browser__refresh;
  146. self->b.seek = ui_browser__hists_seek;
  147. }
  148. return self;
  149. }
  150. static void hist_browser__delete(struct hist_browser *self)
  151. {
  152. newtFormDestroy(self->b.form);
  153. newtPopWindow();
  154. free(self);
  155. }
  156. static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
  157. {
  158. return self->he_selection;
  159. }
  160. static struct thread *hist_browser__selected_thread(struct hist_browser *self)
  161. {
  162. return self->he_selection->thread;
  163. }
  164. static int hist_browser__title(char *bf, size_t size, const char *ev_name,
  165. const struct dso *dso, const struct thread *thread)
  166. {
  167. int printed = 0;
  168. if (thread)
  169. printed += snprintf(bf + printed, size - printed,
  170. "Thread: %s(%d)",
  171. (thread->comm_set ? thread->comm : ""),
  172. thread->pid);
  173. if (dso)
  174. printed += snprintf(bf + printed, size - printed,
  175. "%sDSO: %s", thread ? " " : "",
  176. dso->short_name);
  177. return printed ?: snprintf(bf, size, "Event: %s", ev_name);
  178. }
  179. int hists__browse(struct hists *self, const char *helpline, const char *ev_name)
  180. {
  181. struct hist_browser *browser = hist_browser__new(self);
  182. struct pstack *fstack;
  183. const struct thread *thread_filter = NULL;
  184. const struct dso *dso_filter = NULL;
  185. struct newtExitStruct es;
  186. char msg[160];
  187. int key = -1;
  188. if (browser == NULL)
  189. return -1;
  190. fstack = pstack__new(2);
  191. if (fstack == NULL)
  192. goto out;
  193. ui_helpline__push(helpline);
  194. hist_browser__title(msg, sizeof(msg), ev_name,
  195. dso_filter, thread_filter);
  196. while (1) {
  197. const struct thread *thread;
  198. const struct dso *dso;
  199. char *options[16];
  200. int nr_options = 0, choice = 0, i,
  201. annotate = -2, zoom_dso = -2, zoom_thread = -2,
  202. browse_map = -2;
  203. if (hist_browser__run(browser, msg, &es))
  204. break;
  205. thread = hist_browser__selected_thread(browser);
  206. dso = browser->selection->map ? browser->selection->map->dso : NULL;
  207. if (es.reason == NEWT_EXIT_HOTKEY) {
  208. key = es.u.key;
  209. switch (key) {
  210. case NEWT_KEY_F1:
  211. goto do_help;
  212. case NEWT_KEY_TAB:
  213. case NEWT_KEY_UNTAB:
  214. /*
  215. * Exit the browser, let hists__browser_tree
  216. * go to the next or previous
  217. */
  218. goto out_free_stack;
  219. default:;
  220. }
  221. key = toupper(key);
  222. switch (key) {
  223. case 'A':
  224. if (browser->selection->map == NULL &&
  225. browser->selection->map->dso->annotate_warned)
  226. continue;
  227. goto do_annotate;
  228. case 'D':
  229. goto zoom_dso;
  230. case 'T':
  231. goto zoom_thread;
  232. case 'H':
  233. case '?':
  234. do_help:
  235. ui__help_window("-> Zoom into DSO/Threads & Annotate current symbol\n"
  236. "<- Zoom out\n"
  237. "a Annotate current symbol\n"
  238. "h/?/F1 Show this window\n"
  239. "d Zoom into current DSO\n"
  240. "t Zoom into current Thread\n"
  241. "q/CTRL+C Exit browser");
  242. continue;
  243. default:;
  244. }
  245. if (is_exit_key(key)) {
  246. if (key == NEWT_KEY_ESCAPE) {
  247. if (dialog_yesno("Do you really want to exit?"))
  248. break;
  249. else
  250. continue;
  251. } else
  252. break;
  253. }
  254. if (es.u.key == NEWT_KEY_LEFT) {
  255. const void *top;
  256. if (pstack__empty(fstack))
  257. continue;
  258. top = pstack__pop(fstack);
  259. if (top == &dso_filter)
  260. goto zoom_out_dso;
  261. if (top == &thread_filter)
  262. goto zoom_out_thread;
  263. continue;
  264. }
  265. }
  266. if (browser->selection->sym != NULL &&
  267. !browser->selection->map->dso->annotate_warned &&
  268. asprintf(&options[nr_options], "Annotate %s",
  269. browser->selection->sym->name) > 0)
  270. annotate = nr_options++;
  271. if (thread != NULL &&
  272. asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
  273. (thread_filter ? "out of" : "into"),
  274. (thread->comm_set ? thread->comm : ""),
  275. thread->pid) > 0)
  276. zoom_thread = nr_options++;
  277. if (dso != NULL &&
  278. asprintf(&options[nr_options], "Zoom %s %s DSO",
  279. (dso_filter ? "out of" : "into"),
  280. (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
  281. zoom_dso = nr_options++;
  282. if (browser->selection->map != NULL &&
  283. asprintf(&options[nr_options], "Browse map details") > 0)
  284. browse_map = nr_options++;
  285. options[nr_options++] = (char *)"Exit";
  286. choice = popup_menu(nr_options, options);
  287. for (i = 0; i < nr_options - 1; ++i)
  288. free(options[i]);
  289. if (choice == nr_options - 1)
  290. break;
  291. if (choice == -1)
  292. continue;
  293. if (choice == annotate) {
  294. struct hist_entry *he;
  295. do_annotate:
  296. if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
  297. browser->selection->map->dso->annotate_warned = 1;
  298. ui_helpline__puts("No vmlinux file found, can't "
  299. "annotate with just a "
  300. "kallsyms file");
  301. continue;
  302. }
  303. he = hist_browser__selected_entry(browser);
  304. if (he == NULL)
  305. continue;
  306. hist_entry__tui_annotate(he);
  307. } else if (choice == browse_map)
  308. map__browse(browser->selection->map);
  309. else if (choice == zoom_dso) {
  310. zoom_dso:
  311. if (dso_filter) {
  312. pstack__remove(fstack, &dso_filter);
  313. zoom_out_dso:
  314. ui_helpline__pop();
  315. dso_filter = NULL;
  316. } else {
  317. if (dso == NULL)
  318. continue;
  319. ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
  320. dso->kernel ? "the Kernel" : dso->short_name);
  321. dso_filter = dso;
  322. pstack__push(fstack, &dso_filter);
  323. }
  324. hists__filter_by_dso(self, dso_filter);
  325. hist_browser__title(msg, sizeof(msg), ev_name,
  326. dso_filter, thread_filter);
  327. hist_browser__reset(browser);
  328. } else if (choice == zoom_thread) {
  329. zoom_thread:
  330. if (thread_filter) {
  331. pstack__remove(fstack, &thread_filter);
  332. zoom_out_thread:
  333. ui_helpline__pop();
  334. thread_filter = NULL;
  335. } else {
  336. ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
  337. thread->comm_set ? thread->comm : "",
  338. thread->pid);
  339. thread_filter = thread;
  340. pstack__push(fstack, &thread_filter);
  341. }
  342. hists__filter_by_thread(self, thread_filter);
  343. hist_browser__title(msg, sizeof(msg), ev_name,
  344. dso_filter, thread_filter);
  345. hist_browser__reset(browser);
  346. }
  347. }
  348. out_free_stack:
  349. pstack__delete(fstack);
  350. out:
  351. hist_browser__delete(browser);
  352. return key;
  353. }
  354. int hists__tui_browse_tree(struct rb_root *self, const char *help)
  355. {
  356. struct rb_node *first = rb_first(self), *nd = first, *next;
  357. int key = 0;
  358. while (nd) {
  359. struct hists *hists = rb_entry(nd, struct hists, rb_node);
  360. const char *ev_name = __event_name(hists->type, hists->config);
  361. key = hists__browse(hists, help, ev_name);
  362. if (is_exit_key(key))
  363. break;
  364. switch (key) {
  365. case NEWT_KEY_TAB:
  366. next = rb_next(nd);
  367. if (next)
  368. nd = next;
  369. break;
  370. case NEWT_KEY_UNTAB:
  371. if (nd == first)
  372. continue;
  373. nd = rb_prev(nd);
  374. default:
  375. break;
  376. }
  377. }
  378. return key;
  379. }
  380. static void newt_suspend(void *d __used)
  381. {
  382. newtSuspend();
  383. raise(SIGTSTP);
  384. newtResume();
  385. }
  386. void setup_browser(void)
  387. {
  388. if (!isatty(1) || !use_browser || dump_trace) {
  389. use_browser = 0;
  390. setup_pager();
  391. return;
  392. }
  393. use_browser = 1;
  394. newtInit();
  395. newtCls();
  396. newtSetSuspendCallback(newt_suspend, NULL);
  397. ui_helpline__puts(" ");
  398. ui_browser__init();
  399. }
  400. void exit_browser(bool wait_for_ok)
  401. {
  402. if (use_browser > 0) {
  403. if (wait_for_ok) {
  404. char title[] = "Fatal Error", ok[] = "Ok";
  405. newtWinMessage(title, ok, browser__last_msg);
  406. }
  407. newtFinished();
  408. }
  409. }
  410. static void hist_browser__refresh_dimensions(struct hist_browser *self)
  411. {
  412. /* 3 == +/- toggle symbol before actual hist_entry rendering */
  413. self->b.width = 3 + (hists__sort_list_width(self->hists) +
  414. sizeof("[k]"));
  415. }
  416. static void hist_browser__reset(struct hist_browser *self)
  417. {
  418. self->b.nr_entries = self->hists->nr_entries;
  419. hist_browser__refresh_dimensions(self);
  420. ui_browser__reset_index(&self->b);
  421. }
  422. static char tree__folded_sign(bool unfolded)
  423. {
  424. return unfolded ? '-' : '+';
  425. }
  426. static char map_symbol__folded(const struct map_symbol *self)
  427. {
  428. return self->has_children ? tree__folded_sign(self->unfolded) : ' ';
  429. }
  430. static char hist_entry__folded(const struct hist_entry *self)
  431. {
  432. return map_symbol__folded(&self->ms);
  433. }
  434. static char callchain_list__folded(const struct callchain_list *self)
  435. {
  436. return map_symbol__folded(&self->ms);
  437. }
  438. static bool map_symbol__toggle_fold(struct map_symbol *self)
  439. {
  440. if (!self->has_children)
  441. return false;
  442. self->unfolded = !self->unfolded;
  443. return true;
  444. }
  445. #define LEVEL_OFFSET_STEP 3
  446. static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self,
  447. struct callchain_node *chain_node,
  448. u64 total, int level,
  449. unsigned short row,
  450. off_t *row_offset,
  451. bool *is_current_entry)
  452. {
  453. struct rb_node *node;
  454. int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
  455. u64 new_total, remaining;
  456. if (callchain_param.mode == CHAIN_GRAPH_REL)
  457. new_total = chain_node->children_hit;
  458. else
  459. new_total = total;
  460. remaining = new_total;
  461. node = rb_first(&chain_node->rb_root);
  462. while (node) {
  463. struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
  464. struct rb_node *next = rb_next(node);
  465. u64 cumul = cumul_hits(child);
  466. struct callchain_list *chain;
  467. char folded_sign = ' ';
  468. int first = true;
  469. int extra_offset = 0;
  470. remaining -= cumul;
  471. list_for_each_entry(chain, &child->val, list) {
  472. char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str;
  473. const char *str;
  474. int color;
  475. bool was_first = first;
  476. if (first) {
  477. first = false;
  478. chain->ms.has_children = chain->list.next != &child->val ||
  479. rb_first(&child->rb_root) != NULL;
  480. } else {
  481. extra_offset = LEVEL_OFFSET_STEP;
  482. chain->ms.has_children = chain->list.next == &child->val &&
  483. rb_first(&child->rb_root) != NULL;
  484. }
  485. folded_sign = callchain_list__folded(chain);
  486. if (*row_offset != 0) {
  487. --*row_offset;
  488. goto do_next;
  489. }
  490. alloc_str = NULL;
  491. str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
  492. if (was_first) {
  493. double percent = cumul * 100.0 / new_total;
  494. if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
  495. str = "Not enough memory!";
  496. else
  497. str = alloc_str;
  498. }
  499. color = HE_COLORSET_NORMAL;
  500. width = self->b.width - (offset + extra_offset + 2);
  501. if (ui_browser__is_current_entry(&self->b, row)) {
  502. self->selection = &chain->ms;
  503. color = HE_COLORSET_SELECTED;
  504. *is_current_entry = true;
  505. }
  506. SLsmg_set_color(color);
  507. SLsmg_gotorc(self->b.y + row, self->b.x);
  508. slsmg_write_nstring(" ", offset + extra_offset);
  509. slsmg_printf("%c ", folded_sign);
  510. slsmg_write_nstring(str, width);
  511. free(alloc_str);
  512. if (++row == self->b.height)
  513. goto out;
  514. do_next:
  515. if (folded_sign == '+')
  516. break;
  517. }
  518. if (folded_sign == '-') {
  519. const int new_level = level + (extra_offset ? 2 : 1);
  520. row += hist_browser__show_callchain_node_rb_tree(self, child, new_total,
  521. new_level, row, row_offset,
  522. is_current_entry);
  523. }
  524. if (row == self->b.height)
  525. goto out;
  526. node = next;
  527. }
  528. out:
  529. return row - first_row;
  530. }
  531. static int hist_browser__show_callchain_node(struct hist_browser *self,
  532. struct callchain_node *node,
  533. int level, unsigned short row,
  534. off_t *row_offset,
  535. bool *is_current_entry)
  536. {
  537. struct callchain_list *chain;
  538. int first_row = row,
  539. offset = level * LEVEL_OFFSET_STEP,
  540. width = self->b.width - offset;
  541. char folded_sign = ' ';
  542. list_for_each_entry(chain, &node->val, list) {
  543. char ipstr[BITS_PER_LONG / 4 + 1], *s;
  544. int color;
  545. /*
  546. * FIXME: This should be moved to somewhere else,
  547. * probably when the callchain is created, so as not to
  548. * traverse it all over again
  549. */
  550. chain->ms.has_children = rb_first(&node->rb_root) != NULL;
  551. folded_sign = callchain_list__folded(chain);
  552. if (*row_offset != 0) {
  553. --*row_offset;
  554. continue;
  555. }
  556. color = HE_COLORSET_NORMAL;
  557. if (ui_browser__is_current_entry(&self->b, row)) {
  558. self->selection = &chain->ms;
  559. color = HE_COLORSET_SELECTED;
  560. *is_current_entry = true;
  561. }
  562. s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
  563. SLsmg_gotorc(self->b.y + row, self->b.x);
  564. SLsmg_set_color(color);
  565. slsmg_write_nstring(" ", offset);
  566. slsmg_printf("%c ", folded_sign);
  567. slsmg_write_nstring(s, width - 2);
  568. if (++row == self->b.height)
  569. goto out;
  570. }
  571. if (folded_sign == '-')
  572. row += hist_browser__show_callchain_node_rb_tree(self, node,
  573. self->hists->stats.total_period,
  574. level + 1, row,
  575. row_offset,
  576. is_current_entry);
  577. out:
  578. return row - first_row;
  579. }
  580. static int hist_browser__show_callchain(struct hist_browser *self,
  581. struct rb_root *chain,
  582. int level, unsigned short row,
  583. off_t *row_offset,
  584. bool *is_current_entry)
  585. {
  586. struct rb_node *nd;
  587. int first_row = row;
  588. for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
  589. struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
  590. row += hist_browser__show_callchain_node(self, node, level,
  591. row, row_offset,
  592. is_current_entry);
  593. if (row == self->b.height)
  594. break;
  595. }
  596. return row - first_row;
  597. }
  598. static int hist_browser__show_entry(struct hist_browser *self,
  599. struct hist_entry *entry,
  600. unsigned short row)
  601. {
  602. char s[256];
  603. double percent;
  604. int printed = 0;
  605. int color, width = self->b.width;
  606. char folded_sign = ' ';
  607. bool current_entry = ui_browser__is_current_entry(&self->b, row);
  608. off_t row_offset = entry->row_offset;
  609. if (current_entry) {
  610. self->he_selection = entry;
  611. self->selection = &entry->ms;
  612. }
  613. if (symbol_conf.use_callchain) {
  614. entry->ms.has_children = !RB_EMPTY_ROOT(&entry->sorted_chain);
  615. folded_sign = hist_entry__folded(entry);
  616. }
  617. if (row_offset == 0) {
  618. hist_entry__snprintf(entry, s, sizeof(s), self->hists, NULL, false,
  619. 0, false, self->hists->stats.total_period);
  620. percent = (entry->period * 100.0) / self->hists->stats.total_period;
  621. color = HE_COLORSET_SELECTED;
  622. if (!current_entry) {
  623. if (percent >= MIN_RED)
  624. color = HE_COLORSET_TOP;
  625. else if (percent >= MIN_GREEN)
  626. color = HE_COLORSET_MEDIUM;
  627. else
  628. color = HE_COLORSET_NORMAL;
  629. }
  630. SLsmg_set_color(color);
  631. SLsmg_gotorc(self->b.y + row, self->b.x);
  632. if (symbol_conf.use_callchain) {
  633. slsmg_printf("%c ", folded_sign);
  634. width -= 2;
  635. }
  636. slsmg_write_nstring(s, width);
  637. ++row;
  638. ++printed;
  639. } else
  640. --row_offset;
  641. if (folded_sign == '-' && row != self->b.height) {
  642. printed += hist_browser__show_callchain(self, &entry->sorted_chain,
  643. 1, row, &row_offset,
  644. &current_entry);
  645. if (current_entry)
  646. self->he_selection = entry;
  647. }
  648. return printed;
  649. }
  650. static unsigned int hist_browser__refresh(struct ui_browser *self)
  651. {
  652. unsigned row = 0;
  653. struct rb_node *nd;
  654. struct hist_browser *hb = container_of(self, struct hist_browser, b);
  655. if (self->top == NULL)
  656. self->top = rb_first(&hb->hists->entries);
  657. for (nd = self->top; nd; nd = rb_next(nd)) {
  658. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  659. if (h->filtered)
  660. continue;
  661. row += hist_browser__show_entry(hb, h, row);
  662. if (row == self->height)
  663. break;
  664. }
  665. return row;
  666. }
  667. static void callchain_node__init_have_children_rb_tree(struct callchain_node *self)
  668. {
  669. struct rb_node *nd = rb_first(&self->rb_root);
  670. for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
  671. struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
  672. struct callchain_list *chain;
  673. int first = true;
  674. list_for_each_entry(chain, &child->val, list) {
  675. if (first) {
  676. first = false;
  677. chain->ms.has_children = chain->list.next != &child->val ||
  678. rb_first(&child->rb_root) != NULL;
  679. } else
  680. chain->ms.has_children = chain->list.next == &child->val &&
  681. rb_first(&child->rb_root) != NULL;
  682. }
  683. callchain_node__init_have_children_rb_tree(child);
  684. }
  685. }
  686. static void callchain_node__init_have_children(struct callchain_node *self)
  687. {
  688. struct callchain_list *chain;
  689. list_for_each_entry(chain, &self->val, list)
  690. chain->ms.has_children = rb_first(&self->rb_root) != NULL;
  691. callchain_node__init_have_children_rb_tree(self);
  692. }
  693. static void callchain__init_have_children(struct rb_root *self)
  694. {
  695. struct rb_node *nd;
  696. for (nd = rb_first(self); nd; nd = rb_next(nd)) {
  697. struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
  698. callchain_node__init_have_children(node);
  699. }
  700. }
  701. static void hist_entry__init_have_children(struct hist_entry *self)
  702. {
  703. if (!self->init_have_children) {
  704. callchain__init_have_children(&self->sorted_chain);
  705. self->init_have_children = true;
  706. }
  707. }
  708. static struct rb_node *hists__filter_entries(struct rb_node *nd)
  709. {
  710. while (nd != NULL) {
  711. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  712. if (!h->filtered)
  713. return nd;
  714. nd = rb_next(nd);
  715. }
  716. return NULL;
  717. }
  718. static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
  719. {
  720. while (nd != NULL) {
  721. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  722. if (!h->filtered)
  723. return nd;
  724. nd = rb_prev(nd);
  725. }
  726. return NULL;
  727. }
  728. static void ui_browser__hists_seek(struct ui_browser *self,
  729. off_t offset, int whence)
  730. {
  731. struct hist_entry *h;
  732. struct rb_node *nd;
  733. bool first = true;
  734. switch (whence) {
  735. case SEEK_SET:
  736. nd = hists__filter_entries(rb_first(self->entries));
  737. break;
  738. case SEEK_CUR:
  739. nd = self->top;
  740. goto do_offset;
  741. case SEEK_END:
  742. nd = hists__filter_prev_entries(rb_last(self->entries));
  743. first = false;
  744. break;
  745. default:
  746. return;
  747. }
  748. /*
  749. * Moves not relative to the first visible entry invalidates its
  750. * row_offset:
  751. */
  752. h = rb_entry(self->top, struct hist_entry, rb_node);
  753. h->row_offset = 0;
  754. /*
  755. * Here we have to check if nd is expanded (+), if it is we can't go
  756. * the next top level hist_entry, instead we must compute an offset of
  757. * what _not_ to show and not change the first visible entry.
  758. *
  759. * This offset increments when we are going from top to bottom and
  760. * decreases when we're going from bottom to top.
  761. *
  762. * As we don't have backpointers to the top level in the callchains
  763. * structure, we need to always print the whole hist_entry callchain,
  764. * skipping the first ones that are before the first visible entry
  765. * and stop when we printed enough lines to fill the screen.
  766. */
  767. do_offset:
  768. if (offset > 0) {
  769. do {
  770. h = rb_entry(nd, struct hist_entry, rb_node);
  771. if (h->ms.unfolded) {
  772. u16 remaining = h->nr_rows - h->row_offset;
  773. if (offset > remaining) {
  774. offset -= remaining;
  775. h->row_offset = 0;
  776. } else {
  777. h->row_offset += offset;
  778. offset = 0;
  779. self->top = nd;
  780. break;
  781. }
  782. }
  783. nd = hists__filter_entries(rb_next(nd));
  784. if (nd == NULL)
  785. break;
  786. --offset;
  787. self->top = nd;
  788. } while (offset != 0);
  789. } else if (offset < 0) {
  790. while (1) {
  791. h = rb_entry(nd, struct hist_entry, rb_node);
  792. if (h->ms.unfolded) {
  793. if (first) {
  794. if (-offset > h->row_offset) {
  795. offset += h->row_offset;
  796. h->row_offset = 0;
  797. } else {
  798. h->row_offset += offset;
  799. offset = 0;
  800. self->top = nd;
  801. break;
  802. }
  803. } else {
  804. if (-offset > h->nr_rows) {
  805. offset += h->nr_rows;
  806. h->row_offset = 0;
  807. } else {
  808. h->row_offset = h->nr_rows + offset;
  809. offset = 0;
  810. self->top = nd;
  811. break;
  812. }
  813. }
  814. }
  815. nd = hists__filter_prev_entries(rb_prev(nd));
  816. if (nd == NULL)
  817. break;
  818. ++offset;
  819. self->top = nd;
  820. if (offset == 0) {
  821. /*
  822. * Last unfiltered hist_entry, check if it is
  823. * unfolded, if it is then we should have
  824. * row_offset at its last entry.
  825. */
  826. h = rb_entry(nd, struct hist_entry, rb_node);
  827. if (h->ms.unfolded)
  828. h->row_offset = h->nr_rows;
  829. break;
  830. }
  831. first = false;
  832. }
  833. } else {
  834. self->top = nd;
  835. h = rb_entry(nd, struct hist_entry, rb_node);
  836. h->row_offset = 0;
  837. }
  838. }
  839. static int callchain_node__count_rows_rb_tree(struct callchain_node *self)
  840. {
  841. int n = 0;
  842. struct rb_node *nd;
  843. for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
  844. struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
  845. struct callchain_list *chain;
  846. char folded_sign = ' '; /* No children */
  847. list_for_each_entry(chain, &child->val, list) {
  848. ++n;
  849. /* We need this because we may not have children */
  850. folded_sign = callchain_list__folded(chain);
  851. if (folded_sign == '+')
  852. break;
  853. }
  854. if (folded_sign == '-') /* Have children and they're unfolded */
  855. n += callchain_node__count_rows_rb_tree(child);
  856. }
  857. return n;
  858. }
  859. static int callchain_node__count_rows(struct callchain_node *node)
  860. {
  861. struct callchain_list *chain;
  862. bool unfolded = false;
  863. int n = 0;
  864. list_for_each_entry(chain, &node->val, list) {
  865. ++n;
  866. unfolded = chain->ms.unfolded;
  867. }
  868. if (unfolded)
  869. n += callchain_node__count_rows_rb_tree(node);
  870. return n;
  871. }
  872. static int callchain__count_rows(struct rb_root *chain)
  873. {
  874. struct rb_node *nd;
  875. int n = 0;
  876. for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
  877. struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
  878. n += callchain_node__count_rows(node);
  879. }
  880. return n;
  881. }
  882. static bool hist_browser__toggle_fold(struct hist_browser *self)
  883. {
  884. if (map_symbol__toggle_fold(self->selection)) {
  885. struct hist_entry *he = self->he_selection;
  886. hist_entry__init_have_children(he);
  887. self->hists->nr_entries -= he->nr_rows;
  888. if (he->ms.unfolded)
  889. he->nr_rows = callchain__count_rows(&he->sorted_chain);
  890. else
  891. he->nr_rows = 0;
  892. self->hists->nr_entries += he->nr_rows;
  893. self->b.nr_entries = self->hists->nr_entries;
  894. return true;
  895. }
  896. /* If it doesn't have children, no toggling performed */
  897. return false;
  898. }
  899. static int hist_browser__run(struct hist_browser *self, const char *title,
  900. struct newtExitStruct *es)
  901. {
  902. char str[256], unit;
  903. unsigned long nr_events = self->hists->stats.nr_events[PERF_RECORD_SAMPLE];
  904. self->b.entries = &self->hists->entries;
  905. self->b.nr_entries = self->hists->nr_entries;
  906. hist_browser__refresh_dimensions(self);
  907. nr_events = convert_unit(nr_events, &unit);
  908. snprintf(str, sizeof(str), "Events: %lu%c ",
  909. nr_events, unit);
  910. newtDrawRootText(0, 0, str);
  911. if (ui_browser__show(&self->b, title) < 0)
  912. return -1;
  913. newtFormAddHotKey(self->b.form, 'A');
  914. newtFormAddHotKey(self->b.form, 'a');
  915. newtFormAddHotKey(self->b.form, '?');
  916. newtFormAddHotKey(self->b.form, 'h');
  917. newtFormAddHotKey(self->b.form, 'H');
  918. newtFormAddHotKey(self->b.form, 'd');
  919. newtFormAddHotKey(self->b.form, NEWT_KEY_LEFT);
  920. newtFormAddHotKey(self->b.form, NEWT_KEY_RIGHT);
  921. newtFormAddHotKey(self->b.form, NEWT_KEY_ENTER);
  922. while (1) {
  923. ui_browser__run(&self->b, es);
  924. if (es->reason != NEWT_EXIT_HOTKEY)
  925. break;
  926. switch (es->u.key) {
  927. case 'd': { /* Debug */
  928. static int seq;
  929. struct hist_entry *h = rb_entry(self->b.top,
  930. struct hist_entry, rb_node);
  931. ui_helpline__pop();
  932. ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
  933. seq++, self->b.nr_entries,
  934. self->hists->nr_entries,
  935. self->b.height,
  936. self->b.index,
  937. self->b.top_idx,
  938. h->row_offset, h->nr_rows);
  939. }
  940. continue;
  941. case NEWT_KEY_ENTER:
  942. if (hist_browser__toggle_fold(self))
  943. break;
  944. /* fall thru */
  945. default:
  946. return 0;
  947. }
  948. }
  949. return 0;
  950. }