perfcounters: implement "counter inheritance"
[safe/jmp/linux-2.6] / include / linux / perf_counter.h
1 /*
2  *  Performance counters:
3  *
4  *   Copyright(C) 2008, Thomas Gleixner <tglx@linutronix.de>
5  *   Copyright(C) 2008, Red Hat, Inc., Ingo Molnar
6  *
7  *  Data type definitions, declarations, prototypes.
8  *
9  *  Started by: Thomas Gleixner and Ingo Molnar
10  *
11  *  For licencing details see kernel-base/COPYING
12  */
13 #ifndef _LINUX_PERF_COUNTER_H
14 #define _LINUX_PERF_COUNTER_H
15
16 #include <asm/atomic.h>
17
18 #include <linux/list.h>
19 #include <linux/mutex.h>
20 #include <linux/rculist.h>
21 #include <linux/rcupdate.h>
22 #include <linux/spinlock.h>
23
24 struct task_struct;
25
26 /*
27  * User-space ABI bits:
28  */
29
30 /*
31  * Generalized performance counter event types, used by the hw_event.type
32  * parameter of the sys_perf_counter_open() syscall:
33  */
34 enum hw_event_types {
35         /*
36          * Common hardware events, generalized by the kernel:
37          */
38         PERF_COUNT_CYCLES               =  0,
39         PERF_COUNT_INSTRUCTIONS         =  1,
40         PERF_COUNT_CACHE_REFERENCES     =  2,
41         PERF_COUNT_CACHE_MISSES         =  3,
42         PERF_COUNT_BRANCH_INSTRUCTIONS  =  4,
43         PERF_COUNT_BRANCH_MISSES        =  5,
44
45         /*
46          * Special "software" counters provided by the kernel, even if
47          * the hardware does not support performance counters. These
48          * counters measure various physical and sw events of the
49          * kernel (and allow the profiling of them as well):
50          */
51         PERF_COUNT_CPU_CLOCK            = -1,
52         PERF_COUNT_TASK_CLOCK           = -2,
53         /*
54          * Future software events:
55          */
56         /* PERF_COUNT_PAGE_FAULTS       = -3,
57            PERF_COUNT_CONTEXT_SWITCHES  = -4, */
58 };
59
60 /*
61  * IRQ-notification data record type:
62  */
63 enum perf_counter_record_type {
64         PERF_RECORD_SIMPLE              =  0,
65         PERF_RECORD_IRQ                 =  1,
66         PERF_RECORD_GROUP               =  2,
67 };
68
69 /*
70  * Hardware event to monitor via a performance monitoring counter:
71  */
72 struct perf_counter_hw_event {
73         s64                     type;
74
75         u64                     irq_period;
76         u32                     record_type;
77
78         u32                     disabled     :  1, /* off by default      */
79                                 nmi          :  1, /* NMI sampling        */
80                                 raw          :  1, /* raw event type      */
81                                 inherit      :  1, /* children inherit it */
82                                 __reserved_1 : 28;
83
84         u64                     __reserved_2;
85 };
86
87 /*
88  * Kernel-internal data types:
89  */
90
91 /**
92  * struct hw_perf_counter - performance counter hardware details:
93  */
94 struct hw_perf_counter {
95 #ifdef CONFIG_PERF_COUNTERS
96         u64                             config;
97         unsigned long                   config_base;
98         unsigned long                   counter_base;
99         int                             nmi;
100         unsigned int                    idx;
101         atomic64_t                      prev_count;
102         u64                             irq_period;
103         atomic64_t                      period_left;
104 #endif
105 };
106
107 /*
108  * Hardcoded buffer length limit for now, for IRQ-fed events:
109  */
110 #define PERF_DATA_BUFLEN                2048
111
112 /**
113  * struct perf_data - performance counter IRQ data sampling ...
114  */
115 struct perf_data {
116         int                             len;
117         int                             rd_idx;
118         int                             overrun;
119         u8                              data[PERF_DATA_BUFLEN];
120 };
121
122 struct perf_counter;
123
124 /**
125  * struct hw_perf_counter_ops - performance counter hw ops
126  */
127 struct hw_perf_counter_ops {
128         void (*hw_perf_counter_enable)  (struct perf_counter *counter);
129         void (*hw_perf_counter_disable) (struct perf_counter *counter);
130         void (*hw_perf_counter_read)    (struct perf_counter *counter);
131 };
132
133 /**
134  * enum perf_counter_active_state - the states of a counter
135  */
136 enum perf_counter_active_state {
137         PERF_COUNTER_STATE_OFF          = -1,
138         PERF_COUNTER_STATE_INACTIVE     =  0,
139         PERF_COUNTER_STATE_ACTIVE       =  1,
140 };
141
142 struct file;
143
144 /**
145  * struct perf_counter - performance counter kernel representation:
146  */
147 struct perf_counter {
148 #ifdef CONFIG_PERF_COUNTERS
149         struct list_head                list_entry;
150         struct list_head                sibling_list;
151         struct perf_counter             *group_leader;
152         const struct hw_perf_counter_ops *hw_ops;
153
154         enum perf_counter_active_state  state;
155         atomic64_t                      count;
156
157         struct perf_counter_hw_event    hw_event;
158         struct hw_perf_counter          hw;
159
160         struct perf_counter_context     *ctx;
161         struct task_struct              *task;
162         struct file                     *filp;
163
164         unsigned int                    nr_inherited;
165         struct perf_counter             *parent;
166         /*
167          * Protect attach/detach:
168          */
169         struct mutex                    mutex;
170
171         int                             oncpu;
172         int                             cpu;
173
174         /* read() / irq related data */
175         wait_queue_head_t               waitq;
176         /* optional: for NMIs */
177         int                             wakeup_pending;
178         struct perf_data                *irqdata;
179         struct perf_data                *usrdata;
180         struct perf_data                data[2];
181 #endif
182 };
183
184 /**
185  * struct perf_counter_context - counter context structure
186  *
187  * Used as a container for task counters and CPU counters as well:
188  */
189 struct perf_counter_context {
190 #ifdef CONFIG_PERF_COUNTERS
191         /*
192          * Protect the list of counters:
193          */
194         spinlock_t              lock;
195
196         struct list_head        counter_list;
197         int                     nr_counters;
198         int                     nr_active;
199         struct task_struct      *task;
200 #endif
201 };
202
203 /**
204  * struct perf_counter_cpu_context - per cpu counter context structure
205  */
206 struct perf_cpu_context {
207         struct perf_counter_context     ctx;
208         struct perf_counter_context     *task_ctx;
209         int                             active_oncpu;
210         int                             max_pertask;
211 };
212
213 /*
214  * Set by architecture code:
215  */
216 extern int perf_max_counters;
217
218 #ifdef CONFIG_PERF_COUNTERS
219 extern void
220 perf_counter_show(struct perf_counter *counter, char *str, int trace);
221 extern const struct hw_perf_counter_ops *
222 hw_perf_counter_init(struct perf_counter *counter);
223
224 extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
225 extern void perf_counter_task_sched_out(struct task_struct *task, int cpu);
226 extern void perf_counter_task_tick(struct task_struct *task, int cpu);
227 extern void perf_counter_init_task(struct task_struct *child);
228 extern void perf_counter_exit_task(struct task_struct *child);
229 extern void perf_counter_notify(struct pt_regs *regs);
230 extern void perf_counter_print_debug(void);
231 extern u64 hw_perf_save_disable(void);
232 extern void hw_perf_restore(u64 ctrl);
233 extern int perf_counter_task_disable(void);
234 extern int perf_counter_task_enable(void);
235
236 #else
237 static inline void
238 perf_counter_show(struct perf_counter *counter, char *str, int trace)   { }
239 static inline void
240 perf_counter_task_sched_in(struct task_struct *task, int cpu)           { }
241 static inline void
242 perf_counter_task_sched_out(struct task_struct *task, int cpu)          { }
243 static inline void
244 perf_counter_task_tick(struct task_struct *task, int cpu)               { }
245 static inline void perf_counter_init_task(struct task_struct *child)    { }
246 static inline void perf_counter_exit_task(struct task_struct *child)    { }
247 static inline void perf_counter_notify(struct pt_regs *regs)            { }
248 static inline void perf_counter_print_debug(void)                       { }
249 static inline void hw_perf_restore(u64 ctrl)                    { }
250 static inline u64 hw_perf_save_disable(void)                  { return 0; }
251 static inline int perf_counter_task_disable(void)       { return -EINVAL; }
252 static inline int perf_counter_task_enable(void)        { return -EINVAL; }
253 #endif
254
255 #endif /* _LINUX_PERF_COUNTER_H */