Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @file cpu_buffer.c |
| 3 | * |
| 4 | * @remark Copyright 2002 OProfile authors |
| 5 | * @remark Read the file COPYING |
| 6 | * |
| 7 | * @author John Levon <levon@movementarian.org> |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 8 | * @author Barry Kasindorf <barry.kasindorf@amd.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * |
| 10 | * Each CPU has a local buffer that stores PC value/event |
| 11 | * pairs. We also log context switches when we notice them. |
| 12 | * Eventually each CPU's buffer is processed into the global |
| 13 | * event buffer by sync_buffer(). |
| 14 | * |
| 15 | * We use a local buffer for two reasons: an NMI or similar |
| 16 | * interrupt cannot synchronise, and high sampling rates |
| 17 | * would lead to catastrophic global synchronisation if |
| 18 | * a global buffer was used. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/oprofile.h> |
| 23 | #include <linux/vmalloc.h> |
| 24 | #include <linux/errno.h> |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "event_buffer.h" |
| 27 | #include "cpu_buffer.h" |
| 28 | #include "buffer_sync.h" |
| 29 | #include "oprof.h" |
| 30 | |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 31 | #define OP_BUFFER_FLAGS 0 |
| 32 | |
| 33 | /* |
| 34 | * Read and write access is using spin locking. Thus, writing to the |
| 35 | * buffer by NMI handler (x86) could occur also during critical |
| 36 | * sections when reading the buffer. To avoid this, there are 2 |
| 37 | * buffers for independent read and write access. Read access is in |
| 38 | * process context only, write access only in the NMI handler. If the |
| 39 | * read buffer runs empty, both buffers are swapped atomically. There |
| 40 | * is potentially a small window during swapping where the buffers are |
| 41 | * disabled and samples could be lost. |
| 42 | * |
| 43 | * Using 2 buffers is a little bit overhead, but the solution is clear |
| 44 | * and does not require changes in the ring buffer implementation. It |
| 45 | * can be changed to a single buffer solution when the ring buffer |
| 46 | * access is implemented as non-locking atomic code. |
| 47 | */ |
Robert Richter | 9966718 | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 48 | static struct ring_buffer *op_ring_buffer_read; |
| 49 | static struct ring_buffer *op_ring_buffer_write; |
Eric Dumazet | 8b8b498 | 2008-05-14 16:05:31 -0700 | [diff] [blame] | 50 | DEFINE_PER_CPU(struct oprofile_cpu_buffer, cpu_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 52 | static void wq_sync_buffer(struct work_struct *work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | #define DEFAULT_TIMER_EXPIRE (HZ / 10) |
| 55 | static int work_enabled; |
| 56 | |
Carl Love | a5598ca | 2008-10-14 23:37:01 +0000 | [diff] [blame] | 57 | unsigned long oprofile_get_cpu_buffer_size(void) |
| 58 | { |
Robert Richter | bd2172f | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 59 | return oprofile_cpu_buffer_size; |
Carl Love | a5598ca | 2008-10-14 23:37:01 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void oprofile_cpu_buffer_inc_smpl_lost(void) |
| 63 | { |
| 64 | struct oprofile_cpu_buffer *cpu_buf |
| 65 | = &__get_cpu_var(cpu_buffer); |
| 66 | |
| 67 | cpu_buf->sample_lost_overflow++; |
| 68 | } |
| 69 | |
Robert Richter | 3001577 | 2008-12-23 01:35:12 +0100 | [diff] [blame] | 70 | void free_cpu_buffers(void) |
| 71 | { |
| 72 | if (op_ring_buffer_read) |
| 73 | ring_buffer_free(op_ring_buffer_read); |
| 74 | op_ring_buffer_read = NULL; |
| 75 | if (op_ring_buffer_write) |
| 76 | ring_buffer_free(op_ring_buffer_write); |
| 77 | op_ring_buffer_write = NULL; |
| 78 | } |
| 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | int alloc_cpu_buffers(void) |
| 81 | { |
| 82 | int i; |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 83 | |
Robert Richter | bd2172f | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 84 | unsigned long buffer_size = oprofile_cpu_buffer_size; |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 85 | |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 86 | op_ring_buffer_read = ring_buffer_alloc(buffer_size, OP_BUFFER_FLAGS); |
| 87 | if (!op_ring_buffer_read) |
| 88 | goto fail; |
| 89 | op_ring_buffer_write = ring_buffer_alloc(buffer_size, OP_BUFFER_FLAGS); |
| 90 | if (!op_ring_buffer_write) |
| 91 | goto fail; |
| 92 | |
Chris J Arges | 4bd9b9d | 2008-10-15 11:03:39 -0500 | [diff] [blame] | 93 | for_each_possible_cpu(i) { |
Mike Travis | 608dfdd | 2008-04-28 02:14:15 -0700 | [diff] [blame] | 94 | struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i); |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 95 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | b->last_task = NULL; |
| 97 | b->last_is_kernel = -1; |
| 98 | b->tracing = 0; |
| 99 | b->buffer_size = buffer_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | b->sample_received = 0; |
| 101 | b->sample_lost_overflow = 0; |
Philippe Elie | df9d177 | 2007-11-14 16:58:48 -0800 | [diff] [blame] | 102 | b->backtrace_aborted = 0; |
| 103 | b->sample_invalid_eip = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | b->cpu = i; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 105 | INIT_DELAYED_WORK(&b->work, wq_sync_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
| 107 | return 0; |
| 108 | |
| 109 | fail: |
| 110 | free_cpu_buffers(); |
| 111 | return -ENOMEM; |
| 112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | void start_cpu_work(void) |
| 115 | { |
| 116 | int i; |
| 117 | |
| 118 | work_enabled = 1; |
| 119 | |
| 120 | for_each_online_cpu(i) { |
Mike Travis | 608dfdd | 2008-04-28 02:14:15 -0700 | [diff] [blame] | 121 | struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
| 123 | /* |
| 124 | * Spread the work by 1 jiffy per cpu so they dont all |
| 125 | * fire at once. |
| 126 | */ |
| 127 | schedule_delayed_work_on(i, &b->work, DEFAULT_TIMER_EXPIRE + i); |
| 128 | } |
| 129 | } |
| 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | void end_cpu_work(void) |
| 132 | { |
| 133 | int i; |
| 134 | |
| 135 | work_enabled = 0; |
| 136 | |
| 137 | for_each_online_cpu(i) { |
Mike Travis | 608dfdd | 2008-04-28 02:14:15 -0700 | [diff] [blame] | 138 | struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | cancel_delayed_work(&b->work); |
| 141 | } |
| 142 | |
| 143 | flush_scheduled_work(); |
| 144 | } |
| 145 | |
Robert Richter | 9966718 | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 146 | int op_cpu_buffer_write_entry(struct op_entry *entry) |
| 147 | { |
| 148 | entry->event = ring_buffer_lock_reserve(op_ring_buffer_write, |
| 149 | sizeof(struct op_sample), |
| 150 | &entry->irq_flags); |
| 151 | if (entry->event) |
| 152 | entry->sample = ring_buffer_event_data(entry->event); |
| 153 | else |
| 154 | entry->sample = NULL; |
| 155 | |
| 156 | if (!entry->sample) |
| 157 | return -ENOMEM; |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | int op_cpu_buffer_write_commit(struct op_entry *entry) |
| 163 | { |
| 164 | return ring_buffer_unlock_commit(op_ring_buffer_write, entry->event, |
| 165 | entry->irq_flags); |
| 166 | } |
| 167 | |
| 168 | struct op_sample *op_cpu_buffer_read_entry(int cpu) |
| 169 | { |
| 170 | struct ring_buffer_event *e; |
| 171 | e = ring_buffer_consume(op_ring_buffer_read, cpu, NULL); |
| 172 | if (e) |
| 173 | return ring_buffer_event_data(e); |
| 174 | if (ring_buffer_swap_cpu(op_ring_buffer_read, |
| 175 | op_ring_buffer_write, |
| 176 | cpu)) |
| 177 | return NULL; |
| 178 | e = ring_buffer_consume(op_ring_buffer_read, cpu, NULL); |
| 179 | if (e) |
| 180 | return ring_buffer_event_data(e); |
| 181 | return NULL; |
| 182 | } |
| 183 | |
| 184 | unsigned long op_cpu_buffer_entries(int cpu) |
| 185 | { |
| 186 | return ring_buffer_entries_cpu(op_ring_buffer_read, cpu) |
| 187 | + ring_buffer_entries_cpu(op_ring_buffer_write, cpu); |
| 188 | } |
| 189 | |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 190 | static inline int |
Robert Richter | d0e2338 | 2008-12-23 04:03:05 +0100 | [diff] [blame^] | 191 | op_add_sample(struct oprofile_cpu_buffer *cpu_buf, |
| 192 | unsigned long pc, unsigned long event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | { |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 194 | struct op_entry entry; |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 195 | int ret; |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 196 | |
Robert Richter | 6d2c53f | 2008-12-24 16:53:53 +0100 | [diff] [blame] | 197 | ret = op_cpu_buffer_write_entry(&entry); |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 198 | if (ret) |
| 199 | return ret; |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 200 | |
| 201 | entry.sample->eip = pc; |
| 202 | entry.sample->event = event; |
| 203 | |
Robert Richter | 3967e93 | 2008-12-30 05:10:58 +0100 | [diff] [blame] | 204 | return op_cpu_buffer_write_commit(&entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 207 | static inline int |
Robert Richter | 25ad291 | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 208 | add_code(struct oprofile_cpu_buffer *buffer, unsigned long value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
Robert Richter | d0e2338 | 2008-12-23 04:03:05 +0100 | [diff] [blame^] | 210 | return op_add_sample(buffer, ESCAPE_CODE, value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | /* This must be safe from any context. It's safe writing here |
| 214 | * because of the head/tail separation of the writer and reader |
| 215 | * of the CPU buffer. |
| 216 | * |
| 217 | * is_kernel is needed because on some architectures you cannot |
| 218 | * tell if you are in kernel or user space simply by looking at |
| 219 | * pc. We tag this in the buffer by generating kernel enter/exit |
| 220 | * events whenever is_kernel changes |
| 221 | */ |
Robert Richter | 25ad291 | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 222 | static int log_sample(struct oprofile_cpu_buffer *cpu_buf, unsigned long pc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | int is_kernel, unsigned long event) |
| 224 | { |
Robert Richter | 25ad291 | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 225 | struct task_struct *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
| 227 | cpu_buf->sample_received++; |
| 228 | |
Philippe Elie | df9d177 | 2007-11-14 16:58:48 -0800 | [diff] [blame] | 229 | if (pc == ESCAPE_CODE) { |
| 230 | cpu_buf->sample_invalid_eip++; |
| 231 | return 0; |
| 232 | } |
| 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | is_kernel = !!is_kernel; |
| 235 | |
| 236 | task = current; |
| 237 | |
| 238 | /* notice a switch from user->kernel or vice versa */ |
| 239 | if (cpu_buf->last_is_kernel != is_kernel) { |
| 240 | cpu_buf->last_is_kernel = is_kernel; |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 241 | if (add_code(cpu_buf, is_kernel)) |
| 242 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /* notice a task switch */ |
| 246 | if (cpu_buf->last_task != task) { |
| 247 | cpu_buf->last_task = task; |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 248 | if (add_code(cpu_buf, (unsigned long)task)) |
| 249 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 251 | |
Robert Richter | d0e2338 | 2008-12-23 04:03:05 +0100 | [diff] [blame^] | 252 | if (op_add_sample(cpu_buf, pc, event)) |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 253 | goto fail; |
| 254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | return 1; |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 256 | |
| 257 | fail: |
| 258 | cpu_buf->sample_lost_overflow++; |
| 259 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Robert Richter | 6352d92 | 2008-12-18 22:09:13 +0100 | [diff] [blame] | 262 | static inline void oprofile_begin_trace(struct oprofile_cpu_buffer *cpu_buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | add_code(cpu_buf, CPU_TRACE_BEGIN); |
| 265 | cpu_buf->tracing = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Robert Richter | 6352d92 | 2008-12-18 22:09:13 +0100 | [diff] [blame] | 268 | static inline void oprofile_end_trace(struct oprofile_cpu_buffer *cpu_buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | { |
| 270 | cpu_buf->tracing = 0; |
| 271 | } |
| 272 | |
Robert Richter | d45d23b | 2008-12-16 12:00:10 +0100 | [diff] [blame] | 273 | static inline void |
| 274 | __oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs, |
| 275 | unsigned long event, int is_kernel) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
Mike Travis | 608dfdd | 2008-04-28 02:14:15 -0700 | [diff] [blame] | 277 | struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
Robert Richter | bd2172f | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 279 | if (!oprofile_backtrace_depth) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | log_sample(cpu_buf, pc, is_kernel, event); |
| 281 | return; |
| 282 | } |
| 283 | |
Robert Richter | 6352d92 | 2008-12-18 22:09:13 +0100 | [diff] [blame] | 284 | oprofile_begin_trace(cpu_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
Robert Richter | fd13f6c | 2008-10-19 21:00:09 +0200 | [diff] [blame] | 286 | /* |
| 287 | * if log_sample() fail we can't backtrace since we lost the |
| 288 | * source of this event |
| 289 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | if (log_sample(cpu_buf, pc, is_kernel, event)) |
Robert Richter | bd2172f | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 291 | oprofile_ops.backtrace(regs, oprofile_backtrace_depth); |
Robert Richter | 6352d92 | 2008-12-18 22:09:13 +0100 | [diff] [blame] | 292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | oprofile_end_trace(cpu_buf); |
| 294 | } |
| 295 | |
Robert Richter | d45d23b | 2008-12-16 12:00:10 +0100 | [diff] [blame] | 296 | void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs, |
| 297 | unsigned long event, int is_kernel) |
| 298 | { |
| 299 | __oprofile_add_ext_sample(pc, regs, event, is_kernel); |
| 300 | } |
| 301 | |
Brian Rogan | 2735771 | 2006-03-28 01:56:20 -0800 | [diff] [blame] | 302 | void oprofile_add_sample(struct pt_regs * const regs, unsigned long event) |
| 303 | { |
| 304 | int is_kernel = !user_mode(regs); |
| 305 | unsigned long pc = profile_pc(regs); |
| 306 | |
Robert Richter | d45d23b | 2008-12-16 12:00:10 +0100 | [diff] [blame] | 307 | __oprofile_add_ext_sample(pc, regs, event, is_kernel); |
Brian Rogan | 2735771 | 2006-03-28 01:56:20 -0800 | [diff] [blame] | 308 | } |
| 309 | |
Robert Richter | 852402c | 2008-07-22 21:09:06 +0200 | [diff] [blame] | 310 | #ifdef CONFIG_OPROFILE_IBS |
| 311 | |
Robert Richter | cdc1834 | 2008-09-26 22:18:44 -0400 | [diff] [blame] | 312 | void oprofile_add_ibs_sample(struct pt_regs * const regs, |
| 313 | unsigned int * const ibs_sample, int ibs_code) |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 314 | { |
Robert Richter | e2fee27 | 2008-07-18 17:36:20 +0200 | [diff] [blame] | 315 | int is_kernel = !user_mode(regs); |
| 316 | struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer); |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 317 | struct task_struct *task; |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 318 | int fail = 0; |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 319 | |
| 320 | cpu_buf->sample_received++; |
| 321 | |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 322 | /* notice a switch from user->kernel or vice versa */ |
| 323 | if (cpu_buf->last_is_kernel != is_kernel) { |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 324 | if (add_code(cpu_buf, is_kernel)) |
| 325 | goto fail; |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 326 | cpu_buf->last_is_kernel = is_kernel; |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /* notice a task switch */ |
| 330 | if (!is_kernel) { |
| 331 | task = current; |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 332 | if (cpu_buf->last_task != task) { |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 333 | if (add_code(cpu_buf, (unsigned long)task)) |
| 334 | goto fail; |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 335 | cpu_buf->last_task = task; |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 339 | fail = fail || add_code(cpu_buf, ibs_code); |
Robert Richter | d0e2338 | 2008-12-23 04:03:05 +0100 | [diff] [blame^] | 340 | fail = fail || op_add_sample(cpu_buf, ibs_sample[0], ibs_sample[1]); |
| 341 | fail = fail || op_add_sample(cpu_buf, ibs_sample[2], ibs_sample[3]); |
| 342 | fail = fail || op_add_sample(cpu_buf, ibs_sample[4], ibs_sample[5]); |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 343 | |
| 344 | if (ibs_code == IBS_OP_BEGIN) { |
Robert Richter | d0e2338 | 2008-12-23 04:03:05 +0100 | [diff] [blame^] | 345 | fail = fail || op_add_sample(cpu_buf, ibs_sample[6], ibs_sample[7]); |
| 346 | fail = fail || op_add_sample(cpu_buf, ibs_sample[8], ibs_sample[9]); |
| 347 | fail = fail || op_add_sample(cpu_buf, ibs_sample[10], ibs_sample[11]); |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 348 | } |
| 349 | |
Robert Richter | 8350c78 | 2008-12-19 12:59:28 +0100 | [diff] [blame] | 350 | if (!fail) |
| 351 | return; |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 352 | |
| 353 | fail: |
| 354 | cpu_buf->sample_lost_overflow++; |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 355 | } |
| 356 | |
Robert Richter | 852402c | 2008-07-22 21:09:06 +0200 | [diff] [blame] | 357 | #endif |
| 358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event) |
| 360 | { |
Mike Travis | 608dfdd | 2008-04-28 02:14:15 -0700 | [diff] [blame] | 361 | struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | log_sample(cpu_buf, pc, is_kernel, event); |
| 363 | } |
| 364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | void oprofile_add_trace(unsigned long pc) |
| 366 | { |
Mike Travis | 608dfdd | 2008-04-28 02:14:15 -0700 | [diff] [blame] | 367 | struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
| 369 | if (!cpu_buf->tracing) |
| 370 | return; |
| 371 | |
Robert Richter | fd13f6c | 2008-10-19 21:00:09 +0200 | [diff] [blame] | 372 | /* |
| 373 | * broken frame can give an eip with the same value as an |
| 374 | * escape code, abort the trace if we get it |
| 375 | */ |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 376 | if (pc == ESCAPE_CODE) |
| 377 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
Robert Richter | d0e2338 | 2008-12-23 04:03:05 +0100 | [diff] [blame^] | 379 | if (op_add_sample(cpu_buf, pc, 0)) |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 380 | goto fail; |
| 381 | |
| 382 | return; |
| 383 | fail: |
| 384 | cpu_buf->tracing = 0; |
| 385 | cpu_buf->backtrace_aborted++; |
| 386 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | /* |
| 390 | * This serves to avoid cpu buffer overflow, and makes sure |
| 391 | * the task mortuary progresses |
| 392 | * |
| 393 | * By using schedule_delayed_work_on and then schedule_delayed_work |
| 394 | * we guarantee this will stay on the correct cpu |
| 395 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 396 | static void wq_sync_buffer(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | { |
Robert Richter | 25ad291 | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 398 | struct oprofile_cpu_buffer *b = |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 399 | container_of(work, struct oprofile_cpu_buffer, work.work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | if (b->cpu != smp_processor_id()) { |
Robert Richter | bd17b62 | 2008-07-22 21:09:07 +0200 | [diff] [blame] | 401 | printk(KERN_DEBUG "WQ on CPU%d, prefer CPU%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | smp_processor_id(), b->cpu); |
Chris J Arges | 4bd9b9d | 2008-10-15 11:03:39 -0500 | [diff] [blame] | 403 | |
| 404 | if (!cpu_online(b->cpu)) { |
| 405 | cancel_delayed_work(&b->work); |
| 406 | return; |
| 407 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | } |
| 409 | sync_buffer(b->cpu); |
| 410 | |
| 411 | /* don't re-add the work if we're shutting down */ |
| 412 | if (work_enabled) |
| 413 | schedule_delayed_work(&b->work, DEFAULT_TIMER_EXPIRE); |
| 414 | } |