Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 1 | #define _GNU_SOURCE |
| 2 | #include <stdio.h> |
| 3 | #undef _GNU_SOURCE |
| 4 | #include "../libslang.h" |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include <newt.h> |
| 8 | #include <linux/rbtree.h> |
| 9 | |
| 10 | #include "../../hist.h" |
| 11 | #include "../../pstack.h" |
| 12 | #include "../../sort.h" |
| 13 | #include "../../util.h" |
| 14 | |
| 15 | #include "../browser.h" |
| 16 | #include "../helpline.h" |
| 17 | #include "../util.h" |
| 18 | #include "map.h" |
| 19 | |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 20 | struct hist_browser { |
| 21 | struct ui_browser b; |
| 22 | struct hists *hists; |
| 23 | struct hist_entry *he_selection; |
| 24 | struct map_symbol *selection; |
| 25 | }; |
| 26 | |
| 27 | static void hist_browser__refresh_dimensions(struct hist_browser *self) |
| 28 | { |
| 29 | /* 3 == +/- toggle symbol before actual hist_entry rendering */ |
| 30 | self->b.width = 3 + (hists__sort_list_width(self->hists) + |
| 31 | sizeof("[k]")); |
| 32 | } |
| 33 | |
| 34 | static void hist_browser__reset(struct hist_browser *self) |
| 35 | { |
| 36 | self->b.nr_entries = self->hists->nr_entries; |
| 37 | hist_browser__refresh_dimensions(self); |
| 38 | ui_browser__reset_index(&self->b); |
| 39 | } |
| 40 | |
| 41 | static char tree__folded_sign(bool unfolded) |
| 42 | { |
| 43 | return unfolded ? '-' : '+'; |
| 44 | } |
| 45 | |
| 46 | static char map_symbol__folded(const struct map_symbol *self) |
| 47 | { |
| 48 | return self->has_children ? tree__folded_sign(self->unfolded) : ' '; |
| 49 | } |
| 50 | |
| 51 | static char hist_entry__folded(const struct hist_entry *self) |
| 52 | { |
| 53 | return map_symbol__folded(&self->ms); |
| 54 | } |
| 55 | |
| 56 | static char callchain_list__folded(const struct callchain_list *self) |
| 57 | { |
| 58 | return map_symbol__folded(&self->ms); |
| 59 | } |
| 60 | |
| 61 | static int callchain_node__count_rows_rb_tree(struct callchain_node *self) |
| 62 | { |
| 63 | int n = 0; |
| 64 | struct rb_node *nd; |
| 65 | |
| 66 | for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) { |
| 67 | struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node); |
| 68 | struct callchain_list *chain; |
| 69 | char folded_sign = ' '; /* No children */ |
| 70 | |
| 71 | list_for_each_entry(chain, &child->val, list) { |
| 72 | ++n; |
| 73 | /* We need this because we may not have children */ |
| 74 | folded_sign = callchain_list__folded(chain); |
| 75 | if (folded_sign == '+') |
| 76 | break; |
| 77 | } |
| 78 | |
| 79 | if (folded_sign == '-') /* Have children and they're unfolded */ |
| 80 | n += callchain_node__count_rows_rb_tree(child); |
| 81 | } |
| 82 | |
| 83 | return n; |
| 84 | } |
| 85 | |
| 86 | static int callchain_node__count_rows(struct callchain_node *node) |
| 87 | { |
| 88 | struct callchain_list *chain; |
| 89 | bool unfolded = false; |
| 90 | int n = 0; |
| 91 | |
| 92 | list_for_each_entry(chain, &node->val, list) { |
| 93 | ++n; |
| 94 | unfolded = chain->ms.unfolded; |
| 95 | } |
| 96 | |
| 97 | if (unfolded) |
| 98 | n += callchain_node__count_rows_rb_tree(node); |
| 99 | |
| 100 | return n; |
| 101 | } |
| 102 | |
| 103 | static int callchain__count_rows(struct rb_root *chain) |
| 104 | { |
| 105 | struct rb_node *nd; |
| 106 | int n = 0; |
| 107 | |
| 108 | for (nd = rb_first(chain); nd; nd = rb_next(nd)) { |
| 109 | struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node); |
| 110 | n += callchain_node__count_rows(node); |
| 111 | } |
| 112 | |
| 113 | return n; |
| 114 | } |
| 115 | |
| 116 | static bool map_symbol__toggle_fold(struct map_symbol *self) |
| 117 | { |
| 118 | if (!self->has_children) |
| 119 | return false; |
| 120 | |
| 121 | self->unfolded = !self->unfolded; |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | static void callchain_node__init_have_children_rb_tree(struct callchain_node *self) |
| 126 | { |
| 127 | struct rb_node *nd = rb_first(&self->rb_root); |
| 128 | |
| 129 | for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) { |
| 130 | struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node); |
| 131 | struct callchain_list *chain; |
| 132 | int first = true; |
| 133 | |
| 134 | list_for_each_entry(chain, &child->val, list) { |
| 135 | if (first) { |
| 136 | first = false; |
| 137 | chain->ms.has_children = chain->list.next != &child->val || |
| 138 | rb_first(&child->rb_root) != NULL; |
| 139 | } else |
| 140 | chain->ms.has_children = chain->list.next == &child->val && |
| 141 | rb_first(&child->rb_root) != NULL; |
| 142 | } |
| 143 | |
| 144 | callchain_node__init_have_children_rb_tree(child); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | static void callchain_node__init_have_children(struct callchain_node *self) |
| 149 | { |
| 150 | struct callchain_list *chain; |
| 151 | |
| 152 | list_for_each_entry(chain, &self->val, list) |
| 153 | chain->ms.has_children = rb_first(&self->rb_root) != NULL; |
| 154 | |
| 155 | callchain_node__init_have_children_rb_tree(self); |
| 156 | } |
| 157 | |
| 158 | static void callchain__init_have_children(struct rb_root *self) |
| 159 | { |
| 160 | struct rb_node *nd; |
| 161 | |
| 162 | for (nd = rb_first(self); nd; nd = rb_next(nd)) { |
| 163 | struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node); |
| 164 | callchain_node__init_have_children(node); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | static void hist_entry__init_have_children(struct hist_entry *self) |
| 169 | { |
| 170 | if (!self->init_have_children) { |
| 171 | callchain__init_have_children(&self->sorted_chain); |
| 172 | self->init_have_children = true; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | static bool hist_browser__toggle_fold(struct hist_browser *self) |
| 177 | { |
| 178 | if (map_symbol__toggle_fold(self->selection)) { |
| 179 | struct hist_entry *he = self->he_selection; |
| 180 | |
| 181 | hist_entry__init_have_children(he); |
| 182 | self->hists->nr_entries -= he->nr_rows; |
| 183 | |
| 184 | if (he->ms.unfolded) |
| 185 | he->nr_rows = callchain__count_rows(&he->sorted_chain); |
| 186 | else |
| 187 | he->nr_rows = 0; |
| 188 | self->hists->nr_entries += he->nr_rows; |
| 189 | self->b.nr_entries = self->hists->nr_entries; |
| 190 | |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | /* If it doesn't have children, no toggling performed */ |
| 195 | return false; |
| 196 | } |
| 197 | |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame^] | 198 | static int hist_browser__run(struct hist_browser *self, const char *title) |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 199 | { |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame^] | 200 | int key; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 201 | char str[256], unit; |
| 202 | unsigned long nr_events = self->hists->stats.nr_events[PERF_RECORD_SAMPLE]; |
| 203 | |
| 204 | self->b.entries = &self->hists->entries; |
| 205 | self->b.nr_entries = self->hists->nr_entries; |
| 206 | |
| 207 | hist_browser__refresh_dimensions(self); |
| 208 | |
| 209 | nr_events = convert_unit(nr_events, &unit); |
| 210 | snprintf(str, sizeof(str), "Events: %lu%c ", |
| 211 | nr_events, unit); |
| 212 | newtDrawRootText(0, 0, str); |
| 213 | |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 214 | if (ui_browser__show(&self->b, title, |
| 215 | "Press '?' for help on key bindings") < 0) |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 216 | return -1; |
| 217 | |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 218 | newtFormAddHotKey(self->b.form, 'a'); |
| 219 | newtFormAddHotKey(self->b.form, '?'); |
| 220 | newtFormAddHotKey(self->b.form, 'h'); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 221 | newtFormAddHotKey(self->b.form, 'd'); |
Arnaldo Carvalho de Melo | 4694153 | 2010-08-10 15:50:07 -0300 | [diff] [blame] | 222 | newtFormAddHotKey(self->b.form, 'D'); |
| 223 | newtFormAddHotKey(self->b.form, 't'); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 224 | |
| 225 | newtFormAddHotKey(self->b.form, NEWT_KEY_LEFT); |
| 226 | newtFormAddHotKey(self->b.form, NEWT_KEY_RIGHT); |
| 227 | newtFormAddHotKey(self->b.form, NEWT_KEY_ENTER); |
| 228 | |
| 229 | while (1) { |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame^] | 230 | key = ui_browser__run(&self->b); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 231 | |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame^] | 232 | switch (key) { |
Arnaldo Carvalho de Melo | 4694153 | 2010-08-10 15:50:07 -0300 | [diff] [blame] | 233 | case 'D': { /* Debug */ |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 234 | static int seq; |
| 235 | struct hist_entry *h = rb_entry(self->b.top, |
| 236 | struct hist_entry, rb_node); |
| 237 | ui_helpline__pop(); |
| 238 | ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d", |
| 239 | seq++, self->b.nr_entries, |
| 240 | self->hists->nr_entries, |
| 241 | self->b.height, |
| 242 | self->b.index, |
| 243 | self->b.top_idx, |
| 244 | h->row_offset, h->nr_rows); |
| 245 | } |
| 246 | continue; |
| 247 | case NEWT_KEY_ENTER: |
| 248 | if (hist_browser__toggle_fold(self)) |
| 249 | break; |
| 250 | /* fall thru */ |
| 251 | default: |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame^] | 252 | goto out; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 253 | } |
| 254 | } |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame^] | 255 | out: |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 256 | ui_browser__hide(&self->b); |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame^] | 257 | return key; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | static char *callchain_list__sym_name(struct callchain_list *self, |
| 261 | char *bf, size_t bfsize) |
| 262 | { |
| 263 | if (self->ms.sym) |
| 264 | return self->ms.sym->name; |
| 265 | |
| 266 | snprintf(bf, bfsize, "%#Lx", self->ip); |
| 267 | return bf; |
| 268 | } |
| 269 | |
| 270 | #define LEVEL_OFFSET_STEP 3 |
| 271 | |
| 272 | static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self, |
| 273 | struct callchain_node *chain_node, |
| 274 | u64 total, int level, |
| 275 | unsigned short row, |
| 276 | off_t *row_offset, |
| 277 | bool *is_current_entry) |
| 278 | { |
| 279 | struct rb_node *node; |
| 280 | int first_row = row, width, offset = level * LEVEL_OFFSET_STEP; |
| 281 | u64 new_total, remaining; |
| 282 | |
| 283 | if (callchain_param.mode == CHAIN_GRAPH_REL) |
| 284 | new_total = chain_node->children_hit; |
| 285 | else |
| 286 | new_total = total; |
| 287 | |
| 288 | remaining = new_total; |
| 289 | node = rb_first(&chain_node->rb_root); |
| 290 | while (node) { |
| 291 | struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node); |
| 292 | struct rb_node *next = rb_next(node); |
| 293 | u64 cumul = cumul_hits(child); |
| 294 | struct callchain_list *chain; |
| 295 | char folded_sign = ' '; |
| 296 | int first = true; |
| 297 | int extra_offset = 0; |
| 298 | |
| 299 | remaining -= cumul; |
| 300 | |
| 301 | list_for_each_entry(chain, &child->val, list) { |
| 302 | char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str; |
| 303 | const char *str; |
| 304 | int color; |
| 305 | bool was_first = first; |
| 306 | |
| 307 | if (first) { |
| 308 | first = false; |
| 309 | chain->ms.has_children = chain->list.next != &child->val || |
| 310 | rb_first(&child->rb_root) != NULL; |
| 311 | } else { |
| 312 | extra_offset = LEVEL_OFFSET_STEP; |
| 313 | chain->ms.has_children = chain->list.next == &child->val && |
| 314 | rb_first(&child->rb_root) != NULL; |
| 315 | } |
| 316 | |
| 317 | folded_sign = callchain_list__folded(chain); |
| 318 | if (*row_offset != 0) { |
| 319 | --*row_offset; |
| 320 | goto do_next; |
| 321 | } |
| 322 | |
| 323 | alloc_str = NULL; |
| 324 | str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr)); |
| 325 | if (was_first) { |
| 326 | double percent = cumul * 100.0 / new_total; |
| 327 | |
| 328 | if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0) |
| 329 | str = "Not enough memory!"; |
| 330 | else |
| 331 | str = alloc_str; |
| 332 | } |
| 333 | |
| 334 | color = HE_COLORSET_NORMAL; |
| 335 | width = self->b.width - (offset + extra_offset + 2); |
| 336 | if (ui_browser__is_current_entry(&self->b, row)) { |
| 337 | self->selection = &chain->ms; |
| 338 | color = HE_COLORSET_SELECTED; |
| 339 | *is_current_entry = true; |
| 340 | } |
| 341 | |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 342 | ui_browser__set_color(&self->b, color); |
| 343 | ui_browser__gotorc(&self->b, row, 0); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 344 | slsmg_write_nstring(" ", offset + extra_offset); |
| 345 | slsmg_printf("%c ", folded_sign); |
| 346 | slsmg_write_nstring(str, width); |
| 347 | free(alloc_str); |
| 348 | |
| 349 | if (++row == self->b.height) |
| 350 | goto out; |
| 351 | do_next: |
| 352 | if (folded_sign == '+') |
| 353 | break; |
| 354 | } |
| 355 | |
| 356 | if (folded_sign == '-') { |
| 357 | const int new_level = level + (extra_offset ? 2 : 1); |
| 358 | row += hist_browser__show_callchain_node_rb_tree(self, child, new_total, |
| 359 | new_level, row, row_offset, |
| 360 | is_current_entry); |
| 361 | } |
| 362 | if (row == self->b.height) |
| 363 | goto out; |
| 364 | node = next; |
| 365 | } |
| 366 | out: |
| 367 | return row - first_row; |
| 368 | } |
| 369 | |
| 370 | static int hist_browser__show_callchain_node(struct hist_browser *self, |
| 371 | struct callchain_node *node, |
| 372 | int level, unsigned short row, |
| 373 | off_t *row_offset, |
| 374 | bool *is_current_entry) |
| 375 | { |
| 376 | struct callchain_list *chain; |
| 377 | int first_row = row, |
| 378 | offset = level * LEVEL_OFFSET_STEP, |
| 379 | width = self->b.width - offset; |
| 380 | char folded_sign = ' '; |
| 381 | |
| 382 | list_for_each_entry(chain, &node->val, list) { |
| 383 | char ipstr[BITS_PER_LONG / 4 + 1], *s; |
| 384 | int color; |
| 385 | /* |
| 386 | * FIXME: This should be moved to somewhere else, |
| 387 | * probably when the callchain is created, so as not to |
| 388 | * traverse it all over again |
| 389 | */ |
| 390 | chain->ms.has_children = rb_first(&node->rb_root) != NULL; |
| 391 | folded_sign = callchain_list__folded(chain); |
| 392 | |
| 393 | if (*row_offset != 0) { |
| 394 | --*row_offset; |
| 395 | continue; |
| 396 | } |
| 397 | |
| 398 | color = HE_COLORSET_NORMAL; |
| 399 | if (ui_browser__is_current_entry(&self->b, row)) { |
| 400 | self->selection = &chain->ms; |
| 401 | color = HE_COLORSET_SELECTED; |
| 402 | *is_current_entry = true; |
| 403 | } |
| 404 | |
| 405 | s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr)); |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 406 | ui_browser__gotorc(&self->b, row, 0); |
| 407 | ui_browser__set_color(&self->b, color); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 408 | slsmg_write_nstring(" ", offset); |
| 409 | slsmg_printf("%c ", folded_sign); |
| 410 | slsmg_write_nstring(s, width - 2); |
| 411 | |
| 412 | if (++row == self->b.height) |
| 413 | goto out; |
| 414 | } |
| 415 | |
| 416 | if (folded_sign == '-') |
| 417 | row += hist_browser__show_callchain_node_rb_tree(self, node, |
| 418 | self->hists->stats.total_period, |
| 419 | level + 1, row, |
| 420 | row_offset, |
| 421 | is_current_entry); |
| 422 | out: |
| 423 | return row - first_row; |
| 424 | } |
| 425 | |
| 426 | static int hist_browser__show_callchain(struct hist_browser *self, |
| 427 | struct rb_root *chain, |
| 428 | int level, unsigned short row, |
| 429 | off_t *row_offset, |
| 430 | bool *is_current_entry) |
| 431 | { |
| 432 | struct rb_node *nd; |
| 433 | int first_row = row; |
| 434 | |
| 435 | for (nd = rb_first(chain); nd; nd = rb_next(nd)) { |
| 436 | struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node); |
| 437 | |
| 438 | row += hist_browser__show_callchain_node(self, node, level, |
| 439 | row, row_offset, |
| 440 | is_current_entry); |
| 441 | if (row == self->b.height) |
| 442 | break; |
| 443 | } |
| 444 | |
| 445 | return row - first_row; |
| 446 | } |
| 447 | |
| 448 | static int hist_browser__show_entry(struct hist_browser *self, |
| 449 | struct hist_entry *entry, |
| 450 | unsigned short row) |
| 451 | { |
| 452 | char s[256]; |
| 453 | double percent; |
| 454 | int printed = 0; |
| 455 | int color, width = self->b.width; |
| 456 | char folded_sign = ' '; |
| 457 | bool current_entry = ui_browser__is_current_entry(&self->b, row); |
| 458 | off_t row_offset = entry->row_offset; |
| 459 | |
| 460 | if (current_entry) { |
| 461 | self->he_selection = entry; |
| 462 | self->selection = &entry->ms; |
| 463 | } |
| 464 | |
| 465 | if (symbol_conf.use_callchain) { |
| 466 | entry->ms.has_children = !RB_EMPTY_ROOT(&entry->sorted_chain); |
| 467 | folded_sign = hist_entry__folded(entry); |
| 468 | } |
| 469 | |
| 470 | if (row_offset == 0) { |
| 471 | hist_entry__snprintf(entry, s, sizeof(s), self->hists, NULL, false, |
| 472 | 0, false, self->hists->stats.total_period); |
| 473 | percent = (entry->period * 100.0) / self->hists->stats.total_period; |
| 474 | |
| 475 | color = HE_COLORSET_SELECTED; |
| 476 | if (!current_entry) { |
| 477 | if (percent >= MIN_RED) |
| 478 | color = HE_COLORSET_TOP; |
| 479 | else if (percent >= MIN_GREEN) |
| 480 | color = HE_COLORSET_MEDIUM; |
| 481 | else |
| 482 | color = HE_COLORSET_NORMAL; |
| 483 | } |
| 484 | |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 485 | ui_browser__set_color(&self->b, color); |
| 486 | ui_browser__gotorc(&self->b, row, 0); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 487 | if (symbol_conf.use_callchain) { |
| 488 | slsmg_printf("%c ", folded_sign); |
| 489 | width -= 2; |
| 490 | } |
| 491 | slsmg_write_nstring(s, width); |
| 492 | ++row; |
| 493 | ++printed; |
| 494 | } else |
| 495 | --row_offset; |
| 496 | |
| 497 | if (folded_sign == '-' && row != self->b.height) { |
| 498 | printed += hist_browser__show_callchain(self, &entry->sorted_chain, |
| 499 | 1, row, &row_offset, |
| 500 | ¤t_entry); |
| 501 | if (current_entry) |
| 502 | self->he_selection = entry; |
| 503 | } |
| 504 | |
| 505 | return printed; |
| 506 | } |
| 507 | |
| 508 | static unsigned int hist_browser__refresh(struct ui_browser *self) |
| 509 | { |
| 510 | unsigned row = 0; |
| 511 | struct rb_node *nd; |
| 512 | struct hist_browser *hb = container_of(self, struct hist_browser, b); |
| 513 | |
| 514 | if (self->top == NULL) |
| 515 | self->top = rb_first(&hb->hists->entries); |
| 516 | |
| 517 | for (nd = self->top; nd; nd = rb_next(nd)) { |
| 518 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 519 | |
| 520 | if (h->filtered) |
| 521 | continue; |
| 522 | |
| 523 | row += hist_browser__show_entry(hb, h, row); |
| 524 | if (row == self->height) |
| 525 | break; |
| 526 | } |
| 527 | |
| 528 | return row; |
| 529 | } |
| 530 | |
| 531 | static struct rb_node *hists__filter_entries(struct rb_node *nd) |
| 532 | { |
| 533 | while (nd != NULL) { |
| 534 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 535 | if (!h->filtered) |
| 536 | return nd; |
| 537 | |
| 538 | nd = rb_next(nd); |
| 539 | } |
| 540 | |
| 541 | return NULL; |
| 542 | } |
| 543 | |
| 544 | static struct rb_node *hists__filter_prev_entries(struct rb_node *nd) |
| 545 | { |
| 546 | while (nd != NULL) { |
| 547 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 548 | if (!h->filtered) |
| 549 | return nd; |
| 550 | |
| 551 | nd = rb_prev(nd); |
| 552 | } |
| 553 | |
| 554 | return NULL; |
| 555 | } |
| 556 | |
| 557 | static void ui_browser__hists_seek(struct ui_browser *self, |
| 558 | off_t offset, int whence) |
| 559 | { |
| 560 | struct hist_entry *h; |
| 561 | struct rb_node *nd; |
| 562 | bool first = true; |
| 563 | |
| 564 | switch (whence) { |
| 565 | case SEEK_SET: |
| 566 | nd = hists__filter_entries(rb_first(self->entries)); |
| 567 | break; |
| 568 | case SEEK_CUR: |
| 569 | nd = self->top; |
| 570 | goto do_offset; |
| 571 | case SEEK_END: |
| 572 | nd = hists__filter_prev_entries(rb_last(self->entries)); |
| 573 | first = false; |
| 574 | break; |
| 575 | default: |
| 576 | return; |
| 577 | } |
| 578 | |
| 579 | /* |
| 580 | * Moves not relative to the first visible entry invalidates its |
| 581 | * row_offset: |
| 582 | */ |
| 583 | h = rb_entry(self->top, struct hist_entry, rb_node); |
| 584 | h->row_offset = 0; |
| 585 | |
| 586 | /* |
| 587 | * Here we have to check if nd is expanded (+), if it is we can't go |
| 588 | * the next top level hist_entry, instead we must compute an offset of |
| 589 | * what _not_ to show and not change the first visible entry. |
| 590 | * |
| 591 | * This offset increments when we are going from top to bottom and |
| 592 | * decreases when we're going from bottom to top. |
| 593 | * |
| 594 | * As we don't have backpointers to the top level in the callchains |
| 595 | * structure, we need to always print the whole hist_entry callchain, |
| 596 | * skipping the first ones that are before the first visible entry |
| 597 | * and stop when we printed enough lines to fill the screen. |
| 598 | */ |
| 599 | do_offset: |
| 600 | if (offset > 0) { |
| 601 | do { |
| 602 | h = rb_entry(nd, struct hist_entry, rb_node); |
| 603 | if (h->ms.unfolded) { |
| 604 | u16 remaining = h->nr_rows - h->row_offset; |
| 605 | if (offset > remaining) { |
| 606 | offset -= remaining; |
| 607 | h->row_offset = 0; |
| 608 | } else { |
| 609 | h->row_offset += offset; |
| 610 | offset = 0; |
| 611 | self->top = nd; |
| 612 | break; |
| 613 | } |
| 614 | } |
| 615 | nd = hists__filter_entries(rb_next(nd)); |
| 616 | if (nd == NULL) |
| 617 | break; |
| 618 | --offset; |
| 619 | self->top = nd; |
| 620 | } while (offset != 0); |
| 621 | } else if (offset < 0) { |
| 622 | while (1) { |
| 623 | h = rb_entry(nd, struct hist_entry, rb_node); |
| 624 | if (h->ms.unfolded) { |
| 625 | if (first) { |
| 626 | if (-offset > h->row_offset) { |
| 627 | offset += h->row_offset; |
| 628 | h->row_offset = 0; |
| 629 | } else { |
| 630 | h->row_offset += offset; |
| 631 | offset = 0; |
| 632 | self->top = nd; |
| 633 | break; |
| 634 | } |
| 635 | } else { |
| 636 | if (-offset > h->nr_rows) { |
| 637 | offset += h->nr_rows; |
| 638 | h->row_offset = 0; |
| 639 | } else { |
| 640 | h->row_offset = h->nr_rows + offset; |
| 641 | offset = 0; |
| 642 | self->top = nd; |
| 643 | break; |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | nd = hists__filter_prev_entries(rb_prev(nd)); |
| 649 | if (nd == NULL) |
| 650 | break; |
| 651 | ++offset; |
| 652 | self->top = nd; |
| 653 | if (offset == 0) { |
| 654 | /* |
| 655 | * Last unfiltered hist_entry, check if it is |
| 656 | * unfolded, if it is then we should have |
| 657 | * row_offset at its last entry. |
| 658 | */ |
| 659 | h = rb_entry(nd, struct hist_entry, rb_node); |
| 660 | if (h->ms.unfolded) |
| 661 | h->row_offset = h->nr_rows; |
| 662 | break; |
| 663 | } |
| 664 | first = false; |
| 665 | } |
| 666 | } else { |
| 667 | self->top = nd; |
| 668 | h = rb_entry(nd, struct hist_entry, rb_node); |
| 669 | h->row_offset = 0; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | static struct hist_browser *hist_browser__new(struct hists *hists) |
| 674 | { |
| 675 | struct hist_browser *self = zalloc(sizeof(*self)); |
| 676 | |
| 677 | if (self) { |
| 678 | self->hists = hists; |
| 679 | self->b.refresh = hist_browser__refresh; |
| 680 | self->b.seek = ui_browser__hists_seek; |
| 681 | } |
| 682 | |
| 683 | return self; |
| 684 | } |
| 685 | |
| 686 | static void hist_browser__delete(struct hist_browser *self) |
| 687 | { |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 688 | free(self); |
| 689 | } |
| 690 | |
| 691 | static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self) |
| 692 | { |
| 693 | return self->he_selection; |
| 694 | } |
| 695 | |
| 696 | static struct thread *hist_browser__selected_thread(struct hist_browser *self) |
| 697 | { |
| 698 | return self->he_selection->thread; |
| 699 | } |
| 700 | |
| 701 | static int hist_browser__title(char *bf, size_t size, const char *ev_name, |
| 702 | const struct dso *dso, const struct thread *thread) |
| 703 | { |
| 704 | int printed = 0; |
| 705 | |
| 706 | if (thread) |
| 707 | printed += snprintf(bf + printed, size - printed, |
| 708 | "Thread: %s(%d)", |
| 709 | (thread->comm_set ? thread->comm : ""), |
| 710 | thread->pid); |
| 711 | if (dso) |
| 712 | printed += snprintf(bf + printed, size - printed, |
| 713 | "%sDSO: %s", thread ? " " : "", |
| 714 | dso->short_name); |
| 715 | return printed ?: snprintf(bf, size, "Event: %s", ev_name); |
| 716 | } |
| 717 | |
| 718 | int hists__browse(struct hists *self, const char *helpline, const char *ev_name) |
| 719 | { |
| 720 | struct hist_browser *browser = hist_browser__new(self); |
| 721 | struct pstack *fstack; |
| 722 | const struct thread *thread_filter = NULL; |
| 723 | const struct dso *dso_filter = NULL; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 724 | char msg[160]; |
| 725 | int key = -1; |
| 726 | |
| 727 | if (browser == NULL) |
| 728 | return -1; |
| 729 | |
| 730 | fstack = pstack__new(2); |
| 731 | if (fstack == NULL) |
| 732 | goto out; |
| 733 | |
| 734 | ui_helpline__push(helpline); |
| 735 | |
| 736 | hist_browser__title(msg, sizeof(msg), ev_name, |
| 737 | dso_filter, thread_filter); |
| 738 | |
| 739 | while (1) { |
| 740 | const struct thread *thread; |
| 741 | const struct dso *dso; |
| 742 | char *options[16]; |
| 743 | int nr_options = 0, choice = 0, i, |
| 744 | annotate = -2, zoom_dso = -2, zoom_thread = -2, |
| 745 | browse_map = -2; |
| 746 | |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame^] | 747 | key = hist_browser__run(browser, msg); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 748 | |
| 749 | thread = hist_browser__selected_thread(browser); |
| 750 | dso = browser->selection->map ? browser->selection->map->dso : NULL; |
| 751 | |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame^] | 752 | switch (key) { |
| 753 | case NEWT_KEY_F1: |
| 754 | goto do_help; |
| 755 | case NEWT_KEY_TAB: |
| 756 | case NEWT_KEY_UNTAB: |
| 757 | /* |
| 758 | * Exit the browser, let hists__browser_tree |
| 759 | * go to the next or previous |
| 760 | */ |
| 761 | goto out_free_stack; |
| 762 | case 'a': |
| 763 | if (browser->selection->map == NULL && |
| 764 | browser->selection->map->dso->annotate_warned) |
| 765 | continue; |
| 766 | goto do_annotate; |
| 767 | case 'd': |
| 768 | goto zoom_dso; |
| 769 | case 't': |
| 770 | goto zoom_thread; |
| 771 | case 'h': |
| 772 | case '?': |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 773 | do_help: |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame^] | 774 | ui__help_window("-> Zoom into DSO/Threads & Annotate current symbol\n" |
| 775 | "<- Zoom out\n" |
| 776 | "a Annotate current symbol\n" |
| 777 | "h/?/F1 Show this window\n" |
| 778 | "d Zoom into current DSO\n" |
| 779 | "t Zoom into current Thread\n" |
| 780 | "q/CTRL+C Exit browser"); |
| 781 | continue; |
| 782 | case NEWT_KEY_ENTER: |
| 783 | case NEWT_KEY_RIGHT: |
| 784 | /* menu */ |
| 785 | break; |
| 786 | case NEWT_KEY_LEFT: { |
| 787 | const void *top; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 788 | |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame^] | 789 | if (pstack__empty(fstack)) |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 790 | continue; |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame^] | 791 | top = pstack__pop(fstack); |
| 792 | if (top == &dso_filter) |
| 793 | goto zoom_out_dso; |
| 794 | if (top == &thread_filter) |
| 795 | goto zoom_out_thread; |
| 796 | continue; |
| 797 | } |
| 798 | case NEWT_KEY_ESCAPE: |
| 799 | if (!ui__dialog_yesno("Do you really want to exit?")) |
| 800 | continue; |
| 801 | /* Fall thru */ |
| 802 | default: |
| 803 | goto out_free_stack; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | if (browser->selection->sym != NULL && |
| 807 | !browser->selection->map->dso->annotate_warned && |
| 808 | asprintf(&options[nr_options], "Annotate %s", |
| 809 | browser->selection->sym->name) > 0) |
| 810 | annotate = nr_options++; |
| 811 | |
| 812 | if (thread != NULL && |
| 813 | asprintf(&options[nr_options], "Zoom %s %s(%d) thread", |
| 814 | (thread_filter ? "out of" : "into"), |
| 815 | (thread->comm_set ? thread->comm : ""), |
| 816 | thread->pid) > 0) |
| 817 | zoom_thread = nr_options++; |
| 818 | |
| 819 | if (dso != NULL && |
| 820 | asprintf(&options[nr_options], "Zoom %s %s DSO", |
| 821 | (dso_filter ? "out of" : "into"), |
| 822 | (dso->kernel ? "the Kernel" : dso->short_name)) > 0) |
| 823 | zoom_dso = nr_options++; |
| 824 | |
| 825 | if (browser->selection->map != NULL && |
| 826 | asprintf(&options[nr_options], "Browse map details") > 0) |
| 827 | browse_map = nr_options++; |
| 828 | |
| 829 | options[nr_options++] = (char *)"Exit"; |
| 830 | |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 831 | choice = ui__popup_menu(nr_options, options); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 832 | |
| 833 | for (i = 0; i < nr_options - 1; ++i) |
| 834 | free(options[i]); |
| 835 | |
| 836 | if (choice == nr_options - 1) |
| 837 | break; |
| 838 | |
| 839 | if (choice == -1) |
| 840 | continue; |
| 841 | |
| 842 | if (choice == annotate) { |
| 843 | struct hist_entry *he; |
| 844 | do_annotate: |
| 845 | if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) { |
| 846 | browser->selection->map->dso->annotate_warned = 1; |
| 847 | ui_helpline__puts("No vmlinux file found, can't " |
| 848 | "annotate with just a " |
| 849 | "kallsyms file"); |
| 850 | continue; |
| 851 | } |
| 852 | |
| 853 | he = hist_browser__selected_entry(browser); |
| 854 | if (he == NULL) |
| 855 | continue; |
| 856 | |
| 857 | hist_entry__tui_annotate(he); |
| 858 | } else if (choice == browse_map) |
| 859 | map__browse(browser->selection->map); |
| 860 | else if (choice == zoom_dso) { |
| 861 | zoom_dso: |
| 862 | if (dso_filter) { |
| 863 | pstack__remove(fstack, &dso_filter); |
| 864 | zoom_out_dso: |
| 865 | ui_helpline__pop(); |
| 866 | dso_filter = NULL; |
| 867 | } else { |
| 868 | if (dso == NULL) |
| 869 | continue; |
| 870 | ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"", |
| 871 | dso->kernel ? "the Kernel" : dso->short_name); |
| 872 | dso_filter = dso; |
| 873 | pstack__push(fstack, &dso_filter); |
| 874 | } |
| 875 | hists__filter_by_dso(self, dso_filter); |
| 876 | hist_browser__title(msg, sizeof(msg), ev_name, |
| 877 | dso_filter, thread_filter); |
| 878 | hist_browser__reset(browser); |
| 879 | } else if (choice == zoom_thread) { |
| 880 | zoom_thread: |
| 881 | if (thread_filter) { |
| 882 | pstack__remove(fstack, &thread_filter); |
| 883 | zoom_out_thread: |
| 884 | ui_helpline__pop(); |
| 885 | thread_filter = NULL; |
| 886 | } else { |
| 887 | ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"", |
| 888 | thread->comm_set ? thread->comm : "", |
| 889 | thread->pid); |
| 890 | thread_filter = thread; |
| 891 | pstack__push(fstack, &thread_filter); |
| 892 | } |
| 893 | hists__filter_by_thread(self, thread_filter); |
| 894 | hist_browser__title(msg, sizeof(msg), ev_name, |
| 895 | dso_filter, thread_filter); |
| 896 | hist_browser__reset(browser); |
| 897 | } |
| 898 | } |
| 899 | out_free_stack: |
| 900 | pstack__delete(fstack); |
| 901 | out: |
| 902 | hist_browser__delete(browser); |
| 903 | return key; |
| 904 | } |
| 905 | |
| 906 | int hists__tui_browse_tree(struct rb_root *self, const char *help) |
| 907 | { |
| 908 | struct rb_node *first = rb_first(self), *nd = first, *next; |
| 909 | int key = 0; |
| 910 | |
| 911 | while (nd) { |
| 912 | struct hists *hists = rb_entry(nd, struct hists, rb_node); |
| 913 | const char *ev_name = __event_name(hists->type, hists->config); |
| 914 | |
| 915 | key = hists__browse(hists, help, ev_name); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 916 | switch (key) { |
| 917 | case NEWT_KEY_TAB: |
| 918 | next = rb_next(nd); |
| 919 | if (next) |
| 920 | nd = next; |
| 921 | break; |
| 922 | case NEWT_KEY_UNTAB: |
| 923 | if (nd == first) |
| 924 | continue; |
| 925 | nd = rb_prev(nd); |
| 926 | default: |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame^] | 927 | return key; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 928 | } |
| 929 | } |
| 930 | |
| 931 | return key; |
| 932 | } |