blob: f30486fc55d724a9ae9f1eccd22cee6340539cf2 [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>
17
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>
23
24struct task_struct;
25
26/*
Ingo Molnar9f66a382008-12-10 12:33:23 +010027 * User-space ABI bits:
28 */
29
30/*
31 * Generalized performance counter event types, used by the hw_event.type
32 * parameter of the sys_perf_counter_open() syscall:
Thomas Gleixner0793a612008-12-04 20:12:29 +010033 */
34enum hw_event_types {
Thomas Gleixner0793a612008-12-04 20:12:29 +010035 /*
Ingo Molnar9f66a382008-12-10 12:33:23 +010036 * Common hardware events, generalized by the kernel:
Thomas Gleixner0793a612008-12-04 20:12:29 +010037 */
Ingo Molnar9f66a382008-12-10 12:33:23 +010038 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,
44
Ingo Molnar6c594c22008-12-14 12:34:15 +010045 PERF_HW_EVENTS_MAX = 6,
46
Ingo Molnar9f66a382008-12-10 12:33:23 +010047 /*
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):
52 */
53 PERF_COUNT_CPU_CLOCK = -1,
54 PERF_COUNT_TASK_CLOCK = -2,
Ingo Molnar5d6a27d2008-12-14 12:28:33 +010055 PERF_COUNT_PAGE_FAULTS = -3,
56 PERF_COUNT_CONTEXT_SWITCHES = -4,
Ingo Molnar6c594c22008-12-14 12:34:15 +010057 PERF_COUNT_CPU_MIGRATIONS = -5,
58
59 PERF_SW_EVENTS_MIN = -6,
Thomas Gleixner0793a612008-12-04 20:12:29 +010060};
61
62/*
63 * IRQ-notification data record type:
64 */
Ingo Molnar9f66a382008-12-10 12:33:23 +010065enum perf_counter_record_type {
66 PERF_RECORD_SIMPLE = 0,
67 PERF_RECORD_IRQ = 1,
68 PERF_RECORD_GROUP = 2,
Thomas Gleixner0793a612008-12-04 20:12:29 +010069};
70
Ingo Molnar9f66a382008-12-10 12:33:23 +010071/*
72 * Hardware event to monitor via a performance monitoring counter:
73 */
74struct perf_counter_hw_event {
Ingo Molnar01b28382008-12-11 13:45:51 +010075 s64 type;
Ingo Molnar9f66a382008-12-10 12:33:23 +010076
77 u64 irq_period;
78 u32 record_type;
79
Ingo Molnar9b51f662008-12-12 13:49:45 +010080 u32 disabled : 1, /* off by default */
81 nmi : 1, /* NMI sampling */
82 raw : 1, /* raw event type */
83 inherit : 1, /* children inherit it */
84 __reserved_1 : 28;
Ingo Molnar9f66a382008-12-10 12:33:23 +010085
86 u64 __reserved_2;
Thomas Gleixnereab656a2008-12-08 19:26:59 +010087};
88
Ingo Molnar9f66a382008-12-10 12:33:23 +010089/*
90 * Kernel-internal data types:
91 */
92
Thomas Gleixner0793a612008-12-04 20:12:29 +010093/**
Ingo Molnar9f66a382008-12-10 12:33:23 +010094 * struct hw_perf_counter - performance counter hardware details:
Thomas Gleixner0793a612008-12-04 20:12:29 +010095 */
96struct hw_perf_counter {
Ingo Molnaree060942008-12-13 09:00:03 +010097#ifdef CONFIG_PERF_COUNTERS
Ingo Molnar9f66a382008-12-10 12:33:23 +010098 u64 config;
99 unsigned long config_base;
100 unsigned long counter_base;
101 int nmi;
102 unsigned int idx;
Ingo Molnaree060942008-12-13 09:00:03 +0100103 atomic64_t prev_count;
Ingo Molnar9f66a382008-12-10 12:33:23 +0100104 u64 irq_period;
Ingo Molnaree060942008-12-13 09:00:03 +0100105 atomic64_t period_left;
106#endif
Thomas Gleixner0793a612008-12-04 20:12:29 +0100107};
108
109/*
110 * Hardcoded buffer length limit for now, for IRQ-fed events:
111 */
Ingo Molnar9f66a382008-12-10 12:33:23 +0100112#define PERF_DATA_BUFLEN 2048
Thomas Gleixner0793a612008-12-04 20:12:29 +0100113
114/**
115 * struct perf_data - performance counter IRQ data sampling ...
116 */
117struct perf_data {
Ingo Molnar9f66a382008-12-10 12:33:23 +0100118 int len;
119 int rd_idx;
120 int overrun;
121 u8 data[PERF_DATA_BUFLEN];
Thomas Gleixner0793a612008-12-04 20:12:29 +0100122};
123
Ingo Molnar621a01e2008-12-11 12:46:46 +0100124struct perf_counter;
125
126/**
127 * struct hw_perf_counter_ops - performance counter hw ops
128 */
129struct 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);
133};
134
Thomas Gleixner0793a612008-12-04 20:12:29 +0100135/**
Ingo Molnar6a930702008-12-11 15:17:03 +0100136 * enum perf_counter_active_state - the states of a counter
137 */
138enum perf_counter_active_state {
139 PERF_COUNTER_STATE_OFF = -1,
140 PERF_COUNTER_STATE_INACTIVE = 0,
141 PERF_COUNTER_STATE_ACTIVE = 1,
142};
143
Ingo Molnar9b51f662008-12-12 13:49:45 +0100144struct file;
145
Ingo Molnar6a930702008-12-11 15:17:03 +0100146/**
Thomas Gleixner0793a612008-12-04 20:12:29 +0100147 * struct perf_counter - performance counter kernel representation:
148 */
149struct perf_counter {
Ingo Molnaree060942008-12-13 09:00:03 +0100150#ifdef CONFIG_PERF_COUNTERS
Ingo Molnar04289bb2008-12-11 08:38:42 +0100151 struct list_head list_entry;
152 struct list_head sibling_list;
153 struct perf_counter *group_leader;
Ingo Molnar5c92d122008-12-11 13:21:10 +0100154 const struct hw_perf_counter_ops *hw_ops;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100155
Ingo Molnar6a930702008-12-11 15:17:03 +0100156 enum perf_counter_active_state state;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100157 atomic64_t count;
Ingo Molnaree060942008-12-13 09:00:03 +0100158
Ingo Molnar9f66a382008-12-10 12:33:23 +0100159 struct perf_counter_hw_event hw_event;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100160 struct hw_perf_counter hw;
161
162 struct perf_counter_context *ctx;
163 struct task_struct *task;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100164 struct file *filp;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100165
Ingo Molnar9b51f662008-12-12 13:49:45 +0100166 unsigned int nr_inherited;
167 struct perf_counter *parent;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100168 /*
169 * Protect attach/detach:
170 */
171 struct mutex mutex;
172
173 int oncpu;
174 int cpu;
175
Thomas Gleixner0793a612008-12-04 20:12:29 +0100176 /* read() / irq related data */
177 wait_queue_head_t waitq;
178 /* optional: for NMIs */
179 int wakeup_pending;
180 struct perf_data *irqdata;
181 struct perf_data *usrdata;
182 struct perf_data data[2];
Ingo Molnaree060942008-12-13 09:00:03 +0100183#endif
Thomas Gleixner0793a612008-12-04 20:12:29 +0100184};
185
186/**
187 * struct perf_counter_context - counter context structure
188 *
189 * Used as a container for task counters and CPU counters as well:
190 */
191struct perf_counter_context {
192#ifdef CONFIG_PERF_COUNTERS
193 /*
194 * Protect the list of counters:
195 */
196 spinlock_t lock;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100197
198 struct list_head counter_list;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100199 int nr_counters;
200 int nr_active;
201 struct task_struct *task;
202#endif
203};
204
205/**
206 * struct perf_counter_cpu_context - per cpu counter context structure
207 */
208struct perf_cpu_context {
209 struct perf_counter_context ctx;
210 struct perf_counter_context *task_ctx;
211 int active_oncpu;
212 int max_pertask;
213};
214
215/*
216 * Set by architecture code:
217 */
218extern int perf_max_counters;
219
220#ifdef CONFIG_PERF_COUNTERS
Ingo Molnar9b51f662008-12-12 13:49:45 +0100221extern void
222perf_counter_show(struct perf_counter *counter, char *str, int trace);
Ingo Molnar5c92d122008-12-11 13:21:10 +0100223extern const struct hw_perf_counter_ops *
Ingo Molnar621a01e2008-12-11 12:46:46 +0100224hw_perf_counter_init(struct perf_counter *counter);
225
Thomas Gleixner0793a612008-12-04 20:12:29 +0100226extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
227extern void perf_counter_task_sched_out(struct task_struct *task, int cpu);
228extern void perf_counter_task_tick(struct task_struct *task, int cpu);
Ingo Molnar9b51f662008-12-12 13:49:45 +0100229extern void perf_counter_init_task(struct task_struct *child);
230extern void perf_counter_exit_task(struct task_struct *child);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100231extern void perf_counter_notify(struct pt_regs *regs);
232extern void perf_counter_print_debug(void);
Ingo Molnar01b28382008-12-11 13:45:51 +0100233extern u64 hw_perf_save_disable(void);
234extern void hw_perf_restore(u64 ctrl);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100235extern int perf_counter_task_disable(void);
236extern int perf_counter_task_enable(void);
Ingo Molnar5c92d122008-12-11 13:21:10 +0100237
Thomas Gleixner0793a612008-12-04 20:12:29 +0100238#else
239static inline void
Ingo Molnar9b51f662008-12-12 13:49:45 +0100240perf_counter_show(struct perf_counter *counter, char *str, int trace) { }
241static inline void
Thomas Gleixner0793a612008-12-04 20:12:29 +0100242perf_counter_task_sched_in(struct task_struct *task, int cpu) { }
243static inline void
244perf_counter_task_sched_out(struct task_struct *task, int cpu) { }
245static inline void
246perf_counter_task_tick(struct task_struct *task, int cpu) { }
Ingo Molnar9b51f662008-12-12 13:49:45 +0100247static inline void perf_counter_init_task(struct task_struct *child) { }
248static inline void perf_counter_exit_task(struct task_struct *child) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100249static inline void perf_counter_notify(struct pt_regs *regs) { }
250static inline void perf_counter_print_debug(void) { }
Ingo Molnar01b28382008-12-11 13:45:51 +0100251static inline void hw_perf_restore(u64 ctrl) { }
252static inline u64 hw_perf_save_disable(void) { return 0; }
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100253static inline int perf_counter_task_disable(void) { return -EINVAL; }
254static inline int perf_counter_task_enable(void) { return -EINVAL; }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100255#endif
256
257#endif /* _LINUX_PERF_COUNTER_H */