perf_event.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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/atomic.h>
  46. #include <linux/sysfs.h>
  47. #include <linux/perf_regs.h>
  48. #include <asm/local.h>
  49. struct perf_callchain_entry {
  50. __u64 nr;
  51. __u64 ip[PERF_MAX_STACK_DEPTH];
  52. };
  53. struct perf_raw_record {
  54. u32 size;
  55. void *data;
  56. };
  57. /*
  58. * branch stack layout:
  59. * nr: number of taken branches stored in entries[]
  60. *
  61. * Note that nr can vary from sample to sample
  62. * branches (to, from) are stored from most recent
  63. * to least recent, i.e., entries[0] contains the most
  64. * recent branch.
  65. */
  66. struct perf_branch_stack {
  67. __u64 nr;
  68. struct perf_branch_entry entries[0];
  69. };
  70. struct perf_regs_user {
  71. __u64 abi;
  72. struct pt_regs *regs;
  73. };
  74. struct task_struct;
  75. /*
  76. * extra PMU register associated with an event
  77. */
  78. struct hw_perf_event_extra {
  79. u64 config; /* register value */
  80. unsigned int reg; /* register address or index */
  81. int alloc; /* extra register already allocated */
  82. int idx; /* index in shared_regs->regs[] */
  83. };
  84. struct event_constraint;
  85. /**
  86. * struct hw_perf_event - performance event hardware details:
  87. */
  88. struct hw_perf_event {
  89. #ifdef CONFIG_PERF_EVENTS
  90. union {
  91. struct { /* hardware */
  92. u64 config;
  93. u64 last_tag;
  94. unsigned long config_base;
  95. unsigned long event_base;
  96. int event_base_rdpmc;
  97. int idx;
  98. int last_cpu;
  99. int flags;
  100. struct hw_perf_event_extra extra_reg;
  101. struct hw_perf_event_extra branch_reg;
  102. struct event_constraint *constraint;
  103. };
  104. struct { /* software */
  105. struct hrtimer hrtimer;
  106. };
  107. struct { /* tracepoint */
  108. struct task_struct *tp_target;
  109. /* for tp_event->class */
  110. struct list_head tp_list;
  111. };
  112. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  113. struct { /* breakpoint */
  114. /*
  115. * Crufty hack to avoid the chicken and egg
  116. * problem hw_breakpoint has with context
  117. * creation and event initalization.
  118. */
  119. struct task_struct *bp_target;
  120. struct arch_hw_breakpoint info;
  121. struct list_head bp_list;
  122. };
  123. #endif
  124. };
  125. int state;
  126. local64_t prev_count;
  127. u64 sample_period;
  128. u64 last_period;
  129. local64_t period_left;
  130. u64 interrupts_seq;
  131. u64 interrupts;
  132. u64 freq_time_stamp;
  133. u64 freq_count_stamp;
  134. #endif
  135. };
  136. /*
  137. * hw_perf_event::state flags
  138. */
  139. #define PERF_HES_STOPPED 0x01 /* the counter is stopped */
  140. #define PERF_HES_UPTODATE 0x02 /* event->count up-to-date */
  141. #define PERF_HES_ARCH 0x04
  142. struct perf_event;
  143. /*
  144. * Common implementation detail of pmu::{start,commit,cancel}_txn
  145. */
  146. #define PERF_EVENT_TXN 0x1
  147. /**
  148. * struct pmu - generic performance monitoring unit
  149. */
  150. struct pmu {
  151. struct list_head entry;
  152. struct device *dev;
  153. const struct attribute_group **attr_groups;
  154. const char *name;
  155. int type;
  156. int * __percpu pmu_disable_count;
  157. struct perf_cpu_context * __percpu pmu_cpu_context;
  158. int task_ctx_nr;
  159. int hrtimer_interval_ms;
  160. /*
  161. * Fully disable/enable this PMU, can be used to protect from the PMI
  162. * as well as for lazy/batch writing of the MSRs.
  163. */
  164. void (*pmu_enable) (struct pmu *pmu); /* optional */
  165. void (*pmu_disable) (struct pmu *pmu); /* optional */
  166. /*
  167. * Try and initialize the event for this PMU.
  168. * Should return -ENOENT when the @event doesn't match this PMU.
  169. */
  170. int (*event_init) (struct perf_event *event);
  171. #define PERF_EF_START 0x01 /* start the counter when adding */
  172. #define PERF_EF_RELOAD 0x02 /* reload the counter when starting */
  173. #define PERF_EF_UPDATE 0x04 /* update the counter when stopping */
  174. /*
  175. * Adds/Removes a counter to/from the PMU, can be done inside
  176. * a transaction, see the ->*_txn() methods.
  177. */
  178. int (*add) (struct perf_event *event, int flags);
  179. void (*del) (struct perf_event *event, int flags);
  180. /*
  181. * Starts/Stops a counter present on the PMU. The PMI handler
  182. * should stop the counter when perf_event_overflow() returns
  183. * !0. ->start() will be used to continue.
  184. */
  185. void (*start) (struct perf_event *event, int flags);
  186. void (*stop) (struct perf_event *event, int flags);
  187. /*
  188. * Updates the counter value of the event.
  189. */
  190. void (*read) (struct perf_event *event);
  191. /*
  192. * Group events scheduling is treated as a transaction, add
  193. * group events as a whole and perform one schedulability test.
  194. * If the test fails, roll back the whole group
  195. *
  196. * Start the transaction, after this ->add() doesn't need to
  197. * do schedulability tests.
  198. */
  199. void (*start_txn) (struct pmu *pmu); /* optional */
  200. /*
  201. * If ->start_txn() disabled the ->add() schedulability test
  202. * then ->commit_txn() is required to perform one. On success
  203. * the transaction is closed. On error the transaction is kept
  204. * open until ->cancel_txn() is called.
  205. */
  206. int (*commit_txn) (struct pmu *pmu); /* optional */
  207. /*
  208. * Will cancel the transaction, assumes ->del() is called
  209. * for each successful ->add() during the transaction.
  210. */
  211. void (*cancel_txn) (struct pmu *pmu); /* optional */
  212. /*
  213. * Will return the value for perf_event_mmap_page::index for this event,
  214. * if no implementation is provided it will default to: event->hw.idx + 1.
  215. */
  216. int (*event_idx) (struct perf_event *event); /*optional */
  217. /*
  218. * flush branch stack on context-switches (needed in cpu-wide mode)
  219. */
  220. void (*flush_branch_stack) (void);
  221. };
  222. /**
  223. * enum perf_event_active_state - the states of a event
  224. */
  225. enum perf_event_active_state {
  226. PERF_EVENT_STATE_ERROR = -2,
  227. PERF_EVENT_STATE_OFF = -1,
  228. PERF_EVENT_STATE_INACTIVE = 0,
  229. PERF_EVENT_STATE_ACTIVE = 1,
  230. };
  231. struct file;
  232. struct perf_sample_data;
  233. typedef void (*perf_overflow_handler_t)(struct perf_event *,
  234. struct perf_sample_data *,
  235. struct pt_regs *regs);
  236. enum perf_group_flag {
  237. PERF_GROUP_SOFTWARE = 0x1,
  238. };
  239. #define SWEVENT_HLIST_BITS 8
  240. #define SWEVENT_HLIST_SIZE (1 << SWEVENT_HLIST_BITS)
  241. struct swevent_hlist {
  242. struct hlist_head heads[SWEVENT_HLIST_SIZE];
  243. struct rcu_head rcu_head;
  244. };
  245. #define PERF_ATTACH_CONTEXT 0x01
  246. #define PERF_ATTACH_GROUP 0x02
  247. #define PERF_ATTACH_TASK 0x04
  248. struct perf_cgroup;
  249. struct ring_buffer;
  250. /**
  251. * struct perf_event - performance event kernel representation:
  252. */
  253. struct perf_event {
  254. #ifdef CONFIG_PERF_EVENTS
  255. struct list_head group_entry;
  256. struct list_head event_entry;
  257. struct list_head sibling_list;
  258. struct hlist_node hlist_entry;
  259. int nr_siblings;
  260. int group_flags;
  261. struct perf_event *group_leader;
  262. struct pmu *pmu;
  263. enum perf_event_active_state state;
  264. unsigned int attach_state;
  265. local64_t count;
  266. atomic64_t child_count;
  267. /*
  268. * These are the total time in nanoseconds that the event
  269. * has been enabled (i.e. eligible to run, and the task has
  270. * been scheduled in, if this is a per-task event)
  271. * and running (scheduled onto the CPU), respectively.
  272. *
  273. * They are computed from tstamp_enabled, tstamp_running and
  274. * tstamp_stopped when the event is in INACTIVE or ACTIVE state.
  275. */
  276. u64 total_time_enabled;
  277. u64 total_time_running;
  278. /*
  279. * These are timestamps used for computing total_time_enabled
  280. * and total_time_running when the event is in INACTIVE or
  281. * ACTIVE state, measured in nanoseconds from an arbitrary point
  282. * in time.
  283. * tstamp_enabled: the notional time when the event was enabled
  284. * tstamp_running: the notional time when the event was scheduled on
  285. * tstamp_stopped: in INACTIVE state, the notional time when the
  286. * event was scheduled off.
  287. */
  288. u64 tstamp_enabled;
  289. u64 tstamp_running;
  290. u64 tstamp_stopped;
  291. /*
  292. * timestamp shadows the actual context timing but it can
  293. * be safely used in NMI interrupt context. It reflects the
  294. * context time as it was when the event was last scheduled in.
  295. *
  296. * ctx_time already accounts for ctx->timestamp. Therefore to
  297. * compute ctx_time for a sample, simply add perf_clock().
  298. */
  299. u64 shadow_ctx_time;
  300. struct perf_event_attr attr;
  301. u16 header_size;
  302. u16 id_header_size;
  303. u16 read_size;
  304. struct hw_perf_event hw;
  305. struct perf_event_context *ctx;
  306. atomic_long_t refcount;
  307. /*
  308. * These accumulate total time (in nanoseconds) that children
  309. * events have been enabled and running, respectively.
  310. */
  311. atomic64_t child_total_time_enabled;
  312. atomic64_t child_total_time_running;
  313. /*
  314. * Protect attach/detach and child_list:
  315. */
  316. struct mutex child_mutex;
  317. struct list_head child_list;
  318. struct perf_event *parent;
  319. int oncpu;
  320. int cpu;
  321. struct list_head owner_entry;
  322. struct task_struct *owner;
  323. /* mmap bits */
  324. struct mutex mmap_mutex;
  325. atomic_t mmap_count;
  326. struct ring_buffer *rb;
  327. struct list_head rb_entry;
  328. /* poll related */
  329. wait_queue_head_t waitq;
  330. struct fasync_struct *fasync;
  331. /* delayed work for NMIs and such */
  332. int pending_wakeup;
  333. int pending_kill;
  334. int pending_disable;
  335. struct irq_work pending;
  336. atomic_t event_limit;
  337. void (*destroy)(struct perf_event *);
  338. struct rcu_head rcu_head;
  339. struct pid_namespace *ns;
  340. u64 id;
  341. perf_overflow_handler_t overflow_handler;
  342. void *overflow_handler_context;
  343. #ifdef CONFIG_EVENT_TRACING
  344. struct ftrace_event_call *tp_event;
  345. struct event_filter *filter;
  346. #ifdef CONFIG_FUNCTION_TRACER
  347. struct ftrace_ops ftrace_ops;
  348. #endif
  349. #endif
  350. #ifdef CONFIG_CGROUP_PERF
  351. struct perf_cgroup *cgrp; /* cgroup event is attach to */
  352. int cgrp_defer_enabled;
  353. #endif
  354. #endif /* CONFIG_PERF_EVENTS */
  355. };
  356. enum perf_event_context_type {
  357. task_context,
  358. cpu_context,
  359. };
  360. /**
  361. * struct perf_event_context - event context structure
  362. *
  363. * Used as a container for task events and CPU events as well:
  364. */
  365. struct perf_event_context {
  366. struct pmu *pmu;
  367. enum perf_event_context_type type;
  368. /*
  369. * Protect the states of the events in the list,
  370. * nr_active, and the list:
  371. */
  372. raw_spinlock_t lock;
  373. /*
  374. * Protect the list of events. Locking either mutex or lock
  375. * is sufficient to ensure the list doesn't change; to change
  376. * the list you need to lock both the mutex and the spinlock.
  377. */
  378. struct mutex mutex;
  379. struct list_head pinned_groups;
  380. struct list_head flexible_groups;
  381. struct list_head event_list;
  382. int nr_events;
  383. int nr_active;
  384. int is_active;
  385. int nr_stat;
  386. int nr_freq;
  387. int rotate_disable;
  388. atomic_t refcount;
  389. struct task_struct *task;
  390. /*
  391. * Context clock, runs when context enabled.
  392. */
  393. u64 time;
  394. u64 timestamp;
  395. /*
  396. * These fields let us detect when two contexts have both
  397. * been cloned (inherited) from a common ancestor.
  398. */
  399. struct perf_event_context *parent_ctx;
  400. u64 parent_gen;
  401. u64 generation;
  402. int pin_count;
  403. int nr_cgroups; /* cgroup evts */
  404. int nr_branch_stack; /* branch_stack evt */
  405. struct rcu_head rcu_head;
  406. };
  407. /*
  408. * Number of contexts where an event can trigger:
  409. * task, softirq, hardirq, nmi.
  410. */
  411. #define PERF_NR_CONTEXTS 4
  412. /**
  413. * struct perf_event_cpu_context - per cpu event context structure
  414. */
  415. struct perf_cpu_context {
  416. struct perf_event_context ctx;
  417. struct perf_event_context *task_ctx;
  418. int active_oncpu;
  419. int exclusive;
  420. struct hrtimer hrtimer;
  421. ktime_t hrtimer_interval;
  422. struct list_head rotation_list;
  423. struct pmu *unique_pmu;
  424. struct perf_cgroup *cgrp;
  425. };
  426. struct perf_output_handle {
  427. struct perf_event *event;
  428. struct ring_buffer *rb;
  429. unsigned long wakeup;
  430. unsigned long size;
  431. void *addr;
  432. int page;
  433. };
  434. #ifdef CONFIG_PERF_EVENTS
  435. extern int perf_pmu_register(struct pmu *pmu, const char *name, int type);
  436. extern void perf_pmu_unregister(struct pmu *pmu);
  437. extern int perf_num_counters(void);
  438. extern const char *perf_pmu_name(void);
  439. extern void __perf_event_task_sched_in(struct task_struct *prev,
  440. struct task_struct *task);
  441. extern void __perf_event_task_sched_out(struct task_struct *prev,
  442. struct task_struct *next);
  443. extern int perf_event_init_task(struct task_struct *child);
  444. extern void perf_event_exit_task(struct task_struct *child);
  445. extern void perf_event_free_task(struct task_struct *task);
  446. extern void perf_event_delayed_put(struct task_struct *task);
  447. extern void perf_event_print_debug(void);
  448. extern void perf_pmu_disable(struct pmu *pmu);
  449. extern void perf_pmu_enable(struct pmu *pmu);
  450. extern int perf_event_task_disable(void);
  451. extern int perf_event_task_enable(void);
  452. extern int perf_event_refresh(struct perf_event *event, int refresh);
  453. extern void perf_event_update_userpage(struct perf_event *event);
  454. extern int perf_event_release_kernel(struct perf_event *event);
  455. extern struct perf_event *
  456. perf_event_create_kernel_counter(struct perf_event_attr *attr,
  457. int cpu,
  458. struct task_struct *task,
  459. perf_overflow_handler_t callback,
  460. void *context);
  461. extern void perf_pmu_migrate_context(struct pmu *pmu,
  462. int src_cpu, int dst_cpu);
  463. extern u64 perf_event_read_value(struct perf_event *event,
  464. u64 *enabled, u64 *running);
  465. struct perf_sample_data {
  466. u64 type;
  467. u64 ip;
  468. struct {
  469. u32 pid;
  470. u32 tid;
  471. } tid_entry;
  472. u64 time;
  473. u64 addr;
  474. u64 id;
  475. u64 stream_id;
  476. struct {
  477. u32 cpu;
  478. u32 reserved;
  479. } cpu_entry;
  480. u64 period;
  481. union perf_mem_data_src data_src;
  482. struct perf_callchain_entry *callchain;
  483. struct perf_raw_record *raw;
  484. struct perf_branch_stack *br_stack;
  485. struct perf_regs_user regs_user;
  486. u64 stack_user_size;
  487. u64 weight;
  488. };
  489. static inline void perf_sample_data_init(struct perf_sample_data *data,
  490. u64 addr, u64 period)
  491. {
  492. /* remaining struct members initialized in perf_prepare_sample() */
  493. data->addr = addr;
  494. data->raw = NULL;
  495. data->br_stack = NULL;
  496. data->period = period;
  497. data->regs_user.abi = PERF_SAMPLE_REGS_ABI_NONE;
  498. data->regs_user.regs = NULL;
  499. data->stack_user_size = 0;
  500. data->weight = 0;
  501. data->data_src.val = 0;
  502. }
  503. extern void perf_output_sample(struct perf_output_handle *handle,
  504. struct perf_event_header *header,
  505. struct perf_sample_data *data,
  506. struct perf_event *event);
  507. extern void perf_prepare_sample(struct perf_event_header *header,
  508. struct perf_sample_data *data,
  509. struct perf_event *event,
  510. struct pt_regs *regs);
  511. extern int perf_event_overflow(struct perf_event *event,
  512. struct perf_sample_data *data,
  513. struct pt_regs *regs);
  514. static inline bool is_sampling_event(struct perf_event *event)
  515. {
  516. return event->attr.sample_period != 0;
  517. }
  518. /*
  519. * Return 1 for a software event, 0 for a hardware event
  520. */
  521. static inline int is_software_event(struct perf_event *event)
  522. {
  523. return event->pmu->task_ctx_nr == perf_sw_context;
  524. }
  525. extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
  526. extern void __perf_sw_event(u32, u64, struct pt_regs *, u64);
  527. #ifndef perf_arch_fetch_caller_regs
  528. static inline void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { }
  529. #endif
  530. /*
  531. * Take a snapshot of the regs. Skip ip and frame pointer to
  532. * the nth caller. We only need a few of the regs:
  533. * - ip for PERF_SAMPLE_IP
  534. * - cs for user_mode() tests
  535. * - bp for callchains
  536. * - eflags, for future purposes, just in case
  537. */
  538. static inline void perf_fetch_caller_regs(struct pt_regs *regs)
  539. {
  540. memset(regs, 0, sizeof(*regs));
  541. perf_arch_fetch_caller_regs(regs, CALLER_ADDR0);
  542. }
  543. static __always_inline void
  544. perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
  545. {
  546. struct pt_regs hot_regs;
  547. if (static_key_false(&perf_swevent_enabled[event_id])) {
  548. if (!regs) {
  549. perf_fetch_caller_regs(&hot_regs);
  550. regs = &hot_regs;
  551. }
  552. __perf_sw_event(event_id, nr, regs, addr);
  553. }
  554. }
  555. extern struct static_key_deferred perf_sched_events;
  556. static inline void perf_event_task_sched_in(struct task_struct *prev,
  557. struct task_struct *task)
  558. {
  559. if (static_key_false(&perf_sched_events.key))
  560. __perf_event_task_sched_in(prev, task);
  561. }
  562. static inline void perf_event_task_sched_out(struct task_struct *prev,
  563. struct task_struct *next)
  564. {
  565. perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, NULL, 0);
  566. if (static_key_false(&perf_sched_events.key))
  567. __perf_event_task_sched_out(prev, next);
  568. }
  569. extern void perf_event_mmap(struct vm_area_struct *vma);
  570. extern struct perf_guest_info_callbacks *perf_guest_cbs;
  571. extern int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
  572. extern int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
  573. extern void perf_event_comm(struct task_struct *tsk);
  574. extern void perf_event_fork(struct task_struct *tsk);
  575. /* Callchains */
  576. DECLARE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry);
  577. extern void perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs);
  578. extern void perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs);
  579. static inline void perf_callchain_store(struct perf_callchain_entry *entry, u64 ip)
  580. {
  581. if (entry->nr < PERF_MAX_STACK_DEPTH)
  582. entry->ip[entry->nr++] = ip;
  583. }
  584. extern int sysctl_perf_event_paranoid;
  585. extern int sysctl_perf_event_mlock;
  586. extern int sysctl_perf_event_sample_rate;
  587. extern int sysctl_perf_cpu_time_max_percent;
  588. extern void perf_sample_event_took(u64 sample_len_ns);
  589. extern int perf_proc_update_handler(struct ctl_table *table, int write,
  590. void __user *buffer, size_t *lenp,
  591. loff_t *ppos);
  592. extern int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
  593. void __user *buffer, size_t *lenp,
  594. loff_t *ppos);
  595. static inline bool perf_paranoid_tracepoint_raw(void)
  596. {
  597. return sysctl_perf_event_paranoid > -1;
  598. }
  599. static inline bool perf_paranoid_cpu(void)
  600. {
  601. return sysctl_perf_event_paranoid > 0;
  602. }
  603. static inline bool perf_paranoid_kernel(void)
  604. {
  605. return sysctl_perf_event_paranoid > 1;
  606. }
  607. extern void perf_event_init(void);
  608. extern void perf_tp_event(u64 addr, u64 count, void *record,
  609. int entry_size, struct pt_regs *regs,
  610. struct hlist_head *head, int rctx,
  611. struct task_struct *task);
  612. extern void perf_bp_event(struct perf_event *event, void *data);
  613. #ifndef perf_misc_flags
  614. # define perf_misc_flags(regs) \
  615. (user_mode(regs) ? PERF_RECORD_MISC_USER : PERF_RECORD_MISC_KERNEL)
  616. # define perf_instruction_pointer(regs) instruction_pointer(regs)
  617. #endif
  618. static inline bool has_branch_stack(struct perf_event *event)
  619. {
  620. return event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK;
  621. }
  622. extern int perf_output_begin(struct perf_output_handle *handle,
  623. struct perf_event *event, unsigned int size);
  624. extern void perf_output_end(struct perf_output_handle *handle);
  625. extern unsigned int perf_output_copy(struct perf_output_handle *handle,
  626. const void *buf, unsigned int len);
  627. extern unsigned int perf_output_skip(struct perf_output_handle *handle,
  628. unsigned int len);
  629. extern int perf_swevent_get_recursion_context(void);
  630. extern void perf_swevent_put_recursion_context(int rctx);
  631. extern u64 perf_swevent_set_period(struct perf_event *event);
  632. extern void perf_event_enable(struct perf_event *event);
  633. extern void perf_event_disable(struct perf_event *event);
  634. extern int __perf_event_disable(void *info);
  635. extern void perf_event_task_tick(void);
  636. #else
  637. static inline void
  638. perf_event_task_sched_in(struct task_struct *prev,
  639. struct task_struct *task) { }
  640. static inline void
  641. perf_event_task_sched_out(struct task_struct *prev,
  642. struct task_struct *next) { }
  643. static inline int perf_event_init_task(struct task_struct *child) { return 0; }
  644. static inline void perf_event_exit_task(struct task_struct *child) { }
  645. static inline void perf_event_free_task(struct task_struct *task) { }
  646. static inline void perf_event_delayed_put(struct task_struct *task) { }
  647. static inline void perf_event_print_debug(void) { }
  648. static inline int perf_event_task_disable(void) { return -EINVAL; }
  649. static inline int perf_event_task_enable(void) { return -EINVAL; }
  650. static inline int perf_event_refresh(struct perf_event *event, int refresh)
  651. {
  652. return -EINVAL;
  653. }
  654. static inline void
  655. perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { }
  656. static inline void
  657. perf_bp_event(struct perf_event *event, void *data) { }
  658. static inline int perf_register_guest_info_callbacks
  659. (struct perf_guest_info_callbacks *callbacks) { return 0; }
  660. static inline int perf_unregister_guest_info_callbacks
  661. (struct perf_guest_info_callbacks *callbacks) { return 0; }
  662. static inline void perf_event_mmap(struct vm_area_struct *vma) { }
  663. static inline void perf_event_comm(struct task_struct *tsk) { }
  664. static inline void perf_event_fork(struct task_struct *tsk) { }
  665. static inline void perf_event_init(void) { }
  666. static inline int perf_swevent_get_recursion_context(void) { return -1; }
  667. static inline void perf_swevent_put_recursion_context(int rctx) { }
  668. static inline u64 perf_swevent_set_period(struct perf_event *event) { return 0; }
  669. static inline void perf_event_enable(struct perf_event *event) { }
  670. static inline void perf_event_disable(struct perf_event *event) { }
  671. static inline int __perf_event_disable(void *info) { return -1; }
  672. static inline void perf_event_task_tick(void) { }
  673. #endif
  674. #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_NO_HZ_FULL)
  675. extern bool perf_event_can_stop_tick(void);
  676. #else
  677. static inline bool perf_event_can_stop_tick(void) { return true; }
  678. #endif
  679. #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL)
  680. extern void perf_restore_debug_store(void);
  681. #else
  682. static inline void perf_restore_debug_store(void) { }
  683. #endif
  684. #define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x))
  685. /*
  686. * This has to have a higher priority than migration_notifier in sched/core.c.
  687. */
  688. #define perf_cpu_notifier(fn) \
  689. do { \
  690. static struct notifier_block fn##_nb = \
  691. { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
  692. unsigned long cpu = smp_processor_id(); \
  693. unsigned long flags; \
  694. fn(&fn##_nb, (unsigned long)CPU_UP_PREPARE, \
  695. (void *)(unsigned long)cpu); \
  696. local_irq_save(flags); \
  697. fn(&fn##_nb, (unsigned long)CPU_STARTING, \
  698. (void *)(unsigned long)cpu); \
  699. local_irq_restore(flags); \
  700. fn(&fn##_nb, (unsigned long)CPU_ONLINE, \
  701. (void *)(unsigned long)cpu); \
  702. register_cpu_notifier(&fn##_nb); \
  703. } while (0)
  704. struct perf_pmu_events_attr {
  705. struct device_attribute attr;
  706. u64 id;
  707. const char *event_str;
  708. };
  709. #define PMU_EVENT_ATTR(_name, _var, _id, _show) \
  710. static struct perf_pmu_events_attr _var = { \
  711. .attr = __ATTR(_name, 0444, _show, NULL), \
  712. .id = _id, \
  713. };
  714. #define PMU_FORMAT_ATTR(_name, _format) \
  715. static ssize_t \
  716. _name##_show(struct device *dev, \
  717. struct device_attribute *attr, \
  718. char *page) \
  719. { \
  720. BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
  721. return sprintf(page, _format "\n"); \
  722. } \
  723. \
  724. static struct device_attribute format_attr_##_name = __ATTR_RO(_name)
  725. #endif /* _LINUX_PERF_EVENT_H */