blob: 5b95c44f34354215ba597c19d736d3e63c559d1e [file] [log] [blame]
Namhyung Kim0da41ce92012-12-21 17:20:13 +09001#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
Namhyung Kimcb160082013-01-22 18:09:40 +090011static int __percent_color_snprintf(char *buf, size_t size, double percent)
Namhyung Kim843985e2013-01-22 18:09:36 +090012{
13 int ret = 0;
14 const char *markup;
15
16 markup = perf_gtk__get_percent_color(percent);
17 if (markup)
18 ret += scnprintf(buf, size, markup);
19
Namhyung Kimcb160082013-01-22 18:09:40 +090020 ret += scnprintf(buf + ret, size - ret, " %6.2f%%", percent);
Namhyung Kim843985e2013-01-22 18:09:36 +090021
22 if (markup)
23 ret += scnprintf(buf + ret, size - ret, "</span>");
24
25 return ret;
Namhyung Kim0da41ce92012-12-21 17:20:13 +090026}
27
Namhyung Kim0da41ce92012-12-21 17:20:13 +090028
Namhyung Kim843985e2013-01-22 18:09:36 +090029static int __hpp__color_fmt(struct perf_hpp *hpp, struct hist_entry *he,
30 u64 (*get_field)(struct hist_entry *))
31{
32 int ret;
33 double percent = 0.0;
34 struct hists *hists = he->hists;
Namhyung Kim759ff492013-03-05 14:53:26 +090035 struct perf_evsel *evsel = hists_to_evsel(hists);
Namhyung Kim843985e2013-01-22 18:09:36 +090036
37 if (hists->stats.total_period)
38 percent = 100.0 * get_field(he) / hists->stats.total_period;
39
Namhyung Kimcb160082013-01-22 18:09:40 +090040 ret = __percent_color_snprintf(hpp->buf, hpp->size, percent);
41
Namhyung Kim759ff492013-03-05 14:53:26 +090042 if (perf_evsel__is_group_event(evsel)) {
Namhyung Kimcb160082013-01-22 18:09:40 +090043 int prev_idx, idx_delta;
Namhyung Kimcb160082013-01-22 18:09:40 +090044 struct hist_entry *pair;
45 int nr_members = evsel->nr_members;
46
Namhyung Kimcb160082013-01-22 18:09:40 +090047 prev_idx = perf_evsel__group_idx(evsel);
48
49 list_for_each_entry(pair, &he->pairs.head, pairs.node) {
50 u64 period = get_field(pair);
51 u64 total = pair->hists->stats.total_period;
52
53 evsel = hists_to_evsel(pair->hists);
54 idx_delta = perf_evsel__group_idx(evsel) - prev_idx - 1;
55
56 while (idx_delta--) {
57 /*
58 * zero-fill group members in the middle which
59 * have no sample
60 */
61 ret += __percent_color_snprintf(hpp->buf + ret,
62 hpp->size - ret,
63 0.0);
64 }
65
66 percent = 100.0 * period / total;
67 ret += __percent_color_snprintf(hpp->buf + ret,
68 hpp->size - ret,
69 percent);
70
71 prev_idx = perf_evsel__group_idx(evsel);
72 }
73
74 idx_delta = nr_members - prev_idx - 1;
75
76 while (idx_delta--) {
77 /*
78 * zero-fill group members at last which have no sample
79 */
80 ret += __percent_color_snprintf(hpp->buf + ret,
81 hpp->size - ret,
82 0.0);
83 }
84 }
Namhyung Kim843985e2013-01-22 18:09:36 +090085 return ret;
86}
87
88#define __HPP_COLOR_PERCENT_FN(_type, _field) \
89static u64 he_get_##_field(struct hist_entry *he) \
90{ \
91 return he->stat._field; \
92} \
93 \
Jiri Olsa2c5d4b42013-01-31 23:31:11 +010094static int perf_gtk__hpp_color_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
95 struct perf_hpp *hpp, \
Namhyung Kim843985e2013-01-22 18:09:36 +090096 struct hist_entry *he) \
97{ \
98 return __hpp__color_fmt(hpp, he, he_get_##_field); \
99}
100
101__HPP_COLOR_PERCENT_FN(overhead, period)
102__HPP_COLOR_PERCENT_FN(overhead_sys, period_sys)
103__HPP_COLOR_PERCENT_FN(overhead_us, period_us)
104__HPP_COLOR_PERCENT_FN(overhead_guest_sys, period_guest_sys)
105__HPP_COLOR_PERCENT_FN(overhead_guest_us, period_guest_us)
106
107#undef __HPP_COLOR_PERCENT_FN
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900108
109
110void perf_gtk__init_hpp(void)
111{
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900112 perf_hpp__init();
113
114 perf_hpp__format[PERF_HPP__OVERHEAD].color =
115 perf_gtk__hpp_color_overhead;
116 perf_hpp__format[PERF_HPP__OVERHEAD_SYS].color =
117 perf_gtk__hpp_color_overhead_sys;
118 perf_hpp__format[PERF_HPP__OVERHEAD_US].color =
119 perf_gtk__hpp_color_overhead_us;
120 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_SYS].color =
121 perf_gtk__hpp_color_overhead_guest_sys;
122 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_US].color =
123 perf_gtk__hpp_color_overhead_guest_us;
124}
125
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900126static void callchain_list__sym_name(struct callchain_list *cl,
127 char *bf, size_t bfsize)
128{
129 if (cl->ms.sym)
130 scnprintf(bf, bfsize, "%s", cl->ms.sym->name);
131 else
132 scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);
133}
134
135static void perf_gtk__add_callchain(struct rb_root *root, GtkTreeStore *store,
Namhyung Kimcc60f242013-06-04 18:22:14 +0900136 GtkTreeIter *parent, int col, u64 total)
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900137{
138 struct rb_node *nd;
139 bool has_single_node = (rb_first(root) == rb_last(root));
140
141 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
142 struct callchain_node *node;
143 struct callchain_list *chain;
144 GtkTreeIter iter, new_parent;
145 bool need_new_parent;
Namhyung Kimcc60f242013-06-04 18:22:14 +0900146 double percent;
147 u64 hits, child_total;
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900148
149 node = rb_entry(nd, struct callchain_node, rb_node);
150
Namhyung Kimcc60f242013-06-04 18:22:14 +0900151 hits = callchain_cumul_hits(node);
152 percent = 100.0 * hits / total;
153
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900154 new_parent = *parent;
155 need_new_parent = !has_single_node && (node->val_nr > 1);
156
157 list_for_each_entry(chain, &node->val, list) {
158 char buf[128];
159
160 gtk_tree_store_append(store, &iter, &new_parent);
161
Namhyung Kimcc60f242013-06-04 18:22:14 +0900162 scnprintf(buf, sizeof(buf), "%5.2f%%", percent);
163 gtk_tree_store_set(store, &iter, 0, buf, -1);
164
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900165 callchain_list__sym_name(chain, buf, sizeof(buf));
166 gtk_tree_store_set(store, &iter, col, buf, -1);
167
168 if (need_new_parent) {
169 /*
170 * Only show the top-most symbol in a callchain
171 * if it's not the only callchain.
172 */
173 new_parent = iter;
174 need_new_parent = false;
175 }
176 }
177
Namhyung Kimcc60f242013-06-04 18:22:14 +0900178 if (callchain_param.mode == CHAIN_GRAPH_REL)
179 child_total = node->children_hit;
180 else
181 child_total = total;
182
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900183 /* Now 'iter' contains info of the last callchain_list */
Namhyung Kimcc60f242013-06-04 18:22:14 +0900184 perf_gtk__add_callchain(&node->rb_root, store, &iter, col,
185 child_total);
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900186 }
187}
188
Namhyung Kim450f3902013-06-04 18:22:16 +0900189static void on_row_activated(GtkTreeView *view, GtkTreePath *path,
190 GtkTreeViewColumn *col __maybe_unused,
191 gpointer user_data __maybe_unused)
192{
193 bool expanded = gtk_tree_view_row_expanded(view, path);
194
195 if (expanded)
196 gtk_tree_view_collapse_row(view, path);
197 else
198 gtk_tree_view_expand_row(view, path, FALSE);
199}
200
Namhyung Kim064f1982013-05-14 11:09:04 +0900201static void perf_gtk__show_hists(GtkWidget *window, struct hists *hists,
202 float min_pcnt)
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900203{
204 struct perf_hpp_fmt *fmt;
205 GType col_types[MAX_COLUMNS];
206 GtkCellRenderer *renderer;
207 struct sort_entry *se;
Namhyung Kimf1d9a532013-06-04 18:22:12 +0900208 GtkTreeStore *store;
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900209 struct rb_node *nd;
210 GtkWidget *view;
211 int col_idx;
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900212 int sym_col = -1;
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900213 int nr_cols;
214 char s[512];
215
216 struct perf_hpp hpp = {
217 .buf = s,
218 .size = sizeof(s),
Namhyung Kimcb160082013-01-22 18:09:40 +0900219 .ptr = hists_to_evsel(hists),
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900220 };
221
222 nr_cols = 0;
223
224 perf_hpp__for_each_format(fmt)
225 col_types[nr_cols++] = G_TYPE_STRING;
226
227 list_for_each_entry(se, &hist_entry__sort_list, list) {
228 if (se->elide)
229 continue;
230
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900231 if (se == &sort_sym)
232 sym_col = nr_cols;
233
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900234 col_types[nr_cols++] = G_TYPE_STRING;
235 }
236
Namhyung Kimf1d9a532013-06-04 18:22:12 +0900237 store = gtk_tree_store_newv(nr_cols, col_types);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900238
239 view = gtk_tree_view_new();
240
241 renderer = gtk_cell_renderer_text_new();
242
243 col_idx = 0;
244
245 perf_hpp__for_each_format(fmt) {
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100246 fmt->header(fmt, &hpp);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900247
248 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
Namhyung Kim34b95642013-01-22 18:09:42 +0900249 -1, ltrim(s),
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900250 renderer, "markup",
251 col_idx++, NULL);
252 }
253
254 list_for_each_entry(se, &hist_entry__sort_list, list) {
255 if (se->elide)
256 continue;
257
258 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
259 -1, se->se_header,
260 renderer, "text",
261 col_idx++, NULL);
262 }
263
Namhyung Kim1a309422013-06-04 18:22:15 +0900264 for (col_idx = 0; col_idx < nr_cols; col_idx++) {
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900265 GtkTreeViewColumn *column;
266
Namhyung Kim1a309422013-06-04 18:22:15 +0900267 column = gtk_tree_view_get_column(GTK_TREE_VIEW(view), col_idx);
268 gtk_tree_view_column_set_resizable(column, TRUE);
269
270 if (col_idx == sym_col) {
271 gtk_tree_view_set_expander_column(GTK_TREE_VIEW(view),
272 column);
273 }
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900274 }
275
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900276 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
277
278 g_object_unref(GTK_TREE_MODEL(store));
279
280 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
281 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
282 GtkTreeIter iter;
Namhyung Kim064f1982013-05-14 11:09:04 +0900283 float percent = h->stat.period * 100.0 /
284 hists->stats.total_period;
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900285
286 if (h->filtered)
287 continue;
288
Namhyung Kim064f1982013-05-14 11:09:04 +0900289 if (percent < min_pcnt)
290 continue;
291
Namhyung Kimf1d9a532013-06-04 18:22:12 +0900292 gtk_tree_store_append(store, &iter, NULL);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900293
294 col_idx = 0;
295
296 perf_hpp__for_each_format(fmt) {
297 if (fmt->color)
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100298 fmt->color(fmt, &hpp, h);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900299 else
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100300 fmt->entry(fmt, &hpp, h);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900301
Namhyung Kimf1d9a532013-06-04 18:22:12 +0900302 gtk_tree_store_set(store, &iter, col_idx++, s, -1);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900303 }
304
305 list_for_each_entry(se, &hist_entry__sort_list, list) {
306 if (se->elide)
307 continue;
308
309 se->se_snprintf(h, s, ARRAY_SIZE(s),
310 hists__col_len(hists, se->se_width_idx));
311
Namhyung Kimf1d9a532013-06-04 18:22:12 +0900312 gtk_tree_store_set(store, &iter, col_idx++, s, -1);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900313 }
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900314
315 if (symbol_conf.use_callchain && sort__has_sym) {
Namhyung Kimcc60f242013-06-04 18:22:14 +0900316 u64 total;
317
318 if (callchain_param.mode == CHAIN_GRAPH_REL)
319 total = h->stat.period;
320 else
321 total = hists->stats.total_period;
322
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900323 perf_gtk__add_callchain(&h->sorted_chain, store, &iter,
Namhyung Kimcc60f242013-06-04 18:22:14 +0900324 sym_col, total);
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900325 }
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900326 }
327
Namhyung Kim9d58d2f2013-06-04 18:22:17 +0900328 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(view), TRUE);
329
Namhyung Kim450f3902013-06-04 18:22:16 +0900330 g_signal_connect(view, "row-activated",
331 G_CALLBACK(on_row_activated), NULL);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900332 gtk_container_add(GTK_CONTAINER(window), view);
333}
334
335int perf_evlist__gtk_browse_hists(struct perf_evlist *evlist,
336 const char *help,
Namhyung Kim064f1982013-05-14 11:09:04 +0900337 struct hist_browser_timer *hbt __maybe_unused,
338 float min_pcnt)
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900339{
340 struct perf_evsel *pos;
341 GtkWidget *vbox;
342 GtkWidget *notebook;
343 GtkWidget *info_bar;
344 GtkWidget *statbar;
345 GtkWidget *window;
346
347 signal(SIGSEGV, perf_gtk__signal);
348 signal(SIGFPE, perf_gtk__signal);
349 signal(SIGINT, perf_gtk__signal);
350 signal(SIGQUIT, perf_gtk__signal);
351 signal(SIGTERM, perf_gtk__signal);
352
353 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
354
355 gtk_window_set_title(GTK_WINDOW(window), "perf report");
356
357 g_signal_connect(window, "delete_event", gtk_main_quit, NULL);
358
359 pgctx = perf_gtk__activate_context(window);
360 if (!pgctx)
361 return -1;
362
363 vbox = gtk_vbox_new(FALSE, 0);
364
365 notebook = gtk_notebook_new();
366
Namhyung Kim6bf1a292012-12-21 17:20:14 +0900367 gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 0);
368
369 info_bar = perf_gtk__setup_info_bar();
370 if (info_bar)
371 gtk_box_pack_start(GTK_BOX(vbox), info_bar, FALSE, FALSE, 0);
372
373 statbar = perf_gtk__setup_statusbar();
374 gtk_box_pack_start(GTK_BOX(vbox), statbar, FALSE, FALSE, 0);
375
376 gtk_container_add(GTK_CONTAINER(window), vbox);
377
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300378 evlist__for_each(evlist, pos) {
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900379 struct hists *hists = &pos->hists;
380 const char *evname = perf_evsel__name(pos);
381 GtkWidget *scrolled_window;
382 GtkWidget *tab_label;
Namhyung Kim717e2632013-01-22 18:09:44 +0900383 char buf[512];
384 size_t size = sizeof(buf);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900385
Namhyung Kim717e2632013-01-22 18:09:44 +0900386 if (symbol_conf.event_group) {
387 if (!perf_evsel__is_group_leader(pos))
388 continue;
389
390 if (pos->nr_members > 1) {
391 perf_evsel__group_desc(pos, buf, size);
392 evname = buf;
393 }
394 }
Namhyung Kimfc24d7c2013-01-22 18:09:43 +0900395
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900396 scrolled_window = gtk_scrolled_window_new(NULL, NULL);
397
398 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
399 GTK_POLICY_AUTOMATIC,
400 GTK_POLICY_AUTOMATIC);
401
Namhyung Kim064f1982013-05-14 11:09:04 +0900402 perf_gtk__show_hists(scrolled_window, hists, min_pcnt);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900403
404 tab_label = gtk_label_new(evname);
405
406 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scrolled_window, tab_label);
407 }
408
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900409 gtk_widget_show_all(window);
410
411 perf_gtk__resize_window(window);
412
413 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
414
415 ui_helpline__push(help);
416
417 gtk_main();
418
419 perf_gtk__deactivate_context(&pgctx);
420
421 return 0;
422}