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