perf_event.h 23 KB

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