perf_counter.h 8.9 KB

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