perf_event.h 37 KB

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