blob: 1b6590746be4d0334685cf2b940263bfd12e5f96 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
2 * @file cpu_buffer.c
3 *
Robert Richter2cc28b92008-12-25 17:26:07 +01004 * @remark Copyright 2002-2009 OProfile authors
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * @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>
Robert Richter2cc28b92008-12-25 17:26:07 +01009 * @author Robert Richter <robert.richter@amd.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
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().
15 *
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.
20 */
21
22#include <linux/sched.h>
23#include <linux/oprofile.h>
24#include <linux/vmalloc.h>
25#include <linux/errno.h>
Robert Richter6a180372008-10-16 15:01:40 +020026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "event_buffer.h"
28#include "cpu_buffer.h"
29#include "buffer_sync.h"
30#include "oprof.h"
31
Robert Richter6dad8282008-12-09 01:21:32 +010032#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 */
Robert Richter99667182008-12-16 16:19:54 +010049static struct ring_buffer *op_ring_buffer_read;
50static struct ring_buffer *op_ring_buffer_write;
Eric Dumazet8b8b4982008-05-14 16:05:31 -070051DEFINE_PER_CPU(struct oprofile_cpu_buffer, cpu_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
David Howellsc4028952006-11-22 14:57:56 +000053static void wq_sync_buffer(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#define DEFAULT_TIMER_EXPIRE (HZ / 10)
56static int work_enabled;
57
Carl Lovea5598ca2008-10-14 23:37:01 +000058unsigned long oprofile_get_cpu_buffer_size(void)
59{
Robert Richterbd2172f2008-12-16 16:19:54 +010060 return oprofile_cpu_buffer_size;
Carl Lovea5598ca2008-10-14 23:37:01 +000061}
62
63void oprofile_cpu_buffer_inc_smpl_lost(void)
64{
65 struct oprofile_cpu_buffer *cpu_buf
66 = &__get_cpu_var(cpu_buffer);
67
68 cpu_buf->sample_lost_overflow++;
69}
70
Robert Richter30015772008-12-23 01:35:12 +010071void 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070081int alloc_cpu_buffers(void)
82{
83 int i;
Robert Richter6a180372008-10-16 15:01:40 +020084
Robert Richterbd2172f2008-12-16 16:19:54 +010085 unsigned long buffer_size = oprofile_cpu_buffer_size;
Robert Richter6a180372008-10-16 15:01:40 +020086
Robert Richter6dad8282008-12-09 01:21:32 +010087 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;
93
Chris J Arges4bd9b9d2008-10-15 11:03:39 -050094 for_each_possible_cpu(i) {
Mike Travis608dfdd2008-04-28 02:14:15 -070095 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
Robert Richter6a180372008-10-16 15:01:40 +020096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 b->last_task = NULL;
98 b->last_is_kernel = -1;
99 b->tracing = 0;
100 b->buffer_size = buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 b->sample_received = 0;
102 b->sample_lost_overflow = 0;
Philippe Eliedf9d1772007-11-14 16:58:48 -0800103 b->backtrace_aborted = 0;
104 b->sample_invalid_eip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 b->cpu = i;
David Howellsc4028952006-11-22 14:57:56 +0000106 INIT_DELAYED_WORK(&b->work, wq_sync_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108 return 0;
109
110fail:
111 free_cpu_buffers();
112 return -ENOMEM;
113}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115void start_cpu_work(void)
116{
117 int i;
118
119 work_enabled = 1;
120
121 for_each_online_cpu(i) {
Mike Travis608dfdd2008-04-28 02:14:15 -0700122 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 /*
125 * Spread the work by 1 jiffy per cpu so they dont all
126 * fire at once.
127 */
128 schedule_delayed_work_on(i, &b->work, DEFAULT_TIMER_EXPIRE + i);
129 }
130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132void end_cpu_work(void)
133{
134 int i;
135
136 work_enabled = 0;
137
138 for_each_online_cpu(i) {
Mike Travis608dfdd2008-04-28 02:14:15 -0700139 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 cancel_delayed_work(&b->work);
142 }
143
144 flush_scheduled_work();
145}
146
Robert Richter2cc28b92008-12-25 17:26:07 +0100147/*
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)
Robert Richter99667182008-12-16 16:19:54 +0100161{
Robert Richter2cc28b92008-12-25 17:26:07 +0100162 entry->event = ring_buffer_lock_reserve
163 (op_ring_buffer_write, sizeof(struct op_sample) +
164 size * sizeof(entry->sample->data[0]), &entry->irq_flags);
Robert Richter99667182008-12-16 16:19:54 +0100165 if (entry->event)
166 entry->sample = ring_buffer_event_data(entry->event);
167 else
168 entry->sample = NULL;
169
170 if (!entry->sample)
Robert Richter2cc28b92008-12-25 17:26:07 +0100171 return NULL;
Robert Richter99667182008-12-16 16:19:54 +0100172
Robert Richter2cc28b92008-12-25 17:26:07 +0100173 entry->size = size;
174 entry->data = entry->sample->data;
175
176 return entry->sample;
Robert Richter99667182008-12-16 16:19:54 +0100177}
178
179int op_cpu_buffer_write_commit(struct op_entry *entry)
180{
181 return ring_buffer_unlock_commit(op_ring_buffer_write, entry->event,
182 entry->irq_flags);
183}
184
Robert Richter2d87b142008-12-30 04:10:46 +0100185struct op_sample *op_cpu_buffer_read_entry(struct op_entry *entry, int cpu)
Robert Richter99667182008-12-16 16:19:54 +0100186{
187 struct ring_buffer_event *e;
188 e = ring_buffer_consume(op_ring_buffer_read, cpu, NULL);
189 if (e)
Robert Richter2d87b142008-12-30 04:10:46 +0100190 goto event;
Robert Richter99667182008-12-16 16:19:54 +0100191 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)
Robert Richter2d87b142008-12-30 04:10:46 +0100197 goto event;
Robert Richter99667182008-12-16 16:19:54 +0100198 return NULL;
Robert Richter2d87b142008-12-30 04:10:46 +0100199
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;
Robert Richter99667182008-12-16 16:19:54 +0100207}
208
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);
213}
214
Robert Richterae735e92008-12-25 17:26:07 +0100215static int
216op_add_code(struct oprofile_cpu_buffer *cpu_buf, unsigned long backtrace,
217 int is_kernel, struct task_struct *task)
218{
219 struct op_entry entry;
220 struct op_sample *sample;
221 unsigned long flags;
222 int size;
223
224 flags = 0;
225
226 if (backtrace)
227 flags |= TRACE_BEGIN;
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;
247
248 if (flags & USER_CTX_SWITCH)
249 size = 1;
250 else
251 size = 0;
252
253 sample = op_cpu_buffer_write_reserve(&entry, size);
254 if (!sample)
255 return -ENOMEM;
256
257 sample->eip = ESCAPE_CODE;
258 sample->event = flags;
259
260 if (size)
Robert Richterd9928c22008-12-25 17:26:07 +0100261 op_cpu_buffer_add_data(&entry, (unsigned long)task);
Robert Richterae735e92008-12-25 17:26:07 +0100262
263 op_cpu_buffer_write_commit(&entry);
264
265 return 0;
266}
267
Robert Richter211117f2008-12-09 02:13:25 +0100268static inline int
Robert Richterd0e23382008-12-23 04:03:05 +0100269op_add_sample(struct oprofile_cpu_buffer *cpu_buf,
270 unsigned long pc, unsigned long event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Robert Richter6dad8282008-12-09 01:21:32 +0100272 struct op_entry entry;
Robert Richter2cc28b92008-12-25 17:26:07 +0100273 struct op_sample *sample;
Robert Richter6dad8282008-12-09 01:21:32 +0100274
Robert Richter2cc28b92008-12-25 17:26:07 +0100275 sample = op_cpu_buffer_write_reserve(&entry, 0);
276 if (!sample)
277 return -ENOMEM;
Robert Richter6dad8282008-12-09 01:21:32 +0100278
Robert Richter2cc28b92008-12-25 17:26:07 +0100279 sample->eip = pc;
280 sample->event = event;
Robert Richter6dad8282008-12-09 01:21:32 +0100281
Robert Richter3967e932008-12-30 05:10:58 +0100282 return op_cpu_buffer_write_commit(&entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Robert Richterae735e92008-12-25 17:26:07 +0100285/*
286 * This must be safe from any context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 *
288 * is_kernel is needed because on some architectures you cannot
289 * tell if you are in kernel or user space simply by looking at
290 * pc. We tag this in the buffer by generating kernel enter/exit
291 * events whenever is_kernel changes
292 */
Robert Richterae735e92008-12-25 17:26:07 +0100293static int
294log_sample(struct oprofile_cpu_buffer *cpu_buf, unsigned long pc,
295 unsigned long backtrace, int is_kernel, unsigned long event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 cpu_buf->sample_received++;
298
Philippe Eliedf9d1772007-11-14 16:58:48 -0800299 if (pc == ESCAPE_CODE) {
300 cpu_buf->sample_invalid_eip++;
301 return 0;
302 }
303
Robert Richterae735e92008-12-25 17:26:07 +0100304 if (op_add_code(cpu_buf, backtrace, is_kernel, current))
305 goto fail;
Robert Richter6a180372008-10-16 15:01:40 +0200306
Robert Richterd0e23382008-12-23 04:03:05 +0100307 if (op_add_sample(cpu_buf, pc, event))
Robert Richter211117f2008-12-09 02:13:25 +0100308 goto fail;
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return 1;
Robert Richter211117f2008-12-09 02:13:25 +0100311
312fail:
313 cpu_buf->sample_lost_overflow++;
314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Robert Richter6352d922008-12-18 22:09:13 +0100317static inline void oprofile_begin_trace(struct oprofile_cpu_buffer *cpu_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 cpu_buf->tracing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Robert Richter6352d922008-12-18 22:09:13 +0100322static inline void oprofile_end_trace(struct oprofile_cpu_buffer *cpu_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 cpu_buf->tracing = 0;
325}
326
Robert Richterd45d23b2008-12-16 12:00:10 +0100327static inline void
328__oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
329 unsigned long event, int is_kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Mike Travis608dfdd2008-04-28 02:14:15 -0700331 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
Robert Richterae735e92008-12-25 17:26:07 +0100332 unsigned long backtrace = oprofile_backtrace_depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Robert Richterfd13f6c2008-10-19 21:00:09 +0200334 /*
335 * if log_sample() fail we can't backtrace since we lost the
336 * source of this event
337 */
Robert Richterae735e92008-12-25 17:26:07 +0100338 if (!log_sample(cpu_buf, pc, backtrace, is_kernel, event))
339 /* failed */
340 return;
Robert Richter6352d922008-12-18 22:09:13 +0100341
Robert Richterae735e92008-12-25 17:26:07 +0100342 if (!backtrace)
343 return;
344
345 oprofile_begin_trace(cpu_buf);
346 oprofile_ops.backtrace(regs, backtrace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 oprofile_end_trace(cpu_buf);
348}
349
Robert Richterd45d23b2008-12-16 12:00:10 +0100350void 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
Brian Rogan27357712006-03-28 01:56:20 -0800356void oprofile_add_sample(struct pt_regs * const regs, unsigned long event)
357{
358 int is_kernel = !user_mode(regs);
359 unsigned long pc = profile_pc(regs);
360
Robert Richterd45d23b2008-12-16 12:00:10 +0100361 __oprofile_add_ext_sample(pc, regs, event, is_kernel);
Brian Rogan27357712006-03-28 01:56:20 -0800362}
363
Robert Richter852402c2008-07-22 21:09:06 +0200364#ifdef CONFIG_OPROFILE_IBS
365
Robert Richtercdc18342008-09-26 22:18:44 -0400366void oprofile_add_ibs_sample(struct pt_regs * const regs,
367 unsigned int * const ibs_sample, int ibs_code)
Barry Kasindorf345c2572008-07-22 21:08:54 +0200368{
Robert Richtere2fee272008-07-18 17:36:20 +0200369 int is_kernel = !user_mode(regs);
370 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
Robert Richter211117f2008-12-09 02:13:25 +0100371 int fail = 0;
Barry Kasindorf345c2572008-07-22 21:08:54 +0200372
373 cpu_buf->sample_received++;
374
Robert Richterae735e92008-12-25 17:26:07 +0100375 /* backtraces disabled for ibs */
376 fail = fail || op_add_code(cpu_buf, 0, is_kernel, current);
Barry Kasindorf345c2572008-07-22 21:08:54 +0200377
Robert Richterae735e92008-12-25 17:26:07 +0100378 fail = fail || op_add_sample(cpu_buf, ESCAPE_CODE, ibs_code);
Robert Richterd0e23382008-12-23 04:03:05 +0100379 fail = fail || op_add_sample(cpu_buf, ibs_sample[0], ibs_sample[1]);
380 fail = fail || op_add_sample(cpu_buf, ibs_sample[2], ibs_sample[3]);
381 fail = fail || op_add_sample(cpu_buf, ibs_sample[4], ibs_sample[5]);
Barry Kasindorf345c2572008-07-22 21:08:54 +0200382
383 if (ibs_code == IBS_OP_BEGIN) {
Robert Richterd0e23382008-12-23 04:03:05 +0100384 fail = fail || op_add_sample(cpu_buf, ibs_sample[6], ibs_sample[7]);
385 fail = fail || op_add_sample(cpu_buf, ibs_sample[8], ibs_sample[9]);
386 fail = fail || op_add_sample(cpu_buf, ibs_sample[10], ibs_sample[11]);
Barry Kasindorf345c2572008-07-22 21:08:54 +0200387 }
388
Robert Richterae735e92008-12-25 17:26:07 +0100389 if (fail)
390 cpu_buf->sample_lost_overflow++;
Barry Kasindorf345c2572008-07-22 21:08:54 +0200391}
392
Robert Richter852402c2008-07-22 21:09:06 +0200393#endif
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event)
396{
Mike Travis608dfdd2008-04-28 02:14:15 -0700397 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
Robert Richterae735e92008-12-25 17:26:07 +0100398 log_sample(cpu_buf, pc, 0, is_kernel, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401void oprofile_add_trace(unsigned long pc)
402{
Mike Travis608dfdd2008-04-28 02:14:15 -0700403 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 if (!cpu_buf->tracing)
406 return;
407
Robert Richterfd13f6c2008-10-19 21:00:09 +0200408 /*
409 * broken frame can give an eip with the same value as an
410 * escape code, abort the trace if we get it
411 */
Robert Richter211117f2008-12-09 02:13:25 +0100412 if (pc == ESCAPE_CODE)
413 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Robert Richterd0e23382008-12-23 04:03:05 +0100415 if (op_add_sample(cpu_buf, pc, 0))
Robert Richter211117f2008-12-09 02:13:25 +0100416 goto fail;
417
418 return;
419fail:
420 cpu_buf->tracing = 0;
421 cpu_buf->backtrace_aborted++;
422 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425/*
426 * This serves to avoid cpu buffer overflow, and makes sure
427 * the task mortuary progresses
428 *
429 * By using schedule_delayed_work_on and then schedule_delayed_work
430 * we guarantee this will stay on the correct cpu
431 */
David Howellsc4028952006-11-22 14:57:56 +0000432static void wq_sync_buffer(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Robert Richter25ad29132008-09-05 17:12:36 +0200434 struct oprofile_cpu_buffer *b =
David Howellsc4028952006-11-22 14:57:56 +0000435 container_of(work, struct oprofile_cpu_buffer, work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (b->cpu != smp_processor_id()) {
Robert Richterbd17b622008-07-22 21:09:07 +0200437 printk(KERN_DEBUG "WQ on CPU%d, prefer CPU%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 smp_processor_id(), b->cpu);
Chris J Arges4bd9b9d2008-10-15 11:03:39 -0500439
440 if (!cpu_online(b->cpu)) {
441 cancel_delayed_work(&b->work);
442 return;
443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445 sync_buffer(b->cpu);
446
447 /* don't re-add the work if we're shutting down */
448 if (work_enabled)
449 schedule_delayed_work(&b->work, DEFAULT_TIMER_EXPIRE);
450}