perf_counter.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 <linux/types.h>
  16. #include <linux/ioctl.h>
  17. /*
  18. * User-space ABI bits:
  19. */
  20. /*
  21. * Generalized performance counter event types, used by the hw_event.type
  22. * parameter of the sys_perf_counter_open() syscall:
  23. */
  24. enum hw_event_types {
  25. /*
  26. * Common hardware events, generalized by the kernel:
  27. */
  28. PERF_COUNT_CPU_CYCLES = 0,
  29. PERF_COUNT_INSTRUCTIONS = 1,
  30. PERF_COUNT_CACHE_REFERENCES = 2,
  31. PERF_COUNT_CACHE_MISSES = 3,
  32. PERF_COUNT_BRANCH_INSTRUCTIONS = 4,
  33. PERF_COUNT_BRANCH_MISSES = 5,
  34. PERF_COUNT_BUS_CYCLES = 6,
  35. PERF_HW_EVENTS_MAX = 7,
  36. /*
  37. * Special "software" counters provided by the kernel, even if
  38. * the hardware does not support performance counters. These
  39. * counters measure various physical and sw events of the
  40. * kernel (and allow the profiling of them as well):
  41. */
  42. PERF_COUNT_CPU_CLOCK = -1,
  43. PERF_COUNT_TASK_CLOCK = -2,
  44. PERF_COUNT_PAGE_FAULTS = -3,
  45. PERF_COUNT_CONTEXT_SWITCHES = -4,
  46. PERF_COUNT_CPU_MIGRATIONS = -5,
  47. PERF_COUNT_PAGE_FAULTS_MIN = -6,
  48. PERF_COUNT_PAGE_FAULTS_MAJ = -7,
  49. PERF_SW_EVENTS_MIN = -8,
  50. };
  51. /*
  52. * IRQ-notification data record type:
  53. */
  54. enum perf_counter_record_type {
  55. PERF_RECORD_SIMPLE = 0,
  56. PERF_RECORD_IRQ = 1,
  57. PERF_RECORD_GROUP = 2,
  58. };
  59. /*
  60. * Hardware event to monitor via a performance monitoring counter:
  61. */
  62. struct perf_counter_hw_event {
  63. __s64 type;
  64. __u64 irq_period;
  65. __u64 record_type;
  66. __u64 read_format;
  67. __u64 disabled : 1, /* off by default */
  68. nmi : 1, /* NMI sampling */
  69. raw : 1, /* raw event type */
  70. inherit : 1, /* children inherit it */
  71. pinned : 1, /* must always be on PMU */
  72. exclusive : 1, /* only group on PMU */
  73. exclude_user : 1, /* don't count user */
  74. exclude_kernel : 1, /* ditto kernel */
  75. exclude_hv : 1, /* ditto hypervisor */
  76. exclude_idle : 1, /* don't count when idle */
  77. __reserved_1 : 54;
  78. __u32 extra_config_len;
  79. __u32 __reserved_4;
  80. __u64 __reserved_2;
  81. __u64 __reserved_3;
  82. };
  83. /*
  84. * Ioctls that can be done on a perf counter fd:
  85. */
  86. #define PERF_COUNTER_IOC_ENABLE _IO('$', 0)
  87. #define PERF_COUNTER_IOC_DISABLE _IO('$', 1)
  88. #ifdef __KERNEL__
  89. /*
  90. * Kernel-internal data types and definitions:
  91. */
  92. #ifdef CONFIG_PERF_COUNTERS
  93. # include <asm/perf_counter.h>
  94. #endif
  95. #include <linux/list.h>
  96. #include <linux/mutex.h>
  97. #include <linux/rculist.h>
  98. #include <linux/rcupdate.h>
  99. #include <linux/spinlock.h>
  100. #include <asm/atomic.h>
  101. struct task_struct;
  102. /**
  103. * struct hw_perf_counter - performance counter hardware details:
  104. */
  105. struct hw_perf_counter {
  106. #ifdef CONFIG_PERF_COUNTERS
  107. u64 config;
  108. unsigned long config_base;
  109. unsigned long counter_base;
  110. int nmi;
  111. unsigned int idx;
  112. atomic64_t count; /* software */
  113. atomic64_t prev_count;
  114. u64 irq_period;
  115. atomic64_t period_left;
  116. #endif
  117. };
  118. /*
  119. * Hardcoded buffer length limit for now, for IRQ-fed events:
  120. */
  121. #define PERF_DATA_BUFLEN 2048
  122. /**
  123. * struct perf_data - performance counter IRQ data sampling ...
  124. */
  125. struct perf_data {
  126. int len;
  127. int rd_idx;
  128. int overrun;
  129. u8 data[PERF_DATA_BUFLEN];
  130. };
  131. struct perf_counter;
  132. /**
  133. * struct hw_perf_counter_ops - performance counter hw ops
  134. */
  135. struct hw_perf_counter_ops {
  136. int (*enable) (struct perf_counter *counter);
  137. void (*disable) (struct perf_counter *counter);
  138. void (*read) (struct perf_counter *counter);
  139. };
  140. /**
  141. * enum perf_counter_active_state - the states of a counter
  142. */
  143. enum perf_counter_active_state {
  144. PERF_COUNTER_STATE_ERROR = -2,
  145. PERF_COUNTER_STATE_OFF = -1,
  146. PERF_COUNTER_STATE_INACTIVE = 0,
  147. PERF_COUNTER_STATE_ACTIVE = 1,
  148. };
  149. struct file;
  150. /**
  151. * struct perf_counter - performance counter kernel representation:
  152. */
  153. struct perf_counter {
  154. #ifdef CONFIG_PERF_COUNTERS
  155. struct list_head list_entry;
  156. struct list_head sibling_list;
  157. struct perf_counter *group_leader;
  158. const struct hw_perf_counter_ops *hw_ops;
  159. enum perf_counter_active_state state;
  160. enum perf_counter_active_state prev_state;
  161. atomic64_t count;
  162. struct perf_counter_hw_event hw_event;
  163. struct hw_perf_counter hw;
  164. struct perf_counter_context *ctx;
  165. struct task_struct *task;
  166. struct file *filp;
  167. struct perf_counter *parent;
  168. struct list_head child_list;
  169. /*
  170. * Protect attach/detach and child_list:
  171. */
  172. struct mutex mutex;
  173. int oncpu;
  174. int cpu;
  175. /* read() / irq related data */
  176. wait_queue_head_t waitq;
  177. /* optional: for NMIs */
  178. int wakeup_pending;
  179. struct perf_data *irqdata;
  180. struct perf_data *usrdata;
  181. struct perf_data data[2];
  182. #endif
  183. };
  184. /**
  185. * struct perf_counter_context - counter context structure
  186. *
  187. * Used as a container for task counters and CPU counters as well:
  188. */
  189. struct perf_counter_context {
  190. #ifdef CONFIG_PERF_COUNTERS
  191. /*
  192. * Protect the states of the counters in the list,
  193. * nr_active, and the list:
  194. */
  195. spinlock_t lock;
  196. /*
  197. * Protect the list of counters. Locking either mutex or lock
  198. * is sufficient to ensure the list doesn't change; to change
  199. * the list you need to lock both the mutex and the spinlock.
  200. */
  201. struct mutex mutex;
  202. struct list_head counter_list;
  203. int nr_counters;
  204. int nr_active;
  205. int is_active;
  206. struct task_struct *task;
  207. #endif
  208. };
  209. /**
  210. * struct perf_counter_cpu_context - per cpu counter context structure
  211. */
  212. struct perf_cpu_context {
  213. struct perf_counter_context ctx;
  214. struct perf_counter_context *task_ctx;
  215. int active_oncpu;
  216. int max_pertask;
  217. int exclusive;
  218. };
  219. /*
  220. * Set by architecture code:
  221. */
  222. extern int perf_max_counters;
  223. #ifdef CONFIG_PERF_COUNTERS
  224. extern const struct hw_perf_counter_ops *
  225. hw_perf_counter_init(struct perf_counter *counter);
  226. extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
  227. extern void perf_counter_task_sched_out(struct task_struct *task, int cpu);
  228. extern void perf_counter_task_tick(struct task_struct *task, int cpu);
  229. extern void perf_counter_init_task(struct task_struct *child);
  230. extern void perf_counter_exit_task(struct task_struct *child);
  231. extern void perf_counter_notify(struct pt_regs *regs);
  232. extern void perf_counter_print_debug(void);
  233. extern void perf_counter_unthrottle(void);
  234. extern u64 hw_perf_save_disable(void);
  235. extern void hw_perf_restore(u64 ctrl);
  236. extern int perf_counter_task_disable(void);
  237. extern int perf_counter_task_enable(void);
  238. extern int hw_perf_group_sched_in(struct perf_counter *group_leader,
  239. struct perf_cpu_context *cpuctx,
  240. struct perf_counter_context *ctx, int cpu);
  241. /*
  242. * Return 1 for a software counter, 0 for a hardware counter
  243. */
  244. static inline int is_software_counter(struct perf_counter *counter)
  245. {
  246. return !counter->hw_event.raw && counter->hw_event.type < 0;
  247. }
  248. extern void perf_swcounter_event(enum hw_event_types, u64, int, struct pt_regs *);
  249. #else
  250. static inline void
  251. perf_counter_task_sched_in(struct task_struct *task, int cpu) { }
  252. static inline void
  253. perf_counter_task_sched_out(struct task_struct *task, int cpu) { }
  254. static inline void
  255. perf_counter_task_tick(struct task_struct *task, int cpu) { }
  256. static inline void perf_counter_init_task(struct task_struct *child) { }
  257. static inline void perf_counter_exit_task(struct task_struct *child) { }
  258. static inline void perf_counter_notify(struct pt_regs *regs) { }
  259. static inline void perf_counter_print_debug(void) { }
  260. static inline void perf_counter_unthrottle(void) { }
  261. static inline void hw_perf_restore(u64 ctrl) { }
  262. static inline u64 hw_perf_save_disable(void) { return 0; }
  263. static inline int perf_counter_task_disable(void) { return -EINVAL; }
  264. static inline int perf_counter_task_enable(void) { return -EINVAL; }
  265. static inline void perf_swcounter_event(enum hw_event_types event, u64 nr,
  266. int nmi, struct pt_regs *regs) { }
  267. #endif
  268. #endif /* __KERNEL__ */
  269. #endif /* _LINUX_PERF_COUNTER_H */