5 * slang versions <= 2.0.6 have a "#if HAVE_LONG_LONG" that breaks
6 * the build if it isn't defined. Use the equivalent one that glibc
10 #ifndef HAVE_LONG_LONG
11 #define HAVE_LONG_LONG __GLIBC_HAVE_LONG_LONG
17 #include <sys/ttydefaults.h>
26 #if SLANG_VERSION < 20104
27 #define slsmg_printf(msg, args...) SLsmg_printf((char *)msg, ##args)
28 #define slsmg_write_nstring(msg, len) SLsmg_write_nstring((char *)msg, len)
29 #define sltt_set_color(obj, name, fg, bg) SLtt_set_color(obj,(char *)name,\
30 (char *)fg, (char *)bg)
32 #define slsmg_printf SLsmg_printf
33 #define slsmg_write_nstring SLsmg_write_nstring
34 #define sltt_set_color SLtt_set_color
38 newtComponent form, scale;
41 struct ui_progress *ui_progress__new(const char *title, u64 total)
43 struct ui_progress *self = malloc(sizeof(*self));
50 newtGetScreenSize(&cols, NULL);
52 newtCenteredWindow(cols, 1, title);
53 self->form = newtForm(NULL, NULL, 0);
54 if (self->form == NULL)
56 self->scale = newtScale(0, 0, cols, total);
57 if (self->scale == NULL)
59 newtFormAddComponent(self->form, self->scale);
66 newtFormDestroy(self->form);
72 void ui_progress__update(struct ui_progress *self, u64 curr)
75 * FIXME: We should have a per UI backend way of showing progress,
76 * stdio will just show a percentage as NN%, etc.
80 newtScaleSet(self->scale, curr);
84 void ui_progress__delete(struct ui_progress *self)
86 if (use_browser > 0) {
87 newtFormDestroy(self->form);
93 static void ui_helpline__pop(void)
98 static void ui_helpline__push(const char *msg)
100 newtPushHelpLine(msg);
103 static void ui_helpline__vpush(const char *fmt, va_list ap)
107 if (vasprintf(&s, fmt, ap) < 0)
108 vfprintf(stderr, fmt, ap);
110 ui_helpline__push(s);
115 static void ui_helpline__fpush(const char *fmt, ...)
120 ui_helpline__vpush(fmt, ap);
124 static void ui_helpline__puts(const char *msg)
127 ui_helpline__push(msg);
130 static char browser__last_msg[1024];
132 int browser__show_help(const char *format, va_list ap)
137 ret = vsnprintf(browser__last_msg + backlog,
138 sizeof(browser__last_msg) - backlog, format, ap);
141 if (browser__last_msg[backlog - 1] == '\n') {
142 ui_helpline__puts(browser__last_msg);
150 static void newt_form__set_exit_keys(newtComponent self)
152 newtFormAddHotKey(self, NEWT_KEY_LEFT);
153 newtFormAddHotKey(self, NEWT_KEY_ESCAPE);
154 newtFormAddHotKey(self, 'Q');
155 newtFormAddHotKey(self, 'q');
156 newtFormAddHotKey(self, CTRL('c'));
159 static newtComponent newt_form__new(void)
161 newtComponent self = newtForm(NULL, NULL, 0);
163 newt_form__set_exit_keys(self);
167 static int popup_menu(int argc, char * const argv[])
169 struct newtExitStruct es;
170 int i, rc = -1, max_len = 5;
171 newtComponent listbox, form = newt_form__new();
176 listbox = newtListbox(0, 0, argc, NEWT_FLAG_RETURNEXIT);
178 goto out_destroy_form;
180 newtFormAddComponent(form, listbox);
182 for (i = 0; i < argc; ++i) {
183 int len = strlen(argv[i]);
186 if (newtListboxAddEntry(listbox, argv[i], (void *)(long)i))
187 goto out_destroy_form;
190 newtCenteredWindow(max_len, argc, NULL);
191 newtFormRun(form, &es);
192 rc = newtListboxGetCurrent(listbox) - NULL;
193 if (es.reason == NEWT_EXIT_HOTKEY)
197 newtFormDestroy(form);
201 static int ui__help_window(const char *text)
203 struct newtExitStruct es;
204 newtComponent tb, form = newt_form__new();
206 int max_len = 0, nr_lines = 0;
214 const char *sep = strchr(t, '\n');
218 sep = strchr(t, '\0');
228 tb = newtTextbox(0, 0, max_len, nr_lines, 0);
230 goto out_destroy_form;
232 newtTextboxSetText(tb, text);
233 newtFormAddComponent(form, tb);
234 newtCenteredWindow(max_len, nr_lines, NULL);
235 newtFormRun(form, &es);
239 newtFormDestroy(form);
243 static bool dialog_yesno(const char *msg)
245 /* newtWinChoice should really be accepting const char pointers... */
246 char yes[] = "Yes", no[] = "No";
247 return newtWinChoice(NULL, yes, no, (char *)msg) == 1;
250 static void ui__error_window(const char *fmt, ...)
255 newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap);
259 #define HE_COLORSET_TOP 50
260 #define HE_COLORSET_MEDIUM 51
261 #define HE_COLORSET_NORMAL 52
262 #define HE_COLORSET_SELECTED 53
263 #define HE_COLORSET_CODE 54
265 static int ui_browser__percent_color(double percent, bool current)
268 return HE_COLORSET_SELECTED;
269 if (percent >= MIN_RED)
270 return HE_COLORSET_TOP;
271 if (percent >= MIN_GREEN)
272 return HE_COLORSET_MEDIUM;
273 return HE_COLORSET_NORMAL;
277 newtComponent form, sb;
278 u64 index, first_visible_entry_idx;
279 void *first_visible_entry, *entries;
280 u16 top, left, width, height;
282 unsigned int (*refresh_entries)(struct ui_browser *self);
283 void (*seek)(struct ui_browser *self,
284 off_t offset, int whence);
288 static void ui_browser__list_head_seek(struct ui_browser *self,
289 off_t offset, int whence)
291 struct list_head *head = self->entries;
292 struct list_head *pos;
299 pos = self->first_visible_entry;
309 while (offset-- != 0)
312 while (offset++ != 0)
316 self->first_visible_entry = pos;
319 static bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row)
321 return (self->first_visible_entry_idx + row) == self->index;
324 static void ui_browser__refresh_dimensions(struct ui_browser *self)
327 newtGetScreenSize(&cols, &rows);
329 if (self->width > cols - 4)
330 self->width = cols - 4;
331 self->height = rows - 5;
332 if (self->height > self->nr_entries)
333 self->height = self->nr_entries;
334 self->top = (rows - self->height) / 2;
335 self->left = (cols - self->width) / 2;
338 static void ui_browser__reset_index(struct ui_browser *self)
340 self->index = self->first_visible_entry_idx = 0;
341 self->seek(self, 0, SEEK_SET);
344 static int ui_browser__show(struct ui_browser *self, const char *title)
346 if (self->form != NULL) {
347 newtFormDestroy(self->form);
350 ui_browser__refresh_dimensions(self);
351 newtCenteredWindow(self->width, self->height, title);
352 self->form = newt_form__new();
353 if (self->form == NULL)
356 self->sb = newtVerticalScrollbar(self->width, 0, self->height,
358 HE_COLORSET_SELECTED);
359 if (self->sb == NULL)
362 newtFormAddHotKey(self->form, NEWT_KEY_UP);
363 newtFormAddHotKey(self->form, NEWT_KEY_DOWN);
364 newtFormAddHotKey(self->form, NEWT_KEY_PGUP);
365 newtFormAddHotKey(self->form, NEWT_KEY_PGDN);
366 newtFormAddHotKey(self->form, NEWT_KEY_HOME);
367 newtFormAddHotKey(self->form, NEWT_KEY_END);
368 newtFormAddComponent(self->form, self->sb);
372 static int objdump_line__show(struct objdump_line *self, struct list_head *head,
373 int width, struct hist_entry *he, int len,
376 if (self->offset != -1) {
377 struct symbol *sym = he->ms.sym;
378 unsigned int hits = 0;
379 double percent = 0.0;
381 struct sym_priv *priv = symbol__priv(sym);
382 struct sym_ext *sym_ext = priv->ext;
383 struct sym_hist *h = priv->hist;
384 s64 offset = self->offset;
385 struct objdump_line *next = objdump__get_next_ip_line(head, self);
387 while (offset < (s64)len &&
388 (next == NULL || offset < next->offset)) {
390 percent += sym_ext[offset].percent;
392 hits += h->ip[offset];
397 if (sym_ext == NULL && h->sum)
398 percent = 100.0 * hits / h->sum;
400 color = ui_browser__percent_color(percent, current_entry);
401 SLsmg_set_color(color);
402 slsmg_printf(" %7.2f ", percent);
404 SLsmg_set_color(HE_COLORSET_CODE);
406 int color = ui_browser__percent_color(0, current_entry);
407 SLsmg_set_color(color);
408 slsmg_write_nstring(" ", 9);
411 SLsmg_write_char(':');
412 slsmg_write_nstring(" ", 8);
414 slsmg_write_nstring(" ", width - 18);
416 slsmg_write_nstring(self->line, width - 18);
421 static int ui_browser__refresh_entries(struct ui_browser *self)
425 newtScrollbarSet(self->sb, self->index, self->nr_entries - 1);
426 row = self->refresh_entries(self);
427 SLsmg_set_color(HE_COLORSET_NORMAL);
428 SLsmg_fill_region(self->top + row, self->left,
429 self->height - row, self->width, ' ');
434 static int ui_browser__run(struct ui_browser *self, struct newtExitStruct *es)
436 if (ui_browser__refresh_entries(self) < 0)
442 newtFormRun(self->form, es);
444 if (es->reason != NEWT_EXIT_HOTKEY)
446 if (is_exit_key(es->u.key))
450 if (self->index == self->nr_entries - 1)
453 if (self->index == self->first_visible_entry_idx + self->height) {
454 ++self->first_visible_entry_idx;
455 self->seek(self, +1, SEEK_CUR);
459 if (self->index == 0)
462 if (self->index < self->first_visible_entry_idx) {
463 --self->first_visible_entry_idx;
464 self->seek(self, -1, SEEK_CUR);
469 if (self->first_visible_entry_idx + self->height > self->nr_entries - 1)
472 offset = self->height;
473 if (self->index + offset > self->nr_entries - 1)
474 offset = self->nr_entries - 1 - self->index;
475 self->index += offset;
476 self->first_visible_entry_idx += offset;
477 self->seek(self, +offset, SEEK_CUR);
480 if (self->first_visible_entry_idx == 0)
483 if (self->first_visible_entry_idx < self->height)
484 offset = self->first_visible_entry_idx;
486 offset = self->height;
488 self->index -= offset;
489 self->first_visible_entry_idx -= offset;
490 self->seek(self, -offset, SEEK_CUR);
493 ui_browser__reset_index(self);
496 offset = self->height - 1;
497 if (offset >= self->nr_entries)
498 offset = self->nr_entries - 1;
500 self->index = self->nr_entries - 1;
501 self->first_visible_entry_idx = self->index - offset;
502 self->seek(self, -offset, SEEK_END);
507 if (ui_browser__refresh_entries(self) < 0)
513 static char *callchain_list__sym_name(struct callchain_list *self,
514 char *bf, size_t bfsize)
517 return self->ms.sym->name;
519 snprintf(bf, bfsize, "%#Lx", self->ip);
523 static unsigned int hist_entry__annotate_browser_refresh(struct ui_browser *self)
525 struct objdump_line *pos;
526 struct list_head *head = self->entries;
527 struct hist_entry *he = self->priv;
529 int len = he->ms.sym->end - he->ms.sym->start;
531 if (self->first_visible_entry == NULL || self->first_visible_entry == self->entries)
532 self->first_visible_entry = head->next;
534 pos = list_entry(self->first_visible_entry, struct objdump_line, node);
536 list_for_each_entry_from(pos, head, node) {
537 bool current_entry = ui_browser__is_current_entry(self, row);
538 SLsmg_gotorc(self->top + row, self->left);
539 objdump_line__show(pos, head, self->width,
540 he, len, current_entry);
541 if (++row == self->height)
548 int hist_entry__tui_annotate(struct hist_entry *self)
550 struct ui_browser browser;
551 struct newtExitStruct es;
552 struct objdump_line *pos, *n;
556 if (self->ms.sym == NULL)
559 if (self->ms.map->dso->annotate_warned)
562 if (hist_entry__annotate(self, &head) < 0) {
563 ui__error_window(browser__last_msg);
567 ui_helpline__push("Press <- or ESC to exit");
569 memset(&browser, 0, sizeof(browser));
570 browser.entries = &head;
571 browser.refresh_entries = hist_entry__annotate_browser_refresh;
572 browser.seek = ui_browser__list_head_seek;
574 list_for_each_entry(pos, &head, node) {
575 size_t line_len = strlen(pos->line);
576 if (browser.width < line_len)
577 browser.width = line_len;
578 ++browser.nr_entries;
581 browser.width += 18; /* Percentage */
582 ui_browser__show(&browser, self->ms.sym->name);
583 newtFormAddHotKey(browser.form, ' ');
584 ret = ui_browser__run(&browser, &es);
585 newtFormDestroy(browser.form);
587 list_for_each_entry_safe(pos, n, &head, node) {
588 list_del(&pos->node);
589 objdump_line__free(pos);
595 struct hist_browser {
598 struct hist_entry *he_selection;
599 struct map_symbol *selection;
602 static void hist_browser__reset(struct hist_browser *self);
603 static int hist_browser__run(struct hist_browser *self, const char *title,
604 struct newtExitStruct *es);
605 static unsigned int hist_browser__refresh_entries(struct ui_browser *self);
606 static void ui_browser__hists_seek(struct ui_browser *self,
607 off_t offset, int whence);
609 static struct hist_browser *hist_browser__new(struct hists *hists)
611 struct hist_browser *self = zalloc(sizeof(*self));
615 self->b.refresh_entries = hist_browser__refresh_entries;
616 self->b.seek = ui_browser__hists_seek;
622 static void hist_browser__delete(struct hist_browser *self)
624 newtFormDestroy(self->b.form);
629 static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
631 return self->he_selection;
634 static struct thread *hist_browser__selected_thread(struct hist_browser *self)
636 return self->he_selection->thread;
639 static int hist_browser__title(char *bf, size_t size, const char *ev_name,
640 const struct dso *dso, const struct thread *thread)
645 printed += snprintf(bf + printed, size - printed,
647 (thread->comm_set ? thread->comm : ""),
650 printed += snprintf(bf + printed, size - printed,
651 "%sDSO: %s", thread ? " " : "",
653 return printed ?: snprintf(bf, size, "Event: %s", ev_name);
656 int hists__browse(struct hists *self, const char *helpline, const char *ev_name)
658 struct hist_browser *browser = hist_browser__new(self);
659 struct pstack *fstack;
660 const struct thread *thread_filter = NULL;
661 const struct dso *dso_filter = NULL;
662 struct newtExitStruct es;
669 fstack = pstack__new(2);
673 ui_helpline__push(helpline);
675 hist_browser__title(msg, sizeof(msg), ev_name,
676 dso_filter, thread_filter);
679 const struct thread *thread;
680 const struct dso *dso;
682 int nr_options = 0, choice = 0, i,
683 annotate = -2, zoom_dso = -2, zoom_thread = -2;
685 if (hist_browser__run(browser, msg, &es))
688 thread = hist_browser__selected_thread(browser);
689 dso = browser->selection->map ? browser->selection->map->dso : NULL;
691 if (es.reason == NEWT_EXIT_HOTKEY) {
700 * Exit the browser, let hists__browser_tree
701 * go to the next or previous
710 if (browser->selection->map == NULL &&
711 browser->selection->map->dso->annotate_warned)
721 ui__help_window("-> Zoom into DSO/Threads & Annotate current symbol\n"
723 "a Annotate current symbol\n"
724 "h/?/F1 Show this window\n"
725 "d Zoom into current DSO\n"
726 "t Zoom into current Thread\n"
727 "q/CTRL+C Exit browser");
731 if (is_exit_key(key)) {
732 if (key == NEWT_KEY_ESCAPE) {
733 if (dialog_yesno("Do you really want to exit?"))
741 if (es.u.key == NEWT_KEY_LEFT) {
744 if (pstack__empty(fstack))
746 top = pstack__pop(fstack);
747 if (top == &dso_filter)
749 if (top == &thread_filter)
750 goto zoom_out_thread;
755 if (browser->selection->sym != NULL &&
756 !browser->selection->map->dso->annotate_warned &&
757 asprintf(&options[nr_options], "Annotate %s",
758 browser->selection->sym->name) > 0)
759 annotate = nr_options++;
761 if (thread != NULL &&
762 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
763 (thread_filter ? "out of" : "into"),
764 (thread->comm_set ? thread->comm : ""),
766 zoom_thread = nr_options++;
769 asprintf(&options[nr_options], "Zoom %s %s DSO",
770 (dso_filter ? "out of" : "into"),
771 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
772 zoom_dso = nr_options++;
774 options[nr_options++] = (char *)"Exit";
776 choice = popup_menu(nr_options, options);
778 for (i = 0; i < nr_options - 1; ++i)
781 if (choice == nr_options - 1)
787 if (choice == annotate) {
788 struct hist_entry *he;
790 if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
791 browser->selection->map->dso->annotate_warned = 1;
792 ui_helpline__puts("No vmlinux file found, can't "
793 "annotate with just a "
798 he = hist_browser__selected_entry(browser);
802 hist_entry__tui_annotate(he);
803 } else if (choice == zoom_dso) {
806 pstack__remove(fstack, &dso_filter);
813 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
814 dso->kernel ? "the Kernel" : dso->short_name);
816 pstack__push(fstack, &dso_filter);
818 hists__filter_by_dso(self, dso_filter);
819 hist_browser__title(msg, sizeof(msg), ev_name,
820 dso_filter, thread_filter);
821 hist_browser__reset(browser);
822 } else if (choice == zoom_thread) {
825 pstack__remove(fstack, &thread_filter);
828 thread_filter = NULL;
830 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
831 thread->comm_set ? thread->comm : "",
833 thread_filter = thread;
834 pstack__push(fstack, &thread_filter);
836 hists__filter_by_thread(self, thread_filter);
837 hist_browser__title(msg, sizeof(msg), ev_name,
838 dso_filter, thread_filter);
839 hist_browser__reset(browser);
843 pstack__delete(fstack);
845 hist_browser__delete(browser);
849 int hists__tui_browse_tree(struct rb_root *self, const char *help)
851 struct rb_node *first = rb_first(self), *nd = first, *next;
855 struct hists *hists = rb_entry(nd, struct hists, rb_node);
856 const char *ev_name = __event_name(hists->type, hists->config);
858 key = hists__browse(hists, help, ev_name);
860 if (is_exit_key(key))
881 static struct newtPercentTreeColors {
882 const char *topColorFg, *topColorBg;
883 const char *mediumColorFg, *mediumColorBg;
884 const char *normalColorFg, *normalColorBg;
885 const char *selColorFg, *selColorBg;
886 const char *codeColorFg, *codeColorBg;
887 } defaultPercentTreeColors = {
889 "green", "lightgray",
890 "black", "lightgray",
891 "lightgray", "magenta",
895 static void newt_suspend(void *d __used)
902 void setup_browser(void)
904 struct newtPercentTreeColors *c = &defaultPercentTreeColors;
906 if (!isatty(1) || !use_browser || dump_trace) {
915 newtSetSuspendCallback(newt_suspend, NULL);
916 ui_helpline__puts(" ");
917 sltt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg);
918 sltt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg);
919 sltt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg);
920 sltt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg);
921 sltt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg);
924 void exit_browser(bool wait_for_ok)
926 if (use_browser > 0) {
928 char title[] = "Fatal Error", ok[] = "Ok";
929 newtWinMessage(title, ok, browser__last_msg);
935 static void hist_browser__refresh_dimensions(struct hist_browser *self)
937 /* 3 == +/- toggle symbol before actual hist_entry rendering */
938 self->b.width = 3 + (hists__sort_list_width(self->hists) +
942 static void hist_browser__reset(struct hist_browser *self)
944 self->b.nr_entries = self->hists->nr_entries;
945 hist_browser__refresh_dimensions(self);
946 ui_browser__reset_index(&self->b);
949 static char tree__folded_sign(bool unfolded)
951 return unfolded ? '-' : '+';
954 static char map_symbol__folded(const struct map_symbol *self)
956 return self->has_children ? tree__folded_sign(self->unfolded) : ' ';
959 static char hist_entry__folded(const struct hist_entry *self)
961 return map_symbol__folded(&self->ms);
964 static char callchain_list__folded(const struct callchain_list *self)
966 return map_symbol__folded(&self->ms);
969 static bool map_symbol__toggle_fold(struct map_symbol *self)
971 if (!self->has_children)
974 self->unfolded = !self->unfolded;
978 #define LEVEL_OFFSET_STEP 3
980 static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self,
981 struct callchain_node *chain_node,
982 u64 total, int level,
985 bool *is_current_entry)
987 struct rb_node *node;
988 int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
989 u64 new_total, remaining;
991 if (callchain_param.mode == CHAIN_GRAPH_REL)
992 new_total = chain_node->children_hit;
996 remaining = new_total;
997 node = rb_first(&chain_node->rb_root);
999 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
1000 struct rb_node *next = rb_next(node);
1001 u64 cumul = cumul_hits(child);
1002 struct callchain_list *chain;
1003 char folded_sign = ' ';
1005 int extra_offset = 0;
1009 list_for_each_entry(chain, &child->val, list) {
1010 char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str;
1013 bool was_first = first;
1017 chain->ms.has_children = chain->list.next != &child->val ||
1018 rb_first(&child->rb_root) != NULL;
1020 extra_offset = LEVEL_OFFSET_STEP;
1021 chain->ms.has_children = chain->list.next == &child->val &&
1022 rb_first(&child->rb_root) != NULL;
1025 folded_sign = callchain_list__folded(chain);
1026 if (*row_offset != 0) {
1032 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
1034 double percent = cumul * 100.0 / new_total;
1036 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
1037 str = "Not enough memory!";
1042 color = HE_COLORSET_NORMAL;
1043 width = self->b.width - (offset + extra_offset + 2);
1044 if (ui_browser__is_current_entry(&self->b, row)) {
1045 self->selection = &chain->ms;
1046 color = HE_COLORSET_SELECTED;
1047 *is_current_entry = true;
1050 SLsmg_set_color(color);
1051 SLsmg_gotorc(self->b.top + row, self->b.left);
1052 slsmg_write_nstring(" ", offset + extra_offset);
1053 slsmg_printf("%c ", folded_sign);
1054 slsmg_write_nstring(str, width);
1057 if (++row == self->b.height)
1060 if (folded_sign == '+')
1064 if (folded_sign == '-') {
1065 const int new_level = level + (extra_offset ? 2 : 1);
1066 row += hist_browser__show_callchain_node_rb_tree(self, child, new_total,
1067 new_level, row, row_offset,
1070 if (row == self->b.height)
1075 return row - first_row;
1078 static int hist_browser__show_callchain_node(struct hist_browser *self,
1079 struct callchain_node *node,
1080 int level, unsigned short row,
1082 bool *is_current_entry)
1084 struct callchain_list *chain;
1085 int first_row = row,
1086 offset = level * LEVEL_OFFSET_STEP,
1087 width = self->b.width - offset;
1088 char folded_sign = ' ';
1090 list_for_each_entry(chain, &node->val, list) {
1091 char ipstr[BITS_PER_LONG / 4 + 1], *s;
1094 * FIXME: This should be moved to somewhere else,
1095 * probably when the callchain is created, so as not to
1096 * traverse it all over again
1098 chain->ms.has_children = rb_first(&node->rb_root) != NULL;
1099 folded_sign = callchain_list__folded(chain);
1101 if (*row_offset != 0) {
1106 color = HE_COLORSET_NORMAL;
1107 if (ui_browser__is_current_entry(&self->b, row)) {
1108 self->selection = &chain->ms;
1109 color = HE_COLORSET_SELECTED;
1110 *is_current_entry = true;
1113 s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
1114 SLsmg_gotorc(self->b.top + row, self->b.left);
1115 SLsmg_set_color(color);
1116 slsmg_write_nstring(" ", offset);
1117 slsmg_printf("%c ", folded_sign);
1118 slsmg_write_nstring(s, width - 2);
1120 if (++row == self->b.height)
1124 if (folded_sign == '-')
1125 row += hist_browser__show_callchain_node_rb_tree(self, node,
1126 self->hists->stats.total_period,
1131 return row - first_row;
1134 static int hist_browser__show_callchain(struct hist_browser *self,
1135 struct rb_root *chain,
1136 int level, unsigned short row,
1138 bool *is_current_entry)
1141 int first_row = row;
1143 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
1144 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
1146 row += hist_browser__show_callchain_node(self, node, level,
1149 if (row == self->b.height)
1153 return row - first_row;
1156 static int hist_browser__show_entry(struct hist_browser *self,
1157 struct hist_entry *entry,
1163 int color, width = self->b.width;
1164 char folded_sign = ' ';
1165 bool current_entry = ui_browser__is_current_entry(&self->b, row);
1166 off_t row_offset = entry->row_offset;
1168 if (current_entry) {
1169 self->he_selection = entry;
1170 self->selection = &entry->ms;
1173 if (symbol_conf.use_callchain) {
1174 entry->ms.has_children = !RB_EMPTY_ROOT(&entry->sorted_chain);
1175 folded_sign = hist_entry__folded(entry);
1178 if (row_offset == 0) {
1179 hist_entry__snprintf(entry, s, sizeof(s), self->hists, NULL, false,
1180 0, false, self->hists->stats.total_period);
1181 percent = (entry->period * 100.0) / self->hists->stats.total_period;
1183 color = HE_COLORSET_SELECTED;
1184 if (!current_entry) {
1185 if (percent >= MIN_RED)
1186 color = HE_COLORSET_TOP;
1187 else if (percent >= MIN_GREEN)
1188 color = HE_COLORSET_MEDIUM;
1190 color = HE_COLORSET_NORMAL;
1193 SLsmg_set_color(color);
1194 SLsmg_gotorc(self->b.top + row, self->b.left);
1195 if (symbol_conf.use_callchain) {
1196 slsmg_printf("%c ", folded_sign);
1199 slsmg_write_nstring(s, width);
1205 if (folded_sign == '-' && row != self->b.height) {
1206 printed += hist_browser__show_callchain(self, &entry->sorted_chain,
1207 1, row, &row_offset,
1210 self->he_selection = entry;
1216 static unsigned int hist_browser__refresh_entries(struct ui_browser *self)
1220 struct hist_browser *hb = container_of(self, struct hist_browser, b);
1222 if (self->first_visible_entry == NULL)
1223 self->first_visible_entry = rb_first(&hb->hists->entries);
1225 for (nd = self->first_visible_entry; nd; nd = rb_next(nd)) {
1226 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1231 row += hist_browser__show_entry(hb, h, row);
1232 if (row == self->height)
1239 static void callchain_node__init_have_children_rb_tree(struct callchain_node *self)
1241 struct rb_node *nd = rb_first(&self->rb_root);
1243 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
1244 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
1245 struct callchain_list *chain;
1248 list_for_each_entry(chain, &child->val, list) {
1251 chain->ms.has_children = chain->list.next != &child->val ||
1252 rb_first(&child->rb_root) != NULL;
1254 chain->ms.has_children = chain->list.next == &child->val &&
1255 rb_first(&child->rb_root) != NULL;
1258 callchain_node__init_have_children_rb_tree(child);
1262 static void callchain_node__init_have_children(struct callchain_node *self)
1264 struct callchain_list *chain;
1266 list_for_each_entry(chain, &self->val, list)
1267 chain->ms.has_children = rb_first(&self->rb_root) != NULL;
1269 callchain_node__init_have_children_rb_tree(self);
1272 static void callchain__init_have_children(struct rb_root *self)
1276 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
1277 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
1278 callchain_node__init_have_children(node);
1282 static void hist_entry__init_have_children(struct hist_entry *self)
1284 if (!self->init_have_children) {
1285 callchain__init_have_children(&self->sorted_chain);
1286 self->init_have_children = true;
1290 static struct rb_node *hists__filter_entries(struct rb_node *nd)
1292 while (nd != NULL) {
1293 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1303 static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
1305 while (nd != NULL) {
1306 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1316 static void ui_browser__hists_seek(struct ui_browser *self,
1317 off_t offset, int whence)
1319 struct hist_entry *h;
1325 nd = hists__filter_entries(rb_first(self->entries));
1328 nd = self->first_visible_entry;
1331 nd = hists__filter_prev_entries(rb_last(self->entries));
1339 * Moves not relative to the first visible entry invalidates its
1342 h = rb_entry(self->first_visible_entry, struct hist_entry, rb_node);
1346 * Here we have to check if nd is expanded (+), if it is we can't go
1347 * the next top level hist_entry, instead we must compute an offset of
1348 * what _not_ to show and not change the first visible entry.
1350 * This offset increments when we are going from top to bottom and
1351 * decreases when we're going from bottom to top.
1353 * As we don't have backpointers to the top level in the callchains
1354 * structure, we need to always print the whole hist_entry callchain,
1355 * skipping the first ones that are before the first visible entry
1356 * and stop when we printed enough lines to fill the screen.
1361 h = rb_entry(nd, struct hist_entry, rb_node);
1362 if (h->ms.unfolded) {
1363 u16 remaining = h->nr_rows - h->row_offset;
1364 if (offset > remaining) {
1365 offset -= remaining;
1368 h->row_offset += offset;
1370 self->first_visible_entry = nd;
1374 nd = hists__filter_entries(rb_next(nd));
1378 self->first_visible_entry = nd;
1379 } while (offset != 0);
1380 } else if (offset < 0) {
1382 h = rb_entry(nd, struct hist_entry, rb_node);
1383 if (h->ms.unfolded) {
1385 if (-offset > h->row_offset) {
1386 offset += h->row_offset;
1389 h->row_offset += offset;
1391 self->first_visible_entry = nd;
1395 if (-offset > h->nr_rows) {
1396 offset += h->nr_rows;
1399 h->row_offset = h->nr_rows + offset;
1401 self->first_visible_entry = nd;
1407 nd = hists__filter_prev_entries(rb_prev(nd));
1411 self->first_visible_entry = nd;
1414 * Last unfiltered hist_entry, check if it is
1415 * unfolded, if it is then we should have
1416 * row_offset at its last entry.
1418 h = rb_entry(nd, struct hist_entry, rb_node);
1420 h->row_offset = h->nr_rows;
1426 self->first_visible_entry = nd;
1427 h = rb_entry(nd, struct hist_entry, rb_node);
1432 static int callchain_node__count_rows_rb_tree(struct callchain_node *self)
1437 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
1438 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
1439 struct callchain_list *chain;
1440 char folded_sign = ' '; /* No children */
1442 list_for_each_entry(chain, &child->val, list) {
1444 /* We need this because we may not have children */
1445 folded_sign = callchain_list__folded(chain);
1446 if (folded_sign == '+')
1450 if (folded_sign == '-') /* Have children and they're unfolded */
1451 n += callchain_node__count_rows_rb_tree(child);
1457 static int callchain_node__count_rows(struct callchain_node *node)
1459 struct callchain_list *chain;
1460 bool unfolded = false;
1463 list_for_each_entry(chain, &node->val, list) {
1465 unfolded = chain->ms.unfolded;
1469 n += callchain_node__count_rows_rb_tree(node);
1474 static int callchain__count_rows(struct rb_root *chain)
1479 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
1480 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
1481 n += callchain_node__count_rows(node);
1487 static bool hist_browser__toggle_fold(struct hist_browser *self)
1489 if (map_symbol__toggle_fold(self->selection)) {
1490 struct hist_entry *he = self->he_selection;
1492 hist_entry__init_have_children(he);
1493 self->hists->nr_entries -= he->nr_rows;
1495 if (he->ms.unfolded)
1496 he->nr_rows = callchain__count_rows(&he->sorted_chain);
1499 self->hists->nr_entries += he->nr_rows;
1500 self->b.nr_entries = self->hists->nr_entries;
1505 /* If it doesn't have children, no toggling performed */
1509 static int hist_browser__run(struct hist_browser *self, const char *title,
1510 struct newtExitStruct *es)
1512 char str[256], unit;
1513 unsigned long nr_events = self->hists->stats.nr_events[PERF_RECORD_SAMPLE];
1515 self->b.entries = &self->hists->entries;
1516 self->b.nr_entries = self->hists->nr_entries;
1518 hist_browser__refresh_dimensions(self);
1520 nr_events = convert_unit(nr_events, &unit);
1521 snprintf(str, sizeof(str), "Events: %lu%c ",
1523 newtDrawRootText(0, 0, str);
1525 if (ui_browser__show(&self->b, title) < 0)
1528 newtFormAddHotKey(self->b.form, 'A');
1529 newtFormAddHotKey(self->b.form, 'a');
1530 newtFormAddHotKey(self->b.form, '?');
1531 newtFormAddHotKey(self->b.form, 'h');
1532 newtFormAddHotKey(self->b.form, 'H');
1533 newtFormAddHotKey(self->b.form, 'd');
1535 newtFormAddHotKey(self->b.form, NEWT_KEY_LEFT);
1536 newtFormAddHotKey(self->b.form, NEWT_KEY_RIGHT);
1537 newtFormAddHotKey(self->b.form, NEWT_KEY_ENTER);
1540 ui_browser__run(&self->b, es);
1542 if (es->reason != NEWT_EXIT_HOTKEY)
1544 switch (es->u.key) {
1545 case 'd': { /* Debug */
1547 struct hist_entry *h = rb_entry(self->b.first_visible_entry,
1548 struct hist_entry, rb_node);
1550 ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
1551 seq++, self->b.nr_entries,
1552 self->hists->nr_entries,
1555 self->b.first_visible_entry_idx,
1556 h->row_offset, h->nr_rows);
1559 case NEWT_KEY_ENTER:
1560 if (hist_browser__toggle_fold(self))