perf_event.h 30 KB

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