blob: 430fd555b1619626f2f88e48c08d42cc6937aff7 [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 Kima0088ad2014-03-03 10:14:04 +090011static int __percent_color_snprintf(struct perf_hpp *hpp, const char *fmt, ...)
Namhyung Kim843985e2013-01-22 18:09:36 +090012{
13 int ret = 0;
Namhyung Kim4a621092014-03-03 10:14:03 +090014 va_list args;
15 double percent;
Namhyung Kim843985e2013-01-22 18:09:36 +090016 const char *markup;
Namhyung Kima0088ad2014-03-03 10:14:04 +090017 char *buf = hpp->buf;
18 size_t size = hpp->size;
Namhyung Kim843985e2013-01-22 18:09:36 +090019
Namhyung Kim4a621092014-03-03 10:14:03 +090020 va_start(args, fmt);
21 percent = va_arg(args, double);
22 va_end(args);
23
Namhyung Kim843985e2013-01-22 18:09:36 +090024 markup = perf_gtk__get_percent_color(percent);
25 if (markup)
26 ret += scnprintf(buf, size, markup);
27
Namhyung Kim4a621092014-03-03 10:14:03 +090028 ret += scnprintf(buf + ret, size - ret, fmt, percent);
Namhyung Kim843985e2013-01-22 18:09:36 +090029
30 if (markup)
31 ret += scnprintf(buf + ret, size - ret, "</span>");
32
33 return ret;
Namhyung Kim0da41ce92012-12-21 17:20:13 +090034}
35
Namhyung Kim843985e2013-01-22 18:09:36 +090036#define __HPP_COLOR_PERCENT_FN(_type, _field) \
37static u64 he_get_##_field(struct hist_entry *he) \
38{ \
39 return he->stat._field; \
40} \
41 \
Jiri Olsa2c5d4b42013-01-31 23:31:11 +010042static int perf_gtk__hpp_color_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
43 struct perf_hpp *hpp, \
Namhyung Kim843985e2013-01-22 18:09:36 +090044 struct hist_entry *he) \
45{ \
Namhyung Kim4a621092014-03-03 10:14:03 +090046 return __hpp__fmt(hpp, he, he_get_##_field, " %6.2f%%", \
47 __percent_color_snprintf, true); \
Namhyung Kim843985e2013-01-22 18:09:36 +090048}
49
50__HPP_COLOR_PERCENT_FN(overhead, period)
51__HPP_COLOR_PERCENT_FN(overhead_sys, period_sys)
52__HPP_COLOR_PERCENT_FN(overhead_us, period_us)
53__HPP_COLOR_PERCENT_FN(overhead_guest_sys, period_guest_sys)
54__HPP_COLOR_PERCENT_FN(overhead_guest_us, period_guest_us)
55
56#undef __HPP_COLOR_PERCENT_FN
Namhyung Kim0da41ce92012-12-21 17:20:13 +090057
58
59void perf_gtk__init_hpp(void)
60{
Namhyung Kim0da41ce92012-12-21 17:20:13 +090061 perf_hpp__init();
62
63 perf_hpp__format[PERF_HPP__OVERHEAD].color =
64 perf_gtk__hpp_color_overhead;
65 perf_hpp__format[PERF_HPP__OVERHEAD_SYS].color =
66 perf_gtk__hpp_color_overhead_sys;
67 perf_hpp__format[PERF_HPP__OVERHEAD_US].color =
68 perf_gtk__hpp_color_overhead_us;
69 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_SYS].color =
70 perf_gtk__hpp_color_overhead_guest_sys;
71 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_US].color =
72 perf_gtk__hpp_color_overhead_guest_us;
73}
74
Namhyung Kim2bbc5872013-06-04 18:22:13 +090075static void callchain_list__sym_name(struct callchain_list *cl,
76 char *bf, size_t bfsize)
77{
78 if (cl->ms.sym)
79 scnprintf(bf, bfsize, "%s", cl->ms.sym->name);
80 else
81 scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);
82}
83
84static void perf_gtk__add_callchain(struct rb_root *root, GtkTreeStore *store,
Namhyung Kimcc60f242013-06-04 18:22:14 +090085 GtkTreeIter *parent, int col, u64 total)
Namhyung Kim2bbc5872013-06-04 18:22:13 +090086{
87 struct rb_node *nd;
88 bool has_single_node = (rb_first(root) == rb_last(root));
89
90 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
91 struct callchain_node *node;
92 struct callchain_list *chain;
93 GtkTreeIter iter, new_parent;
94 bool need_new_parent;
Namhyung Kimcc60f242013-06-04 18:22:14 +090095 double percent;
96 u64 hits, child_total;
Namhyung Kim2bbc5872013-06-04 18:22:13 +090097
98 node = rb_entry(nd, struct callchain_node, rb_node);
99
Namhyung Kimcc60f242013-06-04 18:22:14 +0900100 hits = callchain_cumul_hits(node);
101 percent = 100.0 * hits / total;
102
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900103 new_parent = *parent;
104 need_new_parent = !has_single_node && (node->val_nr > 1);
105
106 list_for_each_entry(chain, &node->val, list) {
107 char buf[128];
108
109 gtk_tree_store_append(store, &iter, &new_parent);
110
Namhyung Kimcc60f242013-06-04 18:22:14 +0900111 scnprintf(buf, sizeof(buf), "%5.2f%%", percent);
112 gtk_tree_store_set(store, &iter, 0, buf, -1);
113
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900114 callchain_list__sym_name(chain, buf, sizeof(buf));
115 gtk_tree_store_set(store, &iter, col, buf, -1);
116
117 if (need_new_parent) {
118 /*
119 * Only show the top-most symbol in a callchain
120 * if it's not the only callchain.
121 */
122 new_parent = iter;
123 need_new_parent = false;
124 }
125 }
126
Namhyung Kimcc60f242013-06-04 18:22:14 +0900127 if (callchain_param.mode == CHAIN_GRAPH_REL)
128 child_total = node->children_hit;
129 else
130 child_total = total;
131
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900132 /* Now 'iter' contains info of the last callchain_list */
Namhyung Kimcc60f242013-06-04 18:22:14 +0900133 perf_gtk__add_callchain(&node->rb_root, store, &iter, col,
134 child_total);
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900135 }
136}
137
Namhyung Kim450f3902013-06-04 18:22:16 +0900138static void on_row_activated(GtkTreeView *view, GtkTreePath *path,
139 GtkTreeViewColumn *col __maybe_unused,
140 gpointer user_data __maybe_unused)
141{
142 bool expanded = gtk_tree_view_row_expanded(view, path);
143
144 if (expanded)
145 gtk_tree_view_collapse_row(view, path);
146 else
147 gtk_tree_view_expand_row(view, path, FALSE);
148}
149
Namhyung Kim064f1982013-05-14 11:09:04 +0900150static void perf_gtk__show_hists(GtkWidget *window, struct hists *hists,
151 float min_pcnt)
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900152{
153 struct perf_hpp_fmt *fmt;
154 GType col_types[MAX_COLUMNS];
155 GtkCellRenderer *renderer;
156 struct sort_entry *se;
Namhyung Kimf1d9a532013-06-04 18:22:12 +0900157 GtkTreeStore *store;
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900158 struct rb_node *nd;
159 GtkWidget *view;
160 int col_idx;
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900161 int sym_col = -1;
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900162 int nr_cols;
163 char s[512];
164
165 struct perf_hpp hpp = {
166 .buf = s,
167 .size = sizeof(s),
Namhyung Kimcb160082013-01-22 18:09:40 +0900168 .ptr = hists_to_evsel(hists),
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900169 };
170
171 nr_cols = 0;
172
173 perf_hpp__for_each_format(fmt)
174 col_types[nr_cols++] = G_TYPE_STRING;
175
176 list_for_each_entry(se, &hist_entry__sort_list, list) {
177 if (se->elide)
178 continue;
179
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900180 if (se == &sort_sym)
181 sym_col = nr_cols;
182
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900183 col_types[nr_cols++] = G_TYPE_STRING;
184 }
185
Namhyung Kimf1d9a532013-06-04 18:22:12 +0900186 store = gtk_tree_store_newv(nr_cols, col_types);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900187
188 view = gtk_tree_view_new();
189
190 renderer = gtk_cell_renderer_text_new();
191
192 col_idx = 0;
193
194 perf_hpp__for_each_format(fmt) {
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100195 fmt->header(fmt, &hpp);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900196
197 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
Namhyung Kim34b95642013-01-22 18:09:42 +0900198 -1, ltrim(s),
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900199 renderer, "markup",
200 col_idx++, NULL);
201 }
202
203 list_for_each_entry(se, &hist_entry__sort_list, list) {
204 if (se->elide)
205 continue;
206
207 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
208 -1, se->se_header,
209 renderer, "text",
210 col_idx++, NULL);
211 }
212
Namhyung Kim1a309422013-06-04 18:22:15 +0900213 for (col_idx = 0; col_idx < nr_cols; col_idx++) {
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900214 GtkTreeViewColumn *column;
215
Namhyung Kim1a309422013-06-04 18:22:15 +0900216 column = gtk_tree_view_get_column(GTK_TREE_VIEW(view), col_idx);
217 gtk_tree_view_column_set_resizable(column, TRUE);
218
219 if (col_idx == sym_col) {
220 gtk_tree_view_set_expander_column(GTK_TREE_VIEW(view),
221 column);
222 }
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900223 }
224
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900225 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
226
227 g_object_unref(GTK_TREE_MODEL(store));
228
229 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
230 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
231 GtkTreeIter iter;
Namhyung Kim064f1982013-05-14 11:09:04 +0900232 float percent = h->stat.period * 100.0 /
233 hists->stats.total_period;
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900234
235 if (h->filtered)
236 continue;
237
Namhyung Kim064f1982013-05-14 11:09:04 +0900238 if (percent < min_pcnt)
239 continue;
240
Namhyung Kimf1d9a532013-06-04 18:22:12 +0900241 gtk_tree_store_append(store, &iter, NULL);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900242
243 col_idx = 0;
244
245 perf_hpp__for_each_format(fmt) {
246 if (fmt->color)
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100247 fmt->color(fmt, &hpp, h);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900248 else
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100249 fmt->entry(fmt, &hpp, h);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900250
Namhyung Kimf1d9a532013-06-04 18:22:12 +0900251 gtk_tree_store_set(store, &iter, col_idx++, s, -1);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900252 }
253
254 list_for_each_entry(se, &hist_entry__sort_list, list) {
255 if (se->elide)
256 continue;
257
258 se->se_snprintf(h, s, ARRAY_SIZE(s),
259 hists__col_len(hists, se->se_width_idx));
260
Namhyung Kimf1d9a532013-06-04 18:22:12 +0900261 gtk_tree_store_set(store, &iter, col_idx++, s, -1);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900262 }
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900263
264 if (symbol_conf.use_callchain && sort__has_sym) {
Namhyung Kimcc60f242013-06-04 18:22:14 +0900265 u64 total;
266
267 if (callchain_param.mode == CHAIN_GRAPH_REL)
268 total = h->stat.period;
269 else
270 total = hists->stats.total_period;
271
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900272 perf_gtk__add_callchain(&h->sorted_chain, store, &iter,
Namhyung Kimcc60f242013-06-04 18:22:14 +0900273 sym_col, total);
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900274 }
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900275 }
276
Namhyung Kim9d58d2f2013-06-04 18:22:17 +0900277 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(view), TRUE);
278
Namhyung Kim450f3902013-06-04 18:22:16 +0900279 g_signal_connect(view, "row-activated",
280 G_CALLBACK(on_row_activated), NULL);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900281 gtk_container_add(GTK_CONTAINER(window), view);
282}
283
284int perf_evlist__gtk_browse_hists(struct perf_evlist *evlist,
285 const char *help,
Namhyung Kim064f1982013-05-14 11:09:04 +0900286 struct hist_browser_timer *hbt __maybe_unused,
287 float min_pcnt)
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900288{
289 struct perf_evsel *pos;
290 GtkWidget *vbox;
291 GtkWidget *notebook;
292 GtkWidget *info_bar;
293 GtkWidget *statbar;
294 GtkWidget *window;
295
296 signal(SIGSEGV, perf_gtk__signal);
297 signal(SIGFPE, perf_gtk__signal);
298 signal(SIGINT, perf_gtk__signal);
299 signal(SIGQUIT, perf_gtk__signal);
300 signal(SIGTERM, perf_gtk__signal);
301
302 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
303
304 gtk_window_set_title(GTK_WINDOW(window), "perf report");
305
306 g_signal_connect(window, "delete_event", gtk_main_quit, NULL);
307
308 pgctx = perf_gtk__activate_context(window);
309 if (!pgctx)
310 return -1;
311
312 vbox = gtk_vbox_new(FALSE, 0);
313
314 notebook = gtk_notebook_new();
315
Namhyung Kim6bf1a292012-12-21 17:20:14 +0900316 gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 0);
317
318 info_bar = perf_gtk__setup_info_bar();
319 if (info_bar)
320 gtk_box_pack_start(GTK_BOX(vbox), info_bar, FALSE, FALSE, 0);
321
322 statbar = perf_gtk__setup_statusbar();
323 gtk_box_pack_start(GTK_BOX(vbox), statbar, FALSE, FALSE, 0);
324
325 gtk_container_add(GTK_CONTAINER(window), vbox);
326
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300327 evlist__for_each(evlist, pos) {
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900328 struct hists *hists = &pos->hists;
329 const char *evname = perf_evsel__name(pos);
330 GtkWidget *scrolled_window;
331 GtkWidget *tab_label;
Namhyung Kim717e2632013-01-22 18:09:44 +0900332 char buf[512];
333 size_t size = sizeof(buf);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900334
Namhyung Kim717e2632013-01-22 18:09:44 +0900335 if (symbol_conf.event_group) {
336 if (!perf_evsel__is_group_leader(pos))
337 continue;
338
339 if (pos->nr_members > 1) {
340 perf_evsel__group_desc(pos, buf, size);
341 evname = buf;
342 }
343 }
Namhyung Kimfc24d7c2013-01-22 18:09:43 +0900344
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900345 scrolled_window = gtk_scrolled_window_new(NULL, NULL);
346
347 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
348 GTK_POLICY_AUTOMATIC,
349 GTK_POLICY_AUTOMATIC);
350
Namhyung Kim064f1982013-05-14 11:09:04 +0900351 perf_gtk__show_hists(scrolled_window, hists, min_pcnt);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900352
353 tab_label = gtk_label_new(evname);
354
355 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scrolled_window, tab_label);
356 }
357
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900358 gtk_widget_show_all(window);
359
360 perf_gtk__resize_window(window);
361
362 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
363
364 ui_helpline__push(help);
365
366 gtk_main();
367
368 perf_gtk__deactivate_context(&pgctx);
369
370 return 0;
371}