callchain.c 14.3 KB
Newer Older
1
/*
2
 * Copyright (C) 2009-2011, 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 19
#include "asm/bug.h"

20
#include "hist.h"
21
#include "util.h"
22 23
#include "sort.h"
#include "machine.h"
24 25
#include "callchain.h"

26 27
__thread struct callchain_cursor callchain_cursor;

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
int
parse_callchain_report_opt(const char *arg)
{
	char *tok, *tok2;
	char *endptr;

	symbol_conf.use_callchain = true;

	if (!arg)
		return 0;

	tok = strtok((char *)arg, ",");
	if (!tok)
		return -1;

	/* get the output mode */
	if (!strncmp(tok, "graph", strlen(arg))) {
		callchain_param.mode = CHAIN_GRAPH_ABS;

	} else if (!strncmp(tok, "flat", strlen(arg))) {
		callchain_param.mode = CHAIN_FLAT;
	} else if (!strncmp(tok, "fractal", strlen(arg))) {
		callchain_param.mode = CHAIN_GRAPH_REL;
	} else if (!strncmp(tok, "none", strlen(arg))) {
		callchain_param.mode = CHAIN_NONE;
		symbol_conf.use_callchain = false;
		return 0;
	} else {
		return -1;
	}

	/* get the min percentage */
	tok = strtok(NULL, ",");
	if (!tok)
		goto setup;

	callchain_param.min_percent = strtod(tok, &endptr);
	if (tok == endptr)
		return -1;

	/* get the print limit */
	tok2 = strtok(NULL, ",");
	if (!tok2)
		goto setup;

	if (tok2[0] != 'c') {
		callchain_param.print_limit = strtoul(tok2, &endptr, 0);
		tok2 = strtok(NULL, ",");
		if (!tok2)
			goto setup;
	}

	/* get the call chain order */
	if (!strncmp(tok2, "caller", strlen("caller")))
		callchain_param.order = ORDER_CALLER;
	else if (!strncmp(tok2, "callee", strlen("callee")))
		callchain_param.order = ORDER_CALLEE;
	else
		return -1;

	/* Get the sort key */
	tok2 = strtok(NULL, ",");
	if (!tok2)
		goto setup;
	if (!strncmp(tok2, "function", strlen("function")))
		callchain_param.key = CCKEY_FUNCTION;
	else if (!strncmp(tok2, "address", strlen("address")))
		callchain_param.key = CCKEY_ADDRESS;
	else
		return -1;
setup:
	if (callchain_register_param(&callchain_param) < 0) {
		pr_err("Can't register callchain params\n");
		return -1;
	}
	return 0;
}

106
static void
107 108
rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
		    enum chain_mode mode)
109 110 111 112
{
	struct rb_node **p = &root->rb_node;
	struct rb_node *parent = NULL;
	struct callchain_node *rnode;
113
	u64 chain_cumul = callchain_cumul_hits(chain);
114 115

	while (*p) {
116 117
		u64 rnode_cumul;

118 119
		parent = *p;
		rnode = rb_entry(parent, struct callchain_node, rb_node);
120
		rnode_cumul = callchain_cumul_hits(rnode);
121

122
		switch (mode) {
123
		case CHAIN_FLAT:
124 125 126 127 128
			if (rnode->hit < chain->hit)
				p = &(*p)->rb_left;
			else
				p = &(*p)->rb_right;
			break;
129 130
		case CHAIN_GRAPH_ABS: /* Falldown */
		case CHAIN_GRAPH_REL:
131
			if (rnode_cumul < chain_cumul)
132 133 134 135
				p = &(*p)->rb_left;
			else
				p = &(*p)->rb_right;
			break;
136
		case CHAIN_NONE:
137 138 139
		default:
			break;
		}
140 141 142 143 144 145
	}

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

146 147 148 149
static void
__sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
		  u64 min_hit)
{
150
	struct rb_node *n;
151 152
	struct callchain_node *child;

153 154 155 156 157
	n = rb_first(&node->rb_root_in);
	while (n) {
		child = rb_entry(n, struct callchain_node, rb_node_in);
		n = rb_next(n);

158
		__sort_chain_flat(rb_root, child, min_hit);
159
	}
160 161 162 163 164

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

165 166 167 168
/*
 * Once we get every callchains from the stream, we can now
 * sort them by hit
 */
169
static void
170
sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root,
171
		u64 min_hit, struct callchain_param *param __maybe_unused)
172
{
173
	__sort_chain_flat(rb_root, &root->node, min_hit);
174 175 176 177
}

static void __sort_chain_graph_abs(struct callchain_node *node,
				   u64 min_hit)
178
{
179
	struct rb_node *n;
180 181
	struct callchain_node *child;

182
	node->rb_root = RB_ROOT;
183 184 185 186 187
	n = rb_first(&node->rb_root_in);

	while (n) {
		child = rb_entry(n, struct callchain_node, rb_node_in);
		n = rb_next(n);
188

189
		__sort_chain_graph_abs(child, min_hit);
190
		if (callchain_cumul_hits(child) >= min_hit)
191 192 193 194 195 196
			rb_insert_callchain(&node->rb_root, child,
					    CHAIN_GRAPH_ABS);
	}
}

static void
197
sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root,
198
		     u64 min_hit, struct callchain_param *param __maybe_unused)
199
{
200 201
	__sort_chain_graph_abs(&chain_root->node, min_hit);
	rb_root->rb_node = chain_root->node.rb_root.rb_node;
202 203
}

204 205
static void __sort_chain_graph_rel(struct callchain_node *node,
				   double min_percent)
206
{
207
	struct rb_node *n;
208
	struct callchain_node *child;
209
	u64 min_hit;
210 211

	node->rb_root = RB_ROOT;
212
	min_hit = ceil(node->children_hit * min_percent);
213

214 215 216 217 218
	n = rb_first(&node->rb_root_in);
	while (n) {
		child = rb_entry(n, struct callchain_node, rb_node_in);
		n = rb_next(n);

219
		__sort_chain_graph_rel(child, min_percent);
220
		if (callchain_cumul_hits(child) >= min_hit)
221 222
			rb_insert_callchain(&node->rb_root, child,
					    CHAIN_GRAPH_REL);
223 224 225
	}
}

226
static void
227
sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root,
228
		     u64 min_hit __maybe_unused, struct callchain_param *param)
229
{
230 231
	__sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0);
	rb_root->rb_node = chain_root->node.rb_root.rb_node;
232 233
}

234
int callchain_register_param(struct callchain_param *param)
235 236 237 238 239 240 241 242 243 244 245
{
	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;
246
	case CHAIN_NONE:
247 248 249 250 251 252
	default:
		return -1;
	}
	return 0;
}

253 254 255 256 257 258
/*
 * 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)
259 260 261
{
	struct callchain_node *new;

262
	new = zalloc(sizeof(*new));
263 264 265 266 267 268
	if (!new) {
		perror("not enough memory to create child for code path tree");
		return NULL;
	}
	new->parent = parent;
	INIT_LIST_HEAD(&new->val);
269 270

	if (inherit_children) {
271 272 273 274 275
		struct rb_node *n;
		struct callchain_node *child;

		new->rb_root_in = parent->rb_root_in;
		parent->rb_root_in = RB_ROOT;
276

277 278 279 280 281 282
		n = rb_first(&new->rb_root_in);
		while (n) {
			child = rb_entry(n, struct callchain_node, rb_node_in);
			child->parent = new;
			n = rb_next(n);
		}
283

284 285 286
		/* make it the first child */
		rb_link_node(&new->rb_node_in, NULL, &parent->rb_root_in.rb_node);
		rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
287
	}
288 289 290 291

	return new;
}

292

293 294 295
/*
 * Fill the node with callchain values
 */
296
static void
297
fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
298
{
299 300 301 302 303
	struct callchain_cursor_node *cursor_node;

	node->val_nr = cursor->nr - cursor->pos;
	if (!node->val_nr)
		pr_warning("Warning: empty node in callchain tree\n");
304

305 306 307
	cursor_node = callchain_cursor_current(cursor);

	while (cursor_node) {
308 309
		struct callchain_list *call;

310
		call = zalloc(sizeof(*call));
311 312 313 314
		if (!call) {
			perror("not enough memory for the code path tree");
			return;
		}
315 316 317
		call->ip = cursor_node->ip;
		call->ms.sym = cursor_node->sym;
		call->ms.map = cursor_node->map;
318
		list_add_tail(&call->list, &node->val);
319 320 321

		callchain_cursor_advance(cursor);
		cursor_node = callchain_cursor_current(cursor);
322 323 324
	}
}

325
static struct callchain_node *
326 327 328
add_child(struct callchain_node *parent,
	  struct callchain_cursor *cursor,
	  u64 period)
329 330 331
{
	struct callchain_node *new;

332
	new = create_child(parent, false);
333
	fill_node(new, cursor);
334

335
	new->children_hit = 0;
336
	new->hit = period;
337 338 339 340 341 342 343 344 345 346 347 348 349
	return new;
}

static s64 match_chain(struct callchain_cursor_node *node,
		      struct callchain_list *cnode)
{
	struct symbol *sym = node->sym;

	if (cnode->ms.sym && sym &&
	    callchain_param.key == CCKEY_FUNCTION)
		return cnode->ms.sym->start - sym->start;
	else
		return cnode->ip - node->ip;
350 351
}

352 353 354 355 356
/*
 * 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
 */
357
static void
358 359 360 361
split_add_child(struct callchain_node *parent,
		struct callchain_cursor *cursor,
		struct callchain_list *to_split,
		u64 idx_parents, u64 idx_local, u64 period)
362 363
{
	struct callchain_node *new;
364
	struct list_head *old_tail;
365
	unsigned int idx_total = idx_parents + idx_local;
366 367

	/* split */
368 369 370 371 372 373 374 375 376
	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;
377

378 379
	/* split the hits */
	new->hit = parent->hit;
380
	new->children_hit = parent->children_hit;
381
	parent->children_hit = callchain_cumul_hits(new);
382 383 384 385
	new->val_nr = parent->val_nr - idx_local;
	parent->val_nr = idx_local;

	/* create a new child for the new branch if any */
386
	if (idx_total < cursor->nr) {
387 388 389 390 391
		struct callchain_node *first;
		struct callchain_list *cnode;
		struct callchain_cursor_node *node;
		struct rb_node *p, **pp;

392
		parent->hit = 0;
393
		parent->children_hit += period;
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413

		node = callchain_cursor_current(cursor);
		new = add_child(parent, cursor, period);

		/*
		 * This is second child since we moved parent's children
		 * to new (first) child above.
		 */
		p = parent->rb_root_in.rb_node;
		first = rb_entry(p, struct callchain_node, rb_node_in);
		cnode = list_first_entry(&first->val, struct callchain_list,
					 list);

		if (match_chain(node, cnode) < 0)
			pp = &p->rb_left;
		else
			pp = &p->rb_right;

		rb_link_node(&new->rb_node_in, p, pp);
		rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
414
	} else {
415
		parent->hit = period;
416
	}
417 418 419
}

static int
420 421 422
append_chain(struct callchain_node *root,
	     struct callchain_cursor *cursor,
	     u64 period);
423

424
static void
425 426 427
append_chain_children(struct callchain_node *root,
		      struct callchain_cursor *cursor,
		      u64 period)
428 429
{
	struct callchain_node *rnode;
430 431 432 433 434 435 436
	struct callchain_cursor_node *node;
	struct rb_node **p = &root->rb_root_in.rb_node;
	struct rb_node *parent = NULL;

	node = callchain_cursor_current(cursor);
	if (!node)
		return;
437 438

	/* lookup in childrens */
439 440
	while (*p) {
		s64 ret;
441

442 443 444
		parent = *p;
		rnode = rb_entry(parent, struct callchain_node, rb_node_in);

445 446 447
		/* If at least first entry matches, rely to children */
		ret = append_chain(rnode, cursor, period);
		if (ret == 0)
448
			goto inc_children_hit;
449 450 451 452 453

		if (ret < 0)
			p = &parent->rb_left;
		else
			p = &parent->rb_right;
454
	}
455
	/* nothing in children, add to the current node */
456 457 458
	rnode = add_child(root, cursor, period);
	rb_link_node(&rnode->rb_node_in, parent, p);
	rb_insert_color(&rnode->rb_node_in, &root->rb_root_in);
459

460
inc_children_hit:
461
	root->children_hit += period;
462 463 464
}

static int
465 466 467
append_chain(struct callchain_node *root,
	     struct callchain_cursor *cursor,
	     u64 period)
468 469
{
	struct callchain_list *cnode;
470
	u64 start = cursor->pos;
471
	bool found = false;
472
	u64 matches;
473
	int cmp = 0;
474

475 476 477
	/*
	 * Lookup in the current node
	 * If we have a symbol, then compare the start to match
478 479
	 * anywhere inside a function, unless function
	 * mode is disabled.
480
	 */
481
	list_for_each_entry(cnode, &root->val, list) {
482
		struct callchain_cursor_node *node;
483

484 485
		node = callchain_cursor_current(cursor);
		if (!node)
486
			break;
487

488 489
		cmp = match_chain(node, cnode);
		if (cmp)
490
			break;
491

492
		found = true;
493 494

		callchain_cursor_advance(cursor);
495 496
	}

497
	/* matches not, relay no the parent */
498
	if (!found) {
499 500
		WARN_ONCE(!cmp, "Chain comparison error\n");
		return cmp;
501 502 503
	}

	matches = cursor->pos - start;
504 505

	/* we match only a part of the node. Split it and add the new chain */
506 507
	if (matches < root->val_nr) {
		split_add_child(root, cursor, cnode, start, matches, period);
508 509 510 511
		return 0;
	}

	/* we match 100% of the path, increment the hit */
512
	if (matches == root->val_nr && cursor->pos == cursor->nr) {
513
		root->hit += period;
514 515 516
		return 0;
	}

517
	/* We match the node and still have a part remaining */
518
	append_chain_children(root, cursor, period);
519 520

	return 0;
521 522
}

523 524 525
int callchain_append(struct callchain_root *root,
		     struct callchain_cursor *cursor,
		     u64 period)
526
{
527
	if (!cursor->nr)
528 529
		return 0;

530
	callchain_cursor_commit(cursor);
531

532
	append_chain_children(&root->node, cursor, period);
533

534 535
	if (cursor->nr > root->max_depth)
		root->max_depth = cursor->nr;
536 537

	return 0;
538
}
539 540

static int
541 542
merge_chain_branch(struct callchain_cursor *cursor,
		   struct callchain_node *dst, struct callchain_node *src)
543
{
544
	struct callchain_cursor_node **old_last = cursor->last;
545
	struct callchain_node *child;
546
	struct callchain_list *list, *next_list;
547
	struct rb_node *n;
548
	int old_pos = cursor->nr;
549 550 551
	int err = 0;

	list_for_each_entry_safe(list, next_list, &src->val, list) {
552 553
		callchain_cursor_append(cursor, list->ip,
					list->ms.map, list->ms.sym);
554 555 556 557
		list_del(&list->list);
		free(list);
	}

558 559 560 561
	if (src->hit) {
		callchain_cursor_commit(cursor);
		append_chain_children(dst, cursor, src->hit);
	}
562

563 564 565 566 567 568
	n = rb_first(&src->rb_root_in);
	while (n) {
		child = container_of(n, struct callchain_node, rb_node_in);
		n = rb_next(n);
		rb_erase(&child->rb_node_in, &src->rb_root_in);

569
		err = merge_chain_branch(cursor, dst, child);
570 571 572 573 574 575
		if (err)
			break;

		free(child);
	}

576 577
	cursor->nr = old_pos;
	cursor->last = old_last;
578 579 580 581

	return err;
}

582 583 584 585 586 587 588 589
int callchain_merge(struct callchain_cursor *cursor,
		    struct callchain_root *dst, struct callchain_root *src)
{
	return merge_chain_branch(cursor, &dst->node, &src->node);
}

int callchain_cursor_append(struct callchain_cursor *cursor,
			    u64 ip, struct map *map, struct symbol *sym)
590
{
591
	struct callchain_cursor_node *node = *cursor->last;
592

593
	if (!node) {
594
		node = calloc(1, sizeof(*node));
595 596
		if (!node)
			return -ENOMEM;
597

598 599
		*cursor->last = node;
	}
600

601 602 603
	node->ip = ip;
	node->map = map;
	node->sym = sym;
604

605
	cursor->nr++;
606

607 608 609
	cursor->last = &node->next;

	return 0;
610
}
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631

int sample__resolve_callchain(struct perf_sample *sample, struct symbol **parent,
			      struct perf_evsel *evsel, struct addr_location *al,
			      int max_stack)
{
	if (sample->callchain == NULL)
		return 0;

	if (symbol_conf.use_callchain || sort__has_parent) {
		return machine__resolve_callchain(al->machine, evsel, al->thread,
						  sample, parent, al, max_stack);
	}
	return 0;
}

int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample)
{
	if (!symbol_conf.use_callchain)
		return 0;
	return callchain_append(he->callchain, &callchain_cursor, sample->period);
}