1 // SPDX-License-Identifier: GPL-2.0
8 #include <linux/rbtree.h>
9 #include <sys/ttydefaults.h>
11 #include "../../util/evsel.h"
12 #include "../../util/evlist.h"
13 #include "../../util/hist.h"
14 #include "../../util/pstack.h"
15 #include "../../util/sort.h"
16 #include "../../util/util.h"
17 #include "../../util/top.h"
18 #include "../../util/thread.h"
19 #include "../../arch/common.h"
21 #include "../browsers/hists.h"
22 #include "../helpline.h"
31 #include "sane_ctype.h"
33 extern void hist_browser__init_hpp(void);
35 static int perf_evsel_browser_title(struct hist_browser
*browser
,
36 char *bf
, size_t size
);
37 static void hist_browser__update_nr_entries(struct hist_browser
*hb
);
39 static struct rb_node
*hists__filter_entries(struct rb_node
*nd
,
42 static bool hist_browser__has_filter(struct hist_browser
*hb
)
44 return hists__has_filter(hb
->hists
) || hb
->min_pcnt
|| symbol_conf
.has_filter
|| hb
->c2c_filter
;
47 static int hist_browser__get_folding(struct hist_browser
*browser
)
50 struct hists
*hists
= browser
->hists
;
51 int unfolded_rows
= 0;
53 for (nd
= rb_first(&hists
->entries
);
54 (nd
= hists__filter_entries(nd
, browser
->min_pcnt
)) != NULL
;
55 nd
= rb_hierarchy_next(nd
)) {
56 struct hist_entry
*he
=
57 rb_entry(nd
, struct hist_entry
, rb_node
);
59 if (he
->leaf
&& he
->unfolded
)
60 unfolded_rows
+= he
->nr_rows
;
65 static u32
hist_browser__nr_entries(struct hist_browser
*hb
)
69 if (symbol_conf
.report_hierarchy
)
70 nr_entries
= hb
->nr_hierarchy_entries
;
71 else if (hist_browser__has_filter(hb
))
72 nr_entries
= hb
->nr_non_filtered_entries
;
74 nr_entries
= hb
->hists
->nr_entries
;
76 hb
->nr_callchain_rows
= hist_browser__get_folding(hb
);
77 return nr_entries
+ hb
->nr_callchain_rows
;
80 static void hist_browser__update_rows(struct hist_browser
*hb
)
82 struct ui_browser
*browser
= &hb
->b
;
83 struct hists
*hists
= hb
->hists
;
84 struct perf_hpp_list
*hpp_list
= hists
->hpp_list
;
85 u16 header_offset
, index_row
;
87 header_offset
= hb
->show_headers
? hpp_list
->nr_header_lines
: 0;
88 browser
->rows
= browser
->height
- header_offset
;
90 * Verify if we were at the last line and that line isn't
91 * visibe because we now show the header line(s).
93 index_row
= browser
->index
- browser
->top_idx
;
94 if (index_row
>= browser
->rows
)
95 browser
->index
-= index_row
- browser
->rows
+ 1;
98 static void hist_browser__refresh_dimensions(struct ui_browser
*browser
)
100 struct hist_browser
*hb
= container_of(browser
, struct hist_browser
, b
);
102 /* 3 == +/- toggle symbol before actual hist_entry rendering */
103 browser
->width
= 3 + (hists__sort_list_width(hb
->hists
) + sizeof("[k]"));
105 * FIXME: Just keeping existing behaviour, but this really should be
106 * before updating browser->width, as it will invalidate the
107 * calculation above. Fix this and the fallout in another
110 ui_browser__refresh_dimensions(browser
);
111 hist_browser__update_rows(hb
);
114 static void hist_browser__gotorc(struct hist_browser
*browser
, int row
, int column
)
116 struct hists
*hists
= browser
->hists
;
117 struct perf_hpp_list
*hpp_list
= hists
->hpp_list
;
120 header_offset
= browser
->show_headers
? hpp_list
->nr_header_lines
: 0;
121 ui_browser__gotorc(&browser
->b
, row
+ header_offset
, column
);
124 static void hist_browser__reset(struct hist_browser
*browser
)
127 * The hists__remove_entry_filter() already folds non-filtered
128 * entries so we can assume it has 0 callchain rows.
130 browser
->nr_callchain_rows
= 0;
132 hist_browser__update_nr_entries(browser
);
133 browser
->b
.nr_entries
= hist_browser__nr_entries(browser
);
134 hist_browser__refresh_dimensions(&browser
->b
);
135 ui_browser__reset_index(&browser
->b
);
138 static char tree__folded_sign(bool unfolded
)
140 return unfolded
? '-' : '+';
143 static char hist_entry__folded(const struct hist_entry
*he
)
145 return he
->has_children
? tree__folded_sign(he
->unfolded
) : ' ';
148 static char callchain_list__folded(const struct callchain_list
*cl
)
150 return cl
->has_children
? tree__folded_sign(cl
->unfolded
) : ' ';
153 static void callchain_list__set_folding(struct callchain_list
*cl
, bool unfold
)
155 cl
->unfolded
= unfold
? cl
->has_children
: false;
158 static int callchain_node__count_rows_rb_tree(struct callchain_node
*node
)
163 for (nd
= rb_first(&node
->rb_root
); nd
; nd
= rb_next(nd
)) {
164 struct callchain_node
*child
= rb_entry(nd
, struct callchain_node
, rb_node
);
165 struct callchain_list
*chain
;
166 char folded_sign
= ' '; /* No children */
168 list_for_each_entry(chain
, &child
->val
, list
) {
171 /* We need this because we may not have children */
172 folded_sign
= callchain_list__folded(chain
);
173 if (folded_sign
== '+')
177 if (folded_sign
== '-') /* Have children and they're unfolded */
178 n
+= callchain_node__count_rows_rb_tree(child
);
184 static int callchain_node__count_flat_rows(struct callchain_node
*node
)
186 struct callchain_list
*chain
;
187 char folded_sign
= 0;
190 list_for_each_entry(chain
, &node
->parent_val
, list
) {
192 /* only check first chain list entry */
193 folded_sign
= callchain_list__folded(chain
);
194 if (folded_sign
== '+')
200 list_for_each_entry(chain
, &node
->val
, list
) {
202 /* node->parent_val list might be empty */
203 folded_sign
= callchain_list__folded(chain
);
204 if (folded_sign
== '+')
213 static int callchain_node__count_folded_rows(struct callchain_node
*node __maybe_unused
)
218 static int callchain_node__count_rows(struct callchain_node
*node
)
220 struct callchain_list
*chain
;
221 bool unfolded
= false;
224 if (callchain_param
.mode
== CHAIN_FLAT
)
225 return callchain_node__count_flat_rows(node
);
226 else if (callchain_param
.mode
== CHAIN_FOLDED
)
227 return callchain_node__count_folded_rows(node
);
229 list_for_each_entry(chain
, &node
->val
, list
) {
232 unfolded
= chain
->unfolded
;
236 n
+= callchain_node__count_rows_rb_tree(node
);
241 static int callchain__count_rows(struct rb_root
*chain
)
246 for (nd
= rb_first(chain
); nd
; nd
= rb_next(nd
)) {
247 struct callchain_node
*node
= rb_entry(nd
, struct callchain_node
, rb_node
);
248 n
+= callchain_node__count_rows(node
);
254 static int hierarchy_count_rows(struct hist_browser
*hb
, struct hist_entry
*he
,
255 bool include_children
)
258 struct rb_node
*node
;
259 struct hist_entry
*child
;
262 return callchain__count_rows(&he
->sorted_chain
);
264 if (he
->has_no_entry
)
267 node
= rb_first(&he
->hroot_out
);
271 child
= rb_entry(node
, struct hist_entry
, rb_node
);
272 percent
= hist_entry__get_percent_limit(child
);
274 if (!child
->filtered
&& percent
>= hb
->min_pcnt
) {
277 if (include_children
&& child
->unfolded
)
278 count
+= hierarchy_count_rows(hb
, child
, true);
281 node
= rb_next(node
);
286 static bool hist_entry__toggle_fold(struct hist_entry
*he
)
291 if (!he
->has_children
)
294 he
->unfolded
= !he
->unfolded
;
298 static bool callchain_list__toggle_fold(struct callchain_list
*cl
)
303 if (!cl
->has_children
)
306 cl
->unfolded
= !cl
->unfolded
;
310 static void callchain_node__init_have_children_rb_tree(struct callchain_node
*node
)
312 struct rb_node
*nd
= rb_first(&node
->rb_root
);
314 for (nd
= rb_first(&node
->rb_root
); nd
; nd
= rb_next(nd
)) {
315 struct callchain_node
*child
= rb_entry(nd
, struct callchain_node
, rb_node
);
316 struct callchain_list
*chain
;
319 list_for_each_entry(chain
, &child
->val
, list
) {
322 chain
->has_children
= chain
->list
.next
!= &child
->val
||
323 !RB_EMPTY_ROOT(&child
->rb_root
);
325 chain
->has_children
= chain
->list
.next
== &child
->val
&&
326 !RB_EMPTY_ROOT(&child
->rb_root
);
329 callchain_node__init_have_children_rb_tree(child
);
333 static void callchain_node__init_have_children(struct callchain_node
*node
,
336 struct callchain_list
*chain
;
338 chain
= list_entry(node
->val
.next
, struct callchain_list
, list
);
339 chain
->has_children
= has_sibling
;
341 if (!list_empty(&node
->val
)) {
342 chain
= list_entry(node
->val
.prev
, struct callchain_list
, list
);
343 chain
->has_children
= !RB_EMPTY_ROOT(&node
->rb_root
);
346 callchain_node__init_have_children_rb_tree(node
);
349 static void callchain__init_have_children(struct rb_root
*root
)
351 struct rb_node
*nd
= rb_first(root
);
352 bool has_sibling
= nd
&& rb_next(nd
);
354 for (nd
= rb_first(root
); nd
; nd
= rb_next(nd
)) {
355 struct callchain_node
*node
= rb_entry(nd
, struct callchain_node
, rb_node
);
356 callchain_node__init_have_children(node
, has_sibling
);
357 if (callchain_param
.mode
== CHAIN_FLAT
||
358 callchain_param
.mode
== CHAIN_FOLDED
)
359 callchain_node__make_parent_list(node
);
363 static void hist_entry__init_have_children(struct hist_entry
*he
)
365 if (he
->init_have_children
)
369 he
->has_children
= !RB_EMPTY_ROOT(&he
->sorted_chain
);
370 callchain__init_have_children(&he
->sorted_chain
);
372 he
->has_children
= !RB_EMPTY_ROOT(&he
->hroot_out
);
375 he
->init_have_children
= true;
378 static bool hist_browser__toggle_fold(struct hist_browser
*browser
)
380 struct hist_entry
*he
= browser
->he_selection
;
381 struct map_symbol
*ms
= browser
->selection
;
382 struct callchain_list
*cl
= container_of(ms
, struct callchain_list
, ms
);
389 has_children
= hist_entry__toggle_fold(he
);
391 has_children
= callchain_list__toggle_fold(cl
);
396 hist_entry__init_have_children(he
);
397 browser
->b
.nr_entries
-= he
->nr_rows
;
400 browser
->nr_callchain_rows
-= he
->nr_rows
;
402 browser
->nr_hierarchy_entries
-= he
->nr_rows
;
404 if (symbol_conf
.report_hierarchy
)
405 child_rows
= hierarchy_count_rows(browser
, he
, true);
409 he
->nr_rows
= callchain__count_rows(
412 he
->nr_rows
= hierarchy_count_rows(browser
, he
, false);
414 /* account grand children */
415 if (symbol_conf
.report_hierarchy
)
416 browser
->b
.nr_entries
+= child_rows
- he
->nr_rows
;
418 if (!he
->leaf
&& he
->nr_rows
== 0) {
419 he
->has_no_entry
= true;
423 if (symbol_conf
.report_hierarchy
)
424 browser
->b
.nr_entries
-= child_rows
- he
->nr_rows
;
426 if (he
->has_no_entry
)
427 he
->has_no_entry
= false;
432 browser
->b
.nr_entries
+= he
->nr_rows
;
435 browser
->nr_callchain_rows
+= he
->nr_rows
;
437 browser
->nr_hierarchy_entries
+= he
->nr_rows
;
442 /* If it doesn't have children, no toggling performed */
446 static int callchain_node__set_folding_rb_tree(struct callchain_node
*node
, bool unfold
)
451 for (nd
= rb_first(&node
->rb_root
); nd
; nd
= rb_next(nd
)) {
452 struct callchain_node
*child
= rb_entry(nd
, struct callchain_node
, rb_node
);
453 struct callchain_list
*chain
;
454 bool has_children
= false;
456 list_for_each_entry(chain
, &child
->val
, list
) {
458 callchain_list__set_folding(chain
, unfold
);
459 has_children
= chain
->has_children
;
463 n
+= callchain_node__set_folding_rb_tree(child
, unfold
);
469 static int callchain_node__set_folding(struct callchain_node
*node
, bool unfold
)
471 struct callchain_list
*chain
;
472 bool has_children
= false;
475 list_for_each_entry(chain
, &node
->val
, list
) {
477 callchain_list__set_folding(chain
, unfold
);
478 has_children
= chain
->has_children
;
482 n
+= callchain_node__set_folding_rb_tree(node
, unfold
);
487 static int callchain__set_folding(struct rb_root
*chain
, bool unfold
)
492 for (nd
= rb_first(chain
); nd
; nd
= rb_next(nd
)) {
493 struct callchain_node
*node
= rb_entry(nd
, struct callchain_node
, rb_node
);
494 n
+= callchain_node__set_folding(node
, unfold
);
500 static int hierarchy_set_folding(struct hist_browser
*hb
, struct hist_entry
*he
,
501 bool unfold __maybe_unused
)
505 struct hist_entry
*child
;
508 for (nd
= rb_first(&he
->hroot_out
); nd
; nd
= rb_next(nd
)) {
509 child
= rb_entry(nd
, struct hist_entry
, rb_node
);
510 percent
= hist_entry__get_percent_limit(child
);
511 if (!child
->filtered
&& percent
>= hb
->min_pcnt
)
518 static void __hist_entry__set_folding(struct hist_entry
*he
,
519 struct hist_browser
*hb
, bool unfold
)
521 hist_entry__init_have_children(he
);
522 he
->unfolded
= unfold
? he
->has_children
: false;
524 if (he
->has_children
) {
528 n
= callchain__set_folding(&he
->sorted_chain
, unfold
);
530 n
= hierarchy_set_folding(hb
, he
, unfold
);
532 he
->nr_rows
= unfold
? n
: 0;
537 static void hist_entry__set_folding(struct hist_entry
*he
,
538 struct hist_browser
*browser
, bool unfold
)
542 percent
= hist_entry__get_percent_limit(he
);
543 if (he
->filtered
|| percent
< browser
->min_pcnt
)
546 __hist_entry__set_folding(he
, browser
, unfold
);
548 if (!he
->depth
|| unfold
)
549 browser
->nr_hierarchy_entries
++;
551 browser
->nr_callchain_rows
+= he
->nr_rows
;
552 else if (unfold
&& !hist_entry__has_hierarchy_children(he
, browser
->min_pcnt
)) {
553 browser
->nr_hierarchy_entries
++;
554 he
->has_no_entry
= true;
557 he
->has_no_entry
= false;
561 __hist_browser__set_folding(struct hist_browser
*browser
, bool unfold
)
564 struct hist_entry
*he
;
566 nd
= rb_first(&browser
->hists
->entries
);
568 he
= rb_entry(nd
, struct hist_entry
, rb_node
);
570 /* set folding state even if it's currently folded */
571 nd
= __rb_hierarchy_next(nd
, HMD_FORCE_CHILD
);
573 hist_entry__set_folding(he
, browser
, unfold
);
577 static void hist_browser__set_folding(struct hist_browser
*browser
, bool unfold
)
579 browser
->nr_hierarchy_entries
= 0;
580 browser
->nr_callchain_rows
= 0;
581 __hist_browser__set_folding(browser
, unfold
);
583 browser
->b
.nr_entries
= hist_browser__nr_entries(browser
);
584 /* Go to the start, we may be way after valid entries after a collapse */
585 ui_browser__reset_index(&browser
->b
);
588 static void hist_browser__set_folding_selected(struct hist_browser
*browser
, bool unfold
)
590 if (!browser
->he_selection
)
593 hist_entry__set_folding(browser
->he_selection
, browser
, unfold
);
594 browser
->b
.nr_entries
= hist_browser__nr_entries(browser
);
597 static void ui_browser__warn_lost_events(struct ui_browser
*browser
)
599 ui_browser__warning(browser
, 4,
600 "Events are being lost, check IO/CPU overload!\n\n"
601 "You may want to run 'perf' using a RT scheduler policy:\n\n"
602 " perf top -r 80\n\n"
603 "Or reduce the sampling frequency.");
606 static int hist_browser__title(struct hist_browser
*browser
, char *bf
, size_t size
)
608 return browser
->title
? browser
->title(browser
, bf
, size
) : 0;
611 int hist_browser__run(struct hist_browser
*browser
, const char *help
)
615 struct hist_browser_timer
*hbt
= browser
->hbt
;
616 int delay_secs
= hbt
? hbt
->refresh
: 0;
618 browser
->b
.entries
= &browser
->hists
->entries
;
619 browser
->b
.nr_entries
= hist_browser__nr_entries(browser
);
621 hist_browser__title(browser
, title
, sizeof(title
));
623 if (ui_browser__show(&browser
->b
, title
, "%s", help
) < 0)
627 key
= ui_browser__run(&browser
->b
, delay_secs
);
632 hbt
->timer(hbt
->arg
);
634 if (hist_browser__has_filter(browser
) ||
635 symbol_conf
.report_hierarchy
)
636 hist_browser__update_nr_entries(browser
);
638 nr_entries
= hist_browser__nr_entries(browser
);
639 ui_browser__update_nr_entries(&browser
->b
, nr_entries
);
641 if (browser
->hists
->stats
.nr_lost_warned
!=
642 browser
->hists
->stats
.nr_events
[PERF_RECORD_LOST
]) {
643 browser
->hists
->stats
.nr_lost_warned
=
644 browser
->hists
->stats
.nr_events
[PERF_RECORD_LOST
];
645 ui_browser__warn_lost_events(&browser
->b
);
648 hist_browser__title(browser
, title
, sizeof(title
));
649 ui_browser__show_title(&browser
->b
, title
);
652 case 'D': { /* Debug */
654 struct hist_entry
*h
= rb_entry(browser
->b
.top
,
655 struct hist_entry
, rb_node
);
657 ui_helpline__fpush("%d: nr_ent=(%d,%d), rows=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
658 seq
++, browser
->b
.nr_entries
,
659 browser
->hists
->nr_entries
,
663 h
->row_offset
, h
->nr_rows
);
667 /* Collapse the whole world. */
668 hist_browser__set_folding(browser
, false);
671 /* Collapse the selected entry. */
672 hist_browser__set_folding_selected(browser
, false);
675 /* Expand the whole world. */
676 hist_browser__set_folding(browser
, true);
679 /* Expand the selected entry. */
680 hist_browser__set_folding_selected(browser
, true);
683 browser
->show_headers
= !browser
->show_headers
;
684 hist_browser__update_rows(browser
);
687 if (hist_browser__toggle_fold(browser
))
695 ui_browser__hide(&browser
->b
);
699 struct callchain_print_arg
{
700 /* for hists browser */
702 bool is_current_entry
;
709 typedef void (*print_callchain_entry_fn
)(struct hist_browser
*browser
,
710 struct callchain_list
*chain
,
711 const char *str
, int offset
,
713 struct callchain_print_arg
*arg
);
715 static void hist_browser__show_callchain_entry(struct hist_browser
*browser
,
716 struct callchain_list
*chain
,
717 const char *str
, int offset
,
719 struct callchain_print_arg
*arg
)
722 char folded_sign
= callchain_list__folded(chain
);
723 bool show_annotated
= browser
->show_dso
&& chain
->ms
.sym
&& symbol__annotation(chain
->ms
.sym
)->src
;
725 color
= HE_COLORSET_NORMAL
;
726 width
= browser
->b
.width
- (offset
+ 2);
727 if (ui_browser__is_current_entry(&browser
->b
, row
)) {
728 browser
->selection
= &chain
->ms
;
729 color
= HE_COLORSET_SELECTED
;
730 arg
->is_current_entry
= true;
733 ui_browser__set_color(&browser
->b
, color
);
734 hist_browser__gotorc(browser
, row
, 0);
735 ui_browser__write_nstring(&browser
->b
, " ", offset
);
736 ui_browser__printf(&browser
->b
, "%c", folded_sign
);
737 ui_browser__write_graph(&browser
->b
, show_annotated
? SLSMG_RARROW_CHAR
: ' ');
738 ui_browser__write_nstring(&browser
->b
, str
, width
);
741 static void hist_browser__fprintf_callchain_entry(struct hist_browser
*b __maybe_unused
,
742 struct callchain_list
*chain
,
743 const char *str
, int offset
,
744 unsigned short row __maybe_unused
,
745 struct callchain_print_arg
*arg
)
747 char folded_sign
= callchain_list__folded(chain
);
749 arg
->printed
+= fprintf(arg
->fp
, "%*s%c %s\n", offset
, " ",
753 typedef bool (*check_output_full_fn
)(struct hist_browser
*browser
,
756 static bool hist_browser__check_output_full(struct hist_browser
*browser
,
759 return browser
->b
.rows
== row
;
762 static bool hist_browser__check_dump_full(struct hist_browser
*browser __maybe_unused
,
763 unsigned short row __maybe_unused
)
768 #define LEVEL_OFFSET_STEP 3
770 static int hist_browser__show_callchain_list(struct hist_browser
*browser
,
771 struct callchain_node
*node
,
772 struct callchain_list
*chain
,
773 unsigned short row
, u64 total
,
774 bool need_percent
, int offset
,
775 print_callchain_entry_fn print
,
776 struct callchain_print_arg
*arg
)
778 char bf
[1024], *alloc_str
;
779 char buf
[64], *alloc_str2
;
783 if (arg
->row_offset
!= 0) {
791 str
= callchain_list__sym_name(chain
, bf
, sizeof(bf
),
794 if (symbol_conf
.show_branchflag_count
) {
795 callchain_list_counts__printf_value(chain
, NULL
,
798 if (asprintf(&alloc_str2
, "%s%s", str
, buf
) < 0)
799 str
= "Not enough memory!";
805 callchain_node__scnprintf_value(node
, buf
, sizeof(buf
),
808 if (asprintf(&alloc_str
, "%s %s", buf
, str
) < 0)
809 str
= "Not enough memory!";
814 print(browser
, chain
, str
, offset
, row
, arg
);
821 static bool check_percent_display(struct rb_node
*node
, u64 parent_total
)
823 struct callchain_node
*child
;
831 child
= rb_entry(node
, struct callchain_node
, rb_node
);
832 return callchain_cumul_hits(child
) != parent_total
;
835 static int hist_browser__show_callchain_flat(struct hist_browser
*browser
,
836 struct rb_root
*root
,
837 unsigned short row
, u64 total
,
839 print_callchain_entry_fn print
,
840 struct callchain_print_arg
*arg
,
841 check_output_full_fn is_output_full
)
843 struct rb_node
*node
;
844 int first_row
= row
, offset
= LEVEL_OFFSET_STEP
;
847 node
= rb_first(root
);
848 need_percent
= check_percent_display(node
, parent_total
);
851 struct callchain_node
*child
= rb_entry(node
, struct callchain_node
, rb_node
);
852 struct rb_node
*next
= rb_next(node
);
853 struct callchain_list
*chain
;
854 char folded_sign
= ' ';
856 int extra_offset
= 0;
858 list_for_each_entry(chain
, &child
->parent_val
, list
) {
859 bool was_first
= first
;
863 else if (need_percent
)
864 extra_offset
= LEVEL_OFFSET_STEP
;
866 folded_sign
= callchain_list__folded(chain
);
868 row
+= hist_browser__show_callchain_list(browser
, child
,
870 was_first
&& need_percent
,
871 offset
+ extra_offset
,
874 if (is_output_full(browser
, row
))
877 if (folded_sign
== '+')
881 list_for_each_entry(chain
, &child
->val
, list
) {
882 bool was_first
= first
;
886 else if (need_percent
)
887 extra_offset
= LEVEL_OFFSET_STEP
;
889 folded_sign
= callchain_list__folded(chain
);
891 row
+= hist_browser__show_callchain_list(browser
, child
,
893 was_first
&& need_percent
,
894 offset
+ extra_offset
,
897 if (is_output_full(browser
, row
))
900 if (folded_sign
== '+')
905 if (is_output_full(browser
, row
))
910 return row
- first_row
;
913 static char *hist_browser__folded_callchain_str(struct hist_browser
*browser
,
914 struct callchain_list
*chain
,
915 char *value_str
, char *old_str
)
921 str
= callchain_list__sym_name(chain
, bf
, sizeof(bf
),
924 if (asprintf(&new, "%s%s%s", old_str
,
925 symbol_conf
.field_sep
?: ";", str
) < 0)
929 if (asprintf(&new, "%s %s", value_str
, str
) < 0)
932 if (asprintf(&new, "%s", str
) < 0)
939 static int hist_browser__show_callchain_folded(struct hist_browser
*browser
,
940 struct rb_root
*root
,
941 unsigned short row
, u64 total
,
943 print_callchain_entry_fn print
,
944 struct callchain_print_arg
*arg
,
945 check_output_full_fn is_output_full
)
947 struct rb_node
*node
;
948 int first_row
= row
, offset
= LEVEL_OFFSET_STEP
;
951 node
= rb_first(root
);
952 need_percent
= check_percent_display(node
, parent_total
);
955 struct callchain_node
*child
= rb_entry(node
, struct callchain_node
, rb_node
);
956 struct rb_node
*next
= rb_next(node
);
957 struct callchain_list
*chain
, *first_chain
= NULL
;
959 char *value_str
= NULL
, *value_str_alloc
= NULL
;
960 char *chain_str
= NULL
, *chain_str_alloc
= NULL
;
962 if (arg
->row_offset
!= 0) {
970 callchain_node__scnprintf_value(child
, buf
, sizeof(buf
), total
);
971 if (asprintf(&value_str
, "%s", buf
) < 0) {
972 value_str
= (char *)"<...>";
975 value_str_alloc
= value_str
;
978 list_for_each_entry(chain
, &child
->parent_val
, list
) {
979 chain_str
= hist_browser__folded_callchain_str(browser
,
980 chain
, value_str
, chain_str
);
986 if (chain_str
== NULL
) {
987 chain_str
= (char *)"Not enough memory!";
991 chain_str_alloc
= chain_str
;
994 list_for_each_entry(chain
, &child
->val
, list
) {
995 chain_str
= hist_browser__folded_callchain_str(browser
,
996 chain
, value_str
, chain_str
);
1002 if (chain_str
== NULL
) {
1003 chain_str
= (char *)"Not enough memory!";
1007 chain_str_alloc
= chain_str
;
1011 print(browser
, first_chain
, chain_str
, offset
, row
++, arg
);
1012 free(value_str_alloc
);
1013 free(chain_str_alloc
);
1016 if (is_output_full(browser
, row
))
1021 return row
- first_row
;
1024 static int hist_browser__show_callchain_graph(struct hist_browser
*browser
,
1025 struct rb_root
*root
, int level
,
1026 unsigned short row
, u64 total
,
1028 print_callchain_entry_fn print
,
1029 struct callchain_print_arg
*arg
,
1030 check_output_full_fn is_output_full
)
1032 struct rb_node
*node
;
1033 int first_row
= row
, offset
= level
* LEVEL_OFFSET_STEP
;
1035 u64 percent_total
= total
;
1037 if (callchain_param
.mode
== CHAIN_GRAPH_REL
)
1038 percent_total
= parent_total
;
1040 node
= rb_first(root
);
1041 need_percent
= check_percent_display(node
, parent_total
);
1044 struct callchain_node
*child
= rb_entry(node
, struct callchain_node
, rb_node
);
1045 struct rb_node
*next
= rb_next(node
);
1046 struct callchain_list
*chain
;
1047 char folded_sign
= ' ';
1049 int extra_offset
= 0;
1051 list_for_each_entry(chain
, &child
->val
, list
) {
1052 bool was_first
= first
;
1056 else if (need_percent
)
1057 extra_offset
= LEVEL_OFFSET_STEP
;
1059 folded_sign
= callchain_list__folded(chain
);
1061 row
+= hist_browser__show_callchain_list(browser
, child
,
1062 chain
, row
, percent_total
,
1063 was_first
&& need_percent
,
1064 offset
+ extra_offset
,
1067 if (is_output_full(browser
, row
))
1070 if (folded_sign
== '+')
1074 if (folded_sign
== '-') {
1075 const int new_level
= level
+ (extra_offset
? 2 : 1);
1077 row
+= hist_browser__show_callchain_graph(browser
, &child
->rb_root
,
1078 new_level
, row
, total
,
1079 child
->children_hit
,
1080 print
, arg
, is_output_full
);
1082 if (is_output_full(browser
, row
))
1087 return row
- first_row
;
1090 static int hist_browser__show_callchain(struct hist_browser
*browser
,
1091 struct hist_entry
*entry
, int level
,
1093 print_callchain_entry_fn print
,
1094 struct callchain_print_arg
*arg
,
1095 check_output_full_fn is_output_full
)
1097 u64 total
= hists__total_period(entry
->hists
);
1101 if (symbol_conf
.cumulate_callchain
)
1102 parent_total
= entry
->stat_acc
->period
;
1104 parent_total
= entry
->stat
.period
;
1106 if (callchain_param
.mode
== CHAIN_FLAT
) {
1107 printed
= hist_browser__show_callchain_flat(browser
,
1108 &entry
->sorted_chain
, row
,
1109 total
, parent_total
, print
, arg
,
1111 } else if (callchain_param
.mode
== CHAIN_FOLDED
) {
1112 printed
= hist_browser__show_callchain_folded(browser
,
1113 &entry
->sorted_chain
, row
,
1114 total
, parent_total
, print
, arg
,
1117 printed
= hist_browser__show_callchain_graph(browser
,
1118 &entry
->sorted_chain
, level
, row
,
1119 total
, parent_total
, print
, arg
,
1123 if (arg
->is_current_entry
)
1124 browser
->he_selection
= entry
;
1130 struct ui_browser
*b
;
1135 int __hpp__slsmg_color_printf(struct perf_hpp
*hpp
, const char *fmt
, ...)
1137 struct hpp_arg
*arg
= hpp
->ptr
;
1142 va_start(args
, fmt
);
1143 len
= va_arg(args
, int);
1144 percent
= va_arg(args
, double);
1147 ui_browser__set_percent_color(arg
->b
, percent
, arg
->current_entry
);
1149 ret
= scnprintf(hpp
->buf
, hpp
->size
, fmt
, len
, percent
);
1150 ui_browser__printf(arg
->b
, "%s", hpp
->buf
);
1155 #define __HPP_COLOR_PERCENT_FN(_type, _field) \
1156 static u64 __hpp_get_##_field(struct hist_entry *he) \
1158 return he->stat._field; \
1162 hist_browser__hpp_color_##_type(struct perf_hpp_fmt *fmt, \
1163 struct perf_hpp *hpp, \
1164 struct hist_entry *he) \
1166 return hpp__fmt(fmt, hpp, he, __hpp_get_##_field, " %*.2f%%", \
1167 __hpp__slsmg_color_printf, true); \
1170 #define __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
1171 static u64 __hpp_get_acc_##_field(struct hist_entry *he) \
1173 return he->stat_acc->_field; \
1177 hist_browser__hpp_color_##_type(struct perf_hpp_fmt *fmt, \
1178 struct perf_hpp *hpp, \
1179 struct hist_entry *he) \
1181 if (!symbol_conf.cumulate_callchain) { \
1182 struct hpp_arg *arg = hpp->ptr; \
1183 int len = fmt->user_len ?: fmt->len; \
1184 int ret = scnprintf(hpp->buf, hpp->size, \
1185 "%*s", len, "N/A"); \
1186 ui_browser__printf(arg->b, "%s", hpp->buf); \
1190 return hpp__fmt(fmt, hpp, he, __hpp_get_acc_##_field, \
1191 " %*.2f%%", __hpp__slsmg_color_printf, true); \
1194 __HPP_COLOR_PERCENT_FN(overhead
, period
)
1195 __HPP_COLOR_PERCENT_FN(overhead_sys
, period_sys
)
1196 __HPP_COLOR_PERCENT_FN(overhead_us
, period_us
)
1197 __HPP_COLOR_PERCENT_FN(overhead_guest_sys
, period_guest_sys
)
1198 __HPP_COLOR_PERCENT_FN(overhead_guest_us
, period_guest_us
)
1199 __HPP_COLOR_ACC_PERCENT_FN(overhead_acc
, period
)
1201 #undef __HPP_COLOR_PERCENT_FN
1202 #undef __HPP_COLOR_ACC_PERCENT_FN
1204 void hist_browser__init_hpp(void)
1206 perf_hpp__format
[PERF_HPP__OVERHEAD
].color
=
1207 hist_browser__hpp_color_overhead
;
1208 perf_hpp__format
[PERF_HPP__OVERHEAD_SYS
].color
=
1209 hist_browser__hpp_color_overhead_sys
;
1210 perf_hpp__format
[PERF_HPP__OVERHEAD_US
].color
=
1211 hist_browser__hpp_color_overhead_us
;
1212 perf_hpp__format
[PERF_HPP__OVERHEAD_GUEST_SYS
].color
=
1213 hist_browser__hpp_color_overhead_guest_sys
;
1214 perf_hpp__format
[PERF_HPP__OVERHEAD_GUEST_US
].color
=
1215 hist_browser__hpp_color_overhead_guest_us
;
1216 perf_hpp__format
[PERF_HPP__OVERHEAD_ACC
].color
=
1217 hist_browser__hpp_color_overhead_acc
;
1220 static int hist_browser__show_entry(struct hist_browser
*browser
,
1221 struct hist_entry
*entry
,
1225 int width
= browser
->b
.width
;
1226 char folded_sign
= ' ';
1227 bool current_entry
= ui_browser__is_current_entry(&browser
->b
, row
);
1228 off_t row_offset
= entry
->row_offset
;
1230 struct perf_hpp_fmt
*fmt
;
1232 if (current_entry
) {
1233 browser
->he_selection
= entry
;
1234 browser
->selection
= &entry
->ms
;
1237 if (symbol_conf
.use_callchain
) {
1238 hist_entry__init_have_children(entry
);
1239 folded_sign
= hist_entry__folded(entry
);
1242 if (row_offset
== 0) {
1243 struct hpp_arg arg
= {
1245 .folded_sign
= folded_sign
,
1246 .current_entry
= current_entry
,
1250 hist_browser__gotorc(browser
, row
, 0);
1252 hists__for_each_format(browser
->hists
, fmt
) {
1254 struct perf_hpp hpp
= {
1260 if (perf_hpp__should_skip(fmt
, entry
->hists
) ||
1261 column
++ < browser
->b
.horiz_scroll
)
1264 if (current_entry
&& browser
->b
.navkeypressed
) {
1265 ui_browser__set_color(&browser
->b
,
1266 HE_COLORSET_SELECTED
);
1268 ui_browser__set_color(&browser
->b
,
1269 HE_COLORSET_NORMAL
);
1273 if (symbol_conf
.use_callchain
) {
1274 ui_browser__printf(&browser
->b
, "%c ", folded_sign
);
1279 ui_browser__printf(&browser
->b
, " ");
1284 int ret
= fmt
->color(fmt
, &hpp
, entry
);
1285 hist_entry__snprintf_alignment(entry
, &hpp
, fmt
, ret
);
1287 * fmt->color() already used ui_browser to
1288 * print the non alignment bits, skip it (+ret):
1290 ui_browser__printf(&browser
->b
, "%s", s
+ ret
);
1292 hist_entry__snprintf_alignment(entry
, &hpp
, fmt
, fmt
->entry(fmt
, &hpp
, entry
));
1293 ui_browser__printf(&browser
->b
, "%s", s
);
1295 width
-= hpp
.buf
- s
;
1298 /* The scroll bar isn't being used */
1299 if (!browser
->b
.navkeypressed
)
1302 ui_browser__write_nstring(&browser
->b
, "", width
);
1309 if (folded_sign
== '-' && row
!= browser
->b
.rows
) {
1310 struct callchain_print_arg arg
= {
1311 .row_offset
= row_offset
,
1312 .is_current_entry
= current_entry
,
1315 printed
+= hist_browser__show_callchain(browser
,
1317 hist_browser__show_callchain_entry
,
1319 hist_browser__check_output_full
);
1325 static int hist_browser__show_hierarchy_entry(struct hist_browser
*browser
,
1326 struct hist_entry
*entry
,
1331 int width
= browser
->b
.width
;
1332 char folded_sign
= ' ';
1333 bool current_entry
= ui_browser__is_current_entry(&browser
->b
, row
);
1334 off_t row_offset
= entry
->row_offset
;
1336 struct perf_hpp_fmt
*fmt
;
1337 struct perf_hpp_list_node
*fmt_node
;
1338 struct hpp_arg arg
= {
1340 .current_entry
= current_entry
,
1343 int hierarchy_indent
= (entry
->hists
->nr_hpp_node
- 2) * HIERARCHY_INDENT
;
1345 if (current_entry
) {
1346 browser
->he_selection
= entry
;
1347 browser
->selection
= &entry
->ms
;
1350 hist_entry__init_have_children(entry
);
1351 folded_sign
= hist_entry__folded(entry
);
1352 arg
.folded_sign
= folded_sign
;
1354 if (entry
->leaf
&& row_offset
) {
1356 goto show_callchain
;
1359 hist_browser__gotorc(browser
, row
, 0);
1361 if (current_entry
&& browser
->b
.navkeypressed
)
1362 ui_browser__set_color(&browser
->b
, HE_COLORSET_SELECTED
);
1364 ui_browser__set_color(&browser
->b
, HE_COLORSET_NORMAL
);
1366 ui_browser__write_nstring(&browser
->b
, "", level
* HIERARCHY_INDENT
);
1367 width
-= level
* HIERARCHY_INDENT
;
1369 /* the first hpp_list_node is for overhead columns */
1370 fmt_node
= list_first_entry(&entry
->hists
->hpp_formats
,
1371 struct perf_hpp_list_node
, list
);
1372 perf_hpp_list__for_each_format(&fmt_node
->hpp
, fmt
) {
1374 struct perf_hpp hpp
= {
1380 if (perf_hpp__should_skip(fmt
, entry
->hists
) ||
1381 column
++ < browser
->b
.horiz_scroll
)
1384 if (current_entry
&& browser
->b
.navkeypressed
) {
1385 ui_browser__set_color(&browser
->b
,
1386 HE_COLORSET_SELECTED
);
1388 ui_browser__set_color(&browser
->b
,
1389 HE_COLORSET_NORMAL
);
1393 ui_browser__printf(&browser
->b
, "%c ", folded_sign
);
1397 ui_browser__printf(&browser
->b
, " ");
1402 int ret
= fmt
->color(fmt
, &hpp
, entry
);
1403 hist_entry__snprintf_alignment(entry
, &hpp
, fmt
, ret
);
1405 * fmt->color() already used ui_browser to
1406 * print the non alignment bits, skip it (+ret):
1408 ui_browser__printf(&browser
->b
, "%s", s
+ ret
);
1410 int ret
= fmt
->entry(fmt
, &hpp
, entry
);
1411 hist_entry__snprintf_alignment(entry
, &hpp
, fmt
, ret
);
1412 ui_browser__printf(&browser
->b
, "%s", s
);
1414 width
-= hpp
.buf
- s
;
1418 ui_browser__write_nstring(&browser
->b
, "", hierarchy_indent
);
1419 width
-= hierarchy_indent
;
1422 if (column
>= browser
->b
.horiz_scroll
) {
1424 struct perf_hpp hpp
= {
1430 if (current_entry
&& browser
->b
.navkeypressed
) {
1431 ui_browser__set_color(&browser
->b
,
1432 HE_COLORSET_SELECTED
);
1434 ui_browser__set_color(&browser
->b
,
1435 HE_COLORSET_NORMAL
);
1438 perf_hpp_list__for_each_format(entry
->hpp_list
, fmt
) {
1440 ui_browser__printf(&browser
->b
, "%c ", folded_sign
);
1443 ui_browser__write_nstring(&browser
->b
, "", 2);
1449 * No need to call hist_entry__snprintf_alignment()
1450 * since this fmt is always the last column in the
1454 width
-= fmt
->color(fmt
, &hpp
, entry
);
1458 width
-= fmt
->entry(fmt
, &hpp
, entry
);
1459 ui_browser__printf(&browser
->b
, "%s", ltrim(s
));
1461 while (isspace(s
[i
++]))
1467 /* The scroll bar isn't being used */
1468 if (!browser
->b
.navkeypressed
)
1471 ui_browser__write_nstring(&browser
->b
, "", width
);
1477 if (entry
->leaf
&& folded_sign
== '-' && row
!= browser
->b
.rows
) {
1478 struct callchain_print_arg carg
= {
1479 .row_offset
= row_offset
,
1482 printed
+= hist_browser__show_callchain(browser
, entry
,
1484 hist_browser__show_callchain_entry
, &carg
,
1485 hist_browser__check_output_full
);
1491 static int hist_browser__show_no_entry(struct hist_browser
*browser
,
1492 unsigned short row
, int level
)
1494 int width
= browser
->b
.width
;
1495 bool current_entry
= ui_browser__is_current_entry(&browser
->b
, row
);
1499 struct perf_hpp_fmt
*fmt
;
1500 struct perf_hpp_list_node
*fmt_node
;
1501 int indent
= browser
->hists
->nr_hpp_node
- 2;
1503 if (current_entry
) {
1504 browser
->he_selection
= NULL
;
1505 browser
->selection
= NULL
;
1508 hist_browser__gotorc(browser
, row
, 0);
1510 if (current_entry
&& browser
->b
.navkeypressed
)
1511 ui_browser__set_color(&browser
->b
, HE_COLORSET_SELECTED
);
1513 ui_browser__set_color(&browser
->b
, HE_COLORSET_NORMAL
);
1515 ui_browser__write_nstring(&browser
->b
, "", level
* HIERARCHY_INDENT
);
1516 width
-= level
* HIERARCHY_INDENT
;
1518 /* the first hpp_list_node is for overhead columns */
1519 fmt_node
= list_first_entry(&browser
->hists
->hpp_formats
,
1520 struct perf_hpp_list_node
, list
);
1521 perf_hpp_list__for_each_format(&fmt_node
->hpp
, fmt
) {
1522 if (perf_hpp__should_skip(fmt
, browser
->hists
) ||
1523 column
++ < browser
->b
.horiz_scroll
)
1526 ret
= fmt
->width(fmt
, NULL
, browser
->hists
);
1529 /* for folded sign */
1533 /* space between columns */
1537 ui_browser__write_nstring(&browser
->b
, "", ret
);
1541 ui_browser__write_nstring(&browser
->b
, "", indent
* HIERARCHY_INDENT
);
1542 width
-= indent
* HIERARCHY_INDENT
;
1544 if (column
>= browser
->b
.horiz_scroll
) {
1547 ret
= snprintf(buf
, sizeof(buf
), "no entry >= %.2f%%", browser
->min_pcnt
);
1548 ui_browser__printf(&browser
->b
, " %s", buf
);
1552 /* The scroll bar isn't being used */
1553 if (!browser
->b
.navkeypressed
)
1556 ui_browser__write_nstring(&browser
->b
, "", width
);
1560 static int advance_hpp_check(struct perf_hpp
*hpp
, int inc
)
1562 advance_hpp(hpp
, inc
);
1563 return hpp
->size
<= 0;
1567 hists_browser__scnprintf_headers(struct hist_browser
*browser
, char *buf
,
1568 size_t size
, int line
)
1570 struct hists
*hists
= browser
->hists
;
1571 struct perf_hpp dummy_hpp
= {
1575 struct perf_hpp_fmt
*fmt
;
1580 if (symbol_conf
.use_callchain
) {
1581 ret
= scnprintf(buf
, size
, " ");
1582 if (advance_hpp_check(&dummy_hpp
, ret
))
1586 hists__for_each_format(browser
->hists
, fmt
) {
1587 if (perf_hpp__should_skip(fmt
, hists
) || column
++ < browser
->b
.horiz_scroll
)
1590 ret
= fmt
->header(fmt
, &dummy_hpp
, hists
, line
, &span
);
1591 if (advance_hpp_check(&dummy_hpp
, ret
))
1597 ret
= scnprintf(dummy_hpp
.buf
, dummy_hpp
.size
, " ");
1598 if (advance_hpp_check(&dummy_hpp
, ret
))
1605 static int hists_browser__scnprintf_hierarchy_headers(struct hist_browser
*browser
, char *buf
, size_t size
)
1607 struct hists
*hists
= browser
->hists
;
1608 struct perf_hpp dummy_hpp
= {
1612 struct perf_hpp_fmt
*fmt
;
1613 struct perf_hpp_list_node
*fmt_node
;
1616 int indent
= hists
->nr_hpp_node
- 2;
1617 bool first_node
, first_col
;
1619 ret
= scnprintf(buf
, size
, " ");
1620 if (advance_hpp_check(&dummy_hpp
, ret
))
1624 /* the first hpp_list_node is for overhead columns */
1625 fmt_node
= list_first_entry(&hists
->hpp_formats
,
1626 struct perf_hpp_list_node
, list
);
1627 perf_hpp_list__for_each_format(&fmt_node
->hpp
, fmt
) {
1628 if (column
++ < browser
->b
.horiz_scroll
)
1631 ret
= fmt
->header(fmt
, &dummy_hpp
, hists
, 0, NULL
);
1632 if (advance_hpp_check(&dummy_hpp
, ret
))
1635 ret
= scnprintf(dummy_hpp
.buf
, dummy_hpp
.size
, " ");
1636 if (advance_hpp_check(&dummy_hpp
, ret
))
1643 ret
= scnprintf(dummy_hpp
.buf
, dummy_hpp
.size
, "%*s",
1644 indent
* HIERARCHY_INDENT
, "");
1645 if (advance_hpp_check(&dummy_hpp
, ret
))
1650 list_for_each_entry_continue(fmt_node
, &hists
->hpp_formats
, list
) {
1652 ret
= scnprintf(dummy_hpp
.buf
, dummy_hpp
.size
, " / ");
1653 if (advance_hpp_check(&dummy_hpp
, ret
))
1659 perf_hpp_list__for_each_format(&fmt_node
->hpp
, fmt
) {
1662 if (perf_hpp__should_skip(fmt
, hists
))
1666 ret
= scnprintf(dummy_hpp
.buf
, dummy_hpp
.size
, "+");
1667 if (advance_hpp_check(&dummy_hpp
, ret
))
1672 ret
= fmt
->header(fmt
, &dummy_hpp
, hists
, 0, NULL
);
1673 dummy_hpp
.buf
[ret
] = '\0';
1675 start
= trim(dummy_hpp
.buf
);
1676 ret
= strlen(start
);
1678 if (start
!= dummy_hpp
.buf
)
1679 memmove(dummy_hpp
.buf
, start
, ret
+ 1);
1681 if (advance_hpp_check(&dummy_hpp
, ret
))
1689 static void hists_browser__hierarchy_headers(struct hist_browser
*browser
)
1693 hists_browser__scnprintf_hierarchy_headers(browser
, headers
,
1696 ui_browser__gotorc(&browser
->b
, 0, 0);
1697 ui_browser__set_color(&browser
->b
, HE_COLORSET_ROOT
);
1698 ui_browser__write_nstring(&browser
->b
, headers
, browser
->b
.width
+ 1);
1701 static void hists_browser__headers(struct hist_browser
*browser
)
1703 struct hists
*hists
= browser
->hists
;
1704 struct perf_hpp_list
*hpp_list
= hists
->hpp_list
;
1708 for (line
= 0; line
< hpp_list
->nr_header_lines
; line
++) {
1711 hists_browser__scnprintf_headers(browser
, headers
,
1712 sizeof(headers
), line
);
1714 ui_browser__gotorc(&browser
->b
, line
, 0);
1715 ui_browser__set_color(&browser
->b
, HE_COLORSET_ROOT
);
1716 ui_browser__write_nstring(&browser
->b
, headers
, browser
->b
.width
+ 1);
1720 static void hist_browser__show_headers(struct hist_browser
*browser
)
1722 if (symbol_conf
.report_hierarchy
)
1723 hists_browser__hierarchy_headers(browser
);
1725 hists_browser__headers(browser
);
1728 static void ui_browser__hists_init_top(struct ui_browser
*browser
)
1730 if (browser
->top
== NULL
) {
1731 struct hist_browser
*hb
;
1733 hb
= container_of(browser
, struct hist_browser
, b
);
1734 browser
->top
= rb_first(&hb
->hists
->entries
);
1738 static unsigned int hist_browser__refresh(struct ui_browser
*browser
)
1741 u16 header_offset
= 0;
1743 struct hist_browser
*hb
= container_of(browser
, struct hist_browser
, b
);
1744 struct hists
*hists
= hb
->hists
;
1746 if (hb
->show_headers
) {
1747 struct perf_hpp_list
*hpp_list
= hists
->hpp_list
;
1749 hist_browser__show_headers(hb
);
1750 header_offset
= hpp_list
->nr_header_lines
;
1753 ui_browser__hists_init_top(browser
);
1754 hb
->he_selection
= NULL
;
1755 hb
->selection
= NULL
;
1757 for (nd
= browser
->top
; nd
; nd
= rb_hierarchy_next(nd
)) {
1758 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
1762 /* let it move to sibling */
1763 h
->unfolded
= false;
1767 percent
= hist_entry__get_percent_limit(h
);
1768 if (percent
< hb
->min_pcnt
)
1771 if (symbol_conf
.report_hierarchy
) {
1772 row
+= hist_browser__show_hierarchy_entry(hb
, h
, row
,
1774 if (row
== browser
->rows
)
1777 if (h
->has_no_entry
) {
1778 hist_browser__show_no_entry(hb
, row
, h
->depth
+ 1);
1782 row
+= hist_browser__show_entry(hb
, h
, row
);
1785 if (row
== browser
->rows
)
1789 return row
+ header_offset
;
1792 static struct rb_node
*hists__filter_entries(struct rb_node
*nd
,
1795 while (nd
!= NULL
) {
1796 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
1797 float percent
= hist_entry__get_percent_limit(h
);
1799 if (!h
->filtered
&& percent
>= min_pcnt
)
1803 * If it's filtered, its all children also were filtered.
1804 * So move to sibling node.
1809 nd
= rb_hierarchy_next(nd
);
1815 static struct rb_node
*hists__filter_prev_entries(struct rb_node
*nd
,
1818 while (nd
!= NULL
) {
1819 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
1820 float percent
= hist_entry__get_percent_limit(h
);
1822 if (!h
->filtered
&& percent
>= min_pcnt
)
1825 nd
= rb_hierarchy_prev(nd
);
1831 static void ui_browser__hists_seek(struct ui_browser
*browser
,
1832 off_t offset
, int whence
)
1834 struct hist_entry
*h
;
1837 struct hist_browser
*hb
;
1839 hb
= container_of(browser
, struct hist_browser
, b
);
1841 if (browser
->nr_entries
== 0)
1844 ui_browser__hists_init_top(browser
);
1848 nd
= hists__filter_entries(rb_first(browser
->entries
),
1855 nd
= rb_hierarchy_last(rb_last(browser
->entries
));
1856 nd
= hists__filter_prev_entries(nd
, hb
->min_pcnt
);
1864 * Moves not relative to the first visible entry invalidates its
1867 h
= rb_entry(browser
->top
, struct hist_entry
, rb_node
);
1871 * Here we have to check if nd is expanded (+), if it is we can't go
1872 * the next top level hist_entry, instead we must compute an offset of
1873 * what _not_ to show and not change the first visible entry.
1875 * This offset increments when we are going from top to bottom and
1876 * decreases when we're going from bottom to top.
1878 * As we don't have backpointers to the top level in the callchains
1879 * structure, we need to always print the whole hist_entry callchain,
1880 * skipping the first ones that are before the first visible entry
1881 * and stop when we printed enough lines to fill the screen.
1889 h
= rb_entry(nd
, struct hist_entry
, rb_node
);
1890 if (h
->unfolded
&& h
->leaf
) {
1891 u16 remaining
= h
->nr_rows
- h
->row_offset
;
1892 if (offset
> remaining
) {
1893 offset
-= remaining
;
1896 h
->row_offset
+= offset
;
1902 nd
= hists__filter_entries(rb_hierarchy_next(nd
),
1908 } while (offset
!= 0);
1909 } else if (offset
< 0) {
1911 h
= rb_entry(nd
, struct hist_entry
, rb_node
);
1912 if (h
->unfolded
&& h
->leaf
) {
1914 if (-offset
> h
->row_offset
) {
1915 offset
+= h
->row_offset
;
1918 h
->row_offset
+= offset
;
1924 if (-offset
> h
->nr_rows
) {
1925 offset
+= h
->nr_rows
;
1928 h
->row_offset
= h
->nr_rows
+ offset
;
1936 nd
= hists__filter_prev_entries(rb_hierarchy_prev(nd
),
1944 * Last unfiltered hist_entry, check if it is
1945 * unfolded, if it is then we should have
1946 * row_offset at its last entry.
1948 h
= rb_entry(nd
, struct hist_entry
, rb_node
);
1949 if (h
->unfolded
&& h
->leaf
)
1950 h
->row_offset
= h
->nr_rows
;
1957 h
= rb_entry(nd
, struct hist_entry
, rb_node
);
1962 static int hist_browser__fprintf_callchain(struct hist_browser
*browser
,
1963 struct hist_entry
*he
, FILE *fp
,
1966 struct callchain_print_arg arg
= {
1970 hist_browser__show_callchain(browser
, he
, level
, 0,
1971 hist_browser__fprintf_callchain_entry
, &arg
,
1972 hist_browser__check_dump_full
);
1976 static int hist_browser__fprintf_entry(struct hist_browser
*browser
,
1977 struct hist_entry
*he
, FILE *fp
)
1981 char folded_sign
= ' ';
1982 struct perf_hpp hpp
= {
1986 struct perf_hpp_fmt
*fmt
;
1990 if (symbol_conf
.use_callchain
) {
1991 folded_sign
= hist_entry__folded(he
);
1992 printed
+= fprintf(fp
, "%c ", folded_sign
);
1995 hists__for_each_format(browser
->hists
, fmt
) {
1996 if (perf_hpp__should_skip(fmt
, he
->hists
))
2000 ret
= scnprintf(hpp
.buf
, hpp
.size
, " ");
2001 advance_hpp(&hpp
, ret
);
2005 ret
= fmt
->entry(fmt
, &hpp
, he
);
2006 ret
= hist_entry__snprintf_alignment(he
, &hpp
, fmt
, ret
);
2007 advance_hpp(&hpp
, ret
);
2009 printed
+= fprintf(fp
, "%s\n", s
);
2011 if (folded_sign
== '-')
2012 printed
+= hist_browser__fprintf_callchain(browser
, he
, fp
, 1);
2018 static int hist_browser__fprintf_hierarchy_entry(struct hist_browser
*browser
,
2019 struct hist_entry
*he
,
2020 FILE *fp
, int level
)
2024 char folded_sign
= ' ';
2025 struct perf_hpp hpp
= {
2029 struct perf_hpp_fmt
*fmt
;
2030 struct perf_hpp_list_node
*fmt_node
;
2033 int hierarchy_indent
= (he
->hists
->nr_hpp_node
- 2) * HIERARCHY_INDENT
;
2035 printed
= fprintf(fp
, "%*s", level
* HIERARCHY_INDENT
, "");
2037 folded_sign
= hist_entry__folded(he
);
2038 printed
+= fprintf(fp
, "%c", folded_sign
);
2040 /* the first hpp_list_node is for overhead columns */
2041 fmt_node
= list_first_entry(&he
->hists
->hpp_formats
,
2042 struct perf_hpp_list_node
, list
);
2043 perf_hpp_list__for_each_format(&fmt_node
->hpp
, fmt
) {
2045 ret
= scnprintf(hpp
.buf
, hpp
.size
, " ");
2046 advance_hpp(&hpp
, ret
);
2050 ret
= fmt
->entry(fmt
, &hpp
, he
);
2051 advance_hpp(&hpp
, ret
);
2054 ret
= scnprintf(hpp
.buf
, hpp
.size
, "%*s", hierarchy_indent
, "");
2055 advance_hpp(&hpp
, ret
);
2057 perf_hpp_list__for_each_format(he
->hpp_list
, fmt
) {
2058 ret
= scnprintf(hpp
.buf
, hpp
.size
, " ");
2059 advance_hpp(&hpp
, ret
);
2061 ret
= fmt
->entry(fmt
, &hpp
, he
);
2062 advance_hpp(&hpp
, ret
);
2065 printed
+= fprintf(fp
, "%s\n", rtrim(s
));
2067 if (he
->leaf
&& folded_sign
== '-') {
2068 printed
+= hist_browser__fprintf_callchain(browser
, he
, fp
,
2075 static int hist_browser__fprintf(struct hist_browser
*browser
, FILE *fp
)
2077 struct rb_node
*nd
= hists__filter_entries(rb_first(browser
->b
.entries
),
2082 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
2084 if (symbol_conf
.report_hierarchy
) {
2085 printed
+= hist_browser__fprintf_hierarchy_entry(browser
,
2089 printed
+= hist_browser__fprintf_entry(browser
, h
, fp
);
2092 nd
= hists__filter_entries(rb_hierarchy_next(nd
),
2099 static int hist_browser__dump(struct hist_browser
*browser
)
2105 scnprintf(filename
, sizeof(filename
), "perf.hist.%d", browser
->print_seq
);
2106 if (access(filename
, F_OK
))
2109 * XXX: Just an arbitrary lazy upper limit
2111 if (++browser
->print_seq
== 8192) {
2112 ui_helpline__fpush("Too many perf.hist.N files, nothing written!");
2117 fp
= fopen(filename
, "w");
2120 const char *err
= str_error_r(errno
, bf
, sizeof(bf
));
2121 ui_helpline__fpush("Couldn't write to %s: %s", filename
, err
);
2125 ++browser
->print_seq
;
2126 hist_browser__fprintf(browser
, fp
);
2128 ui_helpline__fpush("%s written!", filename
);
2133 void hist_browser__init(struct hist_browser
*browser
,
2134 struct hists
*hists
)
2136 struct perf_hpp_fmt
*fmt
;
2138 browser
->hists
= hists
;
2139 browser
->b
.refresh
= hist_browser__refresh
;
2140 browser
->b
.refresh_dimensions
= hist_browser__refresh_dimensions
;
2141 browser
->b
.seek
= ui_browser__hists_seek
;
2142 browser
->b
.use_navkeypressed
= true;
2143 browser
->show_headers
= symbol_conf
.show_hist_headers
;
2145 if (symbol_conf
.report_hierarchy
) {
2146 struct perf_hpp_list_node
*fmt_node
;
2148 /* count overhead columns (in the first node) */
2149 fmt_node
= list_first_entry(&hists
->hpp_formats
,
2150 struct perf_hpp_list_node
, list
);
2151 perf_hpp_list__for_each_format(&fmt_node
->hpp
, fmt
)
2152 ++browser
->b
.columns
;
2154 /* add a single column for whole hierarchy sort keys*/
2155 ++browser
->b
.columns
;
2157 hists__for_each_format(hists
, fmt
)
2158 ++browser
->b
.columns
;
2161 hists__reset_column_width(hists
);
2164 struct hist_browser
*hist_browser__new(struct hists
*hists
)
2166 struct hist_browser
*browser
= zalloc(sizeof(*browser
));
2169 hist_browser__init(browser
, hists
);
2174 static struct hist_browser
*
2175 perf_evsel_browser__new(struct perf_evsel
*evsel
,
2176 struct hist_browser_timer
*hbt
,
2177 struct perf_env
*env
)
2179 struct hist_browser
*browser
= hist_browser__new(evsel__hists(evsel
));
2184 browser
->title
= perf_evsel_browser_title
;
2189 void hist_browser__delete(struct hist_browser
*browser
)
2194 static struct hist_entry
*hist_browser__selected_entry(struct hist_browser
*browser
)
2196 return browser
->he_selection
;
2199 static struct thread
*hist_browser__selected_thread(struct hist_browser
*browser
)
2201 return browser
->he_selection
->thread
;
2204 /* Check whether the browser is for 'top' or 'report' */
2205 static inline bool is_report_browser(void *timer
)
2207 return timer
== NULL
;
2210 static int perf_evsel_browser_title(struct hist_browser
*browser
,
2211 char *bf
, size_t size
)
2213 struct hist_browser_timer
*hbt
= browser
->hbt
;
2214 struct hists
*hists
= browser
->hists
;
2217 const struct dso
*dso
= hists
->dso_filter
;
2218 const struct thread
*thread
= hists
->thread_filter
;
2219 int socket_id
= hists
->socket_filter
;
2220 unsigned long nr_samples
= hists
->stats
.nr_events
[PERF_RECORD_SAMPLE
];
2221 u64 nr_events
= hists
->stats
.total_period
;
2222 struct perf_evsel
*evsel
= hists_to_evsel(hists
);
2223 const char *ev_name
= perf_evsel__name(evsel
);
2225 size_t buflen
= sizeof(buf
);
2226 char ref
[30] = " show reference callgraph, ";
2227 bool enable_ref
= false;
2229 if (symbol_conf
.filter_relative
) {
2230 nr_samples
= hists
->stats
.nr_non_filtered_samples
;
2231 nr_events
= hists
->stats
.total_non_filtered_period
;
2234 if (perf_evsel__is_group_event(evsel
)) {
2235 struct perf_evsel
*pos
;
2237 perf_evsel__group_desc(evsel
, buf
, buflen
);
2240 for_each_group_member(pos
, evsel
) {
2241 struct hists
*pos_hists
= evsel__hists(pos
);
2243 if (symbol_conf
.filter_relative
) {
2244 nr_samples
+= pos_hists
->stats
.nr_non_filtered_samples
;
2245 nr_events
+= pos_hists
->stats
.total_non_filtered_period
;
2247 nr_samples
+= pos_hists
->stats
.nr_events
[PERF_RECORD_SAMPLE
];
2248 nr_events
+= pos_hists
->stats
.total_period
;
2253 if (symbol_conf
.show_ref_callgraph
&&
2254 strstr(ev_name
, "call-graph=no"))
2256 nr_samples
= convert_unit(nr_samples
, &unit
);
2257 printed
= scnprintf(bf
, size
,
2258 "Samples: %lu%c of event '%s',%sEvent count (approx.): %" PRIu64
,
2259 nr_samples
, unit
, ev_name
, enable_ref
? ref
: " ", nr_events
);
2262 if (hists
->uid_filter_str
)
2263 printed
+= snprintf(bf
+ printed
, size
- printed
,
2264 ", UID: %s", hists
->uid_filter_str
);
2266 if (hists__has(hists
, thread
)) {
2267 printed
+= scnprintf(bf
+ printed
, size
- printed
,
2269 (thread
->comm_set
? thread__comm_str(thread
) : ""),
2272 printed
+= scnprintf(bf
+ printed
, size
- printed
,
2274 (thread
->comm_set
? thread__comm_str(thread
) : ""));
2278 printed
+= scnprintf(bf
+ printed
, size
- printed
,
2279 ", DSO: %s", dso
->short_name
);
2281 printed
+= scnprintf(bf
+ printed
, size
- printed
,
2282 ", Processor Socket: %d", socket_id
);
2283 if (!is_report_browser(hbt
)) {
2284 struct perf_top
*top
= hbt
->arg
;
2287 printed
+= scnprintf(bf
+ printed
, size
- printed
, " [z]");
2293 static inline void free_popup_options(char **options
, int n
)
2297 for (i
= 0; i
< n
; ++i
)
2302 * Only runtime switching of perf data file will make "input_name" point
2303 * to a malloced buffer. So add "is_input_name_malloced" flag to decide
2304 * whether we need to call free() for current "input_name" during the switch.
2306 static bool is_input_name_malloced
= false;
2308 static int switch_data_file(void)
2310 char *pwd
, *options
[32], *abs_path
[32], *tmp
;
2312 int nr_options
= 0, choice
= -1, ret
= -1;
2313 struct dirent
*dent
;
2315 pwd
= getenv("PWD");
2319 pwd_dir
= opendir(pwd
);
2323 memset(options
, 0, sizeof(options
));
2324 memset(abs_path
, 0, sizeof(abs_path
));
2326 while ((dent
= readdir(pwd_dir
))) {
2327 char path
[PATH_MAX
];
2329 char *name
= dent
->d_name
;
2332 if (!(dent
->d_type
== DT_REG
))
2335 snprintf(path
, sizeof(path
), "%s/%s", pwd
, name
);
2337 file
= fopen(path
, "r");
2341 if (fread(&magic
, 1, 8, file
) < 8)
2342 goto close_file_and_continue
;
2344 if (is_perf_magic(magic
)) {
2345 options
[nr_options
] = strdup(name
);
2346 if (!options
[nr_options
])
2347 goto close_file_and_continue
;
2349 abs_path
[nr_options
] = strdup(path
);
2350 if (!abs_path
[nr_options
]) {
2351 zfree(&options
[nr_options
]);
2352 ui__warning("Can't search all data files due to memory shortage.\n");
2360 close_file_and_continue
:
2362 if (nr_options
>= 32) {
2363 ui__warning("Too many perf data files in PWD!\n"
2364 "Only the first 32 files will be listed.\n");
2371 choice
= ui__popup_menu(nr_options
, options
);
2372 if (choice
< nr_options
&& choice
>= 0) {
2373 tmp
= strdup(abs_path
[choice
]);
2375 if (is_input_name_malloced
)
2376 free((void *)input_name
);
2378 is_input_name_malloced
= true;
2381 ui__warning("Data switch failed due to memory shortage!\n");
2385 free_popup_options(options
, nr_options
);
2386 free_popup_options(abs_path
, nr_options
);
2390 struct popup_action
{
2391 struct thread
*thread
;
2392 struct map_symbol ms
;
2395 int (*fn
)(struct hist_browser
*browser
, struct popup_action
*act
);
2399 do_annotate(struct hist_browser
*browser
, struct popup_action
*act
)
2401 struct perf_evsel
*evsel
;
2402 struct annotation
*notes
;
2403 struct hist_entry
*he
;
2406 if (!objdump_path
&& perf_env__lookup_objdump(browser
->env
))
2409 notes
= symbol__annotation(act
->ms
.sym
);
2413 evsel
= hists_to_evsel(browser
->hists
);
2414 err
= map_symbol__tui_annotate(&act
->ms
, evsel
, browser
->hbt
);
2415 he
= hist_browser__selected_entry(browser
);
2417 * offer option to annotate the other branch source or target
2418 * (if they exists) when returning from annotate
2420 if ((err
== 'q' || err
== CTRL('c')) && he
->branch_info
)
2423 ui_browser__update_nr_entries(&browser
->b
, browser
->hists
->nr_entries
);
2425 ui_browser__handle_resize(&browser
->b
);
2430 add_annotate_opt(struct hist_browser
*browser __maybe_unused
,
2431 struct popup_action
*act
, char **optstr
,
2432 struct map
*map
, struct symbol
*sym
)
2434 if (sym
== NULL
|| map
->dso
->annotate_warned
)
2437 if (asprintf(optstr
, "Annotate %s", sym
->name
) < 0)
2442 act
->fn
= do_annotate
;
2447 do_zoom_thread(struct hist_browser
*browser
, struct popup_action
*act
)
2449 struct thread
*thread
= act
->thread
;
2451 if ((!hists__has(browser
->hists
, thread
) &&
2452 !hists__has(browser
->hists
, comm
)) || thread
== NULL
)
2455 if (browser
->hists
->thread_filter
) {
2456 pstack__remove(browser
->pstack
, &browser
->hists
->thread_filter
);
2457 perf_hpp__set_elide(HISTC_THREAD
, false);
2458 thread__zput(browser
->hists
->thread_filter
);
2461 if (hists__has(browser
->hists
, thread
)) {
2462 ui_helpline__fpush("To zoom out press ESC or ENTER + \"Zoom out of %s(%d) thread\"",
2463 thread
->comm_set
? thread__comm_str(thread
) : "",
2466 ui_helpline__fpush("To zoom out press ESC or ENTER + \"Zoom out of %s thread\"",
2467 thread
->comm_set
? thread__comm_str(thread
) : "");
2470 browser
->hists
->thread_filter
= thread__get(thread
);
2471 perf_hpp__set_elide(HISTC_THREAD
, false);
2472 pstack__push(browser
->pstack
, &browser
->hists
->thread_filter
);
2475 hists__filter_by_thread(browser
->hists
);
2476 hist_browser__reset(browser
);
2481 add_thread_opt(struct hist_browser
*browser
, struct popup_action
*act
,
2482 char **optstr
, struct thread
*thread
)
2486 if ((!hists__has(browser
->hists
, thread
) &&
2487 !hists__has(browser
->hists
, comm
)) || thread
== NULL
)
2490 if (hists__has(browser
->hists
, thread
)) {
2491 ret
= asprintf(optstr
, "Zoom %s %s(%d) thread",
2492 browser
->hists
->thread_filter
? "out of" : "into",
2493 thread
->comm_set
? thread__comm_str(thread
) : "",
2496 ret
= asprintf(optstr
, "Zoom %s %s thread",
2497 browser
->hists
->thread_filter
? "out of" : "into",
2498 thread
->comm_set
? thread__comm_str(thread
) : "");
2503 act
->thread
= thread
;
2504 act
->fn
= do_zoom_thread
;
2509 do_zoom_dso(struct hist_browser
*browser
, struct popup_action
*act
)
2511 struct map
*map
= act
->ms
.map
;
2513 if (!hists__has(browser
->hists
, dso
) || map
== NULL
)
2516 if (browser
->hists
->dso_filter
) {
2517 pstack__remove(browser
->pstack
, &browser
->hists
->dso_filter
);
2518 perf_hpp__set_elide(HISTC_DSO
, false);
2519 browser
->hists
->dso_filter
= NULL
;
2522 ui_helpline__fpush("To zoom out press ESC or ENTER + \"Zoom out of %s DSO\"",
2523 __map__is_kernel(map
) ? "the Kernel" : map
->dso
->short_name
);
2524 browser
->hists
->dso_filter
= map
->dso
;
2525 perf_hpp__set_elide(HISTC_DSO
, true);
2526 pstack__push(browser
->pstack
, &browser
->hists
->dso_filter
);
2529 hists__filter_by_dso(browser
->hists
);
2530 hist_browser__reset(browser
);
2535 add_dso_opt(struct hist_browser
*browser
, struct popup_action
*act
,
2536 char **optstr
, struct map
*map
)
2538 if (!hists__has(browser
->hists
, dso
) || map
== NULL
)
2541 if (asprintf(optstr
, "Zoom %s %s DSO",
2542 browser
->hists
->dso_filter
? "out of" : "into",
2543 __map__is_kernel(map
) ? "the Kernel" : map
->dso
->short_name
) < 0)
2547 act
->fn
= do_zoom_dso
;
2552 do_browse_map(struct hist_browser
*browser __maybe_unused
,
2553 struct popup_action
*act
)
2555 map__browse(act
->ms
.map
);
2560 add_map_opt(struct hist_browser
*browser
,
2561 struct popup_action
*act
, char **optstr
, struct map
*map
)
2563 if (!hists__has(browser
->hists
, dso
) || map
== NULL
)
2566 if (asprintf(optstr
, "Browse map details") < 0)
2570 act
->fn
= do_browse_map
;
2575 do_run_script(struct hist_browser
*browser __maybe_unused
,
2576 struct popup_action
*act
)
2578 char script_opt
[64];
2579 memset(script_opt
, 0, sizeof(script_opt
));
2582 scnprintf(script_opt
, sizeof(script_opt
), " -c %s ",
2583 thread__comm_str(act
->thread
));
2584 } else if (act
->ms
.sym
) {
2585 scnprintf(script_opt
, sizeof(script_opt
), " -S %s ",
2589 script_browse(script_opt
);
2594 add_script_opt(struct hist_browser
*browser __maybe_unused
,
2595 struct popup_action
*act
, char **optstr
,
2596 struct thread
*thread
, struct symbol
*sym
)
2599 if (asprintf(optstr
, "Run scripts for samples of thread [%s]",
2600 thread__comm_str(thread
)) < 0)
2603 if (asprintf(optstr
, "Run scripts for samples of symbol [%s]",
2607 if (asprintf(optstr
, "Run scripts for all samples") < 0)
2611 act
->thread
= thread
;
2613 act
->fn
= do_run_script
;
2618 do_switch_data(struct hist_browser
*browser __maybe_unused
,
2619 struct popup_action
*act __maybe_unused
)
2621 if (switch_data_file()) {
2622 ui__warning("Won't switch the data files due to\n"
2623 "no valid data file get selected!\n");
2627 return K_SWITCH_INPUT_DATA
;
2631 add_switch_opt(struct hist_browser
*browser
,
2632 struct popup_action
*act
, char **optstr
)
2634 if (!is_report_browser(browser
->hbt
))
2637 if (asprintf(optstr
, "Switch to another data file in PWD") < 0)
2640 act
->fn
= do_switch_data
;
2645 do_exit_browser(struct hist_browser
*browser __maybe_unused
,
2646 struct popup_action
*act __maybe_unused
)
2652 add_exit_opt(struct hist_browser
*browser __maybe_unused
,
2653 struct popup_action
*act
, char **optstr
)
2655 if (asprintf(optstr
, "Exit") < 0)
2658 act
->fn
= do_exit_browser
;
2663 do_zoom_socket(struct hist_browser
*browser
, struct popup_action
*act
)
2665 if (!hists__has(browser
->hists
, socket
) || act
->socket
< 0)
2668 if (browser
->hists
->socket_filter
> -1) {
2669 pstack__remove(browser
->pstack
, &browser
->hists
->socket_filter
);
2670 browser
->hists
->socket_filter
= -1;
2671 perf_hpp__set_elide(HISTC_SOCKET
, false);
2673 browser
->hists
->socket_filter
= act
->socket
;
2674 perf_hpp__set_elide(HISTC_SOCKET
, true);
2675 pstack__push(browser
->pstack
, &browser
->hists
->socket_filter
);
2678 hists__filter_by_socket(browser
->hists
);
2679 hist_browser__reset(browser
);
2684 add_socket_opt(struct hist_browser
*browser
, struct popup_action
*act
,
2685 char **optstr
, int socket_id
)
2687 if (!hists__has(browser
->hists
, socket
) || socket_id
< 0)
2690 if (asprintf(optstr
, "Zoom %s Processor Socket %d",
2691 (browser
->hists
->socket_filter
> -1) ? "out of" : "into",
2695 act
->socket
= socket_id
;
2696 act
->fn
= do_zoom_socket
;
2700 static void hist_browser__update_nr_entries(struct hist_browser
*hb
)
2703 struct rb_node
*nd
= rb_first(&hb
->hists
->entries
);
2705 if (hb
->min_pcnt
== 0 && !symbol_conf
.report_hierarchy
) {
2706 hb
->nr_non_filtered_entries
= hb
->hists
->nr_non_filtered_entries
;
2710 while ((nd
= hists__filter_entries(nd
, hb
->min_pcnt
)) != NULL
) {
2712 nd
= rb_hierarchy_next(nd
);
2715 hb
->nr_non_filtered_entries
= nr_entries
;
2716 hb
->nr_hierarchy_entries
= nr_entries
;
2719 static void hist_browser__update_percent_limit(struct hist_browser
*hb
,
2722 struct hist_entry
*he
;
2723 struct rb_node
*nd
= rb_first(&hb
->hists
->entries
);
2724 u64 total
= hists__total_period(hb
->hists
);
2725 u64 min_callchain_hits
= total
* (percent
/ 100);
2727 hb
->min_pcnt
= callchain_param
.min_percent
= percent
;
2729 while ((nd
= hists__filter_entries(nd
, hb
->min_pcnt
)) != NULL
) {
2730 he
= rb_entry(nd
, struct hist_entry
, rb_node
);
2732 if (he
->has_no_entry
) {
2733 he
->has_no_entry
= false;
2737 if (!he
->leaf
|| !symbol_conf
.use_callchain
)
2740 if (callchain_param
.mode
== CHAIN_GRAPH_REL
) {
2741 total
= he
->stat
.period
;
2743 if (symbol_conf
.cumulate_callchain
)
2744 total
= he
->stat_acc
->period
;
2746 min_callchain_hits
= total
* (percent
/ 100);
2749 callchain_param
.sort(&he
->sorted_chain
, he
->callchain
,
2750 min_callchain_hits
, &callchain_param
);
2753 nd
= __rb_hierarchy_next(nd
, HMD_FORCE_CHILD
);
2755 /* force to re-evaluate folding state of callchains */
2756 he
->init_have_children
= false;
2757 hist_entry__set_folding(he
, hb
, false);
2761 static int perf_evsel__hists_browse(struct perf_evsel
*evsel
, int nr_events
,
2762 const char *helpline
,
2764 struct hist_browser_timer
*hbt
,
2766 struct perf_env
*env
)
2768 struct hists
*hists
= evsel__hists(evsel
);
2769 struct hist_browser
*browser
= perf_evsel_browser__new(evsel
, hbt
, env
);
2770 struct branch_info
*bi
;
2771 #define MAX_OPTIONS 16
2772 char *options
[MAX_OPTIONS
];
2773 struct popup_action actions
[MAX_OPTIONS
];
2777 int delay_secs
= hbt
? hbt
->refresh
: 0;
2779 #define HIST_BROWSER_HELP_COMMON \
2780 "h/?/F1 Show this window\n" \
2782 "PGDN/SPACE Navigate\n" \
2783 "q/ESC/CTRL+C Exit browser\n\n" \
2784 "For multiple event sessions:\n\n" \
2785 "TAB/UNTAB Switch events\n\n" \
2786 "For symbolic views (--sort has sym):\n\n" \
2787 "ENTER Zoom into DSO/Threads & Annotate current symbol\n" \
2789 "a Annotate current symbol\n" \
2790 "C Collapse all callchains\n" \
2791 "d Zoom into current DSO\n" \
2792 "E Expand all callchains\n" \
2793 "F Toggle percentage of filtered entries\n" \
2794 "H Display column headers\n" \
2795 "L Change percent limit\n" \
2796 "m Display context menu\n" \
2797 "S Zoom into current Processor Socket\n" \
2799 /* help messages are sorted by lexical order of the hotkey */
2800 const char report_help
[] = HIST_BROWSER_HELP_COMMON
2801 "i Show header information\n"
2802 "P Print histograms to perf.hist.N\n"
2803 "r Run available scripts\n"
2804 "s Switch to another data file in PWD\n"
2805 "t Zoom into current Thread\n"
2806 "V Verbose (DSO names in callchains, etc)\n"
2807 "/ Filter symbol by name";
2808 const char top_help
[] = HIST_BROWSER_HELP_COMMON
2809 "P Print histograms to perf.hist.N\n"
2810 "t Zoom into current Thread\n"
2811 "V Verbose (DSO names in callchains, etc)\n"
2812 "z Toggle zeroing of samples\n"
2813 "f Enable/Disable events\n"
2814 "/ Filter symbol by name";
2816 if (browser
== NULL
)
2819 /* reset abort key so that it can get Ctrl-C as a key */
2821 SLang_init_tty(0, 0, 0);
2824 browser
->min_pcnt
= min_pcnt
;
2825 hist_browser__update_nr_entries(browser
);
2827 browser
->pstack
= pstack__new(3);
2828 if (browser
->pstack
== NULL
)
2831 ui_helpline__push(helpline
);
2833 memset(options
, 0, sizeof(options
));
2834 memset(actions
, 0, sizeof(actions
));
2836 if (symbol_conf
.col_width_list_str
)
2837 perf_hpp__set_user_width(symbol_conf
.col_width_list_str
);
2840 struct thread
*thread
= NULL
;
2841 struct map
*map
= NULL
;
2847 key
= hist_browser__run(browser
, helpline
);
2849 if (browser
->he_selection
!= NULL
) {
2850 thread
= hist_browser__selected_thread(browser
);
2851 map
= browser
->selection
->map
;
2852 socked_id
= browser
->he_selection
->socket
;
2860 * Exit the browser, let hists__browser_tree
2861 * go to the next or previous
2863 goto out_free_stack
;
2865 if (!hists__has(hists
, sym
)) {
2866 ui_browser__warning(&browser
->b
, delay_secs
* 2,
2867 "Annotation is only available for symbolic views, "
2868 "include \"sym*\" in --sort to use it.");
2872 if (browser
->selection
== NULL
||
2873 browser
->selection
->sym
== NULL
||
2874 browser
->selection
->map
->dso
->annotate_warned
)
2877 actions
->ms
.map
= browser
->selection
->map
;
2878 actions
->ms
.sym
= browser
->selection
->sym
;
2879 do_annotate(browser
, actions
);
2882 hist_browser__dump(browser
);
2885 actions
->ms
.map
= map
;
2886 do_zoom_dso(browser
, actions
);
2889 verbose
= (verbose
+ 1) % 4;
2890 browser
->show_dso
= verbose
> 0;
2891 ui_helpline__fpush("Verbosity level set to %d\n",
2895 actions
->thread
= thread
;
2896 do_zoom_thread(browser
, actions
);
2899 actions
->socket
= socked_id
;
2900 do_zoom_socket(browser
, actions
);
2903 if (ui_browser__input_window("Symbol to show",
2904 "Please enter the name of symbol you want to see.\n"
2905 "To remove the filter later, press / + ENTER.",
2906 buf
, "ENTER: OK, ESC: Cancel",
2907 delay_secs
* 2) == K_ENTER
) {
2908 hists
->symbol_filter_str
= *buf
? buf
: NULL
;
2909 hists__filter_by_symbol(hists
);
2910 hist_browser__reset(browser
);
2914 if (is_report_browser(hbt
)) {
2915 actions
->thread
= NULL
;
2916 actions
->ms
.sym
= NULL
;
2917 do_run_script(browser
, actions
);
2921 if (is_report_browser(hbt
)) {
2922 key
= do_switch_data(browser
, actions
);
2923 if (key
== K_SWITCH_INPUT_DATA
)
2924 goto out_free_stack
;
2928 /* env->arch is NULL for live-mode (i.e. perf top) */
2930 tui__header_window(env
);
2933 symbol_conf
.filter_relative
^= 1;
2936 if (!is_report_browser(hbt
)) {
2937 struct perf_top
*top
= hbt
->arg
;
2939 top
->zero
= !top
->zero
;
2943 if (ui_browser__input_window("Percent Limit",
2944 "Please enter the value you want to hide entries under that percent.",
2945 buf
, "ENTER: OK, ESC: Cancel",
2946 delay_secs
* 2) == K_ENTER
) {
2948 double new_percent
= strtod(buf
, &end
);
2950 if (new_percent
< 0 || new_percent
> 100) {
2951 ui_browser__warning(&browser
->b
, delay_secs
* 2,
2952 "Invalid percent: %.2f", new_percent
);
2956 hist_browser__update_percent_limit(browser
, new_percent
);
2957 hist_browser__reset(browser
);
2963 ui_browser__help_window(&browser
->b
,
2964 is_report_browser(hbt
) ? report_help
: top_help
);
2975 if (pstack__empty(browser
->pstack
)) {
2977 * Go back to the perf_evsel_menu__run or other user
2980 goto out_free_stack
;
2983 ui_browser__dialog_yesno(&browser
->b
,
2984 "Do you really want to exit?"))
2985 goto out_free_stack
;
2989 top
= pstack__peek(browser
->pstack
);
2990 if (top
== &browser
->hists
->dso_filter
) {
2992 * No need to set actions->dso here since
2993 * it's just to remove the current filter.
2994 * Ditto for thread below.
2996 do_zoom_dso(browser
, actions
);
2997 } else if (top
== &browser
->hists
->thread_filter
) {
2998 do_zoom_thread(browser
, actions
);
2999 } else if (top
== &browser
->hists
->socket_filter
) {
3000 do_zoom_socket(browser
, actions
);
3006 goto out_free_stack
;
3008 if (!is_report_browser(hbt
)) {
3009 struct perf_top
*top
= hbt
->arg
;
3011 perf_evlist__toggle_enable(top
->evlist
);
3013 * No need to refresh, resort/decay histogram
3014 * entries if we are not collecting samples:
3016 if (top
->evlist
->enabled
) {
3017 helpline
= "Press 'f' to disable the events or 'h' to see other hotkeys";
3018 hbt
->refresh
= delay_secs
;
3020 helpline
= "Press 'f' again to re-enable the events";
3027 helpline
= "Press '?' for help on key bindings";
3031 if (!hists__has(hists
, sym
) || browser
->selection
== NULL
)
3032 goto skip_annotation
;
3034 if (sort__mode
== SORT_MODE__BRANCH
) {
3035 bi
= browser
->he_selection
->branch_info
;
3038 goto skip_annotation
;
3040 nr_options
+= add_annotate_opt(browser
,
3041 &actions
[nr_options
],
3042 &options
[nr_options
],
3045 if (bi
->to
.sym
!= bi
->from
.sym
)
3046 nr_options
+= add_annotate_opt(browser
,
3047 &actions
[nr_options
],
3048 &options
[nr_options
],
3052 nr_options
+= add_annotate_opt(browser
,
3053 &actions
[nr_options
],
3054 &options
[nr_options
],
3055 browser
->selection
->map
,
3056 browser
->selection
->sym
);
3059 nr_options
+= add_thread_opt(browser
, &actions
[nr_options
],
3060 &options
[nr_options
], thread
);
3061 nr_options
+= add_dso_opt(browser
, &actions
[nr_options
],
3062 &options
[nr_options
], map
);
3063 nr_options
+= add_map_opt(browser
, &actions
[nr_options
],
3064 &options
[nr_options
],
3065 browser
->selection
?
3066 browser
->selection
->map
: NULL
);
3067 nr_options
+= add_socket_opt(browser
, &actions
[nr_options
],
3068 &options
[nr_options
],
3070 /* perf script support */
3071 if (!is_report_browser(hbt
))
3072 goto skip_scripting
;
3074 if (browser
->he_selection
) {
3075 if (hists__has(hists
, thread
) && thread
) {
3076 nr_options
+= add_script_opt(browser
,
3077 &actions
[nr_options
],
3078 &options
[nr_options
],
3082 * Note that browser->selection != NULL
3083 * when browser->he_selection is not NULL,
3084 * so we don't need to check browser->selection
3085 * before fetching browser->selection->sym like what
3086 * we do before fetching browser->selection->map.
3088 * See hist_browser__show_entry.
3090 if (hists__has(hists
, sym
) && browser
->selection
->sym
) {
3091 nr_options
+= add_script_opt(browser
,
3092 &actions
[nr_options
],
3093 &options
[nr_options
],
3094 NULL
, browser
->selection
->sym
);
3097 nr_options
+= add_script_opt(browser
, &actions
[nr_options
],
3098 &options
[nr_options
], NULL
, NULL
);
3099 nr_options
+= add_switch_opt(browser
, &actions
[nr_options
],
3100 &options
[nr_options
]);
3102 nr_options
+= add_exit_opt(browser
, &actions
[nr_options
],
3103 &options
[nr_options
]);
3106 struct popup_action
*act
;
3108 choice
= ui__popup_menu(nr_options
, options
);
3109 if (choice
== -1 || choice
>= nr_options
)
3112 act
= &actions
[choice
];
3113 key
= act
->fn(browser
, act
);
3116 if (key
== K_SWITCH_INPUT_DATA
)
3120 pstack__delete(browser
->pstack
);
3122 hist_browser__delete(browser
);
3123 free_popup_options(options
, MAX_OPTIONS
);
3127 struct perf_evsel_menu
{
3128 struct ui_browser b
;
3129 struct perf_evsel
*selection
;
3130 bool lost_events
, lost_events_warned
;
3132 struct perf_env
*env
;
3135 static void perf_evsel_menu__write(struct ui_browser
*browser
,
3136 void *entry
, int row
)
3138 struct perf_evsel_menu
*menu
= container_of(browser
,
3139 struct perf_evsel_menu
, b
);
3140 struct perf_evsel
*evsel
= list_entry(entry
, struct perf_evsel
, node
);
3141 struct hists
*hists
= evsel__hists(evsel
);
3142 bool current_entry
= ui_browser__is_current_entry(browser
, row
);
3143 unsigned long nr_events
= hists
->stats
.nr_events
[PERF_RECORD_SAMPLE
];
3144 const char *ev_name
= perf_evsel__name(evsel
);
3146 const char *warn
= " ";
3149 ui_browser__set_color(browser
, current_entry
? HE_COLORSET_SELECTED
:
3150 HE_COLORSET_NORMAL
);
3152 if (perf_evsel__is_group_event(evsel
)) {
3153 struct perf_evsel
*pos
;
3155 ev_name
= perf_evsel__group_name(evsel
);
3157 for_each_group_member(pos
, evsel
) {
3158 struct hists
*pos_hists
= evsel__hists(pos
);
3159 nr_events
+= pos_hists
->stats
.nr_events
[PERF_RECORD_SAMPLE
];
3163 nr_events
= convert_unit(nr_events
, &unit
);
3164 printed
= scnprintf(bf
, sizeof(bf
), "%lu%c%s%s", nr_events
,
3165 unit
, unit
== ' ' ? "" : " ", ev_name
);
3166 ui_browser__printf(browser
, "%s", bf
);
3168 nr_events
= hists
->stats
.nr_events
[PERF_RECORD_LOST
];
3169 if (nr_events
!= 0) {
3170 menu
->lost_events
= true;
3172 ui_browser__set_color(browser
, HE_COLORSET_TOP
);
3173 nr_events
= convert_unit(nr_events
, &unit
);
3174 printed
+= scnprintf(bf
, sizeof(bf
), ": %ld%c%schunks LOST!",
3175 nr_events
, unit
, unit
== ' ' ? "" : " ");
3179 ui_browser__write_nstring(browser
, warn
, browser
->width
- printed
);
3182 menu
->selection
= evsel
;
3185 static int perf_evsel_menu__run(struct perf_evsel_menu
*menu
,
3186 int nr_events
, const char *help
,
3187 struct hist_browser_timer
*hbt
)
3189 struct perf_evlist
*evlist
= menu
->b
.priv
;
3190 struct perf_evsel
*pos
;
3191 const char *title
= "Available samples";
3192 int delay_secs
= hbt
? hbt
->refresh
: 0;
3195 if (ui_browser__show(&menu
->b
, title
,
3196 "ESC: exit, ENTER|->: Browse histograms") < 0)
3200 key
= ui_browser__run(&menu
->b
, delay_secs
);
3204 hbt
->timer(hbt
->arg
);
3206 if (!menu
->lost_events_warned
&& menu
->lost_events
) {
3207 ui_browser__warn_lost_events(&menu
->b
);
3208 menu
->lost_events_warned
= true;
3213 if (!menu
->selection
)
3215 pos
= menu
->selection
;
3217 perf_evlist__set_selected(evlist
, pos
);
3219 * Give the calling tool a chance to populate the non
3220 * default evsel resorted hists tree.
3223 hbt
->timer(hbt
->arg
);
3224 key
= perf_evsel__hists_browse(pos
, nr_events
, help
,
3228 ui_browser__show_title(&menu
->b
, title
);
3231 if (pos
->node
.next
== &evlist
->entries
)
3232 pos
= perf_evlist__first(evlist
);
3234 pos
= perf_evsel__next(pos
);
3237 if (pos
->node
.prev
== &evlist
->entries
)
3238 pos
= perf_evlist__last(evlist
);
3240 pos
= perf_evsel__prev(pos
);
3242 case K_SWITCH_INPUT_DATA
:
3253 if (!ui_browser__dialog_yesno(&menu
->b
,
3254 "Do you really want to exit?"))
3266 ui_browser__hide(&menu
->b
);
3270 static bool filter_group_entries(struct ui_browser
*browser __maybe_unused
,
3273 struct perf_evsel
*evsel
= list_entry(entry
, struct perf_evsel
, node
);
3275 if (symbol_conf
.event_group
&& !perf_evsel__is_group_leader(evsel
))
3281 static int __perf_evlist__tui_browse_hists(struct perf_evlist
*evlist
,
3282 int nr_entries
, const char *help
,
3283 struct hist_browser_timer
*hbt
,
3285 struct perf_env
*env
)
3287 struct perf_evsel
*pos
;
3288 struct perf_evsel_menu menu
= {
3290 .entries
= &evlist
->entries
,
3291 .refresh
= ui_browser__list_head_refresh
,
3292 .seek
= ui_browser__list_head_seek
,
3293 .write
= perf_evsel_menu__write
,
3294 .filter
= filter_group_entries
,
3295 .nr_entries
= nr_entries
,
3298 .min_pcnt
= min_pcnt
,
3302 ui_helpline__push("Press ESC to exit");
3304 evlist__for_each_entry(evlist
, pos
) {
3305 const char *ev_name
= perf_evsel__name(pos
);
3306 size_t line_len
= strlen(ev_name
) + 7;
3308 if (menu
.b
.width
< line_len
)
3309 menu
.b
.width
= line_len
;
3312 return perf_evsel_menu__run(&menu
, nr_entries
, help
, hbt
);
3315 int perf_evlist__tui_browse_hists(struct perf_evlist
*evlist
, const char *help
,
3316 struct hist_browser_timer
*hbt
,
3318 struct perf_env
*env
)
3320 int nr_entries
= evlist
->nr_entries
;
3323 if (nr_entries
== 1) {
3324 struct perf_evsel
*first
= perf_evlist__first(evlist
);
3326 return perf_evsel__hists_browse(first
, nr_entries
, help
,
3327 false, hbt
, min_pcnt
,
3331 if (symbol_conf
.event_group
) {
3332 struct perf_evsel
*pos
;
3335 evlist__for_each_entry(evlist
, pos
) {
3336 if (perf_evsel__is_group_leader(pos
))
3340 if (nr_entries
== 1)
3344 return __perf_evlist__tui_browse_hists(evlist
, nr_entries
, help
,
3345 hbt
, min_pcnt
, env
);