Merge tag 'dt' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[platform/adaptation/renesas_rcar/renesas_kernel.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         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, "%6.2f%%", percent);  \
60         if (markup)                                                             \
61                 ret += scnprintf(hpp->buf + ret, hpp->size - ret, "</span>");   \
62                                                                                 \
63         return ret;                                                             \
64 }
65
66 HPP__COLOR_FN(overhead, period)
67 HPP__COLOR_FN(overhead_sys, period_sys)
68 HPP__COLOR_FN(overhead_us, period_us)
69 HPP__COLOR_FN(overhead_guest_sys, period_guest_sys)
70 HPP__COLOR_FN(overhead_guest_us, period_guest_us)
71
72 #undef HPP__COLOR_FN
73
74 void 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
90 static void perf_gtk__show_hists(GtkWidget *window, struct hists *hists)
91 {
92         GType col_types[MAX_COLUMNS];
93         GtkCellRenderer *renderer;
94         struct sort_entry *se;
95         GtkListStore *store;
96         struct rb_node *nd;
97         GtkWidget *view;
98         int i, col_idx;
99         int nr_cols;
100         char s[512];
101
102         struct perf_hpp hpp = {
103                 .buf            = s,
104                 .size           = sizeof(s),
105                 .total_period   = hists->stats.total_period,
106         };
107
108         nr_cols = 0;
109
110         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         }
116
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
132         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         }
143
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
158         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;
161
162                 if (h->filtered)
163                         continue;
164
165                 gtk_list_store_append(store, &iter);
166
167                 col_idx = 0;
168
169                 for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
170                         if (!perf_hpp__format[i].cond)
171                                 continue;
172
173                         if (perf_hpp__format[i].color)
174                                 perf_hpp__format[i].color(&hpp, h);
175                         else
176                                 perf_hpp__format[i].entry(&hpp, h);
177
178                         gtk_list_store_set(store, &iter, col_idx++, s, -1);
179                 }
180
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
195 #ifdef HAVE_GTK_INFO_BAR
196 static 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
223 static 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
238 int perf_evlist__gtk_browse_hists(struct perf_evlist *evlist,
239                                   const char *help,
240                                   void (*timer) (void *arg)__maybe_unused,
241                                   void *arg __maybe_unused,
242                                   int delay_secs __maybe_unused)
243 {
244         struct perf_evsel *pos;
245         GtkWidget *vbox;
246         GtkWidget *notebook;
247         GtkWidget *info_bar;
248         GtkWidget *statbar;
249         GtkWidget *window;
250
251         signal(SIGSEGV, perf_gtk__signal);
252         signal(SIGFPE,  perf_gtk__signal);
253         signal(SIGINT,  perf_gtk__signal);
254         signal(SIGQUIT, perf_gtk__signal);
255         signal(SIGTERM, perf_gtk__signal);
256
257         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
258
259         gtk_window_set_title(GTK_WINDOW(window), "perf report");
260
261         g_signal_connect(window, "delete_event", gtk_main_quit, NULL);
262
263         pgctx = perf_gtk__activate_context(window);
264         if (!pgctx)
265                 return -1;
266
267         vbox = gtk_vbox_new(FALSE, 0);
268
269         notebook = gtk_notebook_new();
270
271         list_for_each_entry(pos, &evlist->entries, node) {
272                 struct hists *hists = &pos->hists;
273                 const char *evname = perf_evsel__name(pos);
274                 GtkWidget *scrolled_window;
275                 GtkWidget *tab_label;
276
277                 scrolled_window = gtk_scrolled_window_new(NULL, NULL);
278
279                 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
280                                                         GTK_POLICY_AUTOMATIC,
281                                                         GTK_POLICY_AUTOMATIC);
282
283                 perf_gtk__show_hists(scrolled_window, hists);
284
285                 tab_label = gtk_label_new(evname);
286
287                 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scrolled_window, tab_label);
288         }
289
290         gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 0);
291
292         info_bar = perf_gtk__setup_info_bar();
293         if (info_bar)
294                 gtk_box_pack_start(GTK_BOX(vbox), info_bar, FALSE, FALSE, 0);
295
296         statbar = perf_gtk__setup_statusbar();
297         gtk_box_pack_start(GTK_BOX(vbox), statbar, FALSE, FALSE, 0);
298
299         gtk_container_add(GTK_CONTAINER(window), vbox);
300
301         gtk_widget_show_all(window);
302
303         perf_gtk__resize_window(window);
304
305         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
306
307         ui_helpline__push(help);
308
309         gtk_main();
310
311         perf_gtk__deactivate_context(&pgctx);
312
313         return 0;
314 }