1 // SPDX-License-Identifier: GPL-2.0
8 #include <linux/rbtree.h>
9 #include <linux/string.h>
10 #include <sys/ttydefaults.h>
11 #include <linux/time64.h>
12 #include <linux/zalloc.h>
14 #include "../../util/debug.h"
15 #include "../../util/dso.h"
16 #include "../../util/callchain.h"
17 #include "../../util/evsel.h"
18 #include "../../util/evlist.h"
19 #include "../../util/header.h"
20 #include "../../util/hist.h"
21 #include "../../util/machine.h"
22 #include "../../util/map.h"
23 #include "../../util/maps.h"
24 #include "../../util/symbol.h"
25 #include "../../util/map_symbol.h"
26 #include "../../util/branch.h"
27 #include "../../util/pstack.h"
28 #include "../../util/sort.h"
29 #include "../../util/top.h"
30 #include "../../util/thread.h"
31 #include "../../util/block-info.h"
32 #include "../../util/util.h"
33 #include "../../arch/common.h"
35 #include "../browsers/hists.h"
36 #include "../helpline.h"
44 #include "time-utils.h"
46 #include <linux/ctype.h>
48 extern void hist_browser__init_hpp(void);
50 static int hists_browser__scnprintf_title(struct hist_browser *browser, char *bf, size_t size);
51 static void hist_browser__update_nr_entries(struct hist_browser *hb);
53 static struct rb_node *hists__filter_entries(struct rb_node *nd,
56 static bool hist_browser__has_filter(struct hist_browser *hb)
58 return hists__has_filter(hb->hists) || hb->min_pcnt || symbol_conf.has_filter || hb->c2c_filter;
61 static int hist_browser__get_folding(struct hist_browser *browser)
64 struct hists *hists = browser->hists;
65 int unfolded_rows = 0;
67 for (nd = rb_first_cached(&hists->entries);
68 (nd = hists__filter_entries(nd, browser->min_pcnt)) != NULL;
69 nd = rb_hierarchy_next(nd)) {
70 struct hist_entry *he =
71 rb_entry(nd, struct hist_entry, rb_node);
73 if (he->leaf && he->unfolded)
74 unfolded_rows += he->nr_rows;
79 static void hist_browser__set_title_space(struct hist_browser *hb)
81 struct ui_browser *browser = &hb->b;
82 struct hists *hists = hb->hists;
83 struct perf_hpp_list *hpp_list = hists->hpp_list;
85 browser->extra_title_lines = hb->show_headers ? hpp_list->nr_header_lines : 0;
88 static u32 hist_browser__nr_entries(struct hist_browser *hb)
92 if (symbol_conf.report_hierarchy)
93 nr_entries = hb->nr_hierarchy_entries;
94 else if (hist_browser__has_filter(hb))
95 nr_entries = hb->nr_non_filtered_entries;
97 nr_entries = hb->hists->nr_entries;
99 hb->nr_callchain_rows = hist_browser__get_folding(hb);
100 return nr_entries + hb->nr_callchain_rows;
103 static void hist_browser__update_rows(struct hist_browser *hb)
105 struct ui_browser *browser = &hb->b;
106 struct hists *hists = hb->hists;
107 struct perf_hpp_list *hpp_list = hists->hpp_list;
110 if (!hb->show_headers) {
111 browser->rows += browser->extra_title_lines;
112 browser->extra_title_lines = 0;
116 browser->extra_title_lines = hpp_list->nr_header_lines;
117 browser->rows -= browser->extra_title_lines;
119 * Verify if we were at the last line and that line isn't
120 * visible because we now show the header line(s).
122 index_row = browser->index - browser->top_idx;
123 if (index_row >= browser->rows)
124 browser->index -= index_row - browser->rows + 1;
127 static void hist_browser__refresh_dimensions(struct ui_browser *browser)
129 struct hist_browser *hb = container_of(browser, struct hist_browser, b);
131 /* 3 == +/- toggle symbol before actual hist_entry rendering */
132 browser->width = 3 + (hists__sort_list_width(hb->hists) + sizeof("[k]"));
134 * FIXME: Just keeping existing behaviour, but this really should be
135 * before updating browser->width, as it will invalidate the
136 * calculation above. Fix this and the fallout in another
139 ui_browser__refresh_dimensions(browser);
142 static void hist_browser__reset(struct hist_browser *browser)
145 * The hists__remove_entry_filter() already folds non-filtered
146 * entries so we can assume it has 0 callchain rows.
148 browser->nr_callchain_rows = 0;
150 hist_browser__update_nr_entries(browser);
151 browser->b.nr_entries = hist_browser__nr_entries(browser);
152 hist_browser__refresh_dimensions(&browser->b);
153 ui_browser__reset_index(&browser->b);
156 static char tree__folded_sign(bool unfolded)
158 return unfolded ? '-' : '+';
161 static char hist_entry__folded(const struct hist_entry *he)
163 return he->has_children ? tree__folded_sign(he->unfolded) : ' ';
166 static char callchain_list__folded(const struct callchain_list *cl)
168 return cl->has_children ? tree__folded_sign(cl->unfolded) : ' ';
171 static void callchain_list__set_folding(struct callchain_list *cl, bool unfold)
173 cl->unfolded = unfold ? cl->has_children : false;
176 static int callchain_node__count_rows_rb_tree(struct callchain_node *node)
181 for (nd = rb_first(&node->rb_root); nd; nd = rb_next(nd)) {
182 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
183 struct callchain_list *chain;
184 char folded_sign = ' '; /* No children */
186 list_for_each_entry(chain, &child->val, list) {
189 /* We need this because we may not have children */
190 folded_sign = callchain_list__folded(chain);
191 if (folded_sign == '+')
195 if (folded_sign == '-') /* Have children and they're unfolded */
196 n += callchain_node__count_rows_rb_tree(child);
202 static int callchain_node__count_flat_rows(struct callchain_node *node)
204 struct callchain_list *chain;
205 char folded_sign = 0;
208 list_for_each_entry(chain, &node->parent_val, list) {
210 /* only check first chain list entry */
211 folded_sign = callchain_list__folded(chain);
212 if (folded_sign == '+')
218 list_for_each_entry(chain, &node->val, list) {
220 /* node->parent_val list might be empty */
221 folded_sign = callchain_list__folded(chain);
222 if (folded_sign == '+')
231 static int callchain_node__count_folded_rows(struct callchain_node *node __maybe_unused)
236 static int callchain_node__count_rows(struct callchain_node *node)
238 struct callchain_list *chain;
239 bool unfolded = false;
242 if (callchain_param.mode == CHAIN_FLAT)
243 return callchain_node__count_flat_rows(node);
244 else if (callchain_param.mode == CHAIN_FOLDED)
245 return callchain_node__count_folded_rows(node);
247 list_for_each_entry(chain, &node->val, list) {
250 unfolded = chain->unfolded;
254 n += callchain_node__count_rows_rb_tree(node);
259 static int callchain__count_rows(struct rb_root *chain)
264 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
265 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
266 n += callchain_node__count_rows(node);
272 static int hierarchy_count_rows(struct hist_browser *hb, struct hist_entry *he,
273 bool include_children)
276 struct rb_node *node;
277 struct hist_entry *child;
280 return callchain__count_rows(&he->sorted_chain);
282 if (he->has_no_entry)
285 node = rb_first_cached(&he->hroot_out);
289 child = rb_entry(node, struct hist_entry, rb_node);
290 percent = hist_entry__get_percent_limit(child);
292 if (!child->filtered && percent >= hb->min_pcnt) {
295 if (include_children && child->unfolded)
296 count += hierarchy_count_rows(hb, child, true);
299 node = rb_next(node);
304 static bool hist_entry__toggle_fold(struct hist_entry *he)
309 if (!he->has_children)
312 he->unfolded = !he->unfolded;
316 static bool callchain_list__toggle_fold(struct callchain_list *cl)
321 if (!cl->has_children)
324 cl->unfolded = !cl->unfolded;
328 static void callchain_node__init_have_children_rb_tree(struct callchain_node *node)
330 struct rb_node *nd = rb_first(&node->rb_root);
332 for (nd = rb_first(&node->rb_root); nd; nd = rb_next(nd)) {
333 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
334 struct callchain_list *chain;
337 list_for_each_entry(chain, &child->val, list) {
340 chain->has_children = chain->list.next != &child->val ||
341 !RB_EMPTY_ROOT(&child->rb_root);
343 chain->has_children = chain->list.next == &child->val &&
344 !RB_EMPTY_ROOT(&child->rb_root);
347 callchain_node__init_have_children_rb_tree(child);
351 static void callchain_node__init_have_children(struct callchain_node *node,
354 struct callchain_list *chain;
356 chain = list_entry(node->val.next, struct callchain_list, list);
357 chain->has_children = has_sibling;
359 if (!list_empty(&node->val)) {
360 chain = list_entry(node->val.prev, struct callchain_list, list);
361 chain->has_children = !RB_EMPTY_ROOT(&node->rb_root);
364 callchain_node__init_have_children_rb_tree(node);
367 static void callchain__init_have_children(struct rb_root *root)
369 struct rb_node *nd = rb_first(root);
370 bool has_sibling = nd && rb_next(nd);
372 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
373 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
374 callchain_node__init_have_children(node, has_sibling);
375 if (callchain_param.mode == CHAIN_FLAT ||
376 callchain_param.mode == CHAIN_FOLDED)
377 callchain_node__make_parent_list(node);
381 static void hist_entry__init_have_children(struct hist_entry *he)
383 if (he->init_have_children)
387 he->has_children = !RB_EMPTY_ROOT(&he->sorted_chain);
388 callchain__init_have_children(&he->sorted_chain);
390 he->has_children = !RB_EMPTY_ROOT(&he->hroot_out.rb_root);
393 he->init_have_children = true;
396 static bool hist_browser__selection_has_children(struct hist_browser *browser)
398 struct hist_entry *he = browser->he_selection;
399 struct map_symbol *ms = browser->selection;
405 return he->has_children;
407 return container_of(ms, struct callchain_list, ms)->has_children;
410 static bool hist_browser__selection_unfolded(struct hist_browser *browser)
412 struct hist_entry *he = browser->he_selection;
413 struct map_symbol *ms = browser->selection;
421 return container_of(ms, struct callchain_list, ms)->unfolded;
424 static char *hist_browser__selection_sym_name(struct hist_browser *browser, char *bf, size_t size)
426 struct hist_entry *he = browser->he_selection;
427 struct map_symbol *ms = browser->selection;
428 struct callchain_list *callchain_entry;
434 hist_entry__sym_snprintf(he, bf, size, 0);
435 return bf + 4; // skip the level, e.g. '[k] '
438 callchain_entry = container_of(ms, struct callchain_list, ms);
439 return callchain_list__sym_name(callchain_entry, bf, size, browser->show_dso);
442 static bool hist_browser__toggle_fold(struct hist_browser *browser)
444 struct hist_entry *he = browser->he_selection;
445 struct map_symbol *ms = browser->selection;
446 struct callchain_list *cl = container_of(ms, struct callchain_list, ms);
453 has_children = hist_entry__toggle_fold(he);
455 has_children = callchain_list__toggle_fold(cl);
460 hist_entry__init_have_children(he);
461 browser->b.nr_entries -= he->nr_rows;
464 browser->nr_callchain_rows -= he->nr_rows;
466 browser->nr_hierarchy_entries -= he->nr_rows;
468 if (symbol_conf.report_hierarchy)
469 child_rows = hierarchy_count_rows(browser, he, true);
473 he->nr_rows = callchain__count_rows(
476 he->nr_rows = hierarchy_count_rows(browser, he, false);
478 /* account grand children */
479 if (symbol_conf.report_hierarchy)
480 browser->b.nr_entries += child_rows - he->nr_rows;
482 if (!he->leaf && he->nr_rows == 0) {
483 he->has_no_entry = true;
487 if (symbol_conf.report_hierarchy)
488 browser->b.nr_entries -= child_rows - he->nr_rows;
490 if (he->has_no_entry)
491 he->has_no_entry = false;
496 browser->b.nr_entries += he->nr_rows;
499 browser->nr_callchain_rows += he->nr_rows;
501 browser->nr_hierarchy_entries += he->nr_rows;
506 /* If it doesn't have children, no toggling performed */
510 static int callchain_node__set_folding_rb_tree(struct callchain_node *node, bool unfold)
515 for (nd = rb_first(&node->rb_root); nd; nd = rb_next(nd)) {
516 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
517 struct callchain_list *chain;
518 bool has_children = false;
520 list_for_each_entry(chain, &child->val, list) {
522 callchain_list__set_folding(chain, unfold);
523 has_children = chain->has_children;
527 n += callchain_node__set_folding_rb_tree(child, unfold);
533 static int callchain_node__set_folding(struct callchain_node *node, bool unfold)
535 struct callchain_list *chain;
536 bool has_children = false;
539 list_for_each_entry(chain, &node->val, list) {
541 callchain_list__set_folding(chain, unfold);
542 has_children = chain->has_children;
546 n += callchain_node__set_folding_rb_tree(node, unfold);
551 static int callchain__set_folding(struct rb_root *chain, bool unfold)
556 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
557 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
558 n += callchain_node__set_folding(node, unfold);
564 static int hierarchy_set_folding(struct hist_browser *hb, struct hist_entry *he,
565 bool unfold __maybe_unused)
569 struct hist_entry *child;
572 for (nd = rb_first_cached(&he->hroot_out); nd; nd = rb_next(nd)) {
573 child = rb_entry(nd, struct hist_entry, rb_node);
574 percent = hist_entry__get_percent_limit(child);
575 if (!child->filtered && percent >= hb->min_pcnt)
582 static void hist_entry__set_folding(struct hist_entry *he,
583 struct hist_browser *hb, bool unfold)
585 hist_entry__init_have_children(he);
586 he->unfolded = unfold ? he->has_children : false;
588 if (he->has_children) {
592 n = callchain__set_folding(&he->sorted_chain, unfold);
594 n = hierarchy_set_folding(hb, he, unfold);
596 he->nr_rows = unfold ? n : 0;
602 __hist_browser__set_folding(struct hist_browser *browser, bool unfold)
605 struct hist_entry *he;
608 nd = rb_first_cached(&browser->hists->entries);
610 he = rb_entry(nd, struct hist_entry, rb_node);
612 /* set folding state even if it's currently folded */
613 nd = __rb_hierarchy_next(nd, HMD_FORCE_CHILD);
615 hist_entry__set_folding(he, browser, unfold);
617 percent = hist_entry__get_percent_limit(he);
618 if (he->filtered || percent < browser->min_pcnt)
621 if (!he->depth || unfold)
622 browser->nr_hierarchy_entries++;
624 browser->nr_callchain_rows += he->nr_rows;
625 else if (unfold && !hist_entry__has_hierarchy_children(he, browser->min_pcnt)) {
626 browser->nr_hierarchy_entries++;
627 he->has_no_entry = true;
630 he->has_no_entry = false;
634 static void hist_browser__set_folding(struct hist_browser *browser, bool unfold)
636 browser->nr_hierarchy_entries = 0;
637 browser->nr_callchain_rows = 0;
638 __hist_browser__set_folding(browser, unfold);
640 browser->b.nr_entries = hist_browser__nr_entries(browser);
641 /* Go to the start, we may be way after valid entries after a collapse */
642 ui_browser__reset_index(&browser->b);
645 static void hist_browser__set_folding_selected(struct hist_browser *browser, bool unfold)
647 if (!browser->he_selection)
650 if (unfold == browser->he_selection->unfolded)
653 hist_browser__toggle_fold(browser);
656 static void ui_browser__warn_lost_events(struct ui_browser *browser)
658 ui_browser__warning(browser, 4,
659 "Events are being lost, check IO/CPU overload!\n\n"
660 "You may want to run 'perf' using a RT scheduler policy:\n\n"
661 " perf top -r 80\n\n"
662 "Or reduce the sampling frequency.");
665 static int hist_browser__title(struct hist_browser *browser, char *bf, size_t size)
667 return browser->title ? browser->title(browser, bf, size) : 0;
670 static int hist_browser__handle_hotkey(struct hist_browser *browser, bool warn_lost_event, char *title, size_t size, int key)
674 struct hist_browser_timer *hbt = browser->hbt;
675 struct evsel *evsel = hists_to_evsel(browser->hists);
681 hbt->timer(hbt->arg);
683 if (hist_browser__has_filter(browser) || symbol_conf.report_hierarchy)
684 hist_browser__update_nr_entries(browser);
686 nr_entries = hist_browser__nr_entries(browser);
687 ui_browser__update_nr_entries(&browser->b, nr_entries);
689 if (warn_lost_event &&
690 (evsel->evlist->stats.nr_lost_warned !=
691 evsel->evlist->stats.nr_events[PERF_RECORD_LOST])) {
692 evsel->evlist->stats.nr_lost_warned =
693 evsel->evlist->stats.nr_events[PERF_RECORD_LOST];
694 ui_browser__warn_lost_events(&browser->b);
697 hist_browser__title(browser, title, size);
698 ui_browser__show_title(&browser->b, title);
701 case 'D': { /* Debug */
702 struct hist_entry *h = rb_entry(browser->b.top, struct hist_entry, rb_node);
706 ui_helpline__fpush("%d: nr_ent=(%d,%d), etl: %d, rows=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
707 seq++, browser->b.nr_entries, browser->hists->nr_entries,
708 browser->b.extra_title_lines, browser->b.rows,
709 browser->b.index, browser->b.top_idx, h->row_offset, h->nr_rows);
713 /* Collapse the whole world. */
714 hist_browser__set_folding(browser, false);
717 /* Collapse the selected entry. */
718 hist_browser__set_folding_selected(browser, false);
721 /* Expand the whole world. */
722 hist_browser__set_folding(browser, true);
725 /* Toggle expand/collapse the selected entry. */
726 hist_browser__toggle_fold(browser);
729 browser->show_headers = !browser->show_headers;
730 hist_browser__update_rows(browser);
733 if (hist_browser__toggle_fold(browser))
743 int hist_browser__run(struct hist_browser *browser, const char *help,
744 bool warn_lost_event, int key)
747 struct hist_browser_timer *hbt = browser->hbt;
748 int delay_secs = hbt ? hbt->refresh : 0;
750 browser->b.entries = &browser->hists->entries;
751 browser->b.nr_entries = hist_browser__nr_entries(browser);
753 hist_browser__title(browser, title, sizeof(title));
755 if (ui_browser__show(&browser->b, title, "%s", help) < 0)
758 if (key && hist_browser__handle_hotkey(browser, warn_lost_event, title, sizeof(title), key))
762 key = ui_browser__run(&browser->b, delay_secs);
764 if (hist_browser__handle_hotkey(browser, warn_lost_event, title, sizeof(title), key))
768 ui_browser__hide(&browser->b);
772 struct callchain_print_arg {
773 /* for hists browser */
775 bool is_current_entry;
782 typedef void (*print_callchain_entry_fn)(struct hist_browser *browser,
783 struct callchain_list *chain,
784 const char *str, int offset,
786 struct callchain_print_arg *arg);
788 static void hist_browser__show_callchain_entry(struct hist_browser *browser,
789 struct callchain_list *chain,
790 const char *str, int offset,
792 struct callchain_print_arg *arg)
795 char folded_sign = callchain_list__folded(chain);
796 bool show_annotated = browser->show_dso && chain->ms.sym && symbol__annotation(chain->ms.sym)->src;
798 color = HE_COLORSET_NORMAL;
799 width = browser->b.width - (offset + 2);
800 if (ui_browser__is_current_entry(&browser->b, row)) {
801 browser->selection = &chain->ms;
802 color = HE_COLORSET_SELECTED;
803 arg->is_current_entry = true;
806 ui_browser__set_color(&browser->b, color);
807 ui_browser__gotorc(&browser->b, row, 0);
808 ui_browser__write_nstring(&browser->b, " ", offset);
809 ui_browser__printf(&browser->b, "%c", folded_sign);
810 ui_browser__write_graph(&browser->b, show_annotated ? SLSMG_RARROW_CHAR : ' ');
811 ui_browser__write_nstring(&browser->b, str, width);
814 static void hist_browser__fprintf_callchain_entry(struct hist_browser *b __maybe_unused,
815 struct callchain_list *chain,
816 const char *str, int offset,
817 unsigned short row __maybe_unused,
818 struct callchain_print_arg *arg)
820 char folded_sign = callchain_list__folded(chain);
822 arg->printed += fprintf(arg->fp, "%*s%c %s\n", offset, " ",
826 typedef bool (*check_output_full_fn)(struct hist_browser *browser,
829 static bool hist_browser__check_output_full(struct hist_browser *browser,
832 return browser->b.rows == row;
835 static bool hist_browser__check_dump_full(struct hist_browser *browser __maybe_unused,
836 unsigned short row __maybe_unused)
841 #define LEVEL_OFFSET_STEP 3
843 static int hist_browser__show_callchain_list(struct hist_browser *browser,
844 struct callchain_node *node,
845 struct callchain_list *chain,
846 unsigned short row, u64 total,
847 bool need_percent, int offset,
848 print_callchain_entry_fn print,
849 struct callchain_print_arg *arg)
851 char bf[1024], *alloc_str;
852 char buf[64], *alloc_str2;
856 if (arg->row_offset != 0) {
864 str = callchain_list__sym_name(chain, bf, sizeof(bf),
867 if (symbol_conf.show_branchflag_count) {
868 callchain_list_counts__printf_value(chain, NULL,
871 if (asprintf(&alloc_str2, "%s%s", str, buf) < 0)
872 str = "Not enough memory!";
878 callchain_node__scnprintf_value(node, buf, sizeof(buf),
881 if (asprintf(&alloc_str, "%s %s", buf, str) < 0)
882 str = "Not enough memory!";
887 print(browser, chain, str, offset, row, arg);
894 static bool check_percent_display(struct rb_node *node, u64 parent_total)
896 struct callchain_node *child;
904 child = rb_entry(node, struct callchain_node, rb_node);
905 return callchain_cumul_hits(child) != parent_total;
908 static int hist_browser__show_callchain_flat(struct hist_browser *browser,
909 struct rb_root *root,
910 unsigned short row, u64 total,
912 print_callchain_entry_fn print,
913 struct callchain_print_arg *arg,
914 check_output_full_fn is_output_full)
916 struct rb_node *node;
917 int first_row = row, offset = LEVEL_OFFSET_STEP;
920 node = rb_first(root);
921 need_percent = check_percent_display(node, parent_total);
924 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
925 struct rb_node *next = rb_next(node);
926 struct callchain_list *chain;
927 char folded_sign = ' ';
929 int extra_offset = 0;
931 list_for_each_entry(chain, &child->parent_val, list) {
932 bool was_first = first;
936 else if (need_percent)
937 extra_offset = LEVEL_OFFSET_STEP;
939 folded_sign = callchain_list__folded(chain);
941 row += hist_browser__show_callchain_list(browser, child,
943 was_first && need_percent,
944 offset + extra_offset,
947 if (is_output_full(browser, row))
950 if (folded_sign == '+')
954 list_for_each_entry(chain, &child->val, list) {
955 bool was_first = first;
959 else if (need_percent)
960 extra_offset = LEVEL_OFFSET_STEP;
962 folded_sign = callchain_list__folded(chain);
964 row += hist_browser__show_callchain_list(browser, child,
966 was_first && need_percent,
967 offset + extra_offset,
970 if (is_output_full(browser, row))
973 if (folded_sign == '+')
978 if (is_output_full(browser, row))
983 return row - first_row;
986 static char *hist_browser__folded_callchain_str(struct hist_browser *browser,
987 struct callchain_list *chain,
988 char *value_str, char *old_str)
994 str = callchain_list__sym_name(chain, bf, sizeof(bf),
997 if (asprintf(&new, "%s%s%s", old_str,
998 symbol_conf.field_sep ?: ";", str) < 0)
1002 if (asprintf(&new, "%s %s", value_str, str) < 0)
1005 if (asprintf(&new, "%s", str) < 0)
1012 static int hist_browser__show_callchain_folded(struct hist_browser *browser,
1013 struct rb_root *root,
1014 unsigned short row, u64 total,
1016 print_callchain_entry_fn print,
1017 struct callchain_print_arg *arg,
1018 check_output_full_fn is_output_full)
1020 struct rb_node *node;
1021 int first_row = row, offset = LEVEL_OFFSET_STEP;
1024 node = rb_first(root);
1025 need_percent = check_percent_display(node, parent_total);
1028 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
1029 struct rb_node *next = rb_next(node);
1030 struct callchain_list *chain, *first_chain = NULL;
1032 char *value_str = NULL, *value_str_alloc = NULL;
1033 char *chain_str = NULL, *chain_str_alloc = NULL;
1035 if (arg->row_offset != 0) {
1043 callchain_node__scnprintf_value(child, buf, sizeof(buf), total);
1044 if (asprintf(&value_str, "%s", buf) < 0) {
1045 value_str = (char *)"<...>";
1048 value_str_alloc = value_str;
1051 list_for_each_entry(chain, &child->parent_val, list) {
1052 chain_str = hist_browser__folded_callchain_str(browser,
1053 chain, value_str, chain_str);
1056 first_chain = chain;
1059 if (chain_str == NULL) {
1060 chain_str = (char *)"Not enough memory!";
1064 chain_str_alloc = chain_str;
1067 list_for_each_entry(chain, &child->val, list) {
1068 chain_str = hist_browser__folded_callchain_str(browser,
1069 chain, value_str, chain_str);
1072 first_chain = chain;
1075 if (chain_str == NULL) {
1076 chain_str = (char *)"Not enough memory!";
1080 chain_str_alloc = chain_str;
1084 print(browser, first_chain, chain_str, offset, row++, arg);
1085 free(value_str_alloc);
1086 free(chain_str_alloc);
1089 if (is_output_full(browser, row))
1094 return row - first_row;
1097 static int hist_browser__show_callchain_graph(struct hist_browser *browser,
1098 struct rb_root *root, int level,
1099 unsigned short row, u64 total,
1101 print_callchain_entry_fn print,
1102 struct callchain_print_arg *arg,
1103 check_output_full_fn is_output_full)
1105 struct rb_node *node;
1106 int first_row = row, offset = level * LEVEL_OFFSET_STEP;
1108 u64 percent_total = total;
1110 if (callchain_param.mode == CHAIN_GRAPH_REL)
1111 percent_total = parent_total;
1113 node = rb_first(root);
1114 need_percent = check_percent_display(node, parent_total);
1117 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
1118 struct rb_node *next = rb_next(node);
1119 struct callchain_list *chain;
1120 char folded_sign = ' ';
1122 int extra_offset = 0;
1124 list_for_each_entry(chain, &child->val, list) {
1125 bool was_first = first;
1129 else if (need_percent)
1130 extra_offset = LEVEL_OFFSET_STEP;
1132 folded_sign = callchain_list__folded(chain);
1134 row += hist_browser__show_callchain_list(browser, child,
1135 chain, row, percent_total,
1136 was_first && need_percent,
1137 offset + extra_offset,
1140 if (is_output_full(browser, row))
1143 if (folded_sign == '+')
1147 if (folded_sign == '-') {
1148 const int new_level = level + (extra_offset ? 2 : 1);
1150 row += hist_browser__show_callchain_graph(browser, &child->rb_root,
1151 new_level, row, total,
1152 child->children_hit,
1153 print, arg, is_output_full);
1155 if (is_output_full(browser, row))
1160 return row - first_row;
1163 static int hist_browser__show_callchain(struct hist_browser *browser,
1164 struct hist_entry *entry, int level,
1166 print_callchain_entry_fn print,
1167 struct callchain_print_arg *arg,
1168 check_output_full_fn is_output_full)
1170 u64 total = hists__total_period(entry->hists);
1174 if (symbol_conf.cumulate_callchain)
1175 parent_total = entry->stat_acc->period;
1177 parent_total = entry->stat.period;
1179 if (callchain_param.mode == CHAIN_FLAT) {
1180 printed = hist_browser__show_callchain_flat(browser,
1181 &entry->sorted_chain, row,
1182 total, parent_total, print, arg,
1184 } else if (callchain_param.mode == CHAIN_FOLDED) {
1185 printed = hist_browser__show_callchain_folded(browser,
1186 &entry->sorted_chain, row,
1187 total, parent_total, print, arg,
1190 printed = hist_browser__show_callchain_graph(browser,
1191 &entry->sorted_chain, level, row,
1192 total, parent_total, print, arg,
1196 if (arg->is_current_entry)
1197 browser->he_selection = entry;
1203 struct ui_browser *b;
1208 int __hpp__slsmg_color_printf(struct perf_hpp *hpp, const char *fmt, ...)
1210 struct hpp_arg *arg = hpp->ptr;
1215 va_start(args, fmt);
1216 len = va_arg(args, int);
1217 percent = va_arg(args, double);
1220 ui_browser__set_percent_color(arg->b, percent, arg->current_entry);
1222 ret = scnprintf(hpp->buf, hpp->size, fmt, len, percent);
1223 ui_browser__printf(arg->b, "%s", hpp->buf);
1228 #define __HPP_COLOR_PERCENT_FN(_type, _field) \
1229 static u64 __hpp_get_##_field(struct hist_entry *he) \
1231 return he->stat._field; \
1235 hist_browser__hpp_color_##_type(struct perf_hpp_fmt *fmt, \
1236 struct perf_hpp *hpp, \
1237 struct hist_entry *he) \
1239 return hpp__fmt(fmt, hpp, he, __hpp_get_##_field, " %*.2f%%", \
1240 __hpp__slsmg_color_printf, true); \
1243 #define __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
1244 static u64 __hpp_get_acc_##_field(struct hist_entry *he) \
1246 return he->stat_acc->_field; \
1250 hist_browser__hpp_color_##_type(struct perf_hpp_fmt *fmt, \
1251 struct perf_hpp *hpp, \
1252 struct hist_entry *he) \
1254 if (!symbol_conf.cumulate_callchain) { \
1255 struct hpp_arg *arg = hpp->ptr; \
1256 int len = fmt->user_len ?: fmt->len; \
1257 int ret = scnprintf(hpp->buf, hpp->size, \
1258 "%*s", len, "N/A"); \
1259 ui_browser__printf(arg->b, "%s", hpp->buf); \
1263 return hpp__fmt(fmt, hpp, he, __hpp_get_acc_##_field, \
1264 " %*.2f%%", __hpp__slsmg_color_printf, true); \
1267 __HPP_COLOR_PERCENT_FN(overhead, period)
1268 __HPP_COLOR_PERCENT_FN(overhead_sys, period_sys)
1269 __HPP_COLOR_PERCENT_FN(overhead_us, period_us)
1270 __HPP_COLOR_PERCENT_FN(overhead_guest_sys, period_guest_sys)
1271 __HPP_COLOR_PERCENT_FN(overhead_guest_us, period_guest_us)
1272 __HPP_COLOR_ACC_PERCENT_FN(overhead_acc, period)
1274 #undef __HPP_COLOR_PERCENT_FN
1275 #undef __HPP_COLOR_ACC_PERCENT_FN
1277 void hist_browser__init_hpp(void)
1279 perf_hpp__format[PERF_HPP__OVERHEAD].color =
1280 hist_browser__hpp_color_overhead;
1281 perf_hpp__format[PERF_HPP__OVERHEAD_SYS].color =
1282 hist_browser__hpp_color_overhead_sys;
1283 perf_hpp__format[PERF_HPP__OVERHEAD_US].color =
1284 hist_browser__hpp_color_overhead_us;
1285 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_SYS].color =
1286 hist_browser__hpp_color_overhead_guest_sys;
1287 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_US].color =
1288 hist_browser__hpp_color_overhead_guest_us;
1289 perf_hpp__format[PERF_HPP__OVERHEAD_ACC].color =
1290 hist_browser__hpp_color_overhead_acc;
1295 static int hist_browser__show_entry(struct hist_browser *browser,
1296 struct hist_entry *entry,
1300 int width = browser->b.width;
1301 char folded_sign = ' ';
1302 bool current_entry = ui_browser__is_current_entry(&browser->b, row);
1303 bool use_callchain = hist_entry__has_callchains(entry) && symbol_conf.use_callchain;
1304 off_t row_offset = entry->row_offset;
1306 struct perf_hpp_fmt *fmt;
1308 if (current_entry) {
1309 browser->he_selection = entry;
1310 browser->selection = &entry->ms;
1313 if (use_callchain) {
1314 hist_entry__init_have_children(entry);
1315 folded_sign = hist_entry__folded(entry);
1318 if (row_offset == 0) {
1319 struct hpp_arg arg = {
1321 .folded_sign = folded_sign,
1322 .current_entry = current_entry,
1326 ui_browser__gotorc(&browser->b, row, 0);
1328 hists__for_each_format(browser->hists, fmt) {
1330 struct perf_hpp hpp = {
1336 if (perf_hpp__should_skip(fmt, entry->hists) ||
1337 column++ < browser->b.horiz_scroll)
1340 if (current_entry && browser->b.navkeypressed) {
1341 ui_browser__set_color(&browser->b,
1342 HE_COLORSET_SELECTED);
1344 ui_browser__set_color(&browser->b,
1345 HE_COLORSET_NORMAL);
1349 if (use_callchain) {
1350 ui_browser__printf(&browser->b, "%c ", folded_sign);
1355 ui_browser__printf(&browser->b, " ");
1360 int ret = fmt->color(fmt, &hpp, entry);
1361 hist_entry__snprintf_alignment(entry, &hpp, fmt, ret);
1363 * fmt->color() already used ui_browser to
1364 * print the non alignment bits, skip it (+ret):
1366 ui_browser__printf(&browser->b, "%s", s + ret);
1368 hist_entry__snprintf_alignment(entry, &hpp, fmt, fmt->entry(fmt, &hpp, entry));
1369 ui_browser__printf(&browser->b, "%s", s);
1371 width -= hpp.buf - s;
1374 /* The scroll bar isn't being used */
1375 if (!browser->b.navkeypressed)
1378 ui_browser__write_nstring(&browser->b, "", width);
1385 if (folded_sign == '-' && row != browser->b.rows) {
1386 struct callchain_print_arg arg = {
1387 .row_offset = row_offset,
1388 .is_current_entry = current_entry,
1391 printed += hist_browser__show_callchain(browser,
1393 hist_browser__show_callchain_entry,
1395 hist_browser__check_output_full);
1401 static int hist_browser__show_hierarchy_entry(struct hist_browser *browser,
1402 struct hist_entry *entry,
1407 int width = browser->b.width;
1408 char folded_sign = ' ';
1409 bool current_entry = ui_browser__is_current_entry(&browser->b, row);
1410 off_t row_offset = entry->row_offset;
1412 struct perf_hpp_fmt *fmt;
1413 struct perf_hpp_list_node *fmt_node;
1414 struct hpp_arg arg = {
1416 .current_entry = current_entry,
1419 int hierarchy_indent = (entry->hists->nr_hpp_node - 2) * HIERARCHY_INDENT;
1421 if (current_entry) {
1422 browser->he_selection = entry;
1423 browser->selection = &entry->ms;
1426 hist_entry__init_have_children(entry);
1427 folded_sign = hist_entry__folded(entry);
1428 arg.folded_sign = folded_sign;
1430 if (entry->leaf && row_offset) {
1432 goto show_callchain;
1435 ui_browser__gotorc(&browser->b, row, 0);
1437 if (current_entry && browser->b.navkeypressed)
1438 ui_browser__set_color(&browser->b, HE_COLORSET_SELECTED);
1440 ui_browser__set_color(&browser->b, HE_COLORSET_NORMAL);
1442 ui_browser__write_nstring(&browser->b, "", level * HIERARCHY_INDENT);
1443 width -= level * HIERARCHY_INDENT;
1445 /* the first hpp_list_node is for overhead columns */
1446 fmt_node = list_first_entry(&entry->hists->hpp_formats,
1447 struct perf_hpp_list_node, list);
1448 perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
1450 struct perf_hpp hpp = {
1456 if (perf_hpp__should_skip(fmt, entry->hists) ||
1457 column++ < browser->b.horiz_scroll)
1460 if (current_entry && browser->b.navkeypressed) {
1461 ui_browser__set_color(&browser->b,
1462 HE_COLORSET_SELECTED);
1464 ui_browser__set_color(&browser->b,
1465 HE_COLORSET_NORMAL);
1469 ui_browser__printf(&browser->b, "%c ", folded_sign);
1473 ui_browser__printf(&browser->b, " ");
1478 int ret = fmt->color(fmt, &hpp, entry);
1479 hist_entry__snprintf_alignment(entry, &hpp, fmt, ret);
1481 * fmt->color() already used ui_browser to
1482 * print the non alignment bits, skip it (+ret):
1484 ui_browser__printf(&browser->b, "%s", s + ret);
1486 int ret = fmt->entry(fmt, &hpp, entry);
1487 hist_entry__snprintf_alignment(entry, &hpp, fmt, ret);
1488 ui_browser__printf(&browser->b, "%s", s);
1490 width -= hpp.buf - s;
1494 ui_browser__write_nstring(&browser->b, "", hierarchy_indent);
1495 width -= hierarchy_indent;
1498 if (column >= browser->b.horiz_scroll) {
1500 struct perf_hpp hpp = {
1506 if (current_entry && browser->b.navkeypressed) {
1507 ui_browser__set_color(&browser->b,
1508 HE_COLORSET_SELECTED);
1510 ui_browser__set_color(&browser->b,
1511 HE_COLORSET_NORMAL);
1514 perf_hpp_list__for_each_format(entry->hpp_list, fmt) {
1516 ui_browser__printf(&browser->b, "%c ", folded_sign);
1519 ui_browser__write_nstring(&browser->b, "", 2);
1525 * No need to call hist_entry__snprintf_alignment()
1526 * since this fmt is always the last column in the
1530 width -= fmt->color(fmt, &hpp, entry);
1534 width -= fmt->entry(fmt, &hpp, entry);
1535 ui_browser__printf(&browser->b, "%s", skip_spaces(s));
1537 while (isspace(s[i++]))
1543 /* The scroll bar isn't being used */
1544 if (!browser->b.navkeypressed)
1547 ui_browser__write_nstring(&browser->b, "", width);
1553 if (entry->leaf && folded_sign == '-' && row != browser->b.rows) {
1554 struct callchain_print_arg carg = {
1555 .row_offset = row_offset,
1558 printed += hist_browser__show_callchain(browser, entry,
1560 hist_browser__show_callchain_entry, &carg,
1561 hist_browser__check_output_full);
1567 static int hist_browser__show_no_entry(struct hist_browser *browser,
1568 unsigned short row, int level)
1570 int width = browser->b.width;
1571 bool current_entry = ui_browser__is_current_entry(&browser->b, row);
1575 struct perf_hpp_fmt *fmt;
1576 struct perf_hpp_list_node *fmt_node;
1577 int indent = browser->hists->nr_hpp_node - 2;
1579 if (current_entry) {
1580 browser->he_selection = NULL;
1581 browser->selection = NULL;
1584 ui_browser__gotorc(&browser->b, row, 0);
1586 if (current_entry && browser->b.navkeypressed)
1587 ui_browser__set_color(&browser->b, HE_COLORSET_SELECTED);
1589 ui_browser__set_color(&browser->b, HE_COLORSET_NORMAL);
1591 ui_browser__write_nstring(&browser->b, "", level * HIERARCHY_INDENT);
1592 width -= level * HIERARCHY_INDENT;
1594 /* the first hpp_list_node is for overhead columns */
1595 fmt_node = list_first_entry(&browser->hists->hpp_formats,
1596 struct perf_hpp_list_node, list);
1597 perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
1598 if (perf_hpp__should_skip(fmt, browser->hists) ||
1599 column++ < browser->b.horiz_scroll)
1602 ret = fmt->width(fmt, NULL, browser->hists);
1605 /* for folded sign */
1609 /* space between columns */
1613 ui_browser__write_nstring(&browser->b, "", ret);
1617 ui_browser__write_nstring(&browser->b, "", indent * HIERARCHY_INDENT);
1618 width -= indent * HIERARCHY_INDENT;
1620 if (column >= browser->b.horiz_scroll) {
1623 ret = snprintf(buf, sizeof(buf), "no entry >= %.2f%%", browser->min_pcnt);
1624 ui_browser__printf(&browser->b, " %s", buf);
1628 /* The scroll bar isn't being used */
1629 if (!browser->b.navkeypressed)
1632 ui_browser__write_nstring(&browser->b, "", width);
1636 static int advance_hpp_check(struct perf_hpp *hpp, int inc)
1638 advance_hpp(hpp, inc);
1639 return hpp->size <= 0;
1643 hists_browser__scnprintf_headers(struct hist_browser *browser, char *buf,
1644 size_t size, int line)
1646 struct hists *hists = browser->hists;
1647 struct perf_hpp dummy_hpp = {
1651 struct perf_hpp_fmt *fmt;
1656 if (hists__has_callchains(hists) && symbol_conf.use_callchain) {
1657 ret = scnprintf(buf, size, " ");
1658 if (advance_hpp_check(&dummy_hpp, ret))
1662 hists__for_each_format(browser->hists, fmt) {
1663 if (perf_hpp__should_skip(fmt, hists) || column++ < browser->b.horiz_scroll)
1666 ret = fmt->header(fmt, &dummy_hpp, hists, line, &span);
1667 if (advance_hpp_check(&dummy_hpp, ret))
1673 ret = scnprintf(dummy_hpp.buf, dummy_hpp.size, " ");
1674 if (advance_hpp_check(&dummy_hpp, ret))
1681 static int hists_browser__scnprintf_hierarchy_headers(struct hist_browser *browser, char *buf, size_t size)
1683 struct hists *hists = browser->hists;
1684 struct perf_hpp dummy_hpp = {
1688 struct perf_hpp_fmt *fmt;
1689 struct perf_hpp_list_node *fmt_node;
1692 int indent = hists->nr_hpp_node - 2;
1693 bool first_node, first_col;
1695 ret = scnprintf(buf, size, " ");
1696 if (advance_hpp_check(&dummy_hpp, ret))
1700 /* the first hpp_list_node is for overhead columns */
1701 fmt_node = list_first_entry(&hists->hpp_formats,
1702 struct perf_hpp_list_node, list);
1703 perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
1704 if (column++ < browser->b.horiz_scroll)
1707 ret = fmt->header(fmt, &dummy_hpp, hists, 0, NULL);
1708 if (advance_hpp_check(&dummy_hpp, ret))
1711 ret = scnprintf(dummy_hpp.buf, dummy_hpp.size, " ");
1712 if (advance_hpp_check(&dummy_hpp, ret))
1719 ret = scnprintf(dummy_hpp.buf, dummy_hpp.size, "%*s",
1720 indent * HIERARCHY_INDENT, "");
1721 if (advance_hpp_check(&dummy_hpp, ret))
1726 list_for_each_entry_continue(fmt_node, &hists->hpp_formats, list) {
1728 ret = scnprintf(dummy_hpp.buf, dummy_hpp.size, " / ");
1729 if (advance_hpp_check(&dummy_hpp, ret))
1735 perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
1738 if (perf_hpp__should_skip(fmt, hists))
1742 ret = scnprintf(dummy_hpp.buf, dummy_hpp.size, "+");
1743 if (advance_hpp_check(&dummy_hpp, ret))
1748 ret = fmt->header(fmt, &dummy_hpp, hists, 0, NULL);
1749 dummy_hpp.buf[ret] = '\0';
1751 start = strim(dummy_hpp.buf);
1752 ret = strlen(start);
1754 if (start != dummy_hpp.buf)
1755 memmove(dummy_hpp.buf, start, ret + 1);
1757 if (advance_hpp_check(&dummy_hpp, ret))
1765 static void hists_browser__hierarchy_headers(struct hist_browser *browser)
1769 hists_browser__scnprintf_hierarchy_headers(browser, headers,
1772 ui_browser__gotorc_title(&browser->b, 0, 0);
1773 ui_browser__set_color(&browser->b, HE_COLORSET_ROOT);
1774 ui_browser__write_nstring(&browser->b, headers, browser->b.width + 1);
1777 static void hists_browser__headers(struct hist_browser *browser)
1779 struct hists *hists = browser->hists;
1780 struct perf_hpp_list *hpp_list = hists->hpp_list;
1784 for (line = 0; line < hpp_list->nr_header_lines; line++) {
1787 hists_browser__scnprintf_headers(browser, headers,
1788 sizeof(headers), line);
1790 ui_browser__gotorc_title(&browser->b, line, 0);
1791 ui_browser__set_color(&browser->b, HE_COLORSET_ROOT);
1792 ui_browser__write_nstring(&browser->b, headers, browser->b.width + 1);
1796 static void hist_browser__show_headers(struct hist_browser *browser)
1798 if (symbol_conf.report_hierarchy)
1799 hists_browser__hierarchy_headers(browser);
1801 hists_browser__headers(browser);
1804 static void ui_browser__hists_init_top(struct ui_browser *browser)
1806 if (browser->top == NULL) {
1807 struct hist_browser *hb;
1809 hb = container_of(browser, struct hist_browser, b);
1810 browser->top = rb_first_cached(&hb->hists->entries);
1814 static unsigned int hist_browser__refresh(struct ui_browser *browser)
1818 struct hist_browser *hb = container_of(browser, struct hist_browser, b);
1820 if (hb->show_headers)
1821 hist_browser__show_headers(hb);
1823 ui_browser__hists_init_top(browser);
1824 hb->he_selection = NULL;
1825 hb->selection = NULL;
1827 for (nd = browser->top; nd; nd = rb_hierarchy_next(nd)) {
1828 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1832 /* let it move to sibling */
1833 h->unfolded = false;
1837 if (symbol_conf.report_individual_block)
1838 percent = block_info__total_cycles_percent(h);
1840 percent = hist_entry__get_percent_limit(h);
1842 if (percent < hb->min_pcnt)
1845 if (symbol_conf.report_hierarchy) {
1846 row += hist_browser__show_hierarchy_entry(hb, h, row,
1848 if (row == browser->rows)
1851 if (h->has_no_entry) {
1852 hist_browser__show_no_entry(hb, row, h->depth + 1);
1856 row += hist_browser__show_entry(hb, h, row);
1859 if (row == browser->rows)
1866 static struct rb_node *hists__filter_entries(struct rb_node *nd,
1869 while (nd != NULL) {
1870 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1871 float percent = hist_entry__get_percent_limit(h);
1873 if (!h->filtered && percent >= min_pcnt)
1877 * If it's filtered, its all children also were filtered.
1878 * So move to sibling node.
1883 nd = rb_hierarchy_next(nd);
1889 static struct rb_node *hists__filter_prev_entries(struct rb_node *nd,
1892 while (nd != NULL) {
1893 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1894 float percent = hist_entry__get_percent_limit(h);
1896 if (!h->filtered && percent >= min_pcnt)
1899 nd = rb_hierarchy_prev(nd);
1905 static void ui_browser__hists_seek(struct ui_browser *browser,
1906 off_t offset, int whence)
1908 struct hist_entry *h;
1911 struct hist_browser *hb;
1913 hb = container_of(browser, struct hist_browser, b);
1915 if (browser->nr_entries == 0)
1918 ui_browser__hists_init_top(browser);
1922 nd = hists__filter_entries(rb_first(browser->entries),
1929 nd = rb_hierarchy_last(rb_last(browser->entries));
1930 nd = hists__filter_prev_entries(nd, hb->min_pcnt);
1938 * Moves not relative to the first visible entry invalidates its
1941 h = rb_entry(browser->top, struct hist_entry, rb_node);
1945 * Here we have to check if nd is expanded (+), if it is we can't go
1946 * the next top level hist_entry, instead we must compute an offset of
1947 * what _not_ to show and not change the first visible entry.
1949 * This offset increments when we are going from top to bottom and
1950 * decreases when we're going from bottom to top.
1952 * As we don't have backpointers to the top level in the callchains
1953 * structure, we need to always print the whole hist_entry callchain,
1954 * skipping the first ones that are before the first visible entry
1955 * and stop when we printed enough lines to fill the screen.
1963 h = rb_entry(nd, struct hist_entry, rb_node);
1964 if (h->unfolded && h->leaf) {
1965 u16 remaining = h->nr_rows - h->row_offset;
1966 if (offset > remaining) {
1967 offset -= remaining;
1970 h->row_offset += offset;
1976 nd = hists__filter_entries(rb_hierarchy_next(nd),
1982 } while (offset != 0);
1983 } else if (offset < 0) {
1985 h = rb_entry(nd, struct hist_entry, rb_node);
1986 if (h->unfolded && h->leaf) {
1988 if (-offset > h->row_offset) {
1989 offset += h->row_offset;
1992 h->row_offset += offset;
1998 if (-offset > h->nr_rows) {
1999 offset += h->nr_rows;
2002 h->row_offset = h->nr_rows + offset;
2010 nd = hists__filter_prev_entries(rb_hierarchy_prev(nd),
2018 * Last unfiltered hist_entry, check if it is
2019 * unfolded, if it is then we should have
2020 * row_offset at its last entry.
2022 h = rb_entry(nd, struct hist_entry, rb_node);
2023 if (h->unfolded && h->leaf)
2024 h->row_offset = h->nr_rows;
2031 h = rb_entry(nd, struct hist_entry, rb_node);
2036 static int hist_browser__fprintf_callchain(struct hist_browser *browser,
2037 struct hist_entry *he, FILE *fp,
2040 struct callchain_print_arg arg = {
2044 hist_browser__show_callchain(browser, he, level, 0,
2045 hist_browser__fprintf_callchain_entry, &arg,
2046 hist_browser__check_dump_full);
2050 static int hist_browser__fprintf_entry(struct hist_browser *browser,
2051 struct hist_entry *he, FILE *fp)
2055 char folded_sign = ' ';
2056 struct perf_hpp hpp = {
2060 struct perf_hpp_fmt *fmt;
2064 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain) {
2065 folded_sign = hist_entry__folded(he);
2066 printed += fprintf(fp, "%c ", folded_sign);
2069 hists__for_each_format(browser->hists, fmt) {
2070 if (perf_hpp__should_skip(fmt, he->hists))
2074 ret = scnprintf(hpp.buf, hpp.size, " ");
2075 advance_hpp(&hpp, ret);
2079 ret = fmt->entry(fmt, &hpp, he);
2080 ret = hist_entry__snprintf_alignment(he, &hpp, fmt, ret);
2081 advance_hpp(&hpp, ret);
2083 printed += fprintf(fp, "%s\n", s);
2085 if (folded_sign == '-')
2086 printed += hist_browser__fprintf_callchain(browser, he, fp, 1);
2092 static int hist_browser__fprintf_hierarchy_entry(struct hist_browser *browser,
2093 struct hist_entry *he,
2094 FILE *fp, int level)
2098 char folded_sign = ' ';
2099 struct perf_hpp hpp = {
2103 struct perf_hpp_fmt *fmt;
2104 struct perf_hpp_list_node *fmt_node;
2107 int hierarchy_indent = (he->hists->nr_hpp_node - 2) * HIERARCHY_INDENT;
2109 printed = fprintf(fp, "%*s", level * HIERARCHY_INDENT, "");
2111 folded_sign = hist_entry__folded(he);
2112 printed += fprintf(fp, "%c", folded_sign);
2114 /* the first hpp_list_node is for overhead columns */
2115 fmt_node = list_first_entry(&he->hists->hpp_formats,
2116 struct perf_hpp_list_node, list);
2117 perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
2119 ret = scnprintf(hpp.buf, hpp.size, " ");
2120 advance_hpp(&hpp, ret);
2124 ret = fmt->entry(fmt, &hpp, he);
2125 advance_hpp(&hpp, ret);
2128 ret = scnprintf(hpp.buf, hpp.size, "%*s", hierarchy_indent, "");
2129 advance_hpp(&hpp, ret);
2131 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
2132 ret = scnprintf(hpp.buf, hpp.size, " ");
2133 advance_hpp(&hpp, ret);
2135 ret = fmt->entry(fmt, &hpp, he);
2136 advance_hpp(&hpp, ret);
2140 printed += fprintf(fp, "%s\n", s);
2142 if (he->leaf && folded_sign == '-') {
2143 printed += hist_browser__fprintf_callchain(browser, he, fp,
2150 static int hist_browser__fprintf(struct hist_browser *browser, FILE *fp)
2152 struct rb_node *nd = hists__filter_entries(rb_first(browser->b.entries),
2157 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2159 if (symbol_conf.report_hierarchy) {
2160 printed += hist_browser__fprintf_hierarchy_entry(browser,
2164 printed += hist_browser__fprintf_entry(browser, h, fp);
2167 nd = hists__filter_entries(rb_hierarchy_next(nd),
2174 static int hist_browser__dump(struct hist_browser *browser)
2180 scnprintf(filename, sizeof(filename), "perf.hist.%d", browser->print_seq);
2181 if (access(filename, F_OK))
2184 * XXX: Just an arbitrary lazy upper limit
2186 if (++browser->print_seq == 8192) {
2187 ui_helpline__fpush("Too many perf.hist.N files, nothing written!");
2192 fp = fopen(filename, "w");
2195 const char *err = str_error_r(errno, bf, sizeof(bf));
2196 ui_helpline__fpush("Couldn't write to %s: %s", filename, err);
2200 ++browser->print_seq;
2201 hist_browser__fprintf(browser, fp);
2203 ui_helpline__fpush("%s written!", filename);
2208 void hist_browser__init(struct hist_browser *browser,
2209 struct hists *hists)
2211 struct perf_hpp_fmt *fmt;
2213 browser->hists = hists;
2214 browser->b.refresh = hist_browser__refresh;
2215 browser->b.refresh_dimensions = hist_browser__refresh_dimensions;
2216 browser->b.seek = ui_browser__hists_seek;
2217 browser->b.use_navkeypressed = true;
2218 browser->show_headers = symbol_conf.show_hist_headers;
2219 hist_browser__set_title_space(browser);
2221 if (symbol_conf.report_hierarchy) {
2222 struct perf_hpp_list_node *fmt_node;
2224 /* count overhead columns (in the first node) */
2225 fmt_node = list_first_entry(&hists->hpp_formats,
2226 struct perf_hpp_list_node, list);
2227 perf_hpp_list__for_each_format(&fmt_node->hpp, fmt)
2228 ++browser->b.columns;
2230 /* add a single column for whole hierarchy sort keys*/
2231 ++browser->b.columns;
2233 hists__for_each_format(hists, fmt)
2234 ++browser->b.columns;
2237 hists__reset_column_width(hists);
2240 struct hist_browser *hist_browser__new(struct hists *hists)
2242 struct hist_browser *browser = zalloc(sizeof(*browser));
2245 hist_browser__init(browser, hists);
2250 static struct hist_browser *
2251 perf_evsel_browser__new(struct evsel *evsel,
2252 struct hist_browser_timer *hbt,
2253 struct perf_env *env)
2255 struct hist_browser *browser = hist_browser__new(evsel__hists(evsel));
2260 browser->title = hists_browser__scnprintf_title;
2265 void hist_browser__delete(struct hist_browser *browser)
2270 static struct hist_entry *hist_browser__selected_entry(struct hist_browser *browser)
2272 return browser->he_selection;
2275 static struct thread *hist_browser__selected_thread(struct hist_browser *browser)
2277 return browser->he_selection->thread;
2280 static struct res_sample *hist_browser__selected_res_sample(struct hist_browser *browser)
2282 return browser->he_selection ? browser->he_selection->res_samples : NULL;
2285 /* Check whether the browser is for 'top' or 'report' */
2286 static inline bool is_report_browser(void *timer)
2288 return timer == NULL;
2291 static int hists_browser__scnprintf_title(struct hist_browser *browser, char *bf, size_t size)
2293 struct hist_browser_timer *hbt = browser->hbt;
2294 int printed = __hists__scnprintf_title(browser->hists, bf, size, !is_report_browser(hbt));
2296 if (!is_report_browser(hbt)) {
2297 struct perf_top *top = hbt->arg;
2299 printed += scnprintf(bf + printed, size - printed,
2300 " lost: %" PRIu64 "/%" PRIu64,
2301 top->lost, top->lost_total);
2303 printed += scnprintf(bf + printed, size - printed,
2304 " drop: %" PRIu64 "/%" PRIu64,
2305 top->drop, top->drop_total);
2308 printed += scnprintf(bf + printed, size - printed, " [z]");
2310 perf_top__reset_sample_counters(top);
2317 static inline void free_popup_options(char **options, int n)
2321 for (i = 0; i < n; ++i)
2326 * Only runtime switching of perf data file will make "input_name" point
2327 * to a malloced buffer. So add "is_input_name_malloced" flag to decide
2328 * whether we need to call free() for current "input_name" during the switch.
2330 static bool is_input_name_malloced = false;
2332 static int switch_data_file(void)
2334 char *pwd, *options[32], *abs_path[32], *tmp;
2336 int nr_options = 0, choice = -1, ret = -1;
2337 struct dirent *dent;
2339 pwd = getenv("PWD");
2343 pwd_dir = opendir(pwd);
2347 memset(options, 0, sizeof(options));
2348 memset(abs_path, 0, sizeof(abs_path));
2350 while ((dent = readdir(pwd_dir))) {
2351 char path[PATH_MAX];
2353 char *name = dent->d_name;
2356 if (!(dent->d_type == DT_REG))
2359 snprintf(path, sizeof(path), "%s/%s", pwd, name);
2361 file = fopen(path, "r");
2365 if (fread(&magic, 1, 8, file) < 8)
2366 goto close_file_and_continue;
2368 if (is_perf_magic(magic)) {
2369 options[nr_options] = strdup(name);
2370 if (!options[nr_options])
2371 goto close_file_and_continue;
2373 abs_path[nr_options] = strdup(path);
2374 if (!abs_path[nr_options]) {
2375 zfree(&options[nr_options]);
2376 ui__warning("Can't search all data files due to memory shortage.\n");
2384 close_file_and_continue:
2386 if (nr_options >= 32) {
2387 ui__warning("Too many perf data files in PWD!\n"
2388 "Only the first 32 files will be listed.\n");
2395 choice = ui__popup_menu(nr_options, options, NULL);
2396 if (choice < nr_options && choice >= 0) {
2397 tmp = strdup(abs_path[choice]);
2399 if (is_input_name_malloced)
2400 free((void *)input_name);
2402 is_input_name_malloced = true;
2405 ui__warning("Data switch failed due to memory shortage!\n");
2409 free_popup_options(options, nr_options);
2410 free_popup_options(abs_path, nr_options);
2414 struct popup_action {
2416 struct thread *thread;
2417 struct evsel *evsel;
2418 int (*fn)(struct hist_browser *browser, struct popup_action *act);
2419 struct map_symbol ms;
2426 do_annotate(struct hist_browser *browser, struct popup_action *act)
2428 struct evsel *evsel;
2429 struct annotation *notes;
2430 struct hist_entry *he;
2433 if (!annotate_opts.objdump_path &&
2434 perf_env__lookup_objdump(browser->env, &annotate_opts.objdump_path))
2437 notes = symbol__annotation(act->ms.sym);
2441 if (browser->block_evsel)
2442 evsel = browser->block_evsel;
2444 evsel = hists_to_evsel(browser->hists);
2446 err = map_symbol__tui_annotate(&act->ms, evsel, browser->hbt);
2447 he = hist_browser__selected_entry(browser);
2449 * offer option to annotate the other branch source or target
2450 * (if they exists) when returning from annotate
2452 if ((err == 'q' || err == CTRL('c')) && he->branch_info)
2455 ui_browser__update_nr_entries(&browser->b, browser->hists->nr_entries);
2457 ui_browser__handle_resize(&browser->b);
2461 static struct symbol *symbol__new_unresolved(u64 addr, struct map *map)
2463 struct annotated_source *src;
2467 snprintf(name, sizeof(name), "%.*" PRIx64, BITS_PER_LONG / 4, addr);
2469 sym = symbol__new(addr, ANNOTATION_DUMMY_LEN, 0, 0, name);
2471 src = symbol__hists(sym, 1);
2473 symbol__delete(sym);
2477 dso__insert_symbol(map__dso(map), sym);
2484 add_annotate_opt(struct hist_browser *browser __maybe_unused,
2485 struct popup_action *act, char **optstr,
2486 struct map_symbol *ms,
2491 if (!ms->map || (dso = map__dso(ms->map)) == NULL || dso->annotate_warned)
2495 ms->sym = symbol__new_unresolved(addr, ms->map);
2497 if (ms->sym == NULL || symbol__annotation(ms->sym)->src == NULL)
2500 if (asprintf(optstr, "Annotate %s", ms->sym->name) < 0)
2504 act->fn = do_annotate;
2509 do_zoom_thread(struct hist_browser *browser, struct popup_action *act)
2511 struct thread *thread = act->thread;
2513 if ((!hists__has(browser->hists, thread) &&
2514 !hists__has(browser->hists, comm)) || thread == NULL)
2517 if (browser->hists->thread_filter) {
2518 pstack__remove(browser->pstack, &browser->hists->thread_filter);
2519 perf_hpp__set_elide(HISTC_THREAD, false);
2520 thread__zput(browser->hists->thread_filter);
2523 const char *comm_set_str =
2524 thread__comm_set(thread) ? thread__comm_str(thread) : "";
2526 if (hists__has(browser->hists, thread)) {
2527 ui_helpline__fpush("To zoom out press ESC or ENTER + \"Zoom out of %s(%d) thread\"",
2528 comm_set_str, thread__tid(thread));
2530 ui_helpline__fpush("To zoom out press ESC or ENTER + \"Zoom out of %s thread\"",
2534 browser->hists->thread_filter = thread__get(thread);
2535 perf_hpp__set_elide(HISTC_THREAD, false);
2536 pstack__push(browser->pstack, &browser->hists->thread_filter);
2539 hists__filter_by_thread(browser->hists);
2540 hist_browser__reset(browser);
2545 add_thread_opt(struct hist_browser *browser, struct popup_action *act,
2546 char **optstr, struct thread *thread)
2549 const char *comm_set_str, *in_out;
2551 if ((!hists__has(browser->hists, thread) &&
2552 !hists__has(browser->hists, comm)) || thread == NULL)
2555 in_out = browser->hists->thread_filter ? "out of" : "into";
2556 comm_set_str = thread__comm_set(thread) ? thread__comm_str(thread) : "";
2557 if (hists__has(browser->hists, thread)) {
2558 ret = asprintf(optstr, "Zoom %s %s(%d) thread",
2559 in_out, comm_set_str, thread__tid(thread));
2561 ret = asprintf(optstr, "Zoom %s %s thread", in_out, comm_set_str);
2566 act->thread = thread;
2567 act->fn = do_zoom_thread;
2571 static int hists_browser__zoom_map(struct hist_browser *browser, struct map *map)
2573 if (!hists__has(browser->hists, dso) || map == NULL)
2576 if (browser->hists->dso_filter) {
2577 pstack__remove(browser->pstack, &browser->hists->dso_filter);
2578 perf_hpp__set_elide(HISTC_DSO, false);
2579 browser->hists->dso_filter = NULL;
2582 struct dso *dso = map__dso(map);
2583 ui_helpline__fpush("To zoom out press ESC or ENTER + \"Zoom out of %s DSO\"",
2584 __map__is_kernel(map) ? "the Kernel" : dso->short_name);
2585 browser->hists->dso_filter = dso;
2586 perf_hpp__set_elide(HISTC_DSO, true);
2587 pstack__push(browser->pstack, &browser->hists->dso_filter);
2590 hists__filter_by_dso(browser->hists);
2591 hist_browser__reset(browser);
2596 do_zoom_dso(struct hist_browser *browser, struct popup_action *act)
2598 return hists_browser__zoom_map(browser, act->ms.map);
2602 add_dso_opt(struct hist_browser *browser, struct popup_action *act,
2603 char **optstr, struct map *map)
2605 if (!hists__has(browser->hists, dso) || map == NULL)
2608 if (asprintf(optstr, "Zoom %s %s DSO (use the 'k' hotkey to zoom directly into the kernel)",
2609 browser->hists->dso_filter ? "out of" : "into",
2610 __map__is_kernel(map) ? "the Kernel" : map__dso(map)->short_name) < 0)
2614 act->fn = do_zoom_dso;
2618 static int do_toggle_callchain(struct hist_browser *browser, struct popup_action *act __maybe_unused)
2620 hist_browser__toggle_fold(browser);
2624 static int add_callchain_toggle_opt(struct hist_browser *browser, struct popup_action *act, char **optstr)
2628 if (!hist_browser__selection_has_children(browser))
2631 if (asprintf(optstr, "%s [%s] callchain (one level, same as '+' hotkey, use 'e'/'c' for the whole main level entry)",
2632 hist_browser__selection_unfolded(browser) ? "Collapse" : "Expand",
2633 hist_browser__selection_sym_name(browser, sym_name, sizeof(sym_name))) < 0)
2636 act->fn = do_toggle_callchain;
2641 do_browse_map(struct hist_browser *browser __maybe_unused,
2642 struct popup_action *act)
2644 map__browse(act->ms.map);
2649 add_map_opt(struct hist_browser *browser,
2650 struct popup_action *act, char **optstr, struct map *map)
2652 if (!hists__has(browser->hists, dso) || map == NULL)
2655 if (asprintf(optstr, "Browse map details") < 0)
2659 act->fn = do_browse_map;
2664 do_run_script(struct hist_browser *browser __maybe_unused,
2665 struct popup_action *act)
2673 len += strlen(thread__comm_str(act->thread));
2674 else if (act->ms.sym)
2675 len += strlen(act->ms.sym->name);
2676 script_opt = malloc(len);
2682 n = scnprintf(script_opt, len, " -c %s ",
2683 thread__comm_str(act->thread));
2684 } else if (act->ms.sym) {
2685 n = scnprintf(script_opt, len, " -S %s ",
2690 char start[32], end[32];
2691 unsigned long starttime = act->time;
2692 unsigned long endtime = act->time + symbol_conf.time_quantum;
2694 if (starttime == endtime) { /* Display 1ms as fallback */
2695 starttime -= 1*NSEC_PER_MSEC;
2696 endtime += 1*NSEC_PER_MSEC;
2698 timestamp__scnprintf_usec(starttime, start, sizeof start);
2699 timestamp__scnprintf_usec(endtime, end, sizeof end);
2700 n += snprintf(script_opt + n, len - n, " --time %s,%s", start, end);
2703 script_browse(script_opt, act->evsel);
2709 do_res_sample_script(struct hist_browser *browser __maybe_unused,
2710 struct popup_action *act)
2712 struct hist_entry *he;
2714 he = hist_browser__selected_entry(browser);
2715 res_sample_browse(he->res_samples, he->num_res, act->evsel, act->rstype);
2720 add_script_opt_2(struct hist_browser *browser __maybe_unused,
2721 struct popup_action *act, char **optstr,
2722 struct thread *thread, struct symbol *sym,
2723 struct evsel *evsel, const char *tstr)
2727 if (asprintf(optstr, "Run scripts for samples of thread [%s]%s",
2728 thread__comm_str(thread), tstr) < 0)
2731 if (asprintf(optstr, "Run scripts for samples of symbol [%s]%s",
2732 sym->name, tstr) < 0)
2735 if (asprintf(optstr, "Run scripts for all samples%s", tstr) < 0)
2739 act->thread = thread;
2742 act->fn = do_run_script;
2747 add_script_opt(struct hist_browser *browser,
2748 struct popup_action *act, char **optstr,
2749 struct thread *thread, struct symbol *sym,
2750 struct evsel *evsel)
2753 struct hist_entry *he;
2755 n = add_script_opt_2(browser, act, optstr, thread, sym, evsel, "");
2757 he = hist_browser__selected_entry(browser);
2758 if (sort_order && strstr(sort_order, "time")) {
2763 j = sprintf(tstr, " in ");
2764 j += timestamp__scnprintf_usec(he->time, tstr + j,
2766 j += sprintf(tstr + j, "-");
2767 timestamp__scnprintf_usec(he->time + symbol_conf.time_quantum,
2768 tstr + j, sizeof tstr - j);
2769 n += add_script_opt_2(browser, act, optstr, thread, sym,
2771 act->time = he->time;
2777 add_res_sample_opt(struct hist_browser *browser __maybe_unused,
2778 struct popup_action *act, char **optstr,
2779 struct res_sample *res_sample,
2780 struct evsel *evsel,
2786 if (asprintf(optstr, "Show context for individual samples %s",
2787 type == A_ASM ? "with assembler" :
2788 type == A_SOURCE ? "with source" : "") < 0)
2791 act->fn = do_res_sample_script;
2798 do_switch_data(struct hist_browser *browser __maybe_unused,
2799 struct popup_action *act __maybe_unused)
2801 if (switch_data_file()) {
2802 ui__warning("Won't switch the data files due to\n"
2803 "no valid data file get selected!\n");
2807 return K_SWITCH_INPUT_DATA;
2811 add_switch_opt(struct hist_browser *browser,
2812 struct popup_action *act, char **optstr)
2814 if (!is_report_browser(browser->hbt))
2817 if (asprintf(optstr, "Switch to another data file in PWD") < 0)
2820 act->fn = do_switch_data;
2825 do_exit_browser(struct hist_browser *browser __maybe_unused,
2826 struct popup_action *act __maybe_unused)
2832 add_exit_opt(struct hist_browser *browser __maybe_unused,
2833 struct popup_action *act, char **optstr)
2835 if (asprintf(optstr, "Exit") < 0)
2838 act->fn = do_exit_browser;
2843 do_zoom_socket(struct hist_browser *browser, struct popup_action *act)
2845 if (!hists__has(browser->hists, socket) || act->socket < 0)
2848 if (browser->hists->socket_filter > -1) {
2849 pstack__remove(browser->pstack, &browser->hists->socket_filter);
2850 browser->hists->socket_filter = -1;
2851 perf_hpp__set_elide(HISTC_SOCKET, false);
2853 browser->hists->socket_filter = act->socket;
2854 perf_hpp__set_elide(HISTC_SOCKET, true);
2855 pstack__push(browser->pstack, &browser->hists->socket_filter);
2858 hists__filter_by_socket(browser->hists);
2859 hist_browser__reset(browser);
2864 add_socket_opt(struct hist_browser *browser, struct popup_action *act,
2865 char **optstr, int socket_id)
2867 if (!hists__has(browser->hists, socket) || socket_id < 0)
2870 if (asprintf(optstr, "Zoom %s Processor Socket %d",
2871 (browser->hists->socket_filter > -1) ? "out of" : "into",
2875 act->socket = socket_id;
2876 act->fn = do_zoom_socket;
2880 static void hist_browser__update_nr_entries(struct hist_browser *hb)
2883 struct rb_node *nd = rb_first_cached(&hb->hists->entries);
2885 if (hb->min_pcnt == 0 && !symbol_conf.report_hierarchy) {
2886 hb->nr_non_filtered_entries = hb->hists->nr_non_filtered_entries;
2890 while ((nd = hists__filter_entries(nd, hb->min_pcnt)) != NULL) {
2892 nd = rb_hierarchy_next(nd);
2895 hb->nr_non_filtered_entries = nr_entries;
2896 hb->nr_hierarchy_entries = nr_entries;
2899 static void hist_browser__update_percent_limit(struct hist_browser *hb,
2902 struct hist_entry *he;
2903 struct rb_node *nd = rb_first_cached(&hb->hists->entries);
2904 u64 total = hists__total_period(hb->hists);
2905 u64 min_callchain_hits = total * (percent / 100);
2907 hb->min_pcnt = callchain_param.min_percent = percent;
2909 while ((nd = hists__filter_entries(nd, hb->min_pcnt)) != NULL) {
2910 he = rb_entry(nd, struct hist_entry, rb_node);
2912 if (he->has_no_entry) {
2913 he->has_no_entry = false;
2917 if (!he->leaf || !hist_entry__has_callchains(he) || !symbol_conf.use_callchain)
2920 if (callchain_param.mode == CHAIN_GRAPH_REL) {
2921 total = he->stat.period;
2923 if (symbol_conf.cumulate_callchain)
2924 total = he->stat_acc->period;
2926 min_callchain_hits = total * (percent / 100);
2929 callchain_param.sort(&he->sorted_chain, he->callchain,
2930 min_callchain_hits, &callchain_param);
2933 nd = __rb_hierarchy_next(nd, HMD_FORCE_CHILD);
2935 /* force to re-evaluate folding state of callchains */
2936 he->init_have_children = false;
2937 hist_entry__set_folding(he, hb, false);
2941 static int evsel__hists_browse(struct evsel *evsel, int nr_events, const char *helpline,
2942 bool left_exits, struct hist_browser_timer *hbt, float min_pcnt,
2943 struct perf_env *env, bool warn_lost_event)
2945 struct hists *hists = evsel__hists(evsel);
2946 struct hist_browser *browser = perf_evsel_browser__new(evsel, hbt, env);
2947 struct branch_info *bi = NULL;
2948 #define MAX_OPTIONS 16
2949 char *options[MAX_OPTIONS];
2950 struct popup_action actions[MAX_OPTIONS];
2954 int delay_secs = hbt ? hbt->refresh : 0;
2956 #define HIST_BROWSER_HELP_COMMON \
2957 "h/?/F1 Show this window\n" \
2959 "PGDN/SPACE Navigate\n" \
2960 "q/ESC/CTRL+C Exit browser or go back to previous screen\n\n" \
2961 "For multiple event sessions:\n\n" \
2962 "TAB/UNTAB Switch events\n\n" \
2963 "For symbolic views (--sort has sym):\n\n" \
2964 "ENTER Zoom into DSO/Threads & Annotate current symbol\n" \
2966 "+ Expand/Collapse one callchain level\n" \
2967 "a Annotate current symbol\n" \
2968 "C Collapse all callchains\n" \
2969 "d Zoom into current DSO\n" \
2970 "e Expand/Collapse main entry callchains\n" \
2971 "E Expand all callchains\n" \
2972 "F Toggle percentage of filtered entries\n" \
2973 "H Display column headers\n" \
2974 "k Zoom into the kernel map\n" \
2975 "L Change percent limit\n" \
2976 "m Display context menu\n" \
2977 "S Zoom into current Processor Socket\n" \
2979 /* help messages are sorted by lexical order of the hotkey */
2980 static const char report_help[] = HIST_BROWSER_HELP_COMMON
2981 "i Show header information\n"
2982 "P Print histograms to perf.hist.N\n"
2983 "r Run available scripts\n"
2984 "s Switch to another data file in PWD\n"
2985 "t Zoom into current Thread\n"
2986 "V Verbose (DSO names in callchains, etc)\n"
2987 "/ Filter symbol by name\n"
2988 "0-9 Sort by event n in group";
2989 static const char top_help[] = HIST_BROWSER_HELP_COMMON
2990 "P Print histograms to perf.hist.N\n"
2991 "t Zoom into current Thread\n"
2992 "V Verbose (DSO names in callchains, etc)\n"
2993 "z Toggle zeroing of samples\n"
2994 "f Enable/Disable events\n"
2995 "/ Filter symbol by name";
2997 if (browser == NULL)
3000 /* reset abort key so that it can get Ctrl-C as a key */
3002 SLang_init_tty(0, 0, 0);
3003 SLtty_set_suspend_state(true);
3006 browser->min_pcnt = min_pcnt;
3007 hist_browser__update_nr_entries(browser);
3009 browser->pstack = pstack__new(3);
3010 if (browser->pstack == NULL)
3013 ui_helpline__push(helpline);
3015 memset(options, 0, sizeof(options));
3016 memset(actions, 0, sizeof(actions));
3018 if (symbol_conf.col_width_list_str)
3019 perf_hpp__set_user_width(symbol_conf.col_width_list_str);
3021 if (!is_report_browser(hbt))
3022 browser->b.no_samples_msg = "Collecting samples...";
3025 struct thread *thread = NULL;
3026 struct map *map = NULL;
3030 key = 0; // reset key
3031 do_hotkey: // key came straight from options ui__popup_menu()
3032 choice = nr_options = 0;
3033 key = hist_browser__run(browser, helpline, warn_lost_event, key);
3035 if (browser->he_selection != NULL) {
3036 thread = hist_browser__selected_thread(browser);
3037 map = browser->selection->map;
3038 socked_id = browser->he_selection->socket;
3046 * Exit the browser, let hists__browser_tree
3047 * go to the next or previous
3049 goto out_free_stack;
3051 if (!symbol_conf.event_group ||
3052 evsel->core.nr_members < 2) {
3053 snprintf(buf, sizeof(buf),
3054 "Sort by index only available with group events!");
3059 if (key - '0' == symbol_conf.group_sort_idx)
3062 symbol_conf.group_sort_idx = key - '0';
3064 if (symbol_conf.group_sort_idx >= evsel->core.nr_members) {
3065 snprintf(buf, sizeof(buf),
3066 "Max event group index to sort is %d (index from 0 to %d)",
3067 evsel->core.nr_members - 1,
3068 evsel->core.nr_members - 1);
3074 goto out_free_stack;
3076 if (!hists__has(hists, sym)) {
3077 ui_browser__warning(&browser->b, delay_secs * 2,
3078 "Annotation is only available for symbolic views, "
3079 "include \"sym*\" in --sort to use it.");
3083 if (!browser->selection ||
3084 !browser->selection->map ||
3085 !map__dso(browser->selection->map) ||
3086 map__dso(browser->selection->map)->annotate_warned) {
3090 if (!browser->selection->sym) {
3091 if (!browser->he_selection)
3094 if (sort__mode == SORT_MODE__BRANCH) {
3095 bi = browser->he_selection->branch_info;
3096 if (!bi || !bi->to.ms.map)
3099 actions->ms.sym = symbol__new_unresolved(bi->to.al_addr, bi->to.ms.map);
3100 actions->ms.map = bi->to.ms.map;
3102 actions->ms.sym = symbol__new_unresolved(browser->he_selection->ip,
3103 browser->selection->map);
3104 actions->ms.map = browser->selection->map;
3107 if (!actions->ms.sym)
3110 if (symbol__annotation(browser->selection->sym)->src == NULL) {
3111 ui_browser__warning(&browser->b, delay_secs * 2,
3112 "No samples for the \"%s\" symbol.\n\n"
3113 "Probably appeared just in a callchain",
3114 browser->selection->sym->name);
3118 actions->ms.map = browser->selection->map;
3119 actions->ms.sym = browser->selection->sym;
3122 do_annotate(browser, actions);
3125 hist_browser__dump(browser);
3128 actions->ms.map = map;
3129 do_zoom_dso(browser, actions);
3132 if (browser->selection != NULL)
3133 hists_browser__zoom_map(browser,
3134 maps__machine(browser->selection->maps)->vmlinux_map);
3137 verbose = (verbose + 1) % 4;
3138 browser->show_dso = verbose > 0;
3139 ui_helpline__fpush("Verbosity level set to %d\n",
3143 actions->thread = thread;
3144 do_zoom_thread(browser, actions);
3147 actions->socket = socked_id;
3148 do_zoom_socket(browser, actions);
3151 if (ui_browser__input_window("Symbol to show",
3152 "Please enter the name of symbol you want to see.\n"
3153 "To remove the filter later, press / + ENTER.",
3154 buf, "ENTER: OK, ESC: Cancel",
3155 delay_secs * 2) == K_ENTER) {
3156 hists->symbol_filter_str = *buf ? buf : NULL;
3157 hists__filter_by_symbol(hists);
3158 hist_browser__reset(browser);
3162 if (is_report_browser(hbt)) {
3163 actions->thread = NULL;
3164 actions->ms.sym = NULL;
3165 do_run_script(browser, actions);
3169 if (is_report_browser(hbt)) {
3170 key = do_switch_data(browser, actions);
3171 if (key == K_SWITCH_INPUT_DATA)
3172 goto out_free_stack;
3176 /* env->arch is NULL for live-mode (i.e. perf top) */
3178 tui__header_window(env);
3181 symbol_conf.filter_relative ^= 1;
3184 if (!is_report_browser(hbt)) {
3185 struct perf_top *top = hbt->arg;
3187 top->zero = !top->zero;
3191 if (ui_browser__input_window("Percent Limit",
3192 "Please enter the value you want to hide entries under that percent.",
3193 buf, "ENTER: OK, ESC: Cancel",
3194 delay_secs * 2) == K_ENTER) {
3196 double new_percent = strtod(buf, &end);
3198 if (new_percent < 0 || new_percent > 100) {
3199 ui_browser__warning(&browser->b, delay_secs * 2,
3200 "Invalid percent: %.2f", new_percent);
3204 hist_browser__update_percent_limit(browser, new_percent);
3205 hist_browser__reset(browser);
3211 ui_browser__help_window(&browser->b,
3212 is_report_browser(hbt) ? report_help : top_help);
3223 if (pstack__empty(browser->pstack)) {
3225 * Go back to the perf_evsel_menu__run or other user
3228 goto out_free_stack;
3231 ui_browser__dialog_yesno(&browser->b,
3232 "Do you really want to exit?"))
3233 goto out_free_stack;
3237 actions->ms.map = map;
3238 top = pstack__peek(browser->pstack);
3239 if (top == &browser->hists->dso_filter) {
3241 * No need to set actions->dso here since
3242 * it's just to remove the current filter.
3243 * Ditto for thread below.
3245 do_zoom_dso(browser, actions);
3246 } else if (top == &browser->hists->thread_filter) {
3247 do_zoom_thread(browser, actions);
3248 } else if (top == &browser->hists->socket_filter) {
3249 do_zoom_socket(browser, actions);
3255 goto out_free_stack;
3257 if (!is_report_browser(hbt)) {
3258 struct perf_top *top = hbt->arg;
3260 evlist__toggle_enable(top->evlist);
3262 * No need to refresh, resort/decay histogram
3263 * entries if we are not collecting samples:
3265 if (top->evlist->enabled) {
3266 helpline = "Press 'f' to disable the events or 'h' to see other hotkeys";
3267 hbt->refresh = delay_secs;
3269 helpline = "Press 'f' again to re-enable the events";
3276 helpline = "Press '?' for help on key bindings";
3280 if (!hists__has(hists, sym) || browser->selection == NULL)
3281 goto skip_annotation;
3283 if (sort__mode == SORT_MODE__BRANCH) {
3285 if (browser->he_selection)
3286 bi = browser->he_selection->branch_info;
3289 goto skip_annotation;
3291 nr_options += add_annotate_opt(browser,
3292 &actions[nr_options],
3293 &options[nr_options],
3296 if (bi->to.ms.sym != bi->from.ms.sym)
3297 nr_options += add_annotate_opt(browser,
3298 &actions[nr_options],
3299 &options[nr_options],
3302 } else if (browser->he_selection) {
3303 nr_options += add_annotate_opt(browser,
3304 &actions[nr_options],
3305 &options[nr_options],
3307 browser->he_selection->ip);
3310 nr_options += add_thread_opt(browser, &actions[nr_options],
3311 &options[nr_options], thread);
3312 nr_options += add_dso_opt(browser, &actions[nr_options],
3313 &options[nr_options], map);
3314 nr_options += add_callchain_toggle_opt(browser, &actions[nr_options], &options[nr_options]);
3315 nr_options += add_map_opt(browser, &actions[nr_options],
3316 &options[nr_options],
3317 browser->selection ?
3318 browser->selection->map : NULL);
3319 nr_options += add_socket_opt(browser, &actions[nr_options],
3320 &options[nr_options],
3322 /* perf script support */
3323 if (!is_report_browser(hbt))
3324 goto skip_scripting;
3326 if (browser->he_selection) {
3327 if (hists__has(hists, thread) && thread) {
3328 nr_options += add_script_opt(browser,
3329 &actions[nr_options],
3330 &options[nr_options],
3331 thread, NULL, evsel);
3334 * Note that browser->selection != NULL
3335 * when browser->he_selection is not NULL,
3336 * so we don't need to check browser->selection
3337 * before fetching browser->selection->sym like what
3338 * we do before fetching browser->selection->map.
3340 * See hist_browser__show_entry.
3342 if (hists__has(hists, sym) && browser->selection->sym) {
3343 nr_options += add_script_opt(browser,
3344 &actions[nr_options],
3345 &options[nr_options],
3346 NULL, browser->selection->sym,
3350 nr_options += add_script_opt(browser, &actions[nr_options],
3351 &options[nr_options], NULL, NULL, evsel);
3352 nr_options += add_res_sample_opt(browser, &actions[nr_options],
3353 &options[nr_options],
3354 hist_browser__selected_res_sample(browser),
3356 nr_options += add_res_sample_opt(browser, &actions[nr_options],
3357 &options[nr_options],
3358 hist_browser__selected_res_sample(browser),
3360 nr_options += add_res_sample_opt(browser, &actions[nr_options],
3361 &options[nr_options],
3362 hist_browser__selected_res_sample(browser),
3364 nr_options += add_switch_opt(browser, &actions[nr_options],
3365 &options[nr_options]);
3367 nr_options += add_exit_opt(browser, &actions[nr_options],
3368 &options[nr_options]);
3371 struct popup_action *act;
3373 choice = ui__popup_menu(nr_options, options, &key);
3377 if (choice == nr_options)
3380 act = &actions[choice];
3381 key = act->fn(browser, act);
3384 if (key == K_SWITCH_INPUT_DATA)
3388 pstack__delete(browser->pstack);
3390 hist_browser__delete(browser);
3391 free_popup_options(options, MAX_OPTIONS);
3396 struct ui_browser b;
3397 struct evsel *selection;
3398 bool lost_events, lost_events_warned;
3400 struct perf_env *env;
3403 static void perf_evsel_menu__write(struct ui_browser *browser,
3404 void *entry, int row)
3406 struct evsel_menu *menu = container_of(browser,
3407 struct evsel_menu, b);
3408 struct evsel *evsel = list_entry(entry, struct evsel, core.node);
3409 struct hists *hists = evsel__hists(evsel);
3410 bool current_entry = ui_browser__is_current_entry(browser, row);
3411 unsigned long nr_events = hists->stats.nr_samples;
3412 const char *ev_name = evsel__name(evsel);
3414 const char *warn = " ";
3417 ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
3418 HE_COLORSET_NORMAL);
3420 if (evsel__is_group_event(evsel)) {
3423 ev_name = evsel__group_name(evsel);
3425 for_each_group_member(pos, evsel) {
3426 struct hists *pos_hists = evsel__hists(pos);
3427 nr_events += pos_hists->stats.nr_samples;
3431 nr_events = convert_unit(nr_events, &unit);
3432 printed = scnprintf(bf, sizeof(bf), "%lu%c%s%s", nr_events,
3433 unit, unit == ' ' ? "" : " ", ev_name);
3434 ui_browser__printf(browser, "%s", bf);
3436 nr_events = evsel->evlist->stats.nr_events[PERF_RECORD_LOST];
3437 if (nr_events != 0) {
3438 menu->lost_events = true;
3440 ui_browser__set_color(browser, HE_COLORSET_TOP);
3441 nr_events = convert_unit(nr_events, &unit);
3442 printed += scnprintf(bf, sizeof(bf), ": %ld%c%schunks LOST!",
3443 nr_events, unit, unit == ' ' ? "" : " ");
3447 ui_browser__write_nstring(browser, warn, browser->width - printed);
3450 menu->selection = evsel;
3453 static int perf_evsel_menu__run(struct evsel_menu *menu,
3454 int nr_events, const char *help,
3455 struct hist_browser_timer *hbt,
3456 bool warn_lost_event)
3458 struct evlist *evlist = menu->b.priv;
3460 const char *title = "Available samples";
3461 int delay_secs = hbt ? hbt->refresh : 0;
3464 if (ui_browser__show(&menu->b, title,
3465 "ESC: exit, ENTER|->: Browse histograms") < 0)
3469 key = ui_browser__run(&menu->b, delay_secs);
3474 hbt->timer(hbt->arg);
3476 if (!menu->lost_events_warned &&
3477 menu->lost_events &&
3479 ui_browser__warn_lost_events(&menu->b);
3480 menu->lost_events_warned = true;
3485 if (!menu->selection)
3487 pos = menu->selection;
3489 evlist__set_selected(evlist, pos);
3491 * Give the calling tool a chance to populate the non
3492 * default evsel resorted hists tree.
3495 hbt->timer(hbt->arg);
3496 key = evsel__hists_browse(pos, nr_events, help, true, hbt,
3497 menu->min_pcnt, menu->env,
3499 ui_browser__show_title(&menu->b, title);
3502 if (pos->core.node.next == &evlist->core.entries)
3503 pos = evlist__first(evlist);
3505 pos = evsel__next(pos);
3508 if (pos->core.node.prev == &evlist->core.entries)
3509 pos = evlist__last(evlist);
3511 pos = evsel__prev(pos);
3513 case K_SWITCH_INPUT_DATA:
3525 if (!ui_browser__dialog_yesno(&menu->b,
3526 "Do you really want to exit?"))
3538 ui_browser__hide(&menu->b);
3542 static bool filter_group_entries(struct ui_browser *browser __maybe_unused,
3545 struct evsel *evsel = list_entry(entry, struct evsel, core.node);
3547 if (symbol_conf.event_group && !evsel__is_group_leader(evsel))
3553 static int __evlist__tui_browse_hists(struct evlist *evlist, int nr_entries, const char *help,
3554 struct hist_browser_timer *hbt, float min_pcnt, struct perf_env *env,
3555 bool warn_lost_event)
3558 struct evsel_menu menu = {
3560 .entries = &evlist->core.entries,
3561 .refresh = ui_browser__list_head_refresh,
3562 .seek = ui_browser__list_head_seek,
3563 .write = perf_evsel_menu__write,
3564 .filter = filter_group_entries,
3565 .nr_entries = nr_entries,
3568 .min_pcnt = min_pcnt,
3572 ui_helpline__push("Press ESC to exit");
3574 evlist__for_each_entry(evlist, pos) {
3575 const char *ev_name = evsel__name(pos);
3576 size_t line_len = strlen(ev_name) + 7;
3578 if (menu.b.width < line_len)
3579 menu.b.width = line_len;
3582 return perf_evsel_menu__run(&menu, nr_entries, help,
3583 hbt, warn_lost_event);
3586 static bool evlist__single_entry(struct evlist *evlist)
3588 int nr_entries = evlist->core.nr_entries;
3590 if (nr_entries == 1)
3593 if (nr_entries == 2) {
3594 struct evsel *last = evlist__last(evlist);
3596 if (evsel__is_dummy_event(last))
3603 int evlist__tui_browse_hists(struct evlist *evlist, const char *help, struct hist_browser_timer *hbt,
3604 float min_pcnt, struct perf_env *env, bool warn_lost_event)
3606 int nr_entries = evlist->core.nr_entries;
3608 if (evlist__single_entry(evlist)) {
3610 struct evsel *first = evlist__first(evlist);
3612 return evsel__hists_browse(first, nr_entries, help, false, hbt, min_pcnt,
3613 env, warn_lost_event);
3617 if (symbol_conf.event_group) {
3621 evlist__for_each_entry(evlist, pos) {
3622 if (evsel__is_group_leader(pos))
3626 if (nr_entries == 1)
3630 return __evlist__tui_browse_hists(evlist, nr_entries, help, hbt, min_pcnt, env,
3634 static int block_hists_browser__title(struct hist_browser *browser, char *bf,
3637 struct hists *hists = evsel__hists(browser->block_evsel);
3638 const char *evname = evsel__name(browser->block_evsel);
3639 unsigned long nr_samples = hists->stats.nr_samples;
3642 ret = scnprintf(bf, size, "# Samples: %lu", nr_samples);
3644 scnprintf(bf + ret, size - ret, " of event '%s'", evname);
3649 int block_hists_tui_browse(struct block_hist *bh, struct evsel *evsel,
3650 float min_percent, struct perf_env *env)
3652 struct hists *hists = &bh->block_hists;
3653 struct hist_browser *browser;
3655 struct popup_action action;
3656 static const char help[] =
3659 browser = hist_browser__new(hists);
3663 browser->block_evsel = evsel;
3664 browser->title = block_hists_browser__title;
3665 browser->min_pcnt = min_percent;
3668 /* reset abort key so that it can get Ctrl-C as a key */
3670 SLang_init_tty(0, 0, 0);
3671 SLtty_set_suspend_state(true);
3673 memset(&action, 0, sizeof(action));
3676 key = hist_browser__run(browser, "? - help", true, 0);
3682 ui_browser__help_window(&browser->b, help);
3686 if (!browser->selection ||
3687 !browser->selection->sym) {
3691 action.ms.map = browser->selection->map;
3692 action.ms.sym = browser->selection->sym;
3693 do_annotate(browser, &action);
3701 hist_browser__delete(browser);