perf_counter.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. #define __PERF_COUNTER_MASK(name) \
  75. (((1ULL << PERF_COUNTER_##name##_BITS) - 1) << \
  76. PERF_COUNTER_##name##_SHIFT)
  77. #define PERF_COUNTER_RAW_BITS 1
  78. #define PERF_COUNTER_RAW_SHIFT 63
  79. #define PERF_COUNTER_RAW_MASK __PERF_COUNTER_MASK(RAW)
  80. #define PERF_COUNTER_CONFIG_BITS 63
  81. #define PERF_COUNTER_CONFIG_SHIFT 0
  82. #define PERF_COUNTER_CONFIG_MASK __PERF_COUNTER_MASK(CONFIG)
  83. #define PERF_COUNTER_TYPE_BITS 7
  84. #define PERF_COUNTER_TYPE_SHIFT 56
  85. #define PERF_COUNTER_TYPE_MASK __PERF_COUNTER_MASK(TYPE)
  86. #define PERF_COUNTER_EVENT_BITS 56
  87. #define PERF_COUNTER_EVENT_SHIFT 0
  88. #define PERF_COUNTER_EVENT_MASK __PERF_COUNTER_MASK(EVENT)
  89. /*
  90. * Hardware event to monitor via a performance monitoring counter:
  91. */
  92. struct perf_counter_hw_event {
  93. /*
  94. * The MSB of the config word signifies if the rest contains cpu
  95. * specific (raw) counter configuration data, if unset, the next
  96. * 7 bits are an event type and the rest of the bits are the event
  97. * identifier.
  98. */
  99. __u64 config;
  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. /*
  124. * Structure of the page that can be mapped via mmap
  125. */
  126. struct perf_counter_mmap_page {
  127. __u32 version; /* version number of this structure */
  128. __u32 compat_version; /* lowest version this is compat with */
  129. __u32 lock; /* seqlock for synchronization */
  130. __u32 index; /* hardware counter identifier */
  131. __s64 offset; /* add to hardware counter value */
  132. };
  133. #ifdef __KERNEL__
  134. /*
  135. * Kernel-internal data types and definitions:
  136. */
  137. #ifdef CONFIG_PERF_COUNTERS
  138. # include <asm/perf_counter.h>
  139. #endif
  140. #include <linux/list.h>
  141. #include <linux/mutex.h>
  142. #include <linux/rculist.h>
  143. #include <linux/rcupdate.h>
  144. #include <linux/spinlock.h>
  145. #include <linux/hrtimer.h>
  146. #include <asm/atomic.h>
  147. struct task_struct;
  148. static inline u64 perf_event_raw(struct perf_counter_hw_event *hw_event)
  149. {
  150. return hw_event->config & PERF_COUNTER_RAW_MASK;
  151. }
  152. static inline u64 perf_event_config(struct perf_counter_hw_event *hw_event)
  153. {
  154. return hw_event->config & PERF_COUNTER_CONFIG_MASK;
  155. }
  156. static inline u64 perf_event_type(struct perf_counter_hw_event *hw_event)
  157. {
  158. return (hw_event->config & PERF_COUNTER_TYPE_MASK) >>
  159. PERF_COUNTER_TYPE_SHIFT;
  160. }
  161. static inline u64 perf_event_id(struct perf_counter_hw_event *hw_event)
  162. {
  163. return hw_event->config & PERF_COUNTER_EVENT_MASK;
  164. }
  165. /**
  166. * struct hw_perf_counter - performance counter hardware details:
  167. */
  168. struct hw_perf_counter {
  169. #ifdef CONFIG_PERF_COUNTERS
  170. union {
  171. struct { /* hardware */
  172. u64 config;
  173. unsigned long config_base;
  174. unsigned long counter_base;
  175. int nmi;
  176. unsigned int idx;
  177. };
  178. union { /* software */
  179. atomic64_t count;
  180. struct hrtimer hrtimer;
  181. };
  182. };
  183. atomic64_t prev_count;
  184. u64 irq_period;
  185. atomic64_t period_left;
  186. #endif
  187. };
  188. /*
  189. * Hardcoded buffer length limit for now, for IRQ-fed events:
  190. */
  191. #define PERF_DATA_BUFLEN 2048
  192. /**
  193. * struct perf_data - performance counter IRQ data sampling ...
  194. */
  195. struct perf_data {
  196. int len;
  197. int rd_idx;
  198. int overrun;
  199. u8 data[PERF_DATA_BUFLEN];
  200. };
  201. struct perf_counter;
  202. /**
  203. * struct hw_perf_counter_ops - performance counter hw ops
  204. */
  205. struct hw_perf_counter_ops {
  206. int (*enable) (struct perf_counter *counter);
  207. void (*disable) (struct perf_counter *counter);
  208. void (*read) (struct perf_counter *counter);
  209. };
  210. /**
  211. * enum perf_counter_active_state - the states of a counter
  212. */
  213. enum perf_counter_active_state {
  214. PERF_COUNTER_STATE_ERROR = -2,
  215. PERF_COUNTER_STATE_OFF = -1,
  216. PERF_COUNTER_STATE_INACTIVE = 0,
  217. PERF_COUNTER_STATE_ACTIVE = 1,
  218. };
  219. struct file;
  220. /**
  221. * struct perf_counter - performance counter kernel representation:
  222. */
  223. struct perf_counter {
  224. #ifdef CONFIG_PERF_COUNTERS
  225. struct list_head list_entry;
  226. struct list_head event_entry;
  227. struct list_head sibling_list;
  228. struct perf_counter *group_leader;
  229. const struct hw_perf_counter_ops *hw_ops;
  230. enum perf_counter_active_state state;
  231. enum perf_counter_active_state prev_state;
  232. atomic64_t count;
  233. struct perf_counter_hw_event hw_event;
  234. struct hw_perf_counter hw;
  235. struct perf_counter_context *ctx;
  236. struct task_struct *task;
  237. struct file *filp;
  238. struct perf_counter *parent;
  239. struct list_head child_list;
  240. /*
  241. * Protect attach/detach and child_list:
  242. */
  243. struct mutex mutex;
  244. int oncpu;
  245. int cpu;
  246. /* pointer to page shared with userspace via mmap */
  247. unsigned long user_page;
  248. /* read() / irq related data */
  249. wait_queue_head_t waitq;
  250. /* optional: for NMIs */
  251. int wakeup_pending;
  252. struct perf_data *irqdata;
  253. struct perf_data *usrdata;
  254. struct perf_data data[2];
  255. void (*destroy)(struct perf_counter *);
  256. struct rcu_head rcu_head;
  257. #endif
  258. };
  259. /**
  260. * struct perf_counter_context - counter context structure
  261. *
  262. * Used as a container for task counters and CPU counters as well:
  263. */
  264. struct perf_counter_context {
  265. #ifdef CONFIG_PERF_COUNTERS
  266. /*
  267. * Protect the states of the counters in the list,
  268. * nr_active, and the list:
  269. */
  270. spinlock_t lock;
  271. /*
  272. * Protect the list of counters. Locking either mutex or lock
  273. * is sufficient to ensure the list doesn't change; to change
  274. * the list you need to lock both the mutex and the spinlock.
  275. */
  276. struct mutex mutex;
  277. struct list_head counter_list;
  278. struct list_head event_list;
  279. int nr_counters;
  280. int nr_active;
  281. int is_active;
  282. struct task_struct *task;
  283. #endif
  284. };
  285. /**
  286. * struct perf_counter_cpu_context - per cpu counter context structure
  287. */
  288. struct perf_cpu_context {
  289. struct perf_counter_context ctx;
  290. struct perf_counter_context *task_ctx;
  291. int active_oncpu;
  292. int max_pertask;
  293. int exclusive;
  294. /*
  295. * Recursion avoidance:
  296. *
  297. * task, softirq, irq, nmi context
  298. */
  299. int recursion[4];
  300. };
  301. /*
  302. * Set by architecture code:
  303. */
  304. extern int perf_max_counters;
  305. #ifdef CONFIG_PERF_COUNTERS
  306. extern const struct hw_perf_counter_ops *
  307. hw_perf_counter_init(struct perf_counter *counter);
  308. extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
  309. extern void perf_counter_task_sched_out(struct task_struct *task, int cpu);
  310. extern void perf_counter_task_tick(struct task_struct *task, int cpu);
  311. extern void perf_counter_init_task(struct task_struct *child);
  312. extern void perf_counter_exit_task(struct task_struct *child);
  313. extern void perf_counter_notify(struct pt_regs *regs);
  314. extern void perf_counter_print_debug(void);
  315. extern void perf_counter_unthrottle(void);
  316. extern u64 hw_perf_save_disable(void);
  317. extern void hw_perf_restore(u64 ctrl);
  318. extern int perf_counter_task_disable(void);
  319. extern int perf_counter_task_enable(void);
  320. extern int hw_perf_group_sched_in(struct perf_counter *group_leader,
  321. struct perf_cpu_context *cpuctx,
  322. struct perf_counter_context *ctx, int cpu);
  323. extern void perf_counter_update_userpage(struct perf_counter *counter);
  324. extern void perf_counter_output(struct perf_counter *counter,
  325. int nmi, struct pt_regs *regs);
  326. /*
  327. * Return 1 for a software counter, 0 for a hardware counter
  328. */
  329. static inline int is_software_counter(struct perf_counter *counter)
  330. {
  331. return !perf_event_raw(&counter->hw_event) &&
  332. perf_event_type(&counter->hw_event) != PERF_TYPE_HARDWARE;
  333. }
  334. extern void perf_swcounter_event(u32, u64, int, struct pt_regs *);
  335. #else
  336. static inline void
  337. perf_counter_task_sched_in(struct task_struct *task, int cpu) { }
  338. static inline void
  339. perf_counter_task_sched_out(struct task_struct *task, int cpu) { }
  340. static inline void
  341. perf_counter_task_tick(struct task_struct *task, int cpu) { }
  342. static inline void perf_counter_init_task(struct task_struct *child) { }
  343. static inline void perf_counter_exit_task(struct task_struct *child) { }
  344. static inline void perf_counter_notify(struct pt_regs *regs) { }
  345. static inline void perf_counter_print_debug(void) { }
  346. static inline void perf_counter_unthrottle(void) { }
  347. static inline void hw_perf_restore(u64 ctrl) { }
  348. static inline u64 hw_perf_save_disable(void) { return 0; }
  349. static inline int perf_counter_task_disable(void) { return -EINVAL; }
  350. static inline int perf_counter_task_enable(void) { return -EINVAL; }
  351. static inline void perf_swcounter_event(u32 event, u64 nr,
  352. int nmi, struct pt_regs *regs) { }
  353. #endif
  354. #endif /* __KERNEL__ */
  355. #endif /* _LINUX_PERF_COUNTER_H */