perf_event.h 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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. raw_spinlock_t hrtimer_lock;
  492. struct hrtimer hrtimer;
  493. ktime_t hrtimer_interval;
  494. unsigned int hrtimer_active;
  495. struct pmu *unique_pmu;
  496. struct perf_cgroup *cgrp;
  497. };
  498. struct perf_output_handle {
  499. struct perf_event *event;
  500. struct ring_buffer *rb;
  501. unsigned long wakeup;
  502. unsigned long size;
  503. union {
  504. void *addr;
  505. unsigned long head;
  506. };
  507. int page;
  508. };
  509. #ifdef CONFIG_CGROUP_PERF
  510. /*
  511. * perf_cgroup_info keeps track of time_enabled for a cgroup.
  512. * This is a per-cpu dynamically allocated data structure.
  513. */
  514. struct perf_cgroup_info {
  515. u64 time;
  516. u64 timestamp;
  517. };
  518. struct perf_cgroup {
  519. struct cgroup_subsys_state css;
  520. struct perf_cgroup_info __percpu *info;
  521. };
  522. /*
  523. * Must ensure cgroup is pinned (css_get) before calling
  524. * this function. In other words, we cannot call this function
  525. * if there is no cgroup event for the current CPU context.
  526. */
  527. static inline struct perf_cgroup *
  528. perf_cgroup_from_task(struct task_struct *task)
  529. {
  530. return container_of(task_css(task, perf_event_cgrp_id),
  531. struct perf_cgroup, css);
  532. }
  533. #endif /* CONFIG_CGROUP_PERF */
  534. #ifdef CONFIG_PERF_EVENTS
  535. extern void *perf_aux_output_begin(struct perf_output_handle *handle,
  536. struct perf_event *event);
  537. extern void perf_aux_output_end(struct perf_output_handle *handle,
  538. unsigned long size, bool truncated);
  539. extern int perf_aux_output_skip(struct perf_output_handle *handle,
  540. unsigned long size);
  541. extern void *perf_get_aux(struct perf_output_handle *handle);
  542. extern int perf_pmu_register(struct pmu *pmu, const char *name, int type);
  543. extern void perf_pmu_unregister(struct pmu *pmu);
  544. extern int perf_num_counters(void);
  545. extern const char *perf_pmu_name(void);
  546. extern void __perf_event_task_sched_in(struct task_struct *prev,
  547. struct task_struct *task);
  548. extern void __perf_event_task_sched_out(struct task_struct *prev,
  549. struct task_struct *next);
  550. extern int perf_event_init_task(struct task_struct *child);
  551. extern void perf_event_exit_task(struct task_struct *child);
  552. extern void perf_event_free_task(struct task_struct *task);
  553. extern void perf_event_delayed_put(struct task_struct *task);
  554. extern void perf_event_print_debug(void);
  555. extern void perf_pmu_disable(struct pmu *pmu);
  556. extern void perf_pmu_enable(struct pmu *pmu);
  557. extern void perf_sched_cb_dec(struct pmu *pmu);
  558. extern void perf_sched_cb_inc(struct pmu *pmu);
  559. extern int perf_event_task_disable(void);
  560. extern int perf_event_task_enable(void);
  561. extern int perf_event_refresh(struct perf_event *event, int refresh);
  562. extern void perf_event_update_userpage(struct perf_event *event);
  563. extern int perf_event_release_kernel(struct perf_event *event);
  564. extern struct perf_event *
  565. perf_event_create_kernel_counter(struct perf_event_attr *attr,
  566. int cpu,
  567. struct task_struct *task,
  568. perf_overflow_handler_t callback,
  569. void *context);
  570. extern void perf_pmu_migrate_context(struct pmu *pmu,
  571. int src_cpu, int dst_cpu);
  572. extern u64 perf_event_read_value(struct perf_event *event,
  573. u64 *enabled, u64 *running);
  574. struct perf_sample_data {
  575. /*
  576. * Fields set by perf_sample_data_init(), group so as to
  577. * minimize the cachelines touched.
  578. */
  579. u64 addr;
  580. struct perf_raw_record *raw;
  581. struct perf_branch_stack *br_stack;
  582. u64 period;
  583. u64 weight;
  584. u64 txn;
  585. union perf_mem_data_src data_src;
  586. /*
  587. * The other fields, optionally {set,used} by
  588. * perf_{prepare,output}_sample().
  589. */
  590. u64 type;
  591. u64 ip;
  592. struct {
  593. u32 pid;
  594. u32 tid;
  595. } tid_entry;
  596. u64 time;
  597. u64 id;
  598. u64 stream_id;
  599. struct {
  600. u32 cpu;
  601. u32 reserved;
  602. } cpu_entry;
  603. struct perf_callchain_entry *callchain;
  604. /*
  605. * regs_user may point to task_pt_regs or to regs_user_copy, depending
  606. * on arch details.
  607. */
  608. struct perf_regs regs_user;
  609. struct pt_regs regs_user_copy;
  610. struct perf_regs regs_intr;
  611. u64 stack_user_size;
  612. } ____cacheline_aligned;
  613. /* default value for data source */
  614. #define PERF_MEM_NA (PERF_MEM_S(OP, NA) |\
  615. PERF_MEM_S(LVL, NA) |\
  616. PERF_MEM_S(SNOOP, NA) |\
  617. PERF_MEM_S(LOCK, NA) |\
  618. PERF_MEM_S(TLB, NA))
  619. static inline void perf_sample_data_init(struct perf_sample_data *data,
  620. u64 addr, u64 period)
  621. {
  622. /* remaining struct members initialized in perf_prepare_sample() */
  623. data->addr = addr;
  624. data->raw = NULL;
  625. data->br_stack = NULL;
  626. data->period = period;
  627. data->weight = 0;
  628. data->data_src.val = PERF_MEM_NA;
  629. data->txn = 0;
  630. }
  631. extern void perf_output_sample(struct perf_output_handle *handle,
  632. struct perf_event_header *header,
  633. struct perf_sample_data *data,
  634. struct perf_event *event);
  635. extern void perf_prepare_sample(struct perf_event_header *header,
  636. struct perf_sample_data *data,
  637. struct perf_event *event,
  638. struct pt_regs *regs);
  639. extern int perf_event_overflow(struct perf_event *event,
  640. struct perf_sample_data *data,
  641. struct pt_regs *regs);
  642. static inline bool is_sampling_event(struct perf_event *event)
  643. {
  644. return event->attr.sample_period != 0;
  645. }
  646. /*
  647. * Return 1 for a software event, 0 for a hardware event
  648. */
  649. static inline int is_software_event(struct perf_event *event)
  650. {
  651. return event->pmu->task_ctx_nr == perf_sw_context;
  652. }
  653. extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
  654. extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64);
  655. extern void __perf_sw_event(u32, u64, struct pt_regs *, u64);
  656. #ifndef perf_arch_fetch_caller_regs
  657. static inline void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { }
  658. #endif
  659. /*
  660. * Take a snapshot of the regs. Skip ip and frame pointer to
  661. * the nth caller. We only need a few of the regs:
  662. * - ip for PERF_SAMPLE_IP
  663. * - cs for user_mode() tests
  664. * - bp for callchains
  665. * - eflags, for future purposes, just in case
  666. */
  667. static inline void perf_fetch_caller_regs(struct pt_regs *regs)
  668. {
  669. memset(regs, 0, sizeof(*regs));
  670. perf_arch_fetch_caller_regs(regs, CALLER_ADDR0);
  671. }
  672. static __always_inline void
  673. perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
  674. {
  675. if (static_key_false(&perf_swevent_enabled[event_id]))
  676. __perf_sw_event(event_id, nr, regs, addr);
  677. }
  678. DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]);
  679. /*
  680. * 'Special' version for the scheduler, it hard assumes no recursion,
  681. * which is guaranteed by us not actually scheduling inside other swevents
  682. * because those disable preemption.
  683. */
  684. static __always_inline void
  685. perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
  686. {
  687. if (static_key_false(&perf_swevent_enabled[event_id])) {
  688. struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
  689. perf_fetch_caller_regs(regs);
  690. ___perf_sw_event(event_id, nr, regs, addr);
  691. }
  692. }
  693. extern struct static_key_deferred perf_sched_events;
  694. static inline void perf_event_task_sched_in(struct task_struct *prev,
  695. struct task_struct *task)
  696. {
  697. if (static_key_false(&perf_sched_events.key))
  698. __perf_event_task_sched_in(prev, task);
  699. }
  700. static inline void perf_event_task_sched_out(struct task_struct *prev,
  701. struct task_struct *next)
  702. {
  703. perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
  704. if (static_key_false(&perf_sched_events.key))
  705. __perf_event_task_sched_out(prev, next);
  706. }
  707. static inline u64 __perf_event_count(struct perf_event *event)
  708. {
  709. return local64_read(&event->count) + atomic64_read(&event->child_count);
  710. }
  711. extern void perf_event_mmap(struct vm_area_struct *vma);
  712. extern struct perf_guest_info_callbacks *perf_guest_cbs;
  713. extern int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
  714. extern int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
  715. extern void perf_event_exec(void);
  716. extern void perf_event_comm(struct task_struct *tsk, bool exec);
  717. extern void perf_event_fork(struct task_struct *tsk);
  718. /* Callchains */
  719. DECLARE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry);
  720. extern void perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs);
  721. extern void perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs);
  722. static inline void perf_callchain_store(struct perf_callchain_entry *entry, u64 ip)
  723. {
  724. if (entry->nr < PERF_MAX_STACK_DEPTH)
  725. entry->ip[entry->nr++] = ip;
  726. }
  727. extern int sysctl_perf_event_paranoid;
  728. extern int sysctl_perf_event_mlock;
  729. extern int sysctl_perf_event_sample_rate;
  730. extern int sysctl_perf_cpu_time_max_percent;
  731. extern void perf_sample_event_took(u64 sample_len_ns);
  732. extern int perf_proc_update_handler(struct ctl_table *table, int write,
  733. void __user *buffer, size_t *lenp,
  734. loff_t *ppos);
  735. extern int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
  736. void __user *buffer, size_t *lenp,
  737. loff_t *ppos);
  738. static inline bool perf_paranoid_tracepoint_raw(void)
  739. {
  740. return sysctl_perf_event_paranoid > -1;
  741. }
  742. static inline bool perf_paranoid_cpu(void)
  743. {
  744. return sysctl_perf_event_paranoid > 0;
  745. }
  746. static inline bool perf_paranoid_kernel(void)
  747. {
  748. return sysctl_perf_event_paranoid > 1;
  749. }
  750. extern void perf_event_init(void);
  751. extern void perf_tp_event(u64 addr, u64 count, void *record,
  752. int entry_size, struct pt_regs *regs,
  753. struct hlist_head *head, int rctx,
  754. struct task_struct *task);
  755. extern void perf_bp_event(struct perf_event *event, void *data);
  756. #ifndef perf_misc_flags
  757. # define perf_misc_flags(regs) \
  758. (user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
  759. # define perf_instruction_pointer(regs) instruction_pointer(regs)
  760. #endif
  761. static inline bool has_branch_stack(struct perf_event *event)
  762. {
  763. return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK;
  764. }
  765. static inline bool needs_branch_stack(struct perf_event *event)
  766. {
  767. return event->attr.branch_sample_type != 0;
  768. }
  769. static inline bool has_aux(struct perf_event *event)
  770. {
  771. return event->pmu->setup_aux;
  772. }
  773. extern int perf_output_begin(struct perf_output_handle *handle,
  774. struct perf_event *event, unsigned int size);
  775. extern void perf_output_end(struct perf_output_handle *handle);
  776. extern unsigned int perf_output_copy(struct perf_output_handle *handle,
  777. const void *buf, unsigned int len);
  778. extern unsigned int perf_output_skip(struct perf_output_handle *handle,
  779. unsigned int len);
  780. extern int perf_swevent_get_recursion_context(void);
  781. extern void perf_swevent_put_recursion_context(int rctx);
  782. extern u64 perf_swevent_set_period(struct perf_event *event);
  783. extern void perf_event_enable(struct perf_event *event);
  784. extern void perf_event_disable(struct perf_event *event);
  785. extern int __perf_event_disable(void *info);
  786. extern void perf_event_task_tick(void);
  787. #else /* !CONFIG_PERF_EVENTS: */
  788. static inline void *
  789. perf_aux_output_begin(struct perf_output_handle *handle,
  790. struct perf_event *event) { return NULL; }
  791. static inline void
  792. perf_aux_output_end(struct perf_output_handle *handle, unsigned long size,
  793. bool truncated) { }
  794. static inline int
  795. perf_aux_output_skip(struct perf_output_handle *handle,
  796. unsigned long size) { return -EINVAL; }
  797. static inline void *
  798. perf_get_aux(struct perf_output_handle *handle) { return NULL; }
  799. static inline void
  800. perf_event_task_sched_in(struct task_struct *prev,
  801. struct task_struct *task) { }
  802. static inline void
  803. perf_event_task_sched_out(struct task_struct *prev,
  804. struct task_struct *next) { }
  805. static inline int perf_event_init_task(struct task_struct *child) { return 0; }
  806. static inline void perf_event_exit_task(struct task_struct *child) { }
  807. static inline void perf_event_free_task(struct task_struct *task) { }
  808. static inline void perf_event_delayed_put(struct task_struct *task) { }
  809. static inline void perf_event_print_debug(void) { }
  810. static inline int perf_event_task_disable(void) { return -EINVAL; }
  811. static inline int perf_event_task_enable(void) { return -EINVAL; }
  812. static inline int perf_event_refresh(struct perf_event *event, int refresh)
  813. {
  814. return -EINVAL;
  815. }
  816. static inline void
  817. perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { }
  818. static inline void
  819. perf_sw_event_sched(u32 event_id, u64 nr, u64 addr) { }
  820. static inline void
  821. perf_bp_event(struct perf_event *event, void *data) { }
  822. static inline int perf_register_guest_info_callbacks
  823. (struct perf_guest_info_callbacks *callbacks) { return 0; }
  824. static inline int perf_unregister_guest_info_callbacks
  825. (struct perf_guest_info_callbacks *callbacks) { return 0; }
  826. static inline void perf_event_mmap(struct vm_area_struct *vma) { }
  827. static inline void perf_event_exec(void) { }
  828. static inline void perf_event_comm(struct task_struct *tsk, bool exec) { }
  829. static inline void perf_event_fork(struct task_struct *tsk) { }
  830. static inline void perf_event_init(void) { }
  831. static inline int perf_swevent_get_recursion_context(void) { return -1; }
  832. static inline void perf_swevent_put_recursion_context(int rctx) { }
  833. static inline u64 perf_swevent_set_period(struct perf_event *event) { return 0; }
  834. static inline void perf_event_enable(struct perf_event *event) { }
  835. static inline void perf_event_disable(struct perf_event *event) { }
  836. static inline int __perf_event_disable(void *info) { return -1; }
  837. static inline void perf_event_task_tick(void) { }
  838. #endif
  839. #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_NO_HZ_FULL)
  840. extern bool perf_event_can_stop_tick(void);
  841. #else
  842. static inline bool perf_event_can_stop_tick(void) { return true; }
  843. #endif
  844. #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL)
  845. extern void perf_restore_debug_store(void);
  846. #else
  847. static inline void perf_restore_debug_store(void) { }
  848. #endif
  849. #define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x))
  850. /*
  851. * This has to have a higher priority than migration_notifier in sched/core.c.
  852. */
  853. #define perf_cpu_notifier(fn) \
  854. do { \
  855. static struct notifier_block fn##_nb = \
  856. { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
  857. unsigned long cpu = smp_processor_id(); \
  858. unsigned long flags; \
  859. \
  860. cpu_notifier_register_begin(); \
  861. fn(&fn##_nb, (unsigned long)CPU_UP_PREPARE, \
  862. (void *)(unsigned long)cpu); \
  863. local_irq_save(flags); \
  864. fn(&fn##_nb, (unsigned long)CPU_STARTING, \
  865. (void *)(unsigned long)cpu); \
  866. local_irq_restore(flags); \
  867. fn(&fn##_nb, (unsigned long)CPU_ONLINE, \
  868. (void *)(unsigned long)cpu); \
  869. __register_cpu_notifier(&fn##_nb); \
  870. cpu_notifier_register_done(); \
  871. } while (0)
  872. /*
  873. * Bare-bones version of perf_cpu_notifier(), which doesn't invoke the
  874. * callback for already online CPUs.
  875. */
  876. #define __perf_cpu_notifier(fn) \
  877. do { \
  878. static struct notifier_block fn##_nb = \
  879. { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
  880. \
  881. __register_cpu_notifier(&fn##_nb); \
  882. } while (0)
  883. struct perf_pmu_events_attr {
  884. struct device_attribute attr;
  885. u64 id;
  886. const char *event_str;
  887. };
  888. ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
  889. char *page);
  890. #define PMU_EVENT_ATTR(_name, _var, _id, _show) \
  891. static struct perf_pmu_events_attr _var = { \
  892. .attr = __ATTR(_name, 0444, _show, NULL), \
  893. .id = _id, \
  894. };
  895. #define PMU_EVENT_ATTR_STRING(_name, _var, _str) \
  896. static struct perf_pmu_events_attr _var = { \
  897. .attr = __ATTR(_name, 0444, perf_event_sysfs_show, NULL), \
  898. .id = 0, \
  899. .event_str = _str, \
  900. };
  901. #define PMU_FORMAT_ATTR(_name, _format) \
  902. static ssize_t \
  903. _name##_show(struct device *dev, \
  904. struct device_attribute *attr, \
  905. char *page) \
  906. { \
  907. BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
  908. return sprintf(page, _format "\n"); \
  909. } \
  910. \
  911. static struct device_attribute format_attr_##_name = __ATTR_RO(_name)
  912. #endif /* _LINUX_PERF_EVENT_H */