2 * Copyright (C) 2009, Frederic Weisbecker <fweisbec@gmail.com>
4 * Handle the callchains from the stream in an ad-hoc radix tree and then
5 * sort them in an rbtree.
7 * Using a radix for code path provides a fast retrieval and factorizes
8 * memory use. Also that lets us use the paths in a hierarchical graph view.
18 #include "callchain.h"
20 #define chain_for_each_child(child, parent) \
21 list_for_each_entry(child, &parent->children, brothers)
24 rb_insert_callchain(struct rb_root
*root
, struct callchain_node
*chain
,
27 struct rb_node
**p
= &root
->rb_node
;
28 struct rb_node
*parent
= NULL
;
29 struct callchain_node
*rnode
;
30 u64 chain_cumul
= cumul_hits(chain
);
36 rnode
= rb_entry(parent
, struct callchain_node
, rb_node
);
37 rnode_cumul
= cumul_hits(rnode
);
41 if (rnode
->hit
< chain
->hit
)
46 case CHAIN_GRAPH_ABS
: /* Falldown */
48 if (rnode_cumul
< chain_cumul
)
58 rb_link_node(&chain
->rb_node
, parent
, p
);
59 rb_insert_color(&chain
->rb_node
, root
);
63 __sort_chain_flat(struct rb_root
*rb_root
, struct callchain_node
*node
,
66 struct callchain_node
*child
;
68 chain_for_each_child(child
, node
)
69 __sort_chain_flat(rb_root
, child
, min_hit
);
71 if (node
->hit
&& node
->hit
>= min_hit
)
72 rb_insert_callchain(rb_root
, node
, CHAIN_FLAT
);
76 * Once we get every callchains from the stream, we can now
80 sort_chain_flat(struct rb_root
*rb_root
, struct callchain_node
*node
,
81 u64 min_hit
, struct callchain_param
*param __used
)
83 __sort_chain_flat(rb_root
, node
, min_hit
);
86 static void __sort_chain_graph_abs(struct callchain_node
*node
,
89 struct callchain_node
*child
;
91 node
->rb_root
= RB_ROOT
;
93 chain_for_each_child(child
, node
) {
94 __sort_chain_graph_abs(child
, min_hit
);
95 if (cumul_hits(child
) >= min_hit
)
96 rb_insert_callchain(&node
->rb_root
, child
,
102 sort_chain_graph_abs(struct rb_root
*rb_root
, struct callchain_node
*chain_root
,
103 u64 min_hit
, struct callchain_param
*param __used
)
105 __sort_chain_graph_abs(chain_root
, min_hit
);
106 rb_root
->rb_node
= chain_root
->rb_root
.rb_node
;
109 static void __sort_chain_graph_rel(struct callchain_node
*node
,
112 struct callchain_node
*child
;
115 node
->rb_root
= RB_ROOT
;
116 min_hit
= ceil(node
->children_hit
* min_percent
);
118 chain_for_each_child(child
, node
) {
119 __sort_chain_graph_rel(child
, min_percent
);
120 if (cumul_hits(child
) >= min_hit
)
121 rb_insert_callchain(&node
->rb_root
, child
,
127 sort_chain_graph_rel(struct rb_root
*rb_root
, struct callchain_node
*chain_root
,
128 u64 min_hit __used
, struct callchain_param
*param
)
130 __sort_chain_graph_rel(chain_root
, param
->min_percent
/ 100.0);
131 rb_root
->rb_node
= chain_root
->rb_root
.rb_node
;
134 int register_callchain_param(struct callchain_param
*param
)
136 switch (param
->mode
) {
137 case CHAIN_GRAPH_ABS
:
138 param
->sort
= sort_chain_graph_abs
;
140 case CHAIN_GRAPH_REL
:
141 param
->sort
= sort_chain_graph_rel
;
144 param
->sort
= sort_chain_flat
;
153 * Create a child for a parent. If inherit_children, then the new child
154 * will become the new parent of it's parent children
156 static struct callchain_node
*
157 create_child(struct callchain_node
*parent
, bool inherit_children
)
159 struct callchain_node
*new;
161 new = malloc(sizeof(*new));
163 perror("not enough memory to create child for code path tree");
166 new->parent
= parent
;
167 INIT_LIST_HEAD(&new->children
);
168 INIT_LIST_HEAD(&new->val
);
170 if (inherit_children
) {
171 struct callchain_node
*next
;
173 list_splice(&parent
->children
, &new->children
);
174 INIT_LIST_HEAD(&parent
->children
);
176 chain_for_each_child(next
, new)
179 list_add_tail(&new->brothers
, &parent
->children
);
185 * Fill the node with callchain values
188 fill_node(struct callchain_node
*node
, struct ip_callchain
*chain
,
189 int start
, struct symbol
**syms
)
193 for (i
= start
; i
< chain
->nr
; i
++) {
194 struct callchain_list
*call
;
196 call
= malloc(sizeof(*call
));
198 perror("not enough memory for the code path tree");
201 call
->ip
= chain
->ips
[i
];
203 list_add_tail(&call
->list
, &node
->val
);
205 node
->val_nr
= chain
->nr
- start
;
207 printf("Warning: empty node in callchain tree\n");
211 add_child(struct callchain_node
*parent
, struct ip_callchain
*chain
,
212 int start
, struct symbol
**syms
)
214 struct callchain_node
*new;
216 new = create_child(parent
, false);
217 fill_node(new, chain
, start
, syms
);
219 new->children_hit
= 0;
224 * Split the parent in two parts (a new child is created) and
225 * give a part of its callchain to the created child.
226 * Then create another child to host the given callchain of new branch
229 split_add_child(struct callchain_node
*parent
, struct ip_callchain
*chain
,
230 struct callchain_list
*to_split
, int idx_parents
, int idx_local
,
231 struct symbol
**syms
)
233 struct callchain_node
*new;
234 struct list_head
*old_tail
;
235 unsigned int idx_total
= idx_parents
+ idx_local
;
238 new = create_child(parent
, true);
240 /* split the callchain and move a part to the new child */
241 old_tail
= parent
->val
.prev
;
242 list_del_range(&to_split
->list
, old_tail
);
243 new->val
.next
= &to_split
->list
;
244 new->val
.prev
= old_tail
;
245 to_split
->list
.prev
= &new->val
;
246 old_tail
->next
= &new->val
;
249 new->hit
= parent
->hit
;
250 new->children_hit
= parent
->children_hit
;
251 parent
->children_hit
= cumul_hits(new);
252 new->val_nr
= parent
->val_nr
- idx_local
;
253 parent
->val_nr
= idx_local
;
255 /* create a new child for the new branch if any */
256 if (idx_total
< chain
->nr
) {
258 add_child(parent
, chain
, idx_total
, syms
);
259 parent
->children_hit
++;
266 __append_chain(struct callchain_node
*root
, struct ip_callchain
*chain
,
267 unsigned int start
, struct symbol
**syms
);
270 __append_chain_children(struct callchain_node
*root
, struct ip_callchain
*chain
,
271 struct symbol
**syms
, unsigned int start
)
273 struct callchain_node
*rnode
;
275 /* lookup in childrens */
276 chain_for_each_child(rnode
, root
) {
277 unsigned int ret
= __append_chain(rnode
, chain
, start
, syms
);
280 goto inc_children_hit
;
282 /* nothing in children, add to the current node */
283 add_child(root
, chain
, start
, syms
);
286 root
->children_hit
++;
290 __append_chain(struct callchain_node
*root
, struct ip_callchain
*chain
,
291 unsigned int start
, struct symbol
**syms
)
293 struct callchain_list
*cnode
;
294 unsigned int i
= start
;
298 * Lookup in the current node
299 * If we have a symbol, then compare the start to match
300 * anywhere inside a function.
302 list_for_each_entry(cnode
, &root
->val
, list
) {
305 if (cnode
->sym
&& syms
[i
]) {
306 if (cnode
->sym
->start
!= syms
[i
]->start
)
308 } else if (cnode
->ip
!= chain
->ips
[i
])
315 /* matches not, relay on the parent */
319 /* we match only a part of the node. Split it and add the new chain */
320 if (i
- start
< root
->val_nr
) {
321 split_add_child(root
, chain
, cnode
, start
, i
- start
, syms
);
325 /* we match 100% of the path, increment the hit */
326 if (i
- start
== root
->val_nr
&& i
== chain
->nr
) {
331 /* We match the node and still have a part remaining */
332 __append_chain_children(root
, chain
, syms
, i
);
337 void append_chain(struct callchain_node
*root
, struct ip_callchain
*chain
,
338 struct symbol
**syms
)
342 __append_chain_children(root
, chain
, syms
, 0);