blob: d5911b02bc8c36430db4927798afbe73388d9fe1 [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
Paul Mackerrasf3dfd262009-02-26 22:43:46 +110016#include <linux/types.h>
17#include <linux/ioctl.h>
Paul Mackerras9aaa1312009-03-21 15:31:47 +110018#include <asm/byteorder.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010019
20/*
Ingo Molnar9f66a382008-12-10 12:33:23 +010021 * User-space ABI bits:
22 */
23
24/*
Peter Zijlstra0d486962009-06-02 19:22:16 +020025 * attr.type
Peter Zijlstrab8e83512009-03-19 20:26:18 +010026 */
Peter Zijlstra1c432d82009-06-11 13:19:29 +020027enum perf_type_id {
Peter Zijlstrab8e83512009-03-19 20:26:18 +010028 PERF_TYPE_HARDWARE = 0,
29 PERF_TYPE_SOFTWARE = 1,
30 PERF_TYPE_TRACEPOINT = 2,
Ingo Molnar8326f442009-06-05 20:22:46 +020031 PERF_TYPE_HW_CACHE = 3,
Peter Zijlstra1c432d82009-06-11 13:19:29 +020032 PERF_TYPE_RAW = 4,
Peter Zijlstrab8e83512009-03-19 20:26:18 +010033
Peter Zijlstra1c432d82009-06-11 13:19:29 +020034 PERF_TYPE_MAX, /* non ABI */
Peter Zijlstrab8e83512009-03-19 20:26:18 +010035};
36
37/*
Peter Zijlstra0d486962009-06-02 19:22:16 +020038 * Generalized performance counter event types, used by the attr.event_id
Ingo Molnar9f66a382008-12-10 12:33:23 +010039 * parameter of the sys_perf_counter_open() syscall:
Thomas Gleixner0793a612008-12-04 20:12:29 +010040 */
Peter Zijlstra1c432d82009-06-11 13:19:29 +020041enum perf_hw_id {
Thomas Gleixner0793a612008-12-04 20:12:29 +010042 /*
Ingo Molnar9f66a382008-12-10 12:33:23 +010043 * Common hardware events, generalized by the kernel:
Thomas Gleixner0793a612008-12-04 20:12:29 +010044 */
Peter Zijlstrab8e83512009-03-19 20:26:18 +010045 PERF_COUNT_CPU_CYCLES = 0,
46 PERF_COUNT_INSTRUCTIONS = 1,
47 PERF_COUNT_CACHE_REFERENCES = 2,
48 PERF_COUNT_CACHE_MISSES = 3,
49 PERF_COUNT_BRANCH_INSTRUCTIONS = 4,
50 PERF_COUNT_BRANCH_MISSES = 5,
51 PERF_COUNT_BUS_CYCLES = 6,
Ingo Molnar9f66a382008-12-10 12:33:23 +010052
Peter Zijlstra1c432d82009-06-11 13:19:29 +020053 PERF_HW_EVENTS_MAX, /* non ABI */
Peter Zijlstrab8e83512009-03-19 20:26:18 +010054};
Ingo Molnar6c594c22008-12-14 12:34:15 +010055
Peter Zijlstrab8e83512009-03-19 20:26:18 +010056/*
Ingo Molnar8326f442009-06-05 20:22:46 +020057 * Generalized hardware cache counters:
58 *
59 * { L1-D, L1-I, L2, LLC, ITLB, DTLB, BPU } x
60 * { read, write, prefetch } x
61 * { accesses, misses }
62 */
Peter Zijlstra1c432d82009-06-11 13:19:29 +020063enum perf_hw_cache_id {
64 PERF_COUNT_HW_CACHE_L1D = 0,
65 PERF_COUNT_HW_CACHE_L1I = 1,
66 PERF_COUNT_HW_CACHE_L2 = 2,
67 PERF_COUNT_HW_CACHE_DTLB = 3,
68 PERF_COUNT_HW_CACHE_ITLB = 4,
69 PERF_COUNT_HW_CACHE_BPU = 5,
Ingo Molnar8326f442009-06-05 20:22:46 +020070
Peter Zijlstra1c432d82009-06-11 13:19:29 +020071 PERF_COUNT_HW_CACHE_MAX, /* non ABI */
Ingo Molnar8326f442009-06-05 20:22:46 +020072};
73
Peter Zijlstra1c432d82009-06-11 13:19:29 +020074enum perf_hw_cache_op_id {
75 PERF_COUNT_HW_CACHE_OP_READ = 0,
76 PERF_COUNT_HW_CACHE_OP_WRITE = 1,
77 PERF_COUNT_HW_CACHE_OP_PREFETCH = 2,
Ingo Molnar8326f442009-06-05 20:22:46 +020078
Peter Zijlstra1c432d82009-06-11 13:19:29 +020079 PERF_COUNT_HW_CACHE_OP_MAX, /* non ABI */
Ingo Molnar8326f442009-06-05 20:22:46 +020080};
81
Peter Zijlstra1c432d82009-06-11 13:19:29 +020082enum perf_hw_cache_op_result_id {
83 PERF_COUNT_HW_CACHE_RESULT_ACCESS = 0,
84 PERF_COUNT_HW_CACHE_RESULT_MISS = 1,
Ingo Molnar8326f442009-06-05 20:22:46 +020085
Peter Zijlstra1c432d82009-06-11 13:19:29 +020086 PERF_COUNT_HW_CACHE_RESULT_MAX, /* non ABI */
Ingo Molnar8326f442009-06-05 20:22:46 +020087};
88
89/*
Peter Zijlstrab8e83512009-03-19 20:26:18 +010090 * Special "software" counters provided by the kernel, even if the hardware
91 * does not support performance counters. These counters measure various
92 * physical and sw events of the kernel (and allow the profiling of them as
93 * well):
94 */
Peter Zijlstra1c432d82009-06-11 13:19:29 +020095enum perf_sw_ids {
Peter Zijlstrab8e83512009-03-19 20:26:18 +010096 PERF_COUNT_CPU_CLOCK = 0,
97 PERF_COUNT_TASK_CLOCK = 1,
98 PERF_COUNT_PAGE_FAULTS = 2,
99 PERF_COUNT_CONTEXT_SWITCHES = 3,
100 PERF_COUNT_CPU_MIGRATIONS = 4,
101 PERF_COUNT_PAGE_FAULTS_MIN = 5,
102 PERF_COUNT_PAGE_FAULTS_MAJ = 6,
Ingo Molnar6c594c22008-12-14 12:34:15 +0100103
Peter Zijlstra1c432d82009-06-11 13:19:29 +0200104 PERF_SW_EVENTS_MAX, /* non ABI */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100105};
106
Ingo Molnar9f66a382008-12-10 12:33:23 +0100107/*
Peter Zijlstra0d486962009-06-02 19:22:16 +0200108 * Bits that can be set in attr.sample_type to request information
Peter Zijlstra8a057d82009-04-02 11:11:59 +0200109 * in the overflow packets.
110 */
Peter Zijlstrab23f3322009-06-02 15:13:03 +0200111enum perf_counter_sample_format {
112 PERF_SAMPLE_IP = 1U << 0,
113 PERF_SAMPLE_TID = 1U << 1,
114 PERF_SAMPLE_TIME = 1U << 2,
115 PERF_SAMPLE_ADDR = 1U << 3,
116 PERF_SAMPLE_GROUP = 1U << 4,
117 PERF_SAMPLE_CALLCHAIN = 1U << 5,
Peter Zijlstraac4bcf82009-06-05 14:44:52 +0200118 PERF_SAMPLE_ID = 1U << 6,
Peter Zijlstrab23f3322009-06-02 15:13:03 +0200119 PERF_SAMPLE_CPU = 1U << 7,
Peter Zijlstra689802b2009-06-05 15:05:43 +0200120 PERF_SAMPLE_PERIOD = 1U << 8,
Peter Zijlstra8a057d82009-04-02 11:11:59 +0200121};
122
123/*
Peter Zijlstra0d486962009-06-02 19:22:16 +0200124 * Bits that can be set in attr.read_format to request that
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100125 * reads on the counter should return the indicated quantities,
126 * in increasing order of bit value, after the counter value.
127 */
128enum perf_counter_read_format {
Peter Zijlstra8e5799b2009-06-02 15:08:15 +0200129 PERF_FORMAT_TOTAL_TIME_ENABLED = 1U << 0,
130 PERF_FORMAT_TOTAL_TIME_RUNNING = 1U << 1,
131 PERF_FORMAT_ID = 1U << 2,
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100132};
133
134/*
Ingo Molnar9f66a382008-12-10 12:33:23 +0100135 * Hardware event to monitor via a performance monitoring counter:
136 */
Peter Zijlstra0d486962009-06-02 19:22:16 +0200137struct perf_counter_attr {
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +0100138 /*
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200139 * Major type: hardware/software/tracepoint/etc.
140 */
141 __u32 type;
142 __u32 __reserved_1;
143
144 /*
145 * Type specific configuration information.
Peter Zijlstraf4a2deb42009-03-23 18:22:06 +0100146 */
147 __u64 config;
Ingo Molnar9f66a382008-12-10 12:33:23 +0100148
Peter Zijlstra60db5e02009-05-15 15:19:28 +0200149 union {
Peter Zijlstrab23f3322009-06-02 15:13:03 +0200150 __u64 sample_period;
151 __u64 sample_freq;
Peter Zijlstra60db5e02009-05-15 15:19:28 +0200152 };
153
Peter Zijlstrab23f3322009-06-02 15:13:03 +0200154 __u64 sample_type;
155 __u64 read_format;
Ingo Molnar9f66a382008-12-10 12:33:23 +0100156
Paul Mackerras2743a5b2009-03-04 20:36:51 +1100157 __u64 disabled : 1, /* off by default */
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100158 inherit : 1, /* children inherit it */
159 pinned : 1, /* must always be on PMU */
160 exclusive : 1, /* only group on PMU */
161 exclude_user : 1, /* don't count user */
162 exclude_kernel : 1, /* ditto kernel */
163 exclude_hv : 1, /* ditto hypervisor */
Paul Mackerras2743a5b2009-03-04 20:36:51 +1100164 exclude_idle : 1, /* don't count when idle */
Peter Zijlstra0a4a9392009-03-30 19:07:05 +0200165 mmap : 1, /* include mmap data */
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +0200166 comm : 1, /* include comm data */
Peter Zijlstra60db5e02009-05-15 15:19:28 +0200167 freq : 1, /* use freq, not period */
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100168
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200169 __reserved_2 : 53;
Paul Mackerras2743a5b2009-03-04 20:36:51 +1100170
Peter Zijlstrac4578102009-04-02 11:12:01 +0200171 __u32 wakeup_events; /* wakeup every n events */
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200172 __u32 __reserved_3;
Ingo Molnar9f66a382008-12-10 12:33:23 +0100173
Peter Zijlstrae527ea32009-05-25 14:45:25 +0200174 __u64 __reserved_4;
Thomas Gleixnereab656a2008-12-08 19:26:59 +0100175};
176
Ingo Molnar9f66a382008-12-10 12:33:23 +0100177/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100178 * Ioctls that can be done on a perf counter fd:
179 */
Peter Zijlstra08247e32009-06-02 16:46:57 +0200180#define PERF_COUNTER_IOC_ENABLE _IO ('$', 0)
181#define PERF_COUNTER_IOC_DISABLE _IO ('$', 1)
182#define PERF_COUNTER_IOC_REFRESH _IO ('$', 2)
183#define PERF_COUNTER_IOC_RESET _IO ('$', 3)
184#define PERF_COUNTER_IOC_PERIOD _IOW('$', 4, u64)
Peter Zijlstra3df5eda2009-05-08 18:52:22 +0200185
186enum perf_counter_ioc_flags {
187 PERF_IOC_FLAG_GROUP = 1U << 0,
188};
Paul Mackerrasd859e292009-01-17 18:10:22 +1100189
Paul Mackerras37d81822009-03-23 18:22:08 +0100190/*
191 * Structure of the page that can be mapped via mmap
192 */
193struct perf_counter_mmap_page {
194 __u32 version; /* version number of this structure */
195 __u32 compat_version; /* lowest version this is compat with */
Peter Zijlstra38ff6672009-03-30 19:07:03 +0200196
197 /*
198 * Bits needed to read the hw counters in user-space.
199 *
Peter Zijlstra92f22a32009-04-02 11:12:04 +0200200 * u32 seq;
201 * s64 count;
Peter Zijlstra38ff6672009-03-30 19:07:03 +0200202 *
Peter Zijlstraa2e87d02009-04-06 11:44:59 +0200203 * do {
204 * seq = pc->lock;
Peter Zijlstra38ff6672009-03-30 19:07:03 +0200205 *
Peter Zijlstraa2e87d02009-04-06 11:44:59 +0200206 * barrier()
207 * if (pc->index) {
208 * count = pmc_read(pc->index - 1);
209 * count += pc->offset;
210 * } else
211 * goto regular_read;
Peter Zijlstra38ff6672009-03-30 19:07:03 +0200212 *
Peter Zijlstraa2e87d02009-04-06 11:44:59 +0200213 * barrier();
214 * } while (pc->lock != seq);
Peter Zijlstra38ff6672009-03-30 19:07:03 +0200215 *
Peter Zijlstra92f22a32009-04-02 11:12:04 +0200216 * NOTE: for obvious reason this only works on self-monitoring
217 * processes.
Peter Zijlstra38ff6672009-03-30 19:07:03 +0200218 */
Paul Mackerras37d81822009-03-23 18:22:08 +0100219 __u32 lock; /* seqlock for synchronization */
220 __u32 index; /* hardware counter identifier */
221 __s64 offset; /* add to hardware counter value */
Peter Zijlstra7b732a72009-03-23 18:22:10 +0100222
Peter Zijlstra38ff6672009-03-30 19:07:03 +0200223 /*
224 * Control data for the mmap() data buffer.
225 *
226 * User-space reading this value should issue an rmb(), on SMP capable
227 * platforms, after reading this value -- see perf_counter_wakeup().
228 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +0200229 __u64 data_head; /* head in the data section */
Paul Mackerras37d81822009-03-23 18:22:08 +0100230};
231
Paul Mackerras9d23a902009-05-14 21:48:08 +1000232#define PERF_EVENT_MISC_CPUMODE_MASK (3 << 0)
233#define PERF_EVENT_MISC_CPUMODE_UNKNOWN (0 << 0)
Peter Zijlstra6b6e54862009-04-08 15:01:27 +0200234#define PERF_EVENT_MISC_KERNEL (1 << 0)
Paul Mackerras9d23a902009-05-14 21:48:08 +1000235#define PERF_EVENT_MISC_USER (2 << 0)
236#define PERF_EVENT_MISC_HYPERVISOR (3 << 0)
Peter Zijlstra6b6e54862009-04-08 15:01:27 +0200237#define PERF_EVENT_MISC_OVERFLOW (1 << 2)
Peter Zijlstra6fab0192009-04-08 15:01:26 +0200238
Peter Zijlstra5c148192009-03-25 12:30:23 +0100239struct perf_event_header {
240 __u32 type;
Peter Zijlstra6fab0192009-04-08 15:01:26 +0200241 __u16 misc;
242 __u16 size;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100243};
244
245enum perf_event_type {
Peter Zijlstra5ed00412009-03-30 19:07:12 +0200246
Peter Zijlstra0c593b32009-04-06 11:45:08 +0200247 /*
248 * The MMAP events record the PROT_EXEC mappings so that we can
249 * correlate userspace IPs to code. They have the following structure:
250 *
251 * struct {
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200252 * struct perf_event_header header;
Peter Zijlstra0c593b32009-04-06 11:45:08 +0200253 *
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200254 * u32 pid, tid;
255 * u64 addr;
256 * u64 len;
257 * u64 pgoff;
258 * char filename[];
Peter Zijlstra0c593b32009-04-06 11:45:08 +0200259 * };
260 */
Peter Zijlstra8a057d82009-04-02 11:11:59 +0200261 PERF_EVENT_MMAP = 1,
Peter Zijlstraea5d20c2009-03-25 12:30:25 +0100262
Peter Zijlstra8a057d82009-04-02 11:11:59 +0200263 /*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +0200264 * struct {
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200265 * struct perf_event_header header;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +0200266 *
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200267 * u32 pid, tid;
268 * char comm[];
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +0200269 * };
270 */
271 PERF_EVENT_COMM = 3,
272
273 /*
Peter Zijlstra26b119b2009-05-20 12:21:20 +0200274 * struct {
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200275 * struct perf_event_header header;
276 * u64 time;
Peter Zijlstra689802b2009-06-05 15:05:43 +0200277 * u64 id;
Peter Zijlstrab23f3322009-06-02 15:13:03 +0200278 * u64 sample_period;
Peter Zijlstra26b119b2009-05-20 12:21:20 +0200279 * };
280 */
281 PERF_EVENT_PERIOD = 4,
282
283 /*
Peter Zijlstraa78ac322009-05-25 17:39:05 +0200284 * struct {
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200285 * struct perf_event_header header;
286 * u64 time;
Peter Zijlstraa78ac322009-05-25 17:39:05 +0200287 * };
288 */
289 PERF_EVENT_THROTTLE = 5,
290 PERF_EVENT_UNTHROTTLE = 6,
291
292 /*
Peter Zijlstra60313eb2009-06-04 16:53:44 +0200293 * struct {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200294 * struct perf_event_header header;
295 * u32 pid, ppid;
Peter Zijlstra60313eb2009-06-04 16:53:44 +0200296 * };
297 */
298 PERF_EVENT_FORK = 7,
299
300 /*
Peter Zijlstra6b6e54862009-04-08 15:01:27 +0200301 * When header.misc & PERF_EVENT_MISC_OVERFLOW the event_type field
302 * will be PERF_RECORD_*
Peter Zijlstra0c593b32009-04-06 11:45:08 +0200303 *
304 * struct {
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200305 * struct perf_event_header header;
Peter Zijlstra0c593b32009-04-06 11:45:08 +0200306 *
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200307 * { u64 ip; } && PERF_RECORD_IP
308 * { u32 pid, tid; } && PERF_RECORD_TID
309 * { u64 time; } && PERF_RECORD_TIME
310 * { u64 addr; } && PERF_RECORD_ADDR
311 * { u64 config; } && PERF_RECORD_CONFIG
312 * { u32 cpu, res; } && PERF_RECORD_CPU
Peter Zijlstra0c593b32009-04-06 11:45:08 +0200313 *
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200314 * { u64 nr;
Peter Zijlstra8e5799b2009-06-02 15:08:15 +0200315 * { u64 id, val; } cnt[nr]; } && PERF_RECORD_GROUP
Peter Zijlstra0c593b32009-04-06 11:45:08 +0200316 *
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200317 * { u16 nr,
318 * hv,
319 * kernel,
320 * user;
321 * u64 ips[nr]; } && PERF_RECORD_CALLCHAIN
Peter Zijlstra0c593b32009-04-06 11:45:08 +0200322 * };
Peter Zijlstra8a057d82009-04-02 11:11:59 +0200323 */
Peter Zijlstra5c148192009-03-25 12:30:23 +0100324};
325
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100326#ifdef __KERNEL__
Paul Mackerrasd859e292009-01-17 18:10:22 +1100327/*
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100328 * Kernel-internal data types and definitions:
Ingo Molnar9f66a382008-12-10 12:33:23 +0100329 */
330
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100331#ifdef CONFIG_PERF_COUNTERS
332# include <asm/perf_counter.h>
333#endif
334
335#include <linux/list.h>
336#include <linux/mutex.h>
337#include <linux/rculist.h>
338#include <linux/rcupdate.h>
339#include <linux/spinlock.h>
Peter Zijlstrad6d020e2009-03-13 12:21:35 +0100340#include <linux/hrtimer.h>
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +0200341#include <linux/fs.h>
Peter Zijlstra709e50c2009-06-02 14:13:15 +0200342#include <linux/pid_namespace.h>
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100343#include <asm/atomic.h>
344
345struct task_struct;
346
Thomas Gleixner0793a612008-12-04 20:12:29 +0100347/**
Ingo Molnar9f66a382008-12-10 12:33:23 +0100348 * struct hw_perf_counter - performance counter hardware details:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100349 */
350struct hw_perf_counter {
Ingo Molnaree060942008-12-13 09:00:03 +0100351#ifdef CONFIG_PERF_COUNTERS
Peter Zijlstrad6d020e2009-03-13 12:21:35 +0100352 union {
353 struct { /* hardware */
354 u64 config;
355 unsigned long config_base;
356 unsigned long counter_base;
Robert Richter6f00cad2009-04-29 12:47:17 +0200357 int idx;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +0100358 };
359 union { /* software */
360 atomic64_t count;
361 struct hrtimer hrtimer;
362 };
363 };
Ingo Molnaree060942008-12-13 09:00:03 +0100364 atomic64_t prev_count;
Peter Zijlstrab23f3322009-06-02 15:13:03 +0200365 u64 sample_period;
Peter Zijlstra9e350de2009-06-10 21:34:59 +0200366 u64 last_period;
Ingo Molnaree060942008-12-13 09:00:03 +0100367 atomic64_t period_left;
Peter Zijlstra60db5e02009-05-15 15:19:28 +0200368 u64 interrupts;
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +0200369
370 u64 freq_count;
371 u64 freq_interrupts;
Peter Zijlstrabd2b5b12009-06-10 13:40:57 +0200372 u64 freq_stamp;
Ingo Molnaree060942008-12-13 09:00:03 +0100373#endif
Thomas Gleixner0793a612008-12-04 20:12:29 +0100374};
375
Ingo Molnar621a01e2008-12-11 12:46:46 +0100376struct perf_counter;
377
378/**
Robert Richter4aeb0b42009-04-29 12:47:03 +0200379 * struct pmu - generic performance monitoring unit
Ingo Molnar621a01e2008-12-11 12:46:46 +0100380 */
Robert Richter4aeb0b42009-04-29 12:47:03 +0200381struct pmu {
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100382 int (*enable) (struct perf_counter *counter);
Ingo Molnar76715812008-12-17 14:20:28 +0100383 void (*disable) (struct perf_counter *counter);
384 void (*read) (struct perf_counter *counter);
Peter Zijlstraa78ac322009-05-25 17:39:05 +0200385 void (*unthrottle) (struct perf_counter *counter);
Ingo Molnar621a01e2008-12-11 12:46:46 +0100386};
387
Thomas Gleixner0793a612008-12-04 20:12:29 +0100388/**
Ingo Molnar6a930702008-12-11 15:17:03 +0100389 * enum perf_counter_active_state - the states of a counter
390 */
391enum perf_counter_active_state {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100392 PERF_COUNTER_STATE_ERROR = -2,
Ingo Molnar6a930702008-12-11 15:17:03 +0100393 PERF_COUNTER_STATE_OFF = -1,
394 PERF_COUNTER_STATE_INACTIVE = 0,
395 PERF_COUNTER_STATE_ACTIVE = 1,
396};
397
Ingo Molnar9b51f662008-12-12 13:49:45 +0100398struct file;
399
Peter Zijlstra7b732a72009-03-23 18:22:10 +0100400struct perf_mmap_data {
401 struct rcu_head rcu_head;
Peter Zijlstra8740f942009-04-08 15:01:29 +0200402 int nr_pages; /* nr of data pages */
Peter Zijlstrac5078f72009-05-05 17:50:24 +0200403 int nr_locked; /* nr pages mlocked */
Peter Zijlstra8740f942009-04-08 15:01:29 +0200404
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +0200405 atomic_t poll; /* POLL_ for wakeups */
Peter Zijlstra8740f942009-04-08 15:01:29 +0200406 atomic_t events; /* event limit */
407
Peter Zijlstra8e3747c2009-06-02 16:16:02 +0200408 atomic_long_t head; /* write position */
409 atomic_long_t done_head; /* completed head */
410
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +0200411 atomic_t lock; /* concurrent writes */
412
Peter Zijlstrac66de4a2009-05-05 17:50:22 +0200413 atomic_t wakeup; /* needs a wakeup */
414
Peter Zijlstra7b732a72009-03-23 18:22:10 +0100415 struct perf_counter_mmap_page *user_page;
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200416 void *data_pages[0];
Peter Zijlstra7b732a72009-03-23 18:22:10 +0100417};
418
Peter Zijlstra671dec52009-04-06 11:45:02 +0200419struct perf_pending_entry {
420 struct perf_pending_entry *next;
421 void (*func)(struct perf_pending_entry *);
Peter Zijlstra925d5192009-03-30 19:07:02 +0200422};
423
Ingo Molnar6a930702008-12-11 15:17:03 +0100424/**
Thomas Gleixner0793a612008-12-04 20:12:29 +0100425 * struct perf_counter - performance counter kernel representation:
426 */
427struct perf_counter {
Ingo Molnaree060942008-12-13 09:00:03 +0100428#ifdef CONFIG_PERF_COUNTERS
Ingo Molnar04289bb2008-12-11 08:38:42 +0100429 struct list_head list_entry;
Peter Zijlstra592903c2009-03-13 12:21:36 +0100430 struct list_head event_entry;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100431 struct list_head sibling_list;
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200432 int nr_siblings;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100433 struct perf_counter *group_leader;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200434 const struct pmu *pmu;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100435
Ingo Molnar6a930702008-12-11 15:17:03 +0100436 enum perf_counter_active_state state;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100437 atomic64_t count;
Ingo Molnaree060942008-12-13 09:00:03 +0100438
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100439 /*
440 * These are the total time in nanoseconds that the counter
441 * has been enabled (i.e. eligible to run, and the task has
442 * been scheduled in, if this is a per-task counter)
443 * and running (scheduled onto the CPU), respectively.
444 *
445 * They are computed from tstamp_enabled, tstamp_running and
446 * tstamp_stopped when the counter is in INACTIVE or ACTIVE state.
447 */
448 u64 total_time_enabled;
449 u64 total_time_running;
450
451 /*
452 * These are timestamps used for computing total_time_enabled
453 * and total_time_running when the counter is in INACTIVE or
454 * ACTIVE state, measured in nanoseconds from an arbitrary point
455 * in time.
456 * tstamp_enabled: the notional time when the counter was enabled
457 * tstamp_running: the notional time when the counter was scheduled on
458 * tstamp_stopped: in INACTIVE state, the notional time when the
459 * counter was scheduled off.
460 */
461 u64 tstamp_enabled;
462 u64 tstamp_running;
463 u64 tstamp_stopped;
464
Peter Zijlstra0d486962009-06-02 19:22:16 +0200465 struct perf_counter_attr attr;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100466 struct hw_perf_counter hw;
467
468 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100469 struct file *filp;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100470
471 /*
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100472 * These accumulate total time (in nanoseconds) that children
473 * counters have been enabled and running, respectively.
474 */
475 atomic64_t child_total_time_enabled;
476 atomic64_t child_total_time_running;
477
478 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100479 * Protect attach/detach and child_list:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100480 */
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200481 struct mutex child_mutex;
482 struct list_head child_list;
483 struct perf_counter *parent;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100484
485 int oncpu;
486 int cpu;
487
Peter Zijlstra082ff5a2009-05-23 18:29:00 +0200488 struct list_head owner_entry;
489 struct task_struct *owner;
490
Peter Zijlstra7b732a72009-03-23 18:22:10 +0100491 /* mmap bits */
492 struct mutex mmap_mutex;
493 atomic_t mmap_count;
494 struct perf_mmap_data *data;
Paul Mackerras37d81822009-03-23 18:22:08 +0100495
Peter Zijlstra7b732a72009-03-23 18:22:10 +0100496 /* poll related */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100497 wait_queue_head_t waitq;
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +0200498 struct fasync_struct *fasync;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200499
500 /* delayed work for NMIs and such */
501 int pending_wakeup;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +0200502 int pending_kill;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200503 int pending_disable;
Peter Zijlstra671dec52009-04-06 11:45:02 +0200504 struct perf_pending_entry pending;
Peter Zijlstra592903c2009-03-13 12:21:36 +0100505
Peter Zijlstra79f14642009-04-06 11:45:07 +0200506 atomic_t event_limit;
507
Peter Zijlstrae077df42009-03-19 20:26:17 +0100508 void (*destroy)(struct perf_counter *);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100509 struct rcu_head rcu_head;
Peter Zijlstra709e50c2009-06-02 14:13:15 +0200510
511 struct pid_namespace *ns;
Peter Zijlstra8e5799b2009-06-02 15:08:15 +0200512 u64 id;
Ingo Molnaree060942008-12-13 09:00:03 +0100513#endif
Thomas Gleixner0793a612008-12-04 20:12:29 +0100514};
515
516/**
517 * struct perf_counter_context - counter context structure
518 *
519 * Used as a container for task counters and CPU counters as well:
520 */
521struct perf_counter_context {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100522 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100523 * Protect the states of the counters in the list,
524 * nr_active, and the list:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100525 */
526 spinlock_t lock;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100527 /*
528 * Protect the list of counters. Locking either mutex or lock
529 * is sufficient to ensure the list doesn't change; to change
530 * the list you need to lock both the mutex and the spinlock.
531 */
532 struct mutex mutex;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100533
534 struct list_head counter_list;
Peter Zijlstra592903c2009-03-13 12:21:36 +0100535 struct list_head event_list;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100536 int nr_counters;
537 int nr_active;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100538 int is_active;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000539 atomic_t refcount;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100540 struct task_struct *task;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100541
542 /*
Peter Zijlstra4af49982009-04-06 11:45:10 +0200543 * Context clock, runs when context enabled.
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100544 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200545 u64 time;
546 u64 timestamp;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000547
548 /*
549 * These fields let us detect when two contexts have both
550 * been cloned (inherited) from a common ancestor.
551 */
552 struct perf_counter_context *parent_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000553 u64 parent_gen;
554 u64 generation;
Paul Mackerras25346b932009-06-01 17:48:12 +1000555 int pin_count;
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000556 struct rcu_head rcu_head;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100557};
558
559/**
560 * struct perf_counter_cpu_context - per cpu counter context structure
561 */
562struct perf_cpu_context {
563 struct perf_counter_context ctx;
564 struct perf_counter_context *task_ctx;
565 int active_oncpu;
566 int max_pertask;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100567 int exclusive;
Peter Zijlstra96f6d442009-03-23 18:22:07 +0100568
569 /*
570 * Recursion avoidance:
571 *
572 * task, softirq, irq, nmi context
573 */
Ingo Molnar22a4f652009-06-01 10:13:37 +0200574 int recursion[4];
Thomas Gleixner0793a612008-12-04 20:12:29 +0100575};
576
Robert Richter829b42d2009-04-29 12:46:59 +0200577#ifdef CONFIG_PERF_COUNTERS
578
Thomas Gleixner0793a612008-12-04 20:12:29 +0100579/*
580 * Set by architecture code:
581 */
582extern int perf_max_counters;
583
Robert Richter4aeb0b42009-04-29 12:47:03 +0200584extern const struct pmu *hw_perf_counter_init(struct perf_counter *counter);
Ingo Molnar621a01e2008-12-11 12:46:46 +0100585
Thomas Gleixner0793a612008-12-04 20:12:29 +0100586extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000587extern void perf_counter_task_sched_out(struct task_struct *task,
588 struct task_struct *next, int cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100589extern void perf_counter_task_tick(struct task_struct *task, int cpu);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +0200590extern int perf_counter_init_task(struct task_struct *child);
Ingo Molnar9b51f662008-12-12 13:49:45 +0100591extern void perf_counter_exit_task(struct task_struct *child);
Peter Zijlstrabbbee902009-05-29 14:25:58 +0200592extern void perf_counter_free_task(struct task_struct *task);
Peter Zijlstra925d5192009-03-30 19:07:02 +0200593extern void perf_counter_do_pending(void);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100594extern void perf_counter_print_debug(void);
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200595extern void __perf_disable(void);
596extern bool __perf_enable(void);
597extern void perf_disable(void);
598extern void perf_enable(void);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100599extern int perf_counter_task_disable(void);
600extern int perf_counter_task_enable(void);
Paul Mackerras3cbed422009-01-09 16:43:42 +1100601extern int hw_perf_group_sched_in(struct perf_counter *group_leader,
602 struct perf_cpu_context *cpuctx,
603 struct perf_counter_context *ctx, int cpu);
Paul Mackerras37d81822009-03-23 18:22:08 +0100604extern void perf_counter_update_userpage(struct perf_counter *counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +0100605
Peter Zijlstradf1a1322009-06-10 21:02:22 +0200606struct perf_sample_data {
Peter Zijlstra9e350de2009-06-10 21:34:59 +0200607 struct pt_regs *regs;
608 u64 addr;
609 u64 period;
Peter Zijlstradf1a1322009-06-10 21:02:22 +0200610};
611
612extern int perf_counter_overflow(struct perf_counter *counter, int nmi,
613 struct perf_sample_data *data);
614
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100615/*
616 * Return 1 for a software counter, 0 for a hardware counter
617 */
618static inline int is_software_counter(struct perf_counter *counter)
619{
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200620 return (counter->attr.type != PERF_TYPE_RAW) &&
621 (counter->attr.type != PERF_TYPE_HARDWARE);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100622}
623
Peter Zijlstra78f13e92009-04-08 15:01:33 +0200624extern void perf_swcounter_event(u32, u64, int, struct pt_regs *, u64);
Peter Zijlstra15dbf272009-03-13 12:21:32 +0100625
Peter Zijlstra089dd792009-06-05 14:04:55 +0200626extern void __perf_counter_mmap(struct vm_area_struct *vma);
627
628static inline void perf_counter_mmap(struct vm_area_struct *vma)
629{
630 if (vma->vm_flags & VM_EXEC)
631 __perf_counter_mmap(vma);
632}
Peter Zijlstra0a4a9392009-03-30 19:07:05 +0200633
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +0200634extern void perf_counter_comm(struct task_struct *tsk);
Peter Zijlstra60313eb2009-06-04 16:53:44 +0200635extern void perf_counter_fork(struct task_struct *tsk);
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +0200636
Paul Mackerras3f731ca2009-06-01 17:52:30 +1000637extern void perf_counter_task_migration(struct task_struct *task, int cpu);
638
Peter Zijlstra9c03d882009-04-06 11:45:00 +0200639#define MAX_STACK_DEPTH 255
Peter Zijlstra394ee072009-03-30 19:07:14 +0200640
641struct perf_callchain_entry {
Peter Zijlstra9c03d882009-04-06 11:45:00 +0200642 u16 nr, hv, kernel, user;
Peter Zijlstra394ee072009-03-30 19:07:14 +0200643 u64 ip[MAX_STACK_DEPTH];
644};
645
646extern struct perf_callchain_entry *perf_callchain(struct pt_regs *regs);
647
Peter Zijlstra07647712009-06-11 11:18:36 +0200648extern int sysctl_perf_counter_paranoid;
Peter Zijlstrac5078f72009-05-05 17:50:24 +0200649extern int sysctl_perf_counter_mlock;
Peter Zijlstradf58ab22009-06-11 11:25:05 +0200650extern int sysctl_perf_counter_sample_rate;
Peter Zijlstra1ccd1542009-04-09 10:53:45 +0200651
Ingo Molnar0d905bc2009-05-04 19:13:30 +0200652extern void perf_counter_init(void);
653
Paul Mackerras9d23a902009-05-14 21:48:08 +1000654#ifndef perf_misc_flags
655#define perf_misc_flags(regs) (user_mode(regs) ? PERF_EVENT_MISC_USER : \
656 PERF_EVENT_MISC_KERNEL)
657#define perf_instruction_pointer(regs) instruction_pointer(regs)
658#endif
659
Thomas Gleixner0793a612008-12-04 20:12:29 +0100660#else
661static inline void
662perf_counter_task_sched_in(struct task_struct *task, int cpu) { }
663static inline void
Ingo Molnar910431c2009-05-22 12:32:15 +0200664perf_counter_task_sched_out(struct task_struct *task,
665 struct task_struct *next, int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100666static inline void
667perf_counter_task_tick(struct task_struct *task, int cpu) { }
Ingo Molnard3e78ee2009-05-28 11:41:50 +0200668static inline int perf_counter_init_task(struct task_struct *child) { return 0; }
Ingo Molnar9b51f662008-12-12 13:49:45 +0100669static inline void perf_counter_exit_task(struct task_struct *child) { }
Peter Zijlstrabbbee902009-05-29 14:25:58 +0200670static inline void perf_counter_free_task(struct task_struct *task) { }
Peter Zijlstra925d5192009-03-30 19:07:02 +0200671static inline void perf_counter_do_pending(void) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100672static inline void perf_counter_print_debug(void) { }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200673static inline void perf_disable(void) { }
674static inline void perf_enable(void) { }
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100675static inline int perf_counter_task_disable(void) { return -EINVAL; }
676static inline int perf_counter_task_enable(void) { return -EINVAL; }
Peter Zijlstra15dbf272009-03-13 12:21:32 +0100677
Peter Zijlstra925d5192009-03-30 19:07:02 +0200678static inline void
Peter Zijlstra78f13e92009-04-08 15:01:33 +0200679perf_swcounter_event(u32 event, u64 nr, int nmi,
680 struct pt_regs *regs, u64 addr) { }
Peter Zijlstra0a4a9392009-03-30 19:07:05 +0200681
Peter Zijlstra089dd792009-06-05 14:04:55 +0200682static inline void perf_counter_mmap(struct vm_area_struct *vma) { }
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +0200683static inline void perf_counter_comm(struct task_struct *tsk) { }
Peter Zijlstra60313eb2009-06-04 16:53:44 +0200684static inline void perf_counter_fork(struct task_struct *tsk) { }
Ingo Molnar0d905bc2009-05-04 19:13:30 +0200685static inline void perf_counter_init(void) { }
Paul Mackerras3f731ca2009-06-01 17:52:30 +1000686static inline void perf_counter_task_migration(struct task_struct *task,
687 int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100688#endif
689
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100690#endif /* __KERNEL__ */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100691#endif /* _LINUX_PERF_COUNTER_H */