blob: b47ce038490f1fd436c282f20e23ac8f745ab066 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
2 * @file cpu_buffer.c
3 *
4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
6 *
7 * @author John Levon <levon@movementarian.org>
Barry Kasindorf345c2572008-07-22 21:08:54 +02008 * @author Barry Kasindorf <barry.kasindorf@amd.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
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().
14 *
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.
19 */
20
21#include <linux/sched.h>
22#include <linux/oprofile.h>
23#include <linux/vmalloc.h>
24#include <linux/errno.h>
25
26#include "event_buffer.h"
27#include "cpu_buffer.h"
28#include "buffer_sync.h"
29#include "oprof.h"
30
Eric Dumazet8b8b4982008-05-14 16:05:31 -070031DEFINE_PER_CPU(struct oprofile_cpu_buffer, cpu_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
David Howellsc4028952006-11-22 14:57:56 +000033static void wq_sync_buffer(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#define DEFAULT_TIMER_EXPIRE (HZ / 10)
36static int work_enabled;
37
38void free_cpu_buffers(void)
39{
40 int i;
41
Carl Lovef4156d12008-08-11 17:25:43 +100042 for_each_online_cpu(i) {
Mike Travis608dfdd2008-04-28 02:14:15 -070043 vfree(per_cpu(cpu_buffer, i).buffer);
Carl Lovef4156d12008-08-11 17:25:43 +100044 per_cpu(cpu_buffer, i).buffer = NULL;
45 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
Jesper Juhl77933d72005-07-27 11:46:09 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048int alloc_cpu_buffers(void)
49{
50 int i;
51
52 unsigned long buffer_size = fs_cpu_buffer_size;
53
54 for_each_online_cpu(i) {
Mike Travis608dfdd2008-04-28 02:14:15 -070055 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Eric Dumazet25ab7cd2006-01-08 01:03:21 -080057 b->buffer = vmalloc_node(sizeof(struct op_sample) * buffer_size,
58 cpu_to_node(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (!b->buffer)
60 goto fail;
61
62 b->last_task = NULL;
63 b->last_is_kernel = -1;
64 b->tracing = 0;
65 b->buffer_size = buffer_size;
66 b->tail_pos = 0;
67 b->head_pos = 0;
68 b->sample_received = 0;
69 b->sample_lost_overflow = 0;
Philippe Eliedf9d1772007-11-14 16:58:48 -080070 b->backtrace_aborted = 0;
71 b->sample_invalid_eip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 b->cpu = i;
David Howellsc4028952006-11-22 14:57:56 +000073 INIT_DELAYED_WORK(&b->work, wq_sync_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75 return 0;
76
77fail:
78 free_cpu_buffers();
79 return -ENOMEM;
80}
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82void start_cpu_work(void)
83{
84 int i;
85
86 work_enabled = 1;
87
88 for_each_online_cpu(i) {
Mike Travis608dfdd2008-04-28 02:14:15 -070089 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 /*
92 * Spread the work by 1 jiffy per cpu so they dont all
93 * fire at once.
94 */
95 schedule_delayed_work_on(i, &b->work, DEFAULT_TIMER_EXPIRE + i);
96 }
97}
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099void end_cpu_work(void)
100{
101 int i;
102
103 work_enabled = 0;
104
105 for_each_online_cpu(i) {
Mike Travis608dfdd2008-04-28 02:14:15 -0700106 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 cancel_delayed_work(&b->work);
109 }
110
111 flush_scheduled_work();
112}
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/* Resets the cpu buffer to a sane state. */
Robert Richter25ad29132008-09-05 17:12:36 +0200115void cpu_buffer_reset(struct oprofile_cpu_buffer *cpu_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 /* reset these to invalid values; the next sample
118 * collected will populate the buffer with proper
119 * values to initialize the buffer
120 */
121 cpu_buf->last_is_kernel = -1;
122 cpu_buf->last_task = NULL;
123}
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/* compute number of available slots in cpu_buffer queue */
Robert Richter25ad29132008-09-05 17:12:36 +0200126static unsigned long nr_available_slots(struct oprofile_cpu_buffer const *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 unsigned long head = b->head_pos;
129 unsigned long tail = b->tail_pos;
130
131 if (tail > head)
132 return (tail - head) - 1;
133
134 return tail + (b->buffer_size - head) - 1;
135}
136
Robert Richter25ad29132008-09-05 17:12:36 +0200137static void increment_head(struct oprofile_cpu_buffer *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 unsigned long new_head = b->head_pos + 1;
140
141 /* Ensure anything written to the slot before we
142 * increment is visible */
143 wmb();
144
145 if (new_head < b->buffer_size)
146 b->head_pos = new_head;
147 else
148 b->head_pos = 0;
149}
150
Jesper Juhl77933d72005-07-27 11:46:09 -0700151static inline void
Robert Richter25ad29132008-09-05 17:12:36 +0200152add_sample(struct oprofile_cpu_buffer *cpu_buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 unsigned long pc, unsigned long event)
154{
Robert Richter25ad29132008-09-05 17:12:36 +0200155 struct op_sample *entry = &cpu_buf->buffer[cpu_buf->head_pos];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 entry->eip = pc;
157 entry->event = event;
158 increment_head(cpu_buf);
159}
160
Jesper Juhl77933d72005-07-27 11:46:09 -0700161static inline void
Robert Richter25ad29132008-09-05 17:12:36 +0200162add_code(struct oprofile_cpu_buffer *buffer, unsigned long value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 add_sample(buffer, ESCAPE_CODE, value);
165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/* This must be safe from any context. It's safe writing here
168 * because of the head/tail separation of the writer and reader
169 * of the CPU buffer.
170 *
171 * is_kernel is needed because on some architectures you cannot
172 * tell if you are in kernel or user space simply by looking at
173 * pc. We tag this in the buffer by generating kernel enter/exit
174 * events whenever is_kernel changes
175 */
Robert Richter25ad29132008-09-05 17:12:36 +0200176static int log_sample(struct oprofile_cpu_buffer *cpu_buf, unsigned long pc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 int is_kernel, unsigned long event)
178{
Robert Richter25ad29132008-09-05 17:12:36 +0200179 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 cpu_buf->sample_received++;
182
Philippe Eliedf9d1772007-11-14 16:58:48 -0800183 if (pc == ESCAPE_CODE) {
184 cpu_buf->sample_invalid_eip++;
185 return 0;
186 }
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (nr_available_slots(cpu_buf) < 3) {
189 cpu_buf->sample_lost_overflow++;
190 return 0;
191 }
192
193 is_kernel = !!is_kernel;
194
195 task = current;
196
197 /* notice a switch from user->kernel or vice versa */
198 if (cpu_buf->last_is_kernel != is_kernel) {
199 cpu_buf->last_is_kernel = is_kernel;
200 add_code(cpu_buf, is_kernel);
201 }
202
203 /* notice a task switch */
204 if (cpu_buf->last_task != task) {
205 cpu_buf->last_task = task;
206 add_code(cpu_buf, (unsigned long)task);
207 }
208
209 add_sample(cpu_buf, pc, event);
210 return 1;
211}
212
Barry Kasindorf345c2572008-07-22 21:08:54 +0200213static int oprofile_begin_trace(struct oprofile_cpu_buffer *cpu_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 if (nr_available_slots(cpu_buf) < 4) {
216 cpu_buf->sample_lost_overflow++;
217 return 0;
218 }
219
220 add_code(cpu_buf, CPU_TRACE_BEGIN);
221 cpu_buf->tracing = 1;
222 return 1;
223}
224
Robert Richter25ad29132008-09-05 17:12:36 +0200225static void oprofile_end_trace(struct oprofile_cpu_buffer *cpu_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 cpu_buf->tracing = 0;
228}
229
Brian Rogan27357712006-03-28 01:56:20 -0800230void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
231 unsigned long event, int is_kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Mike Travis608dfdd2008-04-28 02:14:15 -0700233 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 if (!backtrace_depth) {
236 log_sample(cpu_buf, pc, is_kernel, event);
237 return;
238 }
239
240 if (!oprofile_begin_trace(cpu_buf))
241 return;
242
243 /* if log_sample() fail we can't backtrace since we lost the source
244 * of this event */
245 if (log_sample(cpu_buf, pc, is_kernel, event))
246 oprofile_ops.backtrace(regs, backtrace_depth);
247 oprofile_end_trace(cpu_buf);
248}
249
Brian Rogan27357712006-03-28 01:56:20 -0800250void oprofile_add_sample(struct pt_regs * const regs, unsigned long event)
251{
252 int is_kernel = !user_mode(regs);
253 unsigned long pc = profile_pc(regs);
254
255 oprofile_add_ext_sample(pc, regs, event, is_kernel);
256}
257
Robert Richter852402c2008-07-22 21:09:06 +0200258#ifdef CONFIG_OPROFILE_IBS
259
Robert Richtere2fee272008-07-18 17:36:20 +0200260#define MAX_IBS_SAMPLE_SIZE 14
261
262void oprofile_add_ibs_sample(struct pt_regs *const regs,
Robert Richter25ad29132008-09-05 17:12:36 +0200263 unsigned int *const ibs_sample, int ibs_code)
Barry Kasindorf345c2572008-07-22 21:08:54 +0200264{
Robert Richtere2fee272008-07-18 17:36:20 +0200265 int is_kernel = !user_mode(regs);
266 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
Barry Kasindorf345c2572008-07-22 21:08:54 +0200267 struct task_struct *task;
268
269 cpu_buf->sample_received++;
270
271 if (nr_available_slots(cpu_buf) < MAX_IBS_SAMPLE_SIZE) {
Robert Richtere2fee272008-07-18 17:36:20 +0200272 /* we can't backtrace since we lost the source of this event */
Barry Kasindorf345c2572008-07-22 21:08:54 +0200273 cpu_buf->sample_lost_overflow++;
Robert Richtere2fee272008-07-18 17:36:20 +0200274 return;
Barry Kasindorf345c2572008-07-22 21:08:54 +0200275 }
276
Barry Kasindorf345c2572008-07-22 21:08:54 +0200277 /* notice a switch from user->kernel or vice versa */
278 if (cpu_buf->last_is_kernel != is_kernel) {
279 cpu_buf->last_is_kernel = is_kernel;
280 add_code(cpu_buf, is_kernel);
281 }
282
283 /* notice a task switch */
284 if (!is_kernel) {
285 task = current;
Barry Kasindorf345c2572008-07-22 21:08:54 +0200286 if (cpu_buf->last_task != task) {
287 cpu_buf->last_task = task;
288 add_code(cpu_buf, (unsigned long)task);
289 }
290 }
291
292 add_code(cpu_buf, ibs_code);
Robert Richtere2fee272008-07-18 17:36:20 +0200293 add_sample(cpu_buf, ibs_sample[0], ibs_sample[1]);
294 add_sample(cpu_buf, ibs_sample[2], ibs_sample[3]);
295 add_sample(cpu_buf, ibs_sample[4], ibs_sample[5]);
Barry Kasindorf345c2572008-07-22 21:08:54 +0200296
297 if (ibs_code == IBS_OP_BEGIN) {
Robert Richtere2fee272008-07-18 17:36:20 +0200298 add_sample(cpu_buf, ibs_sample[6], ibs_sample[7]);
299 add_sample(cpu_buf, ibs_sample[8], ibs_sample[9]);
300 add_sample(cpu_buf, ibs_sample[10], ibs_sample[11]);
Barry Kasindorf345c2572008-07-22 21:08:54 +0200301 }
302
Robert Richtere2fee272008-07-18 17:36:20 +0200303 if (backtrace_depth)
Barry Kasindorf345c2572008-07-22 21:08:54 +0200304 oprofile_ops.backtrace(regs, backtrace_depth);
305}
306
Robert Richter852402c2008-07-22 21:09:06 +0200307#endif
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event)
310{
Mike Travis608dfdd2008-04-28 02:14:15 -0700311 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 log_sample(cpu_buf, pc, is_kernel, event);
313}
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315void oprofile_add_trace(unsigned long pc)
316{
Mike Travis608dfdd2008-04-28 02:14:15 -0700317 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 if (!cpu_buf->tracing)
320 return;
321
322 if (nr_available_slots(cpu_buf) < 1) {
323 cpu_buf->tracing = 0;
324 cpu_buf->sample_lost_overflow++;
325 return;
326 }
327
328 /* broken frame can give an eip with the same value as an escape code,
329 * abort the trace if we get it */
330 if (pc == ESCAPE_CODE) {
331 cpu_buf->tracing = 0;
332 cpu_buf->backtrace_aborted++;
333 return;
334 }
335
336 add_sample(cpu_buf, pc, 0);
337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339/*
340 * This serves to avoid cpu buffer overflow, and makes sure
341 * the task mortuary progresses
342 *
343 * By using schedule_delayed_work_on and then schedule_delayed_work
344 * we guarantee this will stay on the correct cpu
345 */
David Howellsc4028952006-11-22 14:57:56 +0000346static void wq_sync_buffer(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Robert Richter25ad29132008-09-05 17:12:36 +0200348 struct oprofile_cpu_buffer *b =
David Howellsc4028952006-11-22 14:57:56 +0000349 container_of(work, struct oprofile_cpu_buffer, work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (b->cpu != smp_processor_id()) {
Robert Richterbd17b622008-07-22 21:09:07 +0200351 printk(KERN_DEBUG "WQ on CPU%d, prefer CPU%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 smp_processor_id(), b->cpu);
353 }
354 sync_buffer(b->cpu);
355
356 /* don't re-add the work if we're shutting down */
357 if (work_enabled)
358 schedule_delayed_work(&b->work, DEFAULT_TIMER_EXPIRE);
359}