perf ui/tui: Add 'F' hotkey to toggle percentage output
[deliverable/linux.git] / tools / perf / ui / browsers / hists.c
1 #include <stdio.h>
2 #include "../libslang.h"
3 #include <stdlib.h>
4 #include <string.h>
5 #include <linux/rbtree.h>
6
7 #include "../../util/evsel.h"
8 #include "../../util/evlist.h"
9 #include "../../util/hist.h"
10 #include "../../util/pstack.h"
11 #include "../../util/sort.h"
12 #include "../../util/util.h"
13 #include "../../arch/common.h"
14
15 #include "../browser.h"
16 #include "../helpline.h"
17 #include "../util.h"
18 #include "../ui.h"
19 #include "map.h"
20
21 struct hist_browser {
22 struct ui_browser b;
23 struct hists *hists;
24 struct hist_entry *he_selection;
25 struct map_symbol *selection;
26 int print_seq;
27 bool show_dso;
28 float min_pcnt;
29 u64 nr_pcnt_entries;
30 };
31
32 extern void hist_browser__init_hpp(void);
33
34 static int hists__browser_title(struct hists *hists, char *bf, size_t size,
35 const char *ev_name);
36
37 static void hist_browser__refresh_dimensions(struct hist_browser *browser)
38 {
39 /* 3 == +/- toggle symbol before actual hist_entry rendering */
40 browser->b.width = 3 + (hists__sort_list_width(browser->hists) +
41 sizeof("[k]"));
42 }
43
44 static void hist_browser__reset(struct hist_browser *browser)
45 {
46 browser->b.nr_entries = browser->hists->nr_entries;
47 hist_browser__refresh_dimensions(browser);
48 ui_browser__reset_index(&browser->b);
49 }
50
51 static char tree__folded_sign(bool unfolded)
52 {
53 return unfolded ? '-' : '+';
54 }
55
56 static char map_symbol__folded(const struct map_symbol *ms)
57 {
58 return ms->has_children ? tree__folded_sign(ms->unfolded) : ' ';
59 }
60
61 static char hist_entry__folded(const struct hist_entry *he)
62 {
63 return map_symbol__folded(&he->ms);
64 }
65
66 static char callchain_list__folded(const struct callchain_list *cl)
67 {
68 return map_symbol__folded(&cl->ms);
69 }
70
71 static void map_symbol__set_folding(struct map_symbol *ms, bool unfold)
72 {
73 ms->unfolded = unfold ? ms->has_children : false;
74 }
75
76 static int callchain_node__count_rows_rb_tree(struct callchain_node *node)
77 {
78 int n = 0;
79 struct rb_node *nd;
80
81 for (nd = rb_first(&node->rb_root); nd; nd = rb_next(nd)) {
82 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
83 struct callchain_list *chain;
84 char folded_sign = ' '; /* No children */
85
86 list_for_each_entry(chain, &child->val, list) {
87 ++n;
88 /* We need this because we may not have children */
89 folded_sign = callchain_list__folded(chain);
90 if (folded_sign == '+')
91 break;
92 }
93
94 if (folded_sign == '-') /* Have children and they're unfolded */
95 n += callchain_node__count_rows_rb_tree(child);
96 }
97
98 return n;
99 }
100
101 static int callchain_node__count_rows(struct callchain_node *node)
102 {
103 struct callchain_list *chain;
104 bool unfolded = false;
105 int n = 0;
106
107 list_for_each_entry(chain, &node->val, list) {
108 ++n;
109 unfolded = chain->ms.unfolded;
110 }
111
112 if (unfolded)
113 n += callchain_node__count_rows_rb_tree(node);
114
115 return n;
116 }
117
118 static int callchain__count_rows(struct rb_root *chain)
119 {
120 struct rb_node *nd;
121 int n = 0;
122
123 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
124 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
125 n += callchain_node__count_rows(node);
126 }
127
128 return n;
129 }
130
131 static bool map_symbol__toggle_fold(struct map_symbol *ms)
132 {
133 if (!ms)
134 return false;
135
136 if (!ms->has_children)
137 return false;
138
139 ms->unfolded = !ms->unfolded;
140 return true;
141 }
142
143 static void callchain_node__init_have_children_rb_tree(struct callchain_node *node)
144 {
145 struct rb_node *nd = rb_first(&node->rb_root);
146
147 for (nd = rb_first(&node->rb_root); nd; nd = rb_next(nd)) {
148 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
149 struct callchain_list *chain;
150 bool first = true;
151
152 list_for_each_entry(chain, &child->val, list) {
153 if (first) {
154 first = false;
155 chain->ms.has_children = chain->list.next != &child->val ||
156 !RB_EMPTY_ROOT(&child->rb_root);
157 } else
158 chain->ms.has_children = chain->list.next == &child->val &&
159 !RB_EMPTY_ROOT(&child->rb_root);
160 }
161
162 callchain_node__init_have_children_rb_tree(child);
163 }
164 }
165
166 static void callchain_node__init_have_children(struct callchain_node *node)
167 {
168 struct callchain_list *chain;
169
170 list_for_each_entry(chain, &node->val, list)
171 chain->ms.has_children = !RB_EMPTY_ROOT(&node->rb_root);
172
173 callchain_node__init_have_children_rb_tree(node);
174 }
175
176 static void callchain__init_have_children(struct rb_root *root)
177 {
178 struct rb_node *nd;
179
180 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
181 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
182 callchain_node__init_have_children(node);
183 }
184 }
185
186 static void hist_entry__init_have_children(struct hist_entry *he)
187 {
188 if (!he->init_have_children) {
189 he->ms.has_children = !RB_EMPTY_ROOT(&he->sorted_chain);
190 callchain__init_have_children(&he->sorted_chain);
191 he->init_have_children = true;
192 }
193 }
194
195 static bool hist_browser__toggle_fold(struct hist_browser *browser)
196 {
197 if (map_symbol__toggle_fold(browser->selection)) {
198 struct hist_entry *he = browser->he_selection;
199
200 hist_entry__init_have_children(he);
201 browser->hists->nr_entries -= he->nr_rows;
202
203 if (he->ms.unfolded)
204 he->nr_rows = callchain__count_rows(&he->sorted_chain);
205 else
206 he->nr_rows = 0;
207 browser->hists->nr_entries += he->nr_rows;
208 browser->b.nr_entries = browser->hists->nr_entries;
209
210 return true;
211 }
212
213 /* If it doesn't have children, no toggling performed */
214 return false;
215 }
216
217 static int callchain_node__set_folding_rb_tree(struct callchain_node *node, bool unfold)
218 {
219 int n = 0;
220 struct rb_node *nd;
221
222 for (nd = rb_first(&node->rb_root); nd; nd = rb_next(nd)) {
223 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
224 struct callchain_list *chain;
225 bool has_children = false;
226
227 list_for_each_entry(chain, &child->val, list) {
228 ++n;
229 map_symbol__set_folding(&chain->ms, unfold);
230 has_children = chain->ms.has_children;
231 }
232
233 if (has_children)
234 n += callchain_node__set_folding_rb_tree(child, unfold);
235 }
236
237 return n;
238 }
239
240 static int callchain_node__set_folding(struct callchain_node *node, bool unfold)
241 {
242 struct callchain_list *chain;
243 bool has_children = false;
244 int n = 0;
245
246 list_for_each_entry(chain, &node->val, list) {
247 ++n;
248 map_symbol__set_folding(&chain->ms, unfold);
249 has_children = chain->ms.has_children;
250 }
251
252 if (has_children)
253 n += callchain_node__set_folding_rb_tree(node, unfold);
254
255 return n;
256 }
257
258 static int callchain__set_folding(struct rb_root *chain, bool unfold)
259 {
260 struct rb_node *nd;
261 int n = 0;
262
263 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
264 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
265 n += callchain_node__set_folding(node, unfold);
266 }
267
268 return n;
269 }
270
271 static void hist_entry__set_folding(struct hist_entry *he, bool unfold)
272 {
273 hist_entry__init_have_children(he);
274 map_symbol__set_folding(&he->ms, unfold);
275
276 if (he->ms.has_children) {
277 int n = callchain__set_folding(&he->sorted_chain, unfold);
278 he->nr_rows = unfold ? n : 0;
279 } else
280 he->nr_rows = 0;
281 }
282
283 static void hists__set_folding(struct hists *hists, bool unfold)
284 {
285 struct rb_node *nd;
286
287 hists->nr_entries = 0;
288
289 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
290 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
291 hist_entry__set_folding(he, unfold);
292 hists->nr_entries += 1 + he->nr_rows;
293 }
294 }
295
296 static void hist_browser__set_folding(struct hist_browser *browser, bool unfold)
297 {
298 hists__set_folding(browser->hists, unfold);
299 browser->b.nr_entries = browser->hists->nr_entries;
300 /* Go to the start, we may be way after valid entries after a collapse */
301 ui_browser__reset_index(&browser->b);
302 }
303
304 static void ui_browser__warn_lost_events(struct ui_browser *browser)
305 {
306 ui_browser__warning(browser, 4,
307 "Events are being lost, check IO/CPU overload!\n\n"
308 "You may want to run 'perf' using a RT scheduler policy:\n\n"
309 " perf top -r 80\n\n"
310 "Or reduce the sampling frequency.");
311 }
312
313 static void hist_browser__update_pcnt_entries(struct hist_browser *hb);
314
315 static int hist_browser__run(struct hist_browser *browser, const char *ev_name,
316 struct hist_browser_timer *hbt)
317 {
318 int key;
319 char title[160];
320 int delay_secs = hbt ? hbt->refresh : 0;
321
322 browser->b.entries = &browser->hists->entries;
323 browser->b.nr_entries = browser->hists->nr_entries;
324 if (browser->min_pcnt)
325 browser->b.nr_entries = browser->nr_pcnt_entries;
326
327 hist_browser__refresh_dimensions(browser);
328 hists__browser_title(browser->hists, title, sizeof(title), ev_name);
329
330 if (ui_browser__show(&browser->b, title,
331 "Press '?' for help on key bindings") < 0)
332 return -1;
333
334 while (1) {
335 key = ui_browser__run(&browser->b, delay_secs);
336
337 switch (key) {
338 case K_TIMER: {
339 u64 nr_entries;
340 hbt->timer(hbt->arg);
341
342 if (browser->min_pcnt) {
343 hist_browser__update_pcnt_entries(browser);
344 nr_entries = browser->nr_pcnt_entries;
345 } else {
346 nr_entries = browser->hists->nr_entries;
347 }
348
349 ui_browser__update_nr_entries(&browser->b, nr_entries);
350
351 if (browser->hists->stats.nr_lost_warned !=
352 browser->hists->stats.nr_events[PERF_RECORD_LOST]) {
353 browser->hists->stats.nr_lost_warned =
354 browser->hists->stats.nr_events[PERF_RECORD_LOST];
355 ui_browser__warn_lost_events(&browser->b);
356 }
357
358 hists__browser_title(browser->hists, title, sizeof(title), ev_name);
359 ui_browser__show_title(&browser->b, title);
360 continue;
361 }
362 case 'D': { /* Debug */
363 static int seq;
364 struct hist_entry *h = rb_entry(browser->b.top,
365 struct hist_entry, rb_node);
366 ui_helpline__pop();
367 ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
368 seq++, browser->b.nr_entries,
369 browser->hists->nr_entries,
370 browser->b.height,
371 browser->b.index,
372 browser->b.top_idx,
373 h->row_offset, h->nr_rows);
374 }
375 break;
376 case 'C':
377 /* Collapse the whole world. */
378 hist_browser__set_folding(browser, false);
379 break;
380 case 'E':
381 /* Expand the whole world. */
382 hist_browser__set_folding(browser, true);
383 break;
384 case K_ENTER:
385 if (hist_browser__toggle_fold(browser))
386 break;
387 /* fall thru */
388 default:
389 goto out;
390 }
391 }
392 out:
393 ui_browser__hide(&browser->b);
394 return key;
395 }
396
397 static char *callchain_list__sym_name(struct callchain_list *cl,
398 char *bf, size_t bfsize, bool show_dso)
399 {
400 int printed;
401
402 if (cl->ms.sym)
403 printed = scnprintf(bf, bfsize, "%s", cl->ms.sym->name);
404 else
405 printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);
406
407 if (show_dso)
408 scnprintf(bf + printed, bfsize - printed, " %s",
409 cl->ms.map ? cl->ms.map->dso->short_name : "unknown");
410
411 return bf;
412 }
413
414 #define LEVEL_OFFSET_STEP 3
415
416 static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *browser,
417 struct callchain_node *chain_node,
418 u64 total, int level,
419 unsigned short row,
420 off_t *row_offset,
421 bool *is_current_entry)
422 {
423 struct rb_node *node;
424 int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
425 u64 new_total, remaining;
426
427 if (callchain_param.mode == CHAIN_GRAPH_REL)
428 new_total = chain_node->children_hit;
429 else
430 new_total = total;
431
432 remaining = new_total;
433 node = rb_first(&chain_node->rb_root);
434 while (node) {
435 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
436 struct rb_node *next = rb_next(node);
437 u64 cumul = callchain_cumul_hits(child);
438 struct callchain_list *chain;
439 char folded_sign = ' ';
440 int first = true;
441 int extra_offset = 0;
442
443 remaining -= cumul;
444
445 list_for_each_entry(chain, &child->val, list) {
446 char bf[1024], *alloc_str;
447 const char *str;
448 int color;
449 bool was_first = first;
450
451 if (first)
452 first = false;
453 else
454 extra_offset = LEVEL_OFFSET_STEP;
455
456 folded_sign = callchain_list__folded(chain);
457 if (*row_offset != 0) {
458 --*row_offset;
459 goto do_next;
460 }
461
462 alloc_str = NULL;
463 str = callchain_list__sym_name(chain, bf, sizeof(bf),
464 browser->show_dso);
465 if (was_first) {
466 double percent = cumul * 100.0 / new_total;
467
468 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
469 str = "Not enough memory!";
470 else
471 str = alloc_str;
472 }
473
474 color = HE_COLORSET_NORMAL;
475 width = browser->b.width - (offset + extra_offset + 2);
476 if (ui_browser__is_current_entry(&browser->b, row)) {
477 browser->selection = &chain->ms;
478 color = HE_COLORSET_SELECTED;
479 *is_current_entry = true;
480 }
481
482 ui_browser__set_color(&browser->b, color);
483 ui_browser__gotorc(&browser->b, row, 0);
484 slsmg_write_nstring(" ", offset + extra_offset);
485 slsmg_printf("%c ", folded_sign);
486 slsmg_write_nstring(str, width);
487 free(alloc_str);
488
489 if (++row == browser->b.height)
490 goto out;
491 do_next:
492 if (folded_sign == '+')
493 break;
494 }
495
496 if (folded_sign == '-') {
497 const int new_level = level + (extra_offset ? 2 : 1);
498 row += hist_browser__show_callchain_node_rb_tree(browser, child, new_total,
499 new_level, row, row_offset,
500 is_current_entry);
501 }
502 if (row == browser->b.height)
503 goto out;
504 node = next;
505 }
506 out:
507 return row - first_row;
508 }
509
510 static int hist_browser__show_callchain_node(struct hist_browser *browser,
511 struct callchain_node *node,
512 int level, unsigned short row,
513 off_t *row_offset,
514 bool *is_current_entry)
515 {
516 struct callchain_list *chain;
517 int first_row = row,
518 offset = level * LEVEL_OFFSET_STEP,
519 width = browser->b.width - offset;
520 char folded_sign = ' ';
521
522 list_for_each_entry(chain, &node->val, list) {
523 char bf[1024], *s;
524 int color;
525
526 folded_sign = callchain_list__folded(chain);
527
528 if (*row_offset != 0) {
529 --*row_offset;
530 continue;
531 }
532
533 color = HE_COLORSET_NORMAL;
534 if (ui_browser__is_current_entry(&browser->b, row)) {
535 browser->selection = &chain->ms;
536 color = HE_COLORSET_SELECTED;
537 *is_current_entry = true;
538 }
539
540 s = callchain_list__sym_name(chain, bf, sizeof(bf),
541 browser->show_dso);
542 ui_browser__gotorc(&browser->b, row, 0);
543 ui_browser__set_color(&browser->b, color);
544 slsmg_write_nstring(" ", offset);
545 slsmg_printf("%c ", folded_sign);
546 slsmg_write_nstring(s, width - 2);
547
548 if (++row == browser->b.height)
549 goto out;
550 }
551
552 if (folded_sign == '-')
553 row += hist_browser__show_callchain_node_rb_tree(browser, node,
554 browser->hists->stats.total_period,
555 level + 1, row,
556 row_offset,
557 is_current_entry);
558 out:
559 return row - first_row;
560 }
561
562 static int hist_browser__show_callchain(struct hist_browser *browser,
563 struct rb_root *chain,
564 int level, unsigned short row,
565 off_t *row_offset,
566 bool *is_current_entry)
567 {
568 struct rb_node *nd;
569 int first_row = row;
570
571 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
572 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
573
574 row += hist_browser__show_callchain_node(browser, node, level,
575 row, row_offset,
576 is_current_entry);
577 if (row == browser->b.height)
578 break;
579 }
580
581 return row - first_row;
582 }
583
584 struct hpp_arg {
585 struct ui_browser *b;
586 char folded_sign;
587 bool current_entry;
588 };
589
590 static int __hpp__overhead_callback(struct perf_hpp *hpp, bool front)
591 {
592 struct hpp_arg *arg = hpp->ptr;
593
594 if (arg->current_entry && arg->b->navkeypressed)
595 ui_browser__set_color(arg->b, HE_COLORSET_SELECTED);
596 else
597 ui_browser__set_color(arg->b, HE_COLORSET_NORMAL);
598
599 if (front) {
600 if (!symbol_conf.use_callchain)
601 return 0;
602
603 slsmg_printf("%c ", arg->folded_sign);
604 return 2;
605 }
606
607 return 0;
608 }
609
610 static int __hpp__color_callback(struct perf_hpp *hpp, bool front __maybe_unused)
611 {
612 struct hpp_arg *arg = hpp->ptr;
613
614 if (!arg->current_entry || !arg->b->navkeypressed)
615 ui_browser__set_color(arg->b, HE_COLORSET_NORMAL);
616 return 0;
617 }
618
619 static int __hpp__slsmg_color_printf(struct perf_hpp *hpp, const char *fmt, ...)
620 {
621 struct hpp_arg *arg = hpp->ptr;
622 int ret;
623 va_list args;
624 double percent;
625
626 va_start(args, fmt);
627 percent = va_arg(args, double);
628 va_end(args);
629
630 ui_browser__set_percent_color(arg->b, percent, arg->current_entry);
631
632 ret = scnprintf(hpp->buf, hpp->size, fmt, percent);
633 slsmg_printf("%s", hpp->buf);
634
635 advance_hpp(hpp, ret);
636 return ret;
637 }
638
639 #define __HPP_COLOR_PERCENT_FN(_type, _field, _cb) \
640 static u64 __hpp_get_##_field(struct hist_entry *he) \
641 { \
642 return he->stat._field; \
643 } \
644 \
645 static int \
646 hist_browser__hpp_color_##_type(struct perf_hpp_fmt *fmt __maybe_unused,\
647 struct perf_hpp *hpp, \
648 struct hist_entry *he) \
649 { \
650 return __hpp__fmt(hpp, he, __hpp_get_##_field, _cb, " %6.2f%%", \
651 __hpp__slsmg_color_printf, true); \
652 }
653
654 __HPP_COLOR_PERCENT_FN(overhead, period, __hpp__overhead_callback)
655 __HPP_COLOR_PERCENT_FN(overhead_sys, period_sys, __hpp__color_callback)
656 __HPP_COLOR_PERCENT_FN(overhead_us, period_us, __hpp__color_callback)
657 __HPP_COLOR_PERCENT_FN(overhead_guest_sys, period_guest_sys, __hpp__color_callback)
658 __HPP_COLOR_PERCENT_FN(overhead_guest_us, period_guest_us, __hpp__color_callback)
659
660 #undef __HPP_COLOR_PERCENT_FN
661
662 void hist_browser__init_hpp(void)
663 {
664 perf_hpp__init();
665
666 perf_hpp__format[PERF_HPP__OVERHEAD].color =
667 hist_browser__hpp_color_overhead;
668 perf_hpp__format[PERF_HPP__OVERHEAD_SYS].color =
669 hist_browser__hpp_color_overhead_sys;
670 perf_hpp__format[PERF_HPP__OVERHEAD_US].color =
671 hist_browser__hpp_color_overhead_us;
672 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_SYS].color =
673 hist_browser__hpp_color_overhead_guest_sys;
674 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_US].color =
675 hist_browser__hpp_color_overhead_guest_us;
676 }
677
678 static int hist_browser__show_entry(struct hist_browser *browser,
679 struct hist_entry *entry,
680 unsigned short row)
681 {
682 char s[256];
683 int printed = 0;
684 int width = browser->b.width;
685 char folded_sign = ' ';
686 bool current_entry = ui_browser__is_current_entry(&browser->b, row);
687 off_t row_offset = entry->row_offset;
688 bool first = true;
689 struct perf_hpp_fmt *fmt;
690
691 if (current_entry) {
692 browser->he_selection = entry;
693 browser->selection = &entry->ms;
694 }
695
696 if (symbol_conf.use_callchain) {
697 hist_entry__init_have_children(entry);
698 folded_sign = hist_entry__folded(entry);
699 }
700
701 if (row_offset == 0) {
702 struct hpp_arg arg = {
703 .b = &browser->b,
704 .folded_sign = folded_sign,
705 .current_entry = current_entry,
706 };
707 struct perf_hpp hpp = {
708 .buf = s,
709 .size = sizeof(s),
710 .ptr = &arg,
711 };
712
713 ui_browser__gotorc(&browser->b, row, 0);
714
715 perf_hpp__for_each_format(fmt) {
716 if (!first) {
717 slsmg_printf(" ");
718 width -= 2;
719 }
720 first = false;
721
722 if (fmt->color) {
723 width -= fmt->color(fmt, &hpp, entry);
724 } else {
725 width -= fmt->entry(fmt, &hpp, entry);
726 slsmg_printf("%s", s);
727 }
728 }
729
730 /* The scroll bar isn't being used */
731 if (!browser->b.navkeypressed)
732 width += 1;
733
734 hist_entry__sort_snprintf(entry, s, sizeof(s), browser->hists);
735 slsmg_write_nstring(s, width);
736 ++row;
737 ++printed;
738 } else
739 --row_offset;
740
741 if (folded_sign == '-' && row != browser->b.height) {
742 printed += hist_browser__show_callchain(browser, &entry->sorted_chain,
743 1, row, &row_offset,
744 &current_entry);
745 if (current_entry)
746 browser->he_selection = entry;
747 }
748
749 return printed;
750 }
751
752 static void ui_browser__hists_init_top(struct ui_browser *browser)
753 {
754 if (browser->top == NULL) {
755 struct hist_browser *hb;
756
757 hb = container_of(browser, struct hist_browser, b);
758 browser->top = rb_first(&hb->hists->entries);
759 }
760 }
761
762 static unsigned int hist_browser__refresh(struct ui_browser *browser)
763 {
764 unsigned row = 0;
765 struct rb_node *nd;
766 struct hist_browser *hb = container_of(browser, struct hist_browser, b);
767
768 ui_browser__hists_init_top(browser);
769
770 for (nd = browser->top; nd; nd = rb_next(nd)) {
771 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
772 u64 total = hists__total_period(h->hists);
773 float percent = 0.0;
774
775 if (h->filtered)
776 continue;
777
778 if (total)
779 percent = h->stat.period * 100.0 / total;
780
781 if (percent < hb->min_pcnt)
782 continue;
783
784 row += hist_browser__show_entry(hb, h, row);
785 if (row == browser->height)
786 break;
787 }
788
789 return row;
790 }
791
792 static struct rb_node *hists__filter_entries(struct rb_node *nd,
793 struct hists *hists,
794 float min_pcnt)
795 {
796 while (nd != NULL) {
797 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
798 u64 total = hists__total_period(hists);
799 float percent = 0.0;
800
801 if (total)
802 percent = h->stat.period * 100.0 / total;
803
804 if (percent < min_pcnt)
805 return NULL;
806
807 if (!h->filtered)
808 return nd;
809
810 nd = rb_next(nd);
811 }
812
813 return NULL;
814 }
815
816 static struct rb_node *hists__filter_prev_entries(struct rb_node *nd,
817 struct hists *hists,
818 float min_pcnt)
819 {
820 while (nd != NULL) {
821 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
822 u64 total = hists__total_period(hists);
823 float percent = 0.0;
824
825 if (total)
826 percent = h->stat.period * 100.0 / total;
827
828 if (!h->filtered && percent >= min_pcnt)
829 return nd;
830
831 nd = rb_prev(nd);
832 }
833
834 return NULL;
835 }
836
837 static void ui_browser__hists_seek(struct ui_browser *browser,
838 off_t offset, int whence)
839 {
840 struct hist_entry *h;
841 struct rb_node *nd;
842 bool first = true;
843 struct hist_browser *hb;
844
845 hb = container_of(browser, struct hist_browser, b);
846
847 if (browser->nr_entries == 0)
848 return;
849
850 ui_browser__hists_init_top(browser);
851
852 switch (whence) {
853 case SEEK_SET:
854 nd = hists__filter_entries(rb_first(browser->entries),
855 hb->hists, hb->min_pcnt);
856 break;
857 case SEEK_CUR:
858 nd = browser->top;
859 goto do_offset;
860 case SEEK_END:
861 nd = hists__filter_prev_entries(rb_last(browser->entries),
862 hb->hists, hb->min_pcnt);
863 first = false;
864 break;
865 default:
866 return;
867 }
868
869 /*
870 * Moves not relative to the first visible entry invalidates its
871 * row_offset:
872 */
873 h = rb_entry(browser->top, struct hist_entry, rb_node);
874 h->row_offset = 0;
875
876 /*
877 * Here we have to check if nd is expanded (+), if it is we can't go
878 * the next top level hist_entry, instead we must compute an offset of
879 * what _not_ to show and not change the first visible entry.
880 *
881 * This offset increments when we are going from top to bottom and
882 * decreases when we're going from bottom to top.
883 *
884 * As we don't have backpointers to the top level in the callchains
885 * structure, we need to always print the whole hist_entry callchain,
886 * skipping the first ones that are before the first visible entry
887 * and stop when we printed enough lines to fill the screen.
888 */
889 do_offset:
890 if (offset > 0) {
891 do {
892 h = rb_entry(nd, struct hist_entry, rb_node);
893 if (h->ms.unfolded) {
894 u16 remaining = h->nr_rows - h->row_offset;
895 if (offset > remaining) {
896 offset -= remaining;
897 h->row_offset = 0;
898 } else {
899 h->row_offset += offset;
900 offset = 0;
901 browser->top = nd;
902 break;
903 }
904 }
905 nd = hists__filter_entries(rb_next(nd), hb->hists,
906 hb->min_pcnt);
907 if (nd == NULL)
908 break;
909 --offset;
910 browser->top = nd;
911 } while (offset != 0);
912 } else if (offset < 0) {
913 while (1) {
914 h = rb_entry(nd, struct hist_entry, rb_node);
915 if (h->ms.unfolded) {
916 if (first) {
917 if (-offset > h->row_offset) {
918 offset += h->row_offset;
919 h->row_offset = 0;
920 } else {
921 h->row_offset += offset;
922 offset = 0;
923 browser->top = nd;
924 break;
925 }
926 } else {
927 if (-offset > h->nr_rows) {
928 offset += h->nr_rows;
929 h->row_offset = 0;
930 } else {
931 h->row_offset = h->nr_rows + offset;
932 offset = 0;
933 browser->top = nd;
934 break;
935 }
936 }
937 }
938
939 nd = hists__filter_prev_entries(rb_prev(nd), hb->hists,
940 hb->min_pcnt);
941 if (nd == NULL)
942 break;
943 ++offset;
944 browser->top = nd;
945 if (offset == 0) {
946 /*
947 * Last unfiltered hist_entry, check if it is
948 * unfolded, if it is then we should have
949 * row_offset at its last entry.
950 */
951 h = rb_entry(nd, struct hist_entry, rb_node);
952 if (h->ms.unfolded)
953 h->row_offset = h->nr_rows;
954 break;
955 }
956 first = false;
957 }
958 } else {
959 browser->top = nd;
960 h = rb_entry(nd, struct hist_entry, rb_node);
961 h->row_offset = 0;
962 }
963 }
964
965 static int hist_browser__fprintf_callchain_node_rb_tree(struct hist_browser *browser,
966 struct callchain_node *chain_node,
967 u64 total, int level,
968 FILE *fp)
969 {
970 struct rb_node *node;
971 int offset = level * LEVEL_OFFSET_STEP;
972 u64 new_total, remaining;
973 int printed = 0;
974
975 if (callchain_param.mode == CHAIN_GRAPH_REL)
976 new_total = chain_node->children_hit;
977 else
978 new_total = total;
979
980 remaining = new_total;
981 node = rb_first(&chain_node->rb_root);
982 while (node) {
983 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
984 struct rb_node *next = rb_next(node);
985 u64 cumul = callchain_cumul_hits(child);
986 struct callchain_list *chain;
987 char folded_sign = ' ';
988 int first = true;
989 int extra_offset = 0;
990
991 remaining -= cumul;
992
993 list_for_each_entry(chain, &child->val, list) {
994 char bf[1024], *alloc_str;
995 const char *str;
996 bool was_first = first;
997
998 if (first)
999 first = false;
1000 else
1001 extra_offset = LEVEL_OFFSET_STEP;
1002
1003 folded_sign = callchain_list__folded(chain);
1004
1005 alloc_str = NULL;
1006 str = callchain_list__sym_name(chain, bf, sizeof(bf),
1007 browser->show_dso);
1008 if (was_first) {
1009 double percent = cumul * 100.0 / new_total;
1010
1011 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
1012 str = "Not enough memory!";
1013 else
1014 str = alloc_str;
1015 }
1016
1017 printed += fprintf(fp, "%*s%c %s\n", offset + extra_offset, " ", folded_sign, str);
1018 free(alloc_str);
1019 if (folded_sign == '+')
1020 break;
1021 }
1022
1023 if (folded_sign == '-') {
1024 const int new_level = level + (extra_offset ? 2 : 1);
1025 printed += hist_browser__fprintf_callchain_node_rb_tree(browser, child, new_total,
1026 new_level, fp);
1027 }
1028
1029 node = next;
1030 }
1031
1032 return printed;
1033 }
1034
1035 static int hist_browser__fprintf_callchain_node(struct hist_browser *browser,
1036 struct callchain_node *node,
1037 int level, FILE *fp)
1038 {
1039 struct callchain_list *chain;
1040 int offset = level * LEVEL_OFFSET_STEP;
1041 char folded_sign = ' ';
1042 int printed = 0;
1043
1044 list_for_each_entry(chain, &node->val, list) {
1045 char bf[1024], *s;
1046
1047 folded_sign = callchain_list__folded(chain);
1048 s = callchain_list__sym_name(chain, bf, sizeof(bf), browser->show_dso);
1049 printed += fprintf(fp, "%*s%c %s\n", offset, " ", folded_sign, s);
1050 }
1051
1052 if (folded_sign == '-')
1053 printed += hist_browser__fprintf_callchain_node_rb_tree(browser, node,
1054 browser->hists->stats.total_period,
1055 level + 1, fp);
1056 return printed;
1057 }
1058
1059 static int hist_browser__fprintf_callchain(struct hist_browser *browser,
1060 struct rb_root *chain, int level, FILE *fp)
1061 {
1062 struct rb_node *nd;
1063 int printed = 0;
1064
1065 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
1066 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
1067
1068 printed += hist_browser__fprintf_callchain_node(browser, node, level, fp);
1069 }
1070
1071 return printed;
1072 }
1073
1074 static int hist_browser__fprintf_entry(struct hist_browser *browser,
1075 struct hist_entry *he, FILE *fp)
1076 {
1077 char s[8192];
1078 double percent;
1079 int printed = 0;
1080 char folded_sign = ' ';
1081
1082 if (symbol_conf.use_callchain)
1083 folded_sign = hist_entry__folded(he);
1084
1085 hist_entry__sort_snprintf(he, s, sizeof(s), browser->hists);
1086 percent = (he->stat.period * 100.0) / browser->hists->stats.total_period;
1087
1088 if (symbol_conf.use_callchain)
1089 printed += fprintf(fp, "%c ", folded_sign);
1090
1091 printed += fprintf(fp, " %5.2f%%", percent);
1092
1093 if (symbol_conf.show_nr_samples)
1094 printed += fprintf(fp, " %11u", he->stat.nr_events);
1095
1096 if (symbol_conf.show_total_period)
1097 printed += fprintf(fp, " %12" PRIu64, he->stat.period);
1098
1099 printed += fprintf(fp, "%s\n", rtrim(s));
1100
1101 if (folded_sign == '-')
1102 printed += hist_browser__fprintf_callchain(browser, &he->sorted_chain, 1, fp);
1103
1104 return printed;
1105 }
1106
1107 static int hist_browser__fprintf(struct hist_browser *browser, FILE *fp)
1108 {
1109 struct rb_node *nd = hists__filter_entries(rb_first(browser->b.entries),
1110 browser->hists,
1111 browser->min_pcnt);
1112 int printed = 0;
1113
1114 while (nd) {
1115 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1116
1117 printed += hist_browser__fprintf_entry(browser, h, fp);
1118 nd = hists__filter_entries(rb_next(nd), browser->hists,
1119 browser->min_pcnt);
1120 }
1121
1122 return printed;
1123 }
1124
1125 static int hist_browser__dump(struct hist_browser *browser)
1126 {
1127 char filename[64];
1128 FILE *fp;
1129
1130 while (1) {
1131 scnprintf(filename, sizeof(filename), "perf.hist.%d", browser->print_seq);
1132 if (access(filename, F_OK))
1133 break;
1134 /*
1135 * XXX: Just an arbitrary lazy upper limit
1136 */
1137 if (++browser->print_seq == 8192) {
1138 ui_helpline__fpush("Too many perf.hist.N files, nothing written!");
1139 return -1;
1140 }
1141 }
1142
1143 fp = fopen(filename, "w");
1144 if (fp == NULL) {
1145 char bf[64];
1146 const char *err = strerror_r(errno, bf, sizeof(bf));
1147 ui_helpline__fpush("Couldn't write to %s: %s", filename, err);
1148 return -1;
1149 }
1150
1151 ++browser->print_seq;
1152 hist_browser__fprintf(browser, fp);
1153 fclose(fp);
1154 ui_helpline__fpush("%s written!", filename);
1155
1156 return 0;
1157 }
1158
1159 static struct hist_browser *hist_browser__new(struct hists *hists)
1160 {
1161 struct hist_browser *browser = zalloc(sizeof(*browser));
1162
1163 if (browser) {
1164 browser->hists = hists;
1165 browser->b.refresh = hist_browser__refresh;
1166 browser->b.seek = ui_browser__hists_seek;
1167 browser->b.use_navkeypressed = true;
1168 }
1169
1170 return browser;
1171 }
1172
1173 static void hist_browser__delete(struct hist_browser *browser)
1174 {
1175 free(browser);
1176 }
1177
1178 static struct hist_entry *hist_browser__selected_entry(struct hist_browser *browser)
1179 {
1180 return browser->he_selection;
1181 }
1182
1183 static struct thread *hist_browser__selected_thread(struct hist_browser *browser)
1184 {
1185 return browser->he_selection->thread;
1186 }
1187
1188 static int hists__browser_title(struct hists *hists, char *bf, size_t size,
1189 const char *ev_name)
1190 {
1191 char unit;
1192 int printed;
1193 const struct dso *dso = hists->dso_filter;
1194 const struct thread *thread = hists->thread_filter;
1195 unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
1196 u64 nr_events = hists->stats.total_period;
1197 struct perf_evsel *evsel = hists_to_evsel(hists);
1198 char buf[512];
1199 size_t buflen = sizeof(buf);
1200
1201 if (symbol_conf.filter_relative) {
1202 nr_samples = hists->stats.nr_non_filtered_samples;
1203 nr_events = hists->stats.total_non_filtered_period;
1204 }
1205
1206 if (perf_evsel__is_group_event(evsel)) {
1207 struct perf_evsel *pos;
1208
1209 perf_evsel__group_desc(evsel, buf, buflen);
1210 ev_name = buf;
1211
1212 for_each_group_member(pos, evsel) {
1213 if (symbol_conf.filter_relative) {
1214 nr_samples += pos->hists.stats.nr_non_filtered_samples;
1215 nr_events += pos->hists.stats.total_non_filtered_period;
1216 } else {
1217 nr_samples += pos->hists.stats.nr_events[PERF_RECORD_SAMPLE];
1218 nr_events += pos->hists.stats.total_period;
1219 }
1220 }
1221 }
1222
1223 nr_samples = convert_unit(nr_samples, &unit);
1224 printed = scnprintf(bf, size,
1225 "Samples: %lu%c of event '%s', Event count (approx.): %lu",
1226 nr_samples, unit, ev_name, nr_events);
1227
1228
1229 if (hists->uid_filter_str)
1230 printed += snprintf(bf + printed, size - printed,
1231 ", UID: %s", hists->uid_filter_str);
1232 if (thread)
1233 printed += scnprintf(bf + printed, size - printed,
1234 ", Thread: %s(%d)",
1235 (thread->comm_set ? thread__comm_str(thread) : ""),
1236 thread->tid);
1237 if (dso)
1238 printed += scnprintf(bf + printed, size - printed,
1239 ", DSO: %s", dso->short_name);
1240 return printed;
1241 }
1242
1243 static inline void free_popup_options(char **options, int n)
1244 {
1245 int i;
1246
1247 for (i = 0; i < n; ++i)
1248 zfree(&options[i]);
1249 }
1250
1251 /* Check whether the browser is for 'top' or 'report' */
1252 static inline bool is_report_browser(void *timer)
1253 {
1254 return timer == NULL;
1255 }
1256
1257 /*
1258 * Only runtime switching of perf data file will make "input_name" point
1259 * to a malloced buffer. So add "is_input_name_malloced" flag to decide
1260 * whether we need to call free() for current "input_name" during the switch.
1261 */
1262 static bool is_input_name_malloced = false;
1263
1264 static int switch_data_file(void)
1265 {
1266 char *pwd, *options[32], *abs_path[32], *tmp;
1267 DIR *pwd_dir;
1268 int nr_options = 0, choice = -1, ret = -1;
1269 struct dirent *dent;
1270
1271 pwd = getenv("PWD");
1272 if (!pwd)
1273 return ret;
1274
1275 pwd_dir = opendir(pwd);
1276 if (!pwd_dir)
1277 return ret;
1278
1279 memset(options, 0, sizeof(options));
1280 memset(options, 0, sizeof(abs_path));
1281
1282 while ((dent = readdir(pwd_dir))) {
1283 char path[PATH_MAX];
1284 u64 magic;
1285 char *name = dent->d_name;
1286 FILE *file;
1287
1288 if (!(dent->d_type == DT_REG))
1289 continue;
1290
1291 snprintf(path, sizeof(path), "%s/%s", pwd, name);
1292
1293 file = fopen(path, "r");
1294 if (!file)
1295 continue;
1296
1297 if (fread(&magic, 1, 8, file) < 8)
1298 goto close_file_and_continue;
1299
1300 if (is_perf_magic(magic)) {
1301 options[nr_options] = strdup(name);
1302 if (!options[nr_options])
1303 goto close_file_and_continue;
1304
1305 abs_path[nr_options] = strdup(path);
1306 if (!abs_path[nr_options]) {
1307 zfree(&options[nr_options]);
1308 ui__warning("Can't search all data files due to memory shortage.\n");
1309 fclose(file);
1310 break;
1311 }
1312
1313 nr_options++;
1314 }
1315
1316 close_file_and_continue:
1317 fclose(file);
1318 if (nr_options >= 32) {
1319 ui__warning("Too many perf data files in PWD!\n"
1320 "Only the first 32 files will be listed.\n");
1321 break;
1322 }
1323 }
1324 closedir(pwd_dir);
1325
1326 if (nr_options) {
1327 choice = ui__popup_menu(nr_options, options);
1328 if (choice < nr_options && choice >= 0) {
1329 tmp = strdup(abs_path[choice]);
1330 if (tmp) {
1331 if (is_input_name_malloced)
1332 free((void *)input_name);
1333 input_name = tmp;
1334 is_input_name_malloced = true;
1335 ret = 0;
1336 } else
1337 ui__warning("Data switch failed due to memory shortage!\n");
1338 }
1339 }
1340
1341 free_popup_options(options, nr_options);
1342 free_popup_options(abs_path, nr_options);
1343 return ret;
1344 }
1345
1346 static void hist_browser__update_pcnt_entries(struct hist_browser *hb)
1347 {
1348 u64 nr_entries = 0;
1349 struct rb_node *nd = rb_first(&hb->hists->entries);
1350
1351 while (nd) {
1352 nr_entries++;
1353 nd = hists__filter_entries(rb_next(nd), hb->hists,
1354 hb->min_pcnt);
1355 }
1356
1357 hb->nr_pcnt_entries = nr_entries;
1358 }
1359
1360 static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
1361 const char *helpline, const char *ev_name,
1362 bool left_exits,
1363 struct hist_browser_timer *hbt,
1364 float min_pcnt,
1365 struct perf_session_env *env)
1366 {
1367 struct hists *hists = &evsel->hists;
1368 struct hist_browser *browser = hist_browser__new(hists);
1369 struct branch_info *bi;
1370 struct pstack *fstack;
1371 char *options[16];
1372 int nr_options = 0;
1373 int key = -1;
1374 char buf[64];
1375 char script_opt[64];
1376 int delay_secs = hbt ? hbt->refresh : 0;
1377
1378 #define HIST_BROWSER_HELP_COMMON \
1379 "h/?/F1 Show this window\n" \
1380 "UP/DOWN/PGUP\n" \
1381 "PGDN/SPACE Navigate\n" \
1382 "q/ESC/CTRL+C Exit browser\n\n" \
1383 "For multiple event sessions:\n\n" \
1384 "TAB/UNTAB Switch events\n\n" \
1385 "For symbolic views (--sort has sym):\n\n" \
1386 "-> Zoom into DSO/Threads & Annotate current symbol\n" \
1387 "<- Zoom out\n" \
1388 "a Annotate current symbol\n" \
1389 "C Collapse all callchains\n" \
1390 "d Zoom into current DSO\n" \
1391 "E Expand all callchains\n" \
1392 "F Toggle percentage of filtered entries\n" \
1393
1394 /* help messages are sorted by lexical order of the hotkey */
1395 const char report_help[] = HIST_BROWSER_HELP_COMMON
1396 "i Show header information\n"
1397 "P Print histograms to perf.hist.N\n"
1398 "r Run available scripts\n"
1399 "s Switch to another data file in PWD\n"
1400 "t Zoom into current Thread\n"
1401 "V Verbose (DSO names in callchains, etc)\n"
1402 "/ Filter symbol by name";
1403 const char top_help[] = HIST_BROWSER_HELP_COMMON
1404 "P Print histograms to perf.hist.N\n"
1405 "t Zoom into current Thread\n"
1406 "V Verbose (DSO names in callchains, etc)\n"
1407 "/ Filter symbol by name";
1408
1409 if (browser == NULL)
1410 return -1;
1411
1412 if (min_pcnt) {
1413 browser->min_pcnt = min_pcnt;
1414 hist_browser__update_pcnt_entries(browser);
1415 }
1416
1417 fstack = pstack__new(2);
1418 if (fstack == NULL)
1419 goto out;
1420
1421 ui_helpline__push(helpline);
1422
1423 memset(options, 0, sizeof(options));
1424
1425 while (1) {
1426 const struct thread *thread = NULL;
1427 const struct dso *dso = NULL;
1428 int choice = 0,
1429 annotate = -2, zoom_dso = -2, zoom_thread = -2,
1430 annotate_f = -2, annotate_t = -2, browse_map = -2;
1431 int scripts_comm = -2, scripts_symbol = -2,
1432 scripts_all = -2, switch_data = -2;
1433
1434 nr_options = 0;
1435
1436 key = hist_browser__run(browser, ev_name, hbt);
1437
1438 if (browser->he_selection != NULL) {
1439 thread = hist_browser__selected_thread(browser);
1440 dso = browser->selection->map ? browser->selection->map->dso : NULL;
1441 }
1442 switch (key) {
1443 case K_TAB:
1444 case K_UNTAB:
1445 if (nr_events == 1)
1446 continue;
1447 /*
1448 * Exit the browser, let hists__browser_tree
1449 * go to the next or previous
1450 */
1451 goto out_free_stack;
1452 case 'a':
1453 if (!sort__has_sym) {
1454 ui_browser__warning(&browser->b, delay_secs * 2,
1455 "Annotation is only available for symbolic views, "
1456 "include \"sym*\" in --sort to use it.");
1457 continue;
1458 }
1459
1460 if (browser->selection == NULL ||
1461 browser->selection->sym == NULL ||
1462 browser->selection->map->dso->annotate_warned)
1463 continue;
1464 goto do_annotate;
1465 case 'P':
1466 hist_browser__dump(browser);
1467 continue;
1468 case 'd':
1469 goto zoom_dso;
1470 case 'V':
1471 browser->show_dso = !browser->show_dso;
1472 continue;
1473 case 't':
1474 goto zoom_thread;
1475 case '/':
1476 if (ui_browser__input_window("Symbol to show",
1477 "Please enter the name of symbol you want to see",
1478 buf, "ENTER: OK, ESC: Cancel",
1479 delay_secs * 2) == K_ENTER) {
1480 hists->symbol_filter_str = *buf ? buf : NULL;
1481 hists__filter_by_symbol(hists);
1482 hist_browser__reset(browser);
1483 }
1484 continue;
1485 case 'r':
1486 if (is_report_browser(hbt))
1487 goto do_scripts;
1488 continue;
1489 case 's':
1490 if (is_report_browser(hbt))
1491 goto do_data_switch;
1492 continue;
1493 case 'i':
1494 /* env->arch is NULL for live-mode (i.e. perf top) */
1495 if (env->arch)
1496 tui__header_window(env);
1497 continue;
1498 case 'F':
1499 symbol_conf.filter_relative ^= 1;
1500 continue;
1501 case K_F1:
1502 case 'h':
1503 case '?':
1504 ui_browser__help_window(&browser->b,
1505 is_report_browser(hbt) ? report_help : top_help);
1506 continue;
1507 case K_ENTER:
1508 case K_RIGHT:
1509 /* menu */
1510 break;
1511 case K_LEFT: {
1512 const void *top;
1513
1514 if (pstack__empty(fstack)) {
1515 /*
1516 * Go back to the perf_evsel_menu__run or other user
1517 */
1518 if (left_exits)
1519 goto out_free_stack;
1520 continue;
1521 }
1522 top = pstack__pop(fstack);
1523 if (top == &browser->hists->dso_filter)
1524 goto zoom_out_dso;
1525 if (top == &browser->hists->thread_filter)
1526 goto zoom_out_thread;
1527 continue;
1528 }
1529 case K_ESC:
1530 if (!left_exits &&
1531 !ui_browser__dialog_yesno(&browser->b,
1532 "Do you really want to exit?"))
1533 continue;
1534 /* Fall thru */
1535 case 'q':
1536 case CTRL('c'):
1537 goto out_free_stack;
1538 default:
1539 continue;
1540 }
1541
1542 if (!sort__has_sym)
1543 goto add_exit_option;
1544
1545 if (sort__mode == SORT_MODE__BRANCH) {
1546 bi = browser->he_selection->branch_info;
1547 if (browser->selection != NULL &&
1548 bi &&
1549 bi->from.sym != NULL &&
1550 !bi->from.map->dso->annotate_warned &&
1551 asprintf(&options[nr_options], "Annotate %s",
1552 bi->from.sym->name) > 0)
1553 annotate_f = nr_options++;
1554
1555 if (browser->selection != NULL &&
1556 bi &&
1557 bi->to.sym != NULL &&
1558 !bi->to.map->dso->annotate_warned &&
1559 (bi->to.sym != bi->from.sym ||
1560 bi->to.map->dso != bi->from.map->dso) &&
1561 asprintf(&options[nr_options], "Annotate %s",
1562 bi->to.sym->name) > 0)
1563 annotate_t = nr_options++;
1564 } else {
1565
1566 if (browser->selection != NULL &&
1567 browser->selection->sym != NULL &&
1568 !browser->selection->map->dso->annotate_warned &&
1569 asprintf(&options[nr_options], "Annotate %s",
1570 browser->selection->sym->name) > 0)
1571 annotate = nr_options++;
1572 }
1573
1574 if (thread != NULL &&
1575 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
1576 (browser->hists->thread_filter ? "out of" : "into"),
1577 (thread->comm_set ? thread__comm_str(thread) : ""),
1578 thread->tid) > 0)
1579 zoom_thread = nr_options++;
1580
1581 if (dso != NULL &&
1582 asprintf(&options[nr_options], "Zoom %s %s DSO",
1583 (browser->hists->dso_filter ? "out of" : "into"),
1584 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
1585 zoom_dso = nr_options++;
1586
1587 if (browser->selection != NULL &&
1588 browser->selection->map != NULL &&
1589 asprintf(&options[nr_options], "Browse map details") > 0)
1590 browse_map = nr_options++;
1591
1592 /* perf script support */
1593 if (browser->he_selection) {
1594 struct symbol *sym;
1595
1596 if (asprintf(&options[nr_options], "Run scripts for samples of thread [%s]",
1597 thread__comm_str(browser->he_selection->thread)) > 0)
1598 scripts_comm = nr_options++;
1599
1600 sym = browser->he_selection->ms.sym;
1601 if (sym && sym->namelen &&
1602 asprintf(&options[nr_options], "Run scripts for samples of symbol [%s]",
1603 sym->name) > 0)
1604 scripts_symbol = nr_options++;
1605 }
1606
1607 if (asprintf(&options[nr_options], "Run scripts for all samples") > 0)
1608 scripts_all = nr_options++;
1609
1610 if (is_report_browser(hbt) && asprintf(&options[nr_options],
1611 "Switch to another data file in PWD") > 0)
1612 switch_data = nr_options++;
1613 add_exit_option:
1614 options[nr_options++] = (char *)"Exit";
1615 retry_popup_menu:
1616 choice = ui__popup_menu(nr_options, options);
1617
1618 if (choice == nr_options - 1)
1619 break;
1620
1621 if (choice == -1) {
1622 free_popup_options(options, nr_options - 1);
1623 continue;
1624 }
1625
1626 if (choice == annotate || choice == annotate_t || choice == annotate_f) {
1627 struct hist_entry *he;
1628 int err;
1629 do_annotate:
1630 if (!objdump_path && perf_session_env__lookup_objdump(env))
1631 continue;
1632
1633 he = hist_browser__selected_entry(browser);
1634 if (he == NULL)
1635 continue;
1636
1637 /*
1638 * we stash the branch_info symbol + map into the
1639 * the ms so we don't have to rewrite all the annotation
1640 * code to use branch_info.
1641 * in branch mode, the ms struct is not used
1642 */
1643 if (choice == annotate_f) {
1644 he->ms.sym = he->branch_info->from.sym;
1645 he->ms.map = he->branch_info->from.map;
1646 } else if (choice == annotate_t) {
1647 he->ms.sym = he->branch_info->to.sym;
1648 he->ms.map = he->branch_info->to.map;
1649 }
1650
1651 /*
1652 * Don't let this be freed, say, by hists__decay_entry.
1653 */
1654 he->used = true;
1655 err = hist_entry__tui_annotate(he, evsel, hbt);
1656 he->used = false;
1657 /*
1658 * offer option to annotate the other branch source or target
1659 * (if they exists) when returning from annotate
1660 */
1661 if ((err == 'q' || err == CTRL('c'))
1662 && annotate_t != -2 && annotate_f != -2)
1663 goto retry_popup_menu;
1664
1665 ui_browser__update_nr_entries(&browser->b, browser->hists->nr_entries);
1666 if (err)
1667 ui_browser__handle_resize(&browser->b);
1668
1669 } else if (choice == browse_map)
1670 map__browse(browser->selection->map);
1671 else if (choice == zoom_dso) {
1672 zoom_dso:
1673 if (browser->hists->dso_filter) {
1674 pstack__remove(fstack, &browser->hists->dso_filter);
1675 zoom_out_dso:
1676 ui_helpline__pop();
1677 browser->hists->dso_filter = NULL;
1678 sort_dso.elide = false;
1679 } else {
1680 if (dso == NULL)
1681 continue;
1682 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
1683 dso->kernel ? "the Kernel" : dso->short_name);
1684 browser->hists->dso_filter = dso;
1685 sort_dso.elide = true;
1686 pstack__push(fstack, &browser->hists->dso_filter);
1687 }
1688 hists__filter_by_dso(hists);
1689 hist_browser__reset(browser);
1690 } else if (choice == zoom_thread) {
1691 zoom_thread:
1692 if (browser->hists->thread_filter) {
1693 pstack__remove(fstack, &browser->hists->thread_filter);
1694 zoom_out_thread:
1695 ui_helpline__pop();
1696 browser->hists->thread_filter = NULL;
1697 sort_thread.elide = false;
1698 } else {
1699 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
1700 thread->comm_set ? thread__comm_str(thread) : "",
1701 thread->tid);
1702 browser->hists->thread_filter = thread;
1703 sort_thread.elide = true;
1704 pstack__push(fstack, &browser->hists->thread_filter);
1705 }
1706 hists__filter_by_thread(hists);
1707 hist_browser__reset(browser);
1708 }
1709 /* perf scripts support */
1710 else if (choice == scripts_all || choice == scripts_comm ||
1711 choice == scripts_symbol) {
1712 do_scripts:
1713 memset(script_opt, 0, 64);
1714
1715 if (choice == scripts_comm)
1716 sprintf(script_opt, " -c %s ", thread__comm_str(browser->he_selection->thread));
1717
1718 if (choice == scripts_symbol)
1719 sprintf(script_opt, " -S %s ", browser->he_selection->ms.sym->name);
1720
1721 script_browse(script_opt);
1722 }
1723 /* Switch to another data file */
1724 else if (choice == switch_data) {
1725 do_data_switch:
1726 if (!switch_data_file()) {
1727 key = K_SWITCH_INPUT_DATA;
1728 break;
1729 } else
1730 ui__warning("Won't switch the data files due to\n"
1731 "no valid data file get selected!\n");
1732 }
1733 }
1734 out_free_stack:
1735 pstack__delete(fstack);
1736 out:
1737 hist_browser__delete(browser);
1738 free_popup_options(options, nr_options - 1);
1739 return key;
1740 }
1741
1742 struct perf_evsel_menu {
1743 struct ui_browser b;
1744 struct perf_evsel *selection;
1745 bool lost_events, lost_events_warned;
1746 float min_pcnt;
1747 struct perf_session_env *env;
1748 };
1749
1750 static void perf_evsel_menu__write(struct ui_browser *browser,
1751 void *entry, int row)
1752 {
1753 struct perf_evsel_menu *menu = container_of(browser,
1754 struct perf_evsel_menu, b);
1755 struct perf_evsel *evsel = list_entry(entry, struct perf_evsel, node);
1756 bool current_entry = ui_browser__is_current_entry(browser, row);
1757 unsigned long nr_events = evsel->hists.stats.nr_events[PERF_RECORD_SAMPLE];
1758 const char *ev_name = perf_evsel__name(evsel);
1759 char bf[256], unit;
1760 const char *warn = " ";
1761 size_t printed;
1762
1763 ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
1764 HE_COLORSET_NORMAL);
1765
1766 if (perf_evsel__is_group_event(evsel)) {
1767 struct perf_evsel *pos;
1768
1769 ev_name = perf_evsel__group_name(evsel);
1770
1771 for_each_group_member(pos, evsel) {
1772 nr_events += pos->hists.stats.nr_events[PERF_RECORD_SAMPLE];
1773 }
1774 }
1775
1776 nr_events = convert_unit(nr_events, &unit);
1777 printed = scnprintf(bf, sizeof(bf), "%lu%c%s%s", nr_events,
1778 unit, unit == ' ' ? "" : " ", ev_name);
1779 slsmg_printf("%s", bf);
1780
1781 nr_events = evsel->hists.stats.nr_events[PERF_RECORD_LOST];
1782 if (nr_events != 0) {
1783 menu->lost_events = true;
1784 if (!current_entry)
1785 ui_browser__set_color(browser, HE_COLORSET_TOP);
1786 nr_events = convert_unit(nr_events, &unit);
1787 printed += scnprintf(bf, sizeof(bf), ": %ld%c%schunks LOST!",
1788 nr_events, unit, unit == ' ' ? "" : " ");
1789 warn = bf;
1790 }
1791
1792 slsmg_write_nstring(warn, browser->width - printed);
1793
1794 if (current_entry)
1795 menu->selection = evsel;
1796 }
1797
1798 static int perf_evsel_menu__run(struct perf_evsel_menu *menu,
1799 int nr_events, const char *help,
1800 struct hist_browser_timer *hbt)
1801 {
1802 struct perf_evlist *evlist = menu->b.priv;
1803 struct perf_evsel *pos;
1804 const char *ev_name, *title = "Available samples";
1805 int delay_secs = hbt ? hbt->refresh : 0;
1806 int key;
1807
1808 if (ui_browser__show(&menu->b, title,
1809 "ESC: exit, ENTER|->: Browse histograms") < 0)
1810 return -1;
1811
1812 while (1) {
1813 key = ui_browser__run(&menu->b, delay_secs);
1814
1815 switch (key) {
1816 case K_TIMER:
1817 hbt->timer(hbt->arg);
1818
1819 if (!menu->lost_events_warned && menu->lost_events) {
1820 ui_browser__warn_lost_events(&menu->b);
1821 menu->lost_events_warned = true;
1822 }
1823 continue;
1824 case K_RIGHT:
1825 case K_ENTER:
1826 if (!menu->selection)
1827 continue;
1828 pos = menu->selection;
1829 browse_hists:
1830 perf_evlist__set_selected(evlist, pos);
1831 /*
1832 * Give the calling tool a chance to populate the non
1833 * default evsel resorted hists tree.
1834 */
1835 if (hbt)
1836 hbt->timer(hbt->arg);
1837 ev_name = perf_evsel__name(pos);
1838 key = perf_evsel__hists_browse(pos, nr_events, help,
1839 ev_name, true, hbt,
1840 menu->min_pcnt,
1841 menu->env);
1842 ui_browser__show_title(&menu->b, title);
1843 switch (key) {
1844 case K_TAB:
1845 if (pos->node.next == &evlist->entries)
1846 pos = perf_evlist__first(evlist);
1847 else
1848 pos = perf_evsel__next(pos);
1849 goto browse_hists;
1850 case K_UNTAB:
1851 if (pos->node.prev == &evlist->entries)
1852 pos = perf_evlist__last(evlist);
1853 else
1854 pos = perf_evsel__prev(pos);
1855 goto browse_hists;
1856 case K_ESC:
1857 if (!ui_browser__dialog_yesno(&menu->b,
1858 "Do you really want to exit?"))
1859 continue;
1860 /* Fall thru */
1861 case K_SWITCH_INPUT_DATA:
1862 case 'q':
1863 case CTRL('c'):
1864 goto out;
1865 default:
1866 continue;
1867 }
1868 case K_LEFT:
1869 continue;
1870 case K_ESC:
1871 if (!ui_browser__dialog_yesno(&menu->b,
1872 "Do you really want to exit?"))
1873 continue;
1874 /* Fall thru */
1875 case 'q':
1876 case CTRL('c'):
1877 goto out;
1878 default:
1879 continue;
1880 }
1881 }
1882
1883 out:
1884 ui_browser__hide(&menu->b);
1885 return key;
1886 }
1887
1888 static bool filter_group_entries(struct ui_browser *browser __maybe_unused,
1889 void *entry)
1890 {
1891 struct perf_evsel *evsel = list_entry(entry, struct perf_evsel, node);
1892
1893 if (symbol_conf.event_group && !perf_evsel__is_group_leader(evsel))
1894 return true;
1895
1896 return false;
1897 }
1898
1899 static int __perf_evlist__tui_browse_hists(struct perf_evlist *evlist,
1900 int nr_entries, const char *help,
1901 struct hist_browser_timer *hbt,
1902 float min_pcnt,
1903 struct perf_session_env *env)
1904 {
1905 struct perf_evsel *pos;
1906 struct perf_evsel_menu menu = {
1907 .b = {
1908 .entries = &evlist->entries,
1909 .refresh = ui_browser__list_head_refresh,
1910 .seek = ui_browser__list_head_seek,
1911 .write = perf_evsel_menu__write,
1912 .filter = filter_group_entries,
1913 .nr_entries = nr_entries,
1914 .priv = evlist,
1915 },
1916 .min_pcnt = min_pcnt,
1917 .env = env,
1918 };
1919
1920 ui_helpline__push("Press ESC to exit");
1921
1922 evlist__for_each(evlist, pos) {
1923 const char *ev_name = perf_evsel__name(pos);
1924 size_t line_len = strlen(ev_name) + 7;
1925
1926 if (menu.b.width < line_len)
1927 menu.b.width = line_len;
1928 }
1929
1930 return perf_evsel_menu__run(&menu, nr_entries, help, hbt);
1931 }
1932
1933 int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help,
1934 struct hist_browser_timer *hbt,
1935 float min_pcnt,
1936 struct perf_session_env *env)
1937 {
1938 int nr_entries = evlist->nr_entries;
1939
1940 single_entry:
1941 if (nr_entries == 1) {
1942 struct perf_evsel *first = perf_evlist__first(evlist);
1943 const char *ev_name = perf_evsel__name(first);
1944
1945 return perf_evsel__hists_browse(first, nr_entries, help,
1946 ev_name, false, hbt, min_pcnt,
1947 env);
1948 }
1949
1950 if (symbol_conf.event_group) {
1951 struct perf_evsel *pos;
1952
1953 nr_entries = 0;
1954 evlist__for_each(evlist, pos) {
1955 if (perf_evsel__is_group_leader(pos))
1956 nr_entries++;
1957 }
1958
1959 if (nr_entries == 1)
1960 goto single_entry;
1961 }
1962
1963 return __perf_evlist__tui_browse_hists(evlist, nr_entries, help,
1964 hbt, min_pcnt, env);
1965 }
This page took 0.072671 seconds and 6 git commands to generate.