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/vmalloc.h>
22 #include <linux/errno.h>
24 #include "cpu_buffer.h"
25 #include "buffer_sync.h"
28 struct oprofile_cpu_buffer cpu_buffer[NR_CPUS] __cacheline_aligned;
30 static void wq_sync_buffer(void *);
32 #define DEFAULT_TIMER_EXPIRE (HZ / 10)
35 static void __free_cpu_buffers(int num)
39 for_each_online_cpu(i) {
40 if (cpu_buffer[i].buffer)
41 vfree(cpu_buffer[i].buffer);
46 int alloc_cpu_buffers(void)
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(sizeof(struct op_sample) * buffer_size);
60 b->last_is_kernel = -1;
61 b->buffer_size = buffer_size;
64 b->sample_received = 0;
65 b->sample_lost_overflow = 0;
67 INIT_WORK(&b->work, wq_sync_buffer, b);
72 __free_cpu_buffers(i);
77 void free_cpu_buffers(void)
79 __free_cpu_buffers(NR_CPUS);
83 void start_cpu_work(void)
89 for_each_online_cpu(i) {
90 struct oprofile_cpu_buffer * b = &cpu_buffer[i];
93 * Spread the work by 1 jiffy per cpu so they dont all
96 schedule_delayed_work_on(i, &b->work, DEFAULT_TIMER_EXPIRE + i);
101 void end_cpu_work(void)
107 for_each_online_cpu(i) {
108 struct oprofile_cpu_buffer * b = &cpu_buffer[i];
110 cancel_delayed_work(&b->work);
113 flush_scheduled_work();
117 /* compute number of available slots in cpu_buffer queue */
118 static unsigned long nr_available_slots(struct oprofile_cpu_buffer const * b)
120 unsigned long head = b->head_pos;
121 unsigned long tail = b->tail_pos;
124 return (tail - head) - 1;
126 return tail + (b->buffer_size - head) - 1;
130 static void increment_head(struct oprofile_cpu_buffer * b)
132 unsigned long new_head = b->head_pos + 1;
134 /* Ensure anything written to the slot before we
135 * increment is visible */
138 if (new_head < (b->buffer_size))
139 b->head_pos = new_head;
145 /* This must be safe from any context. It's safe writing here
146 * because of the head/tail separation of the writer and reader
149 * is_kernel is needed because on some architectures you cannot
150 * tell if you are in kernel or user space simply by looking at
151 * eip. We tag this in the buffer by generating kernel enter/exit
152 * events whenever is_kernel changes
154 void oprofile_add_sample(unsigned long eip, unsigned int is_kernel,
155 unsigned long event, int cpu)
157 struct oprofile_cpu_buffer * cpu_buf = &cpu_buffer[cpu];
158 struct task_struct * task;
160 is_kernel = !!is_kernel;
162 cpu_buf->sample_received++;
165 if (nr_available_slots(cpu_buf) < 3) {
166 cpu_buf->sample_lost_overflow++;
172 /* notice a switch from user->kernel or vice versa */
173 if (cpu_buf->last_is_kernel != is_kernel) {
174 cpu_buf->last_is_kernel = is_kernel;
175 cpu_buf->buffer[cpu_buf->head_pos].eip = ~0UL;
176 cpu_buf->buffer[cpu_buf->head_pos].event = is_kernel;
177 increment_head(cpu_buf);
180 /* notice a task switch */
181 if (cpu_buf->last_task != task) {
182 cpu_buf->last_task = task;
183 cpu_buf->buffer[cpu_buf->head_pos].eip = ~0UL;
184 cpu_buf->buffer[cpu_buf->head_pos].event = (unsigned long)task;
185 increment_head(cpu_buf);
188 cpu_buf->buffer[cpu_buf->head_pos].eip = eip;
189 cpu_buf->buffer[cpu_buf->head_pos].event = event;
190 increment_head(cpu_buf);
194 /* Resets the cpu buffer to a sane state. */
195 void cpu_buffer_reset(struct oprofile_cpu_buffer * cpu_buf)
197 /* reset these to invalid values; the next sample
198 * collected will populate the buffer with proper
199 * values to initialize the buffer
201 cpu_buf->last_is_kernel = -1;
202 cpu_buf->last_task = NULL;
207 * This serves to avoid cpu buffer overflow, and makes sure
208 * the task mortuary progresses
210 * By using schedule_delayed_work_on and then schedule_delayed_work
211 * we guarantee this will stay on the correct cpu
213 static void wq_sync_buffer(void * data)
215 struct oprofile_cpu_buffer * b = (struct oprofile_cpu_buffer *)data;
216 if (b->cpu != smp_processor_id()) {
217 printk("WQ on CPU%d, prefer CPU%d\n",
218 smp_processor_id(), b->cpu);
222 /* don't re-add the work if we're shutting down */
224 schedule_delayed_work(&b->work, DEFAULT_TIMER_EXPIRE);