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_first(&child->rb_root) != NULL;
140 chain->ms.has_children = chain->list.next == &child->val &&
141 rb_first(&child->rb_root) != NULL;
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_first(&self->rb_root) != NULL;
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 callchain__init_have_children(&self->sorted_chain);
172 self->init_have_children = true;
176 static bool hist_browser__toggle_fold(struct hist_browser *self)
178 if (map_symbol__toggle_fold(self->selection)) {
179 struct hist_entry *he = self->he_selection;
181 hist_entry__init_have_children(he);
182 self->hists->nr_entries -= he->nr_rows;
185 he->nr_rows = callchain__count_rows(&he->sorted_chain);
188 self->hists->nr_entries += he->nr_rows;
189 self->b.nr_entries = self->hists->nr_entries;
194 /* If it doesn't have children, no toggling performed */
198 static int hist_browser__run(struct hist_browser *self, const char *title,
199 struct newtExitStruct *es)
202 unsigned long nr_events = self->hists->stats.nr_events[PERF_RECORD_SAMPLE];
204 self->b.entries = &self->hists->entries;
205 self->b.nr_entries = self->hists->nr_entries;
207 hist_browser__refresh_dimensions(self);
209 nr_events = convert_unit(nr_events, &unit);
210 snprintf(str, sizeof(str), "Events: %lu%c ",
212 newtDrawRootText(0, 0, str);
214 if (ui_browser__show(&self->b, title,
215 "Press '?' for help on key bindings") < 0)
218 newtFormAddHotKey(self->b.form, 'a');
219 newtFormAddHotKey(self->b.form, '?');
220 newtFormAddHotKey(self->b.form, 'h');
221 newtFormAddHotKey(self->b.form, 'd');
222 newtFormAddHotKey(self->b.form, 'D');
223 newtFormAddHotKey(self->b.form, 't');
225 newtFormAddHotKey(self->b.form, NEWT_KEY_LEFT);
226 newtFormAddHotKey(self->b.form, NEWT_KEY_RIGHT);
227 newtFormAddHotKey(self->b.form, NEWT_KEY_ENTER);
230 ui_browser__run(&self->b, es);
232 if (es->reason != NEWT_EXIT_HOTKEY)
235 case 'D': { /* Debug */
237 struct hist_entry *h = rb_entry(self->b.top,
238 struct hist_entry, rb_node);
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,
246 h->row_offset, h->nr_rows);
250 if (hist_browser__toggle_fold(self))
258 ui_browser__hide(&self->b);
262 static char *callchain_list__sym_name(struct callchain_list *self,
263 char *bf, size_t bfsize)
266 return self->ms.sym->name;
268 snprintf(bf, bfsize, "%#Lx", self->ip);
272 #define LEVEL_OFFSET_STEP 3
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,
279 bool *is_current_entry)
281 struct rb_node *node;
282 int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
283 u64 new_total, remaining;
285 if (callchain_param.mode == CHAIN_GRAPH_REL)
286 new_total = chain_node->children_hit;
290 remaining = new_total;
291 node = rb_first(&chain_node->rb_root);
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 = ' ';
299 int extra_offset = 0;
303 list_for_each_entry(chain, &child->val, list) {
304 char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str;
307 bool was_first = first;
311 chain->ms.has_children = chain->list.next != &child->val ||
312 rb_first(&child->rb_root) != NULL;
314 extra_offset = LEVEL_OFFSET_STEP;
315 chain->ms.has_children = chain->list.next == &child->val &&
316 rb_first(&child->rb_root) != NULL;
319 folded_sign = callchain_list__folded(chain);
320 if (*row_offset != 0) {
326 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
328 double percent = cumul * 100.0 / new_total;
330 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
331 str = "Not enough memory!";
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;
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);
351 if (++row == self->b.height)
354 if (folded_sign == '+')
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,
364 if (row == self->b.height)
369 return row - first_row;
372 static int hist_browser__show_callchain_node(struct hist_browser *self,
373 struct callchain_node *node,
374 int level, unsigned short row,
376 bool *is_current_entry)
378 struct callchain_list *chain;
380 offset = level * LEVEL_OFFSET_STEP,
381 width = self->b.width - offset;
382 char folded_sign = ' ';
384 list_for_each_entry(chain, &node->val, list) {
385 char ipstr[BITS_PER_LONG / 4 + 1], *s;
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
392 chain->ms.has_children = rb_first(&node->rb_root) != NULL;
393 folded_sign = callchain_list__folded(chain);
395 if (*row_offset != 0) {
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;
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);
414 if (++row == self->b.height)
418 if (folded_sign == '-')
419 row += hist_browser__show_callchain_node_rb_tree(self, node,
420 self->hists->stats.total_period,
425 return row - first_row;
428 static int hist_browser__show_callchain(struct hist_browser *self,
429 struct rb_root *chain,
430 int level, unsigned short row,
432 bool *is_current_entry)
437 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
438 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
440 row += hist_browser__show_callchain_node(self, node, level,
443 if (row == self->b.height)
447 return row - first_row;
450 static int hist_browser__show_entry(struct hist_browser *self,
451 struct hist_entry *entry,
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;
463 self->he_selection = entry;
464 self->selection = &entry->ms;
467 if (symbol_conf.use_callchain) {
468 entry->ms.has_children = !RB_EMPTY_ROOT(&entry->sorted_chain);
469 folded_sign = hist_entry__folded(entry);
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;
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;
484 color = HE_COLORSET_NORMAL;
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);
493 slsmg_write_nstring(s, width);
499 if (folded_sign == '-' && row != self->b.height) {
500 printed += hist_browser__show_callchain(self, &entry->sorted_chain,
504 self->he_selection = entry;
510 static unsigned int hist_browser__refresh(struct ui_browser *self)
514 struct hist_browser *hb = container_of(self, struct hist_browser, b);
516 if (self->top == NULL)
517 self->top = rb_first(&hb->hists->entries);
519 for (nd = self->top; nd; nd = rb_next(nd)) {
520 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
525 row += hist_browser__show_entry(hb, h, row);
526 if (row == self->height)
533 static struct rb_node *hists__filter_entries(struct rb_node *nd)
536 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
546 static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
549 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
559 static void ui_browser__hists_seek(struct ui_browser *self,
560 off_t offset, int whence)
562 struct hist_entry *h;
568 nd = hists__filter_entries(rb_first(self->entries));
574 nd = hists__filter_prev_entries(rb_last(self->entries));
582 * Moves not relative to the first visible entry invalidates its
585 h = rb_entry(self->top, struct hist_entry, rb_node);
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.
593 * This offset increments when we are going from top to bottom and
594 * decreases when we're going from bottom to top.
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.
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) {
611 h->row_offset += offset;
617 nd = hists__filter_entries(rb_next(nd));
622 } while (offset != 0);
623 } else if (offset < 0) {
625 h = rb_entry(nd, struct hist_entry, rb_node);
626 if (h->ms.unfolded) {
628 if (-offset > h->row_offset) {
629 offset += h->row_offset;
632 h->row_offset += offset;
638 if (-offset > h->nr_rows) {
639 offset += h->nr_rows;
642 h->row_offset = h->nr_rows + offset;
650 nd = hists__filter_prev_entries(rb_prev(nd));
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.
661 h = rb_entry(nd, struct hist_entry, rb_node);
663 h->row_offset = h->nr_rows;
670 h = rb_entry(nd, struct hist_entry, rb_node);
675 static struct hist_browser *hist_browser__new(struct hists *hists)
677 struct hist_browser *self = zalloc(sizeof(*self));
681 self->b.refresh = hist_browser__refresh;
682 self->b.seek = ui_browser__hists_seek;
688 static void hist_browser__delete(struct hist_browser *self)
690 newtFormDestroy(self->b.form);
695 static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
697 return self->he_selection;
700 static struct thread *hist_browser__selected_thread(struct hist_browser *self)
702 return self->he_selection->thread;
705 static int hist_browser__title(char *bf, size_t size, const char *ev_name,
706 const struct dso *dso, const struct thread *thread)
711 printed += snprintf(bf + printed, size - printed,
713 (thread->comm_set ? thread->comm : ""),
716 printed += snprintf(bf + printed, size - printed,
717 "%sDSO: %s", thread ? " " : "",
719 return printed ?: snprintf(bf, size, "Event: %s", ev_name);
722 int hists__browse(struct hists *self, const char *helpline, const char *ev_name)
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;
735 fstack = pstack__new(2);
739 ui_helpline__push(helpline);
741 hist_browser__title(msg, sizeof(msg), ev_name,
742 dso_filter, thread_filter);
745 const struct thread *thread;
746 const struct dso *dso;
748 int nr_options = 0, choice = 0, i,
749 annotate = -2, zoom_dso = -2, zoom_thread = -2,
752 if (hist_browser__run(browser, msg, &es))
755 thread = hist_browser__selected_thread(browser);
756 dso = browser->selection->map ? browser->selection->map->dso : NULL;
758 if (es.reason == NEWT_EXIT_HOTKEY) {
767 * Exit the browser, let hists__browser_tree
768 * go to the next or previous
776 if (browser->selection->map == NULL &&
777 browser->selection->map->dso->annotate_warned)
787 ui__help_window("-> Zoom into DSO/Threads & Annotate current symbol\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");
797 if (is_exit_key(key)) {
798 if (key == NEWT_KEY_ESCAPE &&
799 !ui__dialog_yesno("Do you really want to exit?"))
804 if (es.u.key == NEWT_KEY_LEFT) {
807 if (pstack__empty(fstack))
809 top = pstack__pop(fstack);
810 if (top == &dso_filter)
812 if (top == &thread_filter)
813 goto zoom_out_thread;
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++;
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 : ""),
829 zoom_thread = nr_options++;
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++;
837 if (browser->selection->map != NULL &&
838 asprintf(&options[nr_options], "Browse map details") > 0)
839 browse_map = nr_options++;
841 options[nr_options++] = (char *)"Exit";
843 choice = ui__popup_menu(nr_options, options);
845 for (i = 0; i < nr_options - 1; ++i)
848 if (choice == nr_options - 1)
854 if (choice == annotate) {
855 struct hist_entry *he;
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 "
865 he = hist_browser__selected_entry(browser);
869 hist_entry__tui_annotate(he);
870 } else if (choice == browse_map)
871 map__browse(browser->selection->map);
872 else if (choice == zoom_dso) {
875 pstack__remove(fstack, &dso_filter);
882 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
883 dso->kernel ? "the Kernel" : dso->short_name);
885 pstack__push(fstack, &dso_filter);
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) {
894 pstack__remove(fstack, &thread_filter);
897 thread_filter = NULL;
899 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
900 thread->comm_set ? thread->comm : "",
902 thread_filter = thread;
903 pstack__push(fstack, &thread_filter);
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);
912 pstack__delete(fstack);
914 hist_browser__delete(browser);
918 int hists__tui_browse_tree(struct rb_root *self, const char *help)
920 struct rb_node *first = rb_first(self), *nd = first, *next;
924 struct hists *hists = rb_entry(nd, struct hists, rb_node);
925 const char *ev_name = __event_name(hists->type, hists->config);
927 key = hists__browse(hists, help, ev_name);
929 if (is_exit_key(key))