Commit | Line | Data |
---|---|---|
1da177e4 LT |
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> | |
345c2573 | 8 | * @author Barry Kasindorf <barry.kasindorf@amd.com> |
1da177e4 LT |
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> | |
6a18037d | 25 | |
1da177e4 LT |
26 | #include "event_buffer.h" |
27 | #include "cpu_buffer.h" | |
28 | #include "buffer_sync.h" | |
29 | #include "oprof.h" | |
30 | ||
6dad828b RR |
31 | #define OP_BUFFER_FLAGS 0 |
32 | ||
33 | /* | |
34 | * Read and write access is using spin locking. Thus, writing to the | |
35 | * buffer by NMI handler (x86) could occur also during critical | |
36 | * sections when reading the buffer. To avoid this, there are 2 | |
37 | * buffers for independent read and write access. Read access is in | |
38 | * process context only, write access only in the NMI handler. If the | |
39 | * read buffer runs empty, both buffers are swapped atomically. There | |
40 | * is potentially a small window during swapping where the buffers are | |
41 | * disabled and samples could be lost. | |
42 | * | |
43 | * Using 2 buffers is a little bit overhead, but the solution is clear | |
44 | * and does not require changes in the ring buffer implementation. It | |
45 | * can be changed to a single buffer solution when the ring buffer | |
46 | * access is implemented as non-locking atomic code. | |
47 | */ | |
48 | struct ring_buffer *op_ring_buffer_read; | |
49 | struct ring_buffer *op_ring_buffer_write; | |
8b8b4988 | 50 | DEFINE_PER_CPU(struct oprofile_cpu_buffer, cpu_buffer); |
1da177e4 | 51 | |
c4028958 | 52 | static void wq_sync_buffer(struct work_struct *work); |
1da177e4 LT |
53 | |
54 | #define DEFAULT_TIMER_EXPIRE (HZ / 10) | |
55 | static int work_enabled; | |
56 | ||
57 | void free_cpu_buffers(void) | |
58 | { | |
6dad828b RR |
59 | if (op_ring_buffer_read) |
60 | ring_buffer_free(op_ring_buffer_read); | |
61 | op_ring_buffer_read = NULL; | |
62 | if (op_ring_buffer_write) | |
63 | ring_buffer_free(op_ring_buffer_write); | |
64 | op_ring_buffer_write = NULL; | |
1da177e4 | 65 | } |
77933d72 | 66 | |
a5598ca0 CL |
67 | unsigned long oprofile_get_cpu_buffer_size(void) |
68 | { | |
69 | return fs_cpu_buffer_size; | |
70 | } | |
71 | ||
72 | void oprofile_cpu_buffer_inc_smpl_lost(void) | |
73 | { | |
74 | struct oprofile_cpu_buffer *cpu_buf | |
75 | = &__get_cpu_var(cpu_buffer); | |
76 | ||
77 | cpu_buf->sample_lost_overflow++; | |
78 | } | |
79 | ||
1da177e4 LT |
80 | int alloc_cpu_buffers(void) |
81 | { | |
82 | int i; | |
6a18037d | 83 | |
1da177e4 | 84 | unsigned long buffer_size = fs_cpu_buffer_size; |
6a18037d | 85 | |
6dad828b RR |
86 | op_ring_buffer_read = ring_buffer_alloc(buffer_size, OP_BUFFER_FLAGS); |
87 | if (!op_ring_buffer_read) | |
88 | goto fail; | |
89 | op_ring_buffer_write = ring_buffer_alloc(buffer_size, OP_BUFFER_FLAGS); | |
90 | if (!op_ring_buffer_write) | |
91 | goto fail; | |
92 | ||
4bd9b9dc | 93 | for_each_possible_cpu(i) { |
608dfddd | 94 | struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i); |
6a18037d | 95 | |
1da177e4 LT |
96 | b->last_task = NULL; |
97 | b->last_is_kernel = -1; | |
98 | b->tracing = 0; | |
99 | b->buffer_size = buffer_size; | |
100 | b->tail_pos = 0; | |
101 | b->head_pos = 0; | |
102 | b->sample_received = 0; | |
103 | b->sample_lost_overflow = 0; | |
df9d177a PE |
104 | b->backtrace_aborted = 0; |
105 | b->sample_invalid_eip = 0; | |
1da177e4 | 106 | b->cpu = i; |
c4028958 | 107 | INIT_DELAYED_WORK(&b->work, wq_sync_buffer); |
1da177e4 LT |
108 | } |
109 | return 0; | |
110 | ||
111 | fail: | |
112 | free_cpu_buffers(); | |
113 | return -ENOMEM; | |
114 | } | |
1da177e4 LT |
115 | |
116 | void start_cpu_work(void) | |
117 | { | |
118 | int i; | |
119 | ||
120 | work_enabled = 1; | |
121 | ||
122 | for_each_online_cpu(i) { | |
608dfddd | 123 | struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i); |
1da177e4 LT |
124 | |
125 | /* | |
126 | * Spread the work by 1 jiffy per cpu so they dont all | |
127 | * fire at once. | |
128 | */ | |
129 | schedule_delayed_work_on(i, &b->work, DEFAULT_TIMER_EXPIRE + i); | |
130 | } | |
131 | } | |
132 | ||
1da177e4 LT |
133 | void end_cpu_work(void) |
134 | { | |
135 | int i; | |
136 | ||
137 | work_enabled = 0; | |
138 | ||
139 | for_each_online_cpu(i) { | |
608dfddd | 140 | struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i); |
1da177e4 LT |
141 | |
142 | cancel_delayed_work(&b->work); | |
143 | } | |
144 | ||
145 | flush_scheduled_work(); | |
146 | } | |
147 | ||
77933d72 | 148 | static inline void |
25ad2913 | 149 | add_sample(struct oprofile_cpu_buffer *cpu_buf, |
6a18037d | 150 | unsigned long pc, unsigned long event) |
1da177e4 | 151 | { |
6dad828b RR |
152 | struct op_entry entry; |
153 | ||
154 | if (cpu_buffer_write_entry(&entry)) | |
155 | goto Error; | |
156 | ||
157 | entry.sample->eip = pc; | |
158 | entry.sample->event = event; | |
159 | ||
160 | if (cpu_buffer_write_commit(&entry)) | |
161 | goto Error; | |
162 | ||
163 | return; | |
164 | ||
165 | Error: | |
166 | cpu_buf->sample_lost_overflow++; | |
167 | return; | |
1da177e4 LT |
168 | } |
169 | ||
77933d72 | 170 | static inline void |
25ad2913 | 171 | add_code(struct oprofile_cpu_buffer *buffer, unsigned long value) |
1da177e4 LT |
172 | { |
173 | add_sample(buffer, ESCAPE_CODE, value); | |
174 | } | |
175 | ||
1da177e4 LT |
176 | /* This must be safe from any context. It's safe writing here |
177 | * because of the head/tail separation of the writer and reader | |
178 | * of the CPU buffer. | |
179 | * | |
180 | * is_kernel is needed because on some architectures you cannot | |
181 | * tell if you are in kernel or user space simply by looking at | |
182 | * pc. We tag this in the buffer by generating kernel enter/exit | |
183 | * events whenever is_kernel changes | |
184 | */ | |
25ad2913 | 185 | static int log_sample(struct oprofile_cpu_buffer *cpu_buf, unsigned long pc, |
1da177e4 LT |
186 | int is_kernel, unsigned long event) |
187 | { | |
25ad2913 | 188 | struct task_struct *task; |
1da177e4 LT |
189 | |
190 | cpu_buf->sample_received++; | |
191 | ||
df9d177a PE |
192 | if (pc == ESCAPE_CODE) { |
193 | cpu_buf->sample_invalid_eip++; | |
194 | return 0; | |
195 | } | |
196 | ||
1da177e4 LT |
197 | is_kernel = !!is_kernel; |
198 | ||
199 | task = current; | |
200 | ||
201 | /* notice a switch from user->kernel or vice versa */ | |
202 | if (cpu_buf->last_is_kernel != is_kernel) { | |
203 | cpu_buf->last_is_kernel = is_kernel; | |
204 | add_code(cpu_buf, is_kernel); | |
205 | } | |
206 | ||
207 | /* notice a task switch */ | |
208 | if (cpu_buf->last_task != task) { | |
209 | cpu_buf->last_task = task; | |
210 | add_code(cpu_buf, (unsigned long)task); | |
211 | } | |
6a18037d | 212 | |
1da177e4 LT |
213 | add_sample(cpu_buf, pc, event); |
214 | return 1; | |
215 | } | |
216 | ||
345c2573 | 217 | static int oprofile_begin_trace(struct oprofile_cpu_buffer *cpu_buf) |
1da177e4 | 218 | { |
1da177e4 LT |
219 | add_code(cpu_buf, CPU_TRACE_BEGIN); |
220 | cpu_buf->tracing = 1; | |
221 | return 1; | |
222 | } | |
223 | ||
25ad2913 | 224 | static void oprofile_end_trace(struct oprofile_cpu_buffer *cpu_buf) |
1da177e4 LT |
225 | { |
226 | cpu_buf->tracing = 0; | |
227 | } | |
228 | ||
27357716 BR |
229 | void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs, |
230 | unsigned long event, int is_kernel) | |
1da177e4 | 231 | { |
608dfddd | 232 | struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer); |
1da177e4 LT |
233 | |
234 | if (!backtrace_depth) { | |
235 | log_sample(cpu_buf, pc, is_kernel, event); | |
236 | return; | |
237 | } | |
238 | ||
239 | if (!oprofile_begin_trace(cpu_buf)) | |
240 | return; | |
241 | ||
fd13f6c8 RR |
242 | /* |
243 | * if log_sample() fail we can't backtrace since we lost the | |
244 | * source of this event | |
245 | */ | |
1da177e4 LT |
246 | if (log_sample(cpu_buf, pc, is_kernel, event)) |
247 | oprofile_ops.backtrace(regs, backtrace_depth); | |
248 | oprofile_end_trace(cpu_buf); | |
249 | } | |
250 | ||
27357716 BR |
251 | void oprofile_add_sample(struct pt_regs * const regs, unsigned long event) |
252 | { | |
253 | int is_kernel = !user_mode(regs); | |
254 | unsigned long pc = profile_pc(regs); | |
255 | ||
256 | oprofile_add_ext_sample(pc, regs, event, is_kernel); | |
257 | } | |
258 | ||
852402cc RR |
259 | #ifdef CONFIG_OPROFILE_IBS |
260 | ||
e2fee276 RR |
261 | #define MAX_IBS_SAMPLE_SIZE 14 |
262 | ||
cdc1834d RR |
263 | void oprofile_add_ibs_sample(struct pt_regs * const regs, |
264 | unsigned int * const ibs_sample, int ibs_code) | |
345c2573 | 265 | { |
e2fee276 RR |
266 | int is_kernel = !user_mode(regs); |
267 | struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer); | |
345c2573 BK |
268 | struct task_struct *task; |
269 | ||
270 | cpu_buf->sample_received++; | |
271 | ||
345c2573 BK |
272 | /* notice a switch from user->kernel or vice versa */ |
273 | if (cpu_buf->last_is_kernel != is_kernel) { | |
274 | cpu_buf->last_is_kernel = is_kernel; | |
275 | add_code(cpu_buf, is_kernel); | |
276 | } | |
277 | ||
278 | /* notice a task switch */ | |
279 | if (!is_kernel) { | |
280 | task = current; | |
345c2573 BK |
281 | if (cpu_buf->last_task != task) { |
282 | cpu_buf->last_task = task; | |
283 | add_code(cpu_buf, (unsigned long)task); | |
284 | } | |
285 | } | |
286 | ||
287 | add_code(cpu_buf, ibs_code); | |
e2fee276 RR |
288 | add_sample(cpu_buf, ibs_sample[0], ibs_sample[1]); |
289 | add_sample(cpu_buf, ibs_sample[2], ibs_sample[3]); | |
290 | add_sample(cpu_buf, ibs_sample[4], ibs_sample[5]); | |
345c2573 BK |
291 | |
292 | if (ibs_code == IBS_OP_BEGIN) { | |
e2fee276 RR |
293 | add_sample(cpu_buf, ibs_sample[6], ibs_sample[7]); |
294 | add_sample(cpu_buf, ibs_sample[8], ibs_sample[9]); | |
295 | add_sample(cpu_buf, ibs_sample[10], ibs_sample[11]); | |
345c2573 BK |
296 | } |
297 | ||
e2fee276 | 298 | if (backtrace_depth) |
345c2573 BK |
299 | oprofile_ops.backtrace(regs, backtrace_depth); |
300 | } | |
301 | ||
852402cc RR |
302 | #endif |
303 | ||
1da177e4 LT |
304 | void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event) |
305 | { | |
608dfddd | 306 | struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer); |
1da177e4 LT |
307 | log_sample(cpu_buf, pc, is_kernel, event); |
308 | } | |
309 | ||
1da177e4 LT |
310 | void oprofile_add_trace(unsigned long pc) |
311 | { | |
608dfddd | 312 | struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer); |
1da177e4 LT |
313 | |
314 | if (!cpu_buf->tracing) | |
315 | return; | |
316 | ||
fd13f6c8 RR |
317 | /* |
318 | * broken frame can give an eip with the same value as an | |
319 | * escape code, abort the trace if we get it | |
320 | */ | |
1da177e4 LT |
321 | if (pc == ESCAPE_CODE) { |
322 | cpu_buf->tracing = 0; | |
323 | cpu_buf->backtrace_aborted++; | |
324 | return; | |
325 | } | |
326 | ||
327 | add_sample(cpu_buf, pc, 0); | |
328 | } | |
329 | ||
1da177e4 LT |
330 | /* |
331 | * This serves to avoid cpu buffer overflow, and makes sure | |
332 | * the task mortuary progresses | |
333 | * | |
334 | * By using schedule_delayed_work_on and then schedule_delayed_work | |
335 | * we guarantee this will stay on the correct cpu | |
336 | */ | |
c4028958 | 337 | static void wq_sync_buffer(struct work_struct *work) |
1da177e4 | 338 | { |
25ad2913 | 339 | struct oprofile_cpu_buffer *b = |
c4028958 | 340 | container_of(work, struct oprofile_cpu_buffer, work.work); |
1da177e4 | 341 | if (b->cpu != smp_processor_id()) { |
bd17b625 | 342 | printk(KERN_DEBUG "WQ on CPU%d, prefer CPU%d\n", |
1da177e4 | 343 | smp_processor_id(), b->cpu); |
4bd9b9dc CA |
344 | |
345 | if (!cpu_online(b->cpu)) { | |
346 | cancel_delayed_work(&b->work); | |
347 | return; | |
348 | } | |
1da177e4 LT |
349 | } |
350 | sync_buffer(b->cpu); | |
351 | ||
352 | /* don't re-add the work if we're shutting down */ | |
353 | if (work_enabled) | |
354 | schedule_delayed_work(&b->work, DEFAULT_TIMER_EXPIRE); | |
355 | } |