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