perf_counter.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. __reserved_1 : 29;
  72. u64 __reserved_2;
  73. };
  74. /*
  75. * Kernel-internal data types:
  76. */
  77. /**
  78. * struct hw_perf_counter - performance counter hardware details:
  79. */
  80. struct hw_perf_counter {
  81. #ifdef CONFIG_PERF_COUNTERS
  82. u64 config;
  83. unsigned long config_base;
  84. unsigned long counter_base;
  85. int nmi;
  86. unsigned int idx;
  87. atomic64_t prev_count;
  88. u64 irq_period;
  89. atomic64_t period_left;
  90. #endif
  91. };
  92. /*
  93. * Hardcoded buffer length limit for now, for IRQ-fed events:
  94. */
  95. #define PERF_DATA_BUFLEN 2048
  96. /**
  97. * struct perf_data - performance counter IRQ data sampling ...
  98. */
  99. struct perf_data {
  100. int len;
  101. int rd_idx;
  102. int overrun;
  103. u8 data[PERF_DATA_BUFLEN];
  104. };
  105. struct perf_counter;
  106. /**
  107. * struct hw_perf_counter_ops - performance counter hw ops
  108. */
  109. struct hw_perf_counter_ops {
  110. void (*hw_perf_counter_enable) (struct perf_counter *counter);
  111. void (*hw_perf_counter_disable) (struct perf_counter *counter);
  112. void (*hw_perf_counter_read) (struct perf_counter *counter);
  113. };
  114. /**
  115. * enum perf_counter_active_state - the states of a counter
  116. */
  117. enum perf_counter_active_state {
  118. PERF_COUNTER_STATE_OFF = -1,
  119. PERF_COUNTER_STATE_INACTIVE = 0,
  120. PERF_COUNTER_STATE_ACTIVE = 1,
  121. };
  122. /**
  123. * struct perf_counter - performance counter kernel representation:
  124. */
  125. struct perf_counter {
  126. #ifdef CONFIG_PERF_COUNTERS
  127. struct list_head list_entry;
  128. struct list_head sibling_list;
  129. struct perf_counter *group_leader;
  130. const struct hw_perf_counter_ops *hw_ops;
  131. enum perf_counter_active_state state;
  132. atomic64_t count;
  133. struct perf_counter_hw_event hw_event;
  134. struct hw_perf_counter hw;
  135. struct perf_counter_context *ctx;
  136. struct task_struct *task;
  137. /*
  138. * Protect attach/detach:
  139. */
  140. struct mutex mutex;
  141. int oncpu;
  142. int cpu;
  143. /* read() / irq related data */
  144. wait_queue_head_t waitq;
  145. /* optional: for NMIs */
  146. int wakeup_pending;
  147. struct perf_data *irqdata;
  148. struct perf_data *usrdata;
  149. struct perf_data data[2];
  150. #endif
  151. };
  152. /**
  153. * struct perf_counter_context - counter context structure
  154. *
  155. * Used as a container for task counters and CPU counters as well:
  156. */
  157. struct perf_counter_context {
  158. #ifdef CONFIG_PERF_COUNTERS
  159. /*
  160. * Protect the list of counters:
  161. */
  162. spinlock_t lock;
  163. struct list_head counter_list;
  164. int nr_counters;
  165. int nr_active;
  166. struct task_struct *task;
  167. #endif
  168. };
  169. /**
  170. * struct perf_counter_cpu_context - per cpu counter context structure
  171. */
  172. struct perf_cpu_context {
  173. struct perf_counter_context ctx;
  174. struct perf_counter_context *task_ctx;
  175. int active_oncpu;
  176. int max_pertask;
  177. };
  178. /*
  179. * Set by architecture code:
  180. */
  181. extern int perf_max_counters;
  182. #ifdef CONFIG_PERF_COUNTERS
  183. extern const struct hw_perf_counter_ops *
  184. hw_perf_counter_init(struct perf_counter *counter);
  185. extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
  186. extern void perf_counter_task_sched_out(struct task_struct *task, int cpu);
  187. extern void perf_counter_task_tick(struct task_struct *task, int cpu);
  188. extern void perf_counter_init_task(struct task_struct *task);
  189. extern void perf_counter_notify(struct pt_regs *regs);
  190. extern void perf_counter_print_debug(void);
  191. extern u64 hw_perf_save_disable(void);
  192. extern void hw_perf_restore(u64 ctrl);
  193. extern int perf_counter_task_disable(void);
  194. extern int perf_counter_task_enable(void);
  195. #else
  196. static inline void
  197. perf_counter_task_sched_in(struct task_struct *task, int cpu) { }
  198. static inline void
  199. perf_counter_task_sched_out(struct task_struct *task, int cpu) { }
  200. static inline void
  201. perf_counter_task_tick(struct task_struct *task, int cpu) { }
  202. static inline void perf_counter_init_task(struct task_struct *task) { }
  203. static inline void perf_counter_notify(struct pt_regs *regs) { }
  204. static inline void perf_counter_print_debug(void) { }
  205. static inline void hw_perf_restore(u64 ctrl) { }
  206. static inline u64 hw_perf_save_disable(void) { return 0; }
  207. static inline int perf_counter_task_disable(void) { return -EINVAL; }
  208. static inline int perf_counter_task_enable(void) { return -EINVAL; }
  209. #endif
  210. #endif /* _LINUX_PERF_COUNTER_H */