perf_counter.h 6.2 KB

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