blob: b8ef8ddcc2921b0f3cc91a53ab13cd0288885974 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/errno.h>
Robert Richter6a180372008-10-16 15:01:40 +020025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "event_buffer.h"
27#include "cpu_buffer.h"
28#include "buffer_sync.h"
29#include "oprof.h"
30
Robert Richter6dad8282008-12-09 01:21:32 +010031#define OP_BUFFER_FLAGS 0
32
Andi Kleencb6e9432010-04-01 03:17:25 +020033static struct ring_buffer *op_ring_buffer;
Tejun Heob3e9f672009-10-29 22:34:13 +090034DEFINE_PER_CPU(struct oprofile_cpu_buffer, op_cpu_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
David Howellsc4028952006-11-22 14:57:56 +000036static void wq_sync_buffer(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#define DEFAULT_TIMER_EXPIRE (HZ / 10)
39static int work_enabled;
40
Carl Lovea5598ca2008-10-14 23:37:01 +000041unsigned long oprofile_get_cpu_buffer_size(void)
42{
Robert Richterbd2172f2008-12-16 16:19:54 +010043 return oprofile_cpu_buffer_size;
Carl Lovea5598ca2008-10-14 23:37:01 +000044}
45
46void oprofile_cpu_buffer_inc_smpl_lost(void)
47{
Tejun Heob3e9f672009-10-29 22:34:13 +090048 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(op_cpu_buffer);
Carl Lovea5598ca2008-10-14 23:37:01 +000049
50 cpu_buf->sample_lost_overflow++;
51}
52
Robert Richter30015772008-12-23 01:35:12 +010053void free_cpu_buffers(void)
54{
Andi Kleencb6e9432010-04-01 03:17:25 +020055 if (op_ring_buffer)
56 ring_buffer_free(op_ring_buffer);
57 op_ring_buffer = NULL;
Robert Richter30015772008-12-23 01:35:12 +010058}
59
Robert Richter54f2c842009-05-07 17:28:59 +020060#define RB_EVENT_HDR_SIZE 4
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062int alloc_cpu_buffers(void)
63{
64 int i;
Robert Richter6a180372008-10-16 15:01:40 +020065
Robert Richterbd2172f2008-12-16 16:19:54 +010066 unsigned long buffer_size = oprofile_cpu_buffer_size;
Robert Richter54f2c842009-05-07 17:28:59 +020067 unsigned long byte_size = buffer_size * (sizeof(struct op_sample) +
68 RB_EVENT_HDR_SIZE);
Robert Richter6a180372008-10-16 15:01:40 +020069
Andi Kleencb6e9432010-04-01 03:17:25 +020070 op_ring_buffer = ring_buffer_alloc(byte_size, OP_BUFFER_FLAGS);
71 if (!op_ring_buffer)
Robert Richter6dad8282008-12-09 01:21:32 +010072 goto fail;
73
Chris J Arges4bd9b9d2008-10-15 11:03:39 -050074 for_each_possible_cpu(i) {
Tejun Heob3e9f672009-10-29 22:34:13 +090075 struct oprofile_cpu_buffer *b = &per_cpu(op_cpu_buffer, i);
Robert Richter6a180372008-10-16 15:01:40 +020076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 b->last_task = NULL;
78 b->last_is_kernel = -1;
79 b->tracing = 0;
80 b->buffer_size = buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 b->sample_received = 0;
82 b->sample_lost_overflow = 0;
Philippe Eliedf9d1772007-11-14 16:58:48 -080083 b->backtrace_aborted = 0;
84 b->sample_invalid_eip = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 b->cpu = i;
David Howellsc4028952006-11-22 14:57:56 +000086 INIT_DELAYED_WORK(&b->work, wq_sync_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
88 return 0;
89
90fail:
91 free_cpu_buffers();
92 return -ENOMEM;
93}
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95void start_cpu_work(void)
96{
97 int i;
98
99 work_enabled = 1;
100
101 for_each_online_cpu(i) {
Tejun Heob3e9f672009-10-29 22:34:13 +0900102 struct oprofile_cpu_buffer *b = &per_cpu(op_cpu_buffer, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 /*
105 * Spread the work by 1 jiffy per cpu so they dont all
106 * fire at once.
107 */
108 schedule_delayed_work_on(i, &b->work, DEFAULT_TIMER_EXPIRE + i);
109 }
110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112void end_cpu_work(void)
113{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 work_enabled = 0;
Tejun Heo3d7851b2010-10-15 09:51:08 -0400115}
116
117void flush_cpu_work(void)
118{
119 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 for_each_online_cpu(i) {
Tejun Heob3e9f672009-10-29 22:34:13 +0900122 struct oprofile_cpu_buffer *b = &per_cpu(op_cpu_buffer, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Tejun Heo3d7851b2010-10-15 09:51:08 -0400124 /* these works are per-cpu, no need for flush_sync */
125 flush_delayed_work(&b->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
Robert Richter2cc28b92008-12-25 17:26:07 +0100129/*
130 * This function prepares the cpu buffer to write a sample.
131 *
132 * Struct op_entry is used during operations on the ring buffer while
133 * struct op_sample contains the data that is stored in the ring
134 * buffer. Struct entry can be uninitialized. The function reserves a
135 * data array that is specified by size. Use
136 * op_cpu_buffer_write_commit() after preparing the sample. In case of
137 * errors a null pointer is returned, otherwise the pointer to the
138 * sample.
139 *
140 */
141struct op_sample
142*op_cpu_buffer_write_reserve(struct op_entry *entry, unsigned long size)
Robert Richter99667182008-12-16 16:19:54 +0100143{
Robert Richter2cc28b92008-12-25 17:26:07 +0100144 entry->event = ring_buffer_lock_reserve
Andi Kleencb6e9432010-04-01 03:17:25 +0200145 (op_ring_buffer, sizeof(struct op_sample) +
Ingo Molnar304cc6a2009-02-06 01:12:02 +0100146 size * sizeof(entry->sample->data[0]));
Andi Kleencb6e9432010-04-01 03:17:25 +0200147 if (!entry->event)
Robert Richter2cc28b92008-12-25 17:26:07 +0100148 return NULL;
Andi Kleencb6e9432010-04-01 03:17:25 +0200149 entry->sample = ring_buffer_event_data(entry->event);
Robert Richter2cc28b92008-12-25 17:26:07 +0100150 entry->size = size;
151 entry->data = entry->sample->data;
152
153 return entry->sample;
Robert Richter99667182008-12-16 16:19:54 +0100154}
155
156int op_cpu_buffer_write_commit(struct op_entry *entry)
157{
Andi Kleencb6e9432010-04-01 03:17:25 +0200158 return ring_buffer_unlock_commit(op_ring_buffer, entry->event);
Robert Richter99667182008-12-16 16:19:54 +0100159}
160
Robert Richter2d87b142008-12-30 04:10:46 +0100161struct op_sample *op_cpu_buffer_read_entry(struct op_entry *entry, int cpu)
Robert Richter99667182008-12-16 16:19:54 +0100162{
163 struct ring_buffer_event *e;
Robert Richterb971f062010-04-23 16:47:51 +0200164 e = ring_buffer_consume(op_ring_buffer, cpu, NULL, NULL);
Andi Kleencb6e9432010-04-01 03:17:25 +0200165 if (!e)
Robert Richter99667182008-12-16 16:19:54 +0100166 return NULL;
Robert Richter2d87b142008-12-30 04:10:46 +0100167
Robert Richter2d87b142008-12-30 04:10:46 +0100168 entry->event = e;
169 entry->sample = ring_buffer_event_data(e);
170 entry->size = (ring_buffer_event_length(e) - sizeof(struct op_sample))
171 / sizeof(entry->sample->data[0]);
172 entry->data = entry->sample->data;
173 return entry->sample;
Robert Richter99667182008-12-16 16:19:54 +0100174}
175
176unsigned long op_cpu_buffer_entries(int cpu)
177{
Andi Kleencb6e9432010-04-01 03:17:25 +0200178 return ring_buffer_entries_cpu(op_ring_buffer, cpu);
Robert Richter99667182008-12-16 16:19:54 +0100179}
180
Robert Richterae735e92008-12-25 17:26:07 +0100181static int
182op_add_code(struct oprofile_cpu_buffer *cpu_buf, unsigned long backtrace,
183 int is_kernel, struct task_struct *task)
184{
185 struct op_entry entry;
186 struct op_sample *sample;
187 unsigned long flags;
188 int size;
189
190 flags = 0;
191
192 if (backtrace)
193 flags |= TRACE_BEGIN;
194
195 /* notice a switch from user->kernel or vice versa */
196 is_kernel = !!is_kernel;
197 if (cpu_buf->last_is_kernel != is_kernel) {
198 cpu_buf->last_is_kernel = is_kernel;
199 flags |= KERNEL_CTX_SWITCH;
200 if (is_kernel)
201 flags |= IS_KERNEL;
202 }
203
204 /* notice a task switch */
205 if (cpu_buf->last_task != task) {
206 cpu_buf->last_task = task;
207 flags |= USER_CTX_SWITCH;
208 }
209
210 if (!flags)
211 /* nothing to do */
212 return 0;
213
214 if (flags & USER_CTX_SWITCH)
215 size = 1;
216 else
217 size = 0;
218
219 sample = op_cpu_buffer_write_reserve(&entry, size);
220 if (!sample)
221 return -ENOMEM;
222
223 sample->eip = ESCAPE_CODE;
224 sample->event = flags;
225
226 if (size)
Robert Richterd9928c22008-12-25 17:26:07 +0100227 op_cpu_buffer_add_data(&entry, (unsigned long)task);
Robert Richterae735e92008-12-25 17:26:07 +0100228
229 op_cpu_buffer_write_commit(&entry);
230
231 return 0;
232}
233
Robert Richter211117f2008-12-09 02:13:25 +0100234static inline int
Robert Richterd0e23382008-12-23 04:03:05 +0100235op_add_sample(struct oprofile_cpu_buffer *cpu_buf,
236 unsigned long pc, unsigned long event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Robert Richter6dad8282008-12-09 01:21:32 +0100238 struct op_entry entry;
Robert Richter2cc28b92008-12-25 17:26:07 +0100239 struct op_sample *sample;
Robert Richter6dad8282008-12-09 01:21:32 +0100240
Robert Richter2cc28b92008-12-25 17:26:07 +0100241 sample = op_cpu_buffer_write_reserve(&entry, 0);
242 if (!sample)
243 return -ENOMEM;
Robert Richter6dad8282008-12-09 01:21:32 +0100244
Robert Richter2cc28b92008-12-25 17:26:07 +0100245 sample->eip = pc;
246 sample->event = event;
Robert Richter6dad8282008-12-09 01:21:32 +0100247
Robert Richter3967e932008-12-30 05:10:58 +0100248 return op_cpu_buffer_write_commit(&entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
Robert Richterae735e92008-12-25 17:26:07 +0100251/*
252 * This must be safe from any context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 *
254 * is_kernel is needed because on some architectures you cannot
255 * tell if you are in kernel or user space simply by looking at
256 * pc. We tag this in the buffer by generating kernel enter/exit
257 * events whenever is_kernel changes
258 */
Robert Richterae735e92008-12-25 17:26:07 +0100259static int
260log_sample(struct oprofile_cpu_buffer *cpu_buf, unsigned long pc,
Heinz Graalfs54ebbe72011-01-21 10:06:54 +0000261 unsigned long backtrace, int is_kernel, unsigned long event,
262 struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Heinz Graalfs54ebbe72011-01-21 10:06:54 +0000264 struct task_struct *tsk = task ? task : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 cpu_buf->sample_received++;
266
Philippe Eliedf9d1772007-11-14 16:58:48 -0800267 if (pc == ESCAPE_CODE) {
268 cpu_buf->sample_invalid_eip++;
269 return 0;
270 }
271
Heinz Graalfs54ebbe72011-01-21 10:06:54 +0000272 if (op_add_code(cpu_buf, backtrace, is_kernel, tsk))
Robert Richterae735e92008-12-25 17:26:07 +0100273 goto fail;
Robert Richter6a180372008-10-16 15:01:40 +0200274
Robert Richterd0e23382008-12-23 04:03:05 +0100275 if (op_add_sample(cpu_buf, pc, event))
Robert Richter211117f2008-12-09 02:13:25 +0100276 goto fail;
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return 1;
Robert Richter211117f2008-12-09 02:13:25 +0100279
280fail:
281 cpu_buf->sample_lost_overflow++;
282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Robert Richter6352d922008-12-18 22:09:13 +0100285static inline void oprofile_begin_trace(struct oprofile_cpu_buffer *cpu_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 cpu_buf->tracing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
Robert Richter6352d922008-12-18 22:09:13 +0100290static inline void oprofile_end_trace(struct oprofile_cpu_buffer *cpu_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 cpu_buf->tracing = 0;
293}
294
Robert Richterd45d23b2008-12-16 12:00:10 +0100295static inline void
296__oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
Heinz Graalfs54ebbe72011-01-21 10:06:54 +0000297 unsigned long event, int is_kernel,
298 struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Tejun Heob3e9f672009-10-29 22:34:13 +0900300 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(op_cpu_buffer);
Robert Richterae735e92008-12-25 17:26:07 +0100301 unsigned long backtrace = oprofile_backtrace_depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Robert Richterfd13f6c2008-10-19 21:00:09 +0200303 /*
304 * if log_sample() fail we can't backtrace since we lost the
305 * source of this event
306 */
Heinz Graalfs54ebbe72011-01-21 10:06:54 +0000307 if (!log_sample(cpu_buf, pc, backtrace, is_kernel, event, task))
Robert Richterae735e92008-12-25 17:26:07 +0100308 /* failed */
309 return;
Robert Richter6352d922008-12-18 22:09:13 +0100310
Robert Richterae735e92008-12-25 17:26:07 +0100311 if (!backtrace)
312 return;
313
314 oprofile_begin_trace(cpu_buf);
315 oprofile_ops.backtrace(regs, backtrace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 oprofile_end_trace(cpu_buf);
317}
318
Heinz Graalfs54ebbe72011-01-21 10:06:54 +0000319void oprofile_add_ext_hw_sample(unsigned long pc, struct pt_regs * const regs,
320 unsigned long event, int is_kernel,
321 struct task_struct *task)
322{
323 __oprofile_add_ext_sample(pc, regs, event, is_kernel, task);
324}
325
Robert Richterd45d23b2008-12-16 12:00:10 +0100326void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
327 unsigned long event, int is_kernel)
328{
Heinz Graalfs54ebbe72011-01-21 10:06:54 +0000329 __oprofile_add_ext_sample(pc, regs, event, is_kernel, NULL);
Robert Richterd45d23b2008-12-16 12:00:10 +0100330}
331
Brian Rogan27357712006-03-28 01:56:20 -0800332void oprofile_add_sample(struct pt_regs * const regs, unsigned long event)
333{
Phil Carmody9414e992010-04-28 12:09:16 -0500334 int is_kernel;
335 unsigned long pc;
336
337 if (likely(regs)) {
338 is_kernel = !user_mode(regs);
339 pc = profile_pc(regs);
340 } else {
341 is_kernel = 0; /* This value will not be used */
342 pc = ESCAPE_CODE; /* as this causes an early return. */
343 }
Brian Rogan27357712006-03-28 01:56:20 -0800344
Heinz Graalfs54ebbe72011-01-21 10:06:54 +0000345 __oprofile_add_ext_sample(pc, regs, event, is_kernel, NULL);
Brian Rogan27357712006-03-28 01:56:20 -0800346}
347
Robert Richter1acda872009-01-05 10:35:31 +0100348/*
349 * Add samples with data to the ring buffer.
350 *
Robert Richter14f0ca82009-01-07 21:50:22 +0100351 * Use oprofile_add_data(&entry, val) to add data and
352 * oprofile_write_commit(&entry) to commit the sample.
Robert Richter1acda872009-01-05 10:35:31 +0100353 */
Robert Richter14f0ca82009-01-07 21:50:22 +0100354void
355oprofile_write_reserve(struct op_entry *entry, struct pt_regs * const regs,
Robert Richter1acda872009-01-05 10:35:31 +0100356 unsigned long pc, int code, int size)
Barry Kasindorf345c2572008-07-22 21:08:54 +0200357{
Robert Richter1acda872009-01-05 10:35:31 +0100358 struct op_sample *sample;
Robert Richtere2fee272008-07-18 17:36:20 +0200359 int is_kernel = !user_mode(regs);
Tejun Heob3e9f672009-10-29 22:34:13 +0900360 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(op_cpu_buffer);
Barry Kasindorf345c2572008-07-22 21:08:54 +0200361
362 cpu_buf->sample_received++;
363
Robert Richter1acda872009-01-05 10:35:31 +0100364 /* no backtraces for samples with data */
365 if (op_add_code(cpu_buf, 0, is_kernel, current))
366 goto fail;
Barry Kasindorf345c2572008-07-22 21:08:54 +0200367
Robert Richter1acda872009-01-05 10:35:31 +0100368 sample = op_cpu_buffer_write_reserve(entry, size + 2);
369 if (!sample)
370 goto fail;
371 sample->eip = ESCAPE_CODE;
372 sample->event = 0; /* no flags */
Barry Kasindorf345c2572008-07-22 21:08:54 +0200373
Robert Richter1acda872009-01-05 10:35:31 +0100374 op_cpu_buffer_add_data(entry, code);
375 op_cpu_buffer_add_data(entry, pc);
Barry Kasindorf345c2572008-07-22 21:08:54 +0200376
Robert Richter1acda872009-01-05 10:35:31 +0100377 return;
378
379fail:
Robert Richterfdb6a8f2009-01-17 17:13:27 +0100380 entry->event = NULL;
Robert Richter1acda872009-01-05 10:35:31 +0100381 cpu_buf->sample_lost_overflow++;
Barry Kasindorf345c2572008-07-22 21:08:54 +0200382}
383
Robert Richter14f0ca82009-01-07 21:50:22 +0100384int oprofile_add_data(struct op_entry *entry, unsigned long val)
385{
Robert Richterfdb6a8f2009-01-17 17:13:27 +0100386 if (!entry->event)
387 return 0;
Robert Richter14f0ca82009-01-07 21:50:22 +0100388 return op_cpu_buffer_add_data(entry, val);
389}
390
Robert Richter51563a02009-06-03 20:54:56 +0200391int oprofile_add_data64(struct op_entry *entry, u64 val)
392{
393 if (!entry->event)
394 return 0;
395 if (op_cpu_buffer_get_size(entry) < 2)
396 /*
397 * the function returns 0 to indicate a too small
398 * buffer, even if there is some space left
399 */
400 return 0;
401 if (!op_cpu_buffer_add_data(entry, (u32)val))
402 return 0;
403 return op_cpu_buffer_add_data(entry, (u32)(val >> 32));
404}
405
Robert Richter14f0ca82009-01-07 21:50:22 +0100406int oprofile_write_commit(struct op_entry *entry)
407{
Robert Richterfdb6a8f2009-01-17 17:13:27 +0100408 if (!entry->event)
409 return -EINVAL;
Robert Richter14f0ca82009-01-07 21:50:22 +0100410 return op_cpu_buffer_write_commit(entry);
411}
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event)
414{
Tejun Heob3e9f672009-10-29 22:34:13 +0900415 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(op_cpu_buffer);
Heinz Graalfs54ebbe72011-01-21 10:06:54 +0000416 log_sample(cpu_buf, pc, 0, is_kernel, event, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419void oprofile_add_trace(unsigned long pc)
420{
Tejun Heob3e9f672009-10-29 22:34:13 +0900421 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(op_cpu_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 if (!cpu_buf->tracing)
424 return;
425
Robert Richterfd13f6c2008-10-19 21:00:09 +0200426 /*
427 * broken frame can give an eip with the same value as an
428 * escape code, abort the trace if we get it
429 */
Robert Richter211117f2008-12-09 02:13:25 +0100430 if (pc == ESCAPE_CODE)
431 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Robert Richterd0e23382008-12-23 04:03:05 +0100433 if (op_add_sample(cpu_buf, pc, 0))
Robert Richter211117f2008-12-09 02:13:25 +0100434 goto fail;
435
436 return;
437fail:
438 cpu_buf->tracing = 0;
439 cpu_buf->backtrace_aborted++;
440 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443/*
444 * This serves to avoid cpu buffer overflow, and makes sure
445 * the task mortuary progresses
446 *
447 * By using schedule_delayed_work_on and then schedule_delayed_work
448 * we guarantee this will stay on the correct cpu
449 */
David Howellsc4028952006-11-22 14:57:56 +0000450static void wq_sync_buffer(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Robert Richter25ad2912008-09-05 17:12:36 +0200452 struct oprofile_cpu_buffer *b =
David Howellsc4028952006-11-22 14:57:56 +0000453 container_of(work, struct oprofile_cpu_buffer, work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (b->cpu != smp_processor_id()) {
Robert Richterbd17b622008-07-22 21:09:07 +0200455 printk(KERN_DEBUG "WQ on CPU%d, prefer CPU%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 smp_processor_id(), b->cpu);
Chris J Arges4bd9b9d2008-10-15 11:03:39 -0500457
458 if (!cpu_online(b->cpu)) {
459 cancel_delayed_work(&b->work);
460 return;
461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463 sync_buffer(b->cpu);
464
465 /* don't re-add the work if we're shutting down */
466 if (work_enabled)
467 schedule_delayed_work(&b->work, DEFAULT_TIMER_EXPIRE);
468}