blob: eb340571e7d6928394b218010926898029569d9d [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;
Frederic Weisbecker529363b2011-01-14 04:52:01 +010024 struct list_head siblings;
Ingo Molnarf37a2912009-07-01 12:37:06 +020025 struct list_head children;
26 struct list_head val;
Frederic Weisbecker4eb3e472009-07-02 17:58:21 +020027 struct rb_node rb_node; /* to sort nodes in an rbtree */
28 struct rb_root rb_root; /* sorted 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
44struct callchain_param {
45 enum chain_mode mode;
Arnaldo Carvalho de Melo232a5c92010-05-09 20:28:10 -030046 u32 print_limit;
Frederic Weisbecker805d1272009-07-05 07:39:21 +020047 double min_percent;
48 sort_chain_func_t sort;
Sam Liaod797fdc2011-06-07 23:49:46 +080049 enum chain_order order;
Frederic Weisbecker805d1272009-07-05 07:39:21 +020050};
51
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020052struct callchain_list {
Ingo Molnarf37a2912009-07-01 12:37:06 +020053 u64 ip;
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -030054 struct map_symbol ms;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020055 struct list_head list;
56};
57
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +010058/*
59 * A callchain cursor is a single linked list that
60 * let one feed a callchain progressively.
Masanari Iida3fd44cd2012-07-18 01:20:59 +090061 * It keeps persistent allocated entries to minimize
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +010062 * allocations.
63 */
64struct callchain_cursor_node {
65 u64 ip;
66 struct map *map;
67 struct symbol *sym;
68 struct callchain_cursor_node *next;
69};
70
71struct callchain_cursor {
72 u64 nr;
73 struct callchain_cursor_node *first;
74 struct callchain_cursor_node **last;
75 u64 pos;
76 struct callchain_cursor_node *curr;
77};
78
Namhyung Kim47260642012-05-31 14:43:26 +090079extern __thread struct callchain_cursor callchain_cursor;
80
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +020081static inline void callchain_init(struct callchain_root *root)
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020082{
Frederic Weisbecker529363b2011-01-14 04:52:01 +010083 INIT_LIST_HEAD(&root->node.siblings);
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +020084 INIT_LIST_HEAD(&root->node.children);
85 INIT_LIST_HEAD(&root->node.val);
Frederic Weisbecker97aa1052010-07-08 06:06:17 +020086
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +020087 root->node.parent = NULL;
88 root->node.hit = 0;
Frederic Weisbecker98ee74a2010-08-27 02:28:40 +020089 root->node.children_hit = 0;
Frederic Weisbeckerd2009c52010-08-22 20:05:22 +020090 root->max_depth = 0;
Frederic Weisbecker8cb76d92009-06-26 16:28:00 +020091}
92
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +010093static inline u64 callchain_cumul_hits(struct callchain_node *node)
Frederic Weisbecker19532872009-08-07 07:11:05 +020094{
95 return node->hit + node->children_hit;
96}
97
Frederic Weisbecker16537f12011-01-14 04:52:00 +010098int callchain_register_param(struct callchain_param *param);
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +010099int callchain_append(struct callchain_root *root,
100 struct callchain_cursor *cursor,
101 u64 period);
102
103int callchain_merge(struct callchain_cursor *cursor,
104 struct callchain_root *dst, struct callchain_root *src);
Arnaldo Carvalho de Melo139633c2010-05-09 11:47:13 -0300105
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200106struct ip_callchain;
107union perf_event;
108
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200109bool ip_callchain__valid(struct ip_callchain *chain,
110 const union perf_event *event);
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}
John Kacur8b40f522009-09-24 18:02:18 +0200146#endif /* __PERF_CALLCHAIN_H */