perf_counter.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. #define __PERF_COUNTER_MASK(name) \
  67. (((1ULL << PERF_COUNTER_##name##_BITS) - 1) << \
  68. PERF_COUNTER_##name##_SHIFT)
  69. #define PERF_COUNTER_RAW_BITS 1
  70. #define PERF_COUNTER_RAW_SHIFT 63
  71. #define PERF_COUNTER_RAW_MASK __PERF_COUNTER_MASK(RAW)
  72. #define PERF_COUNTER_CONFIG_BITS 63
  73. #define PERF_COUNTER_CONFIG_SHIFT 0
  74. #define PERF_COUNTER_CONFIG_MASK __PERF_COUNTER_MASK(CONFIG)
  75. #define PERF_COUNTER_TYPE_BITS 7
  76. #define PERF_COUNTER_TYPE_SHIFT 56
  77. #define PERF_COUNTER_TYPE_MASK __PERF_COUNTER_MASK(TYPE)
  78. #define PERF_COUNTER_EVENT_BITS 56
  79. #define PERF_COUNTER_EVENT_SHIFT 0
  80. #define PERF_COUNTER_EVENT_MASK __PERF_COUNTER_MASK(EVENT)
  81. /*
  82. * Bits that can be set in hw_event.record_type to request information
  83. * in the overflow packets.
  84. */
  85. enum perf_counter_record_format {
  86. PERF_RECORD_IP = 1U << 0,
  87. PERF_RECORD_TID = 1U << 1,
  88. PERF_RECORD_GROUP = 1U << 2,
  89. PERF_RECORD_CALLCHAIN = 1U << 3,
  90. };
  91. /*
  92. * Bits that can be set in hw_event.read_format to request that
  93. * reads on the counter should return the indicated quantities,
  94. * in increasing order of bit value, after the counter value.
  95. */
  96. enum perf_counter_read_format {
  97. PERF_FORMAT_TOTAL_TIME_ENABLED = 1,
  98. PERF_FORMAT_TOTAL_TIME_RUNNING = 2,
  99. };
  100. /*
  101. * Hardware event to monitor via a performance monitoring counter:
  102. */
  103. struct perf_counter_hw_event {
  104. /*
  105. * The MSB of the config word signifies if the rest contains cpu
  106. * specific (raw) counter configuration data, if unset, the next
  107. * 7 bits are an event type and the rest of the bits are the event
  108. * identifier.
  109. */
  110. __u64 config;
  111. __u64 irq_period;
  112. __u32 record_type;
  113. __u32 read_format;
  114. __u64 disabled : 1, /* off by default */
  115. nmi : 1, /* NMI sampling */
  116. inherit : 1, /* children inherit it */
  117. pinned : 1, /* must always be on PMU */
  118. exclusive : 1, /* only group on PMU */
  119. exclude_user : 1, /* don't count user */
  120. exclude_kernel : 1, /* ditto kernel */
  121. exclude_hv : 1, /* ditto hypervisor */
  122. exclude_idle : 1, /* don't count when idle */
  123. mmap : 1, /* include mmap data */
  124. munmap : 1, /* include munmap data */
  125. __reserved_1 : 53;
  126. __u32 extra_config_len;
  127. __u32 wakeup_events; /* wakeup every n events */
  128. __u64 __reserved_2;
  129. __u64 __reserved_3;
  130. };
  131. /*
  132. * Ioctls that can be done on a perf counter fd:
  133. */
  134. #define PERF_COUNTER_IOC_ENABLE _IO('$', 0)
  135. #define PERF_COUNTER_IOC_DISABLE _IO('$', 1)
  136. /*
  137. * Structure of the page that can be mapped via mmap
  138. */
  139. struct perf_counter_mmap_page {
  140. __u32 version; /* version number of this structure */
  141. __u32 compat_version; /* lowest version this is compat with */
  142. /*
  143. * Bits needed to read the hw counters in user-space.
  144. *
  145. * u32 seq;
  146. * s64 count;
  147. *
  148. * again:
  149. * seq = pc->lock;
  150. * if (unlikely(seq & 1)) {
  151. * cpu_relax();
  152. * goto again;
  153. * }
  154. *
  155. * if (pc->index) {
  156. * count = pmc_read(pc->index - 1);
  157. * count += pc->offset;
  158. * } else
  159. * goto regular_read;
  160. *
  161. * barrier();
  162. * if (pc->lock != seq)
  163. * goto again;
  164. *
  165. * NOTE: for obvious reason this only works on self-monitoring
  166. * processes.
  167. */
  168. __u32 lock; /* seqlock for synchronization */
  169. __u32 index; /* hardware counter identifier */
  170. __s64 offset; /* add to hardware counter value */
  171. /*
  172. * Control data for the mmap() data buffer.
  173. *
  174. * User-space reading this value should issue an rmb(), on SMP capable
  175. * platforms, after reading this value -- see perf_counter_wakeup().
  176. */
  177. __u32 data_head; /* head in the data section */
  178. };
  179. struct perf_event_header {
  180. __u32 type;
  181. __u32 size;
  182. };
  183. enum perf_event_type {
  184. PERF_EVENT_MMAP = 1,
  185. PERF_EVENT_MUNMAP = 2,
  186. /*
  187. * Half the event type space is reserved for the counter overflow
  188. * bitfields, as found in hw_event.record_type.
  189. *
  190. * These events will have types of the form:
  191. * PERF_EVENT_COUNTER_OVERFLOW { | __PERF_EVENT_* } *
  192. */
  193. PERF_EVENT_COUNTER_OVERFLOW = 1UL << 31,
  194. __PERF_EVENT_IP = PERF_RECORD_IP,
  195. __PERF_EVENT_TID = PERF_RECORD_TID,
  196. __PERF_EVENT_GROUP = PERF_RECORD_GROUP,
  197. __PERF_EVENT_CALLCHAIN = PERF_RECORD_CALLCHAIN,
  198. };
  199. #ifdef __KERNEL__
  200. /*
  201. * Kernel-internal data types and definitions:
  202. */
  203. #ifdef CONFIG_PERF_COUNTERS
  204. # include <asm/perf_counter.h>
  205. #endif
  206. #include <linux/list.h>
  207. #include <linux/mutex.h>
  208. #include <linux/rculist.h>
  209. #include <linux/rcupdate.h>
  210. #include <linux/spinlock.h>
  211. #include <linux/hrtimer.h>
  212. #include <asm/atomic.h>
  213. struct task_struct;
  214. static inline u64 perf_event_raw(struct perf_counter_hw_event *hw_event)
  215. {
  216. return hw_event->config & PERF_COUNTER_RAW_MASK;
  217. }
  218. static inline u64 perf_event_config(struct perf_counter_hw_event *hw_event)
  219. {
  220. return hw_event->config & PERF_COUNTER_CONFIG_MASK;
  221. }
  222. static inline u64 perf_event_type(struct perf_counter_hw_event *hw_event)
  223. {
  224. return (hw_event->config & PERF_COUNTER_TYPE_MASK) >>
  225. PERF_COUNTER_TYPE_SHIFT;
  226. }
  227. static inline u64 perf_event_id(struct perf_counter_hw_event *hw_event)
  228. {
  229. return hw_event->config & PERF_COUNTER_EVENT_MASK;
  230. }
  231. /**
  232. * struct hw_perf_counter - performance counter hardware details:
  233. */
  234. struct hw_perf_counter {
  235. #ifdef CONFIG_PERF_COUNTERS
  236. union {
  237. struct { /* hardware */
  238. u64 config;
  239. unsigned long config_base;
  240. unsigned long counter_base;
  241. int nmi;
  242. unsigned int idx;
  243. };
  244. union { /* software */
  245. atomic64_t count;
  246. struct hrtimer hrtimer;
  247. };
  248. };
  249. atomic64_t prev_count;
  250. u64 irq_period;
  251. atomic64_t period_left;
  252. #endif
  253. };
  254. struct perf_counter;
  255. /**
  256. * struct hw_perf_counter_ops - performance counter hw ops
  257. */
  258. struct hw_perf_counter_ops {
  259. int (*enable) (struct perf_counter *counter);
  260. void (*disable) (struct perf_counter *counter);
  261. void (*read) (struct perf_counter *counter);
  262. };
  263. /**
  264. * enum perf_counter_active_state - the states of a counter
  265. */
  266. enum perf_counter_active_state {
  267. PERF_COUNTER_STATE_ERROR = -2,
  268. PERF_COUNTER_STATE_OFF = -1,
  269. PERF_COUNTER_STATE_INACTIVE = 0,
  270. PERF_COUNTER_STATE_ACTIVE = 1,
  271. };
  272. struct file;
  273. struct perf_mmap_data {
  274. struct rcu_head rcu_head;
  275. int nr_pages;
  276. atomic_t wakeup;
  277. atomic_t head;
  278. atomic_t events;
  279. struct perf_counter_mmap_page *user_page;
  280. void *data_pages[0];
  281. };
  282. struct perf_wakeup_entry {
  283. struct perf_wakeup_entry *next;
  284. };
  285. /**
  286. * struct perf_counter - performance counter kernel representation:
  287. */
  288. struct perf_counter {
  289. #ifdef CONFIG_PERF_COUNTERS
  290. struct list_head list_entry;
  291. struct list_head event_entry;
  292. struct list_head sibling_list;
  293. int nr_siblings;
  294. struct perf_counter *group_leader;
  295. const struct hw_perf_counter_ops *hw_ops;
  296. enum perf_counter_active_state state;
  297. enum perf_counter_active_state prev_state;
  298. atomic64_t count;
  299. /*
  300. * These are the total time in nanoseconds that the counter
  301. * has been enabled (i.e. eligible to run, and the task has
  302. * been scheduled in, if this is a per-task counter)
  303. * and running (scheduled onto the CPU), respectively.
  304. *
  305. * They are computed from tstamp_enabled, tstamp_running and
  306. * tstamp_stopped when the counter is in INACTIVE or ACTIVE state.
  307. */
  308. u64 total_time_enabled;
  309. u64 total_time_running;
  310. /*
  311. * These are timestamps used for computing total_time_enabled
  312. * and total_time_running when the counter is in INACTIVE or
  313. * ACTIVE state, measured in nanoseconds from an arbitrary point
  314. * in time.
  315. * tstamp_enabled: the notional time when the counter was enabled
  316. * tstamp_running: the notional time when the counter was scheduled on
  317. * tstamp_stopped: in INACTIVE state, the notional time when the
  318. * counter was scheduled off.
  319. */
  320. u64 tstamp_enabled;
  321. u64 tstamp_running;
  322. u64 tstamp_stopped;
  323. struct perf_counter_hw_event hw_event;
  324. struct hw_perf_counter hw;
  325. struct perf_counter_context *ctx;
  326. struct task_struct *task;
  327. struct file *filp;
  328. struct perf_counter *parent;
  329. struct list_head child_list;
  330. /*
  331. * These accumulate total time (in nanoseconds) that children
  332. * counters have been enabled and running, respectively.
  333. */
  334. atomic64_t child_total_time_enabled;
  335. atomic64_t child_total_time_running;
  336. /*
  337. * Protect attach/detach and child_list:
  338. */
  339. struct mutex mutex;
  340. int oncpu;
  341. int cpu;
  342. /* mmap bits */
  343. struct mutex mmap_mutex;
  344. atomic_t mmap_count;
  345. struct perf_mmap_data *data;
  346. /* poll related */
  347. wait_queue_head_t waitq;
  348. /* optional: for NMIs */
  349. struct perf_wakeup_entry wakeup;
  350. void (*destroy)(struct perf_counter *);
  351. struct rcu_head rcu_head;
  352. #endif
  353. };
  354. /**
  355. * struct perf_counter_context - counter context structure
  356. *
  357. * Used as a container for task counters and CPU counters as well:
  358. */
  359. struct perf_counter_context {
  360. #ifdef CONFIG_PERF_COUNTERS
  361. /*
  362. * Protect the states of the counters in the list,
  363. * nr_active, and the list:
  364. */
  365. spinlock_t lock;
  366. /*
  367. * Protect the list of counters. Locking either mutex or lock
  368. * is sufficient to ensure the list doesn't change; to change
  369. * the list you need to lock both the mutex and the spinlock.
  370. */
  371. struct mutex mutex;
  372. struct list_head counter_list;
  373. struct list_head event_list;
  374. int nr_counters;
  375. int nr_active;
  376. int is_active;
  377. struct task_struct *task;
  378. /*
  379. * time_now is the current time in nanoseconds since an arbitrary
  380. * point in the past. For per-task counters, this is based on the
  381. * task clock, and for per-cpu counters it is based on the cpu clock.
  382. * time_lost is an offset from the task/cpu clock, used to make it
  383. * appear that time only passes while the context is scheduled in.
  384. */
  385. u64 time_now;
  386. u64 time_lost;
  387. #endif
  388. };
  389. /**
  390. * struct perf_counter_cpu_context - per cpu counter context structure
  391. */
  392. struct perf_cpu_context {
  393. struct perf_counter_context ctx;
  394. struct perf_counter_context *task_ctx;
  395. int active_oncpu;
  396. int max_pertask;
  397. int exclusive;
  398. /*
  399. * Recursion avoidance:
  400. *
  401. * task, softirq, irq, nmi context
  402. */
  403. int recursion[4];
  404. };
  405. /*
  406. * Set by architecture code:
  407. */
  408. extern int perf_max_counters;
  409. #ifdef CONFIG_PERF_COUNTERS
  410. extern const struct hw_perf_counter_ops *
  411. hw_perf_counter_init(struct perf_counter *counter);
  412. extern void perf_counter_task_sched_in(struct task_struct *task, int cpu);
  413. extern void perf_counter_task_sched_out(struct task_struct *task, int cpu);
  414. extern void perf_counter_task_tick(struct task_struct *task, int cpu);
  415. extern void perf_counter_init_task(struct task_struct *child);
  416. extern void perf_counter_exit_task(struct task_struct *child);
  417. extern void perf_counter_do_pending(void);
  418. extern void perf_counter_print_debug(void);
  419. extern void perf_counter_unthrottle(void);
  420. extern u64 hw_perf_save_disable(void);
  421. extern void hw_perf_restore(u64 ctrl);
  422. extern int perf_counter_task_disable(void);
  423. extern int perf_counter_task_enable(void);
  424. extern int hw_perf_group_sched_in(struct perf_counter *group_leader,
  425. struct perf_cpu_context *cpuctx,
  426. struct perf_counter_context *ctx, int cpu);
  427. extern void perf_counter_update_userpage(struct perf_counter *counter);
  428. extern void perf_counter_output(struct perf_counter *counter,
  429. int nmi, struct pt_regs *regs);
  430. /*
  431. * Return 1 for a software counter, 0 for a hardware counter
  432. */
  433. static inline int is_software_counter(struct perf_counter *counter)
  434. {
  435. return !perf_event_raw(&counter->hw_event) &&
  436. perf_event_type(&counter->hw_event) != PERF_TYPE_HARDWARE;
  437. }
  438. extern void perf_swcounter_event(u32, u64, int, struct pt_regs *);
  439. extern void perf_counter_mmap(unsigned long addr, unsigned long len,
  440. unsigned long pgoff, struct file *file);
  441. extern void perf_counter_munmap(unsigned long addr, unsigned long len,
  442. unsigned long pgoff, struct file *file);
  443. #define MAX_STACK_DEPTH 254
  444. struct perf_callchain_entry {
  445. u32 nr, hv, kernel, user;
  446. u64 ip[MAX_STACK_DEPTH];
  447. };
  448. extern struct perf_callchain_entry *perf_callchain(struct pt_regs *regs);
  449. #else
  450. static inline void
  451. perf_counter_task_sched_in(struct task_struct *task, int cpu) { }
  452. static inline void
  453. perf_counter_task_sched_out(struct task_struct *task, int cpu) { }
  454. static inline void
  455. perf_counter_task_tick(struct task_struct *task, int cpu) { }
  456. static inline void perf_counter_init_task(struct task_struct *child) { }
  457. static inline void perf_counter_exit_task(struct task_struct *child) { }
  458. static inline void perf_counter_do_pending(void) { }
  459. static inline void perf_counter_print_debug(void) { }
  460. static inline void perf_counter_unthrottle(void) { }
  461. static inline void hw_perf_restore(u64 ctrl) { }
  462. static inline u64 hw_perf_save_disable(void) { return 0; }
  463. static inline int perf_counter_task_disable(void) { return -EINVAL; }
  464. static inline int perf_counter_task_enable(void) { return -EINVAL; }
  465. static inline void
  466. perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs) { }
  467. static inline void
  468. perf_counter_mmap(unsigned long addr, unsigned long len,
  469. unsigned long pgoff, struct file *file) { }
  470. static inline void
  471. perf_counter_munmap(unsigned long addr, unsigned long len,
  472. unsigned long pgoff, struct file *file) { }
  473. #endif
  474. #endif /* __KERNEL__ */
  475. #endif /* _LINUX_PERF_COUNTER_H */