perf_event.h 24 KB

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