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 unsigned long latency
, t0
, t1
;
133 cycle_t T0
, T1
, delta
;
138 * usecs conversion is slow so we try to delay the conversion
139 * as long as possible:
141 T0
= data
->preempt_timestamp
;
142 T1
= ftrace_now(cpu
);
145 local_save_flags(flags
);
147 pc
= preempt_count();
149 if (!report_latency(delta
))
152 spin_lock_irqsave(&max_trace_lock
, flags
);
154 /* check if we are still the max latency */
155 if (!report_latency(delta
))
158 trace_function(tr
, CALLER_ADDR0
, parent_ip
, flags
, pc
);
160 latency
= nsecs_to_usecs(delta
);
162 if (data
->critical_sequence
!= max_sequence
)
165 tracing_max_latency
= delta
;
166 t0
= nsecs_to_usecs(T0
);
167 t1
= nsecs_to_usecs(T1
);
169 data
->critical_end
= parent_ip
;
171 update_max_tr_single(tr
, current
, cpu
);
176 spin_unlock_irqrestore(&max_trace_lock
, flags
);
179 data
->critical_sequence
= max_sequence
;
180 data
->preempt_timestamp
= ftrace_now(cpu
);
181 tracing_reset(tr
, cpu
);
182 trace_function(tr
, CALLER_ADDR0
, parent_ip
, flags
, pc
);
186 start_critical_timing(unsigned long ip
, unsigned long parent_ip
)
189 struct trace_array
*tr
= irqsoff_trace
;
190 struct trace_array_cpu
*data
;
193 if (likely(!tracer_enabled
))
196 cpu
= raw_smp_processor_id();
198 if (per_cpu(tracing_cpu
, cpu
))
201 data
= tr
->data
[cpu
];
203 if (unlikely(!data
) || atomic_read(&data
->disabled
))
206 atomic_inc(&data
->disabled
);
208 data
->critical_sequence
= max_sequence
;
209 data
->preempt_timestamp
= ftrace_now(cpu
);
210 data
->critical_start
= parent_ip
? : ip
;
211 tracing_reset(tr
, cpu
);
213 local_save_flags(flags
);
215 trace_function(tr
, ip
, parent_ip
, flags
, preempt_count());
217 per_cpu(tracing_cpu
, cpu
) = 1;
219 atomic_dec(&data
->disabled
);
223 stop_critical_timing(unsigned long ip
, unsigned long parent_ip
)
226 struct trace_array
*tr
= irqsoff_trace
;
227 struct trace_array_cpu
*data
;
230 cpu
= raw_smp_processor_id();
231 /* Always clear the tracing cpu on stopping the trace */
232 if (unlikely(per_cpu(tracing_cpu
, cpu
)))
233 per_cpu(tracing_cpu
, cpu
) = 0;
240 data
= tr
->data
[cpu
];
242 if (unlikely(!data
) ||
243 !data
->critical_start
|| atomic_read(&data
->disabled
))
246 atomic_inc(&data
->disabled
);
248 local_save_flags(flags
);
249 trace_function(tr
, ip
, parent_ip
, flags
, preempt_count());
250 check_critical_timing(tr
, data
, parent_ip
? : ip
, cpu
);
251 data
->critical_start
= 0;
252 atomic_dec(&data
->disabled
);
255 /* start and stop critical timings used to for stoppage (in idle) */
256 void start_critical_timings(void)
258 if (preempt_trace() || irq_trace())
259 start_critical_timing(CALLER_ADDR0
, CALLER_ADDR1
);
261 EXPORT_SYMBOL_GPL(start_critical_timings
);
263 void stop_critical_timings(void)
265 if (preempt_trace() || irq_trace())
266 stop_critical_timing(CALLER_ADDR0
, CALLER_ADDR1
);
268 EXPORT_SYMBOL_GPL(stop_critical_timings
);
270 #ifdef CONFIG_IRQSOFF_TRACER
271 #ifdef CONFIG_PROVE_LOCKING
272 void time_hardirqs_on(unsigned long a0
, unsigned long a1
)
274 if (!preempt_trace() && irq_trace())
275 stop_critical_timing(a0
, a1
);
278 void time_hardirqs_off(unsigned long a0
, unsigned long a1
)
280 if (!preempt_trace() && irq_trace())
281 start_critical_timing(a0
, a1
);
284 #else /* !CONFIG_PROVE_LOCKING */
290 void early_boot_irqs_off(void)
294 void early_boot_irqs_on(void)
298 void trace_softirqs_on(unsigned long ip
)
302 void trace_softirqs_off(unsigned long ip
)
306 inline void print_irqtrace_events(struct task_struct
*curr
)
311 * We are only interested in hardirq on/off events:
313 void trace_hardirqs_on(void)
315 if (!preempt_trace() && irq_trace())
316 stop_critical_timing(CALLER_ADDR0
, CALLER_ADDR1
);
318 EXPORT_SYMBOL(trace_hardirqs_on
);
320 void trace_hardirqs_off(void)
322 if (!preempt_trace() && irq_trace())
323 start_critical_timing(CALLER_ADDR0
, CALLER_ADDR1
);
325 EXPORT_SYMBOL(trace_hardirqs_off
);
327 void trace_hardirqs_on_caller(unsigned long caller_addr
)
329 if (!preempt_trace() && irq_trace())
330 stop_critical_timing(CALLER_ADDR0
, caller_addr
);
332 EXPORT_SYMBOL(trace_hardirqs_on_caller
);
334 void trace_hardirqs_off_caller(unsigned long caller_addr
)
336 if (!preempt_trace() && irq_trace())
337 start_critical_timing(CALLER_ADDR0
, caller_addr
);
339 EXPORT_SYMBOL(trace_hardirqs_off_caller
);
341 #endif /* CONFIG_PROVE_LOCKING */
342 #endif /* CONFIG_IRQSOFF_TRACER */
344 #ifdef CONFIG_PREEMPT_TRACER
345 void trace_preempt_on(unsigned long a0
, unsigned long a1
)
348 stop_critical_timing(a0
, a1
);
351 void trace_preempt_off(unsigned long a0
, unsigned long a1
)
354 start_critical_timing(a0
, a1
);
356 #endif /* CONFIG_PREEMPT_TRACER */
358 static void start_irqsoff_tracer(struct trace_array
*tr
)
360 register_ftrace_function(&trace_ops
);
361 if (tracing_is_enabled())
367 static void stop_irqsoff_tracer(struct trace_array
*tr
)
370 unregister_ftrace_function(&trace_ops
);
373 static void __irqsoff_tracer_init(struct trace_array
*tr
)
375 save_lat_flag
= trace_flags
& TRACE_ITER_LATENCY_FMT
;
376 trace_flags
|= TRACE_ITER_LATENCY_FMT
;
378 tracing_max_latency
= 0;
380 /* make sure that the tracer is visible */
382 start_irqsoff_tracer(tr
);
385 static void irqsoff_tracer_reset(struct trace_array
*tr
)
387 stop_irqsoff_tracer(tr
);
390 trace_flags
&= ~TRACE_ITER_LATENCY_FMT
;
393 static void irqsoff_tracer_start(struct trace_array
*tr
)
398 static void irqsoff_tracer_stop(struct trace_array
*tr
)
403 #ifdef CONFIG_IRQSOFF_TRACER
404 static int irqsoff_tracer_init(struct trace_array
*tr
)
406 trace_type
= TRACER_IRQS_OFF
;
408 __irqsoff_tracer_init(tr
);
411 static struct tracer irqsoff_tracer __read_mostly
=
414 .init
= irqsoff_tracer_init
,
415 .reset
= irqsoff_tracer_reset
,
416 .start
= irqsoff_tracer_start
,
417 .stop
= irqsoff_tracer_stop
,
419 #ifdef CONFIG_FTRACE_SELFTEST
420 .selftest
= trace_selftest_startup_irqsoff
,
423 # define register_irqsoff(trace) register_tracer(&trace)
425 # define register_irqsoff(trace) do { } while (0)
428 #ifdef CONFIG_PREEMPT_TRACER
429 static int preemptoff_tracer_init(struct trace_array
*tr
)
431 trace_type
= TRACER_PREEMPT_OFF
;
433 __irqsoff_tracer_init(tr
);
437 static struct tracer preemptoff_tracer __read_mostly
=
439 .name
= "preemptoff",
440 .init
= preemptoff_tracer_init
,
441 .reset
= irqsoff_tracer_reset
,
442 .start
= irqsoff_tracer_start
,
443 .stop
= irqsoff_tracer_stop
,
445 #ifdef CONFIG_FTRACE_SELFTEST
446 .selftest
= trace_selftest_startup_preemptoff
,
449 # define register_preemptoff(trace) register_tracer(&trace)
451 # define register_preemptoff(trace) do { } while (0)
454 #if defined(CONFIG_IRQSOFF_TRACER) && \
455 defined(CONFIG_PREEMPT_TRACER)
457 static int preemptirqsoff_tracer_init(struct trace_array
*tr
)
459 trace_type
= TRACER_IRQS_OFF
| TRACER_PREEMPT_OFF
;
461 __irqsoff_tracer_init(tr
);
465 static struct tracer preemptirqsoff_tracer __read_mostly
=
467 .name
= "preemptirqsoff",
468 .init
= preemptirqsoff_tracer_init
,
469 .reset
= irqsoff_tracer_reset
,
470 .start
= irqsoff_tracer_start
,
471 .stop
= irqsoff_tracer_stop
,
473 #ifdef CONFIG_FTRACE_SELFTEST
474 .selftest
= trace_selftest_startup_preemptirqsoff
,
478 # define register_preemptirqsoff(trace) register_tracer(&trace)
480 # define register_preemptirqsoff(trace) do { } while (0)
483 __init
static int init_irqsoff_tracer(void)
485 register_irqsoff(irqsoff_tracer
);
486 register_preemptoff(preemptoff_tracer
);
487 register_preemptirqsoff(preemptirqsoff_tracer
);
491 device_initcall(init_irqsoff_tracer
);