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);
68 local_irq_save(flags);
69 cpu = raw_smp_processor_id();
70 data = ctx_trace->data[cpu];
72 if (likely(!atomic_read(&data->disabled)))
73 tracing_sched_wakeup_trace(ctx_trace, wakee, current,
76 local_irq_restore(flags);
79 static int tracing_sched_register(void)
83 ret = register_trace_sched_wakeup(probe_sched_wakeup);
85 pr_info("wakeup trace: Couldn't activate tracepoint"
86 " probe to kernel_sched_wakeup\n");
90 ret = register_trace_sched_wakeup_new(probe_sched_wakeup);
92 pr_info("wakeup trace: Couldn't activate tracepoint"
93 " probe to kernel_sched_wakeup_new\n");
97 ret = register_trace_sched_switch(probe_sched_switch);
99 pr_info("sched trace: Couldn't activate tracepoint"
100 " probe to kernel_sched_switch\n");
101 goto fail_deprobe_wake_new;
105 fail_deprobe_wake_new:
106 unregister_trace_sched_wakeup_new(probe_sched_wakeup);
108 unregister_trace_sched_wakeup(probe_sched_wakeup);
112 static void tracing_sched_unregister(void)
114 unregister_trace_sched_switch(probe_sched_switch);
115 unregister_trace_sched_wakeup_new(probe_sched_wakeup);
116 unregister_trace_sched_wakeup(probe_sched_wakeup);
119 static void tracing_start_sched_switch(void)
121 mutex_lock(&sched_register_mutex);
123 tracing_sched_register();
124 mutex_unlock(&sched_register_mutex);
127 static void tracing_stop_sched_switch(void)
129 mutex_lock(&sched_register_mutex);
131 tracing_sched_unregister();
132 mutex_unlock(&sched_register_mutex);
135 void tracing_start_cmdline_record(void)
137 tracing_start_sched_switch();
140 void tracing_stop_cmdline_record(void)
142 tracing_stop_sched_switch();
146 * tracing_start_sched_switch_record - start tracing context switches
148 * Turns on context switch tracing for a tracer.
150 void tracing_start_sched_switch_record(void)
152 if (unlikely(!ctx_trace)) {
157 tracing_start_sched_switch();
159 mutex_lock(&sched_register_mutex);
161 mutex_unlock(&sched_register_mutex);
165 * tracing_stop_sched_switch_record - start tracing context switches
167 * Turns off context switch tracing for a tracer.
169 void tracing_stop_sched_switch_record(void)
171 mutex_lock(&sched_register_mutex);
173 WARN_ON(tracer_enabled < 0);
174 mutex_unlock(&sched_register_mutex);
176 tracing_stop_sched_switch();
180 * tracing_sched_switch_assign_trace - assign a trace array for ctx switch
181 * @tr: trace array pointer to assign
183 * Some tracers might want to record the context switches in their
184 * trace. This function lets those tracers assign the trace array
187 void tracing_sched_switch_assign_trace(struct trace_array *tr)
192 static void stop_sched_trace(struct trace_array *tr)
194 tracing_stop_sched_switch_record();
197 static int sched_switch_trace_init(struct trace_array *tr)
200 tracing_reset_online_cpus(tr);
201 tracing_start_sched_switch_record();
205 static void sched_switch_trace_reset(struct trace_array *tr)
208 stop_sched_trace(tr);
211 static void sched_switch_trace_start(struct trace_array *tr)
216 static void sched_switch_trace_stop(struct trace_array *tr)
221 static struct tracer sched_switch_trace __read_mostly =
223 .name = "sched_switch",
224 .init = sched_switch_trace_init,
225 .reset = sched_switch_trace_reset,
226 .start = sched_switch_trace_start,
227 .stop = sched_switch_trace_stop,
228 .wait_pipe = poll_wait_pipe,
229 #ifdef CONFIG_FTRACE_SELFTEST
230 .selftest = trace_selftest_startup_sched_switch,
234 __init static int init_sched_switch_trace(void)
236 return register_tracer(&sched_switch_trace);
238 device_initcall(init_sched_switch_trace);