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);
21 static int sched_stopped;
24 probe_sched_switch(struct rq *__rq, struct task_struct *prev,
25 struct task_struct *next)
27 struct trace_array_cpu *data;
32 if (!sched_ref || sched_stopped)
35 tracing_record_cmdline(prev);
36 tracing_record_cmdline(next);
42 local_irq_save(flags);
43 cpu = raw_smp_processor_id();
44 data = ctx_trace->data[cpu];
46 if (likely(!atomic_read(&data->disabled)))
47 tracing_sched_switch_trace(ctx_trace, prev, next, flags, pc);
49 local_irq_restore(flags);
53 probe_sched_wakeup(struct rq *__rq, struct task_struct *wakee, int success)
55 struct trace_array_cpu *data;
59 if (!likely(tracer_enabled))
63 tracing_record_cmdline(current);
65 local_irq_save(flags);
66 cpu = raw_smp_processor_id();
67 data = ctx_trace->data[cpu];
69 if (likely(!atomic_read(&data->disabled)))
70 tracing_sched_wakeup_trace(ctx_trace, wakee, current,
73 local_irq_restore(flags);
76 static int tracing_sched_register(void)
80 ret = register_trace_sched_wakeup(probe_sched_wakeup);
82 pr_info("wakeup trace: Couldn't activate tracepoint"
83 " probe to kernel_sched_wakeup\n");
87 ret = register_trace_sched_wakeup_new(probe_sched_wakeup);
89 pr_info("wakeup trace: Couldn't activate tracepoint"
90 " probe to kernel_sched_wakeup_new\n");
94 ret = register_trace_sched_switch(probe_sched_switch);
96 pr_info("sched trace: Couldn't activate tracepoint"
97 " probe to kernel_sched_switch\n");
98 goto fail_deprobe_wake_new;
102 fail_deprobe_wake_new:
103 unregister_trace_sched_wakeup_new(probe_sched_wakeup);
105 unregister_trace_sched_wakeup(probe_sched_wakeup);
109 static void tracing_sched_unregister(void)
111 unregister_trace_sched_switch(probe_sched_switch);
112 unregister_trace_sched_wakeup_new(probe_sched_wakeup);
113 unregister_trace_sched_wakeup(probe_sched_wakeup);
116 static void tracing_start_sched_switch(void)
118 mutex_lock(&sched_register_mutex);
120 tracing_sched_register();
121 mutex_unlock(&sched_register_mutex);
124 static void tracing_stop_sched_switch(void)
126 mutex_lock(&sched_register_mutex);
128 tracing_sched_unregister();
129 mutex_unlock(&sched_register_mutex);
132 void tracing_start_cmdline_record(void)
134 tracing_start_sched_switch();
137 void tracing_stop_cmdline_record(void)
139 tracing_stop_sched_switch();
143 * tracing_start_sched_switch_record - start tracing context switches
145 * Turns on context switch tracing for a tracer.
147 void tracing_start_sched_switch_record(void)
149 if (unlikely(!ctx_trace)) {
154 tracing_start_sched_switch();
156 mutex_lock(&sched_register_mutex);
158 mutex_unlock(&sched_register_mutex);
162 * tracing_stop_sched_switch_record - start tracing context switches
164 * Turns off context switch tracing for a tracer.
166 void tracing_stop_sched_switch_record(void)
168 mutex_lock(&sched_register_mutex);
170 WARN_ON(tracer_enabled < 0);
171 mutex_unlock(&sched_register_mutex);
173 tracing_stop_sched_switch();
177 * tracing_sched_switch_assign_trace - assign a trace array for ctx switch
178 * @tr: trace array pointer to assign
180 * Some tracers might want to record the context switches in their
181 * trace. This function lets those tracers assign the trace array
184 void tracing_sched_switch_assign_trace(struct trace_array *tr)
189 static void stop_sched_trace(struct trace_array *tr)
191 tracing_stop_sched_switch_record();
194 static int sched_switch_trace_init(struct trace_array *tr)
197 tracing_reset_online_cpus(tr);
198 tracing_start_sched_switch_record();
202 static void sched_switch_trace_reset(struct trace_array *tr)
205 stop_sched_trace(tr);
208 static void sched_switch_trace_start(struct trace_array *tr)
213 static void sched_switch_trace_stop(struct trace_array *tr)
218 static struct tracer sched_switch_trace __read_mostly =
220 .name = "sched_switch",
221 .init = sched_switch_trace_init,
222 .reset = sched_switch_trace_reset,
223 .start = sched_switch_trace_start,
224 .stop = sched_switch_trace_stop,
225 .wait_pipe = poll_wait_pipe,
226 #ifdef CONFIG_FTRACE_SELFTEST
227 .selftest = trace_selftest_startup_sched_switch,
231 __init static int init_sched_switch_trace(void)
233 return register_tracer(&sched_switch_trace);
235 device_initcall(init_sched_switch_trace);