2 #include "../libslang.h"
6 #include <linux/rbtree.h>
8 #include "../../util/evsel.h"
9 #include "../../util/evlist.h"
10 #include "../../util/hist.h"
11 #include "../../util/pstack.h"
12 #include "../../util/sort.h"
13 #include "../../util/util.h"
14 #include "../../arch/common.h"
16 #include "../browser.h"
17 #include "../helpline.h"
25 struct hist_entry *he_selection;
26 struct map_symbol *selection;
32 extern void hist_browser__init_hpp(void);
34 static int hists__browser_title(struct hists *hists, char *bf, size_t size,
37 static void hist_browser__refresh_dimensions(struct hist_browser *browser)
39 /* 3 == +/- toggle symbol before actual hist_entry rendering */
40 browser->b.width = 3 + (hists__sort_list_width(browser->hists) +
44 static void hist_browser__reset(struct hist_browser *browser)
46 browser->b.nr_entries = browser->hists->nr_entries;
47 hist_browser__refresh_dimensions(browser);
48 ui_browser__reset_index(&browser->b);
51 static char tree__folded_sign(bool unfolded)
53 return unfolded ? '-' : '+';
56 static char map_symbol__folded(const struct map_symbol *ms)
58 return ms->has_children ? tree__folded_sign(ms->unfolded) : ' ';
61 static char hist_entry__folded(const struct hist_entry *he)
63 return map_symbol__folded(&he->ms);
66 static char callchain_list__folded(const struct callchain_list *cl)
68 return map_symbol__folded(&cl->ms);
71 static void map_symbol__set_folding(struct map_symbol *ms, bool unfold)
73 ms->unfolded = unfold ? ms->has_children : false;
76 static int callchain_node__count_rows_rb_tree(struct callchain_node *node)
81 for (nd = rb_first(&node->rb_root); nd; nd = rb_next(nd)) {
82 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
83 struct callchain_list *chain;
84 char folded_sign = ' '; /* No children */
86 list_for_each_entry(chain, &child->val, list) {
88 /* We need this because we may not have children */
89 folded_sign = callchain_list__folded(chain);
90 if (folded_sign == '+')
94 if (folded_sign == '-') /* Have children and they're unfolded */
95 n += callchain_node__count_rows_rb_tree(child);
101 static int callchain_node__count_rows(struct callchain_node *node)
103 struct callchain_list *chain;
104 bool unfolded = false;
107 list_for_each_entry(chain, &node->val, list) {
109 unfolded = chain->ms.unfolded;
113 n += callchain_node__count_rows_rb_tree(node);
118 static int callchain__count_rows(struct rb_root *chain)
123 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
124 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
125 n += callchain_node__count_rows(node);
131 static bool map_symbol__toggle_fold(struct map_symbol *ms)
136 if (!ms->has_children)
139 ms->unfolded = !ms->unfolded;
143 static void callchain_node__init_have_children_rb_tree(struct callchain_node *node)
145 struct rb_node *nd = rb_first(&node->rb_root);
147 for (nd = rb_first(&node->rb_root); nd; nd = rb_next(nd)) {
148 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
149 struct callchain_list *chain;
152 list_for_each_entry(chain, &child->val, list) {
155 chain->ms.has_children = chain->list.next != &child->val ||
156 !RB_EMPTY_ROOT(&child->rb_root);
158 chain->ms.has_children = chain->list.next == &child->val &&
159 !RB_EMPTY_ROOT(&child->rb_root);
162 callchain_node__init_have_children_rb_tree(child);
166 static void callchain_node__init_have_children(struct callchain_node *node)
168 struct callchain_list *chain;
170 list_for_each_entry(chain, &node->val, list)
171 chain->ms.has_children = !RB_EMPTY_ROOT(&node->rb_root);
173 callchain_node__init_have_children_rb_tree(node);
176 static void callchain__init_have_children(struct rb_root *root)
180 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
181 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
182 callchain_node__init_have_children(node);
186 static void hist_entry__init_have_children(struct hist_entry *he)
188 if (!he->init_have_children) {
189 he->ms.has_children = !RB_EMPTY_ROOT(&he->sorted_chain);
190 callchain__init_have_children(&he->sorted_chain);
191 he->init_have_children = true;
195 static bool hist_browser__toggle_fold(struct hist_browser *browser)
197 if (map_symbol__toggle_fold(browser->selection)) {
198 struct hist_entry *he = browser->he_selection;
200 hist_entry__init_have_children(he);
201 browser->hists->nr_entries -= he->nr_rows;
204 he->nr_rows = callchain__count_rows(&he->sorted_chain);
207 browser->hists->nr_entries += he->nr_rows;
208 browser->b.nr_entries = browser->hists->nr_entries;
213 /* If it doesn't have children, no toggling performed */
217 static int callchain_node__set_folding_rb_tree(struct callchain_node *node, bool unfold)
222 for (nd = rb_first(&node->rb_root); nd; nd = rb_next(nd)) {
223 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
224 struct callchain_list *chain;
225 bool has_children = false;
227 list_for_each_entry(chain, &child->val, list) {
229 map_symbol__set_folding(&chain->ms, unfold);
230 has_children = chain->ms.has_children;
234 n += callchain_node__set_folding_rb_tree(child, unfold);
240 static int callchain_node__set_folding(struct callchain_node *node, bool unfold)
242 struct callchain_list *chain;
243 bool has_children = false;
246 list_for_each_entry(chain, &node->val, list) {
248 map_symbol__set_folding(&chain->ms, unfold);
249 has_children = chain->ms.has_children;
253 n += callchain_node__set_folding_rb_tree(node, unfold);
258 static int callchain__set_folding(struct rb_root *chain, bool unfold)
263 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
264 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
265 n += callchain_node__set_folding(node, unfold);
271 static void hist_entry__set_folding(struct hist_entry *he, bool unfold)
273 hist_entry__init_have_children(he);
274 map_symbol__set_folding(&he->ms, unfold);
276 if (he->ms.has_children) {
277 int n = callchain__set_folding(&he->sorted_chain, unfold);
278 he->nr_rows = unfold ? n : 0;
283 static void hists__set_folding(struct hists *hists, bool unfold)
287 hists->nr_entries = 0;
289 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
290 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
291 hist_entry__set_folding(he, unfold);
292 hists->nr_entries += 1 + he->nr_rows;
296 static void hist_browser__set_folding(struct hist_browser *browser, bool unfold)
298 hists__set_folding(browser->hists, unfold);
299 browser->b.nr_entries = browser->hists->nr_entries;
300 /* Go to the start, we may be way after valid entries after a collapse */
301 ui_browser__reset_index(&browser->b);
304 static void ui_browser__warn_lost_events(struct ui_browser *browser)
306 ui_browser__warning(browser, 4,
307 "Events are being lost, check IO/CPU overload!\n\n"
308 "You may want to run 'perf' using a RT scheduler policy:\n\n"
309 " perf top -r 80\n\n"
310 "Or reduce the sampling frequency.");
313 static int hist_browser__run(struct hist_browser *browser, const char *ev_name,
314 struct hist_browser_timer *hbt)
318 int delay_secs = hbt ? hbt->refresh : 0;
320 browser->b.entries = &browser->hists->entries;
321 browser->b.nr_entries = browser->hists->nr_entries;
323 hist_browser__refresh_dimensions(browser);
324 hists__browser_title(browser->hists, title, sizeof(title), ev_name);
326 if (ui_browser__show(&browser->b, title,
327 "Press '?' for help on key bindings") < 0)
331 key = ui_browser__run(&browser->b, delay_secs);
335 hbt->timer(hbt->arg);
336 ui_browser__update_nr_entries(&browser->b, browser->hists->nr_entries);
338 if (browser->hists->stats.nr_lost_warned !=
339 browser->hists->stats.nr_events[PERF_RECORD_LOST]) {
340 browser->hists->stats.nr_lost_warned =
341 browser->hists->stats.nr_events[PERF_RECORD_LOST];
342 ui_browser__warn_lost_events(&browser->b);
345 hists__browser_title(browser->hists, title, sizeof(title), ev_name);
346 ui_browser__show_title(&browser->b, title);
348 case 'D': { /* Debug */
350 struct hist_entry *h = rb_entry(browser->b.top,
351 struct hist_entry, rb_node);
353 ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
354 seq++, browser->b.nr_entries,
355 browser->hists->nr_entries,
359 h->row_offset, h->nr_rows);
363 /* Collapse the whole world. */
364 hist_browser__set_folding(browser, false);
367 /* Expand the whole world. */
368 hist_browser__set_folding(browser, true);
371 if (hist_browser__toggle_fold(browser))
379 ui_browser__hide(&browser->b);
383 static char *callchain_list__sym_name(struct callchain_list *cl,
384 char *bf, size_t bfsize, bool show_dso)
389 printed = scnprintf(bf, bfsize, "%s", cl->ms.sym->name);
391 printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);
394 scnprintf(bf + printed, bfsize - printed, " %s",
395 cl->ms.map ? cl->ms.map->dso->short_name : "unknown");
400 #define LEVEL_OFFSET_STEP 3
402 static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *browser,
403 struct callchain_node *chain_node,
404 u64 total, int level,
407 bool *is_current_entry)
409 struct rb_node *node;
410 int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
411 u64 new_total, remaining;
413 if (callchain_param.mode == CHAIN_GRAPH_REL)
414 new_total = chain_node->children_hit;
418 remaining = new_total;
419 node = rb_first(&chain_node->rb_root);
421 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
422 struct rb_node *next = rb_next(node);
423 u64 cumul = callchain_cumul_hits(child);
424 struct callchain_list *chain;
425 char folded_sign = ' ';
427 int extra_offset = 0;
431 list_for_each_entry(chain, &child->val, list) {
432 char bf[1024], *alloc_str;
435 bool was_first = first;
440 extra_offset = LEVEL_OFFSET_STEP;
442 folded_sign = callchain_list__folded(chain);
443 if (*row_offset != 0) {
449 str = callchain_list__sym_name(chain, bf, sizeof(bf),
452 double percent = cumul * 100.0 / new_total;
454 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
455 str = "Not enough memory!";
460 color = HE_COLORSET_NORMAL;
461 width = browser->b.width - (offset + extra_offset + 2);
462 if (ui_browser__is_current_entry(&browser->b, row)) {
463 browser->selection = &chain->ms;
464 color = HE_COLORSET_SELECTED;
465 *is_current_entry = true;
468 ui_browser__set_color(&browser->b, color);
469 ui_browser__gotorc(&browser->b, row, 0);
470 slsmg_write_nstring(" ", offset + extra_offset);
471 slsmg_printf("%c ", folded_sign);
472 slsmg_write_nstring(str, width);
475 if (++row == browser->b.height)
478 if (folded_sign == '+')
482 if (folded_sign == '-') {
483 const int new_level = level + (extra_offset ? 2 : 1);
484 row += hist_browser__show_callchain_node_rb_tree(browser, child, new_total,
485 new_level, row, row_offset,
488 if (row == browser->b.height)
493 return row - first_row;
496 static int hist_browser__show_callchain_node(struct hist_browser *browser,
497 struct callchain_node *node,
498 int level, unsigned short row,
500 bool *is_current_entry)
502 struct callchain_list *chain;
504 offset = level * LEVEL_OFFSET_STEP,
505 width = browser->b.width - offset;
506 char folded_sign = ' ';
508 list_for_each_entry(chain, &node->val, list) {
512 folded_sign = callchain_list__folded(chain);
514 if (*row_offset != 0) {
519 color = HE_COLORSET_NORMAL;
520 if (ui_browser__is_current_entry(&browser->b, row)) {
521 browser->selection = &chain->ms;
522 color = HE_COLORSET_SELECTED;
523 *is_current_entry = true;
526 s = callchain_list__sym_name(chain, bf, sizeof(bf),
528 ui_browser__gotorc(&browser->b, row, 0);
529 ui_browser__set_color(&browser->b, color);
530 slsmg_write_nstring(" ", offset);
531 slsmg_printf("%c ", folded_sign);
532 slsmg_write_nstring(s, width - 2);
534 if (++row == browser->b.height)
538 if (folded_sign == '-')
539 row += hist_browser__show_callchain_node_rb_tree(browser, node,
540 browser->hists->stats.total_period,
545 return row - first_row;
548 static int hist_browser__show_callchain(struct hist_browser *browser,
549 struct rb_root *chain,
550 int level, unsigned short row,
552 bool *is_current_entry)
557 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
558 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
560 row += hist_browser__show_callchain_node(browser, node, level,
563 if (row == browser->b.height)
567 return row - first_row;
570 #define HPP__COLOR_FN(_name, _field) \
571 static int hist_browser__hpp_color_ ## _name(struct perf_hpp *hpp, \
572 struct hist_entry *he) \
574 struct hists *hists = he->hists; \
575 double percent = 100.0 * he->stat._field / hists->stats.total_period; \
576 *(double *)hpp->ptr = percent; \
577 return scnprintf(hpp->buf, hpp->size, "%6.2f%%", percent); \
580 HPP__COLOR_FN(overhead, period)
581 HPP__COLOR_FN(overhead_sys, period_sys)
582 HPP__COLOR_FN(overhead_us, period_us)
583 HPP__COLOR_FN(overhead_guest_sys, period_guest_sys)
584 HPP__COLOR_FN(overhead_guest_us, period_guest_us)
588 void hist_browser__init_hpp(void)
592 perf_hpp__format[PERF_HPP__OVERHEAD].color =
593 hist_browser__hpp_color_overhead;
594 perf_hpp__format[PERF_HPP__OVERHEAD_SYS].color =
595 hist_browser__hpp_color_overhead_sys;
596 perf_hpp__format[PERF_HPP__OVERHEAD_US].color =
597 hist_browser__hpp_color_overhead_us;
598 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_SYS].color =
599 hist_browser__hpp_color_overhead_guest_sys;
600 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_US].color =
601 hist_browser__hpp_color_overhead_guest_us;
604 static int hist_browser__show_entry(struct hist_browser *browser,
605 struct hist_entry *entry,
611 int width = browser->b.width;
612 char folded_sign = ' ';
613 bool current_entry = ui_browser__is_current_entry(&browser->b, row);
614 off_t row_offset = entry->row_offset;
618 browser->he_selection = entry;
619 browser->selection = &entry->ms;
622 if (symbol_conf.use_callchain) {
623 hist_entry__init_have_children(entry);
624 folded_sign = hist_entry__folded(entry);
627 if (row_offset == 0) {
628 struct perf_hpp hpp = {
633 ui_browser__gotorc(&browser->b, row, 0);
635 for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
636 if (!perf_hpp__format[i].cond)
645 if (perf_hpp__format[i].color) {
647 /* It will set percent for us. See HPP__COLOR_FN above. */
648 width -= perf_hpp__format[i].color(&hpp, entry);
650 ui_browser__set_percent_color(&browser->b, percent, current_entry);
652 if (i == PERF_HPP__OVERHEAD && symbol_conf.use_callchain) {
653 slsmg_printf("%c ", folded_sign);
657 slsmg_printf("%s", s);
659 if (!current_entry || !browser->b.navkeypressed)
660 ui_browser__set_color(&browser->b, HE_COLORSET_NORMAL);
662 width -= perf_hpp__format[i].entry(&hpp, entry);
663 slsmg_printf("%s", s);
667 /* The scroll bar isn't being used */
668 if (!browser->b.navkeypressed)
671 hist_entry__sort_snprintf(entry, s, sizeof(s), browser->hists);
672 slsmg_write_nstring(s, width);
678 if (folded_sign == '-' && row != browser->b.height) {
679 printed += hist_browser__show_callchain(browser, &entry->sorted_chain,
683 browser->he_selection = entry;
689 static void ui_browser__hists_init_top(struct ui_browser *browser)
691 if (browser->top == NULL) {
692 struct hist_browser *hb;
694 hb = container_of(browser, struct hist_browser, b);
695 browser->top = rb_first(&hb->hists->entries);
699 static unsigned int hist_browser__refresh(struct ui_browser *browser)
703 struct hist_browser *hb = container_of(browser, struct hist_browser, b);
705 ui_browser__hists_init_top(browser);
707 for (nd = browser->top; nd; nd = rb_next(nd)) {
708 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
713 row += hist_browser__show_entry(hb, h, row);
714 if (row == browser->height)
721 static struct rb_node *hists__filter_entries(struct rb_node *nd)
724 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
734 static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
737 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
747 static void ui_browser__hists_seek(struct ui_browser *browser,
748 off_t offset, int whence)
750 struct hist_entry *h;
754 if (browser->nr_entries == 0)
757 ui_browser__hists_init_top(browser);
761 nd = hists__filter_entries(rb_first(browser->entries));
767 nd = hists__filter_prev_entries(rb_last(browser->entries));
775 * Moves not relative to the first visible entry invalidates its
778 h = rb_entry(browser->top, struct hist_entry, rb_node);
782 * Here we have to check if nd is expanded (+), if it is we can't go
783 * the next top level hist_entry, instead we must compute an offset of
784 * what _not_ to show and not change the first visible entry.
786 * This offset increments when we are going from top to bottom and
787 * decreases when we're going from bottom to top.
789 * As we don't have backpointers to the top level in the callchains
790 * structure, we need to always print the whole hist_entry callchain,
791 * skipping the first ones that are before the first visible entry
792 * and stop when we printed enough lines to fill the screen.
797 h = rb_entry(nd, struct hist_entry, rb_node);
798 if (h->ms.unfolded) {
799 u16 remaining = h->nr_rows - h->row_offset;
800 if (offset > remaining) {
804 h->row_offset += offset;
810 nd = hists__filter_entries(rb_next(nd));
815 } while (offset != 0);
816 } else if (offset < 0) {
818 h = rb_entry(nd, struct hist_entry, rb_node);
819 if (h->ms.unfolded) {
821 if (-offset > h->row_offset) {
822 offset += h->row_offset;
825 h->row_offset += offset;
831 if (-offset > h->nr_rows) {
832 offset += h->nr_rows;
835 h->row_offset = h->nr_rows + offset;
843 nd = hists__filter_prev_entries(rb_prev(nd));
850 * Last unfiltered hist_entry, check if it is
851 * unfolded, if it is then we should have
852 * row_offset at its last entry.
854 h = rb_entry(nd, struct hist_entry, rb_node);
856 h->row_offset = h->nr_rows;
863 h = rb_entry(nd, struct hist_entry, rb_node);
868 static int hist_browser__fprintf_callchain_node_rb_tree(struct hist_browser *browser,
869 struct callchain_node *chain_node,
870 u64 total, int level,
873 struct rb_node *node;
874 int offset = level * LEVEL_OFFSET_STEP;
875 u64 new_total, remaining;
878 if (callchain_param.mode == CHAIN_GRAPH_REL)
879 new_total = chain_node->children_hit;
883 remaining = new_total;
884 node = rb_first(&chain_node->rb_root);
886 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
887 struct rb_node *next = rb_next(node);
888 u64 cumul = callchain_cumul_hits(child);
889 struct callchain_list *chain;
890 char folded_sign = ' ';
892 int extra_offset = 0;
896 list_for_each_entry(chain, &child->val, list) {
897 char bf[1024], *alloc_str;
899 bool was_first = first;
904 extra_offset = LEVEL_OFFSET_STEP;
906 folded_sign = callchain_list__folded(chain);
909 str = callchain_list__sym_name(chain, bf, sizeof(bf),
912 double percent = cumul * 100.0 / new_total;
914 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
915 str = "Not enough memory!";
920 printed += fprintf(fp, "%*s%c %s\n", offset + extra_offset, " ", folded_sign, str);
922 if (folded_sign == '+')
926 if (folded_sign == '-') {
927 const int new_level = level + (extra_offset ? 2 : 1);
928 printed += hist_browser__fprintf_callchain_node_rb_tree(browser, child, new_total,
938 static int hist_browser__fprintf_callchain_node(struct hist_browser *browser,
939 struct callchain_node *node,
942 struct callchain_list *chain;
943 int offset = level * LEVEL_OFFSET_STEP;
944 char folded_sign = ' ';
947 list_for_each_entry(chain, &node->val, list) {
950 folded_sign = callchain_list__folded(chain);
951 s = callchain_list__sym_name(chain, bf, sizeof(bf), browser->show_dso);
952 printed += fprintf(fp, "%*s%c %s\n", offset, " ", folded_sign, s);
955 if (folded_sign == '-')
956 printed += hist_browser__fprintf_callchain_node_rb_tree(browser, node,
957 browser->hists->stats.total_period,
962 static int hist_browser__fprintf_callchain(struct hist_browser *browser,
963 struct rb_root *chain, int level, FILE *fp)
968 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
969 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
971 printed += hist_browser__fprintf_callchain_node(browser, node, level, fp);
977 static int hist_browser__fprintf_entry(struct hist_browser *browser,
978 struct hist_entry *he, FILE *fp)
983 char folded_sign = ' ';
985 if (symbol_conf.use_callchain)
986 folded_sign = hist_entry__folded(he);
988 hist_entry__sort_snprintf(he, s, sizeof(s), browser->hists);
989 percent = (he->stat.period * 100.0) / browser->hists->stats.total_period;
991 if (symbol_conf.use_callchain)
992 printed += fprintf(fp, "%c ", folded_sign);
994 printed += fprintf(fp, " %5.2f%%", percent);
996 if (symbol_conf.show_nr_samples)
997 printed += fprintf(fp, " %11u", he->stat.nr_events);
999 if (symbol_conf.show_total_period)
1000 printed += fprintf(fp, " %12" PRIu64, he->stat.period);
1002 printed += fprintf(fp, "%s\n", rtrim(s));
1004 if (folded_sign == '-')
1005 printed += hist_browser__fprintf_callchain(browser, &he->sorted_chain, 1, fp);
1010 static int hist_browser__fprintf(struct hist_browser *browser, FILE *fp)
1012 struct rb_node *nd = hists__filter_entries(rb_first(browser->b.entries));
1016 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1018 printed += hist_browser__fprintf_entry(browser, h, fp);
1019 nd = hists__filter_entries(rb_next(nd));
1025 static int hist_browser__dump(struct hist_browser *browser)
1031 scnprintf(filename, sizeof(filename), "perf.hist.%d", browser->print_seq);
1032 if (access(filename, F_OK))
1035 * XXX: Just an arbitrary lazy upper limit
1037 if (++browser->print_seq == 8192) {
1038 ui_helpline__fpush("Too many perf.hist.N files, nothing written!");
1043 fp = fopen(filename, "w");
1046 const char *err = strerror_r(errno, bf, sizeof(bf));
1047 ui_helpline__fpush("Couldn't write to %s: %s", filename, err);
1051 ++browser->print_seq;
1052 hist_browser__fprintf(browser, fp);
1054 ui_helpline__fpush("%s written!", filename);
1059 static struct hist_browser *hist_browser__new(struct hists *hists)
1061 struct hist_browser *browser = zalloc(sizeof(*browser));
1064 browser->hists = hists;
1065 browser->b.refresh = hist_browser__refresh;
1066 browser->b.seek = ui_browser__hists_seek;
1067 browser->b.use_navkeypressed = true;
1068 if (sort__branch_mode == 1)
1069 browser->has_symbols = sort_sym_from.list.next != NULL;
1071 browser->has_symbols = sort_sym.list.next != NULL;
1077 static void hist_browser__delete(struct hist_browser *browser)
1082 static struct hist_entry *hist_browser__selected_entry(struct hist_browser *browser)
1084 return browser->he_selection;
1087 static struct thread *hist_browser__selected_thread(struct hist_browser *browser)
1089 return browser->he_selection->thread;
1092 static int hists__browser_title(struct hists *hists, char *bf, size_t size,
1093 const char *ev_name)
1097 const struct dso *dso = hists->dso_filter;
1098 const struct thread *thread = hists->thread_filter;
1099 unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
1100 u64 nr_events = hists->stats.total_period;
1102 nr_samples = convert_unit(nr_samples, &unit);
1103 printed = scnprintf(bf, size,
1104 "Samples: %lu%c of event '%s', Event count (approx.): %lu",
1105 nr_samples, unit, ev_name, nr_events);
1108 if (hists->uid_filter_str)
1109 printed += snprintf(bf + printed, size - printed,
1110 ", UID: %s", hists->uid_filter_str);
1112 printed += scnprintf(bf + printed, size - printed,
1114 (thread->comm_set ? thread->comm : ""),
1117 printed += scnprintf(bf + printed, size - printed,
1118 ", DSO: %s", dso->short_name);
1122 static inline void free_popup_options(char **options, int n)
1126 for (i = 0; i < n; ++i) {
1132 /* Check whether the browser is for 'top' or 'report' */
1133 static inline bool is_report_browser(void *timer)
1135 return timer == NULL;
1138 static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
1139 const char *helpline, const char *ev_name,
1141 struct hist_browser_timer *hbt,
1142 struct perf_session_env *env)
1144 struct hists *hists = &evsel->hists;
1145 struct hist_browser *browser = hist_browser__new(hists);
1146 struct branch_info *bi;
1147 struct pstack *fstack;
1152 char script_opt[64];
1153 int delay_secs = hbt ? hbt->refresh : 0;
1155 if (browser == NULL)
1158 fstack = pstack__new(2);
1162 ui_helpline__push(helpline);
1164 memset(options, 0, sizeof(options));
1167 const struct thread *thread = NULL;
1168 const struct dso *dso = NULL;
1170 annotate = -2, zoom_dso = -2, zoom_thread = -2,
1171 annotate_f = -2, annotate_t = -2, browse_map = -2;
1172 int scripts_comm = -2, scripts_symbol = -2, scripts_all = -2;
1176 key = hist_browser__run(browser, ev_name, hbt);
1178 if (browser->he_selection != NULL) {
1179 thread = hist_browser__selected_thread(browser);
1180 dso = browser->selection->map ? browser->selection->map->dso : NULL;
1188 * Exit the browser, let hists__browser_tree
1189 * go to the next or previous
1191 goto out_free_stack;
1193 if (!browser->has_symbols) {
1194 ui_browser__warning(&browser->b, delay_secs * 2,
1195 "Annotation is only available for symbolic views, "
1196 "include \"sym*\" in --sort to use it.");
1200 if (browser->selection == NULL ||
1201 browser->selection->sym == NULL ||
1202 browser->selection->map->dso->annotate_warned)
1206 hist_browser__dump(browser);
1211 browser->show_dso = !browser->show_dso;
1216 if (ui_browser__input_window("Symbol to show",
1217 "Please enter the name of symbol you want to see",
1218 buf, "ENTER: OK, ESC: Cancel",
1219 delay_secs * 2) == K_ENTER) {
1220 hists->symbol_filter_str = *buf ? buf : NULL;
1221 hists__filter_by_symbol(hists);
1222 hist_browser__reset(browser);
1226 if (is_report_browser(hbt))
1232 ui_browser__help_window(&browser->b,
1233 "h/?/F1 Show this window\n"
1235 "PGDN/SPACE Navigate\n"
1236 "q/ESC/CTRL+C Exit browser\n\n"
1237 "For multiple event sessions:\n\n"
1238 "TAB/UNTAB Switch events\n\n"
1239 "For symbolic views (--sort has sym):\n\n"
1240 "-> Zoom into DSO/Threads & Annotate current symbol\n"
1242 "a Annotate current symbol\n"
1243 "C Collapse all callchains\n"
1244 "E Expand all callchains\n"
1245 "d Zoom into current DSO\n"
1246 "t Zoom into current Thread\n"
1247 "r Run available scripts('perf report' only)\n"
1248 "P Print histograms to perf.hist.N\n"
1249 "V Verbose (DSO names in callchains, etc)\n"
1250 "/ Filter symbol by name");
1259 if (pstack__empty(fstack)) {
1261 * Go back to the perf_evsel_menu__run or other user
1264 goto out_free_stack;
1267 top = pstack__pop(fstack);
1268 if (top == &browser->hists->dso_filter)
1270 if (top == &browser->hists->thread_filter)
1271 goto zoom_out_thread;
1276 !ui_browser__dialog_yesno(&browser->b,
1277 "Do you really want to exit?"))
1282 goto out_free_stack;
1287 if (!browser->has_symbols)
1288 goto add_exit_option;
1290 if (sort__branch_mode == 1) {
1291 bi = browser->he_selection->branch_info;
1292 if (browser->selection != NULL &&
1294 bi->from.sym != NULL &&
1295 !bi->from.map->dso->annotate_warned &&
1296 asprintf(&options[nr_options], "Annotate %s",
1297 bi->from.sym->name) > 0)
1298 annotate_f = nr_options++;
1300 if (browser->selection != NULL &&
1302 bi->to.sym != NULL &&
1303 !bi->to.map->dso->annotate_warned &&
1304 (bi->to.sym != bi->from.sym ||
1305 bi->to.map->dso != bi->from.map->dso) &&
1306 asprintf(&options[nr_options], "Annotate %s",
1307 bi->to.sym->name) > 0)
1308 annotate_t = nr_options++;
1311 if (browser->selection != NULL &&
1312 browser->selection->sym != NULL &&
1313 !browser->selection->map->dso->annotate_warned &&
1314 asprintf(&options[nr_options], "Annotate %s",
1315 browser->selection->sym->name) > 0)
1316 annotate = nr_options++;
1319 if (thread != NULL &&
1320 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
1321 (browser->hists->thread_filter ? "out of" : "into"),
1322 (thread->comm_set ? thread->comm : ""),
1324 zoom_thread = nr_options++;
1327 asprintf(&options[nr_options], "Zoom %s %s DSO",
1328 (browser->hists->dso_filter ? "out of" : "into"),
1329 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
1330 zoom_dso = nr_options++;
1332 if (browser->selection != NULL &&
1333 browser->selection->map != NULL &&
1334 asprintf(&options[nr_options], "Browse map details") > 0)
1335 browse_map = nr_options++;
1337 /* perf script support */
1338 if (browser->he_selection) {
1341 if (asprintf(&options[nr_options], "Run scripts for samples of thread [%s]",
1342 browser->he_selection->thread->comm) > 0)
1343 scripts_comm = nr_options++;
1345 sym = browser->he_selection->ms.sym;
1346 if (sym && sym->namelen &&
1347 asprintf(&options[nr_options], "Run scripts for samples of symbol [%s]",
1349 scripts_symbol = nr_options++;
1352 if (asprintf(&options[nr_options], "Run scripts for all samples") > 0)
1353 scripts_all = nr_options++;
1356 options[nr_options++] = (char *)"Exit";
1358 choice = ui__popup_menu(nr_options, options);
1360 if (choice == nr_options - 1)
1364 free_popup_options(options, nr_options - 1);
1368 if (choice == annotate || choice == annotate_t || choice == annotate_f) {
1369 struct hist_entry *he;
1372 if (!objdump_path && perf_session_env__lookup_objdump(env))
1375 he = hist_browser__selected_entry(browser);
1380 * we stash the branch_info symbol + map into the
1381 * the ms so we don't have to rewrite all the annotation
1382 * code to use branch_info.
1383 * in branch mode, the ms struct is not used
1385 if (choice == annotate_f) {
1386 he->ms.sym = he->branch_info->from.sym;
1387 he->ms.map = he->branch_info->from.map;
1388 } else if (choice == annotate_t) {
1389 he->ms.sym = he->branch_info->to.sym;
1390 he->ms.map = he->branch_info->to.map;
1394 * Don't let this be freed, say, by hists__decay_entry.
1397 err = hist_entry__tui_annotate(he, evsel->idx, hbt);
1400 * offer option to annotate the other branch source or target
1401 * (if they exists) when returning from annotate
1403 if ((err == 'q' || err == CTRL('c'))
1404 && annotate_t != -2 && annotate_f != -2)
1405 goto retry_popup_menu;
1407 ui_browser__update_nr_entries(&browser->b, browser->hists->nr_entries);
1409 ui_browser__handle_resize(&browser->b);
1411 } else if (choice == browse_map)
1412 map__browse(browser->selection->map);
1413 else if (choice == zoom_dso) {
1415 if (browser->hists->dso_filter) {
1416 pstack__remove(fstack, &browser->hists->dso_filter);
1419 browser->hists->dso_filter = NULL;
1420 sort_dso.elide = false;
1424 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
1425 dso->kernel ? "the Kernel" : dso->short_name);
1426 browser->hists->dso_filter = dso;
1427 sort_dso.elide = true;
1428 pstack__push(fstack, &browser->hists->dso_filter);
1430 hists__filter_by_dso(hists);
1431 hist_browser__reset(browser);
1432 } else if (choice == zoom_thread) {
1434 if (browser->hists->thread_filter) {
1435 pstack__remove(fstack, &browser->hists->thread_filter);
1438 browser->hists->thread_filter = NULL;
1439 sort_thread.elide = false;
1441 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
1442 thread->comm_set ? thread->comm : "",
1444 browser->hists->thread_filter = thread;
1445 sort_thread.elide = true;
1446 pstack__push(fstack, &browser->hists->thread_filter);
1448 hists__filter_by_thread(hists);
1449 hist_browser__reset(browser);
1451 /* perf scripts support */
1452 else if (choice == scripts_all || choice == scripts_comm ||
1453 choice == scripts_symbol) {
1455 memset(script_opt, 0, 64);
1457 if (choice == scripts_comm)
1458 sprintf(script_opt, " -c %s ", browser->he_selection->thread->comm);
1460 if (choice == scripts_symbol)
1461 sprintf(script_opt, " -S %s ", browser->he_selection->ms.sym->name);
1463 script_browse(script_opt);
1467 pstack__delete(fstack);
1469 hist_browser__delete(browser);
1470 free_popup_options(options, nr_options - 1);
1474 struct perf_evsel_menu {
1475 struct ui_browser b;
1476 struct perf_evsel *selection;
1477 bool lost_events, lost_events_warned;
1478 struct perf_session_env *env;
1481 static void perf_evsel_menu__write(struct ui_browser *browser,
1482 void *entry, int row)
1484 struct perf_evsel_menu *menu = container_of(browser,
1485 struct perf_evsel_menu, b);
1486 struct perf_evsel *evsel = list_entry(entry, struct perf_evsel, node);
1487 bool current_entry = ui_browser__is_current_entry(browser, row);
1488 unsigned long nr_events = evsel->hists.stats.nr_events[PERF_RECORD_SAMPLE];
1489 const char *ev_name = perf_evsel__name(evsel);
1491 const char *warn = " ";
1494 ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
1495 HE_COLORSET_NORMAL);
1497 nr_events = convert_unit(nr_events, &unit);
1498 printed = scnprintf(bf, sizeof(bf), "%lu%c%s%s", nr_events,
1499 unit, unit == ' ' ? "" : " ", ev_name);
1500 slsmg_printf("%s", bf);
1502 nr_events = evsel->hists.stats.nr_events[PERF_RECORD_LOST];
1503 if (nr_events != 0) {
1504 menu->lost_events = true;
1506 ui_browser__set_color(browser, HE_COLORSET_TOP);
1507 nr_events = convert_unit(nr_events, &unit);
1508 printed += scnprintf(bf, sizeof(bf), ": %ld%c%schunks LOST!",
1509 nr_events, unit, unit == ' ' ? "" : " ");
1513 slsmg_write_nstring(warn, browser->width - printed);
1516 menu->selection = evsel;
1519 static int perf_evsel_menu__run(struct perf_evsel_menu *menu,
1520 int nr_events, const char *help,
1521 struct hist_browser_timer *hbt)
1523 struct perf_evlist *evlist = menu->b.priv;
1524 struct perf_evsel *pos;
1525 const char *ev_name, *title = "Available samples";
1526 int delay_secs = hbt ? hbt->refresh : 0;
1529 if (ui_browser__show(&menu->b, title,
1530 "ESC: exit, ENTER|->: Browse histograms") < 0)
1534 key = ui_browser__run(&menu->b, delay_secs);
1538 hbt->timer(hbt->arg);
1540 if (!menu->lost_events_warned && menu->lost_events) {
1541 ui_browser__warn_lost_events(&menu->b);
1542 menu->lost_events_warned = true;
1547 if (!menu->selection)
1549 pos = menu->selection;
1551 perf_evlist__set_selected(evlist, pos);
1553 * Give the calling tool a chance to populate the non
1554 * default evsel resorted hists tree.
1557 hbt->timer(hbt->arg);
1558 ev_name = perf_evsel__name(pos);
1559 key = perf_evsel__hists_browse(pos, nr_events, help,
1562 ui_browser__show_title(&menu->b, title);
1565 if (pos->node.next == &evlist->entries)
1566 pos = list_entry(evlist->entries.next, struct perf_evsel, node);
1568 pos = list_entry(pos->node.next, struct perf_evsel, node);
1571 if (pos->node.prev == &evlist->entries)
1572 pos = list_entry(evlist->entries.prev, struct perf_evsel, node);
1574 pos = list_entry(pos->node.prev, struct perf_evsel, node);
1577 if (!ui_browser__dialog_yesno(&menu->b,
1578 "Do you really want to exit?"))
1590 if (!ui_browser__dialog_yesno(&menu->b,
1591 "Do you really want to exit?"))
1603 ui_browser__hide(&menu->b);
1607 static int __perf_evlist__tui_browse_hists(struct perf_evlist *evlist,
1609 struct hist_browser_timer *hbt,
1610 struct perf_session_env *env)
1612 struct perf_evsel *pos;
1613 struct perf_evsel_menu menu = {
1615 .entries = &evlist->entries,
1616 .refresh = ui_browser__list_head_refresh,
1617 .seek = ui_browser__list_head_seek,
1618 .write = perf_evsel_menu__write,
1619 .nr_entries = evlist->nr_entries,
1625 ui_helpline__push("Press ESC to exit");
1627 list_for_each_entry(pos, &evlist->entries, node) {
1628 const char *ev_name = perf_evsel__name(pos);
1629 size_t line_len = strlen(ev_name) + 7;
1631 if (menu.b.width < line_len)
1632 menu.b.width = line_len;
1635 return perf_evsel_menu__run(&menu, evlist->nr_entries, help, hbt);
1638 int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help,
1639 struct hist_browser_timer *hbt,
1640 struct perf_session_env *env)
1642 if (evlist->nr_entries == 1) {
1643 struct perf_evsel *first = list_entry(evlist->entries.next,
1644 struct perf_evsel, node);
1645 const char *ev_name = perf_evsel__name(first);
1646 return perf_evsel__hists_browse(first, evlist->nr_entries, help,
1647 ev_name, false, hbt, env);
1650 return __perf_evlist__tui_browse_hists(evlist, help, hbt, env);