4 * Copyright (C) 2007 Steven Rostedt <srostedt@redhat.com>
7 #include <linux/module.h>
9 #include <linux/debugfs.h>
10 #include <linux/kallsyms.h>
11 #include <linux/uaccess.h>
12 #include <linux/marker.h>
13 #include <linux/ftrace.h>
17 static struct trace_array *ctx_trace;
18 static int __read_mostly tracer_enabled;
21 ctx_switch_func(struct task_struct *prev, struct task_struct *next)
23 struct trace_array *tr = ctx_trace;
24 struct trace_array_cpu *data;
32 local_irq_save(flags);
33 cpu = raw_smp_processor_id();
35 disabled = atomic_inc_return(&data->disabled);
37 if (likely(disabled == 1))
38 tracing_sched_switch_trace(tr, data, prev, next, flags);
40 atomic_dec(&data->disabled);
41 local_irq_restore(flags);
44 static void wakeup_func(struct task_struct *wakee, struct task_struct *curr)
46 struct trace_array *tr = ctx_trace;
47 struct trace_array_cpu *data;
55 local_irq_save(flags);
56 cpu = raw_smp_processor_id();
58 disabled = atomic_inc_return(&data->disabled);
60 if (likely(disabled == 1))
61 tracing_sched_wakeup_trace(tr, data, wakee, curr, flags);
63 atomic_dec(&data->disabled);
64 local_irq_restore(flags);
67 void ftrace_ctx_switch(struct task_struct *prev, struct task_struct *next)
69 tracing_record_cmdline(prev);
72 * If tracer_switch_func only points to the local
73 * switch func, it still needs the ptr passed to it.
75 ctx_switch_func(prev, next);
78 * Chain to the wakeup tracer (this is a NOP if disabled):
80 wakeup_sched_switch(prev, next);
84 ftrace_wake_up_task(struct task_struct *wakee, struct task_struct *curr)
86 tracing_record_cmdline(curr);
88 wakeup_func(wakee, curr);
91 * Chain to the wakeup tracer (this is a NOP if disabled):
93 wakeup_sched_wakeup(wakee, curr);
96 static void sched_switch_reset(struct trace_array *tr)
100 tr->time_start = ftrace_now(tr->cpu);
102 for_each_online_cpu(cpu)
103 tracing_reset(tr->data[cpu]);
106 static void start_sched_trace(struct trace_array *tr)
108 sched_switch_reset(tr);
112 static void stop_sched_trace(struct trace_array *tr)
117 static void sched_switch_trace_init(struct trace_array *tr)
122 start_sched_trace(tr);
125 static void sched_switch_trace_reset(struct trace_array *tr)
128 stop_sched_trace(tr);
131 static void sched_switch_trace_ctrl_update(struct trace_array *tr)
133 /* When starting a new trace, reset the buffers */
135 start_sched_trace(tr);
137 stop_sched_trace(tr);
140 static struct tracer sched_switch_trace __read_mostly =
142 .name = "sched_switch",
143 .init = sched_switch_trace_init,
144 .reset = sched_switch_trace_reset,
145 .ctrl_update = sched_switch_trace_ctrl_update,
146 #ifdef CONFIG_FTRACE_SELFTEST
147 .selftest = trace_selftest_startup_sched_switch,
151 __init static int init_sched_switch_trace(void)
153 return register_tracer(&sched_switch_trace);
155 device_initcall(init_sched_switch_trace);