conn rcv_lock converted to spinlock, struct cor_sock created, kernel_packet skb_clone...
[cor_2_6_31.git] / drivers / oprofile / cpu_buffer.c
blob242257b19441fb31bc95b0ba10324ffc2ec8df35
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 #define RB_EVENT_HDR_SIZE 4
83 int alloc_cpu_buffers(void)
85 int i;
87 unsigned long buffer_size = oprofile_cpu_buffer_size;
88 unsigned long byte_size = buffer_size * (sizeof(struct op_sample) +
89 RB_EVENT_HDR_SIZE);
91 op_ring_buffer_read = ring_buffer_alloc(byte_size, OP_BUFFER_FLAGS);
92 if (!op_ring_buffer_read)
93 goto fail;
94 op_ring_buffer_write = ring_buffer_alloc(byte_size, OP_BUFFER_FLAGS);
95 if (!op_ring_buffer_write)
96 goto fail;
98 for_each_possible_cpu(i) {
99 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
101 b->last_task = NULL;
102 b->last_is_kernel = -1;
103 b->tracing = 0;
104 b->buffer_size = buffer_size;
105 b->sample_received = 0;
106 b->sample_lost_overflow = 0;
107 b->backtrace_aborted = 0;
108 b->sample_invalid_eip = 0;
109 b->cpu = i;
110 INIT_DELAYED_WORK(&b->work, wq_sync_buffer);
112 return 0;
114 fail:
115 free_cpu_buffers();
116 return -ENOMEM;
119 void start_cpu_work(void)
121 int i;
123 work_enabled = 1;
125 for_each_online_cpu(i) {
126 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
129 * Spread the work by 1 jiffy per cpu so they dont all
130 * fire at once.
132 schedule_delayed_work_on(i, &b->work, DEFAULT_TIMER_EXPIRE + i);
136 void end_cpu_work(void)
138 int i;
140 work_enabled = 0;
142 for_each_online_cpu(i) {
143 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
145 cancel_delayed_work(&b->work);
148 flush_scheduled_work();
152 * This function prepares the cpu buffer to write a sample.
154 * Struct op_entry is used during operations on the ring buffer while
155 * struct op_sample contains the data that is stored in the ring
156 * buffer. Struct entry can be uninitialized. The function reserves a
157 * data array that is specified by size. Use
158 * op_cpu_buffer_write_commit() after preparing the sample. In case of
159 * errors a null pointer is returned, otherwise the pointer to the
160 * sample.
163 struct op_sample
164 *op_cpu_buffer_write_reserve(struct op_entry *entry, unsigned long size)
166 entry->event = ring_buffer_lock_reserve
167 (op_ring_buffer_write, sizeof(struct op_sample) +
168 size * sizeof(entry->sample->data[0]));
169 if (entry->event)
170 entry->sample = ring_buffer_event_data(entry->event);
171 else
172 entry->sample = NULL;
174 if (!entry->sample)
175 return NULL;
177 entry->size = size;
178 entry->data = entry->sample->data;
180 return entry->sample;
183 int op_cpu_buffer_write_commit(struct op_entry *entry)
185 return ring_buffer_unlock_commit(op_ring_buffer_write, entry->event);
188 struct op_sample *op_cpu_buffer_read_entry(struct op_entry *entry, int cpu)
190 struct ring_buffer_event *e;
191 e = ring_buffer_consume(op_ring_buffer_read, cpu, NULL);
192 if (e)
193 goto event;
194 if (ring_buffer_swap_cpu(op_ring_buffer_read,
195 op_ring_buffer_write,
196 cpu))
197 return NULL;
198 e = ring_buffer_consume(op_ring_buffer_read, cpu, NULL);
199 if (e)
200 goto event;
201 return NULL;
203 event:
204 entry->event = e;
205 entry->sample = ring_buffer_event_data(e);
206 entry->size = (ring_buffer_event_length(e) - sizeof(struct op_sample))
207 / sizeof(entry->sample->data[0]);
208 entry->data = entry->sample->data;
209 return entry->sample;
212 unsigned long op_cpu_buffer_entries(int cpu)
214 return ring_buffer_entries_cpu(op_ring_buffer_read, cpu)
215 + ring_buffer_entries_cpu(op_ring_buffer_write, cpu);
218 static int
219 op_add_code(struct oprofile_cpu_buffer *cpu_buf, unsigned long backtrace,
220 int is_kernel, struct task_struct *task)
222 struct op_entry entry;
223 struct op_sample *sample;
224 unsigned long flags;
225 int size;
227 flags = 0;
229 if (backtrace)
230 flags |= TRACE_BEGIN;
232 /* notice a switch from user->kernel or vice versa */
233 is_kernel = !!is_kernel;
234 if (cpu_buf->last_is_kernel != is_kernel) {
235 cpu_buf->last_is_kernel = is_kernel;
236 flags |= KERNEL_CTX_SWITCH;
237 if (is_kernel)
238 flags |= IS_KERNEL;
241 /* notice a task switch */
242 if (cpu_buf->last_task != task) {
243 cpu_buf->last_task = task;
244 flags |= USER_CTX_SWITCH;
247 if (!flags)
248 /* nothing to do */
249 return 0;
251 if (flags & USER_CTX_SWITCH)
252 size = 1;
253 else
254 size = 0;
256 sample = op_cpu_buffer_write_reserve(&entry, size);
257 if (!sample)
258 return -ENOMEM;
260 sample->eip = ESCAPE_CODE;
261 sample->event = flags;
263 if (size)
264 op_cpu_buffer_add_data(&entry, (unsigned long)task);
266 op_cpu_buffer_write_commit(&entry);
268 return 0;
271 static inline int
272 op_add_sample(struct oprofile_cpu_buffer *cpu_buf,
273 unsigned long pc, unsigned long event)
275 struct op_entry entry;
276 struct op_sample *sample;
278 sample = op_cpu_buffer_write_reserve(&entry, 0);
279 if (!sample)
280 return -ENOMEM;
282 sample->eip = pc;
283 sample->event = event;
285 return op_cpu_buffer_write_commit(&entry);
289 * This must be safe from any context.
291 * is_kernel is needed because on some architectures you cannot
292 * tell if you are in kernel or user space simply by looking at
293 * pc. We tag this in the buffer by generating kernel enter/exit
294 * events whenever is_kernel changes
296 static int
297 log_sample(struct oprofile_cpu_buffer *cpu_buf, unsigned long pc,
298 unsigned long backtrace, int is_kernel, unsigned long event)
300 cpu_buf->sample_received++;
302 if (pc == ESCAPE_CODE) {
303 cpu_buf->sample_invalid_eip++;
304 return 0;
307 if (op_add_code(cpu_buf, backtrace, is_kernel, current))
308 goto fail;
310 if (op_add_sample(cpu_buf, pc, event))
311 goto fail;
313 return 1;
315 fail:
316 cpu_buf->sample_lost_overflow++;
317 return 0;
320 static inline void oprofile_begin_trace(struct oprofile_cpu_buffer *cpu_buf)
322 cpu_buf->tracing = 1;
325 static inline void oprofile_end_trace(struct oprofile_cpu_buffer *cpu_buf)
327 cpu_buf->tracing = 0;
330 static inline void
331 __oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
332 unsigned long event, int is_kernel)
334 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
335 unsigned long backtrace = oprofile_backtrace_depth;
338 * if log_sample() fail we can't backtrace since we lost the
339 * source of this event
341 if (!log_sample(cpu_buf, pc, backtrace, is_kernel, event))
342 /* failed */
343 return;
345 if (!backtrace)
346 return;
348 oprofile_begin_trace(cpu_buf);
349 oprofile_ops.backtrace(regs, backtrace);
350 oprofile_end_trace(cpu_buf);
353 void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
354 unsigned long event, int is_kernel)
356 __oprofile_add_ext_sample(pc, regs, event, is_kernel);
359 void oprofile_add_sample(struct pt_regs * const regs, unsigned long event)
361 int is_kernel = !user_mode(regs);
362 unsigned long pc = profile_pc(regs);
364 __oprofile_add_ext_sample(pc, regs, event, is_kernel);
368 * Add samples with data to the ring buffer.
370 * Use oprofile_add_data(&entry, val) to add data and
371 * oprofile_write_commit(&entry) to commit the sample.
373 void
374 oprofile_write_reserve(struct op_entry *entry, struct pt_regs * const regs,
375 unsigned long pc, int code, int size)
377 struct op_sample *sample;
378 int is_kernel = !user_mode(regs);
379 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
381 cpu_buf->sample_received++;
383 /* no backtraces for samples with data */
384 if (op_add_code(cpu_buf, 0, is_kernel, current))
385 goto fail;
387 sample = op_cpu_buffer_write_reserve(entry, size + 2);
388 if (!sample)
389 goto fail;
390 sample->eip = ESCAPE_CODE;
391 sample->event = 0; /* no flags */
393 op_cpu_buffer_add_data(entry, code);
394 op_cpu_buffer_add_data(entry, pc);
396 return;
398 fail:
399 entry->event = NULL;
400 cpu_buf->sample_lost_overflow++;
403 int oprofile_add_data(struct op_entry *entry, unsigned long val)
405 if (!entry->event)
406 return 0;
407 return op_cpu_buffer_add_data(entry, val);
410 int oprofile_write_commit(struct op_entry *entry)
412 if (!entry->event)
413 return -EINVAL;
414 return op_cpu_buffer_write_commit(entry);
417 void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event)
419 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
420 log_sample(cpu_buf, pc, 0, is_kernel, event);
423 void oprofile_add_trace(unsigned long pc)
425 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
427 if (!cpu_buf->tracing)
428 return;
431 * broken frame can give an eip with the same value as an
432 * escape code, abort the trace if we get it
434 if (pc == ESCAPE_CODE)
435 goto fail;
437 if (op_add_sample(cpu_buf, pc, 0))
438 goto fail;
440 return;
441 fail:
442 cpu_buf->tracing = 0;
443 cpu_buf->backtrace_aborted++;
444 return;
448 * This serves to avoid cpu buffer overflow, and makes sure
449 * the task mortuary progresses
451 * By using schedule_delayed_work_on and then schedule_delayed_work
452 * we guarantee this will stay on the correct cpu
454 static void wq_sync_buffer(struct work_struct *work)
456 struct oprofile_cpu_buffer *b =
457 container_of(work, struct oprofile_cpu_buffer, work.work);
458 if (b->cpu != smp_processor_id()) {
459 printk(KERN_DEBUG "WQ on CPU%d, prefer CPU%d\n",
460 smp_processor_id(), b->cpu);
462 if (!cpu_online(b->cpu)) {
463 cancel_delayed_work(&b->work);
464 return;
467 sync_buffer(b->cpu);
469 /* don't re-add the work if we're shutting down */
470 if (work_enabled)
471 schedule_delayed_work(&b->work, DEFAULT_TIMER_EXPIRE);