ftrace: printk and trace irqsoff and wakeups
[safe/jmp/linux-2.6] / kernel / trace / trace_irqsoff.c
1 /*
2  * trace irqs off criticall timings
3  *
4  * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5  * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6  *
7  * From code in the latency_tracer, that is:
8  *
9  *  Copyright (C) 2004-2006 Ingo Molnar
10  *  Copyright (C) 2004 William Lee Irwin III
11  */
12 #include <linux/kallsyms.h>
13 #include <linux/debugfs.h>
14 #include <linux/uaccess.h>
15 #include <linux/module.h>
16 #include <linux/ftrace.h>
17 #include <linux/fs.h>
18
19 #include "trace.h"
20
21 static struct trace_array               *irqsoff_trace __read_mostly;
22 static int                              tracer_enabled __read_mostly;
23
24 static DEFINE_PER_CPU(int, tracing_cpu);
25
26 static DEFINE_SPINLOCK(max_trace_lock);
27
28 enum {
29         TRACER_IRQS_OFF         = (1 << 1),
30         TRACER_PREEMPT_OFF      = (1 << 2),
31 };
32
33 static int trace_type __read_mostly;
34
35 #ifdef CONFIG_PREEMPT_TRACER
36 static inline int
37 preempt_trace(void)
38 {
39         return ((trace_type & TRACER_PREEMPT_OFF) && preempt_count());
40 }
41 #else
42 # define preempt_trace() (0)
43 #endif
44
45 #ifdef CONFIG_IRQSOFF_TRACER
46 static inline int
47 irq_trace(void)
48 {
49         return ((trace_type & TRACER_IRQS_OFF) &&
50                 irqs_disabled());
51 }
52 #else
53 # define irq_trace() (0)
54 #endif
55
56 /*
57  * Sequence count - we record it when starting a measurement and
58  * skip the latency if the sequence has changed - some other section
59  * did a maximum and could disturb our measurement with serial console
60  * printouts, etc. Truly coinciding maximum latencies should be rare
61  * and what happens together happens separately as well, so this doesnt
62  * decrease the validity of the maximum found:
63  */
64 static __cacheline_aligned_in_smp       unsigned long max_sequence;
65
66 #ifdef CONFIG_FTRACE
67 /*
68  * irqsoff uses its own tracer function to keep the overhead down:
69  */
70 static void
71 irqsoff_tracer_call(unsigned long ip, unsigned long parent_ip)
72 {
73         struct trace_array *tr = irqsoff_trace;
74         struct trace_array_cpu *data;
75         unsigned long flags;
76         long disabled;
77         int cpu;
78
79         /*
80          * Does not matter if we preempt. We test the flags
81          * afterward, to see if irqs are disabled or not.
82          * If we preempt and get a false positive, the flags
83          * test will fail.
84          */
85         cpu = raw_smp_processor_id();
86         if (likely(!per_cpu(tracing_cpu, cpu)))
87                 return;
88
89         local_save_flags(flags);
90         /* slight chance to get a false positive on tracing_cpu */
91         if (!irqs_disabled_flags(flags))
92                 return;
93
94         data = tr->data[cpu];
95         disabled = atomic_inc_return(&data->disabled);
96
97         if (likely(disabled == 1))
98                 trace_function(tr, data, ip, parent_ip, flags);
99
100         atomic_dec(&data->disabled);
101 }
102
103 static struct ftrace_ops trace_ops __read_mostly =
104 {
105         .func = irqsoff_tracer_call,
106 };
107 #endif /* CONFIG_FTRACE */
108
109 /*
110  * Should this new latency be reported/recorded?
111  */
112 static int report_latency(cycle_t delta)
113 {
114         if (tracing_thresh) {
115                 if (delta < tracing_thresh)
116                         return 0;
117         } else {
118                 if (delta <= tracing_max_latency)
119                         return 0;
120         }
121         return 1;
122 }
123
124 static void
125 check_critical_timing(struct trace_array *tr,
126                       struct trace_array_cpu *data,
127                       unsigned long parent_ip,
128                       int cpu)
129 {
130         unsigned long latency, t0, t1;
131         cycle_t T0, T1, delta;
132         unsigned long flags;
133
134         /*
135          * usecs conversion is slow so we try to delay the conversion
136          * as long as possible:
137          */
138         T0 = data->preempt_timestamp;
139         T1 = ftrace_now(cpu);
140         delta = T1-T0;
141
142         local_save_flags(flags);
143
144         if (!report_latency(delta))
145                 goto out;
146
147         spin_lock_irqsave(&max_trace_lock, flags);
148
149         /* check if we are still the max latency */
150         if (!report_latency(delta))
151                 goto out_unlock;
152
153         trace_function(tr, data, CALLER_ADDR0, parent_ip, flags);
154
155         latency = nsecs_to_usecs(delta);
156
157         if (data->critical_sequence != max_sequence)
158                 goto out_unlock;
159
160         tracing_max_latency = delta;
161         t0 = nsecs_to_usecs(T0);
162         t1 = nsecs_to_usecs(T1);
163
164         data->critical_end = parent_ip;
165
166         update_max_tr_single(tr, current, cpu);
167
168         if (!runqueue_is_locked()) {
169                 if (tracing_thresh) {
170                         printk(KERN_INFO "(%16s-%-5d|#%d): %lu us critical"
171                                " section violates %lu us threshold.\n",
172                                current->comm, current->pid,
173                                raw_smp_processor_id(),
174                                latency, nsecs_to_usecs(tracing_thresh));
175                 } else {
176                         printk(KERN_INFO "(%16s-%-5d|#%d): new %lu us"
177                                " maximum-latency critical section.\n",
178                                current->comm, current->pid,
179                                raw_smp_processor_id(),
180                                latency);
181                 }
182         }
183
184         max_sequence++;
185
186 out_unlock:
187         spin_unlock_irqrestore(&max_trace_lock, flags);
188
189 out:
190         data->critical_sequence = max_sequence;
191         data->preempt_timestamp = ftrace_now(cpu);
192         tracing_reset(data);
193         trace_function(tr, data, CALLER_ADDR0, parent_ip, flags);
194 }
195
196 static inline void
197 start_critical_timing(unsigned long ip, unsigned long parent_ip)
198 {
199         int cpu;
200         struct trace_array *tr = irqsoff_trace;
201         struct trace_array_cpu *data;
202         unsigned long flags;
203
204         if (likely(!tracer_enabled))
205                 return;
206
207         if (__get_cpu_var(tracing_cpu))
208                 return;
209
210         cpu = raw_smp_processor_id();
211         data = tr->data[cpu];
212
213         if (unlikely(!data) || unlikely(!head_page(data)) ||
214             atomic_read(&data->disabled))
215                 return;
216
217         atomic_inc(&data->disabled);
218
219         data->critical_sequence = max_sequence;
220         data->preempt_timestamp = ftrace_now(cpu);
221         data->critical_start = parent_ip ? : ip;
222         tracing_reset(data);
223
224         local_save_flags(flags);
225
226         trace_function(tr, data, ip, parent_ip, flags);
227
228         __get_cpu_var(tracing_cpu) = 1;
229
230         atomic_dec(&data->disabled);
231 }
232
233 static inline void
234 stop_critical_timing(unsigned long ip, unsigned long parent_ip)
235 {
236         int cpu;
237         struct trace_array *tr = irqsoff_trace;
238         struct trace_array_cpu *data;
239         unsigned long flags;
240
241         /* Always clear the tracing cpu on stopping the trace */
242         if (unlikely(__get_cpu_var(tracing_cpu)))
243                 __get_cpu_var(tracing_cpu) = 0;
244         else
245                 return;
246
247         if (!tracer_enabled)
248                 return;
249
250         cpu = raw_smp_processor_id();
251         data = tr->data[cpu];
252
253         if (unlikely(!data) || unlikely(!head_page(data)) ||
254             !data->critical_start || atomic_read(&data->disabled))
255                 return;
256
257         atomic_inc(&data->disabled);
258         local_save_flags(flags);
259         trace_function(tr, data, ip, parent_ip, flags);
260         check_critical_timing(tr, data, parent_ip ? : ip, cpu);
261         data->critical_start = 0;
262         atomic_dec(&data->disabled);
263 }
264
265 /* start and stop critical timings used to for stoppage (in idle) */
266 void start_critical_timings(void)
267 {
268         if (preempt_trace() || irq_trace())
269                 start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
270 }
271
272 void stop_critical_timings(void)
273 {
274         if (preempt_trace() || irq_trace())
275                 stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
276 }
277
278 #ifdef CONFIG_IRQSOFF_TRACER
279 #ifdef CONFIG_PROVE_LOCKING
280 void time_hardirqs_on(unsigned long a0, unsigned long a1)
281 {
282         if (!preempt_trace() && irq_trace())
283                 stop_critical_timing(a0, a1);
284 }
285
286 void time_hardirqs_off(unsigned long a0, unsigned long a1)
287 {
288         if (!preempt_trace() && irq_trace())
289                 start_critical_timing(a0, a1);
290 }
291
292 #else /* !CONFIG_PROVE_LOCKING */
293
294 /*
295  * Stubs:
296  */
297
298 void early_boot_irqs_off(void)
299 {
300 }
301
302 void early_boot_irqs_on(void)
303 {
304 }
305
306 void trace_softirqs_on(unsigned long ip)
307 {
308 }
309
310 void trace_softirqs_off(unsigned long ip)
311 {
312 }
313
314 inline void print_irqtrace_events(struct task_struct *curr)
315 {
316 }
317
318 /*
319  * We are only interested in hardirq on/off events:
320  */
321 void trace_hardirqs_on(void)
322 {
323         if (!preempt_trace() && irq_trace())
324                 stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
325 }
326 EXPORT_SYMBOL(trace_hardirqs_on);
327
328 void trace_hardirqs_off(void)
329 {
330         if (!preempt_trace() && irq_trace())
331                 start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
332 }
333 EXPORT_SYMBOL(trace_hardirqs_off);
334
335 void trace_hardirqs_on_caller(unsigned long caller_addr)
336 {
337         if (!preempt_trace() && irq_trace())
338                 stop_critical_timing(CALLER_ADDR0, caller_addr);
339 }
340 EXPORT_SYMBOL(trace_hardirqs_on_caller);
341
342 void trace_hardirqs_off_caller(unsigned long caller_addr)
343 {
344         if (!preempt_trace() && irq_trace())
345                 start_critical_timing(CALLER_ADDR0, caller_addr);
346 }
347 EXPORT_SYMBOL(trace_hardirqs_off_caller);
348
349 #endif /* CONFIG_PROVE_LOCKING */
350 #endif /*  CONFIG_IRQSOFF_TRACER */
351
352 #ifdef CONFIG_PREEMPT_TRACER
353 void trace_preempt_on(unsigned long a0, unsigned long a1)
354 {
355         stop_critical_timing(a0, a1);
356 }
357
358 void trace_preempt_off(unsigned long a0, unsigned long a1)
359 {
360         start_critical_timing(a0, a1);
361 }
362 #endif /* CONFIG_PREEMPT_TRACER */
363
364 static void start_irqsoff_tracer(struct trace_array *tr)
365 {
366         register_ftrace_function(&trace_ops);
367         tracer_enabled = 1;
368 }
369
370 static void stop_irqsoff_tracer(struct trace_array *tr)
371 {
372         tracer_enabled = 0;
373         unregister_ftrace_function(&trace_ops);
374 }
375
376 static void __irqsoff_tracer_init(struct trace_array *tr)
377 {
378         irqsoff_trace = tr;
379         /* make sure that the tracer is visibel */
380         smp_wmb();
381
382         if (tr->ctrl)
383                 start_irqsoff_tracer(tr);
384 }
385
386 static void irqsoff_tracer_reset(struct trace_array *tr)
387 {
388         if (tr->ctrl)
389                 stop_irqsoff_tracer(tr);
390 }
391
392 static void irqsoff_tracer_ctrl_update(struct trace_array *tr)
393 {
394         if (tr->ctrl)
395                 start_irqsoff_tracer(tr);
396         else
397                 stop_irqsoff_tracer(tr);
398 }
399
400 static void irqsoff_tracer_open(struct trace_iterator *iter)
401 {
402         /* stop the trace while dumping */
403         if (iter->tr->ctrl)
404                 stop_irqsoff_tracer(iter->tr);
405 }
406
407 static void irqsoff_tracer_close(struct trace_iterator *iter)
408 {
409         if (iter->tr->ctrl)
410                 start_irqsoff_tracer(iter->tr);
411 }
412
413 #ifdef CONFIG_IRQSOFF_TRACER
414 static void irqsoff_tracer_init(struct trace_array *tr)
415 {
416         trace_type = TRACER_IRQS_OFF;
417
418         __irqsoff_tracer_init(tr);
419 }
420 static struct tracer irqsoff_tracer __read_mostly =
421 {
422         .name           = "irqsoff",
423         .init           = irqsoff_tracer_init,
424         .reset          = irqsoff_tracer_reset,
425         .open           = irqsoff_tracer_open,
426         .close          = irqsoff_tracer_close,
427         .ctrl_update    = irqsoff_tracer_ctrl_update,
428         .print_max      = 1,
429 #ifdef CONFIG_FTRACE_SELFTEST
430         .selftest    = trace_selftest_startup_irqsoff,
431 #endif
432 };
433 # define register_irqsoff(trace) register_tracer(&trace)
434 #else
435 # define register_irqsoff(trace) do { } while (0)
436 #endif
437
438 #ifdef CONFIG_PREEMPT_TRACER
439 static void preemptoff_tracer_init(struct trace_array *tr)
440 {
441         trace_type = TRACER_PREEMPT_OFF;
442
443         __irqsoff_tracer_init(tr);
444 }
445
446 static struct tracer preemptoff_tracer __read_mostly =
447 {
448         .name           = "preemptoff",
449         .init           = preemptoff_tracer_init,
450         .reset          = irqsoff_tracer_reset,
451         .open           = irqsoff_tracer_open,
452         .close          = irqsoff_tracer_close,
453         .ctrl_update    = irqsoff_tracer_ctrl_update,
454         .print_max      = 1,
455 #ifdef CONFIG_FTRACE_SELFTEST
456         .selftest    = trace_selftest_startup_preemptoff,
457 #endif
458 };
459 # define register_preemptoff(trace) register_tracer(&trace)
460 #else
461 # define register_preemptoff(trace) do { } while (0)
462 #endif
463
464 #if defined(CONFIG_IRQSOFF_TRACER) && \
465         defined(CONFIG_PREEMPT_TRACER)
466
467 static void preemptirqsoff_tracer_init(struct trace_array *tr)
468 {
469         trace_type = TRACER_IRQS_OFF | TRACER_PREEMPT_OFF;
470
471         __irqsoff_tracer_init(tr);
472 }
473
474 static struct tracer preemptirqsoff_tracer __read_mostly =
475 {
476         .name           = "preemptirqsoff",
477         .init           = preemptirqsoff_tracer_init,
478         .reset          = irqsoff_tracer_reset,
479         .open           = irqsoff_tracer_open,
480         .close          = irqsoff_tracer_close,
481         .ctrl_update    = irqsoff_tracer_ctrl_update,
482         .print_max      = 1,
483 #ifdef CONFIG_FTRACE_SELFTEST
484         .selftest    = trace_selftest_startup_preemptirqsoff,
485 #endif
486 };
487
488 # define register_preemptirqsoff(trace) register_tracer(&trace)
489 #else
490 # define register_preemptirqsoff(trace) do { } while (0)
491 #endif
492
493 __init static int init_irqsoff_tracer(void)
494 {
495         register_irqsoff(irqsoff_tracer);
496         register_preemptoff(preemptoff_tracer);
497         register_preemptirqsoff(preemptirqsoff_tracer);
498
499         return 0;
500 }
501 device_initcall(init_irqsoff_tracer);