aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util/callchain.c
diff options
context:
space:
mode:
authorDavid S. Miller <davem@davemloft.net>2009-08-12 20:44:53 -0400
committerDavid S. Miller <davem@davemloft.net>2009-08-12 20:44:53 -0400
commitaa11d958d1a6572eda08214d7c6a735804fe48a5 (patch)
treed025b05270ad1e010660d17eeadc6ac3c1abbd7d /tools/perf/util/callchain.c
parent07f6642ee9418e962e54cbc07471cfe2e559c568 (diff)
parent9799218ae36910af50f002a5db1802d576fffb43 (diff)
Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
Conflicts: arch/microblaze/include/asm/socket.h
Diffstat (limited to 'tools/perf/util/callchain.c')
-rw-r--r--tools/perf/util/callchain.c255
1 files changed, 208 insertions, 47 deletions
diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c
index ad3c28578961..9d3c8141b8c1 100644
--- a/tools/perf/util/callchain.c
+++ b/tools/perf/util/callchain.c
@@ -4,6 +4,9 @@
4 * Handle the callchains from the stream in an ad-hoc radix tree and then 4 * Handle the callchains from the stream in an ad-hoc radix tree and then
5 * sort them in an rbtree. 5 * sort them in an rbtree.
6 * 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 *
7 */ 10 */
8 11
9#include <stdlib.h> 12#include <stdlib.h>
@@ -13,8 +16,12 @@
13 16
14#include "callchain.h" 17#include "callchain.h"
15 18
19#define chain_for_each_child(child, parent) \
20 list_for_each_entry(child, &parent->children, brothers)
16 21
17static void rb_insert_callchain(struct rb_root *root, struct callchain_node *chain) 22static void
23rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
24 enum chain_mode mode)
18{ 25{
19 struct rb_node **p = &root->rb_node; 26 struct rb_node **p = &root->rb_node;
20 struct rb_node *parent = NULL; 27 struct rb_node *parent = NULL;
@@ -24,32 +31,125 @@ static void rb_insert_callchain(struct rb_root *root, struct callchain_node *cha
24 parent = *p; 31 parent = *p;
25 rnode = rb_entry(parent, struct callchain_node, rb_node); 32 rnode = rb_entry(parent, struct callchain_node, rb_node);
26 33
27 if (rnode->hit < chain->hit) 34 switch (mode) {
28 p = &(*p)->rb_left; 35 case CHAIN_FLAT:
29 else 36 if (rnode->hit < chain->hit)
30 p = &(*p)->rb_right; 37 p = &(*p)->rb_left;
38 else
39 p = &(*p)->rb_right;
40 break;
41 case CHAIN_GRAPH_ABS: /* Falldown */
42 case CHAIN_GRAPH_REL:
43 if (rnode->cumul_hit < chain->cumul_hit)
44 p = &(*p)->rb_left;
45 else
46 p = &(*p)->rb_right;
47 break;
48 default:
49 break;
50 }
31 } 51 }
32 52
33 rb_link_node(&chain->rb_node, parent, p); 53 rb_link_node(&chain->rb_node, parent, p);
34 rb_insert_color(&chain->rb_node, root); 54 rb_insert_color(&chain->rb_node, root);
35} 55}
36 56
57static void
58__sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
59 u64 min_hit)
60{
61 struct callchain_node *child;
62
63 chain_for_each_child(child, node)
64 __sort_chain_flat(rb_root, child, min_hit);
65
66 if (node->hit && node->hit >= min_hit)
67 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
68}
69
37/* 70/*
38 * Once we get every callchains from the stream, we can now 71 * Once we get every callchains from the stream, we can now
39 * sort them by hit 72 * sort them by hit
40 */ 73 */
41void sort_chain_to_rbtree(struct rb_root *rb_root, struct callchain_node *node) 74static void
75sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
76 u64 min_hit, struct callchain_param *param __used)
77{
78 __sort_chain_flat(rb_root, node, min_hit);
79}
80
81static void __sort_chain_graph_abs(struct callchain_node *node,
82 u64 min_hit)
83{
84 struct callchain_node *child;
85
86 node->rb_root = RB_ROOT;
87
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,
92 CHAIN_GRAPH_ABS);
93 }
94}
95
96static void
97sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_node *chain_root,
98 u64 min_hit, struct callchain_param *param __used)
99{
100 __sort_chain_graph_abs(chain_root, min_hit);
101 rb_root->rb_node = chain_root->rb_root.rb_node;
102}
103
104static void __sort_chain_graph_rel(struct callchain_node *node,
105 double min_percent)
42{ 106{
43 struct callchain_node *child; 107 struct callchain_node *child;
108 u64 min_hit;
44 109
45 list_for_each_entry(child, &node->children, brothers) 110 node->rb_root = RB_ROOT;
46 sort_chain_to_rbtree(rb_root, child); 111 min_hit = node->cumul_hit * min_percent / 100.0;
47 112
48 if (node->hit) 113 chain_for_each_child(child, node) {
49 rb_insert_callchain(rb_root, node); 114 __sort_chain_graph_rel(child, min_percent);
115 if (child->cumul_hit >= min_hit)
116 rb_insert_callchain(&node->rb_root, child,
117 CHAIN_GRAPH_REL);
118 }
50} 119}
51 120
52static struct callchain_node *create_child(struct callchain_node *parent) 121static void
122sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_node *chain_root,
123 u64 min_hit __used, struct callchain_param *param)
124{
125 __sort_chain_graph_rel(chain_root, param->min_percent);
126 rb_root->rb_node = chain_root->rb_root.rb_node;
127}
128
129int register_callchain_param(struct callchain_param *param)
130{
131 switch (param->mode) {
132 case CHAIN_GRAPH_ABS:
133 param->sort = sort_chain_graph_abs;
134 break;
135 case CHAIN_GRAPH_REL:
136 param->sort = sort_chain_graph_rel;
137 break;
138 case CHAIN_FLAT:
139 param->sort = sort_chain_flat;
140 break;
141 default:
142 return -1;
143 }
144 return 0;
145}
146
147/*
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
150 */
151static struct callchain_node *
152create_child(struct callchain_node *parent, bool inherit_children)
53{ 153{
54 struct callchain_node *new; 154 struct callchain_node *new;
55 155
@@ -61,91 +161,147 @@ static struct callchain_node *create_child(struct callchain_node *parent)
61 new->parent = parent; 161 new->parent = parent;
62 INIT_LIST_HEAD(&new->children); 162 INIT_LIST_HEAD(&new->children);
63 INIT_LIST_HEAD(&new->val); 163 INIT_LIST_HEAD(&new->val);
164
165 if (inherit_children) {
166 struct callchain_node *next;
167
168 list_splice(&parent->children, &new->children);
169 INIT_LIST_HEAD(&parent->children);
170
171 chain_for_each_child(next, new)
172 next->parent = new;
173 }
64 list_add_tail(&new->brothers, &parent->children); 174 list_add_tail(&new->brothers, &parent->children);
65 175
66 return new; 176 return new;
67} 177}
68 178
179/*
180 * Fill the node with callchain values
181 */
69static void 182static void
70fill_node(struct callchain_node *node, struct ip_callchain *chain, int start) 183fill_node(struct callchain_node *node, struct ip_callchain *chain,
184 int start, struct symbol **syms)
71{ 185{
72 int i; 186 unsigned int i;
73 187
74 for (i = start; i < chain->nr; i++) { 188 for (i = start; i < chain->nr; i++) {
75 struct callchain_list *call; 189 struct callchain_list *call;
76 190
77 call = malloc(sizeof(*chain)); 191 call = malloc(sizeof(*call));
78 if (!call) { 192 if (!call) {
79 perror("not enough memory for the code path tree"); 193 perror("not enough memory for the code path tree");
80 return; 194 return;
81 } 195 }
82 call->ip = chain->ips[i]; 196 call->ip = chain->ips[i];
197 call->sym = syms[i];
83 list_add_tail(&call->list, &node->val); 198 list_add_tail(&call->list, &node->val);
84 } 199 }
85 node->val_nr = i - start; 200 node->val_nr = chain->nr - start;
201 if (!node->val_nr)
202 printf("Warning: empty node in callchain tree\n");
86} 203}
87 204
88static void add_child(struct callchain_node *parent, struct ip_callchain *chain) 205static void
206add_child(struct callchain_node *parent, struct ip_callchain *chain,
207 int start, struct symbol **syms)
89{ 208{
90 struct callchain_node *new; 209 struct callchain_node *new;
91 210
92 new = create_child(parent); 211 new = create_child(parent, false);
93 fill_node(new, chain, parent->val_nr); 212 fill_node(new, chain, start, syms);
94 213
95 new->hit = 1; 214 new->cumul_hit = new->hit = 1;
96} 215}
97 216
217/*
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
221 */
98static void 222static void
99split_add_child(struct callchain_node *parent, struct ip_callchain *chain, 223split_add_child(struct callchain_node *parent, struct ip_callchain *chain,
100 struct callchain_list *to_split, int idx) 224 struct callchain_list *to_split, int idx_parents, int idx_local,
225 struct symbol **syms)
101{ 226{
102 struct callchain_node *new; 227 struct callchain_node *new;
228 struct list_head *old_tail;
229 unsigned int idx_total = idx_parents + idx_local;
103 230
104 /* split */ 231 /* split */
105 new = create_child(parent); 232 new = create_child(parent, true);
106 list_move_tail(&to_split->list, &new->val);
107 new->hit = parent->hit;
108 parent->hit = 0;
109 parent->val_nr = idx;
110 233
111 /* create the new one */ 234 /* split the callchain and move a part to the new child */
112 add_child(parent, chain); 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;
241
242 /* split the hits */
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;
247
248 /* create a new child for the new branch if any */
249 if (idx_total < chain->nr) {
250 parent->hit = 0;
251 add_child(parent, chain, idx_total, syms);
252 } else {
253 parent->hit = 1;
254 }
113} 255}
114 256
115static int 257static int
116__append_chain(struct callchain_node *root, struct ip_callchain *chain, 258__append_chain(struct callchain_node *root, struct ip_callchain *chain,
117 int start); 259 unsigned int start, struct symbol **syms);
118 260
119static int 261static void
120__append_chain_children(struct callchain_node *root, struct ip_callchain *chain) 262__append_chain_children(struct callchain_node *root, struct ip_callchain *chain,
263 struct symbol **syms, unsigned int start)
121{ 264{
122 struct callchain_node *rnode; 265 struct callchain_node *rnode;
123 266
124 /* lookup in childrens */ 267 /* lookup in childrens */
125 list_for_each_entry(rnode, &root->children, brothers) { 268 chain_for_each_child(rnode, root) {
126 int ret = __append_chain(rnode, chain, root->val_nr); 269 unsigned int ret = __append_chain(rnode, chain, start, syms);
270
127 if (!ret) 271 if (!ret)
128 return 0; 272 goto cumul;
129 } 273 }
130 return -1; 274 /* nothing in children, add to the current node */
275 add_child(root, chain, start, syms);
276
277cumul:
278 root->cumul_hit++;
131} 279}
132 280
133static int 281static int
134__append_chain(struct callchain_node *root, struct ip_callchain *chain, 282__append_chain(struct callchain_node *root, struct ip_callchain *chain,
135 int start) 283 unsigned int start, struct symbol **syms)
136{ 284{
137 struct callchain_list *cnode; 285 struct callchain_list *cnode;
138 int i = start; 286 unsigned int i = start;
139 bool found = false; 287 bool found = false;
140 288
141 /* lookup in the current node */ 289 /*
290 * Lookup in the current node
291 * If we have a symbol, then compare the start to match
292 * anywhere inside a function.
293 */
142 list_for_each_entry(cnode, &root->val, list) { 294 list_for_each_entry(cnode, &root->val, list) {
143 if (cnode->ip != chain->ips[i++]) 295 if (i == chain->nr)
296 break;
297 if (cnode->sym && syms[i]) {
298 if (cnode->sym->start != syms[i]->start)
299 break;
300 } else if (cnode->ip != chain->ips[i])
144 break; 301 break;
145 if (!found) 302 if (!found)
146 found = true; 303 found = true;
147 if (i == chain->nr) 304 i++;
148 break;
149 } 305 }
150 306
151 /* matches not, relay on the parent */ 307 /* matches not, relay on the parent */
@@ -153,22 +309,27 @@ __append_chain(struct callchain_node *root, struct ip_callchain *chain,
153 return -1; 309 return -1;
154 310
155 /* we match only a part of the node. Split it and add the new chain */ 311 /* we match only a part of the node. Split it and add the new chain */
156 if (i < root->val_nr) { 312 if (i - start < root->val_nr) {
157 split_add_child(root, chain, cnode, i); 313 split_add_child(root, chain, cnode, start, i - start, syms);
158 return 0; 314 return 0;
159 } 315 }
160 316
161 /* we match 100% of the path, increment the hit */ 317 /* we match 100% of the path, increment the hit */
162 if (i == root->val_nr) { 318 if (i - start == root->val_nr && i == chain->nr) {
163 root->hit++; 319 root->hit++;
320 root->cumul_hit++;
321
164 return 0; 322 return 0;
165 } 323 }
166 324
167 return __append_chain_children(root, chain); 325 /* We match the node and still have a part remaining */
326 __append_chain_children(root, chain, syms, i);
327
328 return 0;
168} 329}
169 330
170void append_chain(struct callchain_node *root, struct ip_callchain *chain) 331void append_chain(struct callchain_node *root, struct ip_callchain *chain,
332 struct symbol **syms)
171{ 333{
172 if (__append_chain_children(root, chain) == -1) 334 __append_chain_children(root, chain, syms, 0);
173 add_child(root, chain);
174} 335}