perf_event.h 24 KB

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