perf_counter: revamp syscall input ABI
[deliverable/linux.git] / 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 <linux/types.h>
17 #include <linux/ioctl.h>
18
19 /*
20 * User-space ABI bits:
21 */
22
23 /*
24 * hw_event.type
25 */
26 enum perf_event_types {
27 PERF_TYPE_HARDWARE = 0,
28 PERF_TYPE_SOFTWARE = 1,
29 PERF_TYPE_TRACEPOINT = 2,
30
31 /*
32 * available TYPE space, raw is the max value.
33 */
34
35 PERF_TYPE_RAW = 128,
36 };
37
38 /*
39 * Generalized performance counter event types, used by the hw_event.event_id
40 * parameter of the sys_perf_counter_open() syscall:
41 */
42 enum hw_event_ids {
43 /*
44 * Common hardware events, generalized by the kernel:
45 */
46 PERF_COUNT_CPU_CYCLES = 0,
47 PERF_COUNT_INSTRUCTIONS = 1,
48 PERF_COUNT_CACHE_REFERENCES = 2,
49 PERF_COUNT_CACHE_MISSES = 3,
50 PERF_COUNT_BRANCH_INSTRUCTIONS = 4,
51 PERF_COUNT_BRANCH_MISSES = 5,
52 PERF_COUNT_BUS_CYCLES = 6,
53
54 PERF_HW_EVENTS_MAX = 7,
55 };
56
57 /*
58 * Special "software" counters provided by the kernel, even if the hardware
59 * does not support performance counters. These counters measure various
60 * physical and sw events of the kernel (and allow the profiling of them as
61 * well):
62 */
63 enum sw_event_ids {
64 PERF_COUNT_CPU_CLOCK = 0,
65 PERF_COUNT_TASK_CLOCK = 1,
66 PERF_COUNT_PAGE_FAULTS = 2,
67 PERF_COUNT_CONTEXT_SWITCHES = 3,
68 PERF_COUNT_CPU_MIGRATIONS = 4,
69 PERF_COUNT_PAGE_FAULTS_MIN = 5,
70 PERF_COUNT_PAGE_FAULTS_MAJ = 6,
71
72 PERF_SW_EVENTS_MAX = 7,
73 };
74
75 /*
76 * IRQ-notification data record type:
77 */
78 enum perf_counter_record_type {
79 PERF_RECORD_SIMPLE = 0,
80 PERF_RECORD_IRQ = 1,
81 PERF_RECORD_GROUP = 2,
82 };
83
84 /*
85 * Hardware event to monitor via a performance monitoring counter:
86 */
87 struct perf_counter_hw_event {
88 union {
89 struct {
90 __u64 event_id : 56,
91 type : 8;
92 };
93 struct {
94 __u64 raw_event_id : 63,
95 raw_type : 1;
96 };
97 __u64 event_config;
98 };
99
100 __u64 irq_period;
101 __u64 record_type;
102 __u64 read_format;
103
104 __u64 disabled : 1, /* off by default */
105 nmi : 1, /* NMI sampling */
106 inherit : 1, /* children inherit it */
107 pinned : 1, /* must always be on PMU */
108 exclusive : 1, /* only group on PMU */
109 exclude_user : 1, /* don't count user */
110 exclude_kernel : 1, /* ditto kernel */
111 exclude_hv : 1, /* ditto hypervisor */
112 exclude_idle : 1, /* don't count when idle */
113
114 __reserved_1 : 55;
115
116 __u32 extra_config_len;
117 __u32 __reserved_4;
118
119 __u64 __reserved_2;
120 __u64 __reserved_3;
121 };
122
123 /*
124 * Ioctls that can be done on a perf counter fd:
125 */
126 #define PERF_COUNTER_IOC_ENABLE _IO('$', 0)
127 #define PERF_COUNTER_IOC_DISABLE _IO('$', 1)
128
129 #ifdef __KERNEL__
130 /*
131 * Kernel-internal data types and definitions:
132 */
133
134 #ifdef CONFIG_PERF_COUNTERS
135 # include <asm/perf_counter.h>
136 #endif
137
138 #include <linux/list.h>
139 #include <linux/mutex.h>
140 #include <linux/rculist.h>
141 #include <linux/rcupdate.h>
142 #include <linux/spinlock.h>
143 #include <linux/hrtimer.h>
144 #include <asm/atomic.h>
145
146 struct task_struct;
147
148 /**
149 * struct hw_perf_counter - performance counter hardware details:
150 */
151 struct hw_perf_counter {
152 #ifdef CONFIG_PERF_COUNTERS
153 union {
154 struct { /* hardware */
155 u64 config;
156 unsigned long config_base;
157 unsigned long counter_base;
158 int nmi;
159 unsigned int idx;
160 };
161 union { /* software */
162 atomic64_t count;
163 struct hrtimer hrtimer;
164 };
165 };
166 atomic64_t prev_count;
167 u64 irq_period;
168 atomic64_t period_left;
169 #endif
170 };
171
172 /*
173 * Hardcoded buffer length limit for now, for IRQ-fed events:
174 */
175 #define PERF_DATA_BUFLEN 2048
176
177 /**
178 * struct perf_data - performance counter IRQ data sampling ...
179 */
180 struct perf_data {
181 int len;
182 int rd_idx;
183 int overrun;
184 u8 data[PERF_DATA_BUFLEN];
185 };
186
187 struct perf_counter;
188
189 /**
190 * struct hw_perf_counter_ops - performance counter hw ops
191 */
192 struct hw_perf_counter_ops {
193 int (*enable) (struct perf_counter *counter);
194 void (*disable) (struct perf_counter *counter);
195 void (*read) (struct perf_counter *counter);
196 };
197
198 /**
199 * enum perf_counter_active_state - the states of a counter
200 */
201 enum perf_counter_active_state {
202 PERF_COUNTER_STATE_ERROR = -2,
203 PERF_COUNTER_STATE_OFF = -1,
204 PERF_COUNTER_STATE_INACTIVE = 0,
205 PERF_COUNTER_STATE_ACTIVE = 1,
206 };
207
208 struct file;
209
210 /**
211 * struct perf_counter - performance counter kernel representation:
212 */
213 struct perf_counter {
214 #ifdef CONFIG_PERF_COUNTERS
215 struct list_head list_entry;
216 struct list_head event_entry;
217 struct list_head sibling_list;
218 struct perf_counter *group_leader;
219 const struct hw_perf_counter_ops *hw_ops;
220
221 enum perf_counter_active_state state;
222 enum perf_counter_active_state prev_state;
223 atomic64_t count;
224
225 struct perf_counter_hw_event hw_event;
226 struct hw_perf_counter hw;
227
228 struct perf_counter_context *ctx;
229 struct task_struct *task;
230 struct file *filp;
231
232 struct perf_counter *parent;
233 struct list_head child_list;
234
235 /*
236 * Protect attach/detach and child_list:
237 */
238 struct mutex mutex;
239
240 int oncpu;
241 int cpu;
242
243 /* read() / irq related data */
244 wait_queue_head_t waitq;
245 /* optional: for NMIs */
246 int wakeup_pending;
247 struct perf_data *irqdata;
248 struct perf_data *usrdata;
249 struct perf_data data[2];
250
251 void (*destroy)(struct perf_counter *);
252 struct rcu_head rcu_head;
253 #endif
254 };
255
256 /**
257 * struct perf_counter_context - counter context structure
258 *
259 * Used as a container for task counters and CPU counters as well:
260 */
261 struct perf_counter_context {
262 #ifdef CONFIG_PERF_COUNTERS
263 /*
264 * Protect the states of the counters in the list,
265 * nr_active, and the list:
266 */
267 spinlock_t lock;
268 /*
269 * Protect the list of counters. Locking either mutex or lock
270 * is sufficient to ensure the list doesn't change; to change
271 * the list you need to lock both the mutex and the spinlock.
272 */
273 struct mutex mutex;
274
275 struct list_head counter_list;
276 struct list_head event_list;
277 int nr_counters;
278 int nr_active;
279 int is_active;
280 struct task_struct *task;
281 #endif
282 };
283
284 /**
285 * struct perf_counter_cpu_context - per cpu counter context structure
286 */
287 struct perf_cpu_context {
288 struct perf_counter_context ctx;
289 struct perf_counter_context *task_ctx;
290 int active_oncpu;
291 int max_pertask;
292 int exclusive;
293 };
294
295 /*
296 * Set by architecture code:
297 */
298 extern int perf_max_counters;
299
300 #ifdef CONFIG_PERF_COUNTERS
301 extern const struct hw_perf_counter_ops *
302 hw_perf_counter_init(struct perf_counter *counter);
303
304 extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
305 extern void perf_counter_task_sched_out(struct task_struct *task, int cpu);
306 extern void perf_counter_task_tick(struct task_struct *task, int cpu);
307 extern void perf_counter_init_task(struct task_struct *child);
308 extern void perf_counter_exit_task(struct task_struct *child);
309 extern void perf_counter_notify(struct pt_regs *regs);
310 extern void perf_counter_print_debug(void);
311 extern void perf_counter_unthrottle(void);
312 extern u64 hw_perf_save_disable(void);
313 extern void hw_perf_restore(u64 ctrl);
314 extern int perf_counter_task_disable(void);
315 extern int perf_counter_task_enable(void);
316 extern int hw_perf_group_sched_in(struct perf_counter *group_leader,
317 struct perf_cpu_context *cpuctx,
318 struct perf_counter_context *ctx, int cpu);
319
320 /*
321 * Return 1 for a software counter, 0 for a hardware counter
322 */
323 static inline int is_software_counter(struct perf_counter *counter)
324 {
325 return !counter->hw_event.raw_type &&
326 counter->hw_event.type != PERF_TYPE_HARDWARE;
327 }
328
329 extern void perf_swcounter_event(u32, u64, int, struct pt_regs *);
330
331 #else
332 static inline void
333 perf_counter_task_sched_in(struct task_struct *task, int cpu) { }
334 static inline void
335 perf_counter_task_sched_out(struct task_struct *task, int cpu) { }
336 static inline void
337 perf_counter_task_tick(struct task_struct *task, int cpu) { }
338 static inline void perf_counter_init_task(struct task_struct *child) { }
339 static inline void perf_counter_exit_task(struct task_struct *child) { }
340 static inline void perf_counter_notify(struct pt_regs *regs) { }
341 static inline void perf_counter_print_debug(void) { }
342 static inline void perf_counter_unthrottle(void) { }
343 static inline void hw_perf_restore(u64 ctrl) { }
344 static inline u64 hw_perf_save_disable(void) { return 0; }
345 static inline int perf_counter_task_disable(void) { return -EINVAL; }
346 static inline int perf_counter_task_enable(void) { return -EINVAL; }
347
348 static inline void perf_swcounter_event(u32 event, u64 nr,
349 int nmi, struct pt_regs *regs) { }
350 #endif
351
352 #endif /* __KERNEL__ */
353 #endif /* _LINUX_PERF_COUNTER_H */
This page took 0.040789 seconds and 6 git commands to generate.