]>
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 | ||
f3dfd265 PM |
16 | #include <linux/types.h> |
17 | #include <linux/ioctl.h> | |
0793a61d TG |
18 | |
19 | /* | |
9f66a381 IM |
20 | * 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: | |
0793a61d TG |
26 | */ |
27 | enum hw_event_types { | |
0793a61d | 28 | /* |
9f66a381 | 29 | * Common hardware events, generalized by the kernel: |
0793a61d | 30 | */ |
f650a672 | 31 | PERF_COUNT_CPU_CYCLES = 0, |
9f66a381 IM |
32 | 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, | |
f650a672 | 37 | PERF_COUNT_BUS_CYCLES = 6, |
9f66a381 | 38 | |
f650a672 | 39 | PERF_HW_EVENTS_MAX = 7, |
6c594c21 | 40 | |
9f66a381 IM |
41 | /* |
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, | |
5d6a27d8 IM |
49 | PERF_COUNT_PAGE_FAULTS = -3, |
50 | PERF_COUNT_CONTEXT_SWITCHES = -4, | |
6c594c21 | 51 | PERF_COUNT_CPU_MIGRATIONS = -5, |
ac17dc8e PZ |
52 | PERF_COUNT_PAGE_FAULTS_MIN = -6, |
53 | PERF_COUNT_PAGE_FAULTS_MAJ = -7, | |
6c594c21 | 54 | |
ac17dc8e | 55 | PERF_SW_EVENTS_MIN = -8, |
e077df4f PZ |
56 | |
57 | PERF_TP_EVENTS_MIN = -65536 | |
0793a61d TG |
58 | }; |
59 | ||
60 | /* | |
61 | * IRQ-notification data record type: | |
62 | */ | |
9f66a381 IM |
63 | enum perf_counter_record_type { |
64 | PERF_RECORD_SIMPLE = 0, | |
65 | PERF_RECORD_IRQ = 1, | |
66 | PERF_RECORD_GROUP = 2, | |
0793a61d TG |
67 | }; |
68 | ||
9f66a381 IM |
69 | /* |
70 | * Hardware event to monitor via a performance monitoring counter: | |
71 | */ | |
72 | struct perf_counter_hw_event { | |
f3dfd265 | 73 | __s64 type; |
9f66a381 | 74 | |
f3dfd265 | 75 | __u64 irq_period; |
2743a5b0 PM |
76 | __u64 record_type; |
77 | __u64 read_format; | |
9f66a381 | 78 | |
2743a5b0 | 79 | __u64 disabled : 1, /* off by default */ |
0475f9ea PM |
80 | 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 */ | |
2743a5b0 | 88 | exclude_idle : 1, /* don't count when idle */ |
0475f9ea | 89 | |
2485e518 | 90 | __reserved_1 : 54; |
2743a5b0 PM |
91 | |
92 | __u32 extra_config_len; | |
93 | __u32 __reserved_4; | |
9f66a381 | 94 | |
f3dfd265 | 95 | __u64 __reserved_2; |
2743a5b0 | 96 | __u64 __reserved_3; |
eab656ae TG |
97 | }; |
98 | ||
d859e29f PM |
99 | /* |
100 | * 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 | ||
f3dfd265 | 105 | #ifdef __KERNEL__ |
9f66a381 | 106 | /* |
f3dfd265 | 107 | * Kernel-internal data types and definitions: |
9f66a381 IM |
108 | */ |
109 | ||
f3dfd265 PM |
110 | #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> | |
d6d020e9 | 119 | #include <linux/hrtimer.h> |
f3dfd265 PM |
120 | #include <asm/atomic.h> |
121 | ||
122 | struct task_struct; | |
123 | ||
0793a61d | 124 | /** |
9f66a381 | 125 | * struct hw_perf_counter - performance counter hardware details: |
0793a61d TG |
126 | */ |
127 | struct hw_perf_counter { | |
ee06094f | 128 | #ifdef CONFIG_PERF_COUNTERS |
d6d020e9 PZ |
129 | 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 | }; | |
ee06094f | 142 | atomic64_t prev_count; |
9f66a381 | 143 | u64 irq_period; |
ee06094f IM |
144 | atomic64_t period_left; |
145 | #endif | |
0793a61d TG |
146 | }; |
147 | ||
148 | /* | |
149 | * Hardcoded buffer length limit for now, for IRQ-fed events: | |
150 | */ | |
9f66a381 | 151 | #define PERF_DATA_BUFLEN 2048 |
0793a61d TG |
152 | |
153 | /** | |
154 | * struct perf_data - performance counter IRQ data sampling ... | |
155 | */ | |
156 | struct perf_data { | |
9f66a381 IM |
157 | int len; |
158 | int rd_idx; | |
159 | int overrun; | |
160 | u8 data[PERF_DATA_BUFLEN]; | |
0793a61d TG |
161 | }; |
162 | ||
621a01ea IM |
163 | struct perf_counter; |
164 | ||
165 | /** | |
166 | * struct hw_perf_counter_ops - performance counter hw ops | |
167 | */ | |
168 | struct hw_perf_counter_ops { | |
95cdd2e7 | 169 | int (*enable) (struct perf_counter *counter); |
7671581f IM |
170 | void (*disable) (struct perf_counter *counter); |
171 | void (*read) (struct perf_counter *counter); | |
621a01ea IM |
172 | }; |
173 | ||
6a930700 IM |
174 | /** |
175 | * enum perf_counter_active_state - the states of a counter | |
176 | */ | |
177 | enum perf_counter_active_state { | |
3b6f9e5c | 178 | PERF_COUNTER_STATE_ERROR = -2, |
6a930700 IM |
179 | PERF_COUNTER_STATE_OFF = -1, |
180 | PERF_COUNTER_STATE_INACTIVE = 0, | |
181 | PERF_COUNTER_STATE_ACTIVE = 1, | |
182 | }; | |
183 | ||
9b51f66d IM |
184 | struct file; |
185 | ||
0793a61d TG |
186 | /** |
187 | * struct perf_counter - performance counter kernel representation: | |
188 | */ | |
189 | struct perf_counter { | |
ee06094f | 190 | #ifdef CONFIG_PERF_COUNTERS |
04289bb9 | 191 | struct list_head list_entry; |
592903cd | 192 | struct list_head event_entry; |
04289bb9 IM |
193 | struct list_head sibling_list; |
194 | struct perf_counter *group_leader; | |
5c92d124 | 195 | const struct hw_perf_counter_ops *hw_ops; |
04289bb9 | 196 | |
6a930700 | 197 | enum perf_counter_active_state state; |
c07c99b6 | 198 | enum perf_counter_active_state prev_state; |
0793a61d | 199 | atomic64_t count; |
ee06094f | 200 | |
9f66a381 | 201 | struct perf_counter_hw_event hw_event; |
0793a61d TG |
202 | struct hw_perf_counter hw; |
203 | ||
204 | struct perf_counter_context *ctx; | |
205 | struct task_struct *task; | |
9b51f66d | 206 | struct file *filp; |
0793a61d | 207 | |
9b51f66d | 208 | struct perf_counter *parent; |
d859e29f PM |
209 | struct list_head child_list; |
210 | ||
0793a61d | 211 | /* |
d859e29f | 212 | * Protect attach/detach and child_list: |
0793a61d TG |
213 | */ |
214 | struct mutex mutex; | |
215 | ||
216 | int oncpu; | |
217 | int cpu; | |
218 | ||
0793a61d TG |
219 | /* 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]; | |
592903cd | 226 | |
e077df4f | 227 | void (*destroy)(struct perf_counter *); |
592903cd | 228 | struct rcu_head rcu_head; |
ee06094f | 229 | #endif |
0793a61d TG |
230 | }; |
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 | */ | |
237 | struct perf_counter_context { | |
238 | #ifdef CONFIG_PERF_COUNTERS | |
239 | /* | |
d859e29f PM |
240 | * Protect the states of the counters in the list, |
241 | * nr_active, and the list: | |
0793a61d TG |
242 | */ |
243 | spinlock_t lock; | |
d859e29f PM |
244 | /* |
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; | |
04289bb9 IM |
250 | |
251 | struct list_head counter_list; | |
592903cd | 252 | struct list_head event_list; |
0793a61d TG |
253 | int nr_counters; |
254 | int nr_active; | |
d859e29f | 255 | int is_active; |
0793a61d TG |
256 | struct task_struct *task; |
257 | #endif | |
258 | }; | |
259 | ||
260 | /** | |
261 | * struct perf_counter_cpu_context - per cpu counter context structure | |
262 | */ | |
263 | struct perf_cpu_context { | |
264 | struct perf_counter_context ctx; | |
265 | struct perf_counter_context *task_ctx; | |
266 | int active_oncpu; | |
267 | int max_pertask; | |
3b6f9e5c | 268 | int exclusive; |
0793a61d TG |
269 | }; |
270 | ||
271 | /* | |
272 | * Set by architecture code: | |
273 | */ | |
274 | extern int perf_max_counters; | |
275 | ||
276 | #ifdef CONFIG_PERF_COUNTERS | |
5c92d124 | 277 | extern const struct hw_perf_counter_ops * |
621a01ea IM |
278 | hw_perf_counter_init(struct perf_counter *counter); |
279 | ||
0793a61d TG |
280 | extern void perf_counter_task_sched_in(struct task_struct *task, int cpu); |
281 | extern void perf_counter_task_sched_out(struct task_struct *task, int cpu); | |
282 | extern void perf_counter_task_tick(struct task_struct *task, int cpu); | |
9b51f66d IM |
283 | extern void perf_counter_init_task(struct task_struct *child); |
284 | extern void perf_counter_exit_task(struct task_struct *child); | |
0793a61d TG |
285 | extern void perf_counter_notify(struct pt_regs *regs); |
286 | extern void perf_counter_print_debug(void); | |
1b023a96 | 287 | extern void perf_counter_unthrottle(void); |
01b2838c IM |
288 | extern u64 hw_perf_save_disable(void); |
289 | extern void hw_perf_restore(u64 ctrl); | |
1d1c7ddb IM |
290 | extern int perf_counter_task_disable(void); |
291 | extern int perf_counter_task_enable(void); | |
3cbed429 PM |
292 | extern 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); | |
5c92d124 | 295 | |
3b6f9e5c PM |
296 | /* |
297 | * Return 1 for a software counter, 0 for a hardware counter | |
298 | */ | |
299 | static inline int is_software_counter(struct perf_counter *counter) | |
300 | { | |
301 | return !counter->hw_event.raw && counter->hw_event.type < 0; | |
302 | } | |
303 | ||
15dbf27c PZ |
304 | extern void perf_swcounter_event(enum hw_event_types, u64, int, struct pt_regs *); |
305 | ||
0793a61d TG |
306 | #else |
307 | static inline void | |
308 | perf_counter_task_sched_in(struct task_struct *task, int cpu) { } | |
309 | static inline void | |
310 | perf_counter_task_sched_out(struct task_struct *task, int cpu) { } | |
311 | static inline void | |
312 | perf_counter_task_tick(struct task_struct *task, int cpu) { } | |
9b51f66d IM |
313 | static inline void perf_counter_init_task(struct task_struct *child) { } |
314 | static inline void perf_counter_exit_task(struct task_struct *child) { } | |
0793a61d TG |
315 | static inline void perf_counter_notify(struct pt_regs *regs) { } |
316 | static inline void perf_counter_print_debug(void) { } | |
1b023a96 | 317 | static inline void perf_counter_unthrottle(void) { } |
15dbf27c | 318 | static inline void hw_perf_restore(u64 ctrl) { } |
01b2838c | 319 | static inline u64 hw_perf_save_disable(void) { return 0; } |
1d1c7ddb IM |
320 | static inline int perf_counter_task_disable(void) { return -EINVAL; } |
321 | static inline int perf_counter_task_enable(void) { return -EINVAL; } | |
15dbf27c PZ |
322 | |
323 | static inline void perf_swcounter_event(enum hw_event_types event, u64 nr, | |
324 | int nmi, struct pt_regs *regs) { } | |
0793a61d TG |
325 | #endif |
326 | ||
f3dfd265 | 327 | #endif /* __KERNEL__ */ |
0793a61d | 328 | #endif /* _LINUX_PERF_COUNTER_H */ |