aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/oprofile/cpu_buffer.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/oprofile/cpu_buffer.c')
-rw-r--r--drivers/oprofile/cpu_buffer.c393
1 files changed, 240 insertions, 153 deletions
diff --git a/drivers/oprofile/cpu_buffer.c b/drivers/oprofile/cpu_buffer.c
index 01d38e78cde1..2e03b6d796d3 100644
--- a/drivers/oprofile/cpu_buffer.c
+++ b/drivers/oprofile/cpu_buffer.c
@@ -1,11 +1,12 @@
1/** 1/**
2 * @file cpu_buffer.c 2 * @file cpu_buffer.c
3 * 3 *
4 * @remark Copyright 2002 OProfile authors 4 * @remark Copyright 2002-2009 OProfile authors
5 * @remark Read the file COPYING 5 * @remark Read the file COPYING
6 * 6 *
7 * @author John Levon <levon@movementarian.org> 7 * @author John Levon <levon@movementarian.org>
8 * @author Barry Kasindorf <barry.kasindorf@amd.com> 8 * @author Barry Kasindorf <barry.kasindorf@amd.com>
9 * @author Robert Richter <robert.richter@amd.com>
9 * 10 *
10 * Each CPU has a local buffer that stores PC value/event 11 * Each CPU has a local buffer that stores PC value/event
11 * pairs. We also log context switches when we notice them. 12 * pairs. We also log context switches when we notice them.
@@ -28,6 +29,25 @@
28#include "buffer_sync.h" 29#include "buffer_sync.h"
29#include "oprof.h" 30#include "oprof.h"
30 31
32#define OP_BUFFER_FLAGS 0
33
34/*
35 * Read and write access is using spin locking. Thus, writing to the
36 * buffer by NMI handler (x86) could occur also during critical
37 * sections when reading the buffer. To avoid this, there are 2
38 * buffers for independent read and write access. Read access is in
39 * process context only, write access only in the NMI handler. If the
40 * read buffer runs empty, both buffers are swapped atomically. There
41 * is potentially a small window during swapping where the buffers are
42 * disabled and samples could be lost.
43 *
44 * Using 2 buffers is a little bit overhead, but the solution is clear
45 * and does not require changes in the ring buffer implementation. It
46 * can be changed to a single buffer solution when the ring buffer
47 * access is implemented as non-locking atomic code.
48 */
49static struct ring_buffer *op_ring_buffer_read;
50static struct ring_buffer *op_ring_buffer_write;
31DEFINE_PER_CPU(struct oprofile_cpu_buffer, cpu_buffer); 51DEFINE_PER_CPU(struct oprofile_cpu_buffer, cpu_buffer);
32 52
33static void wq_sync_buffer(struct work_struct *work); 53static void wq_sync_buffer(struct work_struct *work);
@@ -35,19 +55,9 @@ static void wq_sync_buffer(struct work_struct *work);
35#define DEFAULT_TIMER_EXPIRE (HZ / 10) 55#define DEFAULT_TIMER_EXPIRE (HZ / 10)
36static int work_enabled; 56static int work_enabled;
37 57
38void free_cpu_buffers(void)
39{
40 int i;
41
42 for_each_possible_cpu(i) {
43 vfree(per_cpu(cpu_buffer, i).buffer);
44 per_cpu(cpu_buffer, i).buffer = NULL;
45 }
46}
47
48unsigned long oprofile_get_cpu_buffer_size(void) 58unsigned long oprofile_get_cpu_buffer_size(void)
49{ 59{
50 return fs_cpu_buffer_size; 60 return oprofile_cpu_buffer_size;
51} 61}
52 62
53void oprofile_cpu_buffer_inc_smpl_lost(void) 63void oprofile_cpu_buffer_inc_smpl_lost(void)
@@ -58,26 +68,36 @@ void oprofile_cpu_buffer_inc_smpl_lost(void)
58 cpu_buf->sample_lost_overflow++; 68 cpu_buf->sample_lost_overflow++;
59} 69}
60 70
71void free_cpu_buffers(void)
72{
73 if (op_ring_buffer_read)
74 ring_buffer_free(op_ring_buffer_read);
75 op_ring_buffer_read = NULL;
76 if (op_ring_buffer_write)
77 ring_buffer_free(op_ring_buffer_write);
78 op_ring_buffer_write = NULL;
79}
80
61int alloc_cpu_buffers(void) 81int alloc_cpu_buffers(void)
62{ 82{
63 int i; 83 int i;
64 84
65 unsigned long buffer_size = fs_cpu_buffer_size; 85 unsigned long buffer_size = oprofile_cpu_buffer_size;
86
87 op_ring_buffer_read = ring_buffer_alloc(buffer_size, OP_BUFFER_FLAGS);
88 if (!op_ring_buffer_read)
89 goto fail;
90 op_ring_buffer_write = ring_buffer_alloc(buffer_size, OP_BUFFER_FLAGS);
91 if (!op_ring_buffer_write)
92 goto fail;
66 93
67 for_each_possible_cpu(i) { 94 for_each_possible_cpu(i) {
68 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i); 95 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
69 96
70 b->buffer = vmalloc_node(sizeof(struct op_sample) * buffer_size,
71 cpu_to_node(i));
72 if (!b->buffer)
73 goto fail;
74
75 b->last_task = NULL; 97 b->last_task = NULL;
76 b->last_is_kernel = -1; 98 b->last_is_kernel = -1;
77 b->tracing = 0; 99 b->tracing = 0;
78 b->buffer_size = buffer_size; 100 b->buffer_size = buffer_size;
79 b->tail_pos = 0;
80 b->head_pos = 0;
81 b->sample_received = 0; 101 b->sample_received = 0;
82 b->sample_lost_overflow = 0; 102 b->sample_lost_overflow = 0;
83 b->backtrace_aborted = 0; 103 b->backtrace_aborted = 0;
@@ -124,73 +144,156 @@ void end_cpu_work(void)
124 flush_scheduled_work(); 144 flush_scheduled_work();
125} 145}
126 146
127/* Resets the cpu buffer to a sane state. */ 147/*
128void cpu_buffer_reset(struct oprofile_cpu_buffer *cpu_buf) 148 * This function prepares the cpu buffer to write a sample.
149 *
150 * Struct op_entry is used during operations on the ring buffer while
151 * struct op_sample contains the data that is stored in the ring
152 * buffer. Struct entry can be uninitialized. The function reserves a
153 * data array that is specified by size. Use
154 * op_cpu_buffer_write_commit() after preparing the sample. In case of
155 * errors a null pointer is returned, otherwise the pointer to the
156 * sample.
157 *
158 */
159struct op_sample
160*op_cpu_buffer_write_reserve(struct op_entry *entry, unsigned long size)
129{ 161{
130 /* reset these to invalid values; the next sample 162 entry->event = ring_buffer_lock_reserve
131 * collected will populate the buffer with proper 163 (op_ring_buffer_write, sizeof(struct op_sample) +
132 * values to initialize the buffer 164 size * sizeof(entry->sample->data[0]), &entry->irq_flags);
133 */ 165 if (entry->event)
134 cpu_buf->last_is_kernel = -1; 166 entry->sample = ring_buffer_event_data(entry->event);
135 cpu_buf->last_task = NULL; 167 else
168 entry->sample = NULL;
169
170 if (!entry->sample)
171 return NULL;
172
173 entry->size = size;
174 entry->data = entry->sample->data;
175
176 return entry->sample;
136} 177}
137 178
138/* compute number of available slots in cpu_buffer queue */ 179int op_cpu_buffer_write_commit(struct op_entry *entry)
139static unsigned long nr_available_slots(struct oprofile_cpu_buffer const *b)
140{ 180{
141 unsigned long head = b->head_pos; 181 return ring_buffer_unlock_commit(op_ring_buffer_write, entry->event,
142 unsigned long tail = b->tail_pos; 182 entry->irq_flags);
183}
143 184
144 if (tail > head) 185struct op_sample *op_cpu_buffer_read_entry(struct op_entry *entry, int cpu)
145 return (tail - head) - 1; 186{
187 struct ring_buffer_event *e;
188 e = ring_buffer_consume(op_ring_buffer_read, cpu, NULL);
189 if (e)
190 goto event;
191 if (ring_buffer_swap_cpu(op_ring_buffer_read,
192 op_ring_buffer_write,
193 cpu))
194 return NULL;
195 e = ring_buffer_consume(op_ring_buffer_read, cpu, NULL);
196 if (e)
197 goto event;
198 return NULL;
199
200event:
201 entry->event = e;
202 entry->sample = ring_buffer_event_data(e);
203 entry->size = (ring_buffer_event_length(e) - sizeof(struct op_sample))
204 / sizeof(entry->sample->data[0]);
205 entry->data = entry->sample->data;
206 return entry->sample;
207}
146 208
147 return tail + (b->buffer_size - head) - 1; 209unsigned long op_cpu_buffer_entries(int cpu)
210{
211 return ring_buffer_entries_cpu(op_ring_buffer_read, cpu)
212 + ring_buffer_entries_cpu(op_ring_buffer_write, cpu);
148} 213}
149 214
150static void increment_head(struct oprofile_cpu_buffer *b) 215static int
216op_add_code(struct oprofile_cpu_buffer *cpu_buf, unsigned long backtrace,
217 int is_kernel, struct task_struct *task)
151{ 218{
152 unsigned long new_head = b->head_pos + 1; 219 struct op_entry entry;
220 struct op_sample *sample;
221 unsigned long flags;
222 int size;
223
224 flags = 0;
153 225
154 /* Ensure anything written to the slot before we 226 if (backtrace)
155 * increment is visible */ 227 flags |= TRACE_BEGIN;
156 wmb(); 228
229 /* notice a switch from user->kernel or vice versa */
230 is_kernel = !!is_kernel;
231 if (cpu_buf->last_is_kernel != is_kernel) {
232 cpu_buf->last_is_kernel = is_kernel;
233 flags |= KERNEL_CTX_SWITCH;
234 if (is_kernel)
235 flags |= IS_KERNEL;
236 }
237
238 /* notice a task switch */
239 if (cpu_buf->last_task != task) {
240 cpu_buf->last_task = task;
241 flags |= USER_CTX_SWITCH;
242 }
243
244 if (!flags)
245 /* nothing to do */
246 return 0;
157 247
158 if (new_head < b->buffer_size) 248 if (flags & USER_CTX_SWITCH)
159 b->head_pos = new_head; 249 size = 1;
160 else 250 else
161 b->head_pos = 0; 251 size = 0;
162}
163 252
164static inline void 253 sample = op_cpu_buffer_write_reserve(&entry, size);
165add_sample(struct oprofile_cpu_buffer *cpu_buf, 254 if (!sample)
166 unsigned long pc, unsigned long event) 255 return -ENOMEM;
167{ 256
168 struct op_sample *entry = &cpu_buf->buffer[cpu_buf->head_pos]; 257 sample->eip = ESCAPE_CODE;
169 entry->eip = pc; 258 sample->event = flags;
170 entry->event = event; 259
171 increment_head(cpu_buf); 260 if (size)
261 op_cpu_buffer_add_data(&entry, (unsigned long)task);
262
263 op_cpu_buffer_write_commit(&entry);
264
265 return 0;
172} 266}
173 267
174static inline void 268static inline int
175add_code(struct oprofile_cpu_buffer *buffer, unsigned long value) 269op_add_sample(struct oprofile_cpu_buffer *cpu_buf,
270 unsigned long pc, unsigned long event)
176{ 271{
177 add_sample(buffer, ESCAPE_CODE, value); 272 struct op_entry entry;
273 struct op_sample *sample;
274
275 sample = op_cpu_buffer_write_reserve(&entry, 0);
276 if (!sample)
277 return -ENOMEM;
278
279 sample->eip = pc;
280 sample->event = event;
281
282 return op_cpu_buffer_write_commit(&entry);
178} 283}
179 284
180/* This must be safe from any context. It's safe writing here 285/*
181 * because of the head/tail separation of the writer and reader 286 * This must be safe from any context.
182 * of the CPU buffer.
183 * 287 *
184 * is_kernel is needed because on some architectures you cannot 288 * is_kernel is needed because on some architectures you cannot
185 * tell if you are in kernel or user space simply by looking at 289 * tell if you are in kernel or user space simply by looking at
186 * pc. We tag this in the buffer by generating kernel enter/exit 290 * pc. We tag this in the buffer by generating kernel enter/exit
187 * events whenever is_kernel changes 291 * events whenever is_kernel changes
188 */ 292 */
189static int log_sample(struct oprofile_cpu_buffer *cpu_buf, unsigned long pc, 293static int
190 int is_kernel, unsigned long event) 294log_sample(struct oprofile_cpu_buffer *cpu_buf, unsigned long pc,
295 unsigned long backtrace, int is_kernel, unsigned long event)
191{ 296{
192 struct task_struct *task;
193
194 cpu_buf->sample_received++; 297 cpu_buf->sample_received++;
195 298
196 if (pc == ESCAPE_CODE) { 299 if (pc == ESCAPE_CODE) {
@@ -198,131 +301,115 @@ static int log_sample(struct oprofile_cpu_buffer *cpu_buf, unsigned long pc,
198 return 0; 301 return 0;
199 } 302 }
200 303
201 if (nr_available_slots(cpu_buf) < 3) { 304 if (op_add_code(cpu_buf, backtrace, is_kernel, current))
202 cpu_buf->sample_lost_overflow++; 305 goto fail;
203 return 0;
204 }
205
206 is_kernel = !!is_kernel;
207 306
208 task = current; 307 if (op_add_sample(cpu_buf, pc, event))
308 goto fail;
209 309
210 /* notice a switch from user->kernel or vice versa */
211 if (cpu_buf->last_is_kernel != is_kernel) {
212 cpu_buf->last_is_kernel = is_kernel;
213 add_code(cpu_buf, is_kernel);
214 }
215
216 /* notice a task switch */
217 if (cpu_buf->last_task != task) {
218 cpu_buf->last_task = task;
219 add_code(cpu_buf, (unsigned long)task);
220 }
221
222 add_sample(cpu_buf, pc, event);
223 return 1; 310 return 1;
311
312fail:
313 cpu_buf->sample_lost_overflow++;
314 return 0;
224} 315}
225 316
226static int oprofile_begin_trace(struct oprofile_cpu_buffer *cpu_buf) 317static inline void oprofile_begin_trace(struct oprofile_cpu_buffer *cpu_buf)
227{ 318{
228 if (nr_available_slots(cpu_buf) < 4) {
229 cpu_buf->sample_lost_overflow++;
230 return 0;
231 }
232
233 add_code(cpu_buf, CPU_TRACE_BEGIN);
234 cpu_buf->tracing = 1; 319 cpu_buf->tracing = 1;
235 return 1;
236} 320}
237 321
238static void oprofile_end_trace(struct oprofile_cpu_buffer *cpu_buf) 322static inline void oprofile_end_trace(struct oprofile_cpu_buffer *cpu_buf)
239{ 323{
240 cpu_buf->tracing = 0; 324 cpu_buf->tracing = 0;
241} 325}
242 326
243void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs, 327static inline void
244 unsigned long event, int is_kernel) 328__oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
329 unsigned long event, int is_kernel)
245{ 330{
246 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer); 331 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
332 unsigned long backtrace = oprofile_backtrace_depth;
247 333
248 if (!backtrace_depth) { 334 /*
249 log_sample(cpu_buf, pc, is_kernel, event); 335 * if log_sample() fail we can't backtrace since we lost the
336 * source of this event
337 */
338 if (!log_sample(cpu_buf, pc, backtrace, is_kernel, event))
339 /* failed */
250 return; 340 return;
251 }
252 341
253 if (!oprofile_begin_trace(cpu_buf)) 342 if (!backtrace)
254 return; 343 return;
255 344
256 /* if log_sample() fail we can't backtrace since we lost the source 345 oprofile_begin_trace(cpu_buf);
257 * of this event */ 346 oprofile_ops.backtrace(regs, backtrace);
258 if (log_sample(cpu_buf, pc, is_kernel, event))
259 oprofile_ops.backtrace(regs, backtrace_depth);
260 oprofile_end_trace(cpu_buf); 347 oprofile_end_trace(cpu_buf);
261} 348}
262 349
350void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
351 unsigned long event, int is_kernel)
352{
353 __oprofile_add_ext_sample(pc, regs, event, is_kernel);
354}
355
263void oprofile_add_sample(struct pt_regs * const regs, unsigned long event) 356void oprofile_add_sample(struct pt_regs * const regs, unsigned long event)
264{ 357{
265 int is_kernel = !user_mode(regs); 358 int is_kernel = !user_mode(regs);
266 unsigned long pc = profile_pc(regs); 359 unsigned long pc = profile_pc(regs);
267 360
268 oprofile_add_ext_sample(pc, regs, event, is_kernel); 361 __oprofile_add_ext_sample(pc, regs, event, is_kernel);
269} 362}
270 363
271#ifdef CONFIG_OPROFILE_IBS 364/*
272 365 * Add samples with data to the ring buffer.
273#define MAX_IBS_SAMPLE_SIZE 14 366 *
274 367 * Use oprofile_add_data(&entry, val) to add data and
275void oprofile_add_ibs_sample(struct pt_regs *const regs, 368 * oprofile_write_commit(&entry) to commit the sample.
276 unsigned int *const ibs_sample, int ibs_code) 369 */
370void
371oprofile_write_reserve(struct op_entry *entry, struct pt_regs * const regs,
372 unsigned long pc, int code, int size)
277{ 373{
374 struct op_sample *sample;
278 int is_kernel = !user_mode(regs); 375 int is_kernel = !user_mode(regs);
279 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer); 376 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
280 struct task_struct *task;
281 377
282 cpu_buf->sample_received++; 378 cpu_buf->sample_received++;
283 379
284 if (nr_available_slots(cpu_buf) < MAX_IBS_SAMPLE_SIZE) { 380 /* no backtraces for samples with data */
285 /* we can't backtrace since we lost the source of this event */ 381 if (op_add_code(cpu_buf, 0, is_kernel, current))
286 cpu_buf->sample_lost_overflow++; 382 goto fail;
287 return;
288 }
289 383
290 /* notice a switch from user->kernel or vice versa */ 384 sample = op_cpu_buffer_write_reserve(entry, size + 2);
291 if (cpu_buf->last_is_kernel != is_kernel) { 385 if (!sample)
292 cpu_buf->last_is_kernel = is_kernel; 386 goto fail;
293 add_code(cpu_buf, is_kernel); 387 sample->eip = ESCAPE_CODE;
294 } 388 sample->event = 0; /* no flags */
295 389
296 /* notice a task switch */ 390 op_cpu_buffer_add_data(entry, code);
297 if (!is_kernel) { 391 op_cpu_buffer_add_data(entry, pc);
298 task = current;
299 if (cpu_buf->last_task != task) {
300 cpu_buf->last_task = task;
301 add_code(cpu_buf, (unsigned long)task);
302 }
303 }
304 392
305 add_code(cpu_buf, ibs_code); 393 return;
306 add_sample(cpu_buf, ibs_sample[0], ibs_sample[1]);
307 add_sample(cpu_buf, ibs_sample[2], ibs_sample[3]);
308 add_sample(cpu_buf, ibs_sample[4], ibs_sample[5]);
309 394
310 if (ibs_code == IBS_OP_BEGIN) { 395fail:
311 add_sample(cpu_buf, ibs_sample[6], ibs_sample[7]); 396 cpu_buf->sample_lost_overflow++;
312 add_sample(cpu_buf, ibs_sample[8], ibs_sample[9]); 397}
313 add_sample(cpu_buf, ibs_sample[10], ibs_sample[11]);
314 }
315 398
316 if (backtrace_depth) 399int oprofile_add_data(struct op_entry *entry, unsigned long val)
317 oprofile_ops.backtrace(regs, backtrace_depth); 400{
401 return op_cpu_buffer_add_data(entry, val);
318} 402}
319 403
320#endif 404int oprofile_write_commit(struct op_entry *entry)
405{
406 return op_cpu_buffer_write_commit(entry);
407}
321 408
322void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event) 409void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event)
323{ 410{
324 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer); 411 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
325 log_sample(cpu_buf, pc, is_kernel, event); 412 log_sample(cpu_buf, pc, 0, is_kernel, event);
326} 413}
327 414
328void oprofile_add_trace(unsigned long pc) 415void oprofile_add_trace(unsigned long pc)
@@ -332,21 +419,21 @@ void oprofile_add_trace(unsigned long pc)
332 if (!cpu_buf->tracing) 419 if (!cpu_buf->tracing)
333 return; 420 return;
334 421
335 if (nr_available_slots(cpu_buf) < 1) { 422 /*
336 cpu_buf->tracing = 0; 423 * broken frame can give an eip with the same value as an
337 cpu_buf->sample_lost_overflow++; 424 * escape code, abort the trace if we get it
338 return; 425 */
339 } 426 if (pc == ESCAPE_CODE)
427 goto fail;
340 428
341 /* broken frame can give an eip with the same value as an escape code, 429 if (op_add_sample(cpu_buf, pc, 0))
342 * abort the trace if we get it */ 430 goto fail;
343 if (pc == ESCAPE_CODE) {
344 cpu_buf->tracing = 0;
345 cpu_buf->backtrace_aborted++;
346 return;
347 }
348 431
349 add_sample(cpu_buf, pc, 0); 432 return;
433fail:
434 cpu_buf->tracing = 0;
435 cpu_buf->backtrace_aborted++;
436 return;
350} 437}
351 438
352/* 439/*