2 * Copyright (C) 2009-2010, 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, event_t *event)
23 unsigned int chain_size = event->header.size;
24 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
25 return chain->nr * sizeof(u64) <= chain_size;
28 #define chain_for_each_child(child, parent) \
29 list_for_each_entry(child, &parent->children, brothers)
32 rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
35 struct rb_node **p = &root->rb_node;
36 struct rb_node *parent = NULL;
37 struct callchain_node *rnode;
38 u64 chain_cumul = cumul_hits(chain);
44 rnode = rb_entry(parent, struct callchain_node, rb_node);
45 rnode_cumul = cumul_hits(rnode);
49 if (rnode->hit < chain->hit)
54 case CHAIN_GRAPH_ABS: /* Falldown */
56 if (rnode_cumul < chain_cumul)
67 rb_link_node(&chain->rb_node, parent, p);
68 rb_insert_color(&chain->rb_node, root);
72 __sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
75 struct callchain_node *child;
77 chain_for_each_child(child, node)
78 __sort_chain_flat(rb_root, child, min_hit);
80 if (node->hit && node->hit >= min_hit)
81 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
85 * Once we get every callchains from the stream, we can now
89 sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
90 u64 min_hit, struct callchain_param *param __used)
92 __sort_chain_flat(rb_root, node, min_hit);
95 static void __sort_chain_graph_abs(struct callchain_node *node,
98 struct callchain_node *child;
100 node->rb_root = RB_ROOT;
102 chain_for_each_child(child, node) {
103 __sort_chain_graph_abs(child, min_hit);
104 if (cumul_hits(child) >= min_hit)
105 rb_insert_callchain(&node->rb_root, child,
111 sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_node *chain_root,
112 u64 min_hit, struct callchain_param *param __used)
114 __sort_chain_graph_abs(chain_root, min_hit);
115 rb_root->rb_node = chain_root->rb_root.rb_node;
118 static void __sort_chain_graph_rel(struct callchain_node *node,
121 struct callchain_node *child;
124 node->rb_root = RB_ROOT;
125 min_hit = ceil(node->children_hit * min_percent);
127 chain_for_each_child(child, node) {
128 __sort_chain_graph_rel(child, min_percent);
129 if (cumul_hits(child) >= min_hit)
130 rb_insert_callchain(&node->rb_root, child,
136 sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_node *chain_root,
137 u64 min_hit __used, struct callchain_param *param)
139 __sort_chain_graph_rel(chain_root, param->min_percent / 100.0);
140 rb_root->rb_node = chain_root->rb_root.rb_node;
143 int register_callchain_param(struct callchain_param *param)
145 switch (param->mode) {
146 case CHAIN_GRAPH_ABS:
147 param->sort = sort_chain_graph_abs;
149 case CHAIN_GRAPH_REL:
150 param->sort = sort_chain_graph_rel;
153 param->sort = sort_chain_flat;
163 * Create a child for a parent. If inherit_children, then the new child
164 * will become the new parent of it's parent children
166 static struct callchain_node *
167 create_child(struct callchain_node *parent, bool inherit_children)
169 struct callchain_node *new;
171 new = zalloc(sizeof(*new));
173 perror("not enough memory to create child for code path tree");
176 new->parent = parent;
177 INIT_LIST_HEAD(&new->children);
178 INIT_LIST_HEAD(&new->val);
180 if (inherit_children) {
181 struct callchain_node *next;
183 list_splice(&parent->children, &new->children);
184 INIT_LIST_HEAD(&parent->children);
186 chain_for_each_child(next, new)
189 list_add_tail(&new->brothers, &parent->children);
197 struct map_symbol ms;
200 struct resolved_chain {
202 struct resolved_ip ips[0];
207 * Fill the node with callchain values
210 fill_node(struct callchain_node *node, struct resolved_chain *chain, int start)
214 for (i = start; i < chain->nr; i++) {
215 struct callchain_list *call;
217 call = zalloc(sizeof(*call));
219 perror("not enough memory for the code path tree");
222 call->ip = chain->ips[i].ip;
223 call->ms = chain->ips[i].ms;
224 list_add_tail(&call->list, &node->val);
226 node->val_nr = chain->nr - start;
228 pr_warning("Warning: empty node in callchain tree\n");
232 add_child(struct callchain_node *parent, struct resolved_chain *chain,
235 struct callchain_node *new;
237 new = create_child(parent, false);
238 fill_node(new, chain, start);
240 new->children_hit = 0;
245 * Split the parent in two parts (a new child is created) and
246 * give a part of its callchain to the created child.
247 * Then create another child to host the given callchain of new branch
250 split_add_child(struct callchain_node *parent, struct resolved_chain *chain,
251 struct callchain_list *to_split, int idx_parents, int idx_local)
253 struct callchain_node *new;
254 struct list_head *old_tail;
255 unsigned int idx_total = idx_parents + idx_local;
258 new = create_child(parent, true);
260 /* split the callchain and move a part to the new child */
261 old_tail = parent->val.prev;
262 list_del_range(&to_split->list, old_tail);
263 new->val.next = &to_split->list;
264 new->val.prev = old_tail;
265 to_split->list.prev = &new->val;
266 old_tail->next = &new->val;
269 new->hit = parent->hit;
270 new->children_hit = parent->children_hit;
271 parent->children_hit = cumul_hits(new);
272 new->val_nr = parent->val_nr - idx_local;
273 parent->val_nr = idx_local;
275 /* create a new child for the new branch if any */
276 if (idx_total < chain->nr) {
278 add_child(parent, chain, idx_total);
279 parent->children_hit++;
286 __append_chain(struct callchain_node *root, struct resolved_chain *chain,
290 __append_chain_children(struct callchain_node *root,
291 struct resolved_chain *chain,
294 struct callchain_node *rnode;
296 /* lookup in childrens */
297 chain_for_each_child(rnode, root) {
298 unsigned int ret = __append_chain(rnode, chain, start);
301 goto inc_children_hit;
303 /* nothing in children, add to the current node */
304 add_child(root, chain, start);
307 root->children_hit++;
311 __append_chain(struct callchain_node *root, struct resolved_chain *chain,
314 struct callchain_list *cnode;
315 unsigned int i = start;
319 * Lookup in the current node
320 * If we have a symbol, then compare the start to match
321 * anywhere inside a function.
323 list_for_each_entry(cnode, &root->val, list) {
329 sym = chain->ips[i].ms.sym;
331 if (cnode->ms.sym && sym) {
332 if (cnode->ms.sym->start != sym->start)
334 } else if (cnode->ip != chain->ips[i].ip)
342 /* matches not, relay on the parent */
346 /* we match only a part of the node. Split it and add the new chain */
347 if (i - start < root->val_nr) {
348 split_add_child(root, chain, cnode, start, i - start);
352 /* we match 100% of the path, increment the hit */
353 if (i - start == root->val_nr && i == chain->nr) {
358 /* We match the node and still have a part remaining */
359 __append_chain_children(root, chain, i);
364 static void filter_context(struct ip_callchain *old, struct resolved_chain *new,
365 struct map_symbol *syms)
369 for (i = 0; i < (int)old->nr; i++) {
370 if (old->ips[i] >= PERF_CONTEXT_MAX)
373 new->ips[j].ip = old->ips[i];
374 new->ips[j].ms = syms[i];
382 int append_chain(struct callchain_node *root, struct ip_callchain *chain,
383 struct map_symbol *syms)
385 struct resolved_chain *filtered;
390 filtered = zalloc(sizeof(*filtered) +
391 chain->nr * sizeof(struct resolved_ip));
395 filter_context(chain, filtered, syms);
400 __append_chain_children(root, filtered, 0);