perf_event.h 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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. __reserved_1 : 46;
  193. union {
  194. __u32 wakeup_events; /* wakeup every n events */
  195. __u32 wakeup_watermark; /* bytes before wakeup */
  196. };
  197. __u32 bp_type;
  198. __u64 bp_addr;
  199. __u64 bp_len;
  200. };
  201. /*
  202. * Ioctls that can be done on a perf event fd:
  203. */
  204. #define PERF_EVENT_IOC_ENABLE _IO ('$', 0)
  205. #define PERF_EVENT_IOC_DISABLE _IO ('$', 1)
  206. #define PERF_EVENT_IOC_REFRESH _IO ('$', 2)
  207. #define PERF_EVENT_IOC_RESET _IO ('$', 3)
  208. #define PERF_EVENT_IOC_PERIOD _IOW('$', 4, __u64)
  209. #define PERF_EVENT_IOC_SET_OUTPUT _IO ('$', 5)
  210. #define PERF_EVENT_IOC_SET_FILTER _IOW('$', 6, char *)
  211. enum perf_event_ioc_flags {
  212. PERF_IOC_FLAG_GROUP = 1U << 0,
  213. };
  214. /*
  215. * Structure of the page that can be mapped via mmap
  216. */
  217. struct perf_event_mmap_page {
  218. __u32 version; /* version number of this structure */
  219. __u32 compat_version; /* lowest version this is compat with */
  220. /*
  221. * Bits needed to read the hw events in user-space.
  222. *
  223. * u32 seq;
  224. * s64 count;
  225. *
  226. * do {
  227. * seq = pc->lock;
  228. *
  229. * barrier()
  230. * if (pc->index) {
  231. * count = pmc_read(pc->index - 1);
  232. * count += pc->offset;
  233. * } else
  234. * goto regular_read;
  235. *
  236. * barrier();
  237. * } while (pc->lock != seq);
  238. *
  239. * NOTE: for obvious reason this only works on self-monitoring
  240. * processes.
  241. */
  242. __u32 lock; /* seqlock for synchronization */
  243. __u32 index; /* hardware event identifier */
  244. __s64 offset; /* add to hardware event value */
  245. __u64 time_enabled; /* time event active */
  246. __u64 time_running; /* time event on cpu */
  247. /*
  248. * Hole for extension of the self monitor capabilities
  249. */
  250. __u64 __reserved[123]; /* align to 1k */
  251. /*
  252. * Control data for the mmap() data buffer.
  253. *
  254. * User-space reading the @data_head value should issue an rmb(), on
  255. * SMP capable platforms, after reading this value -- see
  256. * perf_event_wakeup().
  257. *
  258. * When the mapping is PROT_WRITE the @data_tail value should be
  259. * written by userspace to reflect the last read data. In this case
  260. * the kernel will not over-write unread data.
  261. */
  262. __u64 data_head; /* head in the data section */
  263. __u64 data_tail; /* user-space written tail */
  264. };
  265. #define PERF_RECORD_MISC_CPUMODE_MASK (7 << 0)
  266. #define PERF_RECORD_MISC_CPUMODE_UNKNOWN (0 << 0)
  267. #define PERF_RECORD_MISC_KERNEL (1 << 0)
  268. #define PERF_RECORD_MISC_USER (2 << 0)
  269. #define PERF_RECORD_MISC_HYPERVISOR (3 << 0)
  270. #define PERF_RECORD_MISC_GUEST_KERNEL (4 << 0)
  271. #define PERF_RECORD_MISC_GUEST_USER (5 << 0)
  272. /*
  273. * Indicates that the content of PERF_SAMPLE_IP points to
  274. * the actual instruction that triggered the event. See also
  275. * perf_event_attr::precise_ip.
  276. */
  277. #define PERF_RECORD_MISC_EXACT_IP (1 << 14)
  278. /*
  279. * Reserve the last bit to indicate some extended misc field
  280. */
  281. #define PERF_RECORD_MISC_EXT_RESERVED (1 << 15)
  282. struct perf_event_header {
  283. __u32 type;
  284. __u16 misc;
  285. __u16 size;
  286. };
  287. enum perf_event_type {
  288. /*
  289. * The MMAP events record the PROT_EXEC mappings so that we can
  290. * correlate userspace IPs to code. They have the following structure:
  291. *
  292. * struct {
  293. * struct perf_event_header header;
  294. *
  295. * u32 pid, tid;
  296. * u64 addr;
  297. * u64 len;
  298. * u64 pgoff;
  299. * char filename[];
  300. * };
  301. */
  302. PERF_RECORD_MMAP = 1,
  303. /*
  304. * struct {
  305. * struct perf_event_header header;
  306. * u64 id;
  307. * u64 lost;
  308. * };
  309. */
  310. PERF_RECORD_LOST = 2,
  311. /*
  312. * struct {
  313. * struct perf_event_header header;
  314. *
  315. * u32 pid, tid;
  316. * char comm[];
  317. * };
  318. */
  319. PERF_RECORD_COMM = 3,
  320. /*
  321. * struct {
  322. * struct perf_event_header header;
  323. * u32 pid, ppid;
  324. * u32 tid, ptid;
  325. * u64 time;
  326. * };
  327. */
  328. PERF_RECORD_EXIT = 4,
  329. /*
  330. * struct {
  331. * struct perf_event_header header;
  332. * u64 time;
  333. * u64 id;
  334. * u64 stream_id;
  335. * };
  336. */
  337. PERF_RECORD_THROTTLE = 5,
  338. PERF_RECORD_UNTHROTTLE = 6,
  339. /*
  340. * struct {
  341. * struct perf_event_header header;
  342. * u32 pid, ppid;
  343. * u32 tid, ptid;
  344. * u64 time;
  345. * };
  346. */
  347. PERF_RECORD_FORK = 7,
  348. /*
  349. * struct {
  350. * struct perf_event_header header;
  351. * u32 pid, tid;
  352. *
  353. * struct read_format values;
  354. * };
  355. */
  356. PERF_RECORD_READ = 8,
  357. /*
  358. * struct {
  359. * struct perf_event_header header;
  360. *
  361. * { u64 ip; } && PERF_SAMPLE_IP
  362. * { u32 pid, tid; } && PERF_SAMPLE_TID
  363. * { u64 time; } && PERF_SAMPLE_TIME
  364. * { u64 addr; } && PERF_SAMPLE_ADDR
  365. * { u64 id; } && PERF_SAMPLE_ID
  366. * { u64 stream_id;} && PERF_SAMPLE_STREAM_ID
  367. * { u32 cpu, res; } && PERF_SAMPLE_CPU
  368. * { u64 period; } && PERF_SAMPLE_PERIOD
  369. *
  370. * { struct read_format values; } && PERF_SAMPLE_READ
  371. *
  372. * { u64 nr,
  373. * u64 ips[nr]; } && PERF_SAMPLE_CALLCHAIN
  374. *
  375. * #
  376. * # The RAW record below is opaque data wrt the ABI
  377. * #
  378. * # That is, the ABI doesn't make any promises wrt to
  379. * # the stability of its content, it may vary depending
  380. * # on event, hardware, kernel version and phase of
  381. * # the moon.
  382. * #
  383. * # In other words, PERF_SAMPLE_RAW contents are not an ABI.
  384. * #
  385. *
  386. * { u32 size;
  387. * char data[size];}&& PERF_SAMPLE_RAW
  388. * };
  389. */
  390. PERF_RECORD_SAMPLE = 9,
  391. PERF_RECORD_MAX, /* non-ABI */
  392. };
  393. enum perf_callchain_context {
  394. PERF_CONTEXT_HV = (__u64)-32,
  395. PERF_CONTEXT_KERNEL = (__u64)-128,
  396. PERF_CONTEXT_USER = (__u64)-512,
  397. PERF_CONTEXT_GUEST = (__u64)-2048,
  398. PERF_CONTEXT_GUEST_KERNEL = (__u64)-2176,
  399. PERF_CONTEXT_GUEST_USER = (__u64)-2560,
  400. PERF_CONTEXT_MAX = (__u64)-4095,
  401. };
  402. #define PERF_FLAG_FD_NO_GROUP (1U << 0)
  403. #define PERF_FLAG_FD_OUTPUT (1U << 1)
  404. #ifdef __KERNEL__
  405. /*
  406. * Kernel-internal data types and definitions:
  407. */
  408. #ifdef CONFIG_PERF_EVENTS
  409. # include <asm/perf_event.h>
  410. # include <asm/local64.h>
  411. #endif
  412. struct perf_guest_info_callbacks {
  413. int (*is_in_guest) (void);
  414. int (*is_user_mode) (void);
  415. unsigned long (*get_guest_ip) (void);
  416. };
  417. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  418. #include <asm/hw_breakpoint.h>
  419. #endif
  420. #include <linux/list.h>
  421. #include <linux/mutex.h>
  422. #include <linux/rculist.h>
  423. #include <linux/rcupdate.h>
  424. #include <linux/spinlock.h>
  425. #include <linux/hrtimer.h>
  426. #include <linux/fs.h>
  427. #include <linux/pid_namespace.h>
  428. #include <linux/workqueue.h>
  429. #include <linux/ftrace.h>
  430. #include <linux/cpu.h>
  431. #include <asm/atomic.h>
  432. #include <asm/local.h>
  433. #define PERF_MAX_STACK_DEPTH 255
  434. struct perf_callchain_entry {
  435. __u64 nr;
  436. __u64 ip[PERF_MAX_STACK_DEPTH];
  437. };
  438. struct perf_raw_record {
  439. u32 size;
  440. void *data;
  441. };
  442. struct perf_branch_entry {
  443. __u64 from;
  444. __u64 to;
  445. __u64 flags;
  446. };
  447. struct perf_branch_stack {
  448. __u64 nr;
  449. struct perf_branch_entry entries[0];
  450. };
  451. struct task_struct;
  452. /**
  453. * struct hw_perf_event - performance event hardware details:
  454. */
  455. struct hw_perf_event {
  456. #ifdef CONFIG_PERF_EVENTS
  457. union {
  458. struct { /* hardware */
  459. u64 config;
  460. u64 last_tag;
  461. unsigned long config_base;
  462. unsigned long event_base;
  463. int idx;
  464. int last_cpu;
  465. };
  466. struct { /* software */
  467. s64 remaining;
  468. struct hrtimer hrtimer;
  469. };
  470. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  471. struct { /* breakpoint */
  472. struct arch_hw_breakpoint info;
  473. struct list_head bp_list;
  474. };
  475. #endif
  476. };
  477. local64_t prev_count;
  478. u64 sample_period;
  479. u64 last_period;
  480. local64_t period_left;
  481. u64 interrupts;
  482. u64 freq_time_stamp;
  483. u64 freq_count_stamp;
  484. #endif
  485. };
  486. struct perf_event;
  487. /*
  488. * Common implementation detail of pmu::{start,commit,cancel}_txn
  489. */
  490. #define PERF_EVENT_TXN 0x1
  491. /**
  492. * struct pmu - generic performance monitoring unit
  493. */
  494. struct pmu {
  495. int (*enable) (struct perf_event *event);
  496. void (*disable) (struct perf_event *event);
  497. int (*start) (struct perf_event *event);
  498. void (*stop) (struct perf_event *event);
  499. void (*read) (struct perf_event *event);
  500. void (*unthrottle) (struct perf_event *event);
  501. /*
  502. * Group events scheduling is treated as a transaction, add group
  503. * events as a whole and perform one schedulability test. If the test
  504. * fails, roll back the whole group
  505. */
  506. /*
  507. * Start the transaction, after this ->enable() doesn't need
  508. * to do schedulability tests.
  509. */
  510. void (*start_txn) (const struct pmu *pmu);
  511. /*
  512. * If ->start_txn() disabled the ->enable() schedulability test
  513. * then ->commit_txn() is required to perform one. On success
  514. * the transaction is closed. On error the transaction is kept
  515. * open until ->cancel_txn() is called.
  516. */
  517. int (*commit_txn) (const struct pmu *pmu);
  518. /*
  519. * Will cancel the transaction, assumes ->disable() is called for
  520. * each successfull ->enable() during the transaction.
  521. */
  522. void (*cancel_txn) (const struct pmu *pmu);
  523. };
  524. /**
  525. * enum perf_event_active_state - the states of a event
  526. */
  527. enum perf_event_active_state {
  528. PERF_EVENT_STATE_ERROR = -2,
  529. PERF_EVENT_STATE_OFF = -1,
  530. PERF_EVENT_STATE_INACTIVE = 0,
  531. PERF_EVENT_STATE_ACTIVE = 1,
  532. };
  533. struct file;
  534. #define PERF_BUFFER_WRITABLE 0x01
  535. struct perf_buffer {
  536. atomic_t refcount;
  537. struct rcu_head rcu_head;
  538. #ifdef CONFIG_PERF_USE_VMALLOC
  539. struct work_struct work;
  540. int page_order; /* allocation order */
  541. #endif
  542. int nr_pages; /* nr of data pages */
  543. int writable; /* are we writable */
  544. atomic_t poll; /* POLL_ for wakeups */
  545. local_t head; /* write position */
  546. local_t nest; /* nested writers */
  547. local_t events; /* event limit */
  548. local_t wakeup; /* wakeup stamp */
  549. local_t lost; /* nr records lost */
  550. long watermark; /* wakeup watermark */
  551. struct perf_event_mmap_page *user_page;
  552. void *data_pages[0];
  553. };
  554. struct perf_pending_entry {
  555. struct perf_pending_entry *next;
  556. void (*func)(struct perf_pending_entry *);
  557. };
  558. struct perf_sample_data;
  559. typedef void (*perf_overflow_handler_t)(struct perf_event *, int,
  560. struct perf_sample_data *,
  561. struct pt_regs *regs);
  562. enum perf_group_flag {
  563. PERF_GROUP_SOFTWARE = 0x1,
  564. };
  565. #define SWEVENT_HLIST_BITS 8
  566. #define SWEVENT_HLIST_SIZE (1 << SWEVENT_HLIST_BITS)
  567. struct swevent_hlist {
  568. struct hlist_head heads[SWEVENT_HLIST_SIZE];
  569. struct rcu_head rcu_head;
  570. };
  571. #define PERF_ATTACH_CONTEXT 0x01
  572. #define PERF_ATTACH_GROUP 0x02
  573. /**
  574. * struct perf_event - performance event kernel representation:
  575. */
  576. struct perf_event {
  577. #ifdef CONFIG_PERF_EVENTS
  578. struct list_head group_entry;
  579. struct list_head event_entry;
  580. struct list_head sibling_list;
  581. struct hlist_node hlist_entry;
  582. int nr_siblings;
  583. int group_flags;
  584. struct perf_event *group_leader;
  585. const struct pmu *pmu;
  586. enum perf_event_active_state state;
  587. unsigned int attach_state;
  588. local64_t count;
  589. atomic64_t child_count;
  590. /*
  591. * These are the total time in nanoseconds that the event
  592. * has been enabled (i.e. eligible to run, and the task has
  593. * been scheduled in, if this is a per-task event)
  594. * and running (scheduled onto the CPU), respectively.
  595. *
  596. * They are computed from tstamp_enabled, tstamp_running and
  597. * tstamp_stopped when the event is in INACTIVE or ACTIVE state.
  598. */
  599. u64 total_time_enabled;
  600. u64 total_time_running;
  601. /*
  602. * These are timestamps used for computing total_time_enabled
  603. * and total_time_running when the event is in INACTIVE or
  604. * ACTIVE state, measured in nanoseconds from an arbitrary point
  605. * in time.
  606. * tstamp_enabled: the notional time when the event was enabled
  607. * tstamp_running: the notional time when the event was scheduled on
  608. * tstamp_stopped: in INACTIVE state, the notional time when the
  609. * event was scheduled off.
  610. */
  611. u64 tstamp_enabled;
  612. u64 tstamp_running;
  613. u64 tstamp_stopped;
  614. struct perf_event_attr attr;
  615. struct hw_perf_event hw;
  616. struct perf_event_context *ctx;
  617. struct file *filp;
  618. /*
  619. * These accumulate total time (in nanoseconds) that children
  620. * events have been enabled and running, respectively.
  621. */
  622. atomic64_t child_total_time_enabled;
  623. atomic64_t child_total_time_running;
  624. /*
  625. * Protect attach/detach and child_list:
  626. */
  627. struct mutex child_mutex;
  628. struct list_head child_list;
  629. struct perf_event *parent;
  630. int oncpu;
  631. int cpu;
  632. struct list_head owner_entry;
  633. struct task_struct *owner;
  634. /* mmap bits */
  635. struct mutex mmap_mutex;
  636. atomic_t mmap_count;
  637. int mmap_locked;
  638. struct user_struct *mmap_user;
  639. struct perf_buffer *buffer;
  640. /* poll related */
  641. wait_queue_head_t waitq;
  642. struct fasync_struct *fasync;
  643. /* delayed work for NMIs and such */
  644. int pending_wakeup;
  645. int pending_kill;
  646. int pending_disable;
  647. struct perf_pending_entry pending;
  648. atomic_t event_limit;
  649. void (*destroy)(struct perf_event *);
  650. struct rcu_head rcu_head;
  651. struct pid_namespace *ns;
  652. u64 id;
  653. perf_overflow_handler_t overflow_handler;
  654. #ifdef CONFIG_EVENT_TRACING
  655. struct ftrace_event_call *tp_event;
  656. struct event_filter *filter;
  657. #endif
  658. #endif /* CONFIG_PERF_EVENTS */
  659. };
  660. /**
  661. * struct perf_event_context - event context structure
  662. *
  663. * Used as a container for task events and CPU events as well:
  664. */
  665. struct perf_event_context {
  666. /*
  667. * Protect the states of the events in the list,
  668. * nr_active, and the list:
  669. */
  670. raw_spinlock_t lock;
  671. /*
  672. * Protect the list of events. Locking either mutex or lock
  673. * is sufficient to ensure the list doesn't change; to change
  674. * the list you need to lock both the mutex and the spinlock.
  675. */
  676. struct mutex mutex;
  677. struct list_head pinned_groups;
  678. struct list_head flexible_groups;
  679. struct list_head event_list;
  680. int nr_events;
  681. int nr_active;
  682. int is_active;
  683. int nr_stat;
  684. atomic_t refcount;
  685. struct task_struct *task;
  686. /*
  687. * Context clock, runs when context enabled.
  688. */
  689. u64 time;
  690. u64 timestamp;
  691. /*
  692. * These fields let us detect when two contexts have both
  693. * been cloned (inherited) from a common ancestor.
  694. */
  695. struct perf_event_context *parent_ctx;
  696. u64 parent_gen;
  697. u64 generation;
  698. int pin_count;
  699. struct rcu_head rcu_head;
  700. };
  701. /**
  702. * struct perf_event_cpu_context - per cpu event context structure
  703. */
  704. struct perf_cpu_context {
  705. struct perf_event_context ctx;
  706. struct perf_event_context *task_ctx;
  707. int active_oncpu;
  708. int max_pertask;
  709. int exclusive;
  710. struct swevent_hlist *swevent_hlist;
  711. struct mutex hlist_mutex;
  712. int hlist_refcount;
  713. /*
  714. * Recursion avoidance:
  715. *
  716. * task, softirq, irq, nmi context
  717. */
  718. int recursion[4];
  719. };
  720. struct perf_output_handle {
  721. struct perf_event *event;
  722. struct perf_buffer *buffer;
  723. unsigned long wakeup;
  724. unsigned long size;
  725. void *addr;
  726. int page;
  727. int nmi;
  728. int sample;
  729. };
  730. #ifdef CONFIG_PERF_EVENTS
  731. /*
  732. * Set by architecture code:
  733. */
  734. extern int perf_max_events;
  735. extern const struct pmu *hw_perf_event_init(struct perf_event *event);
  736. extern void perf_event_task_sched_in(struct task_struct *task);
  737. extern void perf_event_task_sched_out(struct task_struct *task, struct task_struct *next);
  738. extern void perf_event_task_tick(struct task_struct *task);
  739. extern int perf_event_init_task(struct task_struct *child);
  740. extern void perf_event_exit_task(struct task_struct *child);
  741. extern void perf_event_free_task(struct task_struct *task);
  742. extern void set_perf_event_pending(void);
  743. extern void perf_event_do_pending(void);
  744. extern void perf_event_print_debug(void);
  745. extern void __perf_disable(void);
  746. extern bool __perf_enable(void);
  747. extern void perf_disable(void);
  748. extern void perf_enable(void);
  749. extern int perf_event_task_disable(void);
  750. extern int perf_event_task_enable(void);
  751. extern void perf_event_update_userpage(struct perf_event *event);
  752. extern int perf_event_release_kernel(struct perf_event *event);
  753. extern struct perf_event *
  754. perf_event_create_kernel_counter(struct perf_event_attr *attr,
  755. int cpu,
  756. pid_t pid,
  757. perf_overflow_handler_t callback);
  758. extern u64 perf_event_read_value(struct perf_event *event,
  759. u64 *enabled, u64 *running);
  760. struct perf_sample_data {
  761. u64 type;
  762. u64 ip;
  763. struct {
  764. u32 pid;
  765. u32 tid;
  766. } tid_entry;
  767. u64 time;
  768. u64 addr;
  769. u64 id;
  770. u64 stream_id;
  771. struct {
  772. u32 cpu;
  773. u32 reserved;
  774. } cpu_entry;
  775. u64 period;
  776. struct perf_callchain_entry *callchain;
  777. struct perf_raw_record *raw;
  778. };
  779. static inline
  780. void perf_sample_data_init(struct perf_sample_data *data, u64 addr)
  781. {
  782. data->addr = addr;
  783. data->raw = NULL;
  784. }
  785. extern void perf_output_sample(struct perf_output_handle *handle,
  786. struct perf_event_header *header,
  787. struct perf_sample_data *data,
  788. struct perf_event *event);
  789. extern void perf_prepare_sample(struct perf_event_header *header,
  790. struct perf_sample_data *data,
  791. struct perf_event *event,
  792. struct pt_regs *regs);
  793. extern int perf_event_overflow(struct perf_event *event, int nmi,
  794. struct perf_sample_data *data,
  795. struct pt_regs *regs);
  796. /*
  797. * Return 1 for a software event, 0 for a hardware event
  798. */
  799. static inline int is_software_event(struct perf_event *event)
  800. {
  801. switch (event->attr.type) {
  802. case PERF_TYPE_SOFTWARE:
  803. case PERF_TYPE_TRACEPOINT:
  804. /* for now the breakpoint stuff also works as software event */
  805. case PERF_TYPE_BREAKPOINT:
  806. return 1;
  807. }
  808. return 0;
  809. }
  810. extern atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
  811. extern void __perf_sw_event(u32, u64, int, struct pt_regs *, u64);
  812. #ifndef perf_arch_fetch_caller_regs
  813. static inline void
  814. perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { }
  815. #endif
  816. /*
  817. * Take a snapshot of the regs. Skip ip and frame pointer to
  818. * the nth caller. We only need a few of the regs:
  819. * - ip for PERF_SAMPLE_IP
  820. * - cs for user_mode() tests
  821. * - bp for callchains
  822. * - eflags, for future purposes, just in case
  823. */
  824. static inline void perf_fetch_caller_regs(struct pt_regs *regs)
  825. {
  826. memset(regs, 0, sizeof(*regs));
  827. perf_arch_fetch_caller_regs(regs, CALLER_ADDR0);
  828. }
  829. static inline void
  830. perf_sw_event(u32 event_id, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
  831. {
  832. if (atomic_read(&perf_swevent_enabled[event_id])) {
  833. struct pt_regs hot_regs;
  834. if (!regs) {
  835. perf_fetch_caller_regs(&hot_regs);
  836. regs = &hot_regs;
  837. }
  838. __perf_sw_event(event_id, nr, nmi, regs, addr);
  839. }
  840. }
  841. extern void perf_event_mmap(struct vm_area_struct *vma);
  842. extern struct perf_guest_info_callbacks *perf_guest_cbs;
  843. extern int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
  844. extern int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
  845. extern void perf_event_comm(struct task_struct *tsk);
  846. extern void perf_event_fork(struct task_struct *tsk);
  847. extern struct perf_callchain_entry *perf_callchain(struct pt_regs *regs);
  848. static inline void
  849. perf_callchain_store(struct perf_callchain_entry *entry, u64 ip)
  850. {
  851. if (entry->nr < PERF_MAX_STACK_DEPTH)
  852. entry->ip[entry->nr++] = ip;
  853. }
  854. extern int sysctl_perf_event_paranoid;
  855. extern int sysctl_perf_event_mlock;
  856. extern int sysctl_perf_event_sample_rate;
  857. static inline bool perf_paranoid_tracepoint_raw(void)
  858. {
  859. return sysctl_perf_event_paranoid > -1;
  860. }
  861. static inline bool perf_paranoid_cpu(void)
  862. {
  863. return sysctl_perf_event_paranoid > 0;
  864. }
  865. static inline bool perf_paranoid_kernel(void)
  866. {
  867. return sysctl_perf_event_paranoid > 1;
  868. }
  869. extern void perf_event_init(void);
  870. extern void perf_tp_event(u64 addr, u64 count, void *record,
  871. int entry_size, struct pt_regs *regs,
  872. struct hlist_head *head, int rctx);
  873. extern void perf_bp_event(struct perf_event *event, void *data);
  874. #ifndef perf_misc_flags
  875. #define perf_misc_flags(regs) (user_mode(regs) ? PERF_RECORD_MISC_USER : \
  876. PERF_RECORD_MISC_KERNEL)
  877. #define perf_instruction_pointer(regs) instruction_pointer(regs)
  878. #endif
  879. extern int perf_output_begin(struct perf_output_handle *handle,
  880. struct perf_event *event, unsigned int size,
  881. int nmi, int sample);
  882. extern void perf_output_end(struct perf_output_handle *handle);
  883. extern void perf_output_copy(struct perf_output_handle *handle,
  884. const void *buf, unsigned int len);
  885. extern int perf_swevent_get_recursion_context(void);
  886. extern void perf_swevent_put_recursion_context(int rctx);
  887. extern void perf_event_enable(struct perf_event *event);
  888. extern void perf_event_disable(struct perf_event *event);
  889. #else
  890. static inline void
  891. perf_event_task_sched_in(struct task_struct *task) { }
  892. static inline void
  893. perf_event_task_sched_out(struct task_struct *task,
  894. struct task_struct *next) { }
  895. static inline void
  896. perf_event_task_tick(struct task_struct *task) { }
  897. static inline int perf_event_init_task(struct task_struct *child) { return 0; }
  898. static inline void perf_event_exit_task(struct task_struct *child) { }
  899. static inline void perf_event_free_task(struct task_struct *task) { }
  900. static inline void perf_event_do_pending(void) { }
  901. static inline void perf_event_print_debug(void) { }
  902. static inline void perf_disable(void) { }
  903. static inline void perf_enable(void) { }
  904. static inline int perf_event_task_disable(void) { return -EINVAL; }
  905. static inline int perf_event_task_enable(void) { return -EINVAL; }
  906. static inline void
  907. perf_sw_event(u32 event_id, u64 nr, int nmi,
  908. struct pt_regs *regs, u64 addr) { }
  909. static inline void
  910. perf_bp_event(struct perf_event *event, void *data) { }
  911. static inline int perf_register_guest_info_callbacks
  912. (struct perf_guest_info_callbacks *callbacks) { return 0; }
  913. static inline int perf_unregister_guest_info_callbacks
  914. (struct perf_guest_info_callbacks *callbacks) { return 0; }
  915. static inline void perf_event_mmap(struct vm_area_struct *vma) { }
  916. static inline void perf_event_comm(struct task_struct *tsk) { }
  917. static inline void perf_event_fork(struct task_struct *tsk) { }
  918. static inline void perf_event_init(void) { }
  919. static inline int perf_swevent_get_recursion_context(void) { return -1; }
  920. static inline void perf_swevent_put_recursion_context(int rctx) { }
  921. static inline void perf_event_enable(struct perf_event *event) { }
  922. static inline void perf_event_disable(struct perf_event *event) { }
  923. #endif
  924. #define perf_output_put(handle, x) \
  925. perf_output_copy((handle), &(x), sizeof(x))
  926. /*
  927. * This has to have a higher priority than migration_notifier in sched.c.
  928. */
  929. #define perf_cpu_notifier(fn) \
  930. do { \
  931. static struct notifier_block fn##_nb __cpuinitdata = \
  932. { .notifier_call = fn, .priority = 20 }; \
  933. fn(&fn##_nb, (unsigned long)CPU_UP_PREPARE, \
  934. (void *)(unsigned long)smp_processor_id()); \
  935. fn(&fn##_nb, (unsigned long)CPU_STARTING, \
  936. (void *)(unsigned long)smp_processor_id()); \
  937. fn(&fn##_nb, (unsigned long)CPU_ONLINE, \
  938. (void *)(unsigned long)smp_processor_id()); \
  939. register_cpu_notifier(&fn##_nb); \
  940. } while (0)
  941. #endif /* __KERNEL__ */
  942. #endif /* _LINUX_PERF_EVENT_H */