perf_counter.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. PERF_COUNT_PAGE_FAULTS = -3,
  48. PERF_COUNT_CONTEXT_SWITCHES = -4,
  49. };
  50. /*
  51. * IRQ-notification data record type:
  52. */
  53. enum perf_counter_record_type {
  54. PERF_RECORD_SIMPLE = 0,
  55. PERF_RECORD_IRQ = 1,
  56. PERF_RECORD_GROUP = 2,
  57. };
  58. /*
  59. * Hardware event to monitor via a performance monitoring counter:
  60. */
  61. struct perf_counter_hw_event {
  62. s64 type;
  63. u64 irq_period;
  64. u32 record_type;
  65. u32 disabled : 1, /* off by default */
  66. nmi : 1, /* NMI sampling */
  67. raw : 1, /* raw event type */
  68. __reserved_1 : 29;
  69. u64 __reserved_2;
  70. };
  71. /*
  72. * Kernel-internal data types:
  73. */
  74. /**
  75. * struct hw_perf_counter - performance counter hardware details:
  76. */
  77. struct hw_perf_counter {
  78. u64 config;
  79. unsigned long config_base;
  80. unsigned long counter_base;
  81. int nmi;
  82. unsigned int idx;
  83. u64 prev_count;
  84. u64 irq_period;
  85. s32 next_count;
  86. };
  87. /*
  88. * Hardcoded buffer length limit for now, for IRQ-fed events:
  89. */
  90. #define PERF_DATA_BUFLEN 2048
  91. /**
  92. * struct perf_data - performance counter IRQ data sampling ...
  93. */
  94. struct perf_data {
  95. int len;
  96. int rd_idx;
  97. int overrun;
  98. u8 data[PERF_DATA_BUFLEN];
  99. };
  100. struct perf_counter;
  101. /**
  102. * struct hw_perf_counter_ops - performance counter hw ops
  103. */
  104. struct hw_perf_counter_ops {
  105. void (*hw_perf_counter_enable) (struct perf_counter *counter);
  106. void (*hw_perf_counter_disable) (struct perf_counter *counter);
  107. void (*hw_perf_counter_read) (struct perf_counter *counter);
  108. };
  109. /**
  110. * struct perf_counter - performance counter kernel representation:
  111. */
  112. struct perf_counter {
  113. struct list_head list_entry;
  114. struct list_head sibling_list;
  115. struct perf_counter *group_leader;
  116. const struct hw_perf_counter_ops *hw_ops;
  117. int active;
  118. #if BITS_PER_LONG == 64
  119. atomic64_t count;
  120. #else
  121. atomic_t count32[2];
  122. #endif
  123. struct perf_counter_hw_event hw_event;
  124. struct hw_perf_counter hw;
  125. struct perf_counter_context *ctx;
  126. struct task_struct *task;
  127. /*
  128. * Protect attach/detach:
  129. */
  130. struct mutex mutex;
  131. int oncpu;
  132. int cpu;
  133. /* read() / irq related data */
  134. wait_queue_head_t waitq;
  135. /* optional: for NMIs */
  136. int wakeup_pending;
  137. struct perf_data *irqdata;
  138. struct perf_data *usrdata;
  139. struct perf_data data[2];
  140. };
  141. /**
  142. * struct perf_counter_context - counter context structure
  143. *
  144. * Used as a container for task counters and CPU counters as well:
  145. */
  146. struct perf_counter_context {
  147. #ifdef CONFIG_PERF_COUNTERS
  148. /*
  149. * Protect the list of counters:
  150. */
  151. spinlock_t lock;
  152. struct list_head counter_list;
  153. int nr_counters;
  154. int nr_active;
  155. struct task_struct *task;
  156. #endif
  157. };
  158. /**
  159. * struct perf_counter_cpu_context - per cpu counter context structure
  160. */
  161. struct perf_cpu_context {
  162. struct perf_counter_context ctx;
  163. struct perf_counter_context *task_ctx;
  164. int active_oncpu;
  165. int max_pertask;
  166. };
  167. /*
  168. * Set by architecture code:
  169. */
  170. extern int perf_max_counters;
  171. #ifdef CONFIG_PERF_COUNTERS
  172. extern const struct hw_perf_counter_ops *
  173. hw_perf_counter_init(struct perf_counter *counter);
  174. extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
  175. extern void perf_counter_task_sched_out(struct task_struct *task, int cpu);
  176. extern void perf_counter_task_tick(struct task_struct *task, int cpu);
  177. extern void perf_counter_init_task(struct task_struct *task);
  178. extern void perf_counter_notify(struct pt_regs *regs);
  179. extern void perf_counter_print_debug(void);
  180. extern u64 hw_perf_save_disable(void);
  181. extern void hw_perf_restore(u64 ctrl);
  182. extern void atomic64_counter_set(struct perf_counter *counter, u64 val64);
  183. extern u64 atomic64_counter_read(struct perf_counter *counter);
  184. #else
  185. static inline void
  186. perf_counter_task_sched_in(struct task_struct *task, int cpu) { }
  187. static inline void
  188. perf_counter_task_sched_out(struct task_struct *task, int cpu) { }
  189. static inline void
  190. perf_counter_task_tick(struct task_struct *task, int cpu) { }
  191. static inline void perf_counter_init_task(struct task_struct *task) { }
  192. static inline void perf_counter_notify(struct pt_regs *regs) { }
  193. static inline void perf_counter_print_debug(void) { }
  194. static inline void hw_perf_restore(u64 ctrl) { }
  195. static inline u64 hw_perf_save_disable(void) { return 0; }
  196. #endif
  197. #endif /* _LINUX_PERF_COUNTER_H */