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 | cb16008 | 2013-01-22 18:09:40 +0900 | [diff] [blame] | 11 | static int __percent_color_snprintf(char *buf, size_t size, double percent) |
Namhyung Kim | 843985e | 2013-01-22 18:09:36 +0900 | [diff] [blame] | 12 | { |
| 13 | int ret = 0; |
| 14 | const char *markup; |
| 15 | |
| 16 | markup = perf_gtk__get_percent_color(percent); |
| 17 | if (markup) |
| 18 | ret += scnprintf(buf, size, markup); |
| 19 | |
Namhyung Kim | cb16008 | 2013-01-22 18:09:40 +0900 | [diff] [blame] | 20 | ret += scnprintf(buf + ret, size - ret, " %6.2f%%", percent); |
Namhyung Kim | 843985e | 2013-01-22 18:09:36 +0900 | [diff] [blame] | 21 | |
| 22 | if (markup) |
| 23 | ret += scnprintf(buf + ret, size - ret, "</span>"); |
| 24 | |
| 25 | return ret; |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 26 | } |
| 27 | |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 28 | |
Namhyung Kim | 843985e | 2013-01-22 18:09:36 +0900 | [diff] [blame] | 29 | static int __hpp__color_fmt(struct perf_hpp *hpp, struct hist_entry *he, |
| 30 | u64 (*get_field)(struct hist_entry *)) |
| 31 | { |
| 32 | int ret; |
| 33 | double percent = 0.0; |
| 34 | struct hists *hists = he->hists; |
Namhyung Kim | 759ff49 | 2013-03-05 14:53:26 +0900 | [diff] [blame] | 35 | struct perf_evsel *evsel = hists_to_evsel(hists); |
Namhyung Kim | 843985e | 2013-01-22 18:09:36 +0900 | [diff] [blame] | 36 | |
| 37 | if (hists->stats.total_period) |
| 38 | percent = 100.0 * get_field(he) / hists->stats.total_period; |
| 39 | |
Namhyung Kim | cb16008 | 2013-01-22 18:09:40 +0900 | [diff] [blame] | 40 | ret = __percent_color_snprintf(hpp->buf, hpp->size, percent); |
| 41 | |
Namhyung Kim | 759ff49 | 2013-03-05 14:53:26 +0900 | [diff] [blame] | 42 | if (perf_evsel__is_group_event(evsel)) { |
Namhyung Kim | cb16008 | 2013-01-22 18:09:40 +0900 | [diff] [blame] | 43 | int prev_idx, idx_delta; |
Namhyung Kim | cb16008 | 2013-01-22 18:09:40 +0900 | [diff] [blame] | 44 | struct hist_entry *pair; |
| 45 | int nr_members = evsel->nr_members; |
| 46 | |
Namhyung Kim | cb16008 | 2013-01-22 18:09:40 +0900 | [diff] [blame] | 47 | prev_idx = perf_evsel__group_idx(evsel); |
| 48 | |
| 49 | list_for_each_entry(pair, &he->pairs.head, pairs.node) { |
| 50 | u64 period = get_field(pair); |
| 51 | u64 total = pair->hists->stats.total_period; |
| 52 | |
| 53 | evsel = hists_to_evsel(pair->hists); |
| 54 | idx_delta = perf_evsel__group_idx(evsel) - prev_idx - 1; |
| 55 | |
| 56 | while (idx_delta--) { |
| 57 | /* |
| 58 | * zero-fill group members in the middle which |
| 59 | * have no sample |
| 60 | */ |
| 61 | ret += __percent_color_snprintf(hpp->buf + ret, |
| 62 | hpp->size - ret, |
| 63 | 0.0); |
| 64 | } |
| 65 | |
| 66 | percent = 100.0 * period / total; |
| 67 | ret += __percent_color_snprintf(hpp->buf + ret, |
| 68 | hpp->size - ret, |
| 69 | percent); |
| 70 | |
| 71 | prev_idx = perf_evsel__group_idx(evsel); |
| 72 | } |
| 73 | |
| 74 | idx_delta = nr_members - prev_idx - 1; |
| 75 | |
| 76 | while (idx_delta--) { |
| 77 | /* |
| 78 | * zero-fill group members at last which have no sample |
| 79 | */ |
| 80 | ret += __percent_color_snprintf(hpp->buf + ret, |
| 81 | hpp->size - ret, |
| 82 | 0.0); |
| 83 | } |
| 84 | } |
Namhyung Kim | 843985e | 2013-01-22 18:09:36 +0900 | [diff] [blame] | 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | #define __HPP_COLOR_PERCENT_FN(_type, _field) \ |
| 89 | static u64 he_get_##_field(struct hist_entry *he) \ |
| 90 | { \ |
| 91 | return he->stat._field; \ |
| 92 | } \ |
| 93 | \ |
| 94 | static int perf_gtk__hpp_color_##_type(struct perf_hpp *hpp, \ |
| 95 | struct hist_entry *he) \ |
| 96 | { \ |
| 97 | return __hpp__color_fmt(hpp, he, he_get_##_field); \ |
| 98 | } |
| 99 | |
| 100 | __HPP_COLOR_PERCENT_FN(overhead, period) |
| 101 | __HPP_COLOR_PERCENT_FN(overhead_sys, period_sys) |
| 102 | __HPP_COLOR_PERCENT_FN(overhead_us, period_us) |
| 103 | __HPP_COLOR_PERCENT_FN(overhead_guest_sys, period_guest_sys) |
| 104 | __HPP_COLOR_PERCENT_FN(overhead_guest_us, period_guest_us) |
| 105 | |
| 106 | #undef __HPP_COLOR_PERCENT_FN |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 107 | |
| 108 | |
| 109 | void perf_gtk__init_hpp(void) |
| 110 | { |
| 111 | perf_hpp__column_enable(PERF_HPP__OVERHEAD); |
| 112 | |
| 113 | perf_hpp__init(); |
| 114 | |
| 115 | perf_hpp__format[PERF_HPP__OVERHEAD].color = |
| 116 | perf_gtk__hpp_color_overhead; |
| 117 | perf_hpp__format[PERF_HPP__OVERHEAD_SYS].color = |
| 118 | perf_gtk__hpp_color_overhead_sys; |
| 119 | perf_hpp__format[PERF_HPP__OVERHEAD_US].color = |
| 120 | perf_gtk__hpp_color_overhead_us; |
| 121 | perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_SYS].color = |
| 122 | perf_gtk__hpp_color_overhead_guest_sys; |
| 123 | perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_US].color = |
| 124 | perf_gtk__hpp_color_overhead_guest_us; |
| 125 | } |
| 126 | |
Namhyung Kim | 2bbc587 | 2013-06-04 18:22:13 +0900 | [diff] [blame^] | 127 | static void callchain_list__sym_name(struct callchain_list *cl, |
| 128 | char *bf, size_t bfsize) |
| 129 | { |
| 130 | if (cl->ms.sym) |
| 131 | scnprintf(bf, bfsize, "%s", cl->ms.sym->name); |
| 132 | else |
| 133 | scnprintf(bf, bfsize, "%#" PRIx64, cl->ip); |
| 134 | } |
| 135 | |
| 136 | static void perf_gtk__add_callchain(struct rb_root *root, GtkTreeStore *store, |
| 137 | GtkTreeIter *parent, int col) |
| 138 | { |
| 139 | struct rb_node *nd; |
| 140 | bool has_single_node = (rb_first(root) == rb_last(root)); |
| 141 | |
| 142 | for (nd = rb_first(root); nd; nd = rb_next(nd)) { |
| 143 | struct callchain_node *node; |
| 144 | struct callchain_list *chain; |
| 145 | GtkTreeIter iter, new_parent; |
| 146 | bool need_new_parent; |
| 147 | |
| 148 | node = rb_entry(nd, struct callchain_node, rb_node); |
| 149 | |
| 150 | new_parent = *parent; |
| 151 | need_new_parent = !has_single_node && (node->val_nr > 1); |
| 152 | |
| 153 | list_for_each_entry(chain, &node->val, list) { |
| 154 | char buf[128]; |
| 155 | |
| 156 | gtk_tree_store_append(store, &iter, &new_parent); |
| 157 | |
| 158 | callchain_list__sym_name(chain, buf, sizeof(buf)); |
| 159 | gtk_tree_store_set(store, &iter, col, buf, -1); |
| 160 | |
| 161 | if (need_new_parent) { |
| 162 | /* |
| 163 | * Only show the top-most symbol in a callchain |
| 164 | * if it's not the only callchain. |
| 165 | */ |
| 166 | new_parent = iter; |
| 167 | need_new_parent = false; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /* Now 'iter' contains info of the last callchain_list */ |
| 172 | perf_gtk__add_callchain(&node->rb_root, store, &iter, col); |
| 173 | } |
| 174 | } |
| 175 | |
Namhyung Kim | 064f198 | 2013-05-14 11:09:04 +0900 | [diff] [blame] | 176 | static void perf_gtk__show_hists(GtkWidget *window, struct hists *hists, |
| 177 | float min_pcnt) |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 178 | { |
| 179 | struct perf_hpp_fmt *fmt; |
| 180 | GType col_types[MAX_COLUMNS]; |
| 181 | GtkCellRenderer *renderer; |
| 182 | struct sort_entry *se; |
Namhyung Kim | f1d9a53 | 2013-06-04 18:22:12 +0900 | [diff] [blame] | 183 | GtkTreeStore *store; |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 184 | struct rb_node *nd; |
| 185 | GtkWidget *view; |
| 186 | int col_idx; |
Namhyung Kim | 2bbc587 | 2013-06-04 18:22:13 +0900 | [diff] [blame^] | 187 | int sym_col = -1; |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 188 | int nr_cols; |
| 189 | char s[512]; |
| 190 | |
| 191 | struct perf_hpp hpp = { |
| 192 | .buf = s, |
| 193 | .size = sizeof(s), |
Namhyung Kim | cb16008 | 2013-01-22 18:09:40 +0900 | [diff] [blame] | 194 | .ptr = hists_to_evsel(hists), |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | nr_cols = 0; |
| 198 | |
| 199 | perf_hpp__for_each_format(fmt) |
| 200 | col_types[nr_cols++] = G_TYPE_STRING; |
| 201 | |
| 202 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 203 | if (se->elide) |
| 204 | continue; |
| 205 | |
Namhyung Kim | 2bbc587 | 2013-06-04 18:22:13 +0900 | [diff] [blame^] | 206 | if (se == &sort_sym) |
| 207 | sym_col = nr_cols; |
| 208 | |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 209 | col_types[nr_cols++] = G_TYPE_STRING; |
| 210 | } |
| 211 | |
Namhyung Kim | f1d9a53 | 2013-06-04 18:22:12 +0900 | [diff] [blame] | 212 | store = gtk_tree_store_newv(nr_cols, col_types); |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 213 | |
| 214 | view = gtk_tree_view_new(); |
| 215 | |
| 216 | renderer = gtk_cell_renderer_text_new(); |
| 217 | |
| 218 | col_idx = 0; |
| 219 | |
| 220 | perf_hpp__for_each_format(fmt) { |
| 221 | fmt->header(&hpp); |
| 222 | |
| 223 | gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), |
Namhyung Kim | 34b9564 | 2013-01-22 18:09:42 +0900 | [diff] [blame] | 224 | -1, ltrim(s), |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 225 | renderer, "markup", |
| 226 | col_idx++, NULL); |
| 227 | } |
| 228 | |
| 229 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 230 | if (se->elide) |
| 231 | continue; |
| 232 | |
| 233 | gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), |
| 234 | -1, se->se_header, |
| 235 | renderer, "text", |
| 236 | col_idx++, NULL); |
| 237 | } |
| 238 | |
Namhyung Kim | 2bbc587 | 2013-06-04 18:22:13 +0900 | [diff] [blame^] | 239 | if (symbol_conf.use_callchain && sort__has_sym) { |
| 240 | GtkTreeViewColumn *column; |
| 241 | |
| 242 | column = gtk_tree_view_get_column(GTK_TREE_VIEW(view), sym_col); |
| 243 | gtk_tree_view_set_expander_column(GTK_TREE_VIEW(view), column); |
| 244 | } |
| 245 | |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 246 | gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store)); |
| 247 | |
| 248 | g_object_unref(GTK_TREE_MODEL(store)); |
| 249 | |
| 250 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { |
| 251 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 252 | GtkTreeIter iter; |
Namhyung Kim | 064f198 | 2013-05-14 11:09:04 +0900 | [diff] [blame] | 253 | float percent = h->stat.period * 100.0 / |
| 254 | hists->stats.total_period; |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 255 | |
| 256 | if (h->filtered) |
| 257 | continue; |
| 258 | |
Namhyung Kim | 064f198 | 2013-05-14 11:09:04 +0900 | [diff] [blame] | 259 | if (percent < min_pcnt) |
| 260 | continue; |
| 261 | |
Namhyung Kim | f1d9a53 | 2013-06-04 18:22:12 +0900 | [diff] [blame] | 262 | gtk_tree_store_append(store, &iter, NULL); |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 263 | |
| 264 | col_idx = 0; |
| 265 | |
| 266 | perf_hpp__for_each_format(fmt) { |
| 267 | if (fmt->color) |
| 268 | fmt->color(&hpp, h); |
| 269 | else |
| 270 | fmt->entry(&hpp, h); |
| 271 | |
Namhyung Kim | f1d9a53 | 2013-06-04 18:22:12 +0900 | [diff] [blame] | 272 | gtk_tree_store_set(store, &iter, col_idx++, s, -1); |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 276 | if (se->elide) |
| 277 | continue; |
| 278 | |
| 279 | se->se_snprintf(h, s, ARRAY_SIZE(s), |
| 280 | hists__col_len(hists, se->se_width_idx)); |
| 281 | |
Namhyung Kim | f1d9a53 | 2013-06-04 18:22:12 +0900 | [diff] [blame] | 282 | gtk_tree_store_set(store, &iter, col_idx++, s, -1); |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 283 | } |
Namhyung Kim | 2bbc587 | 2013-06-04 18:22:13 +0900 | [diff] [blame^] | 284 | |
| 285 | if (symbol_conf.use_callchain && sort__has_sym) { |
| 286 | perf_gtk__add_callchain(&h->sorted_chain, store, &iter, |
| 287 | sym_col); |
| 288 | } |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | gtk_container_add(GTK_CONTAINER(window), view); |
| 292 | } |
| 293 | |
| 294 | int perf_evlist__gtk_browse_hists(struct perf_evlist *evlist, |
| 295 | const char *help, |
Namhyung Kim | 064f198 | 2013-05-14 11:09:04 +0900 | [diff] [blame] | 296 | struct hist_browser_timer *hbt __maybe_unused, |
| 297 | float min_pcnt) |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 298 | { |
| 299 | struct perf_evsel *pos; |
| 300 | GtkWidget *vbox; |
| 301 | GtkWidget *notebook; |
| 302 | GtkWidget *info_bar; |
| 303 | GtkWidget *statbar; |
| 304 | GtkWidget *window; |
| 305 | |
| 306 | signal(SIGSEGV, perf_gtk__signal); |
| 307 | signal(SIGFPE, perf_gtk__signal); |
| 308 | signal(SIGINT, perf_gtk__signal); |
| 309 | signal(SIGQUIT, perf_gtk__signal); |
| 310 | signal(SIGTERM, perf_gtk__signal); |
| 311 | |
| 312 | window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
| 313 | |
| 314 | gtk_window_set_title(GTK_WINDOW(window), "perf report"); |
| 315 | |
| 316 | g_signal_connect(window, "delete_event", gtk_main_quit, NULL); |
| 317 | |
| 318 | pgctx = perf_gtk__activate_context(window); |
| 319 | if (!pgctx) |
| 320 | return -1; |
| 321 | |
| 322 | vbox = gtk_vbox_new(FALSE, 0); |
| 323 | |
| 324 | notebook = gtk_notebook_new(); |
| 325 | |
Namhyung Kim | 6bf1a29 | 2012-12-21 17:20:14 +0900 | [diff] [blame] | 326 | gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 0); |
| 327 | |
| 328 | info_bar = perf_gtk__setup_info_bar(); |
| 329 | if (info_bar) |
| 330 | gtk_box_pack_start(GTK_BOX(vbox), info_bar, FALSE, FALSE, 0); |
| 331 | |
| 332 | statbar = perf_gtk__setup_statusbar(); |
| 333 | gtk_box_pack_start(GTK_BOX(vbox), statbar, FALSE, FALSE, 0); |
| 334 | |
| 335 | gtk_container_add(GTK_CONTAINER(window), vbox); |
| 336 | |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 337 | list_for_each_entry(pos, &evlist->entries, node) { |
| 338 | struct hists *hists = &pos->hists; |
| 339 | const char *evname = perf_evsel__name(pos); |
| 340 | GtkWidget *scrolled_window; |
| 341 | GtkWidget *tab_label; |
Namhyung Kim | 717e263 | 2013-01-22 18:09:44 +0900 | [diff] [blame] | 342 | char buf[512]; |
| 343 | size_t size = sizeof(buf); |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 344 | |
Namhyung Kim | 717e263 | 2013-01-22 18:09:44 +0900 | [diff] [blame] | 345 | if (symbol_conf.event_group) { |
| 346 | if (!perf_evsel__is_group_leader(pos)) |
| 347 | continue; |
| 348 | |
| 349 | if (pos->nr_members > 1) { |
| 350 | perf_evsel__group_desc(pos, buf, size); |
| 351 | evname = buf; |
| 352 | } |
| 353 | } |
Namhyung Kim | fc24d7c | 2013-01-22 18:09:43 +0900 | [diff] [blame] | 354 | |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 355 | scrolled_window = gtk_scrolled_window_new(NULL, NULL); |
| 356 | |
| 357 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), |
| 358 | GTK_POLICY_AUTOMATIC, |
| 359 | GTK_POLICY_AUTOMATIC); |
| 360 | |
Namhyung Kim | 064f198 | 2013-05-14 11:09:04 +0900 | [diff] [blame] | 361 | perf_gtk__show_hists(scrolled_window, hists, min_pcnt); |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 362 | |
| 363 | tab_label = gtk_label_new(evname); |
| 364 | |
| 365 | gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scrolled_window, tab_label); |
| 366 | } |
| 367 | |
Namhyung Kim | 0da41ce9 | 2012-12-21 17:20:13 +0900 | [diff] [blame] | 368 | gtk_widget_show_all(window); |
| 369 | |
| 370 | perf_gtk__resize_window(window); |
| 371 | |
| 372 | gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); |
| 373 | |
| 374 | ui_helpline__push(help); |
| 375 | |
| 376 | gtk_main(); |
| 377 | |
| 378 | perf_gtk__deactivate_context(&pgctx); |
| 379 | |
| 380 | return 0; |
| 381 | } |