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 <asm/atomic.h>
18 #ifdef CONFIG_PERF_COUNTERS
19 # include <asm/perf_counter.h>
22 #include <linux/list.h>
23 #include <linux/mutex.h>
24 #include <linux/rculist.h>
25 #include <linux/rcupdate.h>
26 #include <linux/spinlock.h>
31 * User-space ABI bits:
35 * Generalized performance counter event types, used by the hw_event.type
36 * parameter of the sys_perf_counter_open() syscall:
40 * Common hardware events, generalized by the kernel:
42 PERF_COUNT_CPU_CYCLES = 0,
43 PERF_COUNT_INSTRUCTIONS = 1,
44 PERF_COUNT_CACHE_REFERENCES = 2,
45 PERF_COUNT_CACHE_MISSES = 3,
46 PERF_COUNT_BRANCH_INSTRUCTIONS = 4,
47 PERF_COUNT_BRANCH_MISSES = 5,
48 PERF_COUNT_BUS_CYCLES = 6,
50 PERF_HW_EVENTS_MAX = 7,
53 * Special "software" counters provided by the kernel, even if
54 * the hardware does not support performance counters. These
55 * counters measure various physical and sw events of the
56 * kernel (and allow the profiling of them as well):
58 PERF_COUNT_CPU_CLOCK = -1,
59 PERF_COUNT_TASK_CLOCK = -2,
60 PERF_COUNT_PAGE_FAULTS = -3,
61 PERF_COUNT_CONTEXT_SWITCHES = -4,
62 PERF_COUNT_CPU_MIGRATIONS = -5,
64 PERF_SW_EVENTS_MIN = -6,
68 * IRQ-notification data record type:
70 enum perf_counter_record_type {
71 PERF_RECORD_SIMPLE = 0,
73 PERF_RECORD_GROUP = 2,
77 * Hardware event to monitor via a performance monitoring counter:
79 struct perf_counter_hw_event {
85 u32 disabled : 1, /* off by default */
86 nmi : 1, /* NMI sampling */
87 raw : 1, /* raw event type */
88 inherit : 1, /* children inherit it */
89 pinned : 1, /* must always be on PMU */
90 exclusive : 1, /* only counter on PMU */
98 * Kernel-internal data types:
102 * struct hw_perf_counter - performance counter hardware details:
104 struct hw_perf_counter {
105 #ifdef CONFIG_PERF_COUNTERS
107 unsigned long config_base;
108 unsigned long counter_base;
111 atomic64_t prev_count;
113 atomic64_t period_left;
118 * Hardcoded buffer length limit for now, for IRQ-fed events:
120 #define PERF_DATA_BUFLEN 2048
123 * struct perf_data - performance counter IRQ data sampling ...
129 u8 data[PERF_DATA_BUFLEN];
135 * struct hw_perf_counter_ops - performance counter hw ops
137 struct hw_perf_counter_ops {
138 int (*enable) (struct perf_counter *counter);
139 void (*disable) (struct perf_counter *counter);
140 void (*read) (struct perf_counter *counter);
144 * enum perf_counter_active_state - the states of a counter
146 enum perf_counter_active_state {
147 PERF_COUNTER_STATE_ERROR = -2,
148 PERF_COUNTER_STATE_OFF = -1,
149 PERF_COUNTER_STATE_INACTIVE = 0,
150 PERF_COUNTER_STATE_ACTIVE = 1,
156 * struct perf_counter - performance counter kernel representation:
158 struct perf_counter {
159 #ifdef CONFIG_PERF_COUNTERS
160 struct list_head list_entry;
161 struct list_head sibling_list;
162 struct perf_counter *group_leader;
163 const struct hw_perf_counter_ops *hw_ops;
165 enum perf_counter_active_state state;
168 struct perf_counter_hw_event hw_event;
169 struct hw_perf_counter hw;
171 struct perf_counter_context *ctx;
172 struct task_struct *task;
175 struct perf_counter *parent;
177 * Protect attach/detach:
184 /* read() / irq related data */
185 wait_queue_head_t waitq;
186 /* optional: for NMIs */
188 struct perf_data *irqdata;
189 struct perf_data *usrdata;
190 struct perf_data data[2];
195 * struct perf_counter_context - counter context structure
197 * Used as a container for task counters and CPU counters as well:
199 struct perf_counter_context {
200 #ifdef CONFIG_PERF_COUNTERS
202 * Protect the list of counters:
206 struct list_head counter_list;
209 struct task_struct *task;
214 * struct perf_counter_cpu_context - per cpu counter context structure
216 struct perf_cpu_context {
217 struct perf_counter_context ctx;
218 struct perf_counter_context *task_ctx;
225 * Set by architecture code:
227 extern int perf_max_counters;
229 #ifdef CONFIG_PERF_COUNTERS
230 extern const struct hw_perf_counter_ops *
231 hw_perf_counter_init(struct perf_counter *counter);
233 extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
234 extern void perf_counter_task_sched_out(struct task_struct *task, int cpu);
235 extern void perf_counter_task_tick(struct task_struct *task, int cpu);
236 extern void perf_counter_init_task(struct task_struct *child);
237 extern void perf_counter_exit_task(struct task_struct *child);
238 extern void perf_counter_notify(struct pt_regs *regs);
239 extern void perf_counter_print_debug(void);
240 extern u64 hw_perf_save_disable(void);
241 extern void hw_perf_restore(u64 ctrl);
242 extern int perf_counter_task_disable(void);
243 extern int perf_counter_task_enable(void);
244 extern int hw_perf_group_sched_in(struct perf_counter *group_leader,
245 struct perf_cpu_context *cpuctx,
246 struct perf_counter_context *ctx, int cpu);
249 * Return 1 for a software counter, 0 for a hardware counter
251 static inline int is_software_counter(struct perf_counter *counter)
253 return !counter->hw_event.raw && counter->hw_event.type < 0;
258 perf_counter_task_sched_in(struct task_struct *task, int cpu) { }
260 perf_counter_task_sched_out(struct task_struct *task, int cpu) { }
262 perf_counter_task_tick(struct task_struct *task, int cpu) { }
263 static inline void perf_counter_init_task(struct task_struct *child) { }
264 static inline void perf_counter_exit_task(struct task_struct *child) { }
265 static inline void perf_counter_notify(struct pt_regs *regs) { }
266 static inline void perf_counter_print_debug(void) { }
267 static inline void hw_perf_restore(u64 ctrl) { }
268 static inline u64 hw_perf_save_disable(void) { return 0; }
269 static inline int perf_counter_task_disable(void) { return -EINVAL; }
270 static inline int perf_counter_task_enable(void) { return -EINVAL; }
273 #endif /* _LINUX_PERF_COUNTER_H */