blob: 48f76d2e54c2a83d74b5dedcc63d361ce47bc379 [file] [log] [blame]
Thomas Gleixner0793a612008-12-04 20:12:29 +01001/*
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
16#include <asm/atomic.h>
Ingo Molnareb2b8612008-12-17 09:09:13 +010017#include <asm/perf_counter.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010018
19#include <linux/list.h>
20#include <linux/mutex.h>
21#include <linux/rculist.h>
22#include <linux/rcupdate.h>
23#include <linux/spinlock.h>
24
25struct task_struct;
26
27/*
Ingo Molnar9f66a382008-12-10 12:33:23 +010028 * User-space ABI bits:
29 */
30
31/*
32 * Generalized performance counter event types, used by the hw_event.type
33 * parameter of the sys_perf_counter_open() syscall:
Thomas Gleixner0793a612008-12-04 20:12:29 +010034 */
35enum hw_event_types {
Thomas Gleixner0793a612008-12-04 20:12:29 +010036 /*
Ingo Molnar9f66a382008-12-10 12:33:23 +010037 * Common hardware events, generalized by the kernel:
Thomas Gleixner0793a612008-12-04 20:12:29 +010038 */
Ingo Molnar9f66a382008-12-10 12:33:23 +010039 PERF_COUNT_CYCLES = 0,
40 PERF_COUNT_INSTRUCTIONS = 1,
41 PERF_COUNT_CACHE_REFERENCES = 2,
42 PERF_COUNT_CACHE_MISSES = 3,
43 PERF_COUNT_BRANCH_INSTRUCTIONS = 4,
44 PERF_COUNT_BRANCH_MISSES = 5,
45
Ingo Molnar6c594c22008-12-14 12:34:15 +010046 PERF_HW_EVENTS_MAX = 6,
47
Ingo Molnar9f66a382008-12-10 12:33:23 +010048 /*
49 * Special "software" counters provided by the kernel, even if
50 * the hardware does not support performance counters. These
51 * counters measure various physical and sw events of the
52 * kernel (and allow the profiling of them as well):
53 */
54 PERF_COUNT_CPU_CLOCK = -1,
55 PERF_COUNT_TASK_CLOCK = -2,
Ingo Molnar5d6a27d2008-12-14 12:28:33 +010056 PERF_COUNT_PAGE_FAULTS = -3,
57 PERF_COUNT_CONTEXT_SWITCHES = -4,
Ingo Molnar6c594c22008-12-14 12:34:15 +010058 PERF_COUNT_CPU_MIGRATIONS = -5,
59
60 PERF_SW_EVENTS_MIN = -6,
Thomas Gleixner0793a612008-12-04 20:12:29 +010061};
62
63/*
64 * IRQ-notification data record type:
65 */
Ingo Molnar9f66a382008-12-10 12:33:23 +010066enum perf_counter_record_type {
67 PERF_RECORD_SIMPLE = 0,
68 PERF_RECORD_IRQ = 1,
69 PERF_RECORD_GROUP = 2,
Thomas Gleixner0793a612008-12-04 20:12:29 +010070};
71
Ingo Molnar9f66a382008-12-10 12:33:23 +010072/*
73 * Hardware event to monitor via a performance monitoring counter:
74 */
75struct perf_counter_hw_event {
Ingo Molnar01b28382008-12-11 13:45:51 +010076 s64 type;
Ingo Molnar9f66a382008-12-10 12:33:23 +010077
78 u64 irq_period;
79 u32 record_type;
80
Ingo Molnar9b51f662008-12-12 13:49:45 +010081 u32 disabled : 1, /* off by default */
82 nmi : 1, /* NMI sampling */
83 raw : 1, /* raw event type */
84 inherit : 1, /* children inherit it */
85 __reserved_1 : 28;
Ingo Molnar9f66a382008-12-10 12:33:23 +010086
87 u64 __reserved_2;
Thomas Gleixnereab656a2008-12-08 19:26:59 +010088};
89
Ingo Molnar9f66a382008-12-10 12:33:23 +010090/*
91 * Kernel-internal data types:
92 */
93
Thomas Gleixner0793a612008-12-04 20:12:29 +010094/**
Ingo Molnar9f66a382008-12-10 12:33:23 +010095 * struct hw_perf_counter - performance counter hardware details:
Thomas Gleixner0793a612008-12-04 20:12:29 +010096 */
97struct hw_perf_counter {
Ingo Molnaree060942008-12-13 09:00:03 +010098#ifdef CONFIG_PERF_COUNTERS
Ingo Molnar9f66a382008-12-10 12:33:23 +010099 u64 config;
100 unsigned long config_base;
101 unsigned long counter_base;
102 int nmi;
103 unsigned int idx;
Ingo Molnaree060942008-12-13 09:00:03 +0100104 atomic64_t prev_count;
Ingo Molnar9f66a382008-12-10 12:33:23 +0100105 u64 irq_period;
Ingo Molnaree060942008-12-13 09:00:03 +0100106 atomic64_t period_left;
107#endif
Thomas Gleixner0793a612008-12-04 20:12:29 +0100108};
109
110/*
111 * Hardcoded buffer length limit for now, for IRQ-fed events:
112 */
Ingo Molnar9f66a382008-12-10 12:33:23 +0100113#define PERF_DATA_BUFLEN 2048
Thomas Gleixner0793a612008-12-04 20:12:29 +0100114
115/**
116 * struct perf_data - performance counter IRQ data sampling ...
117 */
118struct perf_data {
Ingo Molnar9f66a382008-12-10 12:33:23 +0100119 int len;
120 int rd_idx;
121 int overrun;
122 u8 data[PERF_DATA_BUFLEN];
Thomas Gleixner0793a612008-12-04 20:12:29 +0100123};
124
Ingo Molnar621a01e2008-12-11 12:46:46 +0100125struct perf_counter;
126
127/**
128 * struct hw_perf_counter_ops - performance counter hw ops
129 */
130struct hw_perf_counter_ops {
Ingo Molnar76715812008-12-17 14:20:28 +0100131 void (*enable) (struct perf_counter *counter);
132 void (*disable) (struct perf_counter *counter);
133 void (*read) (struct perf_counter *counter);
Ingo Molnar621a01e2008-12-11 12:46:46 +0100134};
135
Thomas Gleixner0793a612008-12-04 20:12:29 +0100136/**
Ingo Molnar6a930702008-12-11 15:17:03 +0100137 * enum perf_counter_active_state - the states of a counter
138 */
139enum perf_counter_active_state {
140 PERF_COUNTER_STATE_OFF = -1,
141 PERF_COUNTER_STATE_INACTIVE = 0,
142 PERF_COUNTER_STATE_ACTIVE = 1,
143};
144
Ingo Molnar9b51f662008-12-12 13:49:45 +0100145struct file;
146
Ingo Molnar6a930702008-12-11 15:17:03 +0100147/**
Thomas Gleixner0793a612008-12-04 20:12:29 +0100148 * struct perf_counter - performance counter kernel representation:
149 */
150struct perf_counter {
Ingo Molnaree060942008-12-13 09:00:03 +0100151#ifdef CONFIG_PERF_COUNTERS
Ingo Molnar04289bb2008-12-11 08:38:42 +0100152 struct list_head list_entry;
153 struct list_head sibling_list;
154 struct perf_counter *group_leader;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100155 const struct hw_perf_counter_ops *hw_ops;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100156
Ingo Molnar6a930702008-12-11 15:17:03 +0100157 enum perf_counter_active_state state;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100158 atomic64_t count;
Ingo Molnaree060942008-12-13 09:00:03 +0100159
Ingo Molnar9f66a382008-12-10 12:33:23 +0100160 struct perf_counter_hw_event hw_event;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100161 struct hw_perf_counter hw;
162
163 struct perf_counter_context *ctx;
164 struct task_struct *task;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100165 struct file *filp;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100166
Ingo Molnar9b51f662008-12-12 13:49:45 +0100167 unsigned int nr_inherited;
168 struct perf_counter *parent;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100169 /*
170 * Protect attach/detach:
171 */
172 struct mutex mutex;
173
174 int oncpu;
175 int cpu;
176
Thomas Gleixner0793a612008-12-04 20:12:29 +0100177 /* read() / irq related data */
178 wait_queue_head_t waitq;
179 /* optional: for NMIs */
180 int wakeup_pending;
181 struct perf_data *irqdata;
182 struct perf_data *usrdata;
183 struct perf_data data[2];
Ingo Molnaree060942008-12-13 09:00:03 +0100184#endif
Thomas Gleixner0793a612008-12-04 20:12:29 +0100185};
186
187/**
188 * struct perf_counter_context - counter context structure
189 *
190 * Used as a container for task counters and CPU counters as well:
191 */
192struct perf_counter_context {
193#ifdef CONFIG_PERF_COUNTERS
194 /*
195 * Protect the list of counters:
196 */
197 spinlock_t lock;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100198
199 struct list_head counter_list;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100200 int nr_counters;
201 int nr_active;
202 struct task_struct *task;
203#endif
204};
205
206/**
207 * struct perf_counter_cpu_context - per cpu counter context structure
208 */
209struct perf_cpu_context {
210 struct perf_counter_context ctx;
211 struct perf_counter_context *task_ctx;
212 int active_oncpu;
213 int max_pertask;
214};
215
216/*
217 * Set by architecture code:
218 */
219extern int perf_max_counters;
220
221#ifdef CONFIG_PERF_COUNTERS
Ingo Molnar5c92d122008-12-11 13:21:10 +0100222extern const struct hw_perf_counter_ops *
Ingo Molnar621a01e2008-12-11 12:46:46 +0100223hw_perf_counter_init(struct perf_counter *counter);
224
Thomas Gleixner0793a612008-12-04 20:12:29 +0100225extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
226extern void perf_counter_task_sched_out(struct task_struct *task, int cpu);
227extern void perf_counter_task_tick(struct task_struct *task, int cpu);
Ingo Molnar9b51f662008-12-12 13:49:45 +0100228extern void perf_counter_init_task(struct task_struct *child);
229extern void perf_counter_exit_task(struct task_struct *child);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100230extern void perf_counter_notify(struct pt_regs *regs);
231extern void perf_counter_print_debug(void);
Ingo Molnar01b28382008-12-11 13:45:51 +0100232extern u64 hw_perf_save_disable(void);
233extern void hw_perf_restore(u64 ctrl);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100234extern int perf_counter_task_disable(void);
235extern int perf_counter_task_enable(void);
Ingo Molnar5c92d122008-12-11 13:21:10 +0100236
Thomas Gleixner0793a612008-12-04 20:12:29 +0100237#else
238static inline void
239perf_counter_task_sched_in(struct task_struct *task, int cpu) { }
240static inline void
241perf_counter_task_sched_out(struct task_struct *task, int cpu) { }
242static inline void
243perf_counter_task_tick(struct task_struct *task, int cpu) { }
Ingo Molnar9b51f662008-12-12 13:49:45 +0100244static inline void perf_counter_init_task(struct task_struct *child) { }
245static inline void perf_counter_exit_task(struct task_struct *child) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100246static inline void perf_counter_notify(struct pt_regs *regs) { }
247static inline void perf_counter_print_debug(void) { }
Ingo Molnar01b28382008-12-11 13:45:51 +0100248static inline void hw_perf_restore(u64 ctrl) { }
249static inline u64 hw_perf_save_disable(void) { return 0; }
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100250static inline int perf_counter_task_disable(void) { return -EINVAL; }
251static inline int perf_counter_task_enable(void) { return -EINVAL; }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100252#endif
253
254#endif /* _LINUX_PERF_COUNTER_H */