blob: 2237245bfac0e929511525c4922e3d79675834cf [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__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;
Namhyung Kimf1d9a532013-06-04 18:22:12 +0900156 GtkTreeStore *store;
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900157 struct rb_node *nd;
158 GtkWidget *view;
159 int col_idx;
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900160 int sym_col = -1;
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900161 int nr_cols;
162 char s[512];
163
164 struct perf_hpp hpp = {
165 .buf = s,
166 .size = sizeof(s),
167 };
168
169 nr_cols = 0;
170
171 perf_hpp__for_each_format(fmt)
172 col_types[nr_cols++] = G_TYPE_STRING;
173
Namhyung Kimf1d9a532013-06-04 18:22:12 +0900174 store = gtk_tree_store_newv(nr_cols, col_types);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900175
176 view = gtk_tree_view_new();
177
178 renderer = gtk_cell_renderer_text_new();
179
180 col_idx = 0;
181
182 perf_hpp__for_each_format(fmt) {
Namhyung Kim94a07932014-03-10 16:43:52 +0900183 fmt->header(fmt, &hpp, hists_to_evsel(hists));
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900184
185 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
Namhyung Kim34b95642013-01-22 18:09:42 +0900186 -1, ltrim(s),
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900187 renderer, "markup",
188 col_idx++, NULL);
189 }
190
Namhyung Kim1a309422013-06-04 18:22:15 +0900191 for (col_idx = 0; col_idx < nr_cols; col_idx++) {
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900192 GtkTreeViewColumn *column;
193
Namhyung Kim1a309422013-06-04 18:22:15 +0900194 column = gtk_tree_view_get_column(GTK_TREE_VIEW(view), col_idx);
195 gtk_tree_view_column_set_resizable(column, TRUE);
196
197 if (col_idx == sym_col) {
198 gtk_tree_view_set_expander_column(GTK_TREE_VIEW(view),
199 column);
200 }
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900201 }
202
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900203 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
204
205 g_object_unref(GTK_TREE_MODEL(store));
206
207 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
208 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
209 GtkTreeIter iter;
Namhyung Kimf2148332014-01-14 11:52:48 +0900210 u64 total = hists__total_period(h->hists);
211 float percent = 0.0;
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900212
213 if (h->filtered)
214 continue;
215
Namhyung Kimf2148332014-01-14 11:52:48 +0900216 if (total)
217 percent = h->stat.period * 100.0 / total;
218
Namhyung Kim064f1982013-05-14 11:09:04 +0900219 if (percent < min_pcnt)
220 continue;
221
Namhyung Kimf1d9a532013-06-04 18:22:12 +0900222 gtk_tree_store_append(store, &iter, NULL);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900223
224 col_idx = 0;
225
226 perf_hpp__for_each_format(fmt) {
227 if (fmt->color)
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100228 fmt->color(fmt, &hpp, h);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900229 else
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100230 fmt->entry(fmt, &hpp, h);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900231
Namhyung Kimf1d9a532013-06-04 18:22:12 +0900232 gtk_tree_store_set(store, &iter, col_idx++, s, -1);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900233 }
234
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900235 if (symbol_conf.use_callchain && sort__has_sym) {
Namhyung Kimcc60f242013-06-04 18:22:14 +0900236 if (callchain_param.mode == CHAIN_GRAPH_REL)
237 total = h->stat.period;
Namhyung Kimcc60f242013-06-04 18:22:14 +0900238
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900239 perf_gtk__add_callchain(&h->sorted_chain, store, &iter,
Namhyung Kimcc60f242013-06-04 18:22:14 +0900240 sym_col, total);
Namhyung Kim2bbc5872013-06-04 18:22:13 +0900241 }
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900242 }
243
Namhyung Kim9d58d2f2013-06-04 18:22:17 +0900244 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(view), TRUE);
245
Namhyung Kim450f3902013-06-04 18:22:16 +0900246 g_signal_connect(view, "row-activated",
247 G_CALLBACK(on_row_activated), NULL);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900248 gtk_container_add(GTK_CONTAINER(window), view);
249}
250
251int perf_evlist__gtk_browse_hists(struct perf_evlist *evlist,
252 const char *help,
Namhyung Kim064f1982013-05-14 11:09:04 +0900253 struct hist_browser_timer *hbt __maybe_unused,
254 float min_pcnt)
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900255{
256 struct perf_evsel *pos;
257 GtkWidget *vbox;
258 GtkWidget *notebook;
259 GtkWidget *info_bar;
260 GtkWidget *statbar;
261 GtkWidget *window;
262
263 signal(SIGSEGV, perf_gtk__signal);
264 signal(SIGFPE, perf_gtk__signal);
265 signal(SIGINT, perf_gtk__signal);
266 signal(SIGQUIT, perf_gtk__signal);
267 signal(SIGTERM, perf_gtk__signal);
268
269 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
270
271 gtk_window_set_title(GTK_WINDOW(window), "perf report");
272
273 g_signal_connect(window, "delete_event", gtk_main_quit, NULL);
274
275 pgctx = perf_gtk__activate_context(window);
276 if (!pgctx)
277 return -1;
278
279 vbox = gtk_vbox_new(FALSE, 0);
280
281 notebook = gtk_notebook_new();
282
Namhyung Kim6bf1a292012-12-21 17:20:14 +0900283 gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 0);
284
285 info_bar = perf_gtk__setup_info_bar();
286 if (info_bar)
287 gtk_box_pack_start(GTK_BOX(vbox), info_bar, FALSE, FALSE, 0);
288
289 statbar = perf_gtk__setup_statusbar();
290 gtk_box_pack_start(GTK_BOX(vbox), statbar, FALSE, FALSE, 0);
291
292 gtk_container_add(GTK_CONTAINER(window), vbox);
293
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300294 evlist__for_each(evlist, pos) {
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900295 struct hists *hists = &pos->hists;
296 const char *evname = perf_evsel__name(pos);
297 GtkWidget *scrolled_window;
298 GtkWidget *tab_label;
Namhyung Kim717e2632013-01-22 18:09:44 +0900299 char buf[512];
300 size_t size = sizeof(buf);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900301
Namhyung Kim717e2632013-01-22 18:09:44 +0900302 if (symbol_conf.event_group) {
303 if (!perf_evsel__is_group_leader(pos))
304 continue;
305
306 if (pos->nr_members > 1) {
307 perf_evsel__group_desc(pos, buf, size);
308 evname = buf;
309 }
310 }
Namhyung Kimfc24d7c2013-01-22 18:09:43 +0900311
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900312 scrolled_window = gtk_scrolled_window_new(NULL, NULL);
313
314 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
315 GTK_POLICY_AUTOMATIC,
316 GTK_POLICY_AUTOMATIC);
317
Namhyung Kim064f1982013-05-14 11:09:04 +0900318 perf_gtk__show_hists(scrolled_window, hists, min_pcnt);
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900319
320 tab_label = gtk_label_new(evname);
321
322 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scrolled_window, tab_label);
323 }
324
Namhyung Kim0da41ce92012-12-21 17:20:13 +0900325 gtk_widget_show_all(window);
326
327 perf_gtk__resize_window(window);
328
329 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
330
331 ui_helpline__push(help);
332
333 gtk_main();
334
335 perf_gtk__deactivate_context(&pgctx);
336
337 return 0;
338}