oprofile: adding cpu_buffer_write_commit()
[linux-2.6] / drivers / oprofile / cpu_buffer.c
1 /**
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>
8  * @author Barry Kasindorf <barry.kasindorf@amd.com>
9  *
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
31 DEFINE_PER_CPU(struct oprofile_cpu_buffer, cpu_buffer);
32
33 static void wq_sync_buffer(struct work_struct *work);
34
35 #define DEFAULT_TIMER_EXPIRE (HZ / 10)
36 static int work_enabled;
37
38 void 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
48 unsigned long oprofile_get_cpu_buffer_size(void)
49 {
50         return fs_cpu_buffer_size;
51 }
52
53 void oprofile_cpu_buffer_inc_smpl_lost(void)
54 {
55         struct oprofile_cpu_buffer *cpu_buf
56                 = &__get_cpu_var(cpu_buffer);
57
58         cpu_buf->sample_lost_overflow++;
59 }
60
61 int alloc_cpu_buffers(void)
62 {
63         int i;
64
65         unsigned long buffer_size = fs_cpu_buffer_size;
66
67         for_each_possible_cpu(i) {
68                 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
69
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;
76                 b->last_is_kernel = -1;
77                 b->tracing = 0;
78                 b->buffer_size = buffer_size;
79                 b->tail_pos = 0;
80                 b->head_pos = 0;
81                 b->sample_received = 0;
82                 b->sample_lost_overflow = 0;
83                 b->backtrace_aborted = 0;
84                 b->sample_invalid_eip = 0;
85                 b->cpu = i;
86                 INIT_DELAYED_WORK(&b->work, wq_sync_buffer);
87         }
88         return 0;
89
90 fail:
91         free_cpu_buffers();
92         return -ENOMEM;
93 }
94
95 void start_cpu_work(void)
96 {
97         int i;
98
99         work_enabled = 1;
100
101         for_each_online_cpu(i) {
102                 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
103
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
112 void end_cpu_work(void)
113 {
114         int i;
115
116         work_enabled = 0;
117
118         for_each_online_cpu(i) {
119                 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
120
121                 cancel_delayed_work(&b->work);
122         }
123
124         flush_scheduled_work();
125 }
126
127 /* Resets the cpu buffer to a sane state. */
128 void cpu_buffer_reset(struct oprofile_cpu_buffer *cpu_buf)
129 {
130         /*
131          * reset these to invalid values; the next sample collected
132          * will populate the buffer with proper values to initialize
133          * the buffer
134          */
135         cpu_buf->last_is_kernel = -1;
136         cpu_buf->last_task = NULL;
137 }
138
139 /* compute number of available slots in cpu_buffer queue */
140 static unsigned long nr_available_slots(struct oprofile_cpu_buffer const *b)
141 {
142         unsigned long head = b->head_pos;
143         unsigned long tail = b->tail_pos;
144
145         if (tail > head)
146                 return (tail - head) - 1;
147
148         return tail + (b->buffer_size - head) - 1;
149 }
150
151 static inline void
152 add_sample(struct oprofile_cpu_buffer *cpu_buf,
153            unsigned long pc, unsigned long event)
154 {
155         struct op_sample *entry = cpu_buffer_write_entry(cpu_buf);
156         entry->eip = pc;
157         entry->event = event;
158         cpu_buffer_write_commit(cpu_buf);
159 }
160
161 static inline void
162 add_code(struct oprofile_cpu_buffer *buffer, unsigned long value)
163 {
164         add_sample(buffer, ESCAPE_CODE, value);
165 }
166
167 /* 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  */
176 static int log_sample(struct oprofile_cpu_buffer *cpu_buf, unsigned long pc,
177                       int is_kernel, unsigned long event)
178 {
179         struct task_struct *task;
180
181         cpu_buf->sample_received++;
182
183         if (pc == ESCAPE_CODE) {
184                 cpu_buf->sample_invalid_eip++;
185                 return 0;
186         }
187
188         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
213 static int oprofile_begin_trace(struct oprofile_cpu_buffer *cpu_buf)
214 {
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
225 static void oprofile_end_trace(struct oprofile_cpu_buffer *cpu_buf)
226 {
227         cpu_buf->tracing = 0;
228 }
229
230 void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
231                                 unsigned long event, int is_kernel)
232 {
233         struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
234
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         /*
244          * if log_sample() fail we can't backtrace since we lost the
245          * source of this event
246          */
247         if (log_sample(cpu_buf, pc, is_kernel, event))
248                 oprofile_ops.backtrace(regs, backtrace_depth);
249         oprofile_end_trace(cpu_buf);
250 }
251
252 void oprofile_add_sample(struct pt_regs * const regs, unsigned long event)
253 {
254         int is_kernel = !user_mode(regs);
255         unsigned long pc = profile_pc(regs);
256
257         oprofile_add_ext_sample(pc, regs, event, is_kernel);
258 }
259
260 #ifdef CONFIG_OPROFILE_IBS
261
262 #define MAX_IBS_SAMPLE_SIZE 14
263
264 void oprofile_add_ibs_sample(struct pt_regs * const regs,
265                              unsigned int * const ibs_sample, int ibs_code)
266 {
267         int is_kernel = !user_mode(regs);
268         struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
269         struct task_struct *task;
270
271         cpu_buf->sample_received++;
272
273         if (nr_available_slots(cpu_buf) < MAX_IBS_SAMPLE_SIZE) {
274                 /* we can't backtrace since we lost the source of this event */
275                 cpu_buf->sample_lost_overflow++;
276                 return;
277         }
278
279         /* notice a switch from user->kernel or vice versa */
280         if (cpu_buf->last_is_kernel != is_kernel) {
281                 cpu_buf->last_is_kernel = is_kernel;
282                 add_code(cpu_buf, is_kernel);
283         }
284
285         /* notice a task switch */
286         if (!is_kernel) {
287                 task = current;
288                 if (cpu_buf->last_task != task) {
289                         cpu_buf->last_task = task;
290                         add_code(cpu_buf, (unsigned long)task);
291                 }
292         }
293
294         add_code(cpu_buf, ibs_code);
295         add_sample(cpu_buf, ibs_sample[0], ibs_sample[1]);
296         add_sample(cpu_buf, ibs_sample[2], ibs_sample[3]);
297         add_sample(cpu_buf, ibs_sample[4], ibs_sample[5]);
298
299         if (ibs_code == IBS_OP_BEGIN) {
300                 add_sample(cpu_buf, ibs_sample[6], ibs_sample[7]);
301                 add_sample(cpu_buf, ibs_sample[8], ibs_sample[9]);
302                 add_sample(cpu_buf, ibs_sample[10], ibs_sample[11]);
303         }
304
305         if (backtrace_depth)
306                 oprofile_ops.backtrace(regs, backtrace_depth);
307 }
308
309 #endif
310
311 void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event)
312 {
313         struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
314         log_sample(cpu_buf, pc, is_kernel, event);
315 }
316
317 void oprofile_add_trace(unsigned long pc)
318 {
319         struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
320
321         if (!cpu_buf->tracing)
322                 return;
323
324         if (nr_available_slots(cpu_buf) < 1) {
325                 cpu_buf->tracing = 0;
326                 cpu_buf->sample_lost_overflow++;
327                 return;
328         }
329
330         /*
331          * broken frame can give an eip with the same value as an
332          * escape code, abort the trace if we get it
333          */
334         if (pc == ESCAPE_CODE) {
335                 cpu_buf->tracing = 0;
336                 cpu_buf->backtrace_aborted++;
337                 return;
338         }
339
340         add_sample(cpu_buf, pc, 0);
341 }
342
343 /*
344  * This serves to avoid cpu buffer overflow, and makes sure
345  * the task mortuary progresses
346  *
347  * By using schedule_delayed_work_on and then schedule_delayed_work
348  * we guarantee this will stay on the correct cpu
349  */
350 static void wq_sync_buffer(struct work_struct *work)
351 {
352         struct oprofile_cpu_buffer *b =
353                 container_of(work, struct oprofile_cpu_buffer, work.work);
354         if (b->cpu != smp_processor_id()) {
355                 printk(KERN_DEBUG "WQ on CPU%d, prefer CPU%d\n",
356                        smp_processor_id(), b->cpu);
357
358                 if (!cpu_online(b->cpu)) {
359                         cancel_delayed_work(&b->work);
360                         return;
361                 }
362         }
363         sync_buffer(b->cpu);
364
365         /* don't re-add the work if we're shutting down */
366         if (work_enabled)
367                 schedule_delayed_work(&b->work, DEFAULT_TIMER_EXPIRE);
368 }