]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/oprofile/cpu_buffer.c
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_PER_CPU(struct oprofile_cpu_buffer
, cpu_buffer
);
33 static void wq_sync_buffer(struct work_struct
*work
);
35 #define DEFAULT_TIMER_EXPIRE (HZ / 10)
36 static int work_enabled
;
38 void free_cpu_buffers(void)
42 for_each_online_cpu(i
)
43 vfree(per_cpu(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
= &per_cpu(cpu_buffer
, i
);
55 b
->buffer
= vmalloc_node(sizeof(struct op_sample
) * buffer_size
,
61 b
->last_is_kernel
= -1;
63 b
->buffer_size
= buffer_size
;
66 b
->sample_received
= 0;
67 b
->sample_lost_overflow
= 0;
68 b
->backtrace_aborted
= 0;
69 b
->sample_invalid_eip
= 0;
71 INIT_DELAYED_WORK(&b
->work
, wq_sync_buffer
);
80 void start_cpu_work(void)
86 for_each_online_cpu(i
) {
87 struct oprofile_cpu_buffer
*b
= &per_cpu(cpu_buffer
, i
);
90 * Spread the work by 1 jiffy per cpu so they dont all
93 schedule_delayed_work_on(i
, &b
->work
, DEFAULT_TIMER_EXPIRE
+ i
);
97 void end_cpu_work(void)
103 for_each_online_cpu(i
) {
104 struct oprofile_cpu_buffer
*b
= &per_cpu(cpu_buffer
, i
);
106 cancel_delayed_work(&b
->work
);
109 flush_scheduled_work();
112 /* Resets the cpu buffer to a sane state. */
113 void cpu_buffer_reset(struct oprofile_cpu_buffer
* cpu_buf
)
115 /* reset these to invalid values; the next sample
116 * collected will populate the buffer with proper
117 * values to initialize the buffer
119 cpu_buf
->last_is_kernel
= -1;
120 cpu_buf
->last_task
= NULL
;
123 /* compute number of available slots in cpu_buffer queue */
124 static unsigned long nr_available_slots(struct oprofile_cpu_buffer
const * b
)
126 unsigned long head
= b
->head_pos
;
127 unsigned long tail
= b
->tail_pos
;
130 return (tail
- head
) - 1;
132 return tail
+ (b
->buffer_size
- head
) - 1;
135 static void increment_head(struct oprofile_cpu_buffer
* b
)
137 unsigned long new_head
= b
->head_pos
+ 1;
139 /* Ensure anything written to the slot before we
140 * increment is visible */
143 if (new_head
< b
->buffer_size
)
144 b
->head_pos
= new_head
;
150 add_sample(struct oprofile_cpu_buffer
* cpu_buf
,
151 unsigned long pc
, unsigned long event
)
153 struct op_sample
* entry
= &cpu_buf
->buffer
[cpu_buf
->head_pos
];
155 entry
->event
= event
;
156 increment_head(cpu_buf
);
160 add_code(struct oprofile_cpu_buffer
* buffer
, unsigned long value
)
162 add_sample(buffer
, ESCAPE_CODE
, value
);
165 /* This must be safe from any context. It's safe writing here
166 * because of the head/tail separation of the writer and reader
169 * is_kernel is needed because on some architectures you cannot
170 * tell if you are in kernel or user space simply by looking at
171 * pc. We tag this in the buffer by generating kernel enter/exit
172 * events whenever is_kernel changes
174 static int log_sample(struct oprofile_cpu_buffer
* cpu_buf
, unsigned long pc
,
175 int is_kernel
, unsigned long event
)
177 struct task_struct
* task
;
179 cpu_buf
->sample_received
++;
181 if (pc
== ESCAPE_CODE
) {
182 cpu_buf
->sample_invalid_eip
++;
186 if (nr_available_slots(cpu_buf
) < 3) {
187 cpu_buf
->sample_lost_overflow
++;
191 is_kernel
= !!is_kernel
;
195 /* notice a switch from user->kernel or vice versa */
196 if (cpu_buf
->last_is_kernel
!= is_kernel
) {
197 cpu_buf
->last_is_kernel
= is_kernel
;
198 add_code(cpu_buf
, is_kernel
);
201 /* notice a task switch */
202 if (cpu_buf
->last_task
!= task
) {
203 cpu_buf
->last_task
= task
;
204 add_code(cpu_buf
, (unsigned long)task
);
207 add_sample(cpu_buf
, pc
, event
);
211 static int oprofile_begin_trace(struct oprofile_cpu_buffer
*cpu_buf
)
213 if (nr_available_slots(cpu_buf
) < 4) {
214 cpu_buf
->sample_lost_overflow
++;
218 add_code(cpu_buf
, CPU_TRACE_BEGIN
);
219 cpu_buf
->tracing
= 1;
223 static void oprofile_end_trace(struct oprofile_cpu_buffer
* cpu_buf
)
225 cpu_buf
->tracing
= 0;
228 void oprofile_add_ext_sample(unsigned long pc
, struct pt_regs
* const regs
,
229 unsigned long event
, int is_kernel
)
231 struct oprofile_cpu_buffer
*cpu_buf
= &__get_cpu_var(cpu_buffer
);
233 if (!backtrace_depth
) {
234 log_sample(cpu_buf
, pc
, is_kernel
, event
);
238 if (!oprofile_begin_trace(cpu_buf
))
241 /* if log_sample() fail we can't backtrace since we lost the source
243 if (log_sample(cpu_buf
, pc
, is_kernel
, event
))
244 oprofile_ops
.backtrace(regs
, backtrace_depth
);
245 oprofile_end_trace(cpu_buf
);
248 void oprofile_add_sample(struct pt_regs
* const regs
, unsigned long event
)
250 int is_kernel
= !user_mode(regs
);
251 unsigned long pc
= profile_pc(regs
);
253 oprofile_add_ext_sample(pc
, regs
, event
, is_kernel
);
256 #define MAX_IBS_SAMPLE_SIZE 14
257 static int log_ibs_sample(struct oprofile_cpu_buffer
*cpu_buf
,
258 unsigned long pc
, int is_kernel
, unsigned int *ibs
, int ibs_code
)
260 struct task_struct
*task
;
262 cpu_buf
->sample_received
++;
264 if (nr_available_slots(cpu_buf
) < MAX_IBS_SAMPLE_SIZE
) {
265 cpu_buf
->sample_lost_overflow
++;
269 is_kernel
= !!is_kernel
;
271 /* notice a switch from user->kernel or vice versa */
272 if (cpu_buf
->last_is_kernel
!= is_kernel
) {
273 cpu_buf
->last_is_kernel
= is_kernel
;
274 add_code(cpu_buf
, is_kernel
);
277 /* notice a task switch */
281 if (cpu_buf
->last_task
!= task
) {
282 cpu_buf
->last_task
= task
;
283 add_code(cpu_buf
, (unsigned long)task
);
287 add_code(cpu_buf
, ibs_code
);
288 add_sample(cpu_buf
, ibs
[0], ibs
[1]);
289 add_sample(cpu_buf
, ibs
[2], ibs
[3]);
290 add_sample(cpu_buf
, ibs
[4], ibs
[5]);
292 if (ibs_code
== IBS_OP_BEGIN
) {
293 add_sample(cpu_buf
, ibs
[6], ibs
[7]);
294 add_sample(cpu_buf
, ibs
[8], ibs
[9]);
295 add_sample(cpu_buf
, ibs
[10], ibs
[11]);
301 void oprofile_add_ibs_sample(struct pt_regs
*const regs
,
302 unsigned int * const ibs_sample
, u8 code
)
304 int is_kernel
= !user_mode(regs
);
305 unsigned long pc
= profile_pc(regs
);
307 struct oprofile_cpu_buffer
*cpu_buf
=
308 &per_cpu(cpu_buffer
, smp_processor_id());
310 if (!backtrace_depth
) {
311 log_ibs_sample(cpu_buf
, pc
, is_kernel
, ibs_sample
, code
);
315 /* if log_sample() fails we can't backtrace since we lost the source
317 if (log_ibs_sample(cpu_buf
, pc
, is_kernel
, ibs_sample
, code
))
318 oprofile_ops
.backtrace(regs
, backtrace_depth
);
321 void oprofile_add_pc(unsigned long pc
, int is_kernel
, unsigned long event
)
323 struct oprofile_cpu_buffer
*cpu_buf
= &__get_cpu_var(cpu_buffer
);
324 log_sample(cpu_buf
, pc
, is_kernel
, event
);
327 void oprofile_add_trace(unsigned long pc
)
329 struct oprofile_cpu_buffer
*cpu_buf
= &__get_cpu_var(cpu_buffer
);
331 if (!cpu_buf
->tracing
)
334 if (nr_available_slots(cpu_buf
) < 1) {
335 cpu_buf
->tracing
= 0;
336 cpu_buf
->sample_lost_overflow
++;
340 /* broken frame can give an eip with the same value as an escape code,
341 * abort the trace if we get it */
342 if (pc
== ESCAPE_CODE
) {
343 cpu_buf
->tracing
= 0;
344 cpu_buf
->backtrace_aborted
++;
348 add_sample(cpu_buf
, pc
, 0);
352 * This serves to avoid cpu buffer overflow, and makes sure
353 * the task mortuary progresses
355 * By using schedule_delayed_work_on and then schedule_delayed_work
356 * we guarantee this will stay on the correct cpu
358 static void wq_sync_buffer(struct work_struct
*work
)
360 struct oprofile_cpu_buffer
* b
=
361 container_of(work
, struct oprofile_cpu_buffer
, work
.work
);
362 if (b
->cpu
!= smp_processor_id()) {
363 printk("WQ on CPU%d, prefer CPU%d\n",
364 smp_processor_id(), b
->cpu
);
368 /* don't re-add the work if we're shutting down */
370 schedule_delayed_work(&b
->work
, DEFAULT_TIMER_EXPIRE
);