blob: 01147341164280a4f5a92fe3f5a4152ef40aa03e [file] [log] [blame]
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +02001/*
2 * Copyright (C) 2009, 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 *
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +02007 * 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 *
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020010 */
11
12#include <stdlib.h>
13#include <stdio.h>
14#include <stdbool.h>
15#include <errno.h>
Frederic Weisbeckerc0a88652009-08-09 04:19:15 +020016#include <math.h>
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020017
18#include "callchain.h"
19
Frederic Weisbecker14f46542009-07-02 17:58:19 +020020#define chain_for_each_child(child, parent) \
21 list_for_each_entry(child, &parent->children, brothers)
22
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +020023static void
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020024rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
25 enum chain_mode mode)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020026{
27 struct rb_node **p = &root->rb_node;
28 struct rb_node *parent = NULL;
29 struct callchain_node *rnode;
Frederic Weisbecker19532872009-08-07 07:11:05 +020030 u64 chain_cumul = cumul_hits(chain);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020031
32 while (*p) {
Frederic Weisbecker19532872009-08-07 07:11:05 +020033 u64 rnode_cumul;
34
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020035 parent = *p;
36 rnode = rb_entry(parent, struct callchain_node, rb_node);
Frederic Weisbecker19532872009-08-07 07:11:05 +020037 rnode_cumul = cumul_hits(rnode);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020038
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020039 switch (mode) {
Frederic Weisbecker805d1272009-07-05 07:39:21 +020040 case CHAIN_FLAT:
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020041 if (rnode->hit < chain->hit)
42 p = &(*p)->rb_left;
43 else
44 p = &(*p)->rb_right;
45 break;
Frederic Weisbecker805d1272009-07-05 07:39:21 +020046 case CHAIN_GRAPH_ABS: /* Falldown */
47 case CHAIN_GRAPH_REL:
Frederic Weisbecker19532872009-08-07 07:11:05 +020048 if (rnode_cumul < chain_cumul)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020049 p = &(*p)->rb_left;
50 else
51 p = &(*p)->rb_right;
52 break;
53 default:
54 break;
55 }
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020056 }
57
58 rb_link_node(&chain->rb_node, parent, p);
59 rb_insert_color(&chain->rb_node, root);
60}
61
Frederic Weisbecker805d1272009-07-05 07:39:21 +020062static void
63__sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
64 u64 min_hit)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020065{
66 struct callchain_node *child;
67
Frederic Weisbecker14f46542009-07-02 17:58:19 +020068 chain_for_each_child(child, node)
Frederic Weisbecker805d1272009-07-05 07:39:21 +020069 __sort_chain_flat(rb_root, child, min_hit);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020070
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +020071 if (node->hit && node->hit >= min_hit)
Frederic Weisbecker805d1272009-07-05 07:39:21 +020072 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020073}
74
Frederic Weisbecker805d1272009-07-05 07:39:21 +020075/*
76 * Once we get every callchains from the stream, we can now
77 * sort them by hit
78 */
79static void
80sort_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
86static void __sort_chain_graph_abs(struct callchain_node *node,
87 u64 min_hit)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020088{
89 struct callchain_node *child;
90
91 node->rb_root = RB_ROOT;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020092
93 chain_for_each_child(child, node) {
Frederic Weisbecker805d1272009-07-05 07:39:21 +020094 __sort_chain_graph_abs(child, min_hit);
Frederic Weisbecker19532872009-08-07 07:11:05 +020095 if (cumul_hits(child) >= min_hit)
Frederic Weisbecker805d1272009-07-05 07:39:21 +020096 rb_insert_callchain(&node->rb_root, child,
97 CHAIN_GRAPH_ABS);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020098 }
99}
100
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200101static void
102sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_node *chain_root,
103 u64 min_hit, struct callchain_param *param __used)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200104{
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200105 __sort_chain_graph_abs(chain_root, min_hit);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200106 rb_root->rb_node = chain_root->rb_root.rb_node;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200107}
108
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200109static void __sort_chain_graph_rel(struct callchain_node *node,
110 double min_percent)
111{
112 struct callchain_node *child;
113 u64 min_hit;
114
115 node->rb_root = RB_ROOT;
Frederic Weisbeckerc0a88652009-08-09 04:19:15 +0200116 min_hit = ceil(node->children_hit * min_percent);
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200117
118 chain_for_each_child(child, node) {
119 __sort_chain_graph_rel(child, min_percent);
Frederic Weisbecker19532872009-08-07 07:11:05 +0200120 if (cumul_hits(child) >= min_hit)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200121 rb_insert_callchain(&node->rb_root, child,
122 CHAIN_GRAPH_REL);
123 }
124}
125
126static void
127sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_node *chain_root,
128 u64 min_hit __used, struct callchain_param *param)
129{
Frederic Weisbeckerc0a88652009-08-09 04:19:15 +0200130 __sort_chain_graph_rel(chain_root, param->min_percent / 100.0);
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200131 rb_root->rb_node = chain_root->rb_root.rb_node;
132}
133
134int register_callchain_param(struct callchain_param *param)
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;
150}
151
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200152/*
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 */
156static struct callchain_node *
157create_child(struct callchain_node *parent, bool inherit_children)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200158{
159 struct callchain_node *new;
160
161 new = malloc(sizeof(*new));
162 if (!new) {
163 perror("not enough memory to create child for code path tree");
164 return NULL;
165 }
166 new->parent = parent;
167 INIT_LIST_HEAD(&new->children);
168 INIT_LIST_HEAD(&new->val);
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200169
170 if (inherit_children) {
171 struct callchain_node *next;
172
173 list_splice(&parent->children, &new->children);
174 INIT_LIST_HEAD(&parent->children);
175
Frederic Weisbecker14f46542009-07-02 17:58:19 +0200176 chain_for_each_child(next, new)
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200177 next->parent = new;
178 }
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200179 list_add_tail(&new->brothers, &parent->children);
180
181 return new;
182}
183
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200184/*
185 * Fill the node with callchain values
186 */
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200187static void
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200188fill_node(struct callchain_node *node, struct ip_callchain *chain,
189 int start, struct symbol **syms)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200190{
Ingo Molnarf37a2912009-07-01 12:37:06 +0200191 unsigned int i;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200192
193 for (i = start; i < chain->nr; i++) {
194 struct callchain_list *call;
195
Frederic Weisbecker9198aa72009-07-01 05:35:13 +0200196 call = malloc(sizeof(*call));
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200197 if (!call) {
198 perror("not enough memory for the code path tree");
199 return;
200 }
201 call->ip = chain->ips[i];
Frederic Weisbecker44249612009-07-01 05:35:14 +0200202 call->sym = syms[i];
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200203 list_add_tail(&call->list, &node->val);
204 }
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200205 node->val_nr = chain->nr - start;
206 if (!node->val_nr)
207 printf("Warning: empty node in callchain tree\n");
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200208}
209
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200210static void
211add_child(struct callchain_node *parent, struct ip_callchain *chain,
212 int start, struct symbol **syms)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200213{
214 struct callchain_node *new;
215
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200216 new = create_child(parent, false);
217 fill_node(new, chain, start, syms);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200218
Frederic Weisbecker19532872009-08-07 07:11:05 +0200219 new->children_hit = 0;
220 new->hit = 1;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200221}
222
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200223/*
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 */
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200228static void
229split_add_child(struct callchain_node *parent, struct ip_callchain *chain,
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200230 struct callchain_list *to_split, int idx_parents, int idx_local,
231 struct symbol **syms)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200232{
233 struct callchain_node *new;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200234 struct list_head *old_tail;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200235 unsigned int idx_total = idx_parents + idx_local;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200236
237 /* split */
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200238 new = create_child(parent, true);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200239
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200240 /* split the callchain and move a part to the new child */
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;
Frederic Weisbecker19532872009-08-07 07:11:05 +0200250 new->children_hit = parent->children_hit;
251 parent->children_hit = cumul_hits(new);
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200252 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);
Frederic Weisbecker19532872009-08-07 07:11:05 +0200259 parent->children_hit++;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200260 } else {
261 parent->hit = 1;
262 }
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200263}
264
265static int
266__append_chain(struct callchain_node *root, struct ip_callchain *chain,
Ingo Molnarf37a2912009-07-01 12:37:06 +0200267 unsigned int start, struct symbol **syms);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200268
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200269static void
Frederic Weisbecker44249612009-07-01 05:35:14 +0200270__append_chain_children(struct callchain_node *root, struct ip_callchain *chain,
Ingo Molnarf37a2912009-07-01 12:37:06 +0200271 struct symbol **syms, unsigned int start)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200272{
273 struct callchain_node *rnode;
274
275 /* lookup in childrens */
Frederic Weisbecker14f46542009-07-02 17:58:19 +0200276 chain_for_each_child(rnode, root) {
Ingo Molnarf37a2912009-07-01 12:37:06 +0200277 unsigned int ret = __append_chain(rnode, chain, start, syms);
278
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200279 if (!ret)
Frederic Weisbecker19532872009-08-07 07:11:05 +0200280 goto inc_children_hit;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200281 }
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200282 /* nothing in children, add to the current node */
283 add_child(root, chain, start, syms);
Frederic Weisbeckere05b8762009-07-05 07:39:20 +0200284
Frederic Weisbecker19532872009-08-07 07:11:05 +0200285inc_children_hit:
286 root->children_hit++;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200287}
288
289static int
290__append_chain(struct callchain_node *root, struct ip_callchain *chain,
Ingo Molnarf37a2912009-07-01 12:37:06 +0200291 unsigned int start, struct symbol **syms)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200292{
293 struct callchain_list *cnode;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200294 unsigned int i = start;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200295 bool found = false;
296
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200297 /*
298 * Lookup in the current node
299 * If we have a symbol, then compare the start to match
300 * anywhere inside a function.
301 */
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200302 list_for_each_entry(cnode, &root->val, list) {
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200303 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])
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200309 break;
310 if (!found)
311 found = true;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200312 i++;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200313 }
314
315 /* matches not, relay on the parent */
316 if (!found)
317 return -1;
318
319 /* we match only a part of the node. Split it and add the new chain */
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200320 if (i - start < root->val_nr) {
321 split_add_child(root, chain, cnode, start, i - start, syms);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200322 return 0;
323 }
324
325 /* we match 100% of the path, increment the hit */
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200326 if (i - start == root->val_nr && i == chain->nr) {
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200327 root->hit++;
328 return 0;
329 }
330
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200331 /* We match the node and still have a part remaining */
332 __append_chain_children(root, chain, syms, i);
333
334 return 0;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200335}
336
Frederic Weisbecker44249612009-07-01 05:35:14 +0200337void append_chain(struct callchain_node *root, struct ip_callchain *chain,
338 struct symbol **syms)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200339{
Frederic Weisbeckerb0efe212009-08-08 02:16:23 +0200340 if (!chain->nr)
341 return;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200342 __append_chain_children(root, chain, syms, 0);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200343}