blob: 3c16ab50e0f81459e19f368aed9144e7cb9d2f00 [file] [log] [blame]
Pekka Enbergc31a9452012-03-19 15:13:29 -03001#include "../evlist.h"
2#include "../cache.h"
3#include "../evsel.h"
4#include "../sort.h"
5#include "../hist.h"
Namhyung Kimed70c602012-08-16 17:14:53 +09006#include "../helpline.h"
Pekka Enbergc31a9452012-03-19 15:13:29 -03007#include "gtk.h"
8
9#include <signal.h>
10
11#define MAX_COLUMNS 32
12
Namhyung Kim28e62b92012-04-30 13:55:07 +090013static void perf_gtk__signal(int sig)
Pekka Enbergc31a9452012-03-19 15:13:29 -030014{
Namhyung Kim42ab68a2012-05-29 13:22:59 +090015 perf_gtk__exit(false);
Pekka Enbergc31a9452012-03-19 15:13:29 -030016 psignal(sig, "perf");
Pekka Enbergc31a9452012-03-19 15:13:29 -030017}
18
Namhyung Kim28e62b92012-04-30 13:55:07 +090019static void perf_gtk__resize_window(GtkWidget *window)
Pekka Enbergc31a9452012-03-19 15:13:29 -030020{
21 GdkRectangle rect;
22 GdkScreen *screen;
23 int monitor;
24 int height;
25 int width;
26
27 screen = gtk_widget_get_screen(window);
28
29 monitor = gdk_screen_get_monitor_at_window(screen, window->window);
30
31 gdk_screen_get_monitor_geometry(screen, monitor, &rect);
32
33 width = rect.width * 3 / 4;
34 height = rect.height * 3 / 4;
35
36 gtk_window_resize(GTK_WINDOW(window), width, height);
37}
38
Namhyung Kim12ceade2012-09-03 11:53:10 +090039static const char *perf_gtk__get_percent_color(double percent)
40{
41 if (percent >= MIN_RED)
42 return "<span fgcolor='red'>";
43 if (percent >= MIN_GREEN)
44 return "<span fgcolor='dark green'>";
45 return NULL;
46}
47
48#define HPP__COLOR_FN(_name, _field) \
49static int perf_gtk__hpp_color_ ## _name(struct perf_hpp *hpp, \
50 struct hist_entry *he) \
51{ \
52 double percent = 100.0 * he->_field / hpp->total_period; \
53 const char *markup; \
54 int ret = 0; \
55 \
56 markup = perf_gtk__get_percent_color(percent); \
57 if (markup) \
58 ret += scnprintf(hpp->buf, hpp->size, "%s", markup); \
59 ret += scnprintf(hpp->buf + ret, hpp->size - ret, "%5.2f%%", percent); \
60 if (markup) \
61 ret += scnprintf(hpp->buf + ret, hpp->size - ret, "</span>"); \
62 \
63 return ret; \
64}
65
66HPP__COLOR_FN(overhead, period)
67HPP__COLOR_FN(overhead_sys, period_sys)
68HPP__COLOR_FN(overhead_us, period_us)
69HPP__COLOR_FN(overhead_guest_sys, period_guest_sys)
70HPP__COLOR_FN(overhead_guest_us, period_guest_us)
71
72#undef HPP__COLOR_FN
73
74void perf_gtk__init_hpp(void)
75{
76 perf_hpp__init(false, false);
77
78 perf_hpp__format[PERF_HPP__OVERHEAD].color =
79 perf_gtk__hpp_color_overhead;
80 perf_hpp__format[PERF_HPP__OVERHEAD_SYS].color =
81 perf_gtk__hpp_color_overhead_sys;
82 perf_hpp__format[PERF_HPP__OVERHEAD_US].color =
83 perf_gtk__hpp_color_overhead_us;
84 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_SYS].color =
85 perf_gtk__hpp_color_overhead_guest_sys;
86 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_US].color =
87 perf_gtk__hpp_color_overhead_guest_us;
88}
89
Namhyung Kim28e62b92012-04-30 13:55:07 +090090static void perf_gtk__show_hists(GtkWidget *window, struct hists *hists)
Pekka Enbergc31a9452012-03-19 15:13:29 -030091{
92 GType col_types[MAX_COLUMNS];
93 GtkCellRenderer *renderer;
94 struct sort_entry *se;
95 GtkListStore *store;
96 struct rb_node *nd;
Pekka Enbergc31a9452012-03-19 15:13:29 -030097 GtkWidget *view;
Namhyung Kim12ceade2012-09-03 11:53:10 +090098 int i, col_idx;
Pekka Enbergc31a9452012-03-19 15:13:29 -030099 int nr_cols;
Namhyung Kim12ceade2012-09-03 11:53:10 +0900100 char s[512];
101
102 struct perf_hpp hpp = {
103 .buf = s,
104 .size = sizeof(s),
105 .total_period = hists->stats.total_period,
106 };
Pekka Enbergc31a9452012-03-19 15:13:29 -0300107
108 nr_cols = 0;
109
Namhyung Kim12ceade2012-09-03 11:53:10 +0900110 for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
111 if (!perf_hpp__format[i].cond)
112 continue;
113
114 col_types[nr_cols++] = G_TYPE_STRING;
115 }
Pekka Enbergc31a9452012-03-19 15:13:29 -0300116
117 list_for_each_entry(se, &hist_entry__sort_list, list) {
118 if (se->elide)
119 continue;
120
121 col_types[nr_cols++] = G_TYPE_STRING;
122 }
123
124 store = gtk_list_store_newv(nr_cols, col_types);
125
126 view = gtk_tree_view_new();
127
128 renderer = gtk_cell_renderer_text_new();
129
130 col_idx = 0;
131
Namhyung Kim12ceade2012-09-03 11:53:10 +0900132 for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
133 if (!perf_hpp__format[i].cond)
134 continue;
135
136 perf_hpp__format[i].header(&hpp);
137
138 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
139 -1, s,
140 renderer, "markup",
141 col_idx++, NULL);
142 }
Pekka Enbergc31a9452012-03-19 15:13:29 -0300143
144 list_for_each_entry(se, &hist_entry__sort_list, list) {
145 if (se->elide)
146 continue;
147
148 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
149 -1, se->se_header,
150 renderer, "text",
151 col_idx++, NULL);
152 }
153
154 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
155
156 g_object_unref(GTK_TREE_MODEL(store));
157
Pekka Enbergc31a9452012-03-19 15:13:29 -0300158 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
159 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
160 GtkTreeIter iter;
Pekka Enbergc31a9452012-03-19 15:13:29 -0300161
162 if (h->filtered)
163 continue;
164
165 gtk_list_store_append(store, &iter);
166
167 col_idx = 0;
168
Namhyung Kim12ceade2012-09-03 11:53:10 +0900169 for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
170 if (!perf_hpp__format[i].cond)
171 continue;
Pekka Enbergc31a9452012-03-19 15:13:29 -0300172
Namhyung Kim12ceade2012-09-03 11:53:10 +0900173 if (perf_hpp__format[i].color)
174 perf_hpp__format[i].color(&hpp, h);
175 else
176 perf_hpp__format[i].entry(&hpp, h);
Pekka Enbergc31a9452012-03-19 15:13:29 -0300177
Namhyung Kim12ceade2012-09-03 11:53:10 +0900178 gtk_list_store_set(store, &iter, col_idx++, s, -1);
179 }
Pekka Enbergc31a9452012-03-19 15:13:29 -0300180
181 list_for_each_entry(se, &hist_entry__sort_list, list) {
182 if (se->elide)
183 continue;
184
185 se->se_snprintf(h, s, ARRAY_SIZE(s),
186 hists__col_len(hists, se->se_width_idx));
187
188 gtk_list_store_set(store, &iter, col_idx++, s, -1);
189 }
190 }
191
192 gtk_container_add(GTK_CONTAINER(window), view);
193}
194
Namhyung Kima6b702c2012-05-29 13:23:01 +0900195#ifdef HAVE_GTK_INFO_BAR
196static GtkWidget *perf_gtk__setup_info_bar(void)
197{
198 GtkWidget *info_bar;
199 GtkWidget *label;
200 GtkWidget *content_area;
201
202 info_bar = gtk_info_bar_new();
203 gtk_widget_set_no_show_all(info_bar, TRUE);
204
205 label = gtk_label_new("");
206 gtk_widget_show(label);
207
208 content_area = gtk_info_bar_get_content_area(GTK_INFO_BAR(info_bar));
209 gtk_container_add(GTK_CONTAINER(content_area), label);
210
211 gtk_info_bar_add_button(GTK_INFO_BAR(info_bar), GTK_STOCK_OK,
212 GTK_RESPONSE_OK);
213 g_signal_connect(info_bar, "response",
214 G_CALLBACK(gtk_widget_hide), NULL);
215
216 pgctx->info_bar = info_bar;
217 pgctx->message_label = label;
218
219 return info_bar;
220}
221#endif
222
Namhyung Kimb4418c62012-05-29 13:23:00 +0900223static GtkWidget *perf_gtk__setup_statusbar(void)
224{
225 GtkWidget *stbar;
226 unsigned ctxid;
227
228 stbar = gtk_statusbar_new();
229
230 ctxid = gtk_statusbar_get_context_id(GTK_STATUSBAR(stbar),
231 "perf report");
232 pgctx->statbar = stbar;
233 pgctx->statbar_ctx_id = ctxid;
234
235 return stbar;
236}
237
Pekka Enbergc31a9452012-03-19 15:13:29 -0300238int perf_evlist__gtk_browse_hists(struct perf_evlist *evlist,
Namhyung Kimed70c602012-08-16 17:14:53 +0900239 const char *help,
Pekka Enbergc31a9452012-03-19 15:13:29 -0300240 void (*timer) (void *arg)__used,
241 void *arg __used, int delay_secs __used)
242{
243 struct perf_evsel *pos;
Namhyung Kimb4418c62012-05-29 13:23:00 +0900244 GtkWidget *vbox;
Pekka Enbergc31a9452012-03-19 15:13:29 -0300245 GtkWidget *notebook;
Namhyung Kima6b702c2012-05-29 13:23:01 +0900246 GtkWidget *info_bar;
Namhyung Kimb4418c62012-05-29 13:23:00 +0900247 GtkWidget *statbar;
Pekka Enbergc31a9452012-03-19 15:13:29 -0300248 GtkWidget *window;
249
Namhyung Kim28e62b92012-04-30 13:55:07 +0900250 signal(SIGSEGV, perf_gtk__signal);
251 signal(SIGFPE, perf_gtk__signal);
252 signal(SIGINT, perf_gtk__signal);
253 signal(SIGQUIT, perf_gtk__signal);
254 signal(SIGTERM, perf_gtk__signal);
Pekka Enbergc31a9452012-03-19 15:13:29 -0300255
256 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
257
258 gtk_window_set_title(GTK_WINDOW(window), "perf report");
259
260 g_signal_connect(window, "delete_event", gtk_main_quit, NULL);
261
Namhyung Kim42ab68a2012-05-29 13:22:59 +0900262 pgctx = perf_gtk__activate_context(window);
263 if (!pgctx)
264 return -1;
265
Namhyung Kimb4418c62012-05-29 13:23:00 +0900266 vbox = gtk_vbox_new(FALSE, 0);
267
Pekka Enbergc31a9452012-03-19 15:13:29 -0300268 notebook = gtk_notebook_new();
269
270 list_for_each_entry(pos, &evlist->entries, node) {
271 struct hists *hists = &pos->hists;
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300272 const char *evname = perf_evsel__name(pos);
Pekka Enbergc31a9452012-03-19 15:13:29 -0300273 GtkWidget *scrolled_window;
274 GtkWidget *tab_label;
275
276 scrolled_window = gtk_scrolled_window_new(NULL, NULL);
277
278 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
279 GTK_POLICY_AUTOMATIC,
280 GTK_POLICY_AUTOMATIC);
281
Namhyung Kim28e62b92012-04-30 13:55:07 +0900282 perf_gtk__show_hists(scrolled_window, hists);
Pekka Enbergc31a9452012-03-19 15:13:29 -0300283
284 tab_label = gtk_label_new(evname);
285
286 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scrolled_window, tab_label);
287 }
288
Namhyung Kimb4418c62012-05-29 13:23:00 +0900289 gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 0);
290
Namhyung Kima6b702c2012-05-29 13:23:01 +0900291 info_bar = perf_gtk__setup_info_bar();
292 if (info_bar)
293 gtk_box_pack_start(GTK_BOX(vbox), info_bar, FALSE, FALSE, 0);
294
Namhyung Kimb4418c62012-05-29 13:23:00 +0900295 statbar = perf_gtk__setup_statusbar();
296 gtk_box_pack_start(GTK_BOX(vbox), statbar, FALSE, FALSE, 0);
297
298 gtk_container_add(GTK_CONTAINER(window), vbox);
Pekka Enbergc31a9452012-03-19 15:13:29 -0300299
300 gtk_widget_show_all(window);
301
Namhyung Kim28e62b92012-04-30 13:55:07 +0900302 perf_gtk__resize_window(window);
Pekka Enbergc31a9452012-03-19 15:13:29 -0300303
304 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
305
Namhyung Kimed70c602012-08-16 17:14:53 +0900306 ui_helpline__push(help);
307
Pekka Enbergc31a9452012-03-19 15:13:29 -0300308 gtk_main();
309
Namhyung Kim42ab68a2012-05-29 13:22:59 +0900310 perf_gtk__deactivate_context(&pgctx);
311
Pekka Enbergc31a9452012-03-19 15:13:29 -0300312 return 0;
313}