hists.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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 "../../evsel.h"
  10. #include "../../evlist.h"
  11. #include "../../hist.h"
  12. #include "../../pstack.h"
  13. #include "../../sort.h"
  14. #include "../../util.h"
  15. #include "../browser.h"
  16. #include "../helpline.h"
  17. #include "../util.h"
  18. #include "../ui.h"
  19. #include "map.h"
  20. struct hist_browser {
  21. struct ui_browser b;
  22. struct hists *hists;
  23. struct hist_entry *he_selection;
  24. struct map_symbol *selection;
  25. bool has_symbols;
  26. };
  27. static int hists__browser_title(struct hists *self, char *bf, size_t size,
  28. const char *ev_name);
  29. static void hist_browser__refresh_dimensions(struct hist_browser *self)
  30. {
  31. /* 3 == +/- toggle symbol before actual hist_entry rendering */
  32. self->b.width = 3 + (hists__sort_list_width(self->hists) +
  33. sizeof("[k]"));
  34. }
  35. static void hist_browser__reset(struct hist_browser *self)
  36. {
  37. self->b.nr_entries = self->hists->nr_entries;
  38. hist_browser__refresh_dimensions(self);
  39. ui_browser__reset_index(&self->b);
  40. }
  41. static char tree__folded_sign(bool unfolded)
  42. {
  43. return unfolded ? '-' : '+';
  44. }
  45. static char map_symbol__folded(const struct map_symbol *self)
  46. {
  47. return self->has_children ? tree__folded_sign(self->unfolded) : ' ';
  48. }
  49. static char hist_entry__folded(const struct hist_entry *self)
  50. {
  51. return map_symbol__folded(&self->ms);
  52. }
  53. static char callchain_list__folded(const struct callchain_list *self)
  54. {
  55. return map_symbol__folded(&self->ms);
  56. }
  57. static void map_symbol__set_folding(struct map_symbol *self, bool unfold)
  58. {
  59. self->unfolded = unfold ? self->has_children : false;
  60. }
  61. static int callchain_node__count_rows_rb_tree(struct callchain_node *self)
  62. {
  63. int n = 0;
  64. struct rb_node *nd;
  65. for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
  66. struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
  67. struct callchain_list *chain;
  68. char folded_sign = ' '; /* No children */
  69. list_for_each_entry(chain, &child->val, list) {
  70. ++n;
  71. /* We need this because we may not have children */
  72. folded_sign = callchain_list__folded(chain);
  73. if (folded_sign == '+')
  74. break;
  75. }
  76. if (folded_sign == '-') /* Have children and they're unfolded */
  77. n += callchain_node__count_rows_rb_tree(child);
  78. }
  79. return n;
  80. }
  81. static int callchain_node__count_rows(struct callchain_node *node)
  82. {
  83. struct callchain_list *chain;
  84. bool unfolded = false;
  85. int n = 0;
  86. list_for_each_entry(chain, &node->val, list) {
  87. ++n;
  88. unfolded = chain->ms.unfolded;
  89. }
  90. if (unfolded)
  91. n += callchain_node__count_rows_rb_tree(node);
  92. return n;
  93. }
  94. static int callchain__count_rows(struct rb_root *chain)
  95. {
  96. struct rb_node *nd;
  97. int n = 0;
  98. for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
  99. struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
  100. n += callchain_node__count_rows(node);
  101. }
  102. return n;
  103. }
  104. static bool map_symbol__toggle_fold(struct map_symbol *self)
  105. {
  106. if (!self->has_children)
  107. return false;
  108. self->unfolded = !self->unfolded;
  109. return true;
  110. }
  111. static void callchain_node__init_have_children_rb_tree(struct callchain_node *self)
  112. {
  113. struct rb_node *nd = rb_first(&self->rb_root);
  114. for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
  115. struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
  116. struct callchain_list *chain;
  117. bool first = true;
  118. list_for_each_entry(chain, &child->val, list) {
  119. if (first) {
  120. first = false;
  121. chain->ms.has_children = chain->list.next != &child->val ||
  122. !RB_EMPTY_ROOT(&child->rb_root);
  123. } else
  124. chain->ms.has_children = chain->list.next == &child->val &&
  125. !RB_EMPTY_ROOT(&child->rb_root);
  126. }
  127. callchain_node__init_have_children_rb_tree(child);
  128. }
  129. }
  130. static void callchain_node__init_have_children(struct callchain_node *self)
  131. {
  132. struct callchain_list *chain;
  133. list_for_each_entry(chain, &self->val, list)
  134. chain->ms.has_children = !RB_EMPTY_ROOT(&self->rb_root);
  135. callchain_node__init_have_children_rb_tree(self);
  136. }
  137. static void callchain__init_have_children(struct rb_root *self)
  138. {
  139. struct rb_node *nd;
  140. for (nd = rb_first(self); nd; nd = rb_next(nd)) {
  141. struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
  142. callchain_node__init_have_children(node);
  143. }
  144. }
  145. static void hist_entry__init_have_children(struct hist_entry *self)
  146. {
  147. if (!self->init_have_children) {
  148. self->ms.has_children = !RB_EMPTY_ROOT(&self->sorted_chain);
  149. callchain__init_have_children(&self->sorted_chain);
  150. self->init_have_children = true;
  151. }
  152. }
  153. static bool hist_browser__toggle_fold(struct hist_browser *self)
  154. {
  155. if (map_symbol__toggle_fold(self->selection)) {
  156. struct hist_entry *he = self->he_selection;
  157. hist_entry__init_have_children(he);
  158. self->hists->nr_entries -= he->nr_rows;
  159. if (he->ms.unfolded)
  160. he->nr_rows = callchain__count_rows(&he->sorted_chain);
  161. else
  162. he->nr_rows = 0;
  163. self->hists->nr_entries += he->nr_rows;
  164. self->b.nr_entries = self->hists->nr_entries;
  165. return true;
  166. }
  167. /* If it doesn't have children, no toggling performed */
  168. return false;
  169. }
  170. static int callchain_node__set_folding_rb_tree(struct callchain_node *self, bool unfold)
  171. {
  172. int n = 0;
  173. struct rb_node *nd;
  174. for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
  175. struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
  176. struct callchain_list *chain;
  177. bool has_children = false;
  178. list_for_each_entry(chain, &child->val, list) {
  179. ++n;
  180. map_symbol__set_folding(&chain->ms, unfold);
  181. has_children = chain->ms.has_children;
  182. }
  183. if (has_children)
  184. n += callchain_node__set_folding_rb_tree(child, unfold);
  185. }
  186. return n;
  187. }
  188. static int callchain_node__set_folding(struct callchain_node *node, bool unfold)
  189. {
  190. struct callchain_list *chain;
  191. bool has_children = false;
  192. int n = 0;
  193. list_for_each_entry(chain, &node->val, list) {
  194. ++n;
  195. map_symbol__set_folding(&chain->ms, unfold);
  196. has_children = chain->ms.has_children;
  197. }
  198. if (has_children)
  199. n += callchain_node__set_folding_rb_tree(node, unfold);
  200. return n;
  201. }
  202. static int callchain__set_folding(struct rb_root *chain, bool unfold)
  203. {
  204. struct rb_node *nd;
  205. int n = 0;
  206. for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
  207. struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
  208. n += callchain_node__set_folding(node, unfold);
  209. }
  210. return n;
  211. }
  212. static void hist_entry__set_folding(struct hist_entry *self, bool unfold)
  213. {
  214. hist_entry__init_have_children(self);
  215. map_symbol__set_folding(&self->ms, unfold);
  216. if (self->ms.has_children) {
  217. int n = callchain__set_folding(&self->sorted_chain, unfold);
  218. self->nr_rows = unfold ? n : 0;
  219. } else
  220. self->nr_rows = 0;
  221. }
  222. static void hists__set_folding(struct hists *self, bool unfold)
  223. {
  224. struct rb_node *nd;
  225. self->nr_entries = 0;
  226. for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
  227. struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
  228. hist_entry__set_folding(he, unfold);
  229. self->nr_entries += 1 + he->nr_rows;
  230. }
  231. }
  232. static void hist_browser__set_folding(struct hist_browser *self, bool unfold)
  233. {
  234. hists__set_folding(self->hists, unfold);
  235. self->b.nr_entries = self->hists->nr_entries;
  236. /* Go to the start, we may be way after valid entries after a collapse */
  237. ui_browser__reset_index(&self->b);
  238. }
  239. static int hist_browser__run(struct hist_browser *self, const char *ev_name,
  240. void(*timer)(void *arg), void *arg, int delay_secs)
  241. {
  242. int key;
  243. char title[160];
  244. self->b.entries = &self->hists->entries;
  245. self->b.nr_entries = self->hists->nr_entries;
  246. hist_browser__refresh_dimensions(self);
  247. hists__browser_title(self->hists, title, sizeof(title), ev_name);
  248. if (ui_browser__show(&self->b, title,
  249. "Press '?' for help on key bindings") < 0)
  250. return -1;
  251. while (1) {
  252. key = ui_browser__run(&self->b, delay_secs);
  253. switch (key) {
  254. case K_TIMER:
  255. timer(arg);
  256. ui_browser__update_nr_entries(&self->b, self->hists->nr_entries);
  257. hists__browser_title(self->hists, title, sizeof(title),
  258. ev_name);
  259. ui_browser__show_title(&self->b, title);
  260. continue;
  261. case 'D': { /* Debug */
  262. static int seq;
  263. struct hist_entry *h = rb_entry(self->b.top,
  264. struct hist_entry, rb_node);
  265. ui_helpline__pop();
  266. ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
  267. seq++, self->b.nr_entries,
  268. self->hists->nr_entries,
  269. self->b.height,
  270. self->b.index,
  271. self->b.top_idx,
  272. h->row_offset, h->nr_rows);
  273. }
  274. break;
  275. case 'C':
  276. /* Collapse the whole world. */
  277. hist_browser__set_folding(self, false);
  278. break;
  279. case 'E':
  280. /* Expand the whole world. */
  281. hist_browser__set_folding(self, true);
  282. break;
  283. case K_ENTER:
  284. if (hist_browser__toggle_fold(self))
  285. break;
  286. /* fall thru */
  287. default:
  288. goto out;
  289. }
  290. }
  291. out:
  292. ui_browser__hide(&self->b);
  293. return key;
  294. }
  295. static char *callchain_list__sym_name(struct callchain_list *self,
  296. char *bf, size_t bfsize)
  297. {
  298. if (self->ms.sym)
  299. return self->ms.sym->name;
  300. snprintf(bf, bfsize, "%#" PRIx64, self->ip);
  301. return bf;
  302. }
  303. #define LEVEL_OFFSET_STEP 3
  304. static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self,
  305. struct callchain_node *chain_node,
  306. u64 total, int level,
  307. unsigned short row,
  308. off_t *row_offset,
  309. bool *is_current_entry)
  310. {
  311. struct rb_node *node;
  312. int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
  313. u64 new_total, remaining;
  314. if (callchain_param.mode == CHAIN_GRAPH_REL)
  315. new_total = chain_node->children_hit;
  316. else
  317. new_total = total;
  318. remaining = new_total;
  319. node = rb_first(&chain_node->rb_root);
  320. while (node) {
  321. struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
  322. struct rb_node *next = rb_next(node);
  323. u64 cumul = callchain_cumul_hits(child);
  324. struct callchain_list *chain;
  325. char folded_sign = ' ';
  326. int first = true;
  327. int extra_offset = 0;
  328. remaining -= cumul;
  329. list_for_each_entry(chain, &child->val, list) {
  330. char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str;
  331. const char *str;
  332. int color;
  333. bool was_first = first;
  334. if (first)
  335. first = false;
  336. else
  337. extra_offset = LEVEL_OFFSET_STEP;
  338. folded_sign = callchain_list__folded(chain);
  339. if (*row_offset != 0) {
  340. --*row_offset;
  341. goto do_next;
  342. }
  343. alloc_str = NULL;
  344. str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
  345. if (was_first) {
  346. double percent = cumul * 100.0 / new_total;
  347. if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
  348. str = "Not enough memory!";
  349. else
  350. str = alloc_str;
  351. }
  352. color = HE_COLORSET_NORMAL;
  353. width = self->b.width - (offset + extra_offset + 2);
  354. if (ui_browser__is_current_entry(&self->b, row)) {
  355. self->selection = &chain->ms;
  356. color = HE_COLORSET_SELECTED;
  357. *is_current_entry = true;
  358. }
  359. ui_browser__set_color(&self->b, color);
  360. ui_browser__gotorc(&self->b, row, 0);
  361. slsmg_write_nstring(" ", offset + extra_offset);
  362. slsmg_printf("%c ", folded_sign);
  363. slsmg_write_nstring(str, width);
  364. free(alloc_str);
  365. if (++row == self->b.height)
  366. goto out;
  367. do_next:
  368. if (folded_sign == '+')
  369. break;
  370. }
  371. if (folded_sign == '-') {
  372. const int new_level = level + (extra_offset ? 2 : 1);
  373. row += hist_browser__show_callchain_node_rb_tree(self, child, new_total,
  374. new_level, row, row_offset,
  375. is_current_entry);
  376. }
  377. if (row == self->b.height)
  378. goto out;
  379. node = next;
  380. }
  381. out:
  382. return row - first_row;
  383. }
  384. static int hist_browser__show_callchain_node(struct hist_browser *self,
  385. struct callchain_node *node,
  386. int level, unsigned short row,
  387. off_t *row_offset,
  388. bool *is_current_entry)
  389. {
  390. struct callchain_list *chain;
  391. int first_row = row,
  392. offset = level * LEVEL_OFFSET_STEP,
  393. width = self->b.width - offset;
  394. char folded_sign = ' ';
  395. list_for_each_entry(chain, &node->val, list) {
  396. char ipstr[BITS_PER_LONG / 4 + 1], *s;
  397. int color;
  398. folded_sign = callchain_list__folded(chain);
  399. if (*row_offset != 0) {
  400. --*row_offset;
  401. continue;
  402. }
  403. color = HE_COLORSET_NORMAL;
  404. if (ui_browser__is_current_entry(&self->b, row)) {
  405. self->selection = &chain->ms;
  406. color = HE_COLORSET_SELECTED;
  407. *is_current_entry = true;
  408. }
  409. s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
  410. ui_browser__gotorc(&self->b, row, 0);
  411. ui_browser__set_color(&self->b, color);
  412. slsmg_write_nstring(" ", offset);
  413. slsmg_printf("%c ", folded_sign);
  414. slsmg_write_nstring(s, width - 2);
  415. if (++row == self->b.height)
  416. goto out;
  417. }
  418. if (folded_sign == '-')
  419. row += hist_browser__show_callchain_node_rb_tree(self, node,
  420. self->hists->stats.total_period,
  421. level + 1, row,
  422. row_offset,
  423. is_current_entry);
  424. out:
  425. return row - first_row;
  426. }
  427. static int hist_browser__show_callchain(struct hist_browser *self,
  428. struct rb_root *chain,
  429. int level, unsigned short row,
  430. off_t *row_offset,
  431. bool *is_current_entry)
  432. {
  433. struct rb_node *nd;
  434. int first_row = row;
  435. for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
  436. struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
  437. row += hist_browser__show_callchain_node(self, node, level,
  438. row, row_offset,
  439. is_current_entry);
  440. if (row == self->b.height)
  441. break;
  442. }
  443. return row - first_row;
  444. }
  445. static int hist_browser__show_entry(struct hist_browser *self,
  446. struct hist_entry *entry,
  447. unsigned short row)
  448. {
  449. char s[256];
  450. double percent;
  451. int printed = 0;
  452. int width = self->b.width - 6; /* The percentage */
  453. char folded_sign = ' ';
  454. bool current_entry = ui_browser__is_current_entry(&self->b, row);
  455. off_t row_offset = entry->row_offset;
  456. if (current_entry) {
  457. self->he_selection = entry;
  458. self->selection = &entry->ms;
  459. }
  460. if (symbol_conf.use_callchain) {
  461. hist_entry__init_have_children(entry);
  462. folded_sign = hist_entry__folded(entry);
  463. }
  464. if (row_offset == 0) {
  465. hist_entry__snprintf(entry, s, sizeof(s), self->hists);
  466. percent = (entry->period * 100.0) / self->hists->stats.total_period;
  467. ui_browser__set_percent_color(&self->b, percent, current_entry);
  468. ui_browser__gotorc(&self->b, row, 0);
  469. if (symbol_conf.use_callchain) {
  470. slsmg_printf("%c ", folded_sign);
  471. width -= 2;
  472. }
  473. slsmg_printf(" %5.2f%%", percent);
  474. /* The scroll bar isn't being used */
  475. if (!self->b.navkeypressed)
  476. width += 1;
  477. if (!current_entry || !self->b.navkeypressed)
  478. ui_browser__set_color(&self->b, HE_COLORSET_NORMAL);
  479. if (symbol_conf.show_nr_samples) {
  480. slsmg_printf(" %11u", entry->nr_events);
  481. width -= 12;
  482. }
  483. if (symbol_conf.show_total_period) {
  484. slsmg_printf(" %12" PRIu64, entry->period);
  485. width -= 13;
  486. }
  487. slsmg_write_nstring(s, width);
  488. ++row;
  489. ++printed;
  490. } else
  491. --row_offset;
  492. if (folded_sign == '-' && row != self->b.height) {
  493. printed += hist_browser__show_callchain(self, &entry->sorted_chain,
  494. 1, row, &row_offset,
  495. &current_entry);
  496. if (current_entry)
  497. self->he_selection = entry;
  498. }
  499. return printed;
  500. }
  501. static void ui_browser__hists_init_top(struct ui_browser *browser)
  502. {
  503. if (browser->top == NULL) {
  504. struct hist_browser *hb;
  505. hb = container_of(browser, struct hist_browser, b);
  506. browser->top = rb_first(&hb->hists->entries);
  507. }
  508. }
  509. static unsigned int hist_browser__refresh(struct ui_browser *self)
  510. {
  511. unsigned row = 0;
  512. struct rb_node *nd;
  513. struct hist_browser *hb = container_of(self, struct hist_browser, b);
  514. ui_browser__hists_init_top(self);
  515. for (nd = self->top; nd; nd = rb_next(nd)) {
  516. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  517. if (h->filtered)
  518. continue;
  519. row += hist_browser__show_entry(hb, h, row);
  520. if (row == self->height)
  521. break;
  522. }
  523. return row;
  524. }
  525. static struct rb_node *hists__filter_entries(struct rb_node *nd)
  526. {
  527. while (nd != NULL) {
  528. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  529. if (!h->filtered)
  530. return nd;
  531. nd = rb_next(nd);
  532. }
  533. return NULL;
  534. }
  535. static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
  536. {
  537. while (nd != NULL) {
  538. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  539. if (!h->filtered)
  540. return nd;
  541. nd = rb_prev(nd);
  542. }
  543. return NULL;
  544. }
  545. static void ui_browser__hists_seek(struct ui_browser *self,
  546. off_t offset, int whence)
  547. {
  548. struct hist_entry *h;
  549. struct rb_node *nd;
  550. bool first = true;
  551. if (self->nr_entries == 0)
  552. return;
  553. ui_browser__hists_init_top(self);
  554. switch (whence) {
  555. case SEEK_SET:
  556. nd = hists__filter_entries(rb_first(self->entries));
  557. break;
  558. case SEEK_CUR:
  559. nd = self->top;
  560. goto do_offset;
  561. case SEEK_END:
  562. nd = hists__filter_prev_entries(rb_last(self->entries));
  563. first = false;
  564. break;
  565. default:
  566. return;
  567. }
  568. /*
  569. * Moves not relative to the first visible entry invalidates its
  570. * row_offset:
  571. */
  572. h = rb_entry(self->top, struct hist_entry, rb_node);
  573. h->row_offset = 0;
  574. /*
  575. * Here we have to check if nd is expanded (+), if it is we can't go
  576. * the next top level hist_entry, instead we must compute an offset of
  577. * what _not_ to show and not change the first visible entry.
  578. *
  579. * This offset increments when we are going from top to bottom and
  580. * decreases when we're going from bottom to top.
  581. *
  582. * As we don't have backpointers to the top level in the callchains
  583. * structure, we need to always print the whole hist_entry callchain,
  584. * skipping the first ones that are before the first visible entry
  585. * and stop when we printed enough lines to fill the screen.
  586. */
  587. do_offset:
  588. if (offset > 0) {
  589. do {
  590. h = rb_entry(nd, struct hist_entry, rb_node);
  591. if (h->ms.unfolded) {
  592. u16 remaining = h->nr_rows - h->row_offset;
  593. if (offset > remaining) {
  594. offset -= remaining;
  595. h->row_offset = 0;
  596. } else {
  597. h->row_offset += offset;
  598. offset = 0;
  599. self->top = nd;
  600. break;
  601. }
  602. }
  603. nd = hists__filter_entries(rb_next(nd));
  604. if (nd == NULL)
  605. break;
  606. --offset;
  607. self->top = nd;
  608. } while (offset != 0);
  609. } else if (offset < 0) {
  610. while (1) {
  611. h = rb_entry(nd, struct hist_entry, rb_node);
  612. if (h->ms.unfolded) {
  613. if (first) {
  614. if (-offset > h->row_offset) {
  615. offset += h->row_offset;
  616. h->row_offset = 0;
  617. } else {
  618. h->row_offset += offset;
  619. offset = 0;
  620. self->top = nd;
  621. break;
  622. }
  623. } else {
  624. if (-offset > h->nr_rows) {
  625. offset += h->nr_rows;
  626. h->row_offset = 0;
  627. } else {
  628. h->row_offset = h->nr_rows + offset;
  629. offset = 0;
  630. self->top = nd;
  631. break;
  632. }
  633. }
  634. }
  635. nd = hists__filter_prev_entries(rb_prev(nd));
  636. if (nd == NULL)
  637. break;
  638. ++offset;
  639. self->top = nd;
  640. if (offset == 0) {
  641. /*
  642. * Last unfiltered hist_entry, check if it is
  643. * unfolded, if it is then we should have
  644. * row_offset at its last entry.
  645. */
  646. h = rb_entry(nd, struct hist_entry, rb_node);
  647. if (h->ms.unfolded)
  648. h->row_offset = h->nr_rows;
  649. break;
  650. }
  651. first = false;
  652. }
  653. } else {
  654. self->top = nd;
  655. h = rb_entry(nd, struct hist_entry, rb_node);
  656. h->row_offset = 0;
  657. }
  658. }
  659. static struct hist_browser *hist_browser__new(struct hists *hists)
  660. {
  661. struct hist_browser *self = zalloc(sizeof(*self));
  662. if (self) {
  663. self->hists = hists;
  664. self->b.refresh = hist_browser__refresh;
  665. self->b.seek = ui_browser__hists_seek;
  666. self->b.use_navkeypressed = true,
  667. self->has_symbols = sort_sym.list.next != NULL;
  668. }
  669. return self;
  670. }
  671. static void hist_browser__delete(struct hist_browser *self)
  672. {
  673. free(self);
  674. }
  675. static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
  676. {
  677. return self->he_selection;
  678. }
  679. static struct thread *hist_browser__selected_thread(struct hist_browser *self)
  680. {
  681. return self->he_selection->thread;
  682. }
  683. static int hists__browser_title(struct hists *self, char *bf, size_t size,
  684. const char *ev_name)
  685. {
  686. char unit;
  687. int printed;
  688. const struct dso *dso = self->dso_filter;
  689. const struct thread *thread = self->thread_filter;
  690. unsigned long nr_events = self->stats.nr_events[PERF_RECORD_SAMPLE];
  691. nr_events = convert_unit(nr_events, &unit);
  692. printed = snprintf(bf, size, "Events: %lu%c %s", nr_events, unit, ev_name);
  693. if (thread)
  694. printed += snprintf(bf + printed, size - printed,
  695. ", Thread: %s(%d)",
  696. (thread->comm_set ? thread->comm : ""),
  697. thread->pid);
  698. if (dso)
  699. printed += snprintf(bf + printed, size - printed,
  700. ", DSO: %s", dso->short_name);
  701. return printed;
  702. }
  703. static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
  704. const char *helpline, const char *ev_name,
  705. bool left_exits,
  706. void(*timer)(void *arg), void *arg,
  707. int delay_secs)
  708. {
  709. struct hists *self = &evsel->hists;
  710. struct hist_browser *browser = hist_browser__new(self);
  711. struct pstack *fstack;
  712. int key = -1;
  713. if (browser == NULL)
  714. return -1;
  715. fstack = pstack__new(2);
  716. if (fstack == NULL)
  717. goto out;
  718. ui_helpline__push(helpline);
  719. while (1) {
  720. const struct thread *thread = NULL;
  721. const struct dso *dso = NULL;
  722. char *options[16];
  723. int nr_options = 0, choice = 0, i,
  724. annotate = -2, zoom_dso = -2, zoom_thread = -2,
  725. browse_map = -2;
  726. key = hist_browser__run(browser, ev_name, timer, arg, delay_secs);
  727. if (browser->he_selection != NULL) {
  728. thread = hist_browser__selected_thread(browser);
  729. dso = browser->selection->map ? browser->selection->map->dso : NULL;
  730. }
  731. switch (key) {
  732. case K_TAB:
  733. case K_UNTAB:
  734. if (nr_events == 1)
  735. continue;
  736. /*
  737. * Exit the browser, let hists__browser_tree
  738. * go to the next or previous
  739. */
  740. goto out_free_stack;
  741. case 'a':
  742. if (!browser->has_symbols) {
  743. ui_browser__warning(&browser->b,
  744. "Annotation is only available for symbolic views, "
  745. "include \"sym\" in --sort to use it.");
  746. continue;
  747. }
  748. if (browser->selection == NULL ||
  749. browser->selection->sym == NULL ||
  750. browser->selection->map->dso->annotate_warned)
  751. continue;
  752. goto do_annotate;
  753. case 'd':
  754. goto zoom_dso;
  755. case 't':
  756. goto zoom_thread;
  757. case K_F1:
  758. case 'h':
  759. case '?':
  760. ui_browser__help_window(&browser->b,
  761. "h/?/F1 Show this window\n"
  762. "UP/DOWN/PGUP\n"
  763. "PGDN/SPACE Navigate\n"
  764. "q/ESC/CTRL+C Exit browser\n\n"
  765. "For multiple event sessions:\n\n"
  766. "TAB/UNTAB Switch events\n\n"
  767. "For symbolic views (--sort has sym):\n\n"
  768. "-> Zoom into DSO/Threads & Annotate current symbol\n"
  769. "<- Zoom out\n"
  770. "a Annotate current symbol\n"
  771. "C Collapse all callchains\n"
  772. "E Expand all callchains\n"
  773. "d Zoom into current DSO\n"
  774. "t Zoom into current Thread");
  775. continue;
  776. case K_ENTER:
  777. case K_RIGHT:
  778. /* menu */
  779. break;
  780. case K_LEFT: {
  781. const void *top;
  782. if (pstack__empty(fstack)) {
  783. /*
  784. * Go back to the perf_evsel_menu__run or other user
  785. */
  786. if (left_exits)
  787. goto out_free_stack;
  788. continue;
  789. }
  790. top = pstack__pop(fstack);
  791. if (top == &browser->hists->dso_filter)
  792. goto zoom_out_dso;
  793. if (top == &browser->hists->thread_filter)
  794. goto zoom_out_thread;
  795. continue;
  796. }
  797. case K_ESC:
  798. if (!left_exits &&
  799. !ui_browser__dialog_yesno(&browser->b,
  800. "Do you really want to exit?"))
  801. continue;
  802. /* Fall thru */
  803. case 'q':
  804. case CTRL('c'):
  805. goto out_free_stack;
  806. default:
  807. continue;
  808. }
  809. if (!browser->has_symbols)
  810. goto add_exit_option;
  811. if (browser->selection != NULL &&
  812. browser->selection->sym != NULL &&
  813. !browser->selection->map->dso->annotate_warned &&
  814. asprintf(&options[nr_options], "Annotate %s",
  815. browser->selection->sym->name) > 0)
  816. annotate = nr_options++;
  817. if (thread != NULL &&
  818. asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
  819. (browser->hists->thread_filter ? "out of" : "into"),
  820. (thread->comm_set ? thread->comm : ""),
  821. thread->pid) > 0)
  822. zoom_thread = nr_options++;
  823. if (dso != NULL &&
  824. asprintf(&options[nr_options], "Zoom %s %s DSO",
  825. (browser->hists->dso_filter ? "out of" : "into"),
  826. (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
  827. zoom_dso = nr_options++;
  828. if (browser->selection != NULL &&
  829. browser->selection->map != NULL &&
  830. asprintf(&options[nr_options], "Browse map details") > 0)
  831. browse_map = nr_options++;
  832. add_exit_option:
  833. options[nr_options++] = (char *)"Exit";
  834. choice = ui__popup_menu(nr_options, options);
  835. for (i = 0; i < nr_options - 1; ++i)
  836. free(options[i]);
  837. if (choice == nr_options - 1)
  838. break;
  839. if (choice == -1)
  840. continue;
  841. if (choice == annotate) {
  842. struct hist_entry *he;
  843. int err;
  844. do_annotate:
  845. he = hist_browser__selected_entry(browser);
  846. if (he == NULL)
  847. continue;
  848. /*
  849. * Don't let this be freed, say, by hists__decay_entry.
  850. */
  851. he->used = true;
  852. err = hist_entry__tui_annotate(he, evsel->idx, nr_events,
  853. timer, arg, delay_secs);
  854. he->used = false;
  855. ui_browser__update_nr_entries(&browser->b, browser->hists->nr_entries);
  856. if (err)
  857. ui_browser__handle_resize(&browser->b);
  858. } else if (choice == browse_map)
  859. map__browse(browser->selection->map);
  860. else if (choice == zoom_dso) {
  861. zoom_dso:
  862. if (browser->hists->dso_filter) {
  863. pstack__remove(fstack, &browser->hists->dso_filter);
  864. zoom_out_dso:
  865. ui_helpline__pop();
  866. browser->hists->dso_filter = NULL;
  867. sort_dso.elide = false;
  868. } else {
  869. if (dso == NULL)
  870. continue;
  871. ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
  872. dso->kernel ? "the Kernel" : dso->short_name);
  873. browser->hists->dso_filter = dso;
  874. sort_dso.elide = true;
  875. pstack__push(fstack, &browser->hists->dso_filter);
  876. }
  877. hists__filter_by_dso(self);
  878. hist_browser__reset(browser);
  879. } else if (choice == zoom_thread) {
  880. zoom_thread:
  881. if (browser->hists->thread_filter) {
  882. pstack__remove(fstack, &browser->hists->thread_filter);
  883. zoom_out_thread:
  884. ui_helpline__pop();
  885. browser->hists->thread_filter = NULL;
  886. sort_thread.elide = false;
  887. } else {
  888. ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
  889. thread->comm_set ? thread->comm : "",
  890. thread->pid);
  891. browser->hists->thread_filter = thread;
  892. sort_thread.elide = true;
  893. pstack__push(fstack, &browser->hists->thread_filter);
  894. }
  895. hists__filter_by_thread(self);
  896. hist_browser__reset(browser);
  897. }
  898. }
  899. out_free_stack:
  900. pstack__delete(fstack);
  901. out:
  902. hist_browser__delete(browser);
  903. return key;
  904. }
  905. struct perf_evsel_menu {
  906. struct ui_browser b;
  907. struct perf_evsel *selection;
  908. };
  909. static void perf_evsel_menu__write(struct ui_browser *browser,
  910. void *entry, int row)
  911. {
  912. struct perf_evsel_menu *menu = container_of(browser,
  913. struct perf_evsel_menu, b);
  914. struct perf_evsel *evsel = list_entry(entry, struct perf_evsel, node);
  915. bool current_entry = ui_browser__is_current_entry(browser, row);
  916. unsigned long nr_events = evsel->hists.stats.nr_events[PERF_RECORD_SAMPLE];
  917. const char *ev_name = event_name(evsel);
  918. char bf[256], unit;
  919. ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
  920. HE_COLORSET_NORMAL);
  921. nr_events = convert_unit(nr_events, &unit);
  922. snprintf(bf, sizeof(bf), "%lu%c%s%s", nr_events,
  923. unit, unit == ' ' ? "" : " ", ev_name);
  924. slsmg_write_nstring(bf, browser->width);
  925. if (current_entry)
  926. menu->selection = evsel;
  927. }
  928. static int perf_evsel_menu__run(struct perf_evsel_menu *menu,
  929. int nr_events, const char *help,
  930. void(*timer)(void *arg), void *arg, int delay_secs)
  931. {
  932. struct perf_evlist *evlist = menu->b.priv;
  933. struct perf_evsel *pos;
  934. const char *ev_name, *title = "Available samples";
  935. int key;
  936. if (ui_browser__show(&menu->b, title,
  937. "ESC: exit, ENTER|->: Browse histograms") < 0)
  938. return -1;
  939. while (1) {
  940. key = ui_browser__run(&menu->b, delay_secs);
  941. switch (key) {
  942. case K_TIMER:
  943. timer(arg);
  944. continue;
  945. case K_RIGHT:
  946. case K_ENTER:
  947. if (!menu->selection)
  948. continue;
  949. pos = menu->selection;
  950. browse_hists:
  951. perf_evlist__set_selected(evlist, pos);
  952. /*
  953. * Give the calling tool a chance to populate the non
  954. * default evsel resorted hists tree.
  955. */
  956. if (timer)
  957. timer(arg);
  958. ev_name = event_name(pos);
  959. key = perf_evsel__hists_browse(pos, nr_events, help,
  960. ev_name, true, timer,
  961. arg, delay_secs);
  962. ui_browser__show_title(&menu->b, title);
  963. switch (key) {
  964. case K_TAB:
  965. if (pos->node.next == &evlist->entries)
  966. pos = list_entry(evlist->entries.next, struct perf_evsel, node);
  967. else
  968. pos = list_entry(pos->node.next, struct perf_evsel, node);
  969. goto browse_hists;
  970. case K_UNTAB:
  971. if (pos->node.prev == &evlist->entries)
  972. pos = list_entry(evlist->entries.prev, struct perf_evsel, node);
  973. else
  974. pos = list_entry(pos->node.prev, struct perf_evsel, node);
  975. goto browse_hists;
  976. case K_ESC:
  977. if (!ui_browser__dialog_yesno(&menu->b,
  978. "Do you really want to exit?"))
  979. continue;
  980. /* Fall thru */
  981. case 'q':
  982. case CTRL('c'):
  983. goto out;
  984. default:
  985. continue;
  986. }
  987. case K_LEFT:
  988. continue;
  989. case K_ESC:
  990. if (!ui_browser__dialog_yesno(&menu->b,
  991. "Do you really want to exit?"))
  992. continue;
  993. /* Fall thru */
  994. case 'q':
  995. case CTRL('c'):
  996. goto out;
  997. default:
  998. continue;
  999. }
  1000. }
  1001. out:
  1002. ui_browser__hide(&menu->b);
  1003. return key;
  1004. }
  1005. static int __perf_evlist__tui_browse_hists(struct perf_evlist *evlist,
  1006. const char *help,
  1007. void(*timer)(void *arg), void *arg,
  1008. int delay_secs)
  1009. {
  1010. struct perf_evsel *pos;
  1011. struct perf_evsel_menu menu = {
  1012. .b = {
  1013. .entries = &evlist->entries,
  1014. .refresh = ui_browser__list_head_refresh,
  1015. .seek = ui_browser__list_head_seek,
  1016. .write = perf_evsel_menu__write,
  1017. .nr_entries = evlist->nr_entries,
  1018. .priv = evlist,
  1019. },
  1020. };
  1021. ui_helpline__push("Press ESC to exit");
  1022. list_for_each_entry(pos, &evlist->entries, node) {
  1023. const char *ev_name = event_name(pos);
  1024. size_t line_len = strlen(ev_name) + 7;
  1025. if (menu.b.width < line_len)
  1026. menu.b.width = line_len;
  1027. /*
  1028. * Cache the evsel name, tracepoints have a _high_ cost per
  1029. * event_name() call.
  1030. */
  1031. if (pos->name == NULL)
  1032. pos->name = strdup(ev_name);
  1033. }
  1034. return perf_evsel_menu__run(&menu, evlist->nr_entries, help, timer,
  1035. arg, delay_secs);
  1036. }
  1037. int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help,
  1038. void(*timer)(void *arg), void *arg,
  1039. int delay_secs)
  1040. {
  1041. if (evlist->nr_entries == 1) {
  1042. struct perf_evsel *first = list_entry(evlist->entries.next,
  1043. struct perf_evsel, node);
  1044. const char *ev_name = event_name(first);
  1045. return perf_evsel__hists_browse(first, evlist->nr_entries, help,
  1046. ev_name, false, timer, arg,
  1047. delay_secs);
  1048. }
  1049. return __perf_evlist__tui_browse_hists(evlist, help,
  1050. timer, arg, delay_secs);
  1051. }