OMAP3 SDRC: set FIXEDDELAY when disabling SDRC DLL
[linux-ginger.git] / drivers / oprofile / cpu_buffer.c
blobf0e99d4c066b9acfeada76a623258d5fda744b5d
1 /**
2 * @file cpu_buffer.c
4 * @remark Copyright 2002-2009 OProfile authors
5 * @remark Read the file COPYING
7 * @author John Levon <levon@movementarian.org>
8 * @author Barry Kasindorf <barry.kasindorf@amd.com>
9 * @author Robert Richter <robert.richter@amd.com>
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().
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.
22 #include <linux/sched.h>
23 #include <linux/oprofile.h>
24 #include <linux/vmalloc.h>
25 #include <linux/errno.h>
27 #include "event_buffer.h"
28 #include "cpu_buffer.h"
29 #include "buffer_sync.h"
30 #include "oprof.h"
32 #define OP_BUFFER_FLAGS 0
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.
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.
49 static struct ring_buffer *op_ring_buffer_read;
50 static struct ring_buffer *op_ring_buffer_write;
51 DEFINE_PER_CPU(struct oprofile_cpu_buffer, cpu_buffer);
53 static void wq_sync_buffer(struct work_struct *work);
55 #define DEFAULT_TIMER_EXPIRE (HZ / 10)
56 static int work_enabled;
58 unsigned long oprofile_get_cpu_buffer_size(void)
60 return oprofile_cpu_buffer_size;
63 void oprofile_cpu_buffer_inc_smpl_lost(void)
65 struct oprofile_cpu_buffer *cpu_buf
66 = &__get_cpu_var(cpu_buffer);
68 cpu_buf->sample_lost_overflow++;
71 void free_cpu_buffers(void)
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;
81 int alloc_cpu_buffers(void)
83 int i;
85 unsigned long buffer_size = oprofile_cpu_buffer_size;
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;
94 for_each_possible_cpu(i) {
95 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
97 b->last_task = NULL;
98 b->last_is_kernel = -1;
99 b->tracing = 0;
100 b->buffer_size = buffer_size;
101 b->sample_received = 0;
102 b->sample_lost_overflow = 0;
103 b->backtrace_aborted = 0;
104 b->sample_invalid_eip = 0;
105 b->cpu = i;
106 INIT_DELAYED_WORK(&b->work, wq_sync_buffer);
108 return 0;
110 fail:
111 free_cpu_buffers();
112 return -ENOMEM;
115 void start_cpu_work(void)
117 int i;
119 work_enabled = 1;
121 for_each_online_cpu(i) {
122 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
125 * Spread the work by 1 jiffy per cpu so they dont all
126 * fire at once.
128 schedule_delayed_work_on(i, &b->work, DEFAULT_TIMER_EXPIRE + i);
132 void end_cpu_work(void)
134 int i;
136 work_enabled = 0;
138 for_each_online_cpu(i) {
139 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
141 cancel_delayed_work(&b->work);
144 flush_scheduled_work();
148 * This function prepares the cpu buffer to write a sample.
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.
159 struct op_sample
160 *op_cpu_buffer_write_reserve(struct op_entry *entry, unsigned long size)
162 entry->event = ring_buffer_lock_reserve
163 (op_ring_buffer_write, sizeof(struct op_sample) +
164 size * sizeof(entry->sample->data[0]));
165 if (entry->event)
166 entry->sample = ring_buffer_event_data(entry->event);
167 else
168 entry->sample = NULL;
170 if (!entry->sample)
171 return NULL;
173 entry->size = size;
174 entry->data = entry->sample->data;
176 return entry->sample;
179 int op_cpu_buffer_write_commit(struct op_entry *entry)
181 return ring_buffer_unlock_commit(op_ring_buffer_write, entry->event);
184 struct op_sample *op_cpu_buffer_read_entry(struct op_entry *entry, int cpu)
186 struct ring_buffer_event *e;
187 e = ring_buffer_consume(op_ring_buffer_read, cpu, NULL);
188 if (e)
189 goto event;
190 if (ring_buffer_swap_cpu(op_ring_buffer_read,
191 op_ring_buffer_write,
192 cpu))
193 return NULL;
194 e = ring_buffer_consume(op_ring_buffer_read, cpu, NULL);
195 if (e)
196 goto event;
197 return NULL;
199 event:
200 entry->event = e;
201 entry->sample = ring_buffer_event_data(e);
202 entry->size = (ring_buffer_event_length(e) - sizeof(struct op_sample))
203 / sizeof(entry->sample->data[0]);
204 entry->data = entry->sample->data;
205 return entry->sample;
208 unsigned long op_cpu_buffer_entries(int cpu)
210 return ring_buffer_entries_cpu(op_ring_buffer_read, cpu)
211 + ring_buffer_entries_cpu(op_ring_buffer_write, cpu);
214 static int
215 op_add_code(struct oprofile_cpu_buffer *cpu_buf, unsigned long backtrace,
216 int is_kernel, struct task_struct *task)
218 struct op_entry entry;
219 struct op_sample *sample;
220 unsigned long flags;
221 int size;
223 flags = 0;
225 if (backtrace)
226 flags |= TRACE_BEGIN;
228 /* notice a switch from user->kernel or vice versa */
229 is_kernel = !!is_kernel;
230 if (cpu_buf->last_is_kernel != is_kernel) {
231 cpu_buf->last_is_kernel = is_kernel;
232 flags |= KERNEL_CTX_SWITCH;
233 if (is_kernel)
234 flags |= IS_KERNEL;
237 /* notice a task switch */
238 if (cpu_buf->last_task != task) {
239 cpu_buf->last_task = task;
240 flags |= USER_CTX_SWITCH;
243 if (!flags)
244 /* nothing to do */
245 return 0;
247 if (flags & USER_CTX_SWITCH)
248 size = 1;
249 else
250 size = 0;
252 sample = op_cpu_buffer_write_reserve(&entry, size);
253 if (!sample)
254 return -ENOMEM;
256 sample->eip = ESCAPE_CODE;
257 sample->event = flags;
259 if (size)
260 op_cpu_buffer_add_data(&entry, (unsigned long)task);
262 op_cpu_buffer_write_commit(&entry);
264 return 0;
267 static inline int
268 op_add_sample(struct oprofile_cpu_buffer *cpu_buf,
269 unsigned long pc, unsigned long event)
271 struct op_entry entry;
272 struct op_sample *sample;
274 sample = op_cpu_buffer_write_reserve(&entry, 0);
275 if (!sample)
276 return -ENOMEM;
278 sample->eip = pc;
279 sample->event = event;
281 return op_cpu_buffer_write_commit(&entry);
285 * This must be safe from any context.
287 * is_kernel is needed because on some architectures you cannot
288 * tell if you are in kernel or user space simply by looking at
289 * pc. We tag this in the buffer by generating kernel enter/exit
290 * events whenever is_kernel changes
292 static int
293 log_sample(struct oprofile_cpu_buffer *cpu_buf, unsigned long pc,
294 unsigned long backtrace, int is_kernel, unsigned long event)
296 cpu_buf->sample_received++;
298 if (pc == ESCAPE_CODE) {
299 cpu_buf->sample_invalid_eip++;
300 return 0;
303 if (op_add_code(cpu_buf, backtrace, is_kernel, current))
304 goto fail;
306 if (op_add_sample(cpu_buf, pc, event))
307 goto fail;
309 return 1;
311 fail:
312 cpu_buf->sample_lost_overflow++;
313 return 0;
316 static inline void oprofile_begin_trace(struct oprofile_cpu_buffer *cpu_buf)
318 cpu_buf->tracing = 1;
321 static inline void oprofile_end_trace(struct oprofile_cpu_buffer *cpu_buf)
323 cpu_buf->tracing = 0;
326 static inline void
327 __oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
328 unsigned long event, int is_kernel)
330 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
331 unsigned long backtrace = oprofile_backtrace_depth;
334 * if log_sample() fail we can't backtrace since we lost the
335 * source of this event
337 if (!log_sample(cpu_buf, pc, backtrace, is_kernel, event))
338 /* failed */
339 return;
341 if (!backtrace)
342 return;
344 oprofile_begin_trace(cpu_buf);
345 oprofile_ops.backtrace(regs, backtrace);
346 oprofile_end_trace(cpu_buf);
349 void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
350 unsigned long event, int is_kernel)
352 __oprofile_add_ext_sample(pc, regs, event, is_kernel);
355 void oprofile_add_sample(struct pt_regs * const regs, unsigned long event)
357 int is_kernel = !user_mode(regs);
358 unsigned long pc = profile_pc(regs);
360 __oprofile_add_ext_sample(pc, regs, event, is_kernel);
364 * Add samples with data to the ring buffer.
366 * Use oprofile_add_data(&entry, val) to add data and
367 * oprofile_write_commit(&entry) to commit the sample.
369 void
370 oprofile_write_reserve(struct op_entry *entry, struct pt_regs * const regs,
371 unsigned long pc, int code, int size)
373 struct op_sample *sample;
374 int is_kernel = !user_mode(regs);
375 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
377 cpu_buf->sample_received++;
379 /* no backtraces for samples with data */
380 if (op_add_code(cpu_buf, 0, is_kernel, current))
381 goto fail;
383 sample = op_cpu_buffer_write_reserve(entry, size + 2);
384 if (!sample)
385 goto fail;
386 sample->eip = ESCAPE_CODE;
387 sample->event = 0; /* no flags */
389 op_cpu_buffer_add_data(entry, code);
390 op_cpu_buffer_add_data(entry, pc);
392 return;
394 fail:
395 entry->event = NULL;
396 cpu_buf->sample_lost_overflow++;
399 int oprofile_add_data(struct op_entry *entry, unsigned long val)
401 if (!entry->event)
402 return 0;
403 return op_cpu_buffer_add_data(entry, val);
406 int oprofile_write_commit(struct op_entry *entry)
408 if (!entry->event)
409 return -EINVAL;
410 return op_cpu_buffer_write_commit(entry);
413 void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event)
415 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
416 log_sample(cpu_buf, pc, 0, is_kernel, event);
419 void oprofile_add_trace(unsigned long pc)
421 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
423 if (!cpu_buf->tracing)
424 return;
427 * broken frame can give an eip with the same value as an
428 * escape code, abort the trace if we get it
430 if (pc == ESCAPE_CODE)
431 goto fail;
433 if (op_add_sample(cpu_buf, pc, 0))
434 goto fail;
436 return;
437 fail:
438 cpu_buf->tracing = 0;
439 cpu_buf->backtrace_aborted++;
440 return;
444 * This serves to avoid cpu buffer overflow, and makes sure
445 * the task mortuary progresses
447 * By using schedule_delayed_work_on and then schedule_delayed_work
448 * we guarantee this will stay on the correct cpu
450 static void wq_sync_buffer(struct work_struct *work)
452 struct oprofile_cpu_buffer *b =
453 container_of(work, struct oprofile_cpu_buffer, work.work);
454 if (b->cpu != smp_processor_id()) {
455 printk(KERN_DEBUG "WQ on CPU%d, prefer CPU%d\n",
456 smp_processor_id(), b->cpu);
458 if (!cpu_online(b->cpu)) {
459 cancel_delayed_work(&b->work);
460 return;
463 sync_buffer(b->cpu);
465 /* don't re-add the work if we're shutting down */
466 if (work_enabled)
467 schedule_delayed_work(&b->work, DEFAULT_TIMER_EXPIRE);