1 // SPDX-License-Identifier: GPL-2.0
3 #include "../callchain.h"
7 #include "../helpline.h"
8 #include "../string2.h"
12 #include <linux/string.h>
14 #define MAX_COLUMNS 32
16 static int __percent_color_snprintf(struct perf_hpp
*hpp
, const char *fmt
, ...)
24 size_t size
= hpp
->size
;
27 len
= va_arg(args
, int);
28 percent
= va_arg(args
, double);
31 markup
= perf_gtk__get_percent_color(percent
);
33 ret
+= scnprintf(buf
, size
, markup
);
35 ret
+= scnprintf(buf
+ ret
, size
- ret
, fmt
, len
, percent
);
38 ret
+= scnprintf(buf
+ ret
, size
- ret
, "</span>");
43 #define __HPP_COLOR_PERCENT_FN(_type, _field) \
44 static u64 he_get_##_field(struct hist_entry *he) \
46 return he->stat._field; \
49 static int perf_gtk__hpp_color_##_type(struct perf_hpp_fmt *fmt, \
50 struct perf_hpp *hpp, \
51 struct hist_entry *he) \
53 return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%", \
54 __percent_color_snprintf, true); \
57 #define __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
58 static u64 he_get_acc_##_field(struct hist_entry *he) \
60 return he->stat_acc->_field; \
63 static int perf_gtk__hpp_color_##_type(struct perf_hpp_fmt *fmt, \
64 struct perf_hpp *hpp, \
65 struct hist_entry *he) \
67 return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", \
68 __percent_color_snprintf, true); \
71 __HPP_COLOR_PERCENT_FN(overhead
, period
)
72 __HPP_COLOR_PERCENT_FN(overhead_sys
, period_sys
)
73 __HPP_COLOR_PERCENT_FN(overhead_us
, period_us
)
74 __HPP_COLOR_PERCENT_FN(overhead_guest_sys
, period_guest_sys
)
75 __HPP_COLOR_PERCENT_FN(overhead_guest_us
, period_guest_us
)
76 __HPP_COLOR_ACC_PERCENT_FN(overhead_acc
, period
)
78 #undef __HPP_COLOR_PERCENT_FN
81 void perf_gtk__init_hpp(void)
83 perf_hpp__format
[PERF_HPP__OVERHEAD
].color
=
84 perf_gtk__hpp_color_overhead
;
85 perf_hpp__format
[PERF_HPP__OVERHEAD_SYS
].color
=
86 perf_gtk__hpp_color_overhead_sys
;
87 perf_hpp__format
[PERF_HPP__OVERHEAD_US
].color
=
88 perf_gtk__hpp_color_overhead_us
;
89 perf_hpp__format
[PERF_HPP__OVERHEAD_GUEST_SYS
].color
=
90 perf_gtk__hpp_color_overhead_guest_sys
;
91 perf_hpp__format
[PERF_HPP__OVERHEAD_GUEST_US
].color
=
92 perf_gtk__hpp_color_overhead_guest_us
;
93 perf_hpp__format
[PERF_HPP__OVERHEAD_ACC
].color
=
94 perf_gtk__hpp_color_overhead_acc
;
97 static void perf_gtk__add_callchain_flat(struct rb_root
*root
, GtkTreeStore
*store
,
98 GtkTreeIter
*parent
, int col
, u64 total
)
101 bool has_single_node
= (rb_first(root
) == rb_last(root
));
103 for (nd
= rb_first(root
); nd
; nd
= rb_next(nd
)) {
104 struct callchain_node
*node
;
105 struct callchain_list
*chain
;
106 GtkTreeIter iter
, new_parent
;
107 bool need_new_parent
;
109 node
= rb_entry(nd
, struct callchain_node
, rb_node
);
111 new_parent
= *parent
;
112 need_new_parent
= !has_single_node
;
114 callchain_node__make_parent_list(node
);
116 list_for_each_entry(chain
, &node
->parent_val
, list
) {
119 gtk_tree_store_append(store
, &iter
, &new_parent
);
121 callchain_node__scnprintf_value(node
, buf
, sizeof(buf
), total
);
122 gtk_tree_store_set(store
, &iter
, 0, buf
, -1);
124 callchain_list__sym_name(chain
, buf
, sizeof(buf
), false);
125 gtk_tree_store_set(store
, &iter
, col
, buf
, -1);
127 if (need_new_parent
) {
129 * Only show the top-most symbol in a callchain
130 * if it's not the only callchain.
133 need_new_parent
= false;
137 list_for_each_entry(chain
, &node
->val
, list
) {
140 gtk_tree_store_append(store
, &iter
, &new_parent
);
142 callchain_node__scnprintf_value(node
, buf
, sizeof(buf
), total
);
143 gtk_tree_store_set(store
, &iter
, 0, buf
, -1);
145 callchain_list__sym_name(chain
, buf
, sizeof(buf
), false);
146 gtk_tree_store_set(store
, &iter
, col
, buf
, -1);
148 if (need_new_parent
) {
150 * Only show the top-most symbol in a callchain
151 * if it's not the only callchain.
154 need_new_parent
= false;
160 static void perf_gtk__add_callchain_folded(struct rb_root
*root
, GtkTreeStore
*store
,
161 GtkTreeIter
*parent
, int col
, u64 total
)
165 for (nd
= rb_first(root
); nd
; nd
= rb_next(nd
)) {
166 struct callchain_node
*node
;
167 struct callchain_list
*chain
;
170 char *str
, *str_alloc
= NULL
;
173 node
= rb_entry(nd
, struct callchain_node
, rb_node
);
175 callchain_node__make_parent_list(node
);
177 list_for_each_entry(chain
, &node
->parent_val
, list
) {
180 callchain_list__sym_name(chain
, name
, sizeof(name
), false);
182 if (asprintf(&str
, "%s%s%s",
183 first
? "" : str_alloc
,
184 first
? "" : symbol_conf
.field_sep
?: "; ",
193 list_for_each_entry(chain
, &node
->val
, list
) {
196 callchain_list__sym_name(chain
, name
, sizeof(name
), false);
198 if (asprintf(&str
, "%s%s%s",
199 first
? "" : str_alloc
,
200 first
? "" : symbol_conf
.field_sep
?: "; ",
209 gtk_tree_store_append(store
, &iter
, parent
);
211 callchain_node__scnprintf_value(node
, buf
, sizeof(buf
), total
);
212 gtk_tree_store_set(store
, &iter
, 0, buf
, -1);
214 gtk_tree_store_set(store
, &iter
, col
, str
, -1);
220 static void perf_gtk__add_callchain_graph(struct rb_root
*root
, GtkTreeStore
*store
,
221 GtkTreeIter
*parent
, int col
, u64 total
)
224 bool has_single_node
= (rb_first(root
) == rb_last(root
));
226 for (nd
= rb_first(root
); nd
; nd
= rb_next(nd
)) {
227 struct callchain_node
*node
;
228 struct callchain_list
*chain
;
229 GtkTreeIter iter
, new_parent
;
230 bool need_new_parent
;
233 node
= rb_entry(nd
, struct callchain_node
, rb_node
);
235 new_parent
= *parent
;
236 need_new_parent
= !has_single_node
&& (node
->val_nr
> 1);
238 list_for_each_entry(chain
, &node
->val
, list
) {
241 gtk_tree_store_append(store
, &iter
, &new_parent
);
243 callchain_node__scnprintf_value(node
, buf
, sizeof(buf
), total
);
244 gtk_tree_store_set(store
, &iter
, 0, buf
, -1);
246 callchain_list__sym_name(chain
, buf
, sizeof(buf
), false);
247 gtk_tree_store_set(store
, &iter
, col
, buf
, -1);
249 if (need_new_parent
) {
251 * Only show the top-most symbol in a callchain
252 * if it's not the only callchain.
255 need_new_parent
= false;
259 if (callchain_param
.mode
== CHAIN_GRAPH_REL
)
260 child_total
= node
->children_hit
;
264 /* Now 'iter' contains info of the last callchain_list */
265 perf_gtk__add_callchain_graph(&node
->rb_root
, store
, &iter
, col
,
270 static void perf_gtk__add_callchain(struct rb_root
*root
, GtkTreeStore
*store
,
271 GtkTreeIter
*parent
, int col
, u64 total
)
273 if (callchain_param
.mode
== CHAIN_FLAT
)
274 perf_gtk__add_callchain_flat(root
, store
, parent
, col
, total
);
275 else if (callchain_param
.mode
== CHAIN_FOLDED
)
276 perf_gtk__add_callchain_folded(root
, store
, parent
, col
, total
);
278 perf_gtk__add_callchain_graph(root
, store
, parent
, col
, total
);
281 static void on_row_activated(GtkTreeView
*view
, GtkTreePath
*path
,
282 GtkTreeViewColumn
*col __maybe_unused
,
283 gpointer user_data __maybe_unused
)
285 bool expanded
= gtk_tree_view_row_expanded(view
, path
);
288 gtk_tree_view_collapse_row(view
, path
);
290 gtk_tree_view_expand_row(view
, path
, FALSE
);
293 static void perf_gtk__show_hists(GtkWidget
*window
, struct hists
*hists
,
296 struct perf_hpp_fmt
*fmt
;
297 GType col_types
[MAX_COLUMNS
];
298 GtkCellRenderer
*renderer
;
307 struct perf_hpp hpp
= {
314 hists__for_each_format(hists
, fmt
)
315 col_types
[nr_cols
++] = G_TYPE_STRING
;
317 store
= gtk_tree_store_newv(nr_cols
, col_types
);
319 view
= gtk_tree_view_new();
321 renderer
= gtk_cell_renderer_text_new();
325 hists__for_each_format(hists
, fmt
) {
326 if (perf_hpp__should_skip(fmt
, hists
))
330 * XXX no way to determine where symcol column is..
331 * Just use last column for now.
333 if (perf_hpp__is_sort_entry(fmt
))
336 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view
),
342 for (col_idx
= 0; col_idx
< nr_cols
; col_idx
++) {
343 GtkTreeViewColumn
*column
;
345 column
= gtk_tree_view_get_column(GTK_TREE_VIEW(view
), col_idx
);
346 gtk_tree_view_column_set_resizable(column
, TRUE
);
348 if (col_idx
== sym_col
) {
349 gtk_tree_view_set_expander_column(GTK_TREE_VIEW(view
),
354 gtk_tree_view_set_model(GTK_TREE_VIEW(view
), GTK_TREE_MODEL(store
));
356 g_object_unref(GTK_TREE_MODEL(store
));
358 for (nd
= rb_first_cached(&hists
->entries
); nd
; nd
= rb_next(nd
)) {
359 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
361 u64 total
= hists__total_period(h
->hists
);
367 percent
= hist_entry__get_percent_limit(h
);
368 if (percent
< min_pcnt
)
371 gtk_tree_store_append(store
, &iter
, NULL
);
375 hists__for_each_format(hists
, fmt
) {
376 if (perf_hpp__should_skip(fmt
, h
->hists
))
380 fmt
->color(fmt
, &hpp
, h
);
382 fmt
->entry(fmt
, &hpp
, h
);
384 gtk_tree_store_set(store
, &iter
, col_idx
++, s
, -1);
387 if (hist_entry__has_callchains(h
) &&
388 symbol_conf
.use_callchain
&& hists__has(hists
, sym
)) {
389 if (callchain_param
.mode
== CHAIN_GRAPH_REL
)
390 total
= symbol_conf
.cumulate_callchain
?
391 h
->stat_acc
->period
: h
->stat
.period
;
393 perf_gtk__add_callchain(&h
->sorted_chain
, store
, &iter
,
398 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(view
), TRUE
);
400 g_signal_connect(view
, "row-activated",
401 G_CALLBACK(on_row_activated
), NULL
);
402 gtk_container_add(GTK_CONTAINER(window
), view
);
405 static void perf_gtk__add_hierarchy_entries(struct hists
*hists
,
406 struct rb_root_cached
*root
,
409 struct perf_hpp
*hpp
,
413 struct rb_node
*node
;
414 struct hist_entry
*he
;
415 struct perf_hpp_fmt
*fmt
;
416 struct perf_hpp_list_node
*fmt_node
;
417 u64 total
= hists__total_period(hists
);
420 for (node
= rb_first_cached(root
); node
; node
= rb_next(node
)) {
425 he
= rb_entry(node
, struct hist_entry
, rb_node
);
429 percent
= hist_entry__get_percent_limit(he
);
430 if (percent
< min_pcnt
)
433 gtk_tree_store_append(store
, &iter
, parent
);
437 /* the first hpp_list_node is for overhead columns */
438 fmt_node
= list_first_entry(&hists
->hpp_formats
,
439 struct perf_hpp_list_node
, list
);
440 perf_hpp_list__for_each_format(&fmt_node
->hpp
, fmt
) {
442 fmt
->color(fmt
, hpp
, he
);
444 fmt
->entry(fmt
, hpp
, he
);
446 gtk_tree_store_set(store
, &iter
, col_idx
++, hpp
->buf
, -1);
451 perf_hpp_list__for_each_format(he
->hpp_list
, fmt
) {
455 ret
= fmt
->color(fmt
, hpp
, he
);
457 ret
= fmt
->entry(fmt
, hpp
, he
);
459 snprintf(hpp
->buf
+ ret
, hpp
->size
- ret
, " ");
460 advance_hpp(hpp
, ret
+ 2);
463 gtk_tree_store_set(store
, &iter
, col_idx
, strim(bf
), -1);
469 perf_gtk__add_hierarchy_entries(hists
, &he
->hroot_out
,
473 if (!hist_entry__has_hierarchy_children(he
, min_pcnt
)) {
477 snprintf(buf
, sizeof(buf
), "no entry >= %.2f%%",
480 gtk_tree_store_append(store
, &child
, &iter
);
481 gtk_tree_store_set(store
, &child
, col_idx
, buf
, -1);
485 if (he
->leaf
&& hist_entry__has_callchains(he
) && symbol_conf
.use_callchain
) {
486 if (callchain_param
.mode
== CHAIN_GRAPH_REL
)
487 total
= symbol_conf
.cumulate_callchain
?
488 he
->stat_acc
->period
: he
->stat
.period
;
490 perf_gtk__add_callchain(&he
->sorted_chain
, store
, &iter
,
497 static void perf_gtk__show_hierarchy(GtkWidget
*window
, struct hists
*hists
,
500 struct perf_hpp_fmt
*fmt
;
501 struct perf_hpp_list_node
*fmt_node
;
502 GType col_types
[MAX_COLUMNS
];
503 GtkCellRenderer
*renderer
;
510 bool first_node
, first_col
;
511 struct perf_hpp hpp
= {
516 hists__for_each_format(hists
, fmt
) {
517 if (perf_hpp__is_sort_entry(fmt
) ||
518 perf_hpp__is_dynamic_entry(fmt
))
521 col_types
[nr_cols
++] = G_TYPE_STRING
;
523 col_types
[nr_cols
++] = G_TYPE_STRING
;
525 store
= gtk_tree_store_newv(nr_cols
, col_types
);
526 view
= gtk_tree_view_new();
527 renderer
= gtk_cell_renderer_text_new();
531 /* the first hpp_list_node is for overhead columns */
532 fmt_node
= list_first_entry(&hists
->hpp_formats
,
533 struct perf_hpp_list_node
, list
);
534 perf_hpp_list__for_each_format(&fmt_node
->hpp
, fmt
) {
535 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view
),
541 /* construct merged column header since sort keys share single column */
544 list_for_each_entry_continue(fmt_node
, &hists
->hpp_formats
, list
) {
550 perf_hpp_list__for_each_format(&fmt_node
->hpp
,fmt
) {
551 if (perf_hpp__should_skip(fmt
, hists
))
558 fmt
->header(fmt
, &hpp
, hists
, 0, NULL
);
559 strcat(buf
, strim(hpp
.buf
));
563 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view
),
568 for (col_idx
= 0; col_idx
< nr_cols
; col_idx
++) {
569 GtkTreeViewColumn
*column
;
571 column
= gtk_tree_view_get_column(GTK_TREE_VIEW(view
), col_idx
);
572 gtk_tree_view_column_set_resizable(column
, TRUE
);
575 gtk_tree_view_set_expander_column(GTK_TREE_VIEW(view
),
580 gtk_tree_view_set_model(GTK_TREE_VIEW(view
), GTK_TREE_MODEL(store
));
581 g_object_unref(GTK_TREE_MODEL(store
));
583 perf_gtk__add_hierarchy_entries(hists
, &hists
->entries
, store
,
584 NULL
, &hpp
, min_pcnt
);
586 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(view
), TRUE
);
588 g_signal_connect(view
, "row-activated",
589 G_CALLBACK(on_row_activated
), NULL
);
590 gtk_container_add(GTK_CONTAINER(window
), view
);
593 int evlist__gtk_browse_hists(struct evlist
*evlist
, const char *help
,
594 struct hist_browser_timer
*hbt __maybe_unused
, float min_pcnt
)
603 signal(SIGSEGV
, perf_gtk__signal
);
604 signal(SIGFPE
, perf_gtk__signal
);
605 signal(SIGINT
, perf_gtk__signal
);
606 signal(SIGQUIT
, perf_gtk__signal
);
607 signal(SIGTERM
, perf_gtk__signal
);
609 window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
611 gtk_window_set_title(GTK_WINDOW(window
), "perf report");
613 g_signal_connect(window
, "delete_event", gtk_main_quit
, NULL
);
615 pgctx
= perf_gtk__activate_context(window
);
619 vbox
= gtk_vbox_new(FALSE
, 0);
621 notebook
= gtk_notebook_new();
623 gtk_box_pack_start(GTK_BOX(vbox
), notebook
, TRUE
, TRUE
, 0);
625 info_bar
= perf_gtk__setup_info_bar();
627 gtk_box_pack_start(GTK_BOX(vbox
), info_bar
, FALSE
, FALSE
, 0);
629 statbar
= perf_gtk__setup_statusbar();
630 gtk_box_pack_start(GTK_BOX(vbox
), statbar
, FALSE
, FALSE
, 0);
632 gtk_container_add(GTK_CONTAINER(window
), vbox
);
634 evlist__for_each_entry(evlist
, pos
) {
635 struct hists
*hists
= evsel__hists(pos
);
636 const char *evname
= evsel__name(pos
);
637 GtkWidget
*scrolled_window
;
638 GtkWidget
*tab_label
;
640 size_t size
= sizeof(buf
);
642 if (symbol_conf
.event_group
) {
643 if (!evsel__is_group_leader(pos
))
646 if (pos
->core
.nr_members
> 1) {
647 evsel__group_desc(pos
, buf
, size
);
652 scrolled_window
= gtk_scrolled_window_new(NULL
, NULL
);
654 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window
),
655 GTK_POLICY_AUTOMATIC
,
656 GTK_POLICY_AUTOMATIC
);
658 if (symbol_conf
.report_hierarchy
)
659 perf_gtk__show_hierarchy(scrolled_window
, hists
, min_pcnt
);
661 perf_gtk__show_hists(scrolled_window
, hists
, min_pcnt
);
663 tab_label
= gtk_label_new(evname
);
665 gtk_notebook_append_page(GTK_NOTEBOOK(notebook
), scrolled_window
, tab_label
);
668 gtk_widget_show_all(window
);
670 perf_gtk__resize_window(window
);
672 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
674 ui_helpline__push(help
);
678 perf_gtk__deactivate_context(&pgctx
);