perf_counter.h 6.3 KB

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