perf_event.h 23 KB

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