blob: d3b3f5d82137fd23cbdee5ab4686bec78802ff49 [file] [log] [blame]
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +02001/*
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01002 * Copyright (C) 2009-2011, Frederic Weisbecker <fweisbec@gmail.com>
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +02003 *
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
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -030018#include "util.h"
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020019#include "callchain.h"
20
Namhyung Kim47260642012-05-31 14:43:26 +090021__thread struct callchain_cursor callchain_cursor;
22
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -020023bool ip_callchain__valid(struct ip_callchain *chain,
24 const union perf_event *event)
Arnaldo Carvalho de Melo139633c2010-05-09 11:47:13 -030025{
26 unsigned int chain_size = event->header.size;
27 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
28 return chain->nr * sizeof(u64) <= chain_size;
29}
30
Frederic Weisbecker14f46542009-07-02 17:58:19 +020031#define chain_for_each_child(child, parent) \
Frederic Weisbecker529363b2011-01-14 04:52:01 +010032 list_for_each_entry(child, &parent->children, siblings)
Frederic Weisbecker14f46542009-07-02 17:58:19 +020033
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +020034#define chain_for_each_child_safe(child, next, parent) \
Frederic Weisbecker529363b2011-01-14 04:52:01 +010035 list_for_each_entry_safe(child, next, &parent->children, siblings)
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +020036
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +020037static void
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020038rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
39 enum chain_mode mode)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020040{
41 struct rb_node **p = &root->rb_node;
42 struct rb_node *parent = NULL;
43 struct callchain_node *rnode;
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +010044 u64 chain_cumul = callchain_cumul_hits(chain);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020045
46 while (*p) {
Frederic Weisbecker19532872009-08-07 07:11:05 +020047 u64 rnode_cumul;
48
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020049 parent = *p;
50 rnode = rb_entry(parent, struct callchain_node, rb_node);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +010051 rnode_cumul = callchain_cumul_hits(rnode);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020052
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020053 switch (mode) {
Frederic Weisbecker805d1272009-07-05 07:39:21 +020054 case CHAIN_FLAT:
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020055 if (rnode->hit < chain->hit)
56 p = &(*p)->rb_left;
57 else
58 p = &(*p)->rb_right;
59 break;
Frederic Weisbecker805d1272009-07-05 07:39:21 +020060 case CHAIN_GRAPH_ABS: /* Falldown */
61 case CHAIN_GRAPH_REL:
Frederic Weisbecker19532872009-08-07 07:11:05 +020062 if (rnode_cumul < chain_cumul)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020063 p = &(*p)->rb_left;
64 else
65 p = &(*p)->rb_right;
66 break;
Ingo Molnar83a09442009-08-15 12:26:57 +020067 case CHAIN_NONE:
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020068 default:
69 break;
70 }
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020071 }
72
73 rb_link_node(&chain->rb_node, parent, p);
74 rb_insert_color(&chain->rb_node, root);
75}
76
Frederic Weisbecker805d1272009-07-05 07:39:21 +020077static void
78__sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
79 u64 min_hit)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020080{
81 struct callchain_node *child;
82
Frederic Weisbecker14f46542009-07-02 17:58:19 +020083 chain_for_each_child(child, node)
Frederic Weisbecker805d1272009-07-05 07:39:21 +020084 __sort_chain_flat(rb_root, child, min_hit);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020085
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +020086 if (node->hit && node->hit >= min_hit)
Frederic Weisbecker805d1272009-07-05 07:39:21 +020087 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020088}
89
Frederic Weisbecker805d1272009-07-05 07:39:21 +020090/*
91 * Once we get every callchains from the stream, we can now
92 * sort them by hit
93 */
94static void
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +020095sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root,
Irina Tirdea1d037ca2012-09-11 01:15:03 +030096 u64 min_hit, struct callchain_param *param __maybe_unused)
Frederic Weisbecker805d1272009-07-05 07:39:21 +020097{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +020098 __sort_chain_flat(rb_root, &root->node, min_hit);
Frederic Weisbecker805d1272009-07-05 07:39:21 +020099}
100
101static void __sort_chain_graph_abs(struct callchain_node *node,
102 u64 min_hit)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200103{
104 struct callchain_node *child;
105
106 node->rb_root = RB_ROOT;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200107
108 chain_for_each_child(child, node) {
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200109 __sort_chain_graph_abs(child, min_hit);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100110 if (callchain_cumul_hits(child) >= min_hit)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200111 rb_insert_callchain(&node->rb_root, child,
112 CHAIN_GRAPH_ABS);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200113 }
114}
115
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200116static void
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200117sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300118 u64 min_hit, struct callchain_param *param __maybe_unused)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200119{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200120 __sort_chain_graph_abs(&chain_root->node, min_hit);
121 rb_root->rb_node = chain_root->node.rb_root.rb_node;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200122}
123
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200124static void __sort_chain_graph_rel(struct callchain_node *node,
125 double min_percent)
126{
127 struct callchain_node *child;
128 u64 min_hit;
129
130 node->rb_root = RB_ROOT;
Frederic Weisbeckerc0a88652009-08-09 04:19:15 +0200131 min_hit = ceil(node->children_hit * min_percent);
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200132
133 chain_for_each_child(child, node) {
134 __sort_chain_graph_rel(child, min_percent);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100135 if (callchain_cumul_hits(child) >= min_hit)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200136 rb_insert_callchain(&node->rb_root, child,
137 CHAIN_GRAPH_REL);
138 }
139}
140
141static void
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200142sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300143 u64 min_hit __maybe_unused, struct callchain_param *param)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200144{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200145 __sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0);
146 rb_root->rb_node = chain_root->node.rb_root.rb_node;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200147}
148
Frederic Weisbecker16537f12011-01-14 04:52:00 +0100149int callchain_register_param(struct callchain_param *param)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200150{
151 switch (param->mode) {
152 case CHAIN_GRAPH_ABS:
153 param->sort = sort_chain_graph_abs;
154 break;
155 case CHAIN_GRAPH_REL:
156 param->sort = sort_chain_graph_rel;
157 break;
158 case CHAIN_FLAT:
159 param->sort = sort_chain_flat;
160 break;
Ingo Molnar83a09442009-08-15 12:26:57 +0200161 case CHAIN_NONE:
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200162 default:
163 return -1;
164 }
165 return 0;
166}
167
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200168/*
169 * Create a child for a parent. If inherit_children, then the new child
170 * will become the new parent of it's parent children
171 */
172static struct callchain_node *
173create_child(struct callchain_node *parent, bool inherit_children)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200174{
175 struct callchain_node *new;
176
Arnaldo Carvalho de Melocdd5b75b2010-05-10 10:56:50 -0300177 new = zalloc(sizeof(*new));
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200178 if (!new) {
179 perror("not enough memory to create child for code path tree");
180 return NULL;
181 }
182 new->parent = parent;
183 INIT_LIST_HEAD(&new->children);
184 INIT_LIST_HEAD(&new->val);
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200185
186 if (inherit_children) {
187 struct callchain_node *next;
188
189 list_splice(&parent->children, &new->children);
190 INIT_LIST_HEAD(&parent->children);
191
Frederic Weisbecker14f46542009-07-02 17:58:19 +0200192 chain_for_each_child(next, new)
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200193 next->parent = new;
194 }
Frederic Weisbecker529363b2011-01-14 04:52:01 +0100195 list_add_tail(&new->siblings, &parent->children);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200196
197 return new;
198}
199
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300200
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200201/*
202 * Fill the node with callchain values
203 */
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200204static void
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100205fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200206{
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100207 struct callchain_cursor_node *cursor_node;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200208
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100209 node->val_nr = cursor->nr - cursor->pos;
210 if (!node->val_nr)
211 pr_warning("Warning: empty node in callchain tree\n");
212
213 cursor_node = callchain_cursor_current(cursor);
214
215 while (cursor_node) {
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200216 struct callchain_list *call;
217
Arnaldo Carvalho de Melocdd5b75b2010-05-10 10:56:50 -0300218 call = zalloc(sizeof(*call));
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200219 if (!call) {
220 perror("not enough memory for the code path tree");
221 return;
222 }
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100223 call->ip = cursor_node->ip;
224 call->ms.sym = cursor_node->sym;
225 call->ms.map = cursor_node->map;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200226 list_add_tail(&call->list, &node->val);
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100227
228 callchain_cursor_advance(cursor);
229 cursor_node = callchain_cursor_current(cursor);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200230 }
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200231}
232
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200233static void
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100234add_child(struct callchain_node *parent,
235 struct callchain_cursor *cursor,
236 u64 period)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200237{
238 struct callchain_node *new;
239
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200240 new = create_child(parent, false);
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100241 fill_node(new, cursor);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200242
Frederic Weisbecker19532872009-08-07 07:11:05 +0200243 new->children_hit = 0;
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200244 new->hit = period;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200245}
246
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200247/*
248 * Split the parent in two parts (a new child is created) and
249 * give a part of its callchain to the created child.
250 * Then create another child to host the given callchain of new branch
251 */
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200252static void
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100253split_add_child(struct callchain_node *parent,
254 struct callchain_cursor *cursor,
255 struct callchain_list *to_split,
256 u64 idx_parents, u64 idx_local, u64 period)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200257{
258 struct callchain_node *new;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200259 struct list_head *old_tail;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200260 unsigned int idx_total = idx_parents + idx_local;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200261
262 /* split */
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200263 new = create_child(parent, true);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200264
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200265 /* split the callchain and move a part to the new child */
266 old_tail = parent->val.prev;
267 list_del_range(&to_split->list, old_tail);
268 new->val.next = &to_split->list;
269 new->val.prev = old_tail;
270 to_split->list.prev = &new->val;
271 old_tail->next = &new->val;
272
273 /* split the hits */
274 new->hit = parent->hit;
Frederic Weisbecker19532872009-08-07 07:11:05 +0200275 new->children_hit = parent->children_hit;
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100276 parent->children_hit = callchain_cumul_hits(new);
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200277 new->val_nr = parent->val_nr - idx_local;
278 parent->val_nr = idx_local;
279
280 /* create a new child for the new branch if any */
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100281 if (idx_total < cursor->nr) {
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200282 parent->hit = 0;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100283 add_child(parent, cursor, period);
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200284 parent->children_hit += period;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200285 } else {
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200286 parent->hit = period;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200287 }
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200288}
289
290static int
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100291append_chain(struct callchain_node *root,
292 struct callchain_cursor *cursor,
293 u64 period);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200294
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200295static void
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100296append_chain_children(struct callchain_node *root,
297 struct callchain_cursor *cursor,
298 u64 period)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200299{
300 struct callchain_node *rnode;
301
302 /* lookup in childrens */
Frederic Weisbecker14f46542009-07-02 17:58:19 +0200303 chain_for_each_child(rnode, root) {
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100304 unsigned int ret = append_chain(rnode, cursor, period);
Ingo Molnarf37a2912009-07-01 12:37:06 +0200305
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200306 if (!ret)
Frederic Weisbecker19532872009-08-07 07:11:05 +0200307 goto inc_children_hit;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200308 }
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200309 /* nothing in children, add to the current node */
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100310 add_child(root, cursor, period);
Frederic Weisbeckere05b8762009-07-05 07:39:20 +0200311
Frederic Weisbecker19532872009-08-07 07:11:05 +0200312inc_children_hit:
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200313 root->children_hit += period;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200314}
315
316static int
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100317append_chain(struct callchain_node *root,
318 struct callchain_cursor *cursor,
319 u64 period)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200320{
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100321 struct callchain_cursor_node *curr_snap = cursor->curr;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200322 struct callchain_list *cnode;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100323 u64 start = cursor->pos;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200324 bool found = false;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100325 u64 matches;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200326
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200327 /*
328 * Lookup in the current node
329 * If we have a symbol, then compare the start to match
330 * anywhere inside a function.
331 */
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200332 list_for_each_entry(cnode, &root->val, list) {
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100333 struct callchain_cursor_node *node;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300334 struct symbol *sym;
335
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100336 node = callchain_cursor_current(cursor);
337 if (!node)
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200338 break;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300339
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100340 sym = node->sym;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300341
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300342 if (cnode->ms.sym && sym) {
343 if (cnode->ms.sym->start != sym->start)
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200344 break;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100345 } else if (cnode->ip != node->ip)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200346 break;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300347
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200348 if (!found)
349 found = true;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100350
351 callchain_cursor_advance(cursor);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200352 }
353
354 /* matches not, relay on the parent */
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100355 if (!found) {
356 cursor->curr = curr_snap;
357 cursor->pos = start;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200358 return -1;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100359 }
360
361 matches = cursor->pos - start;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200362
363 /* we match only a part of the node. Split it and add the new chain */
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100364 if (matches < root->val_nr) {
365 split_add_child(root, cursor, cnode, start, matches, period);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200366 return 0;
367 }
368
369 /* we match 100% of the path, increment the hit */
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100370 if (matches == root->val_nr && cursor->pos == cursor->nr) {
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200371 root->hit += period;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200372 return 0;
373 }
374
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200375 /* We match the node and still have a part remaining */
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100376 append_chain_children(root, cursor, period);
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200377
378 return 0;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200379}
380
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100381int callchain_append(struct callchain_root *root,
382 struct callchain_cursor *cursor,
383 u64 period)
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300384{
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100385 if (!cursor->nr)
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300386 return 0;
387
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100388 callchain_cursor_commit(cursor);
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300389
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100390 append_chain_children(&root->node, cursor, period);
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300391
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100392 if (cursor->nr > root->max_depth)
393 root->max_depth = cursor->nr;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300394
395 return 0;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200396}
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200397
398static int
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100399merge_chain_branch(struct callchain_cursor *cursor,
400 struct callchain_node *dst, struct callchain_node *src)
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200401{
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100402 struct callchain_cursor_node **old_last = cursor->last;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200403 struct callchain_node *child, *next_child;
404 struct callchain_list *list, *next_list;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100405 int old_pos = cursor->nr;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200406 int err = 0;
407
408 list_for_each_entry_safe(list, next_list, &src->val, list) {
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100409 callchain_cursor_append(cursor, list->ip,
410 list->ms.map, list->ms.sym);
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200411 list_del(&list->list);
412 free(list);
413 }
414
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100415 if (src->hit) {
416 callchain_cursor_commit(cursor);
417 append_chain_children(dst, cursor, src->hit);
418 }
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200419
420 chain_for_each_child_safe(child, next_child, src) {
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100421 err = merge_chain_branch(cursor, dst, child);
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200422 if (err)
423 break;
424
Frederic Weisbecker529363b2011-01-14 04:52:01 +0100425 list_del(&child->siblings);
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200426 free(child);
427 }
428
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100429 cursor->nr = old_pos;
430 cursor->last = old_last;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200431
432 return err;
433}
434
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100435int callchain_merge(struct callchain_cursor *cursor,
436 struct callchain_root *dst, struct callchain_root *src)
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200437{
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100438 return merge_chain_branch(cursor, &dst->node, &src->node);
439}
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200440
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100441int callchain_cursor_append(struct callchain_cursor *cursor,
442 u64 ip, struct map *map, struct symbol *sym)
443{
444 struct callchain_cursor_node *node = *cursor->last;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200445
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100446 if (!node) {
447 node = calloc(sizeof(*node), 1);
448 if (!node)
449 return -ENOMEM;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200450
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100451 *cursor->last = node;
452 }
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200453
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100454 node->ip = ip;
455 node->map = map;
456 node->sym = sym;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200457
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100458 cursor->nr++;
459
460 cursor->last = &node->next;
461
462 return 0;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200463}