blob: 91f1ca4c01c08e85f4494de9f0a300f11cbb14e1 [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
16#include <asm/atomic.h>
Paul Mackerrasd859e292009-01-17 18:10:22 +110017#include <asm/ioctl.h>
Ingo Molnare44aef52008-12-25 09:02:11 +010018
19#ifdef CONFIG_PERF_COUNTERS
20# include <asm/perf_counter.h>
21#endif
Thomas Gleixner0793a612008-12-04 20:12:29 +010022
23#include <linux/list.h>
24#include <linux/mutex.h>
25#include <linux/rculist.h>
26#include <linux/rcupdate.h>
27#include <linux/spinlock.h>
28
29struct task_struct;
30
31/*
Ingo Molnar9f66a382008-12-10 12:33:23 +010032 * User-space ABI bits:
33 */
34
35/*
36 * Generalized performance counter event types, used by the hw_event.type
37 * parameter of the sys_perf_counter_open() syscall:
Thomas Gleixner0793a612008-12-04 20:12:29 +010038 */
39enum hw_event_types {
Thomas Gleixner0793a612008-12-04 20:12:29 +010040 /*
Ingo Molnar9f66a382008-12-10 12:33:23 +010041 * Common hardware events, generalized by the kernel:
Thomas Gleixner0793a612008-12-04 20:12:29 +010042 */
Ingo Molnarf650a672008-12-23 12:17:29 +010043 PERF_COUNT_CPU_CYCLES = 0,
Ingo Molnar9f66a382008-12-10 12:33:23 +010044 PERF_COUNT_INSTRUCTIONS = 1,
45 PERF_COUNT_CACHE_REFERENCES = 2,
46 PERF_COUNT_CACHE_MISSES = 3,
47 PERF_COUNT_BRANCH_INSTRUCTIONS = 4,
48 PERF_COUNT_BRANCH_MISSES = 5,
Ingo Molnarf650a672008-12-23 12:17:29 +010049 PERF_COUNT_BUS_CYCLES = 6,
Ingo Molnar9f66a382008-12-10 12:33:23 +010050
Ingo Molnarf650a672008-12-23 12:17:29 +010051 PERF_HW_EVENTS_MAX = 7,
Ingo Molnar6c594c22008-12-14 12:34:15 +010052
Ingo Molnar9f66a382008-12-10 12:33:23 +010053 /*
54 * Special "software" counters provided by the kernel, even if
55 * the hardware does not support performance counters. These
56 * counters measure various physical and sw events of the
57 * kernel (and allow the profiling of them as well):
58 */
59 PERF_COUNT_CPU_CLOCK = -1,
60 PERF_COUNT_TASK_CLOCK = -2,
Ingo Molnar5d6a27d2008-12-14 12:28:33 +010061 PERF_COUNT_PAGE_FAULTS = -3,
62 PERF_COUNT_CONTEXT_SWITCHES = -4,
Ingo Molnar6c594c22008-12-14 12:34:15 +010063 PERF_COUNT_CPU_MIGRATIONS = -5,
64
65 PERF_SW_EVENTS_MIN = -6,
Thomas Gleixner0793a612008-12-04 20:12:29 +010066};
67
68/*
69 * IRQ-notification data record type:
70 */
Ingo Molnar9f66a382008-12-10 12:33:23 +010071enum perf_counter_record_type {
72 PERF_RECORD_SIMPLE = 0,
73 PERF_RECORD_IRQ = 1,
74 PERF_RECORD_GROUP = 2,
Thomas Gleixner0793a612008-12-04 20:12:29 +010075};
76
Ingo Molnar9f66a382008-12-10 12:33:23 +010077/*
78 * Hardware event to monitor via a performance monitoring counter:
79 */
80struct perf_counter_hw_event {
Ingo Molnar01b28382008-12-11 13:45:51 +010081 s64 type;
Ingo Molnar9f66a382008-12-10 12:33:23 +010082
83 u64 irq_period;
84 u32 record_type;
85
Ingo Molnar9b51f662008-12-12 13:49:45 +010086 u32 disabled : 1, /* off by default */
87 nmi : 1, /* NMI sampling */
88 raw : 1, /* raw event type */
89 inherit : 1, /* children inherit it */
Paul Mackerras3b6f9e52009-01-14 21:00:30 +110090 pinned : 1, /* must always be on PMU */
91 exclusive : 1, /* only counter on PMU */
92
93 __reserved_1 : 26;
Ingo Molnar9f66a382008-12-10 12:33:23 +010094
95 u64 __reserved_2;
Thomas Gleixnereab656a2008-12-08 19:26:59 +010096};
97
Ingo Molnar9f66a382008-12-10 12:33:23 +010098/*
Paul Mackerrasd859e292009-01-17 18:10:22 +110099 * Ioctls that can be done on a perf counter fd:
100 */
101#define PERF_COUNTER_IOC_ENABLE _IO('$', 0)
102#define PERF_COUNTER_IOC_DISABLE _IO('$', 1)
103
104/*
Ingo Molnar9f66a382008-12-10 12:33:23 +0100105 * Kernel-internal data types:
106 */
107
Thomas Gleixner0793a612008-12-04 20:12:29 +0100108/**
Ingo Molnar9f66a382008-12-10 12:33:23 +0100109 * struct hw_perf_counter - performance counter hardware details:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100110 */
111struct hw_perf_counter {
Ingo Molnaree060942008-12-13 09:00:03 +0100112#ifdef CONFIG_PERF_COUNTERS
Ingo Molnar9f66a382008-12-10 12:33:23 +0100113 u64 config;
114 unsigned long config_base;
115 unsigned long counter_base;
116 int nmi;
117 unsigned int idx;
Ingo Molnaree060942008-12-13 09:00:03 +0100118 atomic64_t prev_count;
Ingo Molnar9f66a382008-12-10 12:33:23 +0100119 u64 irq_period;
Ingo Molnaree060942008-12-13 09:00:03 +0100120 atomic64_t period_left;
121#endif
Thomas Gleixner0793a612008-12-04 20:12:29 +0100122};
123
124/*
125 * Hardcoded buffer length limit for now, for IRQ-fed events:
126 */
Ingo Molnar9f66a382008-12-10 12:33:23 +0100127#define PERF_DATA_BUFLEN 2048
Thomas Gleixner0793a612008-12-04 20:12:29 +0100128
129/**
130 * struct perf_data - performance counter IRQ data sampling ...
131 */
132struct perf_data {
Ingo Molnar9f66a382008-12-10 12:33:23 +0100133 int len;
134 int rd_idx;
135 int overrun;
136 u8 data[PERF_DATA_BUFLEN];
Thomas Gleixner0793a612008-12-04 20:12:29 +0100137};
138
Ingo Molnar621a01e2008-12-11 12:46:46 +0100139struct perf_counter;
140
141/**
142 * struct hw_perf_counter_ops - performance counter hw ops
143 */
144struct hw_perf_counter_ops {
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100145 int (*enable) (struct perf_counter *counter);
Ingo Molnar76715812008-12-17 14:20:28 +0100146 void (*disable) (struct perf_counter *counter);
147 void (*read) (struct perf_counter *counter);
Ingo Molnar621a01e2008-12-11 12:46:46 +0100148};
149
Thomas Gleixner0793a612008-12-04 20:12:29 +0100150/**
Ingo Molnar6a930702008-12-11 15:17:03 +0100151 * enum perf_counter_active_state - the states of a counter
152 */
153enum perf_counter_active_state {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100154 PERF_COUNTER_STATE_ERROR = -2,
Ingo Molnar6a930702008-12-11 15:17:03 +0100155 PERF_COUNTER_STATE_OFF = -1,
156 PERF_COUNTER_STATE_INACTIVE = 0,
157 PERF_COUNTER_STATE_ACTIVE = 1,
158};
159
Ingo Molnar9b51f662008-12-12 13:49:45 +0100160struct file;
161
Ingo Molnar6a930702008-12-11 15:17:03 +0100162/**
Thomas Gleixner0793a612008-12-04 20:12:29 +0100163 * struct perf_counter - performance counter kernel representation:
164 */
165struct perf_counter {
Ingo Molnaree060942008-12-13 09:00:03 +0100166#ifdef CONFIG_PERF_COUNTERS
Ingo Molnar04289bb2008-12-11 08:38:42 +0100167 struct list_head list_entry;
168 struct list_head sibling_list;
169 struct perf_counter *group_leader;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100170 const struct hw_perf_counter_ops *hw_ops;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100171
Ingo Molnar6a930702008-12-11 15:17:03 +0100172 enum perf_counter_active_state state;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100173 atomic64_t count;
Ingo Molnaree060942008-12-13 09:00:03 +0100174
Ingo Molnar9f66a382008-12-10 12:33:23 +0100175 struct perf_counter_hw_event hw_event;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100176 struct hw_perf_counter hw;
177
178 struct perf_counter_context *ctx;
179 struct task_struct *task;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100180 struct file *filp;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100181
Ingo Molnar9b51f662008-12-12 13:49:45 +0100182 struct perf_counter *parent;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100183 struct list_head child_list;
184
Thomas Gleixner0793a612008-12-04 20:12:29 +0100185 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100186 * Protect attach/detach and child_list:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100187 */
188 struct mutex mutex;
189
190 int oncpu;
191 int cpu;
192
Thomas Gleixner0793a612008-12-04 20:12:29 +0100193 /* read() / irq related data */
194 wait_queue_head_t waitq;
195 /* optional: for NMIs */
196 int wakeup_pending;
197 struct perf_data *irqdata;
198 struct perf_data *usrdata;
199 struct perf_data data[2];
Ingo Molnaree060942008-12-13 09:00:03 +0100200#endif
Thomas Gleixner0793a612008-12-04 20:12:29 +0100201};
202
203/**
204 * struct perf_counter_context - counter context structure
205 *
206 * Used as a container for task counters and CPU counters as well:
207 */
208struct perf_counter_context {
209#ifdef CONFIG_PERF_COUNTERS
210 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100211 * Protect the states of the counters in the list,
212 * nr_active, and the list:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100213 */
214 spinlock_t lock;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100215 /*
216 * Protect the list of counters. Locking either mutex or lock
217 * is sufficient to ensure the list doesn't change; to change
218 * the list you need to lock both the mutex and the spinlock.
219 */
220 struct mutex mutex;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100221
222 struct list_head counter_list;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100223 int nr_counters;
224 int nr_active;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100225 int is_active;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100226 struct task_struct *task;
227#endif
228};
229
230/**
231 * struct perf_counter_cpu_context - per cpu counter context structure
232 */
233struct perf_cpu_context {
234 struct perf_counter_context ctx;
235 struct perf_counter_context *task_ctx;
236 int active_oncpu;
237 int max_pertask;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100238 int exclusive;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100239};
240
241/*
242 * Set by architecture code:
243 */
244extern int perf_max_counters;
245
246#ifdef CONFIG_PERF_COUNTERS
Ingo Molnar5c92d122008-12-11 13:21:10 +0100247extern const struct hw_perf_counter_ops *
Ingo Molnar621a01e2008-12-11 12:46:46 +0100248hw_perf_counter_init(struct perf_counter *counter);
249
Thomas Gleixner0793a612008-12-04 20:12:29 +0100250extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
251extern void perf_counter_task_sched_out(struct task_struct *task, int cpu);
252extern void perf_counter_task_tick(struct task_struct *task, int cpu);
Ingo Molnar9b51f662008-12-12 13:49:45 +0100253extern void perf_counter_init_task(struct task_struct *child);
254extern void perf_counter_exit_task(struct task_struct *child);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100255extern void perf_counter_notify(struct pt_regs *regs);
256extern void perf_counter_print_debug(void);
Mike Galbraith1b023a92009-01-23 10:13:01 +0100257extern void perf_counter_unthrottle(void);
Ingo Molnar01b28382008-12-11 13:45:51 +0100258extern u64 hw_perf_save_disable(void);
259extern void hw_perf_restore(u64 ctrl);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100260extern int perf_counter_task_disable(void);
261extern int perf_counter_task_enable(void);
Paul Mackerras3cbed422009-01-09 16:43:42 +1100262extern int hw_perf_group_sched_in(struct perf_counter *group_leader,
263 struct perf_cpu_context *cpuctx,
264 struct perf_counter_context *ctx, int cpu);
Ingo Molnar5c92d122008-12-11 13:21:10 +0100265
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100266/*
267 * Return 1 for a software counter, 0 for a hardware counter
268 */
269static inline int is_software_counter(struct perf_counter *counter)
270{
271 return !counter->hw_event.raw && counter->hw_event.type < 0;
272}
273
Mike Galbraith1b023a92009-01-23 10:13:01 +0100274#define PERFMON_MIN_PERIOD_NS 10000
275
Thomas Gleixner0793a612008-12-04 20:12:29 +0100276#else
277static inline void
278perf_counter_task_sched_in(struct task_struct *task, int cpu) { }
279static inline void
280perf_counter_task_sched_out(struct task_struct *task, int cpu) { }
281static inline void
282perf_counter_task_tick(struct task_struct *task, int cpu) { }
Ingo Molnar9b51f662008-12-12 13:49:45 +0100283static inline void perf_counter_init_task(struct task_struct *child) { }
284static inline void perf_counter_exit_task(struct task_struct *child) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100285static inline void perf_counter_notify(struct pt_regs *regs) { }
286static inline void perf_counter_print_debug(void) { }
Mike Galbraith1b023a92009-01-23 10:13:01 +0100287static inline void perf_counter_unthrottle(void) { }
Ingo Molnar01b28382008-12-11 13:45:51 +0100288static inline void hw_perf_restore(u64 ctrl) { }
289static inline u64 hw_perf_save_disable(void) { return 0; }
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100290static inline int perf_counter_task_disable(void) { return -EINVAL; }
291static inline int perf_counter_task_enable(void) { return -EINVAL; }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100292#endif
293
294#endif /* _LINUX_PERF_COUNTER_H */