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 #include <linux/list.h>
19 #include <linux/mutex.h>
20 #include <linux/rculist.h>
21 #include <linux/rcupdate.h>
22 #include <linux/spinlock.h>
27 * User-space ABI bits:
31 * Generalized performance counter event types, used by the hw_event.type
32 * parameter of the sys_perf_counter_open() syscall:
36 * Common hardware events, generalized by the kernel:
38 PERF_COUNT_CYCLES = 0,
39 PERF_COUNT_INSTRUCTIONS = 1,
40 PERF_COUNT_CACHE_REFERENCES = 2,
41 PERF_COUNT_CACHE_MISSES = 3,
42 PERF_COUNT_BRANCH_INSTRUCTIONS = 4,
43 PERF_COUNT_BRANCH_MISSES = 5,
45 PERF_HW_EVENTS_MAX = 6,
48 * Special "software" counters provided by the kernel, even if
49 * the hardware does not support performance counters. These
50 * counters measure various physical and sw events of the
51 * kernel (and allow the profiling of them as well):
53 PERF_COUNT_CPU_CLOCK = -1,
54 PERF_COUNT_TASK_CLOCK = -2,
55 PERF_COUNT_PAGE_FAULTS = -3,
56 PERF_COUNT_CONTEXT_SWITCHES = -4,
57 PERF_COUNT_CPU_MIGRATIONS = -5,
59 PERF_SW_EVENTS_MIN = -6,
63 * IRQ-notification data record type:
65 enum perf_counter_record_type {
66 PERF_RECORD_SIMPLE = 0,
68 PERF_RECORD_GROUP = 2,
72 * Hardware event to monitor via a performance monitoring counter:
74 struct perf_counter_hw_event {
80 u32 disabled : 1, /* off by default */
81 nmi : 1, /* NMI sampling */
82 raw : 1, /* raw event type */
83 inherit : 1, /* children inherit it */
90 * Kernel-internal data types:
94 * struct hw_perf_counter - performance counter hardware details:
96 struct hw_perf_counter {
97 #ifdef CONFIG_PERF_COUNTERS
99 unsigned long config_base;
100 unsigned long counter_base;
103 atomic64_t prev_count;
105 atomic64_t period_left;
110 * Hardcoded buffer length limit for now, for IRQ-fed events:
112 #define PERF_DATA_BUFLEN 2048
115 * struct perf_data - performance counter IRQ data sampling ...
121 u8 data[PERF_DATA_BUFLEN];
127 * struct hw_perf_counter_ops - performance counter hw ops
129 struct hw_perf_counter_ops {
130 void (*hw_perf_counter_enable) (struct perf_counter *counter);
131 void (*hw_perf_counter_disable) (struct perf_counter *counter);
132 void (*hw_perf_counter_read) (struct perf_counter *counter);
136 * enum perf_counter_active_state - the states of a counter
138 enum perf_counter_active_state {
139 PERF_COUNTER_STATE_OFF = -1,
140 PERF_COUNTER_STATE_INACTIVE = 0,
141 PERF_COUNTER_STATE_ACTIVE = 1,
147 * struct perf_counter - performance counter kernel representation:
149 struct perf_counter {
150 #ifdef CONFIG_PERF_COUNTERS
151 struct list_head list_entry;
152 struct list_head sibling_list;
153 struct perf_counter *group_leader;
154 const struct hw_perf_counter_ops *hw_ops;
156 enum perf_counter_active_state state;
159 struct perf_counter_hw_event hw_event;
160 struct hw_perf_counter hw;
162 struct perf_counter_context *ctx;
163 struct task_struct *task;
166 unsigned int nr_inherited;
167 struct perf_counter *parent;
169 * Protect attach/detach:
176 /* read() / irq related data */
177 wait_queue_head_t waitq;
178 /* optional: for NMIs */
180 struct perf_data *irqdata;
181 struct perf_data *usrdata;
182 struct perf_data data[2];
187 * struct perf_counter_context - counter context structure
189 * Used as a container for task counters and CPU counters as well:
191 struct perf_counter_context {
192 #ifdef CONFIG_PERF_COUNTERS
194 * Protect the list of counters:
198 struct list_head counter_list;
201 struct task_struct *task;
206 * struct perf_counter_cpu_context - per cpu counter context structure
208 struct perf_cpu_context {
209 struct perf_counter_context ctx;
210 struct perf_counter_context *task_ctx;
216 * Set by architecture code:
218 extern int perf_max_counters;
220 #ifdef CONFIG_PERF_COUNTERS
222 perf_counter_show(struct perf_counter *counter, char *str, int trace);
223 extern const struct hw_perf_counter_ops *
224 hw_perf_counter_init(struct perf_counter *counter);
226 extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
227 extern void perf_counter_task_sched_out(struct task_struct *task, int cpu);
228 extern void perf_counter_task_tick(struct task_struct *task, int cpu);
229 extern void perf_counter_init_task(struct task_struct *child);
230 extern void perf_counter_exit_task(struct task_struct *child);
231 extern void perf_counter_notify(struct pt_regs *regs);
232 extern void perf_counter_print_debug(void);
233 extern u64 hw_perf_save_disable(void);
234 extern void hw_perf_restore(u64 ctrl);
235 extern int perf_counter_task_disable(void);
236 extern int perf_counter_task_enable(void);
240 perf_counter_show(struct perf_counter *counter, char *str, int trace) { }
242 perf_counter_task_sched_in(struct task_struct *task, int cpu) { }
244 perf_counter_task_sched_out(struct task_struct *task, int cpu) { }
246 perf_counter_task_tick(struct task_struct *task, int cpu) { }
247 static inline void perf_counter_init_task(struct task_struct *child) { }
248 static inline void perf_counter_exit_task(struct task_struct *child) { }
249 static inline void perf_counter_notify(struct pt_regs *regs) { }
250 static inline void perf_counter_print_debug(void) { }
251 static inline void hw_perf_restore(u64 ctrl) { }
252 static inline u64 hw_perf_save_disable(void) { return 0; }
253 static inline int perf_counter_task_disable(void) { return -EINVAL; }
254 static inline int perf_counter_task_enable(void) { return -EINVAL; }
257 #endif /* _LINUX_PERF_COUNTER_H */