Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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