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/ftrace.h>
13 #include <trace/sched.h>
17 static struct trace_array *ctx_trace;
18 static int __read_mostly tracer_enabled;
20 static DEFINE_MUTEX(sched_register_mutex);
23 probe_sched_switch(struct rq *__rq, struct task_struct *prev,
24 struct task_struct *next)
26 struct trace_array_cpu *data;
34 tracing_record_cmdline(prev);
35 tracing_record_cmdline(next);
41 local_irq_save(flags);
42 cpu = raw_smp_processor_id();
43 data = ctx_trace->data[cpu];
45 if (likely(!atomic_read(&data->disabled)))
46 tracing_sched_switch_trace(ctx_trace, prev, next, flags, pc);
48 local_irq_restore(flags);
52 probe_sched_wakeup(struct rq *__rq, struct task_struct *wakee, int success)
54 struct trace_array_cpu *data;
58 if (!likely(tracer_enabled))
62 tracing_record_cmdline(current);
64 local_irq_save(flags);
65 cpu = raw_smp_processor_id();
66 data = ctx_trace->data[cpu];
68 if (likely(!atomic_read(&data->disabled)))
69 tracing_sched_wakeup_trace(ctx_trace, wakee, current,
72 local_irq_restore(flags);
75 static int tracing_sched_register(void)
79 ret = register_trace_sched_wakeup(probe_sched_wakeup);
81 pr_info("wakeup trace: Couldn't activate tracepoint"
82 " probe to kernel_sched_wakeup\n");
86 ret = register_trace_sched_wakeup_new(probe_sched_wakeup);
88 pr_info("wakeup trace: Couldn't activate tracepoint"
89 " probe to kernel_sched_wakeup_new\n");
93 ret = register_trace_sched_switch(probe_sched_switch);
95 pr_info("sched trace: Couldn't activate tracepoint"
96 " probe to kernel_sched_schedule\n");
97 goto fail_deprobe_wake_new;
101 fail_deprobe_wake_new:
102 unregister_trace_sched_wakeup_new(probe_sched_wakeup);
104 unregister_trace_sched_wakeup(probe_sched_wakeup);
108 static void tracing_sched_unregister(void)
110 unregister_trace_sched_switch(probe_sched_switch);
111 unregister_trace_sched_wakeup_new(probe_sched_wakeup);
112 unregister_trace_sched_wakeup(probe_sched_wakeup);
115 static void tracing_start_sched_switch(void)
117 mutex_lock(&sched_register_mutex);
119 tracing_sched_register();
120 mutex_unlock(&sched_register_mutex);
123 static void tracing_stop_sched_switch(void)
125 mutex_lock(&sched_register_mutex);
127 tracing_sched_unregister();
128 mutex_unlock(&sched_register_mutex);
131 void tracing_start_cmdline_record(void)
133 tracing_start_sched_switch();
136 void tracing_stop_cmdline_record(void)
138 tracing_stop_sched_switch();
142 * tracing_start_sched_switch_record - start tracing context switches
144 * Turns on context switch tracing for a tracer.
146 void tracing_start_sched_switch_record(void)
148 if (unlikely(!ctx_trace)) {
153 tracing_start_sched_switch();
155 mutex_lock(&sched_register_mutex);
157 mutex_unlock(&sched_register_mutex);
161 * tracing_stop_sched_switch_record - start tracing context switches
163 * Turns off context switch tracing for a tracer.
165 void tracing_stop_sched_switch_record(void)
167 mutex_lock(&sched_register_mutex);
169 WARN_ON(tracer_enabled < 0);
170 mutex_unlock(&sched_register_mutex);
172 tracing_stop_sched_switch();
176 * tracing_sched_switch_assign_trace - assign a trace array for ctx switch
177 * @tr: trace array pointer to assign
179 * Some tracers might want to record the context switches in their
180 * trace. This function lets those tracers assign the trace array
183 void tracing_sched_switch_assign_trace(struct trace_array *tr)
188 static void stop_sched_trace(struct trace_array *tr)
190 tracing_stop_sched_switch_record();
193 static int sched_switch_trace_init(struct trace_array *tr)
196 tracing_start_sched_switch_record();
200 static void sched_switch_trace_reset(struct trace_array *tr)
203 stop_sched_trace(tr);
206 static void sched_switch_trace_start(struct trace_array *tr)
208 tracing_reset_online_cpus(tr);
209 tracing_start_sched_switch();
212 static void sched_switch_trace_stop(struct trace_array *tr)
214 tracing_stop_sched_switch();
217 static struct tracer sched_switch_trace __read_mostly =
219 .name = "sched_switch",
220 .init = sched_switch_trace_init,
221 .reset = sched_switch_trace_reset,
222 .start = sched_switch_trace_start,
223 .stop = sched_switch_trace_stop,
224 #ifdef CONFIG_FTRACE_SELFTEST
225 .selftest = trace_selftest_startup_sched_switch,
229 __init static int init_sched_switch_trace(void)
231 return register_tracer(&sched_switch_trace);
233 device_initcall(init_sched_switch_trace);