perf_event.h 38 KB

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