4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
7 * @author John Levon <levon@movementarian.org>
8 * @author Barry Kasindorf <barry.kasindorf@amd.com>
10 * Each CPU has a local buffer that stores PC value/event
11 * pairs. We also log context switches when we notice them.
12 * Eventually each CPU's buffer is processed into the global
13 * event buffer by sync_buffer().
15 * We use a local buffer for two reasons: an NMI or similar
16 * interrupt cannot synchronise, and high sampling rates
17 * would lead to catastrophic global synchronisation if
18 * a global buffer was used.
21 #include <linux/sched.h>
22 #include <linux/oprofile.h>
23 #include <linux/vmalloc.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
34 * Read and write access is using spin locking. Thus, writing to the
35 * buffer by NMI handler (x86) could occur also during critical
36 * sections when reading the buffer. To avoid this, there are 2
37 * buffers for independent read and write access. Read access is in
38 * process context only, write access only in the NMI handler. If the
39 * read buffer runs empty, both buffers are swapped atomically. There
40 * is potentially a small window during swapping where the buffers are
41 * disabled and samples could be lost.
43 * Using 2 buffers is a little bit overhead, but the solution is clear
44 * and does not require changes in the ring buffer implementation. It
45 * can be changed to a single buffer solution when the ring buffer
46 * access is implemented as non-locking atomic code.
48 static struct ring_buffer
*op_ring_buffer_read
;
49 static struct ring_buffer
*op_ring_buffer_write
;
50 DEFINE_PER_CPU(struct oprofile_cpu_buffer
, cpu_buffer
);
52 static void wq_sync_buffer(struct work_struct
*work
);
54 #define DEFAULT_TIMER_EXPIRE (HZ / 10)
55 static int work_enabled
;
57 unsigned long oprofile_get_cpu_buffer_size(void)
59 return oprofile_cpu_buffer_size
;
62 void oprofile_cpu_buffer_inc_smpl_lost(void)
64 struct oprofile_cpu_buffer
*cpu_buf
65 = &__get_cpu_var(cpu_buffer
);
67 cpu_buf
->sample_lost_overflow
++;
70 void free_cpu_buffers(void)
72 if (op_ring_buffer_read
)
73 ring_buffer_free(op_ring_buffer_read
);
74 op_ring_buffer_read
= NULL
;
75 if (op_ring_buffer_write
)
76 ring_buffer_free(op_ring_buffer_write
);
77 op_ring_buffer_write
= NULL
;
80 int alloc_cpu_buffers(void)
84 unsigned long buffer_size
= oprofile_cpu_buffer_size
;
86 op_ring_buffer_read
= ring_buffer_alloc(buffer_size
, OP_BUFFER_FLAGS
);
87 if (!op_ring_buffer_read
)
89 op_ring_buffer_write
= ring_buffer_alloc(buffer_size
, OP_BUFFER_FLAGS
);
90 if (!op_ring_buffer_write
)
93 for_each_possible_cpu(i
) {
94 struct oprofile_cpu_buffer
*b
= &per_cpu(cpu_buffer
, i
);
97 b
->last_is_kernel
= -1;
99 b
->buffer_size
= buffer_size
;
100 b
->sample_received
= 0;
101 b
->sample_lost_overflow
= 0;
102 b
->backtrace_aborted
= 0;
103 b
->sample_invalid_eip
= 0;
105 INIT_DELAYED_WORK(&b
->work
, wq_sync_buffer
);
114 void start_cpu_work(void)
120 for_each_online_cpu(i
) {
121 struct oprofile_cpu_buffer
*b
= &per_cpu(cpu_buffer
, i
);
124 * Spread the work by 1 jiffy per cpu so they dont all
127 schedule_delayed_work_on(i
, &b
->work
, DEFAULT_TIMER_EXPIRE
+ i
);
131 void end_cpu_work(void)
137 for_each_online_cpu(i
) {
138 struct oprofile_cpu_buffer
*b
= &per_cpu(cpu_buffer
, i
);
140 cancel_delayed_work(&b
->work
);
143 flush_scheduled_work();
146 int op_cpu_buffer_write_entry(struct op_entry
*entry
)
148 entry
->event
= ring_buffer_lock_reserve(op_ring_buffer_write
,
149 sizeof(struct op_sample
),
152 entry
->sample
= ring_buffer_event_data(entry
->event
);
154 entry
->sample
= NULL
;
162 int op_cpu_buffer_write_commit(struct op_entry
*entry
)
164 return ring_buffer_unlock_commit(op_ring_buffer_write
, entry
->event
,
168 struct op_sample
*op_cpu_buffer_read_entry(int cpu
)
170 struct ring_buffer_event
*e
;
171 e
= ring_buffer_consume(op_ring_buffer_read
, cpu
, NULL
);
173 return ring_buffer_event_data(e
);
174 if (ring_buffer_swap_cpu(op_ring_buffer_read
,
175 op_ring_buffer_write
,
178 e
= ring_buffer_consume(op_ring_buffer_read
, cpu
, NULL
);
180 return ring_buffer_event_data(e
);
184 unsigned long op_cpu_buffer_entries(int cpu
)
186 return ring_buffer_entries_cpu(op_ring_buffer_read
, cpu
)
187 + ring_buffer_entries_cpu(op_ring_buffer_write
, cpu
);
191 add_sample(struct oprofile_cpu_buffer
*cpu_buf
,
192 unsigned long pc
, unsigned long event
)
194 struct op_entry entry
;
197 ret
= op_cpu_buffer_write_entry(&entry
);
201 entry
.sample
->eip
= pc
;
202 entry
.sample
->event
= event
;
204 return op_cpu_buffer_write_commit(&entry
);
208 add_code(struct oprofile_cpu_buffer
*buffer
, unsigned long value
)
210 return add_sample(buffer
, ESCAPE_CODE
, value
);
213 /* This must be safe from any context. It's safe writing here
214 * because of the head/tail separation of the writer and reader
217 * is_kernel is needed because on some architectures you cannot
218 * tell if you are in kernel or user space simply by looking at
219 * pc. We tag this in the buffer by generating kernel enter/exit
220 * events whenever is_kernel changes
222 static int log_sample(struct oprofile_cpu_buffer
*cpu_buf
, unsigned long pc
,
223 int is_kernel
, unsigned long event
)
225 struct task_struct
*task
;
227 cpu_buf
->sample_received
++;
229 if (pc
== ESCAPE_CODE
) {
230 cpu_buf
->sample_invalid_eip
++;
234 is_kernel
= !!is_kernel
;
238 /* notice a switch from user->kernel or vice versa */
239 if (cpu_buf
->last_is_kernel
!= is_kernel
) {
240 cpu_buf
->last_is_kernel
= is_kernel
;
241 if (add_code(cpu_buf
, is_kernel
))
245 /* notice a task switch */
246 if (cpu_buf
->last_task
!= task
) {
247 cpu_buf
->last_task
= task
;
248 if (add_code(cpu_buf
, (unsigned long)task
))
252 if (add_sample(cpu_buf
, pc
, event
))
258 cpu_buf
->sample_lost_overflow
++;
262 static inline void oprofile_begin_trace(struct oprofile_cpu_buffer
*cpu_buf
)
264 add_code(cpu_buf
, CPU_TRACE_BEGIN
);
265 cpu_buf
->tracing
= 1;
268 static inline void oprofile_end_trace(struct oprofile_cpu_buffer
*cpu_buf
)
270 cpu_buf
->tracing
= 0;
274 __oprofile_add_ext_sample(unsigned long pc
, struct pt_regs
* const regs
,
275 unsigned long event
, int is_kernel
)
277 struct oprofile_cpu_buffer
*cpu_buf
= &__get_cpu_var(cpu_buffer
);
279 if (!oprofile_backtrace_depth
) {
280 log_sample(cpu_buf
, pc
, is_kernel
, event
);
284 oprofile_begin_trace(cpu_buf
);
287 * if log_sample() fail we can't backtrace since we lost the
288 * source of this event
290 if (log_sample(cpu_buf
, pc
, is_kernel
, event
))
291 oprofile_ops
.backtrace(regs
, oprofile_backtrace_depth
);
293 oprofile_end_trace(cpu_buf
);
296 void oprofile_add_ext_sample(unsigned long pc
, struct pt_regs
* const regs
,
297 unsigned long event
, int is_kernel
)
299 __oprofile_add_ext_sample(pc
, regs
, event
, is_kernel
);
302 void oprofile_add_sample(struct pt_regs
* const regs
, unsigned long event
)
304 int is_kernel
= !user_mode(regs
);
305 unsigned long pc
= profile_pc(regs
);
307 __oprofile_add_ext_sample(pc
, regs
, event
, is_kernel
);
310 #ifdef CONFIG_OPROFILE_IBS
312 #define MAX_IBS_SAMPLE_SIZE 14
314 void oprofile_add_ibs_sample(struct pt_regs
* const regs
,
315 unsigned int * const ibs_sample
, int ibs_code
)
317 int is_kernel
= !user_mode(regs
);
318 struct oprofile_cpu_buffer
*cpu_buf
= &__get_cpu_var(cpu_buffer
);
319 struct task_struct
*task
;
322 cpu_buf
->sample_received
++;
324 /* notice a switch from user->kernel or vice versa */
325 if (cpu_buf
->last_is_kernel
!= is_kernel
) {
326 if (add_code(cpu_buf
, is_kernel
))
328 cpu_buf
->last_is_kernel
= is_kernel
;
331 /* notice a task switch */
334 if (cpu_buf
->last_task
!= task
) {
335 if (add_code(cpu_buf
, (unsigned long)task
))
337 cpu_buf
->last_task
= task
;
341 fail
= fail
|| add_code(cpu_buf
, ibs_code
);
342 fail
= fail
|| add_sample(cpu_buf
, ibs_sample
[0], ibs_sample
[1]);
343 fail
= fail
|| add_sample(cpu_buf
, ibs_sample
[2], ibs_sample
[3]);
344 fail
= fail
|| add_sample(cpu_buf
, ibs_sample
[4], ibs_sample
[5]);
346 if (ibs_code
== IBS_OP_BEGIN
) {
347 fail
= fail
|| add_sample(cpu_buf
, ibs_sample
[6], ibs_sample
[7]);
348 fail
= fail
|| add_sample(cpu_buf
, ibs_sample
[8], ibs_sample
[9]);
349 fail
= fail
|| add_sample(cpu_buf
, ibs_sample
[10], ibs_sample
[11]);
355 if (oprofile_backtrace_depth
)
356 oprofile_ops
.backtrace(regs
, oprofile_backtrace_depth
);
361 cpu_buf
->sample_lost_overflow
++;
367 void oprofile_add_pc(unsigned long pc
, int is_kernel
, unsigned long event
)
369 struct oprofile_cpu_buffer
*cpu_buf
= &__get_cpu_var(cpu_buffer
);
370 log_sample(cpu_buf
, pc
, is_kernel
, event
);
373 void oprofile_add_trace(unsigned long pc
)
375 struct oprofile_cpu_buffer
*cpu_buf
= &__get_cpu_var(cpu_buffer
);
377 if (!cpu_buf
->tracing
)
381 * broken frame can give an eip with the same value as an
382 * escape code, abort the trace if we get it
384 if (pc
== ESCAPE_CODE
)
387 if (add_sample(cpu_buf
, pc
, 0))
392 cpu_buf
->tracing
= 0;
393 cpu_buf
->backtrace_aborted
++;
398 * This serves to avoid cpu buffer overflow, and makes sure
399 * the task mortuary progresses
401 * By using schedule_delayed_work_on and then schedule_delayed_work
402 * we guarantee this will stay on the correct cpu
404 static void wq_sync_buffer(struct work_struct
*work
)
406 struct oprofile_cpu_buffer
*b
=
407 container_of(work
, struct oprofile_cpu_buffer
, work
.work
);
408 if (b
->cpu
!= smp_processor_id()) {
409 printk(KERN_DEBUG
"WQ on CPU%d, prefer CPU%d\n",
410 smp_processor_id(), b
->cpu
);
412 if (!cpu_online(b
->cpu
)) {
413 cancel_delayed_work(&b
->work
);
419 /* don't re-add the work if we're shutting down */
421 schedule_delayed_work(&b
->work
, DEFAULT_TIMER_EXPIRE
);