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/errno.h>
26 #include "event_buffer.h"
27 #include "cpu_buffer.h"
28 #include "buffer_sync.h"
31 #define OP_BUFFER_FLAGS 0
33 static struct ring_buffer
*op_ring_buffer
;
34 DEFINE_PER_CPU(struct oprofile_cpu_buffer
, op_cpu_buffer
);
36 static void wq_sync_buffer(struct work_struct
*work
);
38 #define DEFAULT_TIMER_EXPIRE (HZ / 10)
39 static int work_enabled
;
41 unsigned long oprofile_get_cpu_buffer_size(void)
43 return oprofile_cpu_buffer_size
;
46 void oprofile_cpu_buffer_inc_smpl_lost(void)
48 struct oprofile_cpu_buffer
*cpu_buf
= &__get_cpu_var(op_cpu_buffer
);
50 cpu_buf
->sample_lost_overflow
++;
53 void free_cpu_buffers(void)
56 ring_buffer_free(op_ring_buffer
);
57 op_ring_buffer
= NULL
;
60 #define RB_EVENT_HDR_SIZE 4
62 int alloc_cpu_buffers(void)
66 unsigned long buffer_size
= oprofile_cpu_buffer_size
;
67 unsigned long byte_size
= buffer_size
* (sizeof(struct op_sample
) +
70 op_ring_buffer
= ring_buffer_alloc(byte_size
, OP_BUFFER_FLAGS
);
74 for_each_possible_cpu(i
) {
75 struct oprofile_cpu_buffer
*b
= &per_cpu(op_cpu_buffer
, i
);
78 b
->last_is_kernel
= -1;
80 b
->buffer_size
= buffer_size
;
81 b
->sample_received
= 0;
82 b
->sample_lost_overflow
= 0;
83 b
->backtrace_aborted
= 0;
84 b
->sample_invalid_eip
= 0;
86 INIT_DELAYED_WORK(&b
->work
, wq_sync_buffer
);
95 void start_cpu_work(void)
101 for_each_online_cpu(i
) {
102 struct oprofile_cpu_buffer
*b
= &per_cpu(op_cpu_buffer
, i
);
105 * Spread the work by 1 jiffy per cpu so they dont all
108 schedule_delayed_work_on(i
, &b
->work
, DEFAULT_TIMER_EXPIRE
+ i
);
112 void end_cpu_work(void)
118 for_each_online_cpu(i
) {
119 struct oprofile_cpu_buffer
*b
= &per_cpu(op_cpu_buffer
, i
);
121 cancel_delayed_work(&b
->work
);
124 flush_scheduled_work();
128 * This function prepares the cpu buffer to write a sample.
130 * Struct op_entry is used during operations on the ring buffer while
131 * struct op_sample contains the data that is stored in the ring
132 * buffer. Struct entry can be uninitialized. The function reserves a
133 * data array that is specified by size. Use
134 * op_cpu_buffer_write_commit() after preparing the sample. In case of
135 * errors a null pointer is returned, otherwise the pointer to the
140 *op_cpu_buffer_write_reserve(struct op_entry
*entry
, unsigned long size
)
142 entry
->event
= ring_buffer_lock_reserve
143 (op_ring_buffer
, sizeof(struct op_sample
) +
144 size
* sizeof(entry
->sample
->data
[0]));
147 entry
->sample
= ring_buffer_event_data(entry
->event
);
149 entry
->data
= entry
->sample
->data
;
151 return entry
->sample
;
154 int op_cpu_buffer_write_commit(struct op_entry
*entry
)
156 return ring_buffer_unlock_commit(op_ring_buffer
, entry
->event
);
159 struct op_sample
*op_cpu_buffer_read_entry(struct op_entry
*entry
, int cpu
)
161 struct ring_buffer_event
*e
;
162 e
= ring_buffer_consume(op_ring_buffer
, cpu
, NULL
, NULL
);
167 entry
->sample
= ring_buffer_event_data(e
);
168 entry
->size
= (ring_buffer_event_length(e
) - sizeof(struct op_sample
))
169 / sizeof(entry
->sample
->data
[0]);
170 entry
->data
= entry
->sample
->data
;
171 return entry
->sample
;
174 unsigned long op_cpu_buffer_entries(int cpu
)
176 return ring_buffer_entries_cpu(op_ring_buffer
, cpu
);
180 op_add_code(struct oprofile_cpu_buffer
*cpu_buf
, unsigned long backtrace
,
181 int is_kernel
, struct task_struct
*task
)
183 struct op_entry entry
;
184 struct op_sample
*sample
;
191 flags
|= TRACE_BEGIN
;
193 /* notice a switch from user->kernel or vice versa */
194 is_kernel
= !!is_kernel
;
195 if (cpu_buf
->last_is_kernel
!= is_kernel
) {
196 cpu_buf
->last_is_kernel
= is_kernel
;
197 flags
|= KERNEL_CTX_SWITCH
;
202 /* notice a task switch */
203 if (cpu_buf
->last_task
!= task
) {
204 cpu_buf
->last_task
= task
;
205 flags
|= USER_CTX_SWITCH
;
212 if (flags
& USER_CTX_SWITCH
)
217 sample
= op_cpu_buffer_write_reserve(&entry
, size
);
221 sample
->eip
= ESCAPE_CODE
;
222 sample
->event
= flags
;
225 op_cpu_buffer_add_data(&entry
, (unsigned long)task
);
227 op_cpu_buffer_write_commit(&entry
);
233 op_add_sample(struct oprofile_cpu_buffer
*cpu_buf
,
234 unsigned long pc
, unsigned long event
)
236 struct op_entry entry
;
237 struct op_sample
*sample
;
239 sample
= op_cpu_buffer_write_reserve(&entry
, 0);
244 sample
->event
= event
;
246 return op_cpu_buffer_write_commit(&entry
);
250 * This must be safe from any context.
252 * is_kernel is needed because on some architectures you cannot
253 * tell if you are in kernel or user space simply by looking at
254 * pc. We tag this in the buffer by generating kernel enter/exit
255 * events whenever is_kernel changes
258 log_sample(struct oprofile_cpu_buffer
*cpu_buf
, unsigned long pc
,
259 unsigned long backtrace
, int is_kernel
, unsigned long event
)
261 cpu_buf
->sample_received
++;
263 if (pc
== ESCAPE_CODE
) {
264 cpu_buf
->sample_invalid_eip
++;
268 if (op_add_code(cpu_buf
, backtrace
, is_kernel
, current
))
271 if (op_add_sample(cpu_buf
, pc
, event
))
277 cpu_buf
->sample_lost_overflow
++;
281 static inline void oprofile_begin_trace(struct oprofile_cpu_buffer
*cpu_buf
)
283 cpu_buf
->tracing
= 1;
286 static inline void oprofile_end_trace(struct oprofile_cpu_buffer
*cpu_buf
)
288 cpu_buf
->tracing
= 0;
292 __oprofile_add_ext_sample(unsigned long pc
, struct pt_regs
* const regs
,
293 unsigned long event
, int is_kernel
)
295 struct oprofile_cpu_buffer
*cpu_buf
= &__get_cpu_var(op_cpu_buffer
);
296 unsigned long backtrace
= oprofile_backtrace_depth
;
299 * if log_sample() fail we can't backtrace since we lost the
300 * source of this event
302 if (!log_sample(cpu_buf
, pc
, backtrace
, is_kernel
, event
))
309 oprofile_begin_trace(cpu_buf
);
310 oprofile_ops
.backtrace(regs
, backtrace
);
311 oprofile_end_trace(cpu_buf
);
314 void oprofile_add_ext_sample(unsigned long pc
, struct pt_regs
* const regs
,
315 unsigned long event
, int is_kernel
)
317 __oprofile_add_ext_sample(pc
, regs
, event
, is_kernel
);
320 void oprofile_add_sample(struct pt_regs
* const regs
, unsigned long event
)
326 is_kernel
= !user_mode(regs
);
327 pc
= profile_pc(regs
);
329 is_kernel
= 0; /* This value will not be used */
330 pc
= ESCAPE_CODE
; /* as this causes an early return. */
333 __oprofile_add_ext_sample(pc
, regs
, event
, is_kernel
);
337 * Add samples with data to the ring buffer.
339 * Use oprofile_add_data(&entry, val) to add data and
340 * oprofile_write_commit(&entry) to commit the sample.
343 oprofile_write_reserve(struct op_entry
*entry
, struct pt_regs
* const regs
,
344 unsigned long pc
, int code
, int size
)
346 struct op_sample
*sample
;
347 int is_kernel
= !user_mode(regs
);
348 struct oprofile_cpu_buffer
*cpu_buf
= &__get_cpu_var(op_cpu_buffer
);
350 cpu_buf
->sample_received
++;
352 /* no backtraces for samples with data */
353 if (op_add_code(cpu_buf
, 0, is_kernel
, current
))
356 sample
= op_cpu_buffer_write_reserve(entry
, size
+ 2);
359 sample
->eip
= ESCAPE_CODE
;
360 sample
->event
= 0; /* no flags */
362 op_cpu_buffer_add_data(entry
, code
);
363 op_cpu_buffer_add_data(entry
, pc
);
369 cpu_buf
->sample_lost_overflow
++;
372 int oprofile_add_data(struct op_entry
*entry
, unsigned long val
)
376 return op_cpu_buffer_add_data(entry
, val
);
379 int oprofile_add_data64(struct op_entry
*entry
, u64 val
)
383 if (op_cpu_buffer_get_size(entry
) < 2)
385 * the function returns 0 to indicate a too small
386 * buffer, even if there is some space left
389 if (!op_cpu_buffer_add_data(entry
, (u32
)val
))
391 return op_cpu_buffer_add_data(entry
, (u32
)(val
>> 32));
394 int oprofile_write_commit(struct op_entry
*entry
)
398 return op_cpu_buffer_write_commit(entry
);
401 void oprofile_add_pc(unsigned long pc
, int is_kernel
, unsigned long event
)
403 struct oprofile_cpu_buffer
*cpu_buf
= &__get_cpu_var(op_cpu_buffer
);
404 log_sample(cpu_buf
, pc
, 0, is_kernel
, event
);
407 void oprofile_add_trace(unsigned long pc
)
409 struct oprofile_cpu_buffer
*cpu_buf
= &__get_cpu_var(op_cpu_buffer
);
411 if (!cpu_buf
->tracing
)
415 * broken frame can give an eip with the same value as an
416 * escape code, abort the trace if we get it
418 if (pc
== ESCAPE_CODE
)
421 if (op_add_sample(cpu_buf
, pc
, 0))
426 cpu_buf
->tracing
= 0;
427 cpu_buf
->backtrace_aborted
++;
432 * This serves to avoid cpu buffer overflow, and makes sure
433 * the task mortuary progresses
435 * By using schedule_delayed_work_on and then schedule_delayed_work
436 * we guarantee this will stay on the correct cpu
438 static void wq_sync_buffer(struct work_struct
*work
)
440 struct oprofile_cpu_buffer
*b
=
441 container_of(work
, struct oprofile_cpu_buffer
, work
.work
);
442 if (b
->cpu
!= smp_processor_id()) {
443 printk(KERN_DEBUG
"WQ on CPU%d, prefer CPU%d\n",
444 smp_processor_id(), b
->cpu
);
446 if (!cpu_online(b
->cpu
)) {
447 cancel_delayed_work(&b
->work
);
453 /* don't re-add the work if we're shutting down */
455 schedule_delayed_work(&b
->work
, DEFAULT_TIMER_EXPIRE
);