2 * Performance counters:
4 * Copyright(C) 2008, Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2008, Red Hat, Inc., Ingo Molnar
7 * Data type definitions, declarations, prototypes.
9 * Started by: Thomas Gleixner and Ingo Molnar
11 * For licencing details see kernel-base/COPYING
13 #ifndef _LINUX_PERF_COUNTER_H
14 #define _LINUX_PERF_COUNTER_H
16 #include <linux/types.h>
17 #include <linux/ioctl.h>
20 * User-space ABI bits:
26 enum perf_event_types
{
27 PERF_TYPE_HARDWARE
= 0,
28 PERF_TYPE_SOFTWARE
= 1,
29 PERF_TYPE_TRACEPOINT
= 2,
32 * available TYPE space, raw is the max value.
39 * Generalized performance counter event types, used by the hw_event.event_id
40 * parameter of the sys_perf_counter_open() syscall:
44 * Common hardware events, generalized by the kernel:
46 PERF_COUNT_CPU_CYCLES
= 0,
47 PERF_COUNT_INSTRUCTIONS
= 1,
48 PERF_COUNT_CACHE_REFERENCES
= 2,
49 PERF_COUNT_CACHE_MISSES
= 3,
50 PERF_COUNT_BRANCH_INSTRUCTIONS
= 4,
51 PERF_COUNT_BRANCH_MISSES
= 5,
52 PERF_COUNT_BUS_CYCLES
= 6,
54 PERF_HW_EVENTS_MAX
= 7,
58 * Special "software" counters provided by the kernel, even if the hardware
59 * does not support performance counters. These counters measure various
60 * physical and sw events of the kernel (and allow the profiling of them as
64 PERF_COUNT_CPU_CLOCK
= 0,
65 PERF_COUNT_TASK_CLOCK
= 1,
66 PERF_COUNT_PAGE_FAULTS
= 2,
67 PERF_COUNT_CONTEXT_SWITCHES
= 3,
68 PERF_COUNT_CPU_MIGRATIONS
= 4,
69 PERF_COUNT_PAGE_FAULTS_MIN
= 5,
70 PERF_COUNT_PAGE_FAULTS_MAJ
= 6,
72 PERF_SW_EVENTS_MAX
= 7,
76 * IRQ-notification data record type:
78 enum perf_counter_record_type
{
79 PERF_RECORD_SIMPLE
= 0,
81 PERF_RECORD_GROUP
= 2,
85 * Hardware event to monitor via a performance monitoring counter:
87 struct perf_counter_hw_event
{
94 __u64 raw_event_id
: 63,
104 __u64 disabled
: 1, /* off by default */
105 nmi
: 1, /* NMI sampling */
106 inherit
: 1, /* children inherit it */
107 pinned
: 1, /* must always be on PMU */
108 exclusive
: 1, /* only group on PMU */
109 exclude_user
: 1, /* don't count user */
110 exclude_kernel
: 1, /* ditto kernel */
111 exclude_hv
: 1, /* ditto hypervisor */
112 exclude_idle
: 1, /* don't count when idle */
116 __u32 extra_config_len
;
124 * Ioctls that can be done on a perf counter fd:
126 #define PERF_COUNTER_IOC_ENABLE _IO('$', 0)
127 #define PERF_COUNTER_IOC_DISABLE _IO('$', 1)
131 * Kernel-internal data types and definitions:
134 #ifdef CONFIG_PERF_COUNTERS
135 # include <asm/perf_counter.h>
138 #include <linux/list.h>
139 #include <linux/mutex.h>
140 #include <linux/rculist.h>
141 #include <linux/rcupdate.h>
142 #include <linux/spinlock.h>
143 #include <linux/hrtimer.h>
144 #include <asm/atomic.h>
149 * struct hw_perf_counter - performance counter hardware details:
151 struct hw_perf_counter
{
152 #ifdef CONFIG_PERF_COUNTERS
154 struct { /* hardware */
156 unsigned long config_base
;
157 unsigned long counter_base
;
161 union { /* software */
163 struct hrtimer hrtimer
;
166 atomic64_t prev_count
;
168 atomic64_t period_left
;
173 * Hardcoded buffer length limit for now, for IRQ-fed events:
175 #define PERF_DATA_BUFLEN 2048
178 * struct perf_data - performance counter IRQ data sampling ...
184 u8 data
[PERF_DATA_BUFLEN
];
190 * struct hw_perf_counter_ops - performance counter hw ops
192 struct hw_perf_counter_ops
{
193 int (*enable
) (struct perf_counter
*counter
);
194 void (*disable
) (struct perf_counter
*counter
);
195 void (*read
) (struct perf_counter
*counter
);
199 * enum perf_counter_active_state - the states of a counter
201 enum perf_counter_active_state
{
202 PERF_COUNTER_STATE_ERROR
= -2,
203 PERF_COUNTER_STATE_OFF
= -1,
204 PERF_COUNTER_STATE_INACTIVE
= 0,
205 PERF_COUNTER_STATE_ACTIVE
= 1,
211 * struct perf_counter - performance counter kernel representation:
213 struct perf_counter
{
214 #ifdef CONFIG_PERF_COUNTERS
215 struct list_head list_entry
;
216 struct list_head event_entry
;
217 struct list_head sibling_list
;
218 struct perf_counter
*group_leader
;
219 const struct hw_perf_counter_ops
*hw_ops
;
221 enum perf_counter_active_state state
;
222 enum perf_counter_active_state prev_state
;
225 struct perf_counter_hw_event hw_event
;
226 struct hw_perf_counter hw
;
228 struct perf_counter_context
*ctx
;
229 struct task_struct
*task
;
232 struct perf_counter
*parent
;
233 struct list_head child_list
;
236 * Protect attach/detach and child_list:
243 /* read() / irq related data */
244 wait_queue_head_t waitq
;
245 /* optional: for NMIs */
247 struct perf_data
*irqdata
;
248 struct perf_data
*usrdata
;
249 struct perf_data data
[2];
251 void (*destroy
)(struct perf_counter
*);
252 struct rcu_head rcu_head
;
257 * struct perf_counter_context - counter context structure
259 * Used as a container for task counters and CPU counters as well:
261 struct perf_counter_context
{
262 #ifdef CONFIG_PERF_COUNTERS
264 * Protect the states of the counters in the list,
265 * nr_active, and the list:
269 * Protect the list of counters. Locking either mutex or lock
270 * is sufficient to ensure the list doesn't change; to change
271 * the list you need to lock both the mutex and the spinlock.
275 struct list_head counter_list
;
276 struct list_head event_list
;
280 struct task_struct
*task
;
285 * struct perf_counter_cpu_context - per cpu counter context structure
287 struct perf_cpu_context
{
288 struct perf_counter_context ctx
;
289 struct perf_counter_context
*task_ctx
;
296 * Set by architecture code:
298 extern int perf_max_counters
;
300 #ifdef CONFIG_PERF_COUNTERS
301 extern const struct hw_perf_counter_ops
*
302 hw_perf_counter_init(struct perf_counter
*counter
);
304 extern void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
);
305 extern void perf_counter_task_sched_out(struct task_struct
*task
, int cpu
);
306 extern void perf_counter_task_tick(struct task_struct
*task
, int cpu
);
307 extern void perf_counter_init_task(struct task_struct
*child
);
308 extern void perf_counter_exit_task(struct task_struct
*child
);
309 extern void perf_counter_notify(struct pt_regs
*regs
);
310 extern void perf_counter_print_debug(void);
311 extern void perf_counter_unthrottle(void);
312 extern u64
hw_perf_save_disable(void);
313 extern void hw_perf_restore(u64 ctrl
);
314 extern int perf_counter_task_disable(void);
315 extern int perf_counter_task_enable(void);
316 extern int hw_perf_group_sched_in(struct perf_counter
*group_leader
,
317 struct perf_cpu_context
*cpuctx
,
318 struct perf_counter_context
*ctx
, int cpu
);
321 * Return 1 for a software counter, 0 for a hardware counter
323 static inline int is_software_counter(struct perf_counter
*counter
)
325 return !counter
->hw_event
.raw_type
&&
326 counter
->hw_event
.type
!= PERF_TYPE_HARDWARE
;
329 extern void perf_swcounter_event(u32
, u64
, int, struct pt_regs
*);
333 perf_counter_task_sched_in(struct task_struct
*task
, int cpu
) { }
335 perf_counter_task_sched_out(struct task_struct
*task
, int cpu
) { }
337 perf_counter_task_tick(struct task_struct
*task
, int cpu
) { }
338 static inline void perf_counter_init_task(struct task_struct
*child
) { }
339 static inline void perf_counter_exit_task(struct task_struct
*child
) { }
340 static inline void perf_counter_notify(struct pt_regs
*regs
) { }
341 static inline void perf_counter_print_debug(void) { }
342 static inline void perf_counter_unthrottle(void) { }
343 static inline void hw_perf_restore(u64 ctrl
) { }
344 static inline u64
hw_perf_save_disable(void) { return 0; }
345 static inline int perf_counter_task_disable(void) { return -EINVAL
; }
346 static inline int perf_counter_task_enable(void) { return -EINVAL
; }
348 static inline void perf_swcounter_event(u32 event
, u64 nr
,
349 int nmi
, struct pt_regs
*regs
) { }
352 #endif /* __KERNEL__ */
353 #endif /* _LINUX_PERF_COUNTER_H */