hists.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  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 "../../util/evsel.h"
  8. #include "../../util/evlist.h"
  9. #include "../../util/hist.h"
  10. #include "../../util/pstack.h"
  11. #include "../../util/sort.h"
  12. #include "../../util/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 *hists, char *bf, size_t size,
  26. const char *ev_name);
  27. static void hist_browser__refresh_dimensions(struct hist_browser *browser)
  28. {
  29. /* 3 == +/- toggle symbol before actual hist_entry rendering */
  30. browser->b.width = 3 + (hists__sort_list_width(browser->hists) +
  31. sizeof("[k]"));
  32. }
  33. static void hist_browser__reset(struct hist_browser *browser)
  34. {
  35. browser->b.nr_entries = browser->hists->nr_entries;
  36. hist_browser__refresh_dimensions(browser);
  37. ui_browser__reset_index(&browser->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 *ms)
  44. {
  45. return ms->has_children ? tree__folded_sign(ms->unfolded) : ' ';
  46. }
  47. static char hist_entry__folded(const struct hist_entry *he)
  48. {
  49. return map_symbol__folded(&he->ms);
  50. }
  51. static char callchain_list__folded(const struct callchain_list *cl)
  52. {
  53. return map_symbol__folded(&cl->ms);
  54. }
  55. static void map_symbol__set_folding(struct map_symbol *ms, bool unfold)
  56. {
  57. ms->unfolded = unfold ? ms->has_children : false;
  58. }
  59. static int callchain_node__count_rows_rb_tree(struct callchain_node *node)
  60. {
  61. int n = 0;
  62. struct rb_node *nd;
  63. for (nd = rb_first(&node->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 *ms)
  103. {
  104. if (!ms)
  105. return false;
  106. if (!ms->has_children)
  107. return false;
  108. ms->unfolded = !ms->unfolded;
  109. return true;
  110. }
  111. static void callchain_node__init_have_children_rb_tree(struct callchain_node *node)
  112. {
  113. struct rb_node *nd = rb_first(&node->rb_root);
  114. for (nd = rb_first(&node->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 *node)
  131. {
  132. struct callchain_list *chain;
  133. list_for_each_entry(chain, &node->val, list)
  134. chain->ms.has_children = !RB_EMPTY_ROOT(&node->rb_root);
  135. callchain_node__init_have_children_rb_tree(node);
  136. }
  137. static void callchain__init_have_children(struct rb_root *root)
  138. {
  139. struct rb_node *nd;
  140. for (nd = rb_first(root); 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 *he)
  146. {
  147. if (!he->init_have_children) {
  148. he->ms.has_children = !RB_EMPTY_ROOT(&he->sorted_chain);
  149. callchain__init_have_children(&he->sorted_chain);
  150. he->init_have_children = true;
  151. }
  152. }
  153. static bool hist_browser__toggle_fold(struct hist_browser *browser)
  154. {
  155. if (map_symbol__toggle_fold(browser->selection)) {
  156. struct hist_entry *he = browser->he_selection;
  157. hist_entry__init_have_children(he);
  158. browser->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. browser->hists->nr_entries += he->nr_rows;
  164. browser->b.nr_entries = browser->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 *node, bool unfold)
  171. {
  172. int n = 0;
  173. struct rb_node *nd;
  174. for (nd = rb_first(&node->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 *he, bool unfold)
  213. {
  214. hist_entry__init_have_children(he);
  215. map_symbol__set_folding(&he->ms, unfold);
  216. if (he->ms.has_children) {
  217. int n = callchain__set_folding(&he->sorted_chain, unfold);
  218. he->nr_rows = unfold ? n : 0;
  219. } else
  220. he->nr_rows = 0;
  221. }
  222. static void hists__set_folding(struct hists *hists, bool unfold)
  223. {
  224. struct rb_node *nd;
  225. hists->nr_entries = 0;
  226. for (nd = rb_first(&hists->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. hists->nr_entries += 1 + he->nr_rows;
  230. }
  231. }
  232. static void hist_browser__set_folding(struct hist_browser *browser, bool unfold)
  233. {
  234. hists__set_folding(browser->hists, unfold);
  235. browser->b.nr_entries = browser->hists->nr_entries;
  236. /* Go to the start, we may be way after valid entries after a collapse */
  237. ui_browser__reset_index(&browser->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 *browser, const char *ev_name,
  248. void(*timer)(void *arg), void *arg, int delay_secs)
  249. {
  250. int key;
  251. char title[160];
  252. browser->b.entries = &browser->hists->entries;
  253. browser->b.nr_entries = browser->hists->nr_entries;
  254. hist_browser__refresh_dimensions(browser);
  255. hists__browser_title(browser->hists, title, sizeof(title), ev_name);
  256. if (ui_browser__show(&browser->b, title,
  257. "Press '?' for help on key bindings") < 0)
  258. return -1;
  259. while (1) {
  260. key = ui_browser__run(&browser->b, delay_secs);
  261. switch (key) {
  262. case K_TIMER:
  263. timer(arg);
  264. ui_browser__update_nr_entries(&browser->b, browser->hists->nr_entries);
  265. if (browser->hists->stats.nr_lost_warned !=
  266. browser->hists->stats.nr_events[PERF_RECORD_LOST]) {
  267. browser->hists->stats.nr_lost_warned =
  268. browser->hists->stats.nr_events[PERF_RECORD_LOST];
  269. ui_browser__warn_lost_events(&browser->b);
  270. }
  271. hists__browser_title(browser->hists, title, sizeof(title), ev_name);
  272. ui_browser__show_title(&browser->b, title);
  273. continue;
  274. case 'D': { /* Debug */
  275. static int seq;
  276. struct hist_entry *h = rb_entry(browser->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++, browser->b.nr_entries,
  281. browser->hists->nr_entries,
  282. browser->b.height,
  283. browser->b.index,
  284. browser->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(browser, false);
  291. break;
  292. case 'E':
  293. /* Expand the whole world. */
  294. hist_browser__set_folding(browser, true);
  295. break;
  296. case K_ENTER:
  297. if (hist_browser__toggle_fold(browser))
  298. break;
  299. /* fall thru */
  300. default:
  301. goto out;
  302. }
  303. }
  304. out:
  305. ui_browser__hide(&browser->b);
  306. return key;
  307. }
  308. static char *callchain_list__sym_name(struct callchain_list *cl,
  309. char *bf, size_t bfsize)
  310. {
  311. if (cl->ms.sym)
  312. return cl->ms.sym->name;
  313. snprintf(bf, bfsize, "%#" PRIx64, cl->ip);
  314. return bf;
  315. }
  316. #define LEVEL_OFFSET_STEP 3
  317. static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *browser,
  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 = browser->b.width - (offset + extra_offset + 2);
  367. if (ui_browser__is_current_entry(&browser->b, row)) {
  368. browser->selection = &chain->ms;
  369. color = HE_COLORSET_SELECTED;
  370. *is_current_entry = true;
  371. }
  372. ui_browser__set_color(&browser->b, color);
  373. ui_browser__gotorc(&browser->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 == browser->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(browser, child, new_total,
  387. new_level, row, row_offset,
  388. is_current_entry);
  389. }
  390. if (row == browser->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 *browser,
  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 = browser->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(&browser->b, row)) {
  418. browser->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(&browser->b, row, 0);
  424. ui_browser__set_color(&browser->b, color);
  425. slsmg_write_nstring(" ", offset);
  426. slsmg_printf("%c ", folded_sign);
  427. slsmg_write_nstring(s, width - 2);
  428. if (++row == browser->b.height)
  429. goto out;
  430. }
  431. if (folded_sign == '-')
  432. row += hist_browser__show_callchain_node_rb_tree(browser, node,
  433. browser->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 *browser,
  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(browser, node, level,
  451. row, row_offset,
  452. is_current_entry);
  453. if (row == browser->b.height)
  454. break;
  455. }
  456. return row - first_row;
  457. }
  458. static int hist_browser__show_entry(struct hist_browser *browser,
  459. struct hist_entry *entry,
  460. unsigned short row)
  461. {
  462. char s[256];
  463. double percent;
  464. int printed = 0;
  465. int width = browser->b.width - 6; /* The percentage */
  466. char folded_sign = ' ';
  467. bool current_entry = ui_browser__is_current_entry(&browser->b, row);
  468. off_t row_offset = entry->row_offset;
  469. if (current_entry) {
  470. browser->he_selection = entry;
  471. browser->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), browser->hists);
  479. percent = (entry->period * 100.0) / browser->hists->stats.total_period;
  480. ui_browser__set_percent_color(&browser->b, percent, current_entry);
  481. ui_browser__gotorc(&browser->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 (!browser->b.navkeypressed)
  489. width += 1;
  490. if (!current_entry || !browser->b.navkeypressed)
  491. ui_browser__set_color(&browser->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 != browser->b.height) {
  506. printed += hist_browser__show_callchain(browser, &entry->sorted_chain,
  507. 1, row, &row_offset,
  508. &current_entry);
  509. if (current_entry)
  510. browser->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 *browser)
  523. {
  524. unsigned row = 0;
  525. struct rb_node *nd;
  526. struct hist_browser *hb = container_of(browser, struct hist_browser, b);
  527. ui_browser__hists_init_top(browser);
  528. for (nd = browser->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 == browser->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 *browser,
  559. off_t offset, int whence)
  560. {
  561. struct hist_entry *h;
  562. struct rb_node *nd;
  563. bool first = true;
  564. if (browser->nr_entries == 0)
  565. return;
  566. ui_browser__hists_init_top(browser);
  567. switch (whence) {
  568. case SEEK_SET:
  569. nd = hists__filter_entries(rb_first(browser->entries));
  570. break;
  571. case SEEK_CUR:
  572. nd = browser->top;
  573. goto do_offset;
  574. case SEEK_END:
  575. nd = hists__filter_prev_entries(rb_last(browser->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(browser->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. browser->top = nd;
  613. break;
  614. }
  615. }
  616. nd = hists__filter_entries(rb_next(nd));
  617. if (nd == NULL)
  618. break;
  619. --offset;
  620. browser->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. browser->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. browser->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. browser->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. browser->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 *browser = zalloc(sizeof(*browser));
  675. if (browser) {
  676. browser->hists = hists;
  677. browser->b.refresh = hist_browser__refresh;
  678. browser->b.seek = ui_browser__hists_seek;
  679. browser->b.use_navkeypressed = true;
  680. if (sort__branch_mode == 1)
  681. browser->has_symbols = sort_sym_from.list.next != NULL;
  682. else
  683. browser->has_symbols = sort_sym.list.next != NULL;
  684. }
  685. return browser;
  686. }
  687. static void hist_browser__delete(struct hist_browser *browser)
  688. {
  689. free(browser);
  690. }
  691. static struct hist_entry *hist_browser__selected_entry(struct hist_browser *browser)
  692. {
  693. return browser->he_selection;
  694. }
  695. static struct thread *hist_browser__selected_thread(struct hist_browser *browser)
  696. {
  697. return browser->he_selection->thread;
  698. }
  699. static int hists__browser_title(struct hists *hists, char *bf, size_t size,
  700. const char *ev_name)
  701. {
  702. char unit;
  703. int printed;
  704. const struct dso *dso = hists->dso_filter;
  705. const struct thread *thread = hists->thread_filter;
  706. unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
  707. u64 nr_events = hists->stats.total_period;
  708. nr_samples = convert_unit(nr_samples, &unit);
  709. printed = scnprintf(bf, size,
  710. "Samples: %lu%c of event '%s', Event count (approx.): %lu",
  711. nr_samples, unit, ev_name, nr_events);
  712. if (hists->uid_filter_str)
  713. printed += snprintf(bf + printed, size - printed,
  714. ", UID: %s", hists->uid_filter_str);
  715. if (thread)
  716. printed += scnprintf(bf + printed, size - printed,
  717. ", Thread: %s(%d)",
  718. (thread->comm_set ? thread->comm : ""),
  719. thread->pid);
  720. if (dso)
  721. printed += scnprintf(bf + printed, size - printed,
  722. ", DSO: %s", dso->short_name);
  723. return printed;
  724. }
  725. static inline void free_popup_options(char **options, int n)
  726. {
  727. int i;
  728. for (i = 0; i < n; ++i) {
  729. free(options[i]);
  730. options[i] = NULL;
  731. }
  732. }
  733. static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
  734. const char *helpline, const char *ev_name,
  735. bool left_exits,
  736. void(*timer)(void *arg), void *arg,
  737. int delay_secs)
  738. {
  739. struct hists *hists = &evsel->hists;
  740. struct hist_browser *browser = hist_browser__new(hists);
  741. struct branch_info *bi;
  742. struct pstack *fstack;
  743. char *options[16];
  744. int nr_options = 0;
  745. int key = -1;
  746. char buf[64];
  747. if (browser == NULL)
  748. return -1;
  749. fstack = pstack__new(2);
  750. if (fstack == NULL)
  751. goto out;
  752. ui_helpline__push(helpline);
  753. memset(options, 0, sizeof(options));
  754. while (1) {
  755. const struct thread *thread = NULL;
  756. const struct dso *dso = NULL;
  757. int choice = 0,
  758. annotate = -2, zoom_dso = -2, zoom_thread = -2,
  759. annotate_f = -2, annotate_t = -2, browse_map = -2;
  760. nr_options = 0;
  761. key = hist_browser__run(browser, ev_name, timer, arg, delay_secs);
  762. if (browser->he_selection != NULL) {
  763. thread = hist_browser__selected_thread(browser);
  764. dso = browser->selection->map ? browser->selection->map->dso : NULL;
  765. }
  766. switch (key) {
  767. case K_TAB:
  768. case K_UNTAB:
  769. if (nr_events == 1)
  770. continue;
  771. /*
  772. * Exit the browser, let hists__browser_tree
  773. * go to the next or previous
  774. */
  775. goto out_free_stack;
  776. case 'a':
  777. if (!browser->has_symbols) {
  778. ui_browser__warning(&browser->b, delay_secs * 2,
  779. "Annotation is only available for symbolic views, "
  780. "include \"sym*\" in --sort to use it.");
  781. continue;
  782. }
  783. if (browser->selection == NULL ||
  784. browser->selection->sym == NULL ||
  785. browser->selection->map->dso->annotate_warned)
  786. continue;
  787. goto do_annotate;
  788. case 'd':
  789. goto zoom_dso;
  790. case 't':
  791. goto zoom_thread;
  792. case '/':
  793. if (ui_browser__input_window("Symbol to show",
  794. "Please enter the name of symbol you want to see",
  795. buf, "ENTER: OK, ESC: Cancel",
  796. delay_secs * 2) == K_ENTER) {
  797. hists->symbol_filter_str = *buf ? buf : NULL;
  798. hists__filter_by_symbol(hists);
  799. hist_browser__reset(browser);
  800. }
  801. continue;
  802. case K_F1:
  803. case 'h':
  804. case '?':
  805. ui_browser__help_window(&browser->b,
  806. "h/?/F1 Show this window\n"
  807. "UP/DOWN/PGUP\n"
  808. "PGDN/SPACE Navigate\n"
  809. "q/ESC/CTRL+C Exit browser\n\n"
  810. "For multiple event sessions:\n\n"
  811. "TAB/UNTAB Switch events\n\n"
  812. "For symbolic views (--sort has sym):\n\n"
  813. "-> Zoom into DSO/Threads & Annotate current symbol\n"
  814. "<- Zoom out\n"
  815. "a Annotate current symbol\n"
  816. "C Collapse all callchains\n"
  817. "E Expand all callchains\n"
  818. "d Zoom into current DSO\n"
  819. "t Zoom into current Thread\n"
  820. "/ Filter symbol by name");
  821. continue;
  822. case K_ENTER:
  823. case K_RIGHT:
  824. /* menu */
  825. break;
  826. case K_LEFT: {
  827. const void *top;
  828. if (pstack__empty(fstack)) {
  829. /*
  830. * Go back to the perf_evsel_menu__run or other user
  831. */
  832. if (left_exits)
  833. goto out_free_stack;
  834. continue;
  835. }
  836. top = pstack__pop(fstack);
  837. if (top == &browser->hists->dso_filter)
  838. goto zoom_out_dso;
  839. if (top == &browser->hists->thread_filter)
  840. goto zoom_out_thread;
  841. continue;
  842. }
  843. case K_ESC:
  844. if (!left_exits &&
  845. !ui_browser__dialog_yesno(&browser->b,
  846. "Do you really want to exit?"))
  847. continue;
  848. /* Fall thru */
  849. case 'q':
  850. case CTRL('c'):
  851. goto out_free_stack;
  852. default:
  853. continue;
  854. }
  855. if (!browser->has_symbols)
  856. goto add_exit_option;
  857. if (sort__branch_mode == 1) {
  858. bi = browser->he_selection->branch_info;
  859. if (browser->selection != NULL &&
  860. bi &&
  861. bi->from.sym != NULL &&
  862. !bi->from.map->dso->annotate_warned &&
  863. asprintf(&options[nr_options], "Annotate %s",
  864. bi->from.sym->name) > 0)
  865. annotate_f = nr_options++;
  866. if (browser->selection != NULL &&
  867. bi &&
  868. bi->to.sym != NULL &&
  869. !bi->to.map->dso->annotate_warned &&
  870. (bi->to.sym != bi->from.sym ||
  871. bi->to.map->dso != bi->from.map->dso) &&
  872. asprintf(&options[nr_options], "Annotate %s",
  873. bi->to.sym->name) > 0)
  874. annotate_t = nr_options++;
  875. } else {
  876. if (browser->selection != NULL &&
  877. browser->selection->sym != NULL &&
  878. !browser->selection->map->dso->annotate_warned &&
  879. asprintf(&options[nr_options], "Annotate %s",
  880. browser->selection->sym->name) > 0)
  881. annotate = nr_options++;
  882. }
  883. if (thread != NULL &&
  884. asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
  885. (browser->hists->thread_filter ? "out of" : "into"),
  886. (thread->comm_set ? thread->comm : ""),
  887. thread->pid) > 0)
  888. zoom_thread = nr_options++;
  889. if (dso != NULL &&
  890. asprintf(&options[nr_options], "Zoom %s %s DSO",
  891. (browser->hists->dso_filter ? "out of" : "into"),
  892. (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
  893. zoom_dso = nr_options++;
  894. if (browser->selection != NULL &&
  895. browser->selection->map != NULL &&
  896. asprintf(&options[nr_options], "Browse map details") > 0)
  897. browse_map = nr_options++;
  898. add_exit_option:
  899. options[nr_options++] = (char *)"Exit";
  900. retry_popup_menu:
  901. choice = ui__popup_menu(nr_options, options);
  902. if (choice == nr_options - 1)
  903. break;
  904. if (choice == -1) {
  905. free_popup_options(options, nr_options - 1);
  906. continue;
  907. }
  908. if (choice == annotate || choice == annotate_t || choice == annotate_f) {
  909. struct hist_entry *he;
  910. int err;
  911. do_annotate:
  912. he = hist_browser__selected_entry(browser);
  913. if (he == NULL)
  914. continue;
  915. /*
  916. * we stash the branch_info symbol + map into the
  917. * the ms so we don't have to rewrite all the annotation
  918. * code to use branch_info.
  919. * in branch mode, the ms struct is not used
  920. */
  921. if (choice == annotate_f) {
  922. he->ms.sym = he->branch_info->from.sym;
  923. he->ms.map = he->branch_info->from.map;
  924. } else if (choice == annotate_t) {
  925. he->ms.sym = he->branch_info->to.sym;
  926. he->ms.map = he->branch_info->to.map;
  927. }
  928. /*
  929. * Don't let this be freed, say, by hists__decay_entry.
  930. */
  931. he->used = true;
  932. err = hist_entry__tui_annotate(he, evsel->idx,
  933. timer, arg, delay_secs);
  934. he->used = false;
  935. /*
  936. * offer option to annotate the other branch source or target
  937. * (if they exists) when returning from annotate
  938. */
  939. if ((err == 'q' || err == CTRL('c'))
  940. && annotate_t != -2 && annotate_f != -2)
  941. goto retry_popup_menu;
  942. ui_browser__update_nr_entries(&browser->b, browser->hists->nr_entries);
  943. if (err)
  944. ui_browser__handle_resize(&browser->b);
  945. } else if (choice == browse_map)
  946. map__browse(browser->selection->map);
  947. else if (choice == zoom_dso) {
  948. zoom_dso:
  949. if (browser->hists->dso_filter) {
  950. pstack__remove(fstack, &browser->hists->dso_filter);
  951. zoom_out_dso:
  952. ui_helpline__pop();
  953. browser->hists->dso_filter = NULL;
  954. sort_dso.elide = false;
  955. } else {
  956. if (dso == NULL)
  957. continue;
  958. ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
  959. dso->kernel ? "the Kernel" : dso->short_name);
  960. browser->hists->dso_filter = dso;
  961. sort_dso.elide = true;
  962. pstack__push(fstack, &browser->hists->dso_filter);
  963. }
  964. hists__filter_by_dso(hists);
  965. hist_browser__reset(browser);
  966. } else if (choice == zoom_thread) {
  967. zoom_thread:
  968. if (browser->hists->thread_filter) {
  969. pstack__remove(fstack, &browser->hists->thread_filter);
  970. zoom_out_thread:
  971. ui_helpline__pop();
  972. browser->hists->thread_filter = NULL;
  973. sort_thread.elide = false;
  974. } else {
  975. ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
  976. thread->comm_set ? thread->comm : "",
  977. thread->pid);
  978. browser->hists->thread_filter = thread;
  979. sort_thread.elide = true;
  980. pstack__push(fstack, &browser->hists->thread_filter);
  981. }
  982. hists__filter_by_thread(hists);
  983. hist_browser__reset(browser);
  984. }
  985. }
  986. out_free_stack:
  987. pstack__delete(fstack);
  988. out:
  989. hist_browser__delete(browser);
  990. free_popup_options(options, nr_options - 1);
  991. return key;
  992. }
  993. struct perf_evsel_menu {
  994. struct ui_browser b;
  995. struct perf_evsel *selection;
  996. bool lost_events, lost_events_warned;
  997. };
  998. static void perf_evsel_menu__write(struct ui_browser *browser,
  999. void *entry, int row)
  1000. {
  1001. struct perf_evsel_menu *menu = container_of(browser,
  1002. struct perf_evsel_menu, b);
  1003. struct perf_evsel *evsel = list_entry(entry, struct perf_evsel, node);
  1004. bool current_entry = ui_browser__is_current_entry(browser, row);
  1005. unsigned long nr_events = evsel->hists.stats.nr_events[PERF_RECORD_SAMPLE];
  1006. const char *ev_name = event_name(evsel);
  1007. char bf[256], unit;
  1008. const char *warn = " ";
  1009. size_t printed;
  1010. ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
  1011. HE_COLORSET_NORMAL);
  1012. nr_events = convert_unit(nr_events, &unit);
  1013. printed = scnprintf(bf, sizeof(bf), "%lu%c%s%s", nr_events,
  1014. unit, unit == ' ' ? "" : " ", ev_name);
  1015. slsmg_printf("%s", bf);
  1016. nr_events = evsel->hists.stats.nr_events[PERF_RECORD_LOST];
  1017. if (nr_events != 0) {
  1018. menu->lost_events = true;
  1019. if (!current_entry)
  1020. ui_browser__set_color(browser, HE_COLORSET_TOP);
  1021. nr_events = convert_unit(nr_events, &unit);
  1022. printed += scnprintf(bf, sizeof(bf), ": %ld%c%schunks LOST!",
  1023. nr_events, unit, unit == ' ' ? "" : " ");
  1024. warn = bf;
  1025. }
  1026. slsmg_write_nstring(warn, browser->width - printed);
  1027. if (current_entry)
  1028. menu->selection = evsel;
  1029. }
  1030. static int perf_evsel_menu__run(struct perf_evsel_menu *menu,
  1031. int nr_events, const char *help,
  1032. void(*timer)(void *arg), void *arg, int delay_secs)
  1033. {
  1034. struct perf_evlist *evlist = menu->b.priv;
  1035. struct perf_evsel *pos;
  1036. const char *ev_name, *title = "Available samples";
  1037. int key;
  1038. if (ui_browser__show(&menu->b, title,
  1039. "ESC: exit, ENTER|->: Browse histograms") < 0)
  1040. return -1;
  1041. while (1) {
  1042. key = ui_browser__run(&menu->b, delay_secs);
  1043. switch (key) {
  1044. case K_TIMER:
  1045. timer(arg);
  1046. if (!menu->lost_events_warned && menu->lost_events) {
  1047. ui_browser__warn_lost_events(&menu->b);
  1048. menu->lost_events_warned = true;
  1049. }
  1050. continue;
  1051. case K_RIGHT:
  1052. case K_ENTER:
  1053. if (!menu->selection)
  1054. continue;
  1055. pos = menu->selection;
  1056. browse_hists:
  1057. perf_evlist__set_selected(evlist, pos);
  1058. /*
  1059. * Give the calling tool a chance to populate the non
  1060. * default evsel resorted hists tree.
  1061. */
  1062. if (timer)
  1063. timer(arg);
  1064. ev_name = event_name(pos);
  1065. key = perf_evsel__hists_browse(pos, nr_events, help,
  1066. ev_name, true, timer,
  1067. arg, delay_secs);
  1068. ui_browser__show_title(&menu->b, title);
  1069. switch (key) {
  1070. case K_TAB:
  1071. if (pos->node.next == &evlist->entries)
  1072. pos = list_entry(evlist->entries.next, struct perf_evsel, node);
  1073. else
  1074. pos = list_entry(pos->node.next, struct perf_evsel, node);
  1075. goto browse_hists;
  1076. case K_UNTAB:
  1077. if (pos->node.prev == &evlist->entries)
  1078. pos = list_entry(evlist->entries.prev, struct perf_evsel, node);
  1079. else
  1080. pos = list_entry(pos->node.prev, struct perf_evsel, node);
  1081. goto browse_hists;
  1082. case K_ESC:
  1083. if (!ui_browser__dialog_yesno(&menu->b,
  1084. "Do you really want to exit?"))
  1085. continue;
  1086. /* Fall thru */
  1087. case 'q':
  1088. case CTRL('c'):
  1089. goto out;
  1090. default:
  1091. continue;
  1092. }
  1093. case K_LEFT:
  1094. continue;
  1095. case K_ESC:
  1096. if (!ui_browser__dialog_yesno(&menu->b,
  1097. "Do you really want to exit?"))
  1098. continue;
  1099. /* Fall thru */
  1100. case 'q':
  1101. case CTRL('c'):
  1102. goto out;
  1103. default:
  1104. continue;
  1105. }
  1106. }
  1107. out:
  1108. ui_browser__hide(&menu->b);
  1109. return key;
  1110. }
  1111. static int __perf_evlist__tui_browse_hists(struct perf_evlist *evlist,
  1112. const char *help,
  1113. void(*timer)(void *arg), void *arg,
  1114. int delay_secs)
  1115. {
  1116. struct perf_evsel *pos;
  1117. struct perf_evsel_menu menu = {
  1118. .b = {
  1119. .entries = &evlist->entries,
  1120. .refresh = ui_browser__list_head_refresh,
  1121. .seek = ui_browser__list_head_seek,
  1122. .write = perf_evsel_menu__write,
  1123. .nr_entries = evlist->nr_entries,
  1124. .priv = evlist,
  1125. },
  1126. };
  1127. ui_helpline__push("Press ESC to exit");
  1128. list_for_each_entry(pos, &evlist->entries, node) {
  1129. const char *ev_name = event_name(pos);
  1130. size_t line_len = strlen(ev_name) + 7;
  1131. if (menu.b.width < line_len)
  1132. menu.b.width = line_len;
  1133. /*
  1134. * Cache the evsel name, tracepoints have a _high_ cost per
  1135. * event_name() call.
  1136. */
  1137. if (pos->name == NULL)
  1138. pos->name = strdup(ev_name);
  1139. }
  1140. return perf_evsel_menu__run(&menu, evlist->nr_entries, help, timer,
  1141. arg, delay_secs);
  1142. }
  1143. int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help,
  1144. void(*timer)(void *arg), void *arg,
  1145. int delay_secs)
  1146. {
  1147. if (evlist->nr_entries == 1) {
  1148. struct perf_evsel *first = list_entry(evlist->entries.next,
  1149. struct perf_evsel, node);
  1150. const char *ev_name = event_name(first);
  1151. return perf_evsel__hists_browse(first, evlist->nr_entries, help,
  1152. ev_name, false, timer, arg,
  1153. delay_secs);
  1154. }
  1155. return __perf_evlist__tui_browse_hists(evlist, help,
  1156. timer, arg, delay_secs);
  1157. }