blob: f794c69b34c995c87dadd217c4d7296d229f58e3 [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 */
27enum perf_event_types {
28 PERF_TYPE_HARDWARE = 0,
29 PERF_TYPE_SOFTWARE = 1,
30 PERF_TYPE_TRACEPOINT = 2,
31
32 /*
33 * available TYPE space, raw is the max value.
34 */
35
36 PERF_TYPE_RAW = 128,
37};
38
39/*
Peter Zijlstra0d486962009-06-02 19:22:16 +020040 * Generalized performance counter event types, used by the attr.event_id
Ingo Molnar9f66a382008-12-10 12:33:23 +010041 * parameter of the sys_perf_counter_open() syscall:
Thomas Gleixner0793a612008-12-04 20:12:29 +010042 */
Peter Zijlstra0d486962009-06-02 19:22:16 +020043enum attr_ids {
Thomas Gleixner0793a612008-12-04 20:12:29 +010044 /*
Ingo Molnar9f66a382008-12-10 12:33:23 +010045 * Common hardware events, generalized by the kernel:
Thomas Gleixner0793a612008-12-04 20:12:29 +010046 */
Peter Zijlstrab8e83512009-03-19 20:26:18 +010047 PERF_COUNT_CPU_CYCLES = 0,
48 PERF_COUNT_INSTRUCTIONS = 1,
49 PERF_COUNT_CACHE_REFERENCES = 2,
50 PERF_COUNT_CACHE_MISSES = 3,
51 PERF_COUNT_BRANCH_INSTRUCTIONS = 4,
52 PERF_COUNT_BRANCH_MISSES = 5,
53 PERF_COUNT_BUS_CYCLES = 6,
Ingo Molnar9f66a382008-12-10 12:33:23 +010054
Peter Zijlstrab8e83512009-03-19 20:26:18 +010055 PERF_HW_EVENTS_MAX = 7,
56};
Ingo Molnar6c594c22008-12-14 12:34:15 +010057
Peter Zijlstrab8e83512009-03-19 20:26:18 +010058/*
59 * Special "software" counters provided by the kernel, even if the hardware
60 * does not support performance counters. These counters measure various
61 * physical and sw events of the kernel (and allow the profiling of them as
62 * well):
63 */
64enum sw_event_ids {
65 PERF_COUNT_CPU_CLOCK = 0,
66 PERF_COUNT_TASK_CLOCK = 1,
67 PERF_COUNT_PAGE_FAULTS = 2,
68 PERF_COUNT_CONTEXT_SWITCHES = 3,
69 PERF_COUNT_CPU_MIGRATIONS = 4,
70 PERF_COUNT_PAGE_FAULTS_MIN = 5,
71 PERF_COUNT_PAGE_FAULTS_MAJ = 6,
Ingo Molnar6c594c22008-12-14 12:34:15 +010072
Peter Zijlstrab8e83512009-03-19 20:26:18 +010073 PERF_SW_EVENTS_MAX = 7,
Thomas Gleixner0793a612008-12-04 20:12:29 +010074};
75
Ingo Molnar9f66a382008-12-10 12:33:23 +010076/*
Peter Zijlstra0d486962009-06-02 19:22:16 +020077 * Bits that can be set in attr.sample_type to request information
Peter Zijlstra8a057d82009-04-02 11:11:59 +020078 * in the overflow packets.
79 */
Peter Zijlstrab23f3322009-06-02 15:13:03 +020080enum perf_counter_sample_format {
81 PERF_SAMPLE_IP = 1U << 0,
82 PERF_SAMPLE_TID = 1U << 1,
83 PERF_SAMPLE_TIME = 1U << 2,
84 PERF_SAMPLE_ADDR = 1U << 3,
85 PERF_SAMPLE_GROUP = 1U << 4,
86 PERF_SAMPLE_CALLCHAIN = 1U << 5,
Peter Zijlstraac4bcf82009-06-05 14:44:52 +020087 PERF_SAMPLE_ID = 1U << 6,
Peter Zijlstrab23f3322009-06-02 15:13:03 +020088 PERF_SAMPLE_CPU = 1U << 7,
Peter Zijlstra689802b2009-06-05 15:05:43 +020089 PERF_SAMPLE_PERIOD = 1U << 8,
Peter Zijlstra8a057d82009-04-02 11:11:59 +020090};
91
92/*
Peter Zijlstra0d486962009-06-02 19:22:16 +020093 * Bits that can be set in attr.read_format to request that
Paul Mackerras53cfbf52009-03-25 22:46:58 +110094 * reads on the counter should return the indicated quantities,
95 * in increasing order of bit value, after the counter value.
96 */
97enum perf_counter_read_format {
Peter Zijlstra8e5799b2009-06-02 15:08:15 +020098 PERF_FORMAT_TOTAL_TIME_ENABLED = 1U << 0,
99 PERF_FORMAT_TOTAL_TIME_RUNNING = 1U << 1,
100 PERF_FORMAT_ID = 1U << 2,
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100101};
102
103/*
Ingo Molnar9f66a382008-12-10 12:33:23 +0100104 * Hardware event to monitor via a performance monitoring counter:
105 */
Peter Zijlstra0d486962009-06-02 19:22:16 +0200106struct perf_counter_attr {
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +0100107 /*
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200108 * Major type: hardware/software/tracepoint/etc.
109 */
110 __u32 type;
111 __u32 __reserved_1;
112
113 /*
114 * Type specific configuration information.
Peter Zijlstraf4a2deb2009-03-23 18:22:06 +0100115 */
116 __u64 config;
Ingo Molnar9f66a382008-12-10 12:33:23 +0100117
Peter Zijlstra60db5e02009-05-15 15:19:28 +0200118 union {
Peter Zijlstrab23f3322009-06-02 15:13:03 +0200119 __u64 sample_period;
120 __u64 sample_freq;
Peter Zijlstra60db5e02009-05-15 15:19:28 +0200121 };
122
Peter Zijlstrab23f3322009-06-02 15:13:03 +0200123 __u64 sample_type;
124 __u64 read_format;
Ingo Molnar9f66a382008-12-10 12:33:23 +0100125
Paul Mackerras2743a5b2009-03-04 20:36:51 +1100126 __u64 disabled : 1, /* off by default */
Paul Mackerras0475f9e2009-02-11 14:35:35 +1100127 inherit : 1, /* children inherit it */
128 pinned : 1, /* must always be on PMU */
129 exclusive : 1, /* only group on PMU */
130 exclude_user : 1, /* don't count user */
131 exclude_kernel : 1, /* ditto kernel */
132 exclude_hv : 1, /* ditto hypervisor */
Paul Mackerras2743a5b2009-03-04 20:36:51 +1100133 exclude_idle : 1, /* don't count when idle */
Peter Zijlstra0a4a9392009-03-30 19:07:05 +0200134 mmap : 1, /* include mmap data */
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +0200135 comm : 1, /* include comm data */
Peter Zijlstra60db5e02009-05-15 15:19:28 +0200136 freq : 1, /* use freq, not period */
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100137
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200138 __reserved_2 : 53;
Paul Mackerras2743a5b2009-03-04 20:36:51 +1100139
Peter Zijlstrac4578102009-04-02 11:12:01 +0200140 __u32 wakeup_events; /* wakeup every n events */
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200141 __u32 __reserved_3;
Ingo Molnar9f66a382008-12-10 12:33:23 +0100142
Peter Zijlstrae527ea32009-05-25 14:45:25 +0200143 __u64 __reserved_4;
Thomas Gleixnereab656a2008-12-08 19:26:59 +0100144};
145
Ingo Molnar9f66a382008-12-10 12:33:23 +0100146/*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100147 * Ioctls that can be done on a perf counter fd:
148 */
Peter Zijlstra08247e32009-06-02 16:46:57 +0200149#define PERF_COUNTER_IOC_ENABLE _IO ('$', 0)
150#define PERF_COUNTER_IOC_DISABLE _IO ('$', 1)
151#define PERF_COUNTER_IOC_REFRESH _IO ('$', 2)
152#define PERF_COUNTER_IOC_RESET _IO ('$', 3)
153#define PERF_COUNTER_IOC_PERIOD _IOW('$', 4, u64)
Peter Zijlstra3df5eda2009-05-08 18:52:22 +0200154
155enum perf_counter_ioc_flags {
156 PERF_IOC_FLAG_GROUP = 1U << 0,
157};
Paul Mackerrasd859e292009-01-17 18:10:22 +1100158
Paul Mackerras37d81822009-03-23 18:22:08 +0100159/*
160 * Structure of the page that can be mapped via mmap
161 */
162struct perf_counter_mmap_page {
163 __u32 version; /* version number of this structure */
164 __u32 compat_version; /* lowest version this is compat with */
Peter Zijlstra38ff6672009-03-30 19:07:03 +0200165
166 /*
167 * Bits needed to read the hw counters in user-space.
168 *
Peter Zijlstra92f22a32009-04-02 11:12:04 +0200169 * u32 seq;
170 * s64 count;
Peter Zijlstra38ff6672009-03-30 19:07:03 +0200171 *
Peter Zijlstraa2e87d02009-04-06 11:44:59 +0200172 * do {
173 * seq = pc->lock;
Peter Zijlstra38ff6672009-03-30 19:07:03 +0200174 *
Peter Zijlstraa2e87d02009-04-06 11:44:59 +0200175 * barrier()
176 * if (pc->index) {
177 * count = pmc_read(pc->index - 1);
178 * count += pc->offset;
179 * } else
180 * goto regular_read;
Peter Zijlstra38ff6672009-03-30 19:07:03 +0200181 *
Peter Zijlstraa2e87d02009-04-06 11:44:59 +0200182 * barrier();
183 * } while (pc->lock != seq);
Peter Zijlstra38ff6672009-03-30 19:07:03 +0200184 *
Peter Zijlstra92f22a32009-04-02 11:12:04 +0200185 * NOTE: for obvious reason this only works on self-monitoring
186 * processes.
Peter Zijlstra38ff6672009-03-30 19:07:03 +0200187 */
Paul Mackerras37d81822009-03-23 18:22:08 +0100188 __u32 lock; /* seqlock for synchronization */
189 __u32 index; /* hardware counter identifier */
190 __s64 offset; /* add to hardware counter value */
Peter Zijlstra7b732a72009-03-23 18:22:10 +0100191
Peter Zijlstra38ff6672009-03-30 19:07:03 +0200192 /*
193 * Control data for the mmap() data buffer.
194 *
195 * User-space reading this value should issue an rmb(), on SMP capable
196 * platforms, after reading this value -- see perf_counter_wakeup().
197 */
Peter Zijlstra8e3747c2009-06-02 16:16:02 +0200198 __u64 data_head; /* head in the data section */
Paul Mackerras37d81822009-03-23 18:22:08 +0100199};
200
Paul Mackerras9d23a902009-05-14 21:48:08 +1000201#define PERF_EVENT_MISC_CPUMODE_MASK (3 << 0)
202#define PERF_EVENT_MISC_CPUMODE_UNKNOWN (0 << 0)
Peter Zijlstra6b6e5482009-04-08 15:01:27 +0200203#define PERF_EVENT_MISC_KERNEL (1 << 0)
Paul Mackerras9d23a902009-05-14 21:48:08 +1000204#define PERF_EVENT_MISC_USER (2 << 0)
205#define PERF_EVENT_MISC_HYPERVISOR (3 << 0)
Peter Zijlstra6b6e5482009-04-08 15:01:27 +0200206#define PERF_EVENT_MISC_OVERFLOW (1 << 2)
Peter Zijlstra6fab0192009-04-08 15:01:26 +0200207
Peter Zijlstra5c148192009-03-25 12:30:23 +0100208struct perf_event_header {
209 __u32 type;
Peter Zijlstra6fab0192009-04-08 15:01:26 +0200210 __u16 misc;
211 __u16 size;
Peter Zijlstra5c148192009-03-25 12:30:23 +0100212};
213
214enum perf_event_type {
Peter Zijlstra5ed00412009-03-30 19:07:12 +0200215
Peter Zijlstra0c593b32009-04-06 11:45:08 +0200216 /*
217 * The MMAP events record the PROT_EXEC mappings so that we can
218 * correlate userspace IPs to code. They have the following structure:
219 *
220 * struct {
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200221 * struct perf_event_header header;
Peter Zijlstra0c593b32009-04-06 11:45:08 +0200222 *
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200223 * u32 pid, tid;
224 * u64 addr;
225 * u64 len;
226 * u64 pgoff;
227 * char filename[];
Peter Zijlstra0c593b32009-04-06 11:45:08 +0200228 * };
229 */
Peter Zijlstra8a057d82009-04-02 11:11:59 +0200230 PERF_EVENT_MMAP = 1,
Peter Zijlstraea5d20c2009-03-25 12:30:25 +0100231
Peter Zijlstra8a057d82009-04-02 11:11:59 +0200232 /*
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +0200233 * struct {
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200234 * struct perf_event_header header;
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +0200235 *
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200236 * u32 pid, tid;
237 * char comm[];
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +0200238 * };
239 */
240 PERF_EVENT_COMM = 3,
241
242 /*
Peter Zijlstra26b119b2009-05-20 12:21:20 +0200243 * struct {
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200244 * struct perf_event_header header;
245 * u64 time;
Peter Zijlstra689802b2009-06-05 15:05:43 +0200246 * u64 id;
Peter Zijlstrab23f3322009-06-02 15:13:03 +0200247 * u64 sample_period;
Peter Zijlstra26b119b2009-05-20 12:21:20 +0200248 * };
249 */
250 PERF_EVENT_PERIOD = 4,
251
252 /*
Peter Zijlstraa78ac322009-05-25 17:39:05 +0200253 * struct {
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200254 * struct perf_event_header header;
255 * u64 time;
Peter Zijlstraa78ac322009-05-25 17:39:05 +0200256 * };
257 */
258 PERF_EVENT_THROTTLE = 5,
259 PERF_EVENT_UNTHROTTLE = 6,
260
261 /*
Peter Zijlstra60313eb2009-06-04 16:53:44 +0200262 * struct {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200263 * struct perf_event_header header;
264 * u32 pid, ppid;
Peter Zijlstra60313eb2009-06-04 16:53:44 +0200265 * };
266 */
267 PERF_EVENT_FORK = 7,
268
269 /*
Peter Zijlstra6b6e5482009-04-08 15:01:27 +0200270 * When header.misc & PERF_EVENT_MISC_OVERFLOW the event_type field
271 * will be PERF_RECORD_*
Peter Zijlstra0c593b32009-04-06 11:45:08 +0200272 *
273 * struct {
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200274 * struct perf_event_header header;
Peter Zijlstra0c593b32009-04-06 11:45:08 +0200275 *
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200276 * { u64 ip; } && PERF_RECORD_IP
277 * { u32 pid, tid; } && PERF_RECORD_TID
278 * { u64 time; } && PERF_RECORD_TIME
279 * { u64 addr; } && PERF_RECORD_ADDR
280 * { u64 config; } && PERF_RECORD_CONFIG
281 * { u32 cpu, res; } && PERF_RECORD_CPU
Peter Zijlstra0c593b32009-04-06 11:45:08 +0200282 *
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200283 * { u64 nr;
Peter Zijlstra8e5799b2009-06-02 15:08:15 +0200284 * { u64 id, val; } cnt[nr]; } && PERF_RECORD_GROUP
Peter Zijlstra0c593b32009-04-06 11:45:08 +0200285 *
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200286 * { u16 nr,
287 * hv,
288 * kernel,
289 * user;
290 * u64 ips[nr]; } && PERF_RECORD_CALLCHAIN
Peter Zijlstra0c593b32009-04-06 11:45:08 +0200291 * };
Peter Zijlstra8a057d82009-04-02 11:11:59 +0200292 */
Peter Zijlstra5c148192009-03-25 12:30:23 +0100293};
294
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100295#ifdef __KERNEL__
Paul Mackerrasd859e292009-01-17 18:10:22 +1100296/*
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100297 * Kernel-internal data types and definitions:
Ingo Molnar9f66a382008-12-10 12:33:23 +0100298 */
299
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100300#ifdef CONFIG_PERF_COUNTERS
301# include <asm/perf_counter.h>
302#endif
303
304#include <linux/list.h>
305#include <linux/mutex.h>
306#include <linux/rculist.h>
307#include <linux/rcupdate.h>
308#include <linux/spinlock.h>
Peter Zijlstrad6d020e2009-03-13 12:21:35 +0100309#include <linux/hrtimer.h>
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +0200310#include <linux/fs.h>
Peter Zijlstra709e50c2009-06-02 14:13:15 +0200311#include <linux/pid_namespace.h>
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100312#include <asm/atomic.h>
313
314struct task_struct;
315
Thomas Gleixner0793a612008-12-04 20:12:29 +0100316/**
Ingo Molnar9f66a382008-12-10 12:33:23 +0100317 * struct hw_perf_counter - performance counter hardware details:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100318 */
319struct hw_perf_counter {
Ingo Molnaree060942008-12-13 09:00:03 +0100320#ifdef CONFIG_PERF_COUNTERS
Peter Zijlstrad6d020e2009-03-13 12:21:35 +0100321 union {
322 struct { /* hardware */
323 u64 config;
324 unsigned long config_base;
325 unsigned long counter_base;
Robert Richter6f00cad2009-04-29 12:47:17 +0200326 int idx;
Peter Zijlstrad6d020e2009-03-13 12:21:35 +0100327 };
328 union { /* software */
329 atomic64_t count;
330 struct hrtimer hrtimer;
331 };
332 };
Ingo Molnaree060942008-12-13 09:00:03 +0100333 atomic64_t prev_count;
Peter Zijlstrab23f3322009-06-02 15:13:03 +0200334 u64 sample_period;
Ingo Molnaree060942008-12-13 09:00:03 +0100335 atomic64_t period_left;
Peter Zijlstra60db5e02009-05-15 15:19:28 +0200336 u64 interrupts;
Peter Zijlstra6a24ed6c2009-06-05 18:01:29 +0200337
338 u64 freq_count;
339 u64 freq_interrupts;
Ingo Molnaree060942008-12-13 09:00:03 +0100340#endif
Thomas Gleixner0793a612008-12-04 20:12:29 +0100341};
342
Ingo Molnar621a01e2008-12-11 12:46:46 +0100343struct perf_counter;
344
345/**
Robert Richter4aeb0b42009-04-29 12:47:03 +0200346 * struct pmu - generic performance monitoring unit
Ingo Molnar621a01e2008-12-11 12:46:46 +0100347 */
Robert Richter4aeb0b42009-04-29 12:47:03 +0200348struct pmu {
Ingo Molnar95cdd2e2008-12-21 13:50:42 +0100349 int (*enable) (struct perf_counter *counter);
Ingo Molnar76715812008-12-17 14:20:28 +0100350 void (*disable) (struct perf_counter *counter);
351 void (*read) (struct perf_counter *counter);
Peter Zijlstraa78ac322009-05-25 17:39:05 +0200352 void (*unthrottle) (struct perf_counter *counter);
Ingo Molnar621a01e2008-12-11 12:46:46 +0100353};
354
Thomas Gleixner0793a612008-12-04 20:12:29 +0100355/**
Ingo Molnar6a930702008-12-11 15:17:03 +0100356 * enum perf_counter_active_state - the states of a counter
357 */
358enum perf_counter_active_state {
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100359 PERF_COUNTER_STATE_ERROR = -2,
Ingo Molnar6a930702008-12-11 15:17:03 +0100360 PERF_COUNTER_STATE_OFF = -1,
361 PERF_COUNTER_STATE_INACTIVE = 0,
362 PERF_COUNTER_STATE_ACTIVE = 1,
363};
364
Ingo Molnar9b51f662008-12-12 13:49:45 +0100365struct file;
366
Peter Zijlstra7b732a72009-03-23 18:22:10 +0100367struct perf_mmap_data {
368 struct rcu_head rcu_head;
Peter Zijlstra8740f942009-04-08 15:01:29 +0200369 int nr_pages; /* nr of data pages */
Peter Zijlstrac5078f72009-05-05 17:50:24 +0200370 int nr_locked; /* nr pages mlocked */
Peter Zijlstra8740f942009-04-08 15:01:29 +0200371
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +0200372 atomic_t poll; /* POLL_ for wakeups */
Peter Zijlstra8740f942009-04-08 15:01:29 +0200373 atomic_t events; /* event limit */
374
Peter Zijlstra8e3747c2009-06-02 16:16:02 +0200375 atomic_long_t head; /* write position */
376 atomic_long_t done_head; /* completed head */
377
Peter Zijlstrac33a0bc2009-05-01 12:23:16 +0200378 atomic_t lock; /* concurrent writes */
379
Peter Zijlstrac66de4a2009-05-05 17:50:22 +0200380 atomic_t wakeup; /* needs a wakeup */
381
Peter Zijlstra7b732a72009-03-23 18:22:10 +0100382 struct perf_counter_mmap_page *user_page;
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200383 void *data_pages[0];
Peter Zijlstra7b732a72009-03-23 18:22:10 +0100384};
385
Peter Zijlstra671dec52009-04-06 11:45:02 +0200386struct perf_pending_entry {
387 struct perf_pending_entry *next;
388 void (*func)(struct perf_pending_entry *);
Peter Zijlstra925d5192009-03-30 19:07:02 +0200389};
390
Ingo Molnar6a930702008-12-11 15:17:03 +0100391/**
Thomas Gleixner0793a612008-12-04 20:12:29 +0100392 * struct perf_counter - performance counter kernel representation:
393 */
394struct perf_counter {
Ingo Molnaree060942008-12-13 09:00:03 +0100395#ifdef CONFIG_PERF_COUNTERS
Ingo Molnar04289bb2008-12-11 08:38:42 +0100396 struct list_head list_entry;
Peter Zijlstra592903c2009-03-13 12:21:36 +0100397 struct list_head event_entry;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100398 struct list_head sibling_list;
Ingo Molnar0127c3e2009-05-25 22:03:26 +0200399 int nr_siblings;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100400 struct perf_counter *group_leader;
Robert Richter4aeb0b42009-04-29 12:47:03 +0200401 const struct pmu *pmu;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100402
Ingo Molnar6a930702008-12-11 15:17:03 +0100403 enum perf_counter_active_state state;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100404 atomic64_t count;
Ingo Molnaree060942008-12-13 09:00:03 +0100405
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100406 /*
407 * These are the total time in nanoseconds that the counter
408 * has been enabled (i.e. eligible to run, and the task has
409 * been scheduled in, if this is a per-task counter)
410 * and running (scheduled onto the CPU), respectively.
411 *
412 * They are computed from tstamp_enabled, tstamp_running and
413 * tstamp_stopped when the counter is in INACTIVE or ACTIVE state.
414 */
415 u64 total_time_enabled;
416 u64 total_time_running;
417
418 /*
419 * These are timestamps used for computing total_time_enabled
420 * and total_time_running when the counter is in INACTIVE or
421 * ACTIVE state, measured in nanoseconds from an arbitrary point
422 * in time.
423 * tstamp_enabled: the notional time when the counter was enabled
424 * tstamp_running: the notional time when the counter was scheduled on
425 * tstamp_stopped: in INACTIVE state, the notional time when the
426 * counter was scheduled off.
427 */
428 u64 tstamp_enabled;
429 u64 tstamp_running;
430 u64 tstamp_stopped;
431
Peter Zijlstra0d486962009-06-02 19:22:16 +0200432 struct perf_counter_attr attr;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100433 struct hw_perf_counter hw;
434
435 struct perf_counter_context *ctx;
Ingo Molnar9b51f662008-12-12 13:49:45 +0100436 struct file *filp;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100437
438 /*
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100439 * These accumulate total time (in nanoseconds) that children
440 * counters have been enabled and running, respectively.
441 */
442 atomic64_t child_total_time_enabled;
443 atomic64_t child_total_time_running;
444
445 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100446 * Protect attach/detach and child_list:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100447 */
Peter Zijlstrafccc7142009-05-23 18:28:56 +0200448 struct mutex child_mutex;
449 struct list_head child_list;
450 struct perf_counter *parent;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100451
452 int oncpu;
453 int cpu;
454
Peter Zijlstra082ff5a2009-05-23 18:29:00 +0200455 struct list_head owner_entry;
456 struct task_struct *owner;
457
Peter Zijlstra7b732a72009-03-23 18:22:10 +0100458 /* mmap bits */
459 struct mutex mmap_mutex;
460 atomic_t mmap_count;
461 struct perf_mmap_data *data;
Paul Mackerras37d81822009-03-23 18:22:08 +0100462
Peter Zijlstra7b732a72009-03-23 18:22:10 +0100463 /* poll related */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100464 wait_queue_head_t waitq;
Peter Zijlstra3c446b3d2009-04-06 11:45:01 +0200465 struct fasync_struct *fasync;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200466
467 /* delayed work for NMIs and such */
468 int pending_wakeup;
Peter Zijlstra4c9e2542009-04-06 11:45:09 +0200469 int pending_kill;
Peter Zijlstra79f14642009-04-06 11:45:07 +0200470 int pending_disable;
Peter Zijlstra671dec52009-04-06 11:45:02 +0200471 struct perf_pending_entry pending;
Peter Zijlstra592903c2009-03-13 12:21:36 +0100472
Peter Zijlstra79f14642009-04-06 11:45:07 +0200473 atomic_t event_limit;
474
Peter Zijlstrae077df42009-03-19 20:26:17 +0100475 void (*destroy)(struct perf_counter *);
Peter Zijlstra592903c2009-03-13 12:21:36 +0100476 struct rcu_head rcu_head;
Peter Zijlstra709e50c2009-06-02 14:13:15 +0200477
478 struct pid_namespace *ns;
Peter Zijlstra8e5799b2009-06-02 15:08:15 +0200479 u64 id;
Ingo Molnaree060942008-12-13 09:00:03 +0100480#endif
Thomas Gleixner0793a612008-12-04 20:12:29 +0100481};
482
483/**
484 * struct perf_counter_context - counter context structure
485 *
486 * Used as a container for task counters and CPU counters as well:
487 */
488struct perf_counter_context {
Thomas Gleixner0793a612008-12-04 20:12:29 +0100489 /*
Paul Mackerrasd859e292009-01-17 18:10:22 +1100490 * Protect the states of the counters in the list,
491 * nr_active, and the list:
Thomas Gleixner0793a612008-12-04 20:12:29 +0100492 */
493 spinlock_t lock;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100494 /*
495 * Protect the list of counters. Locking either mutex or lock
496 * is sufficient to ensure the list doesn't change; to change
497 * the list you need to lock both the mutex and the spinlock.
498 */
499 struct mutex mutex;
Ingo Molnar04289bb2008-12-11 08:38:42 +0100500
501 struct list_head counter_list;
Peter Zijlstra592903c2009-03-13 12:21:36 +0100502 struct list_head event_list;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100503 int nr_counters;
504 int nr_active;
Paul Mackerrasd859e292009-01-17 18:10:22 +1100505 int is_active;
Paul Mackerrasa63eaf32009-05-22 14:17:31 +1000506 atomic_t refcount;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100507 struct task_struct *task;
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100508
509 /*
Peter Zijlstra4af49982009-04-06 11:45:10 +0200510 * Context clock, runs when context enabled.
Paul Mackerras53cfbf52009-03-25 22:46:58 +1100511 */
Peter Zijlstra4af49982009-04-06 11:45:10 +0200512 u64 time;
513 u64 timestamp;
Paul Mackerras564c2b22009-05-22 14:27:22 +1000514
515 /*
516 * These fields let us detect when two contexts have both
517 * been cloned (inherited) from a common ancestor.
518 */
519 struct perf_counter_context *parent_ctx;
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000520 u64 parent_gen;
521 u64 generation;
Paul Mackerras25346b932009-06-01 17:48:12 +1000522 int pin_count;
Paul Mackerrasc93f7662009-05-28 22:18:17 +1000523 struct rcu_head rcu_head;
Thomas Gleixner0793a612008-12-04 20:12:29 +0100524};
525
526/**
527 * struct perf_counter_cpu_context - per cpu counter context structure
528 */
529struct perf_cpu_context {
530 struct perf_counter_context ctx;
531 struct perf_counter_context *task_ctx;
532 int active_oncpu;
533 int max_pertask;
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100534 int exclusive;
Peter Zijlstra96f6d442009-03-23 18:22:07 +0100535
536 /*
537 * Recursion avoidance:
538 *
539 * task, softirq, irq, nmi context
540 */
Ingo Molnar22a4f652009-06-01 10:13:37 +0200541 int recursion[4];
Thomas Gleixner0793a612008-12-04 20:12:29 +0100542};
543
Robert Richter829b42d2009-04-29 12:46:59 +0200544#ifdef CONFIG_PERF_COUNTERS
545
Thomas Gleixner0793a612008-12-04 20:12:29 +0100546/*
547 * Set by architecture code:
548 */
549extern int perf_max_counters;
550
Robert Richter4aeb0b42009-04-29 12:47:03 +0200551extern const struct pmu *hw_perf_counter_init(struct perf_counter *counter);
Ingo Molnar621a01e2008-12-11 12:46:46 +0100552
Thomas Gleixner0793a612008-12-04 20:12:29 +0100553extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
Paul Mackerras564c2b22009-05-22 14:27:22 +1000554extern void perf_counter_task_sched_out(struct task_struct *task,
555 struct task_struct *next, int cpu);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100556extern void perf_counter_task_tick(struct task_struct *task, int cpu);
Peter Zijlstra6ab423e2009-05-25 14:45:27 +0200557extern int perf_counter_init_task(struct task_struct *child);
Ingo Molnar9b51f662008-12-12 13:49:45 +0100558extern void perf_counter_exit_task(struct task_struct *child);
Peter Zijlstrabbbee902009-05-29 14:25:58 +0200559extern void perf_counter_free_task(struct task_struct *task);
Peter Zijlstra925d5192009-03-30 19:07:02 +0200560extern void perf_counter_do_pending(void);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100561extern void perf_counter_print_debug(void);
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200562extern void __perf_disable(void);
563extern bool __perf_enable(void);
564extern void perf_disable(void);
565extern void perf_enable(void);
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100566extern int perf_counter_task_disable(void);
567extern int perf_counter_task_enable(void);
Paul Mackerras3cbed422009-01-09 16:43:42 +1100568extern int hw_perf_group_sched_in(struct perf_counter *group_leader,
569 struct perf_cpu_context *cpuctx,
570 struct perf_counter_context *ctx, int cpu);
Paul Mackerras37d81822009-03-23 18:22:08 +0100571extern void perf_counter_update_userpage(struct perf_counter *counter);
Ingo Molnar5c92d122008-12-11 13:21:10 +0100572
Peter Zijlstraf6c7d5f2009-04-06 11:45:04 +0200573extern int perf_counter_overflow(struct perf_counter *counter,
Peter Zijlstra78f13e92009-04-08 15:01:33 +0200574 int nmi, struct pt_regs *regs, u64 addr);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100575/*
576 * Return 1 for a software counter, 0 for a hardware counter
577 */
578static inline int is_software_counter(struct perf_counter *counter)
579{
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200580 return (counter->attr.type != PERF_TYPE_RAW) &&
581 (counter->attr.type != PERF_TYPE_HARDWARE);
Paul Mackerras3b6f9e52009-01-14 21:00:30 +1100582}
583
Peter Zijlstra78f13e92009-04-08 15:01:33 +0200584extern void perf_swcounter_event(u32, u64, int, struct pt_regs *, u64);
Peter Zijlstra15dbf272009-03-13 12:21:32 +0100585
Peter Zijlstra089dd792009-06-05 14:04:55 +0200586extern void __perf_counter_mmap(struct vm_area_struct *vma);
587
588static inline void perf_counter_mmap(struct vm_area_struct *vma)
589{
590 if (vma->vm_flags & VM_EXEC)
591 __perf_counter_mmap(vma);
592}
Peter Zijlstra0a4a9392009-03-30 19:07:05 +0200593
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +0200594extern void perf_counter_comm(struct task_struct *tsk);
Peter Zijlstra60313eb2009-06-04 16:53:44 +0200595extern void perf_counter_fork(struct task_struct *tsk);
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +0200596
Paul Mackerras3f731ca2009-06-01 17:52:30 +1000597extern void perf_counter_task_migration(struct task_struct *task, int cpu);
598
Peter Zijlstra9c03d882009-04-06 11:45:00 +0200599#define MAX_STACK_DEPTH 255
Peter Zijlstra394ee072009-03-30 19:07:14 +0200600
601struct perf_callchain_entry {
Peter Zijlstra9c03d882009-04-06 11:45:00 +0200602 u16 nr, hv, kernel, user;
Peter Zijlstra394ee072009-03-30 19:07:14 +0200603 u64 ip[MAX_STACK_DEPTH];
604};
605
606extern struct perf_callchain_entry *perf_callchain(struct pt_regs *regs);
607
Peter Zijlstra1ccd1542009-04-09 10:53:45 +0200608extern int sysctl_perf_counter_priv;
Peter Zijlstrac5078f72009-05-05 17:50:24 +0200609extern int sysctl_perf_counter_mlock;
Peter Zijlstraa78ac322009-05-25 17:39:05 +0200610extern int sysctl_perf_counter_limit;
Peter Zijlstra1ccd1542009-04-09 10:53:45 +0200611
Ingo Molnar0d905bc2009-05-04 19:13:30 +0200612extern void perf_counter_init(void);
613
Paul Mackerras9d23a902009-05-14 21:48:08 +1000614#ifndef perf_misc_flags
615#define perf_misc_flags(regs) (user_mode(regs) ? PERF_EVENT_MISC_USER : \
616 PERF_EVENT_MISC_KERNEL)
617#define perf_instruction_pointer(regs) instruction_pointer(regs)
618#endif
619
Thomas Gleixner0793a612008-12-04 20:12:29 +0100620#else
621static inline void
622perf_counter_task_sched_in(struct task_struct *task, int cpu) { }
623static inline void
Ingo Molnar910431c2009-05-22 12:32:15 +0200624perf_counter_task_sched_out(struct task_struct *task,
625 struct task_struct *next, int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100626static inline void
627perf_counter_task_tick(struct task_struct *task, int cpu) { }
Ingo Molnard3e78ee2009-05-28 11:41:50 +0200628static inline int perf_counter_init_task(struct task_struct *child) { return 0; }
Ingo Molnar9b51f662008-12-12 13:49:45 +0100629static inline void perf_counter_exit_task(struct task_struct *child) { }
Peter Zijlstrabbbee902009-05-29 14:25:58 +0200630static inline void perf_counter_free_task(struct task_struct *task) { }
Peter Zijlstra925d5192009-03-30 19:07:02 +0200631static inline void perf_counter_do_pending(void) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100632static inline void perf_counter_print_debug(void) { }
Peter Zijlstra9e35ad32009-05-13 16:21:38 +0200633static inline void perf_disable(void) { }
634static inline void perf_enable(void) { }
Ingo Molnar1d1c7dd2008-12-11 14:59:31 +0100635static inline int perf_counter_task_disable(void) { return -EINVAL; }
636static inline int perf_counter_task_enable(void) { return -EINVAL; }
Peter Zijlstra15dbf272009-03-13 12:21:32 +0100637
Peter Zijlstra925d5192009-03-30 19:07:02 +0200638static inline void
Peter Zijlstra78f13e92009-04-08 15:01:33 +0200639perf_swcounter_event(u32 event, u64 nr, int nmi,
640 struct pt_regs *regs, u64 addr) { }
Peter Zijlstra0a4a9392009-03-30 19:07:05 +0200641
Peter Zijlstra089dd792009-06-05 14:04:55 +0200642static inline void perf_counter_mmap(struct vm_area_struct *vma) { }
Peter Zijlstra8d1b2d92009-04-08 15:01:30 +0200643static inline void perf_counter_comm(struct task_struct *tsk) { }
Peter Zijlstra60313eb2009-06-04 16:53:44 +0200644static inline void perf_counter_fork(struct task_struct *tsk) { }
Ingo Molnar0d905bc2009-05-04 19:13:30 +0200645static inline void perf_counter_init(void) { }
Paul Mackerras3f731ca2009-06-01 17:52:30 +1000646static inline void perf_counter_task_migration(struct task_struct *task,
647 int cpu) { }
Thomas Gleixner0793a612008-12-04 20:12:29 +0100648#endif
649
Paul Mackerrasf3dfd262009-02-26 22:43:46 +1100650#endif /* __KERNEL__ */
Thomas Gleixner0793a612008-12-04 20:12:29 +0100651#endif /* _LINUX_PERF_COUNTER_H */