xref: /openbmc/linux/tools/perf/util/call-path.c (revision 0898782247ae533d1f4e47a06bc5d4870931b284)
12025cf9eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2451db126SChris Phlipot /*
3451db126SChris Phlipot  * call-path.h: Manipulate a tree data structure containing function call paths
4451db126SChris Phlipot  * Copyright (c) 2014, Intel Corporation.
5451db126SChris Phlipot  */
6451db126SChris Phlipot 
7451db126SChris Phlipot #include <linux/rbtree.h>
8451db126SChris Phlipot #include <linux/list.h>
97f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
107f7c536fSArnaldo Carvalho de Melo #include <stdlib.h>
11451db126SChris Phlipot 
12451db126SChris Phlipot #include "call-path.h"
13451db126SChris Phlipot 
call_path__init(struct call_path * cp,struct call_path * parent,struct symbol * sym,u64 ip,bool in_kernel)14451db126SChris Phlipot static void call_path__init(struct call_path *cp, struct call_path *parent,
15451db126SChris Phlipot 			    struct symbol *sym, u64 ip, bool in_kernel)
16451db126SChris Phlipot {
17451db126SChris Phlipot 	cp->parent = parent;
18451db126SChris Phlipot 	cp->sym = sym;
19451db126SChris Phlipot 	cp->ip = sym ? 0 : ip;
20451db126SChris Phlipot 	cp->db_id = 0;
21451db126SChris Phlipot 	cp->in_kernel = in_kernel;
22451db126SChris Phlipot 	RB_CLEAR_NODE(&cp->rb_node);
23451db126SChris Phlipot 	cp->children = RB_ROOT;
24451db126SChris Phlipot }
25451db126SChris Phlipot 
call_path_root__new(void)26451db126SChris Phlipot struct call_path_root *call_path_root__new(void)
27451db126SChris Phlipot {
28451db126SChris Phlipot 	struct call_path_root *cpr;
29451db126SChris Phlipot 
30451db126SChris Phlipot 	cpr = zalloc(sizeof(struct call_path_root));
31451db126SChris Phlipot 	if (!cpr)
32451db126SChris Phlipot 		return NULL;
33451db126SChris Phlipot 	call_path__init(&cpr->call_path, NULL, NULL, 0, false);
34451db126SChris Phlipot 	INIT_LIST_HEAD(&cpr->blocks);
35451db126SChris Phlipot 	return cpr;
36451db126SChris Phlipot }
37451db126SChris Phlipot 
call_path_root__free(struct call_path_root * cpr)38451db126SChris Phlipot void call_path_root__free(struct call_path_root *cpr)
39451db126SChris Phlipot {
40451db126SChris Phlipot 	struct call_path_block *pos, *n;
41451db126SChris Phlipot 
42451db126SChris Phlipot 	list_for_each_entry_safe(pos, n, &cpr->blocks, node) {
43*e56fbc9dSArnaldo Carvalho de Melo 		list_del_init(&pos->node);
44451db126SChris Phlipot 		free(pos);
45451db126SChris Phlipot 	}
46451db126SChris Phlipot 	free(cpr);
47451db126SChris Phlipot }
48451db126SChris Phlipot 
call_path__new(struct call_path_root * cpr,struct call_path * parent,struct symbol * sym,u64 ip,bool in_kernel)49451db126SChris Phlipot static struct call_path *call_path__new(struct call_path_root *cpr,
50451db126SChris Phlipot 					struct call_path *parent,
51451db126SChris Phlipot 					struct symbol *sym, u64 ip,
52451db126SChris Phlipot 					bool in_kernel)
53451db126SChris Phlipot {
54451db126SChris Phlipot 	struct call_path_block *cpb;
55451db126SChris Phlipot 	struct call_path *cp;
56451db126SChris Phlipot 	size_t n;
57451db126SChris Phlipot 
58451db126SChris Phlipot 	if (cpr->next < cpr->sz) {
59451db126SChris Phlipot 		cpb = list_last_entry(&cpr->blocks, struct call_path_block,
60451db126SChris Phlipot 				      node);
61451db126SChris Phlipot 	} else {
62451db126SChris Phlipot 		cpb = zalloc(sizeof(struct call_path_block));
63451db126SChris Phlipot 		if (!cpb)
64451db126SChris Phlipot 			return NULL;
65451db126SChris Phlipot 		list_add_tail(&cpb->node, &cpr->blocks);
66451db126SChris Phlipot 		cpr->sz += CALL_PATH_BLOCK_SIZE;
67451db126SChris Phlipot 	}
68451db126SChris Phlipot 
69451db126SChris Phlipot 	n = cpr->next++ & CALL_PATH_BLOCK_MASK;
70451db126SChris Phlipot 	cp = &cpb->cp[n];
71451db126SChris Phlipot 
72451db126SChris Phlipot 	call_path__init(cp, parent, sym, ip, in_kernel);
73451db126SChris Phlipot 
74451db126SChris Phlipot 	return cp;
75451db126SChris Phlipot }
76451db126SChris Phlipot 
call_path__findnew(struct call_path_root * cpr,struct call_path * parent,struct symbol * sym,u64 ip,u64 ks)77451db126SChris Phlipot struct call_path *call_path__findnew(struct call_path_root *cpr,
78451db126SChris Phlipot 				     struct call_path *parent,
79451db126SChris Phlipot 				     struct symbol *sym, u64 ip, u64 ks)
80451db126SChris Phlipot {
81451db126SChris Phlipot 	struct rb_node **p;
82451db126SChris Phlipot 	struct rb_node *node_parent = NULL;
83451db126SChris Phlipot 	struct call_path *cp;
84451db126SChris Phlipot 	bool in_kernel = ip >= ks;
85451db126SChris Phlipot 
86451db126SChris Phlipot 	if (sym)
87451db126SChris Phlipot 		ip = 0;
88451db126SChris Phlipot 
89451db126SChris Phlipot 	if (!parent)
90451db126SChris Phlipot 		return call_path__new(cpr, parent, sym, ip, in_kernel);
91451db126SChris Phlipot 
92451db126SChris Phlipot 	p = &parent->children.rb_node;
93451db126SChris Phlipot 	while (*p != NULL) {
94451db126SChris Phlipot 		node_parent = *p;
95451db126SChris Phlipot 		cp = rb_entry(node_parent, struct call_path, rb_node);
96451db126SChris Phlipot 
97451db126SChris Phlipot 		if (cp->sym == sym && cp->ip == ip)
98451db126SChris Phlipot 			return cp;
99451db126SChris Phlipot 
100451db126SChris Phlipot 		if (sym < cp->sym || (sym == cp->sym && ip < cp->ip))
101451db126SChris Phlipot 			p = &(*p)->rb_left;
102451db126SChris Phlipot 		else
103451db126SChris Phlipot 			p = &(*p)->rb_right;
104451db126SChris Phlipot 	}
105451db126SChris Phlipot 
106451db126SChris Phlipot 	cp = call_path__new(cpr, parent, sym, ip, in_kernel);
107451db126SChris Phlipot 	if (!cp)
108451db126SChris Phlipot 		return NULL;
109451db126SChris Phlipot 
110451db126SChris Phlipot 	rb_link_node(&cp->rb_node, node_parent, p);
111451db126SChris Phlipot 	rb_insert_color(&cp->rb_node, &parent->children);
112451db126SChris Phlipot 
113451db126SChris Phlipot 	return cp;
114451db126SChris Phlipot }
115