perf_counter.h 7.9 KB

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