hists.c 31 KB

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