Commit | Line | Data |
---|---|---|
0da41ce9 NK |
1 | #include "../evlist.h" |
2 | #include "../cache.h" | |
3 | #include "../evsel.h" | |
4 | #include "../sort.h" | |
5 | #include "../hist.h" | |
6 | #include "../helpline.h" | |
7 | #include "gtk.h" | |
8 | ||
9 | #define MAX_COLUMNS 32 | |
10 | ||
a0088adc | 11 | static int __percent_color_snprintf(struct perf_hpp *hpp, const char *fmt, ...) |
843985e9 NK |
12 | { |
13 | int ret = 0; | |
4a62109f NK |
14 | va_list args; |
15 | double percent; | |
843985e9 | 16 | const char *markup; |
a0088adc NK |
17 | char *buf = hpp->buf; |
18 | size_t size = hpp->size; | |
843985e9 | 19 | |
4a62109f NK |
20 | va_start(args, fmt); |
21 | percent = va_arg(args, double); | |
22 | va_end(args); | |
23 | ||
843985e9 NK |
24 | markup = perf_gtk__get_percent_color(percent); |
25 | if (markup) | |
26 | ret += scnprintf(buf, size, markup); | |
27 | ||
4a62109f | 28 | ret += scnprintf(buf + ret, size - ret, fmt, percent); |
843985e9 NK |
29 | |
30 | if (markup) | |
31 | ret += scnprintf(buf + ret, size - ret, "</span>"); | |
32 | ||
33 | return ret; | |
34 | } | |
35 | ||
843985e9 NK |
36 | #define __HPP_COLOR_PERCENT_FN(_type, _field) \ |
37 | static u64 he_get_##_field(struct hist_entry *he) \ | |
0da41ce9 | 38 | { \ |
843985e9 NK |
39 | return he->stat._field; \ |
40 | } \ | |
0da41ce9 | 41 | \ |
2c5d4b4a JO |
42 | static int perf_gtk__hpp_color_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \ |
43 | struct perf_hpp *hpp, \ | |
843985e9 NK |
44 | struct hist_entry *he) \ |
45 | { \ | |
fb821c9e | 46 | return __hpp__fmt(hpp, he, he_get_##_field, " %6.2f%%", \ |
4a62109f | 47 | __percent_color_snprintf, true); \ |
0da41ce9 NK |
48 | } |
49 | ||
b09955b2 NK |
50 | #define __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \ |
51 | static u64 he_get_acc_##_field(struct hist_entry *he) \ | |
52 | { \ | |
53 | return he->stat_acc->_field; \ | |
54 | } \ | |
55 | \ | |
56 | static int perf_gtk__hpp_color_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \ | |
57 | struct perf_hpp *hpp, \ | |
58 | struct hist_entry *he) \ | |
59 | { \ | |
60 | return __hpp__fmt_acc(hpp, he, he_get_acc_##_field, " %6.2f%%", \ | |
61 | __percent_color_snprintf, true); \ | |
62 | } | |
63 | ||
843985e9 NK |
64 | __HPP_COLOR_PERCENT_FN(overhead, period) |
65 | __HPP_COLOR_PERCENT_FN(overhead_sys, period_sys) | |
66 | __HPP_COLOR_PERCENT_FN(overhead_us, period_us) | |
67 | __HPP_COLOR_PERCENT_FN(overhead_guest_sys, period_guest_sys) | |
68 | __HPP_COLOR_PERCENT_FN(overhead_guest_us, period_guest_us) | |
b09955b2 | 69 | __HPP_COLOR_ACC_PERCENT_FN(overhead_acc, period) |
0da41ce9 | 70 | |
843985e9 | 71 | #undef __HPP_COLOR_PERCENT_FN |
0da41ce9 NK |
72 | |
73 | ||
74 | void perf_gtk__init_hpp(void) | |
75 | { | |
0da41ce9 NK |
76 | perf_hpp__format[PERF_HPP__OVERHEAD].color = |
77 | perf_gtk__hpp_color_overhead; | |
78 | perf_hpp__format[PERF_HPP__OVERHEAD_SYS].color = | |
79 | perf_gtk__hpp_color_overhead_sys; | |
80 | perf_hpp__format[PERF_HPP__OVERHEAD_US].color = | |
81 | perf_gtk__hpp_color_overhead_us; | |
82 | perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_SYS].color = | |
83 | perf_gtk__hpp_color_overhead_guest_sys; | |
84 | perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_US].color = | |
85 | perf_gtk__hpp_color_overhead_guest_us; | |
b09955b2 NK |
86 | perf_hpp__format[PERF_HPP__OVERHEAD_ACC].color = |
87 | perf_gtk__hpp_color_overhead_acc; | |
0da41ce9 NK |
88 | } |
89 | ||
2bbc5874 NK |
90 | static void callchain_list__sym_name(struct callchain_list *cl, |
91 | char *bf, size_t bfsize) | |
92 | { | |
93 | if (cl->ms.sym) | |
94 | scnprintf(bf, bfsize, "%s", cl->ms.sym->name); | |
95 | else | |
96 | scnprintf(bf, bfsize, "%#" PRIx64, cl->ip); | |
97 | } | |
98 | ||
99 | static void perf_gtk__add_callchain(struct rb_root *root, GtkTreeStore *store, | |
cc60f24e | 100 | GtkTreeIter *parent, int col, u64 total) |
2bbc5874 NK |
101 | { |
102 | struct rb_node *nd; | |
103 | bool has_single_node = (rb_first(root) == rb_last(root)); | |
104 | ||
105 | for (nd = rb_first(root); nd; nd = rb_next(nd)) { | |
106 | struct callchain_node *node; | |
107 | struct callchain_list *chain; | |
108 | GtkTreeIter iter, new_parent; | |
109 | bool need_new_parent; | |
cc60f24e NK |
110 | double percent; |
111 | u64 hits, child_total; | |
2bbc5874 NK |
112 | |
113 | node = rb_entry(nd, struct callchain_node, rb_node); | |
114 | ||
cc60f24e NK |
115 | hits = callchain_cumul_hits(node); |
116 | percent = 100.0 * hits / total; | |
117 | ||
2bbc5874 NK |
118 | new_parent = *parent; |
119 | need_new_parent = !has_single_node && (node->val_nr > 1); | |
120 | ||
121 | list_for_each_entry(chain, &node->val, list) { | |
122 | char buf[128]; | |
123 | ||
124 | gtk_tree_store_append(store, &iter, &new_parent); | |
125 | ||
cc60f24e NK |
126 | scnprintf(buf, sizeof(buf), "%5.2f%%", percent); |
127 | gtk_tree_store_set(store, &iter, 0, buf, -1); | |
128 | ||
2bbc5874 NK |
129 | callchain_list__sym_name(chain, buf, sizeof(buf)); |
130 | gtk_tree_store_set(store, &iter, col, buf, -1); | |
131 | ||
132 | if (need_new_parent) { | |
133 | /* | |
134 | * Only show the top-most symbol in a callchain | |
135 | * if it's not the only callchain. | |
136 | */ | |
137 | new_parent = iter; | |
138 | need_new_parent = false; | |
139 | } | |
140 | } | |
141 | ||
cc60f24e NK |
142 | if (callchain_param.mode == CHAIN_GRAPH_REL) |
143 | child_total = node->children_hit; | |
144 | else | |
145 | child_total = total; | |
146 | ||
2bbc5874 | 147 | /* Now 'iter' contains info of the last callchain_list */ |
cc60f24e NK |
148 | perf_gtk__add_callchain(&node->rb_root, store, &iter, col, |
149 | child_total); | |
2bbc5874 NK |
150 | } |
151 | } | |
152 | ||
450f390a NK |
153 | static void on_row_activated(GtkTreeView *view, GtkTreePath *path, |
154 | GtkTreeViewColumn *col __maybe_unused, | |
155 | gpointer user_data __maybe_unused) | |
156 | { | |
157 | bool expanded = gtk_tree_view_row_expanded(view, path); | |
158 | ||
159 | if (expanded) | |
160 | gtk_tree_view_collapse_row(view, path); | |
161 | else | |
162 | gtk_tree_view_expand_row(view, path, FALSE); | |
163 | } | |
164 | ||
064f1981 NK |
165 | static void perf_gtk__show_hists(GtkWidget *window, struct hists *hists, |
166 | float min_pcnt) | |
0da41ce9 NK |
167 | { |
168 | struct perf_hpp_fmt *fmt; | |
169 | GType col_types[MAX_COLUMNS]; | |
170 | GtkCellRenderer *renderer; | |
f1d9a530 | 171 | GtkTreeStore *store; |
0da41ce9 NK |
172 | struct rb_node *nd; |
173 | GtkWidget *view; | |
174 | int col_idx; | |
2bbc5874 | 175 | int sym_col = -1; |
0da41ce9 NK |
176 | int nr_cols; |
177 | char s[512]; | |
178 | ||
179 | struct perf_hpp hpp = { | |
180 | .buf = s, | |
181 | .size = sizeof(s), | |
182 | }; | |
183 | ||
184 | nr_cols = 0; | |
185 | ||
186 | perf_hpp__for_each_format(fmt) | |
187 | col_types[nr_cols++] = G_TYPE_STRING; | |
188 | ||
f1d9a530 | 189 | store = gtk_tree_store_newv(nr_cols, col_types); |
0da41ce9 NK |
190 | |
191 | view = gtk_tree_view_new(); | |
192 | ||
193 | renderer = gtk_cell_renderer_text_new(); | |
194 | ||
195 | col_idx = 0; | |
196 | ||
197 | perf_hpp__for_each_format(fmt) { | |
e67d49a7 NK |
198 | if (perf_hpp__should_skip(fmt)) |
199 | continue; | |
200 | ||
94a0793d | 201 | fmt->header(fmt, &hpp, hists_to_evsel(hists)); |
0da41ce9 NK |
202 | |
203 | gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), | |
34b95643 | 204 | -1, ltrim(s), |
0da41ce9 NK |
205 | renderer, "markup", |
206 | col_idx++, NULL); | |
207 | } | |
208 | ||
1a309426 | 209 | for (col_idx = 0; col_idx < nr_cols; col_idx++) { |
2bbc5874 NK |
210 | GtkTreeViewColumn *column; |
211 | ||
1a309426 NK |
212 | column = gtk_tree_view_get_column(GTK_TREE_VIEW(view), col_idx); |
213 | gtk_tree_view_column_set_resizable(column, TRUE); | |
214 | ||
215 | if (col_idx == sym_col) { | |
216 | gtk_tree_view_set_expander_column(GTK_TREE_VIEW(view), | |
217 | column); | |
218 | } | |
2bbc5874 NK |
219 | } |
220 | ||
0da41ce9 NK |
221 | gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store)); |
222 | ||
223 | g_object_unref(GTK_TREE_MODEL(store)); | |
224 | ||
225 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { | |
226 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); | |
227 | GtkTreeIter iter; | |
f2148330 | 228 | u64 total = hists__total_period(h->hists); |
14135663 | 229 | float percent; |
0da41ce9 NK |
230 | |
231 | if (h->filtered) | |
232 | continue; | |
233 | ||
14135663 | 234 | percent = hist_entry__get_percent_limit(h); |
064f1981 NK |
235 | if (percent < min_pcnt) |
236 | continue; | |
237 | ||
f1d9a530 | 238 | gtk_tree_store_append(store, &iter, NULL); |
0da41ce9 NK |
239 | |
240 | col_idx = 0; | |
241 | ||
242 | perf_hpp__for_each_format(fmt) { | |
e67d49a7 NK |
243 | if (perf_hpp__should_skip(fmt)) |
244 | continue; | |
245 | ||
0da41ce9 | 246 | if (fmt->color) |
2c5d4b4a | 247 | fmt->color(fmt, &hpp, h); |
0da41ce9 | 248 | else |
2c5d4b4a | 249 | fmt->entry(fmt, &hpp, h); |
0da41ce9 | 250 | |
f1d9a530 | 251 | gtk_tree_store_set(store, &iter, col_idx++, s, -1); |
0da41ce9 NK |
252 | } |
253 | ||
2bbc5874 | 254 | if (symbol_conf.use_callchain && sort__has_sym) { |
cc60f24e NK |
255 | if (callchain_param.mode == CHAIN_GRAPH_REL) |
256 | total = h->stat.period; | |
cc60f24e | 257 | |
2bbc5874 | 258 | perf_gtk__add_callchain(&h->sorted_chain, store, &iter, |
cc60f24e | 259 | sym_col, total); |
2bbc5874 | 260 | } |
0da41ce9 NK |
261 | } |
262 | ||
9d58d2f6 NK |
263 | gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(view), TRUE); |
264 | ||
450f390a NK |
265 | g_signal_connect(view, "row-activated", |
266 | G_CALLBACK(on_row_activated), NULL); | |
0da41ce9 NK |
267 | gtk_container_add(GTK_CONTAINER(window), view); |
268 | } | |
269 | ||
270 | int perf_evlist__gtk_browse_hists(struct perf_evlist *evlist, | |
271 | const char *help, | |
064f1981 NK |
272 | struct hist_browser_timer *hbt __maybe_unused, |
273 | float min_pcnt) | |
0da41ce9 NK |
274 | { |
275 | struct perf_evsel *pos; | |
276 | GtkWidget *vbox; | |
277 | GtkWidget *notebook; | |
278 | GtkWidget *info_bar; | |
279 | GtkWidget *statbar; | |
280 | GtkWidget *window; | |
281 | ||
282 | signal(SIGSEGV, perf_gtk__signal); | |
283 | signal(SIGFPE, perf_gtk__signal); | |
284 | signal(SIGINT, perf_gtk__signal); | |
285 | signal(SIGQUIT, perf_gtk__signal); | |
286 | signal(SIGTERM, perf_gtk__signal); | |
287 | ||
288 | window = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
289 | ||
290 | gtk_window_set_title(GTK_WINDOW(window), "perf report"); | |
291 | ||
292 | g_signal_connect(window, "delete_event", gtk_main_quit, NULL); | |
293 | ||
294 | pgctx = perf_gtk__activate_context(window); | |
295 | if (!pgctx) | |
296 | return -1; | |
297 | ||
298 | vbox = gtk_vbox_new(FALSE, 0); | |
299 | ||
300 | notebook = gtk_notebook_new(); | |
301 | ||
6bf1a295 NK |
302 | gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 0); |
303 | ||
304 | info_bar = perf_gtk__setup_info_bar(); | |
305 | if (info_bar) | |
306 | gtk_box_pack_start(GTK_BOX(vbox), info_bar, FALSE, FALSE, 0); | |
307 | ||
308 | statbar = perf_gtk__setup_statusbar(); | |
309 | gtk_box_pack_start(GTK_BOX(vbox), statbar, FALSE, FALSE, 0); | |
310 | ||
311 | gtk_container_add(GTK_CONTAINER(window), vbox); | |
312 | ||
0050f7aa | 313 | evlist__for_each(evlist, pos) { |
0da41ce9 NK |
314 | struct hists *hists = &pos->hists; |
315 | const char *evname = perf_evsel__name(pos); | |
316 | GtkWidget *scrolled_window; | |
317 | GtkWidget *tab_label; | |
717e263f NK |
318 | char buf[512]; |
319 | size_t size = sizeof(buf); | |
0da41ce9 | 320 | |
717e263f NK |
321 | if (symbol_conf.event_group) { |
322 | if (!perf_evsel__is_group_leader(pos)) | |
323 | continue; | |
324 | ||
325 | if (pos->nr_members > 1) { | |
326 | perf_evsel__group_desc(pos, buf, size); | |
327 | evname = buf; | |
328 | } | |
329 | } | |
fc24d7c2 | 330 | |
0da41ce9 NK |
331 | scrolled_window = gtk_scrolled_window_new(NULL, NULL); |
332 | ||
333 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), | |
334 | GTK_POLICY_AUTOMATIC, | |
335 | GTK_POLICY_AUTOMATIC); | |
336 | ||
064f1981 | 337 | perf_gtk__show_hists(scrolled_window, hists, min_pcnt); |
0da41ce9 NK |
338 | |
339 | tab_label = gtk_label_new(evname); | |
340 | ||
341 | gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scrolled_window, tab_label); | |
342 | } | |
343 | ||
0da41ce9 NK |
344 | gtk_widget_show_all(window); |
345 | ||
346 | perf_gtk__resize_window(window); | |
347 | ||
348 | gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); | |
349 | ||
350 | ui_helpline__push(help); | |
351 | ||
352 | gtk_main(); | |
353 | ||
354 | perf_gtk__deactivate_context(&pgctx); | |
355 | ||
356 | return 0; | |
357 | } |