perf_counter.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. /*
  18. * User-space ABI bits:
  19. */
  20. /*
  21. * Generalized performance counter event types, used by the hw_event.type
  22. * parameter of the sys_perf_counter_open() syscall:
  23. */
  24. enum hw_event_types {
  25. /*
  26. * Common hardware events, generalized by the kernel:
  27. */
  28. PERF_COUNT_CPU_CYCLES = 0,
  29. PERF_COUNT_INSTRUCTIONS = 1,
  30. PERF_COUNT_CACHE_REFERENCES = 2,
  31. PERF_COUNT_CACHE_MISSES = 3,
  32. PERF_COUNT_BRANCH_INSTRUCTIONS = 4,
  33. PERF_COUNT_BRANCH_MISSES = 5,
  34. PERF_COUNT_BUS_CYCLES = 6,
  35. PERF_HW_EVENTS_MAX = 7,
  36. /*
  37. * Special "software" counters provided by the kernel, even if
  38. * the hardware does not support performance counters. These
  39. * counters measure various physical and sw events of the
  40. * kernel (and allow the profiling of them as well):
  41. */
  42. PERF_COUNT_CPU_CLOCK = -1,
  43. PERF_COUNT_TASK_CLOCK = -2,
  44. PERF_COUNT_PAGE_FAULTS = -3,
  45. PERF_COUNT_CONTEXT_SWITCHES = -4,
  46. PERF_COUNT_CPU_MIGRATIONS = -5,
  47. PERF_SW_EVENTS_MIN = -6,
  48. };
  49. /*
  50. * IRQ-notification data record type:
  51. */
  52. enum perf_counter_record_type {
  53. PERF_RECORD_SIMPLE = 0,
  54. PERF_RECORD_IRQ = 1,
  55. PERF_RECORD_GROUP = 2,
  56. };
  57. /*
  58. * Hardware event to monitor via a performance monitoring counter:
  59. */
  60. struct perf_counter_hw_event {
  61. __s64 type;
  62. __u64 irq_period;
  63. __u32 record_type;
  64. __u32 disabled : 1, /* off by default */
  65. nmi : 1, /* NMI sampling */
  66. raw : 1, /* raw event type */
  67. inherit : 1, /* children inherit it */
  68. pinned : 1, /* must always be on PMU */
  69. exclusive : 1, /* only group on PMU */
  70. exclude_user : 1, /* don't count user */
  71. exclude_kernel : 1, /* ditto kernel */
  72. exclude_hv : 1, /* ditto hypervisor */
  73. __reserved_1 : 23;
  74. __u64 __reserved_2;
  75. };
  76. /*
  77. * Ioctls that can be done on a perf counter fd:
  78. */
  79. #define PERF_COUNTER_IOC_ENABLE _IO('$', 0)
  80. #define PERF_COUNTER_IOC_DISABLE _IO('$', 1)
  81. #ifdef __KERNEL__
  82. /*
  83. * Kernel-internal data types and definitions:
  84. */
  85. #ifdef CONFIG_PERF_COUNTERS
  86. # include <asm/perf_counter.h>
  87. #endif
  88. #include <linux/list.h>
  89. #include <linux/mutex.h>
  90. #include <linux/rculist.h>
  91. #include <linux/rcupdate.h>
  92. #include <linux/spinlock.h>
  93. #include <asm/atomic.h>
  94. struct task_struct;
  95. /**
  96. * struct hw_perf_counter - performance counter hardware details:
  97. */
  98. struct hw_perf_counter {
  99. #ifdef CONFIG_PERF_COUNTERS
  100. u64 config;
  101. unsigned long config_base;
  102. unsigned long counter_base;
  103. int nmi;
  104. unsigned int idx;
  105. atomic64_t prev_count;
  106. u64 irq_period;
  107. atomic64_t period_left;
  108. #endif
  109. };
  110. /*
  111. * Hardcoded buffer length limit for now, for IRQ-fed events:
  112. */
  113. #define PERF_DATA_BUFLEN 2048
  114. /**
  115. * struct perf_data - performance counter IRQ data sampling ...
  116. */
  117. struct perf_data {
  118. int len;
  119. int rd_idx;
  120. int overrun;
  121. u8 data[PERF_DATA_BUFLEN];
  122. };
  123. struct perf_counter;
  124. /**
  125. * struct hw_perf_counter_ops - performance counter hw ops
  126. */
  127. struct hw_perf_counter_ops {
  128. int (*enable) (struct perf_counter *counter);
  129. void (*disable) (struct perf_counter *counter);
  130. void (*read) (struct perf_counter *counter);
  131. };
  132. /**
  133. * enum perf_counter_active_state - the states of a counter
  134. */
  135. enum perf_counter_active_state {
  136. PERF_COUNTER_STATE_ERROR = -2,
  137. PERF_COUNTER_STATE_OFF = -1,
  138. PERF_COUNTER_STATE_INACTIVE = 0,
  139. PERF_COUNTER_STATE_ACTIVE = 1,
  140. };
  141. struct file;
  142. /**
  143. * struct perf_counter - performance counter kernel representation:
  144. */
  145. struct perf_counter {
  146. #ifdef CONFIG_PERF_COUNTERS
  147. struct list_head list_entry;
  148. struct list_head sibling_list;
  149. struct perf_counter *group_leader;
  150. const struct hw_perf_counter_ops *hw_ops;
  151. enum perf_counter_active_state state;
  152. enum perf_counter_active_state prev_state;
  153. atomic64_t count;
  154. struct perf_counter_hw_event hw_event;
  155. struct hw_perf_counter hw;
  156. struct perf_counter_context *ctx;
  157. struct task_struct *task;
  158. struct file *filp;
  159. struct perf_counter *parent;
  160. struct list_head child_list;
  161. /*
  162. * Protect attach/detach and child_list:
  163. */
  164. struct mutex mutex;
  165. int oncpu;
  166. int cpu;
  167. /* read() / irq related data */
  168. wait_queue_head_t waitq;
  169. /* optional: for NMIs */
  170. int wakeup_pending;
  171. struct perf_data *irqdata;
  172. struct perf_data *usrdata;
  173. struct perf_data data[2];
  174. #endif
  175. };
  176. /**
  177. * struct perf_counter_context - counter context structure
  178. *
  179. * Used as a container for task counters and CPU counters as well:
  180. */
  181. struct perf_counter_context {
  182. #ifdef CONFIG_PERF_COUNTERS
  183. /*
  184. * Protect the states of the counters in the list,
  185. * nr_active, and the list:
  186. */
  187. spinlock_t lock;
  188. /*
  189. * Protect the list of counters. Locking either mutex or lock
  190. * is sufficient to ensure the list doesn't change; to change
  191. * the list you need to lock both the mutex and the spinlock.
  192. */
  193. struct mutex mutex;
  194. struct list_head counter_list;
  195. int nr_counters;
  196. int nr_active;
  197. int is_active;
  198. struct task_struct *task;
  199. #endif
  200. };
  201. /**
  202. * struct perf_counter_cpu_context - per cpu counter context structure
  203. */
  204. struct perf_cpu_context {
  205. struct perf_counter_context ctx;
  206. struct perf_counter_context *task_ctx;
  207. int active_oncpu;
  208. int max_pertask;
  209. int exclusive;
  210. };
  211. /*
  212. * Set by architecture code:
  213. */
  214. extern int perf_max_counters;
  215. #ifdef CONFIG_PERF_COUNTERS
  216. extern const struct hw_perf_counter_ops *
  217. hw_perf_counter_init(struct perf_counter *counter);
  218. extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
  219. extern void perf_counter_task_sched_out(struct task_struct *task, int cpu);
  220. extern void perf_counter_task_tick(struct task_struct *task, int cpu);
  221. extern void perf_counter_init_task(struct task_struct *child);
  222. extern void perf_counter_exit_task(struct task_struct *child);
  223. extern void perf_counter_notify(struct pt_regs *regs);
  224. extern void perf_counter_print_debug(void);
  225. extern void perf_counter_unthrottle(void);
  226. extern u64 hw_perf_save_disable(void);
  227. extern void hw_perf_restore(u64 ctrl);
  228. extern int perf_counter_task_disable(void);
  229. extern int perf_counter_task_enable(void);
  230. extern int hw_perf_group_sched_in(struct perf_counter *group_leader,
  231. struct perf_cpu_context *cpuctx,
  232. struct perf_counter_context *ctx, int cpu);
  233. /*
  234. * Return 1 for a software counter, 0 for a hardware counter
  235. */
  236. static inline int is_software_counter(struct perf_counter *counter)
  237. {
  238. return !counter->hw_event.raw && counter->hw_event.type < 0;
  239. }
  240. #else
  241. static inline void
  242. perf_counter_task_sched_in(struct task_struct *task, int cpu) { }
  243. static inline void
  244. perf_counter_task_sched_out(struct task_struct *task, int cpu) { }
  245. static inline void
  246. perf_counter_task_tick(struct task_struct *task, int cpu) { }
  247. static inline void perf_counter_init_task(struct task_struct *child) { }
  248. static inline void perf_counter_exit_task(struct task_struct *child) { }
  249. static inline void perf_counter_notify(struct pt_regs *regs) { }
  250. static inline void perf_counter_print_debug(void) { }
  251. static inline void perf_counter_unthrottle(void) { }
  252. static inline void hw_perf_restore(u64 ctrl) { }
  253. static inline u64 hw_perf_save_disable(void) { return 0; }
  254. static inline int perf_counter_task_disable(void) { return -EINVAL; }
  255. static inline int perf_counter_task_enable(void) { return -EINVAL; }
  256. #endif
  257. #endif /* __KERNEL__ */
  258. #endif /* _LINUX_PERF_COUNTER_H */