perf_counter.h 6.1 KB

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