perf_event.h 28 KB

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