4 * Copyright (C) 2008-2009, Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2011, Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2011, Red Hat, Inc., Peter Zijlstra
8 * Data type definitions, declarations, prototypes.
10 * Started by: Thomas Gleixner and Ingo Molnar
12 * For licencing details see kernel-base/COPYING
14 #ifndef _LINUX_PERF_EVENT_H
15 #define _LINUX_PERF_EVENT_H
17 #include <uapi/linux/perf_event.h>
20 * Kernel-internal data types and definitions:
23 #ifdef CONFIG_PERF_EVENTS
24 # include <asm/perf_event.h>
25 # include <asm/local64.h>
28 struct perf_guest_info_callbacks
{
29 int (*is_in_guest
)(void);
30 int (*is_user_mode
)(void);
31 unsigned long (*get_guest_ip
)(void);
34 #ifdef CONFIG_HAVE_HW_BREAKPOINT
35 #include <asm/hw_breakpoint.h>
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>
43 #include <linux/hrtimer.h>
45 #include <linux/pid_namespace.h>
46 #include <linux/workqueue.h>
47 #include <linux/ftrace.h>
48 #include <linux/cpu.h>
49 #include <linux/irq_work.h>
50 #include <linux/static_key.h>
51 #include <linux/jump_label_ratelimit.h>
52 #include <linux/atomic.h>
53 #include <linux/sysfs.h>
54 #include <linux/perf_regs.h>
55 #include <linux/workqueue.h>
56 #include <linux/cgroup.h>
57 #include <asm/local.h>
59 struct perf_callchain_entry
{
61 __u64 ip
[0]; /* /proc/sys/kernel/perf_event_max_stack */
64 struct perf_callchain_entry_ctx
{
65 struct perf_callchain_entry
*entry
;
69 struct perf_raw_record
{
75 * branch stack layout:
76 * nr: number of taken branches stored in entries[]
78 * Note that nr can vary from sample to sample
79 * branches (to, from) are stored from most recent
80 * to least recent, i.e., entries[0] contains the most
83 struct perf_branch_stack
{
85 struct perf_branch_entry entries
[0];
91 * extra PMU register associated with an event
93 struct hw_perf_event_extra
{
94 u64 config
; /* register value */
95 unsigned int reg
; /* register address or index */
96 int alloc
; /* extra register already allocated */
97 int idx
; /* index in shared_regs->regs[] */
101 * struct hw_perf_event - performance event hardware details:
103 struct hw_perf_event
{
104 #ifdef CONFIG_PERF_EVENTS
106 struct { /* hardware */
109 unsigned long config_base
;
110 unsigned long event_base
;
111 int event_base_rdpmc
;
116 struct hw_perf_event_extra extra_reg
;
117 struct hw_perf_event_extra branch_reg
;
119 struct { /* software */
120 struct hrtimer hrtimer
;
122 struct { /* tracepoint */
123 /* for tp_event->class */
124 struct list_head tp_list
;
126 struct { /* intel_cqm */
130 struct list_head cqm_events_entry
;
131 struct list_head cqm_groups_entry
;
132 struct list_head cqm_group_entry
;
134 struct { /* itrace */
137 struct { /* amd_power */
141 #ifdef CONFIG_HAVE_HW_BREAKPOINT
142 struct { /* breakpoint */
144 * Crufty hack to avoid the chicken and egg
145 * problem hw_breakpoint has with context
146 * creation and event initalization.
148 struct arch_hw_breakpoint info
;
149 struct list_head bp_list
;
154 * If the event is a per task event, this will point to the task in
155 * question. See the comment in perf_event_alloc().
157 struct task_struct
*target
;
160 * PMU would store hardware filter configuration
165 /* Last sync'ed generation of filters */
166 unsigned long addr_filters_gen
;
169 * hw_perf_event::state flags; used to track the PERF_EF_* state.
171 #define PERF_HES_STOPPED 0x01 /* the counter is stopped */
172 #define PERF_HES_UPTODATE 0x02 /* event->count up-to-date */
173 #define PERF_HES_ARCH 0x04
178 * The last observed hardware counter value, updated with a
179 * local64_cmpxchg() such that pmu::read() can be called nested.
181 local64_t prev_count
;
184 * The period to start the next sample with.
189 * The period we started this sample with.
194 * However much is left of the current period; note that this is
195 * a full 64bit value and allows for generation of periods longer
196 * than hardware might allow.
198 local64_t period_left
;
201 * State for throttling the event, see __perf_event_overflow() and
202 * perf_adjust_freq_unthr_context().
208 * State for freq target events, see __perf_event_overflow() and
209 * perf_adjust_freq_unthr_context().
212 u64 freq_count_stamp
;
219 * Common implementation detail of pmu::{start,commit,cancel}_txn
221 #define PERF_PMU_TXN_ADD 0x1 /* txn to add/schedule event on PMU */
222 #define PERF_PMU_TXN_READ 0x2 /* txn to read event group from PMU */
225 * pmu::capabilities flags
227 #define PERF_PMU_CAP_NO_INTERRUPT 0x01
228 #define PERF_PMU_CAP_NO_NMI 0x02
229 #define PERF_PMU_CAP_AUX_NO_SG 0x04
230 #define PERF_PMU_CAP_AUX_SW_DOUBLEBUF 0x08
231 #define PERF_PMU_CAP_EXCLUSIVE 0x10
232 #define PERF_PMU_CAP_ITRACE 0x20
233 #define PERF_PMU_CAP_HETEROGENEOUS_CPUS 0x40
236 * struct pmu - generic performance monitoring unit
239 struct list_head entry
;
241 struct module
*module
;
243 const struct attribute_group
**attr_groups
;
248 * various common per-pmu feature flags
252 int * __percpu pmu_disable_count
;
253 struct perf_cpu_context
* __percpu pmu_cpu_context
;
254 atomic_t exclusive_cnt
; /* < 0: cpu; > 0: tsk */
256 int hrtimer_interval_ms
;
258 /* number of address filters this PMU can do */
259 unsigned int nr_addr_filters
;
262 * Fully disable/enable this PMU, can be used to protect from the PMI
263 * as well as for lazy/batch writing of the MSRs.
265 void (*pmu_enable
) (struct pmu
*pmu
); /* optional */
266 void (*pmu_disable
) (struct pmu
*pmu
); /* optional */
269 * Try and initialize the event for this PMU.
272 * -ENOENT -- @event is not for this PMU
274 * -ENODEV -- @event is for this PMU but PMU not present
275 * -EBUSY -- @event is for this PMU but PMU temporarily unavailable
276 * -EINVAL -- @event is for this PMU but @event is not valid
277 * -EOPNOTSUPP -- @event is for this PMU, @event is valid, but not supported
278 * -EACCESS -- @event is for this PMU, @event is valid, but no privilidges
280 * 0 -- @event is for this PMU and valid
282 * Other error return values are allowed.
284 int (*event_init
) (struct perf_event
*event
);
287 * Notification that the event was mapped or unmapped. Called
288 * in the context of the mapping task.
290 void (*event_mapped
) (struct perf_event
*event
); /*optional*/
291 void (*event_unmapped
) (struct perf_event
*event
); /*optional*/
294 * Flags for ->add()/->del()/ ->start()/->stop(). There are
295 * matching hw_perf_event::state flags.
297 #define PERF_EF_START 0x01 /* start the counter when adding */
298 #define PERF_EF_RELOAD 0x02 /* reload the counter when starting */
299 #define PERF_EF_UPDATE 0x04 /* update the counter when stopping */
302 * Adds/Removes a counter to/from the PMU, can be done inside a
303 * transaction, see the ->*_txn() methods.
305 * The add/del callbacks will reserve all hardware resources required
306 * to service the event, this includes any counter constraint
309 * Called with IRQs disabled and the PMU disabled on the CPU the event
312 * ->add() called without PERF_EF_START should result in the same state
313 * as ->add() followed by ->stop().
315 * ->del() must always PERF_EF_UPDATE stop an event. If it calls
316 * ->stop() that must deal with already being stopped without
319 int (*add
) (struct perf_event
*event
, int flags
);
320 void (*del
) (struct perf_event
*event
, int flags
);
323 * Starts/Stops a counter present on the PMU.
325 * The PMI handler should stop the counter when perf_event_overflow()
326 * returns !0. ->start() will be used to continue.
328 * Also used to change the sample period.
330 * Called with IRQs disabled and the PMU disabled on the CPU the event
331 * is on -- will be called from NMI context with the PMU generates
334 * ->stop() with PERF_EF_UPDATE will read the counter and update
335 * period/count values like ->read() would.
337 * ->start() with PERF_EF_RELOAD will reprogram the the counter
338 * value, must be preceded by a ->stop() with PERF_EF_UPDATE.
340 void (*start
) (struct perf_event
*event
, int flags
);
341 void (*stop
) (struct perf_event
*event
, int flags
);
344 * Updates the counter value of the event.
346 * For sampling capable PMUs this will also update the software period
347 * hw_perf_event::period_left field.
349 void (*read
) (struct perf_event
*event
);
352 * Group events scheduling is treated as a transaction, add
353 * group events as a whole and perform one schedulability test.
354 * If the test fails, roll back the whole group
356 * Start the transaction, after this ->add() doesn't need to
357 * do schedulability tests.
361 void (*start_txn
) (struct pmu
*pmu
, unsigned int txn_flags
);
363 * If ->start_txn() disabled the ->add() schedulability test
364 * then ->commit_txn() is required to perform one. On success
365 * the transaction is closed. On error the transaction is kept
366 * open until ->cancel_txn() is called.
370 int (*commit_txn
) (struct pmu
*pmu
);
372 * Will cancel the transaction, assumes ->del() is called
373 * for each successful ->add() during the transaction.
377 void (*cancel_txn
) (struct pmu
*pmu
);
380 * Will return the value for perf_event_mmap_page::index for this event,
381 * if no implementation is provided it will default to: event->hw.idx + 1.
383 int (*event_idx
) (struct perf_event
*event
); /*optional */
386 * context-switches callback
388 void (*sched_task
) (struct perf_event_context
*ctx
,
391 * PMU specific data size
393 size_t task_ctx_size
;
397 * Return the count value for a counter.
399 u64 (*count
) (struct perf_event
*event
); /*optional*/
402 * Set up pmu-private data structures for an AUX area
404 void *(*setup_aux
) (int cpu
, void **pages
,
405 int nr_pages
, bool overwrite
);
409 * Free pmu-private AUX data structures
411 void (*free_aux
) (void *aux
); /* optional */
414 * Validate address range filters: make sure the HW supports the
415 * requested configuration and number of filters; return 0 if the
416 * supplied filters are valid, -errno otherwise.
418 * Runs in the context of the ioctl()ing process and is not serialized
419 * with the rest of the PMU callbacks.
421 int (*addr_filters_validate
) (struct list_head
*filters
);
425 * Synchronize address range filter configuration:
426 * translate hw-agnostic filters into hardware configuration in
427 * event::hw::addr_filters.
429 * Runs as a part of filter sync sequence that is done in ->start()
430 * callback by calling perf_event_addr_filters_sync().
432 * May (and should) traverse event::addr_filters::list, for which its
433 * caller provides necessary serialization.
435 void (*addr_filters_sync
) (struct perf_event
*event
);
439 * Filter events for PMU-specific reasons.
441 int (*filter_match
) (struct perf_event
*event
); /* optional */
445 * struct perf_addr_filter - address range filter definition
446 * @entry: event's filter list linkage
447 * @inode: object file's inode for file-based filters
448 * @offset: filter range offset
449 * @size: filter range size
450 * @range: 1: range, 0: address
451 * @filter: 1: filter/start, 0: stop
453 * This is a hardware-agnostic filter configuration as specified by the user.
455 struct perf_addr_filter
{
456 struct list_head entry
;
458 unsigned long offset
;
460 unsigned int range
: 1,
465 * struct perf_addr_filters_head - container for address range filters
466 * @list: list of filters for this event
467 * @lock: spinlock that serializes accesses to the @list and event's
468 * (and its children's) filter generations.
470 * A child event will use parent's @list (and therefore @lock), so they are
471 * bundled together; see perf_event_addr_filters().
473 struct perf_addr_filters_head
{
474 struct list_head list
;
479 * enum perf_event_active_state - the states of a event
481 enum perf_event_active_state
{
482 PERF_EVENT_STATE_DEAD
= -4,
483 PERF_EVENT_STATE_EXIT
= -3,
484 PERF_EVENT_STATE_ERROR
= -2,
485 PERF_EVENT_STATE_OFF
= -1,
486 PERF_EVENT_STATE_INACTIVE
= 0,
487 PERF_EVENT_STATE_ACTIVE
= 1,
491 struct perf_sample_data
;
493 typedef void (*perf_overflow_handler_t
)(struct perf_event
*,
494 struct perf_sample_data
*,
495 struct pt_regs
*regs
);
497 enum perf_group_flag
{
498 PERF_GROUP_SOFTWARE
= 0x1,
501 #define SWEVENT_HLIST_BITS 8
502 #define SWEVENT_HLIST_SIZE (1 << SWEVENT_HLIST_BITS)
504 struct swevent_hlist
{
505 struct hlist_head heads
[SWEVENT_HLIST_SIZE
];
506 struct rcu_head rcu_head
;
509 #define PERF_ATTACH_CONTEXT 0x01
510 #define PERF_ATTACH_GROUP 0x02
511 #define PERF_ATTACH_TASK 0x04
512 #define PERF_ATTACH_TASK_DATA 0x08
518 * struct perf_event - performance event kernel representation:
521 #ifdef CONFIG_PERF_EVENTS
523 * entry onto perf_event_context::event_list;
524 * modifications require ctx->lock
525 * RCU safe iterations.
527 struct list_head event_entry
;
530 * XXX: group_entry and sibling_list should be mutually exclusive;
531 * either you're a sibling on a group, or you're the group leader.
532 * Rework the code to always use the same list element.
534 * Locked for modification by both ctx->mutex and ctx->lock; holding
535 * either sufficies for read.
537 struct list_head group_entry
;
538 struct list_head sibling_list
;
541 * We need storage to track the entries in perf_pmu_migrate_context; we
542 * cannot use the event_entry because of RCU and we want to keep the
543 * group in tact which avoids us using the other two entries.
545 struct list_head migrate_entry
;
547 struct hlist_node hlist_entry
;
548 struct list_head active_entry
;
551 struct perf_event
*group_leader
;
555 enum perf_event_active_state state
;
556 unsigned int attach_state
;
558 atomic64_t child_count
;
561 * These are the total time in nanoseconds that the event
562 * has been enabled (i.e. eligible to run, and the task has
563 * been scheduled in, if this is a per-task event)
564 * and running (scheduled onto the CPU), respectively.
566 * They are computed from tstamp_enabled, tstamp_running and
567 * tstamp_stopped when the event is in INACTIVE or ACTIVE state.
569 u64 total_time_enabled
;
570 u64 total_time_running
;
573 * These are timestamps used for computing total_time_enabled
574 * and total_time_running when the event is in INACTIVE or
575 * ACTIVE state, measured in nanoseconds from an arbitrary point
577 * tstamp_enabled: the notional time when the event was enabled
578 * tstamp_running: the notional time when the event was scheduled on
579 * tstamp_stopped: in INACTIVE state, the notional time when the
580 * event was scheduled off.
587 * timestamp shadows the actual context timing but it can
588 * be safely used in NMI interrupt context. It reflects the
589 * context time as it was when the event was last scheduled in.
591 * ctx_time already accounts for ctx->timestamp. Therefore to
592 * compute ctx_time for a sample, simply add perf_clock().
596 struct perf_event_attr attr
;
600 struct hw_perf_event hw
;
602 struct perf_event_context
*ctx
;
603 atomic_long_t refcount
;
606 * These accumulate total time (in nanoseconds) that children
607 * events have been enabled and running, respectively.
609 atomic64_t child_total_time_enabled
;
610 atomic64_t child_total_time_running
;
613 * Protect attach/detach and child_list:
615 struct mutex child_mutex
;
616 struct list_head child_list
;
617 struct perf_event
*parent
;
622 struct list_head owner_entry
;
623 struct task_struct
*owner
;
626 struct mutex mmap_mutex
;
629 struct ring_buffer
*rb
;
630 struct list_head rb_entry
;
631 unsigned long rcu_batches
;
635 wait_queue_head_t waitq
;
636 struct fasync_struct
*fasync
;
638 /* delayed work for NMIs and such */
642 struct irq_work pending
;
644 atomic_t event_limit
;
646 /* address range filters */
647 struct perf_addr_filters_head addr_filters
;
648 /* vma address array for file-based filders */
649 unsigned long *addr_filters_offs
;
650 unsigned long addr_filters_gen
;
652 void (*destroy
)(struct perf_event
*);
653 struct rcu_head rcu_head
;
655 struct pid_namespace
*ns
;
659 perf_overflow_handler_t overflow_handler
;
660 void *overflow_handler_context
;
662 #ifdef CONFIG_EVENT_TRACING
663 struct trace_event_call
*tp_event
;
664 struct event_filter
*filter
;
665 #ifdef CONFIG_FUNCTION_TRACER
666 struct ftrace_ops ftrace_ops
;
670 #ifdef CONFIG_CGROUP_PERF
671 struct perf_cgroup
*cgrp
; /* cgroup event is attach to */
672 int cgrp_defer_enabled
;
675 #endif /* CONFIG_PERF_EVENTS */
679 * struct perf_event_context - event context structure
681 * Used as a container for task events and CPU events as well:
683 struct perf_event_context
{
686 * Protect the states of the events in the list,
687 * nr_active, and the list:
691 * Protect the list of events. Locking either mutex or lock
692 * is sufficient to ensure the list doesn't change; to change
693 * the list you need to lock both the mutex and the spinlock.
697 struct list_head active_ctx_list
;
698 struct list_head pinned_groups
;
699 struct list_head flexible_groups
;
700 struct list_head event_list
;
708 struct task_struct
*task
;
711 * Context clock, runs when context enabled.
717 * These fields let us detect when two contexts have both
718 * been cloned (inherited) from a common ancestor.
720 struct perf_event_context
*parent_ctx
;
724 int nr_cgroups
; /* cgroup evts */
725 void *task_ctx_data
; /* pmu specific data */
726 struct rcu_head rcu_head
;
730 * Number of contexts where an event can trigger:
731 * task, softirq, hardirq, nmi.
733 #define PERF_NR_CONTEXTS 4
736 * struct perf_event_cpu_context - per cpu event context structure
738 struct perf_cpu_context
{
739 struct perf_event_context ctx
;
740 struct perf_event_context
*task_ctx
;
744 raw_spinlock_t hrtimer_lock
;
745 struct hrtimer hrtimer
;
746 ktime_t hrtimer_interval
;
747 unsigned int hrtimer_active
;
749 struct pmu
*unique_pmu
;
750 struct perf_cgroup
*cgrp
;
753 struct perf_output_handle
{
754 struct perf_event
*event
;
755 struct ring_buffer
*rb
;
756 unsigned long wakeup
;
765 #ifdef CONFIG_CGROUP_PERF
768 * perf_cgroup_info keeps track of time_enabled for a cgroup.
769 * This is a per-cpu dynamically allocated data structure.
771 struct perf_cgroup_info
{
777 struct cgroup_subsys_state css
;
778 struct perf_cgroup_info __percpu
*info
;
782 * Must ensure cgroup is pinned (css_get) before calling
783 * this function. In other words, we cannot call this function
784 * if there is no cgroup event for the current CPU context.
786 static inline struct perf_cgroup
*
787 perf_cgroup_from_task(struct task_struct
*task
, struct perf_event_context
*ctx
)
789 return container_of(task_css_check(task
, perf_event_cgrp_id
,
790 ctx
? lockdep_is_held(&ctx
->lock
)
792 struct perf_cgroup
, css
);
794 #endif /* CONFIG_CGROUP_PERF */
796 #ifdef CONFIG_PERF_EVENTS
798 extern void *perf_aux_output_begin(struct perf_output_handle
*handle
,
799 struct perf_event
*event
);
800 extern void perf_aux_output_end(struct perf_output_handle
*handle
,
801 unsigned long size
, bool truncated
);
802 extern int perf_aux_output_skip(struct perf_output_handle
*handle
,
804 extern void *perf_get_aux(struct perf_output_handle
*handle
);
806 extern int perf_pmu_register(struct pmu
*pmu
, const char *name
, int type
);
807 extern void perf_pmu_unregister(struct pmu
*pmu
);
809 extern int perf_num_counters(void);
810 extern const char *perf_pmu_name(void);
811 extern void __perf_event_task_sched_in(struct task_struct
*prev
,
812 struct task_struct
*task
);
813 extern void __perf_event_task_sched_out(struct task_struct
*prev
,
814 struct task_struct
*next
);
815 extern int perf_event_init_task(struct task_struct
*child
);
816 extern void perf_event_exit_task(struct task_struct
*child
);
817 extern void perf_event_free_task(struct task_struct
*task
);
818 extern void perf_event_delayed_put(struct task_struct
*task
);
819 extern struct file
*perf_event_get(unsigned int fd
);
820 extern const struct perf_event_attr
*perf_event_attrs(struct perf_event
*event
);
821 extern void perf_event_print_debug(void);
822 extern void perf_pmu_disable(struct pmu
*pmu
);
823 extern void perf_pmu_enable(struct pmu
*pmu
);
824 extern void perf_sched_cb_dec(struct pmu
*pmu
);
825 extern void perf_sched_cb_inc(struct pmu
*pmu
);
826 extern int perf_event_task_disable(void);
827 extern int perf_event_task_enable(void);
828 extern int perf_event_refresh(struct perf_event
*event
, int refresh
);
829 extern void perf_event_update_userpage(struct perf_event
*event
);
830 extern int perf_event_release_kernel(struct perf_event
*event
);
831 extern struct perf_event
*
832 perf_event_create_kernel_counter(struct perf_event_attr
*attr
,
834 struct task_struct
*task
,
835 perf_overflow_handler_t callback
,
837 extern void perf_pmu_migrate_context(struct pmu
*pmu
,
838 int src_cpu
, int dst_cpu
);
839 extern u64
perf_event_read_local(struct perf_event
*event
);
840 extern u64
perf_event_read_value(struct perf_event
*event
,
841 u64
*enabled
, u64
*running
);
844 struct perf_sample_data
{
846 * Fields set by perf_sample_data_init(), group so as to
847 * minimize the cachelines touched.
850 struct perf_raw_record
*raw
;
851 struct perf_branch_stack
*br_stack
;
855 union perf_mem_data_src data_src
;
858 * The other fields, optionally {set,used} by
859 * perf_{prepare,output}_sample().
874 struct perf_callchain_entry
*callchain
;
877 * regs_user may point to task_pt_regs or to regs_user_copy, depending
880 struct perf_regs regs_user
;
881 struct pt_regs regs_user_copy
;
883 struct perf_regs regs_intr
;
885 } ____cacheline_aligned
;
887 /* default value for data source */
888 #define PERF_MEM_NA (PERF_MEM_S(OP, NA) |\
889 PERF_MEM_S(LVL, NA) |\
890 PERF_MEM_S(SNOOP, NA) |\
891 PERF_MEM_S(LOCK, NA) |\
894 static inline void perf_sample_data_init(struct perf_sample_data
*data
,
895 u64 addr
, u64 period
)
897 /* remaining struct members initialized in perf_prepare_sample() */
900 data
->br_stack
= NULL
;
901 data
->period
= period
;
903 data
->data_src
.val
= PERF_MEM_NA
;
907 extern void perf_output_sample(struct perf_output_handle
*handle
,
908 struct perf_event_header
*header
,
909 struct perf_sample_data
*data
,
910 struct perf_event
*event
);
911 extern void perf_prepare_sample(struct perf_event_header
*header
,
912 struct perf_sample_data
*data
,
913 struct perf_event
*event
,
914 struct pt_regs
*regs
);
916 extern int perf_event_overflow(struct perf_event
*event
,
917 struct perf_sample_data
*data
,
918 struct pt_regs
*regs
);
920 extern void perf_event_output_forward(struct perf_event
*event
,
921 struct perf_sample_data
*data
,
922 struct pt_regs
*regs
);
923 extern void perf_event_output_backward(struct perf_event
*event
,
924 struct perf_sample_data
*data
,
925 struct pt_regs
*regs
);
926 extern void perf_event_output(struct perf_event
*event
,
927 struct perf_sample_data
*data
,
928 struct pt_regs
*regs
);
931 is_default_overflow_handler(struct perf_event
*event
)
933 if (likely(event
->overflow_handler
== perf_event_output_forward
))
935 if (unlikely(event
->overflow_handler
== perf_event_output_backward
))
941 perf_event_header__init_id(struct perf_event_header
*header
,
942 struct perf_sample_data
*data
,
943 struct perf_event
*event
);
945 perf_event__output_id_sample(struct perf_event
*event
,
946 struct perf_output_handle
*handle
,
947 struct perf_sample_data
*sample
);
950 perf_log_lost_samples(struct perf_event
*event
, u64 lost
);
952 static inline bool is_sampling_event(struct perf_event
*event
)
954 return event
->attr
.sample_period
!= 0;
958 * Return 1 for a software event, 0 for a hardware event
960 static inline int is_software_event(struct perf_event
*event
)
962 return event
->pmu
->task_ctx_nr
== perf_sw_context
;
965 extern struct static_key perf_swevent_enabled
[PERF_COUNT_SW_MAX
];
967 extern void ___perf_sw_event(u32
, u64
, struct pt_regs
*, u64
);
968 extern void __perf_sw_event(u32
, u64
, struct pt_regs
*, u64
);
970 #ifndef perf_arch_fetch_caller_regs
971 static inline void perf_arch_fetch_caller_regs(struct pt_regs
*regs
, unsigned long ip
) { }
975 * Take a snapshot of the regs. Skip ip and frame pointer to
976 * the nth caller. We only need a few of the regs:
977 * - ip for PERF_SAMPLE_IP
978 * - cs for user_mode() tests
979 * - bp for callchains
980 * - eflags, for future purposes, just in case
982 static inline void perf_fetch_caller_regs(struct pt_regs
*regs
)
984 memset(regs
, 0, sizeof(*regs
));
986 perf_arch_fetch_caller_regs(regs
, CALLER_ADDR0
);
989 static __always_inline
void
990 perf_sw_event(u32 event_id
, u64 nr
, struct pt_regs
*regs
, u64 addr
)
992 if (static_key_false(&perf_swevent_enabled
[event_id
]))
993 __perf_sw_event(event_id
, nr
, regs
, addr
);
996 DECLARE_PER_CPU(struct pt_regs
, __perf_regs
[4]);
999 * 'Special' version for the scheduler, it hard assumes no recursion,
1000 * which is guaranteed by us not actually scheduling inside other swevents
1001 * because those disable preemption.
1003 static __always_inline
void
1004 perf_sw_event_sched(u32 event_id
, u64 nr
, u64 addr
)
1006 if (static_key_false(&perf_swevent_enabled
[event_id
])) {
1007 struct pt_regs
*regs
= this_cpu_ptr(&__perf_regs
[0]);
1009 perf_fetch_caller_regs(regs
);
1010 ___perf_sw_event(event_id
, nr
, regs
, addr
);
1014 extern struct static_key_false perf_sched_events
;
1016 static __always_inline
bool
1017 perf_sw_migrate_enabled(void)
1019 if (static_key_false(&perf_swevent_enabled
[PERF_COUNT_SW_CPU_MIGRATIONS
]))
1024 static inline void perf_event_task_migrate(struct task_struct
*task
)
1026 if (perf_sw_migrate_enabled())
1027 task
->sched_migrated
= 1;
1030 static inline void perf_event_task_sched_in(struct task_struct
*prev
,
1031 struct task_struct
*task
)
1033 if (static_branch_unlikely(&perf_sched_events
))
1034 __perf_event_task_sched_in(prev
, task
);
1036 if (perf_sw_migrate_enabled() && task
->sched_migrated
) {
1037 struct pt_regs
*regs
= this_cpu_ptr(&__perf_regs
[0]);
1039 perf_fetch_caller_regs(regs
);
1040 ___perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS
, 1, regs
, 0);
1041 task
->sched_migrated
= 0;
1045 static inline void perf_event_task_sched_out(struct task_struct
*prev
,
1046 struct task_struct
*next
)
1048 perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES
, 1, 0);
1050 if (static_branch_unlikely(&perf_sched_events
))
1051 __perf_event_task_sched_out(prev
, next
);
1054 static inline u64
__perf_event_count(struct perf_event
*event
)
1056 return local64_read(&event
->count
) + atomic64_read(&event
->child_count
);
1059 extern void perf_event_mmap(struct vm_area_struct
*vma
);
1060 extern struct perf_guest_info_callbacks
*perf_guest_cbs
;
1061 extern int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks
*callbacks
);
1062 extern int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks
*callbacks
);
1064 extern void perf_event_exec(void);
1065 extern void perf_event_comm(struct task_struct
*tsk
, bool exec
);
1066 extern void perf_event_fork(struct task_struct
*tsk
);
1069 DECLARE_PER_CPU(struct perf_callchain_entry
, perf_callchain_entry
);
1071 extern void perf_callchain_user(struct perf_callchain_entry_ctx
*entry
, struct pt_regs
*regs
);
1072 extern void perf_callchain_kernel(struct perf_callchain_entry_ctx
*entry
, struct pt_regs
*regs
);
1073 extern struct perf_callchain_entry
*
1074 get_perf_callchain(struct pt_regs
*regs
, u32 init_nr
, bool kernel
, bool user
,
1075 u32 max_stack
, bool crosstask
, bool add_mark
);
1076 extern int get_callchain_buffers(void);
1077 extern void put_callchain_buffers(void);
1079 extern int sysctl_perf_event_max_stack
;
1081 static inline int perf_callchain_store(struct perf_callchain_entry_ctx
*ctx
, u64 ip
)
1083 struct perf_callchain_entry
*entry
= ctx
->entry
;
1084 if (entry
->nr
< ctx
->max_stack
) {
1085 entry
->ip
[entry
->nr
++] = ip
;
1088 return -1; /* no more room, stop walking the stack */
1092 extern int sysctl_perf_event_paranoid
;
1093 extern int sysctl_perf_event_mlock
;
1094 extern int sysctl_perf_event_sample_rate
;
1095 extern int sysctl_perf_cpu_time_max_percent
;
1097 extern void perf_sample_event_took(u64 sample_len_ns
);
1099 extern int perf_proc_update_handler(struct ctl_table
*table
, int write
,
1100 void __user
*buffer
, size_t *lenp
,
1102 extern int perf_cpu_time_max_percent_handler(struct ctl_table
*table
, int write
,
1103 void __user
*buffer
, size_t *lenp
,
1106 int perf_event_max_stack_handler(struct ctl_table
*table
, int write
,
1107 void __user
*buffer
, size_t *lenp
, loff_t
*ppos
);
1109 static inline bool perf_paranoid_tracepoint_raw(void)
1111 return sysctl_perf_event_paranoid
> -1;
1114 static inline bool perf_paranoid_cpu(void)
1116 return sysctl_perf_event_paranoid
> 0;
1119 static inline bool perf_paranoid_kernel(void)
1121 return sysctl_perf_event_paranoid
> 1;
1124 extern void perf_event_init(void);
1125 extern void perf_tp_event(u64 addr
, u64 count
, void *record
,
1126 int entry_size
, struct pt_regs
*regs
,
1127 struct hlist_head
*head
, int rctx
,
1128 struct task_struct
*task
);
1129 extern void perf_bp_event(struct perf_event
*event
, void *data
);
1131 #ifndef perf_misc_flags
1132 # define perf_misc_flags(regs) \
1133 (user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
1134 # define perf_instruction_pointer(regs) instruction_pointer(regs)
1137 static inline bool has_branch_stack(struct perf_event
*event
)
1139 return event
->attr
.sample_type
& PERF_SAMPLE_BRANCH_STACK
;
1142 static inline bool needs_branch_stack(struct perf_event
*event
)
1144 return event
->attr
.branch_sample_type
!= 0;
1147 static inline bool has_aux(struct perf_event
*event
)
1149 return event
->pmu
->setup_aux
;
1152 static inline bool is_write_backward(struct perf_event
*event
)
1154 return !!event
->attr
.write_backward
;
1157 static inline bool has_addr_filter(struct perf_event
*event
)
1159 return event
->pmu
->nr_addr_filters
;
1163 * An inherited event uses parent's filters
1165 static inline struct perf_addr_filters_head
*
1166 perf_event_addr_filters(struct perf_event
*event
)
1168 struct perf_addr_filters_head
*ifh
= &event
->addr_filters
;
1171 ifh
= &event
->parent
->addr_filters
;
1176 extern void perf_event_addr_filters_sync(struct perf_event
*event
);
1178 extern int perf_output_begin(struct perf_output_handle
*handle
,
1179 struct perf_event
*event
, unsigned int size
);
1180 extern int perf_output_begin_forward(struct perf_output_handle
*handle
,
1181 struct perf_event
*event
,
1183 extern int perf_output_begin_backward(struct perf_output_handle
*handle
,
1184 struct perf_event
*event
,
1187 extern void perf_output_end(struct perf_output_handle
*handle
);
1188 extern unsigned int perf_output_copy(struct perf_output_handle
*handle
,
1189 const void *buf
, unsigned int len
);
1190 extern unsigned int perf_output_skip(struct perf_output_handle
*handle
,
1192 extern int perf_swevent_get_recursion_context(void);
1193 extern void perf_swevent_put_recursion_context(int rctx
);
1194 extern u64
perf_swevent_set_period(struct perf_event
*event
);
1195 extern void perf_event_enable(struct perf_event
*event
);
1196 extern void perf_event_disable(struct perf_event
*event
);
1197 extern void perf_event_disable_local(struct perf_event
*event
);
1198 extern void perf_event_task_tick(void);
1199 #else /* !CONFIG_PERF_EVENTS: */
1200 static inline void *
1201 perf_aux_output_begin(struct perf_output_handle
*handle
,
1202 struct perf_event
*event
) { return NULL
; }
1204 perf_aux_output_end(struct perf_output_handle
*handle
, unsigned long size
,
1207 perf_aux_output_skip(struct perf_output_handle
*handle
,
1208 unsigned long size
) { return -EINVAL
; }
1209 static inline void *
1210 perf_get_aux(struct perf_output_handle
*handle
) { return NULL
; }
1212 perf_event_task_migrate(struct task_struct
*task
) { }
1214 perf_event_task_sched_in(struct task_struct
*prev
,
1215 struct task_struct
*task
) { }
1217 perf_event_task_sched_out(struct task_struct
*prev
,
1218 struct task_struct
*next
) { }
1219 static inline int perf_event_init_task(struct task_struct
*child
) { return 0; }
1220 static inline void perf_event_exit_task(struct task_struct
*child
) { }
1221 static inline void perf_event_free_task(struct task_struct
*task
) { }
1222 static inline void perf_event_delayed_put(struct task_struct
*task
) { }
1223 static inline struct file
*perf_event_get(unsigned int fd
) { return ERR_PTR(-EINVAL
); }
1224 static inline const struct perf_event_attr
*perf_event_attrs(struct perf_event
*event
)
1226 return ERR_PTR(-EINVAL
);
1228 static inline u64
perf_event_read_local(struct perf_event
*event
) { return -EINVAL
; }
1229 static inline void perf_event_print_debug(void) { }
1230 static inline int perf_event_task_disable(void) { return -EINVAL
; }
1231 static inline int perf_event_task_enable(void) { return -EINVAL
; }
1232 static inline int perf_event_refresh(struct perf_event
*event
, int refresh
)
1238 perf_sw_event(u32 event_id
, u64 nr
, struct pt_regs
*regs
, u64 addr
) { }
1240 perf_sw_event_sched(u32 event_id
, u64 nr
, u64 addr
) { }
1242 perf_bp_event(struct perf_event
*event
, void *data
) { }
1244 static inline int perf_register_guest_info_callbacks
1245 (struct perf_guest_info_callbacks
*callbacks
) { return 0; }
1246 static inline int perf_unregister_guest_info_callbacks
1247 (struct perf_guest_info_callbacks
*callbacks
) { return 0; }
1249 static inline void perf_event_mmap(struct vm_area_struct
*vma
) { }
1250 static inline void perf_event_exec(void) { }
1251 static inline void perf_event_comm(struct task_struct
*tsk
, bool exec
) { }
1252 static inline void perf_event_fork(struct task_struct
*tsk
) { }
1253 static inline void perf_event_init(void) { }
1254 static inline int perf_swevent_get_recursion_context(void) { return -1; }
1255 static inline void perf_swevent_put_recursion_context(int rctx
) { }
1256 static inline u64
perf_swevent_set_period(struct perf_event
*event
) { return 0; }
1257 static inline void perf_event_enable(struct perf_event
*event
) { }
1258 static inline void perf_event_disable(struct perf_event
*event
) { }
1259 static inline int __perf_event_disable(void *info
) { return -1; }
1260 static inline void perf_event_task_tick(void) { }
1261 static inline int perf_event_release_kernel(struct perf_event
*event
) { return 0; }
1264 #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL)
1265 extern void perf_restore_debug_store(void);
1267 static inline void perf_restore_debug_store(void) { }
1270 #define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x))
1273 * This has to have a higher priority than migration_notifier in sched/core.c.
1275 #define perf_cpu_notifier(fn) \
1277 static struct notifier_block fn##_nb = \
1278 { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
1279 unsigned long cpu = smp_processor_id(); \
1280 unsigned long flags; \
1282 cpu_notifier_register_begin(); \
1283 fn(&fn##_nb, (unsigned long)CPU_UP_PREPARE, \
1284 (void *)(unsigned long)cpu); \
1285 local_irq_save(flags); \
1286 fn(&fn##_nb, (unsigned long)CPU_STARTING, \
1287 (void *)(unsigned long)cpu); \
1288 local_irq_restore(flags); \
1289 fn(&fn##_nb, (unsigned long)CPU_ONLINE, \
1290 (void *)(unsigned long)cpu); \
1291 __register_cpu_notifier(&fn##_nb); \
1292 cpu_notifier_register_done(); \
1296 * Bare-bones version of perf_cpu_notifier(), which doesn't invoke the
1297 * callback for already online CPUs.
1299 #define __perf_cpu_notifier(fn) \
1301 static struct notifier_block fn##_nb = \
1302 { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
1304 __register_cpu_notifier(&fn##_nb); \
1307 struct perf_pmu_events_attr
{
1308 struct device_attribute attr
;
1310 const char *event_str
;
1313 ssize_t
perf_event_sysfs_show(struct device
*dev
, struct device_attribute
*attr
,
1316 #define PMU_EVENT_ATTR(_name, _var, _id, _show) \
1317 static struct perf_pmu_events_attr _var = { \
1318 .attr = __ATTR(_name, 0444, _show, NULL), \
1322 #define PMU_EVENT_ATTR_STRING(_name, _var, _str) \
1323 static struct perf_pmu_events_attr _var = { \
1324 .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \
1326 .event_str = _str, \
1329 #define PMU_FORMAT_ATTR(_name, _format) \
1331 _name##_show(struct device *dev, \
1332 struct device_attribute *attr, \
1335 BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
1336 return sprintf(page, _format "\n"); \
1339 static struct device_attribute format_attr_##_name = __ATTR_RO(_name)
1341 #endif /* _LINUX_PERF_EVENT_H */