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 int callchain_node__count_rows_rb_tree(struct callchain_node *self)
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 */
71 list_for_each_entry(chain, &child->val, list) {
73 /* We need this because we may not have children */
74 folded_sign = callchain_list__folded(chain);
75 if (folded_sign == '+')
79 if (folded_sign == '-') /* Have children and they're unfolded */
80 n += callchain_node__count_rows_rb_tree(child);
86 static int callchain_node__count_rows(struct callchain_node *node)
88 struct callchain_list *chain;
89 bool unfolded = false;
92 list_for_each_entry(chain, &node->val, list) {
94 unfolded = chain->ms.unfolded;
98 n += callchain_node__count_rows_rb_tree(node);
103 static int callchain__count_rows(struct rb_root *chain)
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);
116 static bool map_symbol__toggle_fold(struct map_symbol *self)
118 if (!self->has_children)
121 self->unfolded = !self->unfolded;
125 static void callchain_node__init_have_children_rb_tree(struct callchain_node *self)
127 struct rb_node *nd = rb_first(&self->rb_root);
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;
134 list_for_each_entry(chain, &child->val, list) {
137 chain->ms.has_children = chain->list.next != &child->val ||
138 !RB_EMPTY_ROOT(&child->rb_root);
140 chain->ms.has_children = chain->list.next == &child->val &&
141 !RB_EMPTY_ROOT(&child->rb_root);
144 callchain_node__init_have_children_rb_tree(child);
148 static void callchain_node__init_have_children(struct callchain_node *self)
150 struct callchain_list *chain;
152 list_for_each_entry(chain, &self->val, list)
153 chain->ms.has_children = !RB_EMPTY_ROOT(&self->rb_root);
155 callchain_node__init_have_children_rb_tree(self);
158 static void callchain__init_have_children(struct rb_root *self)
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);
168 static void hist_entry__init_have_children(struct hist_entry *self)
170 if (!self->init_have_children) {
171 self->ms.has_children = !RB_EMPTY_ROOT(&self->sorted_chain);
172 callchain__init_have_children(&self->sorted_chain);
173 self->init_have_children = true;
177 static bool hist_browser__toggle_fold(struct hist_browser *self)
179 if (map_symbol__toggle_fold(self->selection)) {
180 struct hist_entry *he = self->he_selection;
182 hist_entry__init_have_children(he);
183 self->hists->nr_entries -= he->nr_rows;
186 he->nr_rows = callchain__count_rows(&he->sorted_chain);
189 self->hists->nr_entries += he->nr_rows;
190 self->b.nr_entries = self->hists->nr_entries;
195 /* If it doesn't have children, no toggling performed */
199 static int hist_browser__run(struct hist_browser *self, const char *title)
202 int exit_keys[] = { 'a', '?', 'h', 'd', 'D', 't', NEWT_KEY_ENTER,
203 NEWT_KEY_RIGHT, NEWT_KEY_LEFT, 0, };
205 unsigned long nr_events = self->hists->stats.nr_events[PERF_RECORD_SAMPLE];
207 self->b.entries = &self->hists->entries;
208 self->b.nr_entries = self->hists->nr_entries;
210 hist_browser__refresh_dimensions(self);
212 nr_events = convert_unit(nr_events, &unit);
213 snprintf(str, sizeof(str), "Events: %lu%c ",
215 newtDrawRootText(0, 0, str);
217 if (ui_browser__show(&self->b, title,
218 "Press '?' for help on key bindings") < 0)
221 ui_browser__add_exit_keys(&self->b, exit_keys);
224 key = ui_browser__run(&self->b);
227 case 'D': { /* Debug */
229 struct hist_entry *h = rb_entry(self->b.top,
230 struct hist_entry, rb_node);
232 ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
233 seq++, self->b.nr_entries,
234 self->hists->nr_entries,
238 h->row_offset, h->nr_rows);
242 if (hist_browser__toggle_fold(self))
250 ui_browser__hide(&self->b);
254 static char *callchain_list__sym_name(struct callchain_list *self,
255 char *bf, size_t bfsize)
258 return self->ms.sym->name;
260 snprintf(bf, bfsize, "%#Lx", self->ip);
264 #define LEVEL_OFFSET_STEP 3
266 static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self,
267 struct callchain_node *chain_node,
268 u64 total, int level,
271 bool *is_current_entry)
273 struct rb_node *node;
274 int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
275 u64 new_total, remaining;
277 if (callchain_param.mode == CHAIN_GRAPH_REL)
278 new_total = chain_node->children_hit;
282 remaining = new_total;
283 node = rb_first(&chain_node->rb_root);
285 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
286 struct rb_node *next = rb_next(node);
287 u64 cumul = cumul_hits(child);
288 struct callchain_list *chain;
289 char folded_sign = ' ';
291 int extra_offset = 0;
295 list_for_each_entry(chain, &child->val, list) {
296 char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str;
299 bool was_first = first;
304 extra_offset = LEVEL_OFFSET_STEP;
306 folded_sign = callchain_list__folded(chain);
307 if (*row_offset != 0) {
313 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
315 double percent = cumul * 100.0 / new_total;
317 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
318 str = "Not enough memory!";
323 color = HE_COLORSET_NORMAL;
324 width = self->b.width - (offset + extra_offset + 2);
325 if (ui_browser__is_current_entry(&self->b, row)) {
326 self->selection = &chain->ms;
327 color = HE_COLORSET_SELECTED;
328 *is_current_entry = true;
331 ui_browser__set_color(&self->b, color);
332 ui_browser__gotorc(&self->b, row, 0);
333 slsmg_write_nstring(" ", offset + extra_offset);
334 slsmg_printf("%c ", folded_sign);
335 slsmg_write_nstring(str, width);
338 if (++row == self->b.height)
341 if (folded_sign == '+')
345 if (folded_sign == '-') {
346 const int new_level = level + (extra_offset ? 2 : 1);
347 row += hist_browser__show_callchain_node_rb_tree(self, child, new_total,
348 new_level, row, row_offset,
351 if (row == self->b.height)
356 return row - first_row;
359 static int hist_browser__show_callchain_node(struct hist_browser *self,
360 struct callchain_node *node,
361 int level, unsigned short row,
363 bool *is_current_entry)
365 struct callchain_list *chain;
367 offset = level * LEVEL_OFFSET_STEP,
368 width = self->b.width - offset;
369 char folded_sign = ' ';
371 list_for_each_entry(chain, &node->val, list) {
372 char ipstr[BITS_PER_LONG / 4 + 1], *s;
375 folded_sign = callchain_list__folded(chain);
377 if (*row_offset != 0) {
382 color = HE_COLORSET_NORMAL;
383 if (ui_browser__is_current_entry(&self->b, row)) {
384 self->selection = &chain->ms;
385 color = HE_COLORSET_SELECTED;
386 *is_current_entry = true;
389 s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
390 ui_browser__gotorc(&self->b, row, 0);
391 ui_browser__set_color(&self->b, color);
392 slsmg_write_nstring(" ", offset);
393 slsmg_printf("%c ", folded_sign);
394 slsmg_write_nstring(s, width - 2);
396 if (++row == self->b.height)
400 if (folded_sign == '-')
401 row += hist_browser__show_callchain_node_rb_tree(self, node,
402 self->hists->stats.total_period,
407 return row - first_row;
410 static int hist_browser__show_callchain(struct hist_browser *self,
411 struct rb_root *chain,
412 int level, unsigned short row,
414 bool *is_current_entry)
419 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
420 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
422 row += hist_browser__show_callchain_node(self, node, level,
425 if (row == self->b.height)
429 return row - first_row;
432 static int hist_browser__show_entry(struct hist_browser *self,
433 struct hist_entry *entry,
439 int color, width = self->b.width;
440 char folded_sign = ' ';
441 bool current_entry = ui_browser__is_current_entry(&self->b, row);
442 off_t row_offset = entry->row_offset;
445 self->he_selection = entry;
446 self->selection = &entry->ms;
449 if (symbol_conf.use_callchain) {
450 hist_entry__init_have_children(entry);
451 folded_sign = hist_entry__folded(entry);
454 if (row_offset == 0) {
455 hist_entry__snprintf(entry, s, sizeof(s), self->hists, NULL, false,
456 0, false, self->hists->stats.total_period);
457 percent = (entry->period * 100.0) / self->hists->stats.total_period;
459 color = HE_COLORSET_SELECTED;
460 if (!current_entry) {
461 if (percent >= MIN_RED)
462 color = HE_COLORSET_TOP;
463 else if (percent >= MIN_GREEN)
464 color = HE_COLORSET_MEDIUM;
466 color = HE_COLORSET_NORMAL;
469 ui_browser__set_color(&self->b, color);
470 ui_browser__gotorc(&self->b, row, 0);
471 if (symbol_conf.use_callchain) {
472 slsmg_printf("%c ", folded_sign);
475 slsmg_write_nstring(s, width);
481 if (folded_sign == '-' && row != self->b.height) {
482 printed += hist_browser__show_callchain(self, &entry->sorted_chain,
486 self->he_selection = entry;
492 static unsigned int hist_browser__refresh(struct ui_browser *self)
496 struct hist_browser *hb = container_of(self, struct hist_browser, b);
498 if (self->top == NULL)
499 self->top = rb_first(&hb->hists->entries);
501 for (nd = self->top; nd; nd = rb_next(nd)) {
502 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
507 row += hist_browser__show_entry(hb, h, row);
508 if (row == self->height)
515 static struct rb_node *hists__filter_entries(struct rb_node *nd)
518 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
528 static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
531 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
541 static void ui_browser__hists_seek(struct ui_browser *self,
542 off_t offset, int whence)
544 struct hist_entry *h;
550 nd = hists__filter_entries(rb_first(self->entries));
556 nd = hists__filter_prev_entries(rb_last(self->entries));
564 * Moves not relative to the first visible entry invalidates its
567 h = rb_entry(self->top, struct hist_entry, rb_node);
571 * Here we have to check if nd is expanded (+), if it is we can't go
572 * the next top level hist_entry, instead we must compute an offset of
573 * what _not_ to show and not change the first visible entry.
575 * This offset increments when we are going from top to bottom and
576 * decreases when we're going from bottom to top.
578 * As we don't have backpointers to the top level in the callchains
579 * structure, we need to always print the whole hist_entry callchain,
580 * skipping the first ones that are before the first visible entry
581 * and stop when we printed enough lines to fill the screen.
586 h = rb_entry(nd, struct hist_entry, rb_node);
587 if (h->ms.unfolded) {
588 u16 remaining = h->nr_rows - h->row_offset;
589 if (offset > remaining) {
593 h->row_offset += offset;
599 nd = hists__filter_entries(rb_next(nd));
604 } while (offset != 0);
605 } else if (offset < 0) {
607 h = rb_entry(nd, struct hist_entry, rb_node);
608 if (h->ms.unfolded) {
610 if (-offset > h->row_offset) {
611 offset += h->row_offset;
614 h->row_offset += offset;
620 if (-offset > h->nr_rows) {
621 offset += h->nr_rows;
624 h->row_offset = h->nr_rows + offset;
632 nd = hists__filter_prev_entries(rb_prev(nd));
639 * Last unfiltered hist_entry, check if it is
640 * unfolded, if it is then we should have
641 * row_offset at its last entry.
643 h = rb_entry(nd, struct hist_entry, rb_node);
645 h->row_offset = h->nr_rows;
652 h = rb_entry(nd, struct hist_entry, rb_node);
657 static struct hist_browser *hist_browser__new(struct hists *hists)
659 struct hist_browser *self = zalloc(sizeof(*self));
663 self->b.refresh = hist_browser__refresh;
664 self->b.seek = ui_browser__hists_seek;
670 static void hist_browser__delete(struct hist_browser *self)
675 static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
677 return self->he_selection;
680 static struct thread *hist_browser__selected_thread(struct hist_browser *self)
682 return self->he_selection->thread;
685 static int hist_browser__title(char *bf, size_t size, const char *ev_name,
686 const struct dso *dso, const struct thread *thread)
691 printed += snprintf(bf + printed, size - printed,
693 (thread->comm_set ? thread->comm : ""),
696 printed += snprintf(bf + printed, size - printed,
697 "%sDSO: %s", thread ? " " : "",
699 return printed ?: snprintf(bf, size, "Event: %s", ev_name);
702 int hists__browse(struct hists *self, const char *helpline, const char *ev_name)
704 struct hist_browser *browser = hist_browser__new(self);
705 struct pstack *fstack;
706 const struct thread *thread_filter = NULL;
707 const struct dso *dso_filter = NULL;
714 fstack = pstack__new(2);
718 ui_helpline__push(helpline);
720 hist_browser__title(msg, sizeof(msg), ev_name,
721 dso_filter, thread_filter);
724 const struct thread *thread;
725 const struct dso *dso;
727 int nr_options = 0, choice = 0, i,
728 annotate = -2, zoom_dso = -2, zoom_thread = -2,
731 key = hist_browser__run(browser, msg);
733 thread = hist_browser__selected_thread(browser);
734 dso = browser->selection->map ? browser->selection->map->dso : NULL;
742 * Exit the browser, let hists__browser_tree
743 * go to the next or previous
747 if (browser->selection->map == NULL &&
748 browser->selection->map->dso->annotate_warned)
758 ui__help_window("-> Zoom into DSO/Threads & Annotate current symbol\n"
760 "a Annotate current symbol\n"
761 "h/?/F1 Show this window\n"
762 "d Zoom into current DSO\n"
763 "t Zoom into current Thread\n"
764 "q/CTRL+C Exit browser");
770 case NEWT_KEY_LEFT: {
773 if (pstack__empty(fstack))
775 top = pstack__pop(fstack);
776 if (top == &dso_filter)
778 if (top == &thread_filter)
779 goto zoom_out_thread;
782 case NEWT_KEY_ESCAPE:
783 if (!ui__dialog_yesno("Do you really want to exit?"))
790 if (browser->selection->sym != NULL &&
791 !browser->selection->map->dso->annotate_warned &&
792 asprintf(&options[nr_options], "Annotate %s",
793 browser->selection->sym->name) > 0)
794 annotate = nr_options++;
796 if (thread != NULL &&
797 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
798 (thread_filter ? "out of" : "into"),
799 (thread->comm_set ? thread->comm : ""),
801 zoom_thread = nr_options++;
804 asprintf(&options[nr_options], "Zoom %s %s DSO",
805 (dso_filter ? "out of" : "into"),
806 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
807 zoom_dso = nr_options++;
809 if (browser->selection->map != NULL &&
810 asprintf(&options[nr_options], "Browse map details") > 0)
811 browse_map = nr_options++;
813 options[nr_options++] = (char *)"Exit";
815 choice = ui__popup_menu(nr_options, options);
817 for (i = 0; i < nr_options - 1; ++i)
820 if (choice == nr_options - 1)
826 if (choice == annotate) {
827 struct hist_entry *he;
829 if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
830 browser->selection->map->dso->annotate_warned = 1;
831 ui_helpline__puts("No vmlinux file found, can't "
832 "annotate with just a "
837 he = hist_browser__selected_entry(browser);
841 hist_entry__tui_annotate(he);
842 } else if (choice == browse_map)
843 map__browse(browser->selection->map);
844 else if (choice == zoom_dso) {
847 pstack__remove(fstack, &dso_filter);
854 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
855 dso->kernel ? "the Kernel" : dso->short_name);
857 pstack__push(fstack, &dso_filter);
859 hists__filter_by_dso(self, dso_filter);
860 hist_browser__title(msg, sizeof(msg), ev_name,
861 dso_filter, thread_filter);
862 hist_browser__reset(browser);
863 } else if (choice == zoom_thread) {
866 pstack__remove(fstack, &thread_filter);
869 thread_filter = NULL;
871 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
872 thread->comm_set ? thread->comm : "",
874 thread_filter = thread;
875 pstack__push(fstack, &thread_filter);
877 hists__filter_by_thread(self, thread_filter);
878 hist_browser__title(msg, sizeof(msg), ev_name,
879 dso_filter, thread_filter);
880 hist_browser__reset(browser);
884 pstack__delete(fstack);
886 hist_browser__delete(browser);
890 int hists__tui_browse_tree(struct rb_root *self, const char *help)
892 struct rb_node *first = rb_first(self), *nd = first, *next;
896 struct hists *hists = rb_entry(nd, struct hists, rb_node);
897 const char *ev_name = __event_name(hists->type, hists->config);
899 key = hists__browse(hists, help, ev_name);