4 * @remark Copyright 2002-2009 OProfile authors
5 * @remark Read the file COPYING
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/errno.h>
26 #include <asm/ptrace.h>
28 #include "event_buffer.h"
29 #include "cpu_buffer.h"
30 #include "buffer_sync.h"
33 #define OP_BUFFER_FLAGS 0
35 static struct ring_buffer *op_ring_buffer;
36 DEFINE_PER_CPU(struct oprofile_cpu_buffer, op_cpu_buffer);
38 static void wq_sync_buffer(struct work_struct *work);
40 #define DEFAULT_TIMER_EXPIRE (HZ / 10)
41 static int work_enabled;
43 unsigned long oprofile_get_cpu_buffer_size(void)
45 return oprofile_cpu_buffer_size;
48 void oprofile_cpu_buffer_inc_smpl_lost(void)
50 struct oprofile_cpu_buffer *cpu_buf = this_cpu_ptr(&op_cpu_buffer);
52 cpu_buf->sample_lost_overflow++;
55 void free_cpu_buffers(void)
58 ring_buffer_free(op_ring_buffer);
59 op_ring_buffer = NULL;
62 #define RB_EVENT_HDR_SIZE 4
64 int alloc_cpu_buffers(void)
68 unsigned long buffer_size = oprofile_cpu_buffer_size;
69 unsigned long byte_size = buffer_size * (sizeof(struct op_sample) +
72 op_ring_buffer = ring_buffer_alloc(byte_size, OP_BUFFER_FLAGS);
76 for_each_possible_cpu(i) {
77 struct oprofile_cpu_buffer *b = &per_cpu(op_cpu_buffer, i);
80 b->last_is_kernel = -1;
82 b->buffer_size = buffer_size;
83 b->sample_received = 0;
84 b->sample_lost_overflow = 0;
85 b->backtrace_aborted = 0;
86 b->sample_invalid_eip = 0;
88 INIT_DELAYED_WORK(&b->work, wq_sync_buffer);
97 void start_cpu_work(void)
103 for_each_online_cpu(i) {
104 struct oprofile_cpu_buffer *b = &per_cpu(op_cpu_buffer, i);
107 * Spread the work by 1 jiffy per cpu so they dont all
110 schedule_delayed_work_on(i, &b->work, DEFAULT_TIMER_EXPIRE + i);
114 void end_cpu_work(void)
119 void flush_cpu_work(void)
123 for_each_online_cpu(i) {
124 struct oprofile_cpu_buffer *b = &per_cpu(op_cpu_buffer, i);
126 /* these works are per-cpu, no need for flush_sync */
127 flush_delayed_work(&b->work);
132 * This function prepares the cpu buffer to write a sample.
134 * Struct op_entry is used during operations on the ring buffer while
135 * struct op_sample contains the data that is stored in the ring
136 * buffer. Struct entry can be uninitialized. The function reserves a
137 * data array that is specified by size. Use
138 * op_cpu_buffer_write_commit() after preparing the sample. In case of
139 * errors a null pointer is returned, otherwise the pointer to the
144 *op_cpu_buffer_write_reserve(struct op_entry *entry, unsigned long size)
146 entry->event = ring_buffer_lock_reserve
147 (op_ring_buffer, sizeof(struct op_sample) +
148 size * sizeof(entry->sample->data[0]));
151 entry->sample = ring_buffer_event_data(entry->event);
153 entry->data = entry->sample->data;
155 return entry->sample;
158 int op_cpu_buffer_write_commit(struct op_entry *entry)
160 return ring_buffer_unlock_commit(op_ring_buffer, entry->event);
163 struct op_sample *op_cpu_buffer_read_entry(struct op_entry *entry, int cpu)
165 struct ring_buffer_event *e;
166 e = ring_buffer_consume(op_ring_buffer, cpu, NULL, NULL);
171 entry->sample = ring_buffer_event_data(e);
172 entry->size = (ring_buffer_event_length(e) - sizeof(struct op_sample))
173 / sizeof(entry->sample->data[0]);
174 entry->data = entry->sample->data;
175 return entry->sample;
178 unsigned long op_cpu_buffer_entries(int cpu)
180 return ring_buffer_entries_cpu(op_ring_buffer, cpu);
184 op_add_code(struct oprofile_cpu_buffer *cpu_buf, unsigned long backtrace,
185 int is_kernel, struct task_struct *task)
187 struct op_entry entry;
188 struct op_sample *sample;
195 flags |= TRACE_BEGIN;
197 /* notice a switch from user->kernel or vice versa */
198 is_kernel = !!is_kernel;
199 if (cpu_buf->last_is_kernel != is_kernel) {
200 cpu_buf->last_is_kernel = is_kernel;
201 flags |= KERNEL_CTX_SWITCH;
206 /* notice a task switch */
207 if (cpu_buf->last_task != task) {
208 cpu_buf->last_task = task;
209 flags |= USER_CTX_SWITCH;
216 if (flags & USER_CTX_SWITCH)
221 sample = op_cpu_buffer_write_reserve(&entry, size);
225 sample->eip = ESCAPE_CODE;
226 sample->event = flags;
229 op_cpu_buffer_add_data(&entry, (unsigned long)task);
231 op_cpu_buffer_write_commit(&entry);
237 op_add_sample(struct oprofile_cpu_buffer *cpu_buf,
238 unsigned long pc, unsigned long event)
240 struct op_entry entry;
241 struct op_sample *sample;
243 sample = op_cpu_buffer_write_reserve(&entry, 0);
248 sample->event = event;
250 return op_cpu_buffer_write_commit(&entry);
254 * This must be safe from any context.
256 * is_kernel is needed because on some architectures you cannot
257 * tell if you are in kernel or user space simply by looking at
258 * pc. We tag this in the buffer by generating kernel enter/exit
259 * events whenever is_kernel changes
262 log_sample(struct oprofile_cpu_buffer *cpu_buf, unsigned long pc,
263 unsigned long backtrace, int is_kernel, unsigned long event,
264 struct task_struct *task)
266 struct task_struct *tsk = task ? task : current;
267 cpu_buf->sample_received++;
269 if (pc == ESCAPE_CODE) {
270 cpu_buf->sample_invalid_eip++;
274 if (op_add_code(cpu_buf, backtrace, is_kernel, tsk))
277 if (op_add_sample(cpu_buf, pc, event))
283 cpu_buf->sample_lost_overflow++;
287 static inline void oprofile_begin_trace(struct oprofile_cpu_buffer *cpu_buf)
289 cpu_buf->tracing = 1;
292 static inline void oprofile_end_trace(struct oprofile_cpu_buffer *cpu_buf)
294 cpu_buf->tracing = 0;
298 __oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
299 unsigned long event, int is_kernel,
300 struct task_struct *task)
302 struct oprofile_cpu_buffer *cpu_buf = this_cpu_ptr(&op_cpu_buffer);
303 unsigned long backtrace = oprofile_backtrace_depth;
306 * if log_sample() fail we can't backtrace since we lost the
307 * source of this event
309 if (!log_sample(cpu_buf, pc, backtrace, is_kernel, event, task))
316 oprofile_begin_trace(cpu_buf);
317 oprofile_ops.backtrace(regs, backtrace);
318 oprofile_end_trace(cpu_buf);
321 void oprofile_add_ext_hw_sample(unsigned long pc, struct pt_regs * const regs,
322 unsigned long event, int is_kernel,
323 struct task_struct *task)
325 __oprofile_add_ext_sample(pc, regs, event, is_kernel, task);
328 void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
329 unsigned long event, int is_kernel)
331 __oprofile_add_ext_sample(pc, regs, event, is_kernel, NULL);
334 void oprofile_add_sample(struct pt_regs * const regs, unsigned long event)
340 is_kernel = !user_mode(regs);
341 pc = profile_pc(regs);
343 is_kernel = 0; /* This value will not be used */
344 pc = ESCAPE_CODE; /* as this causes an early return. */
347 __oprofile_add_ext_sample(pc, regs, event, is_kernel, NULL);
351 * Add samples with data to the ring buffer.
353 * Use oprofile_add_data(&entry, val) to add data and
354 * oprofile_write_commit(&entry) to commit the sample.
357 oprofile_write_reserve(struct op_entry *entry, struct pt_regs * const regs,
358 unsigned long pc, int code, int size)
360 struct op_sample *sample;
361 int is_kernel = !user_mode(regs);
362 struct oprofile_cpu_buffer *cpu_buf = this_cpu_ptr(&op_cpu_buffer);
364 cpu_buf->sample_received++;
366 /* no backtraces for samples with data */
367 if (op_add_code(cpu_buf, 0, is_kernel, current))
370 sample = op_cpu_buffer_write_reserve(entry, size + 2);
373 sample->eip = ESCAPE_CODE;
374 sample->event = 0; /* no flags */
376 op_cpu_buffer_add_data(entry, code);
377 op_cpu_buffer_add_data(entry, pc);
383 cpu_buf->sample_lost_overflow++;
386 int oprofile_add_data(struct op_entry *entry, unsigned long val)
390 return op_cpu_buffer_add_data(entry, val);
393 int oprofile_add_data64(struct op_entry *entry, u64 val)
397 if (op_cpu_buffer_get_size(entry) < 2)
399 * the function returns 0 to indicate a too small
400 * buffer, even if there is some space left
403 if (!op_cpu_buffer_add_data(entry, (u32)val))
405 return op_cpu_buffer_add_data(entry, (u32)(val >> 32));
408 int oprofile_write_commit(struct op_entry *entry)
412 return op_cpu_buffer_write_commit(entry);
415 void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event)
417 struct oprofile_cpu_buffer *cpu_buf = this_cpu_ptr(&op_cpu_buffer);
418 log_sample(cpu_buf, pc, 0, is_kernel, event, NULL);
421 void oprofile_add_trace(unsigned long pc)
423 struct oprofile_cpu_buffer *cpu_buf = this_cpu_ptr(&op_cpu_buffer);
425 if (!cpu_buf->tracing)
429 * broken frame can give an eip with the same value as an
430 * escape code, abort the trace if we get it
432 if (pc == ESCAPE_CODE)
435 if (op_add_sample(cpu_buf, pc, 0))
440 cpu_buf->tracing = 0;
441 cpu_buf->backtrace_aborted++;
446 * This serves to avoid cpu buffer overflow, and makes sure
447 * the task mortuary progresses
449 * By using schedule_delayed_work_on and then schedule_delayed_work
450 * we guarantee this will stay on the correct cpu
452 static void wq_sync_buffer(struct work_struct *work)
454 struct oprofile_cpu_buffer *b =
455 container_of(work, struct oprofile_cpu_buffer, work.work);
456 if (b->cpu != smp_processor_id() && !cpu_online(b->cpu)) {
457 cancel_delayed_work(&b->work);
462 /* don't re-add the work if we're shutting down */
464 schedule_delayed_work(&b->work, DEFAULT_TIMER_EXPIRE);