perf_event.h 25 KB

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