blob: f231f43424d27930a286cb52902c21cb4534a068 [file] [log] [blame]
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +02001/*
Frederic Weisbecker301fde22010-03-22 13:09:33 -03002 * Copyright (C) 2009-2010, 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
Arnaldo Carvalho de Melo41a37e22010-06-04 08:02:07 -030021bool ip_callchain__valid(struct ip_callchain *chain, const event_t *event)
Arnaldo Carvalho de Melo139633c2010-05-09 11:47:13 -030022{
23 unsigned int chain_size = event->header.size;
24 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
25 return chain->nr * sizeof(u64) <= chain_size;
26}
27
Frederic Weisbecker14f46542009-07-02 17:58:19 +020028#define chain_for_each_child(child, parent) \
29 list_for_each_entry(child, &parent->children, brothers)
30
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +020031static void
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020032rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
33 enum chain_mode mode)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020034{
35 struct rb_node **p = &root->rb_node;
36 struct rb_node *parent = NULL;
37 struct callchain_node *rnode;
Frederic Weisbecker19532872009-08-07 07:11:05 +020038 u64 chain_cumul = cumul_hits(chain);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020039
40 while (*p) {
Frederic Weisbecker19532872009-08-07 07:11:05 +020041 u64 rnode_cumul;
42
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020043 parent = *p;
44 rnode = rb_entry(parent, struct callchain_node, rb_node);
Frederic Weisbecker19532872009-08-07 07:11:05 +020045 rnode_cumul = cumul_hits(rnode);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020046
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020047 switch (mode) {
Frederic Weisbecker805d1272009-07-05 07:39:21 +020048 case CHAIN_FLAT:
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020049 if (rnode->hit < chain->hit)
50 p = &(*p)->rb_left;
51 else
52 p = &(*p)->rb_right;
53 break;
Frederic Weisbecker805d1272009-07-05 07:39:21 +020054 case CHAIN_GRAPH_ABS: /* Falldown */
55 case CHAIN_GRAPH_REL:
Frederic Weisbecker19532872009-08-07 07:11:05 +020056 if (rnode_cumul < chain_cumul)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020057 p = &(*p)->rb_left;
58 else
59 p = &(*p)->rb_right;
60 break;
Ingo Molnar83a09442009-08-15 12:26:57 +020061 case CHAIN_NONE:
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020062 default:
63 break;
64 }
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020065 }
66
67 rb_link_node(&chain->rb_node, parent, p);
68 rb_insert_color(&chain->rb_node, root);
69}
70
Frederic Weisbecker805d1272009-07-05 07:39:21 +020071static void
72__sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
73 u64 min_hit)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020074{
75 struct callchain_node *child;
76
Frederic Weisbecker14f46542009-07-02 17:58:19 +020077 chain_for_each_child(child, node)
Frederic Weisbecker805d1272009-07-05 07:39:21 +020078 __sort_chain_flat(rb_root, child, min_hit);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020079
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +020080 if (node->hit && node->hit >= min_hit)
Frederic Weisbecker805d1272009-07-05 07:39:21 +020081 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020082}
83
Frederic Weisbecker805d1272009-07-05 07:39:21 +020084/*
85 * Once we get every callchains from the stream, we can now
86 * sort them by hit
87 */
88static void
89sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
90 u64 min_hit, struct callchain_param *param __used)
91{
92 __sort_chain_flat(rb_root, node, min_hit);
93}
94
95static void __sort_chain_graph_abs(struct callchain_node *node,
96 u64 min_hit)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020097{
98 struct callchain_node *child;
99
100 node->rb_root = RB_ROOT;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200101
102 chain_for_each_child(child, node) {
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200103 __sort_chain_graph_abs(child, min_hit);
Frederic Weisbecker19532872009-08-07 07:11:05 +0200104 if (cumul_hits(child) >= min_hit)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200105 rb_insert_callchain(&node->rb_root, child,
106 CHAIN_GRAPH_ABS);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200107 }
108}
109
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200110static void
111sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_node *chain_root,
112 u64 min_hit, struct callchain_param *param __used)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200113{
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200114 __sort_chain_graph_abs(chain_root, min_hit);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200115 rb_root->rb_node = chain_root->rb_root.rb_node;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200116}
117
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200118static void __sort_chain_graph_rel(struct callchain_node *node,
119 double min_percent)
120{
121 struct callchain_node *child;
122 u64 min_hit;
123
124 node->rb_root = RB_ROOT;
Frederic Weisbeckerc0a88652009-08-09 04:19:15 +0200125 min_hit = ceil(node->children_hit * min_percent);
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200126
127 chain_for_each_child(child, node) {
128 __sort_chain_graph_rel(child, min_percent);
Frederic Weisbecker19532872009-08-07 07:11:05 +0200129 if (cumul_hits(child) >= min_hit)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200130 rb_insert_callchain(&node->rb_root, child,
131 CHAIN_GRAPH_REL);
132 }
133}
134
135static void
136sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_node *chain_root,
137 u64 min_hit __used, struct callchain_param *param)
138{
Frederic Weisbeckerc0a88652009-08-09 04:19:15 +0200139 __sort_chain_graph_rel(chain_root, param->min_percent / 100.0);
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200140 rb_root->rb_node = chain_root->rb_root.rb_node;
141}
142
143int register_callchain_param(struct callchain_param *param)
144{
145 switch (param->mode) {
146 case CHAIN_GRAPH_ABS:
147 param->sort = sort_chain_graph_abs;
148 break;
149 case CHAIN_GRAPH_REL:
150 param->sort = sort_chain_graph_rel;
151 break;
152 case CHAIN_FLAT:
153 param->sort = sort_chain_flat;
154 break;
Ingo Molnar83a09442009-08-15 12:26:57 +0200155 case CHAIN_NONE:
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200156 default:
157 return -1;
158 }
159 return 0;
160}
161
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200162/*
163 * Create a child for a parent. If inherit_children, then the new child
164 * will become the new parent of it's parent children
165 */
166static struct callchain_node *
167create_child(struct callchain_node *parent, bool inherit_children)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200168{
169 struct callchain_node *new;
170
Arnaldo Carvalho de Melocdd5b75b2010-05-10 10:56:50 -0300171 new = zalloc(sizeof(*new));
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200172 if (!new) {
173 perror("not enough memory to create child for code path tree");
174 return NULL;
175 }
176 new->parent = parent;
177 INIT_LIST_HEAD(&new->children);
178 INIT_LIST_HEAD(&new->val);
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200179
180 if (inherit_children) {
181 struct callchain_node *next;
182
183 list_splice(&parent->children, &new->children);
184 INIT_LIST_HEAD(&parent->children);
185
Frederic Weisbecker14f46542009-07-02 17:58:19 +0200186 chain_for_each_child(next, new)
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200187 next->parent = new;
188 }
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200189 list_add_tail(&new->brothers, &parent->children);
190
191 return new;
192}
193
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300194
195struct resolved_ip {
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300196 u64 ip;
197 struct map_symbol ms;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300198};
199
200struct resolved_chain {
201 u64 nr;
202 struct resolved_ip ips[0];
203};
204
205
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200206/*
207 * Fill the node with callchain values
208 */
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200209static void
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300210fill_node(struct callchain_node *node, struct resolved_chain *chain, int start)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200211{
Ingo Molnarf37a2912009-07-01 12:37:06 +0200212 unsigned int i;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200213
214 for (i = start; i < chain->nr; i++) {
215 struct callchain_list *call;
216
Arnaldo Carvalho de Melocdd5b75b2010-05-10 10:56:50 -0300217 call = zalloc(sizeof(*call));
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200218 if (!call) {
219 perror("not enough memory for the code path tree");
220 return;
221 }
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300222 call->ip = chain->ips[i].ip;
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300223 call->ms = chain->ips[i].ms;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200224 list_add_tail(&call->list, &node->val);
225 }
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200226 node->val_nr = chain->nr - start;
227 if (!node->val_nr)
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200228 pr_warning("Warning: empty node in callchain tree\n");
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200229}
230
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200231static void
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300232add_child(struct callchain_node *parent, struct resolved_chain *chain,
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200233 int start, u64 period)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200234{
235 struct callchain_node *new;
236
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200237 new = create_child(parent, false);
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300238 fill_node(new, chain, start);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200239
Frederic Weisbecker19532872009-08-07 07:11:05 +0200240 new->children_hit = 0;
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200241 new->hit = period;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200242}
243
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200244/*
245 * Split the parent in two parts (a new child is created) and
246 * give a part of its callchain to the created child.
247 * Then create another child to host the given callchain of new branch
248 */
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200249static void
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300250split_add_child(struct callchain_node *parent, struct resolved_chain *chain,
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200251 struct callchain_list *to_split, int idx_parents, int idx_local,
252 u64 period)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200253{
254 struct callchain_node *new;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200255 struct list_head *old_tail;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200256 unsigned int idx_total = idx_parents + idx_local;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200257
258 /* split */
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200259 new = create_child(parent, true);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200260
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200261 /* split the callchain and move a part to the new child */
262 old_tail = parent->val.prev;
263 list_del_range(&to_split->list, old_tail);
264 new->val.next = &to_split->list;
265 new->val.prev = old_tail;
266 to_split->list.prev = &new->val;
267 old_tail->next = &new->val;
268
269 /* split the hits */
270 new->hit = parent->hit;
Frederic Weisbecker19532872009-08-07 07:11:05 +0200271 new->children_hit = parent->children_hit;
272 parent->children_hit = cumul_hits(new);
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200273 new->val_nr = parent->val_nr - idx_local;
274 parent->val_nr = idx_local;
275
276 /* create a new child for the new branch if any */
277 if (idx_total < chain->nr) {
278 parent->hit = 0;
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200279 add_child(parent, chain, idx_total, period);
280 parent->children_hit += period;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200281 } else {
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200282 parent->hit = period;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200283 }
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200284}
285
286static int
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300287__append_chain(struct callchain_node *root, struct resolved_chain *chain,
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200288 unsigned int start, u64 period);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200289
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200290static void
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300291__append_chain_children(struct callchain_node *root,
292 struct resolved_chain *chain,
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200293 unsigned int start, u64 period)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200294{
295 struct callchain_node *rnode;
296
297 /* lookup in childrens */
Frederic Weisbecker14f46542009-07-02 17:58:19 +0200298 chain_for_each_child(rnode, root) {
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200299 unsigned int ret = __append_chain(rnode, chain, start, period);
Ingo Molnarf37a2912009-07-01 12:37:06 +0200300
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200301 if (!ret)
Frederic Weisbecker19532872009-08-07 07:11:05 +0200302 goto inc_children_hit;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200303 }
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200304 /* nothing in children, add to the current node */
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200305 add_child(root, chain, start, period);
Frederic Weisbeckere05b8762009-07-05 07:39:20 +0200306
Frederic Weisbecker19532872009-08-07 07:11:05 +0200307inc_children_hit:
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200308 root->children_hit += period;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200309}
310
311static int
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300312__append_chain(struct callchain_node *root, struct resolved_chain *chain,
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200313 unsigned int start, u64 period)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200314{
315 struct callchain_list *cnode;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200316 unsigned int i = start;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200317 bool found = false;
318
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200319 /*
320 * Lookup in the current node
321 * If we have a symbol, then compare the start to match
322 * anywhere inside a function.
323 */
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200324 list_for_each_entry(cnode, &root->val, list) {
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300325 struct symbol *sym;
326
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200327 if (i == chain->nr)
328 break;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300329
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300330 sym = chain->ips[i].ms.sym;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300331
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300332 if (cnode->ms.sym && sym) {
333 if (cnode->ms.sym->start != sym->start)
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200334 break;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300335 } else if (cnode->ip != chain->ips[i].ip)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200336 break;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300337
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200338 if (!found)
339 found = true;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200340 i++;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200341 }
342
343 /* matches not, relay on the parent */
344 if (!found)
345 return -1;
346
347 /* we match only a part of the node. Split it and add the new chain */
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200348 if (i - start < root->val_nr) {
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200349 split_add_child(root, chain, cnode, start, i - start, period);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200350 return 0;
351 }
352
353 /* we match 100% of the path, increment the hit */
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200354 if (i - start == root->val_nr && i == chain->nr) {
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200355 root->hit += period;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200356 return 0;
357 }
358
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200359 /* We match the node and still have a part remaining */
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200360 __append_chain_children(root, chain, i, period);
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200361
362 return 0;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200363}
364
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300365static void filter_context(struct ip_callchain *old, struct resolved_chain *new,
366 struct map_symbol *syms)
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300367{
368 int i, j = 0;
369
370 for (i = 0; i < (int)old->nr; i++) {
371 if (old->ips[i] >= PERF_CONTEXT_MAX)
372 continue;
373
374 new->ips[j].ip = old->ips[i];
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300375 new->ips[j].ms = syms[i];
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300376 j++;
377 }
378
379 new->nr = j;
380}
381
382
383int append_chain(struct callchain_node *root, struct ip_callchain *chain,
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200384 struct map_symbol *syms, u64 period)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200385{
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300386 struct resolved_chain *filtered;
387
Frederic Weisbeckerb0efe212009-08-08 02:16:23 +0200388 if (!chain->nr)
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300389 return 0;
390
Arnaldo Carvalho de Melocdd5b75b2010-05-10 10:56:50 -0300391 filtered = zalloc(sizeof(*filtered) +
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300392 chain->nr * sizeof(struct resolved_ip));
393 if (!filtered)
394 return -ENOMEM;
395
396 filter_context(chain, filtered, syms);
397
398 if (!filtered->nr)
399 goto end;
400
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200401 __append_chain_children(root, filtered, 0, period);
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300402end:
403 free(filtered);
404
405 return 0;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200406}