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