USB: serial: add special case for processing of empty read urbs
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / trace / trace_irqsoff.c
blob2974bc7538c74603b15f85b75a62f74e70f7bb4d
1 /*
2 * trace irqs off critical timings
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * From code in the latency_tracer, that is:
9 * Copyright (C) 2004-2006 Ingo Molnar
10 * Copyright (C) 2004 William Lee Irwin III
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>
19 #include "trace.h"
21 static struct trace_array *irqsoff_trace __read_mostly;
22 static int tracer_enabled __read_mostly;
24 static DEFINE_PER_CPU(int, tracing_cpu);
26 static DEFINE_SPINLOCK(max_trace_lock);
28 enum {
29 TRACER_IRQS_OFF = (1 << 1),
30 TRACER_PREEMPT_OFF = (1 << 2),
33 static int trace_type __read_mostly;
35 static int save_lat_flag;
37 #ifdef CONFIG_PREEMPT_TRACER
38 static inline int
39 preempt_trace(void)
41 return ((trace_type & TRACER_PREEMPT_OFF) && preempt_count());
43 #else
44 # define preempt_trace() (0)
45 #endif
47 #ifdef CONFIG_IRQSOFF_TRACER
48 static inline int
49 irq_trace(void)
51 return ((trace_type & TRACER_IRQS_OFF) &&
52 irqs_disabled());
54 #else
55 # define irq_trace() (0)
56 #endif
59 * Sequence count - we record it when starting a measurement and
60 * skip the latency if the sequence has changed - some other section
61 * did a maximum and could disturb our measurement with serial console
62 * printouts, etc. Truly coinciding maximum latencies should be rare
63 * and what happens together happens separately as well, so this doesnt
64 * decrease the validity of the maximum found:
66 static __cacheline_aligned_in_smp unsigned long max_sequence;
68 #ifdef CONFIG_FUNCTION_TRACER
70 * irqsoff uses its own tracer function to keep the overhead down:
72 static void
73 irqsoff_tracer_call(unsigned long ip, unsigned long parent_ip)
75 struct trace_array *tr = irqsoff_trace;
76 struct trace_array_cpu *data;
77 unsigned long flags;
78 long disabled;
79 int cpu;
82 * Does not matter if we preempt. We test the flags
83 * afterward, to see if irqs are disabled or not.
84 * If we preempt and get a false positive, the flags
85 * test will fail.
87 cpu = raw_smp_processor_id();
88 if (likely(!per_cpu(tracing_cpu, cpu)))
89 return;
91 local_save_flags(flags);
92 /* slight chance to get a false positive on tracing_cpu */
93 if (!irqs_disabled_flags(flags))
94 return;
96 data = tr->data[cpu];
97 disabled = atomic_inc_return(&data->disabled);
99 if (likely(disabled == 1))
100 trace_function(tr, ip, parent_ip, flags, preempt_count());
102 atomic_dec(&data->disabled);
105 static struct ftrace_ops trace_ops __read_mostly =
107 .func = irqsoff_tracer_call,
109 #endif /* CONFIG_FUNCTION_TRACER */
112 * Should this new latency be reported/recorded?
114 static int report_latency(cycle_t delta)
116 if (tracing_thresh) {
117 if (delta < tracing_thresh)
118 return 0;
119 } else {
120 if (delta <= tracing_max_latency)
121 return 0;
123 return 1;
126 static void
127 check_critical_timing(struct trace_array *tr,
128 struct trace_array_cpu *data,
129 unsigned long parent_ip,
130 int cpu)
132 cycle_t T0, T1, delta;
133 unsigned long flags;
134 int pc;
136 T0 = data->preempt_timestamp;
137 T1 = ftrace_now(cpu);
138 delta = T1-T0;
140 local_save_flags(flags);
142 pc = preempt_count();
144 if (!report_latency(delta))
145 goto out;
147 spin_lock_irqsave(&max_trace_lock, flags);
149 /* check if we are still the max latency */
150 if (!report_latency(delta))
151 goto out_unlock;
153 trace_function(tr, CALLER_ADDR0, parent_ip, flags, pc);
154 /* Skip 5 functions to get to the irq/preempt enable function */
155 __trace_stack(tr, flags, 5, pc);
157 if (data->critical_sequence != max_sequence)
158 goto out_unlock;
160 data->critical_end = parent_ip;
162 if (likely(!is_tracing_stopped())) {
163 tracing_max_latency = delta;
164 update_max_tr_single(tr, current, cpu);
167 max_sequence++;
169 out_unlock:
170 spin_unlock_irqrestore(&max_trace_lock, flags);
172 out:
173 data->critical_sequence = max_sequence;
174 data->preempt_timestamp = ftrace_now(cpu);
175 trace_function(tr, CALLER_ADDR0, parent_ip, flags, pc);
178 static inline void
179 start_critical_timing(unsigned long ip, unsigned long parent_ip)
181 int cpu;
182 struct trace_array *tr = irqsoff_trace;
183 struct trace_array_cpu *data;
184 unsigned long flags;
186 if (likely(!tracer_enabled))
187 return;
189 cpu = raw_smp_processor_id();
191 if (per_cpu(tracing_cpu, cpu))
192 return;
194 data = tr->data[cpu];
196 if (unlikely(!data) || atomic_read(&data->disabled))
197 return;
199 atomic_inc(&data->disabled);
201 data->critical_sequence = max_sequence;
202 data->preempt_timestamp = ftrace_now(cpu);
203 data->critical_start = parent_ip ? : ip;
205 local_save_flags(flags);
207 trace_function(tr, ip, parent_ip, flags, preempt_count());
209 per_cpu(tracing_cpu, cpu) = 1;
211 atomic_dec(&data->disabled);
214 static inline void
215 stop_critical_timing(unsigned long ip, unsigned long parent_ip)
217 int cpu;
218 struct trace_array *tr = irqsoff_trace;
219 struct trace_array_cpu *data;
220 unsigned long flags;
222 cpu = raw_smp_processor_id();
223 /* Always clear the tracing cpu on stopping the trace */
224 if (unlikely(per_cpu(tracing_cpu, cpu)))
225 per_cpu(tracing_cpu, cpu) = 0;
226 else
227 return;
229 if (!tracer_enabled)
230 return;
232 data = tr->data[cpu];
234 if (unlikely(!data) ||
235 !data->critical_start || atomic_read(&data->disabled))
236 return;
238 atomic_inc(&data->disabled);
240 local_save_flags(flags);
241 trace_function(tr, ip, parent_ip, flags, preempt_count());
242 check_critical_timing(tr, data, parent_ip ? : ip, cpu);
243 data->critical_start = 0;
244 atomic_dec(&data->disabled);
247 /* start and stop critical timings used to for stoppage (in idle) */
248 void start_critical_timings(void)
250 if (preempt_trace() || irq_trace())
251 start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
253 EXPORT_SYMBOL_GPL(start_critical_timings);
255 void stop_critical_timings(void)
257 if (preempt_trace() || irq_trace())
258 stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
260 EXPORT_SYMBOL_GPL(stop_critical_timings);
262 #ifdef CONFIG_IRQSOFF_TRACER
263 #ifdef CONFIG_PROVE_LOCKING
264 void time_hardirqs_on(unsigned long a0, unsigned long a1)
266 if (!preempt_trace() && irq_trace())
267 stop_critical_timing(a0, a1);
270 void time_hardirqs_off(unsigned long a0, unsigned long a1)
272 if (!preempt_trace() && irq_trace())
273 start_critical_timing(a0, a1);
276 #else /* !CONFIG_PROVE_LOCKING */
279 * Stubs:
282 void early_boot_irqs_off(void)
286 void early_boot_irqs_on(void)
290 void trace_softirqs_on(unsigned long ip)
294 void trace_softirqs_off(unsigned long ip)
298 inline void print_irqtrace_events(struct task_struct *curr)
303 * We are only interested in hardirq on/off events:
305 void trace_hardirqs_on(void)
307 if (!preempt_trace() && irq_trace())
308 stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
310 EXPORT_SYMBOL(trace_hardirqs_on);
312 void trace_hardirqs_off(void)
314 if (!preempt_trace() && irq_trace())
315 start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
317 EXPORT_SYMBOL(trace_hardirqs_off);
319 void trace_hardirqs_on_caller(unsigned long caller_addr)
321 if (!preempt_trace() && irq_trace())
322 stop_critical_timing(CALLER_ADDR0, caller_addr);
324 EXPORT_SYMBOL(trace_hardirqs_on_caller);
326 void trace_hardirqs_off_caller(unsigned long caller_addr)
328 if (!preempt_trace() && irq_trace())
329 start_critical_timing(CALLER_ADDR0, caller_addr);
331 EXPORT_SYMBOL(trace_hardirqs_off_caller);
333 #endif /* CONFIG_PROVE_LOCKING */
334 #endif /* CONFIG_IRQSOFF_TRACER */
336 #ifdef CONFIG_PREEMPT_TRACER
337 void trace_preempt_on(unsigned long a0, unsigned long a1)
339 if (preempt_trace())
340 stop_critical_timing(a0, a1);
343 void trace_preempt_off(unsigned long a0, unsigned long a1)
345 if (preempt_trace())
346 start_critical_timing(a0, a1);
348 #endif /* CONFIG_PREEMPT_TRACER */
350 static void start_irqsoff_tracer(struct trace_array *tr)
352 register_ftrace_function(&trace_ops);
353 if (tracing_is_enabled())
354 tracer_enabled = 1;
355 else
356 tracer_enabled = 0;
359 static void stop_irqsoff_tracer(struct trace_array *tr)
361 tracer_enabled = 0;
362 unregister_ftrace_function(&trace_ops);
365 static void __irqsoff_tracer_init(struct trace_array *tr)
367 save_lat_flag = trace_flags & TRACE_ITER_LATENCY_FMT;
368 trace_flags |= TRACE_ITER_LATENCY_FMT;
370 tracing_max_latency = 0;
371 irqsoff_trace = tr;
372 /* make sure that the tracer is visible */
373 smp_wmb();
374 tracing_reset_online_cpus(tr);
375 start_irqsoff_tracer(tr);
378 static void irqsoff_tracer_reset(struct trace_array *tr)
380 stop_irqsoff_tracer(tr);
382 if (!save_lat_flag)
383 trace_flags &= ~TRACE_ITER_LATENCY_FMT;
386 static void irqsoff_tracer_start(struct trace_array *tr)
388 tracer_enabled = 1;
391 static void irqsoff_tracer_stop(struct trace_array *tr)
393 tracer_enabled = 0;
396 #ifdef CONFIG_IRQSOFF_TRACER
397 static int irqsoff_tracer_init(struct trace_array *tr)
399 trace_type = TRACER_IRQS_OFF;
401 __irqsoff_tracer_init(tr);
402 return 0;
404 static struct tracer irqsoff_tracer __read_mostly =
406 .name = "irqsoff",
407 .init = irqsoff_tracer_init,
408 .reset = irqsoff_tracer_reset,
409 .start = irqsoff_tracer_start,
410 .stop = irqsoff_tracer_stop,
411 .print_max = 1,
412 #ifdef CONFIG_FTRACE_SELFTEST
413 .selftest = trace_selftest_startup_irqsoff,
414 #endif
416 # define register_irqsoff(trace) register_tracer(&trace)
417 #else
418 # define register_irqsoff(trace) do { } while (0)
419 #endif
421 #ifdef CONFIG_PREEMPT_TRACER
422 static int preemptoff_tracer_init(struct trace_array *tr)
424 trace_type = TRACER_PREEMPT_OFF;
426 __irqsoff_tracer_init(tr);
427 return 0;
430 static struct tracer preemptoff_tracer __read_mostly =
432 .name = "preemptoff",
433 .init = preemptoff_tracer_init,
434 .reset = irqsoff_tracer_reset,
435 .start = irqsoff_tracer_start,
436 .stop = irqsoff_tracer_stop,
437 .print_max = 1,
438 #ifdef CONFIG_FTRACE_SELFTEST
439 .selftest = trace_selftest_startup_preemptoff,
440 #endif
442 # define register_preemptoff(trace) register_tracer(&trace)
443 #else
444 # define register_preemptoff(trace) do { } while (0)
445 #endif
447 #if defined(CONFIG_IRQSOFF_TRACER) && \
448 defined(CONFIG_PREEMPT_TRACER)
450 static int preemptirqsoff_tracer_init(struct trace_array *tr)
452 trace_type = TRACER_IRQS_OFF | TRACER_PREEMPT_OFF;
454 __irqsoff_tracer_init(tr);
455 return 0;
458 static struct tracer preemptirqsoff_tracer __read_mostly =
460 .name = "preemptirqsoff",
461 .init = preemptirqsoff_tracer_init,
462 .reset = irqsoff_tracer_reset,
463 .start = irqsoff_tracer_start,
464 .stop = irqsoff_tracer_stop,
465 .print_max = 1,
466 #ifdef CONFIG_FTRACE_SELFTEST
467 .selftest = trace_selftest_startup_preemptirqsoff,
468 #endif
471 # define register_preemptirqsoff(trace) register_tracer(&trace)
472 #else
473 # define register_preemptirqsoff(trace) do { } while (0)
474 #endif
476 __init static int init_irqsoff_tracer(void)
478 register_irqsoff(irqsoff_tracer);
479 register_preemptoff(preemptoff_tracer);
480 register_preemptirqsoff(preemptirqsoff_tracer);
482 return 0;
484 device_initcall(init_irqsoff_tracer);