blob: aa248dcb44406e17d918f13ffc31bcf4cee582d3 [file] [log] [blame]
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +02001/*
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01002 * Copyright (C) 2009-2011, Frederic Weisbecker <fweisbec@gmail.com>
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +02003 *
4 * Handle the callchains from the stream in an ad-hoc radix tree and then
5 * sort them in an rbtree.
6 *
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +02007 * 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.
9 *
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020010 */
11
12#include <stdlib.h>
13#include <stdio.h>
14#include <stdbool.h>
15#include <errno.h>
Frederic Weisbeckerc0a88652009-08-09 04:19:15 +020016#include <math.h>
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020017
Frederic Weisbeckerb965bb42014-01-14 16:37:15 +010018#include "asm/bug.h"
19
Andi Kleen99571ab2013-07-18 15:33:57 -070020#include "hist.h"
Arnaldo Carvalho de Melob36f19d2010-05-20 12:15:33 -030021#include "util.h"
Namhyung Kim2dc9fb12014-01-14 14:25:35 +090022#include "sort.h"
23#include "machine.h"
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020024#include "callchain.h"
25
Namhyung Kim47260642012-05-31 14:43:26 +090026__thread struct callchain_cursor callchain_cursor;
27
Kan Liangc3a6a8c2015-08-04 04:30:20 -040028int parse_callchain_record_opt(const char *arg, struct callchain_param *param)
Namhyung Kimf7f084f2014-09-23 10:01:42 +090029{
Kan Liang076a30c2015-08-06 15:44:52 -040030 return parse_callchain_record(arg, param);
Namhyung Kimf7f084f2014-09-23 10:01:42 +090031}
32
Namhyung Kim2b9240c2014-09-23 10:01:43 +090033static int parse_callchain_mode(const char *value)
34{
35 if (!strncmp(value, "graph", strlen(value))) {
36 callchain_param.mode = CHAIN_GRAPH_ABS;
37 return 0;
38 }
39 if (!strncmp(value, "flat", strlen(value))) {
40 callchain_param.mode = CHAIN_FLAT;
41 return 0;
42 }
43 if (!strncmp(value, "fractal", strlen(value))) {
44 callchain_param.mode = CHAIN_GRAPH_REL;
45 return 0;
46 }
Namhyung Kim26e77922015-11-09 14:45:37 +090047 if (!strncmp(value, "folded", strlen(value))) {
48 callchain_param.mode = CHAIN_FOLDED;
49 return 0;
50 }
Namhyung Kim2b9240c2014-09-23 10:01:43 +090051 return -1;
52}
53
54static int parse_callchain_order(const char *value)
55{
56 if (!strncmp(value, "caller", strlen(value))) {
57 callchain_param.order = ORDER_CALLER;
Namhyung Kim792aeaf2015-10-22 16:45:46 +090058 callchain_param.order_set = true;
Namhyung Kim2b9240c2014-09-23 10:01:43 +090059 return 0;
60 }
61 if (!strncmp(value, "callee", strlen(value))) {
62 callchain_param.order = ORDER_CALLEE;
Namhyung Kim792aeaf2015-10-22 16:45:46 +090063 callchain_param.order_set = true;
Namhyung Kim2b9240c2014-09-23 10:01:43 +090064 return 0;
65 }
66 return -1;
67}
68
69static int parse_callchain_sort_key(const char *value)
70{
71 if (!strncmp(value, "function", strlen(value))) {
72 callchain_param.key = CCKEY_FUNCTION;
73 return 0;
74 }
75 if (!strncmp(value, "address", strlen(value))) {
76 callchain_param.key = CCKEY_ADDRESS;
77 return 0;
78 }
Andi Kleen8b7bad52014-11-12 18:05:20 -080079 if (!strncmp(value, "branch", strlen(value))) {
80 callchain_param.branch_callstack = 1;
81 return 0;
82 }
Namhyung Kim2b9240c2014-09-23 10:01:43 +090083 return -1;
84}
85
Namhyung Kimf2af0082015-11-09 14:45:41 +090086static int parse_callchain_value(const char *value)
87{
88 if (!strncmp(value, "percent", strlen(value))) {
89 callchain_param.value = CCVAL_PERCENT;
90 return 0;
91 }
92 if (!strncmp(value, "period", strlen(value))) {
93 callchain_param.value = CCVAL_PERIOD;
94 return 0;
95 }
96 if (!strncmp(value, "count", strlen(value))) {
97 callchain_param.value = CCVAL_COUNT;
98 return 0;
99 }
100 return -1;
101}
102
Namhyung Kima2c10d32015-10-22 15:28:49 +0900103static int
104__parse_callchain_report_opt(const char *arg, bool allow_record_opt)
Don Zickuscff6bb42014-04-07 14:55:24 -0400105{
Namhyung Kime8232f12014-08-14 15:01:38 +0900106 char *tok;
Don Zickuscff6bb42014-04-07 14:55:24 -0400107 char *endptr;
Namhyung Kime8232f12014-08-14 15:01:38 +0900108 bool minpcnt_set = false;
Namhyung Kima2c10d32015-10-22 15:28:49 +0900109 bool record_opt_set = false;
110 bool try_stack_size = false;
Don Zickuscff6bb42014-04-07 14:55:24 -0400111
Arnaldo Carvalho de Melo30234f02016-04-18 11:53:07 -0300112 callchain_param.enabled = true;
Don Zickuscff6bb42014-04-07 14:55:24 -0400113 symbol_conf.use_callchain = true;
114
115 if (!arg)
116 return 0;
117
Namhyung Kime8232f12014-08-14 15:01:38 +0900118 while ((tok = strtok((char *)arg, ",")) != NULL) {
119 if (!strncmp(tok, "none", strlen(tok))) {
120 callchain_param.mode = CHAIN_NONE;
Arnaldo Carvalho de Melo30234f02016-04-18 11:53:07 -0300121 callchain_param.enabled = false;
Namhyung Kime8232f12014-08-14 15:01:38 +0900122 symbol_conf.use_callchain = false;
123 return 0;
124 }
Don Zickuscff6bb42014-04-07 14:55:24 -0400125
Namhyung Kim2b9240c2014-09-23 10:01:43 +0900126 if (!parse_callchain_mode(tok) ||
127 !parse_callchain_order(tok) ||
Namhyung Kimf2af0082015-11-09 14:45:41 +0900128 !parse_callchain_sort_key(tok) ||
129 !parse_callchain_value(tok)) {
Namhyung Kim2b9240c2014-09-23 10:01:43 +0900130 /* parsing ok - move on to the next */
Namhyung Kima2c10d32015-10-22 15:28:49 +0900131 try_stack_size = false;
132 goto next;
133 } else if (allow_record_opt && !record_opt_set) {
134 if (parse_callchain_record(tok, &callchain_param))
135 goto try_numbers;
136
137 /* assume that number followed by 'dwarf' is stack size */
138 if (callchain_param.record_mode == CALLCHAIN_DWARF)
139 try_stack_size = true;
140
141 record_opt_set = true;
142 goto next;
143 }
144
145try_numbers:
146 if (try_stack_size) {
147 unsigned long size = 0;
148
149 if (get_stack_size(tok, &size) < 0)
150 return -1;
151 callchain_param.dump_size = size;
152 try_stack_size = false;
Namhyung Kim2b9240c2014-09-23 10:01:43 +0900153 } else if (!minpcnt_set) {
154 /* try to get the min percent */
Namhyung Kime8232f12014-08-14 15:01:38 +0900155 callchain_param.min_percent = strtod(tok, &endptr);
156 if (tok == endptr)
157 return -1;
158 minpcnt_set = true;
159 } else {
160 /* try print limit at last */
161 callchain_param.print_limit = strtoul(tok, &endptr, 0);
162 if (tok == endptr)
163 return -1;
164 }
Namhyung Kima2c10d32015-10-22 15:28:49 +0900165next:
Namhyung Kime8232f12014-08-14 15:01:38 +0900166 arg = NULL;
Don Zickuscff6bb42014-04-07 14:55:24 -0400167 }
168
Don Zickuscff6bb42014-04-07 14:55:24 -0400169 if (callchain_register_param(&callchain_param) < 0) {
170 pr_err("Can't register callchain params\n");
171 return -1;
172 }
173 return 0;
174}
175
Namhyung Kima2c10d32015-10-22 15:28:49 +0900176int parse_callchain_report_opt(const char *arg)
177{
178 return __parse_callchain_report_opt(arg, false);
179}
180
181int parse_callchain_top_opt(const char *arg)
182{
183 return __parse_callchain_report_opt(arg, true);
184}
185
Namhyung Kim2b9240c2014-09-23 10:01:43 +0900186int perf_callchain_config(const char *var, const char *value)
187{
188 char *endptr;
189
190 if (prefixcmp(var, "call-graph."))
191 return 0;
192 var += sizeof("call-graph.") - 1;
193
194 if (!strcmp(var, "record-mode"))
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400195 return parse_callchain_record_opt(value, &callchain_param);
Namhyung Kim2b9240c2014-09-23 10:01:43 +0900196#ifdef HAVE_DWARF_UNWIND_SUPPORT
197 if (!strcmp(var, "dump-size")) {
198 unsigned long size = 0;
199 int ret;
200
201 ret = get_stack_size(value, &size);
202 callchain_param.dump_size = size;
203
204 return ret;
205 }
206#endif
207 if (!strcmp(var, "print-type"))
208 return parse_callchain_mode(value);
209 if (!strcmp(var, "order"))
210 return parse_callchain_order(value);
211 if (!strcmp(var, "sort-key"))
212 return parse_callchain_sort_key(value);
213 if (!strcmp(var, "threshold")) {
214 callchain_param.min_percent = strtod(value, &endptr);
215 if (value == endptr)
216 return -1;
217 }
218 if (!strcmp(var, "print-limit")) {
219 callchain_param.print_limit = strtod(value, &endptr);
220 if (value == endptr)
221 return -1;
222 }
223
224 return 0;
225}
226
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200227static void
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200228rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
229 enum chain_mode mode)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200230{
231 struct rb_node **p = &root->rb_node;
232 struct rb_node *parent = NULL;
233 struct callchain_node *rnode;
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100234 u64 chain_cumul = callchain_cumul_hits(chain);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200235
236 while (*p) {
Frederic Weisbecker19532872009-08-07 07:11:05 +0200237 u64 rnode_cumul;
238
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200239 parent = *p;
240 rnode = rb_entry(parent, struct callchain_node, rb_node);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100241 rnode_cumul = callchain_cumul_hits(rnode);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200242
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200243 switch (mode) {
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200244 case CHAIN_FLAT:
Namhyung Kim26e77922015-11-09 14:45:37 +0900245 case CHAIN_FOLDED:
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200246 if (rnode->hit < chain->hit)
247 p = &(*p)->rb_left;
248 else
249 p = &(*p)->rb_right;
250 break;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200251 case CHAIN_GRAPH_ABS: /* Falldown */
252 case CHAIN_GRAPH_REL:
Frederic Weisbecker19532872009-08-07 07:11:05 +0200253 if (rnode_cumul < chain_cumul)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200254 p = &(*p)->rb_left;
255 else
256 p = &(*p)->rb_right;
257 break;
Ingo Molnar83a09442009-08-15 12:26:57 +0200258 case CHAIN_NONE:
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200259 default:
260 break;
261 }
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200262 }
263
264 rb_link_node(&chain->rb_node, parent, p);
265 rb_insert_color(&chain->rb_node, root);
266}
267
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200268static void
269__sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
270 u64 min_hit)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200271{
Namhyung Kime3695172013-10-11 14:15:36 +0900272 struct rb_node *n;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200273 struct callchain_node *child;
274
Namhyung Kime3695172013-10-11 14:15:36 +0900275 n = rb_first(&node->rb_root_in);
276 while (n) {
277 child = rb_entry(n, struct callchain_node, rb_node_in);
278 n = rb_next(n);
279
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200280 __sort_chain_flat(rb_root, child, min_hit);
Namhyung Kime3695172013-10-11 14:15:36 +0900281 }
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200282
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200283 if (node->hit && node->hit >= min_hit)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200284 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200285}
286
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200287/*
288 * Once we get every callchains from the stream, we can now
289 * sort them by hit
290 */
291static void
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200292sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300293 u64 min_hit, struct callchain_param *param __maybe_unused)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200294{
Namhyung Kim03562182015-11-26 16:08:18 +0900295 *rb_root = RB_ROOT;
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200296 __sort_chain_flat(rb_root, &root->node, min_hit);
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200297}
298
299static void __sort_chain_graph_abs(struct callchain_node *node,
300 u64 min_hit)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200301{
Namhyung Kime3695172013-10-11 14:15:36 +0900302 struct rb_node *n;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200303 struct callchain_node *child;
304
305 node->rb_root = RB_ROOT;
Namhyung Kime3695172013-10-11 14:15:36 +0900306 n = rb_first(&node->rb_root_in);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200307
Namhyung Kime3695172013-10-11 14:15:36 +0900308 while (n) {
309 child = rb_entry(n, struct callchain_node, rb_node_in);
310 n = rb_next(n);
311
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200312 __sort_chain_graph_abs(child, min_hit);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100313 if (callchain_cumul_hits(child) >= min_hit)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200314 rb_insert_callchain(&node->rb_root, child,
315 CHAIN_GRAPH_ABS);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200316 }
317}
318
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200319static void
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200320sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300321 u64 min_hit, struct callchain_param *param __maybe_unused)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200322{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200323 __sort_chain_graph_abs(&chain_root->node, min_hit);
324 rb_root->rb_node = chain_root->node.rb_root.rb_node;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200325}
326
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200327static void __sort_chain_graph_rel(struct callchain_node *node,
328 double min_percent)
329{
Namhyung Kime3695172013-10-11 14:15:36 +0900330 struct rb_node *n;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200331 struct callchain_node *child;
332 u64 min_hit;
333
334 node->rb_root = RB_ROOT;
Frederic Weisbeckerc0a88652009-08-09 04:19:15 +0200335 min_hit = ceil(node->children_hit * min_percent);
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200336
Namhyung Kime3695172013-10-11 14:15:36 +0900337 n = rb_first(&node->rb_root_in);
338 while (n) {
339 child = rb_entry(n, struct callchain_node, rb_node_in);
340 n = rb_next(n);
341
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200342 __sort_chain_graph_rel(child, min_percent);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100343 if (callchain_cumul_hits(child) >= min_hit)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200344 rb_insert_callchain(&node->rb_root, child,
345 CHAIN_GRAPH_REL);
346 }
347}
348
349static void
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200350sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300351 u64 min_hit __maybe_unused, struct callchain_param *param)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200352{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200353 __sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0);
354 rb_root->rb_node = chain_root->node.rb_root.rb_node;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200355}
356
Frederic Weisbecker16537f12011-01-14 04:52:00 +0100357int callchain_register_param(struct callchain_param *param)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200358{
359 switch (param->mode) {
360 case CHAIN_GRAPH_ABS:
361 param->sort = sort_chain_graph_abs;
362 break;
363 case CHAIN_GRAPH_REL:
364 param->sort = sort_chain_graph_rel;
365 break;
366 case CHAIN_FLAT:
Namhyung Kim26e77922015-11-09 14:45:37 +0900367 case CHAIN_FOLDED:
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200368 param->sort = sort_chain_flat;
369 break;
Ingo Molnar83a09442009-08-15 12:26:57 +0200370 case CHAIN_NONE:
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200371 default:
372 return -1;
373 }
374 return 0;
375}
376
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200377/*
378 * Create a child for a parent. If inherit_children, then the new child
379 * will become the new parent of it's parent children
380 */
381static struct callchain_node *
382create_child(struct callchain_node *parent, bool inherit_children)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200383{
384 struct callchain_node *new;
385
Arnaldo Carvalho de Melocdd5b75b2010-05-10 10:56:50 -0300386 new = zalloc(sizeof(*new));
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200387 if (!new) {
388 perror("not enough memory to create child for code path tree");
389 return NULL;
390 }
391 new->parent = parent;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200392 INIT_LIST_HEAD(&new->val);
Namhyung Kim4b3a3212015-11-09 14:45:43 +0900393 INIT_LIST_HEAD(&new->parent_val);
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200394
395 if (inherit_children) {
Namhyung Kime3695172013-10-11 14:15:36 +0900396 struct rb_node *n;
397 struct callchain_node *child;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200398
Namhyung Kime3695172013-10-11 14:15:36 +0900399 new->rb_root_in = parent->rb_root_in;
400 parent->rb_root_in = RB_ROOT;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200401
Namhyung Kime3695172013-10-11 14:15:36 +0900402 n = rb_first(&new->rb_root_in);
403 while (n) {
404 child = rb_entry(n, struct callchain_node, rb_node_in);
405 child->parent = new;
406 n = rb_next(n);
407 }
408
409 /* make it the first child */
410 rb_link_node(&new->rb_node_in, NULL, &parent->rb_root_in.rb_node);
411 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200412 }
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200413
414 return new;
415}
416
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300417
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200418/*
419 * Fill the node with callchain values
420 */
Namhyung Kim8451cbb2016-02-16 23:08:21 +0900421static int
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100422fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200423{
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100424 struct callchain_cursor_node *cursor_node;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200425
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100426 node->val_nr = cursor->nr - cursor->pos;
427 if (!node->val_nr)
428 pr_warning("Warning: empty node in callchain tree\n");
429
430 cursor_node = callchain_cursor_current(cursor);
431
432 while (cursor_node) {
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200433 struct callchain_list *call;
434
Arnaldo Carvalho de Melocdd5b75b2010-05-10 10:56:50 -0300435 call = zalloc(sizeof(*call));
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200436 if (!call) {
437 perror("not enough memory for the code path tree");
Namhyung Kim8451cbb2016-02-16 23:08:21 +0900438 return -1;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200439 }
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100440 call->ip = cursor_node->ip;
441 call->ms.sym = cursor_node->sym;
442 call->ms.map = cursor_node->map;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200443 list_add_tail(&call->list, &node->val);
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100444
445 callchain_cursor_advance(cursor);
446 cursor_node = callchain_cursor_current(cursor);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200447 }
Namhyung Kim8451cbb2016-02-16 23:08:21 +0900448 return 0;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200449}
450
Namhyung Kime3695172013-10-11 14:15:36 +0900451static struct callchain_node *
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100452add_child(struct callchain_node *parent,
453 struct callchain_cursor *cursor,
454 u64 period)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200455{
456 struct callchain_node *new;
457
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200458 new = create_child(parent, false);
Namhyung Kim7565bd32016-02-16 23:08:20 +0900459 if (new == NULL)
460 return NULL;
461
Namhyung Kim8451cbb2016-02-16 23:08:21 +0900462 if (fill_node(new, cursor) < 0) {
463 struct callchain_list *call, *tmp;
464
465 list_for_each_entry_safe(call, tmp, &new->val, list) {
466 list_del(&call->list);
467 free(call);
468 }
469 free(new);
470 return NULL;
471 }
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200472
Frederic Weisbecker19532872009-08-07 07:11:05 +0200473 new->children_hit = 0;
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200474 new->hit = period;
Namhyung Kim5e47f8f2015-11-09 14:45:40 +0900475 new->children_count = 0;
476 new->count = 1;
Namhyung Kime3695172013-10-11 14:15:36 +0900477 return new;
478}
479
Namhyung Kim2d713b82016-02-16 23:08:22 +0900480enum match_result {
481 MATCH_ERROR = -1,
482 MATCH_EQ,
483 MATCH_LT,
484 MATCH_GT,
485};
486
487static enum match_result match_chain(struct callchain_cursor_node *node,
488 struct callchain_list *cnode)
Namhyung Kime3695172013-10-11 14:15:36 +0900489{
490 struct symbol *sym = node->sym;
Namhyung Kim2d713b82016-02-16 23:08:22 +0900491 u64 left, right;
Namhyung Kime3695172013-10-11 14:15:36 +0900492
493 if (cnode->ms.sym && sym &&
Namhyung Kim2d713b82016-02-16 23:08:22 +0900494 callchain_param.key == CCKEY_FUNCTION) {
495 left = cnode->ms.sym->start;
496 right = sym->start;
497 } else {
498 left = cnode->ip;
499 right = node->ip;
500 }
501
502 if (left == right)
503 return MATCH_EQ;
504
505 return left > right ? MATCH_GT : MATCH_LT;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200506}
507
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200508/*
509 * Split the parent in two parts (a new child is created) and
510 * give a part of its callchain to the created child.
511 * Then create another child to host the given callchain of new branch
512 */
Namhyung Kimf2bb4c52016-02-16 23:08:23 +0900513static int
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100514split_add_child(struct callchain_node *parent,
515 struct callchain_cursor *cursor,
516 struct callchain_list *to_split,
517 u64 idx_parents, u64 idx_local, u64 period)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200518{
519 struct callchain_node *new;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200520 struct list_head *old_tail;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200521 unsigned int idx_total = idx_parents + idx_local;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200522
523 /* split */
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200524 new = create_child(parent, true);
Namhyung Kimf2bb4c52016-02-16 23:08:23 +0900525 if (new == NULL)
526 return -1;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200527
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200528 /* split the callchain and move a part to the new child */
529 old_tail = parent->val.prev;
530 list_del_range(&to_split->list, old_tail);
531 new->val.next = &to_split->list;
532 new->val.prev = old_tail;
533 to_split->list.prev = &new->val;
534 old_tail->next = &new->val;
535
536 /* split the hits */
537 new->hit = parent->hit;
Frederic Weisbecker19532872009-08-07 07:11:05 +0200538 new->children_hit = parent->children_hit;
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100539 parent->children_hit = callchain_cumul_hits(new);
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200540 new->val_nr = parent->val_nr - idx_local;
541 parent->val_nr = idx_local;
Namhyung Kim5e47f8f2015-11-09 14:45:40 +0900542 new->count = parent->count;
543 new->children_count = parent->children_count;
544 parent->children_count = callchain_cumul_counts(new);
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200545
546 /* create a new child for the new branch if any */
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100547 if (idx_total < cursor->nr) {
Namhyung Kime3695172013-10-11 14:15:36 +0900548 struct callchain_node *first;
549 struct callchain_list *cnode;
550 struct callchain_cursor_node *node;
551 struct rb_node *p, **pp;
552
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200553 parent->hit = 0;
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200554 parent->children_hit += period;
Namhyung Kim5e47f8f2015-11-09 14:45:40 +0900555 parent->count = 0;
556 parent->children_count += 1;
Namhyung Kime3695172013-10-11 14:15:36 +0900557
558 node = callchain_cursor_current(cursor);
559 new = add_child(parent, cursor, period);
Namhyung Kim7565bd32016-02-16 23:08:20 +0900560 if (new == NULL)
Namhyung Kimf2bb4c52016-02-16 23:08:23 +0900561 return -1;
Namhyung Kime3695172013-10-11 14:15:36 +0900562
563 /*
564 * This is second child since we moved parent's children
565 * to new (first) child above.
566 */
567 p = parent->rb_root_in.rb_node;
568 first = rb_entry(p, struct callchain_node, rb_node_in);
569 cnode = list_first_entry(&first->val, struct callchain_list,
570 list);
571
Namhyung Kim2d713b82016-02-16 23:08:22 +0900572 if (match_chain(node, cnode) == MATCH_LT)
Namhyung Kime3695172013-10-11 14:15:36 +0900573 pp = &p->rb_left;
574 else
575 pp = &p->rb_right;
576
577 rb_link_node(&new->rb_node_in, p, pp);
578 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200579 } else {
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200580 parent->hit = period;
Namhyung Kim5e47f8f2015-11-09 14:45:40 +0900581 parent->count = 1;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200582 }
Namhyung Kimf2bb4c52016-02-16 23:08:23 +0900583 return 0;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200584}
585
Namhyung Kim2d713b82016-02-16 23:08:22 +0900586static enum match_result
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100587append_chain(struct callchain_node *root,
588 struct callchain_cursor *cursor,
589 u64 period);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200590
Namhyung Kimdca0d122016-02-16 23:08:24 +0900591static int
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100592append_chain_children(struct callchain_node *root,
593 struct callchain_cursor *cursor,
594 u64 period)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200595{
596 struct callchain_node *rnode;
Namhyung Kime3695172013-10-11 14:15:36 +0900597 struct callchain_cursor_node *node;
598 struct rb_node **p = &root->rb_root_in.rb_node;
599 struct rb_node *parent = NULL;
600
601 node = callchain_cursor_current(cursor);
602 if (!node)
Namhyung Kimdca0d122016-02-16 23:08:24 +0900603 return -1;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200604
605 /* lookup in childrens */
Namhyung Kime3695172013-10-11 14:15:36 +0900606 while (*p) {
Namhyung Kim2d713b82016-02-16 23:08:22 +0900607 enum match_result ret;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200608
Namhyung Kime3695172013-10-11 14:15:36 +0900609 parent = *p;
610 rnode = rb_entry(parent, struct callchain_node, rb_node_in);
Namhyung Kime3695172013-10-11 14:15:36 +0900611
Frederic Weisbeckerb965bb42014-01-14 16:37:15 +0100612 /* If at least first entry matches, rely to children */
613 ret = append_chain(rnode, cursor, period);
Namhyung Kim2d713b82016-02-16 23:08:22 +0900614 if (ret == MATCH_EQ)
Frederic Weisbecker19532872009-08-07 07:11:05 +0200615 goto inc_children_hit;
Namhyung Kimdca0d122016-02-16 23:08:24 +0900616 if (ret == MATCH_ERROR)
617 return -1;
Namhyung Kime3695172013-10-11 14:15:36 +0900618
Namhyung Kim2d713b82016-02-16 23:08:22 +0900619 if (ret == MATCH_LT)
Namhyung Kime3695172013-10-11 14:15:36 +0900620 p = &parent->rb_left;
621 else
622 p = &parent->rb_right;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200623 }
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200624 /* nothing in children, add to the current node */
Namhyung Kime3695172013-10-11 14:15:36 +0900625 rnode = add_child(root, cursor, period);
Namhyung Kim7565bd32016-02-16 23:08:20 +0900626 if (rnode == NULL)
Namhyung Kimdca0d122016-02-16 23:08:24 +0900627 return -1;
Namhyung Kim7565bd32016-02-16 23:08:20 +0900628
Namhyung Kime3695172013-10-11 14:15:36 +0900629 rb_link_node(&rnode->rb_node_in, parent, p);
630 rb_insert_color(&rnode->rb_node_in, &root->rb_root_in);
Frederic Weisbeckere05b8762009-07-05 07:39:20 +0200631
Frederic Weisbecker19532872009-08-07 07:11:05 +0200632inc_children_hit:
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200633 root->children_hit += period;
Namhyung Kim5e47f8f2015-11-09 14:45:40 +0900634 root->children_count++;
Namhyung Kimdca0d122016-02-16 23:08:24 +0900635 return 0;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200636}
637
Namhyung Kim2d713b82016-02-16 23:08:22 +0900638static enum match_result
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100639append_chain(struct callchain_node *root,
640 struct callchain_cursor *cursor,
641 u64 period)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200642{
643 struct callchain_list *cnode;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100644 u64 start = cursor->pos;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200645 bool found = false;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100646 u64 matches;
Namhyung Kim2d713b82016-02-16 23:08:22 +0900647 enum match_result cmp = MATCH_ERROR;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200648
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200649 /*
650 * Lookup in the current node
651 * If we have a symbol, then compare the start to match
Andi Kleen99571ab2013-07-18 15:33:57 -0700652 * anywhere inside a function, unless function
653 * mode is disabled.
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200654 */
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200655 list_for_each_entry(cnode, &root->val, list) {
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100656 struct callchain_cursor_node *node;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300657
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100658 node = callchain_cursor_current(cursor);
659 if (!node)
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200660 break;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300661
Frederic Weisbeckerb965bb42014-01-14 16:37:15 +0100662 cmp = match_chain(node, cnode);
Namhyung Kim2d713b82016-02-16 23:08:22 +0900663 if (cmp != MATCH_EQ)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200664 break;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300665
Namhyung Kime3695172013-10-11 14:15:36 +0900666 found = true;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100667
668 callchain_cursor_advance(cursor);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200669 }
670
Namhyung Kime3695172013-10-11 14:15:36 +0900671 /* matches not, relay no the parent */
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100672 if (!found) {
Namhyung Kim2d713b82016-02-16 23:08:22 +0900673 WARN_ONCE(cmp == MATCH_ERROR, "Chain comparison error\n");
Frederic Weisbeckerb965bb42014-01-14 16:37:15 +0100674 return cmp;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100675 }
676
677 matches = cursor->pos - start;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200678
679 /* we match only a part of the node. Split it and add the new chain */
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100680 if (matches < root->val_nr) {
Namhyung Kimf2bb4c52016-02-16 23:08:23 +0900681 if (split_add_child(root, cursor, cnode, start, matches,
682 period) < 0)
683 return MATCH_ERROR;
684
Namhyung Kim2d713b82016-02-16 23:08:22 +0900685 return MATCH_EQ;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200686 }
687
688 /* we match 100% of the path, increment the hit */
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100689 if (matches == root->val_nr && cursor->pos == cursor->nr) {
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200690 root->hit += period;
Namhyung Kim5e47f8f2015-11-09 14:45:40 +0900691 root->count++;
Namhyung Kim2d713b82016-02-16 23:08:22 +0900692 return MATCH_EQ;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200693 }
694
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200695 /* We match the node and still have a part remaining */
Namhyung Kimdca0d122016-02-16 23:08:24 +0900696 if (append_chain_children(root, cursor, period) < 0)
697 return MATCH_ERROR;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200698
Namhyung Kim2d713b82016-02-16 23:08:22 +0900699 return MATCH_EQ;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200700}
701
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100702int callchain_append(struct callchain_root *root,
703 struct callchain_cursor *cursor,
704 u64 period)
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300705{
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100706 if (!cursor->nr)
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300707 return 0;
708
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100709 callchain_cursor_commit(cursor);
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300710
Namhyung Kimdca0d122016-02-16 23:08:24 +0900711 if (append_chain_children(&root->node, cursor, period) < 0)
712 return -1;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300713
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100714 if (cursor->nr > root->max_depth)
715 root->max_depth = cursor->nr;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300716
717 return 0;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200718}
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200719
720static int
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100721merge_chain_branch(struct callchain_cursor *cursor,
722 struct callchain_node *dst, struct callchain_node *src)
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200723{
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100724 struct callchain_cursor_node **old_last = cursor->last;
Namhyung Kime3695172013-10-11 14:15:36 +0900725 struct callchain_node *child;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200726 struct callchain_list *list, *next_list;
Namhyung Kime3695172013-10-11 14:15:36 +0900727 struct rb_node *n;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100728 int old_pos = cursor->nr;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200729 int err = 0;
730
731 list_for_each_entry_safe(list, next_list, &src->val, list) {
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100732 callchain_cursor_append(cursor, list->ip,
733 list->ms.map, list->ms.sym);
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200734 list_del(&list->list);
735 free(list);
736 }
737
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100738 if (src->hit) {
739 callchain_cursor_commit(cursor);
Namhyung Kimdca0d122016-02-16 23:08:24 +0900740 if (append_chain_children(dst, cursor, src->hit) < 0)
741 return -1;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100742 }
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200743
Namhyung Kime3695172013-10-11 14:15:36 +0900744 n = rb_first(&src->rb_root_in);
745 while (n) {
746 child = container_of(n, struct callchain_node, rb_node_in);
747 n = rb_next(n);
748 rb_erase(&child->rb_node_in, &src->rb_root_in);
749
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100750 err = merge_chain_branch(cursor, dst, child);
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200751 if (err)
752 break;
753
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200754 free(child);
755 }
756
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100757 cursor->nr = old_pos;
758 cursor->last = old_last;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200759
760 return err;
761}
762
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100763int callchain_merge(struct callchain_cursor *cursor,
764 struct callchain_root *dst, struct callchain_root *src)
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200765{
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100766 return merge_chain_branch(cursor, &dst->node, &src->node);
767}
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200768
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100769int callchain_cursor_append(struct callchain_cursor *cursor,
770 u64 ip, struct map *map, struct symbol *sym)
771{
772 struct callchain_cursor_node *node = *cursor->last;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200773
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100774 if (!node) {
Paul Gortmaker91b98802013-01-30 20:05:49 -0500775 node = calloc(1, sizeof(*node));
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100776 if (!node)
777 return -ENOMEM;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200778
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100779 *cursor->last = node;
780 }
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200781
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100782 node->ip = ip;
783 node->map = map;
784 node->sym = sym;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200785
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100786 cursor->nr++;
787
788 cursor->last = &node->next;
789
790 return 0;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200791}
Namhyung Kim2dc9fb12014-01-14 14:25:35 +0900792
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -0300793int sample__resolve_callchain(struct perf_sample *sample,
794 struct callchain_cursor *cursor, struct symbol **parent,
Namhyung Kim2dc9fb12014-01-14 14:25:35 +0900795 struct perf_evsel *evsel, struct addr_location *al,
796 int max_stack)
797{
798 if (sample->callchain == NULL)
799 return 0;
800
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900801 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain ||
802 sort__has_parent) {
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -0300803 return thread__resolve_callchain(al->thread, cursor, evsel, sample,
Arnaldo Carvalho de Melocc8b7c22014-10-23 15:26:17 -0300804 parent, al, max_stack);
Namhyung Kim2dc9fb12014-01-14 14:25:35 +0900805 }
806 return 0;
807}
808
809int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample)
810{
Adrian Hunter4d40b052014-07-14 13:02:35 +0300811 if (!symbol_conf.use_callchain || sample->callchain == NULL)
Namhyung Kim2dc9fb12014-01-14 14:25:35 +0900812 return 0;
813 return callchain_append(he->callchain, &callchain_cursor, sample->period);
814}
Namhyung Kimc7405d82013-10-31 13:58:30 +0900815
816int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
817 bool hide_unresolved)
818{
819 al->map = node->map;
820 al->sym = node->sym;
821 if (node->map)
822 al->addr = node->map->map_ip(node->map, node->ip);
823 else
824 al->addr = node->ip;
825
826 if (al->sym == NULL) {
827 if (hide_unresolved)
828 return 0;
829 if (al->map == NULL)
830 goto out;
831 }
832
833 if (al->map->groups == &al->machine->kmaps) {
834 if (machine__is_host(al->machine)) {
835 al->cpumode = PERF_RECORD_MISC_KERNEL;
836 al->level = 'k';
837 } else {
838 al->cpumode = PERF_RECORD_MISC_GUEST_KERNEL;
839 al->level = 'g';
840 }
841 } else {
842 if (machine__is_host(al->machine)) {
843 al->cpumode = PERF_RECORD_MISC_USER;
844 al->level = '.';
845 } else if (perf_guest) {
846 al->cpumode = PERF_RECORD_MISC_GUEST_USER;
847 al->level = 'u';
848 } else {
849 al->cpumode = PERF_RECORD_MISC_HYPERVISOR;
850 al->level = 'H';
851 }
852 }
853
854out:
855 return 1;
856}
Andi Kleen2989cca2014-11-12 18:05:23 -0800857
858char *callchain_list__sym_name(struct callchain_list *cl,
859 char *bf, size_t bfsize, bool show_dso)
860{
861 int printed;
862
863 if (cl->ms.sym) {
Andi Kleen23f09812014-11-12 18:05:24 -0800864 if (callchain_param.key == CCKEY_ADDRESS &&
865 cl->ms.map && !cl->srcline)
866 cl->srcline = get_srcline(cl->ms.map->dso,
867 map__rip_2objdump(cl->ms.map,
Andi Kleen85c116a2014-11-12 18:05:27 -0800868 cl->ip),
869 cl->ms.sym, false);
Andi Kleen23f09812014-11-12 18:05:24 -0800870 if (cl->srcline)
871 printed = scnprintf(bf, bfsize, "%s %s",
872 cl->ms.sym->name, cl->srcline);
873 else
874 printed = scnprintf(bf, bfsize, "%s", cl->ms.sym->name);
Andi Kleen2989cca2014-11-12 18:05:23 -0800875 } else
876 printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);
877
878 if (show_dso)
879 scnprintf(bf + printed, bfsize - printed, " %s",
880 cl->ms.map ?
881 cl->ms.map->dso->short_name :
882 "unknown");
883
884 return bf;
885}
Namhyung Kimd1149602014-12-30 14:38:13 +0900886
Namhyung Kim5ab250c2015-11-09 14:45:39 +0900887char *callchain_node__scnprintf_value(struct callchain_node *node,
888 char *bf, size_t bfsize, u64 total)
889{
890 double percent = 0.0;
891 u64 period = callchain_cumul_hits(node);
Namhyung Kimf2af0082015-11-09 14:45:41 +0900892 unsigned count = callchain_cumul_counts(node);
Namhyung Kim5ab250c2015-11-09 14:45:39 +0900893
Namhyung Kimf2af0082015-11-09 14:45:41 +0900894 if (callchain_param.mode == CHAIN_FOLDED) {
Namhyung Kim5ab250c2015-11-09 14:45:39 +0900895 period = node->hit;
Namhyung Kimf2af0082015-11-09 14:45:41 +0900896 count = node->count;
897 }
Namhyung Kim5ab250c2015-11-09 14:45:39 +0900898
Namhyung Kimf2af0082015-11-09 14:45:41 +0900899 switch (callchain_param.value) {
900 case CCVAL_PERIOD:
901 scnprintf(bf, bfsize, "%"PRIu64, period);
902 break;
903 case CCVAL_COUNT:
904 scnprintf(bf, bfsize, "%u", count);
905 break;
906 case CCVAL_PERCENT:
907 default:
908 if (total)
909 percent = period * 100.0 / total;
910 scnprintf(bf, bfsize, "%.2f%%", percent);
911 break;
912 }
Namhyung Kim5ab250c2015-11-09 14:45:39 +0900913 return bf;
914}
915
916int callchain_node__fprintf_value(struct callchain_node *node,
917 FILE *fp, u64 total)
918{
919 double percent = 0.0;
920 u64 period = callchain_cumul_hits(node);
Namhyung Kimf2af0082015-11-09 14:45:41 +0900921 unsigned count = callchain_cumul_counts(node);
Namhyung Kim5ab250c2015-11-09 14:45:39 +0900922
Namhyung Kimf2af0082015-11-09 14:45:41 +0900923 if (callchain_param.mode == CHAIN_FOLDED) {
Namhyung Kim5ab250c2015-11-09 14:45:39 +0900924 period = node->hit;
Namhyung Kimf2af0082015-11-09 14:45:41 +0900925 count = node->count;
926 }
Namhyung Kim5ab250c2015-11-09 14:45:39 +0900927
Namhyung Kimf2af0082015-11-09 14:45:41 +0900928 switch (callchain_param.value) {
929 case CCVAL_PERIOD:
930 return fprintf(fp, "%"PRIu64, period);
931 case CCVAL_COUNT:
932 return fprintf(fp, "%u", count);
933 case CCVAL_PERCENT:
934 default:
935 if (total)
936 percent = period * 100.0 / total;
937 return percent_color_fprintf(fp, "%.2f%%", percent);
938 }
939 return 0;
Namhyung Kim5ab250c2015-11-09 14:45:39 +0900940}
941
Namhyung Kimd1149602014-12-30 14:38:13 +0900942static void free_callchain_node(struct callchain_node *node)
943{
944 struct callchain_list *list, *tmp;
945 struct callchain_node *child;
946 struct rb_node *n;
947
Namhyung Kim4b3a3212015-11-09 14:45:43 +0900948 list_for_each_entry_safe(list, tmp, &node->parent_val, list) {
949 list_del(&list->list);
950 free(list);
951 }
952
Namhyung Kimd1149602014-12-30 14:38:13 +0900953 list_for_each_entry_safe(list, tmp, &node->val, list) {
954 list_del(&list->list);
955 free(list);
956 }
957
958 n = rb_first(&node->rb_root_in);
959 while (n) {
960 child = container_of(n, struct callchain_node, rb_node_in);
961 n = rb_next(n);
962 rb_erase(&child->rb_node_in, &node->rb_root_in);
963
964 free_callchain_node(child);
965 free(child);
966 }
967}
968
969void free_callchain(struct callchain_root *root)
970{
971 if (!symbol_conf.use_callchain)
972 return;
973
974 free_callchain_node(&root->node);
975}
Namhyung Kim4b3a3212015-11-09 14:45:43 +0900976
Namhyung Kim42b276a2016-01-05 12:06:00 +0900977static u64 decay_callchain_node(struct callchain_node *node)
978{
979 struct callchain_node *child;
980 struct rb_node *n;
981 u64 child_hits = 0;
982
983 n = rb_first(&node->rb_root_in);
984 while (n) {
985 child = container_of(n, struct callchain_node, rb_node_in);
986
987 child_hits += decay_callchain_node(child);
988 n = rb_next(n);
989 }
990
991 node->hit = (node->hit * 7) / 8;
992 node->children_hit = child_hits;
993
994 return node->hit;
995}
996
997void decay_callchain(struct callchain_root *root)
998{
999 if (!symbol_conf.use_callchain)
1000 return;
1001
1002 decay_callchain_node(&root->node);
1003}
1004
Namhyung Kim4b3a3212015-11-09 14:45:43 +09001005int callchain_node__make_parent_list(struct callchain_node *node)
1006{
1007 struct callchain_node *parent = node->parent;
1008 struct callchain_list *chain, *new;
1009 LIST_HEAD(head);
1010
1011 while (parent) {
1012 list_for_each_entry_reverse(chain, &parent->val, list) {
1013 new = malloc(sizeof(*new));
1014 if (new == NULL)
1015 goto out;
1016 *new = *chain;
1017 new->has_children = false;
1018 list_add_tail(&new->list, &head);
1019 }
1020 parent = parent->parent;
1021 }
1022
1023 list_for_each_entry_safe_reverse(chain, new, &head, list)
1024 list_move_tail(&chain->list, &node->parent_val);
1025
1026 if (!list_empty(&node->parent_val)) {
1027 chain = list_first_entry(&node->parent_val, struct callchain_list, list);
1028 chain->has_children = rb_prev(&node->rb_node) || rb_next(&node->rb_node);
1029
1030 chain = list_first_entry(&node->val, struct callchain_list, list);
1031 chain->has_children = false;
1032 }
1033 return 0;
1034
1035out:
1036 list_for_each_entry_safe(chain, new, &head, list) {
1037 list_del(&chain->list);
1038 free(chain);
1039 }
1040 return -ENOMEM;
1041}