perf_counter.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. /*
  67. * IRQ-notification data record type:
  68. */
  69. enum perf_counter_record_type {
  70. PERF_RECORD_SIMPLE = 0,
  71. PERF_RECORD_IRQ = 1,
  72. PERF_RECORD_GROUP = 2,
  73. };
  74. #define __PERF_COUNTER_MASK(name) \
  75. (((1ULL << PERF_COUNTER_##name##_BITS) - 1) << \
  76. PERF_COUNTER_##name##_SHIFT)
  77. #define PERF_COUNTER_RAW_BITS 1
  78. #define PERF_COUNTER_RAW_SHIFT 63
  79. #define PERF_COUNTER_RAW_MASK __PERF_COUNTER_MASK(RAW)
  80. #define PERF_COUNTER_CONFIG_BITS 63
  81. #define PERF_COUNTER_CONFIG_SHIFT 0
  82. #define PERF_COUNTER_CONFIG_MASK __PERF_COUNTER_MASK(CONFIG)
  83. #define PERF_COUNTER_TYPE_BITS 7
  84. #define PERF_COUNTER_TYPE_SHIFT 56
  85. #define PERF_COUNTER_TYPE_MASK __PERF_COUNTER_MASK(TYPE)
  86. #define PERF_COUNTER_EVENT_BITS 56
  87. #define PERF_COUNTER_EVENT_SHIFT 0
  88. #define PERF_COUNTER_EVENT_MASK __PERF_COUNTER_MASK(EVENT)
  89. /*
  90. * Bits that can be set in hw_event.read_format to request that
  91. * reads on the counter should return the indicated quantities,
  92. * in increasing order of bit value, after the counter value.
  93. */
  94. enum perf_counter_read_format {
  95. PERF_FORMAT_TOTAL_TIME_ENABLED = 1,
  96. PERF_FORMAT_TOTAL_TIME_RUNNING = 2,
  97. };
  98. /*
  99. * Hardware event to monitor via a performance monitoring counter:
  100. */
  101. struct perf_counter_hw_event {
  102. /*
  103. * The MSB of the config word signifies if the rest contains cpu
  104. * specific (raw) counter configuration data, if unset, the next
  105. * 7 bits are an event type and the rest of the bits are the event
  106. * identifier.
  107. */
  108. __u64 config;
  109. __u64 irq_period;
  110. __u64 record_type;
  111. __u64 read_format;
  112. __u64 disabled : 1, /* off by default */
  113. nmi : 1, /* NMI sampling */
  114. inherit : 1, /* children inherit it */
  115. pinned : 1, /* must always be on PMU */
  116. exclusive : 1, /* only group on PMU */
  117. exclude_user : 1, /* don't count user */
  118. exclude_kernel : 1, /* ditto kernel */
  119. exclude_hv : 1, /* ditto hypervisor */
  120. exclude_idle : 1, /* don't count when idle */
  121. include_tid : 1, /* include the tid */
  122. mmap : 1, /* include mmap data */
  123. munmap : 1, /* include munmap data */
  124. __reserved_1 : 52;
  125. __u32 extra_config_len;
  126. __u32 __reserved_4;
  127. __u64 __reserved_2;
  128. __u64 __reserved_3;
  129. };
  130. /*
  131. * Ioctls that can be done on a perf counter fd:
  132. */
  133. #define PERF_COUNTER_IOC_ENABLE _IO('$', 0)
  134. #define PERF_COUNTER_IOC_DISABLE _IO('$', 1)
  135. /*
  136. * Structure of the page that can be mapped via mmap
  137. */
  138. struct perf_counter_mmap_page {
  139. __u32 version; /* version number of this structure */
  140. __u32 compat_version; /* lowest version this is compat with */
  141. /*
  142. * Bits needed to read the hw counters in user-space.
  143. *
  144. * The index and offset should be read atomically using the seqlock:
  145. *
  146. * __u32 seq, index;
  147. * __s64 offset;
  148. *
  149. * again:
  150. * rmb();
  151. * seq = pc->lock;
  152. *
  153. * if (unlikely(seq & 1)) {
  154. * cpu_relax();
  155. * goto again;
  156. * }
  157. *
  158. * index = pc->index;
  159. * offset = pc->offset;
  160. *
  161. * rmb();
  162. * if (pc->lock != seq)
  163. * goto again;
  164. *
  165. * After this, index contains architecture specific counter index + 1,
  166. * so that 0 means unavailable, offset contains the value to be added
  167. * to the result of the raw timer read to obtain this counter's value.
  168. */
  169. __u32 lock; /* seqlock for synchronization */
  170. __u32 index; /* hardware counter identifier */
  171. __s64 offset; /* add to hardware counter value */
  172. /*
  173. * Control data for the mmap() data buffer.
  174. *
  175. * User-space reading this value should issue an rmb(), on SMP capable
  176. * platforms, after reading this value -- see perf_counter_wakeup().
  177. */
  178. __u32 data_head; /* head in the data section */
  179. };
  180. struct perf_event_header {
  181. __u32 type;
  182. __u32 size;
  183. };
  184. enum perf_event_type {
  185. PERF_EVENT_IP = 0,
  186. PERF_EVENT_GROUP = 1,
  187. PERF_EVENT_MMAP = 2,
  188. PERF_EVENT_MUNMAP = 3,
  189. __PERF_EVENT_TID = 0x100,
  190. };
  191. #ifdef __KERNEL__
  192. /*
  193. * Kernel-internal data types and definitions:
  194. */
  195. #ifdef CONFIG_PERF_COUNTERS
  196. # include <asm/perf_counter.h>
  197. #endif
  198. #include <linux/list.h>
  199. #include <linux/mutex.h>
  200. #include <linux/rculist.h>
  201. #include <linux/rcupdate.h>
  202. #include <linux/spinlock.h>
  203. #include <linux/hrtimer.h>
  204. #include <asm/atomic.h>
  205. struct task_struct;
  206. static inline u64 perf_event_raw(struct perf_counter_hw_event *hw_event)
  207. {
  208. return hw_event->config & PERF_COUNTER_RAW_MASK;
  209. }
  210. static inline u64 perf_event_config(struct perf_counter_hw_event *hw_event)
  211. {
  212. return hw_event->config & PERF_COUNTER_CONFIG_MASK;
  213. }
  214. static inline u64 perf_event_type(struct perf_counter_hw_event *hw_event)
  215. {
  216. return (hw_event->config & PERF_COUNTER_TYPE_MASK) >>
  217. PERF_COUNTER_TYPE_SHIFT;
  218. }
  219. static inline u64 perf_event_id(struct perf_counter_hw_event *hw_event)
  220. {
  221. return hw_event->config & PERF_COUNTER_EVENT_MASK;
  222. }
  223. /**
  224. * struct hw_perf_counter - performance counter hardware details:
  225. */
  226. struct hw_perf_counter {
  227. #ifdef CONFIG_PERF_COUNTERS
  228. union {
  229. struct { /* hardware */
  230. u64 config;
  231. unsigned long config_base;
  232. unsigned long counter_base;
  233. int nmi;
  234. unsigned int idx;
  235. };
  236. union { /* software */
  237. atomic64_t count;
  238. struct hrtimer hrtimer;
  239. };
  240. };
  241. atomic64_t prev_count;
  242. u64 irq_period;
  243. atomic64_t period_left;
  244. #endif
  245. };
  246. struct perf_counter;
  247. /**
  248. * struct hw_perf_counter_ops - performance counter hw ops
  249. */
  250. struct hw_perf_counter_ops {
  251. int (*enable) (struct perf_counter *counter);
  252. void (*disable) (struct perf_counter *counter);
  253. void (*read) (struct perf_counter *counter);
  254. };
  255. /**
  256. * enum perf_counter_active_state - the states of a counter
  257. */
  258. enum perf_counter_active_state {
  259. PERF_COUNTER_STATE_ERROR = -2,
  260. PERF_COUNTER_STATE_OFF = -1,
  261. PERF_COUNTER_STATE_INACTIVE = 0,
  262. PERF_COUNTER_STATE_ACTIVE = 1,
  263. };
  264. struct file;
  265. struct perf_mmap_data {
  266. struct rcu_head rcu_head;
  267. int nr_pages;
  268. atomic_t wakeup;
  269. atomic_t head;
  270. struct perf_counter_mmap_page *user_page;
  271. void *data_pages[0];
  272. };
  273. struct perf_wakeup_entry {
  274. struct perf_wakeup_entry *next;
  275. };
  276. /**
  277. * struct perf_counter - performance counter kernel representation:
  278. */
  279. struct perf_counter {
  280. #ifdef CONFIG_PERF_COUNTERS
  281. struct list_head list_entry;
  282. struct list_head event_entry;
  283. struct list_head sibling_list;
  284. int nr_siblings;
  285. struct perf_counter *group_leader;
  286. const struct hw_perf_counter_ops *hw_ops;
  287. enum perf_counter_active_state state;
  288. enum perf_counter_active_state prev_state;
  289. atomic64_t count;
  290. /*
  291. * These are the total time in nanoseconds that the counter
  292. * has been enabled (i.e. eligible to run, and the task has
  293. * been scheduled in, if this is a per-task counter)
  294. * and running (scheduled onto the CPU), respectively.
  295. *
  296. * They are computed from tstamp_enabled, tstamp_running and
  297. * tstamp_stopped when the counter is in INACTIVE or ACTIVE state.
  298. */
  299. u64 total_time_enabled;
  300. u64 total_time_running;
  301. /*
  302. * These are timestamps used for computing total_time_enabled
  303. * and total_time_running when the counter is in INACTIVE or
  304. * ACTIVE state, measured in nanoseconds from an arbitrary point
  305. * in time.
  306. * tstamp_enabled: the notional time when the counter was enabled
  307. * tstamp_running: the notional time when the counter was scheduled on
  308. * tstamp_stopped: in INACTIVE state, the notional time when the
  309. * counter was scheduled off.
  310. */
  311. u64 tstamp_enabled;
  312. u64 tstamp_running;
  313. u64 tstamp_stopped;
  314. struct perf_counter_hw_event hw_event;
  315. struct hw_perf_counter hw;
  316. struct perf_counter_context *ctx;
  317. struct task_struct *task;
  318. struct file *filp;
  319. struct perf_counter *parent;
  320. struct list_head child_list;
  321. /*
  322. * These accumulate total time (in nanoseconds) that children
  323. * counters have been enabled and running, respectively.
  324. */
  325. atomic64_t child_total_time_enabled;
  326. atomic64_t child_total_time_running;
  327. /*
  328. * Protect attach/detach and child_list:
  329. */
  330. struct mutex mutex;
  331. int oncpu;
  332. int cpu;
  333. /* mmap bits */
  334. struct mutex mmap_mutex;
  335. atomic_t mmap_count;
  336. struct perf_mmap_data *data;
  337. /* poll related */
  338. wait_queue_head_t waitq;
  339. /* optional: for NMIs */
  340. struct perf_wakeup_entry wakeup;
  341. void (*destroy)(struct perf_counter *);
  342. struct rcu_head rcu_head;
  343. #endif
  344. };
  345. /**
  346. * struct perf_counter_context - counter context structure
  347. *
  348. * Used as a container for task counters and CPU counters as well:
  349. */
  350. struct perf_counter_context {
  351. #ifdef CONFIG_PERF_COUNTERS
  352. /*
  353. * Protect the states of the counters in the list,
  354. * nr_active, and the list:
  355. */
  356. spinlock_t lock;
  357. /*
  358. * Protect the list of counters. Locking either mutex or lock
  359. * is sufficient to ensure the list doesn't change; to change
  360. * the list you need to lock both the mutex and the spinlock.
  361. */
  362. struct mutex mutex;
  363. struct list_head counter_list;
  364. struct list_head event_list;
  365. int nr_counters;
  366. int nr_active;
  367. int is_active;
  368. struct task_struct *task;
  369. /*
  370. * time_now is the current time in nanoseconds since an arbitrary
  371. * point in the past. For per-task counters, this is based on the
  372. * task clock, and for per-cpu counters it is based on the cpu clock.
  373. * time_lost is an offset from the task/cpu clock, used to make it
  374. * appear that time only passes while the context is scheduled in.
  375. */
  376. u64 time_now;
  377. u64 time_lost;
  378. #endif
  379. };
  380. /**
  381. * struct perf_counter_cpu_context - per cpu counter context structure
  382. */
  383. struct perf_cpu_context {
  384. struct perf_counter_context ctx;
  385. struct perf_counter_context *task_ctx;
  386. int active_oncpu;
  387. int max_pertask;
  388. int exclusive;
  389. /*
  390. * Recursion avoidance:
  391. *
  392. * task, softirq, irq, nmi context
  393. */
  394. int recursion[4];
  395. };
  396. /*
  397. * Set by architecture code:
  398. */
  399. extern int perf_max_counters;
  400. #ifdef CONFIG_PERF_COUNTERS
  401. extern const struct hw_perf_counter_ops *
  402. hw_perf_counter_init(struct perf_counter *counter);
  403. extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
  404. extern void perf_counter_task_sched_out(struct task_struct *task, int cpu);
  405. extern void perf_counter_task_tick(struct task_struct *task, int cpu);
  406. extern void perf_counter_init_task(struct task_struct *child);
  407. extern void perf_counter_exit_task(struct task_struct *child);
  408. extern void perf_counter_do_pending(void);
  409. extern void perf_counter_print_debug(void);
  410. extern void perf_counter_unthrottle(void);
  411. extern u64 hw_perf_save_disable(void);
  412. extern void hw_perf_restore(u64 ctrl);
  413. extern int perf_counter_task_disable(void);
  414. extern int perf_counter_task_enable(void);
  415. extern int hw_perf_group_sched_in(struct perf_counter *group_leader,
  416. struct perf_cpu_context *cpuctx,
  417. struct perf_counter_context *ctx, int cpu);
  418. extern void perf_counter_update_userpage(struct perf_counter *counter);
  419. extern void perf_counter_output(struct perf_counter *counter,
  420. int nmi, struct pt_regs *regs);
  421. /*
  422. * Return 1 for a software counter, 0 for a hardware counter
  423. */
  424. static inline int is_software_counter(struct perf_counter *counter)
  425. {
  426. return !perf_event_raw(&counter->hw_event) &&
  427. perf_event_type(&counter->hw_event) != PERF_TYPE_HARDWARE;
  428. }
  429. extern void perf_swcounter_event(u32, u64, int, struct pt_regs *);
  430. extern void perf_counter_mmap(unsigned long addr, unsigned long len,
  431. unsigned long pgoff, struct file *file);
  432. extern void perf_counter_munmap(unsigned long addr, unsigned long len,
  433. unsigned long pgoff, struct file *file);
  434. #else
  435. static inline void
  436. perf_counter_task_sched_in(struct task_struct *task, int cpu) { }
  437. static inline void
  438. perf_counter_task_sched_out(struct task_struct *task, int cpu) { }
  439. static inline void
  440. perf_counter_task_tick(struct task_struct *task, int cpu) { }
  441. static inline void perf_counter_init_task(struct task_struct *child) { }
  442. static inline void perf_counter_exit_task(struct task_struct *child) { }
  443. static inline void perf_counter_do_pending(void) { }
  444. static inline void perf_counter_print_debug(void) { }
  445. static inline void perf_counter_unthrottle(void) { }
  446. static inline void hw_perf_restore(u64 ctrl) { }
  447. static inline u64 hw_perf_save_disable(void) { return 0; }
  448. static inline int perf_counter_task_disable(void) { return -EINVAL; }
  449. static inline int perf_counter_task_enable(void) { return -EINVAL; }
  450. static inline void
  451. perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs) { }
  452. static inline void
  453. perf_counter_mmap(unsigned long addr, unsigned long len,
  454. unsigned long pgoff, struct file *file) { }
  455. static inline void
  456. perf_counter_munmap(unsigned long addr, unsigned long len,
  457. unsigned long pgoff, struct file *file) { }
  458. #endif
  459. #endif /* __KERNEL__ */
  460. #endif /* _LINUX_PERF_COUNTER_H */