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_CPU_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_COUNT_BUS_CYCLES = 6,
  41. PERF_HW_EVENTS_MAX = 7,
  42. /*
  43. * Special "software" counters provided by the kernel, even if
  44. * the hardware does not support performance counters. These
  45. * counters measure various physical and sw events of the
  46. * kernel (and allow the profiling of them as well):
  47. */
  48. PERF_COUNT_CPU_CLOCK = -1,
  49. PERF_COUNT_TASK_CLOCK = -2,
  50. PERF_COUNT_PAGE_FAULTS = -3,
  51. PERF_COUNT_CONTEXT_SWITCHES = -4,
  52. PERF_COUNT_CPU_MIGRATIONS = -5,
  53. PERF_SW_EVENTS_MIN = -6,
  54. };
  55. /*
  56. * IRQ-notification data record type:
  57. */
  58. enum perf_counter_record_type {
  59. PERF_RECORD_SIMPLE = 0,
  60. PERF_RECORD_IRQ = 1,
  61. PERF_RECORD_GROUP = 2,
  62. };
  63. /*
  64. * Hardware event to monitor via a performance monitoring counter:
  65. */
  66. struct perf_counter_hw_event {
  67. s64 type;
  68. u64 irq_period;
  69. u32 record_type;
  70. u32 disabled : 1, /* off by default */
  71. nmi : 1, /* NMI sampling */
  72. raw : 1, /* raw event type */
  73. inherit : 1, /* children inherit it */
  74. __reserved_1 : 28;
  75. u64 __reserved_2;
  76. };
  77. /*
  78. * Kernel-internal data types:
  79. */
  80. /**
  81. * struct hw_perf_counter - performance counter hardware details:
  82. */
  83. struct hw_perf_counter {
  84. #ifdef CONFIG_PERF_COUNTERS
  85. u64 config;
  86. unsigned long config_base;
  87. unsigned long counter_base;
  88. int nmi;
  89. unsigned int idx;
  90. atomic64_t prev_count;
  91. u64 irq_period;
  92. atomic64_t period_left;
  93. #endif
  94. };
  95. /*
  96. * Hardcoded buffer length limit for now, for IRQ-fed events:
  97. */
  98. #define PERF_DATA_BUFLEN 2048
  99. /**
  100. * struct perf_data - performance counter IRQ data sampling ...
  101. */
  102. struct perf_data {
  103. int len;
  104. int rd_idx;
  105. int overrun;
  106. u8 data[PERF_DATA_BUFLEN];
  107. };
  108. struct perf_counter;
  109. /**
  110. * struct hw_perf_counter_ops - performance counter hw ops
  111. */
  112. struct hw_perf_counter_ops {
  113. int (*enable) (struct perf_counter *counter);
  114. void (*disable) (struct perf_counter *counter);
  115. void (*read) (struct perf_counter *counter);
  116. };
  117. /**
  118. * enum perf_counter_active_state - the states of a counter
  119. */
  120. enum perf_counter_active_state {
  121. PERF_COUNTER_STATE_OFF = -1,
  122. PERF_COUNTER_STATE_INACTIVE = 0,
  123. PERF_COUNTER_STATE_ACTIVE = 1,
  124. };
  125. struct file;
  126. /**
  127. * struct perf_counter - performance counter kernel representation:
  128. */
  129. struct perf_counter {
  130. #ifdef CONFIG_PERF_COUNTERS
  131. struct list_head list_entry;
  132. struct list_head sibling_list;
  133. struct perf_counter *group_leader;
  134. const struct hw_perf_counter_ops *hw_ops;
  135. enum perf_counter_active_state state;
  136. atomic64_t count;
  137. struct perf_counter_hw_event hw_event;
  138. struct hw_perf_counter hw;
  139. struct perf_counter_context *ctx;
  140. struct task_struct *task;
  141. struct file *filp;
  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 */