blob: 42922512c1c62fe09eb12f301c281ceb57c2bf3c [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 if (!strcmp(var, "dump-size")) {
197 unsigned long size = 0;
198 int ret;
199
200 ret = get_stack_size(value, &size);
201 callchain_param.dump_size = size;
202
203 return ret;
204 }
Namhyung Kim2b9240c2014-09-23 10:01:43 +0900205 if (!strcmp(var, "print-type"))
206 return parse_callchain_mode(value);
207 if (!strcmp(var, "order"))
208 return parse_callchain_order(value);
209 if (!strcmp(var, "sort-key"))
210 return parse_callchain_sort_key(value);
211 if (!strcmp(var, "threshold")) {
212 callchain_param.min_percent = strtod(value, &endptr);
213 if (value == endptr)
214 return -1;
215 }
216 if (!strcmp(var, "print-limit")) {
217 callchain_param.print_limit = strtod(value, &endptr);
218 if (value == endptr)
219 return -1;
220 }
221
222 return 0;
223}
224
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200225static void
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200226rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
227 enum chain_mode mode)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200228{
229 struct rb_node **p = &root->rb_node;
230 struct rb_node *parent = NULL;
231 struct callchain_node *rnode;
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100232 u64 chain_cumul = callchain_cumul_hits(chain);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200233
234 while (*p) {
Frederic Weisbecker19532872009-08-07 07:11:05 +0200235 u64 rnode_cumul;
236
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200237 parent = *p;
238 rnode = rb_entry(parent, struct callchain_node, rb_node);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100239 rnode_cumul = callchain_cumul_hits(rnode);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200240
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200241 switch (mode) {
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200242 case CHAIN_FLAT:
Namhyung Kim26e77922015-11-09 14:45:37 +0900243 case CHAIN_FOLDED:
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200244 if (rnode->hit < chain->hit)
245 p = &(*p)->rb_left;
246 else
247 p = &(*p)->rb_right;
248 break;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200249 case CHAIN_GRAPH_ABS: /* Falldown */
250 case CHAIN_GRAPH_REL:
Frederic Weisbecker19532872009-08-07 07:11:05 +0200251 if (rnode_cumul < chain_cumul)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200252 p = &(*p)->rb_left;
253 else
254 p = &(*p)->rb_right;
255 break;
Ingo Molnar83a09442009-08-15 12:26:57 +0200256 case CHAIN_NONE:
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200257 default:
258 break;
259 }
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200260 }
261
262 rb_link_node(&chain->rb_node, parent, p);
263 rb_insert_color(&chain->rb_node, root);
264}
265
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200266static void
267__sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
268 u64 min_hit)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200269{
Namhyung Kime3695172013-10-11 14:15:36 +0900270 struct rb_node *n;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200271 struct callchain_node *child;
272
Namhyung Kime3695172013-10-11 14:15:36 +0900273 n = rb_first(&node->rb_root_in);
274 while (n) {
275 child = rb_entry(n, struct callchain_node, rb_node_in);
276 n = rb_next(n);
277
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200278 __sort_chain_flat(rb_root, child, min_hit);
Namhyung Kime3695172013-10-11 14:15:36 +0900279 }
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200280
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200281 if (node->hit && node->hit >= min_hit)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200282 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200283}
284
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200285/*
286 * Once we get every callchains from the stream, we can now
287 * sort them by hit
288 */
289static void
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200290sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300291 u64 min_hit, struct callchain_param *param __maybe_unused)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200292{
Namhyung Kim03562182015-11-26 16:08:18 +0900293 *rb_root = RB_ROOT;
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200294 __sort_chain_flat(rb_root, &root->node, min_hit);
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200295}
296
297static void __sort_chain_graph_abs(struct callchain_node *node,
298 u64 min_hit)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200299{
Namhyung Kime3695172013-10-11 14:15:36 +0900300 struct rb_node *n;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200301 struct callchain_node *child;
302
303 node->rb_root = RB_ROOT;
Namhyung Kime3695172013-10-11 14:15:36 +0900304 n = rb_first(&node->rb_root_in);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200305
Namhyung Kime3695172013-10-11 14:15:36 +0900306 while (n) {
307 child = rb_entry(n, struct callchain_node, rb_node_in);
308 n = rb_next(n);
309
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200310 __sort_chain_graph_abs(child, min_hit);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100311 if (callchain_cumul_hits(child) >= min_hit)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200312 rb_insert_callchain(&node->rb_root, child,
313 CHAIN_GRAPH_ABS);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200314 }
315}
316
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200317static void
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200318sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300319 u64 min_hit, struct callchain_param *param __maybe_unused)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200320{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200321 __sort_chain_graph_abs(&chain_root->node, min_hit);
322 rb_root->rb_node = chain_root->node.rb_root.rb_node;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200323}
324
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200325static void __sort_chain_graph_rel(struct callchain_node *node,
326 double min_percent)
327{
Namhyung Kime3695172013-10-11 14:15:36 +0900328 struct rb_node *n;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200329 struct callchain_node *child;
330 u64 min_hit;
331
332 node->rb_root = RB_ROOT;
Frederic Weisbeckerc0a88652009-08-09 04:19:15 +0200333 min_hit = ceil(node->children_hit * min_percent);
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200334
Namhyung Kime3695172013-10-11 14:15:36 +0900335 n = rb_first(&node->rb_root_in);
336 while (n) {
337 child = rb_entry(n, struct callchain_node, rb_node_in);
338 n = rb_next(n);
339
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200340 __sort_chain_graph_rel(child, min_percent);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100341 if (callchain_cumul_hits(child) >= min_hit)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200342 rb_insert_callchain(&node->rb_root, child,
343 CHAIN_GRAPH_REL);
344 }
345}
346
347static void
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200348sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300349 u64 min_hit __maybe_unused, struct callchain_param *param)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200350{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200351 __sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0);
352 rb_root->rb_node = chain_root->node.rb_root.rb_node;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200353}
354
Frederic Weisbecker16537f12011-01-14 04:52:00 +0100355int callchain_register_param(struct callchain_param *param)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200356{
357 switch (param->mode) {
358 case CHAIN_GRAPH_ABS:
359 param->sort = sort_chain_graph_abs;
360 break;
361 case CHAIN_GRAPH_REL:
362 param->sort = sort_chain_graph_rel;
363 break;
364 case CHAIN_FLAT:
Namhyung Kim26e77922015-11-09 14:45:37 +0900365 case CHAIN_FOLDED:
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200366 param->sort = sort_chain_flat;
367 break;
Ingo Molnar83a09442009-08-15 12:26:57 +0200368 case CHAIN_NONE:
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200369 default:
370 return -1;
371 }
372 return 0;
373}
374
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200375/*
376 * Create a child for a parent. If inherit_children, then the new child
377 * will become the new parent of it's parent children
378 */
379static struct callchain_node *
380create_child(struct callchain_node *parent, bool inherit_children)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200381{
382 struct callchain_node *new;
383
Arnaldo Carvalho de Melocdd5b75b2010-05-10 10:56:50 -0300384 new = zalloc(sizeof(*new));
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200385 if (!new) {
386 perror("not enough memory to create child for code path tree");
387 return NULL;
388 }
389 new->parent = parent;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200390 INIT_LIST_HEAD(&new->val);
Namhyung Kim4b3a3212015-11-09 14:45:43 +0900391 INIT_LIST_HEAD(&new->parent_val);
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200392
393 if (inherit_children) {
Namhyung Kime3695172013-10-11 14:15:36 +0900394 struct rb_node *n;
395 struct callchain_node *child;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200396
Namhyung Kime3695172013-10-11 14:15:36 +0900397 new->rb_root_in = parent->rb_root_in;
398 parent->rb_root_in = RB_ROOT;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200399
Namhyung Kime3695172013-10-11 14:15:36 +0900400 n = rb_first(&new->rb_root_in);
401 while (n) {
402 child = rb_entry(n, struct callchain_node, rb_node_in);
403 child->parent = new;
404 n = rb_next(n);
405 }
406
407 /* make it the first child */
408 rb_link_node(&new->rb_node_in, NULL, &parent->rb_root_in.rb_node);
409 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200410 }
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200411
412 return new;
413}
414
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300415
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200416/*
417 * Fill the node with callchain values
418 */
Namhyung Kim8451cbb2016-02-16 23:08:21 +0900419static int
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100420fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200421{
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100422 struct callchain_cursor_node *cursor_node;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200423
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100424 node->val_nr = cursor->nr - cursor->pos;
425 if (!node->val_nr)
426 pr_warning("Warning: empty node in callchain tree\n");
427
428 cursor_node = callchain_cursor_current(cursor);
429
430 while (cursor_node) {
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200431 struct callchain_list *call;
432
Arnaldo Carvalho de Melocdd5b75b2010-05-10 10:56:50 -0300433 call = zalloc(sizeof(*call));
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200434 if (!call) {
435 perror("not enough memory for the code path tree");
Namhyung Kim8451cbb2016-02-16 23:08:21 +0900436 return -1;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200437 }
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100438 call->ip = cursor_node->ip;
439 call->ms.sym = cursor_node->sym;
440 call->ms.map = cursor_node->map;
Jin Yao3dd029e2016-10-31 09:19:51 +0800441
442 if (cursor_node->branch) {
443 call->branch_count = 1;
444
445 if (cursor_node->branch_flags.predicted)
446 call->predicted_count = 1;
447
448 if (cursor_node->branch_flags.abort)
449 call->abort_count = 1;
450
451 call->cycles_count = cursor_node->branch_flags.cycles;
452 call->iter_count = cursor_node->nr_loop_iter;
453 call->samples_count = cursor_node->samples;
454 }
455
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200456 list_add_tail(&call->list, &node->val);
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100457
458 callchain_cursor_advance(cursor);
459 cursor_node = callchain_cursor_current(cursor);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200460 }
Namhyung Kim8451cbb2016-02-16 23:08:21 +0900461 return 0;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200462}
463
Namhyung Kime3695172013-10-11 14:15:36 +0900464static struct callchain_node *
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100465add_child(struct callchain_node *parent,
466 struct callchain_cursor *cursor,
467 u64 period)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200468{
469 struct callchain_node *new;
470
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200471 new = create_child(parent, false);
Namhyung Kim7565bd32016-02-16 23:08:20 +0900472 if (new == NULL)
473 return NULL;
474
Namhyung Kim8451cbb2016-02-16 23:08:21 +0900475 if (fill_node(new, cursor) < 0) {
476 struct callchain_list *call, *tmp;
477
478 list_for_each_entry_safe(call, tmp, &new->val, list) {
479 list_del(&call->list);
480 free(call);
481 }
482 free(new);
483 return NULL;
484 }
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200485
Frederic Weisbecker19532872009-08-07 07:11:05 +0200486 new->children_hit = 0;
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200487 new->hit = period;
Namhyung Kim5e47f8f2015-11-09 14:45:40 +0900488 new->children_count = 0;
489 new->count = 1;
Namhyung Kime3695172013-10-11 14:15:36 +0900490 return new;
491}
492
Namhyung Kim2d713b82016-02-16 23:08:22 +0900493enum match_result {
494 MATCH_ERROR = -1,
495 MATCH_EQ,
496 MATCH_LT,
497 MATCH_GT,
498};
499
500static enum match_result match_chain(struct callchain_cursor_node *node,
501 struct callchain_list *cnode)
Namhyung Kime3695172013-10-11 14:15:36 +0900502{
503 struct symbol *sym = node->sym;
Namhyung Kim2d713b82016-02-16 23:08:22 +0900504 u64 left, right;
Namhyung Kime3695172013-10-11 14:15:36 +0900505
506 if (cnode->ms.sym && sym &&
Namhyung Kim2d713b82016-02-16 23:08:22 +0900507 callchain_param.key == CCKEY_FUNCTION) {
508 left = cnode->ms.sym->start;
509 right = sym->start;
510 } else {
511 left = cnode->ip;
512 right = node->ip;
513 }
514
Jin Yao3dd029e2016-10-31 09:19:51 +0800515 if (left == right) {
516 if (node->branch) {
517 cnode->branch_count++;
518
519 if (node->branch_flags.predicted)
520 cnode->predicted_count++;
521
522 if (node->branch_flags.abort)
523 cnode->abort_count++;
524
525 cnode->cycles_count += node->branch_flags.cycles;
526 cnode->iter_count += node->nr_loop_iter;
527 cnode->samples_count += node->samples;
528 }
529
Namhyung Kim2d713b82016-02-16 23:08:22 +0900530 return MATCH_EQ;
Jin Yao3dd029e2016-10-31 09:19:51 +0800531 }
Namhyung Kim2d713b82016-02-16 23:08:22 +0900532
533 return left > right ? MATCH_GT : MATCH_LT;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200534}
535
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200536/*
537 * Split the parent in two parts (a new child is created) and
538 * give a part of its callchain to the created child.
539 * Then create another child to host the given callchain of new branch
540 */
Namhyung Kimf2bb4c52016-02-16 23:08:23 +0900541static int
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100542split_add_child(struct callchain_node *parent,
543 struct callchain_cursor *cursor,
544 struct callchain_list *to_split,
545 u64 idx_parents, u64 idx_local, u64 period)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200546{
547 struct callchain_node *new;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200548 struct list_head *old_tail;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200549 unsigned int idx_total = idx_parents + idx_local;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200550
551 /* split */
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200552 new = create_child(parent, true);
Namhyung Kimf2bb4c52016-02-16 23:08:23 +0900553 if (new == NULL)
554 return -1;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200555
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200556 /* split the callchain and move a part to the new child */
557 old_tail = parent->val.prev;
558 list_del_range(&to_split->list, old_tail);
559 new->val.next = &to_split->list;
560 new->val.prev = old_tail;
561 to_split->list.prev = &new->val;
562 old_tail->next = &new->val;
563
564 /* split the hits */
565 new->hit = parent->hit;
Frederic Weisbecker19532872009-08-07 07:11:05 +0200566 new->children_hit = parent->children_hit;
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100567 parent->children_hit = callchain_cumul_hits(new);
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200568 new->val_nr = parent->val_nr - idx_local;
569 parent->val_nr = idx_local;
Namhyung Kim5e47f8f2015-11-09 14:45:40 +0900570 new->count = parent->count;
571 new->children_count = parent->children_count;
572 parent->children_count = callchain_cumul_counts(new);
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200573
574 /* create a new child for the new branch if any */
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100575 if (idx_total < cursor->nr) {
Namhyung Kime3695172013-10-11 14:15:36 +0900576 struct callchain_node *first;
577 struct callchain_list *cnode;
578 struct callchain_cursor_node *node;
579 struct rb_node *p, **pp;
580
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200581 parent->hit = 0;
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200582 parent->children_hit += period;
Namhyung Kim5e47f8f2015-11-09 14:45:40 +0900583 parent->count = 0;
584 parent->children_count += 1;
Namhyung Kime3695172013-10-11 14:15:36 +0900585
586 node = callchain_cursor_current(cursor);
587 new = add_child(parent, cursor, period);
Namhyung Kim7565bd32016-02-16 23:08:20 +0900588 if (new == NULL)
Namhyung Kimf2bb4c52016-02-16 23:08:23 +0900589 return -1;
Namhyung Kime3695172013-10-11 14:15:36 +0900590
591 /*
592 * This is second child since we moved parent's children
593 * to new (first) child above.
594 */
595 p = parent->rb_root_in.rb_node;
596 first = rb_entry(p, struct callchain_node, rb_node_in);
597 cnode = list_first_entry(&first->val, struct callchain_list,
598 list);
599
Namhyung Kim2d713b82016-02-16 23:08:22 +0900600 if (match_chain(node, cnode) == MATCH_LT)
Namhyung Kime3695172013-10-11 14:15:36 +0900601 pp = &p->rb_left;
602 else
603 pp = &p->rb_right;
604
605 rb_link_node(&new->rb_node_in, p, pp);
606 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200607 } else {
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200608 parent->hit = period;
Namhyung Kim5e47f8f2015-11-09 14:45:40 +0900609 parent->count = 1;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200610 }
Namhyung Kimf2bb4c52016-02-16 23:08:23 +0900611 return 0;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200612}
613
Namhyung Kim2d713b82016-02-16 23:08:22 +0900614static enum match_result
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100615append_chain(struct callchain_node *root,
616 struct callchain_cursor *cursor,
617 u64 period);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200618
Namhyung Kimdca0d122016-02-16 23:08:24 +0900619static int
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100620append_chain_children(struct callchain_node *root,
621 struct callchain_cursor *cursor,
622 u64 period)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200623{
624 struct callchain_node *rnode;
Namhyung Kime3695172013-10-11 14:15:36 +0900625 struct callchain_cursor_node *node;
626 struct rb_node **p = &root->rb_root_in.rb_node;
627 struct rb_node *parent = NULL;
628
629 node = callchain_cursor_current(cursor);
630 if (!node)
Namhyung Kimdca0d122016-02-16 23:08:24 +0900631 return -1;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200632
633 /* lookup in childrens */
Namhyung Kime3695172013-10-11 14:15:36 +0900634 while (*p) {
Namhyung Kim2d713b82016-02-16 23:08:22 +0900635 enum match_result ret;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200636
Namhyung Kime3695172013-10-11 14:15:36 +0900637 parent = *p;
638 rnode = rb_entry(parent, struct callchain_node, rb_node_in);
Namhyung Kime3695172013-10-11 14:15:36 +0900639
Frederic Weisbeckerb965bb42014-01-14 16:37:15 +0100640 /* If at least first entry matches, rely to children */
641 ret = append_chain(rnode, cursor, period);
Namhyung Kim2d713b82016-02-16 23:08:22 +0900642 if (ret == MATCH_EQ)
Frederic Weisbecker19532872009-08-07 07:11:05 +0200643 goto inc_children_hit;
Namhyung Kimdca0d122016-02-16 23:08:24 +0900644 if (ret == MATCH_ERROR)
645 return -1;
Namhyung Kime3695172013-10-11 14:15:36 +0900646
Namhyung Kim2d713b82016-02-16 23:08:22 +0900647 if (ret == MATCH_LT)
Namhyung Kime3695172013-10-11 14:15:36 +0900648 p = &parent->rb_left;
649 else
650 p = &parent->rb_right;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200651 }
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200652 /* nothing in children, add to the current node */
Namhyung Kime3695172013-10-11 14:15:36 +0900653 rnode = add_child(root, cursor, period);
Namhyung Kim7565bd32016-02-16 23:08:20 +0900654 if (rnode == NULL)
Namhyung Kimdca0d122016-02-16 23:08:24 +0900655 return -1;
Namhyung Kim7565bd32016-02-16 23:08:20 +0900656
Namhyung Kime3695172013-10-11 14:15:36 +0900657 rb_link_node(&rnode->rb_node_in, parent, p);
658 rb_insert_color(&rnode->rb_node_in, &root->rb_root_in);
Frederic Weisbeckere05b8762009-07-05 07:39:20 +0200659
Frederic Weisbecker19532872009-08-07 07:11:05 +0200660inc_children_hit:
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200661 root->children_hit += period;
Namhyung Kim5e47f8f2015-11-09 14:45:40 +0900662 root->children_count++;
Namhyung Kimdca0d122016-02-16 23:08:24 +0900663 return 0;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200664}
665
Namhyung Kim2d713b82016-02-16 23:08:22 +0900666static enum match_result
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100667append_chain(struct callchain_node *root,
668 struct callchain_cursor *cursor,
669 u64 period)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200670{
671 struct callchain_list *cnode;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100672 u64 start = cursor->pos;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200673 bool found = false;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100674 u64 matches;
Namhyung Kim2d713b82016-02-16 23:08:22 +0900675 enum match_result cmp = MATCH_ERROR;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200676
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200677 /*
678 * Lookup in the current node
679 * If we have a symbol, then compare the start to match
Andi Kleen99571ab2013-07-18 15:33:57 -0700680 * anywhere inside a function, unless function
681 * mode is disabled.
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200682 */
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200683 list_for_each_entry(cnode, &root->val, list) {
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100684 struct callchain_cursor_node *node;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300685
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100686 node = callchain_cursor_current(cursor);
687 if (!node)
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200688 break;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300689
Frederic Weisbeckerb965bb42014-01-14 16:37:15 +0100690 cmp = match_chain(node, cnode);
Namhyung Kim2d713b82016-02-16 23:08:22 +0900691 if (cmp != MATCH_EQ)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200692 break;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300693
Namhyung Kime3695172013-10-11 14:15:36 +0900694 found = true;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100695
696 callchain_cursor_advance(cursor);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200697 }
698
Namhyung Kime3695172013-10-11 14:15:36 +0900699 /* matches not, relay no the parent */
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100700 if (!found) {
Namhyung Kim2d713b82016-02-16 23:08:22 +0900701 WARN_ONCE(cmp == MATCH_ERROR, "Chain comparison error\n");
Frederic Weisbeckerb965bb42014-01-14 16:37:15 +0100702 return cmp;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100703 }
704
705 matches = cursor->pos - start;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200706
707 /* we match only a part of the node. Split it and add the new chain */
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100708 if (matches < root->val_nr) {
Namhyung Kimf2bb4c52016-02-16 23:08:23 +0900709 if (split_add_child(root, cursor, cnode, start, matches,
710 period) < 0)
711 return MATCH_ERROR;
712
Namhyung Kim2d713b82016-02-16 23:08:22 +0900713 return MATCH_EQ;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200714 }
715
716 /* we match 100% of the path, increment the hit */
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100717 if (matches == root->val_nr && cursor->pos == cursor->nr) {
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200718 root->hit += period;
Namhyung Kim5e47f8f2015-11-09 14:45:40 +0900719 root->count++;
Namhyung Kim2d713b82016-02-16 23:08:22 +0900720 return MATCH_EQ;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200721 }
722
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200723 /* We match the node and still have a part remaining */
Namhyung Kimdca0d122016-02-16 23:08:24 +0900724 if (append_chain_children(root, cursor, period) < 0)
725 return MATCH_ERROR;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200726
Namhyung Kim2d713b82016-02-16 23:08:22 +0900727 return MATCH_EQ;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200728}
729
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100730int callchain_append(struct callchain_root *root,
731 struct callchain_cursor *cursor,
732 u64 period)
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300733{
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100734 if (!cursor->nr)
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300735 return 0;
736
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100737 callchain_cursor_commit(cursor);
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300738
Namhyung Kimdca0d122016-02-16 23:08:24 +0900739 if (append_chain_children(&root->node, cursor, period) < 0)
740 return -1;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300741
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100742 if (cursor->nr > root->max_depth)
743 root->max_depth = cursor->nr;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300744
745 return 0;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200746}
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200747
748static int
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100749merge_chain_branch(struct callchain_cursor *cursor,
750 struct callchain_node *dst, struct callchain_node *src)
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200751{
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100752 struct callchain_cursor_node **old_last = cursor->last;
Namhyung Kime3695172013-10-11 14:15:36 +0900753 struct callchain_node *child;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200754 struct callchain_list *list, *next_list;
Namhyung Kime3695172013-10-11 14:15:36 +0900755 struct rb_node *n;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100756 int old_pos = cursor->nr;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200757 int err = 0;
758
759 list_for_each_entry_safe(list, next_list, &src->val, list) {
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100760 callchain_cursor_append(cursor, list->ip,
Jin Yao410024d2016-10-31 09:19:49 +0800761 list->ms.map, list->ms.sym,
762 false, NULL, 0, 0);
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200763 list_del(&list->list);
764 free(list);
765 }
766
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100767 if (src->hit) {
768 callchain_cursor_commit(cursor);
Namhyung Kimdca0d122016-02-16 23:08:24 +0900769 if (append_chain_children(dst, cursor, src->hit) < 0)
770 return -1;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100771 }
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200772
Namhyung Kime3695172013-10-11 14:15:36 +0900773 n = rb_first(&src->rb_root_in);
774 while (n) {
775 child = container_of(n, struct callchain_node, rb_node_in);
776 n = rb_next(n);
777 rb_erase(&child->rb_node_in, &src->rb_root_in);
778
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100779 err = merge_chain_branch(cursor, dst, child);
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200780 if (err)
781 break;
782
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200783 free(child);
784 }
785
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100786 cursor->nr = old_pos;
787 cursor->last = old_last;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200788
789 return err;
790}
791
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100792int callchain_merge(struct callchain_cursor *cursor,
793 struct callchain_root *dst, struct callchain_root *src)
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200794{
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100795 return merge_chain_branch(cursor, &dst->node, &src->node);
796}
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200797
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100798int callchain_cursor_append(struct callchain_cursor *cursor,
Jin Yao410024d2016-10-31 09:19:49 +0800799 u64 ip, struct map *map, struct symbol *sym,
800 bool branch, struct branch_flags *flags,
801 int nr_loop_iter, int samples)
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100802{
803 struct callchain_cursor_node *node = *cursor->last;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200804
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100805 if (!node) {
Paul Gortmaker91b98802013-01-30 20:05:49 -0500806 node = calloc(1, sizeof(*node));
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100807 if (!node)
808 return -ENOMEM;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200809
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100810 *cursor->last = node;
811 }
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200812
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100813 node->ip = ip;
814 node->map = map;
815 node->sym = sym;
Jin Yao410024d2016-10-31 09:19:49 +0800816 node->branch = branch;
817 node->nr_loop_iter = nr_loop_iter;
818 node->samples = samples;
819
820 if (flags)
821 memcpy(&node->branch_flags, flags,
822 sizeof(struct branch_flags));
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200823
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100824 cursor->nr++;
825
826 cursor->last = &node->next;
827
828 return 0;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200829}
Namhyung Kim2dc9fb12014-01-14 14:25:35 +0900830
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -0300831int sample__resolve_callchain(struct perf_sample *sample,
832 struct callchain_cursor *cursor, struct symbol **parent,
Namhyung Kim2dc9fb12014-01-14 14:25:35 +0900833 struct perf_evsel *evsel, struct addr_location *al,
834 int max_stack)
835{
836 if (sample->callchain == NULL)
837 return 0;
838
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900839 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain ||
Jiri Olsade7e6a72016-05-03 13:54:43 +0200840 perf_hpp_list.parent) {
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -0300841 return thread__resolve_callchain(al->thread, cursor, evsel, sample,
Arnaldo Carvalho de Melocc8b7c22014-10-23 15:26:17 -0300842 parent, al, max_stack);
Namhyung Kim2dc9fb12014-01-14 14:25:35 +0900843 }
844 return 0;
845}
846
847int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample)
848{
Adrian Hunter4d40b052014-07-14 13:02:35 +0300849 if (!symbol_conf.use_callchain || sample->callchain == NULL)
Namhyung Kim2dc9fb12014-01-14 14:25:35 +0900850 return 0;
851 return callchain_append(he->callchain, &callchain_cursor, sample->period);
852}
Namhyung Kimc7405d82013-10-31 13:58:30 +0900853
854int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
855 bool hide_unresolved)
856{
857 al->map = node->map;
858 al->sym = node->sym;
859 if (node->map)
860 al->addr = node->map->map_ip(node->map, node->ip);
861 else
862 al->addr = node->ip;
863
864 if (al->sym == NULL) {
865 if (hide_unresolved)
866 return 0;
867 if (al->map == NULL)
868 goto out;
869 }
870
871 if (al->map->groups == &al->machine->kmaps) {
872 if (machine__is_host(al->machine)) {
873 al->cpumode = PERF_RECORD_MISC_KERNEL;
874 al->level = 'k';
875 } else {
876 al->cpumode = PERF_RECORD_MISC_GUEST_KERNEL;
877 al->level = 'g';
878 }
879 } else {
880 if (machine__is_host(al->machine)) {
881 al->cpumode = PERF_RECORD_MISC_USER;
882 al->level = '.';
883 } else if (perf_guest) {
884 al->cpumode = PERF_RECORD_MISC_GUEST_USER;
885 al->level = 'u';
886 } else {
887 al->cpumode = PERF_RECORD_MISC_HYPERVISOR;
888 al->level = 'H';
889 }
890 }
891
892out:
893 return 1;
894}
Andi Kleen2989cca2014-11-12 18:05:23 -0800895
896char *callchain_list__sym_name(struct callchain_list *cl,
897 char *bf, size_t bfsize, bool show_dso)
898{
899 int printed;
900
901 if (cl->ms.sym) {
Andi Kleen23f09812014-11-12 18:05:24 -0800902 if (callchain_param.key == CCKEY_ADDRESS &&
903 cl->ms.map && !cl->srcline)
904 cl->srcline = get_srcline(cl->ms.map->dso,
905 map__rip_2objdump(cl->ms.map,
Andi Kleen85c116a2014-11-12 18:05:27 -0800906 cl->ip),
907 cl->ms.sym, false);
Andi Kleen23f09812014-11-12 18:05:24 -0800908 if (cl->srcline)
909 printed = scnprintf(bf, bfsize, "%s %s",
910 cl->ms.sym->name, cl->srcline);
911 else
912 printed = scnprintf(bf, bfsize, "%s", cl->ms.sym->name);
Andi Kleen2989cca2014-11-12 18:05:23 -0800913 } else
914 printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);
915
916 if (show_dso)
917 scnprintf(bf + printed, bfsize - printed, " %s",
918 cl->ms.map ?
919 cl->ms.map->dso->short_name :
920 "unknown");
921
922 return bf;
923}
Namhyung Kimd1149602014-12-30 14:38:13 +0900924
Namhyung Kim5ab250c2015-11-09 14:45:39 +0900925char *callchain_node__scnprintf_value(struct callchain_node *node,
926 char *bf, size_t bfsize, u64 total)
927{
928 double percent = 0.0;
929 u64 period = callchain_cumul_hits(node);
Namhyung Kimf2af0082015-11-09 14:45:41 +0900930 unsigned count = callchain_cumul_counts(node);
Namhyung Kim5ab250c2015-11-09 14:45:39 +0900931
Namhyung Kimf2af0082015-11-09 14:45:41 +0900932 if (callchain_param.mode == CHAIN_FOLDED) {
Namhyung Kim5ab250c2015-11-09 14:45:39 +0900933 period = node->hit;
Namhyung Kimf2af0082015-11-09 14:45:41 +0900934 count = node->count;
935 }
Namhyung Kim5ab250c2015-11-09 14:45:39 +0900936
Namhyung Kimf2af0082015-11-09 14:45:41 +0900937 switch (callchain_param.value) {
938 case CCVAL_PERIOD:
939 scnprintf(bf, bfsize, "%"PRIu64, period);
940 break;
941 case CCVAL_COUNT:
942 scnprintf(bf, bfsize, "%u", count);
943 break;
944 case CCVAL_PERCENT:
945 default:
946 if (total)
947 percent = period * 100.0 / total;
948 scnprintf(bf, bfsize, "%.2f%%", percent);
949 break;
950 }
Namhyung Kim5ab250c2015-11-09 14:45:39 +0900951 return bf;
952}
953
954int callchain_node__fprintf_value(struct callchain_node *node,
955 FILE *fp, u64 total)
956{
957 double percent = 0.0;
958 u64 period = callchain_cumul_hits(node);
Namhyung Kimf2af0082015-11-09 14:45:41 +0900959 unsigned count = callchain_cumul_counts(node);
Namhyung Kim5ab250c2015-11-09 14:45:39 +0900960
Namhyung Kimf2af0082015-11-09 14:45:41 +0900961 if (callchain_param.mode == CHAIN_FOLDED) {
Namhyung Kim5ab250c2015-11-09 14:45:39 +0900962 period = node->hit;
Namhyung Kimf2af0082015-11-09 14:45:41 +0900963 count = node->count;
964 }
Namhyung Kim5ab250c2015-11-09 14:45:39 +0900965
Namhyung Kimf2af0082015-11-09 14:45:41 +0900966 switch (callchain_param.value) {
967 case CCVAL_PERIOD:
968 return fprintf(fp, "%"PRIu64, period);
969 case CCVAL_COUNT:
970 return fprintf(fp, "%u", count);
971 case CCVAL_PERCENT:
972 default:
973 if (total)
974 percent = period * 100.0 / total;
975 return percent_color_fprintf(fp, "%.2f%%", percent);
976 }
977 return 0;
Namhyung Kim5ab250c2015-11-09 14:45:39 +0900978}
979
Jin Yao3dd029e2016-10-31 09:19:51 +0800980static void callchain_counts_value(struct callchain_node *node,
981 u64 *branch_count, u64 *predicted_count,
982 u64 *abort_count, u64 *cycles_count)
983{
984 struct callchain_list *clist;
985
986 list_for_each_entry(clist, &node->val, list) {
987 if (branch_count)
988 *branch_count += clist->branch_count;
989
990 if (predicted_count)
991 *predicted_count += clist->predicted_count;
992
993 if (abort_count)
994 *abort_count += clist->abort_count;
995
996 if (cycles_count)
997 *cycles_count += clist->cycles_count;
998 }
999}
1000
1001static int callchain_node_branch_counts_cumul(struct callchain_node *node,
1002 u64 *branch_count,
1003 u64 *predicted_count,
1004 u64 *abort_count,
1005 u64 *cycles_count)
1006{
1007 struct callchain_node *child;
1008 struct rb_node *n;
1009
1010 n = rb_first(&node->rb_root_in);
1011 while (n) {
1012 child = rb_entry(n, struct callchain_node, rb_node_in);
1013 n = rb_next(n);
1014
1015 callchain_node_branch_counts_cumul(child, branch_count,
1016 predicted_count,
1017 abort_count,
1018 cycles_count);
1019
1020 callchain_counts_value(child, branch_count,
1021 predicted_count, abort_count,
1022 cycles_count);
1023 }
1024
1025 return 0;
1026}
1027
1028int callchain_branch_counts(struct callchain_root *root,
1029 u64 *branch_count, u64 *predicted_count,
1030 u64 *abort_count, u64 *cycles_count)
1031{
1032 if (branch_count)
1033 *branch_count = 0;
1034
1035 if (predicted_count)
1036 *predicted_count = 0;
1037
1038 if (abort_count)
1039 *abort_count = 0;
1040
1041 if (cycles_count)
1042 *cycles_count = 0;
1043
1044 return callchain_node_branch_counts_cumul(&root->node,
1045 branch_count,
1046 predicted_count,
1047 abort_count,
1048 cycles_count);
1049}
1050
1051static int callchain_counts_printf(FILE *fp, char *bf, int bfsize,
1052 u64 branch_count, u64 predicted_count,
1053 u64 abort_count, u64 cycles_count,
1054 u64 iter_count, u64 samples_count)
1055{
1056 double predicted_percent = 0.0;
1057 const char *null_str = "";
1058 char iter_str[32];
1059 char *str;
1060 u64 cycles = 0;
1061
1062 if (branch_count == 0) {
1063 if (fp)
1064 return fprintf(fp, " (calltrace)");
1065
1066 return scnprintf(bf, bfsize, " (calltrace)");
1067 }
1068
1069 if (iter_count && samples_count) {
1070 scnprintf(iter_str, sizeof(iter_str),
1071 ", iterations:%" PRId64 "",
1072 iter_count / samples_count);
1073 str = iter_str;
1074 } else
1075 str = (char *)null_str;
1076
1077 predicted_percent = predicted_count * 100.0 / branch_count;
1078 cycles = cycles_count / branch_count;
1079
1080 if ((predicted_percent >= 100.0) && (abort_count == 0)) {
1081 if (fp)
1082 return fprintf(fp, " (cycles:%" PRId64 "%s)",
1083 cycles, str);
1084
1085 return scnprintf(bf, bfsize, " (cycles:%" PRId64 "%s)",
1086 cycles, str);
1087 }
1088
1089 if ((predicted_percent < 100.0) && (abort_count == 0)) {
1090 if (fp)
1091 return fprintf(fp,
1092 " (predicted:%.1f%%, cycles:%" PRId64 "%s)",
1093 predicted_percent, cycles, str);
1094
1095 return scnprintf(bf, bfsize,
1096 " (predicted:%.1f%%, cycles:%" PRId64 "%s)",
1097 predicted_percent, cycles, str);
1098 }
1099
1100 if (fp)
1101 return fprintf(fp,
1102 " (predicted:%.1f%%, abort:%" PRId64 ", cycles:%" PRId64 "%s)",
1103 predicted_percent, abort_count, cycles, str);
1104
1105 return scnprintf(bf, bfsize,
1106 " (predicted:%.1f%%, abort:%" PRId64 ", cycles:%" PRId64 "%s)",
1107 predicted_percent, abort_count, cycles, str);
1108}
1109
1110int callchain_list_counts__printf_value(struct callchain_node *node,
1111 struct callchain_list *clist,
1112 FILE *fp, char *bf, int bfsize)
1113{
1114 u64 branch_count, predicted_count;
1115 u64 abort_count, cycles_count;
1116 u64 iter_count = 0, samples_count = 0;
1117
1118 branch_count = clist->branch_count;
1119 predicted_count = clist->predicted_count;
1120 abort_count = clist->abort_count;
1121 cycles_count = clist->cycles_count;
1122
1123 if (node) {
1124 struct callchain_list *call;
1125
1126 list_for_each_entry(call, &node->val, list) {
1127 iter_count += call->iter_count;
1128 samples_count += call->samples_count;
1129 }
1130 }
1131
1132 return callchain_counts_printf(fp, bf, bfsize, branch_count,
1133 predicted_count, abort_count,
1134 cycles_count, iter_count, samples_count);
1135}
1136
Namhyung Kimd1149602014-12-30 14:38:13 +09001137static void free_callchain_node(struct callchain_node *node)
1138{
1139 struct callchain_list *list, *tmp;
1140 struct callchain_node *child;
1141 struct rb_node *n;
1142
Namhyung Kim4b3a3212015-11-09 14:45:43 +09001143 list_for_each_entry_safe(list, tmp, &node->parent_val, list) {
1144 list_del(&list->list);
1145 free(list);
1146 }
1147
Namhyung Kimd1149602014-12-30 14:38:13 +09001148 list_for_each_entry_safe(list, tmp, &node->val, list) {
1149 list_del(&list->list);
1150 free(list);
1151 }
1152
1153 n = rb_first(&node->rb_root_in);
1154 while (n) {
1155 child = container_of(n, struct callchain_node, rb_node_in);
1156 n = rb_next(n);
1157 rb_erase(&child->rb_node_in, &node->rb_root_in);
1158
1159 free_callchain_node(child);
1160 free(child);
1161 }
1162}
1163
1164void free_callchain(struct callchain_root *root)
1165{
1166 if (!symbol_conf.use_callchain)
1167 return;
1168
1169 free_callchain_node(&root->node);
1170}
Namhyung Kim4b3a3212015-11-09 14:45:43 +09001171
Namhyung Kim42b276a2016-01-05 12:06:00 +09001172static u64 decay_callchain_node(struct callchain_node *node)
1173{
1174 struct callchain_node *child;
1175 struct rb_node *n;
1176 u64 child_hits = 0;
1177
1178 n = rb_first(&node->rb_root_in);
1179 while (n) {
1180 child = container_of(n, struct callchain_node, rb_node_in);
1181
1182 child_hits += decay_callchain_node(child);
1183 n = rb_next(n);
1184 }
1185
1186 node->hit = (node->hit * 7) / 8;
1187 node->children_hit = child_hits;
1188
1189 return node->hit;
1190}
1191
1192void decay_callchain(struct callchain_root *root)
1193{
1194 if (!symbol_conf.use_callchain)
1195 return;
1196
1197 decay_callchain_node(&root->node);
1198}
1199
Namhyung Kim4b3a3212015-11-09 14:45:43 +09001200int callchain_node__make_parent_list(struct callchain_node *node)
1201{
1202 struct callchain_node *parent = node->parent;
1203 struct callchain_list *chain, *new;
1204 LIST_HEAD(head);
1205
1206 while (parent) {
1207 list_for_each_entry_reverse(chain, &parent->val, list) {
1208 new = malloc(sizeof(*new));
1209 if (new == NULL)
1210 goto out;
1211 *new = *chain;
1212 new->has_children = false;
1213 list_add_tail(&new->list, &head);
1214 }
1215 parent = parent->parent;
1216 }
1217
1218 list_for_each_entry_safe_reverse(chain, new, &head, list)
1219 list_move_tail(&chain->list, &node->parent_val);
1220
1221 if (!list_empty(&node->parent_val)) {
1222 chain = list_first_entry(&node->parent_val, struct callchain_list, list);
1223 chain->has_children = rb_prev(&node->rb_node) || rb_next(&node->rb_node);
1224
1225 chain = list_first_entry(&node->val, struct callchain_list, list);
1226 chain->has_children = false;
1227 }
1228 return 0;
1229
1230out:
1231 list_for_each_entry_safe(chain, new, &head, list) {
1232 list_del(&chain->list);
1233 free(chain);
1234 }
1235 return -ENOMEM;
1236}
Namhyung Kim571f1eb2016-12-06 12:40:02 +09001237
1238int callchain_cursor__copy(struct callchain_cursor *dst,
1239 struct callchain_cursor *src)
1240{
1241 int rc = 0;
1242
1243 callchain_cursor_reset(dst);
1244 callchain_cursor_commit(src);
1245
1246 while (true) {
1247 struct callchain_cursor_node *node;
1248
1249 node = callchain_cursor_current(src);
1250 if (node == NULL)
1251 break;
1252
1253 rc = callchain_cursor_append(dst, node->ip, node->map, node->sym,
1254 node->branch, &node->branch_flags,
1255 node->nr_loop_iter, node->samples);
1256 if (rc)
1257 break;
1258
1259 callchain_cursor_advance(src);
1260 }
1261
1262 return rc;
1263}