callchain.c 29.2 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, "branch", strlen(value))) {
		callchain_param.branch_callstack = 1;
		return 0;
	}
87 88

	pr_err("Invalid callchain sort key: %s\n", value);
89 90 91
	return -1;
}

92 93 94 95 96 97 98 99 100 101 102 103 104 105
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;
	}
106 107

	pr_err("Invalid callchain config key: %s\n", value);
108 109 110
	return -1;
}

111 112
static int
__parse_callchain_report_opt(const char *arg, bool allow_record_opt)
113
{
114
	char *tok;
115
	char *endptr;
116
	bool minpcnt_set = false;
117 118
	bool record_opt_set = false;
	bool try_stack_size = false;
119

120
	callchain_param.enabled = true;
121 122 123 124 125
	symbol_conf.use_callchain = true;

	if (!arg)
		return 0;

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

134 135
		if (!parse_callchain_mode(tok) ||
		    !parse_callchain_order(tok) ||
136 137
		    !parse_callchain_sort_key(tok) ||
		    !parse_callchain_value(tok)) {
138
			/* parsing ok - move on to the next */
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
			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;
161 162
		} else if (!minpcnt_set) {
			/* try to get the min percent */
163 164 165 166 167 168 169 170 171 172
			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;
		}
173
next:
174
		arg = NULL;
175 176 177 178 179 180 181 182 183
	}

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

184 185 186 187 188 189 190 191 192 193
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);
}

194 195 196 197 198 199 200 201 202
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"))
203
		return parse_callchain_record_opt(value, &callchain_param);
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
	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);
221 222
		if (value == endptr) {
			pr_err("Invalid callchain threshold: %s\n", value);
223
			return -1;
224
		}
225 226 227
	}
	if (!strcmp(var, "print-limit")) {
		callchain_param.print_limit = strtod(value, &endptr);
228 229
		if (value == endptr) {
			pr_err("Invalid callchain print limit: %s\n", value);
230
			return -1;
231
		}
232 233 234 235 236
	}

	return 0;
}

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

	while (*p) {
247 248
		u64 rnode_cumul;

249 250
		parent = *p;
		rnode = rb_entry(parent, struct callchain_node, rb_node);
251
		rnode_cumul = callchain_cumul_hits(rnode);
252

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

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

278 279 280 281
static void
__sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
		  u64 min_hit)
{
282
	struct rb_node *n;
283 284
	struct callchain_node *child;

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

290
		__sort_chain_flat(rb_root, child, min_hit);
291
	}
292 293 294 295 296

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

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

static void __sort_chain_graph_abs(struct callchain_node *node,
				   u64 min_hit)
311
{
312
	struct rb_node *n;
313 314
	struct callchain_node *child;

315
	node->rb_root = RB_ROOT;
316 317 318 319 320
	n = rb_first(&node->rb_root_in);

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

322
		__sort_chain_graph_abs(child, min_hit);
323
		if (callchain_cumul_hits(child) >= min_hit)
324 325 326 327 328 329
			rb_insert_callchain(&node->rb_root, child,
					    CHAIN_GRAPH_ABS);
	}
}

static void
330
sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root,
331
		     u64 min_hit, struct callchain_param *param __maybe_unused)
332
{
333 334
	__sort_chain_graph_abs(&chain_root->node, min_hit);
	rb_root->rb_node = chain_root->node.rb_root.rb_node;
335 336
}

337 338
static void __sort_chain_graph_rel(struct callchain_node *node,
				   double min_percent)
339
{
340
	struct rb_node *n;
341
	struct callchain_node *child;
342
	u64 min_hit;
343 344

	node->rb_root = RB_ROOT;
345
	min_hit = ceil(node->children_hit * min_percent);
346

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

352
		__sort_chain_graph_rel(child, min_percent);
353
		if (callchain_cumul_hits(child) >= min_hit)
354 355
			rb_insert_callchain(&node->rb_root, child,
					    CHAIN_GRAPH_REL);
356 357 358
	}
}

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

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

387 388 389 390 391 392
/*
 * 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)
393 394 395
{
	struct callchain_node *new;

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

	if (inherit_children) {
406 407 408 409 410
		struct rb_node *n;
		struct callchain_node *child;

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

412 413 414 415 416 417
		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);
		}
418

419 420 421
		/* 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);
422
	}
423 424 425 426

	return new;
}

427

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

440 441 442
	cursor_node = callchain_cursor_current(cursor);

	while (cursor_node) {
443 444
		struct callchain_list *call;

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

		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;
		}

468
		list_add_tail(&call->list, &node->val);
469 470 471

		callchain_cursor_advance(cursor);
		cursor_node = callchain_cursor_current(cursor);
472
	}
473
	return 0;
474 475
}

476
static struct callchain_node *
477 478 479
add_child(struct callchain_node *parent,
	  struct callchain_cursor *cursor,
	  u64 period)
480 481 482
{
	struct callchain_node *new;

483
	new = create_child(parent, false);
484 485 486
	if (new == NULL)
		return NULL;

487 488 489 490 491
	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);
492
			map__zput(call->ms.map);
493 494 495 496 497
			free(call);
		}
		free(new);
		return NULL;
	}
498

499
	new->children_hit = 0;
500
	new->hit = period;
501 502
	new->children_count = 0;
	new->count = 1;
503 504 505
	return new;
}

506 507 508 509 510 511 512 513 514
enum match_result {
	MATCH_ERROR  = -1,
	MATCH_EQ,
	MATCH_LT,
	MATCH_GT,
};

static enum match_result match_chain(struct callchain_cursor_node *node,
				     struct callchain_list *cnode)
515 516
{
	struct symbol *sym = node->sym;
517
	u64 left, right;
518 519

	if (cnode->ms.sym && sym &&
520 521 522 523 524 525 526 527
	    callchain_param.key == CCKEY_FUNCTION) {
		left = cnode->ms.sym->start;
		right = sym->start;
	} else {
		left = cnode->ip;
		right = node->ip;
	}

528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
	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;
		}

543
		return MATCH_EQ;
544
	}
545 546

	return left > right ? MATCH_GT : MATCH_LT;
547 548
}

549 550 551 552 553
/*
 * 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
 */
554
static int
555 556 557 558
split_add_child(struct callchain_node *parent,
		struct callchain_cursor *cursor,
		struct callchain_list *to_split,
		u64 idx_parents, u64 idx_local, u64 period)
559 560
{
	struct callchain_node *new;
561
	struct list_head *old_tail;
562
	unsigned int idx_total = idx_parents + idx_local;
563 564

	/* split */
565
	new = create_child(parent, true);
566 567
	if (new == NULL)
		return -1;
568 569 570 571 572 573 574 575

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

577 578
	/* split the hits */
	new->hit = parent->hit;
579
	new->children_hit = parent->children_hit;
580
	parent->children_hit = callchain_cumul_hits(new);
581 582
	new->val_nr = parent->val_nr - idx_local;
	parent->val_nr = idx_local;
583 584 585
	new->count = parent->count;
	new->children_count = parent->children_count;
	parent->children_count = callchain_cumul_counts(new);
586 587

	/* create a new child for the new branch if any */
588
	if (idx_total < cursor->nr) {
589 590 591 592 593
		struct callchain_node *first;
		struct callchain_list *cnode;
		struct callchain_cursor_node *node;
		struct rb_node *p, **pp;

594
		parent->hit = 0;
595
		parent->children_hit += period;
596 597
		parent->count = 0;
		parent->children_count += 1;
598 599 600

		node = callchain_cursor_current(cursor);
		new = add_child(parent, cursor, period);
601
		if (new == NULL)
602
			return -1;
603 604 605 606 607 608 609 610 611 612

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

613
		if (match_chain(node, cnode) == MATCH_LT)
614 615 616 617 618 619
			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);
620
	} else {
621
		parent->hit = period;
622
		parent->count = 1;
623
	}
624
	return 0;
625 626
}

627
static enum match_result
628 629 630
append_chain(struct callchain_node *root,
	     struct callchain_cursor *cursor,
	     u64 period);
631

632
static int
633 634 635
append_chain_children(struct callchain_node *root,
		      struct callchain_cursor *cursor,
		      u64 period)
636 637
{
	struct callchain_node *rnode;
638 639 640 641 642 643
	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)
644
		return -1;
645 646

	/* lookup in childrens */
647
	while (*p) {
648
		enum match_result ret;
649

650 651 652
		parent = *p;
		rnode = rb_entry(parent, struct callchain_node, rb_node_in);

653 654
		/* If at least first entry matches, rely to children */
		ret = append_chain(rnode, cursor, period);
655
		if (ret == MATCH_EQ)
656
			goto inc_children_hit;
657 658
		if (ret == MATCH_ERROR)
			return -1;
659

660
		if (ret == MATCH_LT)
661 662 663
			p = &parent->rb_left;
		else
			p = &parent->rb_right;
664
	}
665
	/* nothing in children, add to the current node */
666
	rnode = add_child(root, cursor, period);
667
	if (rnode == NULL)
668
		return -1;
669

670 671
	rb_link_node(&rnode->rb_node_in, parent, p);
	rb_insert_color(&rnode->rb_node_in, &root->rb_root_in);
672

673
inc_children_hit:
674
	root->children_hit += period;
675
	root->children_count++;
676
	return 0;
677 678
}

679
static enum match_result
680 681 682
append_chain(struct callchain_node *root,
	     struct callchain_cursor *cursor,
	     u64 period)
683 684
{
	struct callchain_list *cnode;
685
	u64 start = cursor->pos;
686
	bool found = false;
687
	u64 matches;
688
	enum match_result cmp = MATCH_ERROR;
689

690 691 692
	/*
	 * Lookup in the current node
	 * If we have a symbol, then compare the start to match
693 694
	 * anywhere inside a function, unless function
	 * mode is disabled.
695
	 */
696
	list_for_each_entry(cnode, &root->val, list) {
697
		struct callchain_cursor_node *node;
698

699 700
		node = callchain_cursor_current(cursor);
		if (!node)
701
			break;
702

703
		cmp = match_chain(node, cnode);
704
		if (cmp != MATCH_EQ)
705
			break;
706

707
		found = true;
708 709

		callchain_cursor_advance(cursor);
710 711
	}

712
	/* matches not, relay no the parent */
713
	if (!found) {
714
		WARN_ONCE(cmp == MATCH_ERROR, "Chain comparison error\n");
715
		return cmp;
716 717 718
	}

	matches = cursor->pos - start;
719 720

	/* we match only a part of the node. Split it and add the new chain */
721
	if (matches < root->val_nr) {
722 723 724 725
		if (split_add_child(root, cursor, cnode, start, matches,
				    period) < 0)
			return MATCH_ERROR;

726
		return MATCH_EQ;
727 728 729
	}

	/* we match 100% of the path, increment the hit */
730
	if (matches == root->val_nr && cursor->pos == cursor->nr) {
731
		root->hit += period;
732
		root->count++;
733
		return MATCH_EQ;
734 735
	}

736
	/* We match the node and still have a part remaining */
737 738
	if (append_chain_children(root, cursor, period) < 0)
		return MATCH_ERROR;
739

740
	return MATCH_EQ;
741 742
}

743 744 745
int callchain_append(struct callchain_root *root,
		     struct callchain_cursor *cursor,
		     u64 period)
746
{
747
	if (!cursor->nr)
748 749
		return 0;

750
	callchain_cursor_commit(cursor);
751

752 753
	if (append_chain_children(&root->node, cursor, period) < 0)
		return -1;
754

755 756
	if (cursor->nr > root->max_depth)
		root->max_depth = cursor->nr;
757 758

	return 0;
759
}
760 761

static int
762 763
merge_chain_branch(struct callchain_cursor *cursor,
		   struct callchain_node *dst, struct callchain_node *src)
764
{
765
	struct callchain_cursor_node **old_last = cursor->last;
766
	struct callchain_node *child;
767
	struct callchain_list *list, *next_list;
768
	struct rb_node *n;
769
	int old_pos = cursor->nr;
770 771 772
	int err = 0;

	list_for_each_entry_safe(list, next_list, &src->val, list) {
773
		callchain_cursor_append(cursor, list->ip,
774 775
					list->ms.map, list->ms.sym,
					false, NULL, 0, 0);
776
		list_del(&list->list);
777
		map__zput(list->ms.map);
778 779 780
		free(list);
	}

781 782
	if (src->hit) {
		callchain_cursor_commit(cursor);
783 784
		if (append_chain_children(dst, cursor, src->hit) < 0)
			return -1;
785
	}
786

787 788 789 790 791 792
	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);

793
		err = merge_chain_branch(cursor, dst, child);
794 795 796 797 798 799
		if (err)
			break;

		free(child);
	}

800 801
	cursor->nr = old_pos;
	cursor->last = old_last;
802 803 804 805

	return err;
}

806 807 808 809 810 811 812
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,
813 814 815
			    u64 ip, struct map *map, struct symbol *sym,
			    bool branch, struct branch_flags *flags,
			    int nr_loop_iter, int samples)
816
{
817
	struct callchain_cursor_node *node = *cursor->last;
818

819
	if (!node) {
820
		node = calloc(1, sizeof(*node));
821 822
		if (!node)
			return -ENOMEM;
823

824 825
		*cursor->last = node;
	}
826

827
	node->ip = ip;
828 829
	map__zput(node->map);
	node->map = map__get(map);
830
	node->sym = sym;
831 832 833 834 835 836 837
	node->branch = branch;
	node->nr_loop_iter = nr_loop_iter;
	node->samples = samples;

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

839
	cursor->nr++;
840

841 842 843
	cursor->last = &node->next;

	return 0;
844
}
845

846 847
int sample__resolve_callchain(struct perf_sample *sample,
			      struct callchain_cursor *cursor, struct symbol **parent,
848 849 850 851 852 853
			      struct perf_evsel *evsel, struct addr_location *al,
			      int max_stack)
{
	if (sample->callchain == NULL)
		return 0;

854
	if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain ||
855
	    perf_hpp_list.parent) {
856
		return thread__resolve_callchain(al->thread, cursor, evsel, sample,
857
						 parent, al, max_stack);
858 859 860 861 862 863
	}
	return 0;
}

int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample)
{
864
	if (!symbol_conf.use_callchain || sample->callchain == NULL)
865 866 867
		return 0;
	return callchain_append(he->callchain, &callchain_cursor, sample->period);
}
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909

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;
}
910 911 912 913 914 915 916

char *callchain_list__sym_name(struct callchain_list *cl,
			       char *bf, size_t bfsize, bool show_dso)
{
	int printed;

	if (cl->ms.sym) {
917 918 919 920
		if (callchain_param.key == CCKEY_ADDRESS &&
		    cl->ms.map && !cl->srcline)
			cl->srcline = get_srcline(cl->ms.map->dso,
						  map__rip_2objdump(cl->ms.map,
921 922
								    cl->ip),
						  cl->ms.sym, false);
923 924 925 926 927
		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);
928 929 930 931 932 933 934 935 936 937 938
	} 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;
}
939

940 941 942 943 944
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);
945
	unsigned count = callchain_cumul_counts(node);
946

947
	if (callchain_param.mode == CHAIN_FOLDED) {
948
		period = node->hit;
949 950
		count = node->count;
	}
951

952 953 954 955 956 957 958 959 960 961 962 963 964 965
	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;
	}
966 967 968 969 970 971 972 973
	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);
974
	unsigned count = callchain_cumul_counts(node);
975

976
	if (callchain_param.mode == CHAIN_FOLDED) {
977
		period = node->hit;
978 979
		count = node->count;
	}
980

981 982 983 984 985 986 987 988 989 990 991 992
	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;
993 994
}

995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 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
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);
}

1152 1153 1154 1155 1156 1157
static void free_callchain_node(struct callchain_node *node)
{
	struct callchain_list *list, *tmp;
	struct callchain_node *child;
	struct rb_node *n;

1158 1159
	list_for_each_entry_safe(list, tmp, &node->parent_val, list) {
		list_del(&list->list);
1160
		map__zput(list->ms.map);
1161 1162 1163
		free(list);
	}

1164 1165
	list_for_each_entry_safe(list, tmp, &node->val, list) {
		list_del(&list->list);
1166
		map__zput(list->ms.map);
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
		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);
}
1188

1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
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);
}

1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
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;
1230
			map__get(new->ms.map);
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
			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);
1251
		map__zput(chain->ms.map);
1252 1253 1254 1255
		free(chain);
	}
	return -ENOMEM;
}
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282

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;
}