perf_event.h 22 KB

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