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