hists.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  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. if (sort__branch_mode == 1)
  679. self->has_symbols = sort_sym_from.list.next != NULL;
  680. else
  681. self->has_symbols = sort_sym.list.next != NULL;
  682. }
  683. return self;
  684. }
  685. static void hist_browser__delete(struct hist_browser *self)
  686. {
  687. free(self);
  688. }
  689. static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
  690. {
  691. return self->he_selection;
  692. }
  693. static struct thread *hist_browser__selected_thread(struct hist_browser *self)
  694. {
  695. return self->he_selection->thread;
  696. }
  697. static int hists__browser_title(struct hists *self, char *bf, size_t size,
  698. const char *ev_name)
  699. {
  700. char unit;
  701. int printed;
  702. const struct dso *dso = self->dso_filter;
  703. const struct thread *thread = self->thread_filter;
  704. unsigned long nr_events = self->stats.nr_events[PERF_RECORD_SAMPLE];
  705. nr_events = convert_unit(nr_events, &unit);
  706. printed = scnprintf(bf, size, "Events: %lu%c %s", nr_events, unit, ev_name);
  707. if (self->uid_filter_str)
  708. printed += snprintf(bf + printed, size - printed,
  709. ", UID: %s", self->uid_filter_str);
  710. if (thread)
  711. printed += scnprintf(bf + printed, size - printed,
  712. ", Thread: %s(%d)",
  713. (thread->comm_set ? thread->comm : ""),
  714. thread->pid);
  715. if (dso)
  716. printed += scnprintf(bf + printed, size - printed,
  717. ", DSO: %s", dso->short_name);
  718. return printed;
  719. }
  720. static inline void free_popup_options(char **options, int n)
  721. {
  722. int i;
  723. for (i = 0; i < n; ++i) {
  724. free(options[i]);
  725. options[i] = NULL;
  726. }
  727. }
  728. static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
  729. const char *helpline, const char *ev_name,
  730. bool left_exits,
  731. void(*timer)(void *arg), void *arg,
  732. int delay_secs)
  733. {
  734. struct hists *self = &evsel->hists;
  735. struct hist_browser *browser = hist_browser__new(self);
  736. struct branch_info *bi;
  737. struct pstack *fstack;
  738. char *options[16];
  739. int nr_options = 0;
  740. int key = -1;
  741. if (browser == NULL)
  742. return -1;
  743. fstack = pstack__new(2);
  744. if (fstack == NULL)
  745. goto out;
  746. ui_helpline__push(helpline);
  747. memset(options, 0, sizeof(options));
  748. while (1) {
  749. const struct thread *thread = NULL;
  750. const struct dso *dso = NULL;
  751. int choice = 0,
  752. annotate = -2, zoom_dso = -2, zoom_thread = -2,
  753. annotate_f = -2, annotate_t = -2, browse_map = -2;
  754. nr_options = 0;
  755. key = hist_browser__run(browser, ev_name, timer, arg, delay_secs);
  756. if (browser->he_selection != NULL) {
  757. thread = hist_browser__selected_thread(browser);
  758. dso = browser->selection->map ? browser->selection->map->dso : NULL;
  759. }
  760. switch (key) {
  761. case K_TAB:
  762. case K_UNTAB:
  763. if (nr_events == 1)
  764. continue;
  765. /*
  766. * Exit the browser, let hists__browser_tree
  767. * go to the next or previous
  768. */
  769. goto out_free_stack;
  770. case 'a':
  771. if (!browser->has_symbols) {
  772. ui_browser__warning(&browser->b, delay_secs * 2,
  773. "Annotation is only available for symbolic views, "
  774. "include \"sym*\" in --sort to use it.");
  775. continue;
  776. }
  777. if (browser->selection == NULL ||
  778. browser->selection->sym == NULL ||
  779. browser->selection->map->dso->annotate_warned)
  780. continue;
  781. goto do_annotate;
  782. case 'd':
  783. goto zoom_dso;
  784. case 't':
  785. goto zoom_thread;
  786. case K_F1:
  787. case 'h':
  788. case '?':
  789. ui_browser__help_window(&browser->b,
  790. "h/?/F1 Show this window\n"
  791. "UP/DOWN/PGUP\n"
  792. "PGDN/SPACE Navigate\n"
  793. "q/ESC/CTRL+C Exit browser\n\n"
  794. "For multiple event sessions:\n\n"
  795. "TAB/UNTAB Switch events\n\n"
  796. "For symbolic views (--sort has sym):\n\n"
  797. "-> Zoom into DSO/Threads & Annotate current symbol\n"
  798. "<- Zoom out\n"
  799. "a Annotate current symbol\n"
  800. "C Collapse all callchains\n"
  801. "E Expand all callchains\n"
  802. "d Zoom into current DSO\n"
  803. "t Zoom into current Thread");
  804. continue;
  805. case K_ENTER:
  806. case K_RIGHT:
  807. /* menu */
  808. break;
  809. case K_LEFT: {
  810. const void *top;
  811. if (pstack__empty(fstack)) {
  812. /*
  813. * Go back to the perf_evsel_menu__run or other user
  814. */
  815. if (left_exits)
  816. goto out_free_stack;
  817. continue;
  818. }
  819. top = pstack__pop(fstack);
  820. if (top == &browser->hists->dso_filter)
  821. goto zoom_out_dso;
  822. if (top == &browser->hists->thread_filter)
  823. goto zoom_out_thread;
  824. continue;
  825. }
  826. case K_ESC:
  827. if (!left_exits &&
  828. !ui_browser__dialog_yesno(&browser->b,
  829. "Do you really want to exit?"))
  830. continue;
  831. /* Fall thru */
  832. case 'q':
  833. case CTRL('c'):
  834. goto out_free_stack;
  835. default:
  836. continue;
  837. }
  838. if (!browser->has_symbols)
  839. goto add_exit_option;
  840. if (sort__branch_mode == 1) {
  841. bi = browser->he_selection->branch_info;
  842. if (browser->selection != NULL &&
  843. bi &&
  844. bi->from.sym != NULL &&
  845. !bi->from.map->dso->annotate_warned &&
  846. asprintf(&options[nr_options], "Annotate %s",
  847. bi->from.sym->name) > 0)
  848. annotate_f = nr_options++;
  849. if (browser->selection != NULL &&
  850. bi &&
  851. bi->to.sym != NULL &&
  852. !bi->to.map->dso->annotate_warned &&
  853. (bi->to.sym != bi->from.sym ||
  854. bi->to.map->dso != bi->from.map->dso) &&
  855. asprintf(&options[nr_options], "Annotate %s",
  856. bi->to.sym->name) > 0)
  857. annotate_t = nr_options++;
  858. } else {
  859. if (browser->selection != NULL &&
  860. browser->selection->sym != NULL &&
  861. !browser->selection->map->dso->annotate_warned &&
  862. asprintf(&options[nr_options], "Annotate %s",
  863. browser->selection->sym->name) > 0)
  864. annotate = nr_options++;
  865. }
  866. if (thread != NULL &&
  867. asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
  868. (browser->hists->thread_filter ? "out of" : "into"),
  869. (thread->comm_set ? thread->comm : ""),
  870. thread->pid) > 0)
  871. zoom_thread = nr_options++;
  872. if (dso != NULL &&
  873. asprintf(&options[nr_options], "Zoom %s %s DSO",
  874. (browser->hists->dso_filter ? "out of" : "into"),
  875. (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
  876. zoom_dso = nr_options++;
  877. if (browser->selection != NULL &&
  878. browser->selection->map != NULL &&
  879. asprintf(&options[nr_options], "Browse map details") > 0)
  880. browse_map = nr_options++;
  881. add_exit_option:
  882. options[nr_options++] = (char *)"Exit";
  883. retry_popup_menu:
  884. choice = ui__popup_menu(nr_options, options);
  885. if (choice == nr_options - 1)
  886. break;
  887. if (choice == -1) {
  888. free_popup_options(options, nr_options - 1);
  889. continue;
  890. }
  891. if (choice == annotate || choice == annotate_t || choice == annotate_f) {
  892. struct hist_entry *he;
  893. int err;
  894. do_annotate:
  895. he = hist_browser__selected_entry(browser);
  896. if (he == NULL)
  897. continue;
  898. /*
  899. * we stash the branch_info symbol + map into the
  900. * the ms so we don't have to rewrite all the annotation
  901. * code to use branch_info.
  902. * in branch mode, the ms struct is not used
  903. */
  904. if (choice == annotate_f) {
  905. he->ms.sym = he->branch_info->from.sym;
  906. he->ms.map = he->branch_info->from.map;
  907. } else if (choice == annotate_t) {
  908. he->ms.sym = he->branch_info->to.sym;
  909. he->ms.map = he->branch_info->to.map;
  910. }
  911. /*
  912. * Don't let this be freed, say, by hists__decay_entry.
  913. */
  914. he->used = true;
  915. err = hist_entry__tui_annotate(he, evsel->idx,
  916. timer, arg, delay_secs);
  917. he->used = false;
  918. /*
  919. * offer option to annotate the other branch source or target
  920. * (if they exists) when returning from annotate
  921. */
  922. if ((err == 'q' || err == CTRL('c'))
  923. && annotate_t != -2 && annotate_f != -2)
  924. goto retry_popup_menu;
  925. ui_browser__update_nr_entries(&browser->b, browser->hists->nr_entries);
  926. if (err)
  927. ui_browser__handle_resize(&browser->b);
  928. } else if (choice == browse_map)
  929. map__browse(browser->selection->map);
  930. else if (choice == zoom_dso) {
  931. zoom_dso:
  932. if (browser->hists->dso_filter) {
  933. pstack__remove(fstack, &browser->hists->dso_filter);
  934. zoom_out_dso:
  935. ui_helpline__pop();
  936. browser->hists->dso_filter = NULL;
  937. sort_dso.elide = false;
  938. } else {
  939. if (dso == NULL)
  940. continue;
  941. ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
  942. dso->kernel ? "the Kernel" : dso->short_name);
  943. browser->hists->dso_filter = dso;
  944. sort_dso.elide = true;
  945. pstack__push(fstack, &browser->hists->dso_filter);
  946. }
  947. hists__filter_by_dso(self);
  948. hist_browser__reset(browser);
  949. } else if (choice == zoom_thread) {
  950. zoom_thread:
  951. if (browser->hists->thread_filter) {
  952. pstack__remove(fstack, &browser->hists->thread_filter);
  953. zoom_out_thread:
  954. ui_helpline__pop();
  955. browser->hists->thread_filter = NULL;
  956. sort_thread.elide = false;
  957. } else {
  958. ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
  959. thread->comm_set ? thread->comm : "",
  960. thread->pid);
  961. browser->hists->thread_filter = thread;
  962. sort_thread.elide = true;
  963. pstack__push(fstack, &browser->hists->thread_filter);
  964. }
  965. hists__filter_by_thread(self);
  966. hist_browser__reset(browser);
  967. }
  968. }
  969. out_free_stack:
  970. pstack__delete(fstack);
  971. out:
  972. hist_browser__delete(browser);
  973. free_popup_options(options, nr_options - 1);
  974. return key;
  975. }
  976. struct perf_evsel_menu {
  977. struct ui_browser b;
  978. struct perf_evsel *selection;
  979. bool lost_events, lost_events_warned;
  980. };
  981. static void perf_evsel_menu__write(struct ui_browser *browser,
  982. void *entry, int row)
  983. {
  984. struct perf_evsel_menu *menu = container_of(browser,
  985. struct perf_evsel_menu, b);
  986. struct perf_evsel *evsel = list_entry(entry, struct perf_evsel, node);
  987. bool current_entry = ui_browser__is_current_entry(browser, row);
  988. unsigned long nr_events = evsel->hists.stats.nr_events[PERF_RECORD_SAMPLE];
  989. const char *ev_name = event_name(evsel);
  990. char bf[256], unit;
  991. const char *warn = " ";
  992. size_t printed;
  993. ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
  994. HE_COLORSET_NORMAL);
  995. nr_events = convert_unit(nr_events, &unit);
  996. printed = scnprintf(bf, sizeof(bf), "%lu%c%s%s", nr_events,
  997. unit, unit == ' ' ? "" : " ", ev_name);
  998. slsmg_printf("%s", bf);
  999. nr_events = evsel->hists.stats.nr_events[PERF_RECORD_LOST];
  1000. if (nr_events != 0) {
  1001. menu->lost_events = true;
  1002. if (!current_entry)
  1003. ui_browser__set_color(browser, HE_COLORSET_TOP);
  1004. nr_events = convert_unit(nr_events, &unit);
  1005. printed += scnprintf(bf, sizeof(bf), ": %ld%c%schunks LOST!",
  1006. nr_events, unit, unit == ' ' ? "" : " ");
  1007. warn = bf;
  1008. }
  1009. slsmg_write_nstring(warn, browser->width - printed);
  1010. if (current_entry)
  1011. menu->selection = evsel;
  1012. }
  1013. static int perf_evsel_menu__run(struct perf_evsel_menu *menu,
  1014. int nr_events, const char *help,
  1015. void(*timer)(void *arg), void *arg, int delay_secs)
  1016. {
  1017. struct perf_evlist *evlist = menu->b.priv;
  1018. struct perf_evsel *pos;
  1019. const char *ev_name, *title = "Available samples";
  1020. int key;
  1021. if (ui_browser__show(&menu->b, title,
  1022. "ESC: exit, ENTER|->: Browse histograms") < 0)
  1023. return -1;
  1024. while (1) {
  1025. key = ui_browser__run(&menu->b, delay_secs);
  1026. switch (key) {
  1027. case K_TIMER:
  1028. timer(arg);
  1029. if (!menu->lost_events_warned && menu->lost_events) {
  1030. ui_browser__warn_lost_events(&menu->b);
  1031. menu->lost_events_warned = true;
  1032. }
  1033. continue;
  1034. case K_RIGHT:
  1035. case K_ENTER:
  1036. if (!menu->selection)
  1037. continue;
  1038. pos = menu->selection;
  1039. browse_hists:
  1040. perf_evlist__set_selected(evlist, pos);
  1041. /*
  1042. * Give the calling tool a chance to populate the non
  1043. * default evsel resorted hists tree.
  1044. */
  1045. if (timer)
  1046. timer(arg);
  1047. ev_name = event_name(pos);
  1048. key = perf_evsel__hists_browse(pos, nr_events, help,
  1049. ev_name, true, timer,
  1050. arg, delay_secs);
  1051. ui_browser__show_title(&menu->b, title);
  1052. switch (key) {
  1053. case K_TAB:
  1054. if (pos->node.next == &evlist->entries)
  1055. pos = list_entry(evlist->entries.next, struct perf_evsel, node);
  1056. else
  1057. pos = list_entry(pos->node.next, struct perf_evsel, node);
  1058. goto browse_hists;
  1059. case K_UNTAB:
  1060. if (pos->node.prev == &evlist->entries)
  1061. pos = list_entry(evlist->entries.prev, struct perf_evsel, node);
  1062. else
  1063. pos = list_entry(pos->node.prev, struct perf_evsel, node);
  1064. goto browse_hists;
  1065. case K_ESC:
  1066. if (!ui_browser__dialog_yesno(&menu->b,
  1067. "Do you really want to exit?"))
  1068. continue;
  1069. /* Fall thru */
  1070. case 'q':
  1071. case CTRL('c'):
  1072. goto out;
  1073. default:
  1074. continue;
  1075. }
  1076. case K_LEFT:
  1077. continue;
  1078. case K_ESC:
  1079. if (!ui_browser__dialog_yesno(&menu->b,
  1080. "Do you really want to exit?"))
  1081. continue;
  1082. /* Fall thru */
  1083. case 'q':
  1084. case CTRL('c'):
  1085. goto out;
  1086. default:
  1087. continue;
  1088. }
  1089. }
  1090. out:
  1091. ui_browser__hide(&menu->b);
  1092. return key;
  1093. }
  1094. static int __perf_evlist__tui_browse_hists(struct perf_evlist *evlist,
  1095. const char *help,
  1096. void(*timer)(void *arg), void *arg,
  1097. int delay_secs)
  1098. {
  1099. struct perf_evsel *pos;
  1100. struct perf_evsel_menu menu = {
  1101. .b = {
  1102. .entries = &evlist->entries,
  1103. .refresh = ui_browser__list_head_refresh,
  1104. .seek = ui_browser__list_head_seek,
  1105. .write = perf_evsel_menu__write,
  1106. .nr_entries = evlist->nr_entries,
  1107. .priv = evlist,
  1108. },
  1109. };
  1110. ui_helpline__push("Press ESC to exit");
  1111. list_for_each_entry(pos, &evlist->entries, node) {
  1112. const char *ev_name = event_name(pos);
  1113. size_t line_len = strlen(ev_name) + 7;
  1114. if (menu.b.width < line_len)
  1115. menu.b.width = line_len;
  1116. /*
  1117. * Cache the evsel name, tracepoints have a _high_ cost per
  1118. * event_name() call.
  1119. */
  1120. if (pos->name == NULL)
  1121. pos->name = strdup(ev_name);
  1122. }
  1123. return perf_evsel_menu__run(&menu, evlist->nr_entries, help, timer,
  1124. arg, delay_secs);
  1125. }
  1126. int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help,
  1127. void(*timer)(void *arg), void *arg,
  1128. int delay_secs)
  1129. {
  1130. if (evlist->nr_entries == 1) {
  1131. struct perf_evsel *first = list_entry(evlist->entries.next,
  1132. struct perf_evsel, node);
  1133. const char *ev_name = event_name(first);
  1134. return perf_evsel__hists_browse(first, evlist->nr_entries, help,
  1135. ev_name, false, timer, arg,
  1136. delay_secs);
  1137. }
  1138. return __perf_evlist__tui_browse_hists(evlist, help,
  1139. timer, arg, delay_secs);
  1140. }