perf_event.h 30 KB

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