1 // SPDX-License-Identifier: GPL-2.0
7 #include "../helpline.h"
8 #include "../string2.h"
12 #define MAX_COLUMNS 32
14 static int __percent_color_snprintf(struct perf_hpp
*hpp
, const char *fmt
, ...)
22 size_t size
= hpp
->size
;
25 len
= va_arg(args
, int);
26 percent
= va_arg(args
, double);
29 markup
= perf_gtk__get_percent_color(percent
);
31 ret
+= scnprintf(buf
, size
, markup
);
33 ret
+= scnprintf(buf
+ ret
, size
- ret
, fmt
, len
, percent
);
36 ret
+= scnprintf(buf
+ ret
, size
- ret
, "</span>");
41 #define __HPP_COLOR_PERCENT_FN(_type, _field) \
42 static u64 he_get_##_field(struct hist_entry *he) \
44 return he->stat._field; \
47 static int perf_gtk__hpp_color_##_type(struct perf_hpp_fmt *fmt, \
48 struct perf_hpp *hpp, \
49 struct hist_entry *he) \
51 return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%", \
52 __percent_color_snprintf, true); \
55 #define __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
56 static u64 he_get_acc_##_field(struct hist_entry *he) \
58 return he->stat_acc->_field; \
61 static int perf_gtk__hpp_color_##_type(struct perf_hpp_fmt *fmt, \
62 struct perf_hpp *hpp, \
63 struct hist_entry *he) \
65 return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", \
66 __percent_color_snprintf, true); \
69 __HPP_COLOR_PERCENT_FN(overhead
, period
)
70 __HPP_COLOR_PERCENT_FN(overhead_sys
, period_sys
)
71 __HPP_COLOR_PERCENT_FN(overhead_us
, period_us
)
72 __HPP_COLOR_PERCENT_FN(overhead_guest_sys
, period_guest_sys
)
73 __HPP_COLOR_PERCENT_FN(overhead_guest_us
, period_guest_us
)
74 __HPP_COLOR_ACC_PERCENT_FN(overhead_acc
, period
)
76 #undef __HPP_COLOR_PERCENT_FN
79 void perf_gtk__init_hpp(void)
81 perf_hpp__format
[PERF_HPP__OVERHEAD
].color
=
82 perf_gtk__hpp_color_overhead
;
83 perf_hpp__format
[PERF_HPP__OVERHEAD_SYS
].color
=
84 perf_gtk__hpp_color_overhead_sys
;
85 perf_hpp__format
[PERF_HPP__OVERHEAD_US
].color
=
86 perf_gtk__hpp_color_overhead_us
;
87 perf_hpp__format
[PERF_HPP__OVERHEAD_GUEST_SYS
].color
=
88 perf_gtk__hpp_color_overhead_guest_sys
;
89 perf_hpp__format
[PERF_HPP__OVERHEAD_GUEST_US
].color
=
90 perf_gtk__hpp_color_overhead_guest_us
;
91 perf_hpp__format
[PERF_HPP__OVERHEAD_ACC
].color
=
92 perf_gtk__hpp_color_overhead_acc
;
95 static void perf_gtk__add_callchain_flat(struct rb_root
*root
, GtkTreeStore
*store
,
96 GtkTreeIter
*parent
, int col
, u64 total
)
99 bool has_single_node
= (rb_first(root
) == rb_last(root
));
101 for (nd
= rb_first(root
); nd
; nd
= rb_next(nd
)) {
102 struct callchain_node
*node
;
103 struct callchain_list
*chain
;
104 GtkTreeIter iter
, new_parent
;
105 bool need_new_parent
;
107 node
= rb_entry(nd
, struct callchain_node
, rb_node
);
109 new_parent
= *parent
;
110 need_new_parent
= !has_single_node
;
112 callchain_node__make_parent_list(node
);
114 list_for_each_entry(chain
, &node
->parent_val
, list
) {
117 gtk_tree_store_append(store
, &iter
, &new_parent
);
119 callchain_node__scnprintf_value(node
, buf
, sizeof(buf
), total
);
120 gtk_tree_store_set(store
, &iter
, 0, buf
, -1);
122 callchain_list__sym_name(chain
, buf
, sizeof(buf
), false);
123 gtk_tree_store_set(store
, &iter
, col
, buf
, -1);
125 if (need_new_parent
) {
127 * Only show the top-most symbol in a callchain
128 * if it's not the only callchain.
131 need_new_parent
= false;
135 list_for_each_entry(chain
, &node
->val
, list
) {
138 gtk_tree_store_append(store
, &iter
, &new_parent
);
140 callchain_node__scnprintf_value(node
, buf
, sizeof(buf
), total
);
141 gtk_tree_store_set(store
, &iter
, 0, buf
, -1);
143 callchain_list__sym_name(chain
, buf
, sizeof(buf
), false);
144 gtk_tree_store_set(store
, &iter
, col
, buf
, -1);
146 if (need_new_parent
) {
148 * Only show the top-most symbol in a callchain
149 * if it's not the only callchain.
152 need_new_parent
= false;
158 static void perf_gtk__add_callchain_folded(struct rb_root
*root
, GtkTreeStore
*store
,
159 GtkTreeIter
*parent
, int col
, u64 total
)
163 for (nd
= rb_first(root
); nd
; nd
= rb_next(nd
)) {
164 struct callchain_node
*node
;
165 struct callchain_list
*chain
;
168 char *str
, *str_alloc
= NULL
;
171 node
= rb_entry(nd
, struct callchain_node
, rb_node
);
173 callchain_node__make_parent_list(node
);
175 list_for_each_entry(chain
, &node
->parent_val
, list
) {
178 callchain_list__sym_name(chain
, name
, sizeof(name
), false);
180 if (asprintf(&str
, "%s%s%s",
181 first
? "" : str_alloc
,
182 first
? "" : symbol_conf
.field_sep
?: "; ",
191 list_for_each_entry(chain
, &node
->val
, list
) {
194 callchain_list__sym_name(chain
, name
, sizeof(name
), false);
196 if (asprintf(&str
, "%s%s%s",
197 first
? "" : str_alloc
,
198 first
? "" : symbol_conf
.field_sep
?: "; ",
207 gtk_tree_store_append(store
, &iter
, parent
);
209 callchain_node__scnprintf_value(node
, buf
, sizeof(buf
), total
);
210 gtk_tree_store_set(store
, &iter
, 0, buf
, -1);
212 gtk_tree_store_set(store
, &iter
, col
, str
, -1);
218 static void perf_gtk__add_callchain_graph(struct rb_root
*root
, GtkTreeStore
*store
,
219 GtkTreeIter
*parent
, int col
, u64 total
)
222 bool has_single_node
= (rb_first(root
) == rb_last(root
));
224 for (nd
= rb_first(root
); nd
; nd
= rb_next(nd
)) {
225 struct callchain_node
*node
;
226 struct callchain_list
*chain
;
227 GtkTreeIter iter
, new_parent
;
228 bool need_new_parent
;
231 node
= rb_entry(nd
, struct callchain_node
, rb_node
);
233 new_parent
= *parent
;
234 need_new_parent
= !has_single_node
&& (node
->val_nr
> 1);
236 list_for_each_entry(chain
, &node
->val
, list
) {
239 gtk_tree_store_append(store
, &iter
, &new_parent
);
241 callchain_node__scnprintf_value(node
, buf
, sizeof(buf
), total
);
242 gtk_tree_store_set(store
, &iter
, 0, buf
, -1);
244 callchain_list__sym_name(chain
, buf
, sizeof(buf
), false);
245 gtk_tree_store_set(store
, &iter
, col
, buf
, -1);
247 if (need_new_parent
) {
249 * Only show the top-most symbol in a callchain
250 * if it's not the only callchain.
253 need_new_parent
= false;
257 if (callchain_param
.mode
== CHAIN_GRAPH_REL
)
258 child_total
= node
->children_hit
;
262 /* Now 'iter' contains info of the last callchain_list */
263 perf_gtk__add_callchain_graph(&node
->rb_root
, store
, &iter
, col
,
268 static void perf_gtk__add_callchain(struct rb_root
*root
, GtkTreeStore
*store
,
269 GtkTreeIter
*parent
, int col
, u64 total
)
271 if (callchain_param
.mode
== CHAIN_FLAT
)
272 perf_gtk__add_callchain_flat(root
, store
, parent
, col
, total
);
273 else if (callchain_param
.mode
== CHAIN_FOLDED
)
274 perf_gtk__add_callchain_folded(root
, store
, parent
, col
, total
);
276 perf_gtk__add_callchain_graph(root
, store
, parent
, col
, total
);
279 static void on_row_activated(GtkTreeView
*view
, GtkTreePath
*path
,
280 GtkTreeViewColumn
*col __maybe_unused
,
281 gpointer user_data __maybe_unused
)
283 bool expanded
= gtk_tree_view_row_expanded(view
, path
);
286 gtk_tree_view_collapse_row(view
, path
);
288 gtk_tree_view_expand_row(view
, path
, FALSE
);
291 static void perf_gtk__show_hists(GtkWidget
*window
, struct hists
*hists
,
294 struct perf_hpp_fmt
*fmt
;
295 GType col_types
[MAX_COLUMNS
];
296 GtkCellRenderer
*renderer
;
305 struct perf_hpp hpp
= {
312 hists__for_each_format(hists
, fmt
)
313 col_types
[nr_cols
++] = G_TYPE_STRING
;
315 store
= gtk_tree_store_newv(nr_cols
, col_types
);
317 view
= gtk_tree_view_new();
319 renderer
= gtk_cell_renderer_text_new();
323 hists__for_each_format(hists
, fmt
) {
324 if (perf_hpp__should_skip(fmt
, hists
))
328 * XXX no way to determine where symcol column is..
329 * Just use last column for now.
331 if (perf_hpp__is_sort_entry(fmt
))
334 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view
),
340 for (col_idx
= 0; col_idx
< nr_cols
; col_idx
++) {
341 GtkTreeViewColumn
*column
;
343 column
= gtk_tree_view_get_column(GTK_TREE_VIEW(view
), col_idx
);
344 gtk_tree_view_column_set_resizable(column
, TRUE
);
346 if (col_idx
== sym_col
) {
347 gtk_tree_view_set_expander_column(GTK_TREE_VIEW(view
),
352 gtk_tree_view_set_model(GTK_TREE_VIEW(view
), GTK_TREE_MODEL(store
));
354 g_object_unref(GTK_TREE_MODEL(store
));
356 for (nd
= rb_first(&hists
->entries
); nd
; nd
= rb_next(nd
)) {
357 struct hist_entry
*h
= rb_entry(nd
, struct hist_entry
, rb_node
);
359 u64 total
= hists__total_period(h
->hists
);
365 percent
= hist_entry__get_percent_limit(h
);
366 if (percent
< min_pcnt
)
369 gtk_tree_store_append(store
, &iter
, NULL
);
373 hists__for_each_format(hists
, fmt
) {
374 if (perf_hpp__should_skip(fmt
, h
->hists
))
378 fmt
->color(fmt
, &hpp
, h
);
380 fmt
->entry(fmt
, &hpp
, h
);
382 gtk_tree_store_set(store
, &iter
, col_idx
++, s
, -1);
385 if (hists__has_callchains(hists
) &&
386 symbol_conf
.use_callchain
&& hists__has(hists
, sym
)) {
387 if (callchain_param
.mode
== CHAIN_GRAPH_REL
)
388 total
= symbol_conf
.cumulate_callchain
?
389 h
->stat_acc
->period
: h
->stat
.period
;
391 perf_gtk__add_callchain(&h
->sorted_chain
, store
, &iter
,
396 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(view
), TRUE
);
398 g_signal_connect(view
, "row-activated",
399 G_CALLBACK(on_row_activated
), NULL
);
400 gtk_container_add(GTK_CONTAINER(window
), view
);
403 static void perf_gtk__add_hierarchy_entries(struct hists
*hists
,
404 struct rb_root
*root
,
407 struct perf_hpp
*hpp
,
411 struct rb_node
*node
;
412 struct hist_entry
*he
;
413 struct perf_hpp_fmt
*fmt
;
414 struct perf_hpp_list_node
*fmt_node
;
415 u64 total
= hists__total_period(hists
);
418 for (node
= rb_first(root
); node
; node
= rb_next(node
)) {
423 he
= rb_entry(node
, struct hist_entry
, rb_node
);
427 percent
= hist_entry__get_percent_limit(he
);
428 if (percent
< min_pcnt
)
431 gtk_tree_store_append(store
, &iter
, parent
);
435 /* the first hpp_list_node is for overhead columns */
436 fmt_node
= list_first_entry(&hists
->hpp_formats
,
437 struct perf_hpp_list_node
, list
);
438 perf_hpp_list__for_each_format(&fmt_node
->hpp
, fmt
) {
440 fmt
->color(fmt
, hpp
, he
);
442 fmt
->entry(fmt
, hpp
, he
);
444 gtk_tree_store_set(store
, &iter
, col_idx
++, hpp
->buf
, -1);
449 perf_hpp_list__for_each_format(he
->hpp_list
, fmt
) {
453 ret
= fmt
->color(fmt
, hpp
, he
);
455 ret
= fmt
->entry(fmt
, hpp
, he
);
457 snprintf(hpp
->buf
+ ret
, hpp
->size
- ret
, " ");
458 advance_hpp(hpp
, ret
+ 2);
461 gtk_tree_store_set(store
, &iter
, col_idx
, ltrim(rtrim(bf
)), -1);
467 perf_gtk__add_hierarchy_entries(hists
, &he
->hroot_out
,
471 if (!hist_entry__has_hierarchy_children(he
, min_pcnt
)) {
475 snprintf(buf
, sizeof(buf
), "no entry >= %.2f%%",
478 gtk_tree_store_append(store
, &child
, &iter
);
479 gtk_tree_store_set(store
, &child
, col_idx
, buf
, -1);
483 if (he
->leaf
&& hist_entry__has_callchains(he
) && symbol_conf
.use_callchain
) {
484 if (callchain_param
.mode
== CHAIN_GRAPH_REL
)
485 total
= symbol_conf
.cumulate_callchain
?
486 he
->stat_acc
->period
: he
->stat
.period
;
488 perf_gtk__add_callchain(&he
->sorted_chain
, store
, &iter
,
495 static void perf_gtk__show_hierarchy(GtkWidget
*window
, struct hists
*hists
,
498 struct perf_hpp_fmt
*fmt
;
499 struct perf_hpp_list_node
*fmt_node
;
500 GType col_types
[MAX_COLUMNS
];
501 GtkCellRenderer
*renderer
;
508 bool first_node
, first_col
;
509 struct perf_hpp hpp
= {
514 hists__for_each_format(hists
, fmt
) {
515 if (perf_hpp__is_sort_entry(fmt
) ||
516 perf_hpp__is_dynamic_entry(fmt
))
519 col_types
[nr_cols
++] = G_TYPE_STRING
;
521 col_types
[nr_cols
++] = G_TYPE_STRING
;
523 store
= gtk_tree_store_newv(nr_cols
, col_types
);
524 view
= gtk_tree_view_new();
525 renderer
= gtk_cell_renderer_text_new();
529 /* the first hpp_list_node is for overhead columns */
530 fmt_node
= list_first_entry(&hists
->hpp_formats
,
531 struct perf_hpp_list_node
, list
);
532 perf_hpp_list__for_each_format(&fmt_node
->hpp
, fmt
) {
533 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view
),
539 /* construct merged column header since sort keys share single column */
542 list_for_each_entry_continue(fmt_node
, &hists
->hpp_formats
, list
) {
548 perf_hpp_list__for_each_format(&fmt_node
->hpp
,fmt
) {
549 if (perf_hpp__should_skip(fmt
, hists
))
556 fmt
->header(fmt
, &hpp
, hists
, 0, NULL
);
557 strcat(buf
, ltrim(rtrim(hpp
.buf
)));
561 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view
),
566 for (col_idx
= 0; col_idx
< nr_cols
; col_idx
++) {
567 GtkTreeViewColumn
*column
;
569 column
= gtk_tree_view_get_column(GTK_TREE_VIEW(view
), col_idx
);
570 gtk_tree_view_column_set_resizable(column
, TRUE
);
573 gtk_tree_view_set_expander_column(GTK_TREE_VIEW(view
),
578 gtk_tree_view_set_model(GTK_TREE_VIEW(view
), GTK_TREE_MODEL(store
));
579 g_object_unref(GTK_TREE_MODEL(store
));
581 perf_gtk__add_hierarchy_entries(hists
, &hists
->entries
, store
,
582 NULL
, &hpp
, min_pcnt
);
584 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(view
), TRUE
);
586 g_signal_connect(view
, "row-activated",
587 G_CALLBACK(on_row_activated
), NULL
);
588 gtk_container_add(GTK_CONTAINER(window
), view
);
591 int perf_evlist__gtk_browse_hists(struct perf_evlist
*evlist
,
593 struct hist_browser_timer
*hbt __maybe_unused
,
596 struct perf_evsel
*pos
;
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
= perf_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 (!perf_evsel__is_group_leader(pos
))
646 if (pos
->nr_members
> 1) {
647 perf_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
);