2 * Copyright (C) 2009-2011, 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.
19 #include "callchain.h"
21 bool ip_callchain__valid(struct ip_callchain
*chain
,
22 const union perf_event
*event
)
24 unsigned int chain_size
= event
->header
.size
;
25 chain_size
-= (unsigned long)&event
->ip
.__more_data
- (unsigned long)event
;
26 return chain
->nr
* sizeof(u64
) <= chain_size
;
29 #define chain_for_each_child(child, parent) \
30 list_for_each_entry(child, &parent->children, siblings)
32 #define chain_for_each_child_safe(child, next, parent) \
33 list_for_each_entry_safe(child, next, &parent->children, siblings)
36 rb_insert_callchain(struct rb_root
*root
, struct callchain_node
*chain
,
39 struct rb_node
**p
= &root
->rb_node
;
40 struct rb_node
*parent
= NULL
;
41 struct callchain_node
*rnode
;
42 u64 chain_cumul
= callchain_cumul_hits(chain
);
48 rnode
= rb_entry(parent
, struct callchain_node
, rb_node
);
49 rnode_cumul
= callchain_cumul_hits(rnode
);
53 if (rnode
->hit
< chain
->hit
)
58 case CHAIN_GRAPH_ABS
: /* Falldown */
60 if (rnode_cumul
< chain_cumul
)
71 rb_link_node(&chain
->rb_node
, parent
, p
);
72 rb_insert_color(&chain
->rb_node
, root
);
76 __sort_chain_flat(struct rb_root
*rb_root
, struct callchain_node
*node
,
79 struct callchain_node
*child
;
81 chain_for_each_child(child
, node
)
82 __sort_chain_flat(rb_root
, child
, min_hit
);
84 if (node
->hit
&& node
->hit
>= min_hit
)
85 rb_insert_callchain(rb_root
, node
, CHAIN_FLAT
);
89 * Once we get every callchains from the stream, we can now
93 sort_chain_flat(struct rb_root
*rb_root
, struct callchain_root
*root
,
94 u64 min_hit
, struct callchain_param
*param __used
)
96 __sort_chain_flat(rb_root
, &root
->node
, min_hit
);
99 static void __sort_chain_graph_abs(struct callchain_node
*node
,
102 struct callchain_node
*child
;
104 node
->rb_root
= RB_ROOT
;
106 chain_for_each_child(child
, node
) {
107 __sort_chain_graph_abs(child
, min_hit
);
108 if (callchain_cumul_hits(child
) >= min_hit
)
109 rb_insert_callchain(&node
->rb_root
, child
,
115 sort_chain_graph_abs(struct rb_root
*rb_root
, struct callchain_root
*chain_root
,
116 u64 min_hit
, struct callchain_param
*param __used
)
118 __sort_chain_graph_abs(&chain_root
->node
, min_hit
);
119 rb_root
->rb_node
= chain_root
->node
.rb_root
.rb_node
;
122 static void __sort_chain_graph_rel(struct callchain_node
*node
,
125 struct callchain_node
*child
;
128 node
->rb_root
= RB_ROOT
;
129 min_hit
= ceil(node
->children_hit
* min_percent
);
131 chain_for_each_child(child
, node
) {
132 __sort_chain_graph_rel(child
, min_percent
);
133 if (callchain_cumul_hits(child
) >= min_hit
)
134 rb_insert_callchain(&node
->rb_root
, child
,
140 sort_chain_graph_rel(struct rb_root
*rb_root
, struct callchain_root
*chain_root
,
141 u64 min_hit __used
, struct callchain_param
*param
)
143 __sort_chain_graph_rel(&chain_root
->node
, param
->min_percent
/ 100.0);
144 rb_root
->rb_node
= chain_root
->node
.rb_root
.rb_node
;
147 int callchain_register_param(struct callchain_param
*param
)
149 switch (param
->mode
) {
150 case CHAIN_GRAPH_ABS
:
151 param
->sort
= sort_chain_graph_abs
;
153 case CHAIN_GRAPH_REL
:
154 param
->sort
= sort_chain_graph_rel
;
157 param
->sort
= sort_chain_flat
;
167 * Create a child for a parent. If inherit_children, then the new child
168 * will become the new parent of it's parent children
170 static struct callchain_node
*
171 create_child(struct callchain_node
*parent
, bool inherit_children
)
173 struct callchain_node
*new;
175 new = zalloc(sizeof(*new));
177 perror("not enough memory to create child for code path tree");
180 new->parent
= parent
;
181 INIT_LIST_HEAD(&new->children
);
182 INIT_LIST_HEAD(&new->val
);
184 if (inherit_children
) {
185 struct callchain_node
*next
;
187 list_splice(&parent
->children
, &new->children
);
188 INIT_LIST_HEAD(&parent
->children
);
190 chain_for_each_child(next
, new)
193 list_add_tail(&new->siblings
, &parent
->children
);
200 * Fill the node with callchain values
203 fill_node(struct callchain_node
*node
, struct callchain_cursor
*cursor
)
205 struct callchain_cursor_node
*cursor_node
;
207 node
->val_nr
= cursor
->nr
- cursor
->pos
;
209 pr_warning("Warning: empty node in callchain tree\n");
211 cursor_node
= callchain_cursor_current(cursor
);
213 while (cursor_node
) {
214 struct callchain_list
*call
;
216 call
= zalloc(sizeof(*call
));
218 perror("not enough memory for the code path tree");
221 call
->ip
= cursor_node
->ip
;
222 call
->ms
.sym
= cursor_node
->sym
;
223 call
->ms
.map
= cursor_node
->map
;
224 list_add_tail(&call
->list
, &node
->val
);
226 callchain_cursor_advance(cursor
);
227 cursor_node
= callchain_cursor_current(cursor
);
232 add_child(struct callchain_node
*parent
,
233 struct callchain_cursor
*cursor
,
236 struct callchain_node
*new;
238 new = create_child(parent
, false);
239 fill_node(new, cursor
);
241 new->children_hit
= 0;
246 * Split the parent in two parts (a new child is created) and
247 * give a part of its callchain to the created child.
248 * Then create another child to host the given callchain of new branch
251 split_add_child(struct callchain_node
*parent
,
252 struct callchain_cursor
*cursor
,
253 struct callchain_list
*to_split
,
254 u64 idx_parents
, u64 idx_local
, u64 period
)
256 struct callchain_node
*new;
257 struct list_head
*old_tail
;
258 unsigned int idx_total
= idx_parents
+ idx_local
;
261 new = create_child(parent
, true);
263 /* split the callchain and move a part to the new child */
264 old_tail
= parent
->val
.prev
;
265 list_del_range(&to_split
->list
, old_tail
);
266 new->val
.next
= &to_split
->list
;
267 new->val
.prev
= old_tail
;
268 to_split
->list
.prev
= &new->val
;
269 old_tail
->next
= &new->val
;
272 new->hit
= parent
->hit
;
273 new->children_hit
= parent
->children_hit
;
274 parent
->children_hit
= callchain_cumul_hits(new);
275 new->val_nr
= parent
->val_nr
- idx_local
;
276 parent
->val_nr
= idx_local
;
278 /* create a new child for the new branch if any */
279 if (idx_total
< cursor
->nr
) {
281 add_child(parent
, cursor
, period
);
282 parent
->children_hit
+= period
;
284 parent
->hit
= period
;
289 append_chain(struct callchain_node
*root
,
290 struct callchain_cursor
*cursor
,
294 append_chain_children(struct callchain_node
*root
,
295 struct callchain_cursor
*cursor
,
298 struct callchain_node
*rnode
;
300 /* lookup in childrens */
301 chain_for_each_child(rnode
, root
) {
302 unsigned int ret
= append_chain(rnode
, cursor
, period
);
305 goto inc_children_hit
;
307 /* nothing in children, add to the current node */
308 add_child(root
, cursor
, period
);
311 root
->children_hit
+= period
;
315 append_chain(struct callchain_node
*root
,
316 struct callchain_cursor
*cursor
,
319 struct callchain_cursor_node
*curr_snap
= cursor
->curr
;
320 struct callchain_list
*cnode
;
321 u64 start
= cursor
->pos
;
326 * Lookup in the current node
327 * If we have a symbol, then compare the start to match
328 * anywhere inside a function.
330 list_for_each_entry(cnode
, &root
->val
, list
) {
331 struct callchain_cursor_node
*node
;
334 node
= callchain_cursor_current(cursor
);
340 if (cnode
->ms
.sym
&& sym
) {
341 if (cnode
->ms
.sym
->start
!= sym
->start
)
343 } else if (cnode
->ip
!= node
->ip
)
349 callchain_cursor_advance(cursor
);
352 /* matches not, relay on the parent */
354 cursor
->curr
= curr_snap
;
359 matches
= cursor
->pos
- start
;
361 /* we match only a part of the node. Split it and add the new chain */
362 if (matches
< root
->val_nr
) {
363 split_add_child(root
, cursor
, cnode
, start
, matches
, period
);
367 /* we match 100% of the path, increment the hit */
368 if (matches
== root
->val_nr
&& cursor
->pos
== cursor
->nr
) {
373 /* We match the node and still have a part remaining */
374 append_chain_children(root
, cursor
, period
);
379 int callchain_append(struct callchain_root
*root
,
380 struct callchain_cursor
*cursor
,
386 callchain_cursor_commit(cursor
);
388 append_chain_children(&root
->node
, cursor
, period
);
390 if (cursor
->nr
> root
->max_depth
)
391 root
->max_depth
= cursor
->nr
;
397 merge_chain_branch(struct callchain_cursor
*cursor
,
398 struct callchain_node
*dst
, struct callchain_node
*src
)
400 struct callchain_cursor_node
**old_last
= cursor
->last
;
401 struct callchain_node
*child
, *next_child
;
402 struct callchain_list
*list
, *next_list
;
403 int old_pos
= cursor
->nr
;
406 list_for_each_entry_safe(list
, next_list
, &src
->val
, list
) {
407 callchain_cursor_append(cursor
, list
->ip
,
408 list
->ms
.map
, list
->ms
.sym
);
409 list_del(&list
->list
);
414 callchain_cursor_commit(cursor
);
415 append_chain_children(dst
, cursor
, src
->hit
);
418 chain_for_each_child_safe(child
, next_child
, src
) {
419 err
= merge_chain_branch(cursor
, dst
, child
);
423 list_del(&child
->siblings
);
427 cursor
->nr
= old_pos
;
428 cursor
->last
= old_last
;
433 int callchain_merge(struct callchain_cursor
*cursor
,
434 struct callchain_root
*dst
, struct callchain_root
*src
)
436 return merge_chain_branch(cursor
, &dst
->node
, &src
->node
);
439 int callchain_cursor_append(struct callchain_cursor
*cursor
,
440 u64 ip
, struct map
*map
, struct symbol
*sym
)
442 struct callchain_cursor_node
*node
= *cursor
->last
;
445 node
= calloc(sizeof(*node
), 1);
449 *cursor
->last
= node
;
458 cursor
->last
= &node
->next
;