callchain.c 9.0 KB
Newer Older
1
/*
2
 * Copyright (C) 2009-2010, Frederic Weisbecker <fweisbec@gmail.com>
3 4 5 6
 *
 * Handle the callchains from the stream in an ad-hoc radix tree and then
 * sort them in an rbtree.
 *
7 8 9
 * Using a radix for code path provides a fast retrieval and factorizes
 * memory use. Also that lets us use the paths in a hierarchical graph view.
 *
10 11 12 13 14 15
 */

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <errno.h>
16
#include <math.h>
17

18
#include "util.h"
19 20
#include "callchain.h"

21 22 23 24 25 26 27
bool ip_callchain__valid(struct ip_callchain *chain, event_t *event)
{
	unsigned int chain_size = event->header.size;
	chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
	return chain->nr * sizeof(u64) <= chain_size;
}

28 29 30
#define chain_for_each_child(child, parent)	\
	list_for_each_entry(child, &parent->children, brothers)

31
static void
32 33
rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
		    enum chain_mode mode)
34 35 36 37
{
	struct rb_node **p = &root->rb_node;
	struct rb_node *parent = NULL;
	struct callchain_node *rnode;
38
	u64 chain_cumul = cumul_hits(chain);
39 40

	while (*p) {
41 42
		u64 rnode_cumul;

43 44
		parent = *p;
		rnode = rb_entry(parent, struct callchain_node, rb_node);
45
		rnode_cumul = cumul_hits(rnode);
46

47
		switch (mode) {
48
		case CHAIN_FLAT:
49 50 51 52 53
			if (rnode->hit < chain->hit)
				p = &(*p)->rb_left;
			else
				p = &(*p)->rb_right;
			break;
54 55
		case CHAIN_GRAPH_ABS: /* Falldown */
		case CHAIN_GRAPH_REL:
56
			if (rnode_cumul < chain_cumul)
57 58 59 60
				p = &(*p)->rb_left;
			else
				p = &(*p)->rb_right;
			break;
61
		case CHAIN_NONE:
62 63 64
		default:
			break;
		}
65 66 67 68 69 70
	}

	rb_link_node(&chain->rb_node, parent, p);
	rb_insert_color(&chain->rb_node, root);
}

71 72 73 74 75 76 77 78 79 80 81 82 83
static void
__sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
		  u64 min_hit)
{
	struct callchain_node *child;

	chain_for_each_child(child, node)
		__sort_chain_flat(rb_root, child, min_hit);

	if (node->hit && node->hit >= min_hit)
		rb_insert_callchain(rb_root, node, CHAIN_FLAT);
}

84 85 86 87
/*
 * Once we get every callchains from the stream, we can now
 * sort them by hit
 */
88 89 90 91 92 93 94 95 96
static void
sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
		u64 min_hit, struct callchain_param *param __used)
{
	__sort_chain_flat(rb_root, node, min_hit);
}

static void __sort_chain_graph_abs(struct callchain_node *node,
				   u64 min_hit)
97 98 99
{
	struct callchain_node *child;

100
	node->rb_root = RB_ROOT;
101

102 103
	chain_for_each_child(child, node) {
		__sort_chain_graph_abs(child, min_hit);
104
		if (cumul_hits(child) >= min_hit)
105 106 107 108 109 110 111 112 113 114 115
			rb_insert_callchain(&node->rb_root, child,
					    CHAIN_GRAPH_ABS);
	}
}

static void
sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_node *chain_root,
		     u64 min_hit, struct callchain_param *param __used)
{
	__sort_chain_graph_abs(chain_root, min_hit);
	rb_root->rb_node = chain_root->rb_root.rb_node;
116 117
}

118 119
static void __sort_chain_graph_rel(struct callchain_node *node,
				   double min_percent)
120 121
{
	struct callchain_node *child;
122
	u64 min_hit;
123 124

	node->rb_root = RB_ROOT;
125
	min_hit = ceil(node->children_hit * min_percent);
126 127

	chain_for_each_child(child, node) {
128
		__sort_chain_graph_rel(child, min_percent);
129
		if (cumul_hits(child) >= min_hit)
130 131
			rb_insert_callchain(&node->rb_root, child,
					    CHAIN_GRAPH_REL);
132 133 134
	}
}

135 136 137
static void
sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_node *chain_root,
		     u64 min_hit __used, struct callchain_param *param)
138
{
139
	__sort_chain_graph_rel(chain_root, param->min_percent / 100.0);
140
	rb_root->rb_node = chain_root->rb_root.rb_node;
141 142
}

143 144 145 146 147 148 149 150 151 152 153 154
int register_callchain_param(struct callchain_param *param)
{
	switch (param->mode) {
	case CHAIN_GRAPH_ABS:
		param->sort = sort_chain_graph_abs;
		break;
	case CHAIN_GRAPH_REL:
		param->sort = sort_chain_graph_rel;
		break;
	case CHAIN_FLAT:
		param->sort = sort_chain_flat;
		break;
155
	case CHAIN_NONE:
156 157 158 159 160 161
	default:
		return -1;
	}
	return 0;
}

162 163 164 165 166 167
/*
 * Create a child for a parent. If inherit_children, then the new child
 * will become the new parent of it's parent children
 */
static struct callchain_node *
create_child(struct callchain_node *parent, bool inherit_children)
168 169 170
{
	struct callchain_node *new;

171
	new = zalloc(sizeof(*new));
172 173 174 175 176 177 178
	if (!new) {
		perror("not enough memory to create child for code path tree");
		return NULL;
	}
	new->parent = parent;
	INIT_LIST_HEAD(&new->children);
	INIT_LIST_HEAD(&new->val);
179 180 181 182 183 184 185

	if (inherit_children) {
		struct callchain_node *next;

		list_splice(&parent->children, &new->children);
		INIT_LIST_HEAD(&parent->children);

186
		chain_for_each_child(next, new)
187 188
			next->parent = new;
	}
189 190 191 192 193
	list_add_tail(&new->brothers, &parent->children);

	return new;
}

194 195

struct resolved_ip {
196 197
	u64		  ip;
	struct map_symbol ms;
198 199 200 201 202 203 204 205
};

struct resolved_chain {
	u64			nr;
	struct resolved_ip	ips[0];
};


206 207 208
/*
 * Fill the node with callchain values
 */
209
static void
210
fill_node(struct callchain_node *node, struct resolved_chain *chain, int start)
211
{
212
	unsigned int i;
213 214 215 216

	for (i = start; i < chain->nr; i++) {
		struct callchain_list *call;

217
		call = zalloc(sizeof(*call));
218 219 220 221
		if (!call) {
			perror("not enough memory for the code path tree");
			return;
		}
222
		call->ip = chain->ips[i].ip;
223
		call->ms = chain->ips[i].ms;
224 225
		list_add_tail(&call->list, &node->val);
	}
226 227
	node->val_nr = chain->nr - start;
	if (!node->val_nr)
228
		pr_warning("Warning: empty node in callchain tree\n");
229 230
}

231
static void
232 233
add_child(struct callchain_node *parent, struct resolved_chain *chain,
	  int start)
234 235 236
{
	struct callchain_node *new;

237
	new = create_child(parent, false);
238
	fill_node(new, chain, start);
239

240 241
	new->children_hit = 0;
	new->hit = 1;
242 243
}

244 245 246 247 248
/*
 * Split the parent in two parts (a new child is created) and
 * give a part of its callchain to the created child.
 * Then create another child to host the given callchain of new branch
 */
249
static void
250 251
split_add_child(struct callchain_node *parent, struct resolved_chain *chain,
		struct callchain_list *to_split, int idx_parents, int idx_local)
252 253
{
	struct callchain_node *new;
254
	struct list_head *old_tail;
255
	unsigned int idx_total = idx_parents + idx_local;
256 257

	/* split */
258 259 260 261 262 263 264 265 266
	new = create_child(parent, true);

	/* split the callchain and move a part to the new child */
	old_tail = parent->val.prev;
	list_del_range(&to_split->list, old_tail);
	new->val.next = &to_split->list;
	new->val.prev = old_tail;
	to_split->list.prev = &new->val;
	old_tail->next = &new->val;
267

268 269
	/* split the hits */
	new->hit = parent->hit;
270 271
	new->children_hit = parent->children_hit;
	parent->children_hit = cumul_hits(new);
272 273 274 275 276 277
	new->val_nr = parent->val_nr - idx_local;
	parent->val_nr = idx_local;

	/* create a new child for the new branch if any */
	if (idx_total < chain->nr) {
		parent->hit = 0;
278
		add_child(parent, chain, idx_total);
279
		parent->children_hit++;
280 281 282
	} else {
		parent->hit = 1;
	}
283 284 285
}

static int
286 287
__append_chain(struct callchain_node *root, struct resolved_chain *chain,
	       unsigned int start);
288

289
static void
290 291 292
__append_chain_children(struct callchain_node *root,
			struct resolved_chain *chain,
			unsigned int start)
293 294 295 296
{
	struct callchain_node *rnode;

	/* lookup in childrens */
297
	chain_for_each_child(rnode, root) {
298
		unsigned int ret = __append_chain(rnode, chain, start);
299

300
		if (!ret)
301
			goto inc_children_hit;
302
	}
303
	/* nothing in children, add to the current node */
304
	add_child(root, chain, start);
305

306 307
inc_children_hit:
	root->children_hit++;
308 309 310
}

static int
311 312
__append_chain(struct callchain_node *root, struct resolved_chain *chain,
	       unsigned int start)
313 314
{
	struct callchain_list *cnode;
315
	unsigned int i = start;
316 317
	bool found = false;

318 319 320 321 322
	/*
	 * Lookup in the current node
	 * If we have a symbol, then compare the start to match
	 * anywhere inside a function.
	 */
323
	list_for_each_entry(cnode, &root->val, list) {
324 325
		struct symbol *sym;

326 327
		if (i == chain->nr)
			break;
328

329
		sym = chain->ips[i].ms.sym;
330

331 332
		if (cnode->ms.sym && sym) {
			if (cnode->ms.sym->start != sym->start)
333
				break;
334
		} else if (cnode->ip != chain->ips[i].ip)
335
			break;
336

337 338
		if (!found)
			found = true;
339
		i++;
340 341 342 343 344 345 346
	}

	/* matches not, relay on the parent */
	if (!found)
		return -1;

	/* we match only a part of the node. Split it and add the new chain */
347
	if (i - start < root->val_nr) {
348
		split_add_child(root, chain, cnode, start, i - start);
349 350 351 352
		return 0;
	}

	/* we match 100% of the path, increment the hit */
353
	if (i - start == root->val_nr && i == chain->nr) {
354 355 356 357
		root->hit++;
		return 0;
	}

358
	/* We match the node and still have a part remaining */
359
	__append_chain_children(root, chain, i);
360 361

	return 0;
362 363
}

364 365
static void filter_context(struct ip_callchain *old, struct resolved_chain *new,
			   struct map_symbol *syms)
366 367 368 369 370 371 372 373
{
	int i, j = 0;

	for (i = 0; i < (int)old->nr; i++) {
		if (old->ips[i] >= PERF_CONTEXT_MAX)
			continue;

		new->ips[j].ip = old->ips[i];
374
		new->ips[j].ms = syms[i];
375 376 377 378 379 380 381 382
		j++;
	}

	new->nr = j;
}


int append_chain(struct callchain_node *root, struct ip_callchain *chain,
383
		 struct map_symbol *syms)
384
{
385 386
	struct resolved_chain *filtered;

387
	if (!chain->nr)
388 389
		return 0;

390
	filtered = zalloc(sizeof(*filtered) +
391 392 393 394 395 396 397 398 399 400 401 402 403 404
			  chain->nr * sizeof(struct resolved_ip));
	if (!filtered)
		return -ENOMEM;

	filter_context(chain, filtered, syms);

	if (!filtered->nr)
		goto end;

	__append_chain_children(root, filtered, 0);
end:
	free(filtered);

	return 0;
405
}