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/uaccess.h>
14 #include <linux/module.h>
15 #include <linux/ftrace.h>
19 static struct trace_array
*irqsoff_trace __read_mostly
;
20 static int tracer_enabled __read_mostly
;
22 static DEFINE_PER_CPU(int, tracing_cpu
);
24 static DEFINE_RAW_SPINLOCK(max_trace_lock
);
27 TRACER_IRQS_OFF
= (1 << 1),
28 TRACER_PREEMPT_OFF
= (1 << 2),
31 static int trace_type __read_mostly
;
33 static int save_flags
;
34 static bool function_enabled
;
36 static void stop_irqsoff_tracer(struct trace_array
*tr
, int graph
);
37 static int start_irqsoff_tracer(struct trace_array
*tr
, int graph
);
39 #ifdef CONFIG_PREEMPT_TRACER
43 return ((trace_type
& TRACER_PREEMPT_OFF
) && preempt_count());
46 # define preempt_trace() (0)
49 #ifdef CONFIG_IRQSOFF_TRACER
53 return ((trace_type
& TRACER_IRQS_OFF
) &&
57 # define irq_trace() (0)
60 #define TRACE_DISPLAY_GRAPH 1
62 static struct tracer_opt trace_opts
[] = {
63 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
64 /* display latency trace as call graph */
65 { TRACER_OPT(display
-graph
, TRACE_DISPLAY_GRAPH
) },
70 static struct tracer_flags tracer_flags
= {
75 #define is_graph() (tracer_flags.val & TRACE_DISPLAY_GRAPH)
78 * Sequence count - we record it when starting a measurement and
79 * skip the latency if the sequence has changed - some other section
80 * did a maximum and could disturb our measurement with serial console
81 * printouts, etc. Truly coinciding maximum latencies should be rare
82 * and what happens together happens separately as well, so this doesn't
83 * decrease the validity of the maximum found:
85 static __cacheline_aligned_in_smp
unsigned long max_sequence
;
87 #ifdef CONFIG_FUNCTION_TRACER
89 * Prologue for the preempt and irqs off function tracers.
91 * Returns 1 if it is OK to continue, and data->disabled is
93 * 0 if the trace is to be ignored, and data->disabled
96 * Note, this function is also used outside this ifdef but
97 * inside the #ifdef of the function graph tracer below.
98 * This is OK, since the function graph tracer is
99 * dependent on the function tracer.
101 static int func_prolog_dec(struct trace_array
*tr
,
102 struct trace_array_cpu
**data
,
103 unsigned long *flags
)
109 * Does not matter if we preempt. We test the flags
110 * afterward, to see if irqs are disabled or not.
111 * If we preempt and get a false positive, the flags
114 cpu
= raw_smp_processor_id();
115 if (likely(!per_cpu(tracing_cpu
, cpu
)))
118 local_save_flags(*flags
);
119 /* slight chance to get a false positive on tracing_cpu */
120 if (!irqs_disabled_flags(*flags
))
123 *data
= per_cpu_ptr(tr
->trace_buffer
.data
, cpu
);
124 disabled
= atomic_inc_return(&(*data
)->disabled
);
126 if (likely(disabled
== 1))
129 atomic_dec(&(*data
)->disabled
);
135 * irqsoff uses its own tracer function to keep the overhead down:
138 irqsoff_tracer_call(unsigned long ip
, unsigned long parent_ip
,
139 struct ftrace_ops
*op
, struct pt_regs
*pt_regs
)
141 struct trace_array
*tr
= irqsoff_trace
;
142 struct trace_array_cpu
*data
;
145 if (!func_prolog_dec(tr
, &data
, &flags
))
148 trace_function(tr
, ip
, parent_ip
, flags
, preempt_count());
150 atomic_dec(&data
->disabled
);
152 #endif /* CONFIG_FUNCTION_TRACER */
154 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
156 irqsoff_set_flag(struct trace_array
*tr
, u32 old_flags
, u32 bit
, int set
)
160 if (!(bit
& TRACE_DISPLAY_GRAPH
))
163 if (!(is_graph() ^ set
))
166 stop_irqsoff_tracer(irqsoff_trace
, !set
);
168 for_each_possible_cpu(cpu
)
169 per_cpu(tracing_cpu
, cpu
) = 0;
172 tracing_reset_online_cpus(&irqsoff_trace
->trace_buffer
);
174 return start_irqsoff_tracer(irqsoff_trace
, set
);
177 static int irqsoff_graph_entry(struct ftrace_graph_ent
*trace
)
179 struct trace_array
*tr
= irqsoff_trace
;
180 struct trace_array_cpu
*data
;
185 if (!func_prolog_dec(tr
, &data
, &flags
))
188 pc
= preempt_count();
189 ret
= __trace_graph_entry(tr
, trace
, flags
, pc
);
190 atomic_dec(&data
->disabled
);
195 static void irqsoff_graph_return(struct ftrace_graph_ret
*trace
)
197 struct trace_array
*tr
= irqsoff_trace
;
198 struct trace_array_cpu
*data
;
202 if (!func_prolog_dec(tr
, &data
, &flags
))
205 pc
= preempt_count();
206 __trace_graph_return(tr
, trace
, flags
, pc
);
207 atomic_dec(&data
->disabled
);
210 static void irqsoff_trace_open(struct trace_iterator
*iter
)
213 graph_trace_open(iter
);
217 static void irqsoff_trace_close(struct trace_iterator
*iter
)
220 graph_trace_close(iter
);
223 #define GRAPH_TRACER_FLAGS (TRACE_GRAPH_PRINT_CPU | \
224 TRACE_GRAPH_PRINT_PROC | \
225 TRACE_GRAPH_PRINT_ABS_TIME | \
226 TRACE_GRAPH_PRINT_DURATION)
228 static enum print_line_t
irqsoff_print_line(struct trace_iterator
*iter
)
231 * In graph mode call the graph tracer output function,
232 * otherwise go with the TRACE_FN event handler
235 return print_graph_function_flags(iter
, GRAPH_TRACER_FLAGS
);
237 return TRACE_TYPE_UNHANDLED
;
240 static void irqsoff_print_header(struct seq_file
*s
)
243 print_graph_headers_flags(s
, GRAPH_TRACER_FLAGS
);
245 trace_default_header(s
);
249 __trace_function(struct trace_array
*tr
,
250 unsigned long ip
, unsigned long parent_ip
,
251 unsigned long flags
, int pc
)
254 trace_graph_function(tr
, ip
, parent_ip
, flags
, pc
);
256 trace_function(tr
, ip
, parent_ip
, flags
, pc
);
260 #define __trace_function trace_function
263 irqsoff_set_flag(struct trace_array
*tr
, u32 old_flags
, u32 bit
, int set
)
268 static int irqsoff_graph_entry(struct ftrace_graph_ent
*trace
)
273 static enum print_line_t
irqsoff_print_line(struct trace_iterator
*iter
)
275 return TRACE_TYPE_UNHANDLED
;
278 static void irqsoff_graph_return(struct ftrace_graph_ret
*trace
) { }
279 static void irqsoff_trace_open(struct trace_iterator
*iter
) { }
280 static void irqsoff_trace_close(struct trace_iterator
*iter
) { }
282 #ifdef CONFIG_FUNCTION_TRACER
283 static void irqsoff_print_header(struct seq_file
*s
)
285 trace_default_header(s
);
288 static void irqsoff_print_header(struct seq_file
*s
)
290 trace_latency_header(s
);
292 #endif /* CONFIG_FUNCTION_TRACER */
293 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
296 * Should this new latency be reported/recorded?
298 static int report_latency(struct trace_array
*tr
, cycle_t delta
)
300 if (tracing_thresh
) {
301 if (delta
< tracing_thresh
)
304 if (delta
<= tr
->max_latency
)
311 check_critical_timing(struct trace_array
*tr
,
312 struct trace_array_cpu
*data
,
313 unsigned long parent_ip
,
316 cycle_t T0
, T1
, delta
;
320 T0
= data
->preempt_timestamp
;
321 T1
= ftrace_now(cpu
);
324 local_save_flags(flags
);
326 pc
= preempt_count();
328 if (!report_latency(tr
, delta
))
331 raw_spin_lock_irqsave(&max_trace_lock
, flags
);
333 /* check if we are still the max latency */
334 if (!report_latency(tr
, delta
))
337 __trace_function(tr
, CALLER_ADDR0
, parent_ip
, flags
, pc
);
338 /* Skip 5 functions to get to the irq/preempt enable function */
339 __trace_stack(tr
, flags
, 5, pc
);
341 if (data
->critical_sequence
!= max_sequence
)
344 data
->critical_end
= parent_ip
;
346 if (likely(!is_tracing_stopped())) {
347 tr
->max_latency
= delta
;
348 update_max_tr_single(tr
, current
, cpu
);
354 raw_spin_unlock_irqrestore(&max_trace_lock
, flags
);
357 data
->critical_sequence
= max_sequence
;
358 data
->preempt_timestamp
= ftrace_now(cpu
);
359 __trace_function(tr
, CALLER_ADDR0
, parent_ip
, flags
, pc
);
363 start_critical_timing(unsigned long ip
, unsigned long parent_ip
)
366 struct trace_array
*tr
= irqsoff_trace
;
367 struct trace_array_cpu
*data
;
370 if (!tracer_enabled
|| !tracing_is_enabled())
373 cpu
= raw_smp_processor_id();
375 if (per_cpu(tracing_cpu
, cpu
))
378 data
= per_cpu_ptr(tr
->trace_buffer
.data
, cpu
);
380 if (unlikely(!data
) || atomic_read(&data
->disabled
))
383 atomic_inc(&data
->disabled
);
385 data
->critical_sequence
= max_sequence
;
386 data
->preempt_timestamp
= ftrace_now(cpu
);
387 data
->critical_start
= parent_ip
? : ip
;
389 local_save_flags(flags
);
391 __trace_function(tr
, ip
, parent_ip
, flags
, preempt_count());
393 per_cpu(tracing_cpu
, cpu
) = 1;
395 atomic_dec(&data
->disabled
);
399 stop_critical_timing(unsigned long ip
, unsigned long parent_ip
)
402 struct trace_array
*tr
= irqsoff_trace
;
403 struct trace_array_cpu
*data
;
406 cpu
= raw_smp_processor_id();
407 /* Always clear the tracing cpu on stopping the trace */
408 if (unlikely(per_cpu(tracing_cpu
, cpu
)))
409 per_cpu(tracing_cpu
, cpu
) = 0;
413 if (!tracer_enabled
|| !tracing_is_enabled())
416 data
= per_cpu_ptr(tr
->trace_buffer
.data
, cpu
);
418 if (unlikely(!data
) ||
419 !data
->critical_start
|| atomic_read(&data
->disabled
))
422 atomic_inc(&data
->disabled
);
424 local_save_flags(flags
);
425 __trace_function(tr
, ip
, parent_ip
, flags
, preempt_count());
426 check_critical_timing(tr
, data
, parent_ip
? : ip
, cpu
);
427 data
->critical_start
= 0;
428 atomic_dec(&data
->disabled
);
431 /* start and stop critical timings used to for stoppage (in idle) */
432 void start_critical_timings(void)
434 if (preempt_trace() || irq_trace())
435 start_critical_timing(CALLER_ADDR0
, CALLER_ADDR1
);
437 EXPORT_SYMBOL_GPL(start_critical_timings
);
439 void stop_critical_timings(void)
441 if (preempt_trace() || irq_trace())
442 stop_critical_timing(CALLER_ADDR0
, CALLER_ADDR1
);
444 EXPORT_SYMBOL_GPL(stop_critical_timings
);
446 #ifdef CONFIG_IRQSOFF_TRACER
447 #ifdef CONFIG_PROVE_LOCKING
448 void time_hardirqs_on(unsigned long a0
, unsigned long a1
)
450 if (!preempt_trace() && irq_trace())
451 stop_critical_timing(a0
, a1
);
454 void time_hardirqs_off(unsigned long a0
, unsigned long a1
)
456 if (!preempt_trace() && irq_trace())
457 start_critical_timing(a0
, a1
);
460 #else /* !CONFIG_PROVE_LOCKING */
466 void trace_softirqs_on(unsigned long ip
)
470 void trace_softirqs_off(unsigned long ip
)
474 inline void print_irqtrace_events(struct task_struct
*curr
)
479 * We are only interested in hardirq on/off events:
481 void trace_hardirqs_on(void)
483 if (!preempt_trace() && irq_trace())
484 stop_critical_timing(CALLER_ADDR0
, CALLER_ADDR1
);
486 EXPORT_SYMBOL(trace_hardirqs_on
);
488 void trace_hardirqs_off(void)
490 if (!preempt_trace() && irq_trace())
491 start_critical_timing(CALLER_ADDR0
, CALLER_ADDR1
);
493 EXPORT_SYMBOL(trace_hardirqs_off
);
495 __visible
void trace_hardirqs_on_caller(unsigned long caller_addr
)
497 if (!preempt_trace() && irq_trace())
498 stop_critical_timing(CALLER_ADDR0
, caller_addr
);
500 EXPORT_SYMBOL(trace_hardirqs_on_caller
);
502 __visible
void trace_hardirqs_off_caller(unsigned long caller_addr
)
504 if (!preempt_trace() && irq_trace())
505 start_critical_timing(CALLER_ADDR0
, caller_addr
);
507 EXPORT_SYMBOL(trace_hardirqs_off_caller
);
509 #endif /* CONFIG_PROVE_LOCKING */
510 #endif /* CONFIG_IRQSOFF_TRACER */
512 #ifdef CONFIG_PREEMPT_TRACER
513 void trace_preempt_on(unsigned long a0
, unsigned long a1
)
515 if (preempt_trace() && !irq_trace())
516 stop_critical_timing(a0
, a1
);
519 void trace_preempt_off(unsigned long a0
, unsigned long a1
)
521 if (preempt_trace() && !irq_trace())
522 start_critical_timing(a0
, a1
);
524 #endif /* CONFIG_PREEMPT_TRACER */
526 static int register_irqsoff_function(struct trace_array
*tr
, int graph
, int set
)
530 /* 'set' is set if TRACE_ITER_FUNCTION is about to be set */
531 if (function_enabled
|| (!set
&& !(trace_flags
& TRACE_ITER_FUNCTION
)))
535 ret
= register_ftrace_graph(&irqsoff_graph_return
,
536 &irqsoff_graph_entry
);
538 ret
= register_ftrace_function(tr
->ops
);
541 function_enabled
= true;
546 static void unregister_irqsoff_function(struct trace_array
*tr
, int graph
)
548 if (!function_enabled
)
552 unregister_ftrace_graph();
554 unregister_ftrace_function(tr
->ops
);
556 function_enabled
= false;
559 static void irqsoff_function_set(struct trace_array
*tr
, int set
)
562 register_irqsoff_function(tr
, is_graph(), 1);
564 unregister_irqsoff_function(tr
, is_graph());
567 static int irqsoff_flag_changed(struct trace_array
*tr
, u32 mask
, int set
)
569 struct tracer
*tracer
= tr
->current_trace
;
571 if (mask
& TRACE_ITER_FUNCTION
)
572 irqsoff_function_set(tr
, set
);
574 return trace_keep_overwrite(tracer
, mask
, set
);
577 static int start_irqsoff_tracer(struct trace_array
*tr
, int graph
)
581 ret
= register_irqsoff_function(tr
, graph
, 0);
583 if (!ret
&& tracing_is_enabled())
591 static void stop_irqsoff_tracer(struct trace_array
*tr
, int graph
)
595 unregister_irqsoff_function(tr
, graph
);
598 static bool irqsoff_busy
;
600 static int __irqsoff_tracer_init(struct trace_array
*tr
)
605 save_flags
= trace_flags
;
607 /* non overwrite screws up the latency tracers */
608 set_tracer_flag(tr
, TRACE_ITER_OVERWRITE
, 1);
609 set_tracer_flag(tr
, TRACE_ITER_LATENCY_FMT
, 1);
613 /* make sure that the tracer is visible */
615 tracing_reset_online_cpus(&tr
->trace_buffer
);
617 ftrace_init_array_ops(tr
, irqsoff_tracer_call
);
619 /* Only toplevel instance supports graph tracing */
620 if (start_irqsoff_tracer(tr
, (tr
->flags
& TRACE_ARRAY_FL_GLOBAL
&&
622 printk(KERN_ERR
"failed to start irqsoff tracer\n");
628 static void irqsoff_tracer_reset(struct trace_array
*tr
)
630 int lat_flag
= save_flags
& TRACE_ITER_LATENCY_FMT
;
631 int overwrite_flag
= save_flags
& TRACE_ITER_OVERWRITE
;
633 stop_irqsoff_tracer(tr
, is_graph());
635 set_tracer_flag(tr
, TRACE_ITER_LATENCY_FMT
, lat_flag
);
636 set_tracer_flag(tr
, TRACE_ITER_OVERWRITE
, overwrite_flag
);
637 ftrace_reset_array_ops(tr
);
639 irqsoff_busy
= false;
642 static void irqsoff_tracer_start(struct trace_array
*tr
)
647 static void irqsoff_tracer_stop(struct trace_array
*tr
)
652 #ifdef CONFIG_IRQSOFF_TRACER
653 static int irqsoff_tracer_init(struct trace_array
*tr
)
655 trace_type
= TRACER_IRQS_OFF
;
657 return __irqsoff_tracer_init(tr
);
659 static struct tracer irqsoff_tracer __read_mostly
=
662 .init
= irqsoff_tracer_init
,
663 .reset
= irqsoff_tracer_reset
,
664 .start
= irqsoff_tracer_start
,
665 .stop
= irqsoff_tracer_stop
,
667 .print_header
= irqsoff_print_header
,
668 .print_line
= irqsoff_print_line
,
669 .flags
= &tracer_flags
,
670 .set_flag
= irqsoff_set_flag
,
671 .flag_changed
= irqsoff_flag_changed
,
672 #ifdef CONFIG_FTRACE_SELFTEST
673 .selftest
= trace_selftest_startup_irqsoff
,
675 .open
= irqsoff_trace_open
,
676 .close
= irqsoff_trace_close
,
677 .allow_instances
= true,
680 # define register_irqsoff(trace) register_tracer(&trace)
682 # define register_irqsoff(trace) do { } while (0)
685 #ifdef CONFIG_PREEMPT_TRACER
686 static int preemptoff_tracer_init(struct trace_array
*tr
)
688 trace_type
= TRACER_PREEMPT_OFF
;
690 return __irqsoff_tracer_init(tr
);
693 static struct tracer preemptoff_tracer __read_mostly
=
695 .name
= "preemptoff",
696 .init
= preemptoff_tracer_init
,
697 .reset
= irqsoff_tracer_reset
,
698 .start
= irqsoff_tracer_start
,
699 .stop
= irqsoff_tracer_stop
,
701 .print_header
= irqsoff_print_header
,
702 .print_line
= irqsoff_print_line
,
703 .flags
= &tracer_flags
,
704 .set_flag
= irqsoff_set_flag
,
705 .flag_changed
= irqsoff_flag_changed
,
706 #ifdef CONFIG_FTRACE_SELFTEST
707 .selftest
= trace_selftest_startup_preemptoff
,
709 .open
= irqsoff_trace_open
,
710 .close
= irqsoff_trace_close
,
711 .allow_instances
= true,
714 # define register_preemptoff(trace) register_tracer(&trace)
716 # define register_preemptoff(trace) do { } while (0)
719 #if defined(CONFIG_IRQSOFF_TRACER) && \
720 defined(CONFIG_PREEMPT_TRACER)
722 static int preemptirqsoff_tracer_init(struct trace_array
*tr
)
724 trace_type
= TRACER_IRQS_OFF
| TRACER_PREEMPT_OFF
;
726 return __irqsoff_tracer_init(tr
);
729 static struct tracer preemptirqsoff_tracer __read_mostly
=
731 .name
= "preemptirqsoff",
732 .init
= preemptirqsoff_tracer_init
,
733 .reset
= irqsoff_tracer_reset
,
734 .start
= irqsoff_tracer_start
,
735 .stop
= irqsoff_tracer_stop
,
737 .print_header
= irqsoff_print_header
,
738 .print_line
= irqsoff_print_line
,
739 .flags
= &tracer_flags
,
740 .set_flag
= irqsoff_set_flag
,
741 .flag_changed
= irqsoff_flag_changed
,
742 #ifdef CONFIG_FTRACE_SELFTEST
743 .selftest
= trace_selftest_startup_preemptirqsoff
,
745 .open
= irqsoff_trace_open
,
746 .close
= irqsoff_trace_close
,
747 .allow_instances
= true,
751 # define register_preemptirqsoff(trace) register_tracer(&trace)
753 # define register_preemptirqsoff(trace) do { } while (0)
756 __init
static int init_irqsoff_tracer(void)
758 register_irqsoff(irqsoff_tracer
);
759 register_preemptoff(preemptoff_tracer
);
760 register_preemptirqsoff(preemptirqsoff_tracer
);
764 core_initcall(init_irqsoff_tracer
);