blob: a4b76c0175f34b3e7fc97dc9bcf288c52253e09d [file] [log] [blame]
Thomas Gleixner0793a612008-12-04 20:12:29 +01001/*
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
Paul Mackerrasf3dfd262009-02-26 22:43:46 +110016#include <linux/types.h>
17#include <linux/ioctl.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010018
19/*
Ingo Molnar9f66a382008-12-10 12:33:23 +010020 * User-space ABI bits:
21 */
22
23/*
Peter Zijlstrab8e83512009-03-19 20:26:18 +010024 * hw_event.type
25 */
26enum 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
Ingo Molnar9f66a382008-12-10 12:33:23 +010040 * parameter of the sys_perf_counter_open() syscall:
Thomas Gleixner0793a612008-12-04 20:12:29 +010041 */
Peter Zijlstrab8e83512009-03-19 20:26:18 +010042enum hw_event_ids {
Thomas Gleixner0793a612008-12-04 20:12:29 +010043 /*
Ingo Molnar9f66a382008-12-10 12:33:23 +010044 * Common hardware events, generalized by the kernel:
Thomas Gleixner0793a612008-12-04 20:12:29 +010045 */
Peter Zijlstrab8e83512009-03-19 20:26:18 +010046 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,
Ingo Molnar9f66a382008-12-10 12:33:23 +010053
Peter Zijlstrab8e83512009-03-19 20:26:18 +010054 PERF_HW_EVENTS_MAX = 7,
55};
Ingo Molnar6c594c22008-12-14 12:34:15 +010056
Peter Zijlstrab8e83512009-03-19 20:26:18 +010057/*
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 */
63enum 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,
Ingo Molnar6c594c22008-12-14 12:34:15 +010071
Peter Zijlstrab8e83512009-03-19 20:26:18 +010072 PERF_SW_EVENTS_MAX = 7,
Thomas Gleixner0793a612008-12-04 20:12:29 +010073};
74
75/*
76 * IRQ-notification data record type:
77 */
Ingo Molnar9f66a382008-12-10 12:33:23 +010078enum perf_counter_record_type {
Peter Zijlstrab8e83512009-03-19 20:26:18 +010079 PERF_RECORD_SIMPLE = 0,
80 PERF_RECORD_IRQ = 1,
81 PERF_RECORD_GROUP = 2,
Thomas Gleixner0793a612008-12-04 20:12:29 +010082};
83
Ingo Molnar9f66a382008-12-10 12:33:23 +010084/*
85 * Hardware event to monitor via a performance monitoring counter:
86 */
87struct perf_counter_hw_event {
Peter Zijlstrab8e83512009-03-19 20:26:18 +010088 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 };
Ingo Molnar9f66a382008-12-10 12:33:23 +010099
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100100 __u64 irq_period;
Paul Mackerras2743a5b2009-03-04 20:36:51 +1100101 __u64 record_type;
102 __u64 read_format;
Ingo Molnar9f66a382008-12-10 12:33:23 +0100103
Paul Mackerras2743a5b2009-03-04 20:36:51 +1100104 __u64 disabled : 1, /* off by default */
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100105 nmi : 1, /* NMI sampling */
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100106 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 */
Paul Mackerras2743a5b2009-03-04 20:36:51 +1100112 exclude_idle : 1, /* don't count when idle */
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100113
Peter Zijlstrab8e83512009-03-19 20:26:18 +0100114 __reserved_1 : 55;
Paul Mackerras2743a5b2009-03-04 20:36:51 +1100115
116 __u32 extra_config_len;
117 __u32 __reserved_4;
Ingo Molnar9f66a382008-12-10 12:33:23 +0100118
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100119 __u64 __reserved_2;
Paul Mackerras2743a5b2009-03-04 20:36:51 +1100120 __u64 __reserved_3;
Thomas Gleixnereab656a2008-12-08 19:26:59 +0100121};
122
Ingo Molnar9f66a382008-12-10 12:33:23 +0100123/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100124 * 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
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100129#ifdef __KERNEL__
Paul Mackerrasd859e292009-01-17 18:10:22 +1100130/*
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100131 * Kernel-internal data types and definitions:
Ingo Molnar9f66a382008-12-10 12:33:23 +0100132 */
133
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100134#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>
Peter Zijlstrad6d020e2009-03-13 12:21:35 +0100143#include <linux/hrtimer.h>
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100144#include <asm/atomic.h>
145
146struct task_struct;
147
Thomas Gleixner0793a612008-12-04 20:12:29 +0100148/**
Ingo Molnar9f66a382008-12-10 12:33:23 +0100149 * struct hw_perf_counter - performance counter hardware details:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100150 */
151struct hw_perf_counter {
Ingo Molnaree060942008-12-13 09:00:03 +0100152#ifdef CONFIG_PERF_COUNTERS
Peter Zijlstrad6d020e2009-03-13 12:21:35 +0100153 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 };
Ingo Molnaree060942008-12-13 09:00:03 +0100166 atomic64_t prev_count;
Ingo Molnar9f66a382008-12-10 12:33:23 +0100167 u64 irq_period;
Ingo Molnaree060942008-12-13 09:00:03 +0100168 atomic64_t period_left;
169#endif
Thomas Gleixner0793a612008-12-04 20:12:29 +0100170};
171
172/*
173 * Hardcoded buffer length limit for now, for IRQ-fed events:
174 */
Ingo Molnar9f66a382008-12-10 12:33:23 +0100175#define PERF_DATA_BUFLEN 2048
Thomas Gleixner0793a612008-12-04 20:12:29 +0100176
177/**
178 * struct perf_data - performance counter IRQ data sampling ...
179 */
180struct perf_data {
Ingo Molnar9f66a382008-12-10 12:33:23 +0100181 int len;
182 int rd_idx;
183 int overrun;
184 u8 data[PERF_DATA_BUFLEN];
Thomas Gleixner0793a612008-12-04 20:12:29 +0100185};
186
Ingo Molnar621a01e2008-12-11 12:46:46 +0100187struct perf_counter;
188
189/**
190 * struct hw_perf_counter_ops - performance counter hw ops
191 */
192struct hw_perf_counter_ops {
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100193 int (*enable) (struct perf_counter *counter);
Ingo Molnar76715812008-12-17 14:20:28 +0100194 void (*disable) (struct perf_counter *counter);
195 void (*read) (struct perf_counter *counter);
Ingo Molnar621a01e2008-12-11 12:46:46 +0100196};
197
Thomas Gleixner0793a612008-12-04 20:12:29 +0100198/**
Ingo Molnar6a930702008-12-11 15:17:03 +0100199 * enum perf_counter_active_state - the states of a counter
200 */
201enum perf_counter_active_state {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100202 PERF_COUNTER_STATE_ERROR = -2,
Ingo Molnar6a930702008-12-11 15:17:03 +0100203 PERF_COUNTER_STATE_OFF = -1,
204 PERF_COUNTER_STATE_INACTIVE = 0,
205 PERF_COUNTER_STATE_ACTIVE = 1,
206};
207
Ingo Molnar9b51f662008-12-12 13:49:45 +0100208struct file;
209
Ingo Molnar6a930702008-12-11 15:17:03 +0100210/**
Thomas Gleixner0793a612008-12-04 20:12:29 +0100211 * struct perf_counter - performance counter kernel representation:
212 */
213struct perf_counter {
Ingo Molnaree060942008-12-13 09:00:03 +0100214#ifdef CONFIG_PERF_COUNTERS
Ingo Molnar04289bb2008-12-11 08:38:42 +0100215 struct list_head list_entry;
Peter Zijlstra592903c2009-03-13 12:21:36 +0100216 struct list_head event_entry;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100217 struct list_head sibling_list;
218 struct perf_counter *group_leader;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100219 const struct hw_perf_counter_ops *hw_ops;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100220
Ingo Molnar6a930702008-12-11 15:17:03 +0100221 enum perf_counter_active_state state;
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100222 enum perf_counter_active_state prev_state;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100223 atomic64_t count;
Ingo Molnaree060942008-12-13 09:00:03 +0100224
Ingo Molnar9f66a382008-12-10 12:33:23 +0100225 struct perf_counter_hw_event hw_event;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100226 struct hw_perf_counter hw;
227
228 struct perf_counter_context *ctx;
229 struct task_struct *task;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100230 struct file *filp;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100231
Ingo Molnar9b51f662008-12-12 13:49:45 +0100232 struct perf_counter *parent;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100233 struct list_head child_list;
234
Thomas Gleixner0793a612008-12-04 20:12:29 +0100235 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100236 * Protect attach/detach and child_list:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100237 */
238 struct mutex mutex;
239
240 int oncpu;
241 int cpu;
242
Thomas Gleixner0793a612008-12-04 20:12:29 +0100243 /* 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];
Peter Zijlstra592903c2009-03-13 12:21:36 +0100250
Peter Zijlstrae077df42009-03-19 20:26:17 +0100251 void (*destroy)(struct perf_counter *);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100252 struct rcu_head rcu_head;
Ingo Molnaree060942008-12-13 09:00:03 +0100253#endif
Thomas Gleixner0793a612008-12-04 20:12:29 +0100254};
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 */
261struct perf_counter_context {
262#ifdef CONFIG_PERF_COUNTERS
263 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100264 * Protect the states of the counters in the list,
265 * nr_active, and the list:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100266 */
267 spinlock_t lock;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100268 /*
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;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100274
275 struct list_head counter_list;
Peter Zijlstra592903c2009-03-13 12:21:36 +0100276 struct list_head event_list;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100277 int nr_counters;
278 int nr_active;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100279 int is_active;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100280 struct task_struct *task;
281#endif
282};
283
284/**
285 * struct perf_counter_cpu_context - per cpu counter context structure
286 */
287struct perf_cpu_context {
288 struct perf_counter_context ctx;
289 struct perf_counter_context *task_ctx;
290 int active_oncpu;
291 int max_pertask;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100292 int exclusive;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100293};
294
295/*
296 * Set by architecture code:
297 */
298extern int perf_max_counters;
299
300#ifdef CONFIG_PERF_COUNTERS
Ingo Molnar5c92d122008-12-11 13:21:10 +0100301extern const struct hw_perf_counter_ops *
Ingo Molnar621a01e2008-12-11 12:46:46 +0100302hw_perf_counter_init(struct perf_counter *counter);
303
Thomas Gleixner0793a612008-12-04 20:12:29 +0100304extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
305extern void perf_counter_task_sched_out(struct task_struct *task, int cpu);
306extern void perf_counter_task_tick(struct task_struct *task, int cpu);
Ingo Molnar9b51f662008-12-12 13:49:45 +0100307extern void perf_counter_init_task(struct task_struct *child);
308extern void perf_counter_exit_task(struct task_struct *child);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100309extern void perf_counter_notify(struct pt_regs *regs);
310extern void perf_counter_print_debug(void);
Mike Galbraith1b023a92009-01-23 10:13:01 +0100311extern void perf_counter_unthrottle(void);
Ingo Molnar01b28382008-12-11 13:45:51 +0100312extern u64 hw_perf_save_disable(void);
313extern void hw_perf_restore(u64 ctrl);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100314extern int perf_counter_task_disable(void);
315extern int perf_counter_task_enable(void);
Paul Mackerras3cbed422009-01-09 16:43:42 +1100316extern 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);
Ingo Molnar5c92d122008-12-11 13:21:10 +0100319
Peter Zijlstra0322cd62009-03-19 20:26:19 +0100320extern void perf_counter_output(struct perf_counter *counter,
321 int nmi, struct pt_regs *regs);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100322/*
323 * Return 1 for a software counter, 0 for a hardware counter
324 */
325static inline int is_software_counter(struct perf_counter *counter)
326{
Peter Zijlstrab8e83512009-03-19 20:26:18 +0100327 return !counter->hw_event.raw_type &&
328 counter->hw_event.type != PERF_TYPE_HARDWARE;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100329}
330
Peter Zijlstrab8e83512009-03-19 20:26:18 +0100331extern void perf_swcounter_event(u32, u64, int, struct pt_regs *);
Peter Zijlstra15dbf272009-03-13 12:21:32 +0100332
Thomas Gleixner0793a612008-12-04 20:12:29 +0100333#else
334static inline void
335perf_counter_task_sched_in(struct task_struct *task, int cpu) { }
336static inline void
337perf_counter_task_sched_out(struct task_struct *task, int cpu) { }
338static inline void
339perf_counter_task_tick(struct task_struct *task, int cpu) { }
Ingo Molnar9b51f662008-12-12 13:49:45 +0100340static inline void perf_counter_init_task(struct task_struct *child) { }
341static inline void perf_counter_exit_task(struct task_struct *child) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100342static inline void perf_counter_notify(struct pt_regs *regs) { }
343static inline void perf_counter_print_debug(void) { }
Mike Galbraith1b023a92009-01-23 10:13:01 +0100344static inline void perf_counter_unthrottle(void) { }
Peter Zijlstra15dbf272009-03-13 12:21:32 +0100345static inline void hw_perf_restore(u64 ctrl) { }
Ingo Molnar01b28382008-12-11 13:45:51 +0100346static inline u64 hw_perf_save_disable(void) { return 0; }
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100347static inline int perf_counter_task_disable(void) { return -EINVAL; }
348static inline int perf_counter_task_enable(void) { return -EINVAL; }
Peter Zijlstra15dbf272009-03-13 12:21:32 +0100349
Peter Zijlstrab8e83512009-03-19 20:26:18 +0100350static inline void perf_swcounter_event(u32 event, u64 nr,
Peter Zijlstra15dbf272009-03-13 12:21:32 +0100351 int nmi, struct pt_regs *regs) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100352#endif
353
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100354#endif /* __KERNEL__ */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100355#endif /* _LINUX_PERF_COUNTER_H */