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