xref: /linux/tools/perf/ui/browsers/hists.c (revision c1a604dff486399ae0be95e6396e0158df95ad5d)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <dirent.h>
3 #include <errno.h>
4 #include <inttypes.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <linux/rbtree.h>
9 #include <linux/string.h>
10 #include <sys/ttydefaults.h>
11 #include <linux/time64.h>
12 #include <linux/zalloc.h>
13 
14 #include "../../util/callchain.h"
15 #include "../../util/evsel.h"
16 #include "../../util/evlist.h"
17 #include "../../util/hist.h"
18 #include "../../util/map.h"
19 #include "../../util/symbol.h"
20 #include "../../util/pstack.h"
21 #include "../../util/sort.h"
22 #include "../../util/top.h"
23 #include "../../util/thread.h"
24 #include "../../arch/common.h"
25 #include "../../perf.h"
26 
27 #include "../browsers/hists.h"
28 #include "../helpline.h"
29 #include "../util.h"
30 #include "../ui.h"
31 #include "map.h"
32 #include "annotate.h"
33 #include "srcline.h"
34 #include "string2.h"
35 #include "units.h"
36 #include "time-utils.h"
37 
38 #include <linux/ctype.h>
39 
40 extern void hist_browser__init_hpp(void);
41 
42 static int hists_browser__scnprintf_title(struct hist_browser *browser, char *bf, size_t size);
43 static void hist_browser__update_nr_entries(struct hist_browser *hb);
44 
45 static struct rb_node *hists__filter_entries(struct rb_node *nd,
46 					     float min_pcnt);
47 
48 static bool hist_browser__has_filter(struct hist_browser *hb)
49 {
50 	return hists__has_filter(hb->hists) || hb->min_pcnt || symbol_conf.has_filter || hb->c2c_filter;
51 }
52 
53 static int hist_browser__get_folding(struct hist_browser *browser)
54 {
55 	struct rb_node *nd;
56 	struct hists *hists = browser->hists;
57 	int unfolded_rows = 0;
58 
59 	for (nd = rb_first_cached(&hists->entries);
60 	     (nd = hists__filter_entries(nd, browser->min_pcnt)) != NULL;
61 	     nd = rb_hierarchy_next(nd)) {
62 		struct hist_entry *he =
63 			rb_entry(nd, struct hist_entry, rb_node);
64 
65 		if (he->leaf && he->unfolded)
66 			unfolded_rows += he->nr_rows;
67 	}
68 	return unfolded_rows;
69 }
70 
71 static void hist_browser__set_title_space(struct hist_browser *hb)
72 {
73 	struct ui_browser *browser = &hb->b;
74 	struct hists *hists = hb->hists;
75 	struct perf_hpp_list *hpp_list = hists->hpp_list;
76 
77 	browser->extra_title_lines = hb->show_headers ? hpp_list->nr_header_lines : 0;
78 }
79 
80 static u32 hist_browser__nr_entries(struct hist_browser *hb)
81 {
82 	u32 nr_entries;
83 
84 	if (symbol_conf.report_hierarchy)
85 		nr_entries = hb->nr_hierarchy_entries;
86 	else if (hist_browser__has_filter(hb))
87 		nr_entries = hb->nr_non_filtered_entries;
88 	else
89 		nr_entries = hb->hists->nr_entries;
90 
91 	hb->nr_callchain_rows = hist_browser__get_folding(hb);
92 	return nr_entries + hb->nr_callchain_rows;
93 }
94 
95 static void hist_browser__update_rows(struct hist_browser *hb)
96 {
97 	struct ui_browser *browser = &hb->b;
98 	struct hists *hists = hb->hists;
99 	struct perf_hpp_list *hpp_list = hists->hpp_list;
100 	u16 index_row;
101 
102 	if (!hb->show_headers) {
103 		browser->rows += browser->extra_title_lines;
104 		browser->extra_title_lines = 0;
105 		return;
106 	}
107 
108 	browser->extra_title_lines = hpp_list->nr_header_lines;
109 	browser->rows -= browser->extra_title_lines;
110 	/*
111 	 * Verify if we were at the last line and that line isn't
112 	 * visibe because we now show the header line(s).
113 	 */
114 	index_row = browser->index - browser->top_idx;
115 	if (index_row >= browser->rows)
116 		browser->index -= index_row - browser->rows + 1;
117 }
118 
119 static void hist_browser__refresh_dimensions(struct ui_browser *browser)
120 {
121 	struct hist_browser *hb = container_of(browser, struct hist_browser, b);
122 
123 	/* 3 == +/- toggle symbol before actual hist_entry rendering */
124 	browser->width = 3 + (hists__sort_list_width(hb->hists) + sizeof("[k]"));
125 	/*
126  	 * FIXME: Just keeping existing behaviour, but this really should be
127  	 *	  before updating browser->width, as it will invalidate the
128  	 *	  calculation above. Fix this and the fallout in another
129  	 *	  changeset.
130  	 */
131 	ui_browser__refresh_dimensions(browser);
132 }
133 
134 static void hist_browser__reset(struct hist_browser *browser)
135 {
136 	/*
137 	 * The hists__remove_entry_filter() already folds non-filtered
138 	 * entries so we can assume it has 0 callchain rows.
139 	 */
140 	browser->nr_callchain_rows = 0;
141 
142 	hist_browser__update_nr_entries(browser);
143 	browser->b.nr_entries = hist_browser__nr_entries(browser);
144 	hist_browser__refresh_dimensions(&browser->b);
145 	ui_browser__reset_index(&browser->b);
146 }
147 
148 static char tree__folded_sign(bool unfolded)
149 {
150 	return unfolded ? '-' : '+';
151 }
152 
153 static char hist_entry__folded(const struct hist_entry *he)
154 {
155 	return he->has_children ? tree__folded_sign(he->unfolded) : ' ';
156 }
157 
158 static char callchain_list__folded(const struct callchain_list *cl)
159 {
160 	return cl->has_children ? tree__folded_sign(cl->unfolded) : ' ';
161 }
162 
163 static void callchain_list__set_folding(struct callchain_list *cl, bool unfold)
164 {
165 	cl->unfolded = unfold ? cl->has_children : false;
166 }
167 
168 static int callchain_node__count_rows_rb_tree(struct callchain_node *node)
169 {
170 	int n = 0;
171 	struct rb_node *nd;
172 
173 	for (nd = rb_first(&node->rb_root); nd; nd = rb_next(nd)) {
174 		struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
175 		struct callchain_list *chain;
176 		char folded_sign = ' '; /* No children */
177 
178 		list_for_each_entry(chain, &child->val, list) {
179 			++n;
180 
181 			/* We need this because we may not have children */
182 			folded_sign = callchain_list__folded(chain);
183 			if (folded_sign == '+')
184 				break;
185 		}
186 
187 		if (folded_sign == '-') /* Have children and they're unfolded */
188 			n += callchain_node__count_rows_rb_tree(child);
189 	}
190 
191 	return n;
192 }
193 
194 static int callchain_node__count_flat_rows(struct callchain_node *node)
195 {
196 	struct callchain_list *chain;
197 	char folded_sign = 0;
198 	int n = 0;
199 
200 	list_for_each_entry(chain, &node->parent_val, list) {
201 		if (!folded_sign) {
202 			/* only check first chain list entry */
203 			folded_sign = callchain_list__folded(chain);
204 			if (folded_sign == '+')
205 				return 1;
206 		}
207 		n++;
208 	}
209 
210 	list_for_each_entry(chain, &node->val, list) {
211 		if (!folded_sign) {
212 			/* node->parent_val list might be empty */
213 			folded_sign = callchain_list__folded(chain);
214 			if (folded_sign == '+')
215 				return 1;
216 		}
217 		n++;
218 	}
219 
220 	return n;
221 }
222 
223 static int callchain_node__count_folded_rows(struct callchain_node *node __maybe_unused)
224 {
225 	return 1;
226 }
227 
228 static int callchain_node__count_rows(struct callchain_node *node)
229 {
230 	struct callchain_list *chain;
231 	bool unfolded = false;
232 	int n = 0;
233 
234 	if (callchain_param.mode == CHAIN_FLAT)
235 		return callchain_node__count_flat_rows(node);
236 	else if (callchain_param.mode == CHAIN_FOLDED)
237 		return callchain_node__count_folded_rows(node);
238 
239 	list_for_each_entry(chain, &node->val, list) {
240 		++n;
241 
242 		unfolded = chain->unfolded;
243 	}
244 
245 	if (unfolded)
246 		n += callchain_node__count_rows_rb_tree(node);
247 
248 	return n;
249 }
250 
251 static int callchain__count_rows(struct rb_root *chain)
252 {
253 	struct rb_node *nd;
254 	int n = 0;
255 
256 	for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
257 		struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
258 		n += callchain_node__count_rows(node);
259 	}
260 
261 	return n;
262 }
263 
264 static int hierarchy_count_rows(struct hist_browser *hb, struct hist_entry *he,
265 				bool include_children)
266 {
267 	int count = 0;
268 	struct rb_node *node;
269 	struct hist_entry *child;
270 
271 	if (he->leaf)
272 		return callchain__count_rows(&he->sorted_chain);
273 
274 	if (he->has_no_entry)
275 		return 1;
276 
277 	node = rb_first_cached(&he->hroot_out);
278 	while (node) {
279 		float percent;
280 
281 		child = rb_entry(node, struct hist_entry, rb_node);
282 		percent = hist_entry__get_percent_limit(child);
283 
284 		if (!child->filtered && percent >= hb->min_pcnt) {
285 			count++;
286 
287 			if (include_children && child->unfolded)
288 				count += hierarchy_count_rows(hb, child, true);
289 		}
290 
291 		node = rb_next(node);
292 	}
293 	return count;
294 }
295 
296 static bool hist_entry__toggle_fold(struct hist_entry *he)
297 {
298 	if (!he)
299 		return false;
300 
301 	if (!he->has_children)
302 		return false;
303 
304 	he->unfolded = !he->unfolded;
305 	return true;
306 }
307 
308 static bool callchain_list__toggle_fold(struct callchain_list *cl)
309 {
310 	if (!cl)
311 		return false;
312 
313 	if (!cl->has_children)
314 		return false;
315 
316 	cl->unfolded = !cl->unfolded;
317 	return true;
318 }
319 
320 static void callchain_node__init_have_children_rb_tree(struct callchain_node *node)
321 {
322 	struct rb_node *nd = rb_first(&node->rb_root);
323 
324 	for (nd = rb_first(&node->rb_root); nd; nd = rb_next(nd)) {
325 		struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
326 		struct callchain_list *chain;
327 		bool first = true;
328 
329 		list_for_each_entry(chain, &child->val, list) {
330 			if (first) {
331 				first = false;
332 				chain->has_children = chain->list.next != &child->val ||
333 							 !RB_EMPTY_ROOT(&child->rb_root);
334 			} else
335 				chain->has_children = chain->list.next == &child->val &&
336 							 !RB_EMPTY_ROOT(&child->rb_root);
337 		}
338 
339 		callchain_node__init_have_children_rb_tree(child);
340 	}
341 }
342 
343 static void callchain_node__init_have_children(struct callchain_node *node,
344 					       bool has_sibling)
345 {
346 	struct callchain_list *chain;
347 
348 	chain = list_entry(node->val.next, struct callchain_list, list);
349 	chain->has_children = has_sibling;
350 
351 	if (!list_empty(&node->val)) {
352 		chain = list_entry(node->val.prev, struct callchain_list, list);
353 		chain->has_children = !RB_EMPTY_ROOT(&node->rb_root);
354 	}
355 
356 	callchain_node__init_have_children_rb_tree(node);
357 }
358 
359 static void callchain__init_have_children(struct rb_root *root)
360 {
361 	struct rb_node *nd = rb_first(root);
362 	bool has_sibling = nd && rb_next(nd);
363 
364 	for (nd = rb_first(root); nd; nd = rb_next(nd)) {
365 		struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
366 		callchain_node__init_have_children(node, has_sibling);
367 		if (callchain_param.mode == CHAIN_FLAT ||
368 		    callchain_param.mode == CHAIN_FOLDED)
369 			callchain_node__make_parent_list(node);
370 	}
371 }
372 
373 static void hist_entry__init_have_children(struct hist_entry *he)
374 {
375 	if (he->init_have_children)
376 		return;
377 
378 	if (he->leaf) {
379 		he->has_children = !RB_EMPTY_ROOT(&he->sorted_chain);
380 		callchain__init_have_children(&he->sorted_chain);
381 	} else {
382 		he->has_children = !RB_EMPTY_ROOT(&he->hroot_out.rb_root);
383 	}
384 
385 	he->init_have_children = true;
386 }
387 
388 static bool hist_browser__toggle_fold(struct hist_browser *browser)
389 {
390 	struct hist_entry *he = browser->he_selection;
391 	struct map_symbol *ms = browser->selection;
392 	struct callchain_list *cl = container_of(ms, struct callchain_list, ms);
393 	bool has_children;
394 
395 	if (!he || !ms)
396 		return false;
397 
398 	if (ms == &he->ms)
399 		has_children = hist_entry__toggle_fold(he);
400 	else
401 		has_children = callchain_list__toggle_fold(cl);
402 
403 	if (has_children) {
404 		int child_rows = 0;
405 
406 		hist_entry__init_have_children(he);
407 		browser->b.nr_entries -= he->nr_rows;
408 
409 		if (he->leaf)
410 			browser->nr_callchain_rows -= he->nr_rows;
411 		else
412 			browser->nr_hierarchy_entries -= he->nr_rows;
413 
414 		if (symbol_conf.report_hierarchy)
415 			child_rows = hierarchy_count_rows(browser, he, true);
416 
417 		if (he->unfolded) {
418 			if (he->leaf)
419 				he->nr_rows = callchain__count_rows(
420 						&he->sorted_chain);
421 			else
422 				he->nr_rows = hierarchy_count_rows(browser, he, false);
423 
424 			/* account grand children */
425 			if (symbol_conf.report_hierarchy)
426 				browser->b.nr_entries += child_rows - he->nr_rows;
427 
428 			if (!he->leaf && he->nr_rows == 0) {
429 				he->has_no_entry = true;
430 				he->nr_rows = 1;
431 			}
432 		} else {
433 			if (symbol_conf.report_hierarchy)
434 				browser->b.nr_entries -= child_rows - he->nr_rows;
435 
436 			if (he->has_no_entry)
437 				he->has_no_entry = false;
438 
439 			he->nr_rows = 0;
440 		}
441 
442 		browser->b.nr_entries += he->nr_rows;
443 
444 		if (he->leaf)
445 			browser->nr_callchain_rows += he->nr_rows;
446 		else
447 			browser->nr_hierarchy_entries += he->nr_rows;
448 
449 		return true;
450 	}
451 
452 	/* If it doesn't have children, no toggling performed */
453 	return false;
454 }
455 
456 static int callchain_node__set_folding_rb_tree(struct callchain_node *node, bool unfold)
457 {
458 	int n = 0;
459 	struct rb_node *nd;
460 
461 	for (nd = rb_first(&node->rb_root); nd; nd = rb_next(nd)) {
462 		struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
463 		struct callchain_list *chain;
464 		bool has_children = false;
465 
466 		list_for_each_entry(chain, &child->val, list) {
467 			++n;
468 			callchain_list__set_folding(chain, unfold);
469 			has_children = chain->has_children;
470 		}
471 
472 		if (has_children)
473 			n += callchain_node__set_folding_rb_tree(child, unfold);
474 	}
475 
476 	return n;
477 }
478 
479 static int callchain_node__set_folding(struct callchain_node *node, bool unfold)
480 {
481 	struct callchain_list *chain;
482 	bool has_children = false;
483 	int n = 0;
484 
485 	list_for_each_entry(chain, &node->val, list) {
486 		++n;
487 		callchain_list__set_folding(chain, unfold);
488 		has_children = chain->has_children;
489 	}
490 
491 	if (has_children)
492 		n += callchain_node__set_folding_rb_tree(node, unfold);
493 
494 	return n;
495 }
496 
497 static int callchain__set_folding(struct rb_root *chain, bool unfold)
498 {
499 	struct rb_node *nd;
500 	int n = 0;
501 
502 	for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
503 		struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
504 		n += callchain_node__set_folding(node, unfold);
505 	}
506 
507 	return n;
508 }
509 
510 static int hierarchy_set_folding(struct hist_browser *hb, struct hist_entry *he,
511 				 bool unfold __maybe_unused)
512 {
513 	float percent;
514 	struct rb_node *nd;
515 	struct hist_entry *child;
516 	int n = 0;
517 
518 	for (nd = rb_first_cached(&he->hroot_out); nd; nd = rb_next(nd)) {
519 		child = rb_entry(nd, struct hist_entry, rb_node);
520 		percent = hist_entry__get_percent_limit(child);
521 		if (!child->filtered && percent >= hb->min_pcnt)
522 			n++;
523 	}
524 
525 	return n;
526 }
527 
528 static void __hist_entry__set_folding(struct hist_entry *he,
529 				      struct hist_browser *hb, bool unfold)
530 {
531 	hist_entry__init_have_children(he);
532 	he->unfolded = unfold ? he->has_children : false;
533 
534 	if (he->has_children) {
535 		int n;
536 
537 		if (he->leaf)
538 			n = callchain__set_folding(&he->sorted_chain, unfold);
539 		else
540 			n = hierarchy_set_folding(hb, he, unfold);
541 
542 		he->nr_rows = unfold ? n : 0;
543 	} else
544 		he->nr_rows = 0;
545 }
546 
547 static void hist_entry__set_folding(struct hist_entry *he,
548 				    struct hist_browser *browser, bool unfold)
549 {
550 	double percent;
551 
552 	percent = hist_entry__get_percent_limit(he);
553 	if (he->filtered || percent < browser->min_pcnt)
554 		return;
555 
556 	__hist_entry__set_folding(he, browser, unfold);
557 
558 	if (!he->depth || unfold)
559 		browser->nr_hierarchy_entries++;
560 	if (he->leaf)
561 		browser->nr_callchain_rows += he->nr_rows;
562 	else if (unfold && !hist_entry__has_hierarchy_children(he, browser->min_pcnt)) {
563 		browser->nr_hierarchy_entries++;
564 		he->has_no_entry = true;
565 		he->nr_rows = 1;
566 	} else
567 		he->has_no_entry = false;
568 }
569 
570 static void
571 __hist_browser__set_folding(struct hist_browser *browser, bool unfold)
572 {
573 	struct rb_node *nd;
574 	struct hist_entry *he;
575 
576 	nd = rb_first_cached(&browser->hists->entries);
577 	while (nd) {
578 		he = rb_entry(nd, struct hist_entry, rb_node);
579 
580 		/* set folding state even if it's currently folded */
581 		nd = __rb_hierarchy_next(nd, HMD_FORCE_CHILD);
582 
583 		hist_entry__set_folding(he, browser, unfold);
584 	}
585 }
586 
587 static void hist_browser__set_folding(struct hist_browser *browser, bool unfold)
588 {
589 	browser->nr_hierarchy_entries = 0;
590 	browser->nr_callchain_rows = 0;
591 	__hist_browser__set_folding(browser, unfold);
592 
593 	browser->b.nr_entries = hist_browser__nr_entries(browser);
594 	/* Go to the start, we may be way after valid entries after a collapse */
595 	ui_browser__reset_index(&browser->b);
596 }
597 
598 static void hist_browser__set_folding_selected(struct hist_browser *browser, bool unfold)
599 {
600 	if (!browser->he_selection)
601 		return;
602 
603 	hist_entry__set_folding(browser->he_selection, browser, unfold);
604 	browser->b.nr_entries = hist_browser__nr_entries(browser);
605 }
606 
607 static void ui_browser__warn_lost_events(struct ui_browser *browser)
608 {
609 	ui_browser__warning(browser, 4,
610 		"Events are being lost, check IO/CPU overload!\n\n"
611 		"You may want to run 'perf' using a RT scheduler policy:\n\n"
612 		" perf top -r 80\n\n"
613 		"Or reduce the sampling frequency.");
614 }
615 
616 static int hist_browser__title(struct hist_browser *browser, char *bf, size_t size)
617 {
618 	return browser->title ? browser->title(browser, bf, size) : 0;
619 }
620 
621 int hist_browser__run(struct hist_browser *browser, const char *help,
622 		      bool warn_lost_event)
623 {
624 	int key;
625 	char title[160];
626 	struct hist_browser_timer *hbt = browser->hbt;
627 	int delay_secs = hbt ? hbt->refresh : 0;
628 
629 	browser->b.entries = &browser->hists->entries;
630 	browser->b.nr_entries = hist_browser__nr_entries(browser);
631 
632 	hist_browser__title(browser, title, sizeof(title));
633 
634 	if (ui_browser__show(&browser->b, title, "%s", help) < 0)
635 		return -1;
636 
637 	while (1) {
638 		key = ui_browser__run(&browser->b, delay_secs);
639 
640 		switch (key) {
641 		case K_TIMER: {
642 			u64 nr_entries;
643 
644 			WARN_ON_ONCE(!hbt);
645 
646 			if (hbt)
647 				hbt->timer(hbt->arg);
648 
649 			if (hist_browser__has_filter(browser) ||
650 			    symbol_conf.report_hierarchy)
651 				hist_browser__update_nr_entries(browser);
652 
653 			nr_entries = hist_browser__nr_entries(browser);
654 			ui_browser__update_nr_entries(&browser->b, nr_entries);
655 
656 			if (warn_lost_event &&
657 			    (browser->hists->stats.nr_lost_warned !=
658 			    browser->hists->stats.nr_events[PERF_RECORD_LOST])) {
659 				browser->hists->stats.nr_lost_warned =
660 					browser->hists->stats.nr_events[PERF_RECORD_LOST];
661 				ui_browser__warn_lost_events(&browser->b);
662 			}
663 
664 			hist_browser__title(browser, title, sizeof(title));
665 			ui_browser__show_title(&browser->b, title);
666 			continue;
667 		}
668 		case 'D': { /* Debug */
669 			static int seq;
670 			struct hist_entry *h = rb_entry(browser->b.top,
671 							struct hist_entry, rb_node);
672 			ui_helpline__pop();
673 			ui_helpline__fpush("%d: nr_ent=(%d,%d), etl: %d, rows=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
674 					   seq++, browser->b.nr_entries,
675 					   browser->hists->nr_entries,
676 					   browser->b.extra_title_lines,
677 					   browser->b.rows,
678 					   browser->b.index,
679 					   browser->b.top_idx,
680 					   h->row_offset, h->nr_rows);
681 		}
682 			break;
683 		case 'C':
684 			/* Collapse the whole world. */
685 			hist_browser__set_folding(browser, false);
686 			break;
687 		case 'c':
688 			/* Collapse the selected entry. */
689 			hist_browser__set_folding_selected(browser, false);
690 			break;
691 		case 'E':
692 			/* Expand the whole world. */
693 			hist_browser__set_folding(browser, true);
694 			break;
695 		case 'e':
696 			/* Expand the selected entry. */
697 			hist_browser__set_folding_selected(browser, true);
698 			break;
699 		case 'H':
700 			browser->show_headers = !browser->show_headers;
701 			hist_browser__update_rows(browser);
702 			break;
703 		case K_ENTER:
704 			if (hist_browser__toggle_fold(browser))
705 				break;
706 			/* fall thru */
707 		default:
708 			goto out;
709 		}
710 	}
711 out:
712 	ui_browser__hide(&browser->b);
713 	return key;
714 }
715 
716 struct callchain_print_arg {
717 	/* for hists browser */
718 	off_t	row_offset;
719 	bool	is_current_entry;
720 
721 	/* for file dump */
722 	FILE	*fp;
723 	int	printed;
724 };
725 
726 typedef void (*print_callchain_entry_fn)(struct hist_browser *browser,
727 					 struct callchain_list *chain,
728 					 const char *str, int offset,
729 					 unsigned short row,
730 					 struct callchain_print_arg *arg);
731 
732 static void hist_browser__show_callchain_entry(struct hist_browser *browser,
733 					       struct callchain_list *chain,
734 					       const char *str, int offset,
735 					       unsigned short row,
736 					       struct callchain_print_arg *arg)
737 {
738 	int color, width;
739 	char folded_sign = callchain_list__folded(chain);
740 	bool show_annotated = browser->show_dso && chain->ms.sym && symbol__annotation(chain->ms.sym)->src;
741 
742 	color = HE_COLORSET_NORMAL;
743 	width = browser->b.width - (offset + 2);
744 	if (ui_browser__is_current_entry(&browser->b, row)) {
745 		browser->selection = &chain->ms;
746 		color = HE_COLORSET_SELECTED;
747 		arg->is_current_entry = true;
748 	}
749 
750 	ui_browser__set_color(&browser->b, color);
751 	ui_browser__gotorc(&browser->b, row, 0);
752 	ui_browser__write_nstring(&browser->b, " ", offset);
753 	ui_browser__printf(&browser->b, "%c", folded_sign);
754 	ui_browser__write_graph(&browser->b, show_annotated ? SLSMG_RARROW_CHAR : ' ');
755 	ui_browser__write_nstring(&browser->b, str, width);
756 }
757 
758 static void hist_browser__fprintf_callchain_entry(struct hist_browser *b __maybe_unused,
759 						  struct callchain_list *chain,
760 						  const char *str, int offset,
761 						  unsigned short row __maybe_unused,
762 						  struct callchain_print_arg *arg)
763 {
764 	char folded_sign = callchain_list__folded(chain);
765 
766 	arg->printed += fprintf(arg->fp, "%*s%c %s\n", offset, " ",
767 				folded_sign, str);
768 }
769 
770 typedef bool (*check_output_full_fn)(struct hist_browser *browser,
771 				     unsigned short row);
772 
773 static bool hist_browser__check_output_full(struct hist_browser *browser,
774 					    unsigned short row)
775 {
776 	return browser->b.rows == row;
777 }
778 
779 static bool hist_browser__check_dump_full(struct hist_browser *browser __maybe_unused,
780 					  unsigned short row __maybe_unused)
781 {
782 	return false;
783 }
784 
785 #define LEVEL_OFFSET_STEP 3
786 
787 static int hist_browser__show_callchain_list(struct hist_browser *browser,
788 					     struct callchain_node *node,
789 					     struct callchain_list *chain,
790 					     unsigned short row, u64 total,
791 					     bool need_percent, int offset,
792 					     print_callchain_entry_fn print,
793 					     struct callchain_print_arg *arg)
794 {
795 	char bf[1024], *alloc_str;
796 	char buf[64], *alloc_str2;
797 	const char *str;
798 	int ret = 1;
799 
800 	if (arg->row_offset != 0) {
801 		arg->row_offset--;
802 		return 0;
803 	}
804 
805 	alloc_str = NULL;
806 	alloc_str2 = NULL;
807 
808 	str = callchain_list__sym_name(chain, bf, sizeof(bf),
809 				       browser->show_dso);
810 
811 	if (symbol_conf.show_branchflag_count) {
812 		callchain_list_counts__printf_value(chain, NULL,
813 						    buf, sizeof(buf));
814 
815 		if (asprintf(&alloc_str2, "%s%s", str, buf) < 0)
816 			str = "Not enough memory!";
817 		else
818 			str = alloc_str2;
819 	}
820 
821 	if (need_percent) {
822 		callchain_node__scnprintf_value(node, buf, sizeof(buf),
823 						total);
824 
825 		if (asprintf(&alloc_str, "%s %s", buf, str) < 0)
826 			str = "Not enough memory!";
827 		else
828 			str = alloc_str;
829 	}
830 
831 	print(browser, chain, str, offset, row, arg);
832 	free(alloc_str);
833 	free(alloc_str2);
834 
835 	return ret;
836 }
837 
838 static bool check_percent_display(struct rb_node *node, u64 parent_total)
839 {
840 	struct callchain_node *child;
841 
842 	if (node == NULL)
843 		return false;
844 
845 	if (rb_next(node))
846 		return true;
847 
848 	child = rb_entry(node, struct callchain_node, rb_node);
849 	return callchain_cumul_hits(child) != parent_total;
850 }
851 
852 static int hist_browser__show_callchain_flat(struct hist_browser *browser,
853 					     struct rb_root *root,
854 					     unsigned short row, u64 total,
855 					     u64 parent_total,
856 					     print_callchain_entry_fn print,
857 					     struct callchain_print_arg *arg,
858 					     check_output_full_fn is_output_full)
859 {
860 	struct rb_node *node;
861 	int first_row = row, offset = LEVEL_OFFSET_STEP;
862 	bool need_percent;
863 
864 	node = rb_first(root);
865 	need_percent = check_percent_display(node, parent_total);
866 
867 	while (node) {
868 		struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
869 		struct rb_node *next = rb_next(node);
870 		struct callchain_list *chain;
871 		char folded_sign = ' ';
872 		int first = true;
873 		int extra_offset = 0;
874 
875 		list_for_each_entry(chain, &child->parent_val, list) {
876 			bool was_first = first;
877 
878 			if (first)
879 				first = false;
880 			else if (need_percent)
881 				extra_offset = LEVEL_OFFSET_STEP;
882 
883 			folded_sign = callchain_list__folded(chain);
884 
885 			row += hist_browser__show_callchain_list(browser, child,
886 							chain, row, total,
887 							was_first && need_percent,
888 							offset + extra_offset,
889 							print, arg);
890 
891 			if (is_output_full(browser, row))
892 				goto out;
893 
894 			if (folded_sign == '+')
895 				goto next;
896 		}
897 
898 		list_for_each_entry(chain, &child->val, list) {
899 			bool was_first = first;
900 
901 			if (first)
902 				first = false;
903 			else if (need_percent)
904 				extra_offset = LEVEL_OFFSET_STEP;
905 
906 			folded_sign = callchain_list__folded(chain);
907 
908 			row += hist_browser__show_callchain_list(browser, child,
909 							chain, row, total,
910 							was_first && need_percent,
911 							offset + extra_offset,
912 							print, arg);
913 
914 			if (is_output_full(browser, row))
915 				goto out;
916 
917 			if (folded_sign == '+')
918 				break;
919 		}
920 
921 next:
922 		if (is_output_full(browser, row))
923 			break;
924 		node = next;
925 	}
926 out:
927 	return row - first_row;
928 }
929 
930 static char *hist_browser__folded_callchain_str(struct hist_browser *browser,
931 						struct callchain_list *chain,
932 						char *value_str, char *old_str)
933 {
934 	char bf[1024];
935 	const char *str;
936 	char *new;
937 
938 	str = callchain_list__sym_name(chain, bf, sizeof(bf),
939 				       browser->show_dso);
940 	if (old_str) {
941 		if (asprintf(&new, "%s%s%s", old_str,
942 			     symbol_conf.field_sep ?: ";", str) < 0)
943 			new = NULL;
944 	} else {
945 		if (value_str) {
946 			if (asprintf(&new, "%s %s", value_str, str) < 0)
947 				new = NULL;
948 		} else {
949 			if (asprintf(&new, "%s", str) < 0)
950 				new = NULL;
951 		}
952 	}
953 	return new;
954 }
955 
956 static int hist_browser__show_callchain_folded(struct hist_browser *browser,
957 					       struct rb_root *root,
958 					       unsigned short row, u64 total,
959 					       u64 parent_total,
960 					       print_callchain_entry_fn print,
961 					       struct callchain_print_arg *arg,
962 					       check_output_full_fn is_output_full)
963 {
964 	struct rb_node *node;
965 	int first_row = row, offset = LEVEL_OFFSET_STEP;
966 	bool need_percent;
967 
968 	node = rb_first(root);
969 	need_percent = check_percent_display(node, parent_total);
970 
971 	while (node) {
972 		struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
973 		struct rb_node *next = rb_next(node);
974 		struct callchain_list *chain, *first_chain = NULL;
975 		int first = true;
976 		char *value_str = NULL, *value_str_alloc = NULL;
977 		char *chain_str = NULL, *chain_str_alloc = NULL;
978 
979 		if (arg->row_offset != 0) {
980 			arg->row_offset--;
981 			goto next;
982 		}
983 
984 		if (need_percent) {
985 			char buf[64];
986 
987 			callchain_node__scnprintf_value(child, buf, sizeof(buf), total);
988 			if (asprintf(&value_str, "%s", buf) < 0) {
989 				value_str = (char *)"<...>";
990 				goto do_print;
991 			}
992 			value_str_alloc = value_str;
993 		}
994 
995 		list_for_each_entry(chain, &child->parent_val, list) {
996 			chain_str = hist_browser__folded_callchain_str(browser,
997 						chain, value_str, chain_str);
998 			if (first) {
999 				first = false;
1000 				first_chain = chain;
1001 			}
1002 
1003 			if (chain_str == NULL) {
1004 				chain_str = (char *)"Not enough memory!";
1005 				goto do_print;
1006 			}
1007 
1008 			chain_str_alloc = chain_str;
1009 		}
1010 
1011 		list_for_each_entry(chain, &child->val, list) {
1012 			chain_str = hist_browser__folded_callchain_str(browser,
1013 						chain, value_str, chain_str);
1014 			if (first) {
1015 				first = false;
1016 				first_chain = chain;
1017 			}
1018 
1019 			if (chain_str == NULL) {
1020 				chain_str = (char *)"Not enough memory!";
1021 				goto do_print;
1022 			}
1023 
1024 			chain_str_alloc = chain_str;
1025 		}
1026 
1027 do_print:
1028 		print(browser, first_chain, chain_str, offset, row++, arg);
1029 		free(value_str_alloc);
1030 		free(chain_str_alloc);
1031 
1032 next:
1033 		if (is_output_full(browser, row))
1034 			break;
1035 		node = next;
1036 	}
1037 
1038 	return row - first_row;
1039 }
1040 
1041 static int hist_browser__show_callchain_graph(struct hist_browser *browser,
1042 					struct rb_root *root, int level,
1043 					unsigned short row, u64 total,
1044 					u64 parent_total,
1045 					print_callchain_entry_fn print,
1046 					struct callchain_print_arg *arg,
1047 					check_output_full_fn is_output_full)
1048 {
1049 	struct rb_node *node;
1050 	int first_row = row, offset = level * LEVEL_OFFSET_STEP;
1051 	bool need_percent;
1052 	u64 percent_total = total;
1053 
1054 	if (callchain_param.mode == CHAIN_GRAPH_REL)
1055 		percent_total = parent_total;
1056 
1057 	node = rb_first(root);
1058 	need_percent = check_percent_display(node, parent_total);
1059 
1060 	while (node) {
1061 		struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
1062 		struct rb_node *next = rb_next(node);
1063 		struct callchain_list *chain;
1064 		char folded_sign = ' ';
1065 		int first = true;
1066 		int extra_offset = 0;
1067 
1068 		list_for_each_entry(chain, &child->val, list) {
1069 			bool was_first = first;
1070 
1071 			if (first)
1072 				first = false;
1073 			else if (need_percent)
1074 				extra_offset = LEVEL_OFFSET_STEP;
1075 
1076 			folded_sign = callchain_list__folded(chain);
1077 
1078 			row += hist_browser__show_callchain_list(browser, child,
1079 							chain, row, percent_total,
1080 							was_first && need_percent,
1081 							offset + extra_offset,
1082 							print, arg);
1083 
1084 			if (is_output_full(browser, row))
1085 				goto out;
1086 
1087 			if (folded_sign == '+')
1088 				break;
1089 		}
1090 
1091 		if (folded_sign == '-') {
1092 			const int new_level = level + (extra_offset ? 2 : 1);
1093 
1094 			row += hist_browser__show_callchain_graph(browser, &child->rb_root,
1095 							    new_level, row, total,
1096 							    child->children_hit,
1097 							    print, arg, is_output_full);
1098 		}
1099 		if (is_output_full(browser, row))
1100 			break;
1101 		node = next;
1102 	}
1103 out:
1104 	return row - first_row;
1105 }
1106 
1107 static int hist_browser__show_callchain(struct hist_browser *browser,
1108 					struct hist_entry *entry, int level,
1109 					unsigned short row,
1110 					print_callchain_entry_fn print,
1111 					struct callchain_print_arg *arg,
1112 					check_output_full_fn is_output_full)
1113 {
1114 	u64 total = hists__total_period(entry->hists);
1115 	u64 parent_total;
1116 	int printed;
1117 
1118 	if (symbol_conf.cumulate_callchain)
1119 		parent_total = entry->stat_acc->period;
1120 	else
1121 		parent_total = entry->stat.period;
1122 
1123 	if (callchain_param.mode == CHAIN_FLAT) {
1124 		printed = hist_browser__show_callchain_flat(browser,
1125 						&entry->sorted_chain, row,
1126 						total, parent_total, print, arg,
1127 						is_output_full);
1128 	} else if (callchain_param.mode == CHAIN_FOLDED) {
1129 		printed = hist_browser__show_callchain_folded(browser,
1130 						&entry->sorted_chain, row,
1131 						total, parent_total, print, arg,
1132 						is_output_full);
1133 	} else {
1134 		printed = hist_browser__show_callchain_graph(browser,
1135 						&entry->sorted_chain, level, row,
1136 						total, parent_total, print, arg,
1137 						is_output_full);
1138 	}
1139 
1140 	if (arg->is_current_entry)
1141 		browser->he_selection = entry;
1142 
1143 	return printed;
1144 }
1145 
1146 struct hpp_arg {
1147 	struct ui_browser *b;
1148 	char folded_sign;
1149 	bool current_entry;
1150 };
1151 
1152 int __hpp__slsmg_color_printf(struct perf_hpp *hpp, const char *fmt, ...)
1153 {
1154 	struct hpp_arg *arg = hpp->ptr;
1155 	int ret, len;
1156 	va_list args;
1157 	double percent;
1158 
1159 	va_start(args, fmt);
1160 	len = va_arg(args, int);
1161 	percent = va_arg(args, double);
1162 	va_end(args);
1163 
1164 	ui_browser__set_percent_color(arg->b, percent, arg->current_entry);
1165 
1166 	ret = scnprintf(hpp->buf, hpp->size, fmt, len, percent);
1167 	ui_browser__printf(arg->b, "%s", hpp->buf);
1168 
1169 	return ret;
1170 }
1171 
1172 #define __HPP_COLOR_PERCENT_FN(_type, _field)				\
1173 static u64 __hpp_get_##_field(struct hist_entry *he)			\
1174 {									\
1175 	return he->stat._field;						\
1176 }									\
1177 									\
1178 static int								\
1179 hist_browser__hpp_color_##_type(struct perf_hpp_fmt *fmt,		\
1180 				struct perf_hpp *hpp,			\
1181 				struct hist_entry *he)			\
1182 {									\
1183 	return hpp__fmt(fmt, hpp, he, __hpp_get_##_field, " %*.2f%%",	\
1184 			__hpp__slsmg_color_printf, true);		\
1185 }
1186 
1187 #define __HPP_COLOR_ACC_PERCENT_FN(_type, _field)			\
1188 static u64 __hpp_get_acc_##_field(struct hist_entry *he)		\
1189 {									\
1190 	return he->stat_acc->_field;					\
1191 }									\
1192 									\
1193 static int								\
1194 hist_browser__hpp_color_##_type(struct perf_hpp_fmt *fmt,		\
1195 				struct perf_hpp *hpp,			\
1196 				struct hist_entry *he)			\
1197 {									\
1198 	if (!symbol_conf.cumulate_callchain) {				\
1199 		struct hpp_arg *arg = hpp->ptr;				\
1200 		int len = fmt->user_len ?: fmt->len;			\
1201 		int ret = scnprintf(hpp->buf, hpp->size,		\
1202 				    "%*s", len, "N/A");			\
1203 		ui_browser__printf(arg->b, "%s", hpp->buf);		\
1204 									\
1205 		return ret;						\
1206 	}								\
1207 	return hpp__fmt(fmt, hpp, he, __hpp_get_acc_##_field,		\
1208 			" %*.2f%%", __hpp__slsmg_color_printf, true);	\
1209 }
1210 
1211 __HPP_COLOR_PERCENT_FN(overhead, period)
1212 __HPP_COLOR_PERCENT_FN(overhead_sys, period_sys)
1213 __HPP_COLOR_PERCENT_FN(overhead_us, period_us)
1214 __HPP_COLOR_PERCENT_FN(overhead_guest_sys, period_guest_sys)
1215 __HPP_COLOR_PERCENT_FN(overhead_guest_us, period_guest_us)
1216 __HPP_COLOR_ACC_PERCENT_FN(overhead_acc, period)
1217 
1218 #undef __HPP_COLOR_PERCENT_FN
1219 #undef __HPP_COLOR_ACC_PERCENT_FN
1220 
1221 void hist_browser__init_hpp(void)
1222 {
1223 	perf_hpp__format[PERF_HPP__OVERHEAD].color =
1224 				hist_browser__hpp_color_overhead;
1225 	perf_hpp__format[PERF_HPP__OVERHEAD_SYS].color =
1226 				hist_browser__hpp_color_overhead_sys;
1227 	perf_hpp__format[PERF_HPP__OVERHEAD_US].color =
1228 				hist_browser__hpp_color_overhead_us;
1229 	perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_SYS].color =
1230 				hist_browser__hpp_color_overhead_guest_sys;
1231 	perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_US].color =
1232 				hist_browser__hpp_color_overhead_guest_us;
1233 	perf_hpp__format[PERF_HPP__OVERHEAD_ACC].color =
1234 				hist_browser__hpp_color_overhead_acc;
1235 
1236 	res_sample_init();
1237 }
1238 
1239 static int hist_browser__show_entry(struct hist_browser *browser,
1240 				    struct hist_entry *entry,
1241 				    unsigned short row)
1242 {
1243 	int printed = 0;
1244 	int width = browser->b.width;
1245 	char folded_sign = ' ';
1246 	bool current_entry = ui_browser__is_current_entry(&browser->b, row);
1247 	bool use_callchain = hist_entry__has_callchains(entry) && symbol_conf.use_callchain;
1248 	off_t row_offset = entry->row_offset;
1249 	bool first = true;
1250 	struct perf_hpp_fmt *fmt;
1251 
1252 	if (current_entry) {
1253 		browser->he_selection = entry;
1254 		browser->selection = &entry->ms;
1255 	}
1256 
1257 	if (use_callchain) {
1258 		hist_entry__init_have_children(entry);
1259 		folded_sign = hist_entry__folded(entry);
1260 	}
1261 
1262 	if (row_offset == 0) {
1263 		struct hpp_arg arg = {
1264 			.b		= &browser->b,
1265 			.folded_sign	= folded_sign,
1266 			.current_entry	= current_entry,
1267 		};
1268 		int column = 0;
1269 
1270 		ui_browser__gotorc(&browser->b, row, 0);
1271 
1272 		hists__for_each_format(browser->hists, fmt) {
1273 			char s[2048];
1274 			struct perf_hpp hpp = {
1275 				.buf	= s,
1276 				.size	= sizeof(s),
1277 				.ptr	= &arg,
1278 			};
1279 
1280 			if (perf_hpp__should_skip(fmt, entry->hists) ||
1281 			    column++ < browser->b.horiz_scroll)
1282 				continue;
1283 
1284 			if (current_entry && browser->b.navkeypressed) {
1285 				ui_browser__set_color(&browser->b,
1286 						      HE_COLORSET_SELECTED);
1287 			} else {
1288 				ui_browser__set_color(&browser->b,
1289 						      HE_COLORSET_NORMAL);
1290 			}
1291 
1292 			if (first) {
1293 				if (use_callchain) {
1294 					ui_browser__printf(&browser->b, "%c ", folded_sign);
1295 					width -= 2;
1296 				}
1297 				first = false;
1298 			} else {
1299 				ui_browser__printf(&browser->b, "  ");
1300 				width -= 2;
1301 			}
1302 
1303 			if (fmt->color) {
1304 				int ret = fmt->color(fmt, &hpp, entry);
1305 				hist_entry__snprintf_alignment(entry, &hpp, fmt, ret);
1306 				/*
1307 				 * fmt->color() already used ui_browser to
1308 				 * print the non alignment bits, skip it (+ret):
1309 				 */
1310 				ui_browser__printf(&browser->b, "%s", s + ret);
1311 			} else {
1312 				hist_entry__snprintf_alignment(entry, &hpp, fmt, fmt->entry(fmt, &hpp, entry));
1313 				ui_browser__printf(&browser->b, "%s", s);
1314 			}
1315 			width -= hpp.buf - s;
1316 		}
1317 
1318 		/* The scroll bar isn't being used */
1319 		if (!browser->b.navkeypressed)
1320 			width += 1;
1321 
1322 		ui_browser__write_nstring(&browser->b, "", width);
1323 
1324 		++row;
1325 		++printed;
1326 	} else
1327 		--row_offset;
1328 
1329 	if (folded_sign == '-' && row != browser->b.rows) {
1330 		struct callchain_print_arg arg = {
1331 			.row_offset = row_offset,
1332 			.is_current_entry = current_entry,
1333 		};
1334 
1335 		printed += hist_browser__show_callchain(browser,
1336 				entry, 1, row,
1337 				hist_browser__show_callchain_entry,
1338 				&arg,
1339 				hist_browser__check_output_full);
1340 	}
1341 
1342 	return printed;
1343 }
1344 
1345 static int hist_browser__show_hierarchy_entry(struct hist_browser *browser,
1346 					      struct hist_entry *entry,
1347 					      unsigned short row,
1348 					      int level)
1349 {
1350 	int printed = 0;
1351 	int width = browser->b.width;
1352 	char folded_sign = ' ';
1353 	bool current_entry = ui_browser__is_current_entry(&browser->b, row);
1354 	off_t row_offset = entry->row_offset;
1355 	bool first = true;
1356 	struct perf_hpp_fmt *fmt;
1357 	struct perf_hpp_list_node *fmt_node;
1358 	struct hpp_arg arg = {
1359 		.b		= &browser->b,
1360 		.current_entry	= current_entry,
1361 	};
1362 	int column = 0;
1363 	int hierarchy_indent = (entry->hists->nr_hpp_node - 2) * HIERARCHY_INDENT;
1364 
1365 	if (current_entry) {
1366 		browser->he_selection = entry;
1367 		browser->selection = &entry->ms;
1368 	}
1369 
1370 	hist_entry__init_have_children(entry);
1371 	folded_sign = hist_entry__folded(entry);
1372 	arg.folded_sign = folded_sign;
1373 
1374 	if (entry->leaf && row_offset) {
1375 		row_offset--;
1376 		goto show_callchain;
1377 	}
1378 
1379 	ui_browser__gotorc(&browser->b, row, 0);
1380 
1381 	if (current_entry && browser->b.navkeypressed)
1382 		ui_browser__set_color(&browser->b, HE_COLORSET_SELECTED);
1383 	else
1384 		ui_browser__set_color(&browser->b, HE_COLORSET_NORMAL);
1385 
1386 	ui_browser__write_nstring(&browser->b, "", level * HIERARCHY_INDENT);
1387 	width -= level * HIERARCHY_INDENT;
1388 
1389 	/* the first hpp_list_node is for overhead columns */
1390 	fmt_node = list_first_entry(&entry->hists->hpp_formats,
1391 				    struct perf_hpp_list_node, list);
1392 	perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
1393 		char s[2048];
1394 		struct perf_hpp hpp = {
1395 			.buf		= s,
1396 			.size		= sizeof(s),
1397 			.ptr		= &arg,
1398 		};
1399 
1400 		if (perf_hpp__should_skip(fmt, entry->hists) ||
1401 		    column++ < browser->b.horiz_scroll)
1402 			continue;
1403 
1404 		if (current_entry && browser->b.navkeypressed) {
1405 			ui_browser__set_color(&browser->b,
1406 					      HE_COLORSET_SELECTED);
1407 		} else {
1408 			ui_browser__set_color(&browser->b,
1409 					      HE_COLORSET_NORMAL);
1410 		}
1411 
1412 		if (first) {
1413 			ui_browser__printf(&browser->b, "%c ", folded_sign);
1414 			width -= 2;
1415 			first = false;
1416 		} else {
1417 			ui_browser__printf(&browser->b, "  ");
1418 			width -= 2;
1419 		}
1420 
1421 		if (fmt->color) {
1422 			int ret = fmt->color(fmt, &hpp, entry);
1423 			hist_entry__snprintf_alignment(entry, &hpp, fmt, ret);
1424 			/*
1425 			 * fmt->color() already used ui_browser to
1426 			 * print the non alignment bits, skip it (+ret):
1427 			 */
1428 			ui_browser__printf(&browser->b, "%s", s + ret);
1429 		} else {
1430 			int ret = fmt->entry(fmt, &hpp, entry);
1431 			hist_entry__snprintf_alignment(entry, &hpp, fmt, ret);
1432 			ui_browser__printf(&browser->b, "%s", s);
1433 		}
1434 		width -= hpp.buf - s;
1435 	}
1436 
1437 	if (!first) {
1438 		ui_browser__write_nstring(&browser->b, "", hierarchy_indent);
1439 		width -= hierarchy_indent;
1440 	}
1441 
1442 	if (column >= browser->b.horiz_scroll) {
1443 		char s[2048];
1444 		struct perf_hpp hpp = {
1445 			.buf		= s,
1446 			.size		= sizeof(s),
1447 			.ptr		= &arg,
1448 		};
1449 
1450 		if (current_entry && browser->b.navkeypressed) {
1451 			ui_browser__set_color(&browser->b,
1452 					      HE_COLORSET_SELECTED);
1453 		} else {
1454 			ui_browser__set_color(&browser->b,
1455 					      HE_COLORSET_NORMAL);
1456 		}
1457 
1458 		perf_hpp_list__for_each_format(entry->hpp_list, fmt) {
1459 			if (first) {
1460 				ui_browser__printf(&browser->b, "%c ", folded_sign);
1461 				first = false;
1462 			} else {
1463 				ui_browser__write_nstring(&browser->b, "", 2);
1464 			}
1465 
1466 			width -= 2;
1467 
1468 			/*
1469 			 * No need to call hist_entry__snprintf_alignment()
1470 			 * since this fmt is always the last column in the
1471 			 * hierarchy mode.
1472 			 */
1473 			if (fmt->color) {
1474 				width -= fmt->color(fmt, &hpp, entry);
1475 			} else {
1476 				int i = 0;
1477 
1478 				width -= fmt->entry(fmt, &hpp, entry);
1479 				ui_browser__printf(&browser->b, "%s", skip_spaces(s));
1480 
1481 				while (isspace(s[i++]))
1482 					width++;
1483 			}
1484 		}
1485 	}
1486 
1487 	/* The scroll bar isn't being used */
1488 	if (!browser->b.navkeypressed)
1489 		width += 1;
1490 
1491 	ui_browser__write_nstring(&browser->b, "", width);
1492 
1493 	++row;
1494 	++printed;
1495 
1496 show_callchain:
1497 	if (entry->leaf && folded_sign == '-' && row != browser->b.rows) {
1498 		struct callchain_print_arg carg = {
1499 			.row_offset = row_offset,
1500 		};
1501 
1502 		printed += hist_browser__show_callchain(browser, entry,
1503 					level + 1, row,
1504 					hist_browser__show_callchain_entry, &carg,
1505 					hist_browser__check_output_full);
1506 	}
1507 
1508 	return printed;
1509 }
1510 
1511 static int hist_browser__show_no_entry(struct hist_browser *browser,
1512 				       unsigned short row, int level)
1513 {
1514 	int width = browser->b.width;
1515 	bool current_entry = ui_browser__is_current_entry(&browser->b, row);
1516 	bool first = true;
1517 	int column = 0;
1518 	int ret;
1519 	struct perf_hpp_fmt *fmt;
1520 	struct perf_hpp_list_node *fmt_node;
1521 	int indent = browser->hists->nr_hpp_node - 2;
1522 
1523 	if (current_entry) {
1524 		browser->he_selection = NULL;
1525 		browser->selection = NULL;
1526 	}
1527 
1528 	ui_browser__gotorc(&browser->b, row, 0);
1529 
1530 	if (current_entry && browser->b.navkeypressed)
1531 		ui_browser__set_color(&browser->b, HE_COLORSET_SELECTED);
1532 	else
1533 		ui_browser__set_color(&browser->b, HE_COLORSET_NORMAL);
1534 
1535 	ui_browser__write_nstring(&browser->b, "", level * HIERARCHY_INDENT);
1536 	width -= level * HIERARCHY_INDENT;
1537 
1538 	/* the first hpp_list_node is for overhead columns */
1539 	fmt_node = list_first_entry(&browser->hists->hpp_formats,
1540 				    struct perf_hpp_list_node, list);
1541 	perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
1542 		if (perf_hpp__should_skip(fmt, browser->hists) ||
1543 		    column++ < browser->b.horiz_scroll)
1544 			continue;
1545 
1546 		ret = fmt->width(fmt, NULL, browser->hists);
1547 
1548 		if (first) {
1549 			/* for folded sign */
1550 			first = false;
1551 			ret++;
1552 		} else {
1553 			/* space between columns */
1554 			ret += 2;
1555 		}
1556 
1557 		ui_browser__write_nstring(&browser->b, "", ret);
1558 		width -= ret;
1559 	}
1560 
1561 	ui_browser__write_nstring(&browser->b, "", indent * HIERARCHY_INDENT);
1562 	width -= indent * HIERARCHY_INDENT;
1563 
1564 	if (column >= browser->b.horiz_scroll) {
1565 		char buf[32];
1566 
1567 		ret = snprintf(buf, sizeof(buf), "no entry >= %.2f%%", browser->min_pcnt);
1568 		ui_browser__printf(&browser->b, "  %s", buf);
1569 		width -= ret + 2;
1570 	}
1571 
1572 	/* The scroll bar isn't being used */
1573 	if (!browser->b.navkeypressed)
1574 		width += 1;
1575 
1576 	ui_browser__write_nstring(&browser->b, "", width);
1577 	return 1;
1578 }
1579 
1580 static int advance_hpp_check(struct perf_hpp *hpp, int inc)
1581 {
1582 	advance_hpp(hpp, inc);
1583 	return hpp->size <= 0;
1584 }
1585 
1586 static int
1587 hists_browser__scnprintf_headers(struct hist_browser *browser, char *buf,
1588 				 size_t size, int line)
1589 {
1590 	struct hists *hists = browser->hists;
1591 	struct perf_hpp dummy_hpp = {
1592 		.buf    = buf,
1593 		.size   = size,
1594 	};
1595 	struct perf_hpp_fmt *fmt;
1596 	size_t ret = 0;
1597 	int column = 0;
1598 	int span = 0;
1599 
1600 	if (hists__has_callchains(hists) && symbol_conf.use_callchain) {
1601 		ret = scnprintf(buf, size, "  ");
1602 		if (advance_hpp_check(&dummy_hpp, ret))
1603 			return ret;
1604 	}
1605 
1606 	hists__for_each_format(browser->hists, fmt) {
1607 		if (perf_hpp__should_skip(fmt, hists)  || column++ < browser->b.horiz_scroll)
1608 			continue;
1609 
1610 		ret = fmt->header(fmt, &dummy_hpp, hists, line, &span);
1611 		if (advance_hpp_check(&dummy_hpp, ret))
1612 			break;
1613 
1614 		if (span)
1615 			continue;
1616 
1617 		ret = scnprintf(dummy_hpp.buf, dummy_hpp.size, "  ");
1618 		if (advance_hpp_check(&dummy_hpp, ret))
1619 			break;
1620 	}
1621 
1622 	return ret;
1623 }
1624 
1625 static int hists_browser__scnprintf_hierarchy_headers(struct hist_browser *browser, char *buf, size_t size)
1626 {
1627 	struct hists *hists = browser->hists;
1628 	struct perf_hpp dummy_hpp = {
1629 		.buf    = buf,
1630 		.size   = size,
1631 	};
1632 	struct perf_hpp_fmt *fmt;
1633 	struct perf_hpp_list_node *fmt_node;
1634 	size_t ret = 0;
1635 	int column = 0;
1636 	int indent = hists->nr_hpp_node - 2;
1637 	bool first_node, first_col;
1638 
1639 	ret = scnprintf(buf, size, "  ");
1640 	if (advance_hpp_check(&dummy_hpp, ret))
1641 		return ret;
1642 
1643 	first_node = true;
1644 	/* the first hpp_list_node is for overhead columns */
1645 	fmt_node = list_first_entry(&hists->hpp_formats,
1646 				    struct perf_hpp_list_node, list);
1647 	perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
1648 		if (column++ < browser->b.horiz_scroll)
1649 			continue;
1650 
1651 		ret = fmt->header(fmt, &dummy_hpp, hists, 0, NULL);
1652 		if (advance_hpp_check(&dummy_hpp, ret))
1653 			break;
1654 
1655 		ret = scnprintf(dummy_hpp.buf, dummy_hpp.size, "  ");
1656 		if (advance_hpp_check(&dummy_hpp, ret))
1657 			break;
1658 
1659 		first_node = false;
1660 	}
1661 
1662 	if (!first_node) {
1663 		ret = scnprintf(dummy_hpp.buf, dummy_hpp.size, "%*s",
1664 				indent * HIERARCHY_INDENT, "");
1665 		if (advance_hpp_check(&dummy_hpp, ret))
1666 			return ret;
1667 	}
1668 
1669 	first_node = true;
1670 	list_for_each_entry_continue(fmt_node, &hists->hpp_formats, list) {
1671 		if (!first_node) {
1672 			ret = scnprintf(dummy_hpp.buf, dummy_hpp.size, " / ");
1673 			if (advance_hpp_check(&dummy_hpp, ret))
1674 				break;
1675 		}
1676 		first_node = false;
1677 
1678 		first_col = true;
1679 		perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
1680 			char *start;
1681 
1682 			if (perf_hpp__should_skip(fmt, hists))
1683 				continue;
1684 
1685 			if (!first_col) {
1686 				ret = scnprintf(dummy_hpp.buf, dummy_hpp.size, "+");
1687 				if (advance_hpp_check(&dummy_hpp, ret))
1688 					break;
1689 			}
1690 			first_col = false;
1691 
1692 			ret = fmt->header(fmt, &dummy_hpp, hists, 0, NULL);
1693 			dummy_hpp.buf[ret] = '\0';
1694 
1695 			start = strim(dummy_hpp.buf);
1696 			ret = strlen(start);
1697 
1698 			if (start != dummy_hpp.buf)
1699 				memmove(dummy_hpp.buf, start, ret + 1);
1700 
1701 			if (advance_hpp_check(&dummy_hpp, ret))
1702 				break;
1703 		}
1704 	}
1705 
1706 	return ret;
1707 }
1708 
1709 static void hists_browser__hierarchy_headers(struct hist_browser *browser)
1710 {
1711 	char headers[1024];
1712 
1713 	hists_browser__scnprintf_hierarchy_headers(browser, headers,
1714 						   sizeof(headers));
1715 
1716 	ui_browser__gotorc(&browser->b, 0, 0);
1717 	ui_browser__set_color(&browser->b, HE_COLORSET_ROOT);
1718 	ui_browser__write_nstring(&browser->b, headers, browser->b.width + 1);
1719 }
1720 
1721 static void hists_browser__headers(struct hist_browser *browser)
1722 {
1723 	struct hists *hists = browser->hists;
1724 	struct perf_hpp_list *hpp_list = hists->hpp_list;
1725 
1726 	int line;
1727 
1728 	for (line = 0; line < hpp_list->nr_header_lines; line++) {
1729 		char headers[1024];
1730 
1731 		hists_browser__scnprintf_headers(browser, headers,
1732 						 sizeof(headers), line);
1733 
1734 		ui_browser__gotorc_title(&browser->b, line, 0);
1735 		ui_browser__set_color(&browser->b, HE_COLORSET_ROOT);
1736 		ui_browser__write_nstring(&browser->b, headers, browser->b.width + 1);
1737 	}
1738 }
1739 
1740 static void hist_browser__show_headers(struct hist_browser *browser)
1741 {
1742 	if (symbol_conf.report_hierarchy)
1743 		hists_browser__hierarchy_headers(browser);
1744 	else
1745 		hists_browser__headers(browser);
1746 }
1747 
1748 static void ui_browser__hists_init_top(struct ui_browser *browser)
1749 {
1750 	if (browser->top == NULL) {
1751 		struct hist_browser *hb;
1752 
1753 		hb = container_of(browser, struct hist_browser, b);
1754 		browser->top = rb_first_cached(&hb->hists->entries);
1755 	}
1756 }
1757 
1758 static unsigned int hist_browser__refresh(struct ui_browser *browser)
1759 {
1760 	unsigned row = 0;
1761 	struct rb_node *nd;
1762 	struct hist_browser *hb = container_of(browser, struct hist_browser, b);
1763 
1764 	if (hb->show_headers)
1765 		hist_browser__show_headers(hb);
1766 
1767 	ui_browser__hists_init_top(browser);
1768 	hb->he_selection = NULL;
1769 	hb->selection = NULL;
1770 
1771 	for (nd = browser->top; nd; nd = rb_hierarchy_next(nd)) {
1772 		struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1773 		float percent;
1774 
1775 		if (h->filtered) {
1776 			/* let it move to sibling */
1777 			h->unfolded = false;
1778 			continue;
1779 		}
1780 
1781 		percent = hist_entry__get_percent_limit(h);
1782 		if (percent < hb->min_pcnt)
1783 			continue;
1784 
1785 		if (symbol_conf.report_hierarchy) {
1786 			row += hist_browser__show_hierarchy_entry(hb, h, row,
1787 								  h->depth);
1788 			if (row == browser->rows)
1789 				break;
1790 
1791 			if (h->has_no_entry) {
1792 				hist_browser__show_no_entry(hb, row, h->depth + 1);
1793 				row++;
1794 			}
1795 		} else {
1796 			row += hist_browser__show_entry(hb, h, row);
1797 		}
1798 
1799 		if (row == browser->rows)
1800 			break;
1801 	}
1802 
1803 	return row;
1804 }
1805 
1806 static struct rb_node *hists__filter_entries(struct rb_node *nd,
1807 					     float min_pcnt)
1808 {
1809 	while (nd != NULL) {
1810 		struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1811 		float percent = hist_entry__get_percent_limit(h);
1812 
1813 		if (!h->filtered && percent >= min_pcnt)
1814 			return nd;
1815 
1816 		/*
1817 		 * If it's filtered, its all children also were filtered.
1818 		 * So move to sibling node.
1819 		 */
1820 		if (rb_next(nd))
1821 			nd = rb_next(nd);
1822 		else
1823 			nd = rb_hierarchy_next(nd);
1824 	}
1825 
1826 	return NULL;
1827 }
1828 
1829 static struct rb_node *hists__filter_prev_entries(struct rb_node *nd,
1830 						  float min_pcnt)
1831 {
1832 	while (nd != NULL) {
1833 		struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1834 		float percent = hist_entry__get_percent_limit(h);
1835 
1836 		if (!h->filtered && percent >= min_pcnt)
1837 			return nd;
1838 
1839 		nd = rb_hierarchy_prev(nd);
1840 	}
1841 
1842 	return NULL;
1843 }
1844 
1845 static void ui_browser__hists_seek(struct ui_browser *browser,
1846 				   off_t offset, int whence)
1847 {
1848 	struct hist_entry *h;
1849 	struct rb_node *nd;
1850 	bool first = true;
1851 	struct hist_browser *hb;
1852 
1853 	hb = container_of(browser, struct hist_browser, b);
1854 
1855 	if (browser->nr_entries == 0)
1856 		return;
1857 
1858 	ui_browser__hists_init_top(browser);
1859 
1860 	switch (whence) {
1861 	case SEEK_SET:
1862 		nd = hists__filter_entries(rb_first(browser->entries),
1863 					   hb->min_pcnt);
1864 		break;
1865 	case SEEK_CUR:
1866 		nd = browser->top;
1867 		goto do_offset;
1868 	case SEEK_END:
1869 		nd = rb_hierarchy_last(rb_last(browser->entries));
1870 		nd = hists__filter_prev_entries(nd, hb->min_pcnt);
1871 		first = false;
1872 		break;
1873 	default:
1874 		return;
1875 	}
1876 
1877 	/*
1878 	 * Moves not relative to the first visible entry invalidates its
1879 	 * row_offset:
1880 	 */
1881 	h = rb_entry(browser->top, struct hist_entry, rb_node);
1882 	h->row_offset = 0;
1883 
1884 	/*
1885 	 * Here we have to check if nd is expanded (+), if it is we can't go
1886 	 * the next top level hist_entry, instead we must compute an offset of
1887 	 * what _not_ to show and not change the first visible entry.
1888 	 *
1889 	 * This offset increments when we are going from top to bottom and
1890 	 * decreases when we're going from bottom to top.
1891 	 *
1892 	 * As we don't have backpointers to the top level in the callchains
1893 	 * structure, we need to always print the whole hist_entry callchain,
1894 	 * skipping the first ones that are before the first visible entry
1895 	 * and stop when we printed enough lines to fill the screen.
1896 	 */
1897 do_offset:
1898 	if (!nd)
1899 		return;
1900 
1901 	if (offset > 0) {
1902 		do {
1903 			h = rb_entry(nd, struct hist_entry, rb_node);
1904 			if (h->unfolded && h->leaf) {
1905 				u16 remaining = h->nr_rows - h->row_offset;
1906 				if (offset > remaining) {
1907 					offset -= remaining;
1908 					h->row_offset = 0;
1909 				} else {
1910 					h->row_offset += offset;
1911 					offset = 0;
1912 					browser->top = nd;
1913 					break;
1914 				}
1915 			}
1916 			nd = hists__filter_entries(rb_hierarchy_next(nd),
1917 						   hb->min_pcnt);
1918 			if (nd == NULL)
1919 				break;
1920 			--offset;
1921 			browser->top = nd;
1922 		} while (offset != 0);
1923 	} else if (offset < 0) {
1924 		while (1) {
1925 			h = rb_entry(nd, struct hist_entry, rb_node);
1926 			if (h->unfolded && h->leaf) {
1927 				if (first) {
1928 					if (-offset > h->row_offset) {
1929 						offset += h->row_offset;
1930 						h->row_offset = 0;
1931 					} else {
1932 						h->row_offset += offset;
1933 						offset = 0;
1934 						browser->top = nd;
1935 						break;
1936 					}
1937 				} else {
1938 					if (-offset > h->nr_rows) {
1939 						offset += h->nr_rows;
1940 						h->row_offset = 0;
1941 					} else {
1942 						h->row_offset = h->nr_rows + offset;
1943 						offset = 0;
1944 						browser->top = nd;
1945 						break;
1946 					}
1947 				}
1948 			}
1949 
1950 			nd = hists__filter_prev_entries(rb_hierarchy_prev(nd),
1951 							hb->min_pcnt);
1952 			if (nd == NULL)
1953 				break;
1954 			++offset;
1955 			browser->top = nd;
1956 			if (offset == 0) {
1957 				/*
1958 				 * Last unfiltered hist_entry, check if it is
1959 				 * unfolded, if it is then we should have
1960 				 * row_offset at its last entry.
1961 				 */
1962 				h = rb_entry(nd, struct hist_entry, rb_node);
1963 				if (h->unfolded && h->leaf)
1964 					h->row_offset = h->nr_rows;
1965 				break;
1966 			}
1967 			first = false;
1968 		}
1969 	} else {
1970 		browser->top = nd;
1971 		h = rb_entry(nd, struct hist_entry, rb_node);
1972 		h->row_offset = 0;
1973 	}
1974 }
1975 
1976 static int hist_browser__fprintf_callchain(struct hist_browser *browser,
1977 					   struct hist_entry *he, FILE *fp,
1978 					   int level)
1979 {
1980 	struct callchain_print_arg arg  = {
1981 		.fp = fp,
1982 	};
1983 
1984 	hist_browser__show_callchain(browser, he, level, 0,
1985 				     hist_browser__fprintf_callchain_entry, &arg,
1986 				     hist_browser__check_dump_full);
1987 	return arg.printed;
1988 }
1989 
1990 static int hist_browser__fprintf_entry(struct hist_browser *browser,
1991 				       struct hist_entry *he, FILE *fp)
1992 {
1993 	char s[8192];
1994 	int printed = 0;
1995 	char folded_sign = ' ';
1996 	struct perf_hpp hpp = {
1997 		.buf = s,
1998 		.size = sizeof(s),
1999 	};
2000 	struct perf_hpp_fmt *fmt;
2001 	bool first = true;
2002 	int ret;
2003 
2004 	if (hist_entry__has_callchains(he) && symbol_conf.use_callchain) {
2005 		folded_sign = hist_entry__folded(he);
2006 		printed += fprintf(fp, "%c ", folded_sign);
2007 	}
2008 
2009 	hists__for_each_format(browser->hists, fmt) {
2010 		if (perf_hpp__should_skip(fmt, he->hists))
2011 			continue;
2012 
2013 		if (!first) {
2014 			ret = scnprintf(hpp.buf, hpp.size, "  ");
2015 			advance_hpp(&hpp, ret);
2016 		} else
2017 			first = false;
2018 
2019 		ret = fmt->entry(fmt, &hpp, he);
2020 		ret = hist_entry__snprintf_alignment(he, &hpp, fmt, ret);
2021 		advance_hpp(&hpp, ret);
2022 	}
2023 	printed += fprintf(fp, "%s\n", s);
2024 
2025 	if (folded_sign == '-')
2026 		printed += hist_browser__fprintf_callchain(browser, he, fp, 1);
2027 
2028 	return printed;
2029 }
2030 
2031 
2032 static int hist_browser__fprintf_hierarchy_entry(struct hist_browser *browser,
2033 						 struct hist_entry *he,
2034 						 FILE *fp, int level)
2035 {
2036 	char s[8192];
2037 	int printed = 0;
2038 	char folded_sign = ' ';
2039 	struct perf_hpp hpp = {
2040 		.buf = s,
2041 		.size = sizeof(s),
2042 	};
2043 	struct perf_hpp_fmt *fmt;
2044 	struct perf_hpp_list_node *fmt_node;
2045 	bool first = true;
2046 	int ret;
2047 	int hierarchy_indent = (he->hists->nr_hpp_node - 2) * HIERARCHY_INDENT;
2048 
2049 	printed = fprintf(fp, "%*s", level * HIERARCHY_INDENT, "");
2050 
2051 	folded_sign = hist_entry__folded(he);
2052 	printed += fprintf(fp, "%c", folded_sign);
2053 
2054 	/* the first hpp_list_node is for overhead columns */
2055 	fmt_node = list_first_entry(&he->hists->hpp_formats,
2056 				    struct perf_hpp_list_node, list);
2057 	perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
2058 		if (!first) {
2059 			ret = scnprintf(hpp.buf, hpp.size, "  ");
2060 			advance_hpp(&hpp, ret);
2061 		} else
2062 			first = false;
2063 
2064 		ret = fmt->entry(fmt, &hpp, he);
2065 		advance_hpp(&hpp, ret);
2066 	}
2067 
2068 	ret = scnprintf(hpp.buf, hpp.size, "%*s", hierarchy_indent, "");
2069 	advance_hpp(&hpp, ret);
2070 
2071 	perf_hpp_list__for_each_format(he->hpp_list, fmt) {
2072 		ret = scnprintf(hpp.buf, hpp.size, "  ");
2073 		advance_hpp(&hpp, ret);
2074 
2075 		ret = fmt->entry(fmt, &hpp, he);
2076 		advance_hpp(&hpp, ret);
2077 	}
2078 
2079 	strim(s);
2080 	printed += fprintf(fp, "%s\n", s);
2081 
2082 	if (he->leaf && folded_sign == '-') {
2083 		printed += hist_browser__fprintf_callchain(browser, he, fp,
2084 							   he->depth + 1);
2085 	}
2086 
2087 	return printed;
2088 }
2089 
2090 static int hist_browser__fprintf(struct hist_browser *browser, FILE *fp)
2091 {
2092 	struct rb_node *nd = hists__filter_entries(rb_first(browser->b.entries),
2093 						   browser->min_pcnt);
2094 	int printed = 0;
2095 
2096 	while (nd) {
2097 		struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2098 
2099 		if (symbol_conf.report_hierarchy) {
2100 			printed += hist_browser__fprintf_hierarchy_entry(browser,
2101 									 h, fp,
2102 									 h->depth);
2103 		} else {
2104 			printed += hist_browser__fprintf_entry(browser, h, fp);
2105 		}
2106 
2107 		nd = hists__filter_entries(rb_hierarchy_next(nd),
2108 					   browser->min_pcnt);
2109 	}
2110 
2111 	return printed;
2112 }
2113 
2114 static int hist_browser__dump(struct hist_browser *browser)
2115 {
2116 	char filename[64];
2117 	FILE *fp;
2118 
2119 	while (1) {
2120 		scnprintf(filename, sizeof(filename), "perf.hist.%d", browser->print_seq);
2121 		if (access(filename, F_OK))
2122 			break;
2123 		/*
2124  		 * XXX: Just an arbitrary lazy upper limit
2125  		 */
2126 		if (++browser->print_seq == 8192) {
2127 			ui_helpline__fpush("Too many perf.hist.N files, nothing written!");
2128 			return -1;
2129 		}
2130 	}
2131 
2132 	fp = fopen(filename, "w");
2133 	if (fp == NULL) {
2134 		char bf[64];
2135 		const char *err = str_error_r(errno, bf, sizeof(bf));
2136 		ui_helpline__fpush("Couldn't write to %s: %s", filename, err);
2137 		return -1;
2138 	}
2139 
2140 	++browser->print_seq;
2141 	hist_browser__fprintf(browser, fp);
2142 	fclose(fp);
2143 	ui_helpline__fpush("%s written!", filename);
2144 
2145 	return 0;
2146 }
2147 
2148 void hist_browser__init(struct hist_browser *browser,
2149 			struct hists *hists)
2150 {
2151 	struct perf_hpp_fmt *fmt;
2152 
2153 	browser->hists			= hists;
2154 	browser->b.refresh		= hist_browser__refresh;
2155 	browser->b.refresh_dimensions	= hist_browser__refresh_dimensions;
2156 	browser->b.seek			= ui_browser__hists_seek;
2157 	browser->b.use_navkeypressed	= true;
2158 	browser->show_headers		= symbol_conf.show_hist_headers;
2159 	hist_browser__set_title_space(browser);
2160 
2161 	if (symbol_conf.report_hierarchy) {
2162 		struct perf_hpp_list_node *fmt_node;
2163 
2164 		/* count overhead columns (in the first node) */
2165 		fmt_node = list_first_entry(&hists->hpp_formats,
2166 					    struct perf_hpp_list_node, list);
2167 		perf_hpp_list__for_each_format(&fmt_node->hpp, fmt)
2168 			++browser->b.columns;
2169 
2170 		/* add a single column for whole hierarchy sort keys*/
2171 		++browser->b.columns;
2172 	} else {
2173 		hists__for_each_format(hists, fmt)
2174 			++browser->b.columns;
2175 	}
2176 
2177 	hists__reset_column_width(hists);
2178 }
2179 
2180 struct hist_browser *hist_browser__new(struct hists *hists)
2181 {
2182 	struct hist_browser *browser = zalloc(sizeof(*browser));
2183 
2184 	if (browser)
2185 		hist_browser__init(browser, hists);
2186 
2187 	return browser;
2188 }
2189 
2190 static struct hist_browser *
2191 perf_evsel_browser__new(struct evsel *evsel,
2192 			struct hist_browser_timer *hbt,
2193 			struct perf_env *env,
2194 			struct annotation_options *annotation_opts)
2195 {
2196 	struct hist_browser *browser = hist_browser__new(evsel__hists(evsel));
2197 
2198 	if (browser) {
2199 		browser->hbt   = hbt;
2200 		browser->env   = env;
2201 		browser->title = hists_browser__scnprintf_title;
2202 		browser->annotation_opts = annotation_opts;
2203 	}
2204 	return browser;
2205 }
2206 
2207 void hist_browser__delete(struct hist_browser *browser)
2208 {
2209 	free(browser);
2210 }
2211 
2212 static struct hist_entry *hist_browser__selected_entry(struct hist_browser *browser)
2213 {
2214 	return browser->he_selection;
2215 }
2216 
2217 static struct thread *hist_browser__selected_thread(struct hist_browser *browser)
2218 {
2219 	return browser->he_selection->thread;
2220 }
2221 
2222 /* Check whether the browser is for 'top' or 'report' */
2223 static inline bool is_report_browser(void *timer)
2224 {
2225 	return timer == NULL;
2226 }
2227 
2228 static int hists_browser__scnprintf_title(struct hist_browser *browser, char *bf, size_t size)
2229 {
2230 	struct hist_browser_timer *hbt = browser->hbt;
2231 	int printed = __hists__scnprintf_title(browser->hists, bf, size, !is_report_browser(hbt));
2232 
2233 	if (!is_report_browser(hbt)) {
2234 		struct perf_top *top = hbt->arg;
2235 
2236 		printed += scnprintf(bf + printed, size - printed,
2237 				     " lost: %" PRIu64 "/%" PRIu64,
2238 				     top->lost, top->lost_total);
2239 
2240 		printed += scnprintf(bf + printed, size - printed,
2241 				     " drop: %" PRIu64 "/%" PRIu64,
2242 				     top->drop, top->drop_total);
2243 
2244 		if (top->zero)
2245 			printed += scnprintf(bf + printed, size - printed, " [z]");
2246 
2247 		perf_top__reset_sample_counters(top);
2248 	}
2249 
2250 
2251 	return printed;
2252 }
2253 
2254 static inline void free_popup_options(char **options, int n)
2255 {
2256 	int i;
2257 
2258 	for (i = 0; i < n; ++i)
2259 		zfree(&options[i]);
2260 }
2261 
2262 /*
2263  * Only runtime switching of perf data file will make "input_name" point
2264  * to a malloced buffer. So add "is_input_name_malloced" flag to decide
2265  * whether we need to call free() for current "input_name" during the switch.
2266  */
2267 static bool is_input_name_malloced = false;
2268 
2269 static int switch_data_file(void)
2270 {
2271 	char *pwd, *options[32], *abs_path[32], *tmp;
2272 	DIR *pwd_dir;
2273 	int nr_options = 0, choice = -1, ret = -1;
2274 	struct dirent *dent;
2275 
2276 	pwd = getenv("PWD");
2277 	if (!pwd)
2278 		return ret;
2279 
2280 	pwd_dir = opendir(pwd);
2281 	if (!pwd_dir)
2282 		return ret;
2283 
2284 	memset(options, 0, sizeof(options));
2285 	memset(abs_path, 0, sizeof(abs_path));
2286 
2287 	while ((dent = readdir(pwd_dir))) {
2288 		char path[PATH_MAX];
2289 		u64 magic;
2290 		char *name = dent->d_name;
2291 		FILE *file;
2292 
2293 		if (!(dent->d_type == DT_REG))
2294 			continue;
2295 
2296 		snprintf(path, sizeof(path), "%s/%s", pwd, name);
2297 
2298 		file = fopen(path, "r");
2299 		if (!file)
2300 			continue;
2301 
2302 		if (fread(&magic, 1, 8, file) < 8)
2303 			goto close_file_and_continue;
2304 
2305 		if (is_perf_magic(magic)) {
2306 			options[nr_options] = strdup(name);
2307 			if (!options[nr_options])
2308 				goto close_file_and_continue;
2309 
2310 			abs_path[nr_options] = strdup(path);
2311 			if (!abs_path[nr_options]) {
2312 				zfree(&options[nr_options]);
2313 				ui__warning("Can't search all data files due to memory shortage.\n");
2314 				fclose(file);
2315 				break;
2316 			}
2317 
2318 			nr_options++;
2319 		}
2320 
2321 close_file_and_continue:
2322 		fclose(file);
2323 		if (nr_options >= 32) {
2324 			ui__warning("Too many perf data files in PWD!\n"
2325 				    "Only the first 32 files will be listed.\n");
2326 			break;
2327 		}
2328 	}
2329 	closedir(pwd_dir);
2330 
2331 	if (nr_options) {
2332 		choice = ui__popup_menu(nr_options, options);
2333 		if (choice < nr_options && choice >= 0) {
2334 			tmp = strdup(abs_path[choice]);
2335 			if (tmp) {
2336 				if (is_input_name_malloced)
2337 					free((void *)input_name);
2338 				input_name = tmp;
2339 				is_input_name_malloced = true;
2340 				ret = 0;
2341 			} else
2342 				ui__warning("Data switch failed due to memory shortage!\n");
2343 		}
2344 	}
2345 
2346 	free_popup_options(options, nr_options);
2347 	free_popup_options(abs_path, nr_options);
2348 	return ret;
2349 }
2350 
2351 struct popup_action {
2352 	unsigned long		time;
2353 	struct thread 		*thread;
2354 	struct map_symbol 	ms;
2355 	int			socket;
2356 	struct evsel	*evsel;
2357 	enum rstype		rstype;
2358 
2359 	int (*fn)(struct hist_browser *browser, struct popup_action *act);
2360 };
2361 
2362 static int
2363 do_annotate(struct hist_browser *browser, struct popup_action *act)
2364 {
2365 	struct evsel *evsel;
2366 	struct annotation *notes;
2367 	struct hist_entry *he;
2368 	int err;
2369 
2370 	if (!browser->annotation_opts->objdump_path &&
2371 	    perf_env__lookup_objdump(browser->env, &browser->annotation_opts->objdump_path))
2372 		return 0;
2373 
2374 	notes = symbol__annotation(act->ms.sym);
2375 	if (!notes->src)
2376 		return 0;
2377 
2378 	evsel = hists_to_evsel(browser->hists);
2379 	err = map_symbol__tui_annotate(&act->ms, evsel, browser->hbt,
2380 				       browser->annotation_opts);
2381 	he = hist_browser__selected_entry(browser);
2382 	/*
2383 	 * offer option to annotate the other branch source or target
2384 	 * (if they exists) when returning from annotate
2385 	 */
2386 	if ((err == 'q' || err == CTRL('c')) && he->branch_info)
2387 		return 1;
2388 
2389 	ui_browser__update_nr_entries(&browser->b, browser->hists->nr_entries);
2390 	if (err)
2391 		ui_browser__handle_resize(&browser->b);
2392 	return 0;
2393 }
2394 
2395 static int
2396 add_annotate_opt(struct hist_browser *browser __maybe_unused,
2397 		 struct popup_action *act, char **optstr,
2398 		 struct map *map, struct symbol *sym)
2399 {
2400 	if (sym == NULL || map->dso->annotate_warned)
2401 		return 0;
2402 
2403 	if (asprintf(optstr, "Annotate %s", sym->name) < 0)
2404 		return 0;
2405 
2406 	act->ms.map = map;
2407 	act->ms.sym = sym;
2408 	act->fn = do_annotate;
2409 	return 1;
2410 }
2411 
2412 static int
2413 do_zoom_thread(struct hist_browser *browser, struct popup_action *act)
2414 {
2415 	struct thread *thread = act->thread;
2416 
2417 	if ((!hists__has(browser->hists, thread) &&
2418 	     !hists__has(browser->hists, comm)) || thread == NULL)
2419 		return 0;
2420 
2421 	if (browser->hists->thread_filter) {
2422 		pstack__remove(browser->pstack, &browser->hists->thread_filter);
2423 		perf_hpp__set_elide(HISTC_THREAD, false);
2424 		thread__zput(browser->hists->thread_filter);
2425 		ui_helpline__pop();
2426 	} else {
2427 		if (hists__has(browser->hists, thread)) {
2428 			ui_helpline__fpush("To zoom out press ESC or ENTER + \"Zoom out of %s(%d) thread\"",
2429 					   thread->comm_set ? thread__comm_str(thread) : "",
2430 					   thread->tid);
2431 		} else {
2432 			ui_helpline__fpush("To zoom out press ESC or ENTER + \"Zoom out of %s thread\"",
2433 					   thread->comm_set ? thread__comm_str(thread) : "");
2434 		}
2435 
2436 		browser->hists->thread_filter = thread__get(thread);
2437 		perf_hpp__set_elide(HISTC_THREAD, false);
2438 		pstack__push(browser->pstack, &browser->hists->thread_filter);
2439 	}
2440 
2441 	hists__filter_by_thread(browser->hists);
2442 	hist_browser__reset(browser);
2443 	return 0;
2444 }
2445 
2446 static int
2447 add_thread_opt(struct hist_browser *browser, struct popup_action *act,
2448 	       char **optstr, struct thread *thread)
2449 {
2450 	int ret;
2451 
2452 	if ((!hists__has(browser->hists, thread) &&
2453 	     !hists__has(browser->hists, comm)) || thread == NULL)
2454 		return 0;
2455 
2456 	if (hists__has(browser->hists, thread)) {
2457 		ret = asprintf(optstr, "Zoom %s %s(%d) thread",
2458 			       browser->hists->thread_filter ? "out of" : "into",
2459 			       thread->comm_set ? thread__comm_str(thread) : "",
2460 			       thread->tid);
2461 	} else {
2462 		ret = asprintf(optstr, "Zoom %s %s thread",
2463 			       browser->hists->thread_filter ? "out of" : "into",
2464 			       thread->comm_set ? thread__comm_str(thread) : "");
2465 	}
2466 	if (ret < 0)
2467 		return 0;
2468 
2469 	act->thread = thread;
2470 	act->fn = do_zoom_thread;
2471 	return 1;
2472 }
2473 
2474 static int
2475 do_zoom_dso(struct hist_browser *browser, struct popup_action *act)
2476 {
2477 	struct map *map = act->ms.map;
2478 
2479 	if (!hists__has(browser->hists, dso) || map == NULL)
2480 		return 0;
2481 
2482 	if (browser->hists->dso_filter) {
2483 		pstack__remove(browser->pstack, &browser->hists->dso_filter);
2484 		perf_hpp__set_elide(HISTC_DSO, false);
2485 		browser->hists->dso_filter = NULL;
2486 		ui_helpline__pop();
2487 	} else {
2488 		ui_helpline__fpush("To zoom out press ESC or ENTER + \"Zoom out of %s DSO\"",
2489 				   __map__is_kernel(map) ? "the Kernel" : map->dso->short_name);
2490 		browser->hists->dso_filter = map->dso;
2491 		perf_hpp__set_elide(HISTC_DSO, true);
2492 		pstack__push(browser->pstack, &browser->hists->dso_filter);
2493 	}
2494 
2495 	hists__filter_by_dso(browser->hists);
2496 	hist_browser__reset(browser);
2497 	return 0;
2498 }
2499 
2500 static int
2501 add_dso_opt(struct hist_browser *browser, struct popup_action *act,
2502 	    char **optstr, struct map *map)
2503 {
2504 	if (!hists__has(browser->hists, dso) || map == NULL)
2505 		return 0;
2506 
2507 	if (asprintf(optstr, "Zoom %s %s DSO",
2508 		     browser->hists->dso_filter ? "out of" : "into",
2509 		     __map__is_kernel(map) ? "the Kernel" : map->dso->short_name) < 0)
2510 		return 0;
2511 
2512 	act->ms.map = map;
2513 	act->fn = do_zoom_dso;
2514 	return 1;
2515 }
2516 
2517 static int
2518 do_browse_map(struct hist_browser *browser __maybe_unused,
2519 	      struct popup_action *act)
2520 {
2521 	map__browse(act->ms.map);
2522 	return 0;
2523 }
2524 
2525 static int
2526 add_map_opt(struct hist_browser *browser,
2527 	    struct popup_action *act, char **optstr, struct map *map)
2528 {
2529 	if (!hists__has(browser->hists, dso) || map == NULL)
2530 		return 0;
2531 
2532 	if (asprintf(optstr, "Browse map details") < 0)
2533 		return 0;
2534 
2535 	act->ms.map = map;
2536 	act->fn = do_browse_map;
2537 	return 1;
2538 }
2539 
2540 static int
2541 do_run_script(struct hist_browser *browser __maybe_unused,
2542 	      struct popup_action *act)
2543 {
2544 	char *script_opt;
2545 	int len;
2546 	int n = 0;
2547 
2548 	len = 100;
2549 	if (act->thread)
2550 		len += strlen(thread__comm_str(act->thread));
2551 	else if (act->ms.sym)
2552 		len += strlen(act->ms.sym->name);
2553 	script_opt = malloc(len);
2554 	if (!script_opt)
2555 		return -1;
2556 
2557 	script_opt[0] = 0;
2558 	if (act->thread) {
2559 		n = scnprintf(script_opt, len, " -c %s ",
2560 			  thread__comm_str(act->thread));
2561 	} else if (act->ms.sym) {
2562 		n = scnprintf(script_opt, len, " -S %s ",
2563 			  act->ms.sym->name);
2564 	}
2565 
2566 	if (act->time) {
2567 		char start[32], end[32];
2568 		unsigned long starttime = act->time;
2569 		unsigned long endtime = act->time + symbol_conf.time_quantum;
2570 
2571 		if (starttime == endtime) { /* Display 1ms as fallback */
2572 			starttime -= 1*NSEC_PER_MSEC;
2573 			endtime += 1*NSEC_PER_MSEC;
2574 		}
2575 		timestamp__scnprintf_usec(starttime, start, sizeof start);
2576 		timestamp__scnprintf_usec(endtime, end, sizeof end);
2577 		n += snprintf(script_opt + n, len - n, " --time %s,%s", start, end);
2578 	}
2579 
2580 	script_browse(script_opt, act->evsel);
2581 	free(script_opt);
2582 	return 0;
2583 }
2584 
2585 static int
2586 do_res_sample_script(struct hist_browser *browser __maybe_unused,
2587 		     struct popup_action *act)
2588 {
2589 	struct hist_entry *he;
2590 
2591 	he = hist_browser__selected_entry(browser);
2592 	res_sample_browse(he->res_samples, he->num_res, act->evsel, act->rstype);
2593 	return 0;
2594 }
2595 
2596 static int
2597 add_script_opt_2(struct hist_browser *browser __maybe_unused,
2598 	       struct popup_action *act, char **optstr,
2599 	       struct thread *thread, struct symbol *sym,
2600 	       struct evsel *evsel, const char *tstr)
2601 {
2602 
2603 	if (thread) {
2604 		if (asprintf(optstr, "Run scripts for samples of thread [%s]%s",
2605 			     thread__comm_str(thread), tstr) < 0)
2606 			return 0;
2607 	} else if (sym) {
2608 		if (asprintf(optstr, "Run scripts for samples of symbol [%s]%s",
2609 			     sym->name, tstr) < 0)
2610 			return 0;
2611 	} else {
2612 		if (asprintf(optstr, "Run scripts for all samples%s", tstr) < 0)
2613 			return 0;
2614 	}
2615 
2616 	act->thread = thread;
2617 	act->ms.sym = sym;
2618 	act->evsel = evsel;
2619 	act->fn = do_run_script;
2620 	return 1;
2621 }
2622 
2623 static int
2624 add_script_opt(struct hist_browser *browser,
2625 	       struct popup_action *act, char **optstr,
2626 	       struct thread *thread, struct symbol *sym,
2627 	       struct evsel *evsel)
2628 {
2629 	int n, j;
2630 	struct hist_entry *he;
2631 
2632 	n = add_script_opt_2(browser, act, optstr, thread, sym, evsel, "");
2633 
2634 	he = hist_browser__selected_entry(browser);
2635 	if (sort_order && strstr(sort_order, "time")) {
2636 		char tstr[128];
2637 
2638 		optstr++;
2639 		act++;
2640 		j = sprintf(tstr, " in ");
2641 		j += timestamp__scnprintf_usec(he->time, tstr + j,
2642 					       sizeof tstr - j);
2643 		j += sprintf(tstr + j, "-");
2644 		timestamp__scnprintf_usec(he->time + symbol_conf.time_quantum,
2645 				          tstr + j, sizeof tstr - j);
2646 		n += add_script_opt_2(browser, act, optstr, thread, sym,
2647 					  evsel, tstr);
2648 		act->time = he->time;
2649 	}
2650 	return n;
2651 }
2652 
2653 static int
2654 add_res_sample_opt(struct hist_browser *browser __maybe_unused,
2655 		   struct popup_action *act, char **optstr,
2656 		   struct res_sample *res_sample,
2657 		   struct evsel *evsel,
2658 		   enum rstype type)
2659 {
2660 	if (!res_sample)
2661 		return 0;
2662 
2663 	if (asprintf(optstr, "Show context for individual samples %s",
2664 		type == A_ASM ? "with assembler" :
2665 		type == A_SOURCE ? "with source" : "") < 0)
2666 		return 0;
2667 
2668 	act->fn = do_res_sample_script;
2669 	act->evsel = evsel;
2670 	act->rstype = type;
2671 	return 1;
2672 }
2673 
2674 static int
2675 do_switch_data(struct hist_browser *browser __maybe_unused,
2676 	       struct popup_action *act __maybe_unused)
2677 {
2678 	if (switch_data_file()) {
2679 		ui__warning("Won't switch the data files due to\n"
2680 			    "no valid data file get selected!\n");
2681 		return 0;
2682 	}
2683 
2684 	return K_SWITCH_INPUT_DATA;
2685 }
2686 
2687 static int
2688 add_switch_opt(struct hist_browser *browser,
2689 	       struct popup_action *act, char **optstr)
2690 {
2691 	if (!is_report_browser(browser->hbt))
2692 		return 0;
2693 
2694 	if (asprintf(optstr, "Switch to another data file in PWD") < 0)
2695 		return 0;
2696 
2697 	act->fn = do_switch_data;
2698 	return 1;
2699 }
2700 
2701 static int
2702 do_exit_browser(struct hist_browser *browser __maybe_unused,
2703 		struct popup_action *act __maybe_unused)
2704 {
2705 	return 0;
2706 }
2707 
2708 static int
2709 add_exit_opt(struct hist_browser *browser __maybe_unused,
2710 	     struct popup_action *act, char **optstr)
2711 {
2712 	if (asprintf(optstr, "Exit") < 0)
2713 		return 0;
2714 
2715 	act->fn = do_exit_browser;
2716 	return 1;
2717 }
2718 
2719 static int
2720 do_zoom_socket(struct hist_browser *browser, struct popup_action *act)
2721 {
2722 	if (!hists__has(browser->hists, socket) || act->socket < 0)
2723 		return 0;
2724 
2725 	if (browser->hists->socket_filter > -1) {
2726 		pstack__remove(browser->pstack, &browser->hists->socket_filter);
2727 		browser->hists->socket_filter = -1;
2728 		perf_hpp__set_elide(HISTC_SOCKET, false);
2729 	} else {
2730 		browser->hists->socket_filter = act->socket;
2731 		perf_hpp__set_elide(HISTC_SOCKET, true);
2732 		pstack__push(browser->pstack, &browser->hists->socket_filter);
2733 	}
2734 
2735 	hists__filter_by_socket(browser->hists);
2736 	hist_browser__reset(browser);
2737 	return 0;
2738 }
2739 
2740 static int
2741 add_socket_opt(struct hist_browser *browser, struct popup_action *act,
2742 	       char **optstr, int socket_id)
2743 {
2744 	if (!hists__has(browser->hists, socket) || socket_id < 0)
2745 		return 0;
2746 
2747 	if (asprintf(optstr, "Zoom %s Processor Socket %d",
2748 		     (browser->hists->socket_filter > -1) ? "out of" : "into",
2749 		     socket_id) < 0)
2750 		return 0;
2751 
2752 	act->socket = socket_id;
2753 	act->fn = do_zoom_socket;
2754 	return 1;
2755 }
2756 
2757 static void hist_browser__update_nr_entries(struct hist_browser *hb)
2758 {
2759 	u64 nr_entries = 0;
2760 	struct rb_node *nd = rb_first_cached(&hb->hists->entries);
2761 
2762 	if (hb->min_pcnt == 0 && !symbol_conf.report_hierarchy) {
2763 		hb->nr_non_filtered_entries = hb->hists->nr_non_filtered_entries;
2764 		return;
2765 	}
2766 
2767 	while ((nd = hists__filter_entries(nd, hb->min_pcnt)) != NULL) {
2768 		nr_entries++;
2769 		nd = rb_hierarchy_next(nd);
2770 	}
2771 
2772 	hb->nr_non_filtered_entries = nr_entries;
2773 	hb->nr_hierarchy_entries = nr_entries;
2774 }
2775 
2776 static void hist_browser__update_percent_limit(struct hist_browser *hb,
2777 					       double percent)
2778 {
2779 	struct hist_entry *he;
2780 	struct rb_node *nd = rb_first_cached(&hb->hists->entries);
2781 	u64 total = hists__total_period(hb->hists);
2782 	u64 min_callchain_hits = total * (percent / 100);
2783 
2784 	hb->min_pcnt = callchain_param.min_percent = percent;
2785 
2786 	while ((nd = hists__filter_entries(nd, hb->min_pcnt)) != NULL) {
2787 		he = rb_entry(nd, struct hist_entry, rb_node);
2788 
2789 		if (he->has_no_entry) {
2790 			he->has_no_entry = false;
2791 			he->nr_rows = 0;
2792 		}
2793 
2794 		if (!he->leaf || !hist_entry__has_callchains(he) || !symbol_conf.use_callchain)
2795 			goto next;
2796 
2797 		if (callchain_param.mode == CHAIN_GRAPH_REL) {
2798 			total = he->stat.period;
2799 
2800 			if (symbol_conf.cumulate_callchain)
2801 				total = he->stat_acc->period;
2802 
2803 			min_callchain_hits = total * (percent / 100);
2804 		}
2805 
2806 		callchain_param.sort(&he->sorted_chain, he->callchain,
2807 				     min_callchain_hits, &callchain_param);
2808 
2809 next:
2810 		nd = __rb_hierarchy_next(nd, HMD_FORCE_CHILD);
2811 
2812 		/* force to re-evaluate folding state of callchains */
2813 		he->init_have_children = false;
2814 		hist_entry__set_folding(he, hb, false);
2815 	}
2816 }
2817 
2818 static int perf_evsel__hists_browse(struct evsel *evsel, int nr_events,
2819 				    const char *helpline,
2820 				    bool left_exits,
2821 				    struct hist_browser_timer *hbt,
2822 				    float min_pcnt,
2823 				    struct perf_env *env,
2824 				    bool warn_lost_event,
2825 				    struct annotation_options *annotation_opts)
2826 {
2827 	struct hists *hists = evsel__hists(evsel);
2828 	struct hist_browser *browser = perf_evsel_browser__new(evsel, hbt, env, annotation_opts);
2829 	struct branch_info *bi = NULL;
2830 #define MAX_OPTIONS  16
2831 	char *options[MAX_OPTIONS];
2832 	struct popup_action actions[MAX_OPTIONS];
2833 	int nr_options = 0;
2834 	int key = -1;
2835 	char buf[64];
2836 	int delay_secs = hbt ? hbt->refresh : 0;
2837 
2838 #define HIST_BROWSER_HELP_COMMON					\
2839 	"h/?/F1        Show this window\n"				\
2840 	"UP/DOWN/PGUP\n"						\
2841 	"PGDN/SPACE    Navigate\n"					\
2842 	"q/ESC/CTRL+C  Exit browser or go back to previous screen\n\n"	\
2843 	"For multiple event sessions:\n\n"				\
2844 	"TAB/UNTAB     Switch events\n\n"				\
2845 	"For symbolic views (--sort has sym):\n\n"			\
2846 	"ENTER         Zoom into DSO/Threads & Annotate current symbol\n" \
2847 	"ESC           Zoom out\n"					\
2848 	"a             Annotate current symbol\n"			\
2849 	"C             Collapse all callchains\n"			\
2850 	"d             Zoom into current DSO\n"				\
2851 	"E             Expand all callchains\n"				\
2852 	"F             Toggle percentage of filtered entries\n"		\
2853 	"H             Display column headers\n"			\
2854 	"L             Change percent limit\n"				\
2855 	"m             Display context menu\n"				\
2856 	"S             Zoom into current Processor Socket\n"		\
2857 
2858 	/* help messages are sorted by lexical order of the hotkey */
2859 	static const char report_help[] = HIST_BROWSER_HELP_COMMON
2860 	"i             Show header information\n"
2861 	"P             Print histograms to perf.hist.N\n"
2862 	"r             Run available scripts\n"
2863 	"s             Switch to another data file in PWD\n"
2864 	"t             Zoom into current Thread\n"
2865 	"V             Verbose (DSO names in callchains, etc)\n"
2866 	"/             Filter symbol by name";
2867 	static const char top_help[] = HIST_BROWSER_HELP_COMMON
2868 	"P             Print histograms to perf.hist.N\n"
2869 	"t             Zoom into current Thread\n"
2870 	"V             Verbose (DSO names in callchains, etc)\n"
2871 	"z             Toggle zeroing of samples\n"
2872 	"f             Enable/Disable events\n"
2873 	"/             Filter symbol by name";
2874 
2875 	if (browser == NULL)
2876 		return -1;
2877 
2878 	/* reset abort key so that it can get Ctrl-C as a key */
2879 	SLang_reset_tty();
2880 	SLang_init_tty(0, 0, 0);
2881 
2882 	if (min_pcnt)
2883 		browser->min_pcnt = min_pcnt;
2884 	hist_browser__update_nr_entries(browser);
2885 
2886 	browser->pstack = pstack__new(3);
2887 	if (browser->pstack == NULL)
2888 		goto out;
2889 
2890 	ui_helpline__push(helpline);
2891 
2892 	memset(options, 0, sizeof(options));
2893 	memset(actions, 0, sizeof(actions));
2894 
2895 	if (symbol_conf.col_width_list_str)
2896 		perf_hpp__set_user_width(symbol_conf.col_width_list_str);
2897 
2898 	if (!is_report_browser(hbt))
2899 		browser->b.no_samples_msg = "Collecting samples...";
2900 
2901 	while (1) {
2902 		struct thread *thread = NULL;
2903 		struct map *map = NULL;
2904 		int choice = 0;
2905 		int socked_id = -1;
2906 
2907 		nr_options = 0;
2908 
2909 		key = hist_browser__run(browser, helpline,
2910 					warn_lost_event);
2911 
2912 		if (browser->he_selection != NULL) {
2913 			thread = hist_browser__selected_thread(browser);
2914 			map = browser->selection->map;
2915 			socked_id = browser->he_selection->socket;
2916 		}
2917 		switch (key) {
2918 		case K_TAB:
2919 		case K_UNTAB:
2920 			if (nr_events == 1)
2921 				continue;
2922 			/*
2923 			 * Exit the browser, let hists__browser_tree
2924 			 * go to the next or previous
2925 			 */
2926 			goto out_free_stack;
2927 		case 'a':
2928 			if (!hists__has(hists, sym)) {
2929 				ui_browser__warning(&browser->b, delay_secs * 2,
2930 			"Annotation is only available for symbolic views, "
2931 			"include \"sym*\" in --sort to use it.");
2932 				continue;
2933 			}
2934 
2935 			if (browser->selection == NULL ||
2936 			    browser->selection->sym == NULL ||
2937 			    browser->selection->map->dso->annotate_warned)
2938 				continue;
2939 
2940 			actions->ms.map = browser->selection->map;
2941 			actions->ms.sym = browser->selection->sym;
2942 			do_annotate(browser, actions);
2943 			continue;
2944 		case 'P':
2945 			hist_browser__dump(browser);
2946 			continue;
2947 		case 'd':
2948 			actions->ms.map = map;
2949 			do_zoom_dso(browser, actions);
2950 			continue;
2951 		case 'V':
2952 			verbose = (verbose + 1) % 4;
2953 			browser->show_dso = verbose > 0;
2954 			ui_helpline__fpush("Verbosity level set to %d\n",
2955 					   verbose);
2956 			continue;
2957 		case 't':
2958 			actions->thread = thread;
2959 			do_zoom_thread(browser, actions);
2960 			continue;
2961 		case 'S':
2962 			actions->socket = socked_id;
2963 			do_zoom_socket(browser, actions);
2964 			continue;
2965 		case '/':
2966 			if (ui_browser__input_window("Symbol to show",
2967 					"Please enter the name of symbol you want to see.\n"
2968 					"To remove the filter later, press / + ENTER.",
2969 					buf, "ENTER: OK, ESC: Cancel",
2970 					delay_secs * 2) == K_ENTER) {
2971 				hists->symbol_filter_str = *buf ? buf : NULL;
2972 				hists__filter_by_symbol(hists);
2973 				hist_browser__reset(browser);
2974 			}
2975 			continue;
2976 		case 'r':
2977 			if (is_report_browser(hbt)) {
2978 				actions->thread = NULL;
2979 				actions->ms.sym = NULL;
2980 				do_run_script(browser, actions);
2981 			}
2982 			continue;
2983 		case 's':
2984 			if (is_report_browser(hbt)) {
2985 				key = do_switch_data(browser, actions);
2986 				if (key == K_SWITCH_INPUT_DATA)
2987 					goto out_free_stack;
2988 			}
2989 			continue;
2990 		case 'i':
2991 			/* env->arch is NULL for live-mode (i.e. perf top) */
2992 			if (env->arch)
2993 				tui__header_window(env);
2994 			continue;
2995 		case 'F':
2996 			symbol_conf.filter_relative ^= 1;
2997 			continue;
2998 		case 'z':
2999 			if (!is_report_browser(hbt)) {
3000 				struct perf_top *top = hbt->arg;
3001 
3002 				top->zero = !top->zero;
3003 			}
3004 			continue;
3005 		case 'L':
3006 			if (ui_browser__input_window("Percent Limit",
3007 					"Please enter the value you want to hide entries under that percent.",
3008 					buf, "ENTER: OK, ESC: Cancel",
3009 					delay_secs * 2) == K_ENTER) {
3010 				char *end;
3011 				double new_percent = strtod(buf, &end);
3012 
3013 				if (new_percent < 0 || new_percent > 100) {
3014 					ui_browser__warning(&browser->b, delay_secs * 2,
3015 						"Invalid percent: %.2f", new_percent);
3016 					continue;
3017 				}
3018 
3019 				hist_browser__update_percent_limit(browser, new_percent);
3020 				hist_browser__reset(browser);
3021 			}
3022 			continue;
3023 		case K_F1:
3024 		case 'h':
3025 		case '?':
3026 			ui_browser__help_window(&browser->b,
3027 				is_report_browser(hbt) ? report_help : top_help);
3028 			continue;
3029 		case K_ENTER:
3030 		case K_RIGHT:
3031 		case 'm':
3032 			/* menu */
3033 			break;
3034 		case K_ESC:
3035 		case K_LEFT: {
3036 			const void *top;
3037 
3038 			if (pstack__empty(browser->pstack)) {
3039 				/*
3040 				 * Go back to the perf_evsel_menu__run or other user
3041 				 */
3042 				if (left_exits)
3043 					goto out_free_stack;
3044 
3045 				if (key == K_ESC &&
3046 				    ui_browser__dialog_yesno(&browser->b,
3047 							     "Do you really want to exit?"))
3048 					goto out_free_stack;
3049 
3050 				continue;
3051 			}
3052 			top = pstack__peek(browser->pstack);
3053 			if (top == &browser->hists->dso_filter) {
3054 				/*
3055 				 * No need to set actions->dso here since
3056 				 * it's just to remove the current filter.
3057 				 * Ditto for thread below.
3058 				 */
3059 				do_zoom_dso(browser, actions);
3060 			} else if (top == &browser->hists->thread_filter) {
3061 				do_zoom_thread(browser, actions);
3062 			} else if (top == &browser->hists->socket_filter) {
3063 				do_zoom_socket(browser, actions);
3064 			}
3065 			continue;
3066 		}
3067 		case 'q':
3068 		case CTRL('c'):
3069 			goto out_free_stack;
3070 		case 'f':
3071 			if (!is_report_browser(hbt)) {
3072 				struct perf_top *top = hbt->arg;
3073 
3074 				perf_evlist__toggle_enable(top->evlist);
3075 				/*
3076 				 * No need to refresh, resort/decay histogram
3077 				 * entries if we are not collecting samples:
3078 				 */
3079 				if (top->evlist->enabled) {
3080 					helpline = "Press 'f' to disable the events or 'h' to see other hotkeys";
3081 					hbt->refresh = delay_secs;
3082 				} else {
3083 					helpline = "Press 'f' again to re-enable the events";
3084 					hbt->refresh = 0;
3085 				}
3086 				continue;
3087 			}
3088 			/* Fall thru */
3089 		default:
3090 			helpline = "Press '?' for help on key bindings";
3091 			continue;
3092 		}
3093 
3094 		if (!hists__has(hists, sym) || browser->selection == NULL)
3095 			goto skip_annotation;
3096 
3097 		if (sort__mode == SORT_MODE__BRANCH) {
3098 
3099 			if (browser->he_selection)
3100 				bi = browser->he_selection->branch_info;
3101 
3102 			if (bi == NULL)
3103 				goto skip_annotation;
3104 
3105 			nr_options += add_annotate_opt(browser,
3106 						       &actions[nr_options],
3107 						       &options[nr_options],
3108 						       bi->from.map,
3109 						       bi->from.sym);
3110 			if (bi->to.sym != bi->from.sym)
3111 				nr_options += add_annotate_opt(browser,
3112 							&actions[nr_options],
3113 							&options[nr_options],
3114 							bi->to.map,
3115 							bi->to.sym);
3116 		} else {
3117 			nr_options += add_annotate_opt(browser,
3118 						       &actions[nr_options],
3119 						       &options[nr_options],
3120 						       browser->selection->map,
3121 						       browser->selection->sym);
3122 		}
3123 skip_annotation:
3124 		nr_options += add_thread_opt(browser, &actions[nr_options],
3125 					     &options[nr_options], thread);
3126 		nr_options += add_dso_opt(browser, &actions[nr_options],
3127 					  &options[nr_options], map);
3128 		nr_options += add_map_opt(browser, &actions[nr_options],
3129 					  &options[nr_options],
3130 					  browser->selection ?
3131 						browser->selection->map : NULL);
3132 		nr_options += add_socket_opt(browser, &actions[nr_options],
3133 					     &options[nr_options],
3134 					     socked_id);
3135 		/* perf script support */
3136 		if (!is_report_browser(hbt))
3137 			goto skip_scripting;
3138 
3139 		if (browser->he_selection) {
3140 			if (hists__has(hists, thread) && thread) {
3141 				nr_options += add_script_opt(browser,
3142 							     &actions[nr_options],
3143 							     &options[nr_options],
3144 							     thread, NULL, evsel);
3145 			}
3146 			/*
3147 			 * Note that browser->selection != NULL
3148 			 * when browser->he_selection is not NULL,
3149 			 * so we don't need to check browser->selection
3150 			 * before fetching browser->selection->sym like what
3151 			 * we do before fetching browser->selection->map.
3152 			 *
3153 			 * See hist_browser__show_entry.
3154 			 */
3155 			if (hists__has(hists, sym) && browser->selection->sym) {
3156 				nr_options += add_script_opt(browser,
3157 							     &actions[nr_options],
3158 							     &options[nr_options],
3159 							     NULL, browser->selection->sym,
3160 							     evsel);
3161 			}
3162 		}
3163 		nr_options += add_script_opt(browser, &actions[nr_options],
3164 					     &options[nr_options], NULL, NULL, evsel);
3165 		nr_options += add_res_sample_opt(browser, &actions[nr_options],
3166 						 &options[nr_options],
3167 				 hist_browser__selected_entry(browser)->res_samples,
3168 				 evsel, A_NORMAL);
3169 		nr_options += add_res_sample_opt(browser, &actions[nr_options],
3170 						 &options[nr_options],
3171 				 hist_browser__selected_entry(browser)->res_samples,
3172 				 evsel, A_ASM);
3173 		nr_options += add_res_sample_opt(browser, &actions[nr_options],
3174 						 &options[nr_options],
3175 				 hist_browser__selected_entry(browser)->res_samples,
3176 				 evsel, A_SOURCE);
3177 		nr_options += add_switch_opt(browser, &actions[nr_options],
3178 					     &options[nr_options]);
3179 skip_scripting:
3180 		nr_options += add_exit_opt(browser, &actions[nr_options],
3181 					   &options[nr_options]);
3182 
3183 		do {
3184 			struct popup_action *act;
3185 
3186 			choice = ui__popup_menu(nr_options, options);
3187 			if (choice == -1 || choice >= nr_options)
3188 				break;
3189 
3190 			act = &actions[choice];
3191 			key = act->fn(browser, act);
3192 		} while (key == 1);
3193 
3194 		if (key == K_SWITCH_INPUT_DATA)
3195 			break;
3196 	}
3197 out_free_stack:
3198 	pstack__delete(browser->pstack);
3199 out:
3200 	hist_browser__delete(browser);
3201 	free_popup_options(options, MAX_OPTIONS);
3202 	return key;
3203 }
3204 
3205 struct evsel_menu {
3206 	struct ui_browser b;
3207 	struct evsel *selection;
3208 	struct annotation_options *annotation_opts;
3209 	bool lost_events, lost_events_warned;
3210 	float min_pcnt;
3211 	struct perf_env *env;
3212 };
3213 
3214 static void perf_evsel_menu__write(struct ui_browser *browser,
3215 				   void *entry, int row)
3216 {
3217 	struct evsel_menu *menu = container_of(browser,
3218 						    struct evsel_menu, b);
3219 	struct evsel *evsel = list_entry(entry, struct evsel, core.node);
3220 	struct hists *hists = evsel__hists(evsel);
3221 	bool current_entry = ui_browser__is_current_entry(browser, row);
3222 	unsigned long nr_events = hists->stats.nr_events[PERF_RECORD_SAMPLE];
3223 	const char *ev_name = perf_evsel__name(evsel);
3224 	char bf[256], unit;
3225 	const char *warn = " ";
3226 	size_t printed;
3227 
3228 	ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
3229 						       HE_COLORSET_NORMAL);
3230 
3231 	if (perf_evsel__is_group_event(evsel)) {
3232 		struct evsel *pos;
3233 
3234 		ev_name = perf_evsel__group_name(evsel);
3235 
3236 		for_each_group_member(pos, evsel) {
3237 			struct hists *pos_hists = evsel__hists(pos);
3238 			nr_events += pos_hists->stats.nr_events[PERF_RECORD_SAMPLE];
3239 		}
3240 	}
3241 
3242 	nr_events = convert_unit(nr_events, &unit);
3243 	printed = scnprintf(bf, sizeof(bf), "%lu%c%s%s", nr_events,
3244 			   unit, unit == ' ' ? "" : " ", ev_name);
3245 	ui_browser__printf(browser, "%s", bf);
3246 
3247 	nr_events = hists->stats.nr_events[PERF_RECORD_LOST];
3248 	if (nr_events != 0) {
3249 		menu->lost_events = true;
3250 		if (!current_entry)
3251 			ui_browser__set_color(browser, HE_COLORSET_TOP);
3252 		nr_events = convert_unit(nr_events, &unit);
3253 		printed += scnprintf(bf, sizeof(bf), ": %ld%c%schunks LOST!",
3254 				     nr_events, unit, unit == ' ' ? "" : " ");
3255 		warn = bf;
3256 	}
3257 
3258 	ui_browser__write_nstring(browser, warn, browser->width - printed);
3259 
3260 	if (current_entry)
3261 		menu->selection = evsel;
3262 }
3263 
3264 static int perf_evsel_menu__run(struct evsel_menu *menu,
3265 				int nr_events, const char *help,
3266 				struct hist_browser_timer *hbt,
3267 				bool warn_lost_event)
3268 {
3269 	struct evlist *evlist = menu->b.priv;
3270 	struct evsel *pos;
3271 	const char *title = "Available samples";
3272 	int delay_secs = hbt ? hbt->refresh : 0;
3273 	int key;
3274 
3275 	if (ui_browser__show(&menu->b, title,
3276 			     "ESC: exit, ENTER|->: Browse histograms") < 0)
3277 		return -1;
3278 
3279 	while (1) {
3280 		key = ui_browser__run(&menu->b, delay_secs);
3281 
3282 		switch (key) {
3283 		case K_TIMER:
3284 			if (hbt)
3285 				hbt->timer(hbt->arg);
3286 
3287 			if (!menu->lost_events_warned &&
3288 			    menu->lost_events &&
3289 			    warn_lost_event) {
3290 				ui_browser__warn_lost_events(&menu->b);
3291 				menu->lost_events_warned = true;
3292 			}
3293 			continue;
3294 		case K_RIGHT:
3295 		case K_ENTER:
3296 			if (!menu->selection)
3297 				continue;
3298 			pos = menu->selection;
3299 browse_hists:
3300 			perf_evlist__set_selected(evlist, pos);
3301 			/*
3302 			 * Give the calling tool a chance to populate the non
3303 			 * default evsel resorted hists tree.
3304 			 */
3305 			if (hbt)
3306 				hbt->timer(hbt->arg);
3307 			key = perf_evsel__hists_browse(pos, nr_events, help,
3308 						       true, hbt,
3309 						       menu->min_pcnt,
3310 						       menu->env,
3311 						       warn_lost_event,
3312 						       menu->annotation_opts);
3313 			ui_browser__show_title(&menu->b, title);
3314 			switch (key) {
3315 			case K_TAB:
3316 				if (pos->core.node.next == &evlist->core.entries)
3317 					pos = perf_evlist__first(evlist);
3318 				else
3319 					pos = perf_evsel__next(pos);
3320 				goto browse_hists;
3321 			case K_UNTAB:
3322 				if (pos->core.node.prev == &evlist->core.entries)
3323 					pos = perf_evlist__last(evlist);
3324 				else
3325 					pos = perf_evsel__prev(pos);
3326 				goto browse_hists;
3327 			case K_SWITCH_INPUT_DATA:
3328 			case 'q':
3329 			case CTRL('c'):
3330 				goto out;
3331 			case K_ESC:
3332 			default:
3333 				continue;
3334 			}
3335 		case K_LEFT:
3336 			continue;
3337 		case K_ESC:
3338 			if (!ui_browser__dialog_yesno(&menu->b,
3339 					       "Do you really want to exit?"))
3340 				continue;
3341 			/* Fall thru */
3342 		case 'q':
3343 		case CTRL('c'):
3344 			goto out;
3345 		default:
3346 			continue;
3347 		}
3348 	}
3349 
3350 out:
3351 	ui_browser__hide(&menu->b);
3352 	return key;
3353 }
3354 
3355 static bool filter_group_entries(struct ui_browser *browser __maybe_unused,
3356 				 void *entry)
3357 {
3358 	struct evsel *evsel = list_entry(entry, struct evsel, core.node);
3359 
3360 	if (symbol_conf.event_group && !perf_evsel__is_group_leader(evsel))
3361 		return true;
3362 
3363 	return false;
3364 }
3365 
3366 static int __perf_evlist__tui_browse_hists(struct evlist *evlist,
3367 					   int nr_entries, const char *help,
3368 					   struct hist_browser_timer *hbt,
3369 					   float min_pcnt,
3370 					   struct perf_env *env,
3371 					   bool warn_lost_event,
3372 					   struct annotation_options *annotation_opts)
3373 {
3374 	struct evsel *pos;
3375 	struct evsel_menu menu = {
3376 		.b = {
3377 			.entries    = &evlist->core.entries,
3378 			.refresh    = ui_browser__list_head_refresh,
3379 			.seek	    = ui_browser__list_head_seek,
3380 			.write	    = perf_evsel_menu__write,
3381 			.filter	    = filter_group_entries,
3382 			.nr_entries = nr_entries,
3383 			.priv	    = evlist,
3384 		},
3385 		.min_pcnt = min_pcnt,
3386 		.env = env,
3387 		.annotation_opts = annotation_opts,
3388 	};
3389 
3390 	ui_helpline__push("Press ESC to exit");
3391 
3392 	evlist__for_each_entry(evlist, pos) {
3393 		const char *ev_name = perf_evsel__name(pos);
3394 		size_t line_len = strlen(ev_name) + 7;
3395 
3396 		if (menu.b.width < line_len)
3397 			menu.b.width = line_len;
3398 	}
3399 
3400 	return perf_evsel_menu__run(&menu, nr_entries, help,
3401 				    hbt, warn_lost_event);
3402 }
3403 
3404 int perf_evlist__tui_browse_hists(struct evlist *evlist, const char *help,
3405 				  struct hist_browser_timer *hbt,
3406 				  float min_pcnt,
3407 				  struct perf_env *env,
3408 				  bool warn_lost_event,
3409 				  struct annotation_options *annotation_opts)
3410 {
3411 	int nr_entries = evlist->core.nr_entries;
3412 
3413 single_entry:
3414 	if (nr_entries == 1) {
3415 		struct evsel *first = perf_evlist__first(evlist);
3416 
3417 		return perf_evsel__hists_browse(first, nr_entries, help,
3418 						false, hbt, min_pcnt,
3419 						env, warn_lost_event,
3420 						annotation_opts);
3421 	}
3422 
3423 	if (symbol_conf.event_group) {
3424 		struct evsel *pos;
3425 
3426 		nr_entries = 0;
3427 		evlist__for_each_entry(evlist, pos) {
3428 			if (perf_evsel__is_group_leader(pos))
3429 				nr_entries++;
3430 		}
3431 
3432 		if (nr_entries == 1)
3433 			goto single_entry;
3434 	}
3435 
3436 	return __perf_evlist__tui_browse_hists(evlist, nr_entries, help,
3437 					       hbt, min_pcnt, env,
3438 					       warn_lost_event,
3439 					       annotation_opts);
3440 }
3441