xref: /linux/tools/perf/util/callchain.c (revision 4f9786035f9e519db41375818e1d0b5f20da2f10)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2009-2011, Frederic Weisbecker <fweisbec@gmail.com>
4  *
5  * Handle the callchains from the stream in an ad-hoc radix tree and then
6  * sort them in an rbtree.
7  *
8  * Using a radix for code path provides a fast retrieval and factorizes
9  * memory use. Also that lets us use the paths in a hierarchical graph view.
10  *
11  */
12 
13 #include <inttypes.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <stdbool.h>
17 #include <errno.h>
18 #include <math.h>
19 #include <linux/string.h>
20 #include <linux/zalloc.h>
21 
22 #include "asm/bug.h"
23 
24 #include "debug.h"
25 #include "dso.h"
26 #include "event.h"
27 #include "hist.h"
28 #include "sort.h"
29 #include "machine.h"
30 #include "map.h"
31 #include "callchain.h"
32 #include "branch.h"
33 #include "symbol.h"
34 #include "util.h"
35 #include "../perf.h"
36 
37 #define CALLCHAIN_PARAM_DEFAULT			\
38 	.mode		= CHAIN_GRAPH_ABS,	\
39 	.min_percent	= 0.5,			\
40 	.order		= ORDER_CALLEE,		\
41 	.key		= CCKEY_FUNCTION,	\
42 	.value		= CCVAL_PERCENT,	\
43 
44 struct callchain_param callchain_param = {
45 	CALLCHAIN_PARAM_DEFAULT
46 };
47 
48 /*
49  * Are there any events usind DWARF callchains?
50  *
51  * I.e.
52  *
53  * -e cycles/call-graph=dwarf/
54  */
55 bool dwarf_callchain_users;
56 
57 struct callchain_param callchain_param_default = {
58 	CALLCHAIN_PARAM_DEFAULT
59 };
60 
61 /* Used for thread-local struct callchain_cursor. */
62 static pthread_key_t callchain_cursor;
63 
64 int parse_callchain_record_opt(const char *arg, struct callchain_param *param)
65 {
66 	return parse_callchain_record(arg, param);
67 }
68 
69 static int parse_callchain_mode(const char *value)
70 {
71 	if (!strncmp(value, "graph", strlen(value))) {
72 		callchain_param.mode = CHAIN_GRAPH_ABS;
73 		return 0;
74 	}
75 	if (!strncmp(value, "flat", strlen(value))) {
76 		callchain_param.mode = CHAIN_FLAT;
77 		return 0;
78 	}
79 	if (!strncmp(value, "fractal", strlen(value))) {
80 		callchain_param.mode = CHAIN_GRAPH_REL;
81 		return 0;
82 	}
83 	if (!strncmp(value, "folded", strlen(value))) {
84 		callchain_param.mode = CHAIN_FOLDED;
85 		return 0;
86 	}
87 	return -1;
88 }
89 
90 static int parse_callchain_order(const char *value)
91 {
92 	if (!strncmp(value, "caller", strlen(value))) {
93 		callchain_param.order = ORDER_CALLER;
94 		callchain_param.order_set = true;
95 		return 0;
96 	}
97 	if (!strncmp(value, "callee", strlen(value))) {
98 		callchain_param.order = ORDER_CALLEE;
99 		callchain_param.order_set = true;
100 		return 0;
101 	}
102 	return -1;
103 }
104 
105 static int parse_callchain_sort_key(const char *value)
106 {
107 	if (!strncmp(value, "function", strlen(value))) {
108 		callchain_param.key = CCKEY_FUNCTION;
109 		return 0;
110 	}
111 	if (!strncmp(value, "address", strlen(value))) {
112 		callchain_param.key = CCKEY_ADDRESS;
113 		return 0;
114 	}
115 	if (!strncmp(value, "srcline", strlen(value))) {
116 		callchain_param.key = CCKEY_SRCLINE;
117 		return 0;
118 	}
119 	if (!strncmp(value, "branch", strlen(value))) {
120 		callchain_param.branch_callstack = 1;
121 		return 0;
122 	}
123 	return -1;
124 }
125 
126 static int parse_callchain_value(const char *value)
127 {
128 	if (!strncmp(value, "percent", strlen(value))) {
129 		callchain_param.value = CCVAL_PERCENT;
130 		return 0;
131 	}
132 	if (!strncmp(value, "period", strlen(value))) {
133 		callchain_param.value = CCVAL_PERIOD;
134 		return 0;
135 	}
136 	if (!strncmp(value, "count", strlen(value))) {
137 		callchain_param.value = CCVAL_COUNT;
138 		return 0;
139 	}
140 	return -1;
141 }
142 
143 static int get_stack_size(const char *str, unsigned long *_size)
144 {
145 	char *endptr;
146 	unsigned long size;
147 	unsigned long max_size = round_down(USHRT_MAX, sizeof(u64));
148 
149 	size = strtoul(str, &endptr, 0);
150 
151 	do {
152 		if (*endptr)
153 			break;
154 
155 		size = round_up(size, sizeof(u64));
156 		if (!size || size > max_size)
157 			break;
158 
159 		*_size = size;
160 		return 0;
161 
162 	} while (0);
163 
164 	pr_err("callchain: Incorrect stack dump size (max %ld): %s\n",
165 	       max_size, str);
166 	return -1;
167 }
168 
169 static int
170 __parse_callchain_report_opt(const char *arg, bool allow_record_opt)
171 {
172 	char *tok;
173 	char *endptr, *saveptr = NULL;
174 	bool minpcnt_set = false;
175 	bool record_opt_set = false;
176 	bool try_stack_size = false;
177 
178 	callchain_param.enabled = true;
179 	symbol_conf.use_callchain = true;
180 
181 	if (!arg)
182 		return 0;
183 
184 	while ((tok = strtok_r((char *)arg, ",", &saveptr)) != NULL) {
185 		if (!strncmp(tok, "none", strlen(tok))) {
186 			callchain_param.mode = CHAIN_NONE;
187 			callchain_param.enabled = false;
188 			symbol_conf.use_callchain = false;
189 			return 0;
190 		}
191 
192 		if (!parse_callchain_mode(tok) ||
193 		    !parse_callchain_order(tok) ||
194 		    !parse_callchain_sort_key(tok) ||
195 		    !parse_callchain_value(tok)) {
196 			/* parsing ok - move on to the next */
197 			try_stack_size = false;
198 			goto next;
199 		} else if (allow_record_opt && !record_opt_set) {
200 			if (parse_callchain_record(tok, &callchain_param))
201 				goto try_numbers;
202 
203 			/* assume that number followed by 'dwarf' is stack size */
204 			if (callchain_param.record_mode == CALLCHAIN_DWARF)
205 				try_stack_size = true;
206 
207 			record_opt_set = true;
208 			goto next;
209 		}
210 
211 try_numbers:
212 		if (try_stack_size) {
213 			unsigned long size = 0;
214 
215 			if (get_stack_size(tok, &size) < 0)
216 				return -1;
217 			callchain_param.dump_size = size;
218 			try_stack_size = false;
219 		} else if (!minpcnt_set) {
220 			/* try to get the min percent */
221 			callchain_param.min_percent = strtod(tok, &endptr);
222 			if (tok == endptr)
223 				return -1;
224 			minpcnt_set = true;
225 		} else {
226 			/* try print limit at last */
227 			callchain_param.print_limit = strtoul(tok, &endptr, 0);
228 			if (tok == endptr)
229 				return -1;
230 		}
231 next:
232 		arg = NULL;
233 	}
234 
235 	if (callchain_register_param(&callchain_param) < 0) {
236 		pr_err("Can't register callchain params\n");
237 		return -1;
238 	}
239 	return 0;
240 }
241 
242 int parse_callchain_report_opt(const char *arg)
243 {
244 	return __parse_callchain_report_opt(arg, false);
245 }
246 
247 int parse_callchain_top_opt(const char *arg)
248 {
249 	return __parse_callchain_report_opt(arg, true);
250 }
251 
252 int parse_callchain_record(const char *arg, struct callchain_param *param)
253 {
254 	char *tok, *name, *saveptr = NULL;
255 	char *buf;
256 	int ret = -1;
257 
258 	/* We need buffer that we know we can write to. */
259 	buf = malloc(strlen(arg) + 1);
260 	if (!buf)
261 		return -ENOMEM;
262 
263 	strcpy(buf, arg);
264 
265 	tok = strtok_r((char *)buf, ",", &saveptr);
266 	name = tok ? : (char *)buf;
267 
268 	do {
269 		/* Framepointer style */
270 		if (!strncmp(name, "fp", sizeof("fp"))) {
271 			ret = 0;
272 			param->record_mode = CALLCHAIN_FP;
273 
274 			tok = strtok_r(NULL, ",", &saveptr);
275 			if (tok) {
276 				unsigned long size;
277 
278 				size = strtoul(tok, &name, 0);
279 				if (size < (unsigned) sysctl__max_stack())
280 					param->max_stack = size;
281 			}
282 			break;
283 
284 		/* Dwarf style */
285 		} else if (!strncmp(name, "dwarf", sizeof("dwarf"))) {
286 			const unsigned long default_stack_dump_size = 8192;
287 
288 			ret = 0;
289 			param->record_mode = CALLCHAIN_DWARF;
290 			param->dump_size = default_stack_dump_size;
291 			dwarf_callchain_users = true;
292 
293 			tok = strtok_r(NULL, ",", &saveptr);
294 			if (tok) {
295 				unsigned long size = 0;
296 
297 				ret = get_stack_size(tok, &size);
298 				param->dump_size = size;
299 			}
300 		} else if (!strncmp(name, "lbr", sizeof("lbr"))) {
301 			if (!strtok_r(NULL, ",", &saveptr)) {
302 				param->record_mode = CALLCHAIN_LBR;
303 				ret = 0;
304 			} else
305 				pr_err("callchain: No more arguments "
306 					"needed for --call-graph lbr\n");
307 			break;
308 		} else {
309 			pr_err("callchain: Unknown --call-graph option "
310 			       "value: %s\n", arg);
311 			break;
312 		}
313 
314 	} while (0);
315 
316 	free(buf);
317 	return ret;
318 }
319 
320 int perf_callchain_config(const char *var, const char *value)
321 {
322 	char *endptr;
323 
324 	if (!strstarts(var, "call-graph."))
325 		return 0;
326 	var += sizeof("call-graph.") - 1;
327 
328 	if (!strcmp(var, "record-mode"))
329 		return parse_callchain_record_opt(value, &callchain_param);
330 	if (!strcmp(var, "dump-size")) {
331 		unsigned long size = 0;
332 		int ret;
333 
334 		ret = get_stack_size(value, &size);
335 		callchain_param.dump_size = size;
336 
337 		return ret;
338 	}
339 	if (!strcmp(var, "print-type")){
340 		int ret;
341 		ret = parse_callchain_mode(value);
342 		if (ret == -1)
343 			pr_err("Invalid callchain mode: %s\n", value);
344 		return ret;
345 	}
346 	if (!strcmp(var, "order")){
347 		int ret;
348 		ret = parse_callchain_order(value);
349 		if (ret == -1)
350 			pr_err("Invalid callchain order: %s\n", value);
351 		return ret;
352 	}
353 	if (!strcmp(var, "sort-key")){
354 		int ret;
355 		ret = parse_callchain_sort_key(value);
356 		if (ret == -1)
357 			pr_err("Invalid callchain sort key: %s\n", value);
358 		return ret;
359 	}
360 	if (!strcmp(var, "threshold")) {
361 		callchain_param.min_percent = strtod(value, &endptr);
362 		if (value == endptr) {
363 			pr_err("Invalid callchain threshold: %s\n", value);
364 			return -1;
365 		}
366 	}
367 	if (!strcmp(var, "print-limit")) {
368 		callchain_param.print_limit = strtod(value, &endptr);
369 		if (value == endptr) {
370 			pr_err("Invalid callchain print limit: %s\n", value);
371 			return -1;
372 		}
373 	}
374 
375 	return 0;
376 }
377 
378 static void
379 rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
380 		    enum chain_mode mode)
381 {
382 	struct rb_node **p = &root->rb_node;
383 	struct rb_node *parent = NULL;
384 	struct callchain_node *rnode;
385 	u64 chain_cumul = callchain_cumul_hits(chain);
386 
387 	while (*p) {
388 		u64 rnode_cumul;
389 
390 		parent = *p;
391 		rnode = rb_entry(parent, struct callchain_node, rb_node);
392 		rnode_cumul = callchain_cumul_hits(rnode);
393 
394 		switch (mode) {
395 		case CHAIN_FLAT:
396 		case CHAIN_FOLDED:
397 			if (rnode->hit < chain->hit)
398 				p = &(*p)->rb_left;
399 			else
400 				p = &(*p)->rb_right;
401 			break;
402 		case CHAIN_GRAPH_ABS: /* Falldown */
403 		case CHAIN_GRAPH_REL:
404 			if (rnode_cumul < chain_cumul)
405 				p = &(*p)->rb_left;
406 			else
407 				p = &(*p)->rb_right;
408 			break;
409 		case CHAIN_NONE:
410 		default:
411 			break;
412 		}
413 	}
414 
415 	rb_link_node(&chain->rb_node, parent, p);
416 	rb_insert_color(&chain->rb_node, root);
417 }
418 
419 static void
420 __sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
421 		  u64 min_hit)
422 {
423 	struct rb_node *n;
424 	struct callchain_node *child;
425 
426 	n = rb_first(&node->rb_root_in);
427 	while (n) {
428 		child = rb_entry(n, struct callchain_node, rb_node_in);
429 		n = rb_next(n);
430 
431 		__sort_chain_flat(rb_root, child, min_hit);
432 	}
433 
434 	if (node->hit && node->hit >= min_hit)
435 		rb_insert_callchain(rb_root, node, CHAIN_FLAT);
436 }
437 
438 /*
439  * Once we get every callchains from the stream, we can now
440  * sort them by hit
441  */
442 static void
443 sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root,
444 		u64 min_hit, struct callchain_param *param __maybe_unused)
445 {
446 	*rb_root = RB_ROOT;
447 	__sort_chain_flat(rb_root, &root->node, min_hit);
448 }
449 
450 static void __sort_chain_graph_abs(struct callchain_node *node,
451 				   u64 min_hit)
452 {
453 	struct rb_node *n;
454 	struct callchain_node *child;
455 
456 	node->rb_root = RB_ROOT;
457 	n = rb_first(&node->rb_root_in);
458 
459 	while (n) {
460 		child = rb_entry(n, struct callchain_node, rb_node_in);
461 		n = rb_next(n);
462 
463 		__sort_chain_graph_abs(child, min_hit);
464 		if (callchain_cumul_hits(child) >= min_hit)
465 			rb_insert_callchain(&node->rb_root, child,
466 					    CHAIN_GRAPH_ABS);
467 	}
468 }
469 
470 static void
471 sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root,
472 		     u64 min_hit, struct callchain_param *param __maybe_unused)
473 {
474 	__sort_chain_graph_abs(&chain_root->node, min_hit);
475 	rb_root->rb_node = chain_root->node.rb_root.rb_node;
476 }
477 
478 static void __sort_chain_graph_rel(struct callchain_node *node,
479 				   double min_percent)
480 {
481 	struct rb_node *n;
482 	struct callchain_node *child;
483 	u64 min_hit;
484 
485 	node->rb_root = RB_ROOT;
486 	min_hit = ceil(node->children_hit * min_percent);
487 
488 	n = rb_first(&node->rb_root_in);
489 	while (n) {
490 		child = rb_entry(n, struct callchain_node, rb_node_in);
491 		n = rb_next(n);
492 
493 		__sort_chain_graph_rel(child, min_percent);
494 		if (callchain_cumul_hits(child) >= min_hit)
495 			rb_insert_callchain(&node->rb_root, child,
496 					    CHAIN_GRAPH_REL);
497 	}
498 }
499 
500 static void
501 sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root,
502 		     u64 min_hit __maybe_unused, struct callchain_param *param)
503 {
504 	__sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0);
505 	rb_root->rb_node = chain_root->node.rb_root.rb_node;
506 }
507 
508 int callchain_register_param(struct callchain_param *param)
509 {
510 	switch (param->mode) {
511 	case CHAIN_GRAPH_ABS:
512 		param->sort = sort_chain_graph_abs;
513 		break;
514 	case CHAIN_GRAPH_REL:
515 		param->sort = sort_chain_graph_rel;
516 		break;
517 	case CHAIN_FLAT:
518 	case CHAIN_FOLDED:
519 		param->sort = sort_chain_flat;
520 		break;
521 	case CHAIN_NONE:
522 	default:
523 		return -1;
524 	}
525 	return 0;
526 }
527 
528 /*
529  * Create a child for a parent. If inherit_children, then the new child
530  * will become the new parent of it's parent children
531  */
532 static struct callchain_node *
533 create_child(struct callchain_node *parent, bool inherit_children)
534 {
535 	struct callchain_node *new;
536 
537 	new = zalloc(sizeof(*new));
538 	if (!new) {
539 		perror("not enough memory to create child for code path tree");
540 		return NULL;
541 	}
542 	new->parent = parent;
543 	INIT_LIST_HEAD(&new->val);
544 	INIT_LIST_HEAD(&new->parent_val);
545 
546 	if (inherit_children) {
547 		struct rb_node *n;
548 		struct callchain_node *child;
549 
550 		new->rb_root_in = parent->rb_root_in;
551 		parent->rb_root_in = RB_ROOT;
552 
553 		n = rb_first(&new->rb_root_in);
554 		while (n) {
555 			child = rb_entry(n, struct callchain_node, rb_node_in);
556 			child->parent = new;
557 			n = rb_next(n);
558 		}
559 
560 		/* make it the first child */
561 		rb_link_node(&new->rb_node_in, NULL, &parent->rb_root_in.rb_node);
562 		rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
563 	}
564 
565 	return new;
566 }
567 
568 
569 /*
570  * Fill the node with callchain values
571  */
572 static int
573 fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
574 {
575 	struct callchain_cursor_node *cursor_node;
576 
577 	node->val_nr = cursor->nr - cursor->pos;
578 	if (!node->val_nr)
579 		pr_warning("Warning: empty node in callchain tree\n");
580 
581 	cursor_node = callchain_cursor_current(cursor);
582 
583 	while (cursor_node) {
584 		struct callchain_list *call;
585 
586 		call = zalloc(sizeof(*call));
587 		if (!call) {
588 			perror("not enough memory for the code path tree");
589 			return -ENOMEM;
590 		}
591 		call->ip = cursor_node->ip;
592 		map_symbol__copy(&call->ms, &cursor_node->ms);
593 		call->srcline = cursor_node->srcline;
594 
595 		if (cursor_node->branch) {
596 			call->branch_count = 1;
597 
598 			if (cursor_node->branch_from) {
599 				/*
600 				 * branch_from is set with value somewhere else
601 				 * to imply it's "to" of a branch.
602 				 */
603 				if (!call->brtype_stat) {
604 					call->brtype_stat = zalloc(sizeof(*call->brtype_stat));
605 					if (!call->brtype_stat) {
606 						perror("not enough memory for the code path branch statistics");
607 						zfree(&call->brtype_stat);
608 						return -ENOMEM;
609 					}
610 				}
611 				call->brtype_stat->branch_to = true;
612 
613 				if (cursor_node->branch_flags.predicted)
614 					call->predicted_count = 1;
615 
616 				if (cursor_node->branch_flags.abort)
617 					call->abort_count = 1;
618 
619 				branch_type_count(call->brtype_stat,
620 						  &cursor_node->branch_flags,
621 						  cursor_node->branch_from,
622 						  cursor_node->ip);
623 			} else {
624 				/*
625 				 * It's "from" of a branch
626 				 */
627 				if (call->brtype_stat && call->brtype_stat->branch_to)
628 					call->brtype_stat->branch_to = false;
629 				call->cycles_count =
630 					cursor_node->branch_flags.cycles;
631 				call->iter_count = cursor_node->nr_loop_iter;
632 				call->iter_cycles = cursor_node->iter_cycles;
633 			}
634 		}
635 
636 		list_add_tail(&call->list, &node->val);
637 
638 		callchain_cursor_advance(cursor);
639 		cursor_node = callchain_cursor_current(cursor);
640 	}
641 	return 0;
642 }
643 
644 static struct callchain_node *
645 add_child(struct callchain_node *parent,
646 	  struct callchain_cursor *cursor,
647 	  u64 period)
648 {
649 	struct callchain_node *new;
650 
651 	new = create_child(parent, false);
652 	if (new == NULL)
653 		return NULL;
654 
655 	if (fill_node(new, cursor) < 0) {
656 		struct callchain_list *call, *tmp;
657 
658 		list_for_each_entry_safe(call, tmp, &new->val, list) {
659 			list_del_init(&call->list);
660 			map_symbol__exit(&call->ms);
661 			zfree(&call->brtype_stat);
662 			free(call);
663 		}
664 		free(new);
665 		return NULL;
666 	}
667 
668 	new->children_hit = 0;
669 	new->hit = period;
670 	new->children_count = 0;
671 	new->count = 1;
672 	return new;
673 }
674 
675 enum match_result {
676 	MATCH_ERROR  = -1,
677 	MATCH_EQ,
678 	MATCH_LT,
679 	MATCH_GT,
680 };
681 
682 static enum match_result match_chain_strings(const char *left,
683 					     const char *right)
684 {
685 	enum match_result ret = MATCH_EQ;
686 	int cmp;
687 
688 	if (left && right)
689 		cmp = strcmp(left, right);
690 	else if (!left && right)
691 		cmp = 1;
692 	else if (left && !right)
693 		cmp = -1;
694 	else
695 		return MATCH_ERROR;
696 
697 	if (cmp != 0)
698 		ret = cmp < 0 ? MATCH_LT : MATCH_GT;
699 
700 	return ret;
701 }
702 
703 /*
704  * We need to always use relative addresses because we're aggregating
705  * callchains from multiple threads, i.e. different address spaces, so
706  * comparing absolute addresses make no sense as a symbol in a DSO may end up
707  * in a different address when used in a different binary or even the same
708  * binary but with some sort of address randomization technique, thus we need
709  * to compare just relative addresses. -acme
710  */
711 static enum match_result match_chain_dso_addresses(struct map *left_map, u64 left_ip,
712 						   struct map *right_map, u64 right_ip)
713 {
714 	struct dso *left_dso = left_map ? map__dso(left_map) : NULL;
715 	struct dso *right_dso = right_map ? map__dso(right_map) : NULL;
716 
717 	if (left_dso != right_dso)
718 		return left_dso < right_dso ? MATCH_LT : MATCH_GT;
719 
720 	if (left_ip != right_ip)
721  		return left_ip < right_ip ? MATCH_LT : MATCH_GT;
722 
723 	return MATCH_EQ;
724 }
725 
726 static enum match_result match_chain(struct callchain_cursor_node *node,
727 				     struct callchain_list *cnode)
728 {
729 	enum match_result match = MATCH_ERROR;
730 
731 	switch (callchain_param.key) {
732 	case CCKEY_SRCLINE:
733 		match = match_chain_strings(cnode->srcline, node->srcline);
734 		if (match != MATCH_ERROR)
735 			break;
736 		/* otherwise fall-back to symbol-based comparison below */
737 		fallthrough;
738 	case CCKEY_FUNCTION:
739 		if (node->ms.sym && cnode->ms.sym) {
740 			/*
741 			 * Compare inlined frames based on their symbol name
742 			 * because different inlined frames will have the same
743 			 * symbol start. Otherwise do a faster comparison based
744 			 * on the symbol start address.
745 			 */
746 			if (cnode->ms.sym->inlined || node->ms.sym->inlined) {
747 				match = match_chain_strings(cnode->ms.sym->name,
748 							    node->ms.sym->name);
749 				if (match != MATCH_ERROR)
750 					break;
751 			} else {
752 				match = match_chain_dso_addresses(cnode->ms.map, cnode->ms.sym->start,
753 								  node->ms.map, node->ms.sym->start);
754 				break;
755 			}
756 		}
757 		/* otherwise fall-back to IP-based comparison below */
758 		fallthrough;
759 	case CCKEY_ADDRESS:
760 	default:
761 		match = match_chain_dso_addresses(cnode->ms.map, cnode->ip, node->ms.map, node->ip);
762 		break;
763 	}
764 
765 	if (match == MATCH_EQ && node->branch) {
766 		cnode->branch_count++;
767 
768 		if (node->branch_from) {
769 			/*
770 			 * It's "to" of a branch
771 			 */
772 			if (!cnode->brtype_stat) {
773 				cnode->brtype_stat = zalloc(sizeof(*cnode->brtype_stat));
774 				if (!cnode->brtype_stat) {
775 					perror("not enough memory for the code path branch statistics");
776 					return MATCH_ERROR;
777 				}
778 			}
779 			cnode->brtype_stat->branch_to = true;
780 
781 			if (node->branch_flags.predicted)
782 				cnode->predicted_count++;
783 
784 			if (node->branch_flags.abort)
785 				cnode->abort_count++;
786 
787 			branch_type_count(cnode->brtype_stat,
788 					  &node->branch_flags,
789 					  node->branch_from,
790 					  node->ip);
791 		} else {
792 			/*
793 			 * It's "from" of a branch
794 			 */
795 			if (cnode->brtype_stat && cnode->brtype_stat->branch_to)
796 				cnode->brtype_stat->branch_to = false;
797 			cnode->cycles_count += node->branch_flags.cycles;
798 			cnode->iter_count += node->nr_loop_iter;
799 			cnode->iter_cycles += node->iter_cycles;
800 			cnode->from_count++;
801 		}
802 	}
803 
804 	return match;
805 }
806 
807 /*
808  * Split the parent in two parts (a new child is created) and
809  * give a part of its callchain to the created child.
810  * Then create another child to host the given callchain of new branch
811  */
812 static int
813 split_add_child(struct callchain_node *parent,
814 		struct callchain_cursor *cursor,
815 		struct callchain_list *to_split,
816 		u64 idx_parents, u64 idx_local, u64 period)
817 {
818 	struct callchain_node *new;
819 	struct list_head *old_tail;
820 	unsigned int idx_total = idx_parents + idx_local;
821 
822 	/* split */
823 	new = create_child(parent, true);
824 	if (new == NULL)
825 		return -1;
826 
827 	/* split the callchain and move a part to the new child */
828 	old_tail = parent->val.prev;
829 	list_del_range(&to_split->list, old_tail);
830 	new->val.next = &to_split->list;
831 	new->val.prev = old_tail;
832 	to_split->list.prev = &new->val;
833 	old_tail->next = &new->val;
834 
835 	/* split the hits */
836 	new->hit = parent->hit;
837 	new->children_hit = parent->children_hit;
838 	parent->children_hit = callchain_cumul_hits(new);
839 	new->val_nr = parent->val_nr - idx_local;
840 	parent->val_nr = idx_local;
841 	new->count = parent->count;
842 	new->children_count = parent->children_count;
843 	parent->children_count = callchain_cumul_counts(new);
844 
845 	/* create a new child for the new branch if any */
846 	if (idx_total < cursor->nr) {
847 		struct callchain_node *first;
848 		struct callchain_list *cnode;
849 		struct callchain_cursor_node *node;
850 		struct rb_node *p, **pp;
851 
852 		parent->hit = 0;
853 		parent->children_hit += period;
854 		parent->count = 0;
855 		parent->children_count += 1;
856 
857 		node = callchain_cursor_current(cursor);
858 		new = add_child(parent, cursor, period);
859 		if (new == NULL)
860 			return -1;
861 
862 		/*
863 		 * This is second child since we moved parent's children
864 		 * to new (first) child above.
865 		 */
866 		p = parent->rb_root_in.rb_node;
867 		first = rb_entry(p, struct callchain_node, rb_node_in);
868 		cnode = list_first_entry(&first->val, struct callchain_list,
869 					 list);
870 
871 		if (match_chain(node, cnode) == MATCH_LT)
872 			pp = &p->rb_left;
873 		else
874 			pp = &p->rb_right;
875 
876 		rb_link_node(&new->rb_node_in, p, pp);
877 		rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
878 	} else {
879 		parent->hit = period;
880 		parent->count = 1;
881 	}
882 	return 0;
883 }
884 
885 static enum match_result
886 append_chain(struct callchain_node *root,
887 	     struct callchain_cursor *cursor,
888 	     u64 period);
889 
890 static int
891 append_chain_children(struct callchain_node *root,
892 		      struct callchain_cursor *cursor,
893 		      u64 period)
894 {
895 	struct callchain_node *rnode;
896 	struct callchain_cursor_node *node;
897 	struct rb_node **p = &root->rb_root_in.rb_node;
898 	struct rb_node *parent = NULL;
899 
900 	node = callchain_cursor_current(cursor);
901 	if (!node)
902 		return -1;
903 
904 	/* lookup in children */
905 	while (*p) {
906 		enum match_result ret;
907 
908 		parent = *p;
909 		rnode = rb_entry(parent, struct callchain_node, rb_node_in);
910 
911 		/* If at least first entry matches, rely to children */
912 		ret = append_chain(rnode, cursor, period);
913 		if (ret == MATCH_EQ)
914 			goto inc_children_hit;
915 		if (ret == MATCH_ERROR)
916 			return -1;
917 
918 		if (ret == MATCH_LT)
919 			p = &parent->rb_left;
920 		else
921 			p = &parent->rb_right;
922 	}
923 	/* nothing in children, add to the current node */
924 	rnode = add_child(root, cursor, period);
925 	if (rnode == NULL)
926 		return -1;
927 
928 	rb_link_node(&rnode->rb_node_in, parent, p);
929 	rb_insert_color(&rnode->rb_node_in, &root->rb_root_in);
930 
931 inc_children_hit:
932 	root->children_hit += period;
933 	root->children_count++;
934 	return 0;
935 }
936 
937 static enum match_result
938 append_chain(struct callchain_node *root,
939 	     struct callchain_cursor *cursor,
940 	     u64 period)
941 {
942 	struct callchain_list *cnode;
943 	u64 start = cursor->pos;
944 	bool found = false;
945 	u64 matches;
946 	enum match_result cmp = MATCH_ERROR;
947 
948 	/*
949 	 * Lookup in the current node
950 	 * If we have a symbol, then compare the start to match
951 	 * anywhere inside a function, unless function
952 	 * mode is disabled.
953 	 */
954 	list_for_each_entry(cnode, &root->val, list) {
955 		struct callchain_cursor_node *node;
956 
957 		node = callchain_cursor_current(cursor);
958 		if (!node)
959 			break;
960 
961 		cmp = match_chain(node, cnode);
962 		if (cmp != MATCH_EQ)
963 			break;
964 
965 		found = true;
966 
967 		callchain_cursor_advance(cursor);
968 	}
969 
970 	/* matches not, relay no the parent */
971 	if (!found) {
972 		WARN_ONCE(cmp == MATCH_ERROR, "Chain comparison error\n");
973 		return cmp;
974 	}
975 
976 	matches = cursor->pos - start;
977 
978 	/* we match only a part of the node. Split it and add the new chain */
979 	if (matches < root->val_nr) {
980 		if (split_add_child(root, cursor, cnode, start, matches,
981 				    period) < 0)
982 			return MATCH_ERROR;
983 
984 		return MATCH_EQ;
985 	}
986 
987 	/* we match 100% of the path, increment the hit */
988 	if (matches == root->val_nr && cursor->pos == cursor->nr) {
989 		root->hit += period;
990 		root->count++;
991 		return MATCH_EQ;
992 	}
993 
994 	/* We match the node and still have a part remaining */
995 	if (append_chain_children(root, cursor, period) < 0)
996 		return MATCH_ERROR;
997 
998 	return MATCH_EQ;
999 }
1000 
1001 int callchain_append(struct callchain_root *root,
1002 		     struct callchain_cursor *cursor,
1003 		     u64 period)
1004 {
1005 	if (cursor == NULL)
1006 		return -1;
1007 
1008 	if (!cursor->nr)
1009 		return 0;
1010 
1011 	callchain_cursor_commit(cursor);
1012 
1013 	if (append_chain_children(&root->node, cursor, period) < 0)
1014 		return -1;
1015 
1016 	if (cursor->nr > root->max_depth)
1017 		root->max_depth = cursor->nr;
1018 
1019 	return 0;
1020 }
1021 
1022 static int
1023 merge_chain_branch(struct callchain_cursor *cursor,
1024 		   struct callchain_node *dst, struct callchain_node *src)
1025 {
1026 	struct callchain_cursor_node **old_last = cursor->last;
1027 	struct callchain_node *child;
1028 	struct callchain_list *list, *next_list;
1029 	struct rb_node *n;
1030 	int old_pos = cursor->nr;
1031 	int err = 0;
1032 
1033 	list_for_each_entry_safe(list, next_list, &src->val, list) {
1034 		struct map_symbol ms = {
1035 			.maps = maps__get(list->ms.maps),
1036 			.map = map__get(list->ms.map),
1037 		};
1038 		callchain_cursor_append(cursor, list->ip, &ms, false, NULL, 0, 0, 0, list->srcline);
1039 		list_del_init(&list->list);
1040 		map_symbol__exit(&ms);
1041 		map_symbol__exit(&list->ms);
1042 		zfree(&list->brtype_stat);
1043 		free(list);
1044 	}
1045 
1046 	if (src->hit) {
1047 		callchain_cursor_commit(cursor);
1048 		if (append_chain_children(dst, cursor, src->hit) < 0)
1049 			return -1;
1050 	}
1051 
1052 	n = rb_first(&src->rb_root_in);
1053 	while (n) {
1054 		child = container_of(n, struct callchain_node, rb_node_in);
1055 		n = rb_next(n);
1056 		rb_erase(&child->rb_node_in, &src->rb_root_in);
1057 
1058 		err = merge_chain_branch(cursor, dst, child);
1059 		if (err)
1060 			break;
1061 
1062 		free(child);
1063 	}
1064 
1065 	cursor->nr = old_pos;
1066 	cursor->last = old_last;
1067 
1068 	return err;
1069 }
1070 
1071 int callchain_merge(struct callchain_cursor *cursor,
1072 		    struct callchain_root *dst, struct callchain_root *src)
1073 {
1074 	return merge_chain_branch(cursor, &dst->node, &src->node);
1075 }
1076 
1077 int callchain_cursor_append(struct callchain_cursor *cursor,
1078 			    u64 ip, struct map_symbol *ms,
1079 			    bool branch, struct branch_flags *flags,
1080 			    int nr_loop_iter, u64 iter_cycles, u64 branch_from,
1081 			    const char *srcline)
1082 {
1083 	struct callchain_cursor_node *node = *cursor->last;
1084 
1085 	if (!node) {
1086 		node = calloc(1, sizeof(*node));
1087 		if (!node)
1088 			return -ENOMEM;
1089 
1090 		*cursor->last = node;
1091 	}
1092 
1093 	node->ip = ip;
1094 	map_symbol__exit(&node->ms);
1095 	map_symbol__copy(&node->ms, ms);
1096 	node->branch = branch;
1097 	node->nr_loop_iter = nr_loop_iter;
1098 	node->iter_cycles = iter_cycles;
1099 	node->srcline = srcline;
1100 
1101 	if (flags)
1102 		memcpy(&node->branch_flags, flags,
1103 			sizeof(struct branch_flags));
1104 
1105 	node->branch_from = branch_from;
1106 	cursor->nr++;
1107 
1108 	cursor->last = &node->next;
1109 
1110 	return 0;
1111 }
1112 
1113 int sample__resolve_callchain(struct perf_sample *sample,
1114 			      struct callchain_cursor *cursor, struct symbol **parent,
1115 			      struct evsel *evsel, struct addr_location *al,
1116 			      int max_stack)
1117 {
1118 	if (sample->callchain == NULL && !symbol_conf.show_branchflag_count)
1119 		return 0;
1120 
1121 	if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain ||
1122 	    perf_hpp_list.parent || symbol_conf.show_branchflag_count) {
1123 		return thread__resolve_callchain(al->thread, cursor, evsel, sample,
1124 						 parent, al, max_stack);
1125 	}
1126 	return 0;
1127 }
1128 
1129 int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample)
1130 {
1131 	if ((!symbol_conf.use_callchain || sample->callchain == NULL) &&
1132 		!symbol_conf.show_branchflag_count)
1133 		return 0;
1134 	return callchain_append(he->callchain, get_tls_callchain_cursor(), sample->period);
1135 }
1136 
1137 int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
1138 			bool hide_unresolved)
1139 {
1140 	struct machine *machine = node->ms.maps ? maps__machine(node->ms.maps) : NULL;
1141 
1142 	maps__put(al->maps);
1143 	al->maps = maps__get(node->ms.maps);
1144 	map__put(al->map);
1145 	al->map = map__get(node->ms.map);
1146 	al->sym = node->ms.sym;
1147 	al->srcline = node->srcline;
1148 	al->addr = node->ip;
1149 
1150 	if (al->sym == NULL) {
1151 		if (hide_unresolved)
1152 			return 0;
1153 		if (al->map == NULL)
1154 			goto out;
1155 	}
1156 	if (maps__equal(al->maps, machine__kernel_maps(machine))) {
1157 		if (machine__is_host(machine)) {
1158 			al->cpumode = PERF_RECORD_MISC_KERNEL;
1159 			al->level = 'k';
1160 		} else {
1161 			al->cpumode = PERF_RECORD_MISC_GUEST_KERNEL;
1162 			al->level = 'g';
1163 		}
1164 	} else {
1165 		if (machine__is_host(machine)) {
1166 			al->cpumode = PERF_RECORD_MISC_USER;
1167 			al->level = '.';
1168 		} else if (perf_guest) {
1169 			al->cpumode = PERF_RECORD_MISC_GUEST_USER;
1170 			al->level = 'u';
1171 		} else {
1172 			al->cpumode = PERF_RECORD_MISC_HYPERVISOR;
1173 			al->level = 'H';
1174 		}
1175 	}
1176 
1177 out:
1178 	return 1;
1179 }
1180 
1181 char *callchain_list__sym_name(struct callchain_list *cl,
1182 			       char *bf, size_t bfsize, bool show_dso)
1183 {
1184 	bool show_addr = callchain_param.key == CCKEY_ADDRESS;
1185 	bool show_srcline = show_addr || callchain_param.key == CCKEY_SRCLINE;
1186 	int printed;
1187 
1188 	if (cl->ms.sym) {
1189 		const char *inlined = cl->ms.sym->inlined ? " (inlined)" : "";
1190 
1191 		if (show_srcline && cl->srcline)
1192 			printed = scnprintf(bf, bfsize, "%s %s%s",
1193 					    cl->ms.sym->name, cl->srcline,
1194 					    inlined);
1195 		else
1196 			printed = scnprintf(bf, bfsize, "%s%s",
1197 					    cl->ms.sym->name, inlined);
1198 	} else
1199 		printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);
1200 
1201 	if (show_dso)
1202 		scnprintf(bf + printed, bfsize - printed, " %s",
1203 			  cl->ms.map ?
1204 			  dso__short_name(map__dso(cl->ms.map)) :
1205 			  "unknown");
1206 
1207 	return bf;
1208 }
1209 
1210 char *callchain_node__scnprintf_value(struct callchain_node *node,
1211 				      char *bf, size_t bfsize, u64 total)
1212 {
1213 	double percent = 0.0;
1214 	u64 period = callchain_cumul_hits(node);
1215 	unsigned count = callchain_cumul_counts(node);
1216 
1217 	if (callchain_param.mode == CHAIN_FOLDED) {
1218 		period = node->hit;
1219 		count = node->count;
1220 	}
1221 
1222 	switch (callchain_param.value) {
1223 	case CCVAL_PERIOD:
1224 		scnprintf(bf, bfsize, "%"PRIu64, period);
1225 		break;
1226 	case CCVAL_COUNT:
1227 		scnprintf(bf, bfsize, "%u", count);
1228 		break;
1229 	case CCVAL_PERCENT:
1230 	default:
1231 		if (total)
1232 			percent = period * 100.0 / total;
1233 		scnprintf(bf, bfsize, "%.2f%%", percent);
1234 		break;
1235 	}
1236 	return bf;
1237 }
1238 
1239 int callchain_node__fprintf_value(struct callchain_node *node,
1240 				 FILE *fp, u64 total)
1241 {
1242 	double percent = 0.0;
1243 	u64 period = callchain_cumul_hits(node);
1244 	unsigned count = callchain_cumul_counts(node);
1245 
1246 	if (callchain_param.mode == CHAIN_FOLDED) {
1247 		period = node->hit;
1248 		count = node->count;
1249 	}
1250 
1251 	switch (callchain_param.value) {
1252 	case CCVAL_PERIOD:
1253 		return fprintf(fp, "%"PRIu64, period);
1254 	case CCVAL_COUNT:
1255 		return fprintf(fp, "%u", count);
1256 	case CCVAL_PERCENT:
1257 	default:
1258 		if (total)
1259 			percent = period * 100.0 / total;
1260 		return percent_color_fprintf(fp, "%.2f%%", percent);
1261 	}
1262 	return 0;
1263 }
1264 
1265 static void callchain_counts_value(struct callchain_node *node,
1266 				   u64 *branch_count, u64 *predicted_count,
1267 				   u64 *abort_count, u64 *cycles_count)
1268 {
1269 	struct callchain_list *clist;
1270 
1271 	list_for_each_entry(clist, &node->val, list) {
1272 		if (branch_count)
1273 			*branch_count += clist->branch_count;
1274 
1275 		if (predicted_count)
1276 			*predicted_count += clist->predicted_count;
1277 
1278 		if (abort_count)
1279 			*abort_count += clist->abort_count;
1280 
1281 		if (cycles_count)
1282 			*cycles_count += clist->cycles_count;
1283 	}
1284 }
1285 
1286 static int callchain_node_branch_counts_cumul(struct callchain_node *node,
1287 					      u64 *branch_count,
1288 					      u64 *predicted_count,
1289 					      u64 *abort_count,
1290 					      u64 *cycles_count)
1291 {
1292 	struct callchain_node *child;
1293 	struct rb_node *n;
1294 
1295 	n = rb_first(&node->rb_root_in);
1296 	while (n) {
1297 		child = rb_entry(n, struct callchain_node, rb_node_in);
1298 		n = rb_next(n);
1299 
1300 		callchain_node_branch_counts_cumul(child, branch_count,
1301 						   predicted_count,
1302 						   abort_count,
1303 						   cycles_count);
1304 
1305 		callchain_counts_value(child, branch_count,
1306 				       predicted_count, abort_count,
1307 				       cycles_count);
1308 	}
1309 
1310 	return 0;
1311 }
1312 
1313 int callchain_branch_counts(struct callchain_root *root,
1314 			    u64 *branch_count, u64 *predicted_count,
1315 			    u64 *abort_count, u64 *cycles_count)
1316 {
1317 	if (branch_count)
1318 		*branch_count = 0;
1319 
1320 	if (predicted_count)
1321 		*predicted_count = 0;
1322 
1323 	if (abort_count)
1324 		*abort_count = 0;
1325 
1326 	if (cycles_count)
1327 		*cycles_count = 0;
1328 
1329 	return callchain_node_branch_counts_cumul(&root->node,
1330 						  branch_count,
1331 						  predicted_count,
1332 						  abort_count,
1333 						  cycles_count);
1334 }
1335 
1336 static int count_pri64_printf(int idx, const char *str, u64 value, char *bf, int bfsize)
1337 {
1338 	return scnprintf(bf, bfsize, "%s%s:%" PRId64 "", (idx) ? " " : " (", str, value);
1339 }
1340 
1341 static int count_float_printf(int idx, const char *str, float value,
1342 			      char *bf, int bfsize, float threshold)
1343 {
1344 	if (threshold != 0.0 && value < threshold)
1345 		return 0;
1346 
1347 	return scnprintf(bf, bfsize, "%s%s:%.1f%%", (idx) ? " " : " (", str, value);
1348 }
1349 
1350 static int branch_to_str(char *bf, int bfsize,
1351 			 u64 branch_count, u64 predicted_count,
1352 			 u64 abort_count,
1353 			 const struct branch_type_stat *brtype_stat)
1354 {
1355 	int printed, i = 0;
1356 
1357 	printed = branch_type_str(brtype_stat, bf, bfsize);
1358 	if (printed)
1359 		i++;
1360 
1361 	if (predicted_count < branch_count) {
1362 		printed += count_float_printf(i++, "predicted",
1363 				predicted_count * 100.0 / branch_count,
1364 				bf + printed, bfsize - printed, 0.0);
1365 	}
1366 
1367 	if (abort_count) {
1368 		printed += count_float_printf(i++, "abort",
1369 				abort_count * 100.0 / branch_count,
1370 				bf + printed, bfsize - printed, 0.1);
1371 	}
1372 
1373 	if (i)
1374 		printed += scnprintf(bf + printed, bfsize - printed, ")");
1375 
1376 	return printed;
1377 }
1378 
1379 static int branch_from_str(char *bf, int bfsize,
1380 			   u64 branch_count,
1381 			   u64 cycles_count, u64 iter_count,
1382 			   u64 iter_cycles, u64 from_count)
1383 {
1384 	int printed = 0, i = 0;
1385 	u64 cycles, v = 0;
1386 
1387 	cycles = cycles_count / branch_count;
1388 	if (cycles) {
1389 		printed += count_pri64_printf(i++, "cycles",
1390 				cycles,
1391 				bf + printed, bfsize - printed);
1392 	}
1393 
1394 	if (iter_count && from_count) {
1395 		v = iter_count / from_count;
1396 		if (v) {
1397 			printed += count_pri64_printf(i++, "iter",
1398 					v, bf + printed, bfsize - printed);
1399 
1400 			printed += count_pri64_printf(i++, "avg_cycles",
1401 					iter_cycles / iter_count,
1402 					bf + printed, bfsize - printed);
1403 		}
1404 	}
1405 
1406 	if (i)
1407 		printed += scnprintf(bf + printed, bfsize - printed, ")");
1408 
1409 	return printed;
1410 }
1411 
1412 static int counts_str_build(char *bf, int bfsize,
1413 			     u64 branch_count, u64 predicted_count,
1414 			     u64 abort_count, u64 cycles_count,
1415 			     u64 iter_count, u64 iter_cycles,
1416 			     u64 from_count,
1417 			     const struct branch_type_stat *brtype_stat)
1418 {
1419 	int printed;
1420 
1421 	if (branch_count == 0)
1422 		return scnprintf(bf, bfsize, " (calltrace)");
1423 
1424 	if (brtype_stat->branch_to) {
1425 		printed = branch_to_str(bf, bfsize, branch_count,
1426 				predicted_count, abort_count, brtype_stat);
1427 	} else {
1428 		printed = branch_from_str(bf, bfsize, branch_count,
1429 				cycles_count, iter_count, iter_cycles,
1430 				from_count);
1431 	}
1432 
1433 	if (!printed)
1434 		bf[0] = 0;
1435 
1436 	return printed;
1437 }
1438 
1439 static int callchain_counts_printf(FILE *fp, char *bf, int bfsize,
1440 				   u64 branch_count, u64 predicted_count,
1441 				   u64 abort_count, u64 cycles_count,
1442 				   u64 iter_count, u64 iter_cycles,
1443 				   u64 from_count,
1444 				   const struct branch_type_stat *brtype_stat)
1445 {
1446 	char str[256];
1447 
1448 	counts_str_build(str, sizeof(str), branch_count,
1449 			 predicted_count, abort_count, cycles_count,
1450 			 iter_count, iter_cycles, from_count, brtype_stat);
1451 
1452 	if (fp)
1453 		return fprintf(fp, "%s", str);
1454 
1455 	return scnprintf(bf, bfsize, "%s", str);
1456 }
1457 
1458 int callchain_list_counts__printf_value(struct callchain_list *clist,
1459 					FILE *fp, char *bf, int bfsize)
1460 {
1461 	static const struct branch_type_stat empty_brtype_stat = {};
1462 	const struct branch_type_stat *brtype_stat;
1463 	u64 branch_count, predicted_count;
1464 	u64 abort_count, cycles_count;
1465 	u64 iter_count, iter_cycles;
1466 	u64 from_count;
1467 
1468 	brtype_stat = clist->brtype_stat ?: &empty_brtype_stat;
1469 	branch_count = clist->branch_count;
1470 	predicted_count = clist->predicted_count;
1471 	abort_count = clist->abort_count;
1472 	cycles_count = clist->cycles_count;
1473 	iter_count = clist->iter_count;
1474 	iter_cycles = clist->iter_cycles;
1475 	from_count = clist->from_count;
1476 
1477 	return callchain_counts_printf(fp, bf, bfsize, branch_count,
1478 				       predicted_count, abort_count,
1479 				       cycles_count, iter_count, iter_cycles,
1480 				       from_count, brtype_stat);
1481 }
1482 
1483 static void free_callchain_node(struct callchain_node *node)
1484 {
1485 	struct callchain_list *list, *tmp;
1486 	struct callchain_node *child;
1487 	struct rb_node *n;
1488 
1489 	list_for_each_entry_safe(list, tmp, &node->parent_val, list) {
1490 		list_del_init(&list->list);
1491 		map_symbol__exit(&list->ms);
1492 		zfree(&list->brtype_stat);
1493 		free(list);
1494 	}
1495 
1496 	list_for_each_entry_safe(list, tmp, &node->val, list) {
1497 		list_del_init(&list->list);
1498 		map_symbol__exit(&list->ms);
1499 		zfree(&list->brtype_stat);
1500 		free(list);
1501 	}
1502 
1503 	n = rb_first(&node->rb_root_in);
1504 	while (n) {
1505 		child = container_of(n, struct callchain_node, rb_node_in);
1506 		n = rb_next(n);
1507 		rb_erase(&child->rb_node_in, &node->rb_root_in);
1508 
1509 		free_callchain_node(child);
1510 		free(child);
1511 	}
1512 }
1513 
1514 void free_callchain(struct callchain_root *root)
1515 {
1516 	if (!symbol_conf.use_callchain)
1517 		return;
1518 
1519 	free_callchain_node(&root->node);
1520 }
1521 
1522 static u64 decay_callchain_node(struct callchain_node *node)
1523 {
1524 	struct callchain_node *child;
1525 	struct rb_node *n;
1526 	u64 child_hits = 0;
1527 
1528 	n = rb_first(&node->rb_root_in);
1529 	while (n) {
1530 		child = container_of(n, struct callchain_node, rb_node_in);
1531 
1532 		child_hits += decay_callchain_node(child);
1533 		n = rb_next(n);
1534 	}
1535 
1536 	node->hit = (node->hit * 7) / 8;
1537 	node->children_hit = child_hits;
1538 
1539 	return node->hit;
1540 }
1541 
1542 void decay_callchain(struct callchain_root *root)
1543 {
1544 	if (!symbol_conf.use_callchain)
1545 		return;
1546 
1547 	decay_callchain_node(&root->node);
1548 }
1549 
1550 int callchain_node__make_parent_list(struct callchain_node *node)
1551 {
1552 	struct callchain_node *parent = node->parent;
1553 	struct callchain_list *chain, *new;
1554 	LIST_HEAD(head);
1555 
1556 	while (parent) {
1557 		list_for_each_entry_reverse(chain, &parent->val, list) {
1558 			new = malloc(sizeof(*new));
1559 			if (new == NULL)
1560 				goto out;
1561 			*new = *chain;
1562 			new->has_children = false;
1563 			map_symbol__copy(&new->ms, &chain->ms);
1564 			list_add_tail(&new->list, &head);
1565 		}
1566 		parent = parent->parent;
1567 	}
1568 
1569 	list_for_each_entry_safe_reverse(chain, new, &head, list)
1570 		list_move_tail(&chain->list, &node->parent_val);
1571 
1572 	if (!list_empty(&node->parent_val)) {
1573 		chain = list_first_entry(&node->parent_val, struct callchain_list, list);
1574 		chain->has_children = rb_prev(&node->rb_node) || rb_next(&node->rb_node);
1575 
1576 		chain = list_first_entry(&node->val, struct callchain_list, list);
1577 		chain->has_children = false;
1578 	}
1579 	return 0;
1580 
1581 out:
1582 	list_for_each_entry_safe(chain, new, &head, list) {
1583 		list_del_init(&chain->list);
1584 		map_symbol__exit(&chain->ms);
1585 		zfree(&chain->brtype_stat);
1586 		free(chain);
1587 	}
1588 	return -ENOMEM;
1589 }
1590 
1591 static void callchain_cursor__delete(void *vcursor)
1592 {
1593 	struct callchain_cursor *cursor = vcursor;
1594 	struct callchain_cursor_node *node, *next;
1595 
1596 	callchain_cursor_reset(cursor);
1597 	for (node = cursor->first; node != NULL; node = next) {
1598 		next = node->next;
1599 		free(node);
1600 	}
1601 	free(cursor);
1602 }
1603 
1604 static void init_callchain_cursor_key(void)
1605 {
1606 	if (pthread_key_create(&callchain_cursor, callchain_cursor__delete)) {
1607 		pr_err("callchain cursor creation failed");
1608 		abort();
1609 	}
1610 }
1611 
1612 struct callchain_cursor *get_tls_callchain_cursor(void)
1613 {
1614 	static pthread_once_t once_control = PTHREAD_ONCE_INIT;
1615 	struct callchain_cursor *cursor;
1616 
1617 	pthread_once(&once_control, init_callchain_cursor_key);
1618 	cursor = pthread_getspecific(callchain_cursor);
1619 	if (!cursor) {
1620 		cursor = zalloc(sizeof(*cursor));
1621 		if (!cursor)
1622 			pr_debug3("%s: not enough memory\n", __func__);
1623 		pthread_setspecific(callchain_cursor, cursor);
1624 	}
1625 	return cursor;
1626 }
1627 
1628 int callchain_cursor__copy(struct callchain_cursor *dst,
1629 			   struct callchain_cursor *src)
1630 {
1631 	int rc = 0;
1632 
1633 	callchain_cursor_reset(dst);
1634 	callchain_cursor_commit(src);
1635 
1636 	while (true) {
1637 		struct callchain_cursor_node *node;
1638 
1639 		node = callchain_cursor_current(src);
1640 		if (node == NULL)
1641 			break;
1642 
1643 		rc = callchain_cursor_append(dst, node->ip, &node->ms,
1644 					     node->branch, &node->branch_flags,
1645 					     node->nr_loop_iter,
1646 					     node->iter_cycles,
1647 					     node->branch_from, node->srcline);
1648 		if (rc)
1649 			break;
1650 
1651 		callchain_cursor_advance(src);
1652 	}
1653 
1654 	return rc;
1655 }
1656 
1657 /*
1658  * Initialize a cursor before adding entries inside, but keep
1659  * the previously allocated entries as a cache.
1660  */
1661 void callchain_cursor_reset(struct callchain_cursor *cursor)
1662 {
1663 	struct callchain_cursor_node *node;
1664 
1665 	cursor->nr = 0;
1666 	cursor->last = &cursor->first;
1667 
1668 	for (node = cursor->first; node != NULL; node = node->next)
1669 		map_symbol__exit(&node->ms);
1670 }
1671 
1672 void callchain_param_setup(u64 sample_type, const char *arch)
1673 {
1674 	if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
1675 		if ((sample_type & PERF_SAMPLE_REGS_USER) &&
1676 		    (sample_type & PERF_SAMPLE_STACK_USER)) {
1677 			callchain_param.record_mode = CALLCHAIN_DWARF;
1678 			dwarf_callchain_users = true;
1679 		} else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
1680 			callchain_param.record_mode = CALLCHAIN_LBR;
1681 		else
1682 			callchain_param.record_mode = CALLCHAIN_FP;
1683 	}
1684 
1685 	/*
1686 	 * It's necessary to use libunwind to reliably determine the caller of
1687 	 * a leaf function on aarch64, as otherwise we cannot know whether to
1688 	 * start from the LR or FP.
1689 	 *
1690 	 * Always starting from the LR can result in duplicate or entirely
1691 	 * erroneous entries. Always skipping the LR and starting from the FP
1692 	 * can result in missing entries.
1693 	 */
1694 	if (callchain_param.record_mode == CALLCHAIN_FP && !strcmp(arch, "arm64"))
1695 		dwarf_callchain_users = true;
1696 }
1697 
1698 static bool chain_match(struct callchain_list *base_chain,
1699 			struct callchain_list *pair_chain)
1700 {
1701 	enum match_result match;
1702 
1703 	match = match_chain_strings(base_chain->srcline,
1704 				    pair_chain->srcline);
1705 	if (match != MATCH_ERROR)
1706 		return match == MATCH_EQ;
1707 
1708 	match = match_chain_dso_addresses(base_chain->ms.map,
1709 					  base_chain->ip,
1710 					  pair_chain->ms.map,
1711 					  pair_chain->ip);
1712 
1713 	return match == MATCH_EQ;
1714 }
1715 
1716 bool callchain_cnode_matched(struct callchain_node *base_cnode,
1717 			     struct callchain_node *pair_cnode)
1718 {
1719 	struct callchain_list *base_chain, *pair_chain;
1720 	bool match = false;
1721 
1722 	pair_chain = list_first_entry(&pair_cnode->val,
1723 				      struct callchain_list,
1724 				      list);
1725 
1726 	list_for_each_entry(base_chain, &base_cnode->val, list) {
1727 		if (&pair_chain->list == &pair_cnode->val)
1728 			return false;
1729 
1730 		if (!base_chain->srcline || !pair_chain->srcline) {
1731 			pair_chain = list_next_entry(pair_chain, list);
1732 			continue;
1733 		}
1734 
1735 		match = chain_match(base_chain, pair_chain);
1736 		if (!match)
1737 			return false;
1738 
1739 		pair_chain = list_next_entry(pair_chain, list);
1740 	}
1741 
1742 	/*
1743 	 * Say chain1 is ABC, chain2 is ABCD, we consider they are
1744 	 * not fully matched.
1745 	 */
1746 	if (pair_chain && (&pair_chain->list != &pair_cnode->val))
1747 		return false;
1748 
1749 	return match;
1750 }
1751 
1752 static u64 count_callchain_hits(struct hist_entry *he)
1753 {
1754 	struct rb_root *root = &he->sorted_chain;
1755 	struct rb_node *rb_node = rb_first(root);
1756 	struct callchain_node *node;
1757 	u64 chain_hits = 0;
1758 
1759 	while (rb_node) {
1760 		node = rb_entry(rb_node, struct callchain_node, rb_node);
1761 		chain_hits += node->hit;
1762 		rb_node = rb_next(rb_node);
1763 	}
1764 
1765 	return chain_hits;
1766 }
1767 
1768 u64 callchain_total_hits(struct hists *hists)
1769 {
1770 	struct rb_node *next = rb_first_cached(&hists->entries);
1771 	u64 chain_hits = 0;
1772 
1773 	while (next) {
1774 		struct hist_entry *he = rb_entry(next, struct hist_entry,
1775 						 rb_node);
1776 
1777 		chain_hits += count_callchain_hits(he);
1778 		next = rb_next(&he->rb_node);
1779 	}
1780 
1781 	return chain_hits;
1782 }
1783 
1784 s64 callchain_avg_cycles(struct callchain_node *cnode)
1785 {
1786 	struct callchain_list *chain;
1787 	s64 cycles = 0;
1788 
1789 	list_for_each_entry(chain, &cnode->val, list) {
1790 		if (chain->srcline && chain->branch_count)
1791 			cycles += chain->cycles_count / chain->branch_count;
1792 	}
1793 
1794 	return cycles;
1795 }
1796 
1797 int sample__for_each_callchain_node(struct thread *thread, struct evsel *evsel,
1798 				    struct perf_sample *sample, int max_stack,
1799 				    bool symbols, callchain_iter_fn cb, void *data)
1800 {
1801 	struct callchain_cursor *cursor = get_tls_callchain_cursor();
1802 	int ret;
1803 
1804 	if (!cursor)
1805 		return -ENOMEM;
1806 
1807 	/* Fill in the callchain. */
1808 	ret = __thread__resolve_callchain(thread, cursor, evsel, sample,
1809 					  /*parent=*/NULL, /*root_al=*/NULL,
1810 					  max_stack, symbols);
1811 	if (ret)
1812 		return ret;
1813 
1814 	/* Switch from writing the callchain to reading it. */
1815 	callchain_cursor_commit(cursor);
1816 
1817 	while (1) {
1818 		struct callchain_cursor_node *node = callchain_cursor_current(cursor);
1819 
1820 		if (!node)
1821 			break;
1822 
1823 		ret = cb(node, data);
1824 		if (ret)
1825 			return ret;
1826 
1827 		callchain_cursor_advance(cursor);
1828 	}
1829 	return 0;
1830 }
1831