perf_event.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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. struct hrtimer hrtimer;
  468. };
  469. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  470. struct { /* breakpoint */
  471. struct arch_hw_breakpoint info;
  472. struct list_head bp_list;
  473. };
  474. #endif
  475. };
  476. int state;
  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. /*
  487. * hw_perf_event::state flags
  488. */
  489. #define PERF_HES_STOPPED 0x01 /* the counter is stopped */
  490. #define PERF_HES_UPTODATE 0x02 /* event->count up-to-date */
  491. #define PERF_HES_ARCH 0x04
  492. struct perf_event;
  493. /*
  494. * Common implementation detail of pmu::{start,commit,cancel}_txn
  495. */
  496. #define PERF_EVENT_TXN 0x1
  497. /**
  498. * struct pmu - generic performance monitoring unit
  499. */
  500. struct pmu {
  501. struct list_head entry;
  502. int * __percpu pmu_disable_count;
  503. struct perf_cpu_context * __percpu pmu_cpu_context;
  504. /*
  505. * Fully disable/enable this PMU, can be used to protect from the PMI
  506. * as well as for lazy/batch writing of the MSRs.
  507. */
  508. void (*pmu_enable) (struct pmu *pmu); /* optional */
  509. void (*pmu_disable) (struct pmu *pmu); /* optional */
  510. /*
  511. * Try and initialize the event for this PMU.
  512. * Should return -ENOENT when the @event doesn't match this PMU.
  513. */
  514. int (*event_init) (struct perf_event *event);
  515. #define PERF_EF_START 0x01 /* start the counter when adding */
  516. #define PERF_EF_RELOAD 0x02 /* reload the counter when starting */
  517. #define PERF_EF_UPDATE 0x04 /* update the counter when stopping */
  518. /*
  519. * Adds/Removes a counter to/from the PMU, can be done inside
  520. * a transaction, see the ->*_txn() methods.
  521. */
  522. int (*add) (struct perf_event *event, int flags);
  523. void (*del) (struct perf_event *event, int flags);
  524. /*
  525. * Starts/Stops a counter present on the PMU. The PMI handler
  526. * should stop the counter when perf_event_overflow() returns
  527. * !0. ->start() will be used to continue.
  528. */
  529. void (*start) (struct perf_event *event, int flags);
  530. void (*stop) (struct perf_event *event, int flags);
  531. /*
  532. * Updates the counter value of the event.
  533. */
  534. void (*read) (struct perf_event *event);
  535. /*
  536. * Group events scheduling is treated as a transaction, add
  537. * group events as a whole and perform one schedulability test.
  538. * If the test fails, roll back the whole group
  539. *
  540. * Start the transaction, after this ->add() doesn't need to
  541. * do schedulability tests.
  542. */
  543. void (*start_txn) (struct pmu *pmu); /* optional */
  544. /*
  545. * If ->start_txn() disabled the ->add() schedulability test
  546. * then ->commit_txn() is required to perform one. On success
  547. * the transaction is closed. On error the transaction is kept
  548. * open until ->cancel_txn() is called.
  549. */
  550. int (*commit_txn) (struct pmu *pmu); /* optional */
  551. /*
  552. * Will cancel the transaction, assumes ->del() is called
  553. * for each successfull ->add() during the transaction.
  554. */
  555. void (*cancel_txn) (struct pmu *pmu); /* optional */
  556. };
  557. /**
  558. * enum perf_event_active_state - the states of a event
  559. */
  560. enum perf_event_active_state {
  561. PERF_EVENT_STATE_ERROR = -2,
  562. PERF_EVENT_STATE_OFF = -1,
  563. PERF_EVENT_STATE_INACTIVE = 0,
  564. PERF_EVENT_STATE_ACTIVE = 1,
  565. };
  566. struct file;
  567. #define PERF_BUFFER_WRITABLE 0x01
  568. struct perf_buffer {
  569. atomic_t refcount;
  570. struct rcu_head rcu_head;
  571. #ifdef CONFIG_PERF_USE_VMALLOC
  572. struct work_struct work;
  573. int page_order; /* allocation order */
  574. #endif
  575. int nr_pages; /* nr of data pages */
  576. int writable; /* are we writable */
  577. atomic_t poll; /* POLL_ for wakeups */
  578. local_t head; /* write position */
  579. local_t nest; /* nested writers */
  580. local_t events; /* event limit */
  581. local_t wakeup; /* wakeup stamp */
  582. local_t lost; /* nr records lost */
  583. long watermark; /* wakeup watermark */
  584. struct perf_event_mmap_page *user_page;
  585. void *data_pages[0];
  586. };
  587. struct perf_pending_entry {
  588. struct perf_pending_entry *next;
  589. void (*func)(struct perf_pending_entry *);
  590. };
  591. struct perf_sample_data;
  592. typedef void (*perf_overflow_handler_t)(struct perf_event *, int,
  593. struct perf_sample_data *,
  594. struct pt_regs *regs);
  595. enum perf_group_flag {
  596. PERF_GROUP_SOFTWARE = 0x1,
  597. };
  598. #define SWEVENT_HLIST_BITS 8
  599. #define SWEVENT_HLIST_SIZE (1 << SWEVENT_HLIST_BITS)
  600. struct swevent_hlist {
  601. struct hlist_head heads[SWEVENT_HLIST_SIZE];
  602. struct rcu_head rcu_head;
  603. };
  604. #define PERF_ATTACH_CONTEXT 0x01
  605. #define PERF_ATTACH_GROUP 0x02
  606. /**
  607. * struct perf_event - performance event kernel representation:
  608. */
  609. struct perf_event {
  610. #ifdef CONFIG_PERF_EVENTS
  611. struct list_head group_entry;
  612. struct list_head event_entry;
  613. struct list_head sibling_list;
  614. struct hlist_node hlist_entry;
  615. int nr_siblings;
  616. int group_flags;
  617. struct perf_event *group_leader;
  618. struct pmu *pmu;
  619. enum perf_event_active_state state;
  620. unsigned int attach_state;
  621. local64_t count;
  622. atomic64_t child_count;
  623. /*
  624. * These are the total time in nanoseconds that the event
  625. * has been enabled (i.e. eligible to run, and the task has
  626. * been scheduled in, if this is a per-task event)
  627. * and running (scheduled onto the CPU), respectively.
  628. *
  629. * They are computed from tstamp_enabled, tstamp_running and
  630. * tstamp_stopped when the event is in INACTIVE or ACTIVE state.
  631. */
  632. u64 total_time_enabled;
  633. u64 total_time_running;
  634. /*
  635. * These are timestamps used for computing total_time_enabled
  636. * and total_time_running when the event is in INACTIVE or
  637. * ACTIVE state, measured in nanoseconds from an arbitrary point
  638. * in time.
  639. * tstamp_enabled: the notional time when the event was enabled
  640. * tstamp_running: the notional time when the event was scheduled on
  641. * tstamp_stopped: in INACTIVE state, the notional time when the
  642. * event was scheduled off.
  643. */
  644. u64 tstamp_enabled;
  645. u64 tstamp_running;
  646. u64 tstamp_stopped;
  647. struct perf_event_attr attr;
  648. struct hw_perf_event hw;
  649. struct perf_event_context *ctx;
  650. struct file *filp;
  651. /*
  652. * These accumulate total time (in nanoseconds) that children
  653. * events have been enabled and running, respectively.
  654. */
  655. atomic64_t child_total_time_enabled;
  656. atomic64_t child_total_time_running;
  657. /*
  658. * Protect attach/detach and child_list:
  659. */
  660. struct mutex child_mutex;
  661. struct list_head child_list;
  662. struct perf_event *parent;
  663. int oncpu;
  664. int cpu;
  665. struct list_head owner_entry;
  666. struct task_struct *owner;
  667. /* mmap bits */
  668. struct mutex mmap_mutex;
  669. atomic_t mmap_count;
  670. int mmap_locked;
  671. struct user_struct *mmap_user;
  672. struct perf_buffer *buffer;
  673. /* poll related */
  674. wait_queue_head_t waitq;
  675. struct fasync_struct *fasync;
  676. /* delayed work for NMIs and such */
  677. int pending_wakeup;
  678. int pending_kill;
  679. int pending_disable;
  680. struct perf_pending_entry pending;
  681. atomic_t event_limit;
  682. void (*destroy)(struct perf_event *);
  683. struct rcu_head rcu_head;
  684. struct pid_namespace *ns;
  685. u64 id;
  686. perf_overflow_handler_t overflow_handler;
  687. #ifdef CONFIG_EVENT_TRACING
  688. struct ftrace_event_call *tp_event;
  689. struct event_filter *filter;
  690. #endif
  691. #endif /* CONFIG_PERF_EVENTS */
  692. };
  693. /**
  694. * struct perf_event_context - event context structure
  695. *
  696. * Used as a container for task events and CPU events as well:
  697. */
  698. struct perf_event_context {
  699. struct pmu *pmu;
  700. /*
  701. * Protect the states of the events in the list,
  702. * nr_active, and the list:
  703. */
  704. raw_spinlock_t lock;
  705. /*
  706. * Protect the list of events. Locking either mutex or lock
  707. * is sufficient to ensure the list doesn't change; to change
  708. * the list you need to lock both the mutex and the spinlock.
  709. */
  710. struct mutex mutex;
  711. struct list_head pinned_groups;
  712. struct list_head flexible_groups;
  713. struct list_head event_list;
  714. int nr_events;
  715. int nr_active;
  716. int is_active;
  717. int nr_stat;
  718. atomic_t refcount;
  719. struct task_struct *task;
  720. /*
  721. * Context clock, runs when context enabled.
  722. */
  723. u64 time;
  724. u64 timestamp;
  725. /*
  726. * These fields let us detect when two contexts have both
  727. * been cloned (inherited) from a common ancestor.
  728. */
  729. struct perf_event_context *parent_ctx;
  730. u64 parent_gen;
  731. u64 generation;
  732. int pin_count;
  733. struct rcu_head rcu_head;
  734. };
  735. /*
  736. * Number of contexts where an event can trigger:
  737. * task, softirq, hardirq, nmi.
  738. */
  739. #define PERF_NR_CONTEXTS 4
  740. /**
  741. * struct perf_event_cpu_context - per cpu event context structure
  742. */
  743. struct perf_cpu_context {
  744. struct perf_event_context ctx;
  745. struct perf_event_context *task_ctx;
  746. int active_oncpu;
  747. int exclusive;
  748. u64 timer_interval;
  749. struct hrtimer timer;
  750. };
  751. struct perf_output_handle {
  752. struct perf_event *event;
  753. struct perf_buffer *buffer;
  754. unsigned long wakeup;
  755. unsigned long size;
  756. void *addr;
  757. int page;
  758. int nmi;
  759. int sample;
  760. };
  761. #ifdef CONFIG_PERF_EVENTS
  762. extern int perf_pmu_register(struct pmu *pmu);
  763. extern void perf_pmu_unregister(struct pmu *pmu);
  764. extern void perf_event_task_sched_in(struct task_struct *task);
  765. extern void perf_event_task_sched_out(struct task_struct *task, struct task_struct *next);
  766. extern int perf_event_init_task(struct task_struct *child);
  767. extern void perf_event_exit_task(struct task_struct *child);
  768. extern void perf_event_free_task(struct task_struct *task);
  769. extern void set_perf_event_pending(void);
  770. extern void perf_event_do_pending(void);
  771. extern void perf_event_print_debug(void);
  772. extern void perf_pmu_disable(struct pmu *pmu);
  773. extern void perf_pmu_enable(struct pmu *pmu);
  774. extern int perf_event_task_disable(void);
  775. extern int perf_event_task_enable(void);
  776. extern void perf_event_update_userpage(struct perf_event *event);
  777. extern int perf_event_release_kernel(struct perf_event *event);
  778. extern struct perf_event *
  779. perf_event_create_kernel_counter(struct perf_event_attr *attr,
  780. int cpu,
  781. pid_t pid,
  782. perf_overflow_handler_t callback);
  783. extern u64 perf_event_read_value(struct perf_event *event,
  784. u64 *enabled, u64 *running);
  785. struct perf_sample_data {
  786. u64 type;
  787. u64 ip;
  788. struct {
  789. u32 pid;
  790. u32 tid;
  791. } tid_entry;
  792. u64 time;
  793. u64 addr;
  794. u64 id;
  795. u64 stream_id;
  796. struct {
  797. u32 cpu;
  798. u32 reserved;
  799. } cpu_entry;
  800. u64 period;
  801. struct perf_callchain_entry *callchain;
  802. struct perf_raw_record *raw;
  803. };
  804. static inline
  805. void perf_sample_data_init(struct perf_sample_data *data, u64 addr)
  806. {
  807. data->addr = addr;
  808. data->raw = NULL;
  809. }
  810. extern void perf_output_sample(struct perf_output_handle *handle,
  811. struct perf_event_header *header,
  812. struct perf_sample_data *data,
  813. struct perf_event *event);
  814. extern void perf_prepare_sample(struct perf_event_header *header,
  815. struct perf_sample_data *data,
  816. struct perf_event *event,
  817. struct pt_regs *regs);
  818. extern int perf_event_overflow(struct perf_event *event, int nmi,
  819. struct perf_sample_data *data,
  820. struct pt_regs *regs);
  821. /*
  822. * Return 1 for a software event, 0 for a hardware event
  823. */
  824. static inline int is_software_event(struct perf_event *event)
  825. {
  826. switch (event->attr.type) {
  827. case PERF_TYPE_SOFTWARE:
  828. case PERF_TYPE_TRACEPOINT:
  829. /* for now the breakpoint stuff also works as software event */
  830. case PERF_TYPE_BREAKPOINT:
  831. return 1;
  832. }
  833. return 0;
  834. }
  835. extern atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
  836. extern void __perf_sw_event(u32, u64, int, struct pt_regs *, u64);
  837. #ifndef perf_arch_fetch_caller_regs
  838. static inline void
  839. perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip) { }
  840. #endif
  841. /*
  842. * Take a snapshot of the regs. Skip ip and frame pointer to
  843. * the nth caller. We only need a few of the regs:
  844. * - ip for PERF_SAMPLE_IP
  845. * - cs for user_mode() tests
  846. * - bp for callchains
  847. * - eflags, for future purposes, just in case
  848. */
  849. static inline void perf_fetch_caller_regs(struct pt_regs *regs)
  850. {
  851. memset(regs, 0, sizeof(*regs));
  852. perf_arch_fetch_caller_regs(regs, CALLER_ADDR0);
  853. }
  854. static inline void
  855. perf_sw_event(u32 event_id, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
  856. {
  857. if (atomic_read(&perf_swevent_enabled[event_id])) {
  858. struct pt_regs hot_regs;
  859. if (!regs) {
  860. perf_fetch_caller_regs(&hot_regs);
  861. regs = &hot_regs;
  862. }
  863. __perf_sw_event(event_id, nr, nmi, regs, addr);
  864. }
  865. }
  866. extern void perf_event_mmap(struct vm_area_struct *vma);
  867. extern struct perf_guest_info_callbacks *perf_guest_cbs;
  868. extern int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
  869. extern int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
  870. extern void perf_event_comm(struct task_struct *tsk);
  871. extern void perf_event_fork(struct task_struct *tsk);
  872. /* Callchains */
  873. DECLARE_PER_CPU(struct perf_callchain_entry, perf_callchain_entry);
  874. extern void perf_callchain_user(struct perf_callchain_entry *entry,
  875. struct pt_regs *regs);
  876. extern void perf_callchain_kernel(struct perf_callchain_entry *entry,
  877. struct pt_regs *regs);
  878. static inline void
  879. perf_callchain_store(struct perf_callchain_entry *entry, u64 ip)
  880. {
  881. if (entry->nr < PERF_MAX_STACK_DEPTH)
  882. entry->ip[entry->nr++] = ip;
  883. }
  884. extern int sysctl_perf_event_paranoid;
  885. extern int sysctl_perf_event_mlock;
  886. extern int sysctl_perf_event_sample_rate;
  887. static inline bool perf_paranoid_tracepoint_raw(void)
  888. {
  889. return sysctl_perf_event_paranoid > -1;
  890. }
  891. static inline bool perf_paranoid_cpu(void)
  892. {
  893. return sysctl_perf_event_paranoid > 0;
  894. }
  895. static inline bool perf_paranoid_kernel(void)
  896. {
  897. return sysctl_perf_event_paranoid > 1;
  898. }
  899. extern void perf_event_init(void);
  900. extern void perf_tp_event(u64 addr, u64 count, void *record,
  901. int entry_size, struct pt_regs *regs,
  902. struct hlist_head *head, int rctx);
  903. extern void perf_bp_event(struct perf_event *event, void *data);
  904. #ifndef perf_misc_flags
  905. #define perf_misc_flags(regs) (user_mode(regs) ? PERF_RECORD_MISC_USER : \
  906. PERF_RECORD_MISC_KERNEL)
  907. #define perf_instruction_pointer(regs) instruction_pointer(regs)
  908. #endif
  909. extern int perf_output_begin(struct perf_output_handle *handle,
  910. struct perf_event *event, unsigned int size,
  911. int nmi, int sample);
  912. extern void perf_output_end(struct perf_output_handle *handle);
  913. extern void perf_output_copy(struct perf_output_handle *handle,
  914. const void *buf, unsigned int len);
  915. extern int perf_swevent_get_recursion_context(void);
  916. extern void perf_swevent_put_recursion_context(int rctx);
  917. extern void perf_event_enable(struct perf_event *event);
  918. extern void perf_event_disable(struct perf_event *event);
  919. #else
  920. static inline void
  921. perf_event_task_sched_in(struct task_struct *task) { }
  922. static inline void
  923. perf_event_task_sched_out(struct task_struct *task,
  924. struct task_struct *next) { }
  925. static inline int perf_event_init_task(struct task_struct *child) { return 0; }
  926. static inline void perf_event_exit_task(struct task_struct *child) { }
  927. static inline void perf_event_free_task(struct task_struct *task) { }
  928. static inline void perf_event_do_pending(void) { }
  929. static inline void perf_event_print_debug(void) { }
  930. static inline int perf_event_task_disable(void) { return -EINVAL; }
  931. static inline int perf_event_task_enable(void) { return -EINVAL; }
  932. static inline void
  933. perf_sw_event(u32 event_id, u64 nr, int nmi,
  934. struct pt_regs *regs, u64 addr) { }
  935. static inline void
  936. perf_bp_event(struct perf_event *event, void *data) { }
  937. static inline int perf_register_guest_info_callbacks
  938. (struct perf_guest_info_callbacks *callbacks) { return 0; }
  939. static inline int perf_unregister_guest_info_callbacks
  940. (struct perf_guest_info_callbacks *callbacks) { return 0; }
  941. static inline void perf_event_mmap(struct vm_area_struct *vma) { }
  942. static inline void perf_event_comm(struct task_struct *tsk) { }
  943. static inline void perf_event_fork(struct task_struct *tsk) { }
  944. static inline void perf_event_init(void) { }
  945. static inline int perf_swevent_get_recursion_context(void) { return -1; }
  946. static inline void perf_swevent_put_recursion_context(int rctx) { }
  947. static inline void perf_event_enable(struct perf_event *event) { }
  948. static inline void perf_event_disable(struct perf_event *event) { }
  949. #endif
  950. #define perf_output_put(handle, x) \
  951. perf_output_copy((handle), &(x), sizeof(x))
  952. /*
  953. * This has to have a higher priority than migration_notifier in sched.c.
  954. */
  955. #define perf_cpu_notifier(fn) \
  956. do { \
  957. static struct notifier_block fn##_nb __cpuinitdata = \
  958. { .notifier_call = fn, .priority = CPU_PRI_PERF }; \
  959. fn(&fn##_nb, (unsigned long)CPU_UP_PREPARE, \
  960. (void *)(unsigned long)smp_processor_id()); \
  961. fn(&fn##_nb, (unsigned long)CPU_STARTING, \
  962. (void *)(unsigned long)smp_processor_id()); \
  963. fn(&fn##_nb, (unsigned long)CPU_ONLINE, \
  964. (void *)(unsigned long)smp_processor_id()); \
  965. register_cpu_notifier(&fn##_nb); \
  966. } while (0)
  967. #endif /* __KERNEL__ */
  968. #endif /* _LINUX_PERF_EVENT_H */