blob: 186efaf49665044f8e4a0123ffff3a9a7405cc23 [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/*
24 * Generalized performance counter event types, used by the hw_event.type
25 * parameter of the sys_perf_counter_open() syscall:
Thomas Gleixner0793a612008-12-04 20:12:29 +010026 */
27enum hw_event_types {
Thomas Gleixner0793a612008-12-04 20:12:29 +010028 /*
Ingo Molnar9f66a382008-12-10 12:33:23 +010029 * Common hardware events, generalized by the kernel:
Thomas Gleixner0793a612008-12-04 20:12:29 +010030 */
Ingo Molnarf650a672008-12-23 12:17:29 +010031 PERF_COUNT_CPU_CYCLES = 0,
Ingo Molnar9f66a382008-12-10 12:33:23 +010032 PERF_COUNT_INSTRUCTIONS = 1,
33 PERF_COUNT_CACHE_REFERENCES = 2,
34 PERF_COUNT_CACHE_MISSES = 3,
35 PERF_COUNT_BRANCH_INSTRUCTIONS = 4,
36 PERF_COUNT_BRANCH_MISSES = 5,
Ingo Molnarf650a672008-12-23 12:17:29 +010037 PERF_COUNT_BUS_CYCLES = 6,
Ingo Molnar9f66a382008-12-10 12:33:23 +010038
Ingo Molnarf650a672008-12-23 12:17:29 +010039 PERF_HW_EVENTS_MAX = 7,
Ingo Molnar6c594c22008-12-14 12:34:15 +010040
Ingo Molnar9f66a382008-12-10 12:33:23 +010041 /*
42 * Special "software" counters provided by the kernel, even if
43 * the hardware does not support performance counters. These
44 * counters measure various physical and sw events of the
45 * kernel (and allow the profiling of them as well):
46 */
47 PERF_COUNT_CPU_CLOCK = -1,
48 PERF_COUNT_TASK_CLOCK = -2,
Ingo Molnar5d6a27d2008-12-14 12:28:33 +010049 PERF_COUNT_PAGE_FAULTS = -3,
50 PERF_COUNT_CONTEXT_SWITCHES = -4,
Ingo Molnar6c594c22008-12-14 12:34:15 +010051 PERF_COUNT_CPU_MIGRATIONS = -5,
52
53 PERF_SW_EVENTS_MIN = -6,
Thomas Gleixner0793a612008-12-04 20:12:29 +010054};
55
56/*
57 * IRQ-notification data record type:
58 */
Ingo Molnar9f66a382008-12-10 12:33:23 +010059enum perf_counter_record_type {
60 PERF_RECORD_SIMPLE = 0,
61 PERF_RECORD_IRQ = 1,
62 PERF_RECORD_GROUP = 2,
Thomas Gleixner0793a612008-12-04 20:12:29 +010063};
64
Ingo Molnar9f66a382008-12-10 12:33:23 +010065/*
66 * Hardware event to monitor via a performance monitoring counter:
67 */
68struct perf_counter_hw_event {
Paul Mackerrasf3dfd262009-02-26 22:43:46 +110069 __s64 type;
Ingo Molnar9f66a382008-12-10 12:33:23 +010070
Paul Mackerrasf3dfd262009-02-26 22:43:46 +110071 __u64 irq_period;
72 __u32 record_type;
Ingo Molnar9f66a382008-12-10 12:33:23 +010073
Paul Mackerrasf3dfd262009-02-26 22:43:46 +110074 __u32 disabled : 1, /* off by default */
Paul Mackerras0475f9e2009-02-11 14:35:35 +110075 nmi : 1, /* NMI sampling */
76 raw : 1, /* raw event type */
77 inherit : 1, /* children inherit it */
78 pinned : 1, /* must always be on PMU */
79 exclusive : 1, /* only group on PMU */
80 exclude_user : 1, /* don't count user */
81 exclude_kernel : 1, /* ditto kernel */
82 exclude_hv : 1, /* ditto hypervisor */
Paul Mackerras3b6f9e52009-01-14 21:00:30 +110083
Paul Mackerras0475f9e2009-02-11 14:35:35 +110084 __reserved_1 : 23;
Ingo Molnar9f66a382008-12-10 12:33:23 +010085
Paul Mackerrasf3dfd262009-02-26 22:43:46 +110086 __u64 __reserved_2;
Thomas Gleixnereab656a2008-12-08 19:26:59 +010087};
88
Ingo Molnar9f66a382008-12-10 12:33:23 +010089/*
Paul Mackerrasd859e292009-01-17 18:10:22 +110090 * Ioctls that can be done on a perf counter fd:
91 */
92#define PERF_COUNTER_IOC_ENABLE _IO('$', 0)
93#define PERF_COUNTER_IOC_DISABLE _IO('$', 1)
94
Paul Mackerrasf3dfd262009-02-26 22:43:46 +110095#ifdef __KERNEL__
Paul Mackerrasd859e292009-01-17 18:10:22 +110096/*
Paul Mackerrasf3dfd262009-02-26 22:43:46 +110097 * Kernel-internal data types and definitions:
Ingo Molnar9f66a382008-12-10 12:33:23 +010098 */
99
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100100#ifdef CONFIG_PERF_COUNTERS
101# include <asm/perf_counter.h>
102#endif
103
104#include <linux/list.h>
105#include <linux/mutex.h>
106#include <linux/rculist.h>
107#include <linux/rcupdate.h>
108#include <linux/spinlock.h>
109#include <asm/atomic.h>
110
111struct task_struct;
112
Thomas Gleixner0793a612008-12-04 20:12:29 +0100113/**
Ingo Molnar9f66a382008-12-10 12:33:23 +0100114 * struct hw_perf_counter - performance counter hardware details:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100115 */
116struct hw_perf_counter {
Ingo Molnaree060942008-12-13 09:00:03 +0100117#ifdef CONFIG_PERF_COUNTERS
Ingo Molnar9f66a382008-12-10 12:33:23 +0100118 u64 config;
119 unsigned long config_base;
120 unsigned long counter_base;
121 int nmi;
122 unsigned int idx;
Ingo Molnaree060942008-12-13 09:00:03 +0100123 atomic64_t prev_count;
Ingo Molnar9f66a382008-12-10 12:33:23 +0100124 u64 irq_period;
Ingo Molnaree060942008-12-13 09:00:03 +0100125 atomic64_t period_left;
126#endif
Thomas Gleixner0793a612008-12-04 20:12:29 +0100127};
128
129/*
130 * Hardcoded buffer length limit for now, for IRQ-fed events:
131 */
Ingo Molnar9f66a382008-12-10 12:33:23 +0100132#define PERF_DATA_BUFLEN 2048
Thomas Gleixner0793a612008-12-04 20:12:29 +0100133
134/**
135 * struct perf_data - performance counter IRQ data sampling ...
136 */
137struct perf_data {
Ingo Molnar9f66a382008-12-10 12:33:23 +0100138 int len;
139 int rd_idx;
140 int overrun;
141 u8 data[PERF_DATA_BUFLEN];
Thomas Gleixner0793a612008-12-04 20:12:29 +0100142};
143
Ingo Molnar621a01e2008-12-11 12:46:46 +0100144struct perf_counter;
145
146/**
147 * struct hw_perf_counter_ops - performance counter hw ops
148 */
149struct hw_perf_counter_ops {
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100150 int (*enable) (struct perf_counter *counter);
Ingo Molnar76715812008-12-17 14:20:28 +0100151 void (*disable) (struct perf_counter *counter);
152 void (*read) (struct perf_counter *counter);
Ingo Molnar621a01e2008-12-11 12:46:46 +0100153};
154
Thomas Gleixner0793a612008-12-04 20:12:29 +0100155/**
Ingo Molnar6a930702008-12-11 15:17:03 +0100156 * enum perf_counter_active_state - the states of a counter
157 */
158enum perf_counter_active_state {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100159 PERF_COUNTER_STATE_ERROR = -2,
Ingo Molnar6a930702008-12-11 15:17:03 +0100160 PERF_COUNTER_STATE_OFF = -1,
161 PERF_COUNTER_STATE_INACTIVE = 0,
162 PERF_COUNTER_STATE_ACTIVE = 1,
163};
164
Ingo Molnar9b51f662008-12-12 13:49:45 +0100165struct file;
166
Ingo Molnar6a930702008-12-11 15:17:03 +0100167/**
Thomas Gleixner0793a612008-12-04 20:12:29 +0100168 * struct perf_counter - performance counter kernel representation:
169 */
170struct perf_counter {
Ingo Molnaree060942008-12-13 09:00:03 +0100171#ifdef CONFIG_PERF_COUNTERS
Ingo Molnar04289bb2008-12-11 08:38:42 +0100172 struct list_head list_entry;
173 struct list_head sibling_list;
174 struct perf_counter *group_leader;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100175 const struct hw_perf_counter_ops *hw_ops;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100176
Ingo Molnar6a930702008-12-11 15:17:03 +0100177 enum perf_counter_active_state state;
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100178 enum perf_counter_active_state prev_state;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100179 atomic64_t count;
Ingo Molnaree060942008-12-13 09:00:03 +0100180
Ingo Molnar9f66a382008-12-10 12:33:23 +0100181 struct perf_counter_hw_event hw_event;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100182 struct hw_perf_counter hw;
183
184 struct perf_counter_context *ctx;
185 struct task_struct *task;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100186 struct file *filp;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100187
Ingo Molnar9b51f662008-12-12 13:49:45 +0100188 struct perf_counter *parent;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100189 struct list_head child_list;
190
Thomas Gleixner0793a612008-12-04 20:12:29 +0100191 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100192 * Protect attach/detach and child_list:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100193 */
194 struct mutex mutex;
195
196 int oncpu;
197 int cpu;
198
Thomas Gleixner0793a612008-12-04 20:12:29 +0100199 /* read() / irq related data */
200 wait_queue_head_t waitq;
201 /* optional: for NMIs */
202 int wakeup_pending;
203 struct perf_data *irqdata;
204 struct perf_data *usrdata;
205 struct perf_data data[2];
Ingo Molnaree060942008-12-13 09:00:03 +0100206#endif
Thomas Gleixner0793a612008-12-04 20:12:29 +0100207};
208
209/**
210 * struct perf_counter_context - counter context structure
211 *
212 * Used as a container for task counters and CPU counters as well:
213 */
214struct perf_counter_context {
215#ifdef CONFIG_PERF_COUNTERS
216 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100217 * Protect the states of the counters in the list,
218 * nr_active, and the list:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100219 */
220 spinlock_t lock;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100221 /*
222 * Protect the list of counters. Locking either mutex or lock
223 * is sufficient to ensure the list doesn't change; to change
224 * the list you need to lock both the mutex and the spinlock.
225 */
226 struct mutex mutex;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100227
228 struct list_head counter_list;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100229 int nr_counters;
230 int nr_active;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100231 int is_active;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100232 struct task_struct *task;
233#endif
234};
235
236/**
237 * struct perf_counter_cpu_context - per cpu counter context structure
238 */
239struct perf_cpu_context {
240 struct perf_counter_context ctx;
241 struct perf_counter_context *task_ctx;
242 int active_oncpu;
243 int max_pertask;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100244 int exclusive;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100245};
246
247/*
248 * Set by architecture code:
249 */
250extern int perf_max_counters;
251
252#ifdef CONFIG_PERF_COUNTERS
Ingo Molnar5c92d122008-12-11 13:21:10 +0100253extern const struct hw_perf_counter_ops *
Ingo Molnar621a01e2008-12-11 12:46:46 +0100254hw_perf_counter_init(struct perf_counter *counter);
255
Thomas Gleixner0793a612008-12-04 20:12:29 +0100256extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
257extern void perf_counter_task_sched_out(struct task_struct *task, int cpu);
258extern void perf_counter_task_tick(struct task_struct *task, int cpu);
Ingo Molnar9b51f662008-12-12 13:49:45 +0100259extern void perf_counter_init_task(struct task_struct *child);
260extern void perf_counter_exit_task(struct task_struct *child);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100261extern void perf_counter_notify(struct pt_regs *regs);
262extern void perf_counter_print_debug(void);
Mike Galbraith1b023a92009-01-23 10:13:01 +0100263extern void perf_counter_unthrottle(void);
Ingo Molnar01b28382008-12-11 13:45:51 +0100264extern u64 hw_perf_save_disable(void);
265extern void hw_perf_restore(u64 ctrl);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100266extern int perf_counter_task_disable(void);
267extern int perf_counter_task_enable(void);
Paul Mackerras3cbed422009-01-09 16:43:42 +1100268extern int hw_perf_group_sched_in(struct perf_counter *group_leader,
269 struct perf_cpu_context *cpuctx,
270 struct perf_counter_context *ctx, int cpu);
Ingo Molnar5c92d122008-12-11 13:21:10 +0100271
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100272/*
273 * Return 1 for a software counter, 0 for a hardware counter
274 */
275static inline int is_software_counter(struct perf_counter *counter)
276{
277 return !counter->hw_event.raw && counter->hw_event.type < 0;
278}
279
Thomas Gleixner0793a612008-12-04 20:12:29 +0100280#else
281static inline void
282perf_counter_task_sched_in(struct task_struct *task, int cpu) { }
283static inline void
284perf_counter_task_sched_out(struct task_struct *task, int cpu) { }
285static inline void
286perf_counter_task_tick(struct task_struct *task, int cpu) { }
Ingo Molnar9b51f662008-12-12 13:49:45 +0100287static inline void perf_counter_init_task(struct task_struct *child) { }
288static inline void perf_counter_exit_task(struct task_struct *child) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100289static inline void perf_counter_notify(struct pt_regs *regs) { }
290static inline void perf_counter_print_debug(void) { }
Mike Galbraith1b023a92009-01-23 10:13:01 +0100291static inline void perf_counter_unthrottle(void) { }
Ingo Molnar01b28382008-12-11 13:45:51 +0100292static inline void hw_perf_restore(u64 ctrl) { }
293static inline u64 hw_perf_save_disable(void) { return 0; }
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100294static inline int perf_counter_task_disable(void) { return -EINVAL; }
295static inline int perf_counter_task_enable(void) { return -EINVAL; }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100296#endif
297
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100298#endif /* __KERNEL__ */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100299#endif /* _LINUX_PERF_COUNTER_H */