blob: 8ad97e9b119fb414e0cf73a945c01be0acf93583 [file] [log] [blame]
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +02001#ifndef __PERF_CALLCHAIN_H
2#define __PERF_CALLCHAIN_H
3
4#include "../perf.h"
Arnaldo Carvalho de Melo5da50252009-07-01 14:46:08 -03005#include <linux/list.h>
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -03006#include <linux/rbtree.h>
Arnaldo Carvalho de Melo139633c2010-05-09 11:47:13 -03007#include "event.h"
Frederic Weisbecker44249612009-07-01 05:35:14 +02008#include "symbol.h"
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +02009
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020010enum chain_mode {
Frederic Weisbeckerb1a88342009-08-08 02:16:24 +020011 CHAIN_NONE,
Frederic Weisbecker805d1272009-07-05 07:39:21 +020012 CHAIN_FLAT,
13 CHAIN_GRAPH_ABS,
14 CHAIN_GRAPH_REL
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020015};
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020016
Sam Liaod797fdc2011-06-07 23:49:46 +080017enum chain_order {
18 ORDER_CALLER,
19 ORDER_CALLEE
20};
21
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020022struct callchain_node {
23 struct callchain_node *parent;
Ingo Molnarf37a2912009-07-01 12:37:06 +020024 struct list_head val;
Namhyung Kime3695172013-10-11 14:15:36 +090025 struct rb_node rb_node_in; /* to insert nodes in an rbtree */
26 struct rb_node rb_node; /* to sort nodes in an output tree */
27 struct rb_root rb_root_in; /* input tree of children */
28 struct rb_root rb_root; /* sorted output tree of children */
Ingo Molnarf37a2912009-07-01 12:37:06 +020029 unsigned int val_nr;
30 u64 hit;
Frederic Weisbecker19532872009-08-07 07:11:05 +020031 u64 children_hit;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020032};
33
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +020034struct callchain_root {
35 u64 max_depth;
36 struct callchain_node node;
37};
38
Frederic Weisbecker805d1272009-07-05 07:39:21 +020039struct callchain_param;
40
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +020041typedef void (*sort_chain_func_t)(struct rb_root *, struct callchain_root *,
Frederic Weisbecker805d1272009-07-05 07:39:21 +020042 u64, struct callchain_param *);
43
Andi Kleen99571ab2013-07-18 15:33:57 -070044enum chain_key {
45 CCKEY_FUNCTION,
46 CCKEY_ADDRESS
47};
48
Frederic Weisbecker805d1272009-07-05 07:39:21 +020049struct callchain_param {
50 enum chain_mode mode;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -030051 u32 print_limit;
Frederic Weisbecker805d1272009-07-05 07:39:21 +020052 double min_percent;
53 sort_chain_func_t sort;
Sam Liaod797fdc2011-06-07 23:49:46 +080054 enum chain_order order;
Andi Kleen99571ab2013-07-18 15:33:57 -070055 enum chain_key key;
Frederic Weisbecker805d1272009-07-05 07:39:21 +020056};
57
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020058struct callchain_list {
Ingo Molnarf37a2912009-07-01 12:37:06 +020059 u64 ip;
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -030060 struct map_symbol ms;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020061 struct list_head list;
62};
63
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +010064/*
65 * A callchain cursor is a single linked list that
66 * let one feed a callchain progressively.
Masanari Iida3fd44cd2012-07-18 01:20:59 +090067 * It keeps persistent allocated entries to minimize
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +010068 * allocations.
69 */
70struct callchain_cursor_node {
71 u64 ip;
72 struct map *map;
73 struct symbol *sym;
74 struct callchain_cursor_node *next;
75};
76
77struct callchain_cursor {
78 u64 nr;
79 struct callchain_cursor_node *first;
80 struct callchain_cursor_node **last;
81 u64 pos;
82 struct callchain_cursor_node *curr;
83};
84
Namhyung Kim47260642012-05-31 14:43:26 +090085extern __thread struct callchain_cursor callchain_cursor;
86
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +020087static inline void callchain_init(struct callchain_root *root)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020088{
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +020089 INIT_LIST_HEAD(&root->node.val);
Frederic Weisbecker97aa1052010-07-08 06:06:17 +020090
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +020091 root->node.parent = NULL;
92 root->node.hit = 0;
Frederic Weisbecker98ee74a2010-08-27 02:28:40 +020093 root->node.children_hit = 0;
Namhyung Kime3695172013-10-11 14:15:36 +090094 root->node.rb_root_in = RB_ROOT;
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +020095 root->max_depth = 0;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020096}
97
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +010098static inline u64 callchain_cumul_hits(struct callchain_node *node)
Frederic Weisbecker19532872009-08-07 07:11:05 +020099{
100 return node->hit + node->children_hit;
101}
102
Frederic Weisbecker16537f12011-01-14 04:52:00 +0100103int callchain_register_param(struct callchain_param *param);
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100104int callchain_append(struct callchain_root *root,
105 struct callchain_cursor *cursor,
106 u64 period);
107
108int callchain_merge(struct callchain_cursor *cursor,
109 struct callchain_root *dst, struct callchain_root *src);
Arnaldo Carvalho de Melo139633c2010-05-09 11:47:13 -0300110
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100111/*
112 * Initialize a cursor before adding entries inside, but keep
113 * the previously allocated entries as a cache.
114 */
115static inline void callchain_cursor_reset(struct callchain_cursor *cursor)
116{
117 cursor->nr = 0;
118 cursor->last = &cursor->first;
119}
120
121int callchain_cursor_append(struct callchain_cursor *cursor, u64 ip,
122 struct map *map, struct symbol *sym);
123
124/* Close a cursor writing session. Initialize for the reader */
125static inline void callchain_cursor_commit(struct callchain_cursor *cursor)
126{
127 cursor->curr = cursor->first;
128 cursor->pos = 0;
129}
130
131/* Cursor reading iteration helpers */
132static inline struct callchain_cursor_node *
133callchain_cursor_current(struct callchain_cursor *cursor)
134{
135 if (cursor->pos == cursor->nr)
136 return NULL;
137
138 return cursor->curr;
139}
140
141static inline void callchain_cursor_advance(struct callchain_cursor *cursor)
142{
143 cursor->curr = cursor->curr->next;
144 cursor->pos++;
145}
Arnaldo Carvalho de Melo75d9a1082012-12-11 16:46:05 -0300146
147struct option;
Namhyung Kim2dc9fb12014-01-14 14:25:35 +0900148struct hist_entry;
Arnaldo Carvalho de Melo75d9a1082012-12-11 16:46:05 -0300149
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -0300150int record_parse_callchain(const char *arg, struct record_opts *opts);
Arnaldo Carvalho de Melo75d9a1082012-12-11 16:46:05 -0300151int record_parse_callchain_opt(const struct option *opt, const char *arg, int unset);
Jiri Olsa09b0fd42013-10-26 16:25:33 +0200152int record_callchain_opt(const struct option *opt, const char *arg, int unset);
153
Namhyung Kim2dc9fb12014-01-14 14:25:35 +0900154int sample__resolve_callchain(struct perf_sample *sample, struct symbol **parent,
155 struct perf_evsel *evsel, struct addr_location *al,
156 int max_stack);
157int hist_entry__append_callchain(struct hist_entry *he, struct perf_sample *sample);
158
Arnaldo Carvalho de Melo75d9a1082012-12-11 16:46:05 -0300159extern const char record_callchain_help[];
John Kacur8b40f522009-09-24 18:02:18 +0200160#endif /* __PERF_CALLCHAIN_H */