Linux 4.1.18
[linux/fpc-iii.git] / kernel / trace / trace_irqsoff.c
blob8523ea345f2b1a3849d53f2d6731ed41fb5d94f4
1 /*
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>
17 #include "trace.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);
26 enum {
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
40 static inline int
41 preempt_trace(void)
43 return ((trace_type & TRACER_PREEMPT_OFF) && preempt_count());
45 #else
46 # define preempt_trace() (0)
47 #endif
49 #ifdef CONFIG_IRQSOFF_TRACER
50 static inline int
51 irq_trace(void)
53 return ((trace_type & TRACER_IRQS_OFF) &&
54 irqs_disabled());
56 #else
57 # define irq_trace() (0)
58 #endif
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) },
66 #endif
67 { } /* Empty entry */
70 static struct tracer_flags tracer_flags = {
71 .val = 0,
72 .opts = trace_opts,
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
92 * incremented.
93 * 0 if the trace is to be ignored, and data->disabled
94 * is kept the same.
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)
105 long disabled;
106 int cpu;
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
112 * test will fail.
114 cpu = raw_smp_processor_id();
115 if (likely(!per_cpu(tracing_cpu, cpu)))
116 return 0;
118 local_save_flags(*flags);
119 /* slight chance to get a false positive on tracing_cpu */
120 if (!irqs_disabled_flags(*flags))
121 return 0;
123 *data = per_cpu_ptr(tr->trace_buffer.data, cpu);
124 disabled = atomic_inc_return(&(*data)->disabled);
126 if (likely(disabled == 1))
127 return 1;
129 atomic_dec(&(*data)->disabled);
131 return 0;
135 * irqsoff uses its own tracer function to keep the overhead down:
137 static void
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;
143 unsigned long flags;
145 if (!func_prolog_dec(tr, &data, &flags))
146 return;
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
155 static int
156 irqsoff_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
158 int cpu;
160 if (!(bit & TRACE_DISPLAY_GRAPH))
161 return -EINVAL;
163 if (!(is_graph() ^ set))
164 return 0;
166 stop_irqsoff_tracer(irqsoff_trace, !set);
168 for_each_possible_cpu(cpu)
169 per_cpu(tracing_cpu, cpu) = 0;
171 tr->max_latency = 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;
181 unsigned long flags;
182 int ret;
183 int pc;
185 if (!func_prolog_dec(tr, &data, &flags))
186 return 0;
188 pc = preempt_count();
189 ret = __trace_graph_entry(tr, trace, flags, pc);
190 atomic_dec(&data->disabled);
192 return ret;
195 static void irqsoff_graph_return(struct ftrace_graph_ret *trace)
197 struct trace_array *tr = irqsoff_trace;
198 struct trace_array_cpu *data;
199 unsigned long flags;
200 int pc;
202 if (!func_prolog_dec(tr, &data, &flags))
203 return;
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)
212 if (is_graph())
213 graph_trace_open(iter);
217 static void irqsoff_trace_close(struct trace_iterator *iter)
219 if (iter->private)
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
234 if (is_graph())
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)
242 if (is_graph())
243 print_graph_headers_flags(s, GRAPH_TRACER_FLAGS);
244 else
245 trace_default_header(s);
248 static void
249 __trace_function(struct trace_array *tr,
250 unsigned long ip, unsigned long parent_ip,
251 unsigned long flags, int pc)
253 if (is_graph())
254 trace_graph_function(tr, ip, parent_ip, flags, pc);
255 else
256 trace_function(tr, ip, parent_ip, flags, pc);
259 #else
260 #define __trace_function trace_function
262 static int
263 irqsoff_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
265 return -EINVAL;
268 static int irqsoff_graph_entry(struct ftrace_graph_ent *trace)
270 return -1;
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);
287 #else
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)
302 return 0;
303 } else {
304 if (delta <= tr->max_latency)
305 return 0;
307 return 1;
310 static void
311 check_critical_timing(struct trace_array *tr,
312 struct trace_array_cpu *data,
313 unsigned long parent_ip,
314 int cpu)
316 cycle_t T0, T1, delta;
317 unsigned long flags;
318 int pc;
320 T0 = data->preempt_timestamp;
321 T1 = ftrace_now(cpu);
322 delta = T1-T0;
324 local_save_flags(flags);
326 pc = preempt_count();
328 if (!report_latency(tr, delta))
329 goto out;
331 raw_spin_lock_irqsave(&max_trace_lock, flags);
333 /* check if we are still the max latency */
334 if (!report_latency(tr, delta))
335 goto out_unlock;
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)
342 goto out_unlock;
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);
351 max_sequence++;
353 out_unlock:
354 raw_spin_unlock_irqrestore(&max_trace_lock, flags);
356 out:
357 data->critical_sequence = max_sequence;
358 data->preempt_timestamp = ftrace_now(cpu);
359 __trace_function(tr, CALLER_ADDR0, parent_ip, flags, pc);
362 static inline void
363 start_critical_timing(unsigned long ip, unsigned long parent_ip)
365 int cpu;
366 struct trace_array *tr = irqsoff_trace;
367 struct trace_array_cpu *data;
368 unsigned long flags;
370 if (!tracer_enabled || !tracing_is_enabled())
371 return;
373 cpu = raw_smp_processor_id();
375 if (per_cpu(tracing_cpu, cpu))
376 return;
378 data = per_cpu_ptr(tr->trace_buffer.data, cpu);
380 if (unlikely(!data) || atomic_read(&data->disabled))
381 return;
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);
398 static inline void
399 stop_critical_timing(unsigned long ip, unsigned long parent_ip)
401 int cpu;
402 struct trace_array *tr = irqsoff_trace;
403 struct trace_array_cpu *data;
404 unsigned long flags;
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;
410 else
411 return;
413 if (!tracer_enabled || !tracing_is_enabled())
414 return;
416 data = per_cpu_ptr(tr->trace_buffer.data, cpu);
418 if (unlikely(!data) ||
419 !data->critical_start || atomic_read(&data->disabled))
420 return;
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 */
463 * Stubs:
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)
528 int ret;
530 /* 'set' is set if TRACE_ITER_FUNCTION is about to be set */
531 if (function_enabled || (!set && !(trace_flags & TRACE_ITER_FUNCTION)))
532 return 0;
534 if (graph)
535 ret = register_ftrace_graph(&irqsoff_graph_return,
536 &irqsoff_graph_entry);
537 else
538 ret = register_ftrace_function(tr->ops);
540 if (!ret)
541 function_enabled = true;
543 return ret;
546 static void unregister_irqsoff_function(struct trace_array *tr, int graph)
548 if (!function_enabled)
549 return;
551 if (graph)
552 unregister_ftrace_graph();
553 else
554 unregister_ftrace_function(tr->ops);
556 function_enabled = false;
559 static void irqsoff_function_set(struct trace_array *tr, int set)
561 if (set)
562 register_irqsoff_function(tr, is_graph(), 1);
563 else
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)
579 int ret;
581 ret = register_irqsoff_function(tr, graph, 0);
583 if (!ret && tracing_is_enabled())
584 tracer_enabled = 1;
585 else
586 tracer_enabled = 0;
588 return ret;
591 static void stop_irqsoff_tracer(struct trace_array *tr, int graph)
593 tracer_enabled = 0;
595 unregister_irqsoff_function(tr, graph);
598 static bool irqsoff_busy;
600 static int __irqsoff_tracer_init(struct trace_array *tr)
602 if (irqsoff_busy)
603 return -EBUSY;
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);
611 tr->max_latency = 0;
612 irqsoff_trace = tr;
613 /* make sure that the tracer is visible */
614 smp_wmb();
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 &&
621 is_graph())))
622 printk(KERN_ERR "failed to start irqsoff tracer\n");
624 irqsoff_busy = true;
625 return 0;
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)
644 tracer_enabled = 1;
647 static void irqsoff_tracer_stop(struct trace_array *tr)
649 tracer_enabled = 0;
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 =
661 .name = "irqsoff",
662 .init = irqsoff_tracer_init,
663 .reset = irqsoff_tracer_reset,
664 .start = irqsoff_tracer_start,
665 .stop = irqsoff_tracer_stop,
666 .print_max = true,
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,
674 #endif
675 .open = irqsoff_trace_open,
676 .close = irqsoff_trace_close,
677 .allow_instances = true,
678 .use_max_tr = true,
680 # define register_irqsoff(trace) register_tracer(&trace)
681 #else
682 # define register_irqsoff(trace) do { } while (0)
683 #endif
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,
700 .print_max = true,
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,
708 #endif
709 .open = irqsoff_trace_open,
710 .close = irqsoff_trace_close,
711 .allow_instances = true,
712 .use_max_tr = true,
714 # define register_preemptoff(trace) register_tracer(&trace)
715 #else
716 # define register_preemptoff(trace) do { } while (0)
717 #endif
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,
736 .print_max = true,
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,
744 #endif
745 .open = irqsoff_trace_open,
746 .close = irqsoff_trace_close,
747 .allow_instances = true,
748 .use_max_tr = true,
751 # define register_preemptirqsoff(trace) register_tracer(&trace)
752 #else
753 # define register_preemptirqsoff(trace) do { } while (0)
754 #endif
756 __init static int init_irqsoff_tracer(void)
758 register_irqsoff(irqsoff_tracer);
759 register_preemptoff(preemptoff_tracer);
760 register_preemptirqsoff(preemptirqsoff_tracer);
762 return 0;
764 core_initcall(init_irqsoff_tracer);