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.
24 #include "callchain.h"
26 __thread struct callchain_cursor callchain_cursor;
29 rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
32 struct rb_node **p = &root->rb_node;
33 struct rb_node *parent = NULL;
34 struct callchain_node *rnode;
35 u64 chain_cumul = callchain_cumul_hits(chain);
41 rnode = rb_entry(parent, struct callchain_node, rb_node);
42 rnode_cumul = callchain_cumul_hits(rnode);
46 if (rnode->hit < chain->hit)
51 case CHAIN_GRAPH_ABS: /* Falldown */
53 if (rnode_cumul < chain_cumul)
64 rb_link_node(&chain->rb_node, parent, p);
65 rb_insert_color(&chain->rb_node, root);
69 __sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
73 struct callchain_node *child;
75 n = rb_first(&node->rb_root_in);
77 child = rb_entry(n, struct callchain_node, rb_node_in);
80 __sort_chain_flat(rb_root, child, min_hit);
83 if (node->hit && node->hit >= min_hit)
84 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
88 * Once we get every callchains from the stream, we can now
92 sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root,
93 u64 min_hit, struct callchain_param *param __maybe_unused)
95 __sort_chain_flat(rb_root, &root->node, min_hit);
98 static void __sort_chain_graph_abs(struct callchain_node *node,
102 struct callchain_node *child;
104 node->rb_root = RB_ROOT;
105 n = rb_first(&node->rb_root_in);
108 child = rb_entry(n, struct callchain_node, rb_node_in);
111 __sort_chain_graph_abs(child, min_hit);
112 if (callchain_cumul_hits(child) >= min_hit)
113 rb_insert_callchain(&node->rb_root, child,
119 sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root,
120 u64 min_hit, struct callchain_param *param __maybe_unused)
122 __sort_chain_graph_abs(&chain_root->node, min_hit);
123 rb_root->rb_node = chain_root->node.rb_root.rb_node;
126 static void __sort_chain_graph_rel(struct callchain_node *node,
130 struct callchain_node *child;
133 node->rb_root = RB_ROOT;
134 min_hit = ceil(node->children_hit * min_percent);
136 n = rb_first(&node->rb_root_in);
138 child = rb_entry(n, struct callchain_node, rb_node_in);
141 __sort_chain_graph_rel(child, min_percent);
142 if (callchain_cumul_hits(child) >= min_hit)
143 rb_insert_callchain(&node->rb_root, child,
149 sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root,
150 u64 min_hit __maybe_unused, struct callchain_param *param)
152 __sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0);
153 rb_root->rb_node = chain_root->node.rb_root.rb_node;
156 int callchain_register_param(struct callchain_param *param)
158 switch (param->mode) {
159 case CHAIN_GRAPH_ABS:
160 param->sort = sort_chain_graph_abs;
162 case CHAIN_GRAPH_REL:
163 param->sort = sort_chain_graph_rel;
166 param->sort = sort_chain_flat;
176 * Create a child for a parent. If inherit_children, then the new child
177 * will become the new parent of it's parent children
179 static struct callchain_node *
180 create_child(struct callchain_node *parent, bool inherit_children)
182 struct callchain_node *new;
184 new = zalloc(sizeof(*new));
186 perror("not enough memory to create child for code path tree");
189 new->parent = parent;
190 INIT_LIST_HEAD(&new->val);
192 if (inherit_children) {
194 struct callchain_node *child;
196 new->rb_root_in = parent->rb_root_in;
197 parent->rb_root_in = RB_ROOT;
199 n = rb_first(&new->rb_root_in);
201 child = rb_entry(n, struct callchain_node, rb_node_in);
206 /* make it the first child */
207 rb_link_node(&new->rb_node_in, NULL, &parent->rb_root_in.rb_node);
208 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
216 * Fill the node with callchain values
219 fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
221 struct callchain_cursor_node *cursor_node;
223 node->val_nr = cursor->nr - cursor->pos;
225 pr_warning("Warning: empty node in callchain tree\n");
227 cursor_node = callchain_cursor_current(cursor);
229 while (cursor_node) {
230 struct callchain_list *call;
232 call = zalloc(sizeof(*call));
234 perror("not enough memory for the code path tree");
237 call->ip = cursor_node->ip;
238 call->ms.sym = cursor_node->sym;
239 call->ms.map = cursor_node->map;
240 list_add_tail(&call->list, &node->val);
242 callchain_cursor_advance(cursor);
243 cursor_node = callchain_cursor_current(cursor);
247 static struct callchain_node *
248 add_child(struct callchain_node *parent,
249 struct callchain_cursor *cursor,
252 struct callchain_node *new;
254 new = create_child(parent, false);
255 fill_node(new, cursor);
257 new->children_hit = 0;
262 static s64 match_chain(struct callchain_cursor_node *node,
263 struct callchain_list *cnode)
265 struct symbol *sym = node->sym;
267 if (cnode->ms.sym && sym &&
268 callchain_param.key == CCKEY_FUNCTION)
269 return cnode->ms.sym->start - sym->start;
271 return cnode->ip - node->ip;
275 * Split the parent in two parts (a new child is created) and
276 * give a part of its callchain to the created child.
277 * Then create another child to host the given callchain of new branch
280 split_add_child(struct callchain_node *parent,
281 struct callchain_cursor *cursor,
282 struct callchain_list *to_split,
283 u64 idx_parents, u64 idx_local, u64 period)
285 struct callchain_node *new;
286 struct list_head *old_tail;
287 unsigned int idx_total = idx_parents + idx_local;
290 new = create_child(parent, true);
292 /* split the callchain and move a part to the new child */
293 old_tail = parent->val.prev;
294 list_del_range(&to_split->list, old_tail);
295 new->val.next = &to_split->list;
296 new->val.prev = old_tail;
297 to_split->list.prev = &new->val;
298 old_tail->next = &new->val;
301 new->hit = parent->hit;
302 new->children_hit = parent->children_hit;
303 parent->children_hit = callchain_cumul_hits(new);
304 new->val_nr = parent->val_nr - idx_local;
305 parent->val_nr = idx_local;
307 /* create a new child for the new branch if any */
308 if (idx_total < cursor->nr) {
309 struct callchain_node *first;
310 struct callchain_list *cnode;
311 struct callchain_cursor_node *node;
312 struct rb_node *p, **pp;
315 parent->children_hit += period;
317 node = callchain_cursor_current(cursor);
318 new = add_child(parent, cursor, period);
321 * This is second child since we moved parent's children
322 * to new (first) child above.
324 p = parent->rb_root_in.rb_node;
325 first = rb_entry(p, struct callchain_node, rb_node_in);
326 cnode = list_first_entry(&first->val, struct callchain_list,
329 if (match_chain(node, cnode) < 0)
334 rb_link_node(&new->rb_node_in, p, pp);
335 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
337 parent->hit = period;
342 append_chain(struct callchain_node *root,
343 struct callchain_cursor *cursor,
347 append_chain_children(struct callchain_node *root,
348 struct callchain_cursor *cursor,
351 struct callchain_node *rnode;
352 struct callchain_cursor_node *node;
353 struct rb_node **p = &root->rb_root_in.rb_node;
354 struct rb_node *parent = NULL;
356 node = callchain_cursor_current(cursor);
360 /* lookup in childrens */
365 rnode = rb_entry(parent, struct callchain_node, rb_node_in);
367 /* If at least first entry matches, rely to children */
368 ret = append_chain(rnode, cursor, period);
370 goto inc_children_hit;
373 p = &parent->rb_left;
375 p = &parent->rb_right;
377 /* nothing in children, add to the current node */
378 rnode = add_child(root, cursor, period);
379 rb_link_node(&rnode->rb_node_in, parent, p);
380 rb_insert_color(&rnode->rb_node_in, &root->rb_root_in);
383 root->children_hit += period;
387 append_chain(struct callchain_node *root,
388 struct callchain_cursor *cursor,
391 struct callchain_list *cnode;
392 u64 start = cursor->pos;
398 * Lookup in the current node
399 * If we have a symbol, then compare the start to match
400 * anywhere inside a function, unless function
403 list_for_each_entry(cnode, &root->val, list) {
404 struct callchain_cursor_node *node;
406 node = callchain_cursor_current(cursor);
410 cmp = match_chain(node, cnode);
416 callchain_cursor_advance(cursor);
419 /* matches not, relay no the parent */
421 WARN_ONCE(!cmp, "Chain comparison error\n");
425 matches = cursor->pos - start;
427 /* we match only a part of the node. Split it and add the new chain */
428 if (matches < root->val_nr) {
429 split_add_child(root, cursor, cnode, start, matches, period);
433 /* we match 100% of the path, increment the hit */
434 if (matches == root->val_nr && cursor->pos == cursor->nr) {
439 /* We match the node and still have a part remaining */
440 append_chain_children(root, cursor, period);
445 int callchain_append(struct callchain_root *root,
446 struct callchain_cursor *cursor,
452 callchain_cursor_commit(cursor);
454 append_chain_children(&root->node, cursor, period);
456 if (cursor->nr > root->max_depth)
457 root->max_depth = cursor->nr;
463 merge_chain_branch(struct callchain_cursor *cursor,
464 struct callchain_node *dst, struct callchain_node *src)
466 struct callchain_cursor_node **old_last = cursor->last;
467 struct callchain_node *child;
468 struct callchain_list *list, *next_list;
470 int old_pos = cursor->nr;
473 list_for_each_entry_safe(list, next_list, &src->val, list) {
474 callchain_cursor_append(cursor, list->ip,
475 list->ms.map, list->ms.sym);
476 list_del(&list->list);
481 callchain_cursor_commit(cursor);
482 append_chain_children(dst, cursor, src->hit);
485 n = rb_first(&src->rb_root_in);
487 child = container_of(n, struct callchain_node, rb_node_in);
489 rb_erase(&child->rb_node_in, &src->rb_root_in);
491 err = merge_chain_branch(cursor, dst, child);
498 cursor->nr = old_pos;
499 cursor->last = old_last;
504 int callchain_merge(struct callchain_cursor *cursor,
505 struct callchain_root *dst, struct callchain_root *src)
507 return merge_chain_branch(cursor, &dst->node, &src->node);
510 int callchain_cursor_append(struct callchain_cursor *cursor,
511 u64 ip, struct map *map, struct symbol *sym)
513 struct callchain_cursor_node *node = *cursor->last;
516 node = calloc(1, sizeof(*node));
520 *cursor->last = node;
529 cursor->last = &node->next;
534 int sample__resolve_callchain(struct perf_sample *sample, struct symbol **parent,
535 struct perf_evsel *evsel, struct addr_location *al,
538 if (sample->callchain == NULL)
541 if (symbol_conf.use_callchain || sort__has_parent) {
542 return machine__resolve_callchain(al->machine, evsel, al->thread,
543 sample, parent, al, max_stack);
548 int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample)
550 if (!symbol_conf.use_callchain)
552 return callchain_append(he->callchain, &callchain_cursor, sample->period);