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>
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
);
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
41 return ((trace_type
& TRACER_PREEMPT_OFF
) && preempt_count());
44 # define preempt_trace() (0)
47 #ifdef CONFIG_IRQSOFF_TRACER
51 return ((trace_type
& TRACER_IRQS_OFF
) &&
55 # define irq_trace() (0)
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:
73 irqsoff_tracer_call(unsigned long ip
, unsigned long parent_ip
)
75 struct trace_array
*tr
= irqsoff_trace
;
76 struct trace_array_cpu
*data
;
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
87 cpu
= raw_smp_processor_id();
88 if (likely(!per_cpu(tracing_cpu
, cpu
)))
91 local_save_flags(flags
);
92 /* slight chance to get a false positive on tracing_cpu */
93 if (!irqs_disabled_flags(flags
))
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
)
120 if (delta
<= tracing_max_latency
)
127 check_critical_timing(struct trace_array
*tr
,
128 struct trace_array_cpu
*data
,
129 unsigned long parent_ip
,
132 cycle_t T0
, T1
, delta
;
136 T0
= data
->preempt_timestamp
;
137 T1
= ftrace_now(cpu
);
140 local_save_flags(flags
);
142 pc
= preempt_count();
144 if (!report_latency(delta
))
147 spin_lock_irqsave(&max_trace_lock
, flags
);
149 /* check if we are still the max latency */
150 if (!report_latency(delta
))
153 trace_function(tr
, CALLER_ADDR0
, parent_ip
, flags
, pc
);
155 if (data
->critical_sequence
!= max_sequence
)
158 data
->critical_end
= parent_ip
;
160 if (likely(!is_tracing_stopped())) {
161 tracing_max_latency
= delta
;
162 update_max_tr_single(tr
, current
, cpu
);
168 spin_unlock_irqrestore(&max_trace_lock
, flags
);
171 data
->critical_sequence
= max_sequence
;
172 data
->preempt_timestamp
= ftrace_now(cpu
);
173 trace_function(tr
, CALLER_ADDR0
, parent_ip
, flags
, pc
);
177 start_critical_timing(unsigned long ip
, unsigned long parent_ip
)
180 struct trace_array
*tr
= irqsoff_trace
;
181 struct trace_array_cpu
*data
;
184 if (likely(!tracer_enabled
))
187 cpu
= raw_smp_processor_id();
189 if (per_cpu(tracing_cpu
, cpu
))
192 data
= tr
->data
[cpu
];
194 if (unlikely(!data
) || atomic_read(&data
->disabled
))
197 atomic_inc(&data
->disabled
);
199 data
->critical_sequence
= max_sequence
;
200 data
->preempt_timestamp
= ftrace_now(cpu
);
201 data
->critical_start
= parent_ip
? : ip
;
203 local_save_flags(flags
);
205 trace_function(tr
, ip
, parent_ip
, flags
, preempt_count());
207 per_cpu(tracing_cpu
, cpu
) = 1;
209 atomic_dec(&data
->disabled
);
213 stop_critical_timing(unsigned long ip
, unsigned long parent_ip
)
216 struct trace_array
*tr
= irqsoff_trace
;
217 struct trace_array_cpu
*data
;
220 cpu
= raw_smp_processor_id();
221 /* Always clear the tracing cpu on stopping the trace */
222 if (unlikely(per_cpu(tracing_cpu
, cpu
)))
223 per_cpu(tracing_cpu
, cpu
) = 0;
230 data
= tr
->data
[cpu
];
232 if (unlikely(!data
) ||
233 !data
->critical_start
|| atomic_read(&data
->disabled
))
236 atomic_inc(&data
->disabled
);
238 local_save_flags(flags
);
239 trace_function(tr
, ip
, parent_ip
, flags
, preempt_count());
240 check_critical_timing(tr
, data
, parent_ip
? : ip
, cpu
);
241 data
->critical_start
= 0;
242 atomic_dec(&data
->disabled
);
245 /* start and stop critical timings used to for stoppage (in idle) */
246 void start_critical_timings(void)
248 if (preempt_trace() || irq_trace())
249 start_critical_timing(CALLER_ADDR0
, CALLER_ADDR1
);
251 EXPORT_SYMBOL_GPL(start_critical_timings
);
253 void stop_critical_timings(void)
255 if (preempt_trace() || irq_trace())
256 stop_critical_timing(CALLER_ADDR0
, CALLER_ADDR1
);
258 EXPORT_SYMBOL_GPL(stop_critical_timings
);
260 #ifdef CONFIG_IRQSOFF_TRACER
261 #ifdef CONFIG_PROVE_LOCKING
262 void time_hardirqs_on(unsigned long a0
, unsigned long a1
)
264 if (!preempt_trace() && irq_trace())
265 stop_critical_timing(a0
, a1
);
268 void time_hardirqs_off(unsigned long a0
, unsigned long a1
)
270 if (!preempt_trace() && irq_trace())
271 start_critical_timing(a0
, a1
);
274 #else /* !CONFIG_PROVE_LOCKING */
280 void early_boot_irqs_off(void)
284 void early_boot_irqs_on(void)
288 void trace_softirqs_on(unsigned long ip
)
292 void trace_softirqs_off(unsigned long ip
)
296 inline void print_irqtrace_events(struct task_struct
*curr
)
301 * We are only interested in hardirq on/off events:
303 void trace_hardirqs_on(void)
305 if (!preempt_trace() && irq_trace())
306 stop_critical_timing(CALLER_ADDR0
, CALLER_ADDR1
);
308 EXPORT_SYMBOL(trace_hardirqs_on
);
310 void trace_hardirqs_off(void)
312 if (!preempt_trace() && irq_trace())
313 start_critical_timing(CALLER_ADDR0
, CALLER_ADDR1
);
315 EXPORT_SYMBOL(trace_hardirqs_off
);
317 void trace_hardirqs_on_caller(unsigned long caller_addr
)
319 if (!preempt_trace() && irq_trace())
320 stop_critical_timing(CALLER_ADDR0
, caller_addr
);
322 EXPORT_SYMBOL(trace_hardirqs_on_caller
);
324 void trace_hardirqs_off_caller(unsigned long caller_addr
)
326 if (!preempt_trace() && irq_trace())
327 start_critical_timing(CALLER_ADDR0
, caller_addr
);
329 EXPORT_SYMBOL(trace_hardirqs_off_caller
);
331 #endif /* CONFIG_PROVE_LOCKING */
332 #endif /* CONFIG_IRQSOFF_TRACER */
334 #ifdef CONFIG_PREEMPT_TRACER
335 void trace_preempt_on(unsigned long a0
, unsigned long a1
)
338 stop_critical_timing(a0
, a1
);
341 void trace_preempt_off(unsigned long a0
, unsigned long a1
)
344 start_critical_timing(a0
, a1
);
346 #endif /* CONFIG_PREEMPT_TRACER */
348 static void start_irqsoff_tracer(struct trace_array
*tr
)
350 register_ftrace_function(&trace_ops
);
351 if (tracing_is_enabled())
357 static void stop_irqsoff_tracer(struct trace_array
*tr
)
360 unregister_ftrace_function(&trace_ops
);
363 static void __irqsoff_tracer_init(struct trace_array
*tr
)
365 save_lat_flag
= trace_flags
& TRACE_ITER_LATENCY_FMT
;
366 trace_flags
|= TRACE_ITER_LATENCY_FMT
;
368 tracing_max_latency
= 0;
370 /* make sure that the tracer is visible */
372 tracing_reset_online_cpus(tr
);
373 start_irqsoff_tracer(tr
);
376 static void irqsoff_tracer_reset(struct trace_array
*tr
)
378 stop_irqsoff_tracer(tr
);
381 trace_flags
&= ~TRACE_ITER_LATENCY_FMT
;
384 static void irqsoff_tracer_start(struct trace_array
*tr
)
389 static void irqsoff_tracer_stop(struct trace_array
*tr
)
394 #ifdef CONFIG_IRQSOFF_TRACER
395 static int irqsoff_tracer_init(struct trace_array
*tr
)
397 trace_type
= TRACER_IRQS_OFF
;
399 __irqsoff_tracer_init(tr
);
402 static struct tracer irqsoff_tracer __read_mostly
=
405 .init
= irqsoff_tracer_init
,
406 .reset
= irqsoff_tracer_reset
,
407 .start
= irqsoff_tracer_start
,
408 .stop
= irqsoff_tracer_stop
,
410 #ifdef CONFIG_FTRACE_SELFTEST
411 .selftest
= trace_selftest_startup_irqsoff
,
414 # define register_irqsoff(trace) register_tracer(&trace)
416 # define register_irqsoff(trace) do { } while (0)
419 #ifdef CONFIG_PREEMPT_TRACER
420 static int preemptoff_tracer_init(struct trace_array
*tr
)
422 trace_type
= TRACER_PREEMPT_OFF
;
424 __irqsoff_tracer_init(tr
);
428 static struct tracer preemptoff_tracer __read_mostly
=
430 .name
= "preemptoff",
431 .init
= preemptoff_tracer_init
,
432 .reset
= irqsoff_tracer_reset
,
433 .start
= irqsoff_tracer_start
,
434 .stop
= irqsoff_tracer_stop
,
436 #ifdef CONFIG_FTRACE_SELFTEST
437 .selftest
= trace_selftest_startup_preemptoff
,
440 # define register_preemptoff(trace) register_tracer(&trace)
442 # define register_preemptoff(trace) do { } while (0)
445 #if defined(CONFIG_IRQSOFF_TRACER) && \
446 defined(CONFIG_PREEMPT_TRACER)
448 static int preemptirqsoff_tracer_init(struct trace_array
*tr
)
450 trace_type
= TRACER_IRQS_OFF
| TRACER_PREEMPT_OFF
;
452 __irqsoff_tracer_init(tr
);
456 static struct tracer preemptirqsoff_tracer __read_mostly
=
458 .name
= "preemptirqsoff",
459 .init
= preemptirqsoff_tracer_init
,
460 .reset
= irqsoff_tracer_reset
,
461 .start
= irqsoff_tracer_start
,
462 .stop
= irqsoff_tracer_stop
,
464 #ifdef CONFIG_FTRACE_SELFTEST
465 .selftest
= trace_selftest_startup_preemptirqsoff
,
469 # define register_preemptirqsoff(trace) register_tracer(&trace)
471 # define register_preemptirqsoff(trace) do { } while (0)
474 __init
static int init_irqsoff_tracer(void)
476 register_irqsoff(irqsoff_tracer
);
477 register_preemptoff(preemptoff_tracer
);
478 register_preemptirqsoff(preemptirqsoff_tracer
);
482 device_initcall(init_irqsoff_tracer
);