perf_counter.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 <linux/hrtimer.h>
  101. #include <asm/atomic.h>
  102. struct task_struct;
  103. /**
  104. * struct hw_perf_counter - performance counter hardware details:
  105. */
  106. struct hw_perf_counter {
  107. #ifdef CONFIG_PERF_COUNTERS
  108. union {
  109. struct { /* hardware */
  110. u64 config;
  111. unsigned long config_base;
  112. unsigned long counter_base;
  113. int nmi;
  114. unsigned int idx;
  115. };
  116. union { /* software */
  117. atomic64_t count;
  118. struct hrtimer hrtimer;
  119. };
  120. };
  121. atomic64_t prev_count;
  122. u64 irq_period;
  123. atomic64_t period_left;
  124. #endif
  125. };
  126. /*
  127. * Hardcoded buffer length limit for now, for IRQ-fed events:
  128. */
  129. #define PERF_DATA_BUFLEN 2048
  130. /**
  131. * struct perf_data - performance counter IRQ data sampling ...
  132. */
  133. struct perf_data {
  134. int len;
  135. int rd_idx;
  136. int overrun;
  137. u8 data[PERF_DATA_BUFLEN];
  138. };
  139. struct perf_counter;
  140. /**
  141. * struct hw_perf_counter_ops - performance counter hw ops
  142. */
  143. struct hw_perf_counter_ops {
  144. int (*enable) (struct perf_counter *counter);
  145. void (*disable) (struct perf_counter *counter);
  146. void (*read) (struct perf_counter *counter);
  147. };
  148. /**
  149. * enum perf_counter_active_state - the states of a counter
  150. */
  151. enum perf_counter_active_state {
  152. PERF_COUNTER_STATE_ERROR = -2,
  153. PERF_COUNTER_STATE_OFF = -1,
  154. PERF_COUNTER_STATE_INACTIVE = 0,
  155. PERF_COUNTER_STATE_ACTIVE = 1,
  156. };
  157. struct file;
  158. /**
  159. * struct perf_counter - performance counter kernel representation:
  160. */
  161. struct perf_counter {
  162. #ifdef CONFIG_PERF_COUNTERS
  163. struct list_head list_entry;
  164. struct list_head event_entry;
  165. struct list_head sibling_list;
  166. struct perf_counter *group_leader;
  167. const struct hw_perf_counter_ops *hw_ops;
  168. enum perf_counter_active_state state;
  169. enum perf_counter_active_state prev_state;
  170. atomic64_t count;
  171. struct perf_counter_hw_event hw_event;
  172. struct hw_perf_counter hw;
  173. struct perf_counter_context *ctx;
  174. struct task_struct *task;
  175. struct file *filp;
  176. struct perf_counter *parent;
  177. struct list_head child_list;
  178. /*
  179. * Protect attach/detach and child_list:
  180. */
  181. struct mutex mutex;
  182. int oncpu;
  183. int cpu;
  184. /* read() / irq related data */
  185. wait_queue_head_t waitq;
  186. /* optional: for NMIs */
  187. int wakeup_pending;
  188. struct perf_data *irqdata;
  189. struct perf_data *usrdata;
  190. struct perf_data data[2];
  191. struct rcu_head rcu_head;
  192. #endif
  193. };
  194. /**
  195. * struct perf_counter_context - counter context structure
  196. *
  197. * Used as a container for task counters and CPU counters as well:
  198. */
  199. struct perf_counter_context {
  200. #ifdef CONFIG_PERF_COUNTERS
  201. /*
  202. * Protect the states of the counters in the list,
  203. * nr_active, and the list:
  204. */
  205. spinlock_t lock;
  206. /*
  207. * Protect the list of counters. Locking either mutex or lock
  208. * is sufficient to ensure the list doesn't change; to change
  209. * the list you need to lock both the mutex and the spinlock.
  210. */
  211. struct mutex mutex;
  212. struct list_head counter_list;
  213. struct list_head event_list;
  214. int nr_counters;
  215. int nr_active;
  216. int is_active;
  217. struct task_struct *task;
  218. #endif
  219. };
  220. /**
  221. * struct perf_counter_cpu_context - per cpu counter context structure
  222. */
  223. struct perf_cpu_context {
  224. struct perf_counter_context ctx;
  225. struct perf_counter_context *task_ctx;
  226. int active_oncpu;
  227. int max_pertask;
  228. int exclusive;
  229. };
  230. /*
  231. * Set by architecture code:
  232. */
  233. extern int perf_max_counters;
  234. #ifdef CONFIG_PERF_COUNTERS
  235. extern const struct hw_perf_counter_ops *
  236. hw_perf_counter_init(struct perf_counter *counter);
  237. extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
  238. extern void perf_counter_task_sched_out(struct task_struct *task, int cpu);
  239. extern void perf_counter_task_tick(struct task_struct *task, int cpu);
  240. extern void perf_counter_init_task(struct task_struct *child);
  241. extern void perf_counter_exit_task(struct task_struct *child);
  242. extern void perf_counter_notify(struct pt_regs *regs);
  243. extern void perf_counter_print_debug(void);
  244. extern void perf_counter_unthrottle(void);
  245. extern u64 hw_perf_save_disable(void);
  246. extern void hw_perf_restore(u64 ctrl);
  247. extern int perf_counter_task_disable(void);
  248. extern int perf_counter_task_enable(void);
  249. extern int hw_perf_group_sched_in(struct perf_counter *group_leader,
  250. struct perf_cpu_context *cpuctx,
  251. struct perf_counter_context *ctx, int cpu);
  252. /*
  253. * Return 1 for a software counter, 0 for a hardware counter
  254. */
  255. static inline int is_software_counter(struct perf_counter *counter)
  256. {
  257. return !counter->hw_event.raw && counter->hw_event.type < 0;
  258. }
  259. extern void perf_swcounter_event(enum hw_event_types, u64, int, struct pt_regs *);
  260. #else
  261. static inline void
  262. perf_counter_task_sched_in(struct task_struct *task, int cpu) { }
  263. static inline void
  264. perf_counter_task_sched_out(struct task_struct *task, int cpu) { }
  265. static inline void
  266. perf_counter_task_tick(struct task_struct *task, int cpu) { }
  267. static inline void perf_counter_init_task(struct task_struct *child) { }
  268. static inline void perf_counter_exit_task(struct task_struct *child) { }
  269. static inline void perf_counter_notify(struct pt_regs *regs) { }
  270. static inline void perf_counter_print_debug(void) { }
  271. static inline void perf_counter_unthrottle(void) { }
  272. static inline void hw_perf_restore(u64 ctrl) { }
  273. static inline u64 hw_perf_save_disable(void) { return 0; }
  274. static inline int perf_counter_task_disable(void) { return -EINVAL; }
  275. static inline int perf_counter_task_enable(void) { return -EINVAL; }
  276. static inline void perf_swcounter_event(enum hw_event_types event, u64 nr,
  277. int nmi, struct pt_regs *regs) { }
  278. #endif
  279. #endif /* __KERNEL__ */
  280. #endif /* _LINUX_PERF_COUNTER_H */