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