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.
22 #include "callchain.h"
24 __thread struct callchain_cursor callchain_cursor;
27 rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
30 struct rb_node **p = &root->rb_node;
31 struct rb_node *parent = NULL;
32 struct callchain_node *rnode;
33 u64 chain_cumul = callchain_cumul_hits(chain);
39 rnode = rb_entry(parent, struct callchain_node, rb_node);
40 rnode_cumul = callchain_cumul_hits(rnode);
44 if (rnode->hit < chain->hit)
49 case CHAIN_GRAPH_ABS: /* Falldown */
51 if (rnode_cumul < chain_cumul)
62 rb_link_node(&chain->rb_node, parent, p);
63 rb_insert_color(&chain->rb_node, root);
67 __sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
71 struct callchain_node *child;
73 n = rb_first(&node->rb_root_in);
75 child = rb_entry(n, struct callchain_node, rb_node_in);
78 __sort_chain_flat(rb_root, child, min_hit);
81 if (node->hit && node->hit >= min_hit)
82 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
86 * Once we get every callchains from the stream, we can now
90 sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root,
91 u64 min_hit, struct callchain_param *param __maybe_unused)
93 __sort_chain_flat(rb_root, &root->node, min_hit);
96 static void __sort_chain_graph_abs(struct callchain_node *node,
100 struct callchain_node *child;
102 node->rb_root = RB_ROOT;
103 n = rb_first(&node->rb_root_in);
106 child = rb_entry(n, struct callchain_node, rb_node_in);
109 __sort_chain_graph_abs(child, min_hit);
110 if (callchain_cumul_hits(child) >= min_hit)
111 rb_insert_callchain(&node->rb_root, child,
117 sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root,
118 u64 min_hit, struct callchain_param *param __maybe_unused)
120 __sort_chain_graph_abs(&chain_root->node, min_hit);
121 rb_root->rb_node = chain_root->node.rb_root.rb_node;
124 static void __sort_chain_graph_rel(struct callchain_node *node,
128 struct callchain_node *child;
131 node->rb_root = RB_ROOT;
132 min_hit = ceil(node->children_hit * min_percent);
134 n = rb_first(&node->rb_root_in);
136 child = rb_entry(n, struct callchain_node, rb_node_in);
139 __sort_chain_graph_rel(child, min_percent);
140 if (callchain_cumul_hits(child) >= min_hit)
141 rb_insert_callchain(&node->rb_root, child,
147 sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root,
148 u64 min_hit __maybe_unused, struct callchain_param *param)
150 __sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0);
151 rb_root->rb_node = chain_root->node.rb_root.rb_node;
154 int callchain_register_param(struct callchain_param *param)
156 switch (param->mode) {
157 case CHAIN_GRAPH_ABS:
158 param->sort = sort_chain_graph_abs;
160 case CHAIN_GRAPH_REL:
161 param->sort = sort_chain_graph_rel;
164 param->sort = sort_chain_flat;
174 * Create a child for a parent. If inherit_children, then the new child
175 * will become the new parent of it's parent children
177 static struct callchain_node *
178 create_child(struct callchain_node *parent, bool inherit_children)
180 struct callchain_node *new;
182 new = zalloc(sizeof(*new));
184 perror("not enough memory to create child for code path tree");
187 new->parent = parent;
188 INIT_LIST_HEAD(&new->val);
190 if (inherit_children) {
192 struct callchain_node *child;
194 new->rb_root_in = parent->rb_root_in;
195 parent->rb_root_in = RB_ROOT;
197 n = rb_first(&new->rb_root_in);
199 child = rb_entry(n, struct callchain_node, rb_node_in);
204 /* make it the first child */
205 rb_link_node(&new->rb_node_in, NULL, &parent->rb_root_in.rb_node);
206 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
214 * Fill the node with callchain values
217 fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
219 struct callchain_cursor_node *cursor_node;
221 node->val_nr = cursor->nr - cursor->pos;
223 pr_warning("Warning: empty node in callchain tree\n");
225 cursor_node = callchain_cursor_current(cursor);
227 while (cursor_node) {
228 struct callchain_list *call;
230 call = zalloc(sizeof(*call));
232 perror("not enough memory for the code path tree");
235 call->ip = cursor_node->ip;
236 call->ms.sym = cursor_node->sym;
237 call->ms.map = cursor_node->map;
238 list_add_tail(&call->list, &node->val);
240 callchain_cursor_advance(cursor);
241 cursor_node = callchain_cursor_current(cursor);
245 static struct callchain_node *
246 add_child(struct callchain_node *parent,
247 struct callchain_cursor *cursor,
250 struct callchain_node *new;
252 new = create_child(parent, false);
253 fill_node(new, cursor);
255 new->children_hit = 0;
260 static s64 match_chain(struct callchain_cursor_node *node,
261 struct callchain_list *cnode)
263 struct symbol *sym = node->sym;
265 if (cnode->ms.sym && sym &&
266 callchain_param.key == CCKEY_FUNCTION)
267 return cnode->ms.sym->start - sym->start;
269 return cnode->ip - node->ip;
273 * Split the parent in two parts (a new child is created) and
274 * give a part of its callchain to the created child.
275 * Then create another child to host the given callchain of new branch
278 split_add_child(struct callchain_node *parent,
279 struct callchain_cursor *cursor,
280 struct callchain_list *to_split,
281 u64 idx_parents, u64 idx_local, u64 period)
283 struct callchain_node *new;
284 struct list_head *old_tail;
285 unsigned int idx_total = idx_parents + idx_local;
288 new = create_child(parent, true);
290 /* split the callchain and move a part to the new child */
291 old_tail = parent->val.prev;
292 list_del_range(&to_split->list, old_tail);
293 new->val.next = &to_split->list;
294 new->val.prev = old_tail;
295 to_split->list.prev = &new->val;
296 old_tail->next = &new->val;
299 new->hit = parent->hit;
300 new->children_hit = parent->children_hit;
301 parent->children_hit = callchain_cumul_hits(new);
302 new->val_nr = parent->val_nr - idx_local;
303 parent->val_nr = idx_local;
305 /* create a new child for the new branch if any */
306 if (idx_total < cursor->nr) {
307 struct callchain_node *first;
308 struct callchain_list *cnode;
309 struct callchain_cursor_node *node;
310 struct rb_node *p, **pp;
313 parent->children_hit += period;
315 node = callchain_cursor_current(cursor);
316 new = add_child(parent, cursor, period);
319 * This is second child since we moved parent's children
320 * to new (first) child above.
322 p = parent->rb_root_in.rb_node;
323 first = rb_entry(p, struct callchain_node, rb_node_in);
324 cnode = list_first_entry(&first->val, struct callchain_list,
327 if (match_chain(node, cnode) < 0)
332 rb_link_node(&new->rb_node_in, p, pp);
333 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
335 parent->hit = period;
340 append_chain(struct callchain_node *root,
341 struct callchain_cursor *cursor,
345 append_chain_children(struct callchain_node *root,
346 struct callchain_cursor *cursor,
349 struct callchain_node *rnode;
350 struct callchain_cursor_node *node;
351 struct rb_node **p = &root->rb_root_in.rb_node;
352 struct rb_node *parent = NULL;
354 node = callchain_cursor_current(cursor);
358 /* lookup in childrens */
361 struct callchain_list *cnode;
364 rnode = rb_entry(parent, struct callchain_node, rb_node_in);
365 cnode = list_first_entry(&rnode->val, struct callchain_list,
368 /* just check first entry */
369 ret = match_chain(node, cnode);
371 append_chain(rnode, cursor, period);
372 goto inc_children_hit;
376 p = &parent->rb_left;
378 p = &parent->rb_right;
380 /* nothing in children, add to the current node */
381 rnode = add_child(root, cursor, period);
382 rb_link_node(&rnode->rb_node_in, parent, p);
383 rb_insert_color(&rnode->rb_node_in, &root->rb_root_in);
386 root->children_hit += period;
390 append_chain(struct callchain_node *root,
391 struct callchain_cursor *cursor,
394 struct callchain_cursor_node *curr_snap = cursor->curr;
395 struct callchain_list *cnode;
396 u64 start = cursor->pos;
401 * Lookup in the current node
402 * If we have a symbol, then compare the start to match
403 * anywhere inside a function, unless function
406 list_for_each_entry(cnode, &root->val, list) {
407 struct callchain_cursor_node *node;
409 node = callchain_cursor_current(cursor);
413 if (match_chain(node, cnode) != 0)
418 callchain_cursor_advance(cursor);
421 /* matches not, relay no the parent */
423 cursor->curr = curr_snap;
428 matches = cursor->pos - start;
430 /* we match only a part of the node. Split it and add the new chain */
431 if (matches < root->val_nr) {
432 split_add_child(root, cursor, cnode, start, matches, period);
436 /* we match 100% of the path, increment the hit */
437 if (matches == root->val_nr && cursor->pos == cursor->nr) {
442 /* We match the node and still have a part remaining */
443 append_chain_children(root, cursor, period);
448 int callchain_append(struct callchain_root *root,
449 struct callchain_cursor *cursor,
455 callchain_cursor_commit(cursor);
457 append_chain_children(&root->node, cursor, period);
459 if (cursor->nr > root->max_depth)
460 root->max_depth = cursor->nr;
466 merge_chain_branch(struct callchain_cursor *cursor,
467 struct callchain_node *dst, struct callchain_node *src)
469 struct callchain_cursor_node **old_last = cursor->last;
470 struct callchain_node *child;
471 struct callchain_list *list, *next_list;
473 int old_pos = cursor->nr;
476 list_for_each_entry_safe(list, next_list, &src->val, list) {
477 callchain_cursor_append(cursor, list->ip,
478 list->ms.map, list->ms.sym);
479 list_del(&list->list);
484 callchain_cursor_commit(cursor);
485 append_chain_children(dst, cursor, src->hit);
488 n = rb_first(&src->rb_root_in);
490 child = container_of(n, struct callchain_node, rb_node_in);
492 rb_erase(&child->rb_node_in, &src->rb_root_in);
494 err = merge_chain_branch(cursor, dst, child);
501 cursor->nr = old_pos;
502 cursor->last = old_last;
507 int callchain_merge(struct callchain_cursor *cursor,
508 struct callchain_root *dst, struct callchain_root *src)
510 return merge_chain_branch(cursor, &dst->node, &src->node);
513 int callchain_cursor_append(struct callchain_cursor *cursor,
514 u64 ip, struct map *map, struct symbol *sym)
516 struct callchain_cursor_node *node = *cursor->last;
519 node = calloc(1, sizeof(*node));
523 *cursor->last = node;
532 cursor->last = &node->next;
537 int sample__resolve_callchain(struct perf_sample *sample, struct symbol **parent,
538 struct perf_evsel *evsel, struct addr_location *al,
541 if (sample->callchain == NULL)
544 if (symbol_conf.use_callchain || sort__has_parent) {
545 return machine__resolve_callchain(al->machine, evsel, al->thread,
546 sample, parent, al, max_stack);
551 int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample)
553 if (!symbol_conf.use_callchain)
555 return callchain_append(he->callchain, &callchain_cursor, sample->period);