perf_counter.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /*
  2. * Performance counters:
  3. *
  4. * Copyright(C) 2008, Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright(C) 2008, Red Hat, Inc., Ingo Molnar
  6. *
  7. * Data type definitions, declarations, prototypes.
  8. *
  9. * Started by: Thomas Gleixner and Ingo Molnar
  10. *
  11. * For licencing details see kernel-base/COPYING
  12. */
  13. #ifndef _LINUX_PERF_COUNTER_H
  14. #define _LINUX_PERF_COUNTER_H
  15. #include <linux/types.h>
  16. #include <linux/ioctl.h>
  17. #include <asm/byteorder.h>
  18. /*
  19. * User-space ABI bits:
  20. */
  21. /*
  22. * hw_event.type
  23. */
  24. enum perf_event_types {
  25. PERF_TYPE_HARDWARE = 0,
  26. PERF_TYPE_SOFTWARE = 1,
  27. PERF_TYPE_TRACEPOINT = 2,
  28. /*
  29. * available TYPE space, raw is the max value.
  30. */
  31. PERF_TYPE_RAW = 128,
  32. };
  33. /*
  34. * Generalized performance counter event types, used by the hw_event.event_id
  35. * parameter of the sys_perf_counter_open() syscall:
  36. */
  37. enum hw_event_ids {
  38. /*
  39. * Common hardware events, generalized by the kernel:
  40. */
  41. PERF_COUNT_CPU_CYCLES = 0,
  42. PERF_COUNT_INSTRUCTIONS = 1,
  43. PERF_COUNT_CACHE_REFERENCES = 2,
  44. PERF_COUNT_CACHE_MISSES = 3,
  45. PERF_COUNT_BRANCH_INSTRUCTIONS = 4,
  46. PERF_COUNT_BRANCH_MISSES = 5,
  47. PERF_COUNT_BUS_CYCLES = 6,
  48. PERF_HW_EVENTS_MAX = 7,
  49. };
  50. /*
  51. * Special "software" counters provided by the kernel, even if the hardware
  52. * does not support performance counters. These counters measure various
  53. * physical and sw events of the kernel (and allow the profiling of them as
  54. * well):
  55. */
  56. enum sw_event_ids {
  57. PERF_COUNT_CPU_CLOCK = 0,
  58. PERF_COUNT_TASK_CLOCK = 1,
  59. PERF_COUNT_PAGE_FAULTS = 2,
  60. PERF_COUNT_CONTEXT_SWITCHES = 3,
  61. PERF_COUNT_CPU_MIGRATIONS = 4,
  62. PERF_COUNT_PAGE_FAULTS_MIN = 5,
  63. PERF_COUNT_PAGE_FAULTS_MAJ = 6,
  64. PERF_SW_EVENTS_MAX = 7,
  65. };
  66. #define __PERF_COUNTER_MASK(name) \
  67. (((1ULL << PERF_COUNTER_##name##_BITS) - 1) << \
  68. PERF_COUNTER_##name##_SHIFT)
  69. #define PERF_COUNTER_RAW_BITS 1
  70. #define PERF_COUNTER_RAW_SHIFT 63
  71. #define PERF_COUNTER_RAW_MASK __PERF_COUNTER_MASK(RAW)
  72. #define PERF_COUNTER_CONFIG_BITS 63
  73. #define PERF_COUNTER_CONFIG_SHIFT 0
  74. #define PERF_COUNTER_CONFIG_MASK __PERF_COUNTER_MASK(CONFIG)
  75. #define PERF_COUNTER_TYPE_BITS 7
  76. #define PERF_COUNTER_TYPE_SHIFT 56
  77. #define PERF_COUNTER_TYPE_MASK __PERF_COUNTER_MASK(TYPE)
  78. #define PERF_COUNTER_EVENT_BITS 56
  79. #define PERF_COUNTER_EVENT_SHIFT 0
  80. #define PERF_COUNTER_EVENT_MASK __PERF_COUNTER_MASK(EVENT)
  81. /*
  82. * Bits that can be set in hw_event.record_type to request information
  83. * in the overflow packets.
  84. */
  85. enum perf_counter_record_format {
  86. PERF_RECORD_IP = 1U << 0,
  87. PERF_RECORD_TID = 1U << 1,
  88. PERF_RECORD_TIME = 1U << 2,
  89. PERF_RECORD_ADDR = 1U << 3,
  90. PERF_RECORD_GROUP = 1U << 4,
  91. PERF_RECORD_CALLCHAIN = 1U << 5,
  92. PERF_RECORD_CONFIG = 1U << 6,
  93. PERF_RECORD_CPU = 1U << 7,
  94. };
  95. /*
  96. * Bits that can be set in hw_event.read_format to request that
  97. * reads on the counter should return the indicated quantities,
  98. * in increasing order of bit value, after the counter value.
  99. */
  100. enum perf_counter_read_format {
  101. PERF_FORMAT_TOTAL_TIME_ENABLED = 1,
  102. PERF_FORMAT_TOTAL_TIME_RUNNING = 2,
  103. };
  104. /*
  105. * Hardware event to monitor via a performance monitoring counter:
  106. */
  107. struct perf_counter_hw_event {
  108. /*
  109. * The MSB of the config word signifies if the rest contains cpu
  110. * specific (raw) counter configuration data, if unset, the next
  111. * 7 bits are an event type and the rest of the bits are the event
  112. * identifier.
  113. */
  114. __u64 config;
  115. __u64 irq_period;
  116. __u32 record_type;
  117. __u32 read_format;
  118. __u64 disabled : 1, /* off by default */
  119. nmi : 1, /* NMI sampling */
  120. inherit : 1, /* children inherit it */
  121. pinned : 1, /* must always be on PMU */
  122. exclusive : 1, /* only group on PMU */
  123. exclude_user : 1, /* don't count user */
  124. exclude_kernel : 1, /* ditto kernel */
  125. exclude_hv : 1, /* ditto hypervisor */
  126. exclude_idle : 1, /* don't count when idle */
  127. mmap : 1, /* include mmap data */
  128. munmap : 1, /* include munmap data */
  129. comm : 1, /* include comm data */
  130. __reserved_1 : 52;
  131. __u32 extra_config_len;
  132. __u32 wakeup_events; /* wakeup every n events */
  133. __u64 __reserved_2;
  134. __u64 __reserved_3;
  135. };
  136. /*
  137. * Ioctls that can be done on a perf counter fd:
  138. */
  139. #define PERF_COUNTER_IOC_ENABLE _IOW('$', 0, u32)
  140. #define PERF_COUNTER_IOC_DISABLE _IOW('$', 1, u32)
  141. #define PERF_COUNTER_IOC_REFRESH _IOW('$', 2, u32)
  142. #define PERF_COUNTER_IOC_RESET _IOW('$', 3, u32)
  143. enum perf_counter_ioc_flags {
  144. PERF_IOC_FLAG_GROUP = 1U << 0,
  145. };
  146. /*
  147. * Structure of the page that can be mapped via mmap
  148. */
  149. struct perf_counter_mmap_page {
  150. __u32 version; /* version number of this structure */
  151. __u32 compat_version; /* lowest version this is compat with */
  152. /*
  153. * Bits needed to read the hw counters in user-space.
  154. *
  155. * u32 seq;
  156. * s64 count;
  157. *
  158. * do {
  159. * seq = pc->lock;
  160. *
  161. * barrier()
  162. * if (pc->index) {
  163. * count = pmc_read(pc->index - 1);
  164. * count += pc->offset;
  165. * } else
  166. * goto regular_read;
  167. *
  168. * barrier();
  169. * } while (pc->lock != seq);
  170. *
  171. * NOTE: for obvious reason this only works on self-monitoring
  172. * processes.
  173. */
  174. __u32 lock; /* seqlock for synchronization */
  175. __u32 index; /* hardware counter identifier */
  176. __s64 offset; /* add to hardware counter value */
  177. /*
  178. * Control data for the mmap() data buffer.
  179. *
  180. * User-space reading this value should issue an rmb(), on SMP capable
  181. * platforms, after reading this value -- see perf_counter_wakeup().
  182. */
  183. __u32 data_head; /* head in the data section */
  184. };
  185. #define PERF_EVENT_MISC_KERNEL (1 << 0)
  186. #define PERF_EVENT_MISC_USER (1 << 1)
  187. #define PERF_EVENT_MISC_OVERFLOW (1 << 2)
  188. struct perf_event_header {
  189. __u32 type;
  190. __u16 misc;
  191. __u16 size;
  192. };
  193. enum perf_event_type {
  194. /*
  195. * The MMAP events record the PROT_EXEC mappings so that we can
  196. * correlate userspace IPs to code. They have the following structure:
  197. *
  198. * struct {
  199. * struct perf_event_header header;
  200. *
  201. * u32 pid, tid;
  202. * u64 addr;
  203. * u64 len;
  204. * u64 pgoff;
  205. * char filename[];
  206. * };
  207. */
  208. PERF_EVENT_MMAP = 1,
  209. PERF_EVENT_MUNMAP = 2,
  210. /*
  211. * struct {
  212. * struct perf_event_header header;
  213. *
  214. * u32 pid, tid;
  215. * char comm[];
  216. * };
  217. */
  218. PERF_EVENT_COMM = 3,
  219. /*
  220. * When header.misc & PERF_EVENT_MISC_OVERFLOW the event_type field
  221. * will be PERF_RECORD_*
  222. *
  223. * struct {
  224. * struct perf_event_header header;
  225. *
  226. * { u64 ip; } && PERF_RECORD_IP
  227. * { u32 pid, tid; } && PERF_RECORD_TID
  228. * { u64 time; } && PERF_RECORD_TIME
  229. * { u64 addr; } && PERF_RECORD_ADDR
  230. * { u64 config; } && PERF_RECORD_CONFIG
  231. * { u32 cpu, res; } && PERF_RECORD_CPU
  232. *
  233. * { u64 nr;
  234. * { u64 event, val; } cnt[nr]; } && PERF_RECORD_GROUP
  235. *
  236. * { u16 nr,
  237. * hv,
  238. * kernel,
  239. * user;
  240. * u64 ips[nr]; } && PERF_RECORD_CALLCHAIN
  241. * };
  242. */
  243. };
  244. #ifdef __KERNEL__
  245. /*
  246. * Kernel-internal data types and definitions:
  247. */
  248. #ifdef CONFIG_PERF_COUNTERS
  249. # include <asm/perf_counter.h>
  250. #endif
  251. #include <linux/list.h>
  252. #include <linux/mutex.h>
  253. #include <linux/rculist.h>
  254. #include <linux/rcupdate.h>
  255. #include <linux/spinlock.h>
  256. #include <linux/hrtimer.h>
  257. #include <linux/fs.h>
  258. #include <asm/atomic.h>
  259. struct task_struct;
  260. static inline u64 perf_event_raw(struct perf_counter_hw_event *hw_event)
  261. {
  262. return hw_event->config & PERF_COUNTER_RAW_MASK;
  263. }
  264. static inline u64 perf_event_config(struct perf_counter_hw_event *hw_event)
  265. {
  266. return hw_event->config & PERF_COUNTER_CONFIG_MASK;
  267. }
  268. static inline u64 perf_event_type(struct perf_counter_hw_event *hw_event)
  269. {
  270. return (hw_event->config & PERF_COUNTER_TYPE_MASK) >>
  271. PERF_COUNTER_TYPE_SHIFT;
  272. }
  273. static inline u64 perf_event_id(struct perf_counter_hw_event *hw_event)
  274. {
  275. return hw_event->config & PERF_COUNTER_EVENT_MASK;
  276. }
  277. /**
  278. * struct hw_perf_counter - performance counter hardware details:
  279. */
  280. struct hw_perf_counter {
  281. #ifdef CONFIG_PERF_COUNTERS
  282. union {
  283. struct { /* hardware */
  284. u64 config;
  285. unsigned long config_base;
  286. unsigned long counter_base;
  287. int nmi;
  288. int idx;
  289. };
  290. union { /* software */
  291. atomic64_t count;
  292. struct hrtimer hrtimer;
  293. };
  294. };
  295. atomic64_t prev_count;
  296. u64 irq_period;
  297. atomic64_t period_left;
  298. #endif
  299. };
  300. struct perf_counter;
  301. /**
  302. * struct pmu - generic performance monitoring unit
  303. */
  304. struct pmu {
  305. int (*enable) (struct perf_counter *counter);
  306. void (*disable) (struct perf_counter *counter);
  307. void (*read) (struct perf_counter *counter);
  308. };
  309. /**
  310. * enum perf_counter_active_state - the states of a counter
  311. */
  312. enum perf_counter_active_state {
  313. PERF_COUNTER_STATE_ERROR = -2,
  314. PERF_COUNTER_STATE_OFF = -1,
  315. PERF_COUNTER_STATE_INACTIVE = 0,
  316. PERF_COUNTER_STATE_ACTIVE = 1,
  317. };
  318. struct file;
  319. struct perf_mmap_data {
  320. struct rcu_head rcu_head;
  321. int nr_pages; /* nr of data pages */
  322. int nr_locked; /* nr pages mlocked */
  323. atomic_t poll; /* POLL_ for wakeups */
  324. atomic_t head; /* write position */
  325. atomic_t events; /* event limit */
  326. atomic_t done_head; /* completed head */
  327. atomic_t lock; /* concurrent writes */
  328. atomic_t wakeup; /* needs a wakeup */
  329. struct perf_counter_mmap_page *user_page;
  330. void *data_pages[0];
  331. };
  332. struct perf_pending_entry {
  333. struct perf_pending_entry *next;
  334. void (*func)(struct perf_pending_entry *);
  335. };
  336. /**
  337. * struct perf_counter - performance counter kernel representation:
  338. */
  339. struct perf_counter {
  340. #ifdef CONFIG_PERF_COUNTERS
  341. struct list_head list_entry;
  342. struct list_head event_entry;
  343. struct list_head sibling_list;
  344. int nr_siblings;
  345. struct perf_counter *group_leader;
  346. const struct pmu *pmu;
  347. enum perf_counter_active_state state;
  348. enum perf_counter_active_state prev_state;
  349. atomic64_t count;
  350. /*
  351. * These are the total time in nanoseconds that the counter
  352. * has been enabled (i.e. eligible to run, and the task has
  353. * been scheduled in, if this is a per-task counter)
  354. * and running (scheduled onto the CPU), respectively.
  355. *
  356. * They are computed from tstamp_enabled, tstamp_running and
  357. * tstamp_stopped when the counter is in INACTIVE or ACTIVE state.
  358. */
  359. u64 total_time_enabled;
  360. u64 total_time_running;
  361. /*
  362. * These are timestamps used for computing total_time_enabled
  363. * and total_time_running when the counter is in INACTIVE or
  364. * ACTIVE state, measured in nanoseconds from an arbitrary point
  365. * in time.
  366. * tstamp_enabled: the notional time when the counter was enabled
  367. * tstamp_running: the notional time when the counter was scheduled on
  368. * tstamp_stopped: in INACTIVE state, the notional time when the
  369. * counter was scheduled off.
  370. */
  371. u64 tstamp_enabled;
  372. u64 tstamp_running;
  373. u64 tstamp_stopped;
  374. struct perf_counter_hw_event hw_event;
  375. struct hw_perf_counter hw;
  376. struct perf_counter_context *ctx;
  377. struct task_struct *task;
  378. struct file *filp;
  379. struct perf_counter *parent;
  380. struct list_head child_list;
  381. /*
  382. * These accumulate total time (in nanoseconds) that children
  383. * counters have been enabled and running, respectively.
  384. */
  385. atomic64_t child_total_time_enabled;
  386. atomic64_t child_total_time_running;
  387. /*
  388. * Protect attach/detach and child_list:
  389. */
  390. struct mutex mutex;
  391. int oncpu;
  392. int cpu;
  393. /* mmap bits */
  394. struct mutex mmap_mutex;
  395. atomic_t mmap_count;
  396. struct perf_mmap_data *data;
  397. /* poll related */
  398. wait_queue_head_t waitq;
  399. struct fasync_struct *fasync;
  400. /* delayed work for NMIs and such */
  401. int pending_wakeup;
  402. int pending_kill;
  403. int pending_disable;
  404. struct perf_pending_entry pending;
  405. atomic_t event_limit;
  406. void (*destroy)(struct perf_counter *);
  407. struct rcu_head rcu_head;
  408. #endif
  409. };
  410. /**
  411. * struct perf_counter_context - counter context structure
  412. *
  413. * Used as a container for task counters and CPU counters as well:
  414. */
  415. struct perf_counter_context {
  416. #ifdef CONFIG_PERF_COUNTERS
  417. /*
  418. * Protect the states of the counters in the list,
  419. * nr_active, and the list:
  420. */
  421. spinlock_t lock;
  422. /*
  423. * Protect the list of counters. Locking either mutex or lock
  424. * is sufficient to ensure the list doesn't change; to change
  425. * the list you need to lock both the mutex and the spinlock.
  426. */
  427. struct mutex mutex;
  428. struct list_head counter_list;
  429. struct list_head event_list;
  430. int nr_counters;
  431. int nr_active;
  432. int is_active;
  433. struct task_struct *task;
  434. /*
  435. * Context clock, runs when context enabled.
  436. */
  437. u64 time;
  438. u64 timestamp;
  439. #endif
  440. };
  441. /**
  442. * struct perf_counter_cpu_context - per cpu counter context structure
  443. */
  444. struct perf_cpu_context {
  445. struct perf_counter_context ctx;
  446. struct perf_counter_context *task_ctx;
  447. int active_oncpu;
  448. int max_pertask;
  449. int exclusive;
  450. /*
  451. * Recursion avoidance:
  452. *
  453. * task, softirq, irq, nmi context
  454. */
  455. int recursion[4];
  456. };
  457. #ifdef CONFIG_PERF_COUNTERS
  458. /*
  459. * Set by architecture code:
  460. */
  461. extern int perf_max_counters;
  462. extern const struct pmu *hw_perf_counter_init(struct perf_counter *counter);
  463. extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
  464. extern void perf_counter_task_sched_out(struct task_struct *task, int cpu);
  465. extern void perf_counter_task_tick(struct task_struct *task, int cpu);
  466. extern void perf_counter_init_task(struct task_struct *child);
  467. extern void perf_counter_exit_task(struct task_struct *child);
  468. extern void perf_counter_do_pending(void);
  469. extern void perf_counter_print_debug(void);
  470. extern void perf_counter_unthrottle(void);
  471. extern u64 hw_perf_save_disable(void);
  472. extern void hw_perf_restore(u64 ctrl);
  473. extern int perf_counter_task_disable(void);
  474. extern int perf_counter_task_enable(void);
  475. extern int hw_perf_group_sched_in(struct perf_counter *group_leader,
  476. struct perf_cpu_context *cpuctx,
  477. struct perf_counter_context *ctx, int cpu);
  478. extern void perf_counter_update_userpage(struct perf_counter *counter);
  479. extern int perf_counter_overflow(struct perf_counter *counter,
  480. int nmi, struct pt_regs *regs, u64 addr);
  481. /*
  482. * Return 1 for a software counter, 0 for a hardware counter
  483. */
  484. static inline int is_software_counter(struct perf_counter *counter)
  485. {
  486. return !perf_event_raw(&counter->hw_event) &&
  487. perf_event_type(&counter->hw_event) != PERF_TYPE_HARDWARE;
  488. }
  489. extern void perf_swcounter_event(u32, u64, int, struct pt_regs *, u64);
  490. extern void perf_counter_mmap(unsigned long addr, unsigned long len,
  491. unsigned long pgoff, struct file *file);
  492. extern void perf_counter_munmap(unsigned long addr, unsigned long len,
  493. unsigned long pgoff, struct file *file);
  494. extern void perf_counter_comm(struct task_struct *tsk);
  495. #define MAX_STACK_DEPTH 255
  496. struct perf_callchain_entry {
  497. u16 nr, hv, kernel, user;
  498. u64 ip[MAX_STACK_DEPTH];
  499. };
  500. extern struct perf_callchain_entry *perf_callchain(struct pt_regs *regs);
  501. extern int sysctl_perf_counter_priv;
  502. extern int sysctl_perf_counter_mlock;
  503. extern void perf_counter_init(void);
  504. #else
  505. static inline void
  506. perf_counter_task_sched_in(struct task_struct *task, int cpu) { }
  507. static inline void
  508. perf_counter_task_sched_out(struct task_struct *task, int cpu) { }
  509. static inline void
  510. perf_counter_task_tick(struct task_struct *task, int cpu) { }
  511. static inline void perf_counter_init_task(struct task_struct *child) { }
  512. static inline void perf_counter_exit_task(struct task_struct *child) { }
  513. static inline void perf_counter_do_pending(void) { }
  514. static inline void perf_counter_print_debug(void) { }
  515. static inline void perf_counter_unthrottle(void) { }
  516. static inline void hw_perf_restore(u64 ctrl) { }
  517. static inline u64 hw_perf_save_disable(void) { return 0; }
  518. static inline int perf_counter_task_disable(void) { return -EINVAL; }
  519. static inline int perf_counter_task_enable(void) { return -EINVAL; }
  520. static inline void
  521. perf_swcounter_event(u32 event, u64 nr, int nmi,
  522. struct pt_regs *regs, u64 addr) { }
  523. static inline void
  524. perf_counter_mmap(unsigned long addr, unsigned long len,
  525. unsigned long pgoff, struct file *file) { }
  526. static inline void
  527. perf_counter_munmap(unsigned long addr, unsigned long len,
  528. unsigned long pgoff, struct file *file) { }
  529. static inline void perf_counter_comm(struct task_struct *tsk) { }
  530. static inline void perf_counter_init(void) { }
  531. #endif
  532. #endif /* __KERNEL__ */
  533. #endif /* _LINUX_PERF_COUNTER_H */