x86: Add support for Intel Cache QoS Monitoring (CQM) detection
[deliverable/linux.git] / include / linux / perf_event.h
CommitLineData
0793a61d 1/*
57c0c15b 2 * Performance events:
0793a61d 3 *
a308444c 4 * Copyright (C) 2008-2009, Thomas Gleixner <tglx@linutronix.de>
e7e7ee2e
IM
5 * Copyright (C) 2008-2011, Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2011, Red Hat, Inc., Peter Zijlstra
0793a61d 7 *
57c0c15b 8 * Data type definitions, declarations, prototypes.
0793a61d 9 *
a308444c 10 * Started by: Thomas Gleixner and Ingo Molnar
0793a61d 11 *
57c0c15b 12 * For licencing details see kernel-base/COPYING
0793a61d 13 */
cdd6c482
IM
14#ifndef _LINUX_PERF_EVENT_H
15#define _LINUX_PERF_EVENT_H
0793a61d 16
607ca46e 17#include <uapi/linux/perf_event.h>
0793a61d 18
9f66a381 19/*
f3dfd265 20 * Kernel-internal data types and definitions:
9f66a381
IM
21 */
22
cdd6c482
IM
23#ifdef CONFIG_PERF_EVENTS
24# include <asm/perf_event.h>
7be79236 25# include <asm/local64.h>
f3dfd265
PM
26#endif
27
39447b38 28struct perf_guest_info_callbacks {
e7e7ee2e
IM
29 int (*is_in_guest)(void);
30 int (*is_user_mode)(void);
31 unsigned long (*get_guest_ip)(void);
39447b38
ZY
32};
33
2ff6cfd7
AB
34#ifdef CONFIG_HAVE_HW_BREAKPOINT
35#include <asm/hw_breakpoint.h>
36#endif
37
f3dfd265
PM
38#include <linux/list.h>
39#include <linux/mutex.h>
40#include <linux/rculist.h>
41#include <linux/rcupdate.h>
42#include <linux/spinlock.h>
d6d020e9 43#include <linux/hrtimer.h>
3c446b3d 44#include <linux/fs.h>
709e50cf 45#include <linux/pid_namespace.h>
906010b2 46#include <linux/workqueue.h>
5331d7b8 47#include <linux/ftrace.h>
85cfabbc 48#include <linux/cpu.h>
e360adbe 49#include <linux/irq_work.h>
c5905afb 50#include <linux/static_key.h>
851cf6e7 51#include <linux/jump_label_ratelimit.h>
60063497 52#include <linux/atomic.h>
641cc938 53#include <linux/sysfs.h>
4018994f 54#include <linux/perf_regs.h>
fadfe7be 55#include <linux/workqueue.h>
39bed6cb 56#include <linux/cgroup.h>
fa588151 57#include <asm/local.h>
f3dfd265 58
f9188e02
PZ
59struct perf_callchain_entry {
60 __u64 nr;
61 __u64 ip[PERF_MAX_STACK_DEPTH];
62};
63
3a43ce68
FW
64struct perf_raw_record {
65 u32 size;
66 void *data;
f413cdb8
FW
67};
68
bce38cd5
SE
69/*
70 * branch stack layout:
71 * nr: number of taken branches stored in entries[]
72 *
73 * Note that nr can vary from sample to sample
74 * branches (to, from) are stored from most recent
75 * to least recent, i.e., entries[0] contains the most
76 * recent branch.
77 */
caff2bef
PZ
78struct perf_branch_stack {
79 __u64 nr;
80 struct perf_branch_entry entries[0];
81};
82
f3dfd265
PM
83struct task_struct;
84
efc9f05d
SE
85/*
86 * extra PMU register associated with an event
87 */
88struct hw_perf_event_extra {
89 u64 config; /* register value */
90 unsigned int reg; /* register address or index */
91 int alloc; /* extra register already allocated */
92 int idx; /* index in shared_regs->regs[] */
93};
94
43b45780
AH
95struct event_constraint;
96
0793a61d 97/**
cdd6c482 98 * struct hw_perf_event - performance event hardware details:
0793a61d 99 */
cdd6c482
IM
100struct hw_perf_event {
101#ifdef CONFIG_PERF_EVENTS
d6d020e9
PZ
102 union {
103 struct { /* hardware */
a308444c 104 u64 config;
447a194b 105 u64 last_tag;
a308444c 106 unsigned long config_base;
cdd6c482 107 unsigned long event_base;
c48b6053 108 int event_base_rdpmc;
a308444c 109 int idx;
447a194b 110 int last_cpu;
9fac2cf3 111 int flags;
bce38cd5 112
efc9f05d 113 struct hw_perf_event_extra extra_reg;
bce38cd5 114 struct hw_perf_event_extra branch_reg;
43b45780
AH
115
116 struct event_constraint *constraint;
d6d020e9 117 };
721a669b 118 struct { /* software */
a308444c 119 struct hrtimer hrtimer;
d6d020e9 120 };
f22c1bb6
ON
121 struct { /* tracepoint */
122 struct task_struct *tp_target;
123 /* for tp_event->class */
124 struct list_head tp_list;
125 };
24f1e32c 126#ifdef CONFIG_HAVE_HW_BREAKPOINT
45a73372 127 struct { /* breakpoint */
d580ff86
PZ
128 /*
129 * Crufty hack to avoid the chicken and egg
130 * problem hw_breakpoint has with context
131 * creation and event initalization.
132 */
133 struct task_struct *bp_target;
f22c1bb6
ON
134 struct arch_hw_breakpoint info;
135 struct list_head bp_list;
45a73372 136 };
24f1e32c 137#endif
d6d020e9 138 };
a4eaf7f1 139 int state;
e7850595 140 local64_t prev_count;
b23f3325 141 u64 sample_period;
9e350de3 142 u64 last_period;
e7850595 143 local64_t period_left;
e050e3f0 144 u64 interrupts_seq;
60db5e09 145 u64 interrupts;
6a24ed6c 146
abd50713
PZ
147 u64 freq_time_stamp;
148 u64 freq_count_stamp;
ee06094f 149#endif
0793a61d
TG
150};
151
a4eaf7f1
PZ
152/*
153 * hw_perf_event::state flags
154 */
155#define PERF_HES_STOPPED 0x01 /* the counter is stopped */
156#define PERF_HES_UPTODATE 0x02 /* event->count up-to-date */
157#define PERF_HES_ARCH 0x04
158
cdd6c482 159struct perf_event;
621a01ea 160
8d2cacbb
PZ
161/*
162 * Common implementation detail of pmu::{start,commit,cancel}_txn
163 */
164#define PERF_EVENT_TXN 0x1
6bde9b6c 165
53b25335
VW
166/**
167 * pmu::capabilities flags
168 */
169#define PERF_PMU_CAP_NO_INTERRUPT 0x01
170
621a01ea 171/**
4aeb0b42 172 * struct pmu - generic performance monitoring unit
621a01ea 173 */
4aeb0b42 174struct pmu {
b0a873eb
PZ
175 struct list_head entry;
176
c464c76e 177 struct module *module;
abe43400 178 struct device *dev;
0c9d42ed 179 const struct attribute_group **attr_groups;
03d8e80b 180 const char *name;
2e80a82a
PZ
181 int type;
182
53b25335
VW
183 /*
184 * various common per-pmu feature flags
185 */
186 int capabilities;
187
108b02cf
PZ
188 int * __percpu pmu_disable_count;
189 struct perf_cpu_context * __percpu pmu_cpu_context;
8dc85d54 190 int task_ctx_nr;
62b85639 191 int hrtimer_interval_ms;
6bde9b6c
LM
192
193 /*
a4eaf7f1
PZ
194 * Fully disable/enable this PMU, can be used to protect from the PMI
195 * as well as for lazy/batch writing of the MSRs.
6bde9b6c 196 */
ad5133b7
PZ
197 void (*pmu_enable) (struct pmu *pmu); /* optional */
198 void (*pmu_disable) (struct pmu *pmu); /* optional */
6bde9b6c 199
8d2cacbb 200 /*
a4eaf7f1 201 * Try and initialize the event for this PMU.
24cd7f54 202 * Should return -ENOENT when the @event doesn't match this PMU.
8d2cacbb 203 */
b0a873eb
PZ
204 int (*event_init) (struct perf_event *event);
205
1e0fb9ec
AL
206 /*
207 * Notification that the event was mapped or unmapped. Called
208 * in the context of the mapping task.
209 */
210 void (*event_mapped) (struct perf_event *event); /*optional*/
211 void (*event_unmapped) (struct perf_event *event); /*optional*/
212
a4eaf7f1
PZ
213#define PERF_EF_START 0x01 /* start the counter when adding */
214#define PERF_EF_RELOAD 0x02 /* reload the counter when starting */
215#define PERF_EF_UPDATE 0x04 /* update the counter when stopping */
216
8d2cacbb 217 /*
a4eaf7f1
PZ
218 * Adds/Removes a counter to/from the PMU, can be done inside
219 * a transaction, see the ->*_txn() methods.
220 */
221 int (*add) (struct perf_event *event, int flags);
222 void (*del) (struct perf_event *event, int flags);
223
224 /*
225 * Starts/Stops a counter present on the PMU. The PMI handler
226 * should stop the counter when perf_event_overflow() returns
227 * !0. ->start() will be used to continue.
228 */
229 void (*start) (struct perf_event *event, int flags);
230 void (*stop) (struct perf_event *event, int flags);
231
232 /*
233 * Updates the counter value of the event.
234 */
cdd6c482 235 void (*read) (struct perf_event *event);
6bde9b6c
LM
236
237 /*
24cd7f54
PZ
238 * Group events scheduling is treated as a transaction, add
239 * group events as a whole and perform one schedulability test.
240 * If the test fails, roll back the whole group
a4eaf7f1
PZ
241 *
242 * Start the transaction, after this ->add() doesn't need to
24cd7f54 243 * do schedulability tests.
8d2cacbb 244 */
e7e7ee2e 245 void (*start_txn) (struct pmu *pmu); /* optional */
8d2cacbb 246 /*
a4eaf7f1 247 * If ->start_txn() disabled the ->add() schedulability test
8d2cacbb
PZ
248 * then ->commit_txn() is required to perform one. On success
249 * the transaction is closed. On error the transaction is kept
250 * open until ->cancel_txn() is called.
251 */
e7e7ee2e 252 int (*commit_txn) (struct pmu *pmu); /* optional */
8d2cacbb 253 /*
a4eaf7f1 254 * Will cancel the transaction, assumes ->del() is called
25985edc 255 * for each successful ->add() during the transaction.
8d2cacbb 256 */
e7e7ee2e 257 void (*cancel_txn) (struct pmu *pmu); /* optional */
35edc2a5
PZ
258
259 /*
260 * Will return the value for perf_event_mmap_page::index for this event,
261 * if no implementation is provided it will default to: event->hw.idx + 1.
262 */
263 int (*event_idx) (struct perf_event *event); /*optional */
d010b332 264
ba532500
YZ
265 /*
266 * context-switches callback
267 */
268 void (*sched_task) (struct perf_event_context *ctx,
269 bool sched_in);
4af57ef2
YZ
270 /*
271 * PMU specific data size
272 */
273 size_t task_ctx_size;
ba532500 274
eacd3ecc
MF
275
276 /*
277 * Return the count value for a counter.
278 */
279 u64 (*count) (struct perf_event *event); /*optional*/
621a01ea
IM
280};
281
6a930700 282/**
cdd6c482 283 * enum perf_event_active_state - the states of a event
6a930700 284 */
cdd6c482 285enum perf_event_active_state {
179033b3 286 PERF_EVENT_STATE_EXIT = -3,
57c0c15b 287 PERF_EVENT_STATE_ERROR = -2,
cdd6c482
IM
288 PERF_EVENT_STATE_OFF = -1,
289 PERF_EVENT_STATE_INACTIVE = 0,
57c0c15b 290 PERF_EVENT_STATE_ACTIVE = 1,
6a930700
IM
291};
292
9b51f66d 293struct file;
453f19ee
PZ
294struct perf_sample_data;
295
a8b0ca17 296typedef void (*perf_overflow_handler_t)(struct perf_event *,
b326e956
FW
297 struct perf_sample_data *,
298 struct pt_regs *regs);
299
d6f962b5 300enum perf_group_flag {
e7e7ee2e 301 PERF_GROUP_SOFTWARE = 0x1,
d6f962b5
FW
302};
303
e7e7ee2e
IM
304#define SWEVENT_HLIST_BITS 8
305#define SWEVENT_HLIST_SIZE (1 << SWEVENT_HLIST_BITS)
76e1d904
FW
306
307struct swevent_hlist {
e7e7ee2e
IM
308 struct hlist_head heads[SWEVENT_HLIST_SIZE];
309 struct rcu_head rcu_head;
76e1d904
FW
310};
311
8a49542c
PZ
312#define PERF_ATTACH_CONTEXT 0x01
313#define PERF_ATTACH_GROUP 0x02
d580ff86 314#define PERF_ATTACH_TASK 0x04
4af57ef2 315#define PERF_ATTACH_TASK_DATA 0x08
8a49542c 316
877c6856 317struct perf_cgroup;
76369139
FW
318struct ring_buffer;
319
0793a61d 320/**
cdd6c482 321 * struct perf_event - performance event kernel representation:
0793a61d 322 */
cdd6c482
IM
323struct perf_event {
324#ifdef CONFIG_PERF_EVENTS
9886167d
PZ
325 /*
326 * entry onto perf_event_context::event_list;
327 * modifications require ctx->lock
328 * RCU safe iterations.
329 */
592903cd 330 struct list_head event_entry;
9886167d
PZ
331
332 /*
333 * XXX: group_entry and sibling_list should be mutually exclusive;
334 * either you're a sibling on a group, or you're the group leader.
335 * Rework the code to always use the same list element.
336 *
337 * Locked for modification by both ctx->mutex and ctx->lock; holding
338 * either sufficies for read.
339 */
340 struct list_head group_entry;
04289bb9 341 struct list_head sibling_list;
9886167d
PZ
342
343 /*
344 * We need storage to track the entries in perf_pmu_migrate_context; we
345 * cannot use the event_entry because of RCU and we want to keep the
346 * group in tact which avoids us using the other two entries.
347 */
348 struct list_head migrate_entry;
349
f3ae75de
SE
350 struct hlist_node hlist_entry;
351 struct list_head active_entry;
0127c3ea 352 int nr_siblings;
d6f962b5 353 int group_flags;
cdd6c482 354 struct perf_event *group_leader;
a4eaf7f1 355 struct pmu *pmu;
04289bb9 356
cdd6c482 357 enum perf_event_active_state state;
8a49542c 358 unsigned int attach_state;
e7850595 359 local64_t count;
a6e6dea6 360 atomic64_t child_count;
ee06094f 361
53cfbf59 362 /*
cdd6c482 363 * These are the total time in nanoseconds that the event
53cfbf59 364 * has been enabled (i.e. eligible to run, and the task has
cdd6c482 365 * been scheduled in, if this is a per-task event)
53cfbf59
PM
366 * and running (scheduled onto the CPU), respectively.
367 *
368 * They are computed from tstamp_enabled, tstamp_running and
cdd6c482 369 * tstamp_stopped when the event is in INACTIVE or ACTIVE state.
53cfbf59
PM
370 */
371 u64 total_time_enabled;
372 u64 total_time_running;
373
374 /*
375 * These are timestamps used for computing total_time_enabled
cdd6c482 376 * and total_time_running when the event is in INACTIVE or
53cfbf59
PM
377 * ACTIVE state, measured in nanoseconds from an arbitrary point
378 * in time.
cdd6c482
IM
379 * tstamp_enabled: the notional time when the event was enabled
380 * tstamp_running: the notional time when the event was scheduled on
53cfbf59 381 * tstamp_stopped: in INACTIVE state, the notional time when the
cdd6c482 382 * event was scheduled off.
53cfbf59
PM
383 */
384 u64 tstamp_enabled;
385 u64 tstamp_running;
386 u64 tstamp_stopped;
387
eed01528
SE
388 /*
389 * timestamp shadows the actual context timing but it can
390 * be safely used in NMI interrupt context. It reflects the
391 * context time as it was when the event was last scheduled in.
392 *
393 * ctx_time already accounts for ctx->timestamp. Therefore to
394 * compute ctx_time for a sample, simply add perf_clock().
395 */
396 u64 shadow_ctx_time;
397
24f1e32c 398 struct perf_event_attr attr;
c320c7b7 399 u16 header_size;
6844c09d 400 u16 id_header_size;
c320c7b7 401 u16 read_size;
cdd6c482 402 struct hw_perf_event hw;
0793a61d 403
cdd6c482 404 struct perf_event_context *ctx;
a6fa941d 405 atomic_long_t refcount;
0793a61d 406
53cfbf59
PM
407 /*
408 * These accumulate total time (in nanoseconds) that children
cdd6c482 409 * events have been enabled and running, respectively.
53cfbf59
PM
410 */
411 atomic64_t child_total_time_enabled;
412 atomic64_t child_total_time_running;
413
0793a61d 414 /*
d859e29f 415 * Protect attach/detach and child_list:
0793a61d 416 */
fccc714b
PZ
417 struct mutex child_mutex;
418 struct list_head child_list;
cdd6c482 419 struct perf_event *parent;
0793a61d
TG
420
421 int oncpu;
422 int cpu;
423
082ff5a2
PZ
424 struct list_head owner_entry;
425 struct task_struct *owner;
426
7b732a75
PZ
427 /* mmap bits */
428 struct mutex mmap_mutex;
429 atomic_t mmap_count;
26cb63ad 430
76369139 431 struct ring_buffer *rb;
10c6db11 432 struct list_head rb_entry;
b69cf536
PZ
433 unsigned long rcu_batches;
434 int rcu_pending;
37d81828 435
7b732a75 436 /* poll related */
0793a61d 437 wait_queue_head_t waitq;
3c446b3d 438 struct fasync_struct *fasync;
79f14641
PZ
439
440 /* delayed work for NMIs and such */
441 int pending_wakeup;
4c9e2542 442 int pending_kill;
79f14641 443 int pending_disable;
e360adbe 444 struct irq_work pending;
592903cd 445
79f14641
PZ
446 atomic_t event_limit;
447
cdd6c482 448 void (*destroy)(struct perf_event *);
592903cd 449 struct rcu_head rcu_head;
709e50cf
PZ
450
451 struct pid_namespace *ns;
8e5799b1 452 u64 id;
6fb2915d 453
b326e956 454 perf_overflow_handler_t overflow_handler;
4dc0da86 455 void *overflow_handler_context;
453f19ee 456
07b139c8 457#ifdef CONFIG_EVENT_TRACING
1c024eca 458 struct ftrace_event_call *tp_event;
6fb2915d 459 struct event_filter *filter;
ced39002
JO
460#ifdef CONFIG_FUNCTION_TRACER
461 struct ftrace_ops ftrace_ops;
462#endif
ee06094f 463#endif
6fb2915d 464
e5d1367f
SE
465#ifdef CONFIG_CGROUP_PERF
466 struct perf_cgroup *cgrp; /* cgroup event is attach to */
467 int cgrp_defer_enabled;
468#endif
469
6fb2915d 470#endif /* CONFIG_PERF_EVENTS */
0793a61d
TG
471};
472
473/**
cdd6c482 474 * struct perf_event_context - event context structure
0793a61d 475 *
cdd6c482 476 * Used as a container for task events and CPU events as well:
0793a61d 477 */
cdd6c482 478struct perf_event_context {
108b02cf 479 struct pmu *pmu;
0793a61d 480 /*
cdd6c482 481 * Protect the states of the events in the list,
d859e29f 482 * nr_active, and the list:
0793a61d 483 */
e625cce1 484 raw_spinlock_t lock;
d859e29f 485 /*
cdd6c482 486 * Protect the list of events. Locking either mutex or lock
d859e29f
PM
487 * is sufficient to ensure the list doesn't change; to change
488 * the list you need to lock both the mutex and the spinlock.
489 */
a308444c 490 struct mutex mutex;
04289bb9 491
2fde4f94 492 struct list_head active_ctx_list;
889ff015
FW
493 struct list_head pinned_groups;
494 struct list_head flexible_groups;
a308444c 495 struct list_head event_list;
cdd6c482 496 int nr_events;
a308444c
IM
497 int nr_active;
498 int is_active;
bfbd3381 499 int nr_stat;
0f5a2601 500 int nr_freq;
dddd3379 501 int rotate_disable;
a308444c
IM
502 atomic_t refcount;
503 struct task_struct *task;
53cfbf59
PM
504
505 /*
4af4998b 506 * Context clock, runs when context enabled.
53cfbf59 507 */
a308444c
IM
508 u64 time;
509 u64 timestamp;
564c2b21
PM
510
511 /*
512 * These fields let us detect when two contexts have both
513 * been cloned (inherited) from a common ancestor.
514 */
cdd6c482 515 struct perf_event_context *parent_ctx;
a308444c
IM
516 u64 parent_gen;
517 u64 generation;
518 int pin_count;
d010b332 519 int nr_cgroups; /* cgroup evts */
4af57ef2 520 void *task_ctx_data; /* pmu specific data */
28009ce4 521 struct rcu_head rcu_head;
fadfe7be
JO
522
523 struct delayed_work orphans_remove;
524 bool orphans_remove_sched;
0793a61d
TG
525};
526
7ae07ea3
FW
527/*
528 * Number of contexts where an event can trigger:
e7e7ee2e 529 * task, softirq, hardirq, nmi.
7ae07ea3
FW
530 */
531#define PERF_NR_CONTEXTS 4
532
0793a61d 533/**
cdd6c482 534 * struct perf_event_cpu_context - per cpu event context structure
0793a61d
TG
535 */
536struct perf_cpu_context {
cdd6c482
IM
537 struct perf_event_context ctx;
538 struct perf_event_context *task_ctx;
0793a61d 539 int active_oncpu;
3b6f9e5c 540 int exclusive;
9e630205
SE
541 struct hrtimer hrtimer;
542 ktime_t hrtimer_interval;
3f1f3320 543 struct pmu *unique_pmu;
e5d1367f 544 struct perf_cgroup *cgrp;
0793a61d
TG
545};
546
5622f295 547struct perf_output_handle {
57c0c15b 548 struct perf_event *event;
76369139 549 struct ring_buffer *rb;
6d1acfd5 550 unsigned long wakeup;
5d967a8b
PZ
551 unsigned long size;
552 void *addr;
553 int page;
5622f295
MM
554};
555
39bed6cb
MF
556#ifdef CONFIG_CGROUP_PERF
557
558/*
559 * perf_cgroup_info keeps track of time_enabled for a cgroup.
560 * This is a per-cpu dynamically allocated data structure.
561 */
562struct perf_cgroup_info {
563 u64 time;
564 u64 timestamp;
565};
566
567struct perf_cgroup {
568 struct cgroup_subsys_state css;
569 struct perf_cgroup_info __percpu *info;
570};
571
572/*
573 * Must ensure cgroup is pinned (css_get) before calling
574 * this function. In other words, we cannot call this function
575 * if there is no cgroup event for the current CPU context.
576 */
577static inline struct perf_cgroup *
578perf_cgroup_from_task(struct task_struct *task)
579{
580 return container_of(task_css(task, perf_event_cgrp_id),
581 struct perf_cgroup, css);
582}
583#endif /* CONFIG_CGROUP_PERF */
584
cdd6c482 585#ifdef CONFIG_PERF_EVENTS
829b42dd 586
03d8e80b 587extern int perf_pmu_register(struct pmu *pmu, const char *name, int type);
b0a873eb 588extern void perf_pmu_unregister(struct pmu *pmu);
621a01ea 589
3bf101ba 590extern int perf_num_counters(void);
84c79910 591extern const char *perf_pmu_name(void);
ab0cce56
JO
592extern void __perf_event_task_sched_in(struct task_struct *prev,
593 struct task_struct *task);
594extern void __perf_event_task_sched_out(struct task_struct *prev,
595 struct task_struct *next);
cdd6c482
IM
596extern int perf_event_init_task(struct task_struct *child);
597extern void perf_event_exit_task(struct task_struct *child);
598extern void perf_event_free_task(struct task_struct *task);
4e231c79 599extern void perf_event_delayed_put(struct task_struct *task);
cdd6c482 600extern void perf_event_print_debug(void);
33696fc0
PZ
601extern void perf_pmu_disable(struct pmu *pmu);
602extern void perf_pmu_enable(struct pmu *pmu);
ba532500
YZ
603extern void perf_sched_cb_dec(struct pmu *pmu);
604extern void perf_sched_cb_inc(struct pmu *pmu);
cdd6c482
IM
605extern int perf_event_task_disable(void);
606extern int perf_event_task_enable(void);
26ca5c11 607extern int perf_event_refresh(struct perf_event *event, int refresh);
cdd6c482 608extern void perf_event_update_userpage(struct perf_event *event);
fb0459d7
AV
609extern int perf_event_release_kernel(struct perf_event *event);
610extern struct perf_event *
611perf_event_create_kernel_counter(struct perf_event_attr *attr,
612 int cpu,
38a81da2 613 struct task_struct *task,
4dc0da86
AK
614 perf_overflow_handler_t callback,
615 void *context);
0cda4c02
YZ
616extern void perf_pmu_migrate_context(struct pmu *pmu,
617 int src_cpu, int dst_cpu);
59ed446f
PZ
618extern u64 perf_event_read_value(struct perf_event *event,
619 u64 *enabled, u64 *running);
5c92d124 620
d010b332 621
df1a132b 622struct perf_sample_data {
2565711f
PZ
623 /*
624 * Fields set by perf_sample_data_init(), group so as to
625 * minimize the cachelines touched.
626 */
627 u64 addr;
628 struct perf_raw_record *raw;
629 struct perf_branch_stack *br_stack;
630 u64 period;
631 u64 weight;
632 u64 txn;
633 union perf_mem_data_src data_src;
5622f295 634
2565711f
PZ
635 /*
636 * The other fields, optionally {set,used} by
637 * perf_{prepare,output}_sample().
638 */
639 u64 type;
5622f295
MM
640 u64 ip;
641 struct {
642 u32 pid;
643 u32 tid;
644 } tid_entry;
645 u64 time;
5622f295
MM
646 u64 id;
647 u64 stream_id;
648 struct {
649 u32 cpu;
650 u32 reserved;
651 } cpu_entry;
5622f295 652 struct perf_callchain_entry *callchain;
88a7c26a
AL
653
654 /*
655 * regs_user may point to task_pt_regs or to regs_user_copy, depending
656 * on arch details.
657 */
60e2364e 658 struct perf_regs regs_user;
88a7c26a
AL
659 struct pt_regs regs_user_copy;
660
60e2364e 661 struct perf_regs regs_intr;
c5ebcedb 662 u64 stack_user_size;
2565711f 663} ____cacheline_aligned;
df1a132b 664
770eee1f
SE
665/* default value for data source */
666#define PERF_MEM_NA (PERF_MEM_S(OP, NA) |\
667 PERF_MEM_S(LVL, NA) |\
668 PERF_MEM_S(SNOOP, NA) |\
669 PERF_MEM_S(LOCK, NA) |\
670 PERF_MEM_S(TLB, NA))
671
fd0d000b
RR
672static inline void perf_sample_data_init(struct perf_sample_data *data,
673 u64 addr, u64 period)
dc1d628a 674{
fd0d000b 675 /* remaining struct members initialized in perf_prepare_sample() */
dc1d628a
PZ
676 data->addr = addr;
677 data->raw = NULL;
bce38cd5 678 data->br_stack = NULL;
4018994f 679 data->period = period;
c3feedf2 680 data->weight = 0;
770eee1f 681 data->data_src.val = PERF_MEM_NA;
fdfbbd07 682 data->txn = 0;
dc1d628a
PZ
683}
684
5622f295
MM
685extern void perf_output_sample(struct perf_output_handle *handle,
686 struct perf_event_header *header,
687 struct perf_sample_data *data,
cdd6c482 688 struct perf_event *event);
5622f295
MM
689extern void perf_prepare_sample(struct perf_event_header *header,
690 struct perf_sample_data *data,
cdd6c482 691 struct perf_event *event,
5622f295
MM
692 struct pt_regs *regs);
693
a8b0ca17 694extern int perf_event_overflow(struct perf_event *event,
5622f295
MM
695 struct perf_sample_data *data,
696 struct pt_regs *regs);
df1a132b 697
6c7e550f
FBH
698static inline bool is_sampling_event(struct perf_event *event)
699{
700 return event->attr.sample_period != 0;
701}
702
3b6f9e5c 703/*
cdd6c482 704 * Return 1 for a software event, 0 for a hardware event
3b6f9e5c 705 */
cdd6c482 706static inline int is_software_event(struct perf_event *event)
3b6f9e5c 707{
89a1e187 708 return event->pmu->task_ctx_nr == perf_sw_context;
3b6f9e5c
PM
709}
710
c5905afb 711extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
f29ac756 712
86038c5e 713extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64);
a8b0ca17 714extern void __perf_sw_event(u32, u64, struct pt_regs *, u64);
f29ac756 715
b0f82b81 716#ifndef perf_arch_fetch_caller_regs
e7e7ee2e 717static inline void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { }
b0f82b81 718#endif
5331d7b8
FW
719
720/*
721 * Take a snapshot of the regs. Skip ip and frame pointer to
722 * the nth caller. We only need a few of the regs:
723 * - ip for PERF_SAMPLE_IP
724 * - cs for user_mode() tests
725 * - bp for callchains
726 * - eflags, for future purposes, just in case
727 */
b0f82b81 728static inline void perf_fetch_caller_regs(struct pt_regs *regs)
5331d7b8 729{
5331d7b8
FW
730 memset(regs, 0, sizeof(*regs));
731
b0f82b81 732 perf_arch_fetch_caller_regs(regs, CALLER_ADDR0);
5331d7b8
FW
733}
734
7e54a5a0 735static __always_inline void
a8b0ca17 736perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
e49a5bd3 737{
86038c5e
PZI
738 if (static_key_false(&perf_swevent_enabled[event_id]))
739 __perf_sw_event(event_id, nr, regs, addr);
740}
741
742DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]);
7e54a5a0 743
86038c5e
PZI
744/*
745 * 'Special' version for the scheduler, it hard assumes no recursion,
746 * which is guaranteed by us not actually scheduling inside other swevents
747 * because those disable preemption.
748 */
749static __always_inline void
750perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
751{
c5905afb 752 if (static_key_false(&perf_swevent_enabled[event_id])) {
86038c5e
PZI
753 struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
754
755 perf_fetch_caller_regs(regs);
756 ___perf_sw_event(event_id, nr, regs, addr);
e49a5bd3
FW
757 }
758}
759
c5905afb 760extern struct static_key_deferred perf_sched_events;
ee6dcfa4 761
ab0cce56 762static inline void perf_event_task_sched_in(struct task_struct *prev,
a8d757ef 763 struct task_struct *task)
ab0cce56
JO
764{
765 if (static_key_false(&perf_sched_events.key))
766 __perf_event_task_sched_in(prev, task);
767}
768
769static inline void perf_event_task_sched_out(struct task_struct *prev,
770 struct task_struct *next)
ee6dcfa4 771{
86038c5e 772 perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
ee6dcfa4 773
c5905afb 774 if (static_key_false(&perf_sched_events.key))
ab0cce56 775 __perf_event_task_sched_out(prev, next);
ee6dcfa4
PZ
776}
777
eacd3ecc
MF
778static inline u64 __perf_event_count(struct perf_event *event)
779{
780 return local64_read(&event->count) + atomic64_read(&event->child_count);
781}
782
3af9e859 783extern void perf_event_mmap(struct vm_area_struct *vma);
39447b38 784extern struct perf_guest_info_callbacks *perf_guest_cbs;
dcf46b94
ZY
785extern int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
786extern int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
39447b38 787
e041e328 788extern void perf_event_exec(void);
82b89778 789extern void perf_event_comm(struct task_struct *tsk, bool exec);
cdd6c482 790extern void perf_event_fork(struct task_struct *tsk);
8d1b2d93 791
56962b44
FW
792/* Callchains */
793DECLARE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry);
794
e7e7ee2e
IM
795extern void perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs);
796extern void perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs);
394ee076 797
e7e7ee2e 798static inline void perf_callchain_store(struct perf_callchain_entry *entry, u64 ip)
70791ce9
FW
799{
800 if (entry->nr < PERF_MAX_STACK_DEPTH)
801 entry->ip[entry->nr++] = ip;
802}
394ee076 803
cdd6c482
IM
804extern int sysctl_perf_event_paranoid;
805extern int sysctl_perf_event_mlock;
806extern int sysctl_perf_event_sample_rate;
14c63f17
DH
807extern int sysctl_perf_cpu_time_max_percent;
808
809extern void perf_sample_event_took(u64 sample_len_ns);
1ccd1549 810
163ec435
PZ
811extern int perf_proc_update_handler(struct ctl_table *table, int write,
812 void __user *buffer, size_t *lenp,
813 loff_t *ppos);
14c63f17
DH
814extern int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
815 void __user *buffer, size_t *lenp,
816 loff_t *ppos);
817
163ec435 818
320ebf09
PZ
819static inline bool perf_paranoid_tracepoint_raw(void)
820{
821 return sysctl_perf_event_paranoid > -1;
822}
823
824static inline bool perf_paranoid_cpu(void)
825{
826 return sysctl_perf_event_paranoid > 0;
827}
828
829static inline bool perf_paranoid_kernel(void)
830{
831 return sysctl_perf_event_paranoid > 1;
832}
833
cdd6c482 834extern void perf_event_init(void);
1c024eca
PZ
835extern void perf_tp_event(u64 addr, u64 count, void *record,
836 int entry_size, struct pt_regs *regs,
e6dab5ff
AV
837 struct hlist_head *head, int rctx,
838 struct task_struct *task);
24f1e32c 839extern void perf_bp_event(struct perf_event *event, void *data);
0d905bca 840
9d23a90a 841#ifndef perf_misc_flags
e7e7ee2e
IM
842# define perf_misc_flags(regs) \
843 (user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
844# define perf_instruction_pointer(regs) instruction_pointer(regs)
9d23a90a
PM
845#endif
846
bce38cd5
SE
847static inline bool has_branch_stack(struct perf_event *event)
848{
849 return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK;
a46a2300
YZ
850}
851
852static inline bool needs_branch_stack(struct perf_event *event)
853{
854 return event->attr.branch_sample_type != 0;
bce38cd5
SE
855}
856
5622f295 857extern int perf_output_begin(struct perf_output_handle *handle,
a7ac67ea 858 struct perf_event *event, unsigned int size);
5622f295 859extern void perf_output_end(struct perf_output_handle *handle);
91d7753a 860extern unsigned int perf_output_copy(struct perf_output_handle *handle,
5622f295 861 const void *buf, unsigned int len);
5685e0ff
JO
862extern unsigned int perf_output_skip(struct perf_output_handle *handle,
863 unsigned int len);
4ed7c92d
PZ
864extern int perf_swevent_get_recursion_context(void);
865extern void perf_swevent_put_recursion_context(int rctx);
ab573844 866extern u64 perf_swevent_set_period(struct perf_event *event);
44234adc
FW
867extern void perf_event_enable(struct perf_event *event);
868extern void perf_event_disable(struct perf_event *event);
500ad2d8 869extern int __perf_event_disable(void *info);
e9d2b064 870extern void perf_event_task_tick(void);
e041e328 871#else /* !CONFIG_PERF_EVENTS: */
0793a61d 872static inline void
ab0cce56
JO
873perf_event_task_sched_in(struct task_struct *prev,
874 struct task_struct *task) { }
875static inline void
876perf_event_task_sched_out(struct task_struct *prev,
877 struct task_struct *next) { }
cdd6c482
IM
878static inline int perf_event_init_task(struct task_struct *child) { return 0; }
879static inline void perf_event_exit_task(struct task_struct *child) { }
880static inline void perf_event_free_task(struct task_struct *task) { }
4e231c79 881static inline void perf_event_delayed_put(struct task_struct *task) { }
57c0c15b 882static inline void perf_event_print_debug(void) { }
57c0c15b
IM
883static inline int perf_event_task_disable(void) { return -EINVAL; }
884static inline int perf_event_task_enable(void) { return -EINVAL; }
26ca5c11
AK
885static inline int perf_event_refresh(struct perf_event *event, int refresh)
886{
887 return -EINVAL;
888}
15dbf27c 889
925d519a 890static inline void
a8b0ca17 891perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { }
24f1e32c 892static inline void
86038c5e
PZI
893perf_sw_event_sched(u32 event_id, u64 nr, u64 addr) { }
894static inline void
184f412c 895perf_bp_event(struct perf_event *event, void *data) { }
0a4a9391 896
39447b38 897static inline int perf_register_guest_info_callbacks
e7e7ee2e 898(struct perf_guest_info_callbacks *callbacks) { return 0; }
39447b38 899static inline int perf_unregister_guest_info_callbacks
e7e7ee2e 900(struct perf_guest_info_callbacks *callbacks) { return 0; }
39447b38 901
57c0c15b 902static inline void perf_event_mmap(struct vm_area_struct *vma) { }
e041e328 903static inline void perf_event_exec(void) { }
82b89778 904static inline void perf_event_comm(struct task_struct *tsk, bool exec) { }
cdd6c482
IM
905static inline void perf_event_fork(struct task_struct *tsk) { }
906static inline void perf_event_init(void) { }
184f412c 907static inline int perf_swevent_get_recursion_context(void) { return -1; }
4ed7c92d 908static inline void perf_swevent_put_recursion_context(int rctx) { }
ab573844 909static inline u64 perf_swevent_set_period(struct perf_event *event) { return 0; }
44234adc
FW
910static inline void perf_event_enable(struct perf_event *event) { }
911static inline void perf_event_disable(struct perf_event *event) { }
500ad2d8 912static inline int __perf_event_disable(void *info) { return -1; }
e9d2b064 913static inline void perf_event_task_tick(void) { }
0793a61d
TG
914#endif
915
026249ef
FW
916#if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_NO_HZ_FULL)
917extern bool perf_event_can_stop_tick(void);
918#else
919static inline bool perf_event_can_stop_tick(void) { return true; }
920#endif
921
6c4d3bc9
DR
922#if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL)
923extern void perf_restore_debug_store(void);
924#else
1d9d8639 925static inline void perf_restore_debug_store(void) { }
0793a61d
TG
926#endif
927
e7e7ee2e 928#define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x))
5622f295 929
3f6da390 930/*
0a0fca9d 931 * This has to have a higher priority than migration_notifier in sched/core.c.
3f6da390 932 */
e7e7ee2e
IM
933#define perf_cpu_notifier(fn) \
934do { \
0db0628d 935 static struct notifier_block fn##_nb = \
e7e7ee2e 936 { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
c13d38e4 937 unsigned long cpu = smp_processor_id(); \
6760bca9 938 unsigned long flags; \
f0bdb5e0
SB
939 \
940 cpu_notifier_register_begin(); \
e7e7ee2e 941 fn(&fn##_nb, (unsigned long)CPU_UP_PREPARE, \
c13d38e4 942 (void *)(unsigned long)cpu); \
6760bca9 943 local_irq_save(flags); \
e7e7ee2e 944 fn(&fn##_nb, (unsigned long)CPU_STARTING, \
c13d38e4 945 (void *)(unsigned long)cpu); \
6760bca9 946 local_irq_restore(flags); \
e7e7ee2e 947 fn(&fn##_nb, (unsigned long)CPU_ONLINE, \
c13d38e4 948 (void *)(unsigned long)cpu); \
f0bdb5e0
SB
949 __register_cpu_notifier(&fn##_nb); \
950 cpu_notifier_register_done(); \
3f6da390
PZ
951} while (0)
952
f0bdb5e0
SB
953/*
954 * Bare-bones version of perf_cpu_notifier(), which doesn't invoke the
955 * callback for already online CPUs.
956 */
957#define __perf_cpu_notifier(fn) \
958do { \
959 static struct notifier_block fn##_nb = \
960 { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
961 \
962 __register_cpu_notifier(&fn##_nb); \
963} while (0)
641cc938 964
2663960c
SB
965struct perf_pmu_events_attr {
966 struct device_attribute attr;
967 u64 id;
3a54aaa0 968 const char *event_str;
2663960c
SB
969};
970
971#define PMU_EVENT_ATTR(_name, _var, _id, _show) \
972static struct perf_pmu_events_attr _var = { \
973 .attr = __ATTR(_name, 0444, _show, NULL), \
974 .id = _id, \
975};
976
641cc938
JO
977#define PMU_FORMAT_ATTR(_name, _format) \
978static ssize_t \
979_name##_show(struct device *dev, \
980 struct device_attribute *attr, \
981 char *page) \
982{ \
983 BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
984 return sprintf(page, _format "\n"); \
985} \
986 \
987static struct device_attribute format_attr_##_name = __ATTR_RO(_name)
988
cdd6c482 989#endif /* _LINUX_PERF_EVENT_H */
This page took 0.583033 seconds and 5 git commands to generate.