perf_event.h 36 KB

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