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;
19 static atomic_t sched_ref;
20 static DEFINE_MUTEX(tracepoint_mutex);
23 probe_sched_switch(struct rq *__rq, struct task_struct *prev,
24 struct task_struct *next)
26 struct trace_array_cpu *data;
31 if (!atomic_read(&sched_ref))
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, data, prev, next, flags, pc);
48 local_irq_restore(flags);
52 probe_sched_wakeup(struct rq *__rq, struct task_struct *wakee)
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, data, wakee, current,
72 local_irq_restore(flags);
75 static void sched_switch_reset(struct trace_array *tr)
79 tr->time_start = ftrace_now(tr->cpu);
81 for_each_online_cpu(cpu)
82 tracing_reset(tr, cpu);
85 static int tracing_sched_register(void)
89 ret = register_trace_sched_wakeup(probe_sched_wakeup);
91 pr_info("wakeup trace: Couldn't activate tracepoint"
92 " probe to kernel_sched_wakeup\n");
96 ret = register_trace_sched_wakeup_new(probe_sched_wakeup);
98 pr_info("wakeup trace: Couldn't activate tracepoint"
99 " probe to kernel_sched_wakeup_new\n");
103 ret = register_trace_sched_switch(probe_sched_switch);
105 pr_info("sched trace: Couldn't activate tracepoint"
106 " probe to kernel_sched_schedule\n");
107 goto fail_deprobe_wake_new;
111 fail_deprobe_wake_new:
112 unregister_trace_sched_wakeup_new(probe_sched_wakeup);
114 unregister_trace_sched_wakeup(probe_sched_wakeup);
118 static void tracing_sched_unregister(void)
120 unregister_trace_sched_switch(probe_sched_switch);
121 unregister_trace_sched_wakeup_new(probe_sched_wakeup);
122 unregister_trace_sched_wakeup(probe_sched_wakeup);
125 static void tracing_start_sched_switch(void)
129 mutex_lock(&tracepoint_mutex);
130 ref = atomic_inc_return(&sched_ref);
132 tracing_sched_register();
133 mutex_unlock(&tracepoint_mutex);
136 static void tracing_stop_sched_switch(void)
140 mutex_lock(&tracepoint_mutex);
141 ref = atomic_dec_and_test(&sched_ref);
143 tracing_sched_unregister();
144 mutex_unlock(&tracepoint_mutex);
147 void tracing_start_cmdline_record(void)
149 tracing_start_sched_switch();
152 void tracing_stop_cmdline_record(void)
154 tracing_stop_sched_switch();
157 static void start_sched_trace(struct trace_array *tr)
159 sched_switch_reset(tr);
160 tracing_start_cmdline_record();
164 static void stop_sched_trace(struct trace_array *tr)
167 tracing_stop_cmdline_record();
170 static void sched_switch_trace_init(struct trace_array *tr)
175 start_sched_trace(tr);
178 static void sched_switch_trace_reset(struct trace_array *tr)
181 stop_sched_trace(tr);
184 static void sched_switch_trace_ctrl_update(struct trace_array *tr)
186 /* When starting a new trace, reset the buffers */
188 start_sched_trace(tr);
190 stop_sched_trace(tr);
193 static struct tracer sched_switch_trace __read_mostly =
195 .name = "sched_switch",
196 .init = sched_switch_trace_init,
197 .reset = sched_switch_trace_reset,
198 .ctrl_update = sched_switch_trace_ctrl_update,
199 #ifdef CONFIG_FTRACE_SELFTEST
200 .selftest = trace_selftest_startup_sched_switch,
204 __init static int init_sched_switch_trace(void)
208 if (atomic_read(&sched_ref))
209 ret = tracing_sched_register();
211 pr_info("error registering scheduler trace\n");
214 return register_tracer(&sched_switch_trace);
216 device_initcall(init_sched_switch_trace);