hists.c 24 KB

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