hists.c 31 KB

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