perf_counter.h 7.4 KB

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