blob: b4a0dd2404a22077aaee9ab8ff60e899cae65007 [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 \
94static int perf_gtk__hpp_color_##_type(struct perf_hpp *hpp, \
95 struct hist_entry *he) \
96{ \
97 return __hpp__color_fmt(hpp, he, he_get_##_field); \
98}
99
100__HPP_COLOR_PERCENT_FN(overhead, period)
101__HPP_COLOR_PERCENT_FN(overhead_sys, period_sys)
102__HPP_COLOR_PERCENT_FN(overhead_us, period_us)
103__HPP_COLOR_PERCENT_FN(overhead_guest_sys, period_guest_sys)
104__HPP_COLOR_PERCENT_FN(overhead_guest_us, period_guest_us)
105
106#undef __HPP_COLOR_PERCENT_FN
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900107
108
109void perf_gtk__init_hpp(void)
110{
111 perf_hpp__column_enable(PERF_HPP__OVERHEAD);
112
113 perf_hpp__init();
114
115 perf_hpp__format[PERF_HPP__OVERHEAD].color =
116 perf_gtk__hpp_color_overhead;
117 perf_hpp__format[PERF_HPP__OVERHEAD_SYS].color =
118 perf_gtk__hpp_color_overhead_sys;
119 perf_hpp__format[PERF_HPP__OVERHEAD_US].color =
120 perf_gtk__hpp_color_overhead_us;
121 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_SYS].color =
122 perf_gtk__hpp_color_overhead_guest_sys;
123 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_US].color =
124 perf_gtk__hpp_color_overhead_guest_us;
125}
126
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900127static void callchain_list__sym_name(struct callchain_list *cl,
128 char *bf, size_t bfsize)
129{
130 if (cl->ms.sym)
131 scnprintf(bf, bfsize, "%s", cl->ms.sym->name);
132 else
133 scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);
134}
135
136static void perf_gtk__add_callchain(struct rb_root *root, GtkTreeStore *store,
Namhyung Kimcc60f242013-06-04 18:22:14 +0900137 GtkTreeIter *parent, int col, u64 total)
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900138{
139 struct rb_node *nd;
140 bool has_single_node = (rb_first(root) == rb_last(root));
141
142 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
143 struct callchain_node *node;
144 struct callchain_list *chain;
145 GtkTreeIter iter, new_parent;
146 bool need_new_parent;
Namhyung Kimcc60f242013-06-04 18:22:14 +0900147 double percent;
148 u64 hits, child_total;
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900149
150 node = rb_entry(nd, struct callchain_node, rb_node);
151
Namhyung Kimcc60f242013-06-04 18:22:14 +0900152 hits = callchain_cumul_hits(node);
153 percent = 100.0 * hits / total;
154
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900155 new_parent = *parent;
156 need_new_parent = !has_single_node && (node->val_nr > 1);
157
158 list_for_each_entry(chain, &node->val, list) {
159 char buf[128];
160
161 gtk_tree_store_append(store, &iter, &new_parent);
162
Namhyung Kimcc60f242013-06-04 18:22:14 +0900163 scnprintf(buf, sizeof(buf), "%5.2f%%", percent);
164 gtk_tree_store_set(store, &iter, 0, buf, -1);
165
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900166 callchain_list__sym_name(chain, buf, sizeof(buf));
167 gtk_tree_store_set(store, &iter, col, buf, -1);
168
169 if (need_new_parent) {
170 /*
171 * Only show the top-most symbol in a callchain
172 * if it's not the only callchain.
173 */
174 new_parent = iter;
175 need_new_parent = false;
176 }
177 }
178
Namhyung Kimcc60f242013-06-04 18:22:14 +0900179 if (callchain_param.mode == CHAIN_GRAPH_REL)
180 child_total = node->children_hit;
181 else
182 child_total = total;
183
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900184 /* Now 'iter' contains info of the last callchain_list */
Namhyung Kimcc60f242013-06-04 18:22:14 +0900185 perf_gtk__add_callchain(&node->rb_root, store, &iter, col,
186 child_total);
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900187 }
188}
189
Namhyung Kim064f1982013-05-14 11:09:04 +0900190static void perf_gtk__show_hists(GtkWidget *window, struct hists *hists,
191 float min_pcnt)
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900192{
193 struct perf_hpp_fmt *fmt;
194 GType col_types[MAX_COLUMNS];
195 GtkCellRenderer *renderer;
196 struct sort_entry *se;
Namhyung Kimf1d9a532013-06-04 18:22:12 +0900197 GtkTreeStore *store;
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900198 struct rb_node *nd;
199 GtkWidget *view;
200 int col_idx;
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900201 int sym_col = -1;
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900202 int nr_cols;
203 char s[512];
204
205 struct perf_hpp hpp = {
206 .buf = s,
207 .size = sizeof(s),
Namhyung Kimcb160082013-01-22 18:09:40 +0900208 .ptr = hists_to_evsel(hists),
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900209 };
210
211 nr_cols = 0;
212
213 perf_hpp__for_each_format(fmt)
214 col_types[nr_cols++] = G_TYPE_STRING;
215
216 list_for_each_entry(se, &hist_entry__sort_list, list) {
217 if (se->elide)
218 continue;
219
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900220 if (se == &sort_sym)
221 sym_col = nr_cols;
222
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900223 col_types[nr_cols++] = G_TYPE_STRING;
224 }
225
Namhyung Kimf1d9a532013-06-04 18:22:12 +0900226 store = gtk_tree_store_newv(nr_cols, col_types);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900227
228 view = gtk_tree_view_new();
229
230 renderer = gtk_cell_renderer_text_new();
231
232 col_idx = 0;
233
234 perf_hpp__for_each_format(fmt) {
235 fmt->header(&hpp);
236
237 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
Namhyung Kim34b95642013-01-22 18:09:42 +0900238 -1, ltrim(s),
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900239 renderer, "markup",
240 col_idx++, NULL);
241 }
242
243 list_for_each_entry(se, &hist_entry__sort_list, list) {
244 if (se->elide)
245 continue;
246
247 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
248 -1, se->se_header,
249 renderer, "text",
250 col_idx++, NULL);
251 }
252
Namhyung Kim1a309422013-06-04 18:22:15 +0900253 for (col_idx = 0; col_idx < nr_cols; col_idx++) {
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900254 GtkTreeViewColumn *column;
255
Namhyung Kim1a309422013-06-04 18:22:15 +0900256 column = gtk_tree_view_get_column(GTK_TREE_VIEW(view), col_idx);
257 gtk_tree_view_column_set_resizable(column, TRUE);
258
259 if (col_idx == sym_col) {
260 gtk_tree_view_set_expander_column(GTK_TREE_VIEW(view),
261 column);
262 }
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900263 }
264
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900265 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
266
267 g_object_unref(GTK_TREE_MODEL(store));
268
269 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
270 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
271 GtkTreeIter iter;
Namhyung Kim064f1982013-05-14 11:09:04 +0900272 float percent = h->stat.period * 100.0 /
273 hists->stats.total_period;
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900274
275 if (h->filtered)
276 continue;
277
Namhyung Kim064f1982013-05-14 11:09:04 +0900278 if (percent < min_pcnt)
279 continue;
280
Namhyung Kimf1d9a532013-06-04 18:22:12 +0900281 gtk_tree_store_append(store, &iter, NULL);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900282
283 col_idx = 0;
284
285 perf_hpp__for_each_format(fmt) {
286 if (fmt->color)
287 fmt->color(&hpp, h);
288 else
289 fmt->entry(&hpp, h);
290
Namhyung Kimf1d9a532013-06-04 18:22:12 +0900291 gtk_tree_store_set(store, &iter, col_idx++, s, -1);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900292 }
293
294 list_for_each_entry(se, &hist_entry__sort_list, list) {
295 if (se->elide)
296 continue;
297
298 se->se_snprintf(h, s, ARRAY_SIZE(s),
299 hists__col_len(hists, se->se_width_idx));
300
Namhyung Kimf1d9a532013-06-04 18:22:12 +0900301 gtk_tree_store_set(store, &iter, col_idx++, s, -1);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900302 }
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900303
304 if (symbol_conf.use_callchain && sort__has_sym) {
Namhyung Kimcc60f242013-06-04 18:22:14 +0900305 u64 total;
306
307 if (callchain_param.mode == CHAIN_GRAPH_REL)
308 total = h->stat.period;
309 else
310 total = hists->stats.total_period;
311
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900312 perf_gtk__add_callchain(&h->sorted_chain, store, &iter,
Namhyung Kimcc60f242013-06-04 18:22:14 +0900313 sym_col, total);
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900314 }
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900315 }
316
317 gtk_container_add(GTK_CONTAINER(window), view);
318}
319
320int perf_evlist__gtk_browse_hists(struct perf_evlist *evlist,
321 const char *help,
Namhyung Kim064f1982013-05-14 11:09:04 +0900322 struct hist_browser_timer *hbt __maybe_unused,
323 float min_pcnt)
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900324{
325 struct perf_evsel *pos;
326 GtkWidget *vbox;
327 GtkWidget *notebook;
328 GtkWidget *info_bar;
329 GtkWidget *statbar;
330 GtkWidget *window;
331
332 signal(SIGSEGV, perf_gtk__signal);
333 signal(SIGFPE, perf_gtk__signal);
334 signal(SIGINT, perf_gtk__signal);
335 signal(SIGQUIT, perf_gtk__signal);
336 signal(SIGTERM, perf_gtk__signal);
337
338 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
339
340 gtk_window_set_title(GTK_WINDOW(window), "perf report");
341
342 g_signal_connect(window, "delete_event", gtk_main_quit, NULL);
343
344 pgctx = perf_gtk__activate_context(window);
345 if (!pgctx)
346 return -1;
347
348 vbox = gtk_vbox_new(FALSE, 0);
349
350 notebook = gtk_notebook_new();
351
Namhyung Kim6bf1a292012-12-21 17:20:14 +0900352 gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 0);
353
354 info_bar = perf_gtk__setup_info_bar();
355 if (info_bar)
356 gtk_box_pack_start(GTK_BOX(vbox), info_bar, FALSE, FALSE, 0);
357
358 statbar = perf_gtk__setup_statusbar();
359 gtk_box_pack_start(GTK_BOX(vbox), statbar, FALSE, FALSE, 0);
360
361 gtk_container_add(GTK_CONTAINER(window), vbox);
362
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900363 list_for_each_entry(pos, &evlist->entries, node) {
364 struct hists *hists = &pos->hists;
365 const char *evname = perf_evsel__name(pos);
366 GtkWidget *scrolled_window;
367 GtkWidget *tab_label;
Namhyung Kim717e2632013-01-22 18:09:44 +0900368 char buf[512];
369 size_t size = sizeof(buf);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900370
Namhyung Kim717e2632013-01-22 18:09:44 +0900371 if (symbol_conf.event_group) {
372 if (!perf_evsel__is_group_leader(pos))
373 continue;
374
375 if (pos->nr_members > 1) {
376 perf_evsel__group_desc(pos, buf, size);
377 evname = buf;
378 }
379 }
Namhyung Kimfc24d7c2013-01-22 18:09:43 +0900380
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900381 scrolled_window = gtk_scrolled_window_new(NULL, NULL);
382
383 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
384 GTK_POLICY_AUTOMATIC,
385 GTK_POLICY_AUTOMATIC);
386
Namhyung Kim064f1982013-05-14 11:09:04 +0900387 perf_gtk__show_hists(scrolled_window, hists, min_pcnt);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900388
389 tab_label = gtk_label_new(evname);
390
391 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scrolled_window, tab_label);
392 }
393
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900394 gtk_widget_show_all(window);
395
396 perf_gtk__resize_window(window);
397
398 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
399
400 ui_helpline__push(help);
401
402 gtk_main();
403
404 perf_gtk__deactivate_context(&pgctx);
405
406 return 0;
407}