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.
17 #include "callchain.h"
19 #define chain_for_each_child(child, parent) \
20 list_for_each_entry(child, &parent->children, brothers)
23 rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
26 struct rb_node **p = &root->rb_node;
27 struct rb_node *parent = NULL;
28 struct callchain_node *rnode;
32 rnode = rb_entry(parent, struct callchain_node, rb_node);
36 if (rnode->hit < chain->hit)
41 case CHAIN_GRAPH_ABS: /* Falldown */
43 if (rnode->cumul_hit < chain->cumul_hit)
53 rb_link_node(&chain->rb_node, parent, p);
54 rb_insert_color(&chain->rb_node, root);
58 __sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
61 struct callchain_node *child;
63 chain_for_each_child(child, node)
64 __sort_chain_flat(rb_root, child, min_hit);
66 if (node->hit && node->hit >= min_hit)
67 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
71 * Once we get every callchains from the stream, we can now
75 sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
76 u64 min_hit, struct callchain_param *param __used)
78 __sort_chain_flat(rb_root, node, min_hit);
81 static void __sort_chain_graph_abs(struct callchain_node *node,
84 struct callchain_node *child;
86 node->rb_root = RB_ROOT;
88 chain_for_each_child(child, node) {
89 __sort_chain_graph_abs(child, min_hit);
90 if (child->cumul_hit >= min_hit)
91 rb_insert_callchain(&node->rb_root, child,
97 sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_node *chain_root,
98 u64 min_hit, struct callchain_param *param __used)
100 __sort_chain_graph_abs(chain_root, min_hit);
101 rb_root->rb_node = chain_root->rb_root.rb_node;
104 static void __sort_chain_graph_rel(struct callchain_node *node,
107 struct callchain_node *child;
110 node->rb_root = RB_ROOT;
111 min_hit = node->cumul_hit * min_percent / 100.0;
113 chain_for_each_child(child, node) {
114 __sort_chain_graph_rel(child, min_percent);
115 if (child->cumul_hit >= min_hit)
116 rb_insert_callchain(&node->rb_root, child,
122 sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_node *chain_root,
123 u64 min_hit __used, struct callchain_param *param)
125 __sort_chain_graph_rel(chain_root, param->min_percent);
126 rb_root->rb_node = chain_root->rb_root.rb_node;
129 int register_callchain_param(struct callchain_param *param)
131 switch (param->mode) {
132 case CHAIN_GRAPH_ABS:
133 param->sort = sort_chain_graph_abs;
135 case CHAIN_GRAPH_REL:
136 param->sort = sort_chain_graph_rel;
139 param->sort = sort_chain_flat;
148 * Create a child for a parent. If inherit_children, then the new child
149 * will become the new parent of it's parent children
151 static struct callchain_node *
152 create_child(struct callchain_node *parent, bool inherit_children)
154 struct callchain_node *new;
156 new = malloc(sizeof(*new));
158 perror("not enough memory to create child for code path tree");
161 new->parent = parent;
162 INIT_LIST_HEAD(&new->children);
163 INIT_LIST_HEAD(&new->val);
165 if (inherit_children) {
166 struct callchain_node *next;
168 list_splice(&parent->children, &new->children);
169 INIT_LIST_HEAD(&parent->children);
171 chain_for_each_child(next, new)
174 list_add_tail(&new->brothers, &parent->children);
180 * Fill the node with callchain values
183 fill_node(struct callchain_node *node, struct ip_callchain *chain,
184 int start, struct symbol **syms)
188 for (i = start; i < chain->nr; i++) {
189 struct callchain_list *call;
191 call = malloc(sizeof(*call));
193 perror("not enough memory for the code path tree");
196 call->ip = chain->ips[i];
198 list_add_tail(&call->list, &node->val);
200 node->val_nr = chain->nr - start;
202 printf("Warning: empty node in callchain tree\n");
206 add_child(struct callchain_node *parent, struct ip_callchain *chain,
207 int start, struct symbol **syms)
209 struct callchain_node *new;
211 new = create_child(parent, false);
212 fill_node(new, chain, start, syms);
214 new->cumul_hit = new->hit = 1;
218 * Split the parent in two parts (a new child is created) and
219 * give a part of its callchain to the created child.
220 * Then create another child to host the given callchain of new branch
223 split_add_child(struct callchain_node *parent, struct ip_callchain *chain,
224 struct callchain_list *to_split, int idx_parents, int idx_local,
225 struct symbol **syms)
227 struct callchain_node *new;
228 struct list_head *old_tail;
229 unsigned int idx_total = idx_parents + idx_local;
232 new = create_child(parent, true);
234 /* split the callchain and move a part to the new child */
235 old_tail = parent->val.prev;
236 list_del_range(&to_split->list, old_tail);
237 new->val.next = &to_split->list;
238 new->val.prev = old_tail;
239 to_split->list.prev = &new->val;
240 old_tail->next = &new->val;
243 new->hit = parent->hit;
244 new->cumul_hit = parent->cumul_hit;
245 new->val_nr = parent->val_nr - idx_local;
246 parent->val_nr = idx_local;
248 /* create a new child for the new branch if any */
249 if (idx_total < chain->nr) {
251 add_child(parent, chain, idx_total, syms);
258 __append_chain(struct callchain_node *root, struct ip_callchain *chain,
259 unsigned int start, struct symbol **syms);
262 __append_chain_children(struct callchain_node *root, struct ip_callchain *chain,
263 struct symbol **syms, unsigned int start)
265 struct callchain_node *rnode;
267 /* lookup in childrens */
268 chain_for_each_child(rnode, root) {
269 unsigned int ret = __append_chain(rnode, chain, start, syms);
274 /* nothing in children, add to the current node */
275 add_child(root, chain, start, syms);
282 __append_chain(struct callchain_node *root, struct ip_callchain *chain,
283 unsigned int start, struct symbol **syms)
285 struct callchain_list *cnode;
286 unsigned int i = start;
290 * Lookup in the current node
291 * If we have a symbol, then compare the start to match
292 * anywhere inside a function.
294 list_for_each_entry(cnode, &root->val, list) {
297 if (cnode->sym && syms[i]) {
298 if (cnode->sym->start != syms[i]->start)
300 } else if (cnode->ip != chain->ips[i])
307 /* matches not, relay on the parent */
311 /* we match only a part of the node. Split it and add the new chain */
312 if (i - start < root->val_nr) {
313 split_add_child(root, chain, cnode, start, i - start, syms);
317 /* we match 100% of the path, increment the hit */
318 if (i - start == root->val_nr && i == chain->nr) {
325 /* We match the node and still have a part remaining */
326 __append_chain_children(root, chain, syms, i);
331 void append_chain(struct callchain_node *root, struct ip_callchain *chain,
332 struct symbol **syms)
334 __append_chain_children(root, chain, syms, 0);