[PATCH] sparc64: task_pt_regs()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / oprofile / cpu_buffer.c
blob78193e4bbdb564981a98b00437bdad6a8b851500
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>
9 * Each CPU has a local buffer that stores PC value/event
10 * pairs. We also log context switches when we notice them.
11 * Eventually each CPU's buffer is processed into the global
12 * event buffer by sync_buffer().
14 * We use a local buffer for two reasons: an NMI or similar
15 * interrupt cannot synchronise, and high sampling rates
16 * would lead to catastrophic global synchronisation if
17 * a global buffer was used.
20 #include <linux/sched.h>
21 #include <linux/oprofile.h>
22 #include <linux/vmalloc.h>
23 #include <linux/errno.h>
25 #include "event_buffer.h"
26 #include "cpu_buffer.h"
27 #include "buffer_sync.h"
28 #include "oprof.h"
30 struct oprofile_cpu_buffer cpu_buffer[NR_CPUS] __cacheline_aligned;
32 static void wq_sync_buffer(void *);
34 #define DEFAULT_TIMER_EXPIRE (HZ / 10)
35 static int work_enabled;
37 void free_cpu_buffers(void)
39 int i;
41 for_each_online_cpu(i) {
42 vfree(cpu_buffer[i].buffer);
46 int alloc_cpu_buffers(void)
48 int i;
50 unsigned long buffer_size = fs_cpu_buffer_size;
52 for_each_online_cpu(i) {
53 struct oprofile_cpu_buffer * b = &cpu_buffer[i];
55 b->buffer = vmalloc_node(sizeof(struct op_sample) * buffer_size,
56 cpu_to_node(i));
57 if (!b->buffer)
58 goto fail;
60 b->last_task = NULL;
61 b->last_is_kernel = -1;
62 b->tracing = 0;
63 b->buffer_size = buffer_size;
64 b->tail_pos = 0;
65 b->head_pos = 0;
66 b->sample_received = 0;
67 b->sample_lost_overflow = 0;
68 b->cpu = i;
69 INIT_WORK(&b->work, wq_sync_buffer, b);
71 return 0;
73 fail:
74 free_cpu_buffers();
75 return -ENOMEM;
78 void start_cpu_work(void)
80 int i;
82 work_enabled = 1;
84 for_each_online_cpu(i) {
85 struct oprofile_cpu_buffer * b = &cpu_buffer[i];
88 * Spread the work by 1 jiffy per cpu so they dont all
89 * fire at once.
91 schedule_delayed_work_on(i, &b->work, DEFAULT_TIMER_EXPIRE + i);
95 void end_cpu_work(void)
97 int i;
99 work_enabled = 0;
101 for_each_online_cpu(i) {
102 struct oprofile_cpu_buffer * b = &cpu_buffer[i];
104 cancel_delayed_work(&b->work);
107 flush_scheduled_work();
110 /* Resets the cpu buffer to a sane state. */
111 void cpu_buffer_reset(struct oprofile_cpu_buffer * cpu_buf)
113 /* reset these to invalid values; the next sample
114 * collected will populate the buffer with proper
115 * values to initialize the buffer
117 cpu_buf->last_is_kernel = -1;
118 cpu_buf->last_task = NULL;
121 /* compute number of available slots in cpu_buffer queue */
122 static unsigned long nr_available_slots(struct oprofile_cpu_buffer const * b)
124 unsigned long head = b->head_pos;
125 unsigned long tail = b->tail_pos;
127 if (tail > head)
128 return (tail - head) - 1;
130 return tail + (b->buffer_size - head) - 1;
133 static void increment_head(struct oprofile_cpu_buffer * b)
135 unsigned long new_head = b->head_pos + 1;
137 /* Ensure anything written to the slot before we
138 * increment is visible */
139 wmb();
141 if (new_head < b->buffer_size)
142 b->head_pos = new_head;
143 else
144 b->head_pos = 0;
147 static inline void
148 add_sample(struct oprofile_cpu_buffer * cpu_buf,
149 unsigned long pc, unsigned long event)
151 struct op_sample * entry = &cpu_buf->buffer[cpu_buf->head_pos];
152 entry->eip = pc;
153 entry->event = event;
154 increment_head(cpu_buf);
157 static inline void
158 add_code(struct oprofile_cpu_buffer * buffer, unsigned long value)
160 add_sample(buffer, ESCAPE_CODE, value);
163 /* This must be safe from any context. It's safe writing here
164 * because of the head/tail separation of the writer and reader
165 * of the CPU buffer.
167 * is_kernel is needed because on some architectures you cannot
168 * tell if you are in kernel or user space simply by looking at
169 * pc. We tag this in the buffer by generating kernel enter/exit
170 * events whenever is_kernel changes
172 static int log_sample(struct oprofile_cpu_buffer * cpu_buf, unsigned long pc,
173 int is_kernel, unsigned long event)
175 struct task_struct * task;
177 cpu_buf->sample_received++;
179 if (nr_available_slots(cpu_buf) < 3) {
180 cpu_buf->sample_lost_overflow++;
181 return 0;
184 is_kernel = !!is_kernel;
186 task = current;
188 /* notice a switch from user->kernel or vice versa */
189 if (cpu_buf->last_is_kernel != is_kernel) {
190 cpu_buf->last_is_kernel = is_kernel;
191 add_code(cpu_buf, is_kernel);
194 /* notice a task switch */
195 if (cpu_buf->last_task != task) {
196 cpu_buf->last_task = task;
197 add_code(cpu_buf, (unsigned long)task);
200 add_sample(cpu_buf, pc, event);
201 return 1;
204 static int oprofile_begin_trace(struct oprofile_cpu_buffer * cpu_buf)
206 if (nr_available_slots(cpu_buf) < 4) {
207 cpu_buf->sample_lost_overflow++;
208 return 0;
211 add_code(cpu_buf, CPU_TRACE_BEGIN);
212 cpu_buf->tracing = 1;
213 return 1;
216 static void oprofile_end_trace(struct oprofile_cpu_buffer * cpu_buf)
218 cpu_buf->tracing = 0;
221 void oprofile_add_sample(struct pt_regs * const regs, unsigned long event)
223 struct oprofile_cpu_buffer * cpu_buf = &cpu_buffer[smp_processor_id()];
224 unsigned long pc = profile_pc(regs);
225 int is_kernel = !user_mode(regs);
227 if (!backtrace_depth) {
228 log_sample(cpu_buf, pc, is_kernel, event);
229 return;
232 if (!oprofile_begin_trace(cpu_buf))
233 return;
235 /* if log_sample() fail we can't backtrace since we lost the source
236 * of this event */
237 if (log_sample(cpu_buf, pc, is_kernel, event))
238 oprofile_ops.backtrace(regs, backtrace_depth);
239 oprofile_end_trace(cpu_buf);
242 void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event)
244 struct oprofile_cpu_buffer * cpu_buf = &cpu_buffer[smp_processor_id()];
245 log_sample(cpu_buf, pc, is_kernel, event);
248 void oprofile_add_trace(unsigned long pc)
250 struct oprofile_cpu_buffer * cpu_buf = &cpu_buffer[smp_processor_id()];
252 if (!cpu_buf->tracing)
253 return;
255 if (nr_available_slots(cpu_buf) < 1) {
256 cpu_buf->tracing = 0;
257 cpu_buf->sample_lost_overflow++;
258 return;
261 /* broken frame can give an eip with the same value as an escape code,
262 * abort the trace if we get it */
263 if (pc == ESCAPE_CODE) {
264 cpu_buf->tracing = 0;
265 cpu_buf->backtrace_aborted++;
266 return;
269 add_sample(cpu_buf, pc, 0);
273 * This serves to avoid cpu buffer overflow, and makes sure
274 * the task mortuary progresses
276 * By using schedule_delayed_work_on and then schedule_delayed_work
277 * we guarantee this will stay on the correct cpu
279 static void wq_sync_buffer(void * data)
281 struct oprofile_cpu_buffer * b = data;
282 if (b->cpu != smp_processor_id()) {
283 printk("WQ on CPU%d, prefer CPU%d\n",
284 smp_processor_id(), b->cpu);
286 sync_buffer(b->cpu);
288 /* don't re-add the work if we're shutting down */
289 if (work_enabled)
290 schedule_delayed_work(&b->work, DEFAULT_TIMER_EXPIRE);