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