ceph: use i_size_{read,write} to get/set i_size
[linux/fpc-iii.git] / tools / perf / util / callchain.c
blob735ad48e1858b0382c9aacc84e0b912e9329c86a
1 /*
2 * Copyright (C) 2009-2011, Frederic Weisbecker <fweisbec@gmail.com>
4 * Handle the callchains from the stream in an ad-hoc radix tree and then
5 * sort them in an rbtree.
7 * Using a radix for code path provides a fast retrieval and factorizes
8 * memory use. Also that lets us use the paths in a hierarchical graph view.
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <stdbool.h>
15 #include <errno.h>
16 #include <math.h>
18 #include "asm/bug.h"
20 #include "hist.h"
21 #include "util.h"
22 #include "sort.h"
23 #include "machine.h"
24 #include "callchain.h"
26 __thread struct callchain_cursor callchain_cursor;
28 int parse_callchain_record_opt(const char *arg, struct callchain_param *param)
30 return parse_callchain_record(arg, param);
33 static int parse_callchain_mode(const char *value)
35 if (!strncmp(value, "graph", strlen(value))) {
36 callchain_param.mode = CHAIN_GRAPH_ABS;
37 return 0;
39 if (!strncmp(value, "flat", strlen(value))) {
40 callchain_param.mode = CHAIN_FLAT;
41 return 0;
43 if (!strncmp(value, "fractal", strlen(value))) {
44 callchain_param.mode = CHAIN_GRAPH_REL;
45 return 0;
47 return -1;
50 static int parse_callchain_order(const char *value)
52 if (!strncmp(value, "caller", strlen(value))) {
53 callchain_param.order = ORDER_CALLER;
54 callchain_param.order_set = true;
55 return 0;
57 if (!strncmp(value, "callee", strlen(value))) {
58 callchain_param.order = ORDER_CALLEE;
59 callchain_param.order_set = true;
60 return 0;
62 return -1;
65 static int parse_callchain_sort_key(const char *value)
67 if (!strncmp(value, "function", strlen(value))) {
68 callchain_param.key = CCKEY_FUNCTION;
69 return 0;
71 if (!strncmp(value, "address", strlen(value))) {
72 callchain_param.key = CCKEY_ADDRESS;
73 return 0;
75 if (!strncmp(value, "branch", strlen(value))) {
76 callchain_param.branch_callstack = 1;
77 return 0;
79 return -1;
82 static int
83 __parse_callchain_report_opt(const char *arg, bool allow_record_opt)
85 char *tok;
86 char *endptr;
87 bool minpcnt_set = false;
88 bool record_opt_set = false;
89 bool try_stack_size = false;
91 symbol_conf.use_callchain = true;
93 if (!arg)
94 return 0;
96 while ((tok = strtok((char *)arg, ",")) != NULL) {
97 if (!strncmp(tok, "none", strlen(tok))) {
98 callchain_param.mode = CHAIN_NONE;
99 symbol_conf.use_callchain = false;
100 return 0;
103 if (!parse_callchain_mode(tok) ||
104 !parse_callchain_order(tok) ||
105 !parse_callchain_sort_key(tok)) {
106 /* parsing ok - move on to the next */
107 try_stack_size = false;
108 goto next;
109 } else if (allow_record_opt && !record_opt_set) {
110 if (parse_callchain_record(tok, &callchain_param))
111 goto try_numbers;
113 /* assume that number followed by 'dwarf' is stack size */
114 if (callchain_param.record_mode == CALLCHAIN_DWARF)
115 try_stack_size = true;
117 record_opt_set = true;
118 goto next;
121 try_numbers:
122 if (try_stack_size) {
123 unsigned long size = 0;
125 if (get_stack_size(tok, &size) < 0)
126 return -1;
127 callchain_param.dump_size = size;
128 try_stack_size = false;
129 } else if (!minpcnt_set) {
130 /* try to get the min percent */
131 callchain_param.min_percent = strtod(tok, &endptr);
132 if (tok == endptr)
133 return -1;
134 minpcnt_set = true;
135 } else {
136 /* try print limit at last */
137 callchain_param.print_limit = strtoul(tok, &endptr, 0);
138 if (tok == endptr)
139 return -1;
141 next:
142 arg = NULL;
145 if (callchain_register_param(&callchain_param) < 0) {
146 pr_err("Can't register callchain params\n");
147 return -1;
149 return 0;
152 int parse_callchain_report_opt(const char *arg)
154 return __parse_callchain_report_opt(arg, false);
157 int parse_callchain_top_opt(const char *arg)
159 return __parse_callchain_report_opt(arg, true);
162 int perf_callchain_config(const char *var, const char *value)
164 char *endptr;
166 if (prefixcmp(var, "call-graph."))
167 return 0;
168 var += sizeof("call-graph.") - 1;
170 if (!strcmp(var, "record-mode"))
171 return parse_callchain_record_opt(value, &callchain_param);
172 #ifdef HAVE_DWARF_UNWIND_SUPPORT
173 if (!strcmp(var, "dump-size")) {
174 unsigned long size = 0;
175 int ret;
177 ret = get_stack_size(value, &size);
178 callchain_param.dump_size = size;
180 return ret;
182 #endif
183 if (!strcmp(var, "print-type"))
184 return parse_callchain_mode(value);
185 if (!strcmp(var, "order"))
186 return parse_callchain_order(value);
187 if (!strcmp(var, "sort-key"))
188 return parse_callchain_sort_key(value);
189 if (!strcmp(var, "threshold")) {
190 callchain_param.min_percent = strtod(value, &endptr);
191 if (value == endptr)
192 return -1;
194 if (!strcmp(var, "print-limit")) {
195 callchain_param.print_limit = strtod(value, &endptr);
196 if (value == endptr)
197 return -1;
200 return 0;
203 static void
204 rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
205 enum chain_mode mode)
207 struct rb_node **p = &root->rb_node;
208 struct rb_node *parent = NULL;
209 struct callchain_node *rnode;
210 u64 chain_cumul = callchain_cumul_hits(chain);
212 while (*p) {
213 u64 rnode_cumul;
215 parent = *p;
216 rnode = rb_entry(parent, struct callchain_node, rb_node);
217 rnode_cumul = callchain_cumul_hits(rnode);
219 switch (mode) {
220 case CHAIN_FLAT:
221 if (rnode->hit < chain->hit)
222 p = &(*p)->rb_left;
223 else
224 p = &(*p)->rb_right;
225 break;
226 case CHAIN_GRAPH_ABS: /* Falldown */
227 case CHAIN_GRAPH_REL:
228 if (rnode_cumul < chain_cumul)
229 p = &(*p)->rb_left;
230 else
231 p = &(*p)->rb_right;
232 break;
233 case CHAIN_NONE:
234 default:
235 break;
239 rb_link_node(&chain->rb_node, parent, p);
240 rb_insert_color(&chain->rb_node, root);
243 static void
244 __sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
245 u64 min_hit)
247 struct rb_node *n;
248 struct callchain_node *child;
250 n = rb_first(&node->rb_root_in);
251 while (n) {
252 child = rb_entry(n, struct callchain_node, rb_node_in);
253 n = rb_next(n);
255 __sort_chain_flat(rb_root, child, min_hit);
258 if (node->hit && node->hit >= min_hit)
259 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
263 * Once we get every callchains from the stream, we can now
264 * sort them by hit
266 static void
267 sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root,
268 u64 min_hit, struct callchain_param *param __maybe_unused)
270 __sort_chain_flat(rb_root, &root->node, min_hit);
273 static void __sort_chain_graph_abs(struct callchain_node *node,
274 u64 min_hit)
276 struct rb_node *n;
277 struct callchain_node *child;
279 node->rb_root = RB_ROOT;
280 n = rb_first(&node->rb_root_in);
282 while (n) {
283 child = rb_entry(n, struct callchain_node, rb_node_in);
284 n = rb_next(n);
286 __sort_chain_graph_abs(child, min_hit);
287 if (callchain_cumul_hits(child) >= min_hit)
288 rb_insert_callchain(&node->rb_root, child,
289 CHAIN_GRAPH_ABS);
293 static void
294 sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root,
295 u64 min_hit, struct callchain_param *param __maybe_unused)
297 __sort_chain_graph_abs(&chain_root->node, min_hit);
298 rb_root->rb_node = chain_root->node.rb_root.rb_node;
301 static void __sort_chain_graph_rel(struct callchain_node *node,
302 double min_percent)
304 struct rb_node *n;
305 struct callchain_node *child;
306 u64 min_hit;
308 node->rb_root = RB_ROOT;
309 min_hit = ceil(node->children_hit * min_percent);
311 n = rb_first(&node->rb_root_in);
312 while (n) {
313 child = rb_entry(n, struct callchain_node, rb_node_in);
314 n = rb_next(n);
316 __sort_chain_graph_rel(child, min_percent);
317 if (callchain_cumul_hits(child) >= min_hit)
318 rb_insert_callchain(&node->rb_root, child,
319 CHAIN_GRAPH_REL);
323 static void
324 sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root,
325 u64 min_hit __maybe_unused, struct callchain_param *param)
327 __sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0);
328 rb_root->rb_node = chain_root->node.rb_root.rb_node;
331 int callchain_register_param(struct callchain_param *param)
333 switch (param->mode) {
334 case CHAIN_GRAPH_ABS:
335 param->sort = sort_chain_graph_abs;
336 break;
337 case CHAIN_GRAPH_REL:
338 param->sort = sort_chain_graph_rel;
339 break;
340 case CHAIN_FLAT:
341 param->sort = sort_chain_flat;
342 break;
343 case CHAIN_NONE:
344 default:
345 return -1;
347 return 0;
351 * Create a child for a parent. If inherit_children, then the new child
352 * will become the new parent of it's parent children
354 static struct callchain_node *
355 create_child(struct callchain_node *parent, bool inherit_children)
357 struct callchain_node *new;
359 new = zalloc(sizeof(*new));
360 if (!new) {
361 perror("not enough memory to create child for code path tree");
362 return NULL;
364 new->parent = parent;
365 INIT_LIST_HEAD(&new->val);
367 if (inherit_children) {
368 struct rb_node *n;
369 struct callchain_node *child;
371 new->rb_root_in = parent->rb_root_in;
372 parent->rb_root_in = RB_ROOT;
374 n = rb_first(&new->rb_root_in);
375 while (n) {
376 child = rb_entry(n, struct callchain_node, rb_node_in);
377 child->parent = new;
378 n = rb_next(n);
381 /* make it the first child */
382 rb_link_node(&new->rb_node_in, NULL, &parent->rb_root_in.rb_node);
383 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
386 return new;
391 * Fill the node with callchain values
393 static void
394 fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
396 struct callchain_cursor_node *cursor_node;
398 node->val_nr = cursor->nr - cursor->pos;
399 if (!node->val_nr)
400 pr_warning("Warning: empty node in callchain tree\n");
402 cursor_node = callchain_cursor_current(cursor);
404 while (cursor_node) {
405 struct callchain_list *call;
407 call = zalloc(sizeof(*call));
408 if (!call) {
409 perror("not enough memory for the code path tree");
410 return;
412 call->ip = cursor_node->ip;
413 call->ms.sym = cursor_node->sym;
414 call->ms.map = cursor_node->map;
415 list_add_tail(&call->list, &node->val);
417 callchain_cursor_advance(cursor);
418 cursor_node = callchain_cursor_current(cursor);
422 static struct callchain_node *
423 add_child(struct callchain_node *parent,
424 struct callchain_cursor *cursor,
425 u64 period)
427 struct callchain_node *new;
429 new = create_child(parent, false);
430 fill_node(new, cursor);
432 new->children_hit = 0;
433 new->hit = period;
434 return new;
437 static s64 match_chain(struct callchain_cursor_node *node,
438 struct callchain_list *cnode)
440 struct symbol *sym = node->sym;
442 if (cnode->ms.sym && sym &&
443 callchain_param.key == CCKEY_FUNCTION)
444 return cnode->ms.sym->start - sym->start;
445 else
446 return cnode->ip - node->ip;
450 * Split the parent in two parts (a new child is created) and
451 * give a part of its callchain to the created child.
452 * Then create another child to host the given callchain of new branch
454 static void
455 split_add_child(struct callchain_node *parent,
456 struct callchain_cursor *cursor,
457 struct callchain_list *to_split,
458 u64 idx_parents, u64 idx_local, u64 period)
460 struct callchain_node *new;
461 struct list_head *old_tail;
462 unsigned int idx_total = idx_parents + idx_local;
464 /* split */
465 new = create_child(parent, true);
467 /* split the callchain and move a part to the new child */
468 old_tail = parent->val.prev;
469 list_del_range(&to_split->list, old_tail);
470 new->val.next = &to_split->list;
471 new->val.prev = old_tail;
472 to_split->list.prev = &new->val;
473 old_tail->next = &new->val;
475 /* split the hits */
476 new->hit = parent->hit;
477 new->children_hit = parent->children_hit;
478 parent->children_hit = callchain_cumul_hits(new);
479 new->val_nr = parent->val_nr - idx_local;
480 parent->val_nr = idx_local;
482 /* create a new child for the new branch if any */
483 if (idx_total < cursor->nr) {
484 struct callchain_node *first;
485 struct callchain_list *cnode;
486 struct callchain_cursor_node *node;
487 struct rb_node *p, **pp;
489 parent->hit = 0;
490 parent->children_hit += period;
492 node = callchain_cursor_current(cursor);
493 new = add_child(parent, cursor, period);
496 * This is second child since we moved parent's children
497 * to new (first) child above.
499 p = parent->rb_root_in.rb_node;
500 first = rb_entry(p, struct callchain_node, rb_node_in);
501 cnode = list_first_entry(&first->val, struct callchain_list,
502 list);
504 if (match_chain(node, cnode) < 0)
505 pp = &p->rb_left;
506 else
507 pp = &p->rb_right;
509 rb_link_node(&new->rb_node_in, p, pp);
510 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
511 } else {
512 parent->hit = period;
516 static int
517 append_chain(struct callchain_node *root,
518 struct callchain_cursor *cursor,
519 u64 period);
521 static void
522 append_chain_children(struct callchain_node *root,
523 struct callchain_cursor *cursor,
524 u64 period)
526 struct callchain_node *rnode;
527 struct callchain_cursor_node *node;
528 struct rb_node **p = &root->rb_root_in.rb_node;
529 struct rb_node *parent = NULL;
531 node = callchain_cursor_current(cursor);
532 if (!node)
533 return;
535 /* lookup in childrens */
536 while (*p) {
537 s64 ret;
539 parent = *p;
540 rnode = rb_entry(parent, struct callchain_node, rb_node_in);
542 /* If at least first entry matches, rely to children */
543 ret = append_chain(rnode, cursor, period);
544 if (ret == 0)
545 goto inc_children_hit;
547 if (ret < 0)
548 p = &parent->rb_left;
549 else
550 p = &parent->rb_right;
552 /* nothing in children, add to the current node */
553 rnode = add_child(root, cursor, period);
554 rb_link_node(&rnode->rb_node_in, parent, p);
555 rb_insert_color(&rnode->rb_node_in, &root->rb_root_in);
557 inc_children_hit:
558 root->children_hit += period;
561 static int
562 append_chain(struct callchain_node *root,
563 struct callchain_cursor *cursor,
564 u64 period)
566 struct callchain_list *cnode;
567 u64 start = cursor->pos;
568 bool found = false;
569 u64 matches;
570 int cmp = 0;
573 * Lookup in the current node
574 * If we have a symbol, then compare the start to match
575 * anywhere inside a function, unless function
576 * mode is disabled.
578 list_for_each_entry(cnode, &root->val, list) {
579 struct callchain_cursor_node *node;
581 node = callchain_cursor_current(cursor);
582 if (!node)
583 break;
585 cmp = match_chain(node, cnode);
586 if (cmp)
587 break;
589 found = true;
591 callchain_cursor_advance(cursor);
594 /* matches not, relay no the parent */
595 if (!found) {
596 WARN_ONCE(!cmp, "Chain comparison error\n");
597 return cmp;
600 matches = cursor->pos - start;
602 /* we match only a part of the node. Split it and add the new chain */
603 if (matches < root->val_nr) {
604 split_add_child(root, cursor, cnode, start, matches, period);
605 return 0;
608 /* we match 100% of the path, increment the hit */
609 if (matches == root->val_nr && cursor->pos == cursor->nr) {
610 root->hit += period;
611 return 0;
614 /* We match the node and still have a part remaining */
615 append_chain_children(root, cursor, period);
617 return 0;
620 int callchain_append(struct callchain_root *root,
621 struct callchain_cursor *cursor,
622 u64 period)
624 if (!cursor->nr)
625 return 0;
627 callchain_cursor_commit(cursor);
629 append_chain_children(&root->node, cursor, period);
631 if (cursor->nr > root->max_depth)
632 root->max_depth = cursor->nr;
634 return 0;
637 static int
638 merge_chain_branch(struct callchain_cursor *cursor,
639 struct callchain_node *dst, struct callchain_node *src)
641 struct callchain_cursor_node **old_last = cursor->last;
642 struct callchain_node *child;
643 struct callchain_list *list, *next_list;
644 struct rb_node *n;
645 int old_pos = cursor->nr;
646 int err = 0;
648 list_for_each_entry_safe(list, next_list, &src->val, list) {
649 callchain_cursor_append(cursor, list->ip,
650 list->ms.map, list->ms.sym);
651 list_del(&list->list);
652 free(list);
655 if (src->hit) {
656 callchain_cursor_commit(cursor);
657 append_chain_children(dst, cursor, src->hit);
660 n = rb_first(&src->rb_root_in);
661 while (n) {
662 child = container_of(n, struct callchain_node, rb_node_in);
663 n = rb_next(n);
664 rb_erase(&child->rb_node_in, &src->rb_root_in);
666 err = merge_chain_branch(cursor, dst, child);
667 if (err)
668 break;
670 free(child);
673 cursor->nr = old_pos;
674 cursor->last = old_last;
676 return err;
679 int callchain_merge(struct callchain_cursor *cursor,
680 struct callchain_root *dst, struct callchain_root *src)
682 return merge_chain_branch(cursor, &dst->node, &src->node);
685 int callchain_cursor_append(struct callchain_cursor *cursor,
686 u64 ip, struct map *map, struct symbol *sym)
688 struct callchain_cursor_node *node = *cursor->last;
690 if (!node) {
691 node = calloc(1, sizeof(*node));
692 if (!node)
693 return -ENOMEM;
695 *cursor->last = node;
698 node->ip = ip;
699 node->map = map;
700 node->sym = sym;
702 cursor->nr++;
704 cursor->last = &node->next;
706 return 0;
709 int sample__resolve_callchain(struct perf_sample *sample, struct symbol **parent,
710 struct perf_evsel *evsel, struct addr_location *al,
711 int max_stack)
713 if (sample->callchain == NULL)
714 return 0;
716 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain ||
717 sort__has_parent) {
718 return thread__resolve_callchain(al->thread, evsel, sample,
719 parent, al, max_stack);
721 return 0;
724 int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample)
726 if (!symbol_conf.use_callchain || sample->callchain == NULL)
727 return 0;
728 return callchain_append(he->callchain, &callchain_cursor, sample->period);
731 int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
732 bool hide_unresolved)
734 al->map = node->map;
735 al->sym = node->sym;
736 if (node->map)
737 al->addr = node->map->map_ip(node->map, node->ip);
738 else
739 al->addr = node->ip;
741 if (al->sym == NULL) {
742 if (hide_unresolved)
743 return 0;
744 if (al->map == NULL)
745 goto out;
748 if (al->map->groups == &al->machine->kmaps) {
749 if (machine__is_host(al->machine)) {
750 al->cpumode = PERF_RECORD_MISC_KERNEL;
751 al->level = 'k';
752 } else {
753 al->cpumode = PERF_RECORD_MISC_GUEST_KERNEL;
754 al->level = 'g';
756 } else {
757 if (machine__is_host(al->machine)) {
758 al->cpumode = PERF_RECORD_MISC_USER;
759 al->level = '.';
760 } else if (perf_guest) {
761 al->cpumode = PERF_RECORD_MISC_GUEST_USER;
762 al->level = 'u';
763 } else {
764 al->cpumode = PERF_RECORD_MISC_HYPERVISOR;
765 al->level = 'H';
769 out:
770 return 1;
773 char *callchain_list__sym_name(struct callchain_list *cl,
774 char *bf, size_t bfsize, bool show_dso)
776 int printed;
778 if (cl->ms.sym) {
779 if (callchain_param.key == CCKEY_ADDRESS &&
780 cl->ms.map && !cl->srcline)
781 cl->srcline = get_srcline(cl->ms.map->dso,
782 map__rip_2objdump(cl->ms.map,
783 cl->ip),
784 cl->ms.sym, false);
785 if (cl->srcline)
786 printed = scnprintf(bf, bfsize, "%s %s",
787 cl->ms.sym->name, cl->srcline);
788 else
789 printed = scnprintf(bf, bfsize, "%s", cl->ms.sym->name);
790 } else
791 printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);
793 if (show_dso)
794 scnprintf(bf + printed, bfsize - printed, " %s",
795 cl->ms.map ?
796 cl->ms.map->dso->short_name :
797 "unknown");
799 return bf;
802 static void free_callchain_node(struct callchain_node *node)
804 struct callchain_list *list, *tmp;
805 struct callchain_node *child;
806 struct rb_node *n;
808 list_for_each_entry_safe(list, tmp, &node->val, list) {
809 list_del(&list->list);
810 free(list);
813 n = rb_first(&node->rb_root_in);
814 while (n) {
815 child = container_of(n, struct callchain_node, rb_node_in);
816 n = rb_next(n);
817 rb_erase(&child->rb_node_in, &node->rb_root_in);
819 free_callchain_node(child);
820 free(child);
824 void free_callchain(struct callchain_root *root)
826 if (!symbol_conf.use_callchain)
827 return;
829 free_callchain_node(&root->node);