]>
Commit | Line | Data |
---|---|---|
0793a61d TG |
1 | /* |
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> | |
d859e29f | 17 | #include <asm/ioctl.h> |
e44aef58 IM |
18 | |
19 | #ifdef CONFIG_PERF_COUNTERS | |
20 | # include <asm/perf_counter.h> | |
21 | #endif | |
0793a61d TG |
22 | |
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 | ||
29 | struct task_struct; | |
30 | ||
31 | /* | |
9f66a381 IM |
32 | * 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: | |
0793a61d TG |
38 | */ |
39 | enum hw_event_types { | |
0793a61d | 40 | /* |
9f66a381 | 41 | * Common hardware events, generalized by the kernel: |
0793a61d | 42 | */ |
f650a672 | 43 | PERF_COUNT_CPU_CYCLES = 0, |
9f66a381 IM |
44 | 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, | |
f650a672 | 49 | PERF_COUNT_BUS_CYCLES = 6, |
9f66a381 | 50 | |
f650a672 | 51 | PERF_HW_EVENTS_MAX = 7, |
6c594c21 | 52 | |
9f66a381 IM |
53 | /* |
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, | |
5d6a27d8 IM |
61 | PERF_COUNT_PAGE_FAULTS = -3, |
62 | PERF_COUNT_CONTEXT_SWITCHES = -4, | |
6c594c21 IM |
63 | PERF_COUNT_CPU_MIGRATIONS = -5, |
64 | ||
65 | PERF_SW_EVENTS_MIN = -6, | |
0793a61d TG |
66 | }; |
67 | ||
68 | /* | |
69 | * IRQ-notification data record type: | |
70 | */ | |
9f66a381 IM |
71 | enum perf_counter_record_type { |
72 | PERF_RECORD_SIMPLE = 0, | |
73 | PERF_RECORD_IRQ = 1, | |
74 | PERF_RECORD_GROUP = 2, | |
0793a61d TG |
75 | }; |
76 | ||
9f66a381 IM |
77 | /* |
78 | * Hardware event to monitor via a performance monitoring counter: | |
79 | */ | |
80 | struct perf_counter_hw_event { | |
01b2838c | 81 | s64 type; |
9f66a381 IM |
82 | |
83 | u64 irq_period; | |
84 | u32 record_type; | |
85 | ||
0475f9ea PM |
86 | u32 disabled : 1, /* off by default */ |
87 | nmi : 1, /* NMI sampling */ | |
88 | raw : 1, /* raw event type */ | |
89 | inherit : 1, /* children inherit it */ | |
90 | pinned : 1, /* must always be on PMU */ | |
91 | exclusive : 1, /* only group on PMU */ | |
92 | exclude_user : 1, /* don't count user */ | |
93 | exclude_kernel : 1, /* ditto kernel */ | |
94 | exclude_hv : 1, /* ditto hypervisor */ | |
95 | ||
96 | __reserved_1 : 23; | |
9f66a381 IM |
97 | |
98 | u64 __reserved_2; | |
eab656ae TG |
99 | }; |
100 | ||
d859e29f PM |
101 | /* |
102 | * Ioctls that can be done on a perf counter fd: | |
103 | */ | |
104 | #define PERF_COUNTER_IOC_ENABLE _IO('$', 0) | |
105 | #define PERF_COUNTER_IOC_DISABLE _IO('$', 1) | |
106 | ||
9f66a381 IM |
107 | /* |
108 | * Kernel-internal data types: | |
109 | */ | |
110 | ||
0793a61d | 111 | /** |
9f66a381 | 112 | * struct hw_perf_counter - performance counter hardware details: |
0793a61d TG |
113 | */ |
114 | struct hw_perf_counter { | |
ee06094f | 115 | #ifdef CONFIG_PERF_COUNTERS |
9f66a381 IM |
116 | u64 config; |
117 | unsigned long config_base; | |
118 | unsigned long counter_base; | |
119 | int nmi; | |
120 | unsigned int idx; | |
ee06094f | 121 | atomic64_t prev_count; |
9f66a381 | 122 | u64 irq_period; |
ee06094f IM |
123 | atomic64_t period_left; |
124 | #endif | |
0793a61d TG |
125 | }; |
126 | ||
127 | /* | |
128 | * Hardcoded buffer length limit for now, for IRQ-fed events: | |
129 | */ | |
9f66a381 | 130 | #define PERF_DATA_BUFLEN 2048 |
0793a61d TG |
131 | |
132 | /** | |
133 | * struct perf_data - performance counter IRQ data sampling ... | |
134 | */ | |
135 | struct perf_data { | |
9f66a381 IM |
136 | int len; |
137 | int rd_idx; | |
138 | int overrun; | |
139 | u8 data[PERF_DATA_BUFLEN]; | |
0793a61d TG |
140 | }; |
141 | ||
621a01ea IM |
142 | struct perf_counter; |
143 | ||
144 | /** | |
145 | * struct hw_perf_counter_ops - performance counter hw ops | |
146 | */ | |
147 | struct hw_perf_counter_ops { | |
95cdd2e7 | 148 | int (*enable) (struct perf_counter *counter); |
7671581f IM |
149 | void (*disable) (struct perf_counter *counter); |
150 | void (*read) (struct perf_counter *counter); | |
621a01ea IM |
151 | }; |
152 | ||
6a930700 IM |
153 | /** |
154 | * enum perf_counter_active_state - the states of a counter | |
155 | */ | |
156 | enum perf_counter_active_state { | |
3b6f9e5c | 157 | PERF_COUNTER_STATE_ERROR = -2, |
6a930700 IM |
158 | PERF_COUNTER_STATE_OFF = -1, |
159 | PERF_COUNTER_STATE_INACTIVE = 0, | |
160 | PERF_COUNTER_STATE_ACTIVE = 1, | |
161 | }; | |
162 | ||
9b51f66d IM |
163 | struct file; |
164 | ||
0793a61d TG |
165 | /** |
166 | * struct perf_counter - performance counter kernel representation: | |
167 | */ | |
168 | struct perf_counter { | |
ee06094f | 169 | #ifdef CONFIG_PERF_COUNTERS |
04289bb9 IM |
170 | struct list_head list_entry; |
171 | struct list_head sibling_list; | |
172 | struct perf_counter *group_leader; | |
5c92d124 | 173 | const struct hw_perf_counter_ops *hw_ops; |
04289bb9 | 174 | |
6a930700 | 175 | enum perf_counter_active_state state; |
c07c99b6 | 176 | enum perf_counter_active_state prev_state; |
0793a61d | 177 | atomic64_t count; |
ee06094f | 178 | |
9f66a381 | 179 | struct perf_counter_hw_event hw_event; |
0793a61d TG |
180 | struct hw_perf_counter hw; |
181 | ||
182 | struct perf_counter_context *ctx; | |
183 | struct task_struct *task; | |
9b51f66d | 184 | struct file *filp; |
0793a61d | 185 | |
9b51f66d | 186 | struct perf_counter *parent; |
d859e29f PM |
187 | struct list_head child_list; |
188 | ||
0793a61d | 189 | /* |
d859e29f | 190 | * Protect attach/detach and child_list: |
0793a61d TG |
191 | */ |
192 | struct mutex mutex; | |
193 | ||
194 | int oncpu; | |
195 | int cpu; | |
196 | ||
0793a61d TG |
197 | /* read() / irq related data */ |
198 | wait_queue_head_t waitq; | |
199 | /* optional: for NMIs */ | |
200 | int wakeup_pending; | |
201 | struct perf_data *irqdata; | |
202 | struct perf_data *usrdata; | |
203 | struct perf_data data[2]; | |
ee06094f | 204 | #endif |
0793a61d TG |
205 | }; |
206 | ||
207 | /** | |
208 | * struct perf_counter_context - counter context structure | |
209 | * | |
210 | * Used as a container for task counters and CPU counters as well: | |
211 | */ | |
212 | struct perf_counter_context { | |
213 | #ifdef CONFIG_PERF_COUNTERS | |
214 | /* | |
d859e29f PM |
215 | * Protect the states of the counters in the list, |
216 | * nr_active, and the list: | |
0793a61d TG |
217 | */ |
218 | spinlock_t lock; | |
d859e29f PM |
219 | /* |
220 | * Protect the list of counters. Locking either mutex or lock | |
221 | * is sufficient to ensure the list doesn't change; to change | |
222 | * the list you need to lock both the mutex and the spinlock. | |
223 | */ | |
224 | struct mutex mutex; | |
04289bb9 IM |
225 | |
226 | struct list_head counter_list; | |
0793a61d TG |
227 | int nr_counters; |
228 | int nr_active; | |
d859e29f | 229 | int is_active; |
0793a61d TG |
230 | struct task_struct *task; |
231 | #endif | |
232 | }; | |
233 | ||
234 | /** | |
235 | * struct perf_counter_cpu_context - per cpu counter context structure | |
236 | */ | |
237 | struct perf_cpu_context { | |
238 | struct perf_counter_context ctx; | |
239 | struct perf_counter_context *task_ctx; | |
240 | int active_oncpu; | |
241 | int max_pertask; | |
3b6f9e5c | 242 | int exclusive; |
0793a61d TG |
243 | }; |
244 | ||
245 | /* | |
246 | * Set by architecture code: | |
247 | */ | |
248 | extern int perf_max_counters; | |
249 | ||
250 | #ifdef CONFIG_PERF_COUNTERS | |
5c92d124 | 251 | extern const struct hw_perf_counter_ops * |
621a01ea IM |
252 | hw_perf_counter_init(struct perf_counter *counter); |
253 | ||
0793a61d TG |
254 | extern void perf_counter_task_sched_in(struct task_struct *task, int cpu); |
255 | extern void perf_counter_task_sched_out(struct task_struct *task, int cpu); | |
256 | extern void perf_counter_task_tick(struct task_struct *task, int cpu); | |
9b51f66d IM |
257 | extern void perf_counter_init_task(struct task_struct *child); |
258 | extern void perf_counter_exit_task(struct task_struct *child); | |
0793a61d TG |
259 | extern void perf_counter_notify(struct pt_regs *regs); |
260 | extern void perf_counter_print_debug(void); | |
1b023a96 | 261 | extern void perf_counter_unthrottle(void); |
01b2838c IM |
262 | extern u64 hw_perf_save_disable(void); |
263 | extern void hw_perf_restore(u64 ctrl); | |
1d1c7ddb IM |
264 | extern int perf_counter_task_disable(void); |
265 | extern int perf_counter_task_enable(void); | |
3cbed429 PM |
266 | extern int hw_perf_group_sched_in(struct perf_counter *group_leader, |
267 | struct perf_cpu_context *cpuctx, | |
268 | struct perf_counter_context *ctx, int cpu); | |
5c92d124 | 269 | |
3b6f9e5c PM |
270 | /* |
271 | * Return 1 for a software counter, 0 for a hardware counter | |
272 | */ | |
273 | static inline int is_software_counter(struct perf_counter *counter) | |
274 | { | |
275 | return !counter->hw_event.raw && counter->hw_event.type < 0; | |
276 | } | |
277 | ||
0793a61d TG |
278 | #else |
279 | static inline void | |
280 | perf_counter_task_sched_in(struct task_struct *task, int cpu) { } | |
281 | static inline void | |
282 | perf_counter_task_sched_out(struct task_struct *task, int cpu) { } | |
283 | static inline void | |
284 | perf_counter_task_tick(struct task_struct *task, int cpu) { } | |
9b51f66d IM |
285 | static inline void perf_counter_init_task(struct task_struct *child) { } |
286 | static inline void perf_counter_exit_task(struct task_struct *child) { } | |
0793a61d TG |
287 | static inline void perf_counter_notify(struct pt_regs *regs) { } |
288 | static inline void perf_counter_print_debug(void) { } | |
1b023a96 | 289 | static inline void perf_counter_unthrottle(void) { } |
01b2838c IM |
290 | static inline void hw_perf_restore(u64 ctrl) { } |
291 | static inline u64 hw_perf_save_disable(void) { return 0; } | |
1d1c7ddb IM |
292 | static inline int perf_counter_task_disable(void) { return -EINVAL; } |
293 | static inline int perf_counter_task_enable(void) { return -EINVAL; } | |
0793a61d TG |
294 | #endif |
295 | ||
296 | #endif /* _LINUX_PERF_COUNTER_H */ |