perf_event.h 31 KB

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