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);
154 #endif /* CONFIG_FUNCTION_TRACER */
156 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
158 irqsoff_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
162 if (!(bit & TRACE_DISPLAY_GRAPH))
165 if (!(is_graph() ^ set))
168 stop_irqsoff_tracer(irqsoff_trace, !set);
170 for_each_possible_cpu(cpu)
171 per_cpu(tracing_cpu, cpu) = 0;
174 tracing_reset_online_cpus(&irqsoff_trace->trace_buffer);
176 return start_irqsoff_tracer(irqsoff_trace, set);
179 static int irqsoff_graph_entry(struct ftrace_graph_ent *trace)
181 struct trace_array *tr = irqsoff_trace;
182 struct trace_array_cpu *data;
187 if (!func_prolog_dec(tr, &data, &flags))
190 pc = preempt_count();
191 ret = __trace_graph_entry(tr, trace, flags, pc);
192 atomic_dec(&data->disabled);
197 static void irqsoff_graph_return(struct ftrace_graph_ret *trace)
199 struct trace_array *tr = irqsoff_trace;
200 struct trace_array_cpu *data;
204 if (!func_prolog_dec(tr, &data, &flags))
207 pc = preempt_count();
208 __trace_graph_return(tr, trace, flags, pc);
209 atomic_dec(&data->disabled);
212 static void irqsoff_trace_open(struct trace_iterator *iter)
215 graph_trace_open(iter);
219 static void irqsoff_trace_close(struct trace_iterator *iter)
222 graph_trace_close(iter);
225 #define GRAPH_TRACER_FLAGS (TRACE_GRAPH_PRINT_CPU | \
226 TRACE_GRAPH_PRINT_PROC | \
227 TRACE_GRAPH_PRINT_ABS_TIME | \
228 TRACE_GRAPH_PRINT_DURATION)
230 static enum print_line_t irqsoff_print_line(struct trace_iterator *iter)
233 * In graph mode call the graph tracer output function,
234 * otherwise go with the TRACE_FN event handler
237 return print_graph_function_flags(iter, GRAPH_TRACER_FLAGS);
239 return TRACE_TYPE_UNHANDLED;
242 static void irqsoff_print_header(struct seq_file *s)
245 print_graph_headers_flags(s, GRAPH_TRACER_FLAGS);
247 trace_default_header(s);
251 __trace_function(struct trace_array *tr,
252 unsigned long ip, unsigned long parent_ip,
253 unsigned long flags, int pc)
256 trace_graph_function(tr, ip, parent_ip, flags, pc);
258 trace_function(tr, ip, parent_ip, flags, pc);
262 #define __trace_function trace_function
265 irqsoff_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
270 static int irqsoff_graph_entry(struct ftrace_graph_ent *trace)
275 static enum print_line_t irqsoff_print_line(struct trace_iterator *iter)
277 return TRACE_TYPE_UNHANDLED;
280 static void irqsoff_graph_return(struct ftrace_graph_ret *trace) { }
281 static void irqsoff_trace_open(struct trace_iterator *iter) { }
282 static void irqsoff_trace_close(struct trace_iterator *iter) { }
284 #ifdef CONFIG_FUNCTION_TRACER
285 static void irqsoff_print_header(struct seq_file *s)
287 trace_default_header(s);
290 static void irqsoff_print_header(struct seq_file *s)
292 trace_latency_header(s);
294 #endif /* CONFIG_FUNCTION_TRACER */
295 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
298 * Should this new latency be reported/recorded?
300 static int report_latency(struct trace_array *tr, cycle_t delta)
302 if (tracing_thresh) {
303 if (delta < tracing_thresh)
306 if (delta <= tr->max_latency)
313 check_critical_timing(struct trace_array *tr,
314 struct trace_array_cpu *data,
315 unsigned long parent_ip,
318 cycle_t T0, T1, delta;
322 T0 = data->preempt_timestamp;
323 T1 = ftrace_now(cpu);
326 local_save_flags(flags);
328 pc = preempt_count();
330 if (!report_latency(tr, delta))
333 raw_spin_lock_irqsave(&max_trace_lock, flags);
335 /* check if we are still the max latency */
336 if (!report_latency(tr, delta))
339 __trace_function(tr, CALLER_ADDR0, parent_ip, flags, pc);
340 /* Skip 5 functions to get to the irq/preempt enable function */
341 __trace_stack(tr, flags, 5, pc);
343 if (data->critical_sequence != max_sequence)
346 data->critical_end = parent_ip;
348 if (likely(!is_tracing_stopped())) {
349 tr->max_latency = delta;
350 update_max_tr_single(tr, current, cpu);
356 raw_spin_unlock_irqrestore(&max_trace_lock, flags);
359 data->critical_sequence = max_sequence;
360 data->preempt_timestamp = ftrace_now(cpu);
361 __trace_function(tr, CALLER_ADDR0, parent_ip, flags, pc);
365 start_critical_timing(unsigned long ip, unsigned long parent_ip)
368 struct trace_array *tr = irqsoff_trace;
369 struct trace_array_cpu *data;
372 if (!tracer_enabled || !tracing_is_enabled())
375 cpu = raw_smp_processor_id();
377 if (per_cpu(tracing_cpu, cpu))
380 data = per_cpu_ptr(tr->trace_buffer.data, cpu);
382 if (unlikely(!data) || atomic_read(&data->disabled))
385 atomic_inc(&data->disabled);
387 data->critical_sequence = max_sequence;
388 data->preempt_timestamp = ftrace_now(cpu);
389 data->critical_start = parent_ip ? : ip;
391 local_save_flags(flags);
393 __trace_function(tr, ip, parent_ip, flags, preempt_count());
395 per_cpu(tracing_cpu, cpu) = 1;
397 atomic_dec(&data->disabled);
401 stop_critical_timing(unsigned long ip, unsigned long parent_ip)
404 struct trace_array *tr = irqsoff_trace;
405 struct trace_array_cpu *data;
408 cpu = raw_smp_processor_id();
409 /* Always clear the tracing cpu on stopping the trace */
410 if (unlikely(per_cpu(tracing_cpu, cpu)))
411 per_cpu(tracing_cpu, cpu) = 0;
415 if (!tracer_enabled || !tracing_is_enabled())
418 data = per_cpu_ptr(tr->trace_buffer.data, cpu);
420 if (unlikely(!data) ||
421 !data->critical_start || atomic_read(&data->disabled))
424 atomic_inc(&data->disabled);
426 local_save_flags(flags);
427 __trace_function(tr, ip, parent_ip, flags, preempt_count());
428 check_critical_timing(tr, data, parent_ip ? : ip, cpu);
429 data->critical_start = 0;
430 atomic_dec(&data->disabled);
433 /* start and stop critical timings used to for stoppage (in idle) */
434 void start_critical_timings(void)
436 if (preempt_trace() || irq_trace())
437 start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
439 EXPORT_SYMBOL_GPL(start_critical_timings);
441 void stop_critical_timings(void)
443 if (preempt_trace() || irq_trace())
444 stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
446 EXPORT_SYMBOL_GPL(stop_critical_timings);
448 #ifdef CONFIG_IRQSOFF_TRACER
449 #ifdef CONFIG_PROVE_LOCKING
450 void time_hardirqs_on(unsigned long a0, unsigned long a1)
452 if (!preempt_trace() && irq_trace())
453 stop_critical_timing(a0, a1);
456 void time_hardirqs_off(unsigned long a0, unsigned long a1)
458 if (!preempt_trace() && irq_trace())
459 start_critical_timing(a0, a1);
462 #else /* !CONFIG_PROVE_LOCKING */
468 void trace_softirqs_on(unsigned long ip)
472 void trace_softirqs_off(unsigned long ip)
476 inline void print_irqtrace_events(struct task_struct *curr)
481 * We are only interested in hardirq on/off events:
483 void trace_hardirqs_on(void)
485 if (!preempt_trace() && irq_trace())
486 stop_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
488 EXPORT_SYMBOL(trace_hardirqs_on);
490 void trace_hardirqs_off(void)
492 if (!preempt_trace() && irq_trace())
493 start_critical_timing(CALLER_ADDR0, CALLER_ADDR1);
495 EXPORT_SYMBOL(trace_hardirqs_off);
497 __visible void trace_hardirqs_on_caller(unsigned long caller_addr)
499 if (!preempt_trace() && irq_trace())
500 stop_critical_timing(CALLER_ADDR0, caller_addr);
502 EXPORT_SYMBOL(trace_hardirqs_on_caller);
504 __visible void trace_hardirqs_off_caller(unsigned long caller_addr)
506 if (!preempt_trace() && irq_trace())
507 start_critical_timing(CALLER_ADDR0, caller_addr);
509 EXPORT_SYMBOL(trace_hardirqs_off_caller);
511 #endif /* CONFIG_PROVE_LOCKING */
512 #endif /* CONFIG_IRQSOFF_TRACER */
514 #ifdef CONFIG_PREEMPT_TRACER
515 void trace_preempt_on(unsigned long a0, unsigned long a1)
517 if (preempt_trace() && !irq_trace())
518 stop_critical_timing(a0, a1);
521 void trace_preempt_off(unsigned long a0, unsigned long a1)
523 if (preempt_trace() && !irq_trace())
524 start_critical_timing(a0, a1);
526 #endif /* CONFIG_PREEMPT_TRACER */
528 static int register_irqsoff_function(struct trace_array *tr, int graph, int set)
532 /* 'set' is set if TRACE_ITER_FUNCTION is about to be set */
533 if (function_enabled || (!set && !(trace_flags & TRACE_ITER_FUNCTION)))
537 ret = register_ftrace_graph(&irqsoff_graph_return,
538 &irqsoff_graph_entry);
540 ret = register_ftrace_function(tr->ops);
543 function_enabled = true;
548 static void unregister_irqsoff_function(struct trace_array *tr, int graph)
550 if (!function_enabled)
554 unregister_ftrace_graph();
556 unregister_ftrace_function(tr->ops);
558 function_enabled = false;
561 static void irqsoff_function_set(struct trace_array *tr, int set)
564 register_irqsoff_function(tr, is_graph(), 1);
566 unregister_irqsoff_function(tr, is_graph());
569 static int irqsoff_flag_changed(struct trace_array *tr, u32 mask, int set)
571 struct tracer *tracer = tr->current_trace;
573 if (mask & TRACE_ITER_FUNCTION)
574 irqsoff_function_set(tr, set);
576 return trace_keep_overwrite(tracer, mask, set);
579 static int start_irqsoff_tracer(struct trace_array *tr, int graph)
583 ret = register_irqsoff_function(tr, graph, 0);
585 if (!ret && tracing_is_enabled())
593 static void stop_irqsoff_tracer(struct trace_array *tr, int graph)
597 unregister_irqsoff_function(tr, graph);
600 static bool irqsoff_busy;
602 static int __irqsoff_tracer_init(struct trace_array *tr)
607 save_flags = trace_flags;
609 /* non overwrite screws up the latency tracers */
610 set_tracer_flag(tr, TRACE_ITER_OVERWRITE, 1);
611 set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, 1);
615 /* make sure that the tracer is visible */
617 tracing_reset_online_cpus(&tr->trace_buffer);
619 ftrace_init_array_ops(tr, irqsoff_tracer_call);
621 /* Only toplevel instance supports graph tracing */
622 if (start_irqsoff_tracer(tr, (tr->flags & TRACE_ARRAY_FL_GLOBAL &&
624 printk(KERN_ERR "failed to start irqsoff tracer\n");
630 static void irqsoff_tracer_reset(struct trace_array *tr)
632 int lat_flag = save_flags & TRACE_ITER_LATENCY_FMT;
633 int overwrite_flag = save_flags & TRACE_ITER_OVERWRITE;
635 stop_irqsoff_tracer(tr, is_graph());
637 set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, lat_flag);
638 set_tracer_flag(tr, TRACE_ITER_OVERWRITE, overwrite_flag);
639 ftrace_reset_array_ops(tr);
641 irqsoff_busy = false;
644 static void irqsoff_tracer_start(struct trace_array *tr)
649 static void irqsoff_tracer_stop(struct trace_array *tr)
654 #ifdef CONFIG_IRQSOFF_TRACER
655 static int irqsoff_tracer_init(struct trace_array *tr)
657 trace_type = TRACER_IRQS_OFF;
659 return __irqsoff_tracer_init(tr);
661 static struct tracer irqsoff_tracer __read_mostly =
664 .init = irqsoff_tracer_init,
665 .reset = irqsoff_tracer_reset,
666 .start = irqsoff_tracer_start,
667 .stop = irqsoff_tracer_stop,
669 .print_header = irqsoff_print_header,
670 .print_line = irqsoff_print_line,
671 .flags = &tracer_flags,
672 .set_flag = irqsoff_set_flag,
673 .flag_changed = irqsoff_flag_changed,
674 #ifdef CONFIG_FTRACE_SELFTEST
675 .selftest = trace_selftest_startup_irqsoff,
677 .open = irqsoff_trace_open,
678 .close = irqsoff_trace_close,
679 .allow_instances = true,
682 # define register_irqsoff(trace) register_tracer(&trace)
684 # define register_irqsoff(trace) do { } while (0)
687 #ifdef CONFIG_PREEMPT_TRACER
688 static int preemptoff_tracer_init(struct trace_array *tr)
690 trace_type = TRACER_PREEMPT_OFF;
692 return __irqsoff_tracer_init(tr);
695 static struct tracer preemptoff_tracer __read_mostly =
697 .name = "preemptoff",
698 .init = preemptoff_tracer_init,
699 .reset = irqsoff_tracer_reset,
700 .start = irqsoff_tracer_start,
701 .stop = irqsoff_tracer_stop,
703 .print_header = irqsoff_print_header,
704 .print_line = irqsoff_print_line,
705 .flags = &tracer_flags,
706 .set_flag = irqsoff_set_flag,
707 .flag_changed = irqsoff_flag_changed,
708 #ifdef CONFIG_FTRACE_SELFTEST
709 .selftest = trace_selftest_startup_preemptoff,
711 .open = irqsoff_trace_open,
712 .close = irqsoff_trace_close,
713 .allow_instances = true,
716 # define register_preemptoff(trace) register_tracer(&trace)
718 # define register_preemptoff(trace) do { } while (0)
721 #if defined(CONFIG_IRQSOFF_TRACER) && \
722 defined(CONFIG_PREEMPT_TRACER)
724 static int preemptirqsoff_tracer_init(struct trace_array *tr)
726 trace_type = TRACER_IRQS_OFF | TRACER_PREEMPT_OFF;
728 return __irqsoff_tracer_init(tr);
731 static struct tracer preemptirqsoff_tracer __read_mostly =
733 .name = "preemptirqsoff",
734 .init = preemptirqsoff_tracer_init,
735 .reset = irqsoff_tracer_reset,
736 .start = irqsoff_tracer_start,
737 .stop = irqsoff_tracer_stop,
739 .print_header = irqsoff_print_header,
740 .print_line = irqsoff_print_line,
741 .flags = &tracer_flags,
742 .set_flag = irqsoff_set_flag,
743 .flag_changed = irqsoff_flag_changed,
744 #ifdef CONFIG_FTRACE_SELFTEST
745 .selftest = trace_selftest_startup_preemptirqsoff,
747 .open = irqsoff_trace_open,
748 .close = irqsoff_trace_close,
749 .allow_instances = true,
753 # define register_preemptirqsoff(trace) register_tracer(&trace)
755 # define register_preemptirqsoff(trace) do { } while (0)
758 __init static int init_irqsoff_tracer(void)
760 register_irqsoff(irqsoff_tracer);
761 register_preemptoff(preemptoff_tracer);
762 register_preemptirqsoff(preemptirqsoff_tracer);
766 core_initcall(init_irqsoff_tracer);