callchain.c 30.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
int parse_callchain_record_opt(const char *arg, struct callchain_param *param)
29
{
30
	return parse_callchain_record(arg, param);
31 32
}

33 34 35 36 37 38 39 40 41 42 43 44 45 46
static int parse_callchain_mode(const char *value)
{
	if (!strncmp(value, "graph", strlen(value))) {
		callchain_param.mode = CHAIN_GRAPH_ABS;
		return 0;
	}
	if (!strncmp(value, "flat", strlen(value))) {
		callchain_param.mode = CHAIN_FLAT;
		return 0;
	}
	if (!strncmp(value, "fractal", strlen(value))) {
		callchain_param.mode = CHAIN_GRAPH_REL;
		return 0;
	}
47 48 49 50
	if (!strncmp(value, "folded", strlen(value))) {
		callchain_param.mode = CHAIN_FOLDED;
		return 0;
	}
51 52

	pr_err("Invalid callchain mode: %s\n", value);
53 54 55 56 57 58 59
	return -1;
}

static int parse_callchain_order(const char *value)
{
	if (!strncmp(value, "caller", strlen(value))) {
		callchain_param.order = ORDER_CALLER;
60
		callchain_param.order_set = true;
61 62 63 64
		return 0;
	}
	if (!strncmp(value, "callee", strlen(value))) {
		callchain_param.order = ORDER_CALLEE;
65
		callchain_param.order_set = true;
66 67
		return 0;
	}
68 69

	pr_err("Invalid callchain order: %s\n", value);
70 71 72 73 74 75 76 77 78 79 80 81 82
	return -1;
}

static int parse_callchain_sort_key(const char *value)
{
	if (!strncmp(value, "function", strlen(value))) {
		callchain_param.key = CCKEY_FUNCTION;
		return 0;
	}
	if (!strncmp(value, "address", strlen(value))) {
		callchain_param.key = CCKEY_ADDRESS;
		return 0;
	}
83 84 85 86
	if (!strncmp(value, "srcline", strlen(value))) {
		callchain_param.key = CCKEY_SRCLINE;
		return 0;
	}
87 88 89 90
	if (!strncmp(value, "branch", strlen(value))) {
		callchain_param.branch_callstack = 1;
		return 0;
	}
91 92

	pr_err("Invalid callchain sort key: %s\n", value);
93 94 95
	return -1;
}

96 97 98 99 100 101 102 103 104 105 106 107 108 109
static int parse_callchain_value(const char *value)
{
	if (!strncmp(value, "percent", strlen(value))) {
		callchain_param.value = CCVAL_PERCENT;
		return 0;
	}
	if (!strncmp(value, "period", strlen(value))) {
		callchain_param.value = CCVAL_PERIOD;
		return 0;
	}
	if (!strncmp(value, "count", strlen(value))) {
		callchain_param.value = CCVAL_COUNT;
		return 0;
	}
110 111

	pr_err("Invalid callchain config key: %s\n", value);
112 113 114
	return -1;
}

115 116
static int
__parse_callchain_report_opt(const char *arg, bool allow_record_opt)
117
{
118
	char *tok;
119
	char *endptr;
120
	bool minpcnt_set = false;
121 122
	bool record_opt_set = false;
	bool try_stack_size = false;
123

124
	callchain_param.enabled = true;
125 126 127 128 129
	symbol_conf.use_callchain = true;

	if (!arg)
		return 0;

130 131 132
	while ((tok = strtok((char *)arg, ",")) != NULL) {
		if (!strncmp(tok, "none", strlen(tok))) {
			callchain_param.mode = CHAIN_NONE;
133
			callchain_param.enabled = false;
134 135 136 137
			symbol_conf.use_callchain = false;
			return 0;
		}

138 139
		if (!parse_callchain_mode(tok) ||
		    !parse_callchain_order(tok) ||
140 141
		    !parse_callchain_sort_key(tok) ||
		    !parse_callchain_value(tok)) {
142
			/* parsing ok - move on to the next */
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
			try_stack_size = false;
			goto next;
		} else if (allow_record_opt && !record_opt_set) {
			if (parse_callchain_record(tok, &callchain_param))
				goto try_numbers;

			/* assume that number followed by 'dwarf' is stack size */
			if (callchain_param.record_mode == CALLCHAIN_DWARF)
				try_stack_size = true;

			record_opt_set = true;
			goto next;
		}

try_numbers:
		if (try_stack_size) {
			unsigned long size = 0;

			if (get_stack_size(tok, &size) < 0)
				return -1;
			callchain_param.dump_size = size;
			try_stack_size = false;
165 166
		} else if (!minpcnt_set) {
			/* try to get the min percent */
167 168 169 170 171 172 173 174 175 176
			callchain_param.min_percent = strtod(tok, &endptr);
			if (tok == endptr)
				return -1;
			minpcnt_set = true;
		} else {
			/* try print limit at last */
			callchain_param.print_limit = strtoul(tok, &endptr, 0);
			if (tok == endptr)
				return -1;
		}
177
next:
178
		arg = NULL;
179 180 181 182 183 184 185 186 187
	}

	if (callchain_register_param(&callchain_param) < 0) {
		pr_err("Can't register callchain params\n");
		return -1;
	}
	return 0;
}

188 189 190 191 192 193 194 195 196 197
int parse_callchain_report_opt(const char *arg)
{
	return __parse_callchain_report_opt(arg, false);
}

int parse_callchain_top_opt(const char *arg)
{
	return __parse_callchain_report_opt(arg, true);
}

198 199 200 201 202 203 204 205 206
int perf_callchain_config(const char *var, const char *value)
{
	char *endptr;

	if (prefixcmp(var, "call-graph."))
		return 0;
	var += sizeof("call-graph.") - 1;

	if (!strcmp(var, "record-mode"))
207
		return parse_callchain_record_opt(value, &callchain_param);
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
	if (!strcmp(var, "dump-size")) {
		unsigned long size = 0;
		int ret;

		ret = get_stack_size(value, &size);
		callchain_param.dump_size = size;

		return ret;
	}
	if (!strcmp(var, "print-type"))
		return parse_callchain_mode(value);
	if (!strcmp(var, "order"))
		return parse_callchain_order(value);
	if (!strcmp(var, "sort-key"))
		return parse_callchain_sort_key(value);
	if (!strcmp(var, "threshold")) {
		callchain_param.min_percent = strtod(value, &endptr);
225 226
		if (value == endptr) {
			pr_err("Invalid callchain threshold: %s\n", value);
227
			return -1;
228
		}
229 230 231
	}
	if (!strcmp(var, "print-limit")) {
		callchain_param.print_limit = strtod(value, &endptr);
232 233
		if (value == endptr) {
			pr_err("Invalid callchain print limit: %s\n", value);
234
			return -1;
235
		}
236 237 238 239 240
	}

	return 0;
}

241
static void
242 243
rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
		    enum chain_mode mode)
244 245 246 247
{
	struct rb_node **p = &root->rb_node;
	struct rb_node *parent = NULL;
	struct callchain_node *rnode;
248
	u64 chain_cumul = callchain_cumul_hits(chain);
249 250

	while (*p) {
251 252
		u64 rnode_cumul;

253 254
		parent = *p;
		rnode = rb_entry(parent, struct callchain_node, rb_node);
255
		rnode_cumul = callchain_cumul_hits(rnode);
256

257
		switch (mode) {
258
		case CHAIN_FLAT:
259
		case CHAIN_FOLDED:
260 261 262 263 264
			if (rnode->hit < chain->hit)
				p = &(*p)->rb_left;
			else
				p = &(*p)->rb_right;
			break;
265 266
		case CHAIN_GRAPH_ABS: /* Falldown */
		case CHAIN_GRAPH_REL:
267
			if (rnode_cumul < chain_cumul)
268 269 270 271
				p = &(*p)->rb_left;
			else
				p = &(*p)->rb_right;
			break;
272
		case CHAIN_NONE:
273 274 275
		default:
			break;
		}
276 277 278 279 280 281
	}

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

282 283 284 285
static void
__sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
		  u64 min_hit)
{
286
	struct rb_node *n;
287 288
	struct callchain_node *child;

289 290 291 292 293
	n = rb_first(&node->rb_root_in);
	while (n) {
		child = rb_entry(n, struct callchain_node, rb_node_in);
		n = rb_next(n);

294
		__sort_chain_flat(rb_root, child, min_hit);
295
	}
296 297 298 299 300

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

301 302 303 304
/*
 * Once we get every callchains from the stream, we can now
 * sort them by hit
 */
305
static void
306
sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root,
307
		u64 min_hit, struct callchain_param *param __maybe_unused)
308
{
309
	*rb_root = RB_ROOT;
310
	__sort_chain_flat(rb_root, &root->node, min_hit);
311 312 313 314
}

static void __sort_chain_graph_abs(struct callchain_node *node,
				   u64 min_hit)
315
{
316
	struct rb_node *n;
317 318
	struct callchain_node *child;

319
	node->rb_root = RB_ROOT;
320 321 322 323 324
	n = rb_first(&node->rb_root_in);

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

326
		__sort_chain_graph_abs(child, min_hit);
327
		if (callchain_cumul_hits(child) >= min_hit)
328 329 330 331 332 333
			rb_insert_callchain(&node->rb_root, child,
					    CHAIN_GRAPH_ABS);
	}
}

static void
334
sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root,
335
		     u64 min_hit, struct callchain_param *param __maybe_unused)
336
{
337 338
	__sort_chain_graph_abs(&chain_root->node, min_hit);
	rb_root->rb_node = chain_root->node.rb_root.rb_node;
339 340
}

341 342
static void __sort_chain_graph_rel(struct callchain_node *node,
				   double min_percent)
343
{
344
	struct rb_node *n;
345
	struct callchain_node *child;
346
	u64 min_hit;
347 348

	node->rb_root = RB_ROOT;
349
	min_hit = ceil(node->children_hit * min_percent);
350

351 352 353 354 355
	n = rb_first(&node->rb_root_in);
	while (n) {
		child = rb_entry(n, struct callchain_node, rb_node_in);
		n = rb_next(n);

356
		__sort_chain_graph_rel(child, min_percent);
357
		if (callchain_cumul_hits(child) >= min_hit)
358 359
			rb_insert_callchain(&node->rb_root, child,
					    CHAIN_GRAPH_REL);
360 361 362
	}
}

363
static void
364
sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root,
365
		     u64 min_hit __maybe_unused, struct callchain_param *param)
366
{
367 368
	__sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0);
	rb_root->rb_node = chain_root->node.rb_root.rb_node;
369 370
}

371
int callchain_register_param(struct callchain_param *param)
372 373 374 375 376 377 378 379 380
{
	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:
381
	case CHAIN_FOLDED:
382 383
		param->sort = sort_chain_flat;
		break;
384
	case CHAIN_NONE:
385 386 387 388 389 390
	default:
		return -1;
	}
	return 0;
}

391 392 393 394 395 396
/*
 * 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)
397 398 399
{
	struct callchain_node *new;

400
	new = zalloc(sizeof(*new));
401 402 403 404 405 406
	if (!new) {
		perror("not enough memory to create child for code path tree");
		return NULL;
	}
	new->parent = parent;
	INIT_LIST_HEAD(&new->val);
407
	INIT_LIST_HEAD(&new->parent_val);
408 409

	if (inherit_children) {
410 411 412 413 414
		struct rb_node *n;
		struct callchain_node *child;

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

416 417 418 419 420 421
		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);
		}
422

423 424 425
		/* 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);
426
	}
427 428 429 430

	return new;
}

431

432 433 434
/*
 * Fill the node with callchain values
 */
435
static int
436
fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
437
{
438 439 440 441 442
	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");
443

444 445 446
	cursor_node = callchain_cursor_current(cursor);

	while (cursor_node) {
447 448
		struct callchain_list *call;

449
		call = zalloc(sizeof(*call));
450 451
		if (!call) {
			perror("not enough memory for the code path tree");
452
			return -1;
453
		}
454 455
		call->ip = cursor_node->ip;
		call->ms.sym = cursor_node->sym;
456
		call->ms.map = map__get(cursor_node->map);
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471

		if (cursor_node->branch) {
			call->branch_count = 1;

			if (cursor_node->branch_flags.predicted)
				call->predicted_count = 1;

			if (cursor_node->branch_flags.abort)
				call->abort_count = 1;

			call->cycles_count = cursor_node->branch_flags.cycles;
			call->iter_count = cursor_node->nr_loop_iter;
			call->samples_count = cursor_node->samples;
		}

472
		list_add_tail(&call->list, &node->val);
473 474 475

		callchain_cursor_advance(cursor);
		cursor_node = callchain_cursor_current(cursor);
476
	}
477
	return 0;
478 479
}

480
static struct callchain_node *
481 482 483
add_child(struct callchain_node *parent,
	  struct callchain_cursor *cursor,
	  u64 period)
484 485 486
{
	struct callchain_node *new;

487
	new = create_child(parent, false);
488 489 490
	if (new == NULL)
		return NULL;

491 492 493 494 495
	if (fill_node(new, cursor) < 0) {
		struct callchain_list *call, *tmp;

		list_for_each_entry_safe(call, tmp, &new->val, list) {
			list_del(&call->list);
496
			map__zput(call->ms.map);
497 498 499 500 501
			free(call);
		}
		free(new);
		return NULL;
	}
502

503
	new->children_hit = 0;
504
	new->hit = period;
505 506
	new->children_count = 0;
	new->count = 1;
507 508 509
	return new;
}

510 511 512 513 514 515 516
enum match_result {
	MATCH_ERROR  = -1,
	MATCH_EQ,
	MATCH_LT,
	MATCH_GT,
};

517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
static enum match_result match_chain_srcline(struct callchain_cursor_node *node,
					     struct callchain_list *cnode)
{
	char *left = get_srcline(cnode->ms.map->dso,
				 map__rip_2objdump(cnode->ms.map, cnode->ip),
				 cnode->ms.sym, true, false);
	char *right = get_srcline(node->map->dso,
				  map__rip_2objdump(node->map, node->ip),
				  node->sym, true, false);
	enum match_result ret = MATCH_EQ;
	int cmp;

	if (left && right)
		cmp = strcmp(left, right);
	else if (!left && right)
		cmp = 1;
	else if (left && !right)
		cmp = -1;
	else if (cnode->ip == node->ip)
		cmp = 0;
	else
		cmp = (cnode->ip < node->ip) ? -1 : 1;

	if (cmp != 0)
		ret = cmp < 0 ? MATCH_LT : MATCH_GT;

	free_srcline(left);
	free_srcline(right);
	return ret;
}

548 549
static enum match_result match_chain(struct callchain_cursor_node *node,
				     struct callchain_list *cnode)
550 551
{
	struct symbol *sym = node->sym;
552
	u64 left, right;
553

554 555 556 557 558 559 560 561
	if (callchain_param.key == CCKEY_SRCLINE) {
		enum match_result match = match_chain_srcline(node, cnode);

		if (match != MATCH_ERROR)
			return match;
	}

	if (cnode->ms.sym && sym && callchain_param.key == CCKEY_FUNCTION) {
562 563 564 565 566 567 568
		left = cnode->ms.sym->start;
		right = sym->start;
	} else {
		left = cnode->ip;
		right = node->ip;
	}

569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
	if (left == right) {
		if (node->branch) {
			cnode->branch_count++;

			if (node->branch_flags.predicted)
				cnode->predicted_count++;

			if (node->branch_flags.abort)
				cnode->abort_count++;

			cnode->cycles_count += node->branch_flags.cycles;
			cnode->iter_count += node->nr_loop_iter;
			cnode->samples_count += node->samples;
		}

584
		return MATCH_EQ;
585
	}
586 587

	return left > right ? MATCH_GT : MATCH_LT;
588 589
}

590 591 592 593 594
/*
 * 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
 */
595
static int
596 597 598 599
split_add_child(struct callchain_node *parent,
		struct callchain_cursor *cursor,
		struct callchain_list *to_split,
		u64 idx_parents, u64 idx_local, u64 period)
600 601
{
	struct callchain_node *new;
602
	struct list_head *old_tail;
603
	unsigned int idx_total = idx_parents + idx_local;
604 605

	/* split */
606
	new = create_child(parent, true);
607 608
	if (new == NULL)
		return -1;
609 610 611 612 613 614 615 616

	/* 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;
617

618 619
	/* split the hits */
	new->hit = parent->hit;
620
	new->children_hit = parent->children_hit;
621
	parent->children_hit = callchain_cumul_hits(new);
622 623
	new->val_nr = parent->val_nr - idx_local;
	parent->val_nr = idx_local;
624 625 626
	new->count = parent->count;
	new->children_count = parent->children_count;
	parent->children_count = callchain_cumul_counts(new);
627 628

	/* create a new child for the new branch if any */
629
	if (idx_total < cursor->nr) {
630 631 632 633 634
		struct callchain_node *first;
		struct callchain_list *cnode;
		struct callchain_cursor_node *node;
		struct rb_node *p, **pp;

635
		parent->hit = 0;
636
		parent->children_hit += period;
637 638
		parent->count = 0;
		parent->children_count += 1;
639 640 641

		node = callchain_cursor_current(cursor);
		new = add_child(parent, cursor, period);
642
		if (new == NULL)
643
			return -1;
644 645 646 647 648 649 650 651 652 653

		/*
		 * 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);

654
		if (match_chain(node, cnode) == MATCH_LT)
655 656 657 658 659 660
			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);
661
	} else {
662
		parent->hit = period;
663
		parent->count = 1;
664
	}
665
	return 0;
666 667
}

668
static enum match_result
669 670 671
append_chain(struct callchain_node *root,
	     struct callchain_cursor *cursor,
	     u64 period);
672

673
static int
674 675 676
append_chain_children(struct callchain_node *root,
		      struct callchain_cursor *cursor,
		      u64 period)
677 678
{
	struct callchain_node *rnode;
679 680 681 682 683 684
	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)
685
		return -1;
686 687

	/* lookup in childrens */
688
	while (*p) {
689
		enum match_result ret;
690

691 692 693
		parent = *p;
		rnode = rb_entry(parent, struct callchain_node, rb_node_in);

694 695
		/* If at least first entry matches, rely to children */
		ret = append_chain(rnode, cursor, period);
696
		if (ret == MATCH_EQ)
697
			goto inc_children_hit;
698 699
		if (ret == MATCH_ERROR)
			return -1;
700

701
		if (ret == MATCH_LT)
702 703 704
			p = &parent->rb_left;
		else
			p = &parent->rb_right;
705
	}
706
	/* nothing in children, add to the current node */
707
	rnode = add_child(root, cursor, period);
708
	if (rnode == NULL)
709
		return -1;
710

711 712
	rb_link_node(&rnode->rb_node_in, parent, p);
	rb_insert_color(&rnode->rb_node_in, &root->rb_root_in);
713

714
inc_children_hit:
715
	root->children_hit += period;
716
	root->children_count++;
717
	return 0;
718 719
}

720
static enum match_result
721 722 723
append_chain(struct callchain_node *root,
	     struct callchain_cursor *cursor,
	     u64 period)
724 725
{
	struct callchain_list *cnode;
726
	u64 start = cursor->pos;
727
	bool found = false;
728
	u64 matches;
729
	enum match_result cmp = MATCH_ERROR;
730

731 732 733
	/*
	 * Lookup in the current node
	 * If we have a symbol, then compare the start to match
734 735
	 * anywhere inside a function, unless function
	 * mode is disabled.
736
	 */
737
	list_for_each_entry(cnode, &root->val, list) {
738
		struct callchain_cursor_node *node;
739

740 741
		node = callchain_cursor_current(cursor);
		if (!node)
742
			break;
743

744
		cmp = match_chain(node, cnode);
745
		if (cmp != MATCH_EQ)
746
			break;
747

748
		found = true;
749 750

		callchain_cursor_advance(cursor);
751 752
	}

753
	/* matches not, relay no the parent */
754
	if (!found) {
755
		WARN_ONCE(cmp == MATCH_ERROR, "Chain comparison error\n");
756
		return cmp;
757 758 759
	}

	matches = cursor->pos - start;
760 761

	/* we match only a part of the node. Split it and add the new chain */
762
	if (matches < root->val_nr) {
763 764 765 766
		if (split_add_child(root, cursor, cnode, start, matches,
				    period) < 0)
			return MATCH_ERROR;

767
		return MATCH_EQ;
768 769 770
	}

	/* we match 100% of the path, increment the hit */
771
	if (matches == root->val_nr && cursor->pos == cursor->nr) {
772
		root->hit += period;
773
		root->count++;
774
		return MATCH_EQ;
775 776
	}

777
	/* We match the node and still have a part remaining */
778 779
	if (append_chain_children(root, cursor, period) < 0)
		return MATCH_ERROR;
780

781
	return MATCH_EQ;
782 783
}

784 785 786
int callchain_append(struct callchain_root *root,
		     struct callchain_cursor *cursor,
		     u64 period)
787
{
788
	if (!cursor->nr)
789 790
		return 0;

791
	callchain_cursor_commit(cursor);
792

793 794
	if (append_chain_children(&root->node, cursor, period) < 0)
		return -1;
795

796 797
	if (cursor->nr > root->max_depth)
		root->max_depth = cursor->nr;
798 799

	return 0;
800
}
801 802

static int
803 804
merge_chain_branch(struct callchain_cursor *cursor,
		   struct callchain_node *dst, struct callchain_node *src)
805
{
806
	struct callchain_cursor_node **old_last = cursor->last;
807
	struct callchain_node *child;
808
	struct callchain_list *list, *next_list;
809
	struct rb_node *n;
810
	int old_pos = cursor->nr;
811 812 813
	int err = 0;

	list_for_each_entry_safe(list, next_list, &src->val, list) {
814
		callchain_cursor_append(cursor, list->ip,
815 816
					list->ms.map, list->ms.sym,
					false, NULL, 0, 0);
817
		list_del(&list->list);
818
		map__zput(list->ms.map);
819 820 821
		free(list);
	}

822 823
	if (src->hit) {
		callchain_cursor_commit(cursor);
824 825
		if (append_chain_children(dst, cursor, src->hit) < 0)
			return -1;
826
	}
827

828 829 830 831 832 833
	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);

834
		err = merge_chain_branch(cursor, dst, child);
835 836 837 838 839 840
		if (err)
			break;

		free(child);
	}

841 842
	cursor->nr = old_pos;
	cursor->last = old_last;
843 844 845 846

	return err;
}

847 848 849 850 851 852 853
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,
854 855 856
			    u64 ip, struct map *map, struct symbol *sym,
			    bool branch, struct branch_flags *flags,
			    int nr_loop_iter, int samples)
857
{
858
	struct callchain_cursor_node *node = *cursor->last;
859

860
	if (!node) {
861
		node = calloc(1, sizeof(*node));
862 863
		if (!node)
			return -ENOMEM;
864

865 866
		*cursor->last = node;
	}
867

868
	node->ip = ip;
869 870
	map__zput(node->map);
	node->map = map__get(map);
871
	node->sym = sym;
872 873 874 875 876 877 878
	node->branch = branch;
	node->nr_loop_iter = nr_loop_iter;
	node->samples = samples;

	if (flags)
		memcpy(&node->branch_flags, flags,
			sizeof(struct branch_flags));
879

880
	cursor->nr++;
881

882 883 884
	cursor->last = &node->next;

	return 0;
885
}
886

887 888
int sample__resolve_callchain(struct perf_sample *sample,
			      struct callchain_cursor *cursor, struct symbol **parent,
889 890 891 892 893 894
			      struct perf_evsel *evsel, struct addr_location *al,
			      int max_stack)
{
	if (sample->callchain == NULL)
		return 0;

895
	if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain ||
896
	    perf_hpp_list.parent) {
897
		return thread__resolve_callchain(al->thread, cursor, evsel, sample,
898
						 parent, al, max_stack);
899 900 901 902 903 904
	}
	return 0;
}

int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample)
{
905
	if (!symbol_conf.use_callchain || sample->callchain == NULL)
906 907 908
		return 0;
	return callchain_append(he->callchain, &callchain_cursor, sample->period);
}
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950

int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
			bool hide_unresolved)
{
	al->map = node->map;
	al->sym = node->sym;
	if (node->map)
		al->addr = node->map->map_ip(node->map, node->ip);
	else
		al->addr = node->ip;

	if (al->sym == NULL) {
		if (hide_unresolved)
			return 0;
		if (al->map == NULL)
			goto out;
	}

	if (al->map->groups == &al->machine->kmaps) {
		if (machine__is_host(al->machine)) {
			al->cpumode = PERF_RECORD_MISC_KERNEL;
			al->level = 'k';
		} else {
			al->cpumode = PERF_RECORD_MISC_GUEST_KERNEL;
			al->level = 'g';
		}
	} else {
		if (machine__is_host(al->machine)) {
			al->cpumode = PERF_RECORD_MISC_USER;
			al->level = '.';
		} else if (perf_guest) {
			al->cpumode = PERF_RECORD_MISC_GUEST_USER;
			al->level = 'u';
		} else {
			al->cpumode = PERF_RECORD_MISC_HYPERVISOR;
			al->level = 'H';
		}
	}

out:
	return 1;
}
951 952 953 954

char *callchain_list__sym_name(struct callchain_list *cl,
			       char *bf, size_t bfsize, bool show_dso)
{
955 956
	bool show_addr = callchain_param.key == CCKEY_ADDRESS;
	bool show_srcline = show_addr || callchain_param.key == CCKEY_SRCLINE;
957 958 959
	int printed;

	if (cl->ms.sym) {
960
		if (show_srcline && cl->ms.map && !cl->srcline)
961 962
			cl->srcline = get_srcline(cl->ms.map->dso,
						  map__rip_2objdump(cl->ms.map,
963
								    cl->ip),
964
						  cl->ms.sym, false, show_addr);
965 966 967 968 969
		if (cl->srcline)
			printed = scnprintf(bf, bfsize, "%s %s",
					cl->ms.sym->name, cl->srcline);
		else
			printed = scnprintf(bf, bfsize, "%s", cl->ms.sym->name);
970 971 972 973 974 975 976 977 978 979 980
	} else
		printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);

	if (show_dso)
		scnprintf(bf + printed, bfsize - printed, " %s",
			  cl->ms.map ?
			  cl->ms.map->dso->short_name :
			  "unknown");

	return bf;
}
981

982 983 984 985 986
char *callchain_node__scnprintf_value(struct callchain_node *node,
				      char *bf, size_t bfsize, u64 total)
{
	double percent = 0.0;
	u64 period = callchain_cumul_hits(node);
987
	unsigned count = callchain_cumul_counts(node);
988

989
	if (callchain_param.mode == CHAIN_FOLDED) {
990
		period = node->hit;
991 992
		count = node->count;
	}
993

994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
	switch (callchain_param.value) {
	case CCVAL_PERIOD:
		scnprintf(bf, bfsize, "%"PRIu64, period);
		break;
	case CCVAL_COUNT:
		scnprintf(bf, bfsize, "%u", count);
		break;
	case CCVAL_PERCENT:
	default:
		if (total)
			percent = period * 100.0 / total;
		scnprintf(bf, bfsize, "%.2f%%", percent);
		break;
	}
1008 1009 1010 1011 1012 1013 1014 1015
	return bf;
}

int callchain_node__fprintf_value(struct callchain_node *node,
				 FILE *fp, u64 total)
{
	double percent = 0.0;
	u64 period = callchain_cumul_hits(node);
1016
	unsigned count = callchain_cumul_counts(node);
1017

1018
	if (callchain_param.mode == CHAIN_FOLDED) {
1019
		period = node->hit;
1020 1021
		count = node->count;
	}
1022

1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
	switch (callchain_param.value) {
	case CCVAL_PERIOD:
		return fprintf(fp, "%"PRIu64, period);
	case CCVAL_COUNT:
		return fprintf(fp, "%u", count);
	case CCVAL_PERCENT:
	default:
		if (total)
			percent = period * 100.0 / total;
		return percent_color_fprintf(fp, "%.2f%%", percent);
	}
	return 0;
1035 1036
}

1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
static void callchain_counts_value(struct callchain_node *node,
				   u64 *branch_count, u64 *predicted_count,
				   u64 *abort_count, u64 *cycles_count)
{
	struct callchain_list *clist;

	list_for_each_entry(clist, &node->val, list) {
		if (branch_count)
			*branch_count += clist->branch_count;

		if (predicted_count)
			*predicted_count += clist->predicted_count;

		if (abort_count)
			*abort_count += clist->abort_count;

		if (cycles_count)
			*cycles_count += clist->cycles_count;
	}
}

static int callchain_node_branch_counts_cumul(struct callchain_node *node,
					      u64 *branch_count,
					      u64 *predicted_count,
					      u64 *abort_count,
					      u64 *cycles_count)
{
	struct callchain_node *child;
	struct rb_node *n;

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

		callchain_node_branch_counts_cumul(child, branch_count,
						   predicted_count,
						   abort_count,
						   cycles_count);

		callchain_counts_value(child, branch_count,
				       predicted_count, abort_count,
				       cycles_count);
	}

	return 0;
}

int callchain_branch_counts(struct callchain_root *root,
			    u64 *branch_count, u64 *predicted_count,
			    u64 *abort_count, u64 *cycles_count)
{
	if (branch_count)
		*branch_count = 0;

	if (predicted_count)
		*predicted_count = 0;

	if (abort_count)
		*abort_count = 0;

	if (cycles_count)
		*cycles_count = 0;

	return callchain_node_branch_counts_cumul(&root->node,
						  branch_count,
						  predicted_count,
						  abort_count,
						  cycles_count);
}

static int callchain_counts_printf(FILE *fp, char *bf, int bfsize,
				   u64 branch_count, u64 predicted_count,
				   u64 abort_count, u64 cycles_count,
				   u64 iter_count, u64 samples_count)
{
	double predicted_percent = 0.0;
	const char *null_str = "";
	char iter_str[32];
	char *str;
	u64 cycles = 0;

	if (branch_count == 0) {
		if (fp)
			return fprintf(fp, " (calltrace)");

		return scnprintf(bf, bfsize, " (calltrace)");
	}

	if (iter_count && samples_count) {
		scnprintf(iter_str, sizeof(iter_str),
			 ", iterations:%" PRId64 "",
			 iter_count / samples_count);
		str = iter_str;
	} else
		str = (char *)null_str;

	predicted_percent = predicted_count * 100.0 / branch_count;
	cycles = cycles_count / branch_count;

	if ((predicted_percent >= 100.0) && (abort_count == 0)) {
		if (fp)
			return fprintf(fp, " (cycles:%" PRId64 "%s)",
				       cycles, str);

		return scnprintf(bf, bfsize, " (cycles:%" PRId64 "%s)",
				 cycles, str);
	}

	if ((predicted_percent < 100.0) && (abort_count == 0)) {
		if (fp)
			return fprintf(fp,
				" (predicted:%.1f%%, cycles:%" PRId64 "%s)",
				predicted_percent, cycles, str);

		return scnprintf(bf, bfsize,
			" (predicted:%.1f%%, cycles:%" PRId64 "%s)",
			predicted_percent, cycles, str);
	}

	if (fp)
		return fprintf(fp,
		" (predicted:%.1f%%, abort:%" PRId64 ", cycles:%" PRId64 "%s)",
			predicted_percent, abort_count, cycles, str);

	return scnprintf(bf, bfsize,
		" (predicted:%.1f%%, abort:%" PRId64 ", cycles:%" PRId64 "%s)",
		predicted_percent, abort_count, cycles, str);
}

int callchain_list_counts__printf_value(struct callchain_node *node,
					struct callchain_list *clist,
					FILE *fp, char *bf, int bfsize)
{
	u64 branch_count, predicted_count;
	u64 abort_count, cycles_count;
	u64 iter_count = 0, samples_count = 0;

	branch_count = clist->branch_count;
	predicted_count = clist->predicted_count;
	abort_count = clist->abort_count;
	cycles_count = clist->cycles_count;

	if (node) {
		struct callchain_list *call;

		list_for_each_entry(call, &node->val, list) {
			iter_count += call->iter_count;
			samples_count += call->samples_count;
		}
	}

	return callchain_counts_printf(fp, bf, bfsize, branch_count,
				       predicted_count, abort_count,
				       cycles_count, iter_count, samples_count);
}

1194 1195 1196 1197 1198 1199
static void free_callchain_node(struct callchain_node *node)
{
	struct callchain_list *list, *tmp;
	struct callchain_node *child;
	struct rb_node *n;

1200 1201
	list_for_each_entry_safe(list, tmp, &node->parent_val, list) {
		list_del(&list->list);
1202
		map__zput(list->ms.map);
1203 1204 1205
		free(list);
	}

1206 1207
	list_for_each_entry_safe(list, tmp, &node->val, list) {
		list_del(&list->list);
1208
		map__zput(list->ms.map);
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
		free(list);
	}

	n = rb_first(&node->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, &node->rb_root_in);

		free_callchain_node(child);
		free(child);
	}
}

void free_callchain(struct callchain_root *root)
{
	if (!symbol_conf.use_callchain)
		return;

	free_callchain_node(&root->node);
}
1230

1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
static u64 decay_callchain_node(struct callchain_node *node)
{
	struct callchain_node *child;
	struct rb_node *n;
	u64 child_hits = 0;

	n = rb_first(&node->rb_root_in);
	while (n) {
		child = container_of(n, struct callchain_node, rb_node_in);

		child_hits += decay_callchain_node(child);
		n = rb_next(n);
	}

	node->hit = (node->hit * 7) / 8;
	node->children_hit = child_hits;

	return node->hit;
}

void decay_callchain(struct callchain_root *root)
{
	if (!symbol_conf.use_callchain)
		return;

	decay_callchain_node(&root->node);
}

1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
int callchain_node__make_parent_list(struct callchain_node *node)
{
	struct callchain_node *parent = node->parent;
	struct callchain_list *chain, *new;
	LIST_HEAD(head);

	while (parent) {
		list_for_each_entry_reverse(chain, &parent->val, list) {
			new = malloc(sizeof(*new));
			if (new == NULL)
				goto out;
			*new = *chain;
			new->has_children = false;
1272
			map__get(new->ms.map);
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
			list_add_tail(&new->list, &head);
		}
		parent = parent->parent;
	}

	list_for_each_entry_safe_reverse(chain, new, &head, list)
		list_move_tail(&chain->list, &node->parent_val);

	if (!list_empty(&node->parent_val)) {
		chain = list_first_entry(&node->parent_val, struct callchain_list, list);
		chain->has_children = rb_prev(&node->rb_node) || rb_next(&node->rb_node);

		chain = list_first_entry(&node->val, struct callchain_list, list);
		chain->has_children = false;
	}
	return 0;

out:
	list_for_each_entry_safe(chain, new, &head, list) {
		list_del(&chain->list);
1293
		map__zput(chain->ms.map);
1294 1295 1296 1297
		free(chain);
	}
	return -ENOMEM;
}
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324

int callchain_cursor__copy(struct callchain_cursor *dst,
			   struct callchain_cursor *src)
{
	int rc = 0;

	callchain_cursor_reset(dst);
	callchain_cursor_commit(src);

	while (true) {
		struct callchain_cursor_node *node;

		node = callchain_cursor_current(src);
		if (node == NULL)
			break;

		rc = callchain_cursor_append(dst, node->ip, node->map, node->sym,
					     node->branch, &node->branch_flags,
					     node->nr_loop_iter, node->samples);
		if (rc)
			break;

		callchain_cursor_advance(src);
	}

	return rc;
}