Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Performance counter core code |
| 3 | * |
| 4 | * Copyright(C) 2008 Thomas Gleixner <tglx@linutronix.de> |
| 5 | * Copyright(C) 2008 Red Hat, Inc., Ingo Molnar |
| 6 | * |
| 7 | * For licencing details see kernel-base/COPYING |
| 8 | */ |
| 9 | |
| 10 | #include <linux/fs.h> |
| 11 | #include <linux/cpu.h> |
| 12 | #include <linux/smp.h> |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 13 | #include <linux/file.h> |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 14 | #include <linux/poll.h> |
| 15 | #include <linux/sysfs.h> |
| 16 | #include <linux/ptrace.h> |
| 17 | #include <linux/percpu.h> |
| 18 | #include <linux/uaccess.h> |
| 19 | #include <linux/syscalls.h> |
| 20 | #include <linux/anon_inodes.h> |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 21 | #include <linux/kernel_stat.h> |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 22 | #include <linux/perf_counter.h> |
| 23 | |
| 24 | /* |
| 25 | * Each CPU has a list of per CPU counters: |
| 26 | */ |
| 27 | DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context); |
| 28 | |
Ingo Molnar | 088e285 | 2008-12-14 20:21:00 +0100 | [diff] [blame] | 29 | int perf_max_counters __read_mostly = 1; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 30 | static int perf_reserved_percpu __read_mostly; |
| 31 | static int perf_overcommit __read_mostly = 1; |
| 32 | |
| 33 | /* |
| 34 | * Mutex for (sysadmin-configurable) counter reservations: |
| 35 | */ |
| 36 | static DEFINE_MUTEX(perf_resource_mutex); |
| 37 | |
| 38 | /* |
| 39 | * Architecture provided APIs - weak aliases: |
| 40 | */ |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 41 | extern __weak const struct hw_perf_counter_ops * |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 42 | hw_perf_counter_init(struct perf_counter *counter) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 43 | { |
Paul Mackerras | ff6f054 | 2009-01-09 16:19:25 +1100 | [diff] [blame] | 44 | return NULL; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 45 | } |
| 46 | |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 47 | u64 __weak hw_perf_save_disable(void) { return 0; } |
Yinghai Lu | 01ea1cc | 2008-12-26 21:05:06 -0800 | [diff] [blame] | 48 | void __weak hw_perf_restore(u64 ctrl) { barrier(); } |
| 49 | void __weak hw_perf_counter_setup(void) { barrier(); } |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame^] | 50 | int __weak hw_perf_group_sched_in(struct perf_counter *group_leader, |
| 51 | struct perf_cpu_context *cpuctx, |
| 52 | struct perf_counter_context *ctx, int cpu) |
| 53 | { |
| 54 | return 0; |
| 55 | } |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 56 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 57 | static void |
| 58 | list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx) |
| 59 | { |
| 60 | struct perf_counter *group_leader = counter->group_leader; |
| 61 | |
| 62 | /* |
| 63 | * Depending on whether it is a standalone or sibling counter, |
| 64 | * add it straight to the context's counter list, or to the group |
| 65 | * leader's sibling list: |
| 66 | */ |
| 67 | if (counter->group_leader == counter) |
| 68 | list_add_tail(&counter->list_entry, &ctx->counter_list); |
| 69 | else |
| 70 | list_add_tail(&counter->list_entry, &group_leader->sibling_list); |
| 71 | } |
| 72 | |
| 73 | static void |
| 74 | list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx) |
| 75 | { |
| 76 | struct perf_counter *sibling, *tmp; |
| 77 | |
| 78 | list_del_init(&counter->list_entry); |
| 79 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 80 | /* |
| 81 | * If this was a group counter with sibling counters then |
| 82 | * upgrade the siblings to singleton counters by adding them |
| 83 | * to the context list directly: |
| 84 | */ |
| 85 | list_for_each_entry_safe(sibling, tmp, |
| 86 | &counter->sibling_list, list_entry) { |
| 87 | |
| 88 | list_del_init(&sibling->list_entry); |
| 89 | list_add_tail(&sibling->list_entry, &ctx->counter_list); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 90 | sibling->group_leader = sibling; |
| 91 | } |
| 92 | } |
| 93 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 94 | /* |
| 95 | * Cross CPU call to remove a performance counter |
| 96 | * |
| 97 | * We disable the counter on the hardware level first. After that we |
| 98 | * remove it from the context list. |
| 99 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 100 | static void __perf_counter_remove_from_context(void *info) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 101 | { |
| 102 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 103 | struct perf_counter *counter = info; |
| 104 | struct perf_counter_context *ctx = counter->ctx; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 105 | unsigned long flags; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 106 | u64 perf_flags; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | * If this is a task context, we need to check whether it is |
| 110 | * the current task context of this cpu. If not it has been |
| 111 | * scheduled out before the smp call arrived. |
| 112 | */ |
| 113 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 114 | return; |
| 115 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 116 | curr_rq_lock_irq_save(&flags); |
| 117 | spin_lock(&ctx->lock); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 118 | |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame] | 119 | if (counter->state == PERF_COUNTER_STATE_ACTIVE) { |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame] | 120 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 121 | counter->hw_ops->disable(counter); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 122 | ctx->nr_active--; |
| 123 | cpuctx->active_oncpu--; |
| 124 | counter->task = NULL; |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 125 | counter->oncpu = -1; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 126 | } |
| 127 | ctx->nr_counters--; |
| 128 | |
| 129 | /* |
| 130 | * Protect the list operation against NMI by disabling the |
| 131 | * counters on a global level. NOP for non NMI based counters. |
| 132 | */ |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 133 | perf_flags = hw_perf_save_disable(); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 134 | list_del_counter(counter, ctx); |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 135 | hw_perf_restore(perf_flags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 136 | |
| 137 | if (!ctx->task) { |
| 138 | /* |
| 139 | * Allow more per task counters with respect to the |
| 140 | * reservation: |
| 141 | */ |
| 142 | cpuctx->max_pertask = |
| 143 | min(perf_max_counters - ctx->nr_counters, |
| 144 | perf_max_counters - perf_reserved_percpu); |
| 145 | } |
| 146 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 147 | spin_unlock(&ctx->lock); |
| 148 | curr_rq_unlock_irq_restore(&flags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | |
| 152 | /* |
| 153 | * Remove the counter from a task's (or a CPU's) list of counters. |
| 154 | * |
| 155 | * Must be called with counter->mutex held. |
| 156 | * |
| 157 | * CPU counters are removed with a smp call. For task counters we only |
| 158 | * call when the task is on a CPU. |
| 159 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 160 | static void perf_counter_remove_from_context(struct perf_counter *counter) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 161 | { |
| 162 | struct perf_counter_context *ctx = counter->ctx; |
| 163 | struct task_struct *task = ctx->task; |
| 164 | |
| 165 | if (!task) { |
| 166 | /* |
| 167 | * Per cpu counters are removed via an smp call and |
| 168 | * the removal is always sucessful. |
| 169 | */ |
| 170 | smp_call_function_single(counter->cpu, |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 171 | __perf_counter_remove_from_context, |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 172 | counter, 1); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | retry: |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 177 | task_oncpu_function_call(task, __perf_counter_remove_from_context, |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 178 | counter); |
| 179 | |
| 180 | spin_lock_irq(&ctx->lock); |
| 181 | /* |
| 182 | * If the context is active we need to retry the smp call. |
| 183 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 184 | if (ctx->nr_active && !list_empty(&counter->list_entry)) { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 185 | spin_unlock_irq(&ctx->lock); |
| 186 | goto retry; |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * The lock prevents that this context is scheduled in so we |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 191 | * can remove the counter safely, if the call above did not |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 192 | * succeed. |
| 193 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 194 | if (!list_empty(&counter->list_entry)) { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 195 | ctx->nr_counters--; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 196 | list_del_counter(counter, ctx); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 197 | counter->task = NULL; |
| 198 | } |
| 199 | spin_unlock_irq(&ctx->lock); |
| 200 | } |
| 201 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 202 | static int |
| 203 | counter_sched_in(struct perf_counter *counter, |
| 204 | struct perf_cpu_context *cpuctx, |
| 205 | struct perf_counter_context *ctx, |
| 206 | int cpu) |
| 207 | { |
| 208 | if (counter->state == PERF_COUNTER_STATE_OFF) |
| 209 | return 0; |
| 210 | |
| 211 | counter->state = PERF_COUNTER_STATE_ACTIVE; |
| 212 | counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */ |
| 213 | /* |
| 214 | * The new state must be visible before we turn it on in the hardware: |
| 215 | */ |
| 216 | smp_wmb(); |
| 217 | |
| 218 | if (counter->hw_ops->enable(counter)) { |
| 219 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
| 220 | counter->oncpu = -1; |
| 221 | return -EAGAIN; |
| 222 | } |
| 223 | |
| 224 | cpuctx->active_oncpu++; |
| 225 | ctx->nr_active++; |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 230 | /* |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 231 | * Cross CPU call to install and enable a performance counter |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 232 | */ |
| 233 | static void __perf_install_in_context(void *info) |
| 234 | { |
| 235 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 236 | struct perf_counter *counter = info; |
| 237 | struct perf_counter_context *ctx = counter->ctx; |
| 238 | int cpu = smp_processor_id(); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 239 | unsigned long flags; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 240 | u64 perf_flags; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 241 | |
| 242 | /* |
| 243 | * If this is a task context, we need to check whether it is |
| 244 | * the current task context of this cpu. If not it has been |
| 245 | * scheduled out before the smp call arrived. |
| 246 | */ |
| 247 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 248 | return; |
| 249 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 250 | curr_rq_lock_irq_save(&flags); |
| 251 | spin_lock(&ctx->lock); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 252 | |
| 253 | /* |
| 254 | * Protect the list operation against NMI by disabling the |
| 255 | * counters on a global level. NOP for non NMI based counters. |
| 256 | */ |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 257 | perf_flags = hw_perf_save_disable(); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 258 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 259 | list_add_counter(counter, ctx); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 260 | ctx->nr_counters++; |
| 261 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 262 | counter_sched_in(counter, cpuctx, ctx, cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 263 | |
| 264 | if (!ctx->task && cpuctx->max_pertask) |
| 265 | cpuctx->max_pertask--; |
| 266 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 267 | hw_perf_restore(perf_flags); |
| 268 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 269 | spin_unlock(&ctx->lock); |
| 270 | curr_rq_unlock_irq_restore(&flags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Attach a performance counter to a context |
| 275 | * |
| 276 | * First we add the counter to the list with the hardware enable bit |
| 277 | * in counter->hw_config cleared. |
| 278 | * |
| 279 | * If the counter is attached to a task which is on a CPU we use a smp |
| 280 | * call to enable it in the task context. The task might have been |
| 281 | * scheduled away, but we check this in the smp call again. |
| 282 | */ |
| 283 | static void |
| 284 | perf_install_in_context(struct perf_counter_context *ctx, |
| 285 | struct perf_counter *counter, |
| 286 | int cpu) |
| 287 | { |
| 288 | struct task_struct *task = ctx->task; |
| 289 | |
| 290 | counter->ctx = ctx; |
| 291 | if (!task) { |
| 292 | /* |
| 293 | * Per cpu counters are installed via an smp call and |
| 294 | * the install is always sucessful. |
| 295 | */ |
| 296 | smp_call_function_single(cpu, __perf_install_in_context, |
| 297 | counter, 1); |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | counter->task = task; |
| 302 | retry: |
| 303 | task_oncpu_function_call(task, __perf_install_in_context, |
| 304 | counter); |
| 305 | |
| 306 | spin_lock_irq(&ctx->lock); |
| 307 | /* |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 308 | * we need to retry the smp call. |
| 309 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 310 | if (ctx->nr_active && list_empty(&counter->list_entry)) { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 311 | spin_unlock_irq(&ctx->lock); |
| 312 | goto retry; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * The lock prevents that this context is scheduled in so we |
| 317 | * can add the counter safely, if it the call above did not |
| 318 | * succeed. |
| 319 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 320 | if (list_empty(&counter->list_entry)) { |
| 321 | list_add_counter(counter, ctx); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 322 | ctx->nr_counters++; |
| 323 | } |
| 324 | spin_unlock_irq(&ctx->lock); |
| 325 | } |
| 326 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 327 | static void |
| 328 | counter_sched_out(struct perf_counter *counter, |
| 329 | struct perf_cpu_context *cpuctx, |
| 330 | struct perf_counter_context *ctx) |
| 331 | { |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame] | 332 | if (counter->state != PERF_COUNTER_STATE_ACTIVE) |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 333 | return; |
| 334 | |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame] | 335 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 336 | counter->hw_ops->disable(counter); |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame] | 337 | counter->oncpu = -1; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 338 | |
| 339 | cpuctx->active_oncpu--; |
| 340 | ctx->nr_active--; |
| 341 | } |
| 342 | |
| 343 | static void |
| 344 | group_sched_out(struct perf_counter *group_counter, |
| 345 | struct perf_cpu_context *cpuctx, |
| 346 | struct perf_counter_context *ctx) |
| 347 | { |
| 348 | struct perf_counter *counter; |
| 349 | |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame^] | 350 | if (group_counter->state != PERF_COUNTER_STATE_ACTIVE) |
| 351 | return; |
| 352 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 353 | counter_sched_out(group_counter, cpuctx, ctx); |
| 354 | |
| 355 | /* |
| 356 | * Schedule out siblings (if any): |
| 357 | */ |
| 358 | list_for_each_entry(counter, &group_counter->sibling_list, list_entry) |
| 359 | counter_sched_out(counter, cpuctx, ctx); |
| 360 | } |
| 361 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 362 | void __perf_counter_sched_out(struct perf_counter_context *ctx, |
| 363 | struct perf_cpu_context *cpuctx) |
| 364 | { |
| 365 | struct perf_counter *counter; |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame^] | 366 | u64 flags; |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 367 | |
| 368 | if (likely(!ctx->nr_counters)) |
| 369 | return; |
| 370 | |
| 371 | spin_lock(&ctx->lock); |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame^] | 372 | flags = hw_perf_save_disable(); |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 373 | if (ctx->nr_active) { |
| 374 | list_for_each_entry(counter, &ctx->counter_list, list_entry) |
| 375 | group_sched_out(counter, cpuctx, ctx); |
| 376 | } |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame^] | 377 | hw_perf_restore(flags); |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 378 | spin_unlock(&ctx->lock); |
| 379 | } |
| 380 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 381 | /* |
| 382 | * Called from scheduler to remove the counters of the current task, |
| 383 | * with interrupts disabled. |
| 384 | * |
| 385 | * We stop each counter and update the counter value in counter->count. |
| 386 | * |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 387 | * This does not protect us against NMI, but disable() |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 388 | * sets the disabled bit in the control field of counter _before_ |
| 389 | * accessing the counter control register. If a NMI hits, then it will |
| 390 | * not restart the counter. |
| 391 | */ |
| 392 | void perf_counter_task_sched_out(struct task_struct *task, int cpu) |
| 393 | { |
| 394 | struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 395 | struct perf_counter_context *ctx = &task->perf_counter_ctx; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 396 | |
| 397 | if (likely(!cpuctx->task_ctx)) |
| 398 | return; |
| 399 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 400 | __perf_counter_sched_out(ctx, cpuctx); |
| 401 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 402 | cpuctx->task_ctx = NULL; |
| 403 | } |
| 404 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 405 | static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx) |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 406 | { |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 407 | __perf_counter_sched_out(&cpuctx->ctx, cpuctx); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 408 | } |
| 409 | |
Ingo Molnar | 7995888 | 2008-12-17 08:54:56 +0100 | [diff] [blame] | 410 | static int |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 411 | group_sched_in(struct perf_counter *group_counter, |
| 412 | struct perf_cpu_context *cpuctx, |
| 413 | struct perf_counter_context *ctx, |
| 414 | int cpu) |
| 415 | { |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 416 | struct perf_counter *counter, *partial_group; |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame^] | 417 | int ret; |
| 418 | |
| 419 | if (group_counter->state == PERF_COUNTER_STATE_OFF) |
| 420 | return 0; |
| 421 | |
| 422 | ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu); |
| 423 | if (ret) |
| 424 | return ret < 0 ? ret : 0; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 425 | |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 426 | if (counter_sched_in(group_counter, cpuctx, ctx, cpu)) |
| 427 | return -EAGAIN; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 428 | |
| 429 | /* |
| 430 | * Schedule in siblings as one group (if any): |
| 431 | */ |
Ingo Molnar | 7995888 | 2008-12-17 08:54:56 +0100 | [diff] [blame] | 432 | list_for_each_entry(counter, &group_counter->sibling_list, list_entry) { |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 433 | if (counter_sched_in(counter, cpuctx, ctx, cpu)) { |
| 434 | partial_group = counter; |
| 435 | goto group_error; |
| 436 | } |
Ingo Molnar | 7995888 | 2008-12-17 08:54:56 +0100 | [diff] [blame] | 437 | } |
| 438 | |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame^] | 439 | return 0; |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 440 | |
| 441 | group_error: |
| 442 | /* |
| 443 | * Groups can be scheduled in as one unit only, so undo any |
| 444 | * partial group before returning: |
| 445 | */ |
| 446 | list_for_each_entry(counter, &group_counter->sibling_list, list_entry) { |
| 447 | if (counter == partial_group) |
| 448 | break; |
| 449 | counter_sched_out(counter, cpuctx, ctx); |
| 450 | } |
| 451 | counter_sched_out(group_counter, cpuctx, ctx); |
| 452 | |
| 453 | return -EAGAIN; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 454 | } |
| 455 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 456 | static void |
| 457 | __perf_counter_sched_in(struct perf_counter_context *ctx, |
| 458 | struct perf_cpu_context *cpuctx, int cpu) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 459 | { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 460 | struct perf_counter *counter; |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame^] | 461 | u64 flags; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 462 | |
| 463 | if (likely(!ctx->nr_counters)) |
| 464 | return; |
| 465 | |
| 466 | spin_lock(&ctx->lock); |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame^] | 467 | flags = hw_perf_save_disable(); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 468 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 469 | /* |
| 470 | * Listen to the 'cpu' scheduling filter constraint |
| 471 | * of counters: |
| 472 | */ |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 473 | if (counter->cpu != -1 && counter->cpu != cpu) |
| 474 | continue; |
| 475 | |
Ingo Molnar | 7995888 | 2008-12-17 08:54:56 +0100 | [diff] [blame] | 476 | /* |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame^] | 477 | * If we scheduled in a group atomically and exclusively, |
| 478 | * or if this group can't go on, break out: |
Ingo Molnar | 7995888 | 2008-12-17 08:54:56 +0100 | [diff] [blame] | 479 | */ |
| 480 | if (group_sched_in(counter, cpuctx, ctx, cpu)) |
| 481 | break; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 482 | } |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame^] | 483 | hw_perf_restore(flags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 484 | spin_unlock(&ctx->lock); |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 485 | } |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 486 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 487 | /* |
| 488 | * Called from scheduler to add the counters of the current task |
| 489 | * with interrupts disabled. |
| 490 | * |
| 491 | * We restore the counter value and then enable it. |
| 492 | * |
| 493 | * This does not protect us against NMI, but enable() |
| 494 | * sets the enabled bit in the control field of counter _before_ |
| 495 | * accessing the counter control register. If a NMI hits, then it will |
| 496 | * keep the counter running. |
| 497 | */ |
| 498 | void perf_counter_task_sched_in(struct task_struct *task, int cpu) |
| 499 | { |
| 500 | struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 501 | struct perf_counter_context *ctx = &task->perf_counter_ctx; |
| 502 | |
| 503 | __perf_counter_sched_in(ctx, cpuctx, cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 504 | cpuctx->task_ctx = ctx; |
| 505 | } |
| 506 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 507 | static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu) |
| 508 | { |
| 509 | struct perf_counter_context *ctx = &cpuctx->ctx; |
| 510 | |
| 511 | __perf_counter_sched_in(ctx, cpuctx, cpu); |
| 512 | } |
| 513 | |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 514 | int perf_counter_task_disable(void) |
| 515 | { |
| 516 | struct task_struct *curr = current; |
| 517 | struct perf_counter_context *ctx = &curr->perf_counter_ctx; |
| 518 | struct perf_counter *counter; |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 519 | unsigned long flags; |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 520 | u64 perf_flags; |
| 521 | int cpu; |
| 522 | |
| 523 | if (likely(!ctx->nr_counters)) |
| 524 | return 0; |
| 525 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 526 | curr_rq_lock_irq_save(&flags); |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 527 | cpu = smp_processor_id(); |
| 528 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 529 | /* force the update of the task clock: */ |
| 530 | __task_delta_exec(curr, 1); |
| 531 | |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 532 | perf_counter_task_sched_out(curr, cpu); |
| 533 | |
| 534 | spin_lock(&ctx->lock); |
| 535 | |
| 536 | /* |
| 537 | * Disable all the counters: |
| 538 | */ |
| 539 | perf_flags = hw_perf_save_disable(); |
| 540 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 541 | list_for_each_entry(counter, &ctx->counter_list, list_entry) |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame] | 542 | counter->state = PERF_COUNTER_STATE_OFF; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 543 | |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 544 | hw_perf_restore(perf_flags); |
| 545 | |
| 546 | spin_unlock(&ctx->lock); |
| 547 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 548 | curr_rq_unlock_irq_restore(&flags); |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 549 | |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | int perf_counter_task_enable(void) |
| 554 | { |
| 555 | struct task_struct *curr = current; |
| 556 | struct perf_counter_context *ctx = &curr->perf_counter_ctx; |
| 557 | struct perf_counter *counter; |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 558 | unsigned long flags; |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 559 | u64 perf_flags; |
| 560 | int cpu; |
| 561 | |
| 562 | if (likely(!ctx->nr_counters)) |
| 563 | return 0; |
| 564 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 565 | curr_rq_lock_irq_save(&flags); |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 566 | cpu = smp_processor_id(); |
| 567 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 568 | /* force the update of the task clock: */ |
| 569 | __task_delta_exec(curr, 1); |
| 570 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 571 | perf_counter_task_sched_out(curr, cpu); |
| 572 | |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 573 | spin_lock(&ctx->lock); |
| 574 | |
| 575 | /* |
| 576 | * Disable all the counters: |
| 577 | */ |
| 578 | perf_flags = hw_perf_save_disable(); |
| 579 | |
| 580 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame] | 581 | if (counter->state != PERF_COUNTER_STATE_OFF) |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 582 | continue; |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame] | 583 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 584 | counter->hw_event.disabled = 0; |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 585 | } |
| 586 | hw_perf_restore(perf_flags); |
| 587 | |
| 588 | spin_unlock(&ctx->lock); |
| 589 | |
| 590 | perf_counter_task_sched_in(curr, cpu); |
| 591 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 592 | curr_rq_unlock_irq_restore(&flags); |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 597 | /* |
| 598 | * Round-robin a context's counters: |
| 599 | */ |
| 600 | static void rotate_ctx(struct perf_counter_context *ctx) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 601 | { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 602 | struct perf_counter *counter; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 603 | u64 perf_flags; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 604 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 605 | if (!ctx->nr_counters) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 606 | return; |
| 607 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 608 | spin_lock(&ctx->lock); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 609 | /* |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 610 | * Rotate the first entry last (works just fine for group counters too): |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 611 | */ |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 612 | perf_flags = hw_perf_save_disable(); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 613 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
| 614 | list_del(&counter->list_entry); |
| 615 | list_add_tail(&counter->list_entry, &ctx->counter_list); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 616 | break; |
| 617 | } |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 618 | hw_perf_restore(perf_flags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 619 | |
| 620 | spin_unlock(&ctx->lock); |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 621 | } |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 622 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 623 | void perf_counter_task_tick(struct task_struct *curr, int cpu) |
| 624 | { |
| 625 | struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 626 | struct perf_counter_context *ctx = &curr->perf_counter_ctx; |
| 627 | const int rotate_percpu = 0; |
| 628 | |
| 629 | if (rotate_percpu) |
| 630 | perf_counter_cpu_sched_out(cpuctx); |
| 631 | perf_counter_task_sched_out(curr, cpu); |
| 632 | |
| 633 | if (rotate_percpu) |
| 634 | rotate_ctx(&cpuctx->ctx); |
| 635 | rotate_ctx(ctx); |
| 636 | |
| 637 | if (rotate_percpu) |
| 638 | perf_counter_cpu_sched_in(cpuctx, cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 639 | perf_counter_task_sched_in(curr, cpu); |
| 640 | } |
| 641 | |
| 642 | /* |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 643 | * Cross CPU call to read the hardware counter |
| 644 | */ |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 645 | static void __read(void *info) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 646 | { |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 647 | struct perf_counter *counter = info; |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 648 | unsigned long flags; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 649 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 650 | curr_rq_lock_irq_save(&flags); |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 651 | counter->hw_ops->read(counter); |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 652 | curr_rq_unlock_irq_restore(&flags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 653 | } |
| 654 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 655 | static u64 perf_counter_read(struct perf_counter *counter) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 656 | { |
| 657 | /* |
| 658 | * If counter is enabled and currently active on a CPU, update the |
| 659 | * value in the counter structure: |
| 660 | */ |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame] | 661 | if (counter->state == PERF_COUNTER_STATE_ACTIVE) { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 662 | smp_call_function_single(counter->oncpu, |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 663 | __read, counter, 1); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 664 | } |
| 665 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 666 | return atomic64_read(&counter->count); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /* |
| 670 | * Cross CPU call to switch performance data pointers |
| 671 | */ |
| 672 | static void __perf_switch_irq_data(void *info) |
| 673 | { |
| 674 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 675 | struct perf_counter *counter = info; |
| 676 | struct perf_counter_context *ctx = counter->ctx; |
| 677 | struct perf_data *oldirqdata = counter->irqdata; |
| 678 | |
| 679 | /* |
| 680 | * If this is a task context, we need to check whether it is |
| 681 | * the current task context of this cpu. If not it has been |
| 682 | * scheduled out before the smp call arrived. |
| 683 | */ |
| 684 | if (ctx->task) { |
| 685 | if (cpuctx->task_ctx != ctx) |
| 686 | return; |
| 687 | spin_lock(&ctx->lock); |
| 688 | } |
| 689 | |
| 690 | /* Change the pointer NMI safe */ |
| 691 | atomic_long_set((atomic_long_t *)&counter->irqdata, |
| 692 | (unsigned long) counter->usrdata); |
| 693 | counter->usrdata = oldirqdata; |
| 694 | |
| 695 | if (ctx->task) |
| 696 | spin_unlock(&ctx->lock); |
| 697 | } |
| 698 | |
| 699 | static struct perf_data *perf_switch_irq_data(struct perf_counter *counter) |
| 700 | { |
| 701 | struct perf_counter_context *ctx = counter->ctx; |
| 702 | struct perf_data *oldirqdata = counter->irqdata; |
| 703 | struct task_struct *task = ctx->task; |
| 704 | |
| 705 | if (!task) { |
| 706 | smp_call_function_single(counter->cpu, |
| 707 | __perf_switch_irq_data, |
| 708 | counter, 1); |
| 709 | return counter->usrdata; |
| 710 | } |
| 711 | |
| 712 | retry: |
| 713 | spin_lock_irq(&ctx->lock); |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame] | 714 | if (counter->state != PERF_COUNTER_STATE_ACTIVE) { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 715 | counter->irqdata = counter->usrdata; |
| 716 | counter->usrdata = oldirqdata; |
| 717 | spin_unlock_irq(&ctx->lock); |
| 718 | return oldirqdata; |
| 719 | } |
| 720 | spin_unlock_irq(&ctx->lock); |
| 721 | task_oncpu_function_call(task, __perf_switch_irq_data, counter); |
| 722 | /* Might have failed, because task was scheduled out */ |
| 723 | if (counter->irqdata == oldirqdata) |
| 724 | goto retry; |
| 725 | |
| 726 | return counter->usrdata; |
| 727 | } |
| 728 | |
| 729 | static void put_context(struct perf_counter_context *ctx) |
| 730 | { |
| 731 | if (ctx->task) |
| 732 | put_task_struct(ctx->task); |
| 733 | } |
| 734 | |
| 735 | static struct perf_counter_context *find_get_context(pid_t pid, int cpu) |
| 736 | { |
| 737 | struct perf_cpu_context *cpuctx; |
| 738 | struct perf_counter_context *ctx; |
| 739 | struct task_struct *task; |
| 740 | |
| 741 | /* |
| 742 | * If cpu is not a wildcard then this is a percpu counter: |
| 743 | */ |
| 744 | if (cpu != -1) { |
| 745 | /* Must be root to operate on a CPU counter: */ |
| 746 | if (!capable(CAP_SYS_ADMIN)) |
| 747 | return ERR_PTR(-EACCES); |
| 748 | |
| 749 | if (cpu < 0 || cpu > num_possible_cpus()) |
| 750 | return ERR_PTR(-EINVAL); |
| 751 | |
| 752 | /* |
| 753 | * We could be clever and allow to attach a counter to an |
| 754 | * offline CPU and activate it when the CPU comes up, but |
| 755 | * that's for later. |
| 756 | */ |
| 757 | if (!cpu_isset(cpu, cpu_online_map)) |
| 758 | return ERR_PTR(-ENODEV); |
| 759 | |
| 760 | cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 761 | ctx = &cpuctx->ctx; |
| 762 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 763 | return ctx; |
| 764 | } |
| 765 | |
| 766 | rcu_read_lock(); |
| 767 | if (!pid) |
| 768 | task = current; |
| 769 | else |
| 770 | task = find_task_by_vpid(pid); |
| 771 | if (task) |
| 772 | get_task_struct(task); |
| 773 | rcu_read_unlock(); |
| 774 | |
| 775 | if (!task) |
| 776 | return ERR_PTR(-ESRCH); |
| 777 | |
| 778 | ctx = &task->perf_counter_ctx; |
| 779 | ctx->task = task; |
| 780 | |
| 781 | /* Reuse ptrace permission checks for now. */ |
| 782 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) { |
| 783 | put_context(ctx); |
| 784 | return ERR_PTR(-EACCES); |
| 785 | } |
| 786 | |
| 787 | return ctx; |
| 788 | } |
| 789 | |
| 790 | /* |
| 791 | * Called when the last reference to the file is gone. |
| 792 | */ |
| 793 | static int perf_release(struct inode *inode, struct file *file) |
| 794 | { |
| 795 | struct perf_counter *counter = file->private_data; |
| 796 | struct perf_counter_context *ctx = counter->ctx; |
| 797 | |
| 798 | file->private_data = NULL; |
| 799 | |
| 800 | mutex_lock(&counter->mutex); |
| 801 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 802 | perf_counter_remove_from_context(counter); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 803 | put_context(ctx); |
| 804 | |
| 805 | mutex_unlock(&counter->mutex); |
| 806 | |
| 807 | kfree(counter); |
| 808 | |
| 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | /* |
| 813 | * Read the performance counter - simple non blocking version for now |
| 814 | */ |
| 815 | static ssize_t |
| 816 | perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count) |
| 817 | { |
| 818 | u64 cntval; |
| 819 | |
| 820 | if (count != sizeof(cntval)) |
| 821 | return -EINVAL; |
| 822 | |
| 823 | mutex_lock(&counter->mutex); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 824 | cntval = perf_counter_read(counter); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 825 | mutex_unlock(&counter->mutex); |
| 826 | |
| 827 | return put_user(cntval, (u64 __user *) buf) ? -EFAULT : sizeof(cntval); |
| 828 | } |
| 829 | |
| 830 | static ssize_t |
| 831 | perf_copy_usrdata(struct perf_data *usrdata, char __user *buf, size_t count) |
| 832 | { |
| 833 | if (!usrdata->len) |
| 834 | return 0; |
| 835 | |
| 836 | count = min(count, (size_t)usrdata->len); |
| 837 | if (copy_to_user(buf, usrdata->data + usrdata->rd_idx, count)) |
| 838 | return -EFAULT; |
| 839 | |
| 840 | /* Adjust the counters */ |
| 841 | usrdata->len -= count; |
| 842 | if (!usrdata->len) |
| 843 | usrdata->rd_idx = 0; |
| 844 | else |
| 845 | usrdata->rd_idx += count; |
| 846 | |
| 847 | return count; |
| 848 | } |
| 849 | |
| 850 | static ssize_t |
| 851 | perf_read_irq_data(struct perf_counter *counter, |
| 852 | char __user *buf, |
| 853 | size_t count, |
| 854 | int nonblocking) |
| 855 | { |
| 856 | struct perf_data *irqdata, *usrdata; |
| 857 | DECLARE_WAITQUEUE(wait, current); |
| 858 | ssize_t res; |
| 859 | |
| 860 | irqdata = counter->irqdata; |
| 861 | usrdata = counter->usrdata; |
| 862 | |
| 863 | if (usrdata->len + irqdata->len >= count) |
| 864 | goto read_pending; |
| 865 | |
| 866 | if (nonblocking) |
| 867 | return -EAGAIN; |
| 868 | |
| 869 | spin_lock_irq(&counter->waitq.lock); |
| 870 | __add_wait_queue(&counter->waitq, &wait); |
| 871 | for (;;) { |
| 872 | set_current_state(TASK_INTERRUPTIBLE); |
| 873 | if (usrdata->len + irqdata->len >= count) |
| 874 | break; |
| 875 | |
| 876 | if (signal_pending(current)) |
| 877 | break; |
| 878 | |
| 879 | spin_unlock_irq(&counter->waitq.lock); |
| 880 | schedule(); |
| 881 | spin_lock_irq(&counter->waitq.lock); |
| 882 | } |
| 883 | __remove_wait_queue(&counter->waitq, &wait); |
| 884 | __set_current_state(TASK_RUNNING); |
| 885 | spin_unlock_irq(&counter->waitq.lock); |
| 886 | |
| 887 | if (usrdata->len + irqdata->len < count) |
| 888 | return -ERESTARTSYS; |
| 889 | read_pending: |
| 890 | mutex_lock(&counter->mutex); |
| 891 | |
| 892 | /* Drain pending data first: */ |
| 893 | res = perf_copy_usrdata(usrdata, buf, count); |
| 894 | if (res < 0 || res == count) |
| 895 | goto out; |
| 896 | |
| 897 | /* Switch irq buffer: */ |
| 898 | usrdata = perf_switch_irq_data(counter); |
| 899 | if (perf_copy_usrdata(usrdata, buf + res, count - res) < 0) { |
| 900 | if (!res) |
| 901 | res = -EFAULT; |
| 902 | } else { |
| 903 | res = count; |
| 904 | } |
| 905 | out: |
| 906 | mutex_unlock(&counter->mutex); |
| 907 | |
| 908 | return res; |
| 909 | } |
| 910 | |
| 911 | static ssize_t |
| 912 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 913 | { |
| 914 | struct perf_counter *counter = file->private_data; |
| 915 | |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 916 | switch (counter->hw_event.record_type) { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 917 | case PERF_RECORD_SIMPLE: |
| 918 | return perf_read_hw(counter, buf, count); |
| 919 | |
| 920 | case PERF_RECORD_IRQ: |
| 921 | case PERF_RECORD_GROUP: |
| 922 | return perf_read_irq_data(counter, buf, count, |
| 923 | file->f_flags & O_NONBLOCK); |
| 924 | } |
| 925 | return -EINVAL; |
| 926 | } |
| 927 | |
| 928 | static unsigned int perf_poll(struct file *file, poll_table *wait) |
| 929 | { |
| 930 | struct perf_counter *counter = file->private_data; |
| 931 | unsigned int events = 0; |
| 932 | unsigned long flags; |
| 933 | |
| 934 | poll_wait(file, &counter->waitq, wait); |
| 935 | |
| 936 | spin_lock_irqsave(&counter->waitq.lock, flags); |
| 937 | if (counter->usrdata->len || counter->irqdata->len) |
| 938 | events |= POLLIN; |
| 939 | spin_unlock_irqrestore(&counter->waitq.lock, flags); |
| 940 | |
| 941 | return events; |
| 942 | } |
| 943 | |
| 944 | static const struct file_operations perf_fops = { |
| 945 | .release = perf_release, |
| 946 | .read = perf_read, |
| 947 | .poll = perf_poll, |
| 948 | }; |
| 949 | |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 950 | static int cpu_clock_perf_counter_enable(struct perf_counter *counter) |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 951 | { |
Paul Mackerras | 9abf8a0 | 2009-01-09 16:26:43 +1100 | [diff] [blame] | 952 | int cpu = raw_smp_processor_id(); |
| 953 | |
| 954 | atomic64_set(&counter->hw.prev_count, cpu_clock(cpu)); |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 955 | return 0; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 956 | } |
| 957 | |
Paul Mackerras | 9abf8a0 | 2009-01-09 16:26:43 +1100 | [diff] [blame] | 958 | static void cpu_clock_perf_counter_update(struct perf_counter *counter) |
| 959 | { |
| 960 | int cpu = raw_smp_processor_id(); |
| 961 | s64 prev; |
| 962 | u64 now; |
| 963 | |
| 964 | now = cpu_clock(cpu); |
| 965 | prev = atomic64_read(&counter->hw.prev_count); |
| 966 | atomic64_set(&counter->hw.prev_count, now); |
| 967 | atomic64_add(now - prev, &counter->count); |
| 968 | } |
| 969 | |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 970 | static void cpu_clock_perf_counter_disable(struct perf_counter *counter) |
| 971 | { |
Paul Mackerras | 9abf8a0 | 2009-01-09 16:26:43 +1100 | [diff] [blame] | 972 | cpu_clock_perf_counter_update(counter); |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | static void cpu_clock_perf_counter_read(struct perf_counter *counter) |
| 976 | { |
Paul Mackerras | 9abf8a0 | 2009-01-09 16:26:43 +1100 | [diff] [blame] | 977 | cpu_clock_perf_counter_update(counter); |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | static const struct hw_perf_counter_ops perf_ops_cpu_clock = { |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 981 | .enable = cpu_clock_perf_counter_enable, |
| 982 | .disable = cpu_clock_perf_counter_disable, |
| 983 | .read = cpu_clock_perf_counter_read, |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 984 | }; |
| 985 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 986 | /* |
| 987 | * Called from within the scheduler: |
| 988 | */ |
| 989 | static u64 task_clock_perf_counter_val(struct perf_counter *counter, int update) |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 990 | { |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 991 | struct task_struct *curr = counter->task; |
| 992 | u64 delta; |
| 993 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 994 | delta = __task_delta_exec(curr, update); |
| 995 | |
| 996 | return curr->se.sum_exec_runtime + delta; |
| 997 | } |
| 998 | |
| 999 | static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now) |
| 1000 | { |
| 1001 | u64 prev; |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 1002 | s64 delta; |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 1003 | |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 1004 | prev = atomic64_read(&counter->hw.prev_count); |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 1005 | |
| 1006 | atomic64_set(&counter->hw.prev_count, now); |
| 1007 | |
| 1008 | delta = now - prev; |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 1009 | |
| 1010 | atomic64_add(delta, &counter->count); |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | static void task_clock_perf_counter_read(struct perf_counter *counter) |
| 1014 | { |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1015 | u64 now = task_clock_perf_counter_val(counter, 1); |
| 1016 | |
| 1017 | task_clock_perf_counter_update(counter, now); |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 1018 | } |
| 1019 | |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 1020 | static int task_clock_perf_counter_enable(struct perf_counter *counter) |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 1021 | { |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1022 | u64 now = task_clock_perf_counter_val(counter, 0); |
| 1023 | |
| 1024 | atomic64_set(&counter->hw.prev_count, now); |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 1025 | |
| 1026 | return 0; |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | static void task_clock_perf_counter_disable(struct perf_counter *counter) |
| 1030 | { |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1031 | u64 now = task_clock_perf_counter_val(counter, 0); |
| 1032 | |
| 1033 | task_clock_perf_counter_update(counter, now); |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | static const struct hw_perf_counter_ops perf_ops_task_clock = { |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 1037 | .enable = task_clock_perf_counter_enable, |
| 1038 | .disable = task_clock_perf_counter_disable, |
| 1039 | .read = task_clock_perf_counter_read, |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 1040 | }; |
| 1041 | |
Ingo Molnar | e06c61a | 2008-12-14 14:44:31 +0100 | [diff] [blame] | 1042 | static u64 get_page_faults(void) |
| 1043 | { |
| 1044 | struct task_struct *curr = current; |
| 1045 | |
| 1046 | return curr->maj_flt + curr->min_flt; |
| 1047 | } |
| 1048 | |
| 1049 | static void page_faults_perf_counter_update(struct perf_counter *counter) |
| 1050 | { |
| 1051 | u64 prev, now; |
| 1052 | s64 delta; |
| 1053 | |
| 1054 | prev = atomic64_read(&counter->hw.prev_count); |
| 1055 | now = get_page_faults(); |
| 1056 | |
| 1057 | atomic64_set(&counter->hw.prev_count, now); |
| 1058 | |
| 1059 | delta = now - prev; |
Ingo Molnar | e06c61a | 2008-12-14 14:44:31 +0100 | [diff] [blame] | 1060 | |
| 1061 | atomic64_add(delta, &counter->count); |
| 1062 | } |
| 1063 | |
| 1064 | static void page_faults_perf_counter_read(struct perf_counter *counter) |
| 1065 | { |
| 1066 | page_faults_perf_counter_update(counter); |
| 1067 | } |
| 1068 | |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 1069 | static int page_faults_perf_counter_enable(struct perf_counter *counter) |
Ingo Molnar | e06c61a | 2008-12-14 14:44:31 +0100 | [diff] [blame] | 1070 | { |
| 1071 | /* |
| 1072 | * page-faults is a per-task value already, |
| 1073 | * so we dont have to clear it on switch-in. |
| 1074 | */ |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 1075 | |
| 1076 | return 0; |
Ingo Molnar | e06c61a | 2008-12-14 14:44:31 +0100 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | static void page_faults_perf_counter_disable(struct perf_counter *counter) |
| 1080 | { |
| 1081 | page_faults_perf_counter_update(counter); |
| 1082 | } |
| 1083 | |
| 1084 | static const struct hw_perf_counter_ops perf_ops_page_faults = { |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 1085 | .enable = page_faults_perf_counter_enable, |
| 1086 | .disable = page_faults_perf_counter_disable, |
| 1087 | .read = page_faults_perf_counter_read, |
Ingo Molnar | e06c61a | 2008-12-14 14:44:31 +0100 | [diff] [blame] | 1088 | }; |
| 1089 | |
Ingo Molnar | 5d6a27d | 2008-12-14 12:28:33 +0100 | [diff] [blame] | 1090 | static u64 get_context_switches(void) |
| 1091 | { |
| 1092 | struct task_struct *curr = current; |
| 1093 | |
| 1094 | return curr->nvcsw + curr->nivcsw; |
| 1095 | } |
| 1096 | |
| 1097 | static void context_switches_perf_counter_update(struct perf_counter *counter) |
| 1098 | { |
| 1099 | u64 prev, now; |
| 1100 | s64 delta; |
| 1101 | |
| 1102 | prev = atomic64_read(&counter->hw.prev_count); |
| 1103 | now = get_context_switches(); |
| 1104 | |
| 1105 | atomic64_set(&counter->hw.prev_count, now); |
| 1106 | |
| 1107 | delta = now - prev; |
Ingo Molnar | 5d6a27d | 2008-12-14 12:28:33 +0100 | [diff] [blame] | 1108 | |
| 1109 | atomic64_add(delta, &counter->count); |
| 1110 | } |
| 1111 | |
| 1112 | static void context_switches_perf_counter_read(struct perf_counter *counter) |
| 1113 | { |
| 1114 | context_switches_perf_counter_update(counter); |
| 1115 | } |
| 1116 | |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 1117 | static int context_switches_perf_counter_enable(struct perf_counter *counter) |
Ingo Molnar | 5d6a27d | 2008-12-14 12:28:33 +0100 | [diff] [blame] | 1118 | { |
| 1119 | /* |
| 1120 | * ->nvcsw + curr->nivcsw is a per-task value already, |
| 1121 | * so we dont have to clear it on switch-in. |
| 1122 | */ |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 1123 | |
| 1124 | return 0; |
Ingo Molnar | 5d6a27d | 2008-12-14 12:28:33 +0100 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | static void context_switches_perf_counter_disable(struct perf_counter *counter) |
| 1128 | { |
| 1129 | context_switches_perf_counter_update(counter); |
| 1130 | } |
| 1131 | |
| 1132 | static const struct hw_perf_counter_ops perf_ops_context_switches = { |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 1133 | .enable = context_switches_perf_counter_enable, |
| 1134 | .disable = context_switches_perf_counter_disable, |
| 1135 | .read = context_switches_perf_counter_read, |
Ingo Molnar | 5d6a27d | 2008-12-14 12:28:33 +0100 | [diff] [blame] | 1136 | }; |
| 1137 | |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 1138 | static inline u64 get_cpu_migrations(void) |
| 1139 | { |
| 1140 | return current->se.nr_migrations; |
| 1141 | } |
| 1142 | |
| 1143 | static void cpu_migrations_perf_counter_update(struct perf_counter *counter) |
| 1144 | { |
| 1145 | u64 prev, now; |
| 1146 | s64 delta; |
| 1147 | |
| 1148 | prev = atomic64_read(&counter->hw.prev_count); |
| 1149 | now = get_cpu_migrations(); |
| 1150 | |
| 1151 | atomic64_set(&counter->hw.prev_count, now); |
| 1152 | |
| 1153 | delta = now - prev; |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 1154 | |
| 1155 | atomic64_add(delta, &counter->count); |
| 1156 | } |
| 1157 | |
| 1158 | static void cpu_migrations_perf_counter_read(struct perf_counter *counter) |
| 1159 | { |
| 1160 | cpu_migrations_perf_counter_update(counter); |
| 1161 | } |
| 1162 | |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 1163 | static int cpu_migrations_perf_counter_enable(struct perf_counter *counter) |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 1164 | { |
| 1165 | /* |
| 1166 | * se.nr_migrations is a per-task value already, |
| 1167 | * so we dont have to clear it on switch-in. |
| 1168 | */ |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 1169 | |
| 1170 | return 0; |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | static void cpu_migrations_perf_counter_disable(struct perf_counter *counter) |
| 1174 | { |
| 1175 | cpu_migrations_perf_counter_update(counter); |
| 1176 | } |
| 1177 | |
| 1178 | static const struct hw_perf_counter_ops perf_ops_cpu_migrations = { |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 1179 | .enable = cpu_migrations_perf_counter_enable, |
| 1180 | .disable = cpu_migrations_perf_counter_disable, |
| 1181 | .read = cpu_migrations_perf_counter_read, |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 1182 | }; |
| 1183 | |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 1184 | static const struct hw_perf_counter_ops * |
| 1185 | sw_perf_counter_init(struct perf_counter *counter) |
| 1186 | { |
| 1187 | const struct hw_perf_counter_ops *hw_ops = NULL; |
| 1188 | |
| 1189 | switch (counter->hw_event.type) { |
| 1190 | case PERF_COUNT_CPU_CLOCK: |
| 1191 | hw_ops = &perf_ops_cpu_clock; |
| 1192 | break; |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 1193 | case PERF_COUNT_TASK_CLOCK: |
| 1194 | hw_ops = &perf_ops_task_clock; |
| 1195 | break; |
Ingo Molnar | e06c61a | 2008-12-14 14:44:31 +0100 | [diff] [blame] | 1196 | case PERF_COUNT_PAGE_FAULTS: |
| 1197 | hw_ops = &perf_ops_page_faults; |
| 1198 | break; |
Ingo Molnar | 5d6a27d | 2008-12-14 12:28:33 +0100 | [diff] [blame] | 1199 | case PERF_COUNT_CONTEXT_SWITCHES: |
| 1200 | hw_ops = &perf_ops_context_switches; |
| 1201 | break; |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 1202 | case PERF_COUNT_CPU_MIGRATIONS: |
| 1203 | hw_ops = &perf_ops_cpu_migrations; |
| 1204 | break; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 1205 | default: |
| 1206 | break; |
| 1207 | } |
| 1208 | return hw_ops; |
| 1209 | } |
| 1210 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1211 | /* |
| 1212 | * Allocate and initialize a counter structure |
| 1213 | */ |
| 1214 | static struct perf_counter * |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1215 | perf_counter_alloc(struct perf_counter_hw_event *hw_event, |
| 1216 | int cpu, |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 1217 | struct perf_counter *group_leader, |
| 1218 | gfp_t gfpflags) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1219 | { |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 1220 | const struct hw_perf_counter_ops *hw_ops; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1221 | struct perf_counter *counter; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1222 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 1223 | counter = kzalloc(sizeof(*counter), gfpflags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1224 | if (!counter) |
| 1225 | return NULL; |
| 1226 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1227 | /* |
| 1228 | * Single counters are their own group leaders, with an |
| 1229 | * empty sibling list: |
| 1230 | */ |
| 1231 | if (!group_leader) |
| 1232 | group_leader = counter; |
| 1233 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1234 | mutex_init(&counter->mutex); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1235 | INIT_LIST_HEAD(&counter->list_entry); |
| 1236 | INIT_LIST_HEAD(&counter->sibling_list); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1237 | init_waitqueue_head(&counter->waitq); |
| 1238 | |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 1239 | counter->irqdata = &counter->data[0]; |
| 1240 | counter->usrdata = &counter->data[1]; |
| 1241 | counter->cpu = cpu; |
| 1242 | counter->hw_event = *hw_event; |
| 1243 | counter->wakeup_pending = 0; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1244 | counter->group_leader = group_leader; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1245 | counter->hw_ops = NULL; |
| 1246 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1247 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
Ingo Molnar | a86ed50 | 2008-12-17 00:43:10 +0100 | [diff] [blame] | 1248 | if (hw_event->disabled) |
| 1249 | counter->state = PERF_COUNTER_STATE_OFF; |
| 1250 | |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 1251 | hw_ops = NULL; |
| 1252 | if (!hw_event->raw && hw_event->type < 0) |
| 1253 | hw_ops = sw_perf_counter_init(counter); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 1254 | if (!hw_ops) |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 1255 | hw_ops = hw_perf_counter_init(counter); |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 1256 | |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1257 | if (!hw_ops) { |
| 1258 | kfree(counter); |
| 1259 | return NULL; |
| 1260 | } |
| 1261 | counter->hw_ops = hw_ops; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1262 | |
| 1263 | return counter; |
| 1264 | } |
| 1265 | |
| 1266 | /** |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 1267 | * sys_perf_task_open - open a performance counter, associate it to a task/cpu |
| 1268 | * |
| 1269 | * @hw_event_uptr: event type attributes for monitoring/sampling |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1270 | * @pid: target pid |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 1271 | * @cpu: target cpu |
| 1272 | * @group_fd: group leader counter fd |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1273 | */ |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 1274 | asmlinkage int |
| 1275 | sys_perf_counter_open(struct perf_counter_hw_event *hw_event_uptr __user, |
| 1276 | pid_t pid, int cpu, int group_fd) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1277 | { |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1278 | struct perf_counter *counter, *group_leader; |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 1279 | struct perf_counter_hw_event hw_event; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1280 | struct perf_counter_context *ctx; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 1281 | struct file *counter_file = NULL; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1282 | struct file *group_file = NULL; |
| 1283 | int fput_needed = 0; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 1284 | int fput_needed2 = 0; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1285 | int ret; |
| 1286 | |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 1287 | if (copy_from_user(&hw_event, hw_event_uptr, sizeof(hw_event)) != 0) |
Thomas Gleixner | eab656a | 2008-12-08 19:26:59 +0100 | [diff] [blame] | 1288 | return -EFAULT; |
| 1289 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1290 | /* |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 1291 | * Get the target context (task or percpu): |
| 1292 | */ |
| 1293 | ctx = find_get_context(pid, cpu); |
| 1294 | if (IS_ERR(ctx)) |
| 1295 | return PTR_ERR(ctx); |
| 1296 | |
| 1297 | /* |
| 1298 | * Look up the group leader (we will attach this counter to it): |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1299 | */ |
| 1300 | group_leader = NULL; |
| 1301 | if (group_fd != -1) { |
| 1302 | ret = -EINVAL; |
| 1303 | group_file = fget_light(group_fd, &fput_needed); |
| 1304 | if (!group_file) |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 1305 | goto err_put_context; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1306 | if (group_file->f_op != &perf_fops) |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 1307 | goto err_put_context; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1308 | |
| 1309 | group_leader = group_file->private_data; |
| 1310 | /* |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 1311 | * Do not allow a recursive hierarchy (this new sibling |
| 1312 | * becoming part of another group-sibling): |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1313 | */ |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 1314 | if (group_leader->group_leader != group_leader) |
| 1315 | goto err_put_context; |
| 1316 | /* |
| 1317 | * Do not allow to attach to a group in a different |
| 1318 | * task or CPU context: |
| 1319 | */ |
| 1320 | if (group_leader->ctx != ctx) |
| 1321 | goto err_put_context; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1322 | } |
| 1323 | |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 1324 | ret = -EINVAL; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 1325 | counter = perf_counter_alloc(&hw_event, cpu, group_leader, GFP_KERNEL); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1326 | if (!counter) |
| 1327 | goto err_put_context; |
| 1328 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1329 | ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0); |
| 1330 | if (ret < 0) |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 1331 | goto err_free_put_context; |
| 1332 | |
| 1333 | counter_file = fget_light(ret, &fput_needed2); |
| 1334 | if (!counter_file) |
| 1335 | goto err_free_put_context; |
| 1336 | |
| 1337 | counter->filp = counter_file; |
| 1338 | perf_install_in_context(ctx, counter, cpu); |
| 1339 | |
| 1340 | fput_light(counter_file, fput_needed2); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1341 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1342 | out_fput: |
| 1343 | fput_light(group_file, fput_needed); |
| 1344 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1345 | return ret; |
| 1346 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 1347 | err_free_put_context: |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1348 | kfree(counter); |
| 1349 | |
| 1350 | err_put_context: |
| 1351 | put_context(ctx); |
| 1352 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1353 | goto out_fput; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1354 | } |
| 1355 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 1356 | /* |
| 1357 | * Initialize the perf_counter context in a task_struct: |
| 1358 | */ |
| 1359 | static void |
| 1360 | __perf_counter_init_context(struct perf_counter_context *ctx, |
| 1361 | struct task_struct *task) |
| 1362 | { |
| 1363 | memset(ctx, 0, sizeof(*ctx)); |
| 1364 | spin_lock_init(&ctx->lock); |
| 1365 | INIT_LIST_HEAD(&ctx->counter_list); |
| 1366 | ctx->task = task; |
| 1367 | } |
| 1368 | |
| 1369 | /* |
| 1370 | * inherit a counter from parent task to child task: |
| 1371 | */ |
| 1372 | static int |
| 1373 | inherit_counter(struct perf_counter *parent_counter, |
| 1374 | struct task_struct *parent, |
| 1375 | struct perf_counter_context *parent_ctx, |
| 1376 | struct task_struct *child, |
| 1377 | struct perf_counter_context *child_ctx) |
| 1378 | { |
| 1379 | struct perf_counter *child_counter; |
| 1380 | |
| 1381 | child_counter = perf_counter_alloc(&parent_counter->hw_event, |
| 1382 | parent_counter->cpu, NULL, |
| 1383 | GFP_ATOMIC); |
| 1384 | if (!child_counter) |
| 1385 | return -ENOMEM; |
| 1386 | |
| 1387 | /* |
| 1388 | * Link it up in the child's context: |
| 1389 | */ |
| 1390 | child_counter->ctx = child_ctx; |
| 1391 | child_counter->task = child; |
| 1392 | list_add_counter(child_counter, child_ctx); |
| 1393 | child_ctx->nr_counters++; |
| 1394 | |
| 1395 | child_counter->parent = parent_counter; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 1396 | /* |
| 1397 | * inherit into child's child as well: |
| 1398 | */ |
| 1399 | child_counter->hw_event.inherit = 1; |
| 1400 | |
| 1401 | /* |
| 1402 | * Get a reference to the parent filp - we will fput it |
| 1403 | * when the child counter exits. This is safe to do because |
| 1404 | * we are in the parent and we know that the filp still |
| 1405 | * exists and has a nonzero count: |
| 1406 | */ |
| 1407 | atomic_long_inc(&parent_counter->filp->f_count); |
| 1408 | |
| 1409 | return 0; |
| 1410 | } |
| 1411 | |
| 1412 | static void |
| 1413 | __perf_counter_exit_task(struct task_struct *child, |
| 1414 | struct perf_counter *child_counter, |
| 1415 | struct perf_counter_context *child_ctx) |
| 1416 | { |
| 1417 | struct perf_counter *parent_counter; |
| 1418 | u64 parent_val, child_val; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 1419 | |
| 1420 | /* |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1421 | * If we do not self-reap then we have to wait for the |
| 1422 | * child task to unschedule (it will happen for sure), |
| 1423 | * so that its counter is at its final count. (This |
| 1424 | * condition triggers rarely - child tasks usually get |
| 1425 | * off their CPU before the parent has a chance to |
| 1426 | * get this far into the reaping action) |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 1427 | */ |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1428 | if (child != current) { |
| 1429 | wait_task_inactive(child, 0); |
| 1430 | list_del_init(&child_counter->list_entry); |
| 1431 | } else { |
Ingo Molnar | 0cc0c02 | 2008-12-14 23:20:36 +0100 | [diff] [blame] | 1432 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1433 | unsigned long flags; |
| 1434 | u64 perf_flags; |
| 1435 | |
| 1436 | /* |
| 1437 | * Disable and unlink this counter. |
| 1438 | * |
| 1439 | * Be careful about zapping the list - IRQ/NMI context |
| 1440 | * could still be processing it: |
| 1441 | */ |
| 1442 | curr_rq_lock_irq_save(&flags); |
| 1443 | perf_flags = hw_perf_save_disable(); |
Ingo Molnar | 0cc0c02 | 2008-12-14 23:20:36 +0100 | [diff] [blame] | 1444 | |
| 1445 | cpuctx = &__get_cpu_var(perf_cpu_context); |
| 1446 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1447 | if (child_counter->state == PERF_COUNTER_STATE_ACTIVE) { |
| 1448 | child_counter->state = PERF_COUNTER_STATE_INACTIVE; |
| 1449 | child_counter->hw_ops->disable(child_counter); |
| 1450 | cpuctx->active_oncpu--; |
| 1451 | child_ctx->nr_active--; |
| 1452 | child_counter->oncpu = -1; |
| 1453 | } |
Ingo Molnar | 0cc0c02 | 2008-12-14 23:20:36 +0100 | [diff] [blame] | 1454 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1455 | list_del_init(&child_counter->list_entry); |
| 1456 | |
| 1457 | child_ctx->nr_counters--; |
| 1458 | |
| 1459 | hw_perf_restore(perf_flags); |
| 1460 | curr_rq_unlock_irq_restore(&flags); |
Ingo Molnar | 0cc0c02 | 2008-12-14 23:20:36 +0100 | [diff] [blame] | 1461 | } |
| 1462 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 1463 | parent_counter = child_counter->parent; |
| 1464 | /* |
| 1465 | * It can happen that parent exits first, and has counters |
| 1466 | * that are still around due to the child reference. These |
| 1467 | * counters need to be zapped - but otherwise linger. |
| 1468 | */ |
| 1469 | if (!parent_counter) |
| 1470 | return; |
| 1471 | |
| 1472 | parent_val = atomic64_read(&parent_counter->count); |
| 1473 | child_val = atomic64_read(&child_counter->count); |
| 1474 | |
| 1475 | /* |
| 1476 | * Add back the child's count to the parent's count: |
| 1477 | */ |
| 1478 | atomic64_add(child_val, &parent_counter->count); |
| 1479 | |
| 1480 | fput(parent_counter->filp); |
| 1481 | |
| 1482 | kfree(child_counter); |
| 1483 | } |
| 1484 | |
| 1485 | /* |
| 1486 | * When a child task exist, feed back counter values to parent counters. |
| 1487 | * |
| 1488 | * Note: we are running in child context, but the PID is not hashed |
| 1489 | * anymore so new counters will not be added. |
| 1490 | */ |
| 1491 | void perf_counter_exit_task(struct task_struct *child) |
| 1492 | { |
| 1493 | struct perf_counter *child_counter, *tmp; |
| 1494 | struct perf_counter_context *child_ctx; |
| 1495 | |
| 1496 | child_ctx = &child->perf_counter_ctx; |
| 1497 | |
| 1498 | if (likely(!child_ctx->nr_counters)) |
| 1499 | return; |
| 1500 | |
| 1501 | list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list, |
| 1502 | list_entry) |
| 1503 | __perf_counter_exit_task(child, child_counter, child_ctx); |
| 1504 | } |
| 1505 | |
| 1506 | /* |
| 1507 | * Initialize the perf_counter context in task_struct |
| 1508 | */ |
| 1509 | void perf_counter_init_task(struct task_struct *child) |
| 1510 | { |
| 1511 | struct perf_counter_context *child_ctx, *parent_ctx; |
| 1512 | struct perf_counter *counter, *parent_counter; |
| 1513 | struct task_struct *parent = current; |
| 1514 | unsigned long flags; |
| 1515 | |
| 1516 | child_ctx = &child->perf_counter_ctx; |
| 1517 | parent_ctx = &parent->perf_counter_ctx; |
| 1518 | |
| 1519 | __perf_counter_init_context(child_ctx, child); |
| 1520 | |
| 1521 | /* |
| 1522 | * This is executed from the parent task context, so inherit |
| 1523 | * counters that have been marked for cloning: |
| 1524 | */ |
| 1525 | |
| 1526 | if (likely(!parent_ctx->nr_counters)) |
| 1527 | return; |
| 1528 | |
| 1529 | /* |
| 1530 | * Lock the parent list. No need to lock the child - not PID |
| 1531 | * hashed yet and not running, so nobody can access it. |
| 1532 | */ |
| 1533 | spin_lock_irqsave(&parent_ctx->lock, flags); |
| 1534 | |
| 1535 | /* |
| 1536 | * We dont have to disable NMIs - we are only looking at |
| 1537 | * the list, not manipulating it: |
| 1538 | */ |
| 1539 | list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) { |
| 1540 | if (!counter->hw_event.inherit || counter->group_leader != counter) |
| 1541 | continue; |
| 1542 | |
| 1543 | /* |
| 1544 | * Instead of creating recursive hierarchies of counters, |
| 1545 | * we link inheritd counters back to the original parent, |
| 1546 | * which has a filp for sure, which we use as the reference |
| 1547 | * count: |
| 1548 | */ |
| 1549 | parent_counter = counter; |
| 1550 | if (counter->parent) |
| 1551 | parent_counter = counter->parent; |
| 1552 | |
| 1553 | if (inherit_counter(parent_counter, parent, |
| 1554 | parent_ctx, child, child_ctx)) |
| 1555 | break; |
| 1556 | } |
| 1557 | |
| 1558 | spin_unlock_irqrestore(&parent_ctx->lock, flags); |
| 1559 | } |
| 1560 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1561 | static void __cpuinit perf_counter_init_cpu(int cpu) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1562 | { |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1563 | struct perf_cpu_context *cpuctx; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1564 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1565 | cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 1566 | __perf_counter_init_context(&cpuctx->ctx, NULL); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1567 | |
| 1568 | mutex_lock(&perf_resource_mutex); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1569 | cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1570 | mutex_unlock(&perf_resource_mutex); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1571 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1572 | hw_perf_counter_setup(); |
| 1573 | } |
| 1574 | |
| 1575 | #ifdef CONFIG_HOTPLUG_CPU |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1576 | static void __perf_counter_exit_cpu(void *info) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1577 | { |
| 1578 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 1579 | struct perf_counter_context *ctx = &cpuctx->ctx; |
| 1580 | struct perf_counter *counter, *tmp; |
| 1581 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1582 | list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) |
| 1583 | __perf_counter_remove_from_context(counter); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1584 | |
| 1585 | } |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1586 | static void perf_counter_exit_cpu(int cpu) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1587 | { |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1588 | smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1589 | } |
| 1590 | #else |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1591 | static inline void perf_counter_exit_cpu(int cpu) { } |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1592 | #endif |
| 1593 | |
| 1594 | static int __cpuinit |
| 1595 | perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu) |
| 1596 | { |
| 1597 | unsigned int cpu = (long)hcpu; |
| 1598 | |
| 1599 | switch (action) { |
| 1600 | |
| 1601 | case CPU_UP_PREPARE: |
| 1602 | case CPU_UP_PREPARE_FROZEN: |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1603 | perf_counter_init_cpu(cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1604 | break; |
| 1605 | |
| 1606 | case CPU_DOWN_PREPARE: |
| 1607 | case CPU_DOWN_PREPARE_FROZEN: |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1608 | perf_counter_exit_cpu(cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1609 | break; |
| 1610 | |
| 1611 | default: |
| 1612 | break; |
| 1613 | } |
| 1614 | |
| 1615 | return NOTIFY_OK; |
| 1616 | } |
| 1617 | |
| 1618 | static struct notifier_block __cpuinitdata perf_cpu_nb = { |
| 1619 | .notifier_call = perf_cpu_notify, |
| 1620 | }; |
| 1621 | |
| 1622 | static int __init perf_counter_init(void) |
| 1623 | { |
| 1624 | perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE, |
| 1625 | (void *)(long)smp_processor_id()); |
| 1626 | register_cpu_notifier(&perf_cpu_nb); |
| 1627 | |
| 1628 | return 0; |
| 1629 | } |
| 1630 | early_initcall(perf_counter_init); |
| 1631 | |
| 1632 | static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf) |
| 1633 | { |
| 1634 | return sprintf(buf, "%d\n", perf_reserved_percpu); |
| 1635 | } |
| 1636 | |
| 1637 | static ssize_t |
| 1638 | perf_set_reserve_percpu(struct sysdev_class *class, |
| 1639 | const char *buf, |
| 1640 | size_t count) |
| 1641 | { |
| 1642 | struct perf_cpu_context *cpuctx; |
| 1643 | unsigned long val; |
| 1644 | int err, cpu, mpt; |
| 1645 | |
| 1646 | err = strict_strtoul(buf, 10, &val); |
| 1647 | if (err) |
| 1648 | return err; |
| 1649 | if (val > perf_max_counters) |
| 1650 | return -EINVAL; |
| 1651 | |
| 1652 | mutex_lock(&perf_resource_mutex); |
| 1653 | perf_reserved_percpu = val; |
| 1654 | for_each_online_cpu(cpu) { |
| 1655 | cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 1656 | spin_lock_irq(&cpuctx->ctx.lock); |
| 1657 | mpt = min(perf_max_counters - cpuctx->ctx.nr_counters, |
| 1658 | perf_max_counters - perf_reserved_percpu); |
| 1659 | cpuctx->max_pertask = mpt; |
| 1660 | spin_unlock_irq(&cpuctx->ctx.lock); |
| 1661 | } |
| 1662 | mutex_unlock(&perf_resource_mutex); |
| 1663 | |
| 1664 | return count; |
| 1665 | } |
| 1666 | |
| 1667 | static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf) |
| 1668 | { |
| 1669 | return sprintf(buf, "%d\n", perf_overcommit); |
| 1670 | } |
| 1671 | |
| 1672 | static ssize_t |
| 1673 | perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count) |
| 1674 | { |
| 1675 | unsigned long val; |
| 1676 | int err; |
| 1677 | |
| 1678 | err = strict_strtoul(buf, 10, &val); |
| 1679 | if (err) |
| 1680 | return err; |
| 1681 | if (val > 1) |
| 1682 | return -EINVAL; |
| 1683 | |
| 1684 | mutex_lock(&perf_resource_mutex); |
| 1685 | perf_overcommit = val; |
| 1686 | mutex_unlock(&perf_resource_mutex); |
| 1687 | |
| 1688 | return count; |
| 1689 | } |
| 1690 | |
| 1691 | static SYSDEV_CLASS_ATTR( |
| 1692 | reserve_percpu, |
| 1693 | 0644, |
| 1694 | perf_show_reserve_percpu, |
| 1695 | perf_set_reserve_percpu |
| 1696 | ); |
| 1697 | |
| 1698 | static SYSDEV_CLASS_ATTR( |
| 1699 | overcommit, |
| 1700 | 0644, |
| 1701 | perf_show_overcommit, |
| 1702 | perf_set_overcommit |
| 1703 | ); |
| 1704 | |
| 1705 | static struct attribute *perfclass_attrs[] = { |
| 1706 | &attr_reserve_percpu.attr, |
| 1707 | &attr_overcommit.attr, |
| 1708 | NULL |
| 1709 | }; |
| 1710 | |
| 1711 | static struct attribute_group perfclass_attr_group = { |
| 1712 | .attrs = perfclass_attrs, |
| 1713 | .name = "perf_counters", |
| 1714 | }; |
| 1715 | |
| 1716 | static int __init perf_counter_sysfs_init(void) |
| 1717 | { |
| 1718 | return sysfs_create_group(&cpu_sysdev_class.kset.kobj, |
| 1719 | &perfclass_attr_group); |
| 1720 | } |
| 1721 | device_initcall(perf_counter_sysfs_init); |