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
);
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
)
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
);
170 spin_unlock_irqrestore(&max_trace_lock
, flags
);
173 data
->critical_sequence
= max_sequence
;
174 data
->preempt_timestamp
= ftrace_now(cpu
);
175 trace_function(tr
, CALLER_ADDR0
, parent_ip
, flags
, pc
);
179 start_critical_timing(unsigned long ip
, unsigned long parent_ip
)
182 struct trace_array
*tr
= irqsoff_trace
;
183 struct trace_array_cpu
*data
;
186 if (likely(!tracer_enabled
))
189 cpu
= raw_smp_processor_id();
191 if (per_cpu(tracing_cpu
, cpu
))
194 data
= tr
->data
[cpu
];
196 if (unlikely(!data
) || atomic_read(&data
->disabled
))
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
);
215 stop_critical_timing(unsigned long ip
, unsigned long parent_ip
)
218 struct trace_array
*tr
= irqsoff_trace
;
219 struct trace_array_cpu
*data
;
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;
232 data
= tr
->data
[cpu
];
234 if (unlikely(!data
) ||
235 !data
->critical_start
|| atomic_read(&data
->disabled
))
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 */
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
)
340 stop_critical_timing(a0
, a1
);
343 void trace_preempt_off(unsigned long a0
, unsigned long a1
)
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())
359 static void stop_irqsoff_tracer(struct trace_array
*tr
)
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;
372 /* make sure that the tracer is visible */
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
);
383 trace_flags
&= ~TRACE_ITER_LATENCY_FMT
;
386 static void irqsoff_tracer_start(struct trace_array
*tr
)
391 static void irqsoff_tracer_stop(struct trace_array
*tr
)
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
);
404 static struct tracer irqsoff_tracer __read_mostly
=
407 .init
= irqsoff_tracer_init
,
408 .reset
= irqsoff_tracer_reset
,
409 .start
= irqsoff_tracer_start
,
410 .stop
= irqsoff_tracer_stop
,
412 #ifdef CONFIG_FTRACE_SELFTEST
413 .selftest
= trace_selftest_startup_irqsoff
,
416 # define register_irqsoff(trace) register_tracer(&trace)
418 # define register_irqsoff(trace) do { } while (0)
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
);
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
,
438 #ifdef CONFIG_FTRACE_SELFTEST
439 .selftest
= trace_selftest_startup_preemptoff
,
442 # define register_preemptoff(trace) register_tracer(&trace)
444 # define register_preemptoff(trace) do { } while (0)
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
);
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
,
466 #ifdef CONFIG_FTRACE_SELFTEST
467 .selftest
= trace_selftest_startup_preemptirqsoff
,
471 # define register_preemptirqsoff(trace) register_tracer(&trace)
473 # define register_preemptirqsoff(trace) do { } while (0)
476 __init
static int init_irqsoff_tracer(void)
478 register_irqsoff(irqsoff_tracer
);
479 register_preemptoff(preemptoff_tracer
);
480 register_preemptirqsoff(preemptirqsoff_tracer
);
484 device_initcall(init_irqsoff_tracer
);