4 #include "../libslang.h"
8 #include <linux/rbtree.h>
10 #include "../../hist.h"
11 #include "../../pstack.h"
12 #include "../../sort.h"
13 #include "../../util.h"
15 #include "../browser.h"
16 #include "../helpline.h"
23 struct hist_entry *he_selection;
24 struct map_symbol *selection;
27 static void hist_browser__refresh_dimensions(struct hist_browser *self)
29 /* 3 == +/- toggle symbol before actual hist_entry rendering */
30 self->b.width = 3 + (hists__sort_list_width(self->hists) +
34 static void hist_browser__reset(struct hist_browser *self)
36 self->b.nr_entries = self->hists->nr_entries;
37 hist_browser__refresh_dimensions(self);
38 ui_browser__reset_index(&self->b);
41 static char tree__folded_sign(bool unfolded)
43 return unfolded ? '-' : '+';
46 static char map_symbol__folded(const struct map_symbol *self)
48 return self->has_children ? tree__folded_sign(self->unfolded) : ' ';
51 static char hist_entry__folded(const struct hist_entry *self)
53 return map_symbol__folded(&self->ms);
56 static char callchain_list__folded(const struct callchain_list *self)
58 return map_symbol__folded(&self->ms);
61 static void map_symbol__set_folding(struct map_symbol *self, bool unfold)
63 self->unfolded = unfold ? self->has_children : false;
66 static int callchain_node__count_rows_rb_tree(struct callchain_node *self)
71 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
72 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
73 struct callchain_list *chain;
74 char folded_sign = ' '; /* No children */
76 list_for_each_entry(chain, &child->val, list) {
78 /* We need this because we may not have children */
79 folded_sign = callchain_list__folded(chain);
80 if (folded_sign == '+')
84 if (folded_sign == '-') /* Have children and they're unfolded */
85 n += callchain_node__count_rows_rb_tree(child);
91 static int callchain_node__count_rows(struct callchain_node *node)
93 struct callchain_list *chain;
94 bool unfolded = false;
97 list_for_each_entry(chain, &node->val, list) {
99 unfolded = chain->ms.unfolded;
103 n += callchain_node__count_rows_rb_tree(node);
108 static int callchain__count_rows(struct rb_root *chain)
113 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
114 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
115 n += callchain_node__count_rows(node);
121 static bool map_symbol__toggle_fold(struct map_symbol *self)
123 if (!self->has_children)
126 self->unfolded = !self->unfolded;
130 static void callchain_node__init_have_children_rb_tree(struct callchain_node *self)
132 struct rb_node *nd = rb_first(&self->rb_root);
134 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
135 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
136 struct callchain_list *chain;
139 list_for_each_entry(chain, &child->val, list) {
142 chain->ms.has_children = chain->list.next != &child->val ||
143 !RB_EMPTY_ROOT(&child->rb_root);
145 chain->ms.has_children = chain->list.next == &child->val &&
146 !RB_EMPTY_ROOT(&child->rb_root);
149 callchain_node__init_have_children_rb_tree(child);
153 static void callchain_node__init_have_children(struct callchain_node *self)
155 struct callchain_list *chain;
157 list_for_each_entry(chain, &self->val, list)
158 chain->ms.has_children = !RB_EMPTY_ROOT(&self->rb_root);
160 callchain_node__init_have_children_rb_tree(self);
163 static void callchain__init_have_children(struct rb_root *self)
167 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
168 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
169 callchain_node__init_have_children(node);
173 static void hist_entry__init_have_children(struct hist_entry *self)
175 if (!self->init_have_children) {
176 self->ms.has_children = !RB_EMPTY_ROOT(&self->sorted_chain);
177 callchain__init_have_children(&self->sorted_chain);
178 self->init_have_children = true;
182 static bool hist_browser__toggle_fold(struct hist_browser *self)
184 if (map_symbol__toggle_fold(self->selection)) {
185 struct hist_entry *he = self->he_selection;
187 hist_entry__init_have_children(he);
188 self->hists->nr_entries -= he->nr_rows;
191 he->nr_rows = callchain__count_rows(&he->sorted_chain);
194 self->hists->nr_entries += he->nr_rows;
195 self->b.nr_entries = self->hists->nr_entries;
200 /* If it doesn't have children, no toggling performed */
204 static int callchain_node__set_folding_rb_tree(struct callchain_node *self, bool unfold)
209 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
210 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
211 struct callchain_list *chain;
212 bool has_children = false;
214 list_for_each_entry(chain, &child->val, list) {
216 map_symbol__set_folding(&chain->ms, unfold);
217 has_children = chain->ms.has_children;
221 n += callchain_node__set_folding_rb_tree(child, unfold);
227 static int callchain_node__set_folding(struct callchain_node *node, bool unfold)
229 struct callchain_list *chain;
230 bool has_children = false;
233 list_for_each_entry(chain, &node->val, list) {
235 map_symbol__set_folding(&chain->ms, unfold);
236 has_children = chain->ms.has_children;
240 n += callchain_node__set_folding_rb_tree(node, unfold);
245 static int callchain__set_folding(struct rb_root *chain, bool unfold)
250 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
251 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
252 n += callchain_node__set_folding(node, unfold);
258 static void hist_entry__set_folding(struct hist_entry *self, bool unfold)
260 hist_entry__init_have_children(self);
261 map_symbol__set_folding(&self->ms, unfold);
263 if (self->ms.has_children) {
264 int n = callchain__set_folding(&self->sorted_chain, unfold);
265 self->nr_rows = unfold ? n : 0;
270 static void hists__set_folding(struct hists *self, bool unfold)
274 self->nr_entries = 0;
276 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
277 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
278 hist_entry__set_folding(he, unfold);
279 self->nr_entries += 1 + he->nr_rows;
283 static void hist_browser__set_folding(struct hist_browser *self, bool unfold)
285 hists__set_folding(self->hists, unfold);
286 self->b.nr_entries = self->hists->nr_entries;
287 /* Go to the start, we may be way after valid entries after a collapse */
288 ui_browser__reset_index(&self->b);
291 static int hist_browser__run(struct hist_browser *self, const char *title)
294 int exit_keys[] = { 'a', '?', 'h', 'C', 'd', 'D', 'E', 't',
295 NEWT_KEY_ENTER, NEWT_KEY_RIGHT, NEWT_KEY_LEFT, 0, };
297 unsigned long nr_events = self->hists->stats.nr_events[PERF_RECORD_SAMPLE];
299 self->b.entries = &self->hists->entries;
300 self->b.nr_entries = self->hists->nr_entries;
302 hist_browser__refresh_dimensions(self);
304 nr_events = convert_unit(nr_events, &unit);
305 snprintf(str, sizeof(str), "Events: %lu%c ",
307 newtDrawRootText(0, 0, str);
309 if (ui_browser__show(&self->b, title,
310 "Press '?' for help on key bindings") < 0)
313 ui_browser__add_exit_keys(&self->b, exit_keys);
316 key = ui_browser__run(&self->b);
319 case 'D': { /* Debug */
321 struct hist_entry *h = rb_entry(self->b.top,
322 struct hist_entry, rb_node);
324 ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
325 seq++, self->b.nr_entries,
326 self->hists->nr_entries,
330 h->row_offset, h->nr_rows);
334 /* Collapse the whole world. */
335 hist_browser__set_folding(self, false);
338 /* Expand the whole world. */
339 hist_browser__set_folding(self, true);
342 if (hist_browser__toggle_fold(self))
350 ui_browser__hide(&self->b);
354 static char *callchain_list__sym_name(struct callchain_list *self,
355 char *bf, size_t bfsize)
358 return self->ms.sym->name;
360 snprintf(bf, bfsize, "%#Lx", self->ip);
364 #define LEVEL_OFFSET_STEP 3
366 static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self,
367 struct callchain_node *chain_node,
368 u64 total, int level,
371 bool *is_current_entry)
373 struct rb_node *node;
374 int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
375 u64 new_total, remaining;
377 if (callchain_param.mode == CHAIN_GRAPH_REL)
378 new_total = chain_node->children_hit;
382 remaining = new_total;
383 node = rb_first(&chain_node->rb_root);
385 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
386 struct rb_node *next = rb_next(node);
387 u64 cumul = cumul_hits(child);
388 struct callchain_list *chain;
389 char folded_sign = ' ';
391 int extra_offset = 0;
395 list_for_each_entry(chain, &child->val, list) {
396 char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str;
399 bool was_first = first;
404 extra_offset = LEVEL_OFFSET_STEP;
406 folded_sign = callchain_list__folded(chain);
407 if (*row_offset != 0) {
413 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
415 double percent = cumul * 100.0 / new_total;
417 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
418 str = "Not enough memory!";
423 color = HE_COLORSET_NORMAL;
424 width = self->b.width - (offset + extra_offset + 2);
425 if (ui_browser__is_current_entry(&self->b, row)) {
426 self->selection = &chain->ms;
427 color = HE_COLORSET_SELECTED;
428 *is_current_entry = true;
431 ui_browser__set_color(&self->b, color);
432 ui_browser__gotorc(&self->b, row, 0);
433 slsmg_write_nstring(" ", offset + extra_offset);
434 slsmg_printf("%c ", folded_sign);
435 slsmg_write_nstring(str, width);
438 if (++row == self->b.height)
441 if (folded_sign == '+')
445 if (folded_sign == '-') {
446 const int new_level = level + (extra_offset ? 2 : 1);
447 row += hist_browser__show_callchain_node_rb_tree(self, child, new_total,
448 new_level, row, row_offset,
451 if (row == self->b.height)
456 return row - first_row;
459 static int hist_browser__show_callchain_node(struct hist_browser *self,
460 struct callchain_node *node,
461 int level, unsigned short row,
463 bool *is_current_entry)
465 struct callchain_list *chain;
467 offset = level * LEVEL_OFFSET_STEP,
468 width = self->b.width - offset;
469 char folded_sign = ' ';
471 list_for_each_entry(chain, &node->val, list) {
472 char ipstr[BITS_PER_LONG / 4 + 1], *s;
475 folded_sign = callchain_list__folded(chain);
477 if (*row_offset != 0) {
482 color = HE_COLORSET_NORMAL;
483 if (ui_browser__is_current_entry(&self->b, row)) {
484 self->selection = &chain->ms;
485 color = HE_COLORSET_SELECTED;
486 *is_current_entry = true;
489 s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
490 ui_browser__gotorc(&self->b, row, 0);
491 ui_browser__set_color(&self->b, color);
492 slsmg_write_nstring(" ", offset);
493 slsmg_printf("%c ", folded_sign);
494 slsmg_write_nstring(s, width - 2);
496 if (++row == self->b.height)
500 if (folded_sign == '-')
501 row += hist_browser__show_callchain_node_rb_tree(self, node,
502 self->hists->stats.total_period,
507 return row - first_row;
510 static int hist_browser__show_callchain(struct hist_browser *self,
511 struct rb_root *chain,
512 int level, unsigned short row,
514 bool *is_current_entry)
519 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
520 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
522 row += hist_browser__show_callchain_node(self, node, level,
525 if (row == self->b.height)
529 return row - first_row;
532 static int hist_browser__show_entry(struct hist_browser *self,
533 struct hist_entry *entry,
539 int color, width = self->b.width;
540 char folded_sign = ' ';
541 bool current_entry = ui_browser__is_current_entry(&self->b, row);
542 off_t row_offset = entry->row_offset;
545 self->he_selection = entry;
546 self->selection = &entry->ms;
549 if (symbol_conf.use_callchain) {
550 hist_entry__init_have_children(entry);
551 folded_sign = hist_entry__folded(entry);
554 if (row_offset == 0) {
555 hist_entry__snprintf(entry, s, sizeof(s), self->hists, NULL, false,
556 0, false, self->hists->stats.total_period);
557 percent = (entry->period * 100.0) / self->hists->stats.total_period;
559 color = HE_COLORSET_SELECTED;
560 if (!current_entry) {
561 if (percent >= MIN_RED)
562 color = HE_COLORSET_TOP;
563 else if (percent >= MIN_GREEN)
564 color = HE_COLORSET_MEDIUM;
566 color = HE_COLORSET_NORMAL;
569 ui_browser__set_color(&self->b, color);
570 ui_browser__gotorc(&self->b, row, 0);
571 if (symbol_conf.use_callchain) {
572 slsmg_printf("%c ", folded_sign);
575 slsmg_write_nstring(s, width);
581 if (folded_sign == '-' && row != self->b.height) {
582 printed += hist_browser__show_callchain(self, &entry->sorted_chain,
586 self->he_selection = entry;
592 static unsigned int hist_browser__refresh(struct ui_browser *self)
596 struct hist_browser *hb = container_of(self, struct hist_browser, b);
598 if (self->top == NULL)
599 self->top = rb_first(&hb->hists->entries);
601 for (nd = self->top; nd; nd = rb_next(nd)) {
602 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
607 row += hist_browser__show_entry(hb, h, row);
608 if (row == self->height)
615 static struct rb_node *hists__filter_entries(struct rb_node *nd)
618 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
628 static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
631 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
641 static void ui_browser__hists_seek(struct ui_browser *self,
642 off_t offset, int whence)
644 struct hist_entry *h;
650 nd = hists__filter_entries(rb_first(self->entries));
656 nd = hists__filter_prev_entries(rb_last(self->entries));
664 * Moves not relative to the first visible entry invalidates its
667 h = rb_entry(self->top, struct hist_entry, rb_node);
671 * Here we have to check if nd is expanded (+), if it is we can't go
672 * the next top level hist_entry, instead we must compute an offset of
673 * what _not_ to show and not change the first visible entry.
675 * This offset increments when we are going from top to bottom and
676 * decreases when we're going from bottom to top.
678 * As we don't have backpointers to the top level in the callchains
679 * structure, we need to always print the whole hist_entry callchain,
680 * skipping the first ones that are before the first visible entry
681 * and stop when we printed enough lines to fill the screen.
686 h = rb_entry(nd, struct hist_entry, rb_node);
687 if (h->ms.unfolded) {
688 u16 remaining = h->nr_rows - h->row_offset;
689 if (offset > remaining) {
693 h->row_offset += offset;
699 nd = hists__filter_entries(rb_next(nd));
704 } while (offset != 0);
705 } else if (offset < 0) {
707 h = rb_entry(nd, struct hist_entry, rb_node);
708 if (h->ms.unfolded) {
710 if (-offset > h->row_offset) {
711 offset += h->row_offset;
714 h->row_offset += offset;
720 if (-offset > h->nr_rows) {
721 offset += h->nr_rows;
724 h->row_offset = h->nr_rows + offset;
732 nd = hists__filter_prev_entries(rb_prev(nd));
739 * Last unfiltered hist_entry, check if it is
740 * unfolded, if it is then we should have
741 * row_offset at its last entry.
743 h = rb_entry(nd, struct hist_entry, rb_node);
745 h->row_offset = h->nr_rows;
752 h = rb_entry(nd, struct hist_entry, rb_node);
757 static struct hist_browser *hist_browser__new(struct hists *hists)
759 struct hist_browser *self = zalloc(sizeof(*self));
763 self->b.refresh = hist_browser__refresh;
764 self->b.seek = ui_browser__hists_seek;
770 static void hist_browser__delete(struct hist_browser *self)
775 static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
777 return self->he_selection;
780 static struct thread *hist_browser__selected_thread(struct hist_browser *self)
782 return self->he_selection->thread;
785 static int hist_browser__title(char *bf, size_t size, const char *ev_name,
786 const struct dso *dso, const struct thread *thread)
791 printed += snprintf(bf + printed, size - printed,
793 (thread->comm_set ? thread->comm : ""),
796 printed += snprintf(bf + printed, size - printed,
797 "%sDSO: %s", thread ? " " : "",
799 return printed ?: snprintf(bf, size, "Event: %s", ev_name);
802 int hists__browse(struct hists *self, const char *helpline, const char *ev_name)
804 struct hist_browser *browser = hist_browser__new(self);
805 struct pstack *fstack;
806 const struct thread *thread_filter = NULL;
807 const struct dso *dso_filter = NULL;
814 fstack = pstack__new(2);
818 ui_helpline__push(helpline);
820 hist_browser__title(msg, sizeof(msg), ev_name,
821 dso_filter, thread_filter);
824 const struct thread *thread;
825 const struct dso *dso;
827 int nr_options = 0, choice = 0, i,
828 annotate = -2, zoom_dso = -2, zoom_thread = -2,
831 key = hist_browser__run(browser, msg);
833 thread = hist_browser__selected_thread(browser);
834 dso = browser->selection->map ? browser->selection->map->dso : NULL;
840 * Exit the browser, let hists__browser_tree
841 * go to the next or previous
845 if (browser->selection->map == NULL &&
846 browser->selection->map->dso->annotate_warned)
856 ui__help_window("-> Zoom into DSO/Threads & Annotate current symbol\n"
858 "a Annotate current symbol\n"
859 "h/?/F1 Show this window\n"
860 "C Collapse all callchains\n"
861 "E Expand all callchains\n"
862 "d Zoom into current DSO\n"
863 "t Zoom into current Thread\n"
864 "q/CTRL+C Exit browser");
870 case NEWT_KEY_LEFT: {
873 if (pstack__empty(fstack))
875 top = pstack__pop(fstack);
876 if (top == &dso_filter)
878 if (top == &thread_filter)
879 goto zoom_out_thread;
882 case NEWT_KEY_ESCAPE:
883 if (!ui__dialog_yesno("Do you really want to exit?"))
890 if (browser->selection->sym != NULL &&
891 !browser->selection->map->dso->annotate_warned &&
892 asprintf(&options[nr_options], "Annotate %s",
893 browser->selection->sym->name) > 0)
894 annotate = nr_options++;
896 if (thread != NULL &&
897 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
898 (thread_filter ? "out of" : "into"),
899 (thread->comm_set ? thread->comm : ""),
901 zoom_thread = nr_options++;
904 asprintf(&options[nr_options], "Zoom %s %s DSO",
905 (dso_filter ? "out of" : "into"),
906 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
907 zoom_dso = nr_options++;
909 if (browser->selection->map != NULL &&
910 asprintf(&options[nr_options], "Browse map details") > 0)
911 browse_map = nr_options++;
913 options[nr_options++] = (char *)"Exit";
915 choice = ui__popup_menu(nr_options, options);
917 for (i = 0; i < nr_options - 1; ++i)
920 if (choice == nr_options - 1)
926 if (choice == annotate) {
927 struct hist_entry *he;
929 if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
930 browser->selection->map->dso->annotate_warned = 1;
931 ui_helpline__puts("No vmlinux file found, can't "
932 "annotate with just a "
937 he = hist_browser__selected_entry(browser);
941 hist_entry__tui_annotate(he);
942 } else if (choice == browse_map)
943 map__browse(browser->selection->map);
944 else if (choice == zoom_dso) {
947 pstack__remove(fstack, &dso_filter);
954 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
955 dso->kernel ? "the Kernel" : dso->short_name);
957 pstack__push(fstack, &dso_filter);
959 hists__filter_by_dso(self, dso_filter);
960 hist_browser__title(msg, sizeof(msg), ev_name,
961 dso_filter, thread_filter);
962 hist_browser__reset(browser);
963 } else if (choice == zoom_thread) {
966 pstack__remove(fstack, &thread_filter);
969 thread_filter = NULL;
971 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
972 thread->comm_set ? thread->comm : "",
974 thread_filter = thread;
975 pstack__push(fstack, &thread_filter);
977 hists__filter_by_thread(self, thread_filter);
978 hist_browser__title(msg, sizeof(msg), ev_name,
979 dso_filter, thread_filter);
980 hist_browser__reset(browser);
984 pstack__delete(fstack);
986 hist_browser__delete(browser);
990 int hists__tui_browse_tree(struct rb_root *self, const char *help)
992 struct rb_node *first = rb_first(self), *nd = first, *next;
996 struct hists *hists = rb_entry(nd, struct hists, rb_node);
997 const char *ev_name = __event_name(hists->type, hists->config);
999 key = hists__browse(hists, help, ev_name);
1006 case NEWT_KEY_UNTAB: