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;
19 static atomic_t sched_ref;
22 sched_switch_func(void *private, void *__rq, struct task_struct *prev,
23 struct task_struct *next)
25 struct trace_array **ptr = private;
26 struct trace_array *tr = *ptr;
27 struct trace_array_cpu *data;
32 tracing_record_cmdline(prev);
33 tracing_record_cmdline(next);
38 local_irq_save(flags);
39 cpu = raw_smp_processor_id();
41 disabled = atomic_inc_return(&data->disabled);
43 if (likely(disabled == 1))
44 tracing_sched_switch_trace(tr, data, prev, next, flags);
46 atomic_dec(&data->disabled);
47 local_irq_restore(flags);
51 sched_switch_callback(void *probe_data, void *call_data,
52 const char *format, va_list *args)
54 struct task_struct *prev;
55 struct task_struct *next;
58 if (!atomic_read(&sched_ref))
61 /* skip prev_pid %d next_pid %d prev_state %ld */
62 (void)va_arg(*args, int);
63 (void)va_arg(*args, int);
64 (void)va_arg(*args, long);
65 __rq = va_arg(*args, typeof(__rq));
66 prev = va_arg(*args, typeof(prev));
67 next = va_arg(*args, typeof(next));
70 * If tracer_switch_func only points to the local
71 * switch func, it still needs the ptr passed to it.
73 sched_switch_func(probe_data, __rq, prev, next);
77 wakeup_func(void *private, void *__rq, struct task_struct *wakee, struct
80 struct trace_array **ptr = private;
81 struct trace_array *tr = *ptr;
82 struct trace_array_cpu *data;
90 tracing_record_cmdline(curr);
92 local_irq_save(flags);
93 cpu = raw_smp_processor_id();
95 disabled = atomic_inc_return(&data->disabled);
97 if (likely(disabled == 1))
98 tracing_sched_wakeup_trace(tr, data, wakee, curr, flags);
100 atomic_dec(&data->disabled);
101 local_irq_restore(flags);
105 wake_up_callback(void *probe_data, void *call_data,
106 const char *format, va_list *args)
108 struct task_struct *curr;
109 struct task_struct *task;
112 if (likely(!tracer_enabled))
115 /* Skip pid %d state %ld */
116 (void)va_arg(*args, int);
117 (void)va_arg(*args, long);
118 /* now get the meat: "rq %p task %p rq->curr %p" */
119 __rq = va_arg(*args, typeof(__rq));
120 task = va_arg(*args, typeof(task));
121 curr = va_arg(*args, typeof(curr));
123 tracing_record_cmdline(task);
124 tracing_record_cmdline(curr);
126 wakeup_func(probe_data, __rq, task, curr);
129 static void sched_switch_reset(struct trace_array *tr)
133 tr->time_start = ftrace_now(tr->cpu);
135 for_each_online_cpu(cpu)
136 tracing_reset(tr->data[cpu]);
139 static int tracing_sched_register(void)
143 ret = marker_probe_register("kernel_sched_wakeup",
144 "pid %d state %ld ## rq %p task %p rq->curr %p",
148 pr_info("wakeup trace: Couldn't add marker"
149 " probe to kernel_sched_wakeup\n");
153 ret = marker_probe_register("kernel_sched_wakeup_new",
154 "pid %d state %ld ## rq %p task %p rq->curr %p",
158 pr_info("wakeup trace: Couldn't add marker"
159 " probe to kernel_sched_wakeup_new\n");
163 ret = marker_probe_register("kernel_sched_schedule",
164 "prev_pid %d next_pid %d prev_state %ld "
165 "## rq %p prev %p next %p",
166 sched_switch_callback,
169 pr_info("sched trace: Couldn't add marker"
170 " probe to kernel_sched_schedule\n");
171 goto fail_deprobe_wake_new;
175 fail_deprobe_wake_new:
176 marker_probe_unregister("kernel_sched_wakeup_new",
180 marker_probe_unregister("kernel_sched_wakeup",
186 static void tracing_sched_unregister(void)
188 marker_probe_unregister("kernel_sched_schedule",
189 sched_switch_callback,
191 marker_probe_unregister("kernel_sched_wakeup_new",
194 marker_probe_unregister("kernel_sched_wakeup",
199 void tracing_start_sched_switch(void)
203 ref = atomic_inc_return(&sched_ref);
205 tracing_sched_register();
208 void tracing_stop_sched_switch(void)
212 ref = atomic_dec_and_test(&sched_ref);
214 tracing_sched_unregister();
217 void tracing_start_cmdline_record(void)
219 tracing_start_sched_switch();
222 void tracing_stop_cmdline_record(void)
224 tracing_stop_sched_switch();
227 static void start_sched_trace(struct trace_array *tr)
229 sched_switch_reset(tr);
231 tracing_start_cmdline_record();
234 static void stop_sched_trace(struct trace_array *tr)
236 tracing_stop_cmdline_record();
240 static void sched_switch_trace_init(struct trace_array *tr)
245 start_sched_trace(tr);
248 static void sched_switch_trace_reset(struct trace_array *tr)
251 stop_sched_trace(tr);
254 static void sched_switch_trace_ctrl_update(struct trace_array *tr)
256 /* When starting a new trace, reset the buffers */
258 start_sched_trace(tr);
260 stop_sched_trace(tr);
263 static struct tracer sched_switch_trace __read_mostly =
265 .name = "sched_switch",
266 .init = sched_switch_trace_init,
267 .reset = sched_switch_trace_reset,
268 .ctrl_update = sched_switch_trace_ctrl_update,
269 #ifdef CONFIG_FTRACE_SELFTEST
270 .selftest = trace_selftest_startup_sched_switch,
274 __init static int init_sched_switch_trace(void)
278 if (atomic_read(&sched_ref))
279 ret = tracing_sched_register();
281 pr_info("error registering scheduler trace\n");
284 return register_tracer(&sched_switch_trace);
286 device_initcall(init_sched_switch_trace);