blob: e5d25bf8f74e869f01ce08eaefe62545275c1e21 [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>
17
18#include <linux/list.h>
19#include <linux/mutex.h>
20#include <linux/rculist.h>
21#include <linux/rcupdate.h>
22#include <linux/spinlock.h>
23
24struct task_struct;
25
26/*
Ingo Molnar9f66a382008-12-10 12:33:23 +010027 * User-space ABI bits:
28 */
29
30/*
31 * Generalized performance counter event types, used by the hw_event.type
32 * parameter of the sys_perf_counter_open() syscall:
Thomas Gleixner0793a612008-12-04 20:12:29 +010033 */
34enum hw_event_types {
Thomas Gleixner0793a612008-12-04 20:12:29 +010035 /*
Ingo Molnar9f66a382008-12-10 12:33:23 +010036 * Common hardware events, generalized by the kernel:
Thomas Gleixner0793a612008-12-04 20:12:29 +010037 */
Ingo Molnar9f66a382008-12-10 12:33:23 +010038 PERF_COUNT_CYCLES = 0,
39 PERF_COUNT_INSTRUCTIONS = 1,
40 PERF_COUNT_CACHE_REFERENCES = 2,
41 PERF_COUNT_CACHE_MISSES = 3,
42 PERF_COUNT_BRANCH_INSTRUCTIONS = 4,
43 PERF_COUNT_BRANCH_MISSES = 5,
44
45 /*
46 * Special "software" counters provided by the kernel, even if
47 * the hardware does not support performance counters. These
48 * counters measure various physical and sw events of the
49 * kernel (and allow the profiling of them as well):
50 */
51 PERF_COUNT_CPU_CLOCK = -1,
52 PERF_COUNT_TASK_CLOCK = -2,
Ingo Molnarbae43c92008-12-11 14:03:20 +010053 /*
54 * Future software events:
55 */
56 /* PERF_COUNT_PAGE_FAULTS = -3,
57 PERF_COUNT_CONTEXT_SWITCHES = -4, */
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 {
Ingo Molnar01b28382008-12-11 13:45:51 +010073 s64 type;
Ingo Molnar9f66a382008-12-10 12:33:23 +010074
75 u64 irq_period;
76 u32 record_type;
77
Ingo Molnar9b51f662008-12-12 13:49:45 +010078 u32 disabled : 1, /* off by default */
79 nmi : 1, /* NMI sampling */
80 raw : 1, /* raw event type */
81 inherit : 1, /* children inherit it */
82 __reserved_1 : 28;
Ingo Molnar9f66a382008-12-10 12:33:23 +010083
84 u64 __reserved_2;
Thomas Gleixnereab656a2008-12-08 19:26:59 +010085};
86
Ingo Molnar9f66a382008-12-10 12:33:23 +010087/*
88 * Kernel-internal data types:
89 */
90
Thomas Gleixner0793a612008-12-04 20:12:29 +010091/**
Ingo Molnar9f66a382008-12-10 12:33:23 +010092 * struct hw_perf_counter - performance counter hardware details:
Thomas Gleixner0793a612008-12-04 20:12:29 +010093 */
94struct hw_perf_counter {
Ingo Molnaree060942008-12-13 09:00:03 +010095#ifdef CONFIG_PERF_COUNTERS
Ingo Molnar9f66a382008-12-10 12:33:23 +010096 u64 config;
97 unsigned long config_base;
98 unsigned long counter_base;
99 int nmi;
100 unsigned int idx;
Ingo Molnaree060942008-12-13 09:00:03 +0100101 atomic64_t prev_count;
Ingo Molnar9f66a382008-12-10 12:33:23 +0100102 u64 irq_period;
Ingo Molnaree060942008-12-13 09:00:03 +0100103 atomic64_t period_left;
104#endif
Thomas Gleixner0793a612008-12-04 20:12:29 +0100105};
106
107/*
108 * Hardcoded buffer length limit for now, for IRQ-fed events:
109 */
Ingo Molnar9f66a382008-12-10 12:33:23 +0100110#define PERF_DATA_BUFLEN 2048
Thomas Gleixner0793a612008-12-04 20:12:29 +0100111
112/**
113 * struct perf_data - performance counter IRQ data sampling ...
114 */
115struct perf_data {
Ingo Molnar9f66a382008-12-10 12:33:23 +0100116 int len;
117 int rd_idx;
118 int overrun;
119 u8 data[PERF_DATA_BUFLEN];
Thomas Gleixner0793a612008-12-04 20:12:29 +0100120};
121
Ingo Molnar621a01e2008-12-11 12:46:46 +0100122struct perf_counter;
123
124/**
125 * struct hw_perf_counter_ops - performance counter hw ops
126 */
127struct hw_perf_counter_ops {
128 void (*hw_perf_counter_enable) (struct perf_counter *counter);
129 void (*hw_perf_counter_disable) (struct perf_counter *counter);
130 void (*hw_perf_counter_read) (struct perf_counter *counter);
131};
132
Thomas Gleixner0793a612008-12-04 20:12:29 +0100133/**
Ingo Molnar6a930702008-12-11 15:17:03 +0100134 * enum perf_counter_active_state - the states of a counter
135 */
136enum perf_counter_active_state {
137 PERF_COUNTER_STATE_OFF = -1,
138 PERF_COUNTER_STATE_INACTIVE = 0,
139 PERF_COUNTER_STATE_ACTIVE = 1,
140};
141
Ingo Molnar9b51f662008-12-12 13:49:45 +0100142struct file;
143
Ingo Molnar6a930702008-12-11 15:17:03 +0100144/**
Thomas Gleixner0793a612008-12-04 20:12:29 +0100145 * struct perf_counter - performance counter kernel representation:
146 */
147struct perf_counter {
Ingo Molnaree060942008-12-13 09:00:03 +0100148#ifdef CONFIG_PERF_COUNTERS
Ingo Molnar04289bb2008-12-11 08:38:42 +0100149 struct list_head list_entry;
150 struct list_head sibling_list;
151 struct perf_counter *group_leader;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100152 const struct hw_perf_counter_ops *hw_ops;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100153
Ingo Molnar6a930702008-12-11 15:17:03 +0100154 enum perf_counter_active_state state;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100155 atomic64_t count;
Ingo Molnaree060942008-12-13 09:00:03 +0100156
Ingo Molnar9f66a382008-12-10 12:33:23 +0100157 struct perf_counter_hw_event hw_event;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100158 struct hw_perf_counter hw;
159
160 struct perf_counter_context *ctx;
161 struct task_struct *task;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100162 struct file *filp;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100163
Ingo Molnar9b51f662008-12-12 13:49:45 +0100164 unsigned int nr_inherited;
165 struct perf_counter *parent;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100166 /*
167 * Protect attach/detach:
168 */
169 struct mutex mutex;
170
171 int oncpu;
172 int cpu;
173
Thomas Gleixner0793a612008-12-04 20:12:29 +0100174 /* read() / irq related data */
175 wait_queue_head_t waitq;
176 /* optional: for NMIs */
177 int wakeup_pending;
178 struct perf_data *irqdata;
179 struct perf_data *usrdata;
180 struct perf_data data[2];
Ingo Molnaree060942008-12-13 09:00:03 +0100181#endif
Thomas Gleixner0793a612008-12-04 20:12:29 +0100182};
183
184/**
185 * struct perf_counter_context - counter context structure
186 *
187 * Used as a container for task counters and CPU counters as well:
188 */
189struct perf_counter_context {
190#ifdef CONFIG_PERF_COUNTERS
191 /*
192 * Protect the list of counters:
193 */
194 spinlock_t lock;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100195
196 struct list_head counter_list;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100197 int nr_counters;
198 int nr_active;
199 struct task_struct *task;
200#endif
201};
202
203/**
204 * struct perf_counter_cpu_context - per cpu counter context structure
205 */
206struct perf_cpu_context {
207 struct perf_counter_context ctx;
208 struct perf_counter_context *task_ctx;
209 int active_oncpu;
210 int max_pertask;
211};
212
213/*
214 * Set by architecture code:
215 */
216extern int perf_max_counters;
217
218#ifdef CONFIG_PERF_COUNTERS
Ingo Molnar9b51f662008-12-12 13:49:45 +0100219extern void
220perf_counter_show(struct perf_counter *counter, char *str, int trace);
Ingo Molnar5c92d122008-12-11 13:21:10 +0100221extern const struct hw_perf_counter_ops *
Ingo Molnar621a01e2008-12-11 12:46:46 +0100222hw_perf_counter_init(struct perf_counter *counter);
223
Thomas Gleixner0793a612008-12-04 20:12:29 +0100224extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
225extern void perf_counter_task_sched_out(struct task_struct *task, int cpu);
226extern void perf_counter_task_tick(struct task_struct *task, int cpu);
Ingo Molnar9b51f662008-12-12 13:49:45 +0100227extern void perf_counter_init_task(struct task_struct *child);
228extern void perf_counter_exit_task(struct task_struct *child);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100229extern void perf_counter_notify(struct pt_regs *regs);
230extern void perf_counter_print_debug(void);
Ingo Molnar01b28382008-12-11 13:45:51 +0100231extern u64 hw_perf_save_disable(void);
232extern void hw_perf_restore(u64 ctrl);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100233extern int perf_counter_task_disable(void);
234extern int perf_counter_task_enable(void);
Ingo Molnar5c92d122008-12-11 13:21:10 +0100235
Thomas Gleixner0793a612008-12-04 20:12:29 +0100236#else
237static inline void
Ingo Molnar9b51f662008-12-12 13:49:45 +0100238perf_counter_show(struct perf_counter *counter, char *str, int trace) { }
239static inline void
Thomas Gleixner0793a612008-12-04 20:12:29 +0100240perf_counter_task_sched_in(struct task_struct *task, int cpu) { }
241static inline void
242perf_counter_task_sched_out(struct task_struct *task, int cpu) { }
243static inline void
244perf_counter_task_tick(struct task_struct *task, int cpu) { }
Ingo Molnar9b51f662008-12-12 13:49:45 +0100245static inline void perf_counter_init_task(struct task_struct *child) { }
246static inline void perf_counter_exit_task(struct task_struct *child) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100247static inline void perf_counter_notify(struct pt_regs *regs) { }
248static inline void perf_counter_print_debug(void) { }
Ingo Molnar01b28382008-12-11 13:45:51 +0100249static inline void hw_perf_restore(u64 ctrl) { }
250static inline u64 hw_perf_save_disable(void) { return 0; }
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100251static inline int perf_counter_task_disable(void) { return -EINVAL; }
252static inline int perf_counter_task_enable(void) { return -EINVAL; }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100253#endif
254
255#endif /* _LINUX_PERF_COUNTER_H */