perf_event.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /*
  2. * Performance events:
  3. *
  4. * Copyright (C) 2008-2009, Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright (C) 2008-2009, Red Hat, Inc., Ingo Molnar
  6. * Copyright (C) 2008-2009, 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 <linux/types.h>
  17. #include <linux/ioctl.h>
  18. #include <asm/byteorder.h>
  19. /*
  20. * User-space ABI bits:
  21. */
  22. /*
  23. * attr.type
  24. */
  25. enum perf_type_id {
  26. PERF_TYPE_HARDWARE = 0,
  27. PERF_TYPE_SOFTWARE = 1,
  28. PERF_TYPE_TRACEPOINT = 2,
  29. PERF_TYPE_HW_CACHE = 3,
  30. PERF_TYPE_RAW = 4,
  31. PERF_TYPE_MAX, /* non-ABI */
  32. };
  33. /*
  34. * Generalized performance event event_id types, used by the
  35. * attr.event_id parameter of the sys_perf_event_open()
  36. * syscall:
  37. */
  38. enum perf_hw_id {
  39. /*
  40. * Common hardware events, generalized by the kernel:
  41. */
  42. PERF_COUNT_HW_CPU_CYCLES = 0,
  43. PERF_COUNT_HW_INSTRUCTIONS = 1,
  44. PERF_COUNT_HW_CACHE_REFERENCES = 2,
  45. PERF_COUNT_HW_CACHE_MISSES = 3,
  46. PERF_COUNT_HW_BRANCH_INSTRUCTIONS = 4,
  47. PERF_COUNT_HW_BRANCH_MISSES = 5,
  48. PERF_COUNT_HW_BUS_CYCLES = 6,
  49. PERF_COUNT_HW_MAX, /* non-ABI */
  50. };
  51. /*
  52. * Generalized hardware cache events:
  53. *
  54. * { L1-D, L1-I, LLC, ITLB, DTLB, BPU } x
  55. * { read, write, prefetch } x
  56. * { accesses, misses }
  57. */
  58. enum perf_hw_cache_id {
  59. PERF_COUNT_HW_CACHE_L1D = 0,
  60. PERF_COUNT_HW_CACHE_L1I = 1,
  61. PERF_COUNT_HW_CACHE_LL = 2,
  62. PERF_COUNT_HW_CACHE_DTLB = 3,
  63. PERF_COUNT_HW_CACHE_ITLB = 4,
  64. PERF_COUNT_HW_CACHE_BPU = 5,
  65. PERF_COUNT_HW_CACHE_MAX, /* non-ABI */
  66. };
  67. enum perf_hw_cache_op_id {
  68. PERF_COUNT_HW_CACHE_OP_READ = 0,
  69. PERF_COUNT_HW_CACHE_OP_WRITE = 1,
  70. PERF_COUNT_HW_CACHE_OP_PREFETCH = 2,
  71. PERF_COUNT_HW_CACHE_OP_MAX, /* non-ABI */
  72. };
  73. enum perf_hw_cache_op_result_id {
  74. PERF_COUNT_HW_CACHE_RESULT_ACCESS = 0,
  75. PERF_COUNT_HW_CACHE_RESULT_MISS = 1,
  76. PERF_COUNT_HW_CACHE_RESULT_MAX, /* non-ABI */
  77. };
  78. /*
  79. * Special "software" events provided by the kernel, even if the hardware
  80. * does not support performance events. These events measure various
  81. * physical and sw events of the kernel (and allow the profiling of them as
  82. * well):
  83. */
  84. enum perf_sw_ids {
  85. PERF_COUNT_SW_CPU_CLOCK = 0,
  86. PERF_COUNT_SW_TASK_CLOCK = 1,
  87. PERF_COUNT_SW_PAGE_FAULTS = 2,
  88. PERF_COUNT_SW_CONTEXT_SWITCHES = 3,
  89. PERF_COUNT_SW_CPU_MIGRATIONS = 4,
  90. PERF_COUNT_SW_PAGE_FAULTS_MIN = 5,
  91. PERF_COUNT_SW_PAGE_FAULTS_MAJ = 6,
  92. PERF_COUNT_SW_ALIGNMENT_FAULTS = 7,
  93. PERF_COUNT_SW_EMULATION_FAULTS = 8,
  94. PERF_COUNT_SW_MAX, /* non-ABI */
  95. };
  96. /*
  97. * Bits that can be set in attr.sample_type to request information
  98. * in the overflow packets.
  99. */
  100. enum perf_event_sample_format {
  101. PERF_SAMPLE_IP = 1U << 0,
  102. PERF_SAMPLE_TID = 1U << 1,
  103. PERF_SAMPLE_TIME = 1U << 2,
  104. PERF_SAMPLE_ADDR = 1U << 3,
  105. PERF_SAMPLE_READ = 1U << 4,
  106. PERF_SAMPLE_CALLCHAIN = 1U << 5,
  107. PERF_SAMPLE_ID = 1U << 6,
  108. PERF_SAMPLE_CPU = 1U << 7,
  109. PERF_SAMPLE_PERIOD = 1U << 8,
  110. PERF_SAMPLE_STREAM_ID = 1U << 9,
  111. PERF_SAMPLE_RAW = 1U << 10,
  112. PERF_SAMPLE_MAX = 1U << 11, /* non-ABI */
  113. };
  114. /*
  115. * The format of the data returned by read() on a perf event fd,
  116. * as specified by attr.read_format:
  117. *
  118. * struct read_format {
  119. * { u64 value;
  120. * { u64 time_enabled; } && PERF_FORMAT_ENABLED
  121. * { u64 time_running; } && PERF_FORMAT_RUNNING
  122. * { u64 id; } && PERF_FORMAT_ID
  123. * } && !PERF_FORMAT_GROUP
  124. *
  125. * { u64 nr;
  126. * { u64 time_enabled; } && PERF_FORMAT_ENABLED
  127. * { u64 time_running; } && PERF_FORMAT_RUNNING
  128. * { u64 value;
  129. * { u64 id; } && PERF_FORMAT_ID
  130. * } cntr[nr];
  131. * } && PERF_FORMAT_GROUP
  132. * };
  133. */
  134. enum perf_event_read_format {
  135. PERF_FORMAT_TOTAL_TIME_ENABLED = 1U << 0,
  136. PERF_FORMAT_TOTAL_TIME_RUNNING = 1U << 1,
  137. PERF_FORMAT_ID = 1U << 2,
  138. PERF_FORMAT_GROUP = 1U << 3,
  139. PERF_FORMAT_MAX = 1U << 4, /* non-ABI */
  140. };
  141. #define PERF_ATTR_SIZE_VER0 64 /* sizeof first published struct */
  142. /*
  143. * Hardware event_id to monitor via a performance monitoring event:
  144. */
  145. struct perf_event_attr {
  146. /*
  147. * Major type: hardware/software/tracepoint/etc.
  148. */
  149. __u32 type;
  150. /*
  151. * Size of the attr structure, for fwd/bwd compat.
  152. */
  153. __u32 size;
  154. /*
  155. * Type specific configuration information.
  156. */
  157. __u64 config;
  158. union {
  159. __u64 sample_period;
  160. __u64 sample_freq;
  161. };
  162. __u64 sample_type;
  163. __u64 read_format;
  164. __u64 disabled : 1, /* off by default */
  165. inherit : 1, /* children inherit it */
  166. pinned : 1, /* must always be on PMU */
  167. exclusive : 1, /* only group on PMU */
  168. exclude_user : 1, /* don't count user */
  169. exclude_kernel : 1, /* ditto kernel */
  170. exclude_hv : 1, /* ditto hypervisor */
  171. exclude_idle : 1, /* don't count when idle */
  172. mmap : 1, /* include mmap data */
  173. comm : 1, /* include comm data */
  174. freq : 1, /* use freq, not period */
  175. inherit_stat : 1, /* per task counts */
  176. enable_on_exec : 1, /* next exec enables */
  177. task : 1, /* trace fork/exit */
  178. watermark : 1, /* wakeup_watermark */
  179. __reserved_1 : 49;
  180. union {
  181. __u32 wakeup_events; /* wakeup every n events */
  182. __u32 wakeup_watermark; /* bytes before wakeup */
  183. };
  184. __u32 __reserved_2;
  185. __u64 __reserved_3;
  186. };
  187. /*
  188. * Ioctls that can be done on a perf event fd:
  189. */
  190. #define PERF_EVENT_IOC_ENABLE _IO ('$', 0)
  191. #define PERF_EVENT_IOC_DISABLE _IO ('$', 1)
  192. #define PERF_EVENT_IOC_REFRESH _IO ('$', 2)
  193. #define PERF_EVENT_IOC_RESET _IO ('$', 3)
  194. #define PERF_EVENT_IOC_PERIOD _IOW('$', 4, __u64)
  195. #define PERF_EVENT_IOC_SET_OUTPUT _IO ('$', 5)
  196. #define PERF_EVENT_IOC_SET_FILTER _IOW('$', 6, char *)
  197. enum perf_event_ioc_flags {
  198. PERF_IOC_FLAG_GROUP = 1U << 0,
  199. };
  200. /*
  201. * Structure of the page that can be mapped via mmap
  202. */
  203. struct perf_event_mmap_page {
  204. __u32 version; /* version number of this structure */
  205. __u32 compat_version; /* lowest version this is compat with */
  206. /*
  207. * Bits needed to read the hw events in user-space.
  208. *
  209. * u32 seq;
  210. * s64 count;
  211. *
  212. * do {
  213. * seq = pc->lock;
  214. *
  215. * barrier()
  216. * if (pc->index) {
  217. * count = pmc_read(pc->index - 1);
  218. * count += pc->offset;
  219. * } else
  220. * goto regular_read;
  221. *
  222. * barrier();
  223. * } while (pc->lock != seq);
  224. *
  225. * NOTE: for obvious reason this only works on self-monitoring
  226. * processes.
  227. */
  228. __u32 lock; /* seqlock for synchronization */
  229. __u32 index; /* hardware event identifier */
  230. __s64 offset; /* add to hardware event value */
  231. __u64 time_enabled; /* time event active */
  232. __u64 time_running; /* time event on cpu */
  233. /*
  234. * Hole for extension of the self monitor capabilities
  235. */
  236. __u64 __reserved[123]; /* align to 1k */
  237. /*
  238. * Control data for the mmap() data buffer.
  239. *
  240. * User-space reading the @data_head value should issue an rmb(), on
  241. * SMP capable platforms, after reading this value -- see
  242. * perf_event_wakeup().
  243. *
  244. * When the mapping is PROT_WRITE the @data_tail value should be
  245. * written by userspace to reflect the last read data. In this case
  246. * the kernel will not over-write unread data.
  247. */
  248. __u64 data_head; /* head in the data section */
  249. __u64 data_tail; /* user-space written tail */
  250. };
  251. #define PERF_RECORD_MISC_CPUMODE_MASK (3 << 0)
  252. #define PERF_RECORD_MISC_CPUMODE_UNKNOWN (0 << 0)
  253. #define PERF_RECORD_MISC_KERNEL (1 << 0)
  254. #define PERF_RECORD_MISC_USER (2 << 0)
  255. #define PERF_RECORD_MISC_HYPERVISOR (3 << 0)
  256. struct perf_event_header {
  257. __u32 type;
  258. __u16 misc;
  259. __u16 size;
  260. };
  261. enum perf_event_type {
  262. /*
  263. * The MMAP events record the PROT_EXEC mappings so that we can
  264. * correlate userspace IPs to code. They have the following structure:
  265. *
  266. * struct {
  267. * struct perf_event_header header;
  268. *
  269. * u32 pid, tid;
  270. * u64 addr;
  271. * u64 len;
  272. * u64 pgoff;
  273. * char filename[];
  274. * };
  275. */
  276. PERF_RECORD_MMAP = 1,
  277. /*
  278. * struct {
  279. * struct perf_event_header header;
  280. * u64 id;
  281. * u64 lost;
  282. * };
  283. */
  284. PERF_RECORD_LOST = 2,
  285. /*
  286. * struct {
  287. * struct perf_event_header header;
  288. *
  289. * u32 pid, tid;
  290. * char comm[];
  291. * };
  292. */
  293. PERF_RECORD_COMM = 3,
  294. /*
  295. * struct {
  296. * struct perf_event_header header;
  297. * u32 pid, ppid;
  298. * u32 tid, ptid;
  299. * u64 time;
  300. * };
  301. */
  302. PERF_RECORD_EXIT = 4,
  303. /*
  304. * struct {
  305. * struct perf_event_header header;
  306. * u64 time;
  307. * u64 id;
  308. * u64 stream_id;
  309. * };
  310. */
  311. PERF_RECORD_THROTTLE = 5,
  312. PERF_RECORD_UNTHROTTLE = 6,
  313. /*
  314. * struct {
  315. * struct perf_event_header header;
  316. * u32 pid, ppid;
  317. * u32 tid, ptid;
  318. * u64 time;
  319. * };
  320. */
  321. PERF_RECORD_FORK = 7,
  322. /*
  323. * struct {
  324. * struct perf_event_header header;
  325. * u32 pid, tid;
  326. *
  327. * struct read_format values;
  328. * };
  329. */
  330. PERF_RECORD_READ = 8,
  331. /*
  332. * struct {
  333. * struct perf_event_header header;
  334. *
  335. * { u64 ip; } && PERF_SAMPLE_IP
  336. * { u32 pid, tid; } && PERF_SAMPLE_TID
  337. * { u64 time; } && PERF_SAMPLE_TIME
  338. * { u64 addr; } && PERF_SAMPLE_ADDR
  339. * { u64 id; } && PERF_SAMPLE_ID
  340. * { u64 stream_id;} && PERF_SAMPLE_STREAM_ID
  341. * { u32 cpu, res; } && PERF_SAMPLE_CPU
  342. * { u64 period; } && PERF_SAMPLE_PERIOD
  343. *
  344. * { struct read_format values; } && PERF_SAMPLE_READ
  345. *
  346. * { u64 nr,
  347. * u64 ips[nr]; } && PERF_SAMPLE_CALLCHAIN
  348. *
  349. * #
  350. * # The RAW record below is opaque data wrt the ABI
  351. * #
  352. * # That is, the ABI doesn't make any promises wrt to
  353. * # the stability of its content, it may vary depending
  354. * # on event, hardware, kernel version and phase of
  355. * # the moon.
  356. * #
  357. * # In other words, PERF_SAMPLE_RAW contents are not an ABI.
  358. * #
  359. *
  360. * { u32 size;
  361. * char data[size];}&& PERF_SAMPLE_RAW
  362. * };
  363. */
  364. PERF_RECORD_SAMPLE = 9,
  365. PERF_RECORD_MAX, /* non-ABI */
  366. };
  367. enum perf_callchain_context {
  368. PERF_CONTEXT_HV = (__u64)-32,
  369. PERF_CONTEXT_KERNEL = (__u64)-128,
  370. PERF_CONTEXT_USER = (__u64)-512,
  371. PERF_CONTEXT_GUEST = (__u64)-2048,
  372. PERF_CONTEXT_GUEST_KERNEL = (__u64)-2176,
  373. PERF_CONTEXT_GUEST_USER = (__u64)-2560,
  374. PERF_CONTEXT_MAX = (__u64)-4095,
  375. };
  376. #define PERF_FLAG_FD_NO_GROUP (1U << 0)
  377. #define PERF_FLAG_FD_OUTPUT (1U << 1)
  378. #ifdef __KERNEL__
  379. /*
  380. * Kernel-internal data types and definitions:
  381. */
  382. #ifdef CONFIG_PERF_EVENTS
  383. # include <asm/perf_event.h>
  384. #endif
  385. #include <linux/list.h>
  386. #include <linux/mutex.h>
  387. #include <linux/rculist.h>
  388. #include <linux/rcupdate.h>
  389. #include <linux/spinlock.h>
  390. #include <linux/hrtimer.h>
  391. #include <linux/fs.h>
  392. #include <linux/pid_namespace.h>
  393. #include <linux/workqueue.h>
  394. #include <asm/atomic.h>
  395. #define PERF_MAX_STACK_DEPTH 255
  396. struct perf_callchain_entry {
  397. __u64 nr;
  398. __u64 ip[PERF_MAX_STACK_DEPTH];
  399. };
  400. struct perf_raw_record {
  401. u32 size;
  402. void *data;
  403. };
  404. struct task_struct;
  405. /**
  406. * struct hw_perf_event - performance event hardware details:
  407. */
  408. struct hw_perf_event {
  409. #ifdef CONFIG_PERF_EVENTS
  410. union {
  411. struct { /* hardware */
  412. u64 config;
  413. unsigned long config_base;
  414. unsigned long event_base;
  415. int idx;
  416. };
  417. struct { /* software */
  418. s64 remaining;
  419. struct hrtimer hrtimer;
  420. };
  421. };
  422. atomic64_t prev_count;
  423. u64 sample_period;
  424. u64 last_period;
  425. atomic64_t period_left;
  426. u64 interrupts;
  427. u64 freq_count;
  428. u64 freq_interrupts;
  429. u64 freq_stamp;
  430. #endif
  431. };
  432. struct perf_event;
  433. /**
  434. * struct pmu - generic performance monitoring unit
  435. */
  436. struct pmu {
  437. int (*enable) (struct perf_event *event);
  438. void (*disable) (struct perf_event *event);
  439. void (*read) (struct perf_event *event);
  440. void (*unthrottle) (struct perf_event *event);
  441. };
  442. /**
  443. * enum perf_event_active_state - the states of a event
  444. */
  445. enum perf_event_active_state {
  446. PERF_EVENT_STATE_ERROR = -2,
  447. PERF_EVENT_STATE_OFF = -1,
  448. PERF_EVENT_STATE_INACTIVE = 0,
  449. PERF_EVENT_STATE_ACTIVE = 1,
  450. };
  451. struct file;
  452. struct perf_mmap_data {
  453. struct rcu_head rcu_head;
  454. #ifdef CONFIG_PERF_USE_VMALLOC
  455. struct work_struct work;
  456. #endif
  457. int data_order;
  458. int nr_pages; /* nr of data pages */
  459. int writable; /* are we writable */
  460. int nr_locked; /* nr pages mlocked */
  461. atomic_t poll; /* POLL_ for wakeups */
  462. atomic_t events; /* event_id limit */
  463. atomic_long_t head; /* write position */
  464. atomic_long_t done_head; /* completed head */
  465. atomic_t lock; /* concurrent writes */
  466. atomic_t wakeup; /* needs a wakeup */
  467. atomic_t lost; /* nr records lost */
  468. long watermark; /* wakeup watermark */
  469. struct perf_event_mmap_page *user_page;
  470. void *data_pages[0];
  471. };
  472. struct perf_pending_entry {
  473. struct perf_pending_entry *next;
  474. void (*func)(struct perf_pending_entry *);
  475. };
  476. /**
  477. * struct perf_event - performance event kernel representation:
  478. */
  479. struct perf_event {
  480. #ifdef CONFIG_PERF_EVENTS
  481. struct list_head group_entry;
  482. struct list_head event_entry;
  483. struct list_head sibling_list;
  484. int nr_siblings;
  485. struct perf_event *group_leader;
  486. struct perf_event *output;
  487. const struct pmu *pmu;
  488. enum perf_event_active_state state;
  489. atomic64_t count;
  490. /*
  491. * These are the total time in nanoseconds that the event
  492. * has been enabled (i.e. eligible to run, and the task has
  493. * been scheduled in, if this is a per-task event)
  494. * and running (scheduled onto the CPU), respectively.
  495. *
  496. * They are computed from tstamp_enabled, tstamp_running and
  497. * tstamp_stopped when the event is in INACTIVE or ACTIVE state.
  498. */
  499. u64 total_time_enabled;
  500. u64 total_time_running;
  501. /*
  502. * These are timestamps used for computing total_time_enabled
  503. * and total_time_running when the event is in INACTIVE or
  504. * ACTIVE state, measured in nanoseconds from an arbitrary point
  505. * in time.
  506. * tstamp_enabled: the notional time when the event was enabled
  507. * tstamp_running: the notional time when the event was scheduled on
  508. * tstamp_stopped: in INACTIVE state, the notional time when the
  509. * event was scheduled off.
  510. */
  511. u64 tstamp_enabled;
  512. u64 tstamp_running;
  513. u64 tstamp_stopped;
  514. struct perf_event_attr attr;
  515. struct hw_perf_event hw;
  516. struct perf_event_context *ctx;
  517. struct file *filp;
  518. /*
  519. * These accumulate total time (in nanoseconds) that children
  520. * events have been enabled and running, respectively.
  521. */
  522. atomic64_t child_total_time_enabled;
  523. atomic64_t child_total_time_running;
  524. /*
  525. * Protect attach/detach and child_list:
  526. */
  527. struct mutex child_mutex;
  528. struct list_head child_list;
  529. struct perf_event *parent;
  530. int oncpu;
  531. int cpu;
  532. struct list_head owner_entry;
  533. struct task_struct *owner;
  534. /* mmap bits */
  535. struct mutex mmap_mutex;
  536. atomic_t mmap_count;
  537. struct perf_mmap_data *data;
  538. /* poll related */
  539. wait_queue_head_t waitq;
  540. struct fasync_struct *fasync;
  541. /* delayed work for NMIs and such */
  542. int pending_wakeup;
  543. int pending_kill;
  544. int pending_disable;
  545. struct perf_pending_entry pending;
  546. atomic_t event_limit;
  547. void (*destroy)(struct perf_event *);
  548. struct rcu_head rcu_head;
  549. struct pid_namespace *ns;
  550. u64 id;
  551. #ifdef CONFIG_EVENT_PROFILE
  552. struct event_filter *filter;
  553. #endif
  554. #endif /* CONFIG_PERF_EVENTS */
  555. };
  556. /**
  557. * struct perf_event_context - event context structure
  558. *
  559. * Used as a container for task events and CPU events as well:
  560. */
  561. struct perf_event_context {
  562. /*
  563. * Protect the states of the events in the list,
  564. * nr_active, and the list:
  565. */
  566. spinlock_t lock;
  567. /*
  568. * Protect the list of events. Locking either mutex or lock
  569. * is sufficient to ensure the list doesn't change; to change
  570. * the list you need to lock both the mutex and the spinlock.
  571. */
  572. struct mutex mutex;
  573. struct list_head group_list;
  574. struct list_head event_list;
  575. int nr_events;
  576. int nr_active;
  577. int is_active;
  578. int nr_stat;
  579. atomic_t refcount;
  580. struct task_struct *task;
  581. /*
  582. * Context clock, runs when context enabled.
  583. */
  584. u64 time;
  585. u64 timestamp;
  586. /*
  587. * These fields let us detect when two contexts have both
  588. * been cloned (inherited) from a common ancestor.
  589. */
  590. struct perf_event_context *parent_ctx;
  591. u64 parent_gen;
  592. u64 generation;
  593. int pin_count;
  594. struct rcu_head rcu_head;
  595. };
  596. /**
  597. * struct perf_event_cpu_context - per cpu event context structure
  598. */
  599. struct perf_cpu_context {
  600. struct perf_event_context ctx;
  601. struct perf_event_context *task_ctx;
  602. int active_oncpu;
  603. int max_pertask;
  604. int exclusive;
  605. /*
  606. * Recursion avoidance:
  607. *
  608. * task, softirq, irq, nmi context
  609. */
  610. int recursion[4];
  611. };
  612. struct perf_output_handle {
  613. struct perf_event *event;
  614. struct perf_mmap_data *data;
  615. unsigned long head;
  616. unsigned long offset;
  617. int nmi;
  618. int sample;
  619. int locked;
  620. unsigned long flags;
  621. };
  622. #ifdef CONFIG_PERF_EVENTS
  623. /*
  624. * Set by architecture code:
  625. */
  626. extern int perf_max_events;
  627. extern const struct pmu *hw_perf_event_init(struct perf_event *event);
  628. extern void perf_event_task_sched_in(struct task_struct *task, int cpu);
  629. extern void perf_event_task_sched_out(struct task_struct *task,
  630. struct task_struct *next, int cpu);
  631. extern void perf_event_task_tick(struct task_struct *task, int cpu);
  632. extern int perf_event_init_task(struct task_struct *child);
  633. extern void perf_event_exit_task(struct task_struct *child);
  634. extern void perf_event_free_task(struct task_struct *task);
  635. extern void set_perf_event_pending(void);
  636. extern void perf_event_do_pending(void);
  637. extern void perf_event_print_debug(void);
  638. extern void __perf_disable(void);
  639. extern bool __perf_enable(void);
  640. extern void perf_disable(void);
  641. extern void perf_enable(void);
  642. extern int perf_event_task_disable(void);
  643. extern int perf_event_task_enable(void);
  644. extern int hw_perf_group_sched_in(struct perf_event *group_leader,
  645. struct perf_cpu_context *cpuctx,
  646. struct perf_event_context *ctx, int cpu);
  647. extern void perf_event_update_userpage(struct perf_event *event);
  648. struct perf_sample_data {
  649. u64 type;
  650. u64 ip;
  651. struct {
  652. u32 pid;
  653. u32 tid;
  654. } tid_entry;
  655. u64 time;
  656. u64 addr;
  657. u64 id;
  658. u64 stream_id;
  659. struct {
  660. u32 cpu;
  661. u32 reserved;
  662. } cpu_entry;
  663. u64 period;
  664. struct perf_callchain_entry *callchain;
  665. struct perf_raw_record *raw;
  666. };
  667. extern void perf_output_sample(struct perf_output_handle *handle,
  668. struct perf_event_header *header,
  669. struct perf_sample_data *data,
  670. struct perf_event *event);
  671. extern void perf_prepare_sample(struct perf_event_header *header,
  672. struct perf_sample_data *data,
  673. struct perf_event *event,
  674. struct pt_regs *regs);
  675. extern int perf_event_overflow(struct perf_event *event, int nmi,
  676. struct perf_sample_data *data,
  677. struct pt_regs *regs);
  678. /*
  679. * Return 1 for a software event, 0 for a hardware event
  680. */
  681. static inline int is_software_event(struct perf_event *event)
  682. {
  683. return (event->attr.type != PERF_TYPE_RAW) &&
  684. (event->attr.type != PERF_TYPE_HARDWARE) &&
  685. (event->attr.type != PERF_TYPE_HW_CACHE);
  686. }
  687. extern atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
  688. extern void __perf_sw_event(u32, u64, int, struct pt_regs *, u64);
  689. static inline void
  690. perf_sw_event(u32 event_id, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
  691. {
  692. if (atomic_read(&perf_swevent_enabled[event_id]))
  693. __perf_sw_event(event_id, nr, nmi, regs, addr);
  694. }
  695. extern void __perf_event_mmap(struct vm_area_struct *vma);
  696. static inline void perf_event_mmap(struct vm_area_struct *vma)
  697. {
  698. if (vma->vm_flags & VM_EXEC)
  699. __perf_event_mmap(vma);
  700. }
  701. extern void perf_event_comm(struct task_struct *tsk);
  702. extern void perf_event_fork(struct task_struct *tsk);
  703. extern struct perf_callchain_entry *perf_callchain(struct pt_regs *regs);
  704. extern int sysctl_perf_event_paranoid;
  705. extern int sysctl_perf_event_mlock;
  706. extern int sysctl_perf_event_sample_rate;
  707. extern void perf_event_init(void);
  708. extern void perf_tp_event(int event_id, u64 addr, u64 count,
  709. void *record, int entry_size);
  710. #ifndef perf_misc_flags
  711. #define perf_misc_flags(regs) (user_mode(regs) ? PERF_RECORD_MISC_USER : \
  712. PERF_RECORD_MISC_KERNEL)
  713. #define perf_instruction_pointer(regs) instruction_pointer(regs)
  714. #endif
  715. extern int perf_output_begin(struct perf_output_handle *handle,
  716. struct perf_event *event, unsigned int size,
  717. int nmi, int sample);
  718. extern void perf_output_end(struct perf_output_handle *handle);
  719. extern void perf_output_copy(struct perf_output_handle *handle,
  720. const void *buf, unsigned int len);
  721. #else
  722. static inline void
  723. perf_event_task_sched_in(struct task_struct *task, int cpu) { }
  724. static inline void
  725. perf_event_task_sched_out(struct task_struct *task,
  726. struct task_struct *next, int cpu) { }
  727. static inline void
  728. perf_event_task_tick(struct task_struct *task, int cpu) { }
  729. static inline int perf_event_init_task(struct task_struct *child) { return 0; }
  730. static inline void perf_event_exit_task(struct task_struct *child) { }
  731. static inline void perf_event_free_task(struct task_struct *task) { }
  732. static inline void perf_event_do_pending(void) { }
  733. static inline void perf_event_print_debug(void) { }
  734. static inline void perf_disable(void) { }
  735. static inline void perf_enable(void) { }
  736. static inline int perf_event_task_disable(void) { return -EINVAL; }
  737. static inline int perf_event_task_enable(void) { return -EINVAL; }
  738. static inline void
  739. perf_sw_event(u32 event_id, u64 nr, int nmi,
  740. struct pt_regs *regs, u64 addr) { }
  741. static inline void perf_event_mmap(struct vm_area_struct *vma) { }
  742. static inline void perf_event_comm(struct task_struct *tsk) { }
  743. static inline void perf_event_fork(struct task_struct *tsk) { }
  744. static inline void perf_event_init(void) { }
  745. #endif
  746. #define perf_output_put(handle, x) \
  747. perf_output_copy((handle), &(x), sizeof(x))
  748. #endif /* __KERNEL__ */
  749. #endif /* _LINUX_PERF_EVENT_H */