• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef __PERF_CALLCHAIN_H
2 #define __PERF_CALLCHAIN_H
3 
4 #include "../perf.h"
5 #include <linux/list.h>
6 #include <linux/rbtree.h>
7 #include "event.h"
8 #include "symbol.h"
9 
10 enum chain_mode {
11 	CHAIN_NONE,
12 	CHAIN_FLAT,
13 	CHAIN_GRAPH_ABS,
14 	CHAIN_GRAPH_REL
15 };
16 
17 enum chain_order {
18 	ORDER_CALLER,
19 	ORDER_CALLEE
20 };
21 
22 struct callchain_node {
23 	struct callchain_node	*parent;
24 	struct list_head	siblings;
25 	struct list_head	children;
26 	struct list_head	val;
27 	struct rb_node		rb_node; /* to sort nodes in an rbtree */
28 	struct rb_root		rb_root; /* sorted tree of children */
29 	unsigned int		val_nr;
30 	u64			hit;
31 	u64			children_hit;
32 };
33 
34 struct callchain_root {
35 	u64			max_depth;
36 	struct callchain_node	node;
37 };
38 
39 struct callchain_param;
40 
41 typedef void (*sort_chain_func_t)(struct rb_root *, struct callchain_root *,
42 				 u64, struct callchain_param *);
43 
44 enum chain_key {
45 	CCKEY_FUNCTION,
46 	CCKEY_ADDRESS
47 };
48 
49 struct callchain_param {
50 	enum chain_mode 	mode;
51 	u32			print_limit;
52 	double			min_percent;
53 	sort_chain_func_t	sort;
54 	enum chain_order	order;
55 	enum chain_key		key;
56 };
57 
58 struct callchain_list {
59 	u64			ip;
60 	struct map_symbol	ms;
61 	struct list_head	list;
62 };
63 
64 /*
65  * A callchain cursor is a single linked list that
66  * let one feed a callchain progressively.
67  * It keeps persistent allocated entries to minimize
68  * allocations.
69  */
70 struct callchain_cursor_node {
71 	u64				ip;
72 	struct map			*map;
73 	struct symbol			*sym;
74 	struct callchain_cursor_node	*next;
75 };
76 
77 struct 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 
85 #ifdef __APPLE__
86 extern struct callchain_cursor callchain_cursor;
87 #else
88 extern __thread struct callchain_cursor callchain_cursor;
89 #endif
90 
callchain_init(struct callchain_root * root)91 static inline void callchain_init(struct callchain_root *root)
92 {
93 	INIT_LIST_HEAD(&root->node.siblings);
94 	INIT_LIST_HEAD(&root->node.children);
95 	INIT_LIST_HEAD(&root->node.val);
96 
97 	root->node.parent = NULL;
98 	root->node.hit = 0;
99 	root->node.children_hit = 0;
100 	root->max_depth = 0;
101 }
102 
callchain_cumul_hits(struct callchain_node * node)103 static inline u64 callchain_cumul_hits(struct callchain_node *node)
104 {
105 	return node->hit + node->children_hit;
106 }
107 
108 int callchain_register_param(struct callchain_param *param);
109 int callchain_append(struct callchain_root *root,
110 		     struct callchain_cursor *cursor,
111 		     u64 period);
112 
113 int callchain_merge(struct callchain_cursor *cursor,
114 		    struct callchain_root *dst, struct callchain_root *src);
115 
116 /*
117  * Initialize a cursor before adding entries inside, but keep
118  * the previously allocated entries as a cache.
119  */
callchain_cursor_reset(struct callchain_cursor * cursor)120 static inline void callchain_cursor_reset(struct callchain_cursor *cursor)
121 {
122 	cursor->nr = 0;
123 	cursor->last = &cursor->first;
124 }
125 
126 int callchain_cursor_append(struct callchain_cursor *cursor, u64 ip,
127 			    struct map *map, struct symbol *sym);
128 
129 /* Close a cursor writing session. Initialize for the reader */
callchain_cursor_commit(struct callchain_cursor * cursor)130 static inline void callchain_cursor_commit(struct callchain_cursor *cursor)
131 {
132 	cursor->curr = cursor->first;
133 	cursor->pos = 0;
134 }
135 
136 /* Cursor reading iteration helpers */
137 static inline struct callchain_cursor_node *
callchain_cursor_current(struct callchain_cursor * cursor)138 callchain_cursor_current(struct callchain_cursor *cursor)
139 {
140 	if (cursor->pos == cursor->nr)
141 		return NULL;
142 
143 	return cursor->curr;
144 }
145 
callchain_cursor_advance(struct callchain_cursor * cursor)146 static inline void callchain_cursor_advance(struct callchain_cursor *cursor)
147 {
148 	cursor->curr = cursor->curr->next;
149 	cursor->pos++;
150 }
151 
152 struct option;
153 
154 int record_parse_callchain(const char *arg, struct perf_record_opts *opts);
155 int record_parse_callchain_opt(const struct option *opt, const char *arg, int unset);
156 int record_callchain_opt(const struct option *opt, const char *arg, int unset);
157 
158 extern const char record_callchain_help[];
159 #endif	/* __PERF_CALLCHAIN_H */
160