net: sctp: implement rfc6458, 8.1.31. SCTP_DEFAULT_SNDINFO support
[linux-2.6/btrfs-unstable.git] / kernel / trace / trace_sched_wakeup.c
blob19bd8928ce944da6232c1f63708d723a2563020f
1 /*
2 * trace task wakeup timings
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Based on code from the latency_tracer, that is:
9 * Copyright (C) 2004-2006 Ingo Molnar
10 * Copyright (C) 2004 Nadia Yvette Chambers
12 #include <linux/module.h>
13 #include <linux/fs.h>
14 #include <linux/debugfs.h>
15 #include <linux/kallsyms.h>
16 #include <linux/uaccess.h>
17 #include <linux/ftrace.h>
18 #include <linux/sched/rt.h>
19 #include <linux/sched/deadline.h>
20 #include <trace/events/sched.h>
21 #include "trace.h"
23 static struct trace_array *wakeup_trace;
24 static int __read_mostly tracer_enabled;
26 static struct task_struct *wakeup_task;
27 static int wakeup_cpu;
28 static int wakeup_current_cpu;
29 static unsigned wakeup_prio = -1;
30 static int wakeup_rt;
31 static int wakeup_dl;
32 static int tracing_dl = 0;
34 static arch_spinlock_t wakeup_lock =
35 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
37 static void wakeup_reset(struct trace_array *tr);
38 static void __wakeup_reset(struct trace_array *tr);
39 static int wakeup_graph_entry(struct ftrace_graph_ent *trace);
40 static void wakeup_graph_return(struct ftrace_graph_ret *trace);
42 static int save_flags;
43 static bool function_enabled;
45 #define TRACE_DISPLAY_GRAPH 1
47 static struct tracer_opt trace_opts[] = {
48 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
49 /* display latency trace as call graph */
50 { TRACER_OPT(display-graph, TRACE_DISPLAY_GRAPH) },
51 #endif
52 { } /* Empty entry */
55 static struct tracer_flags tracer_flags = {
56 .val = 0,
57 .opts = trace_opts,
60 #define is_graph() (tracer_flags.val & TRACE_DISPLAY_GRAPH)
62 #ifdef CONFIG_FUNCTION_TRACER
65 * Prologue for the wakeup function tracers.
67 * Returns 1 if it is OK to continue, and preemption
68 * is disabled and data->disabled is incremented.
69 * 0 if the trace is to be ignored, and preemption
70 * is not disabled and data->disabled is
71 * kept the same.
73 * Note, this function is also used outside this ifdef but
74 * inside the #ifdef of the function graph tracer below.
75 * This is OK, since the function graph tracer is
76 * dependent on the function tracer.
78 static int
79 func_prolog_preempt_disable(struct trace_array *tr,
80 struct trace_array_cpu **data,
81 int *pc)
83 long disabled;
84 int cpu;
86 if (likely(!wakeup_task))
87 return 0;
89 *pc = preempt_count();
90 preempt_disable_notrace();
92 cpu = raw_smp_processor_id();
93 if (cpu != wakeup_current_cpu)
94 goto out_enable;
96 *data = per_cpu_ptr(tr->trace_buffer.data, cpu);
97 disabled = atomic_inc_return(&(*data)->disabled);
98 if (unlikely(disabled != 1))
99 goto out;
101 return 1;
103 out:
104 atomic_dec(&(*data)->disabled);
106 out_enable:
107 preempt_enable_notrace();
108 return 0;
112 * wakeup uses its own tracer function to keep the overhead down:
114 static void
115 wakeup_tracer_call(unsigned long ip, unsigned long parent_ip,
116 struct ftrace_ops *op, struct pt_regs *pt_regs)
118 struct trace_array *tr = wakeup_trace;
119 struct trace_array_cpu *data;
120 unsigned long flags;
121 int pc;
123 if (!func_prolog_preempt_disable(tr, &data, &pc))
124 return;
126 local_irq_save(flags);
127 trace_function(tr, ip, parent_ip, flags, pc);
128 local_irq_restore(flags);
130 atomic_dec(&data->disabled);
131 preempt_enable_notrace();
133 #endif /* CONFIG_FUNCTION_TRACER */
135 static int register_wakeup_function(struct trace_array *tr, int graph, int set)
137 int ret;
139 /* 'set' is set if TRACE_ITER_FUNCTION is about to be set */
140 if (function_enabled || (!set && !(trace_flags & TRACE_ITER_FUNCTION)))
141 return 0;
143 if (graph)
144 ret = register_ftrace_graph(&wakeup_graph_return,
145 &wakeup_graph_entry);
146 else
147 ret = register_ftrace_function(tr->ops);
149 if (!ret)
150 function_enabled = true;
152 return ret;
155 static void unregister_wakeup_function(struct trace_array *tr, int graph)
157 if (!function_enabled)
158 return;
160 if (graph)
161 unregister_ftrace_graph();
162 else
163 unregister_ftrace_function(tr->ops);
165 function_enabled = false;
168 static void wakeup_function_set(struct trace_array *tr, int set)
170 if (set)
171 register_wakeup_function(tr, is_graph(), 1);
172 else
173 unregister_wakeup_function(tr, is_graph());
176 static int wakeup_flag_changed(struct trace_array *tr, u32 mask, int set)
178 struct tracer *tracer = tr->current_trace;
180 if (mask & TRACE_ITER_FUNCTION)
181 wakeup_function_set(tr, set);
183 return trace_keep_overwrite(tracer, mask, set);
186 static int start_func_tracer(struct trace_array *tr, int graph)
188 int ret;
190 ret = register_wakeup_function(tr, graph, 0);
192 if (!ret && tracing_is_enabled())
193 tracer_enabled = 1;
194 else
195 tracer_enabled = 0;
197 return ret;
200 static void stop_func_tracer(struct trace_array *tr, int graph)
202 tracer_enabled = 0;
204 unregister_wakeup_function(tr, graph);
207 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
208 static int
209 wakeup_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
212 if (!(bit & TRACE_DISPLAY_GRAPH))
213 return -EINVAL;
215 if (!(is_graph() ^ set))
216 return 0;
218 stop_func_tracer(tr, !set);
220 wakeup_reset(wakeup_trace);
221 tr->max_latency = 0;
223 return start_func_tracer(tr, set);
226 static int wakeup_graph_entry(struct ftrace_graph_ent *trace)
228 struct trace_array *tr = wakeup_trace;
229 struct trace_array_cpu *data;
230 unsigned long flags;
231 int pc, ret = 0;
233 if (!func_prolog_preempt_disable(tr, &data, &pc))
234 return 0;
236 local_save_flags(flags);
237 ret = __trace_graph_entry(tr, trace, flags, pc);
238 atomic_dec(&data->disabled);
239 preempt_enable_notrace();
241 return ret;
244 static void wakeup_graph_return(struct ftrace_graph_ret *trace)
246 struct trace_array *tr = wakeup_trace;
247 struct trace_array_cpu *data;
248 unsigned long flags;
249 int pc;
251 if (!func_prolog_preempt_disable(tr, &data, &pc))
252 return;
254 local_save_flags(flags);
255 __trace_graph_return(tr, trace, flags, pc);
256 atomic_dec(&data->disabled);
258 preempt_enable_notrace();
259 return;
262 static void wakeup_trace_open(struct trace_iterator *iter)
264 if (is_graph())
265 graph_trace_open(iter);
268 static void wakeup_trace_close(struct trace_iterator *iter)
270 if (iter->private)
271 graph_trace_close(iter);
274 #define GRAPH_TRACER_FLAGS (TRACE_GRAPH_PRINT_PROC | \
275 TRACE_GRAPH_PRINT_ABS_TIME | \
276 TRACE_GRAPH_PRINT_DURATION)
278 static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
281 * In graph mode call the graph tracer output function,
282 * otherwise go with the TRACE_FN event handler
284 if (is_graph())
285 return print_graph_function_flags(iter, GRAPH_TRACER_FLAGS);
287 return TRACE_TYPE_UNHANDLED;
290 static void wakeup_print_header(struct seq_file *s)
292 if (is_graph())
293 print_graph_headers_flags(s, GRAPH_TRACER_FLAGS);
294 else
295 trace_default_header(s);
298 static void
299 __trace_function(struct trace_array *tr,
300 unsigned long ip, unsigned long parent_ip,
301 unsigned long flags, int pc)
303 if (is_graph())
304 trace_graph_function(tr, ip, parent_ip, flags, pc);
305 else
306 trace_function(tr, ip, parent_ip, flags, pc);
308 #else
309 #define __trace_function trace_function
311 static int
312 wakeup_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
314 return -EINVAL;
317 static int wakeup_graph_entry(struct ftrace_graph_ent *trace)
319 return -1;
322 static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
324 return TRACE_TYPE_UNHANDLED;
327 static void wakeup_graph_return(struct ftrace_graph_ret *trace) { }
328 static void wakeup_trace_open(struct trace_iterator *iter) { }
329 static void wakeup_trace_close(struct trace_iterator *iter) { }
331 #ifdef CONFIG_FUNCTION_TRACER
332 static void wakeup_print_header(struct seq_file *s)
334 trace_default_header(s);
336 #else
337 static void wakeup_print_header(struct seq_file *s)
339 trace_latency_header(s);
341 #endif /* CONFIG_FUNCTION_TRACER */
342 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
345 * Should this new latency be reported/recorded?
347 static int report_latency(struct trace_array *tr, cycle_t delta)
349 if (tracing_thresh) {
350 if (delta < tracing_thresh)
351 return 0;
352 } else {
353 if (delta <= tr->max_latency)
354 return 0;
356 return 1;
359 static void
360 probe_wakeup_migrate_task(void *ignore, struct task_struct *task, int cpu)
362 if (task != wakeup_task)
363 return;
365 wakeup_current_cpu = cpu;
368 static void notrace
369 probe_wakeup_sched_switch(void *ignore,
370 struct task_struct *prev, struct task_struct *next)
372 struct trace_array_cpu *data;
373 cycle_t T0, T1, delta;
374 unsigned long flags;
375 long disabled;
376 int cpu;
377 int pc;
379 tracing_record_cmdline(prev);
381 if (unlikely(!tracer_enabled))
382 return;
385 * When we start a new trace, we set wakeup_task to NULL
386 * and then set tracer_enabled = 1. We want to make sure
387 * that another CPU does not see the tracer_enabled = 1
388 * and the wakeup_task with an older task, that might
389 * actually be the same as next.
391 smp_rmb();
393 if (next != wakeup_task)
394 return;
396 pc = preempt_count();
398 /* disable local data, not wakeup_cpu data */
399 cpu = raw_smp_processor_id();
400 disabled = atomic_inc_return(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
401 if (likely(disabled != 1))
402 goto out;
404 local_irq_save(flags);
405 arch_spin_lock(&wakeup_lock);
407 /* We could race with grabbing wakeup_lock */
408 if (unlikely(!tracer_enabled || next != wakeup_task))
409 goto out_unlock;
411 /* The task we are waiting for is waking up */
412 data = per_cpu_ptr(wakeup_trace->trace_buffer.data, wakeup_cpu);
414 __trace_function(wakeup_trace, CALLER_ADDR0, CALLER_ADDR1, flags, pc);
415 tracing_sched_switch_trace(wakeup_trace, prev, next, flags, pc);
417 T0 = data->preempt_timestamp;
418 T1 = ftrace_now(cpu);
419 delta = T1-T0;
421 if (!report_latency(wakeup_trace, delta))
422 goto out_unlock;
424 if (likely(!is_tracing_stopped())) {
425 wakeup_trace->max_latency = delta;
426 update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu);
429 out_unlock:
430 __wakeup_reset(wakeup_trace);
431 arch_spin_unlock(&wakeup_lock);
432 local_irq_restore(flags);
433 out:
434 atomic_dec(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
437 static void __wakeup_reset(struct trace_array *tr)
439 wakeup_cpu = -1;
440 wakeup_prio = -1;
441 tracing_dl = 0;
443 if (wakeup_task)
444 put_task_struct(wakeup_task);
446 wakeup_task = NULL;
449 static void wakeup_reset(struct trace_array *tr)
451 unsigned long flags;
453 tracing_reset_online_cpus(&tr->trace_buffer);
455 local_irq_save(flags);
456 arch_spin_lock(&wakeup_lock);
457 __wakeup_reset(tr);
458 arch_spin_unlock(&wakeup_lock);
459 local_irq_restore(flags);
462 static void
463 probe_wakeup(void *ignore, struct task_struct *p, int success)
465 struct trace_array_cpu *data;
466 int cpu = smp_processor_id();
467 unsigned long flags;
468 long disabled;
469 int pc;
471 if (likely(!tracer_enabled))
472 return;
474 tracing_record_cmdline(p);
475 tracing_record_cmdline(current);
478 * Semantic is like this:
479 * - wakeup tracer handles all tasks in the system, independently
480 * from their scheduling class;
481 * - wakeup_rt tracer handles tasks belonging to sched_dl and
482 * sched_rt class;
483 * - wakeup_dl handles tasks belonging to sched_dl class only.
485 if (tracing_dl || (wakeup_dl && !dl_task(p)) ||
486 (wakeup_rt && !dl_task(p) && !rt_task(p)) ||
487 (!dl_task(p) && (p->prio >= wakeup_prio || p->prio >= current->prio)))
488 return;
490 pc = preempt_count();
491 disabled = atomic_inc_return(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
492 if (unlikely(disabled != 1))
493 goto out;
495 /* interrupts should be off from try_to_wake_up */
496 arch_spin_lock(&wakeup_lock);
498 /* check for races. */
499 if (!tracer_enabled || tracing_dl ||
500 (!dl_task(p) && p->prio >= wakeup_prio))
501 goto out_locked;
503 /* reset the trace */
504 __wakeup_reset(wakeup_trace);
506 wakeup_cpu = task_cpu(p);
507 wakeup_current_cpu = wakeup_cpu;
508 wakeup_prio = p->prio;
511 * Once you start tracing a -deadline task, don't bother tracing
512 * another task until the first one wakes up.
514 if (dl_task(p))
515 tracing_dl = 1;
516 else
517 tracing_dl = 0;
519 wakeup_task = p;
520 get_task_struct(wakeup_task);
522 local_save_flags(flags);
524 data = per_cpu_ptr(wakeup_trace->trace_buffer.data, wakeup_cpu);
525 data->preempt_timestamp = ftrace_now(cpu);
526 tracing_sched_wakeup_trace(wakeup_trace, p, current, flags, pc);
529 * We must be careful in using CALLER_ADDR2. But since wake_up
530 * is not called by an assembly function (where as schedule is)
531 * it should be safe to use it here.
533 __trace_function(wakeup_trace, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
535 out_locked:
536 arch_spin_unlock(&wakeup_lock);
537 out:
538 atomic_dec(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
541 static void start_wakeup_tracer(struct trace_array *tr)
543 int ret;
545 ret = register_trace_sched_wakeup(probe_wakeup, NULL);
546 if (ret) {
547 pr_info("wakeup trace: Couldn't activate tracepoint"
548 " probe to kernel_sched_wakeup\n");
549 return;
552 ret = register_trace_sched_wakeup_new(probe_wakeup, NULL);
553 if (ret) {
554 pr_info("wakeup trace: Couldn't activate tracepoint"
555 " probe to kernel_sched_wakeup_new\n");
556 goto fail_deprobe;
559 ret = register_trace_sched_switch(probe_wakeup_sched_switch, NULL);
560 if (ret) {
561 pr_info("sched trace: Couldn't activate tracepoint"
562 " probe to kernel_sched_switch\n");
563 goto fail_deprobe_wake_new;
566 ret = register_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
567 if (ret) {
568 pr_info("wakeup trace: Couldn't activate tracepoint"
569 " probe to kernel_sched_migrate_task\n");
570 return;
573 wakeup_reset(tr);
576 * Don't let the tracer_enabled = 1 show up before
577 * the wakeup_task is reset. This may be overkill since
578 * wakeup_reset does a spin_unlock after setting the
579 * wakeup_task to NULL, but I want to be safe.
580 * This is a slow path anyway.
582 smp_wmb();
584 if (start_func_tracer(tr, is_graph()))
585 printk(KERN_ERR "failed to start wakeup tracer\n");
587 return;
588 fail_deprobe_wake_new:
589 unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
590 fail_deprobe:
591 unregister_trace_sched_wakeup(probe_wakeup, NULL);
594 static void stop_wakeup_tracer(struct trace_array *tr)
596 tracer_enabled = 0;
597 stop_func_tracer(tr, is_graph());
598 unregister_trace_sched_switch(probe_wakeup_sched_switch, NULL);
599 unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
600 unregister_trace_sched_wakeup(probe_wakeup, NULL);
601 unregister_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
604 static bool wakeup_busy;
606 static int __wakeup_tracer_init(struct trace_array *tr)
608 save_flags = trace_flags;
610 /* non overwrite screws up the latency tracers */
611 set_tracer_flag(tr, TRACE_ITER_OVERWRITE, 1);
612 set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, 1);
614 tr->max_latency = 0;
615 wakeup_trace = tr;
616 ftrace_init_array_ops(tr, wakeup_tracer_call);
617 start_wakeup_tracer(tr);
619 wakeup_busy = true;
620 return 0;
623 static int wakeup_tracer_init(struct trace_array *tr)
625 if (wakeup_busy)
626 return -EBUSY;
628 wakeup_dl = 0;
629 wakeup_rt = 0;
630 return __wakeup_tracer_init(tr);
633 static int wakeup_rt_tracer_init(struct trace_array *tr)
635 if (wakeup_busy)
636 return -EBUSY;
638 wakeup_dl = 0;
639 wakeup_rt = 1;
640 return __wakeup_tracer_init(tr);
643 static int wakeup_dl_tracer_init(struct trace_array *tr)
645 if (wakeup_busy)
646 return -EBUSY;
648 wakeup_dl = 1;
649 wakeup_rt = 0;
650 return __wakeup_tracer_init(tr);
653 static void wakeup_tracer_reset(struct trace_array *tr)
655 int lat_flag = save_flags & TRACE_ITER_LATENCY_FMT;
656 int overwrite_flag = save_flags & TRACE_ITER_OVERWRITE;
658 stop_wakeup_tracer(tr);
659 /* make sure we put back any tasks we are tracing */
660 wakeup_reset(tr);
662 set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, lat_flag);
663 set_tracer_flag(tr, TRACE_ITER_OVERWRITE, overwrite_flag);
664 ftrace_reset_array_ops(tr);
665 wakeup_busy = false;
668 static void wakeup_tracer_start(struct trace_array *tr)
670 wakeup_reset(tr);
671 tracer_enabled = 1;
674 static void wakeup_tracer_stop(struct trace_array *tr)
676 tracer_enabled = 0;
679 static struct tracer wakeup_tracer __read_mostly =
681 .name = "wakeup",
682 .init = wakeup_tracer_init,
683 .reset = wakeup_tracer_reset,
684 .start = wakeup_tracer_start,
685 .stop = wakeup_tracer_stop,
686 .print_max = true,
687 .print_header = wakeup_print_header,
688 .print_line = wakeup_print_line,
689 .flags = &tracer_flags,
690 .set_flag = wakeup_set_flag,
691 .flag_changed = wakeup_flag_changed,
692 #ifdef CONFIG_FTRACE_SELFTEST
693 .selftest = trace_selftest_startup_wakeup,
694 #endif
695 .open = wakeup_trace_open,
696 .close = wakeup_trace_close,
697 .allow_instances = true,
698 .use_max_tr = true,
701 static struct tracer wakeup_rt_tracer __read_mostly =
703 .name = "wakeup_rt",
704 .init = wakeup_rt_tracer_init,
705 .reset = wakeup_tracer_reset,
706 .start = wakeup_tracer_start,
707 .stop = wakeup_tracer_stop,
708 .print_max = true,
709 .print_header = wakeup_print_header,
710 .print_line = wakeup_print_line,
711 .flags = &tracer_flags,
712 .set_flag = wakeup_set_flag,
713 .flag_changed = wakeup_flag_changed,
714 #ifdef CONFIG_FTRACE_SELFTEST
715 .selftest = trace_selftest_startup_wakeup,
716 #endif
717 .open = wakeup_trace_open,
718 .close = wakeup_trace_close,
719 .allow_instances = true,
720 .use_max_tr = true,
723 static struct tracer wakeup_dl_tracer __read_mostly =
725 .name = "wakeup_dl",
726 .init = wakeup_dl_tracer_init,
727 .reset = wakeup_tracer_reset,
728 .start = wakeup_tracer_start,
729 .stop = wakeup_tracer_stop,
730 .print_max = true,
731 .print_header = wakeup_print_header,
732 .print_line = wakeup_print_line,
733 .flags = &tracer_flags,
734 .set_flag = wakeup_set_flag,
735 .flag_changed = wakeup_flag_changed,
736 #ifdef CONFIG_FTRACE_SELFTEST
737 .selftest = trace_selftest_startup_wakeup,
738 #endif
739 .open = wakeup_trace_open,
740 .close = wakeup_trace_close,
741 .use_max_tr = true,
744 __init static int init_wakeup_tracer(void)
746 int ret;
748 ret = register_tracer(&wakeup_tracer);
749 if (ret)
750 return ret;
752 ret = register_tracer(&wakeup_rt_tracer);
753 if (ret)
754 return ret;
756 ret = register_tracer(&wakeup_dl_tracer);
757 if (ret)
758 return ret;
760 return 0;
762 core_initcall(init_wakeup_tracer);