blob: 5cf7efe38e672d5495f9410928fd2044ccfa7b93 [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>
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
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;
Carl Lovea5598ca2008-10-14 23:37:01 +000041
Chris J Arges4bd9b9d2008-10-15 11:03:39 -050042 for_each_possible_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
Carl Lovea5598ca2008-10-14 23:37:01 +000048unsigned long oprofile_get_cpu_buffer_size(void)
49{
50 return fs_cpu_buffer_size;
51}
52
53void 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070061int alloc_cpu_buffers(void)
62{
63 int i;
Robert Richter6a180372008-10-16 15:01:40 +020064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 unsigned long buffer_size = fs_cpu_buffer_size;
Robert Richter6a180372008-10-16 15:01:40 +020066
Chris J Arges4bd9b9d2008-10-15 11:03:39 -050067 for_each_possible_cpu(i) {
Mike Travis608dfdd2008-04-28 02:14:15 -070068 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
Robert Richter6a180372008-10-16 15:01:40 +020069
Eric Dumazet25ab7cd2006-01-08 01:03:21 -080070 b->buffer = vmalloc_node(sizeof(struct op_sample) * buffer_size,
71 cpu_to_node(i));
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (!b->buffer)
73 goto fail;
Robert Richter6a180372008-10-16 15:01:40 +020074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 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;
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) {
Mike Travis608dfdd2008-04-28 02:14:15 -0700102 struct oprofile_cpu_buffer *b = &per_cpu(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{
114 int i;
115
116 work_enabled = 0;
117
118 for_each_online_cpu(i) {
Mike Travis608dfdd2008-04-28 02:14:15 -0700119 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 cancel_delayed_work(&b->work);
122 }
123
124 flush_scheduled_work();
125}
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/* compute number of available slots in cpu_buffer queue */
Robert Richter25ad29132008-09-05 17:12:36 +0200128static unsigned long nr_available_slots(struct oprofile_cpu_buffer const *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 unsigned long head = b->head_pos;
131 unsigned long tail = b->tail_pos;
132
133 if (tail > head)
134 return (tail - head) - 1;
135
136 return tail + (b->buffer_size - head) - 1;
137}
138
Jesper Juhl77933d72005-07-27 11:46:09 -0700139static inline void
Robert Richter25ad29132008-09-05 17:12:36 +0200140add_sample(struct oprofile_cpu_buffer *cpu_buf,
Robert Richter6a180372008-10-16 15:01:40 +0200141 unsigned long pc, unsigned long event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Robert Richter7d468ab2008-11-27 10:57:09 +0100143 struct op_sample *entry = cpu_buffer_write_entry(cpu_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 entry->eip = pc;
145 entry->event = event;
Robert Richter229234a2008-11-27 18:36:08 +0100146 cpu_buffer_write_commit(cpu_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Jesper Juhl77933d72005-07-27 11:46:09 -0700149static inline void
Robert Richter25ad29132008-09-05 17:12:36 +0200150add_code(struct oprofile_cpu_buffer *buffer, unsigned long value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 add_sample(buffer, ESCAPE_CODE, value);
153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/* This must be safe from any context. It's safe writing here
156 * because of the head/tail separation of the writer and reader
157 * of the CPU buffer.
158 *
159 * is_kernel is needed because on some architectures you cannot
160 * tell if you are in kernel or user space simply by looking at
161 * pc. We tag this in the buffer by generating kernel enter/exit
162 * events whenever is_kernel changes
163 */
Robert Richter25ad29132008-09-05 17:12:36 +0200164static int log_sample(struct oprofile_cpu_buffer *cpu_buf, unsigned long pc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 int is_kernel, unsigned long event)
166{
Robert Richter25ad29132008-09-05 17:12:36 +0200167 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 cpu_buf->sample_received++;
170
Philippe Eliedf9d1772007-11-14 16:58:48 -0800171 if (pc == ESCAPE_CODE) {
172 cpu_buf->sample_invalid_eip++;
173 return 0;
174 }
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (nr_available_slots(cpu_buf) < 3) {
177 cpu_buf->sample_lost_overflow++;
178 return 0;
179 }
180
181 is_kernel = !!is_kernel;
182
183 task = current;
184
185 /* notice a switch from user->kernel or vice versa */
186 if (cpu_buf->last_is_kernel != is_kernel) {
187 cpu_buf->last_is_kernel = is_kernel;
188 add_code(cpu_buf, is_kernel);
189 }
190
191 /* notice a task switch */
192 if (cpu_buf->last_task != task) {
193 cpu_buf->last_task = task;
194 add_code(cpu_buf, (unsigned long)task);
195 }
Robert Richter6a180372008-10-16 15:01:40 +0200196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 add_sample(cpu_buf, pc, event);
198 return 1;
199}
200
Barry Kasindorf345c2572008-07-22 21:08:54 +0200201static int oprofile_begin_trace(struct oprofile_cpu_buffer *cpu_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 if (nr_available_slots(cpu_buf) < 4) {
204 cpu_buf->sample_lost_overflow++;
205 return 0;
206 }
207
208 add_code(cpu_buf, CPU_TRACE_BEGIN);
209 cpu_buf->tracing = 1;
210 return 1;
211}
212
Robert Richter25ad29132008-09-05 17:12:36 +0200213static void oprofile_end_trace(struct oprofile_cpu_buffer *cpu_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 cpu_buf->tracing = 0;
216}
217
Brian Rogan27357712006-03-28 01:56:20 -0800218void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
219 unsigned long event, int is_kernel)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Mike Travis608dfdd2008-04-28 02:14:15 -0700221 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 if (!backtrace_depth) {
224 log_sample(cpu_buf, pc, is_kernel, event);
225 return;
226 }
227
228 if (!oprofile_begin_trace(cpu_buf))
229 return;
230
Robert Richterfd13f6c2008-10-19 21:00:09 +0200231 /*
232 * if log_sample() fail we can't backtrace since we lost the
233 * source of this event
234 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (log_sample(cpu_buf, pc, is_kernel, event))
236 oprofile_ops.backtrace(regs, backtrace_depth);
237 oprofile_end_trace(cpu_buf);
238}
239
Brian Rogan27357712006-03-28 01:56:20 -0800240void oprofile_add_sample(struct pt_regs * const regs, unsigned long event)
241{
242 int is_kernel = !user_mode(regs);
243 unsigned long pc = profile_pc(regs);
244
245 oprofile_add_ext_sample(pc, regs, event, is_kernel);
246}
247
Robert Richter852402c2008-07-22 21:09:06 +0200248#ifdef CONFIG_OPROFILE_IBS
249
Robert Richtere2fee272008-07-18 17:36:20 +0200250#define MAX_IBS_SAMPLE_SIZE 14
251
Robert Richtercdc18342008-09-26 22:18:44 -0400252void oprofile_add_ibs_sample(struct pt_regs * const regs,
253 unsigned int * const ibs_sample, int ibs_code)
Barry Kasindorf345c2572008-07-22 21:08:54 +0200254{
Robert Richtere2fee272008-07-18 17:36:20 +0200255 int is_kernel = !user_mode(regs);
256 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
Barry Kasindorf345c2572008-07-22 21:08:54 +0200257 struct task_struct *task;
258
259 cpu_buf->sample_received++;
260
261 if (nr_available_slots(cpu_buf) < MAX_IBS_SAMPLE_SIZE) {
Robert Richtere2fee272008-07-18 17:36:20 +0200262 /* we can't backtrace since we lost the source of this event */
Barry Kasindorf345c2572008-07-22 21:08:54 +0200263 cpu_buf->sample_lost_overflow++;
Robert Richtere2fee272008-07-18 17:36:20 +0200264 return;
Barry Kasindorf345c2572008-07-22 21:08:54 +0200265 }
266
Barry Kasindorf345c2572008-07-22 21:08:54 +0200267 /* notice a switch from user->kernel or vice versa */
268 if (cpu_buf->last_is_kernel != is_kernel) {
269 cpu_buf->last_is_kernel = is_kernel;
270 add_code(cpu_buf, is_kernel);
271 }
272
273 /* notice a task switch */
274 if (!is_kernel) {
275 task = current;
Barry Kasindorf345c2572008-07-22 21:08:54 +0200276 if (cpu_buf->last_task != task) {
277 cpu_buf->last_task = task;
278 add_code(cpu_buf, (unsigned long)task);
279 }
280 }
281
282 add_code(cpu_buf, ibs_code);
Robert Richtere2fee272008-07-18 17:36:20 +0200283 add_sample(cpu_buf, ibs_sample[0], ibs_sample[1]);
284 add_sample(cpu_buf, ibs_sample[2], ibs_sample[3]);
285 add_sample(cpu_buf, ibs_sample[4], ibs_sample[5]);
Barry Kasindorf345c2572008-07-22 21:08:54 +0200286
287 if (ibs_code == IBS_OP_BEGIN) {
Robert Richtere2fee272008-07-18 17:36:20 +0200288 add_sample(cpu_buf, ibs_sample[6], ibs_sample[7]);
289 add_sample(cpu_buf, ibs_sample[8], ibs_sample[9]);
290 add_sample(cpu_buf, ibs_sample[10], ibs_sample[11]);
Barry Kasindorf345c2572008-07-22 21:08:54 +0200291 }
292
Robert Richtere2fee272008-07-18 17:36:20 +0200293 if (backtrace_depth)
Barry Kasindorf345c2572008-07-22 21:08:54 +0200294 oprofile_ops.backtrace(regs, backtrace_depth);
295}
296
Robert Richter852402c2008-07-22 21:09:06 +0200297#endif
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event)
300{
Mike Travis608dfdd2008-04-28 02:14:15 -0700301 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 log_sample(cpu_buf, pc, is_kernel, event);
303}
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305void oprofile_add_trace(unsigned long pc)
306{
Mike Travis608dfdd2008-04-28 02:14:15 -0700307 struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 if (!cpu_buf->tracing)
310 return;
311
312 if (nr_available_slots(cpu_buf) < 1) {
313 cpu_buf->tracing = 0;
314 cpu_buf->sample_lost_overflow++;
315 return;
316 }
317
Robert Richterfd13f6c2008-10-19 21:00:09 +0200318 /*
319 * broken frame can give an eip with the same value as an
320 * escape code, abort the trace if we get it
321 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (pc == ESCAPE_CODE) {
323 cpu_buf->tracing = 0;
324 cpu_buf->backtrace_aborted++;
325 return;
326 }
327
328 add_sample(cpu_buf, pc, 0);
329}
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331/*
332 * This serves to avoid cpu buffer overflow, and makes sure
333 * the task mortuary progresses
334 *
335 * By using schedule_delayed_work_on and then schedule_delayed_work
336 * we guarantee this will stay on the correct cpu
337 */
David Howellsc4028952006-11-22 14:57:56 +0000338static void wq_sync_buffer(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Robert Richter25ad29132008-09-05 17:12:36 +0200340 struct oprofile_cpu_buffer *b =
David Howellsc4028952006-11-22 14:57:56 +0000341 container_of(work, struct oprofile_cpu_buffer, work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (b->cpu != smp_processor_id()) {
Robert Richterbd17b622008-07-22 21:09:07 +0200343 printk(KERN_DEBUG "WQ on CPU%d, prefer CPU%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 smp_processor_id(), b->cpu);
Chris J Arges4bd9b9d2008-10-15 11:03:39 -0500345
346 if (!cpu_online(b->cpu)) {
347 cancel_delayed_work(&b->work);
348 return;
349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351 sync_buffer(b->cpu);
352
353 /* don't re-add the work if we're shutting down */
354 if (work_enabled)
355 schedule_delayed_work(&b->work, DEFAULT_TIMER_EXPIRE);
356}