perf_event.h 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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_BREAKPOINT = 5,
  32. PERF_TYPE_MAX, /* non-ABI */
  33. };
  34. /*
  35. * Generalized performance event event_id types, used by the
  36. * attr.event_id parameter of the sys_perf_event_open()
  37. * syscall:
  38. */
  39. enum perf_hw_id {
  40. /*
  41. * Common hardware events, generalized by the kernel:
  42. */
  43. PERF_COUNT_HW_CPU_CYCLES = 0,
  44. PERF_COUNT_HW_INSTRUCTIONS = 1,
  45. PERF_COUNT_HW_CACHE_REFERENCES = 2,
  46. PERF_COUNT_HW_CACHE_MISSES = 3,
  47. PERF_COUNT_HW_BRANCH_INSTRUCTIONS = 4,
  48. PERF_COUNT_HW_BRANCH_MISSES = 5,
  49. PERF_COUNT_HW_BUS_CYCLES = 6,
  50. PERF_COUNT_HW_MAX, /* non-ABI */
  51. };
  52. /*
  53. * Generalized hardware cache events:
  54. *
  55. * { L1-D, L1-I, LLC, ITLB, DTLB, BPU } x
  56. * { read, write, prefetch } x
  57. * { accesses, misses }
  58. */
  59. enum perf_hw_cache_id {
  60. PERF_COUNT_HW_CACHE_L1D = 0,
  61. PERF_COUNT_HW_CACHE_L1I = 1,
  62. PERF_COUNT_HW_CACHE_LL = 2,
  63. PERF_COUNT_HW_CACHE_DTLB = 3,
  64. PERF_COUNT_HW_CACHE_ITLB = 4,
  65. PERF_COUNT_HW_CACHE_BPU = 5,
  66. PERF_COUNT_HW_CACHE_MAX, /* non-ABI */
  67. };
  68. enum perf_hw_cache_op_id {
  69. PERF_COUNT_HW_CACHE_OP_READ = 0,
  70. PERF_COUNT_HW_CACHE_OP_WRITE = 1,
  71. PERF_COUNT_HW_CACHE_OP_PREFETCH = 2,
  72. PERF_COUNT_HW_CACHE_OP_MAX, /* non-ABI */
  73. };
  74. enum perf_hw_cache_op_result_id {
  75. PERF_COUNT_HW_CACHE_RESULT_ACCESS = 0,
  76. PERF_COUNT_HW_CACHE_RESULT_MISS = 1,
  77. PERF_COUNT_HW_CACHE_RESULT_MAX, /* non-ABI */
  78. };
  79. /*
  80. * Special "software" events provided by the kernel, even if the hardware
  81. * does not support performance events. These events measure various
  82. * physical and sw events of the kernel (and allow the profiling of them as
  83. * well):
  84. */
  85. enum perf_sw_ids {
  86. PERF_COUNT_SW_CPU_CLOCK = 0,
  87. PERF_COUNT_SW_TASK_CLOCK = 1,
  88. PERF_COUNT_SW_PAGE_FAULTS = 2,
  89. PERF_COUNT_SW_CONTEXT_SWITCHES = 3,
  90. PERF_COUNT_SW_CPU_MIGRATIONS = 4,
  91. PERF_COUNT_SW_PAGE_FAULTS_MIN = 5,
  92. PERF_COUNT_SW_PAGE_FAULTS_MAJ = 6,
  93. PERF_COUNT_SW_ALIGNMENT_FAULTS = 7,
  94. PERF_COUNT_SW_EMULATION_FAULTS = 8,
  95. PERF_COUNT_SW_MAX, /* non-ABI */
  96. };
  97. /*
  98. * Bits that can be set in attr.sample_type to request information
  99. * in the overflow packets.
  100. */
  101. enum perf_event_sample_format {
  102. PERF_SAMPLE_IP = 1U << 0,
  103. PERF_SAMPLE_TID = 1U << 1,
  104. PERF_SAMPLE_TIME = 1U << 2,
  105. PERF_SAMPLE_ADDR = 1U << 3,
  106. PERF_SAMPLE_READ = 1U << 4,
  107. PERF_SAMPLE_CALLCHAIN = 1U << 5,
  108. PERF_SAMPLE_ID = 1U << 6,
  109. PERF_SAMPLE_CPU = 1U << 7,
  110. PERF_SAMPLE_PERIOD = 1U << 8,
  111. PERF_SAMPLE_STREAM_ID = 1U << 9,
  112. PERF_SAMPLE_RAW = 1U << 10,
  113. PERF_SAMPLE_MAX = 1U << 11, /* non-ABI */
  114. };
  115. /*
  116. * The format of the data returned by read() on a perf event fd,
  117. * as specified by attr.read_format:
  118. *
  119. * struct read_format {
  120. * { u64 value;
  121. * { u64 time_enabled; } && PERF_FORMAT_ENABLED
  122. * { u64 time_running; } && PERF_FORMAT_RUNNING
  123. * { u64 id; } && PERF_FORMAT_ID
  124. * } && !PERF_FORMAT_GROUP
  125. *
  126. * { u64 nr;
  127. * { u64 time_enabled; } && PERF_FORMAT_ENABLED
  128. * { u64 time_running; } && PERF_FORMAT_RUNNING
  129. * { u64 value;
  130. * { u64 id; } && PERF_FORMAT_ID
  131. * } cntr[nr];
  132. * } && PERF_FORMAT_GROUP
  133. * };
  134. */
  135. enum perf_event_read_format {
  136. PERF_FORMAT_TOTAL_TIME_ENABLED = 1U << 0,
  137. PERF_FORMAT_TOTAL_TIME_RUNNING = 1U << 1,
  138. PERF_FORMAT_ID = 1U << 2,
  139. PERF_FORMAT_GROUP = 1U << 3,
  140. PERF_FORMAT_MAX = 1U << 4, /* non-ABI */
  141. };
  142. #define PERF_ATTR_SIZE_VER0 64 /* sizeof first published struct */
  143. /*
  144. * Hardware event_id to monitor via a performance monitoring event:
  145. */
  146. struct perf_event_attr {
  147. /*
  148. * Major type: hardware/software/tracepoint/etc.
  149. */
  150. __u32 type;
  151. /*
  152. * Size of the attr structure, for fwd/bwd compat.
  153. */
  154. __u32 size;
  155. /*
  156. * Type specific configuration information.
  157. */
  158. __u64 config;
  159. union {
  160. __u64 sample_period;
  161. __u64 sample_freq;
  162. };
  163. __u64 sample_type;
  164. __u64 read_format;
  165. __u64 disabled : 1, /* off by default */
  166. inherit : 1, /* children inherit it */
  167. pinned : 1, /* must always be on PMU */
  168. exclusive : 1, /* only group on PMU */
  169. exclude_user : 1, /* don't count user */
  170. exclude_kernel : 1, /* ditto kernel */
  171. exclude_hv : 1, /* ditto hypervisor */
  172. exclude_idle : 1, /* don't count when idle */
  173. mmap : 1, /* include mmap data */
  174. comm : 1, /* include comm data */
  175. freq : 1, /* use freq, not period */
  176. inherit_stat : 1, /* per task counts */
  177. enable_on_exec : 1, /* next exec enables */
  178. task : 1, /* trace fork/exit */
  179. watermark : 1, /* wakeup_watermark */
  180. /*
  181. * precise_ip:
  182. *
  183. * 0 - SAMPLE_IP can have arbitrary skid
  184. * 1 - SAMPLE_IP must have constant skid
  185. * 2 - SAMPLE_IP requested to have 0 skid
  186. * 3 - SAMPLE_IP must have 0 skid
  187. *
  188. * See also PERF_RECORD_MISC_EXACT_IP
  189. */
  190. precise_ip : 2, /* skid constraint */
  191. mmap_data : 1, /* non-exec mmap data */
  192. __reserved_1 : 46;
  193. union {
  194. __u32 wakeup_events; /* wakeup every n events */
  195. __u32 wakeup_watermark; /* bytes before wakeup */
  196. };
  197. __u32 bp_type;
  198. __u64 bp_addr;
  199. __u64 bp_len;
  200. };
  201. /*
  202. * Ioctls that can be done on a perf event fd:
  203. */
  204. #define PERF_EVENT_IOC_ENABLE _IO ('$', 0)
  205. #define PERF_EVENT_IOC_DISABLE _IO ('$', 1)
  206. #define PERF_EVENT_IOC_REFRESH _IO ('$', 2)
  207. #define PERF_EVENT_IOC_RESET _IO ('$', 3)
  208. #define PERF_EVENT_IOC_PERIOD _IOW('$', 4, __u64)
  209. #define PERF_EVENT_IOC_SET_OUTPUT _IO ('$', 5)
  210. #define PERF_EVENT_IOC_SET_FILTER _IOW('$', 6, char *)
  211. enum perf_event_ioc_flags {
  212. PERF_IOC_FLAG_GROUP = 1U << 0,
  213. };
  214. /*
  215. * Structure of the page that can be mapped via mmap
  216. */
  217. struct perf_event_mmap_page {
  218. __u32 version; /* version number of this structure */
  219. __u32 compat_version; /* lowest version this is compat with */
  220. /*
  221. * Bits needed to read the hw events in user-space.
  222. *
  223. * u32 seq;
  224. * s64 count;
  225. *
  226. * do {
  227. * seq = pc->lock;
  228. *
  229. * barrier()
  230. * if (pc->index) {
  231. * count = pmc_read(pc->index - 1);
  232. * count += pc->offset;
  233. * } else
  234. * goto regular_read;
  235. *
  236. * barrier();
  237. * } while (pc->lock != seq);
  238. *
  239. * NOTE: for obvious reason this only works on self-monitoring
  240. * processes.
  241. */
  242. __u32 lock; /* seqlock for synchronization */
  243. __u32 index; /* hardware event identifier */
  244. __s64 offset; /* add to hardware event value */
  245. __u64 time_enabled; /* time event active */
  246. __u64 time_running; /* time event on cpu */
  247. /*
  248. * Hole for extension of the self monitor capabilities
  249. */
  250. __u64 __reserved[123]; /* align to 1k */
  251. /*
  252. * Control data for the mmap() data buffer.
  253. *
  254. * User-space reading the @data_head value should issue an rmb(), on
  255. * SMP capable platforms, after reading this value -- see
  256. * perf_event_wakeup().
  257. *
  258. * When the mapping is PROT_WRITE the @data_tail value should be
  259. * written by userspace to reflect the last read data. In this case
  260. * the kernel will not over-write unread data.
  261. */
  262. __u64 data_head; /* head in the data section */
  263. __u64 data_tail; /* user-space written tail */
  264. };
  265. #define PERF_RECORD_MISC_CPUMODE_MASK (7 << 0)
  266. #define PERF_RECORD_MISC_CPUMODE_UNKNOWN (0 << 0)
  267. #define PERF_RECORD_MISC_KERNEL (1 << 0)
  268. #define PERF_RECORD_MISC_USER (2 << 0)
  269. #define PERF_RECORD_MISC_HYPERVISOR (3 << 0)
  270. #define PERF_RECORD_MISC_GUEST_KERNEL (4 << 0)
  271. #define PERF_RECORD_MISC_GUEST_USER (5 << 0)
  272. /*
  273. * Indicates that the content of PERF_SAMPLE_IP points to
  274. * the actual instruction that triggered the event. See also
  275. * perf_event_attr::precise_ip.
  276. */
  277. #define PERF_RECORD_MISC_EXACT_IP (1 << 14)
  278. /*
  279. * Reserve the last bit to indicate some extended misc field
  280. */
  281. #define PERF_RECORD_MISC_EXT_RESERVED (1 << 15)
  282. struct perf_event_header {
  283. __u32 type;
  284. __u16 misc;
  285. __u16 size;
  286. };
  287. enum perf_event_type {
  288. /*
  289. * The MMAP events record the PROT_EXEC mappings so that we can
  290. * correlate userspace IPs to code. They have the following structure:
  291. *
  292. * struct {
  293. * struct perf_event_header header;
  294. *
  295. * u32 pid, tid;
  296. * u64 addr;
  297. * u64 len;
  298. * u64 pgoff;
  299. * char filename[];
  300. * };
  301. */
  302. PERF_RECORD_MMAP = 1,
  303. /*
  304. * struct {
  305. * struct perf_event_header header;
  306. * u64 id;
  307. * u64 lost;
  308. * };
  309. */
  310. PERF_RECORD_LOST = 2,
  311. /*
  312. * struct {
  313. * struct perf_event_header header;
  314. *
  315. * u32 pid, tid;
  316. * char comm[];
  317. * };
  318. */
  319. PERF_RECORD_COMM = 3,
  320. /*
  321. * struct {
  322. * struct perf_event_header header;
  323. * u32 pid, ppid;
  324. * u32 tid, ptid;
  325. * u64 time;
  326. * };
  327. */
  328. PERF_RECORD_EXIT = 4,
  329. /*
  330. * struct {
  331. * struct perf_event_header header;
  332. * u64 time;
  333. * u64 id;
  334. * u64 stream_id;
  335. * };
  336. */
  337. PERF_RECORD_THROTTLE = 5,
  338. PERF_RECORD_UNTHROTTLE = 6,
  339. /*
  340. * struct {
  341. * struct perf_event_header header;
  342. * u32 pid, ppid;
  343. * u32 tid, ptid;
  344. * u64 time;
  345. * };
  346. */
  347. PERF_RECORD_FORK = 7,
  348. /*
  349. * struct {
  350. * struct perf_event_header header;
  351. * u32 pid, tid;
  352. *
  353. * struct read_format values;
  354. * };
  355. */
  356. PERF_RECORD_READ = 8,
  357. /*
  358. * struct {
  359. * struct perf_event_header header;
  360. *
  361. * { u64 ip; } && PERF_SAMPLE_IP
  362. * { u32 pid, tid; } && PERF_SAMPLE_TID
  363. * { u64 time; } && PERF_SAMPLE_TIME
  364. * { u64 addr; } && PERF_SAMPLE_ADDR
  365. * { u64 id; } && PERF_SAMPLE_ID
  366. * { u64 stream_id;} && PERF_SAMPLE_STREAM_ID
  367. * { u32 cpu, res; } && PERF_SAMPLE_CPU
  368. * { u64 period; } && PERF_SAMPLE_PERIOD
  369. *
  370. * { struct read_format values; } && PERF_SAMPLE_READ
  371. *
  372. * { u64 nr,
  373. * u64 ips[nr]; } && PERF_SAMPLE_CALLCHAIN
  374. *
  375. * #
  376. * # The RAW record below is opaque data wrt the ABI
  377. * #
  378. * # That is, the ABI doesn't make any promises wrt to
  379. * # the stability of its content, it may vary depending
  380. * # on event, hardware, kernel version and phase of
  381. * # the moon.
  382. * #
  383. * # In other words, PERF_SAMPLE_RAW contents are not an ABI.
  384. * #
  385. *
  386. * { u32 size;
  387. * char data[size];}&& PERF_SAMPLE_RAW
  388. * };
  389. */
  390. PERF_RECORD_SAMPLE = 9,
  391. PERF_RECORD_MAX, /* non-ABI */
  392. };
  393. enum perf_callchain_context {
  394. PERF_CONTEXT_HV = (__u64)-32,
  395. PERF_CONTEXT_KERNEL = (__u64)-128,
  396. PERF_CONTEXT_USER = (__u64)-512,
  397. PERF_CONTEXT_GUEST = (__u64)-2048,
  398. PERF_CONTEXT_GUEST_KERNEL = (__u64)-2176,
  399. PERF_CONTEXT_GUEST_USER = (__u64)-2560,
  400. PERF_CONTEXT_MAX = (__u64)-4095,
  401. };
  402. #define PERF_FLAG_FD_NO_GROUP (1U << 0)
  403. #define PERF_FLAG_FD_OUTPUT (1U << 1)
  404. #ifdef __KERNEL__
  405. /*
  406. * Kernel-internal data types and definitions:
  407. */
  408. #ifdef CONFIG_PERF_EVENTS
  409. # include <asm/perf_event.h>
  410. # include <asm/local64.h>
  411. #endif
  412. struct perf_guest_info_callbacks {
  413. int (*is_in_guest) (void);
  414. int (*is_user_mode) (void);
  415. unsigned long (*get_guest_ip) (void);
  416. };
  417. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  418. #include <asm/hw_breakpoint.h>
  419. #endif
  420. #include <linux/list.h>
  421. #include <linux/mutex.h>
  422. #include <linux/rculist.h>
  423. #include <linux/rcupdate.h>
  424. #include <linux/spinlock.h>
  425. #include <linux/hrtimer.h>
  426. #include <linux/fs.h>
  427. #include <linux/pid_namespace.h>
  428. #include <linux/workqueue.h>
  429. #include <linux/ftrace.h>
  430. #include <linux/cpu.h>
  431. #include <linux/irq_work.h>
  432. #include <linux/jump_label_ref.h>
  433. #include <asm/atomic.h>
  434. #include <asm/local.h>
  435. #define PERF_MAX_STACK_DEPTH 255
  436. struct perf_callchain_entry {
  437. __u64 nr;
  438. __u64 ip[PERF_MAX_STACK_DEPTH];
  439. };
  440. struct perf_raw_record {
  441. u32 size;
  442. void *data;
  443. };
  444. struct perf_branch_entry {
  445. __u64 from;
  446. __u64 to;
  447. __u64 flags;
  448. };
  449. struct perf_branch_stack {
  450. __u64 nr;
  451. struct perf_branch_entry entries[0];
  452. };
  453. struct task_struct;
  454. /**
  455. * struct hw_perf_event - performance event hardware details:
  456. */
  457. struct hw_perf_event {
  458. #ifdef CONFIG_PERF_EVENTS
  459. union {
  460. struct { /* hardware */
  461. u64 config;
  462. u64 last_tag;
  463. unsigned long config_base;
  464. unsigned long event_base;
  465. int idx;
  466. int last_cpu;
  467. };
  468. struct { /* software */
  469. struct hrtimer hrtimer;
  470. };
  471. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  472. struct { /* breakpoint */
  473. struct arch_hw_breakpoint info;
  474. struct list_head bp_list;
  475. /*
  476. * Crufty hack to avoid the chicken and egg
  477. * problem hw_breakpoint has with context
  478. * creation and event initalization.
  479. */
  480. struct task_struct *bp_target;
  481. };
  482. #endif
  483. };
  484. int state;
  485. local64_t prev_count;
  486. u64 sample_period;
  487. u64 last_period;
  488. local64_t period_left;
  489. u64 interrupts;
  490. u64 freq_time_stamp;
  491. u64 freq_count_stamp;
  492. #endif
  493. };
  494. /*
  495. * hw_perf_event::state flags
  496. */
  497. #define PERF_HES_STOPPED 0x01 /* the counter is stopped */
  498. #define PERF_HES_UPTODATE 0x02 /* event->count up-to-date */
  499. #define PERF_HES_ARCH 0x04
  500. struct perf_event;
  501. /*
  502. * Common implementation detail of pmu::{start,commit,cancel}_txn
  503. */
  504. #define PERF_EVENT_TXN 0x1
  505. /**
  506. * struct pmu - generic performance monitoring unit
  507. */
  508. struct pmu {
  509. struct list_head entry;
  510. int * __percpu pmu_disable_count;
  511. struct perf_cpu_context * __percpu pmu_cpu_context;
  512. int task_ctx_nr;
  513. /*
  514. * Fully disable/enable this PMU, can be used to protect from the PMI
  515. * as well as for lazy/batch writing of the MSRs.
  516. */
  517. void (*pmu_enable) (struct pmu *pmu); /* optional */
  518. void (*pmu_disable) (struct pmu *pmu); /* optional */
  519. /*
  520. * Try and initialize the event for this PMU.
  521. * Should return -ENOENT when the @event doesn't match this PMU.
  522. */
  523. int (*event_init) (struct perf_event *event);
  524. #define PERF_EF_START 0x01 /* start the counter when adding */
  525. #define PERF_EF_RELOAD 0x02 /* reload the counter when starting */
  526. #define PERF_EF_UPDATE 0x04 /* update the counter when stopping */
  527. /*
  528. * Adds/Removes a counter to/from the PMU, can be done inside
  529. * a transaction, see the ->*_txn() methods.
  530. */
  531. int (*add) (struct perf_event *event, int flags);
  532. void (*del) (struct perf_event *event, int flags);
  533. /*
  534. * Starts/Stops a counter present on the PMU. The PMI handler
  535. * should stop the counter when perf_event_overflow() returns
  536. * !0. ->start() will be used to continue.
  537. */
  538. void (*start) (struct perf_event *event, int flags);
  539. void (*stop) (struct perf_event *event, int flags);
  540. /*
  541. * Updates the counter value of the event.
  542. */
  543. void (*read) (struct perf_event *event);
  544. /*
  545. * Group events scheduling is treated as a transaction, add
  546. * group events as a whole and perform one schedulability test.
  547. * If the test fails, roll back the whole group
  548. *
  549. * Start the transaction, after this ->add() doesn't need to
  550. * do schedulability tests.
  551. */
  552. void (*start_txn) (struct pmu *pmu); /* optional */
  553. /*
  554. * If ->start_txn() disabled the ->add() schedulability test
  555. * then ->commit_txn() is required to perform one. On success
  556. * the transaction is closed. On error the transaction is kept
  557. * open until ->cancel_txn() is called.
  558. */
  559. int (*commit_txn) (struct pmu *pmu); /* optional */
  560. /*
  561. * Will cancel the transaction, assumes ->del() is called
  562. * for each successfull ->add() during the transaction.
  563. */
  564. void (*cancel_txn) (struct pmu *pmu); /* optional */
  565. };
  566. /**
  567. * enum perf_event_active_state - the states of a event
  568. */
  569. enum perf_event_active_state {
  570. PERF_EVENT_STATE_ERROR = -2,
  571. PERF_EVENT_STATE_OFF = -1,
  572. PERF_EVENT_STATE_INACTIVE = 0,
  573. PERF_EVENT_STATE_ACTIVE = 1,
  574. };
  575. struct file;
  576. #define PERF_BUFFER_WRITABLE 0x01
  577. struct perf_buffer {
  578. atomic_t refcount;
  579. struct rcu_head rcu_head;
  580. #ifdef CONFIG_PERF_USE_VMALLOC
  581. struct work_struct work;
  582. int page_order; /* allocation order */
  583. #endif
  584. int nr_pages; /* nr of data pages */
  585. int writable; /* are we writable */
  586. atomic_t poll; /* POLL_ for wakeups */
  587. local_t head; /* write position */
  588. local_t nest; /* nested writers */
  589. local_t events; /* event limit */
  590. local_t wakeup; /* wakeup stamp */
  591. local_t lost; /* nr records lost */
  592. long watermark; /* wakeup watermark */
  593. struct perf_event_mmap_page *user_page;
  594. void *data_pages[0];
  595. };
  596. struct perf_sample_data;
  597. typedef void (*perf_overflow_handler_t)(struct perf_event *, int,
  598. struct perf_sample_data *,
  599. struct pt_regs *regs);
  600. enum perf_group_flag {
  601. PERF_GROUP_SOFTWARE = 0x1,
  602. };
  603. #define SWEVENT_HLIST_BITS 8
  604. #define SWEVENT_HLIST_SIZE (1 << SWEVENT_HLIST_BITS)
  605. struct swevent_hlist {
  606. struct hlist_head heads[SWEVENT_HLIST_SIZE];
  607. struct rcu_head rcu_head;
  608. };
  609. #define PERF_ATTACH_CONTEXT 0x01
  610. #define PERF_ATTACH_GROUP 0x02
  611. #define PERF_ATTACH_TASK 0x04
  612. /**
  613. * struct perf_event - performance event kernel representation:
  614. */
  615. struct perf_event {
  616. #ifdef CONFIG_PERF_EVENTS
  617. struct list_head group_entry;
  618. struct list_head event_entry;
  619. struct list_head sibling_list;
  620. struct hlist_node hlist_entry;
  621. int nr_siblings;
  622. int group_flags;
  623. struct perf_event *group_leader;
  624. struct pmu *pmu;
  625. enum perf_event_active_state state;
  626. unsigned int attach_state;
  627. local64_t count;
  628. atomic64_t child_count;
  629. /*
  630. * These are the total time in nanoseconds that the event
  631. * has been enabled (i.e. eligible to run, and the task has
  632. * been scheduled in, if this is a per-task event)
  633. * and running (scheduled onto the CPU), respectively.
  634. *
  635. * They are computed from tstamp_enabled, tstamp_running and
  636. * tstamp_stopped when the event is in INACTIVE or ACTIVE state.
  637. */
  638. u64 total_time_enabled;
  639. u64 total_time_running;
  640. /*
  641. * These are timestamps used for computing total_time_enabled
  642. * and total_time_running when the event is in INACTIVE or
  643. * ACTIVE state, measured in nanoseconds from an arbitrary point
  644. * in time.
  645. * tstamp_enabled: the notional time when the event was enabled
  646. * tstamp_running: the notional time when the event was scheduled on
  647. * tstamp_stopped: in INACTIVE state, the notional time when the
  648. * event was scheduled off.
  649. */
  650. u64 tstamp_enabled;
  651. u64 tstamp_running;
  652. u64 tstamp_stopped;
  653. /*
  654. * timestamp shadows the actual context timing but it can
  655. * be safely used in NMI interrupt context. It reflects the
  656. * context time as it was when the event was last scheduled in.
  657. *
  658. * ctx_time already accounts for ctx->timestamp. Therefore to
  659. * compute ctx_time for a sample, simply add perf_clock().
  660. */
  661. u64 shadow_ctx_time;
  662. struct perf_event_attr attr;
  663. struct hw_perf_event hw;
  664. struct perf_event_context *ctx;
  665. struct file *filp;
  666. /*
  667. * These accumulate total time (in nanoseconds) that children
  668. * events have been enabled and running, respectively.
  669. */
  670. atomic64_t child_total_time_enabled;
  671. atomic64_t child_total_time_running;
  672. /*
  673. * Protect attach/detach and child_list:
  674. */
  675. struct mutex child_mutex;
  676. struct list_head child_list;
  677. struct perf_event *parent;
  678. int oncpu;
  679. int cpu;
  680. struct list_head owner_entry;
  681. struct task_struct *owner;
  682. /* mmap bits */
  683. struct mutex mmap_mutex;
  684. atomic_t mmap_count;
  685. int mmap_locked;
  686. struct user_struct *mmap_user;
  687. struct perf_buffer *buffer;
  688. /* poll related */
  689. wait_queue_head_t waitq;
  690. struct fasync_struct *fasync;
  691. /* delayed work for NMIs and such */
  692. int pending_wakeup;
  693. int pending_kill;
  694. int pending_disable;
  695. struct irq_work pending;
  696. atomic_t event_limit;
  697. void (*destroy)(struct perf_event *);
  698. struct rcu_head rcu_head;
  699. struct pid_namespace *ns;
  700. u64 id;
  701. perf_overflow_handler_t overflow_handler;
  702. #ifdef CONFIG_EVENT_TRACING
  703. struct ftrace_event_call *tp_event;
  704. struct event_filter *filter;
  705. #endif
  706. #endif /* CONFIG_PERF_EVENTS */
  707. };
  708. enum perf_event_context_type {
  709. task_context,
  710. cpu_context,
  711. };
  712. /**
  713. * struct perf_event_context - event context structure
  714. *
  715. * Used as a container for task events and CPU events as well:
  716. */
  717. struct perf_event_context {
  718. enum perf_event_context_type type;
  719. struct pmu *pmu;
  720. /*
  721. * Protect the states of the events in the list,
  722. * nr_active, and the list:
  723. */
  724. raw_spinlock_t lock;
  725. /*
  726. * Protect the list of events. Locking either mutex or lock
  727. * is sufficient to ensure the list doesn't change; to change
  728. * the list you need to lock both the mutex and the spinlock.
  729. */
  730. struct mutex mutex;
  731. struct list_head pinned_groups;
  732. struct list_head flexible_groups;
  733. struct list_head event_list;
  734. int nr_events;
  735. int nr_active;
  736. int is_active;
  737. int nr_stat;
  738. int rotate_disable;
  739. atomic_t refcount;
  740. struct task_struct *task;
  741. /*
  742. * Context clock, runs when context enabled.
  743. */
  744. u64 time;
  745. u64 timestamp;
  746. /*
  747. * These fields let us detect when two contexts have both
  748. * been cloned (inherited) from a common ancestor.
  749. */
  750. struct perf_event_context *parent_ctx;
  751. u64 parent_gen;
  752. u64 generation;
  753. int pin_count;
  754. struct rcu_head rcu_head;
  755. };
  756. /*
  757. * Number of contexts where an event can trigger:
  758. * task, softirq, hardirq, nmi.
  759. */
  760. #define PERF_NR_CONTEXTS 4
  761. /**
  762. * struct perf_event_cpu_context - per cpu event context structure
  763. */
  764. struct perf_cpu_context {
  765. struct perf_event_context ctx;
  766. struct perf_event_context *task_ctx;
  767. int active_oncpu;
  768. int exclusive;
  769. struct list_head rotation_list;
  770. int jiffies_interval;
  771. };
  772. struct perf_output_handle {
  773. struct perf_event *event;
  774. struct perf_buffer *buffer;
  775. unsigned long wakeup;
  776. unsigned long size;
  777. void *addr;
  778. int page;
  779. int nmi;
  780. int sample;
  781. };
  782. #ifdef CONFIG_PERF_EVENTS
  783. extern int perf_pmu_register(struct pmu *pmu);
  784. extern void perf_pmu_unregister(struct pmu *pmu);
  785. extern int perf_num_counters(void);
  786. extern const char *perf_pmu_name(void);
  787. extern void __perf_event_task_sched_in(struct task_struct *task);
  788. extern void __perf_event_task_sched_out(struct task_struct *task, struct task_struct *next);
  789. extern int perf_event_init_task(struct task_struct *child);
  790. extern void perf_event_exit_task(struct task_struct *child);
  791. extern void perf_event_free_task(struct task_struct *task);
  792. extern void perf_event_delayed_put(struct task_struct *task);
  793. extern void perf_event_print_debug(void);
  794. extern void perf_pmu_disable(struct pmu *pmu);
  795. extern void perf_pmu_enable(struct pmu *pmu);
  796. extern int perf_event_task_disable(void);
  797. extern int perf_event_task_enable(void);
  798. extern void perf_event_update_userpage(struct perf_event *event);
  799. extern int perf_event_release_kernel(struct perf_event *event);
  800. extern struct perf_event *
  801. perf_event_create_kernel_counter(struct perf_event_attr *attr,
  802. int cpu,
  803. struct task_struct *task,
  804. perf_overflow_handler_t callback);
  805. extern u64 perf_event_read_value(struct perf_event *event,
  806. u64 *enabled, u64 *running);
  807. struct perf_sample_data {
  808. u64 type;
  809. u64 ip;
  810. struct {
  811. u32 pid;
  812. u32 tid;
  813. } tid_entry;
  814. u64 time;
  815. u64 addr;
  816. u64 id;
  817. u64 stream_id;
  818. struct {
  819. u32 cpu;
  820. u32 reserved;
  821. } cpu_entry;
  822. u64 period;
  823. struct perf_callchain_entry *callchain;
  824. struct perf_raw_record *raw;
  825. };
  826. static inline
  827. void perf_sample_data_init(struct perf_sample_data *data, u64 addr)
  828. {
  829. data->addr = addr;
  830. data->raw = NULL;
  831. }
  832. extern void perf_output_sample(struct perf_output_handle *handle,
  833. struct perf_event_header *header,
  834. struct perf_sample_data *data,
  835. struct perf_event *event);
  836. extern void perf_prepare_sample(struct perf_event_header *header,
  837. struct perf_sample_data *data,
  838. struct perf_event *event,
  839. struct pt_regs *regs);
  840. extern int perf_event_overflow(struct perf_event *event, int nmi,
  841. struct perf_sample_data *data,
  842. struct pt_regs *regs);
  843. static inline bool is_sampling_event(struct perf_event *event)
  844. {
  845. return event->attr.sample_period != 0;
  846. }
  847. /*
  848. * Return 1 for a software event, 0 for a hardware event
  849. */
  850. static inline int is_software_event(struct perf_event *event)
  851. {
  852. return event->pmu->task_ctx_nr == perf_sw_context;
  853. }
  854. extern atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
  855. extern void __perf_sw_event(u32, u64, int, struct pt_regs *, u64);
  856. #ifndef perf_arch_fetch_caller_regs
  857. static inline void
  858. perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { }
  859. #endif
  860. /*
  861. * Take a snapshot of the regs. Skip ip and frame pointer to
  862. * the nth caller. We only need a few of the regs:
  863. * - ip for PERF_SAMPLE_IP
  864. * - cs for user_mode() tests
  865. * - bp for callchains
  866. * - eflags, for future purposes, just in case
  867. */
  868. static inline void perf_fetch_caller_regs(struct pt_regs *regs)
  869. {
  870. memset(regs, 0, sizeof(*regs));
  871. perf_arch_fetch_caller_regs(regs, CALLER_ADDR0);
  872. }
  873. static __always_inline void
  874. perf_sw_event(u32 event_id, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
  875. {
  876. struct pt_regs hot_regs;
  877. JUMP_LABEL(&perf_swevent_enabled[event_id], have_event);
  878. return;
  879. have_event:
  880. if (!regs) {
  881. perf_fetch_caller_regs(&hot_regs);
  882. regs = &hot_regs;
  883. }
  884. __perf_sw_event(event_id, nr, nmi, regs, addr);
  885. }
  886. extern atomic_t perf_task_events;
  887. static inline void perf_event_task_sched_in(struct task_struct *task)
  888. {
  889. COND_STMT(&perf_task_events, __perf_event_task_sched_in(task));
  890. }
  891. static inline
  892. void perf_event_task_sched_out(struct task_struct *task, struct task_struct *next)
  893. {
  894. perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
  895. COND_STMT(&perf_task_events, __perf_event_task_sched_out(task, next));
  896. }
  897. extern void perf_event_mmap(struct vm_area_struct *vma);
  898. extern struct perf_guest_info_callbacks *perf_guest_cbs;
  899. extern int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
  900. extern int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
  901. extern void perf_event_comm(struct task_struct *tsk);
  902. extern void perf_event_fork(struct task_struct *tsk);
  903. /* Callchains */
  904. DECLARE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry);
  905. extern void perf_callchain_user(struct perf_callchain_entry *entry,
  906. struct pt_regs *regs);
  907. extern void perf_callchain_kernel(struct perf_callchain_entry *entry,
  908. struct pt_regs *regs);
  909. static inline void
  910. perf_callchain_store(struct perf_callchain_entry *entry, u64 ip)
  911. {
  912. if (entry->nr < PERF_MAX_STACK_DEPTH)
  913. entry->ip[entry->nr++] = ip;
  914. }
  915. extern int sysctl_perf_event_paranoid;
  916. extern int sysctl_perf_event_mlock;
  917. extern int sysctl_perf_event_sample_rate;
  918. static inline bool perf_paranoid_tracepoint_raw(void)
  919. {
  920. return sysctl_perf_event_paranoid > -1;
  921. }
  922. static inline bool perf_paranoid_cpu(void)
  923. {
  924. return sysctl_perf_event_paranoid > 0;
  925. }
  926. static inline bool perf_paranoid_kernel(void)
  927. {
  928. return sysctl_perf_event_paranoid > 1;
  929. }
  930. extern void perf_event_init(void);
  931. extern void perf_tp_event(u64 addr, u64 count, void *record,
  932. int entry_size, struct pt_regs *regs,
  933. struct hlist_head *head, int rctx);
  934. extern void perf_bp_event(struct perf_event *event, void *data);
  935. #ifndef perf_misc_flags
  936. #define perf_misc_flags(regs) (user_mode(regs) ? PERF_RECORD_MISC_USER : \
  937. PERF_RECORD_MISC_KERNEL)
  938. #define perf_instruction_pointer(regs) instruction_pointer(regs)
  939. #endif
  940. extern int perf_output_begin(struct perf_output_handle *handle,
  941. struct perf_event *event, unsigned int size,
  942. int nmi, int sample);
  943. extern void perf_output_end(struct perf_output_handle *handle);
  944. extern void perf_output_copy(struct perf_output_handle *handle,
  945. const void *buf, unsigned int len);
  946. extern int perf_swevent_get_recursion_context(void);
  947. extern void perf_swevent_put_recursion_context(int rctx);
  948. extern void perf_event_enable(struct perf_event *event);
  949. extern void perf_event_disable(struct perf_event *event);
  950. extern void perf_event_task_tick(void);
  951. #else
  952. static inline void
  953. perf_event_task_sched_in(struct task_struct *task) { }
  954. static inline void
  955. perf_event_task_sched_out(struct task_struct *task,
  956. struct task_struct *next) { }
  957. static inline int perf_event_init_task(struct task_struct *child) { return 0; }
  958. static inline void perf_event_exit_task(struct task_struct *child) { }
  959. static inline void perf_event_free_task(struct task_struct *task) { }
  960. static inline void perf_event_delayed_put(struct task_struct *task) { }
  961. static inline void perf_event_print_debug(void) { }
  962. static inline int perf_event_task_disable(void) { return -EINVAL; }
  963. static inline int perf_event_task_enable(void) { return -EINVAL; }
  964. static inline void
  965. perf_sw_event(u32 event_id, u64 nr, int nmi,
  966. struct pt_regs *regs, u64 addr) { }
  967. static inline void
  968. perf_bp_event(struct perf_event *event, void *data) { }
  969. static inline int perf_register_guest_info_callbacks
  970. (struct perf_guest_info_callbacks *callbacks) { return 0; }
  971. static inline int perf_unregister_guest_info_callbacks
  972. (struct perf_guest_info_callbacks *callbacks) { return 0; }
  973. static inline void perf_event_mmap(struct vm_area_struct *vma) { }
  974. static inline void perf_event_comm(struct task_struct *tsk) { }
  975. static inline void perf_event_fork(struct task_struct *tsk) { }
  976. static inline void perf_event_init(void) { }
  977. static inline int perf_swevent_get_recursion_context(void) { return -1; }
  978. static inline void perf_swevent_put_recursion_context(int rctx) { }
  979. static inline void perf_event_enable(struct perf_event *event) { }
  980. static inline void perf_event_disable(struct perf_event *event) { }
  981. static inline void perf_event_task_tick(void) { }
  982. #endif
  983. #define perf_output_put(handle, x) \
  984. perf_output_copy((handle), &(x), sizeof(x))
  985. /*
  986. * This has to have a higher priority than migration_notifier in sched.c.
  987. */
  988. #define perf_cpu_notifier(fn) \
  989. do { \
  990. static struct notifier_block fn##_nb __cpuinitdata = \
  991. { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
  992. fn(&fn##_nb, (unsigned long)CPU_UP_PREPARE, \
  993. (void *)(unsigned long)smp_processor_id()); \
  994. fn(&fn##_nb, (unsigned long)CPU_STARTING, \
  995. (void *)(unsigned long)smp_processor_id()); \
  996. fn(&fn##_nb, (unsigned long)CPU_ONLINE, \
  997. (void *)(unsigned long)smp_processor_id()); \
  998. register_cpu_notifier(&fn##_nb); \
  999. } while (0)
  1000. #endif /* __KERNEL__ */
  1001. #endif /* _LINUX_PERF_EVENT_H */