perf_event.h 30 KB

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