hists.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  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 "map.h"
  19. struct hist_browser {
  20. struct ui_browser b;
  21. struct hists *hists;
  22. struct hist_entry *he_selection;
  23. struct map_symbol *selection;
  24. bool has_symbols;
  25. };
  26. static int hists__browser_title(struct hists *self, char *bf, size_t size,
  27. const char *ev_name);
  28. static void hist_browser__refresh_dimensions(struct hist_browser *self)
  29. {
  30. /* 3 == +/- toggle symbol before actual hist_entry rendering */
  31. self->b.width = 3 + (hists__sort_list_width(self->hists) +
  32. sizeof("[k]"));
  33. }
  34. static void hist_browser__reset(struct hist_browser *self)
  35. {
  36. self->b.nr_entries = self->hists->nr_entries;
  37. hist_browser__refresh_dimensions(self);
  38. ui_browser__reset_index(&self->b);
  39. }
  40. static char tree__folded_sign(bool unfolded)
  41. {
  42. return unfolded ? '-' : '+';
  43. }
  44. static char map_symbol__folded(const struct map_symbol *self)
  45. {
  46. return self->has_children ? tree__folded_sign(self->unfolded) : ' ';
  47. }
  48. static char hist_entry__folded(const struct hist_entry *self)
  49. {
  50. return map_symbol__folded(&self->ms);
  51. }
  52. static char callchain_list__folded(const struct callchain_list *self)
  53. {
  54. return map_symbol__folded(&self->ms);
  55. }
  56. static void map_symbol__set_folding(struct map_symbol *self, bool unfold)
  57. {
  58. self->unfolded = unfold ? self->has_children : false;
  59. }
  60. static int callchain_node__count_rows_rb_tree(struct callchain_node *self)
  61. {
  62. int n = 0;
  63. struct rb_node *nd;
  64. for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
  65. struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
  66. struct callchain_list *chain;
  67. char folded_sign = ' '; /* No children */
  68. list_for_each_entry(chain, &child->val, list) {
  69. ++n;
  70. /* We need this because we may not have children */
  71. folded_sign = callchain_list__folded(chain);
  72. if (folded_sign == '+')
  73. break;
  74. }
  75. if (folded_sign == '-') /* Have children and they're unfolded */
  76. n += callchain_node__count_rows_rb_tree(child);
  77. }
  78. return n;
  79. }
  80. static int callchain_node__count_rows(struct callchain_node *node)
  81. {
  82. struct callchain_list *chain;
  83. bool unfolded = false;
  84. int n = 0;
  85. list_for_each_entry(chain, &node->val, list) {
  86. ++n;
  87. unfolded = chain->ms.unfolded;
  88. }
  89. if (unfolded)
  90. n += callchain_node__count_rows_rb_tree(node);
  91. return n;
  92. }
  93. static int callchain__count_rows(struct rb_root *chain)
  94. {
  95. struct rb_node *nd;
  96. int n = 0;
  97. for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
  98. struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
  99. n += callchain_node__count_rows(node);
  100. }
  101. return n;
  102. }
  103. static bool map_symbol__toggle_fold(struct map_symbol *self)
  104. {
  105. if (!self->has_children)
  106. return false;
  107. self->unfolded = !self->unfolded;
  108. return true;
  109. }
  110. static void callchain_node__init_have_children_rb_tree(struct callchain_node *self)
  111. {
  112. struct rb_node *nd = rb_first(&self->rb_root);
  113. for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
  114. struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
  115. struct callchain_list *chain;
  116. bool first = true;
  117. list_for_each_entry(chain, &child->val, list) {
  118. if (first) {
  119. first = false;
  120. chain->ms.has_children = chain->list.next != &child->val ||
  121. !RB_EMPTY_ROOT(&child->rb_root);
  122. } else
  123. chain->ms.has_children = chain->list.next == &child->val &&
  124. !RB_EMPTY_ROOT(&child->rb_root);
  125. }
  126. callchain_node__init_have_children_rb_tree(child);
  127. }
  128. }
  129. static void callchain_node__init_have_children(struct callchain_node *self)
  130. {
  131. struct callchain_list *chain;
  132. list_for_each_entry(chain, &self->val, list)
  133. chain->ms.has_children = !RB_EMPTY_ROOT(&self->rb_root);
  134. callchain_node__init_have_children_rb_tree(self);
  135. }
  136. static void callchain__init_have_children(struct rb_root *self)
  137. {
  138. struct rb_node *nd;
  139. for (nd = rb_first(self); nd; nd = rb_next(nd)) {
  140. struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
  141. callchain_node__init_have_children(node);
  142. }
  143. }
  144. static void hist_entry__init_have_children(struct hist_entry *self)
  145. {
  146. if (!self->init_have_children) {
  147. self->ms.has_children = !RB_EMPTY_ROOT(&self->sorted_chain);
  148. callchain__init_have_children(&self->sorted_chain);
  149. self->init_have_children = true;
  150. }
  151. }
  152. static bool hist_browser__toggle_fold(struct hist_browser *self)
  153. {
  154. if (map_symbol__toggle_fold(self->selection)) {
  155. struct hist_entry *he = self->he_selection;
  156. hist_entry__init_have_children(he);
  157. self->hists->nr_entries -= he->nr_rows;
  158. if (he->ms.unfolded)
  159. he->nr_rows = callchain__count_rows(&he->sorted_chain);
  160. else
  161. he->nr_rows = 0;
  162. self->hists->nr_entries += he->nr_rows;
  163. self->b.nr_entries = self->hists->nr_entries;
  164. return true;
  165. }
  166. /* If it doesn't have children, no toggling performed */
  167. return false;
  168. }
  169. static int callchain_node__set_folding_rb_tree(struct callchain_node *self, bool unfold)
  170. {
  171. int n = 0;
  172. struct rb_node *nd;
  173. for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
  174. struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
  175. struct callchain_list *chain;
  176. bool has_children = false;
  177. list_for_each_entry(chain, &child->val, list) {
  178. ++n;
  179. map_symbol__set_folding(&chain->ms, unfold);
  180. has_children = chain->ms.has_children;
  181. }
  182. if (has_children)
  183. n += callchain_node__set_folding_rb_tree(child, unfold);
  184. }
  185. return n;
  186. }
  187. static int callchain_node__set_folding(struct callchain_node *node, bool unfold)
  188. {
  189. struct callchain_list *chain;
  190. bool has_children = false;
  191. int n = 0;
  192. list_for_each_entry(chain, &node->val, list) {
  193. ++n;
  194. map_symbol__set_folding(&chain->ms, unfold);
  195. has_children = chain->ms.has_children;
  196. }
  197. if (has_children)
  198. n += callchain_node__set_folding_rb_tree(node, unfold);
  199. return n;
  200. }
  201. static int callchain__set_folding(struct rb_root *chain, bool unfold)
  202. {
  203. struct rb_node *nd;
  204. int n = 0;
  205. for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
  206. struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
  207. n += callchain_node__set_folding(node, unfold);
  208. }
  209. return n;
  210. }
  211. static void hist_entry__set_folding(struct hist_entry *self, bool unfold)
  212. {
  213. hist_entry__init_have_children(self);
  214. map_symbol__set_folding(&self->ms, unfold);
  215. if (self->ms.has_children) {
  216. int n = callchain__set_folding(&self->sorted_chain, unfold);
  217. self->nr_rows = unfold ? n : 0;
  218. } else
  219. self->nr_rows = 0;
  220. }
  221. static void hists__set_folding(struct hists *self, bool unfold)
  222. {
  223. struct rb_node *nd;
  224. self->nr_entries = 0;
  225. for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
  226. struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
  227. hist_entry__set_folding(he, unfold);
  228. self->nr_entries += 1 + he->nr_rows;
  229. }
  230. }
  231. static void hist_browser__set_folding(struct hist_browser *self, bool unfold)
  232. {
  233. hists__set_folding(self->hists, unfold);
  234. self->b.nr_entries = self->hists->nr_entries;
  235. /* Go to the start, we may be way after valid entries after a collapse */
  236. ui_browser__reset_index(&self->b);
  237. }
  238. static int hist_browser__run(struct hist_browser *self, const char *ev_name,
  239. void(*timer)(void *arg), void *arg, int delay_secs)
  240. {
  241. int key;
  242. char title[160];
  243. self->b.entries = &self->hists->entries;
  244. self->b.nr_entries = self->hists->nr_entries;
  245. hist_browser__refresh_dimensions(self);
  246. hists__browser_title(self->hists, title, sizeof(title), ev_name);
  247. if (ui_browser__show(&self->b, title,
  248. "Press '?' for help on key bindings") < 0)
  249. return -1;
  250. while (1) {
  251. key = ui_browser__run(&self->b, delay_secs);
  252. switch (key) {
  253. case -1:
  254. /* FIXME we need to check if it was es.reason == NEWT_EXIT_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__warning(
  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__help_window("h/?/F1 Show this window\n"
  761. "UP/DOWN/PGUP\n"
  762. "PGDN/SPACE Navigate\n"
  763. "q/ESC/CTRL+C Exit browser\n\n"
  764. "For multiple event sessions:\n\n"
  765. "TAB/UNTAB Switch events\n\n"
  766. "For symbolic views (--sort has sym):\n\n"
  767. "-> Zoom into DSO/Threads & Annotate current symbol\n"
  768. "<- Zoom out\n"
  769. "a Annotate current symbol\n"
  770. "C Collapse all callchains\n"
  771. "E Expand all callchains\n"
  772. "d Zoom into current DSO\n"
  773. "t Zoom into current Thread\n");
  774. continue;
  775. case K_ENTER:
  776. case K_RIGHT:
  777. /* menu */
  778. break;
  779. case K_LEFT: {
  780. const void *top;
  781. if (pstack__empty(fstack)) {
  782. /*
  783. * Go back to the perf_evsel_menu__run or other user
  784. */
  785. if (left_exits)
  786. goto out_free_stack;
  787. continue;
  788. }
  789. top = pstack__pop(fstack);
  790. if (top == &browser->hists->dso_filter)
  791. goto zoom_out_dso;
  792. if (top == &browser->hists->thread_filter)
  793. goto zoom_out_thread;
  794. continue;
  795. }
  796. case K_ESC:
  797. if (!left_exits &&
  798. !ui__dialog_yesno("Do you really want to exit?"))
  799. continue;
  800. /* Fall thru */
  801. case 'q':
  802. case CTRL('c'):
  803. goto out_free_stack;
  804. default:
  805. continue;
  806. }
  807. if (!browser->has_symbols)
  808. goto add_exit_option;
  809. if (browser->selection != NULL &&
  810. browser->selection->sym != NULL &&
  811. !browser->selection->map->dso->annotate_warned &&
  812. asprintf(&options[nr_options], "Annotate %s",
  813. browser->selection->sym->name) > 0)
  814. annotate = nr_options++;
  815. if (thread != NULL &&
  816. asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
  817. (browser->hists->thread_filter ? "out of" : "into"),
  818. (thread->comm_set ? thread->comm : ""),
  819. thread->pid) > 0)
  820. zoom_thread = nr_options++;
  821. if (dso != NULL &&
  822. asprintf(&options[nr_options], "Zoom %s %s DSO",
  823. (browser->hists->dso_filter ? "out of" : "into"),
  824. (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
  825. zoom_dso = nr_options++;
  826. if (browser->selection != NULL &&
  827. browser->selection->map != NULL &&
  828. asprintf(&options[nr_options], "Browse map details") > 0)
  829. browse_map = nr_options++;
  830. add_exit_option:
  831. options[nr_options++] = (char *)"Exit";
  832. choice = ui__popup_menu(nr_options, options);
  833. for (i = 0; i < nr_options - 1; ++i)
  834. free(options[i]);
  835. if (choice == nr_options - 1)
  836. break;
  837. if (choice == -1)
  838. continue;
  839. if (choice == annotate) {
  840. struct hist_entry *he;
  841. do_annotate:
  842. he = hist_browser__selected_entry(browser);
  843. if (he == NULL)
  844. continue;
  845. /*
  846. * Don't let this be freed, say, by hists__decay_entry.
  847. */
  848. he->used = true;
  849. hist_entry__tui_annotate(he, evsel->idx, nr_events,
  850. timer, arg, delay_secs);
  851. he->used = false;
  852. ui_browser__update_nr_entries(&browser->b, browser->hists->nr_entries);
  853. } else if (choice == browse_map)
  854. map__browse(browser->selection->map);
  855. else if (choice == zoom_dso) {
  856. zoom_dso:
  857. if (browser->hists->dso_filter) {
  858. pstack__remove(fstack, &browser->hists->dso_filter);
  859. zoom_out_dso:
  860. ui_helpline__pop();
  861. browser->hists->dso_filter = NULL;
  862. sort_dso.elide = false;
  863. } else {
  864. if (dso == NULL)
  865. continue;
  866. ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
  867. dso->kernel ? "the Kernel" : dso->short_name);
  868. browser->hists->dso_filter = dso;
  869. sort_dso.elide = true;
  870. pstack__push(fstack, &browser->hists->dso_filter);
  871. }
  872. hists__filter_by_dso(self);
  873. hist_browser__reset(browser);
  874. } else if (choice == zoom_thread) {
  875. zoom_thread:
  876. if (browser->hists->thread_filter) {
  877. pstack__remove(fstack, &browser->hists->thread_filter);
  878. zoom_out_thread:
  879. ui_helpline__pop();
  880. browser->hists->thread_filter = NULL;
  881. sort_thread.elide = false;
  882. } else {
  883. ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
  884. thread->comm_set ? thread->comm : "",
  885. thread->pid);
  886. browser->hists->thread_filter = thread;
  887. sort_thread.elide = true;
  888. pstack__push(fstack, &browser->hists->thread_filter);
  889. }
  890. hists__filter_by_thread(self);
  891. hist_browser__reset(browser);
  892. }
  893. }
  894. out_free_stack:
  895. pstack__delete(fstack);
  896. out:
  897. hist_browser__delete(browser);
  898. return key;
  899. }
  900. struct perf_evsel_menu {
  901. struct ui_browser b;
  902. struct perf_evsel *selection;
  903. };
  904. static void perf_evsel_menu__write(struct ui_browser *browser,
  905. void *entry, int row)
  906. {
  907. struct perf_evsel_menu *menu = container_of(browser,
  908. struct perf_evsel_menu, b);
  909. struct perf_evsel *evsel = list_entry(entry, struct perf_evsel, node);
  910. bool current_entry = ui_browser__is_current_entry(browser, row);
  911. unsigned long nr_events = evsel->hists.stats.nr_events[PERF_RECORD_SAMPLE];
  912. const char *ev_name = event_name(evsel);
  913. char bf[256], unit;
  914. ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
  915. HE_COLORSET_NORMAL);
  916. nr_events = convert_unit(nr_events, &unit);
  917. snprintf(bf, sizeof(bf), "%lu%c%s%s", nr_events,
  918. unit, unit == ' ' ? "" : " ", ev_name);
  919. slsmg_write_nstring(bf, browser->width);
  920. if (current_entry)
  921. menu->selection = evsel;
  922. }
  923. static int perf_evsel_menu__run(struct perf_evsel_menu *menu,
  924. int nr_events, const char *help,
  925. void(*timer)(void *arg), void *arg, int delay_secs)
  926. {
  927. struct perf_evlist *evlist = menu->b.priv;
  928. struct perf_evsel *pos;
  929. const char *ev_name, *title = "Available samples";
  930. int key;
  931. if (ui_browser__show(&menu->b, title,
  932. "ESC: exit, ENTER|->: Browse histograms") < 0)
  933. return -1;
  934. while (1) {
  935. key = ui_browser__run(&menu->b, delay_secs);
  936. switch (key) {
  937. case K_TIMER:
  938. timer(arg);
  939. continue;
  940. case K_RIGHT:
  941. case K_ENTER:
  942. if (!menu->selection)
  943. continue;
  944. pos = menu->selection;
  945. browse_hists:
  946. perf_evlist__set_selected(evlist, pos);
  947. /*
  948. * Give the calling tool a chance to populate the non
  949. * default evsel resorted hists tree.
  950. */
  951. if (timer)
  952. timer(arg);
  953. ev_name = event_name(pos);
  954. key = perf_evsel__hists_browse(pos, nr_events, help,
  955. ev_name, true, timer,
  956. arg, delay_secs);
  957. ui_browser__show_title(&menu->b, title);
  958. switch (key) {
  959. case K_TAB:
  960. if (pos->node.next == &evlist->entries)
  961. pos = list_entry(evlist->entries.next, struct perf_evsel, node);
  962. else
  963. pos = list_entry(pos->node.next, struct perf_evsel, node);
  964. goto browse_hists;
  965. case K_UNTAB:
  966. if (pos->node.prev == &evlist->entries)
  967. pos = list_entry(evlist->entries.prev, struct perf_evsel, node);
  968. else
  969. pos = list_entry(pos->node.prev, struct perf_evsel, node);
  970. goto browse_hists;
  971. case K_ESC:
  972. if (!ui__dialog_yesno("Do you really want to exit?"))
  973. continue;
  974. /* Fall thru */
  975. case 'q':
  976. case CTRL('c'):
  977. goto out;
  978. default:
  979. continue;
  980. }
  981. case K_LEFT:
  982. continue;
  983. case K_ESC:
  984. if (!ui__dialog_yesno("Do you really want to exit?"))
  985. continue;
  986. /* Fall thru */
  987. case 'q':
  988. case CTRL('c'):
  989. goto out;
  990. default:
  991. continue;
  992. }
  993. }
  994. out:
  995. ui_browser__hide(&menu->b);
  996. return key;
  997. }
  998. static int __perf_evlist__tui_browse_hists(struct perf_evlist *evlist,
  999. const char *help,
  1000. void(*timer)(void *arg), void *arg,
  1001. int delay_secs)
  1002. {
  1003. struct perf_evsel *pos;
  1004. struct perf_evsel_menu menu = {
  1005. .b = {
  1006. .entries = &evlist->entries,
  1007. .refresh = ui_browser__list_head_refresh,
  1008. .seek = ui_browser__list_head_seek,
  1009. .write = perf_evsel_menu__write,
  1010. .nr_entries = evlist->nr_entries,
  1011. .priv = evlist,
  1012. },
  1013. };
  1014. ui_helpline__push("Press ESC to exit");
  1015. list_for_each_entry(pos, &evlist->entries, node) {
  1016. const char *ev_name = event_name(pos);
  1017. size_t line_len = strlen(ev_name) + 7;
  1018. if (menu.b.width < line_len)
  1019. menu.b.width = line_len;
  1020. /*
  1021. * Cache the evsel name, tracepoints have a _high_ cost per
  1022. * event_name() call.
  1023. */
  1024. if (pos->name == NULL)
  1025. pos->name = strdup(ev_name);
  1026. }
  1027. return perf_evsel_menu__run(&menu, evlist->nr_entries, help, timer,
  1028. arg, delay_secs);
  1029. }
  1030. int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help,
  1031. void(*timer)(void *arg), void *arg,
  1032. int delay_secs)
  1033. {
  1034. if (evlist->nr_entries == 1) {
  1035. struct perf_evsel *first = list_entry(evlist->entries.next,
  1036. struct perf_evsel, node);
  1037. const char *ev_name = event_name(first);
  1038. return perf_evsel__hists_browse(first, evlist->nr_entries, help,
  1039. ev_name, false, timer, arg,
  1040. delay_secs);
  1041. }
  1042. return __perf_evlist__tui_browse_hists(evlist, help,
  1043. timer, arg, delay_secs);
  1044. }