Merge commit 'v2.6.27-rc5' into tip/oprofile
[linux-2.6/mini2440.git] / drivers / oprofile / cpu_buffer.c
blobe1bd5a937f6c77d33ac1f92a16966e556c8bfff8
1 /**
2 * @file cpu_buffer.c
4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
7 * @author John Levon <levon@movementarian.org>
8 * @author Barry Kasindorf <barry.kasindorf@amd.com>
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().
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.
21 #include <linux/sched.h>
22 #include <linux/oprofile.h>
23 #include <linux/vmalloc.h>
24 #include <linux/errno.h>
26 #include "event_buffer.h"
27 #include "cpu_buffer.h"
28 #include "buffer_sync.h"
29 #include "oprof.h"
31 DEFINE_PER_CPU(struct oprofile_cpu_buffer, cpu_buffer);
33 static void wq_sync_buffer(struct work_struct *work);
35 #define DEFAULT_TIMER_EXPIRE (HZ / 10)
36 static int work_enabled;
38 void free_cpu_buffers(void)
40 int i;
42 for_each_online_cpu(i) {
43 vfree(per_cpu(cpu_buffer, i).buffer);
44 per_cpu(cpu_buffer, i).buffer = NULL;
48 int alloc_cpu_buffers(void)
50 int i;
52 unsigned long buffer_size = fs_cpu_buffer_size;
54 for_each_online_cpu(i) {
55 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
57 b->buffer = vmalloc_node(sizeof(struct op_sample) * buffer_size,
58 cpu_to_node(i));
59 if (!b->buffer)
60 goto fail;
62 b->last_task = NULL;
63 b->last_is_kernel = -1;
64 b->tracing = 0;
65 b->buffer_size = buffer_size;
66 b->tail_pos = 0;
67 b->head_pos = 0;
68 b->sample_received = 0;
69 b->sample_lost_overflow = 0;
70 b->backtrace_aborted = 0;
71 b->sample_invalid_eip = 0;
72 b->cpu = i;
73 INIT_DELAYED_WORK(&b->work, wq_sync_buffer);
75 return 0;
77 fail:
78 free_cpu_buffers();
79 return -ENOMEM;
82 void start_cpu_work(void)
84 int i;
86 work_enabled = 1;
88 for_each_online_cpu(i) {
89 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
92 * Spread the work by 1 jiffy per cpu so they dont all
93 * fire at once.
95 schedule_delayed_work_on(i, &b->work, DEFAULT_TIMER_EXPIRE + i);
99 void end_cpu_work(void)
101 int i;
103 work_enabled = 0;
105 for_each_online_cpu(i) {
106 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
108 cancel_delayed_work(&b->work);
111 flush_scheduled_work();
114 /* Resets the cpu buffer to a sane state. */
115 void cpu_buffer_reset(struct oprofile_cpu_buffer * cpu_buf)
117 /* reset these to invalid values; the next sample
118 * collected will populate the buffer with proper
119 * values to initialize the buffer
121 cpu_buf->last_is_kernel = -1;
122 cpu_buf->last_task = NULL;
125 /* compute number of available slots in cpu_buffer queue */
126 static unsigned long nr_available_slots(struct oprofile_cpu_buffer const * b)
128 unsigned long head = b->head_pos;
129 unsigned long tail = b->tail_pos;
131 if (tail > head)
132 return (tail - head) - 1;
134 return tail + (b->buffer_size - head) - 1;
137 static void increment_head(struct oprofile_cpu_buffer * b)
139 unsigned long new_head = b->head_pos + 1;
141 /* Ensure anything written to the slot before we
142 * increment is visible */
143 wmb();
145 if (new_head < b->buffer_size)
146 b->head_pos = new_head;
147 else
148 b->head_pos = 0;
151 static inline void
152 add_sample(struct oprofile_cpu_buffer * cpu_buf,
153 unsigned long pc, unsigned long event)
155 struct op_sample * entry = &cpu_buf->buffer[cpu_buf->head_pos];
156 entry->eip = pc;
157 entry->event = event;
158 increment_head(cpu_buf);
161 static inline void
162 add_code(struct oprofile_cpu_buffer * buffer, unsigned long value)
164 add_sample(buffer, ESCAPE_CODE, value);
167 /* This must be safe from any context. It's safe writing here
168 * because of the head/tail separation of the writer and reader
169 * of the CPU buffer.
171 * is_kernel is needed because on some architectures you cannot
172 * tell if you are in kernel or user space simply by looking at
173 * pc. We tag this in the buffer by generating kernel enter/exit
174 * events whenever is_kernel changes
176 static int log_sample(struct oprofile_cpu_buffer * cpu_buf, unsigned long pc,
177 int is_kernel, unsigned long event)
179 struct task_struct * task;
181 cpu_buf->sample_received++;
183 if (pc == ESCAPE_CODE) {
184 cpu_buf->sample_invalid_eip++;
185 return 0;
188 if (nr_available_slots(cpu_buf) < 3) {
189 cpu_buf->sample_lost_overflow++;
190 return 0;
193 is_kernel = !!is_kernel;
195 task = current;
197 /* notice a switch from user->kernel or vice versa */
198 if (cpu_buf->last_is_kernel != is_kernel) {
199 cpu_buf->last_is_kernel = is_kernel;
200 add_code(cpu_buf, is_kernel);
203 /* notice a task switch */
204 if (cpu_buf->last_task != task) {
205 cpu_buf->last_task = task;
206 add_code(cpu_buf, (unsigned long)task);
209 add_sample(cpu_buf, pc, event);
210 return 1;
213 static int oprofile_begin_trace(struct oprofile_cpu_buffer *cpu_buf)
215 if (nr_available_slots(cpu_buf) < 4) {
216 cpu_buf->sample_lost_overflow++;
217 return 0;
220 add_code(cpu_buf, CPU_TRACE_BEGIN);
221 cpu_buf->tracing = 1;
222 return 1;
225 static void oprofile_end_trace(struct oprofile_cpu_buffer * cpu_buf)
227 cpu_buf->tracing = 0;
230 void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
231 unsigned long event, int is_kernel)
233 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
235 if (!backtrace_depth) {
236 log_sample(cpu_buf, pc, is_kernel, event);
237 return;
240 if (!oprofile_begin_trace(cpu_buf))
241 return;
243 /* if log_sample() fail we can't backtrace since we lost the source
244 * of this event */
245 if (log_sample(cpu_buf, pc, is_kernel, event))
246 oprofile_ops.backtrace(regs, backtrace_depth);
247 oprofile_end_trace(cpu_buf);
250 void oprofile_add_sample(struct pt_regs * const regs, unsigned long event)
252 int is_kernel = !user_mode(regs);
253 unsigned long pc = profile_pc(regs);
255 oprofile_add_ext_sample(pc, regs, event, is_kernel);
258 #ifdef CONFIG_OPROFILE_IBS
260 #define MAX_IBS_SAMPLE_SIZE 14
261 static int log_ibs_sample(struct oprofile_cpu_buffer *cpu_buf,
262 unsigned long pc, int is_kernel, unsigned int *ibs, int ibs_code)
264 struct task_struct *task;
266 cpu_buf->sample_received++;
268 if (nr_available_slots(cpu_buf) < MAX_IBS_SAMPLE_SIZE) {
269 cpu_buf->sample_lost_overflow++;
270 return 0;
273 is_kernel = !!is_kernel;
275 /* notice a switch from user->kernel or vice versa */
276 if (cpu_buf->last_is_kernel != is_kernel) {
277 cpu_buf->last_is_kernel = is_kernel;
278 add_code(cpu_buf, is_kernel);
281 /* notice a task switch */
282 if (!is_kernel) {
283 task = current;
285 if (cpu_buf->last_task != task) {
286 cpu_buf->last_task = task;
287 add_code(cpu_buf, (unsigned long)task);
291 add_code(cpu_buf, ibs_code);
292 add_sample(cpu_buf, ibs[0], ibs[1]);
293 add_sample(cpu_buf, ibs[2], ibs[3]);
294 add_sample(cpu_buf, ibs[4], ibs[5]);
296 if (ibs_code == IBS_OP_BEGIN) {
297 add_sample(cpu_buf, ibs[6], ibs[7]);
298 add_sample(cpu_buf, ibs[8], ibs[9]);
299 add_sample(cpu_buf, ibs[10], ibs[11]);
302 return 1;
305 void oprofile_add_ibs_sample(struct pt_regs *const regs,
306 unsigned int * const ibs_sample, u8 code)
308 int is_kernel = !user_mode(regs);
309 unsigned long pc = profile_pc(regs);
311 struct oprofile_cpu_buffer *cpu_buf =
312 &per_cpu(cpu_buffer, smp_processor_id());
314 if (!backtrace_depth) {
315 log_ibs_sample(cpu_buf, pc, is_kernel, ibs_sample, code);
316 return;
319 /* if log_sample() fails we can't backtrace since we lost the source
320 * of this event */
321 if (log_ibs_sample(cpu_buf, pc, is_kernel, ibs_sample, code))
322 oprofile_ops.backtrace(regs, backtrace_depth);
325 #endif
327 void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event)
329 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
330 log_sample(cpu_buf, pc, is_kernel, event);
333 void oprofile_add_trace(unsigned long pc)
335 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
337 if (!cpu_buf->tracing)
338 return;
340 if (nr_available_slots(cpu_buf) < 1) {
341 cpu_buf->tracing = 0;
342 cpu_buf->sample_lost_overflow++;
343 return;
346 /* broken frame can give an eip with the same value as an escape code,
347 * abort the trace if we get it */
348 if (pc == ESCAPE_CODE) {
349 cpu_buf->tracing = 0;
350 cpu_buf->backtrace_aborted++;
351 return;
354 add_sample(cpu_buf, pc, 0);
358 * This serves to avoid cpu buffer overflow, and makes sure
359 * the task mortuary progresses
361 * By using schedule_delayed_work_on and then schedule_delayed_work
362 * we guarantee this will stay on the correct cpu
364 static void wq_sync_buffer(struct work_struct *work)
366 struct oprofile_cpu_buffer * b =
367 container_of(work, struct oprofile_cpu_buffer, work.work);
368 if (b->cpu != smp_processor_id()) {
369 printk(KERN_DEBUG "WQ on CPU%d, prefer CPU%d\n",
370 smp_processor_id(), b->cpu);
372 sync_buffer(b->cpu);
374 /* don't re-add the work if we're shutting down */
375 if (work_enabled)
376 schedule_delayed_work(&b->work, DEFAULT_TIMER_EXPIRE);