blob: 065984c1ff579b8799c6eb3d68b7d4b3cc7d3575 [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,
Peter Zijlstraac17dc82009-03-13 12:21:34 +010052 PERF_COUNT_PAGE_FAULTS_MIN = -6,
53 PERF_COUNT_PAGE_FAULTS_MAJ = -7,
Ingo Molnar6c594c22008-12-14 12:34:15 +010054
Peter Zijlstraac17dc82009-03-13 12:21:34 +010055 PERF_SW_EVENTS_MIN = -8,
Peter Zijlstrae077df42009-03-19 20:26:17 +010056
57 PERF_TP_EVENTS_MIN = -65536
Thomas Gleixner0793a612008-12-04 20:12:29 +010058};
59
60/*
61 * IRQ-notification data record type:
62 */
Ingo Molnar9f66a382008-12-10 12:33:23 +010063enum perf_counter_record_type {
64 PERF_RECORD_SIMPLE = 0,
65 PERF_RECORD_IRQ = 1,
66 PERF_RECORD_GROUP = 2,
Thomas Gleixner0793a612008-12-04 20:12:29 +010067};
68
Ingo Molnar9f66a382008-12-10 12:33:23 +010069/*
70 * Hardware event to monitor via a performance monitoring counter:
71 */
72struct perf_counter_hw_event {
Paul Mackerrasf3dfd262009-02-26 22:43:46 +110073 __s64 type;
Ingo Molnar9f66a382008-12-10 12:33:23 +010074
Paul Mackerrasf3dfd262009-02-26 22:43:46 +110075 __u64 irq_period;
Paul Mackerras2743a5b2009-03-04 20:36:51 +110076 __u64 record_type;
77 __u64 read_format;
Ingo Molnar9f66a382008-12-10 12:33:23 +010078
Paul Mackerras2743a5b2009-03-04 20:36:51 +110079 __u64 disabled : 1, /* off by default */
Paul Mackerras0475f9e2009-02-11 14:35:35 +110080 nmi : 1, /* NMI sampling */
81 raw : 1, /* raw event type */
82 inherit : 1, /* children inherit it */
83 pinned : 1, /* must always be on PMU */
84 exclusive : 1, /* only group on PMU */
85 exclude_user : 1, /* don't count user */
86 exclude_kernel : 1, /* ditto kernel */
87 exclude_hv : 1, /* ditto hypervisor */
Paul Mackerras2743a5b2009-03-04 20:36:51 +110088 exclude_idle : 1, /* don't count when idle */
Paul Mackerras3b6f9e52009-01-14 21:00:30 +110089
Ingo Molnar2485e512009-03-05 12:33:16 +010090 __reserved_1 : 54;
Paul Mackerras2743a5b2009-03-04 20:36:51 +110091
92 __u32 extra_config_len;
93 __u32 __reserved_4;
Ingo Molnar9f66a382008-12-10 12:33:23 +010094
Paul Mackerrasf3dfd262009-02-26 22:43:46 +110095 __u64 __reserved_2;
Paul Mackerras2743a5b2009-03-04 20:36:51 +110096 __u64 __reserved_3;
Thomas Gleixnereab656a2008-12-08 19:26:59 +010097};
98
Ingo Molnar9f66a382008-12-10 12:33:23 +010099/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100100 * Ioctls that can be done on a perf counter fd:
101 */
102#define PERF_COUNTER_IOC_ENABLE _IO('$', 0)
103#define PERF_COUNTER_IOC_DISABLE _IO('$', 1)
104
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100105#ifdef __KERNEL__
Paul Mackerrasd859e292009-01-17 18:10:22 +1100106/*
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100107 * Kernel-internal data types and definitions:
Ingo Molnar9f66a382008-12-10 12:33:23 +0100108 */
109
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100110#ifdef CONFIG_PERF_COUNTERS
111# include <asm/perf_counter.h>
112#endif
113
114#include <linux/list.h>
115#include <linux/mutex.h>
116#include <linux/rculist.h>
117#include <linux/rcupdate.h>
118#include <linux/spinlock.h>
Peter Zijlstrad6d020e2009-03-13 12:21:35 +0100119#include <linux/hrtimer.h>
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100120#include <asm/atomic.h>
121
122struct task_struct;
123
Thomas Gleixner0793a612008-12-04 20:12:29 +0100124/**
Ingo Molnar9f66a382008-12-10 12:33:23 +0100125 * struct hw_perf_counter - performance counter hardware details:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100126 */
127struct hw_perf_counter {
Ingo Molnaree060942008-12-13 09:00:03 +0100128#ifdef CONFIG_PERF_COUNTERS
Peter Zijlstrad6d020e2009-03-13 12:21:35 +0100129 union {
130 struct { /* hardware */
131 u64 config;
132 unsigned long config_base;
133 unsigned long counter_base;
134 int nmi;
135 unsigned int idx;
136 };
137 union { /* software */
138 atomic64_t count;
139 struct hrtimer hrtimer;
140 };
141 };
Ingo Molnaree060942008-12-13 09:00:03 +0100142 atomic64_t prev_count;
Ingo Molnar9f66a382008-12-10 12:33:23 +0100143 u64 irq_period;
Ingo Molnaree060942008-12-13 09:00:03 +0100144 atomic64_t period_left;
145#endif
Thomas Gleixner0793a612008-12-04 20:12:29 +0100146};
147
148/*
149 * Hardcoded buffer length limit for now, for IRQ-fed events:
150 */
Ingo Molnar9f66a382008-12-10 12:33:23 +0100151#define PERF_DATA_BUFLEN 2048
Thomas Gleixner0793a612008-12-04 20:12:29 +0100152
153/**
154 * struct perf_data - performance counter IRQ data sampling ...
155 */
156struct perf_data {
Ingo Molnar9f66a382008-12-10 12:33:23 +0100157 int len;
158 int rd_idx;
159 int overrun;
160 u8 data[PERF_DATA_BUFLEN];
Thomas Gleixner0793a612008-12-04 20:12:29 +0100161};
162
Ingo Molnar621a01e2008-12-11 12:46:46 +0100163struct perf_counter;
164
165/**
166 * struct hw_perf_counter_ops - performance counter hw ops
167 */
168struct hw_perf_counter_ops {
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100169 int (*enable) (struct perf_counter *counter);
Ingo Molnar76715812008-12-17 14:20:28 +0100170 void (*disable) (struct perf_counter *counter);
171 void (*read) (struct perf_counter *counter);
Ingo Molnar621a01e2008-12-11 12:46:46 +0100172};
173
Thomas Gleixner0793a612008-12-04 20:12:29 +0100174/**
Ingo Molnar6a930702008-12-11 15:17:03 +0100175 * enum perf_counter_active_state - the states of a counter
176 */
177enum perf_counter_active_state {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100178 PERF_COUNTER_STATE_ERROR = -2,
Ingo Molnar6a930702008-12-11 15:17:03 +0100179 PERF_COUNTER_STATE_OFF = -1,
180 PERF_COUNTER_STATE_INACTIVE = 0,
181 PERF_COUNTER_STATE_ACTIVE = 1,
182};
183
Ingo Molnar9b51f662008-12-12 13:49:45 +0100184struct file;
185
Ingo Molnar6a930702008-12-11 15:17:03 +0100186/**
Thomas Gleixner0793a612008-12-04 20:12:29 +0100187 * struct perf_counter - performance counter kernel representation:
188 */
189struct perf_counter {
Ingo Molnaree060942008-12-13 09:00:03 +0100190#ifdef CONFIG_PERF_COUNTERS
Ingo Molnar04289bb2008-12-11 08:38:42 +0100191 struct list_head list_entry;
Peter Zijlstra592903c2009-03-13 12:21:36 +0100192 struct list_head event_entry;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100193 struct list_head sibling_list;
194 struct perf_counter *group_leader;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100195 const struct hw_perf_counter_ops *hw_ops;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100196
Ingo Molnar6a930702008-12-11 15:17:03 +0100197 enum perf_counter_active_state state;
Paul Mackerrasc07c99b2009-02-13 22:10:34 +1100198 enum perf_counter_active_state prev_state;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100199 atomic64_t count;
Ingo Molnaree060942008-12-13 09:00:03 +0100200
Ingo Molnar9f66a382008-12-10 12:33:23 +0100201 struct perf_counter_hw_event hw_event;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100202 struct hw_perf_counter hw;
203
204 struct perf_counter_context *ctx;
205 struct task_struct *task;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100206 struct file *filp;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100207
Ingo Molnar9b51f662008-12-12 13:49:45 +0100208 struct perf_counter *parent;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100209 struct list_head child_list;
210
Thomas Gleixner0793a612008-12-04 20:12:29 +0100211 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100212 * Protect attach/detach and child_list:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100213 */
214 struct mutex mutex;
215
216 int oncpu;
217 int cpu;
218
Thomas Gleixner0793a612008-12-04 20:12:29 +0100219 /* read() / irq related data */
220 wait_queue_head_t waitq;
221 /* optional: for NMIs */
222 int wakeup_pending;
223 struct perf_data *irqdata;
224 struct perf_data *usrdata;
225 struct perf_data data[2];
Peter Zijlstra592903c2009-03-13 12:21:36 +0100226
Peter Zijlstrae077df42009-03-19 20:26:17 +0100227 void (*destroy)(struct perf_counter *);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100228 struct rcu_head rcu_head;
Ingo Molnaree060942008-12-13 09:00:03 +0100229#endif
Thomas Gleixner0793a612008-12-04 20:12:29 +0100230};
231
232/**
233 * struct perf_counter_context - counter context structure
234 *
235 * Used as a container for task counters and CPU counters as well:
236 */
237struct perf_counter_context {
238#ifdef CONFIG_PERF_COUNTERS
239 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100240 * Protect the states of the counters in the list,
241 * nr_active, and the list:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100242 */
243 spinlock_t lock;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100244 /*
245 * Protect the list of counters. Locking either mutex or lock
246 * is sufficient to ensure the list doesn't change; to change
247 * the list you need to lock both the mutex and the spinlock.
248 */
249 struct mutex mutex;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100250
251 struct list_head counter_list;
Peter Zijlstra592903c2009-03-13 12:21:36 +0100252 struct list_head event_list;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100253 int nr_counters;
254 int nr_active;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100255 int is_active;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100256 struct task_struct *task;
257#endif
258};
259
260/**
261 * struct perf_counter_cpu_context - per cpu counter context structure
262 */
263struct perf_cpu_context {
264 struct perf_counter_context ctx;
265 struct perf_counter_context *task_ctx;
266 int active_oncpu;
267 int max_pertask;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100268 int exclusive;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100269};
270
271/*
272 * Set by architecture code:
273 */
274extern int perf_max_counters;
275
276#ifdef CONFIG_PERF_COUNTERS
Ingo Molnar5c92d122008-12-11 13:21:10 +0100277extern const struct hw_perf_counter_ops *
Ingo Molnar621a01e2008-12-11 12:46:46 +0100278hw_perf_counter_init(struct perf_counter *counter);
279
Thomas Gleixner0793a612008-12-04 20:12:29 +0100280extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
281extern void perf_counter_task_sched_out(struct task_struct *task, int cpu);
282extern void perf_counter_task_tick(struct task_struct *task, int cpu);
Ingo Molnar9b51f662008-12-12 13:49:45 +0100283extern void perf_counter_init_task(struct task_struct *child);
284extern void perf_counter_exit_task(struct task_struct *child);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100285extern void perf_counter_notify(struct pt_regs *regs);
286extern void perf_counter_print_debug(void);
Mike Galbraith1b023a92009-01-23 10:13:01 +0100287extern void perf_counter_unthrottle(void);
Ingo Molnar01b28382008-12-11 13:45:51 +0100288extern u64 hw_perf_save_disable(void);
289extern void hw_perf_restore(u64 ctrl);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100290extern int perf_counter_task_disable(void);
291extern int perf_counter_task_enable(void);
Paul Mackerras3cbed422009-01-09 16:43:42 +1100292extern int hw_perf_group_sched_in(struct perf_counter *group_leader,
293 struct perf_cpu_context *cpuctx,
294 struct perf_counter_context *ctx, int cpu);
Ingo Molnar5c92d122008-12-11 13:21:10 +0100295
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100296/*
297 * Return 1 for a software counter, 0 for a hardware counter
298 */
299static inline int is_software_counter(struct perf_counter *counter)
300{
301 return !counter->hw_event.raw && counter->hw_event.type < 0;
302}
303
Peter Zijlstra15dbf272009-03-13 12:21:32 +0100304extern void perf_swcounter_event(enum hw_event_types, u64, int, struct pt_regs *);
305
Thomas Gleixner0793a612008-12-04 20:12:29 +0100306#else
307static inline void
308perf_counter_task_sched_in(struct task_struct *task, int cpu) { }
309static inline void
310perf_counter_task_sched_out(struct task_struct *task, int cpu) { }
311static inline void
312perf_counter_task_tick(struct task_struct *task, int cpu) { }
Ingo Molnar9b51f662008-12-12 13:49:45 +0100313static inline void perf_counter_init_task(struct task_struct *child) { }
314static inline void perf_counter_exit_task(struct task_struct *child) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100315static inline void perf_counter_notify(struct pt_regs *regs) { }
316static inline void perf_counter_print_debug(void) { }
Mike Galbraith1b023a92009-01-23 10:13:01 +0100317static inline void perf_counter_unthrottle(void) { }
Peter Zijlstra15dbf272009-03-13 12:21:32 +0100318static inline void hw_perf_restore(u64 ctrl) { }
Ingo Molnar01b28382008-12-11 13:45:51 +0100319static inline u64 hw_perf_save_disable(void) { return 0; }
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100320static inline int perf_counter_task_disable(void) { return -EINVAL; }
321static inline int perf_counter_task_enable(void) { return -EINVAL; }
Peter Zijlstra15dbf272009-03-13 12:21:32 +0100322
323static inline void perf_swcounter_event(enum hw_event_types event, u64 nr,
324 int nmi, struct pt_regs *regs) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100325#endif
326
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100327#endif /* __KERNEL__ */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100328#endif /* _LINUX_PERF_COUNTER_H */