Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 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 | #define MAX_COLUMNS 32 |
| 10 | |
Namhyung Kim | a0088ad | 2014-03-03 10:14:04 +0900 | [diff] [blame] | 11 | static int __percent_color_snprintf(struct perf_hpp *hpp, const char *fmt, ...) |
Namhyung Kim | 843985e | 2013-01-22 18:09:36 +0900 | [diff] [blame] | 12 | { |
| 13 | int ret = 0; |
Namhyung Kim | 4a62109 | 2014-03-03 10:14:03 +0900 | [diff] [blame] | 14 | va_list args; |
| 15 | double percent; |
Namhyung Kim | 843985e | 2013-01-22 18:09:36 +0900 | [diff] [blame] | 16 | const char *markup; |
Namhyung Kim | a0088ad | 2014-03-03 10:14:04 +0900 | [diff] [blame] | 17 | char *buf = hpp->buf; |
| 18 | size_t size = hpp->size; |
Namhyung Kim | 843985e | 2013-01-22 18:09:36 +0900 | [diff] [blame] | 19 | |
Namhyung Kim | 4a62109 | 2014-03-03 10:14:03 +0900 | [diff] [blame] | 20 | va_start(args, fmt); |
| 21 | percent = va_arg(args, double); |
| 22 | va_end(args); |
| 23 | |
Namhyung Kim | 843985e | 2013-01-22 18:09:36 +0900 | [diff] [blame] | 24 | markup = perf_gtk__get_percent_color(percent); |
| 25 | if (markup) |
| 26 | ret += scnprintf(buf, size, markup); |
| 27 | |
Namhyung Kim | 4a62109 | 2014-03-03 10:14:03 +0900 | [diff] [blame] | 28 | ret += scnprintf(buf + ret, size - ret, fmt, percent); |
Namhyung Kim | 843985e | 2013-01-22 18:09:36 +0900 | [diff] [blame] | 29 | |
| 30 | if (markup) |
| 31 | ret += scnprintf(buf + ret, size - ret, "</span>"); |
| 32 | |
| 33 | return ret; |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 34 | } |
| 35 | |
Namhyung Kim | 843985e | 2013-01-22 18:09:36 +0900 | [diff] [blame] | 36 | #define __HPP_COLOR_PERCENT_FN(_type, _field) \ |
| 37 | static u64 he_get_##_field(struct hist_entry *he) \ |
| 38 | { \ |
| 39 | return he->stat._field; \ |
| 40 | } \ |
| 41 | \ |
Jiri Olsa | 2c5d4b4 | 2013-01-31 23:31:11 +0100 | [diff] [blame] | 42 | static int perf_gtk__hpp_color_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \ |
| 43 | struct perf_hpp *hpp, \ |
Namhyung Kim | 843985e | 2013-01-22 18:09:36 +0900 | [diff] [blame] | 44 | struct hist_entry *he) \ |
| 45 | { \ |
Namhyung Kim | fb821c9e | 2014-03-03 17:05:19 +0900 | [diff] [blame^] | 46 | return __hpp__fmt(hpp, he, he_get_##_field, " %6.2f%%", \ |
Namhyung Kim | 4a62109 | 2014-03-03 10:14:03 +0900 | [diff] [blame] | 47 | __percent_color_snprintf, true); \ |
Namhyung Kim | 843985e | 2013-01-22 18:09:36 +0900 | [diff] [blame] | 48 | } |
| 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 Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 57 | |
| 58 | |
| 59 | void perf_gtk__init_hpp(void) |
| 60 | { |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 61 | 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 Kim | 2bbc587 | 2013-06-04 18:22:13 +0900 | [diff] [blame] | 75 | static 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 | |
| 84 | static void perf_gtk__add_callchain(struct rb_root *root, GtkTreeStore *store, |
Namhyung Kim | cc60f24 | 2013-06-04 18:22:14 +0900 | [diff] [blame] | 85 | GtkTreeIter *parent, int col, u64 total) |
Namhyung Kim | 2bbc587 | 2013-06-04 18:22:13 +0900 | [diff] [blame] | 86 | { |
| 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 Kim | cc60f24 | 2013-06-04 18:22:14 +0900 | [diff] [blame] | 95 | double percent; |
| 96 | u64 hits, child_total; |
Namhyung Kim | 2bbc587 | 2013-06-04 18:22:13 +0900 | [diff] [blame] | 97 | |
| 98 | node = rb_entry(nd, struct callchain_node, rb_node); |
| 99 | |
Namhyung Kim | cc60f24 | 2013-06-04 18:22:14 +0900 | [diff] [blame] | 100 | hits = callchain_cumul_hits(node); |
| 101 | percent = 100.0 * hits / total; |
| 102 | |
Namhyung Kim | 2bbc587 | 2013-06-04 18:22:13 +0900 | [diff] [blame] | 103 | 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 Kim | cc60f24 | 2013-06-04 18:22:14 +0900 | [diff] [blame] | 111 | scnprintf(buf, sizeof(buf), "%5.2f%%", percent); |
| 112 | gtk_tree_store_set(store, &iter, 0, buf, -1); |
| 113 | |
Namhyung Kim | 2bbc587 | 2013-06-04 18:22:13 +0900 | [diff] [blame] | 114 | 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 Kim | cc60f24 | 2013-06-04 18:22:14 +0900 | [diff] [blame] | 127 | if (callchain_param.mode == CHAIN_GRAPH_REL) |
| 128 | child_total = node->children_hit; |
| 129 | else |
| 130 | child_total = total; |
| 131 | |
Namhyung Kim | 2bbc587 | 2013-06-04 18:22:13 +0900 | [diff] [blame] | 132 | /* Now 'iter' contains info of the last callchain_list */ |
Namhyung Kim | cc60f24 | 2013-06-04 18:22:14 +0900 | [diff] [blame] | 133 | perf_gtk__add_callchain(&node->rb_root, store, &iter, col, |
| 134 | child_total); |
Namhyung Kim | 2bbc587 | 2013-06-04 18:22:13 +0900 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Namhyung Kim | 450f390 | 2013-06-04 18:22:16 +0900 | [diff] [blame] | 138 | static 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 Kim | 064f198 | 2013-05-14 11:09:04 +0900 | [diff] [blame] | 150 | static void perf_gtk__show_hists(GtkWidget *window, struct hists *hists, |
| 151 | float min_pcnt) |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 152 | { |
| 153 | struct perf_hpp_fmt *fmt; |
| 154 | GType col_types[MAX_COLUMNS]; |
| 155 | GtkCellRenderer *renderer; |
Namhyung Kim | f1d9a53 | 2013-06-04 18:22:12 +0900 | [diff] [blame] | 156 | GtkTreeStore *store; |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 157 | struct rb_node *nd; |
| 158 | GtkWidget *view; |
| 159 | int col_idx; |
Namhyung Kim | 2bbc587 | 2013-06-04 18:22:13 +0900 | [diff] [blame] | 160 | int sym_col = -1; |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 161 | 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 Kim | f1d9a53 | 2013-06-04 18:22:12 +0900 | [diff] [blame] | 174 | store = gtk_tree_store_newv(nr_cols, col_types); |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 175 | |
| 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 Kim | 94a0793 | 2014-03-10 16:43:52 +0900 | [diff] [blame] | 183 | fmt->header(fmt, &hpp, hists_to_evsel(hists)); |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 184 | |
| 185 | gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), |
Namhyung Kim | 34b9564 | 2013-01-22 18:09:42 +0900 | [diff] [blame] | 186 | -1, ltrim(s), |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 187 | renderer, "markup", |
| 188 | col_idx++, NULL); |
| 189 | } |
| 190 | |
Namhyung Kim | 1a30942 | 2013-06-04 18:22:15 +0900 | [diff] [blame] | 191 | for (col_idx = 0; col_idx < nr_cols; col_idx++) { |
Namhyung Kim | 2bbc587 | 2013-06-04 18:22:13 +0900 | [diff] [blame] | 192 | GtkTreeViewColumn *column; |
| 193 | |
Namhyung Kim | 1a30942 | 2013-06-04 18:22:15 +0900 | [diff] [blame] | 194 | 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 Kim | 2bbc587 | 2013-06-04 18:22:13 +0900 | [diff] [blame] | 201 | } |
| 202 | |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 203 | 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 Kim | f214833 | 2014-01-14 11:52:48 +0900 | [diff] [blame] | 210 | u64 total = hists__total_period(h->hists); |
| 211 | float percent = 0.0; |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 212 | |
| 213 | if (h->filtered) |
| 214 | continue; |
| 215 | |
Namhyung Kim | f214833 | 2014-01-14 11:52:48 +0900 | [diff] [blame] | 216 | if (total) |
| 217 | percent = h->stat.period * 100.0 / total; |
| 218 | |
Namhyung Kim | 064f198 | 2013-05-14 11:09:04 +0900 | [diff] [blame] | 219 | if (percent < min_pcnt) |
| 220 | continue; |
| 221 | |
Namhyung Kim | f1d9a53 | 2013-06-04 18:22:12 +0900 | [diff] [blame] | 222 | gtk_tree_store_append(store, &iter, NULL); |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 223 | |
| 224 | col_idx = 0; |
| 225 | |
| 226 | perf_hpp__for_each_format(fmt) { |
| 227 | if (fmt->color) |
Jiri Olsa | 2c5d4b4 | 2013-01-31 23:31:11 +0100 | [diff] [blame] | 228 | fmt->color(fmt, &hpp, h); |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 229 | else |
Jiri Olsa | 2c5d4b4 | 2013-01-31 23:31:11 +0100 | [diff] [blame] | 230 | fmt->entry(fmt, &hpp, h); |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 231 | |
Namhyung Kim | f1d9a53 | 2013-06-04 18:22:12 +0900 | [diff] [blame] | 232 | gtk_tree_store_set(store, &iter, col_idx++, s, -1); |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 233 | } |
| 234 | |
Namhyung Kim | 2bbc587 | 2013-06-04 18:22:13 +0900 | [diff] [blame] | 235 | if (symbol_conf.use_callchain && sort__has_sym) { |
Namhyung Kim | cc60f24 | 2013-06-04 18:22:14 +0900 | [diff] [blame] | 236 | if (callchain_param.mode == CHAIN_GRAPH_REL) |
| 237 | total = h->stat.period; |
Namhyung Kim | cc60f24 | 2013-06-04 18:22:14 +0900 | [diff] [blame] | 238 | |
Namhyung Kim | 2bbc587 | 2013-06-04 18:22:13 +0900 | [diff] [blame] | 239 | perf_gtk__add_callchain(&h->sorted_chain, store, &iter, |
Namhyung Kim | cc60f24 | 2013-06-04 18:22:14 +0900 | [diff] [blame] | 240 | sym_col, total); |
Namhyung Kim | 2bbc587 | 2013-06-04 18:22:13 +0900 | [diff] [blame] | 241 | } |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 242 | } |
| 243 | |
Namhyung Kim | 9d58d2f | 2013-06-04 18:22:17 +0900 | [diff] [blame] | 244 | gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(view), TRUE); |
| 245 | |
Namhyung Kim | 450f390 | 2013-06-04 18:22:16 +0900 | [diff] [blame] | 246 | g_signal_connect(view, "row-activated", |
| 247 | G_CALLBACK(on_row_activated), NULL); |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 248 | gtk_container_add(GTK_CONTAINER(window), view); |
| 249 | } |
| 250 | |
| 251 | int perf_evlist__gtk_browse_hists(struct perf_evlist *evlist, |
| 252 | const char *help, |
Namhyung Kim | 064f198 | 2013-05-14 11:09:04 +0900 | [diff] [blame] | 253 | struct hist_browser_timer *hbt __maybe_unused, |
| 254 | float min_pcnt) |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 255 | { |
| 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 Kim | 6bf1a29 | 2012-12-21 17:20:14 +0900 | [diff] [blame] | 283 | 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 Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 294 | evlist__for_each(evlist, pos) { |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 295 | struct hists *hists = &pos->hists; |
| 296 | const char *evname = perf_evsel__name(pos); |
| 297 | GtkWidget *scrolled_window; |
| 298 | GtkWidget *tab_label; |
Namhyung Kim | 717e263 | 2013-01-22 18:09:44 +0900 | [diff] [blame] | 299 | char buf[512]; |
| 300 | size_t size = sizeof(buf); |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 301 | |
Namhyung Kim | 717e263 | 2013-01-22 18:09:44 +0900 | [diff] [blame] | 302 | 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 Kim | fc24d7c | 2013-01-22 18:09:43 +0900 | [diff] [blame] | 311 | |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 312 | 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 Kim | 064f198 | 2013-05-14 11:09:04 +0900 | [diff] [blame] | 318 | perf_gtk__show_hists(scrolled_window, hists, min_pcnt); |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 319 | |
| 320 | tab_label = gtk_label_new(evname); |
| 321 | |
| 322 | gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scrolled_window, tab_label); |
| 323 | } |
| 324 | |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 325 | 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 | } |