Merge tag 'perf-core-for-mingo' of git://git.kernel.org/pub/scm/linux/kernel/git...
[~shefty/rdma-dev.git] / tools / perf / ui / gtk / browser.c
1 #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 #include <signal.h>
10
11 #define MAX_COLUMNS                     32
12
13 static void perf_gtk__signal(int sig)
14 {
15         perf_gtk__exit(false);
16         psignal(sig, "perf");
17 }
18
19 static void perf_gtk__resize_window(GtkWidget *window)
20 {
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
39 static 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)                                            \
49 static int perf_gtk__hpp_color_ ## _name(struct perf_hpp *hpp,                  \
50                                          struct hist_entry *he)                 \
51 {                                                                               \
52         struct hists *hists = he->hists;                                        \
53         double percent = 100.0 * he->stat._field / hists->stats.total_period;   \
54         const char *markup;                                                     \
55         int ret = 0;                                                            \
56                                                                                 \
57         markup = perf_gtk__get_percent_color(percent);                          \
58         if (markup)                                                             \
59                 ret += scnprintf(hpp->buf, hpp->size, "%s", markup);            \
60         ret += scnprintf(hpp->buf + ret, hpp->size - ret, "%6.2f%%", percent);  \
61         if (markup)                                                             \
62                 ret += scnprintf(hpp->buf + ret, hpp->size - ret, "</span>");   \
63                                                                                 \
64         return ret;                                                             \
65 }
66
67 HPP__COLOR_FN(overhead, period)
68 HPP__COLOR_FN(overhead_sys, period_sys)
69 HPP__COLOR_FN(overhead_us, period_us)
70 HPP__COLOR_FN(overhead_guest_sys, period_guest_sys)
71 HPP__COLOR_FN(overhead_guest_us, period_guest_us)
72
73 #undef HPP__COLOR_FN
74
75 void perf_gtk__init_hpp(void)
76 {
77         perf_hpp__column_enable(PERF_HPP__OVERHEAD);
78
79         perf_hpp__init();
80
81         perf_hpp__format[PERF_HPP__OVERHEAD].color =
82                                 perf_gtk__hpp_color_overhead;
83         perf_hpp__format[PERF_HPP__OVERHEAD_SYS].color =
84                                 perf_gtk__hpp_color_overhead_sys;
85         perf_hpp__format[PERF_HPP__OVERHEAD_US].color =
86                                 perf_gtk__hpp_color_overhead_us;
87         perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_SYS].color =
88                                 perf_gtk__hpp_color_overhead_guest_sys;
89         perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_US].color =
90                                 perf_gtk__hpp_color_overhead_guest_us;
91 }
92
93 static void perf_gtk__show_hists(GtkWidget *window, struct hists *hists)
94 {
95         struct perf_hpp_fmt *fmt;
96         GType col_types[MAX_COLUMNS];
97         GtkCellRenderer *renderer;
98         struct sort_entry *se;
99         GtkListStore *store;
100         struct rb_node *nd;
101         GtkWidget *view;
102         int col_idx;
103         int nr_cols;
104         char s[512];
105
106         struct perf_hpp hpp = {
107                 .buf            = s,
108                 .size           = sizeof(s),
109         };
110
111         nr_cols = 0;
112
113         perf_hpp__for_each_format(fmt)
114                 col_types[nr_cols++] = G_TYPE_STRING;
115
116         list_for_each_entry(se, &hist_entry__sort_list, list) {
117                 if (se->elide)
118                         continue;
119
120                 col_types[nr_cols++] = G_TYPE_STRING;
121         }
122
123         store = gtk_list_store_newv(nr_cols, col_types);
124
125         view = gtk_tree_view_new();
126
127         renderer = gtk_cell_renderer_text_new();
128
129         col_idx = 0;
130
131         perf_hpp__for_each_format(fmt) {
132                 fmt->header(&hpp);
133                 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
134                                                             -1, s,
135                                                             renderer, "markup",
136                                                             col_idx++, NULL);
137         }
138
139         list_for_each_entry(se, &hist_entry__sort_list, list) {
140                 if (se->elide)
141                         continue;
142
143                 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
144                                                             -1, se->se_header,
145                                                             renderer, "text",
146                                                             col_idx++, NULL);
147         }
148
149         gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
150
151         g_object_unref(GTK_TREE_MODEL(store));
152
153         for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
154                 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
155                 GtkTreeIter iter;
156
157                 if (h->filtered)
158                         continue;
159
160                 gtk_list_store_append(store, &iter);
161
162                 col_idx = 0;
163
164                 perf_hpp__for_each_format(fmt) {
165                         if (fmt->color)
166                                 fmt->color(&hpp, h);
167                         else
168                                 fmt->entry(&hpp, h);
169
170                         gtk_list_store_set(store, &iter, col_idx++, s, -1);
171                 }
172
173                 list_for_each_entry(se, &hist_entry__sort_list, list) {
174                         if (se->elide)
175                                 continue;
176
177                         se->se_snprintf(h, s, ARRAY_SIZE(s),
178                                         hists__col_len(hists, se->se_width_idx));
179
180                         gtk_list_store_set(store, &iter, col_idx++, s, -1);
181                 }
182         }
183
184         gtk_container_add(GTK_CONTAINER(window), view);
185 }
186
187 #ifdef HAVE_GTK_INFO_BAR
188 static GtkWidget *perf_gtk__setup_info_bar(void)
189 {
190         GtkWidget *info_bar;
191         GtkWidget *label;
192         GtkWidget *content_area;
193
194         info_bar = gtk_info_bar_new();
195         gtk_widget_set_no_show_all(info_bar, TRUE);
196
197         label = gtk_label_new("");
198         gtk_widget_show(label);
199
200         content_area = gtk_info_bar_get_content_area(GTK_INFO_BAR(info_bar));
201         gtk_container_add(GTK_CONTAINER(content_area), label);
202
203         gtk_info_bar_add_button(GTK_INFO_BAR(info_bar), GTK_STOCK_OK,
204                                 GTK_RESPONSE_OK);
205         g_signal_connect(info_bar, "response",
206                          G_CALLBACK(gtk_widget_hide), NULL);
207
208         pgctx->info_bar = info_bar;
209         pgctx->message_label = label;
210
211         return info_bar;
212 }
213 #endif
214
215 static GtkWidget *perf_gtk__setup_statusbar(void)
216 {
217         GtkWidget *stbar;
218         unsigned ctxid;
219
220         stbar = gtk_statusbar_new();
221
222         ctxid = gtk_statusbar_get_context_id(GTK_STATUSBAR(stbar),
223                                              "perf report");
224         pgctx->statbar = stbar;
225         pgctx->statbar_ctx_id = ctxid;
226
227         return stbar;
228 }
229
230 int perf_evlist__gtk_browse_hists(struct perf_evlist *evlist,
231                                   const char *help,
232                                   struct hist_browser_timer *hbt __maybe_unused)
233 {
234         struct perf_evsel *pos;
235         GtkWidget *vbox;
236         GtkWidget *notebook;
237         GtkWidget *info_bar;
238         GtkWidget *statbar;
239         GtkWidget *window;
240
241         signal(SIGSEGV, perf_gtk__signal);
242         signal(SIGFPE,  perf_gtk__signal);
243         signal(SIGINT,  perf_gtk__signal);
244         signal(SIGQUIT, perf_gtk__signal);
245         signal(SIGTERM, perf_gtk__signal);
246
247         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
248
249         gtk_window_set_title(GTK_WINDOW(window), "perf report");
250
251         g_signal_connect(window, "delete_event", gtk_main_quit, NULL);
252
253         pgctx = perf_gtk__activate_context(window);
254         if (!pgctx)
255                 return -1;
256
257         vbox = gtk_vbox_new(FALSE, 0);
258
259         notebook = gtk_notebook_new();
260
261         list_for_each_entry(pos, &evlist->entries, node) {
262                 struct hists *hists = &pos->hists;
263                 const char *evname = perf_evsel__name(pos);
264                 GtkWidget *scrolled_window;
265                 GtkWidget *tab_label;
266
267                 scrolled_window = gtk_scrolled_window_new(NULL, NULL);
268
269                 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
270                                                         GTK_POLICY_AUTOMATIC,
271                                                         GTK_POLICY_AUTOMATIC);
272
273                 perf_gtk__show_hists(scrolled_window, hists);
274
275                 tab_label = gtk_label_new(evname);
276
277                 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scrolled_window, tab_label);
278         }
279
280         gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 0);
281
282         info_bar = perf_gtk__setup_info_bar();
283         if (info_bar)
284                 gtk_box_pack_start(GTK_BOX(vbox), info_bar, FALSE, FALSE, 0);
285
286         statbar = perf_gtk__setup_statusbar();
287         gtk_box_pack_start(GTK_BOX(vbox), statbar, FALSE, FALSE, 0);
288
289         gtk_container_add(GTK_CONTAINER(window), vbox);
290
291         gtk_widget_show_all(window);
292
293         perf_gtk__resize_window(window);
294
295         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
296
297         ui_helpline__push(help);
298
299         gtk_main();
300
301         perf_gtk__deactivate_context(&pgctx);
302
303         return 0;
304 }