hists.c 33 KB

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