blob: 9d90683914d46cc28c1aa88db2e2d54e863f5ae9 [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 Kimfb821c9e2014-03-03 17:05:19 +090046 return __hpp__fmt(hpp, he, he_get_##_field, " %6.2f%%", \
Namhyung Kim4a621092014-03-03 10:14:03 +090047 __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__format[PERF_HPP__OVERHEAD].color =
62 perf_gtk__hpp_color_overhead;
63 perf_hpp__format[PERF_HPP__OVERHEAD_SYS].color =
64 perf_gtk__hpp_color_overhead_sys;
65 perf_hpp__format[PERF_HPP__OVERHEAD_US].color =
66 perf_gtk__hpp_color_overhead_us;
67 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_SYS].color =
68 perf_gtk__hpp_color_overhead_guest_sys;
69 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_US].color =
70 perf_gtk__hpp_color_overhead_guest_us;
71}
72
Namhyung Kim2bbc5872013-06-04 18:22:13 +090073static void callchain_list__sym_name(struct callchain_list *cl,
74 char *bf, size_t bfsize)
75{
76 if (cl->ms.sym)
77 scnprintf(bf, bfsize, "%s", cl->ms.sym->name);
78 else
79 scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);
80}
81
82static void perf_gtk__add_callchain(struct rb_root *root, GtkTreeStore *store,
Namhyung Kimcc60f242013-06-04 18:22:14 +090083 GtkTreeIter *parent, int col, u64 total)
Namhyung Kim2bbc5872013-06-04 18:22:13 +090084{
85 struct rb_node *nd;
86 bool has_single_node = (rb_first(root) == rb_last(root));
87
88 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
89 struct callchain_node *node;
90 struct callchain_list *chain;
91 GtkTreeIter iter, new_parent;
92 bool need_new_parent;
Namhyung Kimcc60f242013-06-04 18:22:14 +090093 double percent;
94 u64 hits, child_total;
Namhyung Kim2bbc5872013-06-04 18:22:13 +090095
96 node = rb_entry(nd, struct callchain_node, rb_node);
97
Namhyung Kimcc60f242013-06-04 18:22:14 +090098 hits = callchain_cumul_hits(node);
99 percent = 100.0 * hits / total;
100
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900101 new_parent = *parent;
102 need_new_parent = !has_single_node && (node->val_nr > 1);
103
104 list_for_each_entry(chain, &node->val, list) {
105 char buf[128];
106
107 gtk_tree_store_append(store, &iter, &new_parent);
108
Namhyung Kimcc60f242013-06-04 18:22:14 +0900109 scnprintf(buf, sizeof(buf), "%5.2f%%", percent);
110 gtk_tree_store_set(store, &iter, 0, buf, -1);
111
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900112 callchain_list__sym_name(chain, buf, sizeof(buf));
113 gtk_tree_store_set(store, &iter, col, buf, -1);
114
115 if (need_new_parent) {
116 /*
117 * Only show the top-most symbol in a callchain
118 * if it's not the only callchain.
119 */
120 new_parent = iter;
121 need_new_parent = false;
122 }
123 }
124
Namhyung Kimcc60f242013-06-04 18:22:14 +0900125 if (callchain_param.mode == CHAIN_GRAPH_REL)
126 child_total = node->children_hit;
127 else
128 child_total = total;
129
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900130 /* Now 'iter' contains info of the last callchain_list */
Namhyung Kimcc60f242013-06-04 18:22:14 +0900131 perf_gtk__add_callchain(&node->rb_root, store, &iter, col,
132 child_total);
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900133 }
134}
135
Namhyung Kim450f3902013-06-04 18:22:16 +0900136static void on_row_activated(GtkTreeView *view, GtkTreePath *path,
137 GtkTreeViewColumn *col __maybe_unused,
138 gpointer user_data __maybe_unused)
139{
140 bool expanded = gtk_tree_view_row_expanded(view, path);
141
142 if (expanded)
143 gtk_tree_view_collapse_row(view, path);
144 else
145 gtk_tree_view_expand_row(view, path, FALSE);
146}
147
Namhyung Kim064f1982013-05-14 11:09:04 +0900148static void perf_gtk__show_hists(GtkWidget *window, struct hists *hists,
149 float min_pcnt)
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900150{
151 struct perf_hpp_fmt *fmt;
152 GType col_types[MAX_COLUMNS];
153 GtkCellRenderer *renderer;
Namhyung Kimf1d9a532013-06-04 18:22:12 +0900154 GtkTreeStore *store;
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900155 struct rb_node *nd;
156 GtkWidget *view;
157 int col_idx;
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900158 int sym_col = -1;
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900159 int nr_cols;
160 char s[512];
161
162 struct perf_hpp hpp = {
163 .buf = s,
164 .size = sizeof(s),
165 };
166
167 nr_cols = 0;
168
169 perf_hpp__for_each_format(fmt)
170 col_types[nr_cols++] = G_TYPE_STRING;
171
Namhyung Kimf1d9a532013-06-04 18:22:12 +0900172 store = gtk_tree_store_newv(nr_cols, col_types);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900173
174 view = gtk_tree_view_new();
175
176 renderer = gtk_cell_renderer_text_new();
177
178 col_idx = 0;
179
180 perf_hpp__for_each_format(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +0900181 if (perf_hpp__should_skip(fmt))
182 continue;
183
Namhyung Kim94a07932014-03-10 16:43:52 +0900184 fmt->header(fmt, &hpp, hists_to_evsel(hists));
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900185
186 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
Namhyung Kim34b95642013-01-22 18:09:42 +0900187 -1, ltrim(s),
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900188 renderer, "markup",
189 col_idx++, NULL);
190 }
191
Namhyung Kim1a309422013-06-04 18:22:15 +0900192 for (col_idx = 0; col_idx < nr_cols; col_idx++) {
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900193 GtkTreeViewColumn *column;
194
Namhyung Kim1a309422013-06-04 18:22:15 +0900195 column = gtk_tree_view_get_column(GTK_TREE_VIEW(view), col_idx);
196 gtk_tree_view_column_set_resizable(column, TRUE);
197
198 if (col_idx == sym_col) {
199 gtk_tree_view_set_expander_column(GTK_TREE_VIEW(view),
200 column);
201 }
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900202 }
203
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900204 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
205
206 g_object_unref(GTK_TREE_MODEL(store));
207
208 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
209 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
210 GtkTreeIter iter;
Namhyung Kimf2148332014-01-14 11:52:48 +0900211 u64 total = hists__total_period(h->hists);
212 float percent = 0.0;
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900213
214 if (h->filtered)
215 continue;
216
Namhyung Kimf2148332014-01-14 11:52:48 +0900217 if (total)
218 percent = h->stat.period * 100.0 / total;
219
Namhyung Kim064f1982013-05-14 11:09:04 +0900220 if (percent < min_pcnt)
221 continue;
222
Namhyung Kimf1d9a532013-06-04 18:22:12 +0900223 gtk_tree_store_append(store, &iter, NULL);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900224
225 col_idx = 0;
226
227 perf_hpp__for_each_format(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +0900228 if (perf_hpp__should_skip(fmt))
229 continue;
230
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900231 if (fmt->color)
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100232 fmt->color(fmt, &hpp, h);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900233 else
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100234 fmt->entry(fmt, &hpp, h);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900235
Namhyung Kimf1d9a532013-06-04 18:22:12 +0900236 gtk_tree_store_set(store, &iter, col_idx++, s, -1);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900237 }
238
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900239 if (symbol_conf.use_callchain && sort__has_sym) {
Namhyung Kimcc60f242013-06-04 18:22:14 +0900240 if (callchain_param.mode == CHAIN_GRAPH_REL)
241 total = h->stat.period;
Namhyung Kimcc60f242013-06-04 18:22:14 +0900242
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900243 perf_gtk__add_callchain(&h->sorted_chain, store, &iter,
Namhyung Kimcc60f242013-06-04 18:22:14 +0900244 sym_col, total);
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900245 }
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900246 }
247
Namhyung Kim9d58d2f2013-06-04 18:22:17 +0900248 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(view), TRUE);
249
Namhyung Kim450f3902013-06-04 18:22:16 +0900250 g_signal_connect(view, "row-activated",
251 G_CALLBACK(on_row_activated), NULL);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900252 gtk_container_add(GTK_CONTAINER(window), view);
253}
254
255int perf_evlist__gtk_browse_hists(struct perf_evlist *evlist,
256 const char *help,
Namhyung Kim064f1982013-05-14 11:09:04 +0900257 struct hist_browser_timer *hbt __maybe_unused,
258 float min_pcnt)
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900259{
260 struct perf_evsel *pos;
261 GtkWidget *vbox;
262 GtkWidget *notebook;
263 GtkWidget *info_bar;
264 GtkWidget *statbar;
265 GtkWidget *window;
266
267 signal(SIGSEGV, perf_gtk__signal);
268 signal(SIGFPE, perf_gtk__signal);
269 signal(SIGINT, perf_gtk__signal);
270 signal(SIGQUIT, perf_gtk__signal);
271 signal(SIGTERM, perf_gtk__signal);
272
273 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
274
275 gtk_window_set_title(GTK_WINDOW(window), "perf report");
276
277 g_signal_connect(window, "delete_event", gtk_main_quit, NULL);
278
279 pgctx = perf_gtk__activate_context(window);
280 if (!pgctx)
281 return -1;
282
283 vbox = gtk_vbox_new(FALSE, 0);
284
285 notebook = gtk_notebook_new();
286
Namhyung Kim6bf1a292012-12-21 17:20:14 +0900287 gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 0);
288
289 info_bar = perf_gtk__setup_info_bar();
290 if (info_bar)
291 gtk_box_pack_start(GTK_BOX(vbox), info_bar, FALSE, FALSE, 0);
292
293 statbar = perf_gtk__setup_statusbar();
294 gtk_box_pack_start(GTK_BOX(vbox), statbar, FALSE, FALSE, 0);
295
296 gtk_container_add(GTK_CONTAINER(window), vbox);
297
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300298 evlist__for_each(evlist, pos) {
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900299 struct hists *hists = &pos->hists;
300 const char *evname = perf_evsel__name(pos);
301 GtkWidget *scrolled_window;
302 GtkWidget *tab_label;
Namhyung Kim717e2632013-01-22 18:09:44 +0900303 char buf[512];
304 size_t size = sizeof(buf);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900305
Namhyung Kim717e2632013-01-22 18:09:44 +0900306 if (symbol_conf.event_group) {
307 if (!perf_evsel__is_group_leader(pos))
308 continue;
309
310 if (pos->nr_members > 1) {
311 perf_evsel__group_desc(pos, buf, size);
312 evname = buf;
313 }
314 }
Namhyung Kimfc24d7c2013-01-22 18:09:43 +0900315
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900316 scrolled_window = gtk_scrolled_window_new(NULL, NULL);
317
318 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
319 GTK_POLICY_AUTOMATIC,
320 GTK_POLICY_AUTOMATIC);
321
Namhyung Kim064f1982013-05-14 11:09:04 +0900322 perf_gtk__show_hists(scrolled_window, hists, min_pcnt);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900323
324 tab_label = gtk_label_new(evname);
325
326 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scrolled_window, tab_label);
327 }
328
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900329 gtk_widget_show_all(window);
330
331 perf_gtk__resize_window(window);
332
333 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
334
335 ui_helpline__push(help);
336
337 gtk_main();
338
339 perf_gtk__deactivate_context(&pgctx);
340
341 return 0;
342}