perf_event.h 31 KB

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