perf ui browser: Make the colors configurable and change the defaults
[linux-2.6-block.git] / tools / perf / util / ui / browsers / hists.c
CommitLineData
d1b4f249
ACM
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
e248de33
ACM
10#include "../../evsel.h"
11#include "../../evlist.h"
d1b4f249
ACM
12#include "../../hist.h"
13#include "../../pstack.h"
14#include "../../sort.h"
15#include "../../util.h"
16
17#include "../browser.h"
18#include "../helpline.h"
19#include "../util.h"
20#include "map.h"
21
d1b4f249
ACM
22struct hist_browser {
23 struct ui_browser b;
24 struct hists *hists;
25 struct hist_entry *he_selection;
26 struct map_symbol *selection;
81cce8de
ACM
27 const struct thread *thread_filter;
28 const struct dso *dso_filter;
724c9c9f 29 bool has_symbols;
d1b4f249
ACM
30};
31
81cce8de
ACM
32static int hists__browser_title(struct hists *self, char *bf, size_t size,
33 const char *ev_name, const struct dso *dso,
34 const struct thread *thread);
35
d1b4f249
ACM
36static void hist_browser__refresh_dimensions(struct hist_browser *self)
37{
38 /* 3 == +/- toggle symbol before actual hist_entry rendering */
39 self->b.width = 3 + (hists__sort_list_width(self->hists) +
40 sizeof("[k]"));
41}
42
43static void hist_browser__reset(struct hist_browser *self)
44{
45 self->b.nr_entries = self->hists->nr_entries;
46 hist_browser__refresh_dimensions(self);
47 ui_browser__reset_index(&self->b);
48}
49
50static char tree__folded_sign(bool unfolded)
51{
52 return unfolded ? '-' : '+';
53}
54
55static char map_symbol__folded(const struct map_symbol *self)
56{
57 return self->has_children ? tree__folded_sign(self->unfolded) : ' ';
58}
59
60static char hist_entry__folded(const struct hist_entry *self)
61{
62 return map_symbol__folded(&self->ms);
63}
64
65static char callchain_list__folded(const struct callchain_list *self)
66{
67 return map_symbol__folded(&self->ms);
68}
69
3c916cc2
ACM
70static void map_symbol__set_folding(struct map_symbol *self, bool unfold)
71{
72 self->unfolded = unfold ? self->has_children : false;
73}
74
d1b4f249
ACM
75static int callchain_node__count_rows_rb_tree(struct callchain_node *self)
76{
77 int n = 0;
78 struct rb_node *nd;
79
80 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
81 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
82 struct callchain_list *chain;
83 char folded_sign = ' '; /* No children */
84
85 list_for_each_entry(chain, &child->val, list) {
86 ++n;
87 /* We need this because we may not have children */
88 folded_sign = callchain_list__folded(chain);
89 if (folded_sign == '+')
90 break;
91 }
92
93 if (folded_sign == '-') /* Have children and they're unfolded */
94 n += callchain_node__count_rows_rb_tree(child);
95 }
96
97 return n;
98}
99
100static int callchain_node__count_rows(struct callchain_node *node)
101{
102 struct callchain_list *chain;
103 bool unfolded = false;
104 int n = 0;
105
106 list_for_each_entry(chain, &node->val, list) {
107 ++n;
108 unfolded = chain->ms.unfolded;
109 }
110
111 if (unfolded)
112 n += callchain_node__count_rows_rb_tree(node);
113
114 return n;
115}
116
117static int callchain__count_rows(struct rb_root *chain)
118{
119 struct rb_node *nd;
120 int n = 0;
121
122 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
123 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
124 n += callchain_node__count_rows(node);
125 }
126
127 return n;
128}
129
130static bool map_symbol__toggle_fold(struct map_symbol *self)
131{
132 if (!self->has_children)
133 return false;
134
135 self->unfolded = !self->unfolded;
136 return true;
137}
138
139static void callchain_node__init_have_children_rb_tree(struct callchain_node *self)
140{
141 struct rb_node *nd = rb_first(&self->rb_root);
142
143 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
144 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
145 struct callchain_list *chain;
293db47f 146 bool first = true;
d1b4f249
ACM
147
148 list_for_each_entry(chain, &child->val, list) {
149 if (first) {
150 first = false;
151 chain->ms.has_children = chain->list.next != &child->val ||
293db47f 152 !RB_EMPTY_ROOT(&child->rb_root);
d1b4f249
ACM
153 } else
154 chain->ms.has_children = chain->list.next == &child->val &&
293db47f 155 !RB_EMPTY_ROOT(&child->rb_root);
d1b4f249
ACM
156 }
157
158 callchain_node__init_have_children_rb_tree(child);
159 }
160}
161
162static void callchain_node__init_have_children(struct callchain_node *self)
163{
164 struct callchain_list *chain;
165
166 list_for_each_entry(chain, &self->val, list)
293db47f 167 chain->ms.has_children = !RB_EMPTY_ROOT(&self->rb_root);
d1b4f249
ACM
168
169 callchain_node__init_have_children_rb_tree(self);
170}
171
172static void callchain__init_have_children(struct rb_root *self)
173{
174 struct rb_node *nd;
175
176 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
177 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
178 callchain_node__init_have_children(node);
179 }
180}
181
182static void hist_entry__init_have_children(struct hist_entry *self)
183{
184 if (!self->init_have_children) {
18b308d7 185 self->ms.has_children = !RB_EMPTY_ROOT(&self->sorted_chain);
d1b4f249
ACM
186 callchain__init_have_children(&self->sorted_chain);
187 self->init_have_children = true;
188 }
189}
190
191static bool hist_browser__toggle_fold(struct hist_browser *self)
192{
193 if (map_symbol__toggle_fold(self->selection)) {
194 struct hist_entry *he = self->he_selection;
195
196 hist_entry__init_have_children(he);
197 self->hists->nr_entries -= he->nr_rows;
198
199 if (he->ms.unfolded)
200 he->nr_rows = callchain__count_rows(&he->sorted_chain);
201 else
202 he->nr_rows = 0;
203 self->hists->nr_entries += he->nr_rows;
204 self->b.nr_entries = self->hists->nr_entries;
205
206 return true;
207 }
208
209 /* If it doesn't have children, no toggling performed */
210 return false;
211}
212
3c916cc2
ACM
213static int callchain_node__set_folding_rb_tree(struct callchain_node *self, bool unfold)
214{
215 int n = 0;
216 struct rb_node *nd;
217
218 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
219 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
220 struct callchain_list *chain;
221 bool has_children = false;
222
223 list_for_each_entry(chain, &child->val, list) {
224 ++n;
225 map_symbol__set_folding(&chain->ms, unfold);
226 has_children = chain->ms.has_children;
227 }
228
229 if (has_children)
230 n += callchain_node__set_folding_rb_tree(child, unfold);
231 }
232
233 return n;
234}
235
236static int callchain_node__set_folding(struct callchain_node *node, bool unfold)
237{
238 struct callchain_list *chain;
239 bool has_children = false;
240 int n = 0;
241
242 list_for_each_entry(chain, &node->val, list) {
243 ++n;
244 map_symbol__set_folding(&chain->ms, unfold);
245 has_children = chain->ms.has_children;
246 }
247
248 if (has_children)
249 n += callchain_node__set_folding_rb_tree(node, unfold);
250
251 return n;
252}
253
254static int callchain__set_folding(struct rb_root *chain, bool unfold)
255{
256 struct rb_node *nd;
257 int n = 0;
258
259 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
260 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
261 n += callchain_node__set_folding(node, unfold);
262 }
263
264 return n;
265}
266
267static void hist_entry__set_folding(struct hist_entry *self, bool unfold)
268{
269 hist_entry__init_have_children(self);
270 map_symbol__set_folding(&self->ms, unfold);
271
272 if (self->ms.has_children) {
273 int n = callchain__set_folding(&self->sorted_chain, unfold);
274 self->nr_rows = unfold ? n : 0;
275 } else
276 self->nr_rows = 0;
277}
278
279static void hists__set_folding(struct hists *self, bool unfold)
280{
281 struct rb_node *nd;
282
283 self->nr_entries = 0;
284
285 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
286 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
287 hist_entry__set_folding(he, unfold);
288 self->nr_entries += 1 + he->nr_rows;
289 }
290}
291
292static void hist_browser__set_folding(struct hist_browser *self, bool unfold)
293{
294 hists__set_folding(self->hists, unfold);
295 self->b.nr_entries = self->hists->nr_entries;
296 /* Go to the start, we may be way after valid entries after a collapse */
297 ui_browser__reset_index(&self->b);
298}
299
81cce8de
ACM
300static int hist_browser__run(struct hist_browser *self, const char *ev_name,
301 void(*timer)(void *arg), void *arg, int delay_secs)
d1b4f249 302{
b50e003d 303 int key;
81cce8de 304 char title[160];
d1b4f249
ACM
305
306 self->b.entries = &self->hists->entries;
307 self->b.nr_entries = self->hists->nr_entries;
308
309 hist_browser__refresh_dimensions(self);
81cce8de
ACM
310 hists__browser_title(self->hists, title, sizeof(title), ev_name,
311 self->dso_filter, self->thread_filter);
d1b4f249 312
59e8fe32
ACM
313 if (ui_browser__show(&self->b, title,
314 "Press '?' for help on key bindings") < 0)
d1b4f249
ACM
315 return -1;
316
d1b4f249 317 while (1) {
3af6e338 318 key = ui_browser__run(&self->b, delay_secs);
d1b4f249 319
b50e003d 320 switch (key) {
81cce8de
ACM
321 case -1:
322 /* FIXME we need to check if it was es.reason == NEWT_EXIT_TIMER */
323 timer(arg);
900e14a8 324 ui_browser__update_nr_entries(&self->b, self->hists->nr_entries);
81cce8de
ACM
325 hists__browser_title(self->hists, title, sizeof(title),
326 ev_name, self->dso_filter,
327 self->thread_filter);
328 ui_browser__show_title(&self->b, title);
329 continue;
4694153c 330 case 'D': { /* Debug */
d1b4f249
ACM
331 static int seq;
332 struct hist_entry *h = rb_entry(self->b.top,
333 struct hist_entry, rb_node);
334 ui_helpline__pop();
335 ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
336 seq++, self->b.nr_entries,
337 self->hists->nr_entries,
338 self->b.height,
339 self->b.index,
340 self->b.top_idx,
341 h->row_offset, h->nr_rows);
342 }
3c916cc2
ACM
343 break;
344 case 'C':
345 /* Collapse the whole world. */
346 hist_browser__set_folding(self, false);
347 break;
348 case 'E':
349 /* Expand the whole world. */
350 hist_browser__set_folding(self, true);
351 break;
d1b4f249
ACM
352 case NEWT_KEY_ENTER:
353 if (hist_browser__toggle_fold(self))
354 break;
355 /* fall thru */
356 default:
b50e003d 357 goto out;
d1b4f249
ACM
358 }
359 }
b50e003d 360out:
59e8fe32 361 ui_browser__hide(&self->b);
b50e003d 362 return key;
d1b4f249
ACM
363}
364
365static char *callchain_list__sym_name(struct callchain_list *self,
366 char *bf, size_t bfsize)
367{
368 if (self->ms.sym)
369 return self->ms.sym->name;
370
9486aa38 371 snprintf(bf, bfsize, "%#" PRIx64, self->ip);
d1b4f249
ACM
372 return bf;
373}
374
375#define LEVEL_OFFSET_STEP 3
376
377static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self,
378 struct callchain_node *chain_node,
379 u64 total, int level,
380 unsigned short row,
381 off_t *row_offset,
382 bool *is_current_entry)
383{
384 struct rb_node *node;
385 int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
386 u64 new_total, remaining;
387
388 if (callchain_param.mode == CHAIN_GRAPH_REL)
389 new_total = chain_node->children_hit;
390 else
391 new_total = total;
392
393 remaining = new_total;
394 node = rb_first(&chain_node->rb_root);
395 while (node) {
396 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
397 struct rb_node *next = rb_next(node);
f08c3154 398 u64 cumul = callchain_cumul_hits(child);
d1b4f249
ACM
399 struct callchain_list *chain;
400 char folded_sign = ' ';
401 int first = true;
402 int extra_offset = 0;
403
404 remaining -= cumul;
405
406 list_for_each_entry(chain, &child->val, list) {
407 char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str;
408 const char *str;
409 int color;
410 bool was_first = first;
411
163caed9 412 if (first)
d1b4f249 413 first = false;
163caed9 414 else
d1b4f249 415 extra_offset = LEVEL_OFFSET_STEP;
d1b4f249
ACM
416
417 folded_sign = callchain_list__folded(chain);
418 if (*row_offset != 0) {
419 --*row_offset;
420 goto do_next;
421 }
422
423 alloc_str = NULL;
424 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
425 if (was_first) {
426 double percent = cumul * 100.0 / new_total;
427
428 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
429 str = "Not enough memory!";
430 else
431 str = alloc_str;
432 }
433
434 color = HE_COLORSET_NORMAL;
435 width = self->b.width - (offset + extra_offset + 2);
436 if (ui_browser__is_current_entry(&self->b, row)) {
437 self->selection = &chain->ms;
438 color = HE_COLORSET_SELECTED;
439 *is_current_entry = true;
440 }
441
8f9bbc40
ACM
442 ui_browser__set_color(&self->b, color);
443 ui_browser__gotorc(&self->b, row, 0);
d1b4f249
ACM
444 slsmg_write_nstring(" ", offset + extra_offset);
445 slsmg_printf("%c ", folded_sign);
446 slsmg_write_nstring(str, width);
447 free(alloc_str);
448
449 if (++row == self->b.height)
450 goto out;
451do_next:
452 if (folded_sign == '+')
453 break;
454 }
455
456 if (folded_sign == '-') {
457 const int new_level = level + (extra_offset ? 2 : 1);
458 row += hist_browser__show_callchain_node_rb_tree(self, child, new_total,
459 new_level, row, row_offset,
460 is_current_entry);
461 }
462 if (row == self->b.height)
463 goto out;
464 node = next;
465 }
466out:
467 return row - first_row;
468}
469
470static int hist_browser__show_callchain_node(struct hist_browser *self,
471 struct callchain_node *node,
472 int level, unsigned short row,
473 off_t *row_offset,
474 bool *is_current_entry)
475{
476 struct callchain_list *chain;
477 int first_row = row,
478 offset = level * LEVEL_OFFSET_STEP,
479 width = self->b.width - offset;
480 char folded_sign = ' ';
481
482 list_for_each_entry(chain, &node->val, list) {
483 char ipstr[BITS_PER_LONG / 4 + 1], *s;
484 int color;
163caed9 485
d1b4f249
ACM
486 folded_sign = callchain_list__folded(chain);
487
488 if (*row_offset != 0) {
489 --*row_offset;
490 continue;
491 }
492
493 color = HE_COLORSET_NORMAL;
494 if (ui_browser__is_current_entry(&self->b, row)) {
495 self->selection = &chain->ms;
496 color = HE_COLORSET_SELECTED;
497 *is_current_entry = true;
498 }
499
500 s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
8f9bbc40
ACM
501 ui_browser__gotorc(&self->b, row, 0);
502 ui_browser__set_color(&self->b, color);
d1b4f249
ACM
503 slsmg_write_nstring(" ", offset);
504 slsmg_printf("%c ", folded_sign);
505 slsmg_write_nstring(s, width - 2);
506
507 if (++row == self->b.height)
508 goto out;
509 }
510
511 if (folded_sign == '-')
512 row += hist_browser__show_callchain_node_rb_tree(self, node,
513 self->hists->stats.total_period,
514 level + 1, row,
515 row_offset,
516 is_current_entry);
517out:
518 return row - first_row;
519}
520
521static int hist_browser__show_callchain(struct hist_browser *self,
522 struct rb_root *chain,
523 int level, unsigned short row,
524 off_t *row_offset,
525 bool *is_current_entry)
526{
527 struct rb_node *nd;
528 int first_row = row;
529
530 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
531 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
532
533 row += hist_browser__show_callchain_node(self, node, level,
534 row, row_offset,
535 is_current_entry);
536 if (row == self->b.height)
537 break;
538 }
539
540 return row - first_row;
541}
542
543static int hist_browser__show_entry(struct hist_browser *self,
544 struct hist_entry *entry,
545 unsigned short row)
546{
547 char s[256];
548 double percent;
549 int printed = 0;
f1cf602c 550 int width = self->b.width - 6; /* The percentage */
d1b4f249
ACM
551 char folded_sign = ' ';
552 bool current_entry = ui_browser__is_current_entry(&self->b, row);
553 off_t row_offset = entry->row_offset;
554
555 if (current_entry) {
556 self->he_selection = entry;
557 self->selection = &entry->ms;
558 }
559
560 if (symbol_conf.use_callchain) {
163caed9 561 hist_entry__init_have_children(entry);
d1b4f249
ACM
562 folded_sign = hist_entry__folded(entry);
563 }
564
565 if (row_offset == 0) {
f1cf602c 566 hist_entry__snprintf(entry, s, sizeof(s), self->hists);
d1b4f249
ACM
567 percent = (entry->period * 100.0) / self->hists->stats.total_period;
568
c172f742 569 ui_browser__set_percent_color(&self->b, percent, current_entry);
8f9bbc40 570 ui_browser__gotorc(&self->b, row, 0);
d1b4f249
ACM
571 if (symbol_conf.use_callchain) {
572 slsmg_printf("%c ", folded_sign);
573 width -= 2;
574 }
c172f742 575
f1cf602c
ACM
576 slsmg_printf(" %5.2f%%", percent);
577
c172f742
ACM
578 /* The scroll bar isn't being used */
579 if (!self->b.navkeypressed)
580 width += 1;
581
d1b4f249
ACM
582 slsmg_write_nstring(s, width);
583 ++row;
584 ++printed;
585 } else
586 --row_offset;
587
588 if (folded_sign == '-' && row != self->b.height) {
589 printed += hist_browser__show_callchain(self, &entry->sorted_chain,
590 1, row, &row_offset,
591 &current_entry);
592 if (current_entry)
593 self->he_selection = entry;
594 }
595
596 return printed;
597}
598
437cfe7a
ACM
599static void ui_browser__hists_init_top(struct ui_browser *browser)
600{
601 if (browser->top == NULL) {
602 struct hist_browser *hb;
603
604 hb = container_of(browser, struct hist_browser, b);
605 browser->top = rb_first(&hb->hists->entries);
606 }
607}
608
d1b4f249
ACM
609static unsigned int hist_browser__refresh(struct ui_browser *self)
610{
611 unsigned row = 0;
612 struct rb_node *nd;
613 struct hist_browser *hb = container_of(self, struct hist_browser, b);
614
437cfe7a 615 ui_browser__hists_init_top(self);
d1b4f249
ACM
616
617 for (nd = self->top; nd; nd = rb_next(nd)) {
618 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
619
620 if (h->filtered)
621 continue;
622
623 row += hist_browser__show_entry(hb, h, row);
624 if (row == self->height)
625 break;
626 }
627
628 return row;
629}
630
631static struct rb_node *hists__filter_entries(struct rb_node *nd)
632{
633 while (nd != NULL) {
634 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
635 if (!h->filtered)
636 return nd;
637
638 nd = rb_next(nd);
639 }
640
641 return NULL;
642}
643
644static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
645{
646 while (nd != NULL) {
647 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
648 if (!h->filtered)
649 return nd;
650
651 nd = rb_prev(nd);
652 }
653
654 return NULL;
655}
656
657static void ui_browser__hists_seek(struct ui_browser *self,
658 off_t offset, int whence)
659{
660 struct hist_entry *h;
661 struct rb_node *nd;
662 bool first = true;
663
60098917
ACM
664 if (self->nr_entries == 0)
665 return;
666
437cfe7a
ACM
667 ui_browser__hists_init_top(self);
668
d1b4f249
ACM
669 switch (whence) {
670 case SEEK_SET:
671 nd = hists__filter_entries(rb_first(self->entries));
672 break;
673 case SEEK_CUR:
674 nd = self->top;
675 goto do_offset;
676 case SEEK_END:
677 nd = hists__filter_prev_entries(rb_last(self->entries));
678 first = false;
679 break;
680 default:
681 return;
682 }
683
684 /*
685 * Moves not relative to the first visible entry invalidates its
686 * row_offset:
687 */
688 h = rb_entry(self->top, struct hist_entry, rb_node);
689 h->row_offset = 0;
690
691 /*
692 * Here we have to check if nd is expanded (+), if it is we can't go
693 * the next top level hist_entry, instead we must compute an offset of
694 * what _not_ to show and not change the first visible entry.
695 *
696 * This offset increments when we are going from top to bottom and
697 * decreases when we're going from bottom to top.
698 *
699 * As we don't have backpointers to the top level in the callchains
700 * structure, we need to always print the whole hist_entry callchain,
701 * skipping the first ones that are before the first visible entry
702 * and stop when we printed enough lines to fill the screen.
703 */
704do_offset:
705 if (offset > 0) {
706 do {
707 h = rb_entry(nd, struct hist_entry, rb_node);
708 if (h->ms.unfolded) {
709 u16 remaining = h->nr_rows - h->row_offset;
710 if (offset > remaining) {
711 offset -= remaining;
712 h->row_offset = 0;
713 } else {
714 h->row_offset += offset;
715 offset = 0;
716 self->top = nd;
717 break;
718 }
719 }
720 nd = hists__filter_entries(rb_next(nd));
721 if (nd == NULL)
722 break;
723 --offset;
724 self->top = nd;
725 } while (offset != 0);
726 } else if (offset < 0) {
727 while (1) {
728 h = rb_entry(nd, struct hist_entry, rb_node);
729 if (h->ms.unfolded) {
730 if (first) {
731 if (-offset > h->row_offset) {
732 offset += h->row_offset;
733 h->row_offset = 0;
734 } else {
735 h->row_offset += offset;
736 offset = 0;
737 self->top = nd;
738 break;
739 }
740 } else {
741 if (-offset > h->nr_rows) {
742 offset += h->nr_rows;
743 h->row_offset = 0;
744 } else {
745 h->row_offset = h->nr_rows + offset;
746 offset = 0;
747 self->top = nd;
748 break;
749 }
750 }
751 }
752
753 nd = hists__filter_prev_entries(rb_prev(nd));
754 if (nd == NULL)
755 break;
756 ++offset;
757 self->top = nd;
758 if (offset == 0) {
759 /*
760 * Last unfiltered hist_entry, check if it is
761 * unfolded, if it is then we should have
762 * row_offset at its last entry.
763 */
764 h = rb_entry(nd, struct hist_entry, rb_node);
765 if (h->ms.unfolded)
766 h->row_offset = h->nr_rows;
767 break;
768 }
769 first = false;
770 }
771 } else {
772 self->top = nd;
773 h = rb_entry(nd, struct hist_entry, rb_node);
774 h->row_offset = 0;
775 }
776}
777
778static struct hist_browser *hist_browser__new(struct hists *hists)
779{
780 struct hist_browser *self = zalloc(sizeof(*self));
781
782 if (self) {
783 self->hists = hists;
784 self->b.refresh = hist_browser__refresh;
785 self->b.seek = ui_browser__hists_seek;
c172f742 786 self->b.use_navkeypressed = true,
724c9c9f 787 self->has_symbols = sort_sym.list.next != NULL;
d1b4f249
ACM
788 }
789
790 return self;
791}
792
793static void hist_browser__delete(struct hist_browser *self)
794{
d1b4f249
ACM
795 free(self);
796}
797
798static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
799{
800 return self->he_selection;
801}
802
803static struct thread *hist_browser__selected_thread(struct hist_browser *self)
804{
805 return self->he_selection->thread;
806}
807
469917ce
ACM
808static int hists__browser_title(struct hists *self, char *bf, size_t size,
809 const char *ev_name, const struct dso *dso,
810 const struct thread *thread)
d1b4f249 811{
469917ce
ACM
812 char unit;
813 int printed;
814 unsigned long nr_events = self->stats.nr_events[PERF_RECORD_SAMPLE];
815
816 nr_events = convert_unit(nr_events, &unit);
817 printed = snprintf(bf, size, "Events: %lu%c %s", nr_events, unit, ev_name);
d1b4f249
ACM
818
819 if (thread)
820 printed += snprintf(bf + printed, size - printed,
469917ce
ACM
821 ", Thread: %s(%d)",
822 (thread->comm_set ? thread->comm : ""),
d1b4f249
ACM
823 thread->pid);
824 if (dso)
825 printed += snprintf(bf + printed, size - printed,
469917ce
ACM
826 ", DSO: %s", dso->short_name);
827 return printed;
d1b4f249
ACM
828}
829
34958544 830static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
7f0030b2 831 const char *helpline, const char *ev_name,
81cce8de
ACM
832 bool left_exits,
833 void(*timer)(void *arg), void *arg,
834 int delay_secs)
d1b4f249 835{
7f0030b2 836 struct hists *self = &evsel->hists;
d1b4f249
ACM
837 struct hist_browser *browser = hist_browser__new(self);
838 struct pstack *fstack;
d1b4f249
ACM
839 int key = -1;
840
841 if (browser == NULL)
842 return -1;
843
844 fstack = pstack__new(2);
845 if (fstack == NULL)
846 goto out;
847
848 ui_helpline__push(helpline);
849
d1b4f249 850 while (1) {
60098917
ACM
851 const struct thread *thread = NULL;
852 const struct dso *dso = NULL;
d1b4f249
ACM
853 char *options[16];
854 int nr_options = 0, choice = 0, i,
855 annotate = -2, zoom_dso = -2, zoom_thread = -2,
856 browse_map = -2;
857
81cce8de 858 key = hist_browser__run(browser, ev_name, timer, arg, delay_secs);
d1b4f249 859
60098917
ACM
860 if (browser->he_selection != NULL) {
861 thread = hist_browser__selected_thread(browser);
862 dso = browser->selection->map ? browser->selection->map->dso : NULL;
863 }
d1b4f249 864
b50e003d 865 switch (key) {
b50e003d
ACM
866 case NEWT_KEY_TAB:
867 case NEWT_KEY_UNTAB:
868 /*
869 * Exit the browser, let hists__browser_tree
870 * go to the next or previous
871 */
872 goto out_free_stack;
873 case 'a':
60098917 874 if (browser->selection == NULL ||
db9a9cbc 875 browser->selection->sym == NULL ||
b50e003d 876 browser->selection->map->dso->annotate_warned)
d1b4f249 877 continue;
b50e003d
ACM
878 goto do_annotate;
879 case 'd':
880 goto zoom_dso;
881 case 't':
882 goto zoom_thread;
3c916cc2 883 case NEWT_KEY_F1:
b50e003d
ACM
884 case 'h':
885 case '?':
2d5646c0
ACM
886 ui__help_window("h/?/F1 Show this window\n"
887 "UP/DOWN/PGUP\n"
888 "PGDN/SPACE Navigate\n"
889 "q/ESC/CTRL+C Exit browser\n\n"
890 "For multiple event sessions:\n\n"
891 "TAB/UNTAB Switch events\n\n"
724c9c9f 892 "For symbolic views (--sort has sym):\n\n"
2d5646c0
ACM
893 "-> Zoom into DSO/Threads & Annotate current symbol\n"
894 "<- Zoom out\n"
895 "a Annotate current symbol\n"
896 "C Collapse all callchains\n"
897 "E Expand all callchains\n"
898 "d Zoom into current DSO\n"
899 "t Zoom into current Thread\n");
b50e003d
ACM
900 continue;
901 case NEWT_KEY_ENTER:
902 case NEWT_KEY_RIGHT:
903 /* menu */
904 break;
905 case NEWT_KEY_LEFT: {
906 const void *top;
d1b4f249 907
7f0030b2
ACM
908 if (pstack__empty(fstack)) {
909 /*
910 * Go back to the perf_evsel_menu__run or other user
911 */
912 if (left_exits)
913 goto out_free_stack;
d1b4f249 914 continue;
7f0030b2 915 }
b50e003d 916 top = pstack__pop(fstack);
81cce8de 917 if (top == &browser->dso_filter)
b50e003d 918 goto zoom_out_dso;
81cce8de 919 if (top == &browser->thread_filter)
b50e003d
ACM
920 goto zoom_out_thread;
921 continue;
922 }
923 case NEWT_KEY_ESCAPE:
7f0030b2
ACM
924 if (!left_exits &&
925 !ui__dialog_yesno("Do you really want to exit?"))
b50e003d
ACM
926 continue;
927 /* Fall thru */
ed7e5662
ACM
928 case 'q':
929 case CTRL('c'):
b50e003d 930 goto out_free_stack;
ed7e5662
ACM
931 default:
932 continue;
d1b4f249
ACM
933 }
934
724c9c9f
ACM
935 if (!browser->has_symbols)
936 goto add_exit_option;
937
60098917
ACM
938 if (browser->selection != NULL &&
939 browser->selection->sym != NULL &&
d1b4f249
ACM
940 !browser->selection->map->dso->annotate_warned &&
941 asprintf(&options[nr_options], "Annotate %s",
942 browser->selection->sym->name) > 0)
943 annotate = nr_options++;
944
945 if (thread != NULL &&
946 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
81cce8de 947 (browser->thread_filter ? "out of" : "into"),
d1b4f249
ACM
948 (thread->comm_set ? thread->comm : ""),
949 thread->pid) > 0)
950 zoom_thread = nr_options++;
951
952 if (dso != NULL &&
953 asprintf(&options[nr_options], "Zoom %s %s DSO",
81cce8de 954 (browser->dso_filter ? "out of" : "into"),
d1b4f249
ACM
955 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
956 zoom_dso = nr_options++;
957
60098917
ACM
958 if (browser->selection != NULL &&
959 browser->selection->map != NULL &&
d1b4f249
ACM
960 asprintf(&options[nr_options], "Browse map details") > 0)
961 browse_map = nr_options++;
724c9c9f 962add_exit_option:
d1b4f249
ACM
963 options[nr_options++] = (char *)"Exit";
964
1e6dd077 965 choice = ui__popup_menu(nr_options, options);
d1b4f249
ACM
966
967 for (i = 0; i < nr_options - 1; ++i)
968 free(options[i]);
969
970 if (choice == nr_options - 1)
971 break;
972
973 if (choice == -1)
974 continue;
975
976 if (choice == annotate) {
977 struct hist_entry *he;
978do_annotate:
d1b4f249
ACM
979 he = hist_browser__selected_entry(browser);
980 if (he == NULL)
981 continue;
df71d95f
ACM
982 /*
983 * Don't let this be freed, say, by hists__decay_entry.
984 */
985 he->used = true;
34958544 986 hist_entry__tui_annotate(he, evsel->idx, nr_events,
81cce8de 987 timer, arg, delay_secs);
df71d95f 988 he->used = false;
900e14a8 989 ui_browser__update_nr_entries(&browser->b, browser->hists->nr_entries);
d1b4f249
ACM
990 } else if (choice == browse_map)
991 map__browse(browser->selection->map);
992 else if (choice == zoom_dso) {
993zoom_dso:
81cce8de
ACM
994 if (browser->dso_filter) {
995 pstack__remove(fstack, &browser->dso_filter);
d1b4f249
ACM
996zoom_out_dso:
997 ui_helpline__pop();
81cce8de 998 browser->dso_filter = NULL;
d1b4f249
ACM
999 } else {
1000 if (dso == NULL)
1001 continue;
1002 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
1003 dso->kernel ? "the Kernel" : dso->short_name);
81cce8de
ACM
1004 browser->dso_filter = dso;
1005 pstack__push(fstack, &browser->dso_filter);
d1b4f249 1006 }
81cce8de 1007 hists__filter_by_dso(self, browser->dso_filter);
d1b4f249
ACM
1008 hist_browser__reset(browser);
1009 } else if (choice == zoom_thread) {
1010zoom_thread:
81cce8de
ACM
1011 if (browser->thread_filter) {
1012 pstack__remove(fstack, &browser->thread_filter);
d1b4f249
ACM
1013zoom_out_thread:
1014 ui_helpline__pop();
81cce8de 1015 browser->thread_filter = NULL;
d1b4f249
ACM
1016 } else {
1017 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
1018 thread->comm_set ? thread->comm : "",
1019 thread->pid);
81cce8de
ACM
1020 browser->thread_filter = thread;
1021 pstack__push(fstack, &browser->thread_filter);
d1b4f249 1022 }
81cce8de 1023 hists__filter_by_thread(self, browser->thread_filter);
d1b4f249
ACM
1024 hist_browser__reset(browser);
1025 }
1026 }
1027out_free_stack:
1028 pstack__delete(fstack);
1029out:
1030 hist_browser__delete(browser);
1031 return key;
1032}
1033
7f0030b2
ACM
1034struct perf_evsel_menu {
1035 struct ui_browser b;
1036 struct perf_evsel *selection;
1037};
1038
1039static void perf_evsel_menu__write(struct ui_browser *browser,
1040 void *entry, int row)
1041{
1042 struct perf_evsel_menu *menu = container_of(browser,
1043 struct perf_evsel_menu, b);
1044 struct perf_evsel *evsel = list_entry(entry, struct perf_evsel, node);
1045 bool current_entry = ui_browser__is_current_entry(browser, row);
1046 unsigned long nr_events = evsel->hists.stats.nr_events[PERF_RECORD_SAMPLE];
1047 const char *ev_name = event_name(evsel);
1048 char bf[256], unit;
1049
1050 ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
1051 HE_COLORSET_NORMAL);
1052
1053 nr_events = convert_unit(nr_events, &unit);
1054 snprintf(bf, sizeof(bf), "%lu%c%s%s", nr_events,
1055 unit, unit == ' ' ? "" : " ", ev_name);
1056 slsmg_write_nstring(bf, browser->width);
1057
1058 if (current_entry)
1059 menu->selection = evsel;
1060}
1061
34958544
ACM
1062static int perf_evsel_menu__run(struct perf_evsel_menu *menu,
1063 int nr_events, const char *help,
81cce8de 1064 void(*timer)(void *arg), void *arg, int delay_secs)
d1b4f249 1065{
7f0030b2 1066 struct perf_evlist *evlist = menu->b.priv;
e248de33 1067 struct perf_evsel *pos;
7f0030b2
ACM
1068 const char *ev_name, *title = "Available samples";
1069 int key;
d1b4f249 1070
7f0030b2
ACM
1071 if (ui_browser__show(&menu->b, title,
1072 "ESC: exit, ENTER|->: Browse histograms") < 0)
1073 return -1;
1074
7f0030b2 1075 while (1) {
3af6e338 1076 key = ui_browser__run(&menu->b, delay_secs);
7f0030b2
ACM
1077
1078 switch (key) {
81cce8de
ACM
1079 case -1:
1080 /* FIXME we need to check if it was es.reason == NEWT_EXIT_TIMER */
1081 timer(arg);
1082 continue;
7f0030b2
ACM
1083 case NEWT_KEY_RIGHT:
1084 case NEWT_KEY_ENTER:
1085 if (!menu->selection)
1086 continue;
1087 pos = menu->selection;
1088browse_hists:
18eaf0b8
ACM
1089 perf_evlist__set_selected(evlist, pos);
1090 /*
1091 * Give the calling tool a chance to populate the non
1092 * default evsel resorted hists tree.
1093 */
1094 if (timer)
1095 timer(arg);
7f0030b2 1096 ev_name = event_name(pos);
34958544
ACM
1097 key = perf_evsel__hists_browse(pos, nr_events, help,
1098 ev_name, true, timer,
1099 arg, delay_secs);
7f0030b2 1100 ui_browser__show_title(&menu->b, title);
18eaf0b8
ACM
1101 switch (key) {
1102 case NEWT_KEY_TAB:
1103 if (pos->node.next == &evlist->entries)
1104 pos = list_entry(evlist->entries.next, struct perf_evsel, node);
1105 else
1106 pos = list_entry(pos->node.next, struct perf_evsel, node);
1107 goto browse_hists;
1108 case NEWT_KEY_UNTAB:
1109 if (pos->node.prev == &evlist->entries)
1110 pos = list_entry(evlist->entries.prev, struct perf_evsel, node);
1111 else
1112 pos = list_entry(pos->node.prev, struct perf_evsel, node);
1113 goto browse_hists;
1114 case NEWT_KEY_ESCAPE:
1115 if (!ui__dialog_yesno("Do you really want to exit?"))
1116 continue;
1117 /* Fall thru */
1118 case 'q':
1119 case CTRL('c'):
1120 goto out;
1121 default:
1122 continue;
1123 }
7f0030b2
ACM
1124 case NEWT_KEY_LEFT:
1125 continue;
ed7e5662
ACM
1126 case NEWT_KEY_ESCAPE:
1127 if (!ui__dialog_yesno("Do you really want to exit?"))
1128 continue;
1129 /* Fall thru */
7f0030b2
ACM
1130 case 'q':
1131 case CTRL('c'):
1132 goto out;
d1b4f249 1133 default:
18eaf0b8 1134 continue;
d1b4f249
ACM
1135 }
1136 }
1137
7f0030b2
ACM
1138out:
1139 ui_browser__hide(&menu->b);
1140 return key;
1141}
1142
1143static int __perf_evlist__tui_browse_hists(struct perf_evlist *evlist,
81cce8de
ACM
1144 const char *help,
1145 void(*timer)(void *arg), void *arg,
1146 int delay_secs)
7f0030b2
ACM
1147{
1148 struct perf_evsel *pos;
1149 struct perf_evsel_menu menu = {
1150 .b = {
1151 .entries = &evlist->entries,
1152 .refresh = ui_browser__list_head_refresh,
1153 .seek = ui_browser__list_head_seek,
1154 .write = perf_evsel_menu__write,
1155 .nr_entries = evlist->nr_entries,
1156 .priv = evlist,
1157 },
1158 };
1159
1160 ui_helpline__push("Press ESC to exit");
1161
1162 list_for_each_entry(pos, &evlist->entries, node) {
1163 const char *ev_name = event_name(pos);
1164 size_t line_len = strlen(ev_name) + 7;
1165
1166 if (menu.b.width < line_len)
1167 menu.b.width = line_len;
1168 /*
1169 * Cache the evsel name, tracepoints have a _high_ cost per
1170 * event_name() call.
1171 */
1172 if (pos->name == NULL)
1173 pos->name = strdup(ev_name);
1174 }
1175
34958544
ACM
1176 return perf_evsel_menu__run(&menu, evlist->nr_entries, help, timer,
1177 arg, delay_secs);
7f0030b2
ACM
1178}
1179
81cce8de
ACM
1180int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help,
1181 void(*timer)(void *arg), void *arg,
1182 int delay_secs)
7f0030b2
ACM
1183{
1184
1185 if (evlist->nr_entries == 1) {
1186 struct perf_evsel *first = list_entry(evlist->entries.next,
1187 struct perf_evsel, node);
1188 const char *ev_name = event_name(first);
34958544
ACM
1189 return perf_evsel__hists_browse(first, evlist->nr_entries, help,
1190 ev_name, false, timer, arg,
1191 delay_secs);
7f0030b2
ACM
1192 }
1193
81cce8de
ACM
1194 return __perf_evlist__tui_browse_hists(evlist, help,
1195 timer, arg, delay_secs);
d1b4f249 1196}