perf_event.h 31 KB

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