perfcounters: ratelimit performance counter interrupts
[platform/kernel/linux-starfive.git] / include / linux / perf_counter.h
1 /*
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 #include <asm/ioctl.h>
18
19 #ifdef CONFIG_PERF_COUNTERS
20 # include <asm/perf_counter.h>
21 #endif
22
23 #include <linux/list.h>
24 #include <linux/mutex.h>
25 #include <linux/rculist.h>
26 #include <linux/rcupdate.h>
27 #include <linux/spinlock.h>
28
29 struct task_struct;
30
31 /*
32  * User-space ABI bits:
33  */
34
35 /*
36  * Generalized performance counter event types, used by the hw_event.type
37  * parameter of the sys_perf_counter_open() syscall:
38  */
39 enum hw_event_types {
40         /*
41          * Common hardware events, generalized by the kernel:
42          */
43         PERF_COUNT_CPU_CYCLES           =  0,
44         PERF_COUNT_INSTRUCTIONS         =  1,
45         PERF_COUNT_CACHE_REFERENCES     =  2,
46         PERF_COUNT_CACHE_MISSES         =  3,
47         PERF_COUNT_BRANCH_INSTRUCTIONS  =  4,
48         PERF_COUNT_BRANCH_MISSES        =  5,
49         PERF_COUNT_BUS_CYCLES           =  6,
50
51         PERF_HW_EVENTS_MAX              =  7,
52
53         /*
54          * Special "software" counters provided by the kernel, even if
55          * the hardware does not support performance counters. These
56          * counters measure various physical and sw events of the
57          * kernel (and allow the profiling of them as well):
58          */
59         PERF_COUNT_CPU_CLOCK            = -1,
60         PERF_COUNT_TASK_CLOCK           = -2,
61         PERF_COUNT_PAGE_FAULTS          = -3,
62         PERF_COUNT_CONTEXT_SWITCHES     = -4,
63         PERF_COUNT_CPU_MIGRATIONS       = -5,
64
65         PERF_SW_EVENTS_MIN              = -6,
66 };
67
68 /*
69  * IRQ-notification data record type:
70  */
71 enum perf_counter_record_type {
72         PERF_RECORD_SIMPLE              =  0,
73         PERF_RECORD_IRQ                 =  1,
74         PERF_RECORD_GROUP               =  2,
75 };
76
77 /*
78  * Hardware event to monitor via a performance monitoring counter:
79  */
80 struct perf_counter_hw_event {
81         s64                     type;
82
83         u64                     irq_period;
84         u32                     record_type;
85
86         u32                     disabled     :  1, /* off by default      */
87                                 nmi          :  1, /* NMI sampling        */
88                                 raw          :  1, /* raw event type      */
89                                 inherit      :  1, /* children inherit it */
90                                 pinned       :  1, /* must always be on PMU */
91                                 exclusive    :  1, /* only counter on PMU */
92
93                                 __reserved_1 : 26;
94
95         u64                     __reserved_2;
96 };
97
98 /*
99  * Ioctls that can be done on a perf counter fd:
100  */
101 #define PERF_COUNTER_IOC_ENABLE         _IO('$', 0)
102 #define PERF_COUNTER_IOC_DISABLE        _IO('$', 1)
103
104 /*
105  * Kernel-internal data types:
106  */
107
108 /**
109  * struct hw_perf_counter - performance counter hardware details:
110  */
111 struct hw_perf_counter {
112 #ifdef CONFIG_PERF_COUNTERS
113         u64                             config;
114         unsigned long                   config_base;
115         unsigned long                   counter_base;
116         int                             nmi;
117         unsigned int                    idx;
118         atomic64_t                      prev_count;
119         u64                             irq_period;
120         atomic64_t                      period_left;
121 #endif
122 };
123
124 /*
125  * Hardcoded buffer length limit for now, for IRQ-fed events:
126  */
127 #define PERF_DATA_BUFLEN                2048
128
129 /**
130  * struct perf_data - performance counter IRQ data sampling ...
131  */
132 struct perf_data {
133         int                             len;
134         int                             rd_idx;
135         int                             overrun;
136         u8                              data[PERF_DATA_BUFLEN];
137 };
138
139 struct perf_counter;
140
141 /**
142  * struct hw_perf_counter_ops - performance counter hw ops
143  */
144 struct hw_perf_counter_ops {
145         int (*enable)                   (struct perf_counter *counter);
146         void (*disable)                 (struct perf_counter *counter);
147         void (*read)                    (struct perf_counter *counter);
148 };
149
150 /**
151  * enum perf_counter_active_state - the states of a counter
152  */
153 enum perf_counter_active_state {
154         PERF_COUNTER_STATE_ERROR        = -2,
155         PERF_COUNTER_STATE_OFF          = -1,
156         PERF_COUNTER_STATE_INACTIVE     =  0,
157         PERF_COUNTER_STATE_ACTIVE       =  1,
158 };
159
160 struct file;
161
162 /**
163  * struct perf_counter - performance counter kernel representation:
164  */
165 struct perf_counter {
166 #ifdef CONFIG_PERF_COUNTERS
167         struct list_head                list_entry;
168         struct list_head                sibling_list;
169         struct perf_counter             *group_leader;
170         const struct hw_perf_counter_ops *hw_ops;
171
172         enum perf_counter_active_state  state;
173         atomic64_t                      count;
174
175         struct perf_counter_hw_event    hw_event;
176         struct hw_perf_counter          hw;
177
178         struct perf_counter_context     *ctx;
179         struct task_struct              *task;
180         struct file                     *filp;
181
182         struct perf_counter             *parent;
183         struct list_head                child_list;
184
185         /*
186          * Protect attach/detach and child_list:
187          */
188         struct mutex                    mutex;
189
190         int                             oncpu;
191         int                             cpu;
192
193         /* read() / irq related data */
194         wait_queue_head_t               waitq;
195         /* optional: for NMIs */
196         int                             wakeup_pending;
197         struct perf_data                *irqdata;
198         struct perf_data                *usrdata;
199         struct perf_data                data[2];
200 #endif
201 };
202
203 /**
204  * struct perf_counter_context - counter context structure
205  *
206  * Used as a container for task counters and CPU counters as well:
207  */
208 struct perf_counter_context {
209 #ifdef CONFIG_PERF_COUNTERS
210         /*
211          * Protect the states of the counters in the list,
212          * nr_active, and the list:
213          */
214         spinlock_t              lock;
215         /*
216          * Protect the list of counters.  Locking either mutex or lock
217          * is sufficient to ensure the list doesn't change; to change
218          * the list you need to lock both the mutex and the spinlock.
219          */
220         struct mutex            mutex;
221
222         struct list_head        counter_list;
223         int                     nr_counters;
224         int                     nr_active;
225         int                     is_active;
226         struct task_struct      *task;
227 #endif
228 };
229
230 /**
231  * struct perf_counter_cpu_context - per cpu counter context structure
232  */
233 struct perf_cpu_context {
234         struct perf_counter_context     ctx;
235         struct perf_counter_context     *task_ctx;
236         int                             active_oncpu;
237         int                             max_pertask;
238         int                             exclusive;
239 };
240
241 /*
242  * Set by architecture code:
243  */
244 extern int perf_max_counters;
245
246 #ifdef CONFIG_PERF_COUNTERS
247 extern const struct hw_perf_counter_ops *
248 hw_perf_counter_init(struct perf_counter *counter);
249
250 extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
251 extern void perf_counter_task_sched_out(struct task_struct *task, int cpu);
252 extern void perf_counter_task_tick(struct task_struct *task, int cpu);
253 extern void perf_counter_init_task(struct task_struct *child);
254 extern void perf_counter_exit_task(struct task_struct *child);
255 extern void perf_counter_notify(struct pt_regs *regs);
256 extern void perf_counter_print_debug(void);
257 extern void perf_counter_unthrottle(void);
258 extern u64 hw_perf_save_disable(void);
259 extern void hw_perf_restore(u64 ctrl);
260 extern int perf_counter_task_disable(void);
261 extern int perf_counter_task_enable(void);
262 extern int hw_perf_group_sched_in(struct perf_counter *group_leader,
263                struct perf_cpu_context *cpuctx,
264                struct perf_counter_context *ctx, int cpu);
265
266 /*
267  * Return 1 for a software counter, 0 for a hardware counter
268  */
269 static inline int is_software_counter(struct perf_counter *counter)
270 {
271         return !counter->hw_event.raw && counter->hw_event.type < 0;
272 }
273
274 #else
275 static inline void
276 perf_counter_task_sched_in(struct task_struct *task, int cpu)           { }
277 static inline void
278 perf_counter_task_sched_out(struct task_struct *task, int cpu)          { }
279 static inline void
280 perf_counter_task_tick(struct task_struct *task, int cpu)               { }
281 static inline void perf_counter_init_task(struct task_struct *child)    { }
282 static inline void perf_counter_exit_task(struct task_struct *child)    { }
283 static inline void perf_counter_notify(struct pt_regs *regs)            { }
284 static inline void perf_counter_print_debug(void)                       { }
285 static inline void perf_counter_unthrottle(void)                        { }
286 static inline void hw_perf_restore(u64 ctrl)                    { }
287 static inline u64 hw_perf_save_disable(void)                  { return 0; }
288 static inline int perf_counter_task_disable(void)       { return -EINVAL; }
289 static inline int perf_counter_task_enable(void)        { return -EINVAL; }
290 #endif
291
292 #endif /* _LINUX_PERF_COUNTER_H */