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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Andi Kleen | cb6e943 | 2010-04-01 03:17:25 +0200 | [diff] [blame] | 33 | static struct ring_buffer *op_ring_buffer; |
Tejun Heo | b3e9f67 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 34 | DEFINE_PER_CPU(struct oprofile_cpu_buffer, op_cpu_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 36 | static void wq_sync_buffer(struct work_struct *work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | #define DEFAULT_TIMER_EXPIRE (HZ / 10) |
| 39 | static int work_enabled; |
| 40 | |
Carl Love | a5598ca | 2008-10-14 23:37:01 +0000 | [diff] [blame] | 41 | unsigned long oprofile_get_cpu_buffer_size(void) |
| 42 | { |
Robert Richter | bd2172f | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 43 | return oprofile_cpu_buffer_size; |
Carl Love | a5598ca | 2008-10-14 23:37:01 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void oprofile_cpu_buffer_inc_smpl_lost(void) |
| 47 | { |
Tejun Heo | b3e9f67 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 48 | struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(op_cpu_buffer); |
Carl Love | a5598ca | 2008-10-14 23:37:01 +0000 | [diff] [blame] | 49 | |
| 50 | cpu_buf->sample_lost_overflow++; |
| 51 | } |
| 52 | |
Robert Richter | 3001577 | 2008-12-23 01:35:12 +0100 | [diff] [blame] | 53 | void free_cpu_buffers(void) |
| 54 | { |
Andi Kleen | cb6e943 | 2010-04-01 03:17:25 +0200 | [diff] [blame] | 55 | if (op_ring_buffer) |
| 56 | ring_buffer_free(op_ring_buffer); |
| 57 | op_ring_buffer = NULL; |
Robert Richter | 3001577 | 2008-12-23 01:35:12 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Robert Richter | 54f2c84 | 2009-05-07 17:28:59 +0200 | [diff] [blame] | 60 | #define RB_EVENT_HDR_SIZE 4 |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | int alloc_cpu_buffers(void) |
| 63 | { |
| 64 | int i; |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 65 | |
Robert Richter | bd2172f | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 66 | unsigned long buffer_size = oprofile_cpu_buffer_size; |
Robert Richter | 54f2c84 | 2009-05-07 17:28:59 +0200 | [diff] [blame] | 67 | unsigned long byte_size = buffer_size * (sizeof(struct op_sample) + |
| 68 | RB_EVENT_HDR_SIZE); |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 69 | |
Andi Kleen | cb6e943 | 2010-04-01 03:17:25 +0200 | [diff] [blame] | 70 | op_ring_buffer = ring_buffer_alloc(byte_size, OP_BUFFER_FLAGS); |
| 71 | if (!op_ring_buffer) |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 72 | goto fail; |
| 73 | |
Chris J Arges | 4bd9b9d | 2008-10-15 11:03:39 -0500 | [diff] [blame] | 74 | for_each_possible_cpu(i) { |
Tejun Heo | b3e9f67 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 75 | struct oprofile_cpu_buffer *b = &per_cpu(op_cpu_buffer, i); |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | b->last_task = NULL; |
| 78 | b->last_is_kernel = -1; |
| 79 | b->tracing = 0; |
| 80 | b->buffer_size = buffer_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | b->sample_received = 0; |
| 82 | b->sample_lost_overflow = 0; |
Philippe Elie | df9d177 | 2007-11-14 16:58:48 -0800 | [diff] [blame] | 83 | b->backtrace_aborted = 0; |
| 84 | b->sample_invalid_eip = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | b->cpu = i; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 86 | INIT_DELAYED_WORK(&b->work, wq_sync_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | return 0; |
| 89 | |
| 90 | fail: |
| 91 | free_cpu_buffers(); |
| 92 | return -ENOMEM; |
| 93 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
| 95 | void start_cpu_work(void) |
| 96 | { |
| 97 | int i; |
| 98 | |
| 99 | work_enabled = 1; |
| 100 | |
| 101 | for_each_online_cpu(i) { |
Tejun Heo | b3e9f67 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 102 | struct oprofile_cpu_buffer *b = &per_cpu(op_cpu_buffer, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
| 104 | /* |
| 105 | * Spread the work by 1 jiffy per cpu so they dont all |
| 106 | * fire at once. |
| 107 | */ |
| 108 | schedule_delayed_work_on(i, &b->work, DEFAULT_TIMER_EXPIRE + i); |
| 109 | } |
| 110 | } |
| 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | void end_cpu_work(void) |
| 113 | { |
| 114 | int i; |
| 115 | |
| 116 | work_enabled = 0; |
| 117 | |
| 118 | for_each_online_cpu(i) { |
Tejun Heo | b3e9f67 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 119 | struct oprofile_cpu_buffer *b = &per_cpu(op_cpu_buffer, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
| 121 | cancel_delayed_work(&b->work); |
| 122 | } |
| 123 | |
| 124 | flush_scheduled_work(); |
| 125 | } |
| 126 | |
Robert Richter | 2cc28b9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 127 | /* |
| 128 | * This function prepares the cpu buffer to write a sample. |
| 129 | * |
| 130 | * Struct op_entry is used during operations on the ring buffer while |
| 131 | * struct op_sample contains the data that is stored in the ring |
| 132 | * buffer. Struct entry can be uninitialized. The function reserves a |
| 133 | * data array that is specified by size. Use |
| 134 | * op_cpu_buffer_write_commit() after preparing the sample. In case of |
| 135 | * errors a null pointer is returned, otherwise the pointer to the |
| 136 | * sample. |
| 137 | * |
| 138 | */ |
| 139 | struct op_sample |
| 140 | *op_cpu_buffer_write_reserve(struct op_entry *entry, unsigned long size) |
Robert Richter | 9966718 | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 141 | { |
Robert Richter | 2cc28b9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 142 | entry->event = ring_buffer_lock_reserve |
Andi Kleen | cb6e943 | 2010-04-01 03:17:25 +0200 | [diff] [blame] | 143 | (op_ring_buffer, sizeof(struct op_sample) + |
Ingo Molnar | 304cc6a | 2009-02-06 01:12:02 +0100 | [diff] [blame] | 144 | size * sizeof(entry->sample->data[0])); |
Andi Kleen | cb6e943 | 2010-04-01 03:17:25 +0200 | [diff] [blame] | 145 | if (!entry->event) |
Robert Richter | 2cc28b9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 146 | return NULL; |
Andi Kleen | cb6e943 | 2010-04-01 03:17:25 +0200 | [diff] [blame] | 147 | entry->sample = ring_buffer_event_data(entry->event); |
Robert Richter | 2cc28b9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 148 | entry->size = size; |
| 149 | entry->data = entry->sample->data; |
| 150 | |
| 151 | return entry->sample; |
Robert Richter | 9966718 | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | int op_cpu_buffer_write_commit(struct op_entry *entry) |
| 155 | { |
Andi Kleen | cb6e943 | 2010-04-01 03:17:25 +0200 | [diff] [blame] | 156 | return ring_buffer_unlock_commit(op_ring_buffer, entry->event); |
Robert Richter | 9966718 | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 157 | } |
| 158 | |
Robert Richter | 2d87b14 | 2008-12-30 04:10:46 +0100 | [diff] [blame] | 159 | struct op_sample *op_cpu_buffer_read_entry(struct op_entry *entry, int cpu) |
Robert Richter | 9966718 | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 160 | { |
| 161 | struct ring_buffer_event *e; |
Robert Richter | b971f06 | 2010-04-23 16:47:51 +0200 | [diff] [blame] | 162 | e = ring_buffer_consume(op_ring_buffer, cpu, NULL, NULL); |
Andi Kleen | cb6e943 | 2010-04-01 03:17:25 +0200 | [diff] [blame] | 163 | if (!e) |
Robert Richter | 9966718 | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 164 | return NULL; |
Robert Richter | 2d87b14 | 2008-12-30 04:10:46 +0100 | [diff] [blame] | 165 | |
Robert Richter | 2d87b14 | 2008-12-30 04:10:46 +0100 | [diff] [blame] | 166 | entry->event = e; |
| 167 | entry->sample = ring_buffer_event_data(e); |
| 168 | entry->size = (ring_buffer_event_length(e) - sizeof(struct op_sample)) |
| 169 | / sizeof(entry->sample->data[0]); |
| 170 | entry->data = entry->sample->data; |
| 171 | return entry->sample; |
Robert Richter | 9966718 | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | unsigned long op_cpu_buffer_entries(int cpu) |
| 175 | { |
Andi Kleen | cb6e943 | 2010-04-01 03:17:25 +0200 | [diff] [blame] | 176 | return ring_buffer_entries_cpu(op_ring_buffer, cpu); |
Robert Richter | 9966718 | 2008-12-16 16:19:54 +0100 | [diff] [blame] | 177 | } |
| 178 | |
Robert Richter | ae735e9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 179 | static int |
| 180 | op_add_code(struct oprofile_cpu_buffer *cpu_buf, unsigned long backtrace, |
| 181 | int is_kernel, struct task_struct *task) |
| 182 | { |
| 183 | struct op_entry entry; |
| 184 | struct op_sample *sample; |
| 185 | unsigned long flags; |
| 186 | int size; |
| 187 | |
| 188 | flags = 0; |
| 189 | |
| 190 | if (backtrace) |
| 191 | flags |= TRACE_BEGIN; |
| 192 | |
| 193 | /* notice a switch from user->kernel or vice versa */ |
| 194 | is_kernel = !!is_kernel; |
| 195 | if (cpu_buf->last_is_kernel != is_kernel) { |
| 196 | cpu_buf->last_is_kernel = is_kernel; |
| 197 | flags |= KERNEL_CTX_SWITCH; |
| 198 | if (is_kernel) |
| 199 | flags |= IS_KERNEL; |
| 200 | } |
| 201 | |
| 202 | /* notice a task switch */ |
| 203 | if (cpu_buf->last_task != task) { |
| 204 | cpu_buf->last_task = task; |
| 205 | flags |= USER_CTX_SWITCH; |
| 206 | } |
| 207 | |
| 208 | if (!flags) |
| 209 | /* nothing to do */ |
| 210 | return 0; |
| 211 | |
| 212 | if (flags & USER_CTX_SWITCH) |
| 213 | size = 1; |
| 214 | else |
| 215 | size = 0; |
| 216 | |
| 217 | sample = op_cpu_buffer_write_reserve(&entry, size); |
| 218 | if (!sample) |
| 219 | return -ENOMEM; |
| 220 | |
| 221 | sample->eip = ESCAPE_CODE; |
| 222 | sample->event = flags; |
| 223 | |
| 224 | if (size) |
Robert Richter | d9928c2 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 225 | op_cpu_buffer_add_data(&entry, (unsigned long)task); |
Robert Richter | ae735e9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 226 | |
| 227 | op_cpu_buffer_write_commit(&entry); |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 232 | static inline int |
Robert Richter | d0e2338 | 2008-12-23 04:03:05 +0100 | [diff] [blame] | 233 | op_add_sample(struct oprofile_cpu_buffer *cpu_buf, |
| 234 | unsigned long pc, unsigned long event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | { |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 236 | struct op_entry entry; |
Robert Richter | 2cc28b9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 237 | struct op_sample *sample; |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 238 | |
Robert Richter | 2cc28b9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 239 | sample = op_cpu_buffer_write_reserve(&entry, 0); |
| 240 | if (!sample) |
| 241 | return -ENOMEM; |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 242 | |
Robert Richter | 2cc28b9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 243 | sample->eip = pc; |
| 244 | sample->event = event; |
Robert Richter | 6dad828 | 2008-12-09 01:21:32 +0100 | [diff] [blame] | 245 | |
Robert Richter | 3967e93 | 2008-12-30 05:10:58 +0100 | [diff] [blame] | 246 | return op_cpu_buffer_write_commit(&entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Robert Richter | ae735e9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 249 | /* |
| 250 | * This must be safe from any context. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | * |
| 252 | * is_kernel is needed because on some architectures you cannot |
| 253 | * tell if you are in kernel or user space simply by looking at |
| 254 | * pc. We tag this in the buffer by generating kernel enter/exit |
| 255 | * events whenever is_kernel changes |
| 256 | */ |
Robert Richter | ae735e9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 257 | static int |
| 258 | log_sample(struct oprofile_cpu_buffer *cpu_buf, unsigned long pc, |
| 259 | unsigned long backtrace, int is_kernel, unsigned long event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | cpu_buf->sample_received++; |
| 262 | |
Philippe Elie | df9d177 | 2007-11-14 16:58:48 -0800 | [diff] [blame] | 263 | if (pc == ESCAPE_CODE) { |
| 264 | cpu_buf->sample_invalid_eip++; |
| 265 | return 0; |
| 266 | } |
| 267 | |
Robert Richter | ae735e9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 268 | if (op_add_code(cpu_buf, backtrace, is_kernel, current)) |
| 269 | goto fail; |
Robert Richter | 6a18037 | 2008-10-16 15:01:40 +0200 | [diff] [blame] | 270 | |
Robert Richter | d0e2338 | 2008-12-23 04:03:05 +0100 | [diff] [blame] | 271 | if (op_add_sample(cpu_buf, pc, event)) |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 272 | goto fail; |
| 273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | return 1; |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 275 | |
| 276 | fail: |
| 277 | cpu_buf->sample_lost_overflow++; |
| 278 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Robert Richter | 6352d92 | 2008-12-18 22:09:13 +0100 | [diff] [blame] | 281 | static inline void oprofile_begin_trace(struct oprofile_cpu_buffer *cpu_buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | cpu_buf->tracing = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Robert Richter | 6352d92 | 2008-12-18 22:09:13 +0100 | [diff] [blame] | 286 | static inline void oprofile_end_trace(struct oprofile_cpu_buffer *cpu_buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | { |
| 288 | cpu_buf->tracing = 0; |
| 289 | } |
| 290 | |
Robert Richter | d45d23b | 2008-12-16 12:00:10 +0100 | [diff] [blame] | 291 | static inline void |
| 292 | __oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs, |
| 293 | unsigned long event, int is_kernel) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | { |
Tejun Heo | b3e9f67 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 295 | struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(op_cpu_buffer); |
Robert Richter | ae735e9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 296 | unsigned long backtrace = oprofile_backtrace_depth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
Robert Richter | fd13f6c | 2008-10-19 21:00:09 +0200 | [diff] [blame] | 298 | /* |
| 299 | * if log_sample() fail we can't backtrace since we lost the |
| 300 | * source of this event |
| 301 | */ |
Robert Richter | ae735e9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 302 | if (!log_sample(cpu_buf, pc, backtrace, is_kernel, event)) |
| 303 | /* failed */ |
| 304 | return; |
Robert Richter | 6352d92 | 2008-12-18 22:09:13 +0100 | [diff] [blame] | 305 | |
Robert Richter | ae735e9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 306 | if (!backtrace) |
| 307 | return; |
| 308 | |
| 309 | oprofile_begin_trace(cpu_buf); |
| 310 | oprofile_ops.backtrace(regs, backtrace); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | oprofile_end_trace(cpu_buf); |
| 312 | } |
| 313 | |
Robert Richter | d45d23b | 2008-12-16 12:00:10 +0100 | [diff] [blame] | 314 | void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs, |
| 315 | unsigned long event, int is_kernel) |
| 316 | { |
| 317 | __oprofile_add_ext_sample(pc, regs, event, is_kernel); |
| 318 | } |
| 319 | |
Brian Rogan | 2735771 | 2006-03-28 01:56:20 -0800 | [diff] [blame] | 320 | void oprofile_add_sample(struct pt_regs * const regs, unsigned long event) |
| 321 | { |
Phil Carmody | 9414e99 | 2010-04-28 12:09:16 -0500 | [diff] [blame] | 322 | int is_kernel; |
| 323 | unsigned long pc; |
| 324 | |
| 325 | if (likely(regs)) { |
| 326 | is_kernel = !user_mode(regs); |
| 327 | pc = profile_pc(regs); |
| 328 | } else { |
| 329 | is_kernel = 0; /* This value will not be used */ |
| 330 | pc = ESCAPE_CODE; /* as this causes an early return. */ |
| 331 | } |
Brian Rogan | 2735771 | 2006-03-28 01:56:20 -0800 | [diff] [blame] | 332 | |
Robert Richter | d45d23b | 2008-12-16 12:00:10 +0100 | [diff] [blame] | 333 | __oprofile_add_ext_sample(pc, regs, event, is_kernel); |
Brian Rogan | 2735771 | 2006-03-28 01:56:20 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Robert Richter | 1acda87 | 2009-01-05 10:35:31 +0100 | [diff] [blame] | 336 | /* |
| 337 | * Add samples with data to the ring buffer. |
| 338 | * |
Robert Richter | 14f0ca8 | 2009-01-07 21:50:22 +0100 | [diff] [blame] | 339 | * Use oprofile_add_data(&entry, val) to add data and |
| 340 | * oprofile_write_commit(&entry) to commit the sample. |
Robert Richter | 1acda87 | 2009-01-05 10:35:31 +0100 | [diff] [blame] | 341 | */ |
Robert Richter | 14f0ca8 | 2009-01-07 21:50:22 +0100 | [diff] [blame] | 342 | void |
| 343 | oprofile_write_reserve(struct op_entry *entry, struct pt_regs * const regs, |
Robert Richter | 1acda87 | 2009-01-05 10:35:31 +0100 | [diff] [blame] | 344 | unsigned long pc, int code, int size) |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 345 | { |
Robert Richter | 1acda87 | 2009-01-05 10:35:31 +0100 | [diff] [blame] | 346 | struct op_sample *sample; |
Robert Richter | e2fee27 | 2008-07-18 17:36:20 +0200 | [diff] [blame] | 347 | int is_kernel = !user_mode(regs); |
Tejun Heo | b3e9f67 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 348 | struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(op_cpu_buffer); |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 349 | |
| 350 | cpu_buf->sample_received++; |
| 351 | |
Robert Richter | 1acda87 | 2009-01-05 10:35:31 +0100 | [diff] [blame] | 352 | /* no backtraces for samples with data */ |
| 353 | if (op_add_code(cpu_buf, 0, is_kernel, current)) |
| 354 | goto fail; |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 355 | |
Robert Richter | 1acda87 | 2009-01-05 10:35:31 +0100 | [diff] [blame] | 356 | sample = op_cpu_buffer_write_reserve(entry, size + 2); |
| 357 | if (!sample) |
| 358 | goto fail; |
| 359 | sample->eip = ESCAPE_CODE; |
| 360 | sample->event = 0; /* no flags */ |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 361 | |
Robert Richter | 1acda87 | 2009-01-05 10:35:31 +0100 | [diff] [blame] | 362 | op_cpu_buffer_add_data(entry, code); |
| 363 | op_cpu_buffer_add_data(entry, pc); |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 364 | |
Robert Richter | 1acda87 | 2009-01-05 10:35:31 +0100 | [diff] [blame] | 365 | return; |
| 366 | |
| 367 | fail: |
Robert Richter | fdb6a8f | 2009-01-17 17:13:27 +0100 | [diff] [blame] | 368 | entry->event = NULL; |
Robert Richter | 1acda87 | 2009-01-05 10:35:31 +0100 | [diff] [blame] | 369 | cpu_buf->sample_lost_overflow++; |
Barry Kasindorf | 345c257 | 2008-07-22 21:08:54 +0200 | [diff] [blame] | 370 | } |
| 371 | |
Robert Richter | 14f0ca8 | 2009-01-07 21:50:22 +0100 | [diff] [blame] | 372 | int oprofile_add_data(struct op_entry *entry, unsigned long val) |
| 373 | { |
Robert Richter | fdb6a8f | 2009-01-17 17:13:27 +0100 | [diff] [blame] | 374 | if (!entry->event) |
| 375 | return 0; |
Robert Richter | 14f0ca8 | 2009-01-07 21:50:22 +0100 | [diff] [blame] | 376 | return op_cpu_buffer_add_data(entry, val); |
| 377 | } |
| 378 | |
Robert Richter | 51563a0 | 2009-06-03 20:54:56 +0200 | [diff] [blame] | 379 | int oprofile_add_data64(struct op_entry *entry, u64 val) |
| 380 | { |
| 381 | if (!entry->event) |
| 382 | return 0; |
| 383 | if (op_cpu_buffer_get_size(entry) < 2) |
| 384 | /* |
| 385 | * the function returns 0 to indicate a too small |
| 386 | * buffer, even if there is some space left |
| 387 | */ |
| 388 | return 0; |
| 389 | if (!op_cpu_buffer_add_data(entry, (u32)val)) |
| 390 | return 0; |
| 391 | return op_cpu_buffer_add_data(entry, (u32)(val >> 32)); |
| 392 | } |
| 393 | |
Robert Richter | 14f0ca8 | 2009-01-07 21:50:22 +0100 | [diff] [blame] | 394 | int oprofile_write_commit(struct op_entry *entry) |
| 395 | { |
Robert Richter | fdb6a8f | 2009-01-17 17:13:27 +0100 | [diff] [blame] | 396 | if (!entry->event) |
| 397 | return -EINVAL; |
Robert Richter | 14f0ca8 | 2009-01-07 21:50:22 +0100 | [diff] [blame] | 398 | return op_cpu_buffer_write_commit(entry); |
| 399 | } |
| 400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event) |
| 402 | { |
Tejun Heo | b3e9f67 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 403 | struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(op_cpu_buffer); |
Robert Richter | ae735e9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 404 | log_sample(cpu_buf, pc, 0, is_kernel, event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | void oprofile_add_trace(unsigned long pc) |
| 408 | { |
Tejun Heo | b3e9f67 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 409 | struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(op_cpu_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
| 411 | if (!cpu_buf->tracing) |
| 412 | return; |
| 413 | |
Robert Richter | fd13f6c | 2008-10-19 21:00:09 +0200 | [diff] [blame] | 414 | /* |
| 415 | * broken frame can give an eip with the same value as an |
| 416 | * escape code, abort the trace if we get it |
| 417 | */ |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 418 | if (pc == ESCAPE_CODE) |
| 419 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
Robert Richter | d0e2338 | 2008-12-23 04:03:05 +0100 | [diff] [blame] | 421 | if (op_add_sample(cpu_buf, pc, 0)) |
Robert Richter | 211117f | 2008-12-09 02:13:25 +0100 | [diff] [blame] | 422 | goto fail; |
| 423 | |
| 424 | return; |
| 425 | fail: |
| 426 | cpu_buf->tracing = 0; |
| 427 | cpu_buf->backtrace_aborted++; |
| 428 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | /* |
| 432 | * This serves to avoid cpu buffer overflow, and makes sure |
| 433 | * the task mortuary progresses |
| 434 | * |
| 435 | * By using schedule_delayed_work_on and then schedule_delayed_work |
| 436 | * we guarantee this will stay on the correct cpu |
| 437 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 438 | static void wq_sync_buffer(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | { |
Robert Richter | 25ad2913 | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 440 | struct oprofile_cpu_buffer *b = |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 441 | container_of(work, struct oprofile_cpu_buffer, work.work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | if (b->cpu != smp_processor_id()) { |
Robert Richter | bd17b62 | 2008-07-22 21:09:07 +0200 | [diff] [blame] | 443 | printk(KERN_DEBUG "WQ on CPU%d, prefer CPU%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | smp_processor_id(), b->cpu); |
Chris J Arges | 4bd9b9d | 2008-10-15 11:03:39 -0500 | [diff] [blame] | 445 | |
| 446 | if (!cpu_online(b->cpu)) { |
| 447 | cancel_delayed_work(&b->work); |
| 448 | return; |
| 449 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | } |
| 451 | sync_buffer(b->cpu); |
| 452 | |
| 453 | /* don't re-add the work if we're shutting down */ |
| 454 | if (work_enabled) |
| 455 | schedule_delayed_work(&b->work, DEFAULT_TIMER_EXPIRE); |
| 456 | } |