perf_event.h 29 KB

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