perf_event.h 27 KB

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