Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 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 | |
| 16 | #include <asm/atomic.h> |
Ingo Molnar | eb2b861 | 2008-12-17 09:09:13 +0100 | [diff] [blame] | 17 | #include <asm/perf_counter.h> |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 18 | |
| 19 | #include <linux/list.h> |
| 20 | #include <linux/mutex.h> |
| 21 | #include <linux/rculist.h> |
| 22 | #include <linux/rcupdate.h> |
| 23 | #include <linux/spinlock.h> |
| 24 | |
| 25 | struct task_struct; |
| 26 | |
| 27 | /* |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 28 | * User-space ABI bits: |
| 29 | */ |
| 30 | |
| 31 | /* |
| 32 | * Generalized performance counter event types, used by the hw_event.type |
| 33 | * parameter of the sys_perf_counter_open() syscall: |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 34 | */ |
| 35 | enum hw_event_types { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 36 | /* |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 37 | * Common hardware events, generalized by the kernel: |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 38 | */ |
Ingo Molnar | f650a67 | 2008-12-23 12:17:29 +0100 | [diff] [blame^] | 39 | PERF_COUNT_CPU_CYCLES = 0, |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 40 | PERF_COUNT_INSTRUCTIONS = 1, |
| 41 | PERF_COUNT_CACHE_REFERENCES = 2, |
| 42 | PERF_COUNT_CACHE_MISSES = 3, |
| 43 | PERF_COUNT_BRANCH_INSTRUCTIONS = 4, |
| 44 | PERF_COUNT_BRANCH_MISSES = 5, |
Ingo Molnar | f650a67 | 2008-12-23 12:17:29 +0100 | [diff] [blame^] | 45 | PERF_COUNT_BUS_CYCLES = 6, |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 46 | |
Ingo Molnar | f650a67 | 2008-12-23 12:17:29 +0100 | [diff] [blame^] | 47 | PERF_HW_EVENTS_MAX = 7, |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 48 | |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 49 | /* |
| 50 | * Special "software" counters provided by the kernel, even if |
| 51 | * the hardware does not support performance counters. These |
| 52 | * counters measure various physical and sw events of the |
| 53 | * kernel (and allow the profiling of them as well): |
| 54 | */ |
| 55 | PERF_COUNT_CPU_CLOCK = -1, |
| 56 | PERF_COUNT_TASK_CLOCK = -2, |
Ingo Molnar | 5d6a27d | 2008-12-14 12:28:33 +0100 | [diff] [blame] | 57 | PERF_COUNT_PAGE_FAULTS = -3, |
| 58 | PERF_COUNT_CONTEXT_SWITCHES = -4, |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 59 | PERF_COUNT_CPU_MIGRATIONS = -5, |
| 60 | |
| 61 | PERF_SW_EVENTS_MIN = -6, |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | /* |
| 65 | * IRQ-notification data record type: |
| 66 | */ |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 67 | enum perf_counter_record_type { |
| 68 | PERF_RECORD_SIMPLE = 0, |
| 69 | PERF_RECORD_IRQ = 1, |
| 70 | PERF_RECORD_GROUP = 2, |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 71 | }; |
| 72 | |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 73 | /* |
| 74 | * Hardware event to monitor via a performance monitoring counter: |
| 75 | */ |
| 76 | struct perf_counter_hw_event { |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 77 | s64 type; |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 78 | |
| 79 | u64 irq_period; |
| 80 | u32 record_type; |
| 81 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 82 | u32 disabled : 1, /* off by default */ |
| 83 | nmi : 1, /* NMI sampling */ |
| 84 | raw : 1, /* raw event type */ |
| 85 | inherit : 1, /* children inherit it */ |
| 86 | __reserved_1 : 28; |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 87 | |
| 88 | u64 __reserved_2; |
Thomas Gleixner | eab656a | 2008-12-08 19:26:59 +0100 | [diff] [blame] | 89 | }; |
| 90 | |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 91 | /* |
| 92 | * Kernel-internal data types: |
| 93 | */ |
| 94 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 95 | /** |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 96 | * struct hw_perf_counter - performance counter hardware details: |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 97 | */ |
| 98 | struct hw_perf_counter { |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 99 | #ifdef CONFIG_PERF_COUNTERS |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 100 | u64 config; |
| 101 | unsigned long config_base; |
| 102 | unsigned long counter_base; |
| 103 | int nmi; |
| 104 | unsigned int idx; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 105 | atomic64_t prev_count; |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 106 | u64 irq_period; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 107 | atomic64_t period_left; |
| 108 | #endif |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | /* |
| 112 | * Hardcoded buffer length limit for now, for IRQ-fed events: |
| 113 | */ |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 114 | #define PERF_DATA_BUFLEN 2048 |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * struct perf_data - performance counter IRQ data sampling ... |
| 118 | */ |
| 119 | struct perf_data { |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 120 | int len; |
| 121 | int rd_idx; |
| 122 | int overrun; |
| 123 | u8 data[PERF_DATA_BUFLEN]; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 124 | }; |
| 125 | |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 126 | struct perf_counter; |
| 127 | |
| 128 | /** |
| 129 | * struct hw_perf_counter_ops - performance counter hw ops |
| 130 | */ |
| 131 | struct hw_perf_counter_ops { |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 132 | int (*enable) (struct perf_counter *counter); |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 133 | void (*disable) (struct perf_counter *counter); |
| 134 | void (*read) (struct perf_counter *counter); |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 135 | }; |
| 136 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 137 | /** |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame] | 138 | * enum perf_counter_active_state - the states of a counter |
| 139 | */ |
| 140 | enum perf_counter_active_state { |
| 141 | PERF_COUNTER_STATE_OFF = -1, |
| 142 | PERF_COUNTER_STATE_INACTIVE = 0, |
| 143 | PERF_COUNTER_STATE_ACTIVE = 1, |
| 144 | }; |
| 145 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 146 | struct file; |
| 147 | |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame] | 148 | /** |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 149 | * struct perf_counter - performance counter kernel representation: |
| 150 | */ |
| 151 | struct perf_counter { |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 152 | #ifdef CONFIG_PERF_COUNTERS |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 153 | struct list_head list_entry; |
| 154 | struct list_head sibling_list; |
| 155 | struct perf_counter *group_leader; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 156 | const struct hw_perf_counter_ops *hw_ops; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 157 | |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame] | 158 | enum perf_counter_active_state state; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 159 | atomic64_t count; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 160 | |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 161 | struct perf_counter_hw_event hw_event; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 162 | struct hw_perf_counter hw; |
| 163 | |
| 164 | struct perf_counter_context *ctx; |
| 165 | struct task_struct *task; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 166 | struct file *filp; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 167 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 168 | struct perf_counter *parent; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 169 | /* |
| 170 | * Protect attach/detach: |
| 171 | */ |
| 172 | struct mutex mutex; |
| 173 | |
| 174 | int oncpu; |
| 175 | int cpu; |
| 176 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 177 | /* read() / irq related data */ |
| 178 | wait_queue_head_t waitq; |
| 179 | /* optional: for NMIs */ |
| 180 | int wakeup_pending; |
| 181 | struct perf_data *irqdata; |
| 182 | struct perf_data *usrdata; |
| 183 | struct perf_data data[2]; |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 184 | #endif |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | /** |
| 188 | * struct perf_counter_context - counter context structure |
| 189 | * |
| 190 | * Used as a container for task counters and CPU counters as well: |
| 191 | */ |
| 192 | struct perf_counter_context { |
| 193 | #ifdef CONFIG_PERF_COUNTERS |
| 194 | /* |
| 195 | * Protect the list of counters: |
| 196 | */ |
| 197 | spinlock_t lock; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 198 | |
| 199 | struct list_head counter_list; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 200 | int nr_counters; |
| 201 | int nr_active; |
| 202 | struct task_struct *task; |
| 203 | #endif |
| 204 | }; |
| 205 | |
| 206 | /** |
| 207 | * struct perf_counter_cpu_context - per cpu counter context structure |
| 208 | */ |
| 209 | struct perf_cpu_context { |
| 210 | struct perf_counter_context ctx; |
| 211 | struct perf_counter_context *task_ctx; |
| 212 | int active_oncpu; |
| 213 | int max_pertask; |
| 214 | }; |
| 215 | |
| 216 | /* |
| 217 | * Set by architecture code: |
| 218 | */ |
| 219 | extern int perf_max_counters; |
| 220 | |
| 221 | #ifdef CONFIG_PERF_COUNTERS |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 222 | extern const struct hw_perf_counter_ops * |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 223 | hw_perf_counter_init(struct perf_counter *counter); |
| 224 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 225 | extern void perf_counter_task_sched_in(struct task_struct *task, int cpu); |
| 226 | extern void perf_counter_task_sched_out(struct task_struct *task, int cpu); |
| 227 | extern void perf_counter_task_tick(struct task_struct *task, int cpu); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 228 | extern void perf_counter_init_task(struct task_struct *child); |
| 229 | extern void perf_counter_exit_task(struct task_struct *child); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 230 | extern void perf_counter_notify(struct pt_regs *regs); |
| 231 | extern void perf_counter_print_debug(void); |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 232 | extern u64 hw_perf_save_disable(void); |
| 233 | extern void hw_perf_restore(u64 ctrl); |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 234 | extern int perf_counter_task_disable(void); |
| 235 | extern int perf_counter_task_enable(void); |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 236 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 237 | #else |
| 238 | static inline void |
| 239 | perf_counter_task_sched_in(struct task_struct *task, int cpu) { } |
| 240 | static inline void |
| 241 | perf_counter_task_sched_out(struct task_struct *task, int cpu) { } |
| 242 | static inline void |
| 243 | perf_counter_task_tick(struct task_struct *task, int cpu) { } |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 244 | static inline void perf_counter_init_task(struct task_struct *child) { } |
| 245 | static inline void perf_counter_exit_task(struct task_struct *child) { } |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 246 | static inline void perf_counter_notify(struct pt_regs *regs) { } |
| 247 | static inline void perf_counter_print_debug(void) { } |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 248 | static inline void hw_perf_restore(u64 ctrl) { } |
| 249 | static inline u64 hw_perf_save_disable(void) { return 0; } |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 250 | static inline int perf_counter_task_disable(void) { return -EINVAL; } |
| 251 | static inline int perf_counter_task_enable(void) { return -EINVAL; } |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 252 | #endif |
| 253 | |
| 254 | #endif /* _LINUX_PERF_COUNTER_H */ |