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 | |
| 198 | static int hist_browser__run(struct hist_browser *self, const char *title, |
| 199 | struct newtExitStruct *es) |
| 200 | { |
| 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) { |
| 230 | ui_browser__run(&self->b, es); |
| 231 | |
| 232 | if (es->reason != NEWT_EXIT_HOTKEY) |
| 233 | break; |
| 234 | switch (es->u.key) { |
Arnaldo Carvalho de Melo | 4694153 | 2010-08-10 15:50:07 -0300 | [diff] [blame] | 235 | case 'D': { /* Debug */ |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 236 | static int seq; |
| 237 | struct hist_entry *h = rb_entry(self->b.top, |
| 238 | struct hist_entry, rb_node); |
| 239 | ui_helpline__pop(); |
| 240 | ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d", |
| 241 | seq++, self->b.nr_entries, |
| 242 | self->hists->nr_entries, |
| 243 | self->b.height, |
| 244 | self->b.index, |
| 245 | self->b.top_idx, |
| 246 | h->row_offset, h->nr_rows); |
| 247 | } |
| 248 | continue; |
| 249 | case NEWT_KEY_ENTER: |
| 250 | if (hist_browser__toggle_fold(self)) |
| 251 | break; |
| 252 | /* fall thru */ |
| 253 | default: |
| 254 | return 0; |
| 255 | } |
| 256 | } |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 257 | |
| 258 | ui_browser__hide(&self->b); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | static char *callchain_list__sym_name(struct callchain_list *self, |
| 263 | char *bf, size_t bfsize) |
| 264 | { |
| 265 | if (self->ms.sym) |
| 266 | return self->ms.sym->name; |
| 267 | |
| 268 | snprintf(bf, bfsize, "%#Lx", self->ip); |
| 269 | return bf; |
| 270 | } |
| 271 | |
| 272 | #define LEVEL_OFFSET_STEP 3 |
| 273 | |
| 274 | static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self, |
| 275 | struct callchain_node *chain_node, |
| 276 | u64 total, int level, |
| 277 | unsigned short row, |
| 278 | off_t *row_offset, |
| 279 | bool *is_current_entry) |
| 280 | { |
| 281 | struct rb_node *node; |
| 282 | int first_row = row, width, offset = level * LEVEL_OFFSET_STEP; |
| 283 | u64 new_total, remaining; |
| 284 | |
| 285 | if (callchain_param.mode == CHAIN_GRAPH_REL) |
| 286 | new_total = chain_node->children_hit; |
| 287 | else |
| 288 | new_total = total; |
| 289 | |
| 290 | remaining = new_total; |
| 291 | node = rb_first(&chain_node->rb_root); |
| 292 | while (node) { |
| 293 | struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node); |
| 294 | struct rb_node *next = rb_next(node); |
| 295 | u64 cumul = cumul_hits(child); |
| 296 | struct callchain_list *chain; |
| 297 | char folded_sign = ' '; |
| 298 | int first = true; |
| 299 | int extra_offset = 0; |
| 300 | |
| 301 | remaining -= cumul; |
| 302 | |
| 303 | list_for_each_entry(chain, &child->val, list) { |
| 304 | char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str; |
| 305 | const char *str; |
| 306 | int color; |
| 307 | bool was_first = first; |
| 308 | |
| 309 | if (first) { |
| 310 | first = false; |
| 311 | chain->ms.has_children = chain->list.next != &child->val || |
| 312 | rb_first(&child->rb_root) != NULL; |
| 313 | } else { |
| 314 | extra_offset = LEVEL_OFFSET_STEP; |
| 315 | chain->ms.has_children = chain->list.next == &child->val && |
| 316 | rb_first(&child->rb_root) != NULL; |
| 317 | } |
| 318 | |
| 319 | folded_sign = callchain_list__folded(chain); |
| 320 | if (*row_offset != 0) { |
| 321 | --*row_offset; |
| 322 | goto do_next; |
| 323 | } |
| 324 | |
| 325 | alloc_str = NULL; |
| 326 | str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr)); |
| 327 | if (was_first) { |
| 328 | double percent = cumul * 100.0 / new_total; |
| 329 | |
| 330 | if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0) |
| 331 | str = "Not enough memory!"; |
| 332 | else |
| 333 | str = alloc_str; |
| 334 | } |
| 335 | |
| 336 | color = HE_COLORSET_NORMAL; |
| 337 | width = self->b.width - (offset + extra_offset + 2); |
| 338 | if (ui_browser__is_current_entry(&self->b, row)) { |
| 339 | self->selection = &chain->ms; |
| 340 | color = HE_COLORSET_SELECTED; |
| 341 | *is_current_entry = true; |
| 342 | } |
| 343 | |
| 344 | SLsmg_set_color(color); |
| 345 | SLsmg_gotorc(self->b.y + row, self->b.x); |
| 346 | slsmg_write_nstring(" ", offset + extra_offset); |
| 347 | slsmg_printf("%c ", folded_sign); |
| 348 | slsmg_write_nstring(str, width); |
| 349 | free(alloc_str); |
| 350 | |
| 351 | if (++row == self->b.height) |
| 352 | goto out; |
| 353 | do_next: |
| 354 | if (folded_sign == '+') |
| 355 | break; |
| 356 | } |
| 357 | |
| 358 | if (folded_sign == '-') { |
| 359 | const int new_level = level + (extra_offset ? 2 : 1); |
| 360 | row += hist_browser__show_callchain_node_rb_tree(self, child, new_total, |
| 361 | new_level, row, row_offset, |
| 362 | is_current_entry); |
| 363 | } |
| 364 | if (row == self->b.height) |
| 365 | goto out; |
| 366 | node = next; |
| 367 | } |
| 368 | out: |
| 369 | return row - first_row; |
| 370 | } |
| 371 | |
| 372 | static int hist_browser__show_callchain_node(struct hist_browser *self, |
| 373 | struct callchain_node *node, |
| 374 | int level, unsigned short row, |
| 375 | off_t *row_offset, |
| 376 | bool *is_current_entry) |
| 377 | { |
| 378 | struct callchain_list *chain; |
| 379 | int first_row = row, |
| 380 | offset = level * LEVEL_OFFSET_STEP, |
| 381 | width = self->b.width - offset; |
| 382 | char folded_sign = ' '; |
| 383 | |
| 384 | list_for_each_entry(chain, &node->val, list) { |
| 385 | char ipstr[BITS_PER_LONG / 4 + 1], *s; |
| 386 | int color; |
| 387 | /* |
| 388 | * FIXME: This should be moved to somewhere else, |
| 389 | * probably when the callchain is created, so as not to |
| 390 | * traverse it all over again |
| 391 | */ |
| 392 | chain->ms.has_children = rb_first(&node->rb_root) != NULL; |
| 393 | folded_sign = callchain_list__folded(chain); |
| 394 | |
| 395 | if (*row_offset != 0) { |
| 396 | --*row_offset; |
| 397 | continue; |
| 398 | } |
| 399 | |
| 400 | color = HE_COLORSET_NORMAL; |
| 401 | if (ui_browser__is_current_entry(&self->b, row)) { |
| 402 | self->selection = &chain->ms; |
| 403 | color = HE_COLORSET_SELECTED; |
| 404 | *is_current_entry = true; |
| 405 | } |
| 406 | |
| 407 | s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr)); |
| 408 | SLsmg_gotorc(self->b.y + row, self->b.x); |
| 409 | SLsmg_set_color(color); |
| 410 | slsmg_write_nstring(" ", offset); |
| 411 | slsmg_printf("%c ", folded_sign); |
| 412 | slsmg_write_nstring(s, width - 2); |
| 413 | |
| 414 | if (++row == self->b.height) |
| 415 | goto out; |
| 416 | } |
| 417 | |
| 418 | if (folded_sign == '-') |
| 419 | row += hist_browser__show_callchain_node_rb_tree(self, node, |
| 420 | self->hists->stats.total_period, |
| 421 | level + 1, row, |
| 422 | row_offset, |
| 423 | is_current_entry); |
| 424 | out: |
| 425 | return row - first_row; |
| 426 | } |
| 427 | |
| 428 | static int hist_browser__show_callchain(struct hist_browser *self, |
| 429 | struct rb_root *chain, |
| 430 | int level, unsigned short row, |
| 431 | off_t *row_offset, |
| 432 | bool *is_current_entry) |
| 433 | { |
| 434 | struct rb_node *nd; |
| 435 | int first_row = row; |
| 436 | |
| 437 | for (nd = rb_first(chain); nd; nd = rb_next(nd)) { |
| 438 | struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node); |
| 439 | |
| 440 | row += hist_browser__show_callchain_node(self, node, level, |
| 441 | row, row_offset, |
| 442 | is_current_entry); |
| 443 | if (row == self->b.height) |
| 444 | break; |
| 445 | } |
| 446 | |
| 447 | return row - first_row; |
| 448 | } |
| 449 | |
| 450 | static int hist_browser__show_entry(struct hist_browser *self, |
| 451 | struct hist_entry *entry, |
| 452 | unsigned short row) |
| 453 | { |
| 454 | char s[256]; |
| 455 | double percent; |
| 456 | int printed = 0; |
| 457 | int color, width = self->b.width; |
| 458 | char folded_sign = ' '; |
| 459 | bool current_entry = ui_browser__is_current_entry(&self->b, row); |
| 460 | off_t row_offset = entry->row_offset; |
| 461 | |
| 462 | if (current_entry) { |
| 463 | self->he_selection = entry; |
| 464 | self->selection = &entry->ms; |
| 465 | } |
| 466 | |
| 467 | if (symbol_conf.use_callchain) { |
| 468 | entry->ms.has_children = !RB_EMPTY_ROOT(&entry->sorted_chain); |
| 469 | folded_sign = hist_entry__folded(entry); |
| 470 | } |
| 471 | |
| 472 | if (row_offset == 0) { |
| 473 | hist_entry__snprintf(entry, s, sizeof(s), self->hists, NULL, false, |
| 474 | 0, false, self->hists->stats.total_period); |
| 475 | percent = (entry->period * 100.0) / self->hists->stats.total_period; |
| 476 | |
| 477 | color = HE_COLORSET_SELECTED; |
| 478 | if (!current_entry) { |
| 479 | if (percent >= MIN_RED) |
| 480 | color = HE_COLORSET_TOP; |
| 481 | else if (percent >= MIN_GREEN) |
| 482 | color = HE_COLORSET_MEDIUM; |
| 483 | else |
| 484 | color = HE_COLORSET_NORMAL; |
| 485 | } |
| 486 | |
| 487 | SLsmg_set_color(color); |
| 488 | SLsmg_gotorc(self->b.y + row, self->b.x); |
| 489 | if (symbol_conf.use_callchain) { |
| 490 | slsmg_printf("%c ", folded_sign); |
| 491 | width -= 2; |
| 492 | } |
| 493 | slsmg_write_nstring(s, width); |
| 494 | ++row; |
| 495 | ++printed; |
| 496 | } else |
| 497 | --row_offset; |
| 498 | |
| 499 | if (folded_sign == '-' && row != self->b.height) { |
| 500 | printed += hist_browser__show_callchain(self, &entry->sorted_chain, |
| 501 | 1, row, &row_offset, |
| 502 | ¤t_entry); |
| 503 | if (current_entry) |
| 504 | self->he_selection = entry; |
| 505 | } |
| 506 | |
| 507 | return printed; |
| 508 | } |
| 509 | |
| 510 | static unsigned int hist_browser__refresh(struct ui_browser *self) |
| 511 | { |
| 512 | unsigned row = 0; |
| 513 | struct rb_node *nd; |
| 514 | struct hist_browser *hb = container_of(self, struct hist_browser, b); |
| 515 | |
| 516 | if (self->top == NULL) |
| 517 | self->top = rb_first(&hb->hists->entries); |
| 518 | |
| 519 | for (nd = self->top; nd; nd = rb_next(nd)) { |
| 520 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 521 | |
| 522 | if (h->filtered) |
| 523 | continue; |
| 524 | |
| 525 | row += hist_browser__show_entry(hb, h, row); |
| 526 | if (row == self->height) |
| 527 | break; |
| 528 | } |
| 529 | |
| 530 | return row; |
| 531 | } |
| 532 | |
| 533 | static struct rb_node *hists__filter_entries(struct rb_node *nd) |
| 534 | { |
| 535 | while (nd != NULL) { |
| 536 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 537 | if (!h->filtered) |
| 538 | return nd; |
| 539 | |
| 540 | nd = rb_next(nd); |
| 541 | } |
| 542 | |
| 543 | return NULL; |
| 544 | } |
| 545 | |
| 546 | static struct rb_node *hists__filter_prev_entries(struct rb_node *nd) |
| 547 | { |
| 548 | while (nd != NULL) { |
| 549 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 550 | if (!h->filtered) |
| 551 | return nd; |
| 552 | |
| 553 | nd = rb_prev(nd); |
| 554 | } |
| 555 | |
| 556 | return NULL; |
| 557 | } |
| 558 | |
| 559 | static void ui_browser__hists_seek(struct ui_browser *self, |
| 560 | off_t offset, int whence) |
| 561 | { |
| 562 | struct hist_entry *h; |
| 563 | struct rb_node *nd; |
| 564 | bool first = true; |
| 565 | |
| 566 | switch (whence) { |
| 567 | case SEEK_SET: |
| 568 | nd = hists__filter_entries(rb_first(self->entries)); |
| 569 | break; |
| 570 | case SEEK_CUR: |
| 571 | nd = self->top; |
| 572 | goto do_offset; |
| 573 | case SEEK_END: |
| 574 | nd = hists__filter_prev_entries(rb_last(self->entries)); |
| 575 | first = false; |
| 576 | break; |
| 577 | default: |
| 578 | return; |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | * Moves not relative to the first visible entry invalidates its |
| 583 | * row_offset: |
| 584 | */ |
| 585 | h = rb_entry(self->top, struct hist_entry, rb_node); |
| 586 | h->row_offset = 0; |
| 587 | |
| 588 | /* |
| 589 | * Here we have to check if nd is expanded (+), if it is we can't go |
| 590 | * the next top level hist_entry, instead we must compute an offset of |
| 591 | * what _not_ to show and not change the first visible entry. |
| 592 | * |
| 593 | * This offset increments when we are going from top to bottom and |
| 594 | * decreases when we're going from bottom to top. |
| 595 | * |
| 596 | * As we don't have backpointers to the top level in the callchains |
| 597 | * structure, we need to always print the whole hist_entry callchain, |
| 598 | * skipping the first ones that are before the first visible entry |
| 599 | * and stop when we printed enough lines to fill the screen. |
| 600 | */ |
| 601 | do_offset: |
| 602 | if (offset > 0) { |
| 603 | do { |
| 604 | h = rb_entry(nd, struct hist_entry, rb_node); |
| 605 | if (h->ms.unfolded) { |
| 606 | u16 remaining = h->nr_rows - h->row_offset; |
| 607 | if (offset > remaining) { |
| 608 | offset -= remaining; |
| 609 | h->row_offset = 0; |
| 610 | } else { |
| 611 | h->row_offset += offset; |
| 612 | offset = 0; |
| 613 | self->top = nd; |
| 614 | break; |
| 615 | } |
| 616 | } |
| 617 | nd = hists__filter_entries(rb_next(nd)); |
| 618 | if (nd == NULL) |
| 619 | break; |
| 620 | --offset; |
| 621 | self->top = nd; |
| 622 | } while (offset != 0); |
| 623 | } else if (offset < 0) { |
| 624 | while (1) { |
| 625 | h = rb_entry(nd, struct hist_entry, rb_node); |
| 626 | if (h->ms.unfolded) { |
| 627 | if (first) { |
| 628 | if (-offset > h->row_offset) { |
| 629 | offset += h->row_offset; |
| 630 | h->row_offset = 0; |
| 631 | } else { |
| 632 | h->row_offset += offset; |
| 633 | offset = 0; |
| 634 | self->top = nd; |
| 635 | break; |
| 636 | } |
| 637 | } else { |
| 638 | if (-offset > h->nr_rows) { |
| 639 | offset += h->nr_rows; |
| 640 | h->row_offset = 0; |
| 641 | } else { |
| 642 | h->row_offset = h->nr_rows + offset; |
| 643 | offset = 0; |
| 644 | self->top = nd; |
| 645 | break; |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | nd = hists__filter_prev_entries(rb_prev(nd)); |
| 651 | if (nd == NULL) |
| 652 | break; |
| 653 | ++offset; |
| 654 | self->top = nd; |
| 655 | if (offset == 0) { |
| 656 | /* |
| 657 | * Last unfiltered hist_entry, check if it is |
| 658 | * unfolded, if it is then we should have |
| 659 | * row_offset at its last entry. |
| 660 | */ |
| 661 | h = rb_entry(nd, struct hist_entry, rb_node); |
| 662 | if (h->ms.unfolded) |
| 663 | h->row_offset = h->nr_rows; |
| 664 | break; |
| 665 | } |
| 666 | first = false; |
| 667 | } |
| 668 | } else { |
| 669 | self->top = nd; |
| 670 | h = rb_entry(nd, struct hist_entry, rb_node); |
| 671 | h->row_offset = 0; |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | static struct hist_browser *hist_browser__new(struct hists *hists) |
| 676 | { |
| 677 | struct hist_browser *self = zalloc(sizeof(*self)); |
| 678 | |
| 679 | if (self) { |
| 680 | self->hists = hists; |
| 681 | self->b.refresh = hist_browser__refresh; |
| 682 | self->b.seek = ui_browser__hists_seek; |
| 683 | } |
| 684 | |
| 685 | return self; |
| 686 | } |
| 687 | |
| 688 | static void hist_browser__delete(struct hist_browser *self) |
| 689 | { |
| 690 | newtFormDestroy(self->b.form); |
| 691 | newtPopWindow(); |
| 692 | free(self); |
| 693 | } |
| 694 | |
| 695 | static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self) |
| 696 | { |
| 697 | return self->he_selection; |
| 698 | } |
| 699 | |
| 700 | static struct thread *hist_browser__selected_thread(struct hist_browser *self) |
| 701 | { |
| 702 | return self->he_selection->thread; |
| 703 | } |
| 704 | |
| 705 | static int hist_browser__title(char *bf, size_t size, const char *ev_name, |
| 706 | const struct dso *dso, const struct thread *thread) |
| 707 | { |
| 708 | int printed = 0; |
| 709 | |
| 710 | if (thread) |
| 711 | printed += snprintf(bf + printed, size - printed, |
| 712 | "Thread: %s(%d)", |
| 713 | (thread->comm_set ? thread->comm : ""), |
| 714 | thread->pid); |
| 715 | if (dso) |
| 716 | printed += snprintf(bf + printed, size - printed, |
| 717 | "%sDSO: %s", thread ? " " : "", |
| 718 | dso->short_name); |
| 719 | return printed ?: snprintf(bf, size, "Event: %s", ev_name); |
| 720 | } |
| 721 | |
| 722 | int hists__browse(struct hists *self, const char *helpline, const char *ev_name) |
| 723 | { |
| 724 | struct hist_browser *browser = hist_browser__new(self); |
| 725 | struct pstack *fstack; |
| 726 | const struct thread *thread_filter = NULL; |
| 727 | const struct dso *dso_filter = NULL; |
| 728 | struct newtExitStruct es; |
| 729 | char msg[160]; |
| 730 | int key = -1; |
| 731 | |
| 732 | if (browser == NULL) |
| 733 | return -1; |
| 734 | |
| 735 | fstack = pstack__new(2); |
| 736 | if (fstack == NULL) |
| 737 | goto out; |
| 738 | |
| 739 | ui_helpline__push(helpline); |
| 740 | |
| 741 | hist_browser__title(msg, sizeof(msg), ev_name, |
| 742 | dso_filter, thread_filter); |
| 743 | |
| 744 | while (1) { |
| 745 | const struct thread *thread; |
| 746 | const struct dso *dso; |
| 747 | char *options[16]; |
| 748 | int nr_options = 0, choice = 0, i, |
| 749 | annotate = -2, zoom_dso = -2, zoom_thread = -2, |
| 750 | browse_map = -2; |
| 751 | |
| 752 | if (hist_browser__run(browser, msg, &es)) |
| 753 | break; |
| 754 | |
| 755 | thread = hist_browser__selected_thread(browser); |
| 756 | dso = browser->selection->map ? browser->selection->map->dso : NULL; |
| 757 | |
| 758 | if (es.reason == NEWT_EXIT_HOTKEY) { |
| 759 | key = es.u.key; |
| 760 | |
| 761 | switch (key) { |
| 762 | case NEWT_KEY_F1: |
| 763 | goto do_help; |
| 764 | case NEWT_KEY_TAB: |
| 765 | case NEWT_KEY_UNTAB: |
| 766 | /* |
| 767 | * Exit the browser, let hists__browser_tree |
| 768 | * go to the next or previous |
| 769 | */ |
| 770 | goto out_free_stack; |
| 771 | default:; |
| 772 | } |
| 773 | |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 774 | switch (key) { |
Arnaldo Carvalho de Melo | 4694153 | 2010-08-10 15:50:07 -0300 | [diff] [blame] | 775 | case 'a': |
Frederik Deweerdt | c569d33 | 2010-09-23 22:19:01 +0200 | [diff] [blame^] | 776 | if (browser->selection->map == NULL || |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 777 | browser->selection->map->dso->annotate_warned) |
| 778 | continue; |
| 779 | goto do_annotate; |
Arnaldo Carvalho de Melo | 4694153 | 2010-08-10 15:50:07 -0300 | [diff] [blame] | 780 | case 'd': |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 781 | goto zoom_dso; |
Arnaldo Carvalho de Melo | 4694153 | 2010-08-10 15:50:07 -0300 | [diff] [blame] | 782 | case 't': |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 783 | goto zoom_thread; |
Arnaldo Carvalho de Melo | 4694153 | 2010-08-10 15:50:07 -0300 | [diff] [blame] | 784 | case 'h': |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 785 | case '?': |
| 786 | do_help: |
| 787 | ui__help_window("-> Zoom into DSO/Threads & Annotate current symbol\n" |
| 788 | "<- Zoom out\n" |
| 789 | "a Annotate current symbol\n" |
| 790 | "h/?/F1 Show this window\n" |
| 791 | "d Zoom into current DSO\n" |
| 792 | "t Zoom into current Thread\n" |
| 793 | "q/CTRL+C Exit browser"); |
| 794 | continue; |
| 795 | default:; |
| 796 | } |
| 797 | if (is_exit_key(key)) { |
| 798 | if (key == NEWT_KEY_ESCAPE && |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 799 | !ui__dialog_yesno("Do you really want to exit?")) |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 800 | continue; |
| 801 | break; |
| 802 | } |
| 803 | |
| 804 | if (es.u.key == NEWT_KEY_LEFT) { |
| 805 | const void *top; |
| 806 | |
| 807 | if (pstack__empty(fstack)) |
| 808 | continue; |
| 809 | top = pstack__pop(fstack); |
| 810 | if (top == &dso_filter) |
| 811 | goto zoom_out_dso; |
| 812 | if (top == &thread_filter) |
| 813 | goto zoom_out_thread; |
| 814 | continue; |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | if (browser->selection->sym != NULL && |
| 819 | !browser->selection->map->dso->annotate_warned && |
| 820 | asprintf(&options[nr_options], "Annotate %s", |
| 821 | browser->selection->sym->name) > 0) |
| 822 | annotate = nr_options++; |
| 823 | |
| 824 | if (thread != NULL && |
| 825 | asprintf(&options[nr_options], "Zoom %s %s(%d) thread", |
| 826 | (thread_filter ? "out of" : "into"), |
| 827 | (thread->comm_set ? thread->comm : ""), |
| 828 | thread->pid) > 0) |
| 829 | zoom_thread = nr_options++; |
| 830 | |
| 831 | if (dso != NULL && |
| 832 | asprintf(&options[nr_options], "Zoom %s %s DSO", |
| 833 | (dso_filter ? "out of" : "into"), |
| 834 | (dso->kernel ? "the Kernel" : dso->short_name)) > 0) |
| 835 | zoom_dso = nr_options++; |
| 836 | |
| 837 | if (browser->selection->map != NULL && |
| 838 | asprintf(&options[nr_options], "Browse map details") > 0) |
| 839 | browse_map = nr_options++; |
| 840 | |
| 841 | options[nr_options++] = (char *)"Exit"; |
| 842 | |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 843 | choice = ui__popup_menu(nr_options, options); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 844 | |
| 845 | for (i = 0; i < nr_options - 1; ++i) |
| 846 | free(options[i]); |
| 847 | |
| 848 | if (choice == nr_options - 1) |
| 849 | break; |
| 850 | |
| 851 | if (choice == -1) |
| 852 | continue; |
| 853 | |
| 854 | if (choice == annotate) { |
| 855 | struct hist_entry *he; |
| 856 | do_annotate: |
| 857 | if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) { |
| 858 | browser->selection->map->dso->annotate_warned = 1; |
| 859 | ui_helpline__puts("No vmlinux file found, can't " |
| 860 | "annotate with just a " |
| 861 | "kallsyms file"); |
| 862 | continue; |
| 863 | } |
| 864 | |
| 865 | he = hist_browser__selected_entry(browser); |
| 866 | if (he == NULL) |
| 867 | continue; |
| 868 | |
| 869 | hist_entry__tui_annotate(he); |
| 870 | } else if (choice == browse_map) |
| 871 | map__browse(browser->selection->map); |
| 872 | else if (choice == zoom_dso) { |
| 873 | zoom_dso: |
| 874 | if (dso_filter) { |
| 875 | pstack__remove(fstack, &dso_filter); |
| 876 | zoom_out_dso: |
| 877 | ui_helpline__pop(); |
| 878 | dso_filter = NULL; |
| 879 | } else { |
| 880 | if (dso == NULL) |
| 881 | continue; |
| 882 | ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"", |
| 883 | dso->kernel ? "the Kernel" : dso->short_name); |
| 884 | dso_filter = dso; |
| 885 | pstack__push(fstack, &dso_filter); |
| 886 | } |
| 887 | hists__filter_by_dso(self, dso_filter); |
| 888 | hist_browser__title(msg, sizeof(msg), ev_name, |
| 889 | dso_filter, thread_filter); |
| 890 | hist_browser__reset(browser); |
| 891 | } else if (choice == zoom_thread) { |
| 892 | zoom_thread: |
| 893 | if (thread_filter) { |
| 894 | pstack__remove(fstack, &thread_filter); |
| 895 | zoom_out_thread: |
| 896 | ui_helpline__pop(); |
| 897 | thread_filter = NULL; |
| 898 | } else { |
| 899 | ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"", |
| 900 | thread->comm_set ? thread->comm : "", |
| 901 | thread->pid); |
| 902 | thread_filter = thread; |
| 903 | pstack__push(fstack, &thread_filter); |
| 904 | } |
| 905 | hists__filter_by_thread(self, thread_filter); |
| 906 | hist_browser__title(msg, sizeof(msg), ev_name, |
| 907 | dso_filter, thread_filter); |
| 908 | hist_browser__reset(browser); |
| 909 | } |
| 910 | } |
| 911 | out_free_stack: |
| 912 | pstack__delete(fstack); |
| 913 | out: |
| 914 | hist_browser__delete(browser); |
| 915 | return key; |
| 916 | } |
| 917 | |
| 918 | int hists__tui_browse_tree(struct rb_root *self, const char *help) |
| 919 | { |
| 920 | struct rb_node *first = rb_first(self), *nd = first, *next; |
| 921 | int key = 0; |
| 922 | |
| 923 | while (nd) { |
| 924 | struct hists *hists = rb_entry(nd, struct hists, rb_node); |
| 925 | const char *ev_name = __event_name(hists->type, hists->config); |
| 926 | |
| 927 | key = hists__browse(hists, help, ev_name); |
| 928 | |
| 929 | if (is_exit_key(key)) |
| 930 | break; |
| 931 | |
| 932 | switch (key) { |
| 933 | case NEWT_KEY_TAB: |
| 934 | next = rb_next(nd); |
| 935 | if (next) |
| 936 | nd = next; |
| 937 | break; |
| 938 | case NEWT_KEY_UNTAB: |
| 939 | if (nd == first) |
| 940 | continue; |
| 941 | nd = rb_prev(nd); |
| 942 | default: |
| 943 | break; |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | return key; |
| 948 | } |