perf_event.h 38 KB

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