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 Nadia Yvette Chambers
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_RAW_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_flags
;
36 static bool function_enabled
;
38 static void stop_irqsoff_tracer(struct trace_array
*tr
, int graph
);
39 static int start_irqsoff_tracer(struct trace_array
*tr
, int graph
);
41 #ifdef CONFIG_PREEMPT_TRACER
45 return ((trace_type
& TRACER_PREEMPT_OFF
) && preempt_count());
48 # define preempt_trace() (0)
51 #ifdef CONFIG_IRQSOFF_TRACER
55 return ((trace_type
& TRACER_IRQS_OFF
) &&
59 # define irq_trace() (0)
62 #define TRACE_DISPLAY_GRAPH 1
64 static struct tracer_opt trace_opts
[] = {
65 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
66 /* display latency trace as call graph */
67 { TRACER_OPT(display
-graph
, TRACE_DISPLAY_GRAPH
) },
72 static struct tracer_flags tracer_flags
= {
77 #define is_graph() (tracer_flags.val & TRACE_DISPLAY_GRAPH)
80 * Sequence count - we record it when starting a measurement and
81 * skip the latency if the sequence has changed - some other section
82 * did a maximum and could disturb our measurement with serial console
83 * printouts, etc. Truly coinciding maximum latencies should be rare
84 * and what happens together happens separately as well, so this doesn't
85 * decrease the validity of the maximum found:
87 static __cacheline_aligned_in_smp
unsigned long max_sequence
;
89 #ifdef CONFIG_FUNCTION_TRACER
91 * Prologue for the preempt and irqs off function tracers.
93 * Returns 1 if it is OK to continue, and data->disabled is
95 * 0 if the trace is to be ignored, and data->disabled
98 * Note, this function is also used outside this ifdef but
99 * inside the #ifdef of the function graph tracer below.
100 * This is OK, since the function graph tracer is
101 * dependent on the function tracer.
103 static int func_prolog_dec(struct trace_array
*tr
,
104 struct trace_array_cpu
**data
,
105 unsigned long *flags
)
111 * Does not matter if we preempt. We test the flags
112 * afterward, to see if irqs are disabled or not.
113 * If we preempt and get a false positive, the flags
116 cpu
= raw_smp_processor_id();
117 if (likely(!per_cpu(tracing_cpu
, cpu
)))
120 local_save_flags(*flags
);
121 /* slight chance to get a false positive on tracing_cpu */
122 if (!irqs_disabled_flags(*flags
))
125 *data
= per_cpu_ptr(tr
->trace_buffer
.data
, cpu
);
126 disabled
= atomic_inc_return(&(*data
)->disabled
);
128 if (likely(disabled
== 1))
131 atomic_dec(&(*data
)->disabled
);
137 * irqsoff uses its own tracer function to keep the overhead down:
140 irqsoff_tracer_call(unsigned long ip
, unsigned long parent_ip
,
141 struct ftrace_ops
*op
, struct pt_regs
*pt_regs
)
143 struct trace_array
*tr
= irqsoff_trace
;
144 struct trace_array_cpu
*data
;
147 if (!func_prolog_dec(tr
, &data
, &flags
))
150 trace_function(tr
, ip
, parent_ip
, flags
, preempt_count());
152 atomic_dec(&data
->disabled
);
155 static struct ftrace_ops trace_ops __read_mostly
=
157 .func
= irqsoff_tracer_call
,
158 .flags
= FTRACE_OPS_FL_GLOBAL
| FTRACE_OPS_FL_RECURSION_SAFE
,
160 #endif /* CONFIG_FUNCTION_TRACER */
162 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
163 static int irqsoff_set_flag(u32 old_flags
, u32 bit
, int set
)
167 if (!(bit
& TRACE_DISPLAY_GRAPH
))
170 if (!(is_graph() ^ set
))
173 stop_irqsoff_tracer(irqsoff_trace
, !set
);
175 for_each_possible_cpu(cpu
)
176 per_cpu(tracing_cpu
, cpu
) = 0;
178 tracing_max_latency
= 0;
179 tracing_reset_online_cpus(&irqsoff_trace
->trace_buffer
);
181 return start_irqsoff_tracer(irqsoff_trace
, set
);
184 static int irqsoff_graph_entry(struct ftrace_graph_ent
*trace
)
186 struct trace_array
*tr
= irqsoff_trace
;
187 struct trace_array_cpu
*data
;
192 if (!func_prolog_dec(tr
, &data
, &flags
))
195 pc
= preempt_count();
196 ret
= __trace_graph_entry(tr
, trace
, flags
, pc
);
197 atomic_dec(&data
->disabled
);
202 static void irqsoff_graph_return(struct ftrace_graph_ret
*trace
)
204 struct trace_array
*tr
= irqsoff_trace
;
205 struct trace_array_cpu
*data
;
209 if (!func_prolog_dec(tr
, &data
, &flags
))
212 pc
= preempt_count();
213 __trace_graph_return(tr
, trace
, flags
, pc
);
214 atomic_dec(&data
->disabled
);
217 static void irqsoff_trace_open(struct trace_iterator
*iter
)
220 graph_trace_open(iter
);
224 static void irqsoff_trace_close(struct trace_iterator
*iter
)
227 graph_trace_close(iter
);
230 #define GRAPH_TRACER_FLAGS (TRACE_GRAPH_PRINT_CPU | \
231 TRACE_GRAPH_PRINT_PROC | \
232 TRACE_GRAPH_PRINT_ABS_TIME | \
233 TRACE_GRAPH_PRINT_DURATION)
235 static enum print_line_t
irqsoff_print_line(struct trace_iterator
*iter
)
238 * In graph mode call the graph tracer output function,
239 * otherwise go with the TRACE_FN event handler
242 return print_graph_function_flags(iter
, GRAPH_TRACER_FLAGS
);
244 return TRACE_TYPE_UNHANDLED
;
247 static void irqsoff_print_header(struct seq_file
*s
)
250 print_graph_headers_flags(s
, GRAPH_TRACER_FLAGS
);
252 trace_default_header(s
);
256 __trace_function(struct trace_array
*tr
,
257 unsigned long ip
, unsigned long parent_ip
,
258 unsigned long flags
, int pc
)
261 trace_graph_function(tr
, ip
, parent_ip
, flags
, pc
);
263 trace_function(tr
, ip
, parent_ip
, flags
, pc
);
267 #define __trace_function trace_function
269 static int irqsoff_set_flag(u32 old_flags
, u32 bit
, int set
)
274 static int irqsoff_graph_entry(struct ftrace_graph_ent
*trace
)
279 static enum print_line_t
irqsoff_print_line(struct trace_iterator
*iter
)
281 return TRACE_TYPE_UNHANDLED
;
284 static void irqsoff_graph_return(struct ftrace_graph_ret
*trace
) { }
285 static void irqsoff_trace_open(struct trace_iterator
*iter
) { }
286 static void irqsoff_trace_close(struct trace_iterator
*iter
) { }
288 #ifdef CONFIG_FUNCTION_TRACER
289 static void irqsoff_print_header(struct seq_file
*s
)
291 trace_default_header(s
);
294 static void irqsoff_print_header(struct seq_file
*s
)
296 trace_latency_header(s
);
298 #endif /* CONFIG_FUNCTION_TRACER */
299 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
302 * Should this new latency be reported/recorded?
304 static int report_latency(cycle_t delta
)
306 if (tracing_thresh
) {
307 if (delta
< tracing_thresh
)
310 if (delta
<= tracing_max_latency
)
317 check_critical_timing(struct trace_array
*tr
,
318 struct trace_array_cpu
*data
,
319 unsigned long parent_ip
,
322 cycle_t T0
, T1
, delta
;
326 T0
= data
->preempt_timestamp
;
327 T1
= ftrace_now(cpu
);
330 local_save_flags(flags
);
332 pc
= preempt_count();
334 if (!report_latency(delta
))
337 raw_spin_lock_irqsave(&max_trace_lock
, flags
);
339 /* check if we are still the max latency */
340 if (!report_latency(delta
))
343 __trace_function(tr
, CALLER_ADDR0
, parent_ip
, flags
, pc
);
344 /* Skip 5 functions to get to the irq/preempt enable function */
345 __trace_stack(tr
, flags
, 5, pc
);
347 if (data
->critical_sequence
!= max_sequence
)
350 data
->critical_end
= parent_ip
;
352 if (likely(!is_tracing_stopped())) {
353 tracing_max_latency
= delta
;
354 update_max_tr_single(tr
, current
, cpu
);
360 raw_spin_unlock_irqrestore(&max_trace_lock
, flags
);
363 data
->critical_sequence
= max_sequence
;
364 data
->preempt_timestamp
= ftrace_now(cpu
);
365 __trace_function(tr
, CALLER_ADDR0
, parent_ip
, flags
, pc
);
369 start_critical_timing(unsigned long ip
, unsigned long parent_ip
)
372 struct trace_array
*tr
= irqsoff_trace
;
373 struct trace_array_cpu
*data
;
376 if (!tracer_enabled
|| !tracing_is_enabled())
379 cpu
= raw_smp_processor_id();
381 if (per_cpu(tracing_cpu
, cpu
))
384 data
= per_cpu_ptr(tr
->trace_buffer
.data
, cpu
);
386 if (unlikely(!data
) || atomic_read(&data
->disabled
))
389 atomic_inc(&data
->disabled
);
391 data
->critical_sequence
= max_sequence
;
392 data
->preempt_timestamp
= ftrace_now(cpu
);
393 data
->critical_start
= parent_ip
? : ip
;
395 local_save_flags(flags
);
397 __trace_function(tr
, ip
, parent_ip
, flags
, preempt_count());
399 per_cpu(tracing_cpu
, cpu
) = 1;
401 atomic_dec(&data
->disabled
);
405 stop_critical_timing(unsigned long ip
, unsigned long parent_ip
)
408 struct trace_array
*tr
= irqsoff_trace
;
409 struct trace_array_cpu
*data
;
412 cpu
= raw_smp_processor_id();
413 /* Always clear the tracing cpu on stopping the trace */
414 if (unlikely(per_cpu(tracing_cpu
, cpu
)))
415 per_cpu(tracing_cpu
, cpu
) = 0;
419 if (!tracer_enabled
|| !tracing_is_enabled())
422 data
= per_cpu_ptr(tr
->trace_buffer
.data
, cpu
);
424 if (unlikely(!data
) ||
425 !data
->critical_start
|| atomic_read(&data
->disabled
))
428 atomic_inc(&data
->disabled
);
430 local_save_flags(flags
);
431 __trace_function(tr
, ip
, parent_ip
, flags
, preempt_count());
432 check_critical_timing(tr
, data
, parent_ip
? : ip
, cpu
);
433 data
->critical_start
= 0;
434 atomic_dec(&data
->disabled
);
437 /* start and stop critical timings used to for stoppage (in idle) */
438 void start_critical_timings(void)
440 if (preempt_trace() || irq_trace())
441 start_critical_timing(CALLER_ADDR0
, CALLER_ADDR1
);
443 EXPORT_SYMBOL_GPL(start_critical_timings
);
445 void stop_critical_timings(void)
447 if (preempt_trace() || irq_trace())
448 stop_critical_timing(CALLER_ADDR0
, CALLER_ADDR1
);
450 EXPORT_SYMBOL_GPL(stop_critical_timings
);
452 #ifdef CONFIG_IRQSOFF_TRACER
453 #ifdef CONFIG_PROVE_LOCKING
454 void time_hardirqs_on(unsigned long a0
, unsigned long a1
)
456 if (!preempt_trace() && irq_trace())
457 stop_critical_timing(a0
, a1
);
460 void time_hardirqs_off(unsigned long a0
, unsigned long a1
)
462 if (!preempt_trace() && irq_trace())
463 start_critical_timing(a0
, a1
);
466 #else /* !CONFIG_PROVE_LOCKING */
472 void trace_softirqs_on(unsigned long ip
)
476 void trace_softirqs_off(unsigned long ip
)
480 inline void print_irqtrace_events(struct task_struct
*curr
)
485 * We are only interested in hardirq on/off events:
487 void trace_hardirqs_on(void)
489 if (!preempt_trace() && irq_trace())
490 stop_critical_timing(CALLER_ADDR0
, CALLER_ADDR1
);
492 EXPORT_SYMBOL(trace_hardirqs_on
);
494 void trace_hardirqs_off(void)
496 if (!preempt_trace() && irq_trace())
497 start_critical_timing(CALLER_ADDR0
, CALLER_ADDR1
);
499 EXPORT_SYMBOL(trace_hardirqs_off
);
501 void trace_hardirqs_on_caller(unsigned long caller_addr
)
503 if (!preempt_trace() && irq_trace())
504 stop_critical_timing(CALLER_ADDR0
, caller_addr
);
506 EXPORT_SYMBOL(trace_hardirqs_on_caller
);
508 void trace_hardirqs_off_caller(unsigned long caller_addr
)
510 if (!preempt_trace() && irq_trace())
511 start_critical_timing(CALLER_ADDR0
, caller_addr
);
513 EXPORT_SYMBOL(trace_hardirqs_off_caller
);
515 #endif /* CONFIG_PROVE_LOCKING */
516 #endif /* CONFIG_IRQSOFF_TRACER */
518 #ifdef CONFIG_PREEMPT_TRACER
519 void trace_preempt_on(unsigned long a0
, unsigned long a1
)
521 if (preempt_trace() && !irq_trace())
522 stop_critical_timing(a0
, a1
);
525 void trace_preempt_off(unsigned long a0
, unsigned long a1
)
527 if (preempt_trace() && !irq_trace())
528 start_critical_timing(a0
, a1
);
530 #endif /* CONFIG_PREEMPT_TRACER */
532 static int register_irqsoff_function(int graph
, int set
)
536 /* 'set' is set if TRACE_ITER_FUNCTION is about to be set */
537 if (function_enabled
|| (!set
&& !(trace_flags
& TRACE_ITER_FUNCTION
)))
541 ret
= register_ftrace_graph(&irqsoff_graph_return
,
542 &irqsoff_graph_entry
);
544 ret
= register_ftrace_function(&trace_ops
);
547 function_enabled
= true;
552 static void unregister_irqsoff_function(int graph
)
554 if (!function_enabled
)
558 unregister_ftrace_graph();
560 unregister_ftrace_function(&trace_ops
);
562 function_enabled
= false;
565 static void irqsoff_function_set(int set
)
568 register_irqsoff_function(is_graph(), 1);
570 unregister_irqsoff_function(is_graph());
573 static int irqsoff_flag_changed(struct tracer
*tracer
, u32 mask
, int set
)
575 if (mask
& TRACE_ITER_FUNCTION
)
576 irqsoff_function_set(set
);
578 return trace_keep_overwrite(tracer
, mask
, set
);
581 static int start_irqsoff_tracer(struct trace_array
*tr
, int graph
)
585 ret
= register_irqsoff_function(graph
, 0);
587 if (!ret
&& tracing_is_enabled())
595 static void stop_irqsoff_tracer(struct trace_array
*tr
, int graph
)
599 unregister_irqsoff_function(graph
);
602 static void __irqsoff_tracer_init(struct trace_array
*tr
)
604 save_flags
= trace_flags
;
606 /* non overwrite screws up the latency tracers */
607 set_tracer_flag(tr
, TRACE_ITER_OVERWRITE
, 1);
608 set_tracer_flag(tr
, TRACE_ITER_LATENCY_FMT
, 1);
610 tracing_max_latency
= 0;
612 /* make sure that the tracer is visible */
614 tracing_reset_online_cpus(&tr
->trace_buffer
);
616 if (start_irqsoff_tracer(tr
, is_graph()))
617 printk(KERN_ERR
"failed to start irqsoff tracer\n");
620 static void irqsoff_tracer_reset(struct trace_array
*tr
)
622 int lat_flag
= save_flags
& TRACE_ITER_LATENCY_FMT
;
623 int overwrite_flag
= save_flags
& TRACE_ITER_OVERWRITE
;
625 stop_irqsoff_tracer(tr
, is_graph());
627 set_tracer_flag(tr
, TRACE_ITER_LATENCY_FMT
, lat_flag
);
628 set_tracer_flag(tr
, TRACE_ITER_OVERWRITE
, overwrite_flag
);
631 static void irqsoff_tracer_start(struct trace_array
*tr
)
636 static void irqsoff_tracer_stop(struct trace_array
*tr
)
641 #ifdef CONFIG_IRQSOFF_TRACER
642 static int irqsoff_tracer_init(struct trace_array
*tr
)
644 trace_type
= TRACER_IRQS_OFF
;
646 __irqsoff_tracer_init(tr
);
649 static struct tracer irqsoff_tracer __read_mostly
=
652 .init
= irqsoff_tracer_init
,
653 .reset
= irqsoff_tracer_reset
,
654 .start
= irqsoff_tracer_start
,
655 .stop
= irqsoff_tracer_stop
,
657 .print_header
= irqsoff_print_header
,
658 .print_line
= irqsoff_print_line
,
659 .flags
= &tracer_flags
,
660 .set_flag
= irqsoff_set_flag
,
661 .flag_changed
= irqsoff_flag_changed
,
662 #ifdef CONFIG_FTRACE_SELFTEST
663 .selftest
= trace_selftest_startup_irqsoff
,
665 .open
= irqsoff_trace_open
,
666 .close
= irqsoff_trace_close
,
669 # define register_irqsoff(trace) register_tracer(&trace)
671 # define register_irqsoff(trace) do { } while (0)
674 #ifdef CONFIG_PREEMPT_TRACER
675 static int preemptoff_tracer_init(struct trace_array
*tr
)
677 trace_type
= TRACER_PREEMPT_OFF
;
679 __irqsoff_tracer_init(tr
);
683 static struct tracer preemptoff_tracer __read_mostly
=
685 .name
= "preemptoff",
686 .init
= preemptoff_tracer_init
,
687 .reset
= irqsoff_tracer_reset
,
688 .start
= irqsoff_tracer_start
,
689 .stop
= irqsoff_tracer_stop
,
691 .print_header
= irqsoff_print_header
,
692 .print_line
= irqsoff_print_line
,
693 .flags
= &tracer_flags
,
694 .set_flag
= irqsoff_set_flag
,
695 .flag_changed
= irqsoff_flag_changed
,
696 #ifdef CONFIG_FTRACE_SELFTEST
697 .selftest
= trace_selftest_startup_preemptoff
,
699 .open
= irqsoff_trace_open
,
700 .close
= irqsoff_trace_close
,
703 # define register_preemptoff(trace) register_tracer(&trace)
705 # define register_preemptoff(trace) do { } while (0)
708 #if defined(CONFIG_IRQSOFF_TRACER) && \
709 defined(CONFIG_PREEMPT_TRACER)
711 static int preemptirqsoff_tracer_init(struct trace_array
*tr
)
713 trace_type
= TRACER_IRQS_OFF
| TRACER_PREEMPT_OFF
;
715 __irqsoff_tracer_init(tr
);
719 static struct tracer preemptirqsoff_tracer __read_mostly
=
721 .name
= "preemptirqsoff",
722 .init
= preemptirqsoff_tracer_init
,
723 .reset
= irqsoff_tracer_reset
,
724 .start
= irqsoff_tracer_start
,
725 .stop
= irqsoff_tracer_stop
,
727 .print_header
= irqsoff_print_header
,
728 .print_line
= irqsoff_print_line
,
729 .flags
= &tracer_flags
,
730 .set_flag
= irqsoff_set_flag
,
731 .flag_changed
= irqsoff_flag_changed
,
732 #ifdef CONFIG_FTRACE_SELFTEST
733 .selftest
= trace_selftest_startup_preemptirqsoff
,
735 .open
= irqsoff_trace_open
,
736 .close
= irqsoff_trace_close
,
740 # define register_preemptirqsoff(trace) register_tracer(&trace)
742 # define register_preemptirqsoff(trace) do { } while (0)
745 __init
static int init_irqsoff_tracer(void)
747 register_irqsoff(irqsoff_tracer
);
748 register_preemptoff(preemptoff_tracer
);
749 register_preemptirqsoff(preemptirqsoff_tracer
);
753 core_initcall(init_irqsoff_tracer
);