perf probe: Show function entry line as probe-able
[safe/jmp/linux-2.6] / tools / perf / util / callchain.c
1 /*
2  * Copyright (C) 2009-2010, Frederic Weisbecker <fweisbec@gmail.com>
3  *
4  * Handle the callchains from the stream in an ad-hoc radix tree and then
5  * sort them in an rbtree.
6  *
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.
9  *
10  */
11
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <stdbool.h>
15 #include <errno.h>
16 #include <math.h>
17
18 #include "callchain.h"
19
20 #define chain_for_each_child(child, parent)     \
21         list_for_each_entry(child, &parent->children, brothers)
22
23 static void
24 rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
25                     enum chain_mode mode)
26 {
27         struct rb_node **p = &root->rb_node;
28         struct rb_node *parent = NULL;
29         struct callchain_node *rnode;
30         u64 chain_cumul = cumul_hits(chain);
31
32         while (*p) {
33                 u64 rnode_cumul;
34
35                 parent = *p;
36                 rnode = rb_entry(parent, struct callchain_node, rb_node);
37                 rnode_cumul = cumul_hits(rnode);
38
39                 switch (mode) {
40                 case CHAIN_FLAT:
41                         if (rnode->hit < chain->hit)
42                                 p = &(*p)->rb_left;
43                         else
44                                 p = &(*p)->rb_right;
45                         break;
46                 case CHAIN_GRAPH_ABS: /* Falldown */
47                 case CHAIN_GRAPH_REL:
48                         if (rnode_cumul < chain_cumul)
49                                 p = &(*p)->rb_left;
50                         else
51                                 p = &(*p)->rb_right;
52                         break;
53                 case CHAIN_NONE:
54                 default:
55                         break;
56                 }
57         }
58
59         rb_link_node(&chain->rb_node, parent, p);
60         rb_insert_color(&chain->rb_node, root);
61 }
62
63 static void
64 __sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
65                   u64 min_hit)
66 {
67         struct callchain_node *child;
68
69         chain_for_each_child(child, node)
70                 __sort_chain_flat(rb_root, child, min_hit);
71
72         if (node->hit && node->hit >= min_hit)
73                 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
74 }
75
76 /*
77  * Once we get every callchains from the stream, we can now
78  * sort them by hit
79  */
80 static void
81 sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
82                 u64 min_hit, struct callchain_param *param __used)
83 {
84         __sort_chain_flat(rb_root, node, min_hit);
85 }
86
87 static void __sort_chain_graph_abs(struct callchain_node *node,
88                                    u64 min_hit)
89 {
90         struct callchain_node *child;
91
92         node->rb_root = RB_ROOT;
93
94         chain_for_each_child(child, node) {
95                 __sort_chain_graph_abs(child, min_hit);
96                 if (cumul_hits(child) >= min_hit)
97                         rb_insert_callchain(&node->rb_root, child,
98                                             CHAIN_GRAPH_ABS);
99         }
100 }
101
102 static void
103 sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_node *chain_root,
104                      u64 min_hit, struct callchain_param *param __used)
105 {
106         __sort_chain_graph_abs(chain_root, min_hit);
107         rb_root->rb_node = chain_root->rb_root.rb_node;
108 }
109
110 static void __sort_chain_graph_rel(struct callchain_node *node,
111                                    double min_percent)
112 {
113         struct callchain_node *child;
114         u64 min_hit;
115
116         node->rb_root = RB_ROOT;
117         min_hit = ceil(node->children_hit * min_percent);
118
119         chain_for_each_child(child, node) {
120                 __sort_chain_graph_rel(child, min_percent);
121                 if (cumul_hits(child) >= min_hit)
122                         rb_insert_callchain(&node->rb_root, child,
123                                             CHAIN_GRAPH_REL);
124         }
125 }
126
127 static void
128 sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_node *chain_root,
129                      u64 min_hit __used, struct callchain_param *param)
130 {
131         __sort_chain_graph_rel(chain_root, param->min_percent / 100.0);
132         rb_root->rb_node = chain_root->rb_root.rb_node;
133 }
134
135 int register_callchain_param(struct callchain_param *param)
136 {
137         switch (param->mode) {
138         case CHAIN_GRAPH_ABS:
139                 param->sort = sort_chain_graph_abs;
140                 break;
141         case CHAIN_GRAPH_REL:
142                 param->sort = sort_chain_graph_rel;
143                 break;
144         case CHAIN_FLAT:
145                 param->sort = sort_chain_flat;
146                 break;
147         case CHAIN_NONE:
148         default:
149                 return -1;
150         }
151         return 0;
152 }
153
154 /*
155  * Create a child for a parent. If inherit_children, then the new child
156  * will become the new parent of it's parent children
157  */
158 static struct callchain_node *
159 create_child(struct callchain_node *parent, bool inherit_children)
160 {
161         struct callchain_node *new;
162
163         new = malloc(sizeof(*new));
164         if (!new) {
165                 perror("not enough memory to create child for code path tree");
166                 return NULL;
167         }
168         new->parent = parent;
169         INIT_LIST_HEAD(&new->children);
170         INIT_LIST_HEAD(&new->val);
171
172         if (inherit_children) {
173                 struct callchain_node *next;
174
175                 list_splice(&parent->children, &new->children);
176                 INIT_LIST_HEAD(&parent->children);
177
178                 chain_for_each_child(next, new)
179                         next->parent = new;
180         }
181         list_add_tail(&new->brothers, &parent->children);
182
183         return new;
184 }
185
186
187 struct resolved_ip {
188         u64               ip;
189         struct map_symbol ms;
190 };
191
192 struct resolved_chain {
193         u64                     nr;
194         struct resolved_ip      ips[0];
195 };
196
197
198 /*
199  * Fill the node with callchain values
200  */
201 static void
202 fill_node(struct callchain_node *node, struct resolved_chain *chain, int start)
203 {
204         unsigned int i;
205
206         for (i = start; i < chain->nr; i++) {
207                 struct callchain_list *call;
208
209                 call = malloc(sizeof(*call));
210                 if (!call) {
211                         perror("not enough memory for the code path tree");
212                         return;
213                 }
214                 call->ip = chain->ips[i].ip;
215                 call->ms = chain->ips[i].ms;
216                 list_add_tail(&call->list, &node->val);
217         }
218         node->val_nr = chain->nr - start;
219         if (!node->val_nr)
220                 pr_warning("Warning: empty node in callchain tree\n");
221 }
222
223 static void
224 add_child(struct callchain_node *parent, struct resolved_chain *chain,
225           int start)
226 {
227         struct callchain_node *new;
228
229         new = create_child(parent, false);
230         fill_node(new, chain, start);
231
232         new->children_hit = 0;
233         new->hit = 1;
234 }
235
236 /*
237  * Split the parent in two parts (a new child is created) and
238  * give a part of its callchain to the created child.
239  * Then create another child to host the given callchain of new branch
240  */
241 static void
242 split_add_child(struct callchain_node *parent, struct resolved_chain *chain,
243                 struct callchain_list *to_split, int idx_parents, int idx_local)
244 {
245         struct callchain_node *new;
246         struct list_head *old_tail;
247         unsigned int idx_total = idx_parents + idx_local;
248
249         /* split */
250         new = create_child(parent, true);
251
252         /* split the callchain and move a part to the new child */
253         old_tail = parent->val.prev;
254         list_del_range(&to_split->list, old_tail);
255         new->val.next = &to_split->list;
256         new->val.prev = old_tail;
257         to_split->list.prev = &new->val;
258         old_tail->next = &new->val;
259
260         /* split the hits */
261         new->hit = parent->hit;
262         new->children_hit = parent->children_hit;
263         parent->children_hit = cumul_hits(new);
264         new->val_nr = parent->val_nr - idx_local;
265         parent->val_nr = idx_local;
266
267         /* create a new child for the new branch if any */
268         if (idx_total < chain->nr) {
269                 parent->hit = 0;
270                 add_child(parent, chain, idx_total);
271                 parent->children_hit++;
272         } else {
273                 parent->hit = 1;
274         }
275 }
276
277 static int
278 __append_chain(struct callchain_node *root, struct resolved_chain *chain,
279                unsigned int start);
280
281 static void
282 __append_chain_children(struct callchain_node *root,
283                         struct resolved_chain *chain,
284                         unsigned int start)
285 {
286         struct callchain_node *rnode;
287
288         /* lookup in childrens */
289         chain_for_each_child(rnode, root) {
290                 unsigned int ret = __append_chain(rnode, chain, start);
291
292                 if (!ret)
293                         goto inc_children_hit;
294         }
295         /* nothing in children, add to the current node */
296         add_child(root, chain, start);
297
298 inc_children_hit:
299         root->children_hit++;
300 }
301
302 static int
303 __append_chain(struct callchain_node *root, struct resolved_chain *chain,
304                unsigned int start)
305 {
306         struct callchain_list *cnode;
307         unsigned int i = start;
308         bool found = false;
309
310         /*
311          * Lookup in the current node
312          * If we have a symbol, then compare the start to match
313          * anywhere inside a function.
314          */
315         list_for_each_entry(cnode, &root->val, list) {
316                 struct symbol *sym;
317
318                 if (i == chain->nr)
319                         break;
320
321                 sym = chain->ips[i].ms.sym;
322
323                 if (cnode->ms.sym && sym) {
324                         if (cnode->ms.sym->start != sym->start)
325                                 break;
326                 } else if (cnode->ip != chain->ips[i].ip)
327                         break;
328
329                 if (!found)
330                         found = true;
331                 i++;
332         }
333
334         /* matches not, relay on the parent */
335         if (!found)
336                 return -1;
337
338         /* we match only a part of the node. Split it and add the new chain */
339         if (i - start < root->val_nr) {
340                 split_add_child(root, chain, cnode, start, i - start);
341                 return 0;
342         }
343
344         /* we match 100% of the path, increment the hit */
345         if (i - start == root->val_nr && i == chain->nr) {
346                 root->hit++;
347                 return 0;
348         }
349
350         /* We match the node and still have a part remaining */
351         __append_chain_children(root, chain, i);
352
353         return 0;
354 }
355
356 static void filter_context(struct ip_callchain *old, struct resolved_chain *new,
357                            struct map_symbol *syms)
358 {
359         int i, j = 0;
360
361         for (i = 0; i < (int)old->nr; i++) {
362                 if (old->ips[i] >= PERF_CONTEXT_MAX)
363                         continue;
364
365                 new->ips[j].ip = old->ips[i];
366                 new->ips[j].ms = syms[i];
367                 j++;
368         }
369
370         new->nr = j;
371 }
372
373
374 int append_chain(struct callchain_node *root, struct ip_callchain *chain,
375                  struct map_symbol *syms)
376 {
377         struct resolved_chain *filtered;
378
379         if (!chain->nr)
380                 return 0;
381
382         filtered = malloc(sizeof(*filtered) +
383                           chain->nr * sizeof(struct resolved_ip));
384         if (!filtered)
385                 return -ENOMEM;
386
387         filter_context(chain, filtered, syms);
388
389         if (!filtered->nr)
390                 goto end;
391
392         __append_chain_children(root, filtered, 0);
393 end:
394         free(filtered);
395
396         return 0;
397 }