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:
24 * Generalized performance counter event types, used by the hw_event.type
25 * parameter of the sys_perf_counter_open() syscall:
29 * Common hardware events, generalized by the kernel:
31 PERF_COUNT_CPU_CYCLES
= 0,
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,
37 PERF_COUNT_BUS_CYCLES
= 6,
39 PERF_HW_EVENTS_MAX
= 7,
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):
47 PERF_COUNT_CPU_CLOCK
= -1,
48 PERF_COUNT_TASK_CLOCK
= -2,
49 PERF_COUNT_PAGE_FAULTS
= -3,
50 PERF_COUNT_CONTEXT_SWITCHES
= -4,
51 PERF_COUNT_CPU_MIGRATIONS
= -5,
53 PERF_SW_EVENTS_MIN
= -6,
57 * IRQ-notification data record type:
59 enum perf_counter_record_type
{
60 PERF_RECORD_SIMPLE
= 0,
62 PERF_RECORD_GROUP
= 2,
66 * Hardware event to monitor via a performance monitoring counter:
68 struct perf_counter_hw_event
{
74 __u32 disabled
: 1, /* off by default */
75 nmi
: 1, /* NMI sampling */
76 raw
: 1, /* raw event type */
77 inherit
: 1, /* children inherit it */
78 pinned
: 1, /* must always be on PMU */
79 exclusive
: 1, /* only group on PMU */
80 exclude_user
: 1, /* don't count user */
81 exclude_kernel
: 1, /* ditto kernel */
82 exclude_hv
: 1, /* ditto hypervisor */
90 * Ioctls that can be done on a perf counter fd:
92 #define PERF_COUNTER_IOC_ENABLE _IO('$', 0)
93 #define PERF_COUNTER_IOC_DISABLE _IO('$', 1)
97 * Kernel-internal data types and definitions:
100 #ifdef CONFIG_PERF_COUNTERS
101 # include <asm/perf_counter.h>
104 #include <linux/list.h>
105 #include <linux/mutex.h>
106 #include <linux/rculist.h>
107 #include <linux/rcupdate.h>
108 #include <linux/spinlock.h>
109 #include <asm/atomic.h>
114 * struct hw_perf_counter - performance counter hardware details:
116 struct hw_perf_counter
{
117 #ifdef CONFIG_PERF_COUNTERS
119 unsigned long config_base
;
120 unsigned long counter_base
;
123 atomic64_t prev_count
;
125 atomic64_t period_left
;
130 * Hardcoded buffer length limit for now, for IRQ-fed events:
132 #define PERF_DATA_BUFLEN 2048
135 * struct perf_data - performance counter IRQ data sampling ...
141 u8 data
[PERF_DATA_BUFLEN
];
147 * struct hw_perf_counter_ops - performance counter hw ops
149 struct hw_perf_counter_ops
{
150 int (*enable
) (struct perf_counter
*counter
);
151 void (*disable
) (struct perf_counter
*counter
);
152 void (*read
) (struct perf_counter
*counter
);
156 * enum perf_counter_active_state - the states of a counter
158 enum perf_counter_active_state
{
159 PERF_COUNTER_STATE_ERROR
= -2,
160 PERF_COUNTER_STATE_OFF
= -1,
161 PERF_COUNTER_STATE_INACTIVE
= 0,
162 PERF_COUNTER_STATE_ACTIVE
= 1,
168 * struct perf_counter - performance counter kernel representation:
170 struct perf_counter
{
171 #ifdef CONFIG_PERF_COUNTERS
172 struct list_head list_entry
;
173 struct list_head sibling_list
;
174 struct perf_counter
*group_leader
;
175 const struct hw_perf_counter_ops
*hw_ops
;
177 enum perf_counter_active_state state
;
178 enum perf_counter_active_state prev_state
;
181 struct perf_counter_hw_event hw_event
;
182 struct hw_perf_counter hw
;
184 struct perf_counter_context
*ctx
;
185 struct task_struct
*task
;
188 struct perf_counter
*parent
;
189 struct list_head child_list
;
192 * Protect attach/detach and child_list:
199 /* read() / irq related data */
200 wait_queue_head_t waitq
;
201 /* optional: for NMIs */
203 struct perf_data
*irqdata
;
204 struct perf_data
*usrdata
;
205 struct perf_data data
[2];
210 * struct perf_counter_context - counter context structure
212 * Used as a container for task counters and CPU counters as well:
214 struct perf_counter_context
{
215 #ifdef CONFIG_PERF_COUNTERS
217 * Protect the states of the counters in the list,
218 * nr_active, and the list:
222 * Protect the list of counters. Locking either mutex or lock
223 * is sufficient to ensure the list doesn't change; to change
224 * the list you need to lock both the mutex and the spinlock.
228 struct list_head counter_list
;
232 struct task_struct
*task
;
237 * struct perf_counter_cpu_context - per cpu counter context structure
239 struct perf_cpu_context
{
240 struct perf_counter_context ctx
;
241 struct perf_counter_context
*task_ctx
;
248 * Set by architecture code:
250 extern int perf_max_counters
;
252 #ifdef CONFIG_PERF_COUNTERS
253 extern const struct hw_perf_counter_ops
*
254 hw_perf_counter_init(struct perf_counter
*counter
);
256 extern void perf_counter_task_sched_in(struct task_struct
*task
, int cpu
);
257 extern void perf_counter_task_sched_out(struct task_struct
*task
, int cpu
);
258 extern void perf_counter_task_tick(struct task_struct
*task
, int cpu
);
259 extern void perf_counter_init_task(struct task_struct
*child
);
260 extern void perf_counter_exit_task(struct task_struct
*child
);
261 extern void perf_counter_notify(struct pt_regs
*regs
);
262 extern void perf_counter_print_debug(void);
263 extern void perf_counter_unthrottle(void);
264 extern u64
hw_perf_save_disable(void);
265 extern void hw_perf_restore(u64 ctrl
);
266 extern int perf_counter_task_disable(void);
267 extern int perf_counter_task_enable(void);
268 extern int hw_perf_group_sched_in(struct perf_counter
*group_leader
,
269 struct perf_cpu_context
*cpuctx
,
270 struct perf_counter_context
*ctx
, int cpu
);
273 * Return 1 for a software counter, 0 for a hardware counter
275 static inline int is_software_counter(struct perf_counter
*counter
)
277 return !counter
->hw_event
.raw
&& counter
->hw_event
.type
< 0;
282 perf_counter_task_sched_in(struct task_struct
*task
, int cpu
) { }
284 perf_counter_task_sched_out(struct task_struct
*task
, int cpu
) { }
286 perf_counter_task_tick(struct task_struct
*task
, int cpu
) { }
287 static inline void perf_counter_init_task(struct task_struct
*child
) { }
288 static inline void perf_counter_exit_task(struct task_struct
*child
) { }
289 static inline void perf_counter_notify(struct pt_regs
*regs
) { }
290 static inline void perf_counter_print_debug(void) { }
291 static inline void perf_counter_unthrottle(void) { }
292 static inline void hw_perf_restore(u64 ctrl
) { }
293 static inline u64
hw_perf_save_disable(void) { return 0; }
294 static inline int perf_counter_task_disable(void) { return -EINVAL
; }
295 static inline int perf_counter_task_enable(void) { return -EINVAL
; }
298 #endif /* __KERNEL__ */
299 #endif /* _LINUX_PERF_COUNTER_H */