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 | * |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 7 | * |
| 8 | * For licensing details see kernel-base/COPYING |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/fs.h> |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 12 | #include <linux/mm.h> |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 13 | #include <linux/cpu.h> |
| 14 | #include <linux/smp.h> |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 15 | #include <linux/file.h> |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 16 | #include <linux/poll.h> |
| 17 | #include <linux/sysfs.h> |
| 18 | #include <linux/ptrace.h> |
| 19 | #include <linux/percpu.h> |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 20 | #include <linux/vmstat.h> |
| 21 | #include <linux/hardirq.h> |
| 22 | #include <linux/rculist.h> |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 23 | #include <linux/uaccess.h> |
| 24 | #include <linux/syscalls.h> |
| 25 | #include <linux/anon_inodes.h> |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 26 | #include <linux/kernel_stat.h> |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 27 | #include <linux/perf_counter.h> |
Peter Zijlstra | 0a4a939 | 2009-03-30 19:07:05 +0200 | [diff] [blame] | 28 | #include <linux/dcache.h> |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 29 | |
Tim Blechmann | 4e193bd | 2009-03-14 14:29:25 +0100 | [diff] [blame] | 30 | #include <asm/irq_regs.h> |
| 31 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 32 | /* |
| 33 | * Each CPU has a list of per CPU counters: |
| 34 | */ |
| 35 | DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context); |
| 36 | |
Ingo Molnar | 088e285 | 2008-12-14 20:21:00 +0100 | [diff] [blame] | 37 | int perf_max_counters __read_mostly = 1; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 38 | static int perf_reserved_percpu __read_mostly; |
| 39 | static int perf_overcommit __read_mostly = 1; |
| 40 | |
| 41 | /* |
| 42 | * Mutex for (sysadmin-configurable) counter reservations: |
| 43 | */ |
| 44 | static DEFINE_MUTEX(perf_resource_mutex); |
| 45 | |
| 46 | /* |
| 47 | * Architecture provided APIs - weak aliases: |
| 48 | */ |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 49 | extern __weak const struct hw_perf_counter_ops * |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 50 | hw_perf_counter_init(struct perf_counter *counter) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 51 | { |
Paul Mackerras | ff6f054 | 2009-01-09 16:19:25 +1100 | [diff] [blame] | 52 | return NULL; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 55 | u64 __weak hw_perf_save_disable(void) { return 0; } |
Yinghai Lu | 01ea1cc | 2008-12-26 21:05:06 -0800 | [diff] [blame] | 56 | void __weak hw_perf_restore(u64 ctrl) { barrier(); } |
Paul Mackerras | 01d0287 | 2009-01-14 13:44:19 +1100 | [diff] [blame] | 57 | void __weak hw_perf_counter_setup(int cpu) { barrier(); } |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame] | 58 | int __weak hw_perf_group_sched_in(struct perf_counter *group_leader, |
| 59 | struct perf_cpu_context *cpuctx, |
| 60 | struct perf_counter_context *ctx, int cpu) |
| 61 | { |
| 62 | return 0; |
| 63 | } |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 64 | |
Paul Mackerras | 4eb96fc | 2009-01-09 17:24:34 +1100 | [diff] [blame] | 65 | void __weak perf_counter_print_debug(void) { } |
| 66 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 67 | static void |
| 68 | list_add_counter(struct perf_counter *counter, struct perf_counter_context *ctx) |
| 69 | { |
| 70 | struct perf_counter *group_leader = counter->group_leader; |
| 71 | |
| 72 | /* |
| 73 | * Depending on whether it is a standalone or sibling counter, |
| 74 | * add it straight to the context's counter list, or to the group |
| 75 | * leader's sibling list: |
| 76 | */ |
| 77 | if (counter->group_leader == counter) |
| 78 | list_add_tail(&counter->list_entry, &ctx->counter_list); |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 79 | else { |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 80 | list_add_tail(&counter->list_entry, &group_leader->sibling_list); |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 81 | group_leader->nr_siblings++; |
| 82 | } |
Peter Zijlstra | 592903c | 2009-03-13 12:21:36 +0100 | [diff] [blame] | 83 | |
| 84 | list_add_rcu(&counter->event_entry, &ctx->event_list); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static void |
| 88 | list_del_counter(struct perf_counter *counter, struct perf_counter_context *ctx) |
| 89 | { |
| 90 | struct perf_counter *sibling, *tmp; |
| 91 | |
| 92 | list_del_init(&counter->list_entry); |
Peter Zijlstra | 592903c | 2009-03-13 12:21:36 +0100 | [diff] [blame] | 93 | list_del_rcu(&counter->event_entry); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 94 | |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 95 | if (counter->group_leader != counter) |
| 96 | counter->group_leader->nr_siblings--; |
| 97 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 98 | /* |
| 99 | * If this was a group counter with sibling counters then |
| 100 | * upgrade the siblings to singleton counters by adding them |
| 101 | * to the context list directly: |
| 102 | */ |
| 103 | list_for_each_entry_safe(sibling, tmp, |
| 104 | &counter->sibling_list, list_entry) { |
| 105 | |
Peter Zijlstra | 7556423 | 2009-03-13 12:21:29 +0100 | [diff] [blame] | 106 | list_move_tail(&sibling->list_entry, &ctx->counter_list); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 107 | sibling->group_leader = sibling; |
| 108 | } |
| 109 | } |
| 110 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 111 | static void |
| 112 | counter_sched_out(struct perf_counter *counter, |
| 113 | struct perf_cpu_context *cpuctx, |
| 114 | struct perf_counter_context *ctx) |
| 115 | { |
| 116 | if (counter->state != PERF_COUNTER_STATE_ACTIVE) |
| 117 | return; |
| 118 | |
| 119 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 120 | counter->tstamp_stopped = ctx->time_now; |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 121 | counter->hw_ops->disable(counter); |
| 122 | counter->oncpu = -1; |
| 123 | |
| 124 | if (!is_software_counter(counter)) |
| 125 | cpuctx->active_oncpu--; |
| 126 | ctx->nr_active--; |
| 127 | if (counter->hw_event.exclusive || !cpuctx->active_oncpu) |
| 128 | cpuctx->exclusive = 0; |
| 129 | } |
| 130 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 131 | static void |
| 132 | group_sched_out(struct perf_counter *group_counter, |
| 133 | struct perf_cpu_context *cpuctx, |
| 134 | struct perf_counter_context *ctx) |
| 135 | { |
| 136 | struct perf_counter *counter; |
| 137 | |
| 138 | if (group_counter->state != PERF_COUNTER_STATE_ACTIVE) |
| 139 | return; |
| 140 | |
| 141 | counter_sched_out(group_counter, cpuctx, ctx); |
| 142 | |
| 143 | /* |
| 144 | * Schedule out siblings (if any): |
| 145 | */ |
| 146 | list_for_each_entry(counter, &group_counter->sibling_list, list_entry) |
| 147 | counter_sched_out(counter, cpuctx, ctx); |
| 148 | |
| 149 | if (group_counter->hw_event.exclusive) |
| 150 | cpuctx->exclusive = 0; |
| 151 | } |
| 152 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 153 | /* |
| 154 | * Cross CPU call to remove a performance counter |
| 155 | * |
| 156 | * We disable the counter on the hardware level first. After that we |
| 157 | * remove it from the context list. |
| 158 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 159 | static void __perf_counter_remove_from_context(void *info) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 160 | { |
| 161 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 162 | struct perf_counter *counter = info; |
| 163 | struct perf_counter_context *ctx = counter->ctx; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 164 | unsigned long flags; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 165 | u64 perf_flags; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | * If this is a task context, we need to check whether it is |
| 169 | * the current task context of this cpu. If not it has been |
| 170 | * scheduled out before the smp call arrived. |
| 171 | */ |
| 172 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 173 | return; |
| 174 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 175 | curr_rq_lock_irq_save(&flags); |
| 176 | spin_lock(&ctx->lock); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 177 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 178 | counter_sched_out(counter, cpuctx, ctx); |
| 179 | |
| 180 | counter->task = NULL; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 181 | ctx->nr_counters--; |
| 182 | |
| 183 | /* |
| 184 | * Protect the list operation against NMI by disabling the |
| 185 | * counters on a global level. NOP for non NMI based counters. |
| 186 | */ |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 187 | perf_flags = hw_perf_save_disable(); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 188 | list_del_counter(counter, ctx); |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 189 | hw_perf_restore(perf_flags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 190 | |
| 191 | if (!ctx->task) { |
| 192 | /* |
| 193 | * Allow more per task counters with respect to the |
| 194 | * reservation: |
| 195 | */ |
| 196 | cpuctx->max_pertask = |
| 197 | min(perf_max_counters - ctx->nr_counters, |
| 198 | perf_max_counters - perf_reserved_percpu); |
| 199 | } |
| 200 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 201 | spin_unlock(&ctx->lock); |
| 202 | curr_rq_unlock_irq_restore(&flags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | |
| 206 | /* |
| 207 | * Remove the counter from a task's (or a CPU's) list of counters. |
| 208 | * |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 209 | * Must be called with counter->mutex and ctx->mutex held. |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 210 | * |
| 211 | * CPU counters are removed with a smp call. For task counters we only |
| 212 | * call when the task is on a CPU. |
| 213 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 214 | static void perf_counter_remove_from_context(struct perf_counter *counter) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 215 | { |
| 216 | struct perf_counter_context *ctx = counter->ctx; |
| 217 | struct task_struct *task = ctx->task; |
| 218 | |
| 219 | if (!task) { |
| 220 | /* |
| 221 | * Per cpu counters are removed via an smp call and |
| 222 | * the removal is always sucessful. |
| 223 | */ |
| 224 | smp_call_function_single(counter->cpu, |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 225 | __perf_counter_remove_from_context, |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 226 | counter, 1); |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | retry: |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 231 | task_oncpu_function_call(task, __perf_counter_remove_from_context, |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 232 | counter); |
| 233 | |
| 234 | spin_lock_irq(&ctx->lock); |
| 235 | /* |
| 236 | * If the context is active we need to retry the smp call. |
| 237 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 238 | if (ctx->nr_active && !list_empty(&counter->list_entry)) { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 239 | spin_unlock_irq(&ctx->lock); |
| 240 | goto retry; |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * The lock prevents that this context is scheduled in so we |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 245 | * can remove the counter safely, if the call above did not |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 246 | * succeed. |
| 247 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 248 | if (!list_empty(&counter->list_entry)) { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 249 | ctx->nr_counters--; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 250 | list_del_counter(counter, ctx); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 251 | counter->task = NULL; |
| 252 | } |
| 253 | spin_unlock_irq(&ctx->lock); |
| 254 | } |
| 255 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 256 | /* |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 257 | * Get the current time for this context. |
| 258 | * If this is a task context, we use the task's task clock, |
| 259 | * or for a per-cpu context, we use the cpu clock. |
| 260 | */ |
| 261 | static u64 get_context_time(struct perf_counter_context *ctx, int update) |
| 262 | { |
| 263 | struct task_struct *curr = ctx->task; |
| 264 | |
| 265 | if (!curr) |
| 266 | return cpu_clock(smp_processor_id()); |
| 267 | |
| 268 | return __task_delta_exec(curr, update) + curr->se.sum_exec_runtime; |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Update the record of the current time in a context. |
| 273 | */ |
| 274 | static void update_context_time(struct perf_counter_context *ctx, int update) |
| 275 | { |
| 276 | ctx->time_now = get_context_time(ctx, update) - ctx->time_lost; |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * Update the total_time_enabled and total_time_running fields for a counter. |
| 281 | */ |
| 282 | static void update_counter_times(struct perf_counter *counter) |
| 283 | { |
| 284 | struct perf_counter_context *ctx = counter->ctx; |
| 285 | u64 run_end; |
| 286 | |
| 287 | if (counter->state >= PERF_COUNTER_STATE_INACTIVE) { |
| 288 | counter->total_time_enabled = ctx->time_now - |
| 289 | counter->tstamp_enabled; |
| 290 | if (counter->state == PERF_COUNTER_STATE_INACTIVE) |
| 291 | run_end = counter->tstamp_stopped; |
| 292 | else |
| 293 | run_end = ctx->time_now; |
| 294 | counter->total_time_running = run_end - counter->tstamp_running; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Update total_time_enabled and total_time_running for all counters in a group. |
| 300 | */ |
| 301 | static void update_group_times(struct perf_counter *leader) |
| 302 | { |
| 303 | struct perf_counter *counter; |
| 304 | |
| 305 | update_counter_times(leader); |
| 306 | list_for_each_entry(counter, &leader->sibling_list, list_entry) |
| 307 | update_counter_times(counter); |
| 308 | } |
| 309 | |
| 310 | /* |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 311 | * Cross CPU call to disable a performance counter |
| 312 | */ |
| 313 | static void __perf_counter_disable(void *info) |
| 314 | { |
| 315 | struct perf_counter *counter = info; |
| 316 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 317 | struct perf_counter_context *ctx = counter->ctx; |
| 318 | unsigned long flags; |
| 319 | |
| 320 | /* |
| 321 | * If this is a per-task counter, need to check whether this |
| 322 | * counter's task is the current task on this cpu. |
| 323 | */ |
| 324 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 325 | return; |
| 326 | |
| 327 | curr_rq_lock_irq_save(&flags); |
| 328 | spin_lock(&ctx->lock); |
| 329 | |
| 330 | /* |
| 331 | * If the counter is on, turn it off. |
| 332 | * If it is in error state, leave it in error state. |
| 333 | */ |
| 334 | if (counter->state >= PERF_COUNTER_STATE_INACTIVE) { |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 335 | update_context_time(ctx, 1); |
| 336 | update_counter_times(counter); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 337 | if (counter == counter->group_leader) |
| 338 | group_sched_out(counter, cpuctx, ctx); |
| 339 | else |
| 340 | counter_sched_out(counter, cpuctx, ctx); |
| 341 | counter->state = PERF_COUNTER_STATE_OFF; |
| 342 | } |
| 343 | |
| 344 | spin_unlock(&ctx->lock); |
| 345 | curr_rq_unlock_irq_restore(&flags); |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | * Disable a counter. |
| 350 | */ |
| 351 | static void perf_counter_disable(struct perf_counter *counter) |
| 352 | { |
| 353 | struct perf_counter_context *ctx = counter->ctx; |
| 354 | struct task_struct *task = ctx->task; |
| 355 | |
| 356 | if (!task) { |
| 357 | /* |
| 358 | * Disable the counter on the cpu that it's on |
| 359 | */ |
| 360 | smp_call_function_single(counter->cpu, __perf_counter_disable, |
| 361 | counter, 1); |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | retry: |
| 366 | task_oncpu_function_call(task, __perf_counter_disable, counter); |
| 367 | |
| 368 | spin_lock_irq(&ctx->lock); |
| 369 | /* |
| 370 | * If the counter is still active, we need to retry the cross-call. |
| 371 | */ |
| 372 | if (counter->state == PERF_COUNTER_STATE_ACTIVE) { |
| 373 | spin_unlock_irq(&ctx->lock); |
| 374 | goto retry; |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * Since we have the lock this context can't be scheduled |
| 379 | * in, so we can change the state safely. |
| 380 | */ |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 381 | if (counter->state == PERF_COUNTER_STATE_INACTIVE) { |
| 382 | update_counter_times(counter); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 383 | counter->state = PERF_COUNTER_STATE_OFF; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 384 | } |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 385 | |
| 386 | spin_unlock_irq(&ctx->lock); |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Disable a counter and all its children. |
| 391 | */ |
| 392 | static void perf_counter_disable_family(struct perf_counter *counter) |
| 393 | { |
| 394 | struct perf_counter *child; |
| 395 | |
| 396 | perf_counter_disable(counter); |
| 397 | |
| 398 | /* |
| 399 | * Lock the mutex to protect the list of children |
| 400 | */ |
| 401 | mutex_lock(&counter->mutex); |
| 402 | list_for_each_entry(child, &counter->child_list, child_list) |
| 403 | perf_counter_disable(child); |
| 404 | mutex_unlock(&counter->mutex); |
| 405 | } |
| 406 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 407 | static int |
| 408 | counter_sched_in(struct perf_counter *counter, |
| 409 | struct perf_cpu_context *cpuctx, |
| 410 | struct perf_counter_context *ctx, |
| 411 | int cpu) |
| 412 | { |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 413 | if (counter->state <= PERF_COUNTER_STATE_OFF) |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 414 | return 0; |
| 415 | |
| 416 | counter->state = PERF_COUNTER_STATE_ACTIVE; |
| 417 | counter->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */ |
| 418 | /* |
| 419 | * The new state must be visible before we turn it on in the hardware: |
| 420 | */ |
| 421 | smp_wmb(); |
| 422 | |
| 423 | if (counter->hw_ops->enable(counter)) { |
| 424 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
| 425 | counter->oncpu = -1; |
| 426 | return -EAGAIN; |
| 427 | } |
| 428 | |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 429 | counter->tstamp_running += ctx->time_now - counter->tstamp_stopped; |
| 430 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 431 | if (!is_software_counter(counter)) |
| 432 | cpuctx->active_oncpu++; |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 433 | ctx->nr_active++; |
| 434 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 435 | if (counter->hw_event.exclusive) |
| 436 | cpuctx->exclusive = 1; |
| 437 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 438 | return 0; |
| 439 | } |
| 440 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 441 | /* |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 442 | * Return 1 for a group consisting entirely of software counters, |
| 443 | * 0 if the group contains any hardware counters. |
| 444 | */ |
| 445 | static int is_software_only_group(struct perf_counter *leader) |
| 446 | { |
| 447 | struct perf_counter *counter; |
| 448 | |
| 449 | if (!is_software_counter(leader)) |
| 450 | return 0; |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 451 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 452 | list_for_each_entry(counter, &leader->sibling_list, list_entry) |
| 453 | if (!is_software_counter(counter)) |
| 454 | return 0; |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 455 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 456 | return 1; |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | * Work out whether we can put this counter group on the CPU now. |
| 461 | */ |
| 462 | static int group_can_go_on(struct perf_counter *counter, |
| 463 | struct perf_cpu_context *cpuctx, |
| 464 | int can_add_hw) |
| 465 | { |
| 466 | /* |
| 467 | * Groups consisting entirely of software counters can always go on. |
| 468 | */ |
| 469 | if (is_software_only_group(counter)) |
| 470 | return 1; |
| 471 | /* |
| 472 | * If an exclusive group is already on, no other hardware |
| 473 | * counters can go on. |
| 474 | */ |
| 475 | if (cpuctx->exclusive) |
| 476 | return 0; |
| 477 | /* |
| 478 | * If this group is exclusive and there are already |
| 479 | * counters on the CPU, it can't go on. |
| 480 | */ |
| 481 | if (counter->hw_event.exclusive && cpuctx->active_oncpu) |
| 482 | return 0; |
| 483 | /* |
| 484 | * Otherwise, try to add it if all previous groups were able |
| 485 | * to go on. |
| 486 | */ |
| 487 | return can_add_hw; |
| 488 | } |
| 489 | |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 490 | static void add_counter_to_ctx(struct perf_counter *counter, |
| 491 | struct perf_counter_context *ctx) |
| 492 | { |
| 493 | list_add_counter(counter, ctx); |
| 494 | ctx->nr_counters++; |
| 495 | counter->prev_state = PERF_COUNTER_STATE_OFF; |
| 496 | counter->tstamp_enabled = ctx->time_now; |
| 497 | counter->tstamp_running = ctx->time_now; |
| 498 | counter->tstamp_stopped = ctx->time_now; |
| 499 | } |
| 500 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 501 | /* |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 502 | * Cross CPU call to install and enable a performance counter |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 503 | */ |
| 504 | static void __perf_install_in_context(void *info) |
| 505 | { |
| 506 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 507 | struct perf_counter *counter = info; |
| 508 | struct perf_counter_context *ctx = counter->ctx; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 509 | struct perf_counter *leader = counter->group_leader; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 510 | int cpu = smp_processor_id(); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 511 | unsigned long flags; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 512 | u64 perf_flags; |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 513 | int err; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 514 | |
| 515 | /* |
| 516 | * If this is a task context, we need to check whether it is |
| 517 | * the current task context of this cpu. If not it has been |
| 518 | * scheduled out before the smp call arrived. |
| 519 | */ |
| 520 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 521 | return; |
| 522 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 523 | curr_rq_lock_irq_save(&flags); |
| 524 | spin_lock(&ctx->lock); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 525 | update_context_time(ctx, 1); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 526 | |
| 527 | /* |
| 528 | * Protect the list operation against NMI by disabling the |
| 529 | * counters on a global level. NOP for non NMI based counters. |
| 530 | */ |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 531 | perf_flags = hw_perf_save_disable(); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 532 | |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 533 | add_counter_to_ctx(counter, ctx); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 534 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 535 | /* |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 536 | * Don't put the counter on if it is disabled or if |
| 537 | * it is in a group and the group isn't on. |
| 538 | */ |
| 539 | if (counter->state != PERF_COUNTER_STATE_INACTIVE || |
| 540 | (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE)) |
| 541 | goto unlock; |
| 542 | |
| 543 | /* |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 544 | * An exclusive counter can't go on if there are already active |
| 545 | * hardware counters, and no hardware counter can go on if there |
| 546 | * is already an exclusive counter on. |
| 547 | */ |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 548 | if (!group_can_go_on(counter, cpuctx, 1)) |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 549 | err = -EEXIST; |
| 550 | else |
| 551 | err = counter_sched_in(counter, cpuctx, ctx, cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 552 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 553 | if (err) { |
| 554 | /* |
| 555 | * This counter couldn't go on. If it is in a group |
| 556 | * then we have to pull the whole group off. |
| 557 | * If the counter group is pinned then put it in error state. |
| 558 | */ |
| 559 | if (leader != counter) |
| 560 | group_sched_out(leader, cpuctx, ctx); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 561 | if (leader->hw_event.pinned) { |
| 562 | update_group_times(leader); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 563 | leader->state = PERF_COUNTER_STATE_ERROR; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 564 | } |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 565 | } |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 566 | |
| 567 | if (!err && !ctx->task && cpuctx->max_pertask) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 568 | cpuctx->max_pertask--; |
| 569 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 570 | unlock: |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 571 | hw_perf_restore(perf_flags); |
| 572 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 573 | spin_unlock(&ctx->lock); |
| 574 | curr_rq_unlock_irq_restore(&flags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /* |
| 578 | * Attach a performance counter to a context |
| 579 | * |
| 580 | * First we add the counter to the list with the hardware enable bit |
| 581 | * in counter->hw_config cleared. |
| 582 | * |
| 583 | * If the counter is attached to a task which is on a CPU we use a smp |
| 584 | * call to enable it in the task context. The task might have been |
| 585 | * scheduled away, but we check this in the smp call again. |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 586 | * |
| 587 | * Must be called with ctx->mutex held. |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 588 | */ |
| 589 | static void |
| 590 | perf_install_in_context(struct perf_counter_context *ctx, |
| 591 | struct perf_counter *counter, |
| 592 | int cpu) |
| 593 | { |
| 594 | struct task_struct *task = ctx->task; |
| 595 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 596 | if (!task) { |
| 597 | /* |
| 598 | * Per cpu counters are installed via an smp call and |
| 599 | * the install is always sucessful. |
| 600 | */ |
| 601 | smp_call_function_single(cpu, __perf_install_in_context, |
| 602 | counter, 1); |
| 603 | return; |
| 604 | } |
| 605 | |
| 606 | counter->task = task; |
| 607 | retry: |
| 608 | task_oncpu_function_call(task, __perf_install_in_context, |
| 609 | counter); |
| 610 | |
| 611 | spin_lock_irq(&ctx->lock); |
| 612 | /* |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 613 | * we need to retry the smp call. |
| 614 | */ |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 615 | if (ctx->is_active && list_empty(&counter->list_entry)) { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 616 | spin_unlock_irq(&ctx->lock); |
| 617 | goto retry; |
| 618 | } |
| 619 | |
| 620 | /* |
| 621 | * The lock prevents that this context is scheduled in so we |
| 622 | * can add the counter safely, if it the call above did not |
| 623 | * succeed. |
| 624 | */ |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 625 | if (list_empty(&counter->list_entry)) |
| 626 | add_counter_to_ctx(counter, ctx); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 627 | spin_unlock_irq(&ctx->lock); |
| 628 | } |
| 629 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 630 | /* |
| 631 | * Cross CPU call to enable a performance counter |
| 632 | */ |
| 633 | static void __perf_counter_enable(void *info) |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 634 | { |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 635 | struct perf_counter *counter = info; |
| 636 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 637 | struct perf_counter_context *ctx = counter->ctx; |
| 638 | struct perf_counter *leader = counter->group_leader; |
| 639 | unsigned long flags; |
| 640 | int err; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 641 | |
| 642 | /* |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 643 | * If this is a per-task counter, need to check whether this |
| 644 | * counter's task is the current task on this cpu. |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 645 | */ |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 646 | if (ctx->task && cpuctx->task_ctx != ctx) |
| 647 | return; |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 648 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 649 | curr_rq_lock_irq_save(&flags); |
| 650 | spin_lock(&ctx->lock); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 651 | update_context_time(ctx, 1); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 652 | |
Paul Mackerras | c07c99b | 2009-02-13 22:10:34 +1100 | [diff] [blame] | 653 | counter->prev_state = counter->state; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 654 | if (counter->state >= PERF_COUNTER_STATE_INACTIVE) |
| 655 | goto unlock; |
| 656 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 657 | counter->tstamp_enabled = ctx->time_now - counter->total_time_enabled; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 658 | |
| 659 | /* |
| 660 | * If the counter is in a group and isn't the group leader, |
| 661 | * then don't put it on unless the group is on. |
| 662 | */ |
| 663 | if (leader != counter && leader->state != PERF_COUNTER_STATE_ACTIVE) |
| 664 | goto unlock; |
| 665 | |
| 666 | if (!group_can_go_on(counter, cpuctx, 1)) |
| 667 | err = -EEXIST; |
| 668 | else |
| 669 | err = counter_sched_in(counter, cpuctx, ctx, |
| 670 | smp_processor_id()); |
| 671 | |
| 672 | if (err) { |
| 673 | /* |
| 674 | * If this counter can't go on and it's part of a |
| 675 | * group, then the whole group has to come off. |
| 676 | */ |
| 677 | if (leader != counter) |
| 678 | group_sched_out(leader, cpuctx, ctx); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 679 | if (leader->hw_event.pinned) { |
| 680 | update_group_times(leader); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 681 | leader->state = PERF_COUNTER_STATE_ERROR; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 682 | } |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | unlock: |
| 686 | spin_unlock(&ctx->lock); |
| 687 | curr_rq_unlock_irq_restore(&flags); |
| 688 | } |
| 689 | |
| 690 | /* |
| 691 | * Enable a counter. |
| 692 | */ |
| 693 | static void perf_counter_enable(struct perf_counter *counter) |
| 694 | { |
| 695 | struct perf_counter_context *ctx = counter->ctx; |
| 696 | struct task_struct *task = ctx->task; |
| 697 | |
| 698 | if (!task) { |
| 699 | /* |
| 700 | * Enable the counter on the cpu that it's on |
| 701 | */ |
| 702 | smp_call_function_single(counter->cpu, __perf_counter_enable, |
| 703 | counter, 1); |
| 704 | return; |
| 705 | } |
| 706 | |
| 707 | spin_lock_irq(&ctx->lock); |
| 708 | if (counter->state >= PERF_COUNTER_STATE_INACTIVE) |
| 709 | goto out; |
| 710 | |
| 711 | /* |
| 712 | * If the counter is in error state, clear that first. |
| 713 | * That way, if we see the counter in error state below, we |
| 714 | * know that it has gone back into error state, as distinct |
| 715 | * from the task having been scheduled away before the |
| 716 | * cross-call arrived. |
| 717 | */ |
| 718 | if (counter->state == PERF_COUNTER_STATE_ERROR) |
| 719 | counter->state = PERF_COUNTER_STATE_OFF; |
| 720 | |
| 721 | retry: |
| 722 | spin_unlock_irq(&ctx->lock); |
| 723 | task_oncpu_function_call(task, __perf_counter_enable, counter); |
| 724 | |
| 725 | spin_lock_irq(&ctx->lock); |
| 726 | |
| 727 | /* |
| 728 | * If the context is active and the counter is still off, |
| 729 | * we need to retry the cross-call. |
| 730 | */ |
| 731 | if (ctx->is_active && counter->state == PERF_COUNTER_STATE_OFF) |
| 732 | goto retry; |
| 733 | |
| 734 | /* |
| 735 | * Since we have the lock this context can't be scheduled |
| 736 | * in, so we can change the state safely. |
| 737 | */ |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 738 | if (counter->state == PERF_COUNTER_STATE_OFF) { |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 739 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 740 | counter->tstamp_enabled = ctx->time_now - |
| 741 | counter->total_time_enabled; |
| 742 | } |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 743 | out: |
| 744 | spin_unlock_irq(&ctx->lock); |
| 745 | } |
| 746 | |
| 747 | /* |
| 748 | * Enable a counter and all its children. |
| 749 | */ |
| 750 | static void perf_counter_enable_family(struct perf_counter *counter) |
| 751 | { |
| 752 | struct perf_counter *child; |
| 753 | |
| 754 | perf_counter_enable(counter); |
| 755 | |
| 756 | /* |
| 757 | * Lock the mutex to protect the list of children |
| 758 | */ |
| 759 | mutex_lock(&counter->mutex); |
| 760 | list_for_each_entry(child, &counter->child_list, child_list) |
| 761 | perf_counter_enable(child); |
| 762 | mutex_unlock(&counter->mutex); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 763 | } |
| 764 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 765 | void __perf_counter_sched_out(struct perf_counter_context *ctx, |
| 766 | struct perf_cpu_context *cpuctx) |
| 767 | { |
| 768 | struct perf_counter *counter; |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame] | 769 | u64 flags; |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 770 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 771 | spin_lock(&ctx->lock); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 772 | ctx->is_active = 0; |
| 773 | if (likely(!ctx->nr_counters)) |
| 774 | goto out; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 775 | update_context_time(ctx, 0); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 776 | |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame] | 777 | flags = hw_perf_save_disable(); |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 778 | if (ctx->nr_active) { |
| 779 | list_for_each_entry(counter, &ctx->counter_list, list_entry) |
| 780 | group_sched_out(counter, cpuctx, ctx); |
| 781 | } |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame] | 782 | hw_perf_restore(flags); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 783 | out: |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 784 | spin_unlock(&ctx->lock); |
| 785 | } |
| 786 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 787 | /* |
| 788 | * Called from scheduler to remove the counters of the current task, |
| 789 | * with interrupts disabled. |
| 790 | * |
| 791 | * We stop each counter and update the counter value in counter->count. |
| 792 | * |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 793 | * This does not protect us against NMI, but disable() |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 794 | * sets the disabled bit in the control field of counter _before_ |
| 795 | * accessing the counter control register. If a NMI hits, then it will |
| 796 | * not restart the counter. |
| 797 | */ |
| 798 | void perf_counter_task_sched_out(struct task_struct *task, int cpu) |
| 799 | { |
| 800 | struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 801 | struct perf_counter_context *ctx = &task->perf_counter_ctx; |
Peter Zijlstra | 4a0deca | 2009-03-19 20:26:12 +0100 | [diff] [blame] | 802 | struct pt_regs *regs; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 803 | |
| 804 | if (likely(!cpuctx->task_ctx)) |
| 805 | return; |
| 806 | |
Peter Zijlstra | 4a0deca | 2009-03-19 20:26:12 +0100 | [diff] [blame] | 807 | regs = task_pt_regs(task); |
| 808 | perf_swcounter_event(PERF_COUNT_CONTEXT_SWITCHES, 1, 1, regs); |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 809 | __perf_counter_sched_out(ctx, cpuctx); |
| 810 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 811 | cpuctx->task_ctx = NULL; |
| 812 | } |
| 813 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 814 | static void perf_counter_cpu_sched_out(struct perf_cpu_context *cpuctx) |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 815 | { |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 816 | __perf_counter_sched_out(&cpuctx->ctx, cpuctx); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 817 | } |
| 818 | |
Ingo Molnar | 7995888 | 2008-12-17 08:54:56 +0100 | [diff] [blame] | 819 | static int |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 820 | group_sched_in(struct perf_counter *group_counter, |
| 821 | struct perf_cpu_context *cpuctx, |
| 822 | struct perf_counter_context *ctx, |
| 823 | int cpu) |
| 824 | { |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 825 | struct perf_counter *counter, *partial_group; |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame] | 826 | int ret; |
| 827 | |
| 828 | if (group_counter->state == PERF_COUNTER_STATE_OFF) |
| 829 | return 0; |
| 830 | |
| 831 | ret = hw_perf_group_sched_in(group_counter, cpuctx, ctx, cpu); |
| 832 | if (ret) |
| 833 | return ret < 0 ? ret : 0; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 834 | |
Paul Mackerras | c07c99b | 2009-02-13 22:10:34 +1100 | [diff] [blame] | 835 | group_counter->prev_state = group_counter->state; |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 836 | if (counter_sched_in(group_counter, cpuctx, ctx, cpu)) |
| 837 | return -EAGAIN; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 838 | |
| 839 | /* |
| 840 | * Schedule in siblings as one group (if any): |
| 841 | */ |
Ingo Molnar | 7995888 | 2008-12-17 08:54:56 +0100 | [diff] [blame] | 842 | list_for_each_entry(counter, &group_counter->sibling_list, list_entry) { |
Paul Mackerras | c07c99b | 2009-02-13 22:10:34 +1100 | [diff] [blame] | 843 | counter->prev_state = counter->state; |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 844 | if (counter_sched_in(counter, cpuctx, ctx, cpu)) { |
| 845 | partial_group = counter; |
| 846 | goto group_error; |
| 847 | } |
Ingo Molnar | 7995888 | 2008-12-17 08:54:56 +0100 | [diff] [blame] | 848 | } |
| 849 | |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame] | 850 | return 0; |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 851 | |
| 852 | group_error: |
| 853 | /* |
| 854 | * Groups can be scheduled in as one unit only, so undo any |
| 855 | * partial group before returning: |
| 856 | */ |
| 857 | list_for_each_entry(counter, &group_counter->sibling_list, list_entry) { |
| 858 | if (counter == partial_group) |
| 859 | break; |
| 860 | counter_sched_out(counter, cpuctx, ctx); |
| 861 | } |
| 862 | counter_sched_out(group_counter, cpuctx, ctx); |
| 863 | |
| 864 | return -EAGAIN; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 865 | } |
| 866 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 867 | static void |
| 868 | __perf_counter_sched_in(struct perf_counter_context *ctx, |
| 869 | struct perf_cpu_context *cpuctx, int cpu) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 870 | { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 871 | struct perf_counter *counter; |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame] | 872 | u64 flags; |
Paul Mackerras | dd0e6ba | 2009-01-12 15:11:00 +1100 | [diff] [blame] | 873 | int can_add_hw = 1; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 874 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 875 | spin_lock(&ctx->lock); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 876 | ctx->is_active = 1; |
| 877 | if (likely(!ctx->nr_counters)) |
| 878 | goto out; |
| 879 | |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 880 | /* |
| 881 | * Add any time since the last sched_out to the lost time |
| 882 | * so it doesn't get included in the total_time_enabled and |
| 883 | * total_time_running measures for counters in the context. |
| 884 | */ |
| 885 | ctx->time_lost = get_context_time(ctx, 0) - ctx->time_now; |
| 886 | |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame] | 887 | flags = hw_perf_save_disable(); |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 888 | |
| 889 | /* |
| 890 | * First go through the list and put on any pinned groups |
| 891 | * in order to give them the best chance of going on. |
| 892 | */ |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 893 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 894 | if (counter->state <= PERF_COUNTER_STATE_OFF || |
| 895 | !counter->hw_event.pinned) |
| 896 | continue; |
| 897 | if (counter->cpu != -1 && counter->cpu != cpu) |
| 898 | continue; |
| 899 | |
| 900 | if (group_can_go_on(counter, cpuctx, 1)) |
| 901 | group_sched_in(counter, cpuctx, ctx, cpu); |
| 902 | |
| 903 | /* |
| 904 | * If this pinned group hasn't been scheduled, |
| 905 | * put it in error state. |
| 906 | */ |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 907 | if (counter->state == PERF_COUNTER_STATE_INACTIVE) { |
| 908 | update_group_times(counter); |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 909 | counter->state = PERF_COUNTER_STATE_ERROR; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 910 | } |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
| 914 | /* |
| 915 | * Ignore counters in OFF or ERROR state, and |
| 916 | * ignore pinned counters since we did them already. |
| 917 | */ |
| 918 | if (counter->state <= PERF_COUNTER_STATE_OFF || |
| 919 | counter->hw_event.pinned) |
| 920 | continue; |
| 921 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 922 | /* |
| 923 | * Listen to the 'cpu' scheduling filter constraint |
| 924 | * of counters: |
| 925 | */ |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 926 | if (counter->cpu != -1 && counter->cpu != cpu) |
| 927 | continue; |
| 928 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 929 | if (group_can_go_on(counter, cpuctx, can_add_hw)) { |
Paul Mackerras | dd0e6ba | 2009-01-12 15:11:00 +1100 | [diff] [blame] | 930 | if (group_sched_in(counter, cpuctx, ctx, cpu)) |
| 931 | can_add_hw = 0; |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 932 | } |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 933 | } |
Paul Mackerras | 3cbed42 | 2009-01-09 16:43:42 +1100 | [diff] [blame] | 934 | hw_perf_restore(flags); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 935 | out: |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 936 | spin_unlock(&ctx->lock); |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 937 | } |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 938 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 939 | /* |
| 940 | * Called from scheduler to add the counters of the current task |
| 941 | * with interrupts disabled. |
| 942 | * |
| 943 | * We restore the counter value and then enable it. |
| 944 | * |
| 945 | * This does not protect us against NMI, but enable() |
| 946 | * sets the enabled bit in the control field of counter _before_ |
| 947 | * accessing the counter control register. If a NMI hits, then it will |
| 948 | * keep the counter running. |
| 949 | */ |
| 950 | void perf_counter_task_sched_in(struct task_struct *task, int cpu) |
| 951 | { |
| 952 | struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 953 | struct perf_counter_context *ctx = &task->perf_counter_ctx; |
| 954 | |
| 955 | __perf_counter_sched_in(ctx, cpuctx, cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 956 | cpuctx->task_ctx = ctx; |
| 957 | } |
| 958 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 959 | static void perf_counter_cpu_sched_in(struct perf_cpu_context *cpuctx, int cpu) |
| 960 | { |
| 961 | struct perf_counter_context *ctx = &cpuctx->ctx; |
| 962 | |
| 963 | __perf_counter_sched_in(ctx, cpuctx, cpu); |
| 964 | } |
| 965 | |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 966 | int perf_counter_task_disable(void) |
| 967 | { |
| 968 | struct task_struct *curr = current; |
| 969 | struct perf_counter_context *ctx = &curr->perf_counter_ctx; |
| 970 | struct perf_counter *counter; |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 971 | unsigned long flags; |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 972 | u64 perf_flags; |
| 973 | int cpu; |
| 974 | |
| 975 | if (likely(!ctx->nr_counters)) |
| 976 | return 0; |
| 977 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 978 | curr_rq_lock_irq_save(&flags); |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 979 | cpu = smp_processor_id(); |
| 980 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 981 | /* force the update of the task clock: */ |
| 982 | __task_delta_exec(curr, 1); |
| 983 | |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 984 | perf_counter_task_sched_out(curr, cpu); |
| 985 | |
| 986 | spin_lock(&ctx->lock); |
| 987 | |
| 988 | /* |
| 989 | * Disable all the counters: |
| 990 | */ |
| 991 | perf_flags = hw_perf_save_disable(); |
| 992 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 993 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 994 | if (counter->state != PERF_COUNTER_STATE_ERROR) { |
| 995 | update_group_times(counter); |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 996 | counter->state = PERF_COUNTER_STATE_OFF; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 997 | } |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 998 | } |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 999 | |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 1000 | hw_perf_restore(perf_flags); |
| 1001 | |
| 1002 | spin_unlock(&ctx->lock); |
| 1003 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1004 | curr_rq_unlock_irq_restore(&flags); |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 1005 | |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | int perf_counter_task_enable(void) |
| 1010 | { |
| 1011 | struct task_struct *curr = current; |
| 1012 | struct perf_counter_context *ctx = &curr->perf_counter_ctx; |
| 1013 | struct perf_counter *counter; |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1014 | unsigned long flags; |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 1015 | u64 perf_flags; |
| 1016 | int cpu; |
| 1017 | |
| 1018 | if (likely(!ctx->nr_counters)) |
| 1019 | return 0; |
| 1020 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1021 | curr_rq_lock_irq_save(&flags); |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 1022 | cpu = smp_processor_id(); |
| 1023 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1024 | /* force the update of the task clock: */ |
| 1025 | __task_delta_exec(curr, 1); |
| 1026 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1027 | perf_counter_task_sched_out(curr, cpu); |
| 1028 | |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 1029 | spin_lock(&ctx->lock); |
| 1030 | |
| 1031 | /* |
| 1032 | * Disable all the counters: |
| 1033 | */ |
| 1034 | perf_flags = hw_perf_save_disable(); |
| 1035 | |
| 1036 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 1037 | if (counter->state > PERF_COUNTER_STATE_OFF) |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 1038 | continue; |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame] | 1039 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 1040 | counter->tstamp_enabled = ctx->time_now - |
| 1041 | counter->total_time_enabled; |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1042 | counter->hw_event.disabled = 0; |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 1043 | } |
| 1044 | hw_perf_restore(perf_flags); |
| 1045 | |
| 1046 | spin_unlock(&ctx->lock); |
| 1047 | |
| 1048 | perf_counter_task_sched_in(curr, cpu); |
| 1049 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1050 | curr_rq_unlock_irq_restore(&flags); |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 1051 | |
| 1052 | return 0; |
| 1053 | } |
| 1054 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1055 | /* |
| 1056 | * Round-robin a context's counters: |
| 1057 | */ |
| 1058 | static void rotate_ctx(struct perf_counter_context *ctx) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1059 | { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1060 | struct perf_counter *counter; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 1061 | u64 perf_flags; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1062 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1063 | if (!ctx->nr_counters) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1064 | return; |
| 1065 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1066 | spin_lock(&ctx->lock); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1067 | /* |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1068 | * Rotate the first entry last (works just fine for group counters too): |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1069 | */ |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 1070 | perf_flags = hw_perf_save_disable(); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1071 | list_for_each_entry(counter, &ctx->counter_list, list_entry) { |
Peter Zijlstra | 7556423 | 2009-03-13 12:21:29 +0100 | [diff] [blame] | 1072 | list_move_tail(&counter->list_entry, &ctx->counter_list); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1073 | break; |
| 1074 | } |
Ingo Molnar | 01b2838 | 2008-12-11 13:45:51 +0100 | [diff] [blame] | 1075 | hw_perf_restore(perf_flags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1076 | |
| 1077 | spin_unlock(&ctx->lock); |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1078 | } |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1079 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 1080 | void perf_counter_task_tick(struct task_struct *curr, int cpu) |
| 1081 | { |
| 1082 | struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 1083 | struct perf_counter_context *ctx = &curr->perf_counter_ctx; |
| 1084 | const int rotate_percpu = 0; |
| 1085 | |
| 1086 | if (rotate_percpu) |
| 1087 | perf_counter_cpu_sched_out(cpuctx); |
| 1088 | perf_counter_task_sched_out(curr, cpu); |
| 1089 | |
| 1090 | if (rotate_percpu) |
| 1091 | rotate_ctx(&cpuctx->ctx); |
| 1092 | rotate_ctx(ctx); |
| 1093 | |
| 1094 | if (rotate_percpu) |
| 1095 | perf_counter_cpu_sched_in(cpuctx, cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1096 | perf_counter_task_sched_in(curr, cpu); |
| 1097 | } |
| 1098 | |
| 1099 | /* |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1100 | * Cross CPU call to read the hardware counter |
| 1101 | */ |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 1102 | static void __read(void *info) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1103 | { |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1104 | struct perf_counter *counter = info; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 1105 | struct perf_counter_context *ctx = counter->ctx; |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1106 | unsigned long flags; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 1107 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1108 | curr_rq_lock_irq_save(&flags); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 1109 | if (ctx->is_active) |
| 1110 | update_context_time(ctx, 1); |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 1111 | counter->hw_ops->read(counter); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 1112 | update_counter_times(counter); |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 1113 | curr_rq_unlock_irq_restore(&flags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1114 | } |
| 1115 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1116 | static u64 perf_counter_read(struct perf_counter *counter) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1117 | { |
| 1118 | /* |
| 1119 | * If counter is enabled and currently active on a CPU, update the |
| 1120 | * value in the counter structure: |
| 1121 | */ |
Ingo Molnar | 6a93070 | 2008-12-11 15:17:03 +0100 | [diff] [blame] | 1122 | if (counter->state == PERF_COUNTER_STATE_ACTIVE) { |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1123 | smp_call_function_single(counter->oncpu, |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 1124 | __read, counter, 1); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 1125 | } else if (counter->state == PERF_COUNTER_STATE_INACTIVE) { |
| 1126 | update_counter_times(counter); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1127 | } |
| 1128 | |
Ingo Molnar | ee06094 | 2008-12-13 09:00:03 +0100 | [diff] [blame] | 1129 | return atomic64_read(&counter->count); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1130 | } |
| 1131 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1132 | static void put_context(struct perf_counter_context *ctx) |
| 1133 | { |
| 1134 | if (ctx->task) |
| 1135 | put_task_struct(ctx->task); |
| 1136 | } |
| 1137 | |
| 1138 | static struct perf_counter_context *find_get_context(pid_t pid, int cpu) |
| 1139 | { |
| 1140 | struct perf_cpu_context *cpuctx; |
| 1141 | struct perf_counter_context *ctx; |
| 1142 | struct task_struct *task; |
| 1143 | |
| 1144 | /* |
| 1145 | * If cpu is not a wildcard then this is a percpu counter: |
| 1146 | */ |
| 1147 | if (cpu != -1) { |
| 1148 | /* Must be root to operate on a CPU counter: */ |
| 1149 | if (!capable(CAP_SYS_ADMIN)) |
| 1150 | return ERR_PTR(-EACCES); |
| 1151 | |
| 1152 | if (cpu < 0 || cpu > num_possible_cpus()) |
| 1153 | return ERR_PTR(-EINVAL); |
| 1154 | |
| 1155 | /* |
| 1156 | * We could be clever and allow to attach a counter to an |
| 1157 | * offline CPU and activate it when the CPU comes up, but |
| 1158 | * that's for later. |
| 1159 | */ |
| 1160 | if (!cpu_isset(cpu, cpu_online_map)) |
| 1161 | return ERR_PTR(-ENODEV); |
| 1162 | |
| 1163 | cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 1164 | ctx = &cpuctx->ctx; |
| 1165 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1166 | return ctx; |
| 1167 | } |
| 1168 | |
| 1169 | rcu_read_lock(); |
| 1170 | if (!pid) |
| 1171 | task = current; |
| 1172 | else |
| 1173 | task = find_task_by_vpid(pid); |
| 1174 | if (task) |
| 1175 | get_task_struct(task); |
| 1176 | rcu_read_unlock(); |
| 1177 | |
| 1178 | if (!task) |
| 1179 | return ERR_PTR(-ESRCH); |
| 1180 | |
| 1181 | ctx = &task->perf_counter_ctx; |
| 1182 | ctx->task = task; |
| 1183 | |
| 1184 | /* Reuse ptrace permission checks for now. */ |
| 1185 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) { |
| 1186 | put_context(ctx); |
| 1187 | return ERR_PTR(-EACCES); |
| 1188 | } |
| 1189 | |
| 1190 | return ctx; |
| 1191 | } |
| 1192 | |
Peter Zijlstra | 592903c | 2009-03-13 12:21:36 +0100 | [diff] [blame] | 1193 | static void free_counter_rcu(struct rcu_head *head) |
| 1194 | { |
| 1195 | struct perf_counter *counter; |
| 1196 | |
| 1197 | counter = container_of(head, struct perf_counter, rcu_head); |
| 1198 | kfree(counter); |
| 1199 | } |
| 1200 | |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1201 | static void perf_pending_sync(struct perf_counter *counter); |
| 1202 | |
Peter Zijlstra | f160095 | 2009-03-19 20:26:16 +0100 | [diff] [blame] | 1203 | static void free_counter(struct perf_counter *counter) |
| 1204 | { |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1205 | perf_pending_sync(counter); |
| 1206 | |
Peter Zijlstra | e077df4 | 2009-03-19 20:26:17 +0100 | [diff] [blame] | 1207 | if (counter->destroy) |
| 1208 | counter->destroy(counter); |
| 1209 | |
Peter Zijlstra | f160095 | 2009-03-19 20:26:16 +0100 | [diff] [blame] | 1210 | call_rcu(&counter->rcu_head, free_counter_rcu); |
| 1211 | } |
| 1212 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1213 | /* |
| 1214 | * Called when the last reference to the file is gone. |
| 1215 | */ |
| 1216 | static int perf_release(struct inode *inode, struct file *file) |
| 1217 | { |
| 1218 | struct perf_counter *counter = file->private_data; |
| 1219 | struct perf_counter_context *ctx = counter->ctx; |
| 1220 | |
| 1221 | file->private_data = NULL; |
| 1222 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 1223 | mutex_lock(&ctx->mutex); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1224 | mutex_lock(&counter->mutex); |
| 1225 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 1226 | perf_counter_remove_from_context(counter); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1227 | |
| 1228 | mutex_unlock(&counter->mutex); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 1229 | mutex_unlock(&ctx->mutex); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1230 | |
Peter Zijlstra | f160095 | 2009-03-19 20:26:16 +0100 | [diff] [blame] | 1231 | free_counter(counter); |
Mike Galbraith | 5af7591 | 2009-02-11 10:53:37 +0100 | [diff] [blame] | 1232 | put_context(ctx); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1233 | |
| 1234 | return 0; |
| 1235 | } |
| 1236 | |
| 1237 | /* |
| 1238 | * Read the performance counter - simple non blocking version for now |
| 1239 | */ |
| 1240 | static ssize_t |
| 1241 | perf_read_hw(struct perf_counter *counter, char __user *buf, size_t count) |
| 1242 | { |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 1243 | u64 values[3]; |
| 1244 | int n; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1245 | |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 1246 | /* |
| 1247 | * Return end-of-file for a read on a counter that is in |
| 1248 | * error state (i.e. because it was pinned but it couldn't be |
| 1249 | * scheduled on to the CPU at some point). |
| 1250 | */ |
| 1251 | if (counter->state == PERF_COUNTER_STATE_ERROR) |
| 1252 | return 0; |
| 1253 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1254 | mutex_lock(&counter->mutex); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 1255 | values[0] = perf_counter_read(counter); |
| 1256 | n = 1; |
| 1257 | if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 1258 | values[n++] = counter->total_time_enabled + |
| 1259 | atomic64_read(&counter->child_total_time_enabled); |
| 1260 | if (counter->hw_event.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 1261 | values[n++] = counter->total_time_running + |
| 1262 | atomic64_read(&counter->child_total_time_running); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1263 | mutex_unlock(&counter->mutex); |
| 1264 | |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 1265 | if (count < n * sizeof(u64)) |
| 1266 | return -EINVAL; |
| 1267 | count = n * sizeof(u64); |
| 1268 | |
| 1269 | if (copy_to_user(buf, values, count)) |
| 1270 | return -EFAULT; |
| 1271 | |
| 1272 | return count; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | static ssize_t |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1276 | perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
| 1277 | { |
| 1278 | struct perf_counter *counter = file->private_data; |
| 1279 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1280 | return perf_read_hw(counter, buf, count); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | static unsigned int perf_poll(struct file *file, poll_table *wait) |
| 1284 | { |
| 1285 | struct perf_counter *counter = file->private_data; |
Peter Zijlstra | c7138f3 | 2009-03-24 13:18:16 +0100 | [diff] [blame] | 1286 | struct perf_mmap_data *data; |
| 1287 | unsigned int events; |
| 1288 | |
| 1289 | rcu_read_lock(); |
| 1290 | data = rcu_dereference(counter->data); |
| 1291 | if (data) |
| 1292 | events = atomic_xchg(&data->wakeup, 0); |
| 1293 | else |
| 1294 | events = POLL_HUP; |
| 1295 | rcu_read_unlock(); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1296 | |
| 1297 | poll_wait(file, &counter->waitq, wait); |
| 1298 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1299 | return events; |
| 1300 | } |
| 1301 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 1302 | static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 1303 | { |
| 1304 | struct perf_counter *counter = file->private_data; |
| 1305 | int err = 0; |
| 1306 | |
| 1307 | switch (cmd) { |
| 1308 | case PERF_COUNTER_IOC_ENABLE: |
| 1309 | perf_counter_enable_family(counter); |
| 1310 | break; |
| 1311 | case PERF_COUNTER_IOC_DISABLE: |
| 1312 | perf_counter_disable_family(counter); |
| 1313 | break; |
| 1314 | default: |
| 1315 | err = -ENOTTY; |
| 1316 | } |
| 1317 | return err; |
| 1318 | } |
| 1319 | |
Peter Zijlstra | 38ff667 | 2009-03-30 19:07:03 +0200 | [diff] [blame] | 1320 | /* |
| 1321 | * Callers need to ensure there can be no nesting of this function, otherwise |
| 1322 | * the seqlock logic goes bad. We can not serialize this because the arch |
| 1323 | * code calls this from NMI context. |
| 1324 | */ |
| 1325 | void perf_counter_update_userpage(struct perf_counter *counter) |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1326 | { |
Peter Zijlstra | 38ff667 | 2009-03-30 19:07:03 +0200 | [diff] [blame] | 1327 | struct perf_mmap_data *data; |
| 1328 | struct perf_counter_mmap_page *userpg; |
| 1329 | |
| 1330 | rcu_read_lock(); |
| 1331 | data = rcu_dereference(counter->data); |
| 1332 | if (!data) |
| 1333 | goto unlock; |
| 1334 | |
| 1335 | userpg = data->user_page; |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1336 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1337 | /* |
| 1338 | * Disable preemption so as to not let the corresponding user-space |
| 1339 | * spin too long if we get preempted. |
| 1340 | */ |
| 1341 | preempt_disable(); |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1342 | ++userpg->lock; |
Peter Zijlstra | 92f22a3 | 2009-04-02 11:12:04 +0200 | [diff] [blame] | 1343 | barrier(); |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1344 | userpg->index = counter->hw.idx; |
| 1345 | userpg->offset = atomic64_read(&counter->count); |
| 1346 | if (counter->state == PERF_COUNTER_STATE_ACTIVE) |
| 1347 | userpg->offset -= atomic64_read(&counter->hw.prev_count); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1348 | |
Peter Zijlstra | 92f22a3 | 2009-04-02 11:12:04 +0200 | [diff] [blame] | 1349 | barrier(); |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1350 | ++userpg->lock; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1351 | preempt_enable(); |
Peter Zijlstra | 38ff667 | 2009-03-30 19:07:03 +0200 | [diff] [blame] | 1352 | unlock: |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1353 | rcu_read_unlock(); |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 1357 | { |
| 1358 | struct perf_counter *counter = vma->vm_file->private_data; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1359 | struct perf_mmap_data *data; |
| 1360 | int ret = VM_FAULT_SIGBUS; |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1361 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1362 | rcu_read_lock(); |
| 1363 | data = rcu_dereference(counter->data); |
| 1364 | if (!data) |
| 1365 | goto unlock; |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1366 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1367 | if (vmf->pgoff == 0) { |
| 1368 | vmf->page = virt_to_page(data->user_page); |
| 1369 | } else { |
| 1370 | int nr = vmf->pgoff - 1; |
| 1371 | |
| 1372 | if ((unsigned)nr > data->nr_pages) |
| 1373 | goto unlock; |
| 1374 | |
| 1375 | vmf->page = virt_to_page(data->data_pages[nr]); |
| 1376 | } |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1377 | get_page(vmf->page); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1378 | ret = 0; |
| 1379 | unlock: |
| 1380 | rcu_read_unlock(); |
| 1381 | |
| 1382 | return ret; |
| 1383 | } |
| 1384 | |
| 1385 | static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages) |
| 1386 | { |
| 1387 | struct perf_mmap_data *data; |
| 1388 | unsigned long size; |
| 1389 | int i; |
| 1390 | |
| 1391 | WARN_ON(atomic_read(&counter->mmap_count)); |
| 1392 | |
| 1393 | size = sizeof(struct perf_mmap_data); |
| 1394 | size += nr_pages * sizeof(void *); |
| 1395 | |
| 1396 | data = kzalloc(size, GFP_KERNEL); |
| 1397 | if (!data) |
| 1398 | goto fail; |
| 1399 | |
| 1400 | data->user_page = (void *)get_zeroed_page(GFP_KERNEL); |
| 1401 | if (!data->user_page) |
| 1402 | goto fail_user_page; |
| 1403 | |
| 1404 | for (i = 0; i < nr_pages; i++) { |
| 1405 | data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL); |
| 1406 | if (!data->data_pages[i]) |
| 1407 | goto fail_data_pages; |
| 1408 | } |
| 1409 | |
| 1410 | data->nr_pages = nr_pages; |
| 1411 | |
| 1412 | rcu_assign_pointer(counter->data, data); |
| 1413 | |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1414 | return 0; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1415 | |
| 1416 | fail_data_pages: |
| 1417 | for (i--; i >= 0; i--) |
| 1418 | free_page((unsigned long)data->data_pages[i]); |
| 1419 | |
| 1420 | free_page((unsigned long)data->user_page); |
| 1421 | |
| 1422 | fail_user_page: |
| 1423 | kfree(data); |
| 1424 | |
| 1425 | fail: |
| 1426 | return -ENOMEM; |
| 1427 | } |
| 1428 | |
| 1429 | static void __perf_mmap_data_free(struct rcu_head *rcu_head) |
| 1430 | { |
| 1431 | struct perf_mmap_data *data = container_of(rcu_head, |
| 1432 | struct perf_mmap_data, rcu_head); |
| 1433 | int i; |
| 1434 | |
| 1435 | free_page((unsigned long)data->user_page); |
| 1436 | for (i = 0; i < data->nr_pages; i++) |
| 1437 | free_page((unsigned long)data->data_pages[i]); |
| 1438 | kfree(data); |
| 1439 | } |
| 1440 | |
| 1441 | static void perf_mmap_data_free(struct perf_counter *counter) |
| 1442 | { |
| 1443 | struct perf_mmap_data *data = counter->data; |
| 1444 | |
| 1445 | WARN_ON(atomic_read(&counter->mmap_count)); |
| 1446 | |
| 1447 | rcu_assign_pointer(counter->data, NULL); |
| 1448 | call_rcu(&data->rcu_head, __perf_mmap_data_free); |
| 1449 | } |
| 1450 | |
| 1451 | static void perf_mmap_open(struct vm_area_struct *vma) |
| 1452 | { |
| 1453 | struct perf_counter *counter = vma->vm_file->private_data; |
| 1454 | |
| 1455 | atomic_inc(&counter->mmap_count); |
| 1456 | } |
| 1457 | |
| 1458 | static void perf_mmap_close(struct vm_area_struct *vma) |
| 1459 | { |
| 1460 | struct perf_counter *counter = vma->vm_file->private_data; |
| 1461 | |
| 1462 | if (atomic_dec_and_mutex_lock(&counter->mmap_count, |
| 1463 | &counter->mmap_mutex)) { |
| 1464 | perf_mmap_data_free(counter); |
| 1465 | mutex_unlock(&counter->mmap_mutex); |
| 1466 | } |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | static struct vm_operations_struct perf_mmap_vmops = { |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1470 | .open = perf_mmap_open, |
| 1471 | .close = perf_mmap_close, |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1472 | .fault = perf_mmap_fault, |
| 1473 | }; |
| 1474 | |
| 1475 | static int perf_mmap(struct file *file, struct vm_area_struct *vma) |
| 1476 | { |
| 1477 | struct perf_counter *counter = file->private_data; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1478 | unsigned long vma_size; |
| 1479 | unsigned long nr_pages; |
| 1480 | unsigned long locked, lock_limit; |
| 1481 | int ret = 0; |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1482 | |
| 1483 | if (!(vma->vm_flags & VM_SHARED) || (vma->vm_flags & VM_WRITE)) |
| 1484 | return -EINVAL; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1485 | |
| 1486 | vma_size = vma->vm_end - vma->vm_start; |
| 1487 | nr_pages = (vma_size / PAGE_SIZE) - 1; |
| 1488 | |
Peter Zijlstra | 7730d86 | 2009-03-25 12:48:31 +0100 | [diff] [blame] | 1489 | /* |
| 1490 | * If we have data pages ensure they're a power-of-two number, so we |
| 1491 | * can do bitmasks instead of modulo. |
| 1492 | */ |
| 1493 | if (nr_pages != 0 && !is_power_of_2(nr_pages)) |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1494 | return -EINVAL; |
| 1495 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1496 | if (vma_size != PAGE_SIZE * (1 + nr_pages)) |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1497 | return -EINVAL; |
| 1498 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1499 | if (vma->vm_pgoff != 0) |
| 1500 | return -EINVAL; |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1501 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1502 | locked = vma_size >> PAGE_SHIFT; |
| 1503 | locked += vma->vm_mm->locked_vm; |
| 1504 | |
| 1505 | lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur; |
| 1506 | lock_limit >>= PAGE_SHIFT; |
| 1507 | |
| 1508 | if ((locked > lock_limit) && !capable(CAP_IPC_LOCK)) |
| 1509 | return -EPERM; |
| 1510 | |
| 1511 | mutex_lock(&counter->mmap_mutex); |
| 1512 | if (atomic_inc_not_zero(&counter->mmap_count)) |
| 1513 | goto out; |
| 1514 | |
| 1515 | WARN_ON(counter->data); |
| 1516 | ret = perf_mmap_data_alloc(counter, nr_pages); |
| 1517 | if (!ret) |
| 1518 | atomic_set(&counter->mmap_count, 1); |
| 1519 | out: |
| 1520 | mutex_unlock(&counter->mmap_mutex); |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1521 | |
| 1522 | vma->vm_flags &= ~VM_MAYWRITE; |
| 1523 | vma->vm_flags |= VM_RESERVED; |
| 1524 | vma->vm_ops = &perf_mmap_vmops; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1525 | |
| 1526 | return ret; |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1527 | } |
| 1528 | |
Peter Zijlstra | 3c446b3 | 2009-04-06 11:45:01 +0200 | [diff] [blame^] | 1529 | static int perf_fasync(int fd, struct file *filp, int on) |
| 1530 | { |
| 1531 | struct perf_counter *counter = filp->private_data; |
| 1532 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 1533 | int retval; |
| 1534 | |
| 1535 | mutex_lock(&inode->i_mutex); |
| 1536 | retval = fasync_helper(fd, filp, on, &counter->fasync); |
| 1537 | mutex_unlock(&inode->i_mutex); |
| 1538 | |
| 1539 | if (retval < 0) |
| 1540 | return retval; |
| 1541 | |
| 1542 | return 0; |
| 1543 | } |
| 1544 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1545 | static const struct file_operations perf_fops = { |
| 1546 | .release = perf_release, |
| 1547 | .read = perf_read, |
| 1548 | .poll = perf_poll, |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 1549 | .unlocked_ioctl = perf_ioctl, |
| 1550 | .compat_ioctl = perf_ioctl, |
Paul Mackerras | 37d8182 | 2009-03-23 18:22:08 +0100 | [diff] [blame] | 1551 | .mmap = perf_mmap, |
Peter Zijlstra | 3c446b3 | 2009-04-06 11:45:01 +0200 | [diff] [blame^] | 1552 | .fasync = perf_fasync, |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 1553 | }; |
| 1554 | |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 1555 | /* |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1556 | * Perf counter wakeup |
| 1557 | * |
| 1558 | * If there's data, ensure we set the poll() state and publish everything |
| 1559 | * to user-space before waking everybody up. |
| 1560 | */ |
| 1561 | |
| 1562 | void perf_counter_wakeup(struct perf_counter *counter) |
| 1563 | { |
| 1564 | struct perf_mmap_data *data; |
| 1565 | |
| 1566 | rcu_read_lock(); |
| 1567 | data = rcu_dereference(counter->data); |
| 1568 | if (data) { |
Peter Zijlstra | 3c446b3 | 2009-04-06 11:45:01 +0200 | [diff] [blame^] | 1569 | atomic_set(&data->wakeup, POLL_IN); |
Peter Zijlstra | 38ff667 | 2009-03-30 19:07:03 +0200 | [diff] [blame] | 1570 | /* |
| 1571 | * Ensure all data writes are issued before updating the |
| 1572 | * user-space data head information. The matching rmb() |
| 1573 | * will be in userspace after reading this value. |
| 1574 | */ |
| 1575 | smp_wmb(); |
| 1576 | data->user_page->data_head = atomic_read(&data->head); |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1577 | } |
| 1578 | rcu_read_unlock(); |
| 1579 | |
| 1580 | wake_up_all(&counter->waitq); |
Peter Zijlstra | 3c446b3 | 2009-04-06 11:45:01 +0200 | [diff] [blame^] | 1581 | kill_fasync(&counter->fasync, SIGIO, POLL_IN); |
Peter Zijlstra | 925d519 | 2009-03-30 19:07:02 +0200 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | /* |
| 1585 | * Pending wakeups |
| 1586 | * |
| 1587 | * Handle the case where we need to wakeup up from NMI (or rq->lock) context. |
| 1588 | * |
| 1589 | * The NMI bit means we cannot possibly take locks. Therefore, maintain a |
| 1590 | * single linked list and use cmpxchg() to add entries lockless. |
| 1591 | */ |
| 1592 | |
| 1593 | #define PENDING_TAIL ((struct perf_wakeup_entry *)-1UL) |
| 1594 | |
| 1595 | static DEFINE_PER_CPU(struct perf_wakeup_entry *, perf_wakeup_head) = { |
| 1596 | PENDING_TAIL, |
| 1597 | }; |
| 1598 | |
| 1599 | static void perf_pending_queue(struct perf_counter *counter) |
| 1600 | { |
| 1601 | struct perf_wakeup_entry **head; |
| 1602 | struct perf_wakeup_entry *prev, *next; |
| 1603 | |
| 1604 | if (cmpxchg(&counter->wakeup.next, NULL, PENDING_TAIL) != NULL) |
| 1605 | return; |
| 1606 | |
| 1607 | head = &get_cpu_var(perf_wakeup_head); |
| 1608 | |
| 1609 | do { |
| 1610 | prev = counter->wakeup.next = *head; |
| 1611 | next = &counter->wakeup; |
| 1612 | } while (cmpxchg(head, prev, next) != prev); |
| 1613 | |
| 1614 | set_perf_counter_pending(); |
| 1615 | |
| 1616 | put_cpu_var(perf_wakeup_head); |
| 1617 | } |
| 1618 | |
| 1619 | static int __perf_pending_run(void) |
| 1620 | { |
| 1621 | struct perf_wakeup_entry *list; |
| 1622 | int nr = 0; |
| 1623 | |
| 1624 | list = xchg(&__get_cpu_var(perf_wakeup_head), PENDING_TAIL); |
| 1625 | while (list != PENDING_TAIL) { |
| 1626 | struct perf_counter *counter = container_of(list, |
| 1627 | struct perf_counter, wakeup); |
| 1628 | |
| 1629 | list = list->next; |
| 1630 | |
| 1631 | counter->wakeup.next = NULL; |
| 1632 | /* |
| 1633 | * Ensure we observe the unqueue before we issue the wakeup, |
| 1634 | * so that we won't be waiting forever. |
| 1635 | * -- see perf_not_pending(). |
| 1636 | */ |
| 1637 | smp_wmb(); |
| 1638 | |
| 1639 | perf_counter_wakeup(counter); |
| 1640 | nr++; |
| 1641 | } |
| 1642 | |
| 1643 | return nr; |
| 1644 | } |
| 1645 | |
| 1646 | static inline int perf_not_pending(struct perf_counter *counter) |
| 1647 | { |
| 1648 | /* |
| 1649 | * If we flush on whatever cpu we run, there is a chance we don't |
| 1650 | * need to wait. |
| 1651 | */ |
| 1652 | get_cpu(); |
| 1653 | __perf_pending_run(); |
| 1654 | put_cpu(); |
| 1655 | |
| 1656 | /* |
| 1657 | * Ensure we see the proper queue state before going to sleep |
| 1658 | * so that we do not miss the wakeup. -- see perf_pending_handle() |
| 1659 | */ |
| 1660 | smp_rmb(); |
| 1661 | return counter->wakeup.next == NULL; |
| 1662 | } |
| 1663 | |
| 1664 | static void perf_pending_sync(struct perf_counter *counter) |
| 1665 | { |
| 1666 | wait_event(counter->waitq, perf_not_pending(counter)); |
| 1667 | } |
| 1668 | |
| 1669 | void perf_counter_do_pending(void) |
| 1670 | { |
| 1671 | __perf_pending_run(); |
| 1672 | } |
| 1673 | |
| 1674 | /* |
Peter Zijlstra | 394ee07 | 2009-03-30 19:07:14 +0200 | [diff] [blame] | 1675 | * Callchain support -- arch specific |
| 1676 | */ |
| 1677 | |
Peter Zijlstra | 9c03d88 | 2009-04-06 11:45:00 +0200 | [diff] [blame] | 1678 | __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs) |
Peter Zijlstra | 394ee07 | 2009-03-30 19:07:14 +0200 | [diff] [blame] | 1679 | { |
| 1680 | return NULL; |
| 1681 | } |
| 1682 | |
| 1683 | /* |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 1684 | * Output |
| 1685 | */ |
| 1686 | |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 1687 | struct perf_output_handle { |
| 1688 | struct perf_counter *counter; |
| 1689 | struct perf_mmap_data *data; |
| 1690 | unsigned int offset; |
Peter Zijlstra | 63e35b2 | 2009-03-25 12:30:24 +0100 | [diff] [blame] | 1691 | unsigned int head; |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 1692 | int wakeup; |
Peter Zijlstra | 78d613e | 2009-03-30 19:07:11 +0200 | [diff] [blame] | 1693 | int nmi; |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 1694 | }; |
| 1695 | |
Peter Zijlstra | 78d613e | 2009-03-30 19:07:11 +0200 | [diff] [blame] | 1696 | static inline void __perf_output_wakeup(struct perf_output_handle *handle) |
| 1697 | { |
| 1698 | if (handle->nmi) |
| 1699 | perf_pending_queue(handle->counter); |
| 1700 | else |
| 1701 | perf_counter_wakeup(handle->counter); |
| 1702 | } |
| 1703 | |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 1704 | static int perf_output_begin(struct perf_output_handle *handle, |
Peter Zijlstra | 78d613e | 2009-03-30 19:07:11 +0200 | [diff] [blame] | 1705 | struct perf_counter *counter, unsigned int size, |
| 1706 | int nmi) |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 1707 | { |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1708 | struct perf_mmap_data *data; |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 1709 | unsigned int offset, head; |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 1710 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1711 | rcu_read_lock(); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1712 | data = rcu_dereference(counter->data); |
| 1713 | if (!data) |
| 1714 | goto out; |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 1715 | |
Peter Zijlstra | 78d613e | 2009-03-30 19:07:11 +0200 | [diff] [blame] | 1716 | handle->counter = counter; |
| 1717 | handle->nmi = nmi; |
| 1718 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1719 | if (!data->nr_pages) |
Peter Zijlstra | 78d613e | 2009-03-30 19:07:11 +0200 | [diff] [blame] | 1720 | goto fail; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1721 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1722 | do { |
| 1723 | offset = head = atomic_read(&data->head); |
Peter Zijlstra | c7138f3 | 2009-03-24 13:18:16 +0100 | [diff] [blame] | 1724 | head += size; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1725 | } while (atomic_cmpxchg(&data->head, offset, head) != offset); |
| 1726 | |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 1727 | handle->data = data; |
| 1728 | handle->offset = offset; |
Peter Zijlstra | 63e35b2 | 2009-03-25 12:30:24 +0100 | [diff] [blame] | 1729 | handle->head = head; |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 1730 | handle->wakeup = (offset >> PAGE_SHIFT) != (head >> PAGE_SHIFT); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1731 | |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 1732 | return 0; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1733 | |
Peter Zijlstra | 78d613e | 2009-03-30 19:07:11 +0200 | [diff] [blame] | 1734 | fail: |
| 1735 | __perf_output_wakeup(handle); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1736 | out: |
| 1737 | rcu_read_unlock(); |
| 1738 | |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 1739 | return -ENOSPC; |
| 1740 | } |
| 1741 | |
| 1742 | static void perf_output_copy(struct perf_output_handle *handle, |
| 1743 | void *buf, unsigned int len) |
| 1744 | { |
| 1745 | unsigned int pages_mask; |
| 1746 | unsigned int offset; |
| 1747 | unsigned int size; |
| 1748 | void **pages; |
| 1749 | |
| 1750 | offset = handle->offset; |
| 1751 | pages_mask = handle->data->nr_pages - 1; |
| 1752 | pages = handle->data->data_pages; |
| 1753 | |
| 1754 | do { |
| 1755 | unsigned int page_offset; |
| 1756 | int nr; |
| 1757 | |
| 1758 | nr = (offset >> PAGE_SHIFT) & pages_mask; |
| 1759 | page_offset = offset & (PAGE_SIZE - 1); |
| 1760 | size = min_t(unsigned int, PAGE_SIZE - page_offset, len); |
| 1761 | |
| 1762 | memcpy(pages[nr] + page_offset, buf, size); |
| 1763 | |
| 1764 | len -= size; |
| 1765 | buf += size; |
| 1766 | offset += size; |
| 1767 | } while (len); |
| 1768 | |
| 1769 | handle->offset = offset; |
Peter Zijlstra | 63e35b2 | 2009-03-25 12:30:24 +0100 | [diff] [blame] | 1770 | |
| 1771 | WARN_ON_ONCE(handle->offset > handle->head); |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 1772 | } |
| 1773 | |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 1774 | #define perf_output_put(handle, x) \ |
| 1775 | perf_output_copy((handle), &(x), sizeof(x)) |
| 1776 | |
Peter Zijlstra | 78d613e | 2009-03-30 19:07:11 +0200 | [diff] [blame] | 1777 | static void perf_output_end(struct perf_output_handle *handle) |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 1778 | { |
Peter Zijlstra | c457810 | 2009-04-02 11:12:01 +0200 | [diff] [blame] | 1779 | int wakeup_events = handle->counter->hw_event.wakeup_events; |
| 1780 | |
| 1781 | if (wakeup_events) { |
| 1782 | int events = atomic_inc_return(&handle->data->events); |
| 1783 | if (events >= wakeup_events) { |
| 1784 | atomic_sub(wakeup_events, &handle->data->events); |
| 1785 | __perf_output_wakeup(handle); |
| 1786 | } |
| 1787 | } else if (handle->wakeup) |
Peter Zijlstra | 78d613e | 2009-03-30 19:07:11 +0200 | [diff] [blame] | 1788 | __perf_output_wakeup(handle); |
Peter Zijlstra | b9cacc7 | 2009-03-25 12:30:22 +0100 | [diff] [blame] | 1789 | rcu_read_unlock(); |
| 1790 | } |
| 1791 | |
Peter Zijlstra | 8a057d8 | 2009-04-02 11:11:59 +0200 | [diff] [blame] | 1792 | void perf_counter_output(struct perf_counter *counter, |
| 1793 | int nmi, struct pt_regs *regs) |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1794 | { |
Peter Zijlstra | 5ed0041 | 2009-03-30 19:07:12 +0200 | [diff] [blame] | 1795 | int ret; |
Peter Zijlstra | 8a057d8 | 2009-04-02 11:11:59 +0200 | [diff] [blame] | 1796 | u64 record_type = counter->hw_event.record_type; |
Peter Zijlstra | 5ed0041 | 2009-03-30 19:07:12 +0200 | [diff] [blame] | 1797 | struct perf_output_handle handle; |
| 1798 | struct perf_event_header header; |
| 1799 | u64 ip; |
Peter Zijlstra | 5c14819 | 2009-03-25 12:30:23 +0100 | [diff] [blame] | 1800 | struct { |
Peter Zijlstra | ea5d20c | 2009-03-25 12:30:25 +0100 | [diff] [blame] | 1801 | u32 pid, tid; |
Peter Zijlstra | 5ed0041 | 2009-03-30 19:07:12 +0200 | [diff] [blame] | 1802 | } tid_entry; |
Peter Zijlstra | 8a057d8 | 2009-04-02 11:11:59 +0200 | [diff] [blame] | 1803 | struct { |
| 1804 | u64 event; |
| 1805 | u64 counter; |
| 1806 | } group_entry; |
Peter Zijlstra | 394ee07 | 2009-03-30 19:07:14 +0200 | [diff] [blame] | 1807 | struct perf_callchain_entry *callchain = NULL; |
| 1808 | int callchain_size = 0; |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1809 | |
Peter Zijlstra | 8a057d8 | 2009-04-02 11:11:59 +0200 | [diff] [blame] | 1810 | header.type = PERF_EVENT_COUNTER_OVERFLOW; |
Peter Zijlstra | 5ed0041 | 2009-03-30 19:07:12 +0200 | [diff] [blame] | 1811 | header.size = sizeof(header); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1812 | |
Peter Zijlstra | 8a057d8 | 2009-04-02 11:11:59 +0200 | [diff] [blame] | 1813 | if (record_type & PERF_RECORD_IP) { |
| 1814 | ip = instruction_pointer(regs); |
| 1815 | header.type |= __PERF_EVENT_IP; |
| 1816 | header.size += sizeof(ip); |
| 1817 | } |
Peter Zijlstra | ea5d20c | 2009-03-25 12:30:25 +0100 | [diff] [blame] | 1818 | |
Peter Zijlstra | 8a057d8 | 2009-04-02 11:11:59 +0200 | [diff] [blame] | 1819 | if (record_type & PERF_RECORD_TID) { |
Peter Zijlstra | ea5d20c | 2009-03-25 12:30:25 +0100 | [diff] [blame] | 1820 | /* namespace issues */ |
Peter Zijlstra | 5ed0041 | 2009-03-30 19:07:12 +0200 | [diff] [blame] | 1821 | tid_entry.pid = current->group_leader->pid; |
| 1822 | tid_entry.tid = current->pid; |
Peter Zijlstra | ea5d20c | 2009-03-25 12:30:25 +0100 | [diff] [blame] | 1823 | |
Peter Zijlstra | 5ed0041 | 2009-03-30 19:07:12 +0200 | [diff] [blame] | 1824 | header.type |= __PERF_EVENT_TID; |
| 1825 | header.size += sizeof(tid_entry); |
| 1826 | } |
Peter Zijlstra | ea5d20c | 2009-03-25 12:30:25 +0100 | [diff] [blame] | 1827 | |
Peter Zijlstra | 8a057d8 | 2009-04-02 11:11:59 +0200 | [diff] [blame] | 1828 | if (record_type & PERF_RECORD_GROUP) { |
| 1829 | header.type |= __PERF_EVENT_GROUP; |
| 1830 | header.size += sizeof(u64) + |
| 1831 | counter->nr_siblings * sizeof(group_entry); |
| 1832 | } |
| 1833 | |
| 1834 | if (record_type & PERF_RECORD_CALLCHAIN) { |
Peter Zijlstra | 394ee07 | 2009-03-30 19:07:14 +0200 | [diff] [blame] | 1835 | callchain = perf_callchain(regs); |
| 1836 | |
| 1837 | if (callchain) { |
Peter Zijlstra | 9c03d88 | 2009-04-06 11:45:00 +0200 | [diff] [blame] | 1838 | callchain_size = (1 + callchain->nr) * sizeof(u64); |
Peter Zijlstra | 394ee07 | 2009-03-30 19:07:14 +0200 | [diff] [blame] | 1839 | |
| 1840 | header.type |= __PERF_EVENT_CALLCHAIN; |
| 1841 | header.size += callchain_size; |
| 1842 | } |
| 1843 | } |
| 1844 | |
Peter Zijlstra | 5ed0041 | 2009-03-30 19:07:12 +0200 | [diff] [blame] | 1845 | ret = perf_output_begin(&handle, counter, header.size, nmi); |
| 1846 | if (ret) |
| 1847 | return; |
Peter Zijlstra | ea5d20c | 2009-03-25 12:30:25 +0100 | [diff] [blame] | 1848 | |
Peter Zijlstra | 5ed0041 | 2009-03-30 19:07:12 +0200 | [diff] [blame] | 1849 | perf_output_put(&handle, header); |
Peter Zijlstra | 5ed0041 | 2009-03-30 19:07:12 +0200 | [diff] [blame] | 1850 | |
Peter Zijlstra | 8a057d8 | 2009-04-02 11:11:59 +0200 | [diff] [blame] | 1851 | if (record_type & PERF_RECORD_IP) |
| 1852 | perf_output_put(&handle, ip); |
| 1853 | |
| 1854 | if (record_type & PERF_RECORD_TID) |
Peter Zijlstra | 5ed0041 | 2009-03-30 19:07:12 +0200 | [diff] [blame] | 1855 | perf_output_put(&handle, tid_entry); |
| 1856 | |
Peter Zijlstra | 8a057d8 | 2009-04-02 11:11:59 +0200 | [diff] [blame] | 1857 | if (record_type & PERF_RECORD_GROUP) { |
| 1858 | struct perf_counter *leader, *sub; |
| 1859 | u64 nr = counter->nr_siblings; |
| 1860 | |
| 1861 | perf_output_put(&handle, nr); |
| 1862 | |
| 1863 | leader = counter->group_leader; |
| 1864 | list_for_each_entry(sub, &leader->sibling_list, list_entry) { |
| 1865 | if (sub != counter) |
| 1866 | sub->hw_ops->read(sub); |
| 1867 | |
| 1868 | group_entry.event = sub->hw_event.config; |
| 1869 | group_entry.counter = atomic64_read(&sub->count); |
| 1870 | |
| 1871 | perf_output_put(&handle, group_entry); |
| 1872 | } |
| 1873 | } |
| 1874 | |
Peter Zijlstra | 394ee07 | 2009-03-30 19:07:14 +0200 | [diff] [blame] | 1875 | if (callchain) |
| 1876 | perf_output_copy(&handle, callchain, callchain_size); |
| 1877 | |
Peter Zijlstra | 5ed0041 | 2009-03-30 19:07:12 +0200 | [diff] [blame] | 1878 | perf_output_end(&handle); |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 1879 | } |
| 1880 | |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 1881 | /* |
Peter Zijlstra | 0a4a939 | 2009-03-30 19:07:05 +0200 | [diff] [blame] | 1882 | * mmap tracking |
| 1883 | */ |
| 1884 | |
| 1885 | struct perf_mmap_event { |
| 1886 | struct file *file; |
| 1887 | char *file_name; |
| 1888 | int file_size; |
| 1889 | |
| 1890 | struct { |
| 1891 | struct perf_event_header header; |
| 1892 | |
| 1893 | u32 pid; |
| 1894 | u32 tid; |
| 1895 | u64 start; |
| 1896 | u64 len; |
| 1897 | u64 pgoff; |
| 1898 | } event; |
| 1899 | }; |
| 1900 | |
| 1901 | static void perf_counter_mmap_output(struct perf_counter *counter, |
| 1902 | struct perf_mmap_event *mmap_event) |
| 1903 | { |
| 1904 | struct perf_output_handle handle; |
| 1905 | int size = mmap_event->event.header.size; |
Peter Zijlstra | 78d613e | 2009-03-30 19:07:11 +0200 | [diff] [blame] | 1906 | int ret = perf_output_begin(&handle, counter, size, 0); |
Peter Zijlstra | 0a4a939 | 2009-03-30 19:07:05 +0200 | [diff] [blame] | 1907 | |
| 1908 | if (ret) |
| 1909 | return; |
| 1910 | |
| 1911 | perf_output_put(&handle, mmap_event->event); |
| 1912 | perf_output_copy(&handle, mmap_event->file_name, |
| 1913 | mmap_event->file_size); |
Peter Zijlstra | 78d613e | 2009-03-30 19:07:11 +0200 | [diff] [blame] | 1914 | perf_output_end(&handle); |
Peter Zijlstra | 0a4a939 | 2009-03-30 19:07:05 +0200 | [diff] [blame] | 1915 | } |
| 1916 | |
| 1917 | static int perf_counter_mmap_match(struct perf_counter *counter, |
| 1918 | struct perf_mmap_event *mmap_event) |
| 1919 | { |
| 1920 | if (counter->hw_event.mmap && |
| 1921 | mmap_event->event.header.type == PERF_EVENT_MMAP) |
| 1922 | return 1; |
| 1923 | |
| 1924 | if (counter->hw_event.munmap && |
| 1925 | mmap_event->event.header.type == PERF_EVENT_MUNMAP) |
| 1926 | return 1; |
| 1927 | |
| 1928 | return 0; |
| 1929 | } |
| 1930 | |
| 1931 | static void perf_counter_mmap_ctx(struct perf_counter_context *ctx, |
| 1932 | struct perf_mmap_event *mmap_event) |
| 1933 | { |
| 1934 | struct perf_counter *counter; |
| 1935 | |
| 1936 | if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list)) |
| 1937 | return; |
| 1938 | |
| 1939 | rcu_read_lock(); |
| 1940 | list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) { |
| 1941 | if (perf_counter_mmap_match(counter, mmap_event)) |
| 1942 | perf_counter_mmap_output(counter, mmap_event); |
| 1943 | } |
| 1944 | rcu_read_unlock(); |
| 1945 | } |
| 1946 | |
| 1947 | static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event) |
| 1948 | { |
| 1949 | struct perf_cpu_context *cpuctx; |
| 1950 | struct file *file = mmap_event->file; |
| 1951 | unsigned int size; |
| 1952 | char tmp[16]; |
| 1953 | char *buf = NULL; |
| 1954 | char *name; |
| 1955 | |
| 1956 | if (file) { |
| 1957 | buf = kzalloc(PATH_MAX, GFP_KERNEL); |
| 1958 | if (!buf) { |
| 1959 | name = strncpy(tmp, "//enomem", sizeof(tmp)); |
| 1960 | goto got_name; |
| 1961 | } |
| 1962 | name = dentry_path(file->f_dentry, buf, PATH_MAX); |
| 1963 | if (IS_ERR(name)) { |
| 1964 | name = strncpy(tmp, "//toolong", sizeof(tmp)); |
| 1965 | goto got_name; |
| 1966 | } |
| 1967 | } else { |
| 1968 | name = strncpy(tmp, "//anon", sizeof(tmp)); |
| 1969 | goto got_name; |
| 1970 | } |
| 1971 | |
| 1972 | got_name: |
| 1973 | size = ALIGN(strlen(name), sizeof(u64)); |
| 1974 | |
| 1975 | mmap_event->file_name = name; |
| 1976 | mmap_event->file_size = size; |
| 1977 | |
| 1978 | mmap_event->event.header.size = sizeof(mmap_event->event) + size; |
| 1979 | |
| 1980 | cpuctx = &get_cpu_var(perf_cpu_context); |
| 1981 | perf_counter_mmap_ctx(&cpuctx->ctx, mmap_event); |
| 1982 | put_cpu_var(perf_cpu_context); |
| 1983 | |
| 1984 | perf_counter_mmap_ctx(¤t->perf_counter_ctx, mmap_event); |
| 1985 | |
| 1986 | kfree(buf); |
| 1987 | } |
| 1988 | |
| 1989 | void perf_counter_mmap(unsigned long addr, unsigned long len, |
| 1990 | unsigned long pgoff, struct file *file) |
| 1991 | { |
| 1992 | struct perf_mmap_event mmap_event = { |
| 1993 | .file = file, |
| 1994 | .event = { |
| 1995 | .header = { .type = PERF_EVENT_MMAP, }, |
| 1996 | .pid = current->group_leader->pid, |
| 1997 | .tid = current->pid, |
| 1998 | .start = addr, |
| 1999 | .len = len, |
| 2000 | .pgoff = pgoff, |
| 2001 | }, |
| 2002 | }; |
| 2003 | |
| 2004 | perf_counter_mmap_event(&mmap_event); |
| 2005 | } |
| 2006 | |
| 2007 | void perf_counter_munmap(unsigned long addr, unsigned long len, |
| 2008 | unsigned long pgoff, struct file *file) |
| 2009 | { |
| 2010 | struct perf_mmap_event mmap_event = { |
| 2011 | .file = file, |
| 2012 | .event = { |
| 2013 | .header = { .type = PERF_EVENT_MUNMAP, }, |
| 2014 | .pid = current->group_leader->pid, |
| 2015 | .tid = current->pid, |
| 2016 | .start = addr, |
| 2017 | .len = len, |
| 2018 | .pgoff = pgoff, |
| 2019 | }, |
| 2020 | }; |
| 2021 | |
| 2022 | perf_counter_mmap_event(&mmap_event); |
| 2023 | } |
| 2024 | |
| 2025 | /* |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2026 | * Generic software counter infrastructure |
| 2027 | */ |
| 2028 | |
| 2029 | static void perf_swcounter_update(struct perf_counter *counter) |
| 2030 | { |
| 2031 | struct hw_perf_counter *hwc = &counter->hw; |
| 2032 | u64 prev, now; |
| 2033 | s64 delta; |
| 2034 | |
| 2035 | again: |
| 2036 | prev = atomic64_read(&hwc->prev_count); |
| 2037 | now = atomic64_read(&hwc->count); |
| 2038 | if (atomic64_cmpxchg(&hwc->prev_count, prev, now) != prev) |
| 2039 | goto again; |
| 2040 | |
| 2041 | delta = now - prev; |
| 2042 | |
| 2043 | atomic64_add(delta, &counter->count); |
| 2044 | atomic64_sub(delta, &hwc->period_left); |
| 2045 | } |
| 2046 | |
| 2047 | static void perf_swcounter_set_period(struct perf_counter *counter) |
| 2048 | { |
| 2049 | struct hw_perf_counter *hwc = &counter->hw; |
| 2050 | s64 left = atomic64_read(&hwc->period_left); |
| 2051 | s64 period = hwc->irq_period; |
| 2052 | |
| 2053 | if (unlikely(left <= -period)) { |
| 2054 | left = period; |
| 2055 | atomic64_set(&hwc->period_left, left); |
| 2056 | } |
| 2057 | |
| 2058 | if (unlikely(left <= 0)) { |
| 2059 | left += period; |
| 2060 | atomic64_add(period, &hwc->period_left); |
| 2061 | } |
| 2062 | |
| 2063 | atomic64_set(&hwc->prev_count, -left); |
| 2064 | atomic64_set(&hwc->count, -left); |
| 2065 | } |
| 2066 | |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2067 | static enum hrtimer_restart perf_swcounter_hrtimer(struct hrtimer *hrtimer) |
| 2068 | { |
| 2069 | struct perf_counter *counter; |
| 2070 | struct pt_regs *regs; |
| 2071 | |
| 2072 | counter = container_of(hrtimer, struct perf_counter, hw.hrtimer); |
| 2073 | counter->hw_ops->read(counter); |
| 2074 | |
| 2075 | regs = get_irq_regs(); |
| 2076 | /* |
| 2077 | * In case we exclude kernel IPs or are somehow not in interrupt |
| 2078 | * context, provide the next best thing, the user IP. |
| 2079 | */ |
| 2080 | if ((counter->hw_event.exclude_kernel || !regs) && |
| 2081 | !counter->hw_event.exclude_user) |
| 2082 | regs = task_pt_regs(current); |
| 2083 | |
| 2084 | if (regs) |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 2085 | perf_counter_output(counter, 0, regs); |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2086 | |
| 2087 | hrtimer_forward_now(hrtimer, ns_to_ktime(counter->hw.irq_period)); |
| 2088 | |
| 2089 | return HRTIMER_RESTART; |
| 2090 | } |
| 2091 | |
| 2092 | static void perf_swcounter_overflow(struct perf_counter *counter, |
| 2093 | int nmi, struct pt_regs *regs) |
| 2094 | { |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2095 | perf_swcounter_update(counter); |
| 2096 | perf_swcounter_set_period(counter); |
Peter Zijlstra | 0322cd6 | 2009-03-19 20:26:19 +0100 | [diff] [blame] | 2097 | perf_counter_output(counter, nmi, regs); |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2098 | } |
| 2099 | |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2100 | static int perf_swcounter_match(struct perf_counter *counter, |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2101 | enum perf_event_types type, |
| 2102 | u32 event, struct pt_regs *regs) |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2103 | { |
| 2104 | if (counter->state != PERF_COUNTER_STATE_ACTIVE) |
| 2105 | return 0; |
| 2106 | |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 2107 | if (perf_event_raw(&counter->hw_event)) |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2108 | return 0; |
| 2109 | |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 2110 | if (perf_event_type(&counter->hw_event) != type) |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2111 | return 0; |
| 2112 | |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 2113 | if (perf_event_id(&counter->hw_event) != event) |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2114 | return 0; |
| 2115 | |
| 2116 | if (counter->hw_event.exclude_user && user_mode(regs)) |
| 2117 | return 0; |
| 2118 | |
| 2119 | if (counter->hw_event.exclude_kernel && !user_mode(regs)) |
| 2120 | return 0; |
| 2121 | |
| 2122 | return 1; |
| 2123 | } |
| 2124 | |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2125 | static void perf_swcounter_add(struct perf_counter *counter, u64 nr, |
| 2126 | int nmi, struct pt_regs *regs) |
| 2127 | { |
| 2128 | int neg = atomic64_add_negative(nr, &counter->hw.count); |
| 2129 | if (counter->hw.irq_period && !neg) |
| 2130 | perf_swcounter_overflow(counter, nmi, regs); |
| 2131 | } |
| 2132 | |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2133 | static void perf_swcounter_ctx_event(struct perf_counter_context *ctx, |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2134 | enum perf_event_types type, u32 event, |
| 2135 | u64 nr, int nmi, struct pt_regs *regs) |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2136 | { |
| 2137 | struct perf_counter *counter; |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2138 | |
Peter Zijlstra | 01ef09d | 2009-03-19 20:26:11 +0100 | [diff] [blame] | 2139 | if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list)) |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2140 | return; |
| 2141 | |
Peter Zijlstra | 592903c | 2009-03-13 12:21:36 +0100 | [diff] [blame] | 2142 | rcu_read_lock(); |
| 2143 | list_for_each_entry_rcu(counter, &ctx->event_list, event_entry) { |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2144 | if (perf_swcounter_match(counter, type, event, regs)) |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2145 | perf_swcounter_add(counter, nr, nmi, regs); |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2146 | } |
Peter Zijlstra | 592903c | 2009-03-13 12:21:36 +0100 | [diff] [blame] | 2147 | rcu_read_unlock(); |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2148 | } |
| 2149 | |
Peter Zijlstra | 96f6d44 | 2009-03-23 18:22:07 +0100 | [diff] [blame] | 2150 | static int *perf_swcounter_recursion_context(struct perf_cpu_context *cpuctx) |
| 2151 | { |
| 2152 | if (in_nmi()) |
| 2153 | return &cpuctx->recursion[3]; |
| 2154 | |
| 2155 | if (in_irq()) |
| 2156 | return &cpuctx->recursion[2]; |
| 2157 | |
| 2158 | if (in_softirq()) |
| 2159 | return &cpuctx->recursion[1]; |
| 2160 | |
| 2161 | return &cpuctx->recursion[0]; |
| 2162 | } |
| 2163 | |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2164 | static void __perf_swcounter_event(enum perf_event_types type, u32 event, |
| 2165 | u64 nr, int nmi, struct pt_regs *regs) |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2166 | { |
| 2167 | struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context); |
Peter Zijlstra | 96f6d44 | 2009-03-23 18:22:07 +0100 | [diff] [blame] | 2168 | int *recursion = perf_swcounter_recursion_context(cpuctx); |
| 2169 | |
| 2170 | if (*recursion) |
| 2171 | goto out; |
| 2172 | |
| 2173 | (*recursion)++; |
| 2174 | barrier(); |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2175 | |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2176 | perf_swcounter_ctx_event(&cpuctx->ctx, type, event, nr, nmi, regs); |
| 2177 | if (cpuctx->task_ctx) { |
| 2178 | perf_swcounter_ctx_event(cpuctx->task_ctx, type, event, |
| 2179 | nr, nmi, regs); |
| 2180 | } |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2181 | |
Peter Zijlstra | 96f6d44 | 2009-03-23 18:22:07 +0100 | [diff] [blame] | 2182 | barrier(); |
| 2183 | (*recursion)--; |
| 2184 | |
| 2185 | out: |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2186 | put_cpu_var(perf_cpu_context); |
| 2187 | } |
| 2188 | |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2189 | void perf_swcounter_event(u32 event, u64 nr, int nmi, struct pt_regs *regs) |
| 2190 | { |
| 2191 | __perf_swcounter_event(PERF_TYPE_SOFTWARE, event, nr, nmi, regs); |
| 2192 | } |
| 2193 | |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2194 | static void perf_swcounter_read(struct perf_counter *counter) |
| 2195 | { |
| 2196 | perf_swcounter_update(counter); |
| 2197 | } |
| 2198 | |
| 2199 | static int perf_swcounter_enable(struct perf_counter *counter) |
| 2200 | { |
| 2201 | perf_swcounter_set_period(counter); |
| 2202 | return 0; |
| 2203 | } |
| 2204 | |
| 2205 | static void perf_swcounter_disable(struct perf_counter *counter) |
| 2206 | { |
| 2207 | perf_swcounter_update(counter); |
| 2208 | } |
| 2209 | |
Peter Zijlstra | ac17dc8 | 2009-03-13 12:21:34 +0100 | [diff] [blame] | 2210 | static const struct hw_perf_counter_ops perf_ops_generic = { |
| 2211 | .enable = perf_swcounter_enable, |
| 2212 | .disable = perf_swcounter_disable, |
| 2213 | .read = perf_swcounter_read, |
| 2214 | }; |
| 2215 | |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2216 | /* |
| 2217 | * Software counter: cpu wall time clock |
| 2218 | */ |
| 2219 | |
Paul Mackerras | 9abf8a0 | 2009-01-09 16:26:43 +1100 | [diff] [blame] | 2220 | static void cpu_clock_perf_counter_update(struct perf_counter *counter) |
| 2221 | { |
| 2222 | int cpu = raw_smp_processor_id(); |
| 2223 | s64 prev; |
| 2224 | u64 now; |
| 2225 | |
| 2226 | now = cpu_clock(cpu); |
| 2227 | prev = atomic64_read(&counter->hw.prev_count); |
| 2228 | atomic64_set(&counter->hw.prev_count, now); |
| 2229 | atomic64_add(now - prev, &counter->count); |
| 2230 | } |
| 2231 | |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2232 | static int cpu_clock_perf_counter_enable(struct perf_counter *counter) |
| 2233 | { |
| 2234 | struct hw_perf_counter *hwc = &counter->hw; |
| 2235 | int cpu = raw_smp_processor_id(); |
| 2236 | |
| 2237 | atomic64_set(&hwc->prev_count, cpu_clock(cpu)); |
Peter Zijlstra | 039fc91 | 2009-03-13 16:43:47 +0100 | [diff] [blame] | 2238 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 2239 | hwc->hrtimer.function = perf_swcounter_hrtimer; |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2240 | if (hwc->irq_period) { |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2241 | __hrtimer_start_range_ns(&hwc->hrtimer, |
| 2242 | ns_to_ktime(hwc->irq_period), 0, |
| 2243 | HRTIMER_MODE_REL, 0); |
| 2244 | } |
| 2245 | |
| 2246 | return 0; |
| 2247 | } |
| 2248 | |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2249 | static void cpu_clock_perf_counter_disable(struct perf_counter *counter) |
| 2250 | { |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2251 | hrtimer_cancel(&counter->hw.hrtimer); |
Paul Mackerras | 9abf8a0 | 2009-01-09 16:26:43 +1100 | [diff] [blame] | 2252 | cpu_clock_perf_counter_update(counter); |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | static void cpu_clock_perf_counter_read(struct perf_counter *counter) |
| 2256 | { |
Paul Mackerras | 9abf8a0 | 2009-01-09 16:26:43 +1100 | [diff] [blame] | 2257 | cpu_clock_perf_counter_update(counter); |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2258 | } |
| 2259 | |
| 2260 | static const struct hw_perf_counter_ops perf_ops_cpu_clock = { |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 2261 | .enable = cpu_clock_perf_counter_enable, |
| 2262 | .disable = cpu_clock_perf_counter_disable, |
| 2263 | .read = cpu_clock_perf_counter_read, |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2264 | }; |
| 2265 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 2266 | /* |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2267 | * Software counter: task time clock |
| 2268 | */ |
| 2269 | |
| 2270 | /* |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 2271 | * Called from within the scheduler: |
| 2272 | */ |
| 2273 | 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] | 2274 | { |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 2275 | struct task_struct *curr = counter->task; |
| 2276 | u64 delta; |
| 2277 | |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 2278 | delta = __task_delta_exec(curr, update); |
| 2279 | |
| 2280 | return curr->se.sum_exec_runtime + delta; |
| 2281 | } |
| 2282 | |
| 2283 | static void task_clock_perf_counter_update(struct perf_counter *counter, u64 now) |
| 2284 | { |
| 2285 | u64 prev; |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 2286 | s64 delta; |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 2287 | |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 2288 | prev = atomic64_read(&counter->hw.prev_count); |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 2289 | |
| 2290 | atomic64_set(&counter->hw.prev_count, now); |
| 2291 | |
| 2292 | delta = now - prev; |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 2293 | |
| 2294 | atomic64_add(delta, &counter->count); |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 2295 | } |
| 2296 | |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 2297 | static int task_clock_perf_counter_enable(struct perf_counter *counter) |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 2298 | { |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2299 | struct hw_perf_counter *hwc = &counter->hw; |
| 2300 | |
| 2301 | atomic64_set(&hwc->prev_count, task_clock_perf_counter_val(counter, 0)); |
Peter Zijlstra | 039fc91 | 2009-03-13 16:43:47 +0100 | [diff] [blame] | 2302 | hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 2303 | hwc->hrtimer.function = perf_swcounter_hrtimer; |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2304 | if (hwc->irq_period) { |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2305 | __hrtimer_start_range_ns(&hwc->hrtimer, |
| 2306 | ns_to_ktime(hwc->irq_period), 0, |
| 2307 | HRTIMER_MODE_REL, 0); |
| 2308 | } |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 2309 | |
| 2310 | return 0; |
Ingo Molnar | 8cb391e | 2008-12-14 12:22:31 +0100 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | static void task_clock_perf_counter_disable(struct perf_counter *counter) |
| 2314 | { |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2315 | hrtimer_cancel(&counter->hw.hrtimer); |
| 2316 | task_clock_perf_counter_update(counter, |
| 2317 | task_clock_perf_counter_val(counter, 0)); |
| 2318 | } |
Ingo Molnar | aa9c4c0 | 2008-12-17 14:10:57 +0100 | [diff] [blame] | 2319 | |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2320 | static void task_clock_perf_counter_read(struct perf_counter *counter) |
| 2321 | { |
| 2322 | task_clock_perf_counter_update(counter, |
| 2323 | task_clock_perf_counter_val(counter, 1)); |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 2324 | } |
| 2325 | |
| 2326 | static const struct hw_perf_counter_ops perf_ops_task_clock = { |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 2327 | .enable = task_clock_perf_counter_enable, |
| 2328 | .disable = task_clock_perf_counter_disable, |
| 2329 | .read = task_clock_perf_counter_read, |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 2330 | }; |
| 2331 | |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2332 | /* |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2333 | * Software counter: cpu migrations |
| 2334 | */ |
| 2335 | |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 2336 | static inline u64 get_cpu_migrations(struct perf_counter *counter) |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 2337 | { |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 2338 | struct task_struct *curr = counter->ctx->task; |
| 2339 | |
| 2340 | if (curr) |
| 2341 | return curr->se.nr_migrations; |
| 2342 | return cpu_nr_migrations(smp_processor_id()); |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 2343 | } |
| 2344 | |
| 2345 | static void cpu_migrations_perf_counter_update(struct perf_counter *counter) |
| 2346 | { |
| 2347 | u64 prev, now; |
| 2348 | s64 delta; |
| 2349 | |
| 2350 | prev = atomic64_read(&counter->hw.prev_count); |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 2351 | now = get_cpu_migrations(counter); |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 2352 | |
| 2353 | atomic64_set(&counter->hw.prev_count, now); |
| 2354 | |
| 2355 | delta = now - prev; |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 2356 | |
| 2357 | atomic64_add(delta, &counter->count); |
| 2358 | } |
| 2359 | |
| 2360 | static void cpu_migrations_perf_counter_read(struct perf_counter *counter) |
| 2361 | { |
| 2362 | cpu_migrations_perf_counter_update(counter); |
| 2363 | } |
| 2364 | |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 2365 | static int cpu_migrations_perf_counter_enable(struct perf_counter *counter) |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 2366 | { |
Paul Mackerras | c07c99b | 2009-02-13 22:10:34 +1100 | [diff] [blame] | 2367 | if (counter->prev_state <= PERF_COUNTER_STATE_OFF) |
| 2368 | atomic64_set(&counter->hw.prev_count, |
| 2369 | get_cpu_migrations(counter)); |
Ingo Molnar | 95cdd2e | 2008-12-21 13:50:42 +0100 | [diff] [blame] | 2370 | return 0; |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 2371 | } |
| 2372 | |
| 2373 | static void cpu_migrations_perf_counter_disable(struct perf_counter *counter) |
| 2374 | { |
| 2375 | cpu_migrations_perf_counter_update(counter); |
| 2376 | } |
| 2377 | |
| 2378 | static const struct hw_perf_counter_ops perf_ops_cpu_migrations = { |
Ingo Molnar | 7671581 | 2008-12-17 14:20:28 +0100 | [diff] [blame] | 2379 | .enable = cpu_migrations_perf_counter_enable, |
| 2380 | .disable = cpu_migrations_perf_counter_disable, |
| 2381 | .read = cpu_migrations_perf_counter_read, |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 2382 | }; |
| 2383 | |
Peter Zijlstra | e077df4 | 2009-03-19 20:26:17 +0100 | [diff] [blame] | 2384 | #ifdef CONFIG_EVENT_PROFILE |
| 2385 | void perf_tpcounter_event(int event_id) |
| 2386 | { |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2387 | struct pt_regs *regs = get_irq_regs(); |
| 2388 | |
| 2389 | if (!regs) |
| 2390 | regs = task_pt_regs(current); |
| 2391 | |
| 2392 | __perf_swcounter_event(PERF_TYPE_TRACEPOINT, event_id, 1, 1, regs); |
Peter Zijlstra | e077df4 | 2009-03-19 20:26:17 +0100 | [diff] [blame] | 2393 | } |
| 2394 | |
| 2395 | extern int ftrace_profile_enable(int); |
| 2396 | extern void ftrace_profile_disable(int); |
| 2397 | |
| 2398 | static void tp_perf_counter_destroy(struct perf_counter *counter) |
| 2399 | { |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 2400 | ftrace_profile_disable(perf_event_id(&counter->hw_event)); |
Peter Zijlstra | e077df4 | 2009-03-19 20:26:17 +0100 | [diff] [blame] | 2401 | } |
| 2402 | |
| 2403 | static const struct hw_perf_counter_ops * |
| 2404 | tp_perf_counter_init(struct perf_counter *counter) |
| 2405 | { |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 2406 | int event_id = perf_event_id(&counter->hw_event); |
Peter Zijlstra | e077df4 | 2009-03-19 20:26:17 +0100 | [diff] [blame] | 2407 | int ret; |
| 2408 | |
| 2409 | ret = ftrace_profile_enable(event_id); |
| 2410 | if (ret) |
| 2411 | return NULL; |
| 2412 | |
| 2413 | counter->destroy = tp_perf_counter_destroy; |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2414 | counter->hw.irq_period = counter->hw_event.irq_period; |
Peter Zijlstra | e077df4 | 2009-03-19 20:26:17 +0100 | [diff] [blame] | 2415 | |
| 2416 | return &perf_ops_generic; |
| 2417 | } |
| 2418 | #else |
| 2419 | static const struct hw_perf_counter_ops * |
| 2420 | tp_perf_counter_init(struct perf_counter *counter) |
| 2421 | { |
| 2422 | return NULL; |
| 2423 | } |
| 2424 | #endif |
| 2425 | |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2426 | static const struct hw_perf_counter_ops * |
| 2427 | sw_perf_counter_init(struct perf_counter *counter) |
| 2428 | { |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2429 | struct perf_counter_hw_event *hw_event = &counter->hw_event; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2430 | const struct hw_perf_counter_ops *hw_ops = NULL; |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2431 | struct hw_perf_counter *hwc = &counter->hw; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2432 | |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 2433 | /* |
| 2434 | * Software counters (currently) can't in general distinguish |
| 2435 | * between user, kernel and hypervisor events. |
| 2436 | * However, context switches and cpu migrations are considered |
| 2437 | * to be kernel events, and page faults are never hypervisor |
| 2438 | * events. |
| 2439 | */ |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 2440 | switch (perf_event_id(&counter->hw_event)) { |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2441 | case PERF_COUNT_CPU_CLOCK: |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2442 | hw_ops = &perf_ops_cpu_clock; |
| 2443 | |
| 2444 | if (hw_event->irq_period && hw_event->irq_period < 10000) |
| 2445 | hw_event->irq_period = 10000; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2446 | break; |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 2447 | case PERF_COUNT_TASK_CLOCK: |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 2448 | /* |
| 2449 | * If the user instantiates this as a per-cpu counter, |
| 2450 | * use the cpu_clock counter instead. |
| 2451 | */ |
| 2452 | if (counter->ctx->task) |
| 2453 | hw_ops = &perf_ops_task_clock; |
| 2454 | else |
| 2455 | hw_ops = &perf_ops_cpu_clock; |
Peter Zijlstra | d6d020e | 2009-03-13 12:21:35 +0100 | [diff] [blame] | 2456 | |
| 2457 | if (hw_event->irq_period && hw_event->irq_period < 10000) |
| 2458 | hw_event->irq_period = 10000; |
Ingo Molnar | bae43c9 | 2008-12-11 14:03:20 +0100 | [diff] [blame] | 2459 | break; |
Ingo Molnar | e06c61a | 2008-12-14 14:44:31 +0100 | [diff] [blame] | 2460 | case PERF_COUNT_PAGE_FAULTS: |
Peter Zijlstra | ac17dc8 | 2009-03-13 12:21:34 +0100 | [diff] [blame] | 2461 | case PERF_COUNT_PAGE_FAULTS_MIN: |
| 2462 | case PERF_COUNT_PAGE_FAULTS_MAJ: |
Ingo Molnar | 5d6a27d | 2008-12-14 12:28:33 +0100 | [diff] [blame] | 2463 | case PERF_COUNT_CONTEXT_SWITCHES: |
Peter Zijlstra | 4a0deca | 2009-03-19 20:26:12 +0100 | [diff] [blame] | 2464 | hw_ops = &perf_ops_generic; |
Ingo Molnar | 5d6a27d | 2008-12-14 12:28:33 +0100 | [diff] [blame] | 2465 | break; |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 2466 | case PERF_COUNT_CPU_MIGRATIONS: |
Paul Mackerras | 0475f9e | 2009-02-11 14:35:35 +1100 | [diff] [blame] | 2467 | if (!counter->hw_event.exclude_kernel) |
| 2468 | hw_ops = &perf_ops_cpu_migrations; |
Ingo Molnar | 6c594c2 | 2008-12-14 12:34:15 +0100 | [diff] [blame] | 2469 | break; |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2470 | } |
Peter Zijlstra | 15dbf27 | 2009-03-13 12:21:32 +0100 | [diff] [blame] | 2471 | |
| 2472 | if (hw_ops) |
| 2473 | hwc->irq_period = hw_event->irq_period; |
| 2474 | |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2475 | return hw_ops; |
| 2476 | } |
| 2477 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2478 | /* |
| 2479 | * Allocate and initialize a counter structure |
| 2480 | */ |
| 2481 | static struct perf_counter * |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2482 | perf_counter_alloc(struct perf_counter_hw_event *hw_event, |
| 2483 | int cpu, |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 2484 | struct perf_counter_context *ctx, |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2485 | struct perf_counter *group_leader, |
| 2486 | gfp_t gfpflags) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2487 | { |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2488 | const struct hw_perf_counter_ops *hw_ops; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 2489 | struct perf_counter *counter; |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 2490 | long err; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2491 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2492 | counter = kzalloc(sizeof(*counter), gfpflags); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2493 | if (!counter) |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 2494 | return ERR_PTR(-ENOMEM); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2495 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2496 | /* |
| 2497 | * Single counters are their own group leaders, with an |
| 2498 | * empty sibling list: |
| 2499 | */ |
| 2500 | if (!group_leader) |
| 2501 | group_leader = counter; |
| 2502 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2503 | mutex_init(&counter->mutex); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2504 | INIT_LIST_HEAD(&counter->list_entry); |
Peter Zijlstra | 592903c | 2009-03-13 12:21:36 +0100 | [diff] [blame] | 2505 | INIT_LIST_HEAD(&counter->event_entry); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2506 | INIT_LIST_HEAD(&counter->sibling_list); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2507 | init_waitqueue_head(&counter->waitq); |
| 2508 | |
Peter Zijlstra | 7b732a7 | 2009-03-23 18:22:10 +0100 | [diff] [blame] | 2509 | mutex_init(&counter->mmap_mutex); |
| 2510 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2511 | INIT_LIST_HEAD(&counter->child_list); |
| 2512 | |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 2513 | counter->cpu = cpu; |
| 2514 | counter->hw_event = *hw_event; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2515 | counter->group_leader = group_leader; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 2516 | counter->hw_ops = NULL; |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 2517 | counter->ctx = ctx; |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 2518 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 2519 | counter->state = PERF_COUNTER_STATE_INACTIVE; |
Ingo Molnar | a86ed50 | 2008-12-17 00:43:10 +0100 | [diff] [blame] | 2520 | if (hw_event->disabled) |
| 2521 | counter->state = PERF_COUNTER_STATE_OFF; |
| 2522 | |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2523 | hw_ops = NULL; |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2524 | |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 2525 | if (perf_event_raw(hw_event)) { |
Ingo Molnar | 5c92d12 | 2008-12-11 13:21:10 +0100 | [diff] [blame] | 2526 | hw_ops = hw_perf_counter_init(counter); |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 2527 | goto done; |
| 2528 | } |
| 2529 | |
| 2530 | switch (perf_event_type(hw_event)) { |
Peter Zijlstra | b8e8351 | 2009-03-19 20:26:18 +0100 | [diff] [blame] | 2531 | case PERF_TYPE_HARDWARE: |
| 2532 | hw_ops = hw_perf_counter_init(counter); |
| 2533 | break; |
| 2534 | |
| 2535 | case PERF_TYPE_SOFTWARE: |
| 2536 | hw_ops = sw_perf_counter_init(counter); |
| 2537 | break; |
| 2538 | |
| 2539 | case PERF_TYPE_TRACEPOINT: |
| 2540 | hw_ops = tp_perf_counter_init(counter); |
| 2541 | break; |
| 2542 | } |
Peter Zijlstra | f4a2deb | 2009-03-23 18:22:06 +0100 | [diff] [blame] | 2543 | done: |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 2544 | err = 0; |
| 2545 | if (!hw_ops) |
| 2546 | err = -EINVAL; |
| 2547 | else if (IS_ERR(hw_ops)) |
| 2548 | err = PTR_ERR(hw_ops); |
| 2549 | |
| 2550 | if (err) { |
| 2551 | kfree(counter); |
| 2552 | return ERR_PTR(err); |
| 2553 | } |
| 2554 | |
Ingo Molnar | 621a01e | 2008-12-11 12:46:46 +0100 | [diff] [blame] | 2555 | counter->hw_ops = hw_ops; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2556 | |
| 2557 | return counter; |
| 2558 | } |
| 2559 | |
| 2560 | /** |
Paul Mackerras | 2743a5b | 2009-03-04 20:36:51 +1100 | [diff] [blame] | 2561 | * sys_perf_counter_open - open a performance counter, associate it to a task/cpu |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 2562 | * |
| 2563 | * @hw_event_uptr: event type attributes for monitoring/sampling |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2564 | * @pid: target pid |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 2565 | * @cpu: target cpu |
| 2566 | * @group_fd: group leader counter fd |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2567 | */ |
Paul Mackerras | 2743a5b | 2009-03-04 20:36:51 +1100 | [diff] [blame] | 2568 | SYSCALL_DEFINE5(perf_counter_open, |
Paul Mackerras | f3dfd26 | 2009-02-26 22:43:46 +1100 | [diff] [blame] | 2569 | const struct perf_counter_hw_event __user *, hw_event_uptr, |
Paul Mackerras | 2743a5b | 2009-03-04 20:36:51 +1100 | [diff] [blame] | 2570 | pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2571 | { |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2572 | struct perf_counter *counter, *group_leader; |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 2573 | struct perf_counter_hw_event hw_event; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2574 | struct perf_counter_context *ctx; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2575 | struct file *counter_file = NULL; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2576 | struct file *group_file = NULL; |
| 2577 | int fput_needed = 0; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2578 | int fput_needed2 = 0; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2579 | int ret; |
| 2580 | |
Paul Mackerras | 2743a5b | 2009-03-04 20:36:51 +1100 | [diff] [blame] | 2581 | /* for future expandability... */ |
| 2582 | if (flags) |
| 2583 | return -EINVAL; |
| 2584 | |
Ingo Molnar | 9f66a38 | 2008-12-10 12:33:23 +0100 | [diff] [blame] | 2585 | 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] | 2586 | return -EFAULT; |
| 2587 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2588 | /* |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 2589 | * Get the target context (task or percpu): |
| 2590 | */ |
| 2591 | ctx = find_get_context(pid, cpu); |
| 2592 | if (IS_ERR(ctx)) |
| 2593 | return PTR_ERR(ctx); |
| 2594 | |
| 2595 | /* |
| 2596 | * Look up the group leader (we will attach this counter to it): |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2597 | */ |
| 2598 | group_leader = NULL; |
| 2599 | if (group_fd != -1) { |
| 2600 | ret = -EINVAL; |
| 2601 | group_file = fget_light(group_fd, &fput_needed); |
| 2602 | if (!group_file) |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 2603 | goto err_put_context; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2604 | if (group_file->f_op != &perf_fops) |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 2605 | goto err_put_context; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2606 | |
| 2607 | group_leader = group_file->private_data; |
| 2608 | /* |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 2609 | * Do not allow a recursive hierarchy (this new sibling |
| 2610 | * becoming part of another group-sibling): |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2611 | */ |
Ingo Molnar | ccff286 | 2008-12-11 11:26:29 +0100 | [diff] [blame] | 2612 | if (group_leader->group_leader != group_leader) |
| 2613 | goto err_put_context; |
| 2614 | /* |
| 2615 | * Do not allow to attach to a group in a different |
| 2616 | * task or CPU context: |
| 2617 | */ |
| 2618 | if (group_leader->ctx != ctx) |
| 2619 | goto err_put_context; |
Paul Mackerras | 3b6f9e5 | 2009-01-14 21:00:30 +1100 | [diff] [blame] | 2620 | /* |
| 2621 | * Only a group leader can be exclusive or pinned |
| 2622 | */ |
| 2623 | if (hw_event.exclusive || hw_event.pinned) |
| 2624 | goto err_put_context; |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2625 | } |
| 2626 | |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 2627 | counter = perf_counter_alloc(&hw_event, cpu, ctx, group_leader, |
| 2628 | GFP_KERNEL); |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 2629 | ret = PTR_ERR(counter); |
| 2630 | if (IS_ERR(counter)) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2631 | goto err_put_context; |
| 2632 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2633 | ret = anon_inode_getfd("[perf_counter]", &perf_fops, counter, 0); |
| 2634 | if (ret < 0) |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2635 | goto err_free_put_context; |
| 2636 | |
| 2637 | counter_file = fget_light(ret, &fput_needed2); |
| 2638 | if (!counter_file) |
| 2639 | goto err_free_put_context; |
| 2640 | |
| 2641 | counter->filp = counter_file; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2642 | mutex_lock(&ctx->mutex); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2643 | perf_install_in_context(ctx, counter, cpu); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2644 | mutex_unlock(&ctx->mutex); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2645 | |
| 2646 | fput_light(counter_file, fput_needed2); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2647 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2648 | out_fput: |
| 2649 | fput_light(group_file, fput_needed); |
| 2650 | |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2651 | return ret; |
| 2652 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2653 | err_free_put_context: |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2654 | kfree(counter); |
| 2655 | |
| 2656 | err_put_context: |
| 2657 | put_context(ctx); |
| 2658 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2659 | goto out_fput; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2660 | } |
| 2661 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2662 | /* |
| 2663 | * Initialize the perf_counter context in a task_struct: |
| 2664 | */ |
| 2665 | static void |
| 2666 | __perf_counter_init_context(struct perf_counter_context *ctx, |
| 2667 | struct task_struct *task) |
| 2668 | { |
| 2669 | memset(ctx, 0, sizeof(*ctx)); |
| 2670 | spin_lock_init(&ctx->lock); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2671 | mutex_init(&ctx->mutex); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2672 | INIT_LIST_HEAD(&ctx->counter_list); |
Peter Zijlstra | 592903c | 2009-03-13 12:21:36 +0100 | [diff] [blame] | 2673 | INIT_LIST_HEAD(&ctx->event_list); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2674 | ctx->task = task; |
| 2675 | } |
| 2676 | |
| 2677 | /* |
| 2678 | * inherit a counter from parent task to child task: |
| 2679 | */ |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2680 | static struct perf_counter * |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2681 | inherit_counter(struct perf_counter *parent_counter, |
| 2682 | struct task_struct *parent, |
| 2683 | struct perf_counter_context *parent_ctx, |
| 2684 | struct task_struct *child, |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2685 | struct perf_counter *group_leader, |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2686 | struct perf_counter_context *child_ctx) |
| 2687 | { |
| 2688 | struct perf_counter *child_counter; |
| 2689 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2690 | /* |
| 2691 | * Instead of creating recursive hierarchies of counters, |
| 2692 | * we link inherited counters back to the original parent, |
| 2693 | * which has a filp for sure, which we use as the reference |
| 2694 | * count: |
| 2695 | */ |
| 2696 | if (parent_counter->parent) |
| 2697 | parent_counter = parent_counter->parent; |
| 2698 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2699 | child_counter = perf_counter_alloc(&parent_counter->hw_event, |
Paul Mackerras | 23a185c | 2009-02-09 22:42:47 +1100 | [diff] [blame] | 2700 | parent_counter->cpu, child_ctx, |
| 2701 | group_leader, GFP_KERNEL); |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 2702 | if (IS_ERR(child_counter)) |
| 2703 | return child_counter; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2704 | |
| 2705 | /* |
| 2706 | * Link it up in the child's context: |
| 2707 | */ |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2708 | child_counter->task = child; |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 2709 | add_counter_to_ctx(child_counter, child_ctx); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2710 | |
| 2711 | child_counter->parent = parent_counter; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2712 | /* |
| 2713 | * inherit into child's child as well: |
| 2714 | */ |
| 2715 | child_counter->hw_event.inherit = 1; |
| 2716 | |
| 2717 | /* |
| 2718 | * Get a reference to the parent filp - we will fput it |
| 2719 | * when the child counter exits. This is safe to do because |
| 2720 | * we are in the parent and we know that the filp still |
| 2721 | * exists and has a nonzero count: |
| 2722 | */ |
| 2723 | atomic_long_inc(&parent_counter->filp->f_count); |
| 2724 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2725 | /* |
| 2726 | * Link this into the parent counter's child list |
| 2727 | */ |
| 2728 | mutex_lock(&parent_counter->mutex); |
| 2729 | list_add_tail(&child_counter->child_list, &parent_counter->child_list); |
| 2730 | |
| 2731 | /* |
| 2732 | * Make the child state follow the state of the parent counter, |
| 2733 | * not its hw_event.disabled bit. We hold the parent's mutex, |
| 2734 | * so we won't race with perf_counter_{en,dis}able_family. |
| 2735 | */ |
| 2736 | if (parent_counter->state >= PERF_COUNTER_STATE_INACTIVE) |
| 2737 | child_counter->state = PERF_COUNTER_STATE_INACTIVE; |
| 2738 | else |
| 2739 | child_counter->state = PERF_COUNTER_STATE_OFF; |
| 2740 | |
| 2741 | mutex_unlock(&parent_counter->mutex); |
| 2742 | |
| 2743 | return child_counter; |
| 2744 | } |
| 2745 | |
| 2746 | static int inherit_group(struct perf_counter *parent_counter, |
| 2747 | struct task_struct *parent, |
| 2748 | struct perf_counter_context *parent_ctx, |
| 2749 | struct task_struct *child, |
| 2750 | struct perf_counter_context *child_ctx) |
| 2751 | { |
| 2752 | struct perf_counter *leader; |
| 2753 | struct perf_counter *sub; |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 2754 | struct perf_counter *child_ctr; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2755 | |
| 2756 | leader = inherit_counter(parent_counter, parent, parent_ctx, |
| 2757 | child, NULL, child_ctx); |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 2758 | if (IS_ERR(leader)) |
| 2759 | return PTR_ERR(leader); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2760 | list_for_each_entry(sub, &parent_counter->sibling_list, list_entry) { |
Paul Mackerras | d5d2bc0d | 2009-03-30 19:07:08 +0200 | [diff] [blame] | 2761 | child_ctr = inherit_counter(sub, parent, parent_ctx, |
| 2762 | child, leader, child_ctx); |
| 2763 | if (IS_ERR(child_ctr)) |
| 2764 | return PTR_ERR(child_ctr); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2765 | } |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2766 | return 0; |
| 2767 | } |
| 2768 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2769 | static void sync_child_counter(struct perf_counter *child_counter, |
| 2770 | struct perf_counter *parent_counter) |
| 2771 | { |
| 2772 | u64 parent_val, child_val; |
| 2773 | |
| 2774 | parent_val = atomic64_read(&parent_counter->count); |
| 2775 | child_val = atomic64_read(&child_counter->count); |
| 2776 | |
| 2777 | /* |
| 2778 | * Add back the child's count to the parent's count: |
| 2779 | */ |
| 2780 | atomic64_add(child_val, &parent_counter->count); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 2781 | atomic64_add(child_counter->total_time_enabled, |
| 2782 | &parent_counter->child_total_time_enabled); |
| 2783 | atomic64_add(child_counter->total_time_running, |
| 2784 | &parent_counter->child_total_time_running); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2785 | |
| 2786 | /* |
| 2787 | * Remove this counter from the parent's list |
| 2788 | */ |
| 2789 | mutex_lock(&parent_counter->mutex); |
| 2790 | list_del_init(&child_counter->child_list); |
| 2791 | mutex_unlock(&parent_counter->mutex); |
| 2792 | |
| 2793 | /* |
| 2794 | * Release the parent counter, if this was the last |
| 2795 | * reference to it. |
| 2796 | */ |
| 2797 | fput(parent_counter->filp); |
| 2798 | } |
| 2799 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2800 | static void |
| 2801 | __perf_counter_exit_task(struct task_struct *child, |
| 2802 | struct perf_counter *child_counter, |
| 2803 | struct perf_counter_context *child_ctx) |
| 2804 | { |
| 2805 | struct perf_counter *parent_counter; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2806 | struct perf_counter *sub, *tmp; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2807 | |
| 2808 | /* |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 2809 | * If we do not self-reap then we have to wait for the |
| 2810 | * child task to unschedule (it will happen for sure), |
| 2811 | * so that its counter is at its final count. (This |
| 2812 | * condition triggers rarely - child tasks usually get |
| 2813 | * off their CPU before the parent has a chance to |
| 2814 | * get this far into the reaping action) |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2815 | */ |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 2816 | if (child != current) { |
| 2817 | wait_task_inactive(child, 0); |
| 2818 | list_del_init(&child_counter->list_entry); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 2819 | update_counter_times(child_counter); |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 2820 | } else { |
Ingo Molnar | 0cc0c02 | 2008-12-14 23:20:36 +0100 | [diff] [blame] | 2821 | struct perf_cpu_context *cpuctx; |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 2822 | unsigned long flags; |
| 2823 | u64 perf_flags; |
| 2824 | |
| 2825 | /* |
| 2826 | * Disable and unlink this counter. |
| 2827 | * |
| 2828 | * Be careful about zapping the list - IRQ/NMI context |
| 2829 | * could still be processing it: |
| 2830 | */ |
| 2831 | curr_rq_lock_irq_save(&flags); |
| 2832 | perf_flags = hw_perf_save_disable(); |
Ingo Molnar | 0cc0c02 | 2008-12-14 23:20:36 +0100 | [diff] [blame] | 2833 | |
| 2834 | cpuctx = &__get_cpu_var(perf_cpu_context); |
| 2835 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2836 | group_sched_out(child_counter, cpuctx, child_ctx); |
Paul Mackerras | 53cfbf5 | 2009-03-25 22:46:58 +1100 | [diff] [blame] | 2837 | update_counter_times(child_counter); |
Ingo Molnar | 0cc0c02 | 2008-12-14 23:20:36 +0100 | [diff] [blame] | 2838 | |
Ingo Molnar | 235c7fc | 2008-12-21 14:43:25 +0100 | [diff] [blame] | 2839 | list_del_init(&child_counter->list_entry); |
| 2840 | |
| 2841 | child_ctx->nr_counters--; |
| 2842 | |
| 2843 | hw_perf_restore(perf_flags); |
| 2844 | curr_rq_unlock_irq_restore(&flags); |
Ingo Molnar | 0cc0c02 | 2008-12-14 23:20:36 +0100 | [diff] [blame] | 2845 | } |
| 2846 | |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2847 | parent_counter = child_counter->parent; |
| 2848 | /* |
| 2849 | * It can happen that parent exits first, and has counters |
| 2850 | * that are still around due to the child reference. These |
| 2851 | * counters need to be zapped - but otherwise linger. |
| 2852 | */ |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2853 | if (parent_counter) { |
| 2854 | sync_child_counter(child_counter, parent_counter); |
| 2855 | list_for_each_entry_safe(sub, tmp, &child_counter->sibling_list, |
| 2856 | list_entry) { |
Paul Mackerras | 4bcf349 | 2009-02-11 13:53:19 +0100 | [diff] [blame] | 2857 | if (sub->parent) { |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2858 | sync_child_counter(sub, sub->parent); |
Peter Zijlstra | f160095 | 2009-03-19 20:26:16 +0100 | [diff] [blame] | 2859 | free_counter(sub); |
Paul Mackerras | 4bcf349 | 2009-02-11 13:53:19 +0100 | [diff] [blame] | 2860 | } |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2861 | } |
Peter Zijlstra | f160095 | 2009-03-19 20:26:16 +0100 | [diff] [blame] | 2862 | free_counter(child_counter); |
Paul Mackerras | 4bcf349 | 2009-02-11 13:53:19 +0100 | [diff] [blame] | 2863 | } |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2864 | } |
| 2865 | |
| 2866 | /* |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2867 | * When a child task exits, feed back counter values to parent counters. |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2868 | * |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2869 | * Note: we may be running in child context, but the PID is not hashed |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2870 | * anymore so new counters will not be added. |
| 2871 | */ |
| 2872 | void perf_counter_exit_task(struct task_struct *child) |
| 2873 | { |
| 2874 | struct perf_counter *child_counter, *tmp; |
| 2875 | struct perf_counter_context *child_ctx; |
| 2876 | |
| 2877 | child_ctx = &child->perf_counter_ctx; |
| 2878 | |
| 2879 | if (likely(!child_ctx->nr_counters)) |
| 2880 | return; |
| 2881 | |
| 2882 | list_for_each_entry_safe(child_counter, tmp, &child_ctx->counter_list, |
| 2883 | list_entry) |
| 2884 | __perf_counter_exit_task(child, child_counter, child_ctx); |
| 2885 | } |
| 2886 | |
| 2887 | /* |
| 2888 | * Initialize the perf_counter context in task_struct |
| 2889 | */ |
| 2890 | void perf_counter_init_task(struct task_struct *child) |
| 2891 | { |
| 2892 | struct perf_counter_context *child_ctx, *parent_ctx; |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2893 | struct perf_counter *counter; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2894 | struct task_struct *parent = current; |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2895 | |
| 2896 | child_ctx = &child->perf_counter_ctx; |
| 2897 | parent_ctx = &parent->perf_counter_ctx; |
| 2898 | |
| 2899 | __perf_counter_init_context(child_ctx, child); |
| 2900 | |
| 2901 | /* |
| 2902 | * This is executed from the parent task context, so inherit |
| 2903 | * counters that have been marked for cloning: |
| 2904 | */ |
| 2905 | |
| 2906 | if (likely(!parent_ctx->nr_counters)) |
| 2907 | return; |
| 2908 | |
| 2909 | /* |
| 2910 | * Lock the parent list. No need to lock the child - not PID |
| 2911 | * hashed yet and not running, so nobody can access it. |
| 2912 | */ |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2913 | mutex_lock(&parent_ctx->mutex); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2914 | |
| 2915 | /* |
| 2916 | * We dont have to disable NMIs - we are only looking at |
| 2917 | * the list, not manipulating it: |
| 2918 | */ |
| 2919 | list_for_each_entry(counter, &parent_ctx->counter_list, list_entry) { |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2920 | if (!counter->hw_event.inherit) |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2921 | continue; |
| 2922 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2923 | if (inherit_group(counter, parent, |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2924 | parent_ctx, child, child_ctx)) |
| 2925 | break; |
| 2926 | } |
| 2927 | |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2928 | mutex_unlock(&parent_ctx->mutex); |
Ingo Molnar | 9b51f66 | 2008-12-12 13:49:45 +0100 | [diff] [blame] | 2929 | } |
| 2930 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2931 | static void __cpuinit perf_counter_init_cpu(int cpu) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2932 | { |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2933 | struct perf_cpu_context *cpuctx; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2934 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2935 | cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 2936 | __perf_counter_init_context(&cpuctx->ctx, NULL); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2937 | |
| 2938 | mutex_lock(&perf_resource_mutex); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2939 | cpuctx->max_pertask = perf_max_counters - perf_reserved_percpu; |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2940 | mutex_unlock(&perf_resource_mutex); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2941 | |
Paul Mackerras | 01d0287 | 2009-01-14 13:44:19 +1100 | [diff] [blame] | 2942 | hw_perf_counter_setup(cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2943 | } |
| 2944 | |
| 2945 | #ifdef CONFIG_HOTPLUG_CPU |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2946 | static void __perf_counter_exit_cpu(void *info) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2947 | { |
| 2948 | struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context); |
| 2949 | struct perf_counter_context *ctx = &cpuctx->ctx; |
| 2950 | struct perf_counter *counter, *tmp; |
| 2951 | |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2952 | list_for_each_entry_safe(counter, tmp, &ctx->counter_list, list_entry) |
| 2953 | __perf_counter_remove_from_context(counter); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2954 | } |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2955 | static void perf_counter_exit_cpu(int cpu) |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2956 | { |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2957 | struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 2958 | struct perf_counter_context *ctx = &cpuctx->ctx; |
| 2959 | |
| 2960 | mutex_lock(&ctx->mutex); |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2961 | smp_call_function_single(cpu, __perf_counter_exit_cpu, NULL, 1); |
Paul Mackerras | d859e29 | 2009-01-17 18:10:22 +1100 | [diff] [blame] | 2962 | mutex_unlock(&ctx->mutex); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2963 | } |
| 2964 | #else |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2965 | static inline void perf_counter_exit_cpu(int cpu) { } |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2966 | #endif |
| 2967 | |
| 2968 | static int __cpuinit |
| 2969 | perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu) |
| 2970 | { |
| 2971 | unsigned int cpu = (long)hcpu; |
| 2972 | |
| 2973 | switch (action) { |
| 2974 | |
| 2975 | case CPU_UP_PREPARE: |
| 2976 | case CPU_UP_PREPARE_FROZEN: |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2977 | perf_counter_init_cpu(cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2978 | break; |
| 2979 | |
| 2980 | case CPU_DOWN_PREPARE: |
| 2981 | case CPU_DOWN_PREPARE_FROZEN: |
Ingo Molnar | 04289bb | 2008-12-11 08:38:42 +0100 | [diff] [blame] | 2982 | perf_counter_exit_cpu(cpu); |
Thomas Gleixner | 0793a61 | 2008-12-04 20:12:29 +0100 | [diff] [blame] | 2983 | break; |
| 2984 | |
| 2985 | default: |
| 2986 | break; |
| 2987 | } |
| 2988 | |
| 2989 | return NOTIFY_OK; |
| 2990 | } |
| 2991 | |
| 2992 | static struct notifier_block __cpuinitdata perf_cpu_nb = { |
| 2993 | .notifier_call = perf_cpu_notify, |
| 2994 | }; |
| 2995 | |
| 2996 | static int __init perf_counter_init(void) |
| 2997 | { |
| 2998 | perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE, |
| 2999 | (void *)(long)smp_processor_id()); |
| 3000 | register_cpu_notifier(&perf_cpu_nb); |
| 3001 | |
| 3002 | return 0; |
| 3003 | } |
| 3004 | early_initcall(perf_counter_init); |
| 3005 | |
| 3006 | static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf) |
| 3007 | { |
| 3008 | return sprintf(buf, "%d\n", perf_reserved_percpu); |
| 3009 | } |
| 3010 | |
| 3011 | static ssize_t |
| 3012 | perf_set_reserve_percpu(struct sysdev_class *class, |
| 3013 | const char *buf, |
| 3014 | size_t count) |
| 3015 | { |
| 3016 | struct perf_cpu_context *cpuctx; |
| 3017 | unsigned long val; |
| 3018 | int err, cpu, mpt; |
| 3019 | |
| 3020 | err = strict_strtoul(buf, 10, &val); |
| 3021 | if (err) |
| 3022 | return err; |
| 3023 | if (val > perf_max_counters) |
| 3024 | return -EINVAL; |
| 3025 | |
| 3026 | mutex_lock(&perf_resource_mutex); |
| 3027 | perf_reserved_percpu = val; |
| 3028 | for_each_online_cpu(cpu) { |
| 3029 | cpuctx = &per_cpu(perf_cpu_context, cpu); |
| 3030 | spin_lock_irq(&cpuctx->ctx.lock); |
| 3031 | mpt = min(perf_max_counters - cpuctx->ctx.nr_counters, |
| 3032 | perf_max_counters - perf_reserved_percpu); |
| 3033 | cpuctx->max_pertask = mpt; |
| 3034 | spin_unlock_irq(&cpuctx->ctx.lock); |
| 3035 | } |
| 3036 | mutex_unlock(&perf_resource_mutex); |
| 3037 | |
| 3038 | return count; |
| 3039 | } |
| 3040 | |
| 3041 | static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf) |
| 3042 | { |
| 3043 | return sprintf(buf, "%d\n", perf_overcommit); |
| 3044 | } |
| 3045 | |
| 3046 | static ssize_t |
| 3047 | perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count) |
| 3048 | { |
| 3049 | unsigned long val; |
| 3050 | int err; |
| 3051 | |
| 3052 | err = strict_strtoul(buf, 10, &val); |
| 3053 | if (err) |
| 3054 | return err; |
| 3055 | if (val > 1) |
| 3056 | return -EINVAL; |
| 3057 | |
| 3058 | mutex_lock(&perf_resource_mutex); |
| 3059 | perf_overcommit = val; |
| 3060 | mutex_unlock(&perf_resource_mutex); |
| 3061 | |
| 3062 | return count; |
| 3063 | } |
| 3064 | |
| 3065 | static SYSDEV_CLASS_ATTR( |
| 3066 | reserve_percpu, |
| 3067 | 0644, |
| 3068 | perf_show_reserve_percpu, |
| 3069 | perf_set_reserve_percpu |
| 3070 | ); |
| 3071 | |
| 3072 | static SYSDEV_CLASS_ATTR( |
| 3073 | overcommit, |
| 3074 | 0644, |
| 3075 | perf_show_overcommit, |
| 3076 | perf_set_overcommit |
| 3077 | ); |
| 3078 | |
| 3079 | static struct attribute *perfclass_attrs[] = { |
| 3080 | &attr_reserve_percpu.attr, |
| 3081 | &attr_overcommit.attr, |
| 3082 | NULL |
| 3083 | }; |
| 3084 | |
| 3085 | static struct attribute_group perfclass_attr_group = { |
| 3086 | .attrs = perfclass_attrs, |
| 3087 | .name = "perf_counters", |
| 3088 | }; |
| 3089 | |
| 3090 | static int __init perf_counter_sysfs_init(void) |
| 3091 | { |
| 3092 | return sysfs_create_group(&cpu_sysdev_class.kset.kobj, |
| 3093 | &perfclass_attr_group); |
| 3094 | } |
| 3095 | device_initcall(perf_counter_sysfs_init); |