blob: 735ad48e1858b0382c9aacc84e0b912e9329c86a [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 }
47 return -1;
48}
49
50static int parse_callchain_order(const char *value)
51{
52 if (!strncmp(value, "caller", strlen(value))) {
53 callchain_param.order = ORDER_CALLER;
Namhyung Kim792aeaf2015-10-22 16:45:46 +090054 callchain_param.order_set = true;
Namhyung Kim2b9240c2014-09-23 10:01:43 +090055 return 0;
56 }
57 if (!strncmp(value, "callee", strlen(value))) {
58 callchain_param.order = ORDER_CALLEE;
Namhyung Kim792aeaf2015-10-22 16:45:46 +090059 callchain_param.order_set = true;
Namhyung Kim2b9240c2014-09-23 10:01:43 +090060 return 0;
61 }
62 return -1;
63}
64
65static int parse_callchain_sort_key(const char *value)
66{
67 if (!strncmp(value, "function", strlen(value))) {
68 callchain_param.key = CCKEY_FUNCTION;
69 return 0;
70 }
71 if (!strncmp(value, "address", strlen(value))) {
72 callchain_param.key = CCKEY_ADDRESS;
73 return 0;
74 }
Andi Kleen8b7bad52014-11-12 18:05:20 -080075 if (!strncmp(value, "branch", strlen(value))) {
76 callchain_param.branch_callstack = 1;
77 return 0;
78 }
Namhyung Kim2b9240c2014-09-23 10:01:43 +090079 return -1;
80}
81
Namhyung Kima2c10d32015-10-22 15:28:49 +090082static int
83__parse_callchain_report_opt(const char *arg, bool allow_record_opt)
Don Zickuscff6bb42014-04-07 14:55:24 -040084{
Namhyung Kime8232f12014-08-14 15:01:38 +090085 char *tok;
Don Zickuscff6bb42014-04-07 14:55:24 -040086 char *endptr;
Namhyung Kime8232f12014-08-14 15:01:38 +090087 bool minpcnt_set = false;
Namhyung Kima2c10d32015-10-22 15:28:49 +090088 bool record_opt_set = false;
89 bool try_stack_size = false;
Don Zickuscff6bb42014-04-07 14:55:24 -040090
91 symbol_conf.use_callchain = true;
92
93 if (!arg)
94 return 0;
95
Namhyung Kime8232f12014-08-14 15:01:38 +090096 while ((tok = strtok((char *)arg, ",")) != NULL) {
97 if (!strncmp(tok, "none", strlen(tok))) {
98 callchain_param.mode = CHAIN_NONE;
99 symbol_conf.use_callchain = false;
100 return 0;
101 }
Don Zickuscff6bb42014-04-07 14:55:24 -0400102
Namhyung Kim2b9240c2014-09-23 10:01:43 +0900103 if (!parse_callchain_mode(tok) ||
104 !parse_callchain_order(tok) ||
105 !parse_callchain_sort_key(tok)) {
106 /* parsing ok - move on to the next */
Namhyung Kima2c10d32015-10-22 15:28:49 +0900107 try_stack_size = false;
108 goto next;
109 } else if (allow_record_opt && !record_opt_set) {
110 if (parse_callchain_record(tok, &callchain_param))
111 goto try_numbers;
112
113 /* assume that number followed by 'dwarf' is stack size */
114 if (callchain_param.record_mode == CALLCHAIN_DWARF)
115 try_stack_size = true;
116
117 record_opt_set = true;
118 goto next;
119 }
120
121try_numbers:
122 if (try_stack_size) {
123 unsigned long size = 0;
124
125 if (get_stack_size(tok, &size) < 0)
126 return -1;
127 callchain_param.dump_size = size;
128 try_stack_size = false;
Namhyung Kim2b9240c2014-09-23 10:01:43 +0900129 } else if (!minpcnt_set) {
130 /* try to get the min percent */
Namhyung Kime8232f12014-08-14 15:01:38 +0900131 callchain_param.min_percent = strtod(tok, &endptr);
132 if (tok == endptr)
133 return -1;
134 minpcnt_set = true;
135 } else {
136 /* try print limit at last */
137 callchain_param.print_limit = strtoul(tok, &endptr, 0);
138 if (tok == endptr)
139 return -1;
140 }
Namhyung Kima2c10d32015-10-22 15:28:49 +0900141next:
Namhyung Kime8232f12014-08-14 15:01:38 +0900142 arg = NULL;
Don Zickuscff6bb42014-04-07 14:55:24 -0400143 }
144
Don Zickuscff6bb42014-04-07 14:55:24 -0400145 if (callchain_register_param(&callchain_param) < 0) {
146 pr_err("Can't register callchain params\n");
147 return -1;
148 }
149 return 0;
150}
151
Namhyung Kima2c10d32015-10-22 15:28:49 +0900152int parse_callchain_report_opt(const char *arg)
153{
154 return __parse_callchain_report_opt(arg, false);
155}
156
157int parse_callchain_top_opt(const char *arg)
158{
159 return __parse_callchain_report_opt(arg, true);
160}
161
Namhyung Kim2b9240c2014-09-23 10:01:43 +0900162int perf_callchain_config(const char *var, const char *value)
163{
164 char *endptr;
165
166 if (prefixcmp(var, "call-graph."))
167 return 0;
168 var += sizeof("call-graph.") - 1;
169
170 if (!strcmp(var, "record-mode"))
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400171 return parse_callchain_record_opt(value, &callchain_param);
Namhyung Kim2b9240c2014-09-23 10:01:43 +0900172#ifdef HAVE_DWARF_UNWIND_SUPPORT
173 if (!strcmp(var, "dump-size")) {
174 unsigned long size = 0;
175 int ret;
176
177 ret = get_stack_size(value, &size);
178 callchain_param.dump_size = size;
179
180 return ret;
181 }
182#endif
183 if (!strcmp(var, "print-type"))
184 return parse_callchain_mode(value);
185 if (!strcmp(var, "order"))
186 return parse_callchain_order(value);
187 if (!strcmp(var, "sort-key"))
188 return parse_callchain_sort_key(value);
189 if (!strcmp(var, "threshold")) {
190 callchain_param.min_percent = strtod(value, &endptr);
191 if (value == endptr)
192 return -1;
193 }
194 if (!strcmp(var, "print-limit")) {
195 callchain_param.print_limit = strtod(value, &endptr);
196 if (value == endptr)
197 return -1;
198 }
199
200 return 0;
201}
202
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200203static void
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200204rb_insert_callchain(struct rb_root *root, struct callchain_node *chain,
205 enum chain_mode mode)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200206{
207 struct rb_node **p = &root->rb_node;
208 struct rb_node *parent = NULL;
209 struct callchain_node *rnode;
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100210 u64 chain_cumul = callchain_cumul_hits(chain);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200211
212 while (*p) {
Frederic Weisbecker19532872009-08-07 07:11:05 +0200213 u64 rnode_cumul;
214
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200215 parent = *p;
216 rnode = rb_entry(parent, struct callchain_node, rb_node);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100217 rnode_cumul = callchain_cumul_hits(rnode);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200218
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200219 switch (mode) {
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200220 case CHAIN_FLAT:
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200221 if (rnode->hit < chain->hit)
222 p = &(*p)->rb_left;
223 else
224 p = &(*p)->rb_right;
225 break;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200226 case CHAIN_GRAPH_ABS: /* Falldown */
227 case CHAIN_GRAPH_REL:
Frederic Weisbecker19532872009-08-07 07:11:05 +0200228 if (rnode_cumul < chain_cumul)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200229 p = &(*p)->rb_left;
230 else
231 p = &(*p)->rb_right;
232 break;
Ingo Molnar83a09442009-08-15 12:26:57 +0200233 case CHAIN_NONE:
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200234 default:
235 break;
236 }
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200237 }
238
239 rb_link_node(&chain->rb_node, parent, p);
240 rb_insert_color(&chain->rb_node, root);
241}
242
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200243static void
244__sort_chain_flat(struct rb_root *rb_root, struct callchain_node *node,
245 u64 min_hit)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200246{
Namhyung Kime3695172013-10-11 14:15:36 +0900247 struct rb_node *n;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200248 struct callchain_node *child;
249
Namhyung Kime3695172013-10-11 14:15:36 +0900250 n = rb_first(&node->rb_root_in);
251 while (n) {
252 child = rb_entry(n, struct callchain_node, rb_node_in);
253 n = rb_next(n);
254
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200255 __sort_chain_flat(rb_root, child, min_hit);
Namhyung Kime3695172013-10-11 14:15:36 +0900256 }
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200257
Frederic Weisbeckerc20ab372009-07-02 20:14:33 +0200258 if (node->hit && node->hit >= min_hit)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200259 rb_insert_callchain(rb_root, node, CHAIN_FLAT);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200260}
261
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200262/*
263 * Once we get every callchains from the stream, we can now
264 * sort them by hit
265 */
266static void
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200267sort_chain_flat(struct rb_root *rb_root, struct callchain_root *root,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300268 u64 min_hit, struct callchain_param *param __maybe_unused)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200269{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200270 __sort_chain_flat(rb_root, &root->node, min_hit);
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200271}
272
273static void __sort_chain_graph_abs(struct callchain_node *node,
274 u64 min_hit)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200275{
Namhyung Kime3695172013-10-11 14:15:36 +0900276 struct rb_node *n;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200277 struct callchain_node *child;
278
279 node->rb_root = RB_ROOT;
Namhyung Kime3695172013-10-11 14:15:36 +0900280 n = rb_first(&node->rb_root_in);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200281
Namhyung Kime3695172013-10-11 14:15:36 +0900282 while (n) {
283 child = rb_entry(n, struct callchain_node, rb_node_in);
284 n = rb_next(n);
285
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200286 __sort_chain_graph_abs(child, min_hit);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100287 if (callchain_cumul_hits(child) >= min_hit)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200288 rb_insert_callchain(&node->rb_root, child,
289 CHAIN_GRAPH_ABS);
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200290 }
291}
292
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200293static void
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200294sort_chain_graph_abs(struct rb_root *rb_root, struct callchain_root *chain_root,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300295 u64 min_hit, struct callchain_param *param __maybe_unused)
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +0200296{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200297 __sort_chain_graph_abs(&chain_root->node, min_hit);
298 rb_root->rb_node = chain_root->node.rb_root.rb_node;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200299}
300
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200301static void __sort_chain_graph_rel(struct callchain_node *node,
302 double min_percent)
303{
Namhyung Kime3695172013-10-11 14:15:36 +0900304 struct rb_node *n;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200305 struct callchain_node *child;
306 u64 min_hit;
307
308 node->rb_root = RB_ROOT;
Frederic Weisbeckerc0a88652009-08-09 04:19:15 +0200309 min_hit = ceil(node->children_hit * min_percent);
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200310
Namhyung Kime3695172013-10-11 14:15:36 +0900311 n = rb_first(&node->rb_root_in);
312 while (n) {
313 child = rb_entry(n, struct callchain_node, rb_node_in);
314 n = rb_next(n);
315
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200316 __sort_chain_graph_rel(child, min_percent);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100317 if (callchain_cumul_hits(child) >= min_hit)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200318 rb_insert_callchain(&node->rb_root, child,
319 CHAIN_GRAPH_REL);
320 }
321}
322
323static void
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200324sort_chain_graph_rel(struct rb_root *rb_root, struct callchain_root *chain_root,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300325 u64 min_hit __maybe_unused, struct callchain_param *param)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200326{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +0200327 __sort_chain_graph_rel(&chain_root->node, param->min_percent / 100.0);
328 rb_root->rb_node = chain_root->node.rb_root.rb_node;
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200329}
330
Frederic Weisbecker16537f12011-01-14 04:52:00 +0100331int callchain_register_param(struct callchain_param *param)
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200332{
333 switch (param->mode) {
334 case CHAIN_GRAPH_ABS:
335 param->sort = sort_chain_graph_abs;
336 break;
337 case CHAIN_GRAPH_REL:
338 param->sort = sort_chain_graph_rel;
339 break;
340 case CHAIN_FLAT:
341 param->sort = sort_chain_flat;
342 break;
Ingo Molnar83a09442009-08-15 12:26:57 +0200343 case CHAIN_NONE:
Frederic Weisbecker805d1272009-07-05 07:39:21 +0200344 default:
345 return -1;
346 }
347 return 0;
348}
349
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200350/*
351 * Create a child for a parent. If inherit_children, then the new child
352 * will become the new parent of it's parent children
353 */
354static struct callchain_node *
355create_child(struct callchain_node *parent, bool inherit_children)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200356{
357 struct callchain_node *new;
358
Arnaldo Carvalho de Melocdd5b75b2010-05-10 10:56:50 -0300359 new = zalloc(sizeof(*new));
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200360 if (!new) {
361 perror("not enough memory to create child for code path tree");
362 return NULL;
363 }
364 new->parent = parent;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200365 INIT_LIST_HEAD(&new->val);
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200366
367 if (inherit_children) {
Namhyung Kime3695172013-10-11 14:15:36 +0900368 struct rb_node *n;
369 struct callchain_node *child;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200370
Namhyung Kime3695172013-10-11 14:15:36 +0900371 new->rb_root_in = parent->rb_root_in;
372 parent->rb_root_in = RB_ROOT;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200373
Namhyung Kime3695172013-10-11 14:15:36 +0900374 n = rb_first(&new->rb_root_in);
375 while (n) {
376 child = rb_entry(n, struct callchain_node, rb_node_in);
377 child->parent = new;
378 n = rb_next(n);
379 }
380
381 /* make it the first child */
382 rb_link_node(&new->rb_node_in, NULL, &parent->rb_root_in.rb_node);
383 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200384 }
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200385
386 return new;
387}
388
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300389
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200390/*
391 * Fill the node with callchain values
392 */
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200393static void
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100394fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200395{
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100396 struct callchain_cursor_node *cursor_node;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200397
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100398 node->val_nr = cursor->nr - cursor->pos;
399 if (!node->val_nr)
400 pr_warning("Warning: empty node in callchain tree\n");
401
402 cursor_node = callchain_cursor_current(cursor);
403
404 while (cursor_node) {
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200405 struct callchain_list *call;
406
Arnaldo Carvalho de Melocdd5b75b2010-05-10 10:56:50 -0300407 call = zalloc(sizeof(*call));
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200408 if (!call) {
409 perror("not enough memory for the code path tree");
410 return;
411 }
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100412 call->ip = cursor_node->ip;
413 call->ms.sym = cursor_node->sym;
414 call->ms.map = cursor_node->map;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200415 list_add_tail(&call->list, &node->val);
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100416
417 callchain_cursor_advance(cursor);
418 cursor_node = callchain_cursor_current(cursor);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200419 }
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200420}
421
Namhyung Kime3695172013-10-11 14:15:36 +0900422static struct callchain_node *
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100423add_child(struct callchain_node *parent,
424 struct callchain_cursor *cursor,
425 u64 period)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200426{
427 struct callchain_node *new;
428
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200429 new = create_child(parent, false);
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100430 fill_node(new, cursor);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200431
Frederic Weisbecker19532872009-08-07 07:11:05 +0200432 new->children_hit = 0;
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200433 new->hit = period;
Namhyung Kime3695172013-10-11 14:15:36 +0900434 return new;
435}
436
437static s64 match_chain(struct callchain_cursor_node *node,
438 struct callchain_list *cnode)
439{
440 struct symbol *sym = node->sym;
441
442 if (cnode->ms.sym && sym &&
443 callchain_param.key == CCKEY_FUNCTION)
444 return cnode->ms.sym->start - sym->start;
445 else
446 return cnode->ip - node->ip;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200447}
448
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200449/*
450 * Split the parent in two parts (a new child is created) and
451 * give a part of its callchain to the created child.
452 * Then create another child to host the given callchain of new branch
453 */
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200454static void
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100455split_add_child(struct callchain_node *parent,
456 struct callchain_cursor *cursor,
457 struct callchain_list *to_split,
458 u64 idx_parents, u64 idx_local, u64 period)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200459{
460 struct callchain_node *new;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200461 struct list_head *old_tail;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200462 unsigned int idx_total = idx_parents + idx_local;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200463
464 /* split */
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200465 new = create_child(parent, true);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200466
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200467 /* split the callchain and move a part to the new child */
468 old_tail = parent->val.prev;
469 list_del_range(&to_split->list, old_tail);
470 new->val.next = &to_split->list;
471 new->val.prev = old_tail;
472 to_split->list.prev = &new->val;
473 old_tail->next = &new->val;
474
475 /* split the hits */
476 new->hit = parent->hit;
Frederic Weisbecker19532872009-08-07 07:11:05 +0200477 new->children_hit = parent->children_hit;
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100478 parent->children_hit = callchain_cumul_hits(new);
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200479 new->val_nr = parent->val_nr - idx_local;
480 parent->val_nr = idx_local;
481
482 /* create a new child for the new branch if any */
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100483 if (idx_total < cursor->nr) {
Namhyung Kime3695172013-10-11 14:15:36 +0900484 struct callchain_node *first;
485 struct callchain_list *cnode;
486 struct callchain_cursor_node *node;
487 struct rb_node *p, **pp;
488
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200489 parent->hit = 0;
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200490 parent->children_hit += period;
Namhyung Kime3695172013-10-11 14:15:36 +0900491
492 node = callchain_cursor_current(cursor);
493 new = add_child(parent, cursor, period);
494
495 /*
496 * This is second child since we moved parent's children
497 * to new (first) child above.
498 */
499 p = parent->rb_root_in.rb_node;
500 first = rb_entry(p, struct callchain_node, rb_node_in);
501 cnode = list_first_entry(&first->val, struct callchain_list,
502 list);
503
504 if (match_chain(node, cnode) < 0)
505 pp = &p->rb_left;
506 else
507 pp = &p->rb_right;
508
509 rb_link_node(&new->rb_node_in, p, pp);
510 rb_insert_color(&new->rb_node_in, &parent->rb_root_in);
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200511 } else {
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200512 parent->hit = period;
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200513 }
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200514}
515
516static int
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100517append_chain(struct callchain_node *root,
518 struct callchain_cursor *cursor,
519 u64 period);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200520
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200521static void
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100522append_chain_children(struct callchain_node *root,
523 struct callchain_cursor *cursor,
524 u64 period)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200525{
526 struct callchain_node *rnode;
Namhyung Kime3695172013-10-11 14:15:36 +0900527 struct callchain_cursor_node *node;
528 struct rb_node **p = &root->rb_root_in.rb_node;
529 struct rb_node *parent = NULL;
530
531 node = callchain_cursor_current(cursor);
532 if (!node)
533 return;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200534
535 /* lookup in childrens */
Namhyung Kime3695172013-10-11 14:15:36 +0900536 while (*p) {
537 s64 ret;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200538
Namhyung Kime3695172013-10-11 14:15:36 +0900539 parent = *p;
540 rnode = rb_entry(parent, struct callchain_node, rb_node_in);
Namhyung Kime3695172013-10-11 14:15:36 +0900541
Frederic Weisbeckerb965bb42014-01-14 16:37:15 +0100542 /* If at least first entry matches, rely to children */
543 ret = append_chain(rnode, cursor, period);
544 if (ret == 0)
Frederic Weisbecker19532872009-08-07 07:11:05 +0200545 goto inc_children_hit;
Namhyung Kime3695172013-10-11 14:15:36 +0900546
547 if (ret < 0)
548 p = &parent->rb_left;
549 else
550 p = &parent->rb_right;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200551 }
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200552 /* nothing in children, add to the current node */
Namhyung Kime3695172013-10-11 14:15:36 +0900553 rnode = add_child(root, cursor, period);
554 rb_link_node(&rnode->rb_node_in, parent, p);
555 rb_insert_color(&rnode->rb_node_in, &root->rb_root_in);
Frederic Weisbeckere05b8762009-07-05 07:39:20 +0200556
Frederic Weisbecker19532872009-08-07 07:11:05 +0200557inc_children_hit:
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200558 root->children_hit += period;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200559}
560
561static int
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100562append_chain(struct callchain_node *root,
563 struct callchain_cursor *cursor,
564 u64 period)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200565{
566 struct callchain_list *cnode;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100567 u64 start = cursor->pos;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200568 bool found = false;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100569 u64 matches;
Frederic Weisbeckerb965bb42014-01-14 16:37:15 +0100570 int cmp = 0;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200571
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200572 /*
573 * Lookup in the current node
574 * If we have a symbol, then compare the start to match
Andi Kleen99571ab2013-07-18 15:33:57 -0700575 * anywhere inside a function, unless function
576 * mode is disabled.
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200577 */
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200578 list_for_each_entry(cnode, &root->val, list) {
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100579 struct callchain_cursor_node *node;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300580
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100581 node = callchain_cursor_current(cursor);
582 if (!node)
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200583 break;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300584
Frederic Weisbeckerb965bb42014-01-14 16:37:15 +0100585 cmp = match_chain(node, cnode);
586 if (cmp)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200587 break;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300588
Namhyung Kime3695172013-10-11 14:15:36 +0900589 found = true;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100590
591 callchain_cursor_advance(cursor);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200592 }
593
Namhyung Kime3695172013-10-11 14:15:36 +0900594 /* matches not, relay no the parent */
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100595 if (!found) {
Frederic Weisbeckerb965bb42014-01-14 16:37:15 +0100596 WARN_ONCE(!cmp, "Chain comparison error\n");
Frederic Weisbeckerb965bb42014-01-14 16:37:15 +0100597 return cmp;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100598 }
599
600 matches = cursor->pos - start;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200601
602 /* we match only a part of the node. Split it and add the new chain */
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100603 if (matches < root->val_nr) {
604 split_add_child(root, cursor, cnode, start, matches, period);
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200605 return 0;
606 }
607
608 /* we match 100% of the path, increment the hit */
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100609 if (matches == root->val_nr && cursor->pos == cursor->nr) {
Frederic Weisbecker108553e2010-07-08 03:41:46 +0200610 root->hit += period;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200611 return 0;
612 }
613
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200614 /* We match the node and still have a part remaining */
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100615 append_chain_children(root, cursor, period);
Frederic Weisbeckerdeac9112009-07-01 05:35:15 +0200616
617 return 0;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200618}
619
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100620int callchain_append(struct callchain_root *root,
621 struct callchain_cursor *cursor,
622 u64 period)
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300623{
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100624 if (!cursor->nr)
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300625 return 0;
626
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100627 callchain_cursor_commit(cursor);
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300628
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100629 append_chain_children(&root->node, cursor, period);
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300630
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100631 if (cursor->nr > root->max_depth)
632 root->max_depth = cursor->nr;
Frederic Weisbecker301fde22010-03-22 13:09:33 -0300633
634 return 0;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +0200635}
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200636
637static int
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100638merge_chain_branch(struct callchain_cursor *cursor,
639 struct callchain_node *dst, struct callchain_node *src)
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200640{
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100641 struct callchain_cursor_node **old_last = cursor->last;
Namhyung Kime3695172013-10-11 14:15:36 +0900642 struct callchain_node *child;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200643 struct callchain_list *list, *next_list;
Namhyung Kime3695172013-10-11 14:15:36 +0900644 struct rb_node *n;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100645 int old_pos = cursor->nr;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200646 int err = 0;
647
648 list_for_each_entry_safe(list, next_list, &src->val, list) {
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100649 callchain_cursor_append(cursor, list->ip,
650 list->ms.map, list->ms.sym);
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200651 list_del(&list->list);
652 free(list);
653 }
654
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100655 if (src->hit) {
656 callchain_cursor_commit(cursor);
657 append_chain_children(dst, cursor, src->hit);
658 }
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200659
Namhyung Kime3695172013-10-11 14:15:36 +0900660 n = rb_first(&src->rb_root_in);
661 while (n) {
662 child = container_of(n, struct callchain_node, rb_node_in);
663 n = rb_next(n);
664 rb_erase(&child->rb_node_in, &src->rb_root_in);
665
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100666 err = merge_chain_branch(cursor, dst, child);
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200667 if (err)
668 break;
669
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200670 free(child);
671 }
672
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100673 cursor->nr = old_pos;
674 cursor->last = old_last;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200675
676 return err;
677}
678
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100679int callchain_merge(struct callchain_cursor *cursor,
680 struct callchain_root *dst, struct callchain_root *src)
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200681{
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100682 return merge_chain_branch(cursor, &dst->node, &src->node);
683}
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200684
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100685int callchain_cursor_append(struct callchain_cursor *cursor,
686 u64 ip, struct map *map, struct symbol *sym)
687{
688 struct callchain_cursor_node *node = *cursor->last;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200689
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100690 if (!node) {
Paul Gortmaker91b98802013-01-30 20:05:49 -0500691 node = calloc(1, sizeof(*node));
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100692 if (!node)
693 return -ENOMEM;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200694
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100695 *cursor->last = node;
696 }
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200697
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100698 node->ip = ip;
699 node->map = map;
700 node->sym = sym;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200701
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100702 cursor->nr++;
703
704 cursor->last = &node->next;
705
706 return 0;
Frederic Weisbecker612d4fd2010-08-22 21:10:35 +0200707}
Namhyung Kim2dc9fb12014-01-14 14:25:35 +0900708
709int sample__resolve_callchain(struct perf_sample *sample, struct symbol **parent,
710 struct perf_evsel *evsel, struct addr_location *al,
711 int max_stack)
712{
713 if (sample->callchain == NULL)
714 return 0;
715
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900716 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain ||
717 sort__has_parent) {
Arnaldo Carvalho de Melocc8b7c22014-10-23 15:26:17 -0300718 return thread__resolve_callchain(al->thread, evsel, sample,
719 parent, al, max_stack);
Namhyung Kim2dc9fb12014-01-14 14:25:35 +0900720 }
721 return 0;
722}
723
724int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample)
725{
Adrian Hunter4d40b052014-07-14 13:02:35 +0300726 if (!symbol_conf.use_callchain || sample->callchain == NULL)
Namhyung Kim2dc9fb12014-01-14 14:25:35 +0900727 return 0;
728 return callchain_append(he->callchain, &callchain_cursor, sample->period);
729}
Namhyung Kimc7405d82013-10-31 13:58:30 +0900730
731int fill_callchain_info(struct addr_location *al, struct callchain_cursor_node *node,
732 bool hide_unresolved)
733{
734 al->map = node->map;
735 al->sym = node->sym;
736 if (node->map)
737 al->addr = node->map->map_ip(node->map, node->ip);
738 else
739 al->addr = node->ip;
740
741 if (al->sym == NULL) {
742 if (hide_unresolved)
743 return 0;
744 if (al->map == NULL)
745 goto out;
746 }
747
748 if (al->map->groups == &al->machine->kmaps) {
749 if (machine__is_host(al->machine)) {
750 al->cpumode = PERF_RECORD_MISC_KERNEL;
751 al->level = 'k';
752 } else {
753 al->cpumode = PERF_RECORD_MISC_GUEST_KERNEL;
754 al->level = 'g';
755 }
756 } else {
757 if (machine__is_host(al->machine)) {
758 al->cpumode = PERF_RECORD_MISC_USER;
759 al->level = '.';
760 } else if (perf_guest) {
761 al->cpumode = PERF_RECORD_MISC_GUEST_USER;
762 al->level = 'u';
763 } else {
764 al->cpumode = PERF_RECORD_MISC_HYPERVISOR;
765 al->level = 'H';
766 }
767 }
768
769out:
770 return 1;
771}
Andi Kleen2989cca2014-11-12 18:05:23 -0800772
773char *callchain_list__sym_name(struct callchain_list *cl,
774 char *bf, size_t bfsize, bool show_dso)
775{
776 int printed;
777
778 if (cl->ms.sym) {
Andi Kleen23f09812014-11-12 18:05:24 -0800779 if (callchain_param.key == CCKEY_ADDRESS &&
780 cl->ms.map && !cl->srcline)
781 cl->srcline = get_srcline(cl->ms.map->dso,
782 map__rip_2objdump(cl->ms.map,
Andi Kleen85c116a2014-11-12 18:05:27 -0800783 cl->ip),
784 cl->ms.sym, false);
Andi Kleen23f09812014-11-12 18:05:24 -0800785 if (cl->srcline)
786 printed = scnprintf(bf, bfsize, "%s %s",
787 cl->ms.sym->name, cl->srcline);
788 else
789 printed = scnprintf(bf, bfsize, "%s", cl->ms.sym->name);
Andi Kleen2989cca2014-11-12 18:05:23 -0800790 } else
791 printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);
792
793 if (show_dso)
794 scnprintf(bf + printed, bfsize - printed, " %s",
795 cl->ms.map ?
796 cl->ms.map->dso->short_name :
797 "unknown");
798
799 return bf;
800}
Namhyung Kimd1149602014-12-30 14:38:13 +0900801
802static void free_callchain_node(struct callchain_node *node)
803{
804 struct callchain_list *list, *tmp;
805 struct callchain_node *child;
806 struct rb_node *n;
807
808 list_for_each_entry_safe(list, tmp, &node->val, list) {
809 list_del(&list->list);
810 free(list);
811 }
812
813 n = rb_first(&node->rb_root_in);
814 while (n) {
815 child = container_of(n, struct callchain_node, rb_node_in);
816 n = rb_next(n);
817 rb_erase(&child->rb_node_in, &node->rb_root_in);
818
819 free_callchain_node(child);
820 free(child);
821 }
822}
823
824void free_callchain(struct callchain_root *root)
825{
826 if (!symbol_conf.use_callchain)
827 return;
828
829 free_callchain_node(&root->node);
830}