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/events/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
;
25 tracing_sched_switch_trace(struct trace_array
*tr
,
26 struct task_struct
*prev
,
27 struct task_struct
*next
,
28 unsigned long flags
, int pc
)
30 struct ftrace_event_call
*call
= &event_context_switch
;
31 struct ring_buffer
*buffer
= tr
->buffer
;
32 struct ring_buffer_event
*event
;
33 struct ctx_switch_entry
*entry
;
35 event
= trace_buffer_lock_reserve(buffer
, TRACE_CTX
,
36 sizeof(*entry
), flags
, pc
);
39 entry
= ring_buffer_event_data(event
);
40 entry
->prev_pid
= prev
->pid
;
41 entry
->prev_prio
= prev
->prio
;
42 entry
->prev_state
= prev
->state
;
43 entry
->next_pid
= next
->pid
;
44 entry
->next_prio
= next
->prio
;
45 entry
->next_state
= next
->state
;
46 entry
->next_cpu
= task_cpu(next
);
48 if (!filter_check_discard(call
, entry
, buffer
, event
))
49 trace_buffer_unlock_commit(buffer
, event
, flags
, pc
);
53 probe_sched_switch(void *ignore
, struct task_struct
*prev
, struct task_struct
*next
)
55 struct trace_array_cpu
*data
;
60 if (unlikely(!sched_ref
))
63 tracing_record_cmdline(prev
);
64 tracing_record_cmdline(next
);
66 if (!tracer_enabled
|| sched_stopped
)
70 local_irq_save(flags
);
71 cpu
= raw_smp_processor_id();
72 data
= ctx_trace
->data
[cpu
];
74 if (likely(!atomic_read(&data
->disabled
)))
75 tracing_sched_switch_trace(ctx_trace
, prev
, next
, flags
, pc
);
77 local_irq_restore(flags
);
81 tracing_sched_wakeup_trace(struct trace_array
*tr
,
82 struct task_struct
*wakee
,
83 struct task_struct
*curr
,
84 unsigned long flags
, int pc
)
86 struct ftrace_event_call
*call
= &event_wakeup
;
87 struct ring_buffer_event
*event
;
88 struct ctx_switch_entry
*entry
;
89 struct ring_buffer
*buffer
= tr
->buffer
;
91 event
= trace_buffer_lock_reserve(buffer
, TRACE_WAKE
,
92 sizeof(*entry
), flags
, pc
);
95 entry
= ring_buffer_event_data(event
);
96 entry
->prev_pid
= curr
->pid
;
97 entry
->prev_prio
= curr
->prio
;
98 entry
->prev_state
= curr
->state
;
99 entry
->next_pid
= wakee
->pid
;
100 entry
->next_prio
= wakee
->prio
;
101 entry
->next_state
= wakee
->state
;
102 entry
->next_cpu
= task_cpu(wakee
);
104 if (!filter_check_discard(call
, entry
, buffer
, event
))
105 ring_buffer_unlock_commit(buffer
, event
);
106 ftrace_trace_stack(tr
->buffer
, flags
, 6, pc
);
107 ftrace_trace_userstack(tr
->buffer
, flags
, pc
);
111 probe_sched_wakeup(void *ignore
, struct task_struct
*wakee
, int success
)
113 struct trace_array_cpu
*data
;
117 if (unlikely(!sched_ref
))
120 tracing_record_cmdline(current
);
122 if (!tracer_enabled
|| sched_stopped
)
125 pc
= preempt_count();
126 local_irq_save(flags
);
127 cpu
= raw_smp_processor_id();
128 data
= ctx_trace
->data
[cpu
];
130 if (likely(!atomic_read(&data
->disabled
)))
131 tracing_sched_wakeup_trace(ctx_trace
, wakee
, current
,
134 local_irq_restore(flags
);
137 static int tracing_sched_register(void)
141 ret
= register_trace_sched_wakeup(probe_sched_wakeup
, NULL
);
143 pr_info("wakeup trace: Couldn't activate tracepoint"
144 " probe to kernel_sched_wakeup\n");
148 ret
= register_trace_sched_wakeup_new(probe_sched_wakeup
, NULL
);
150 pr_info("wakeup trace: Couldn't activate tracepoint"
151 " probe to kernel_sched_wakeup_new\n");
155 ret
= register_trace_sched_switch(probe_sched_switch
, NULL
);
157 pr_info("sched trace: Couldn't activate tracepoint"
158 " probe to kernel_sched_switch\n");
159 goto fail_deprobe_wake_new
;
163 fail_deprobe_wake_new
:
164 unregister_trace_sched_wakeup_new(probe_sched_wakeup
, NULL
);
166 unregister_trace_sched_wakeup(probe_sched_wakeup
, NULL
);
170 static void tracing_sched_unregister(void)
172 unregister_trace_sched_switch(probe_sched_switch
, NULL
);
173 unregister_trace_sched_wakeup_new(probe_sched_wakeup
, NULL
);
174 unregister_trace_sched_wakeup(probe_sched_wakeup
, NULL
);
177 static void tracing_start_sched_switch(void)
179 mutex_lock(&sched_register_mutex
);
181 tracing_sched_register();
182 mutex_unlock(&sched_register_mutex
);
185 static void tracing_stop_sched_switch(void)
187 mutex_lock(&sched_register_mutex
);
189 tracing_sched_unregister();
190 mutex_unlock(&sched_register_mutex
);
193 void tracing_start_cmdline_record(void)
195 tracing_start_sched_switch();
198 void tracing_stop_cmdline_record(void)
200 tracing_stop_sched_switch();
204 * tracing_start_sched_switch_record - start tracing context switches
206 * Turns on context switch tracing for a tracer.
208 void tracing_start_sched_switch_record(void)
210 if (unlikely(!ctx_trace
)) {
215 tracing_start_sched_switch();
217 mutex_lock(&sched_register_mutex
);
219 mutex_unlock(&sched_register_mutex
);
223 * tracing_stop_sched_switch_record - start tracing context switches
225 * Turns off context switch tracing for a tracer.
227 void tracing_stop_sched_switch_record(void)
229 mutex_lock(&sched_register_mutex
);
231 WARN_ON(tracer_enabled
< 0);
232 mutex_unlock(&sched_register_mutex
);
234 tracing_stop_sched_switch();
238 * tracing_sched_switch_assign_trace - assign a trace array for ctx switch
239 * @tr: trace array pointer to assign
241 * Some tracers might want to record the context switches in their
242 * trace. This function lets those tracers assign the trace array
245 void tracing_sched_switch_assign_trace(struct trace_array
*tr
)
250 static void stop_sched_trace(struct trace_array
*tr
)
252 tracing_stop_sched_switch_record();
255 static int sched_switch_trace_init(struct trace_array
*tr
)
258 tracing_reset_online_cpus(tr
);
259 tracing_start_sched_switch_record();
263 static void sched_switch_trace_reset(struct trace_array
*tr
)
266 stop_sched_trace(tr
);
269 static void sched_switch_trace_start(struct trace_array
*tr
)
274 static void sched_switch_trace_stop(struct trace_array
*tr
)
279 static struct tracer sched_switch_trace __read_mostly
=
281 .name
= "sched_switch",
282 .init
= sched_switch_trace_init
,
283 .reset
= sched_switch_trace_reset
,
284 .start
= sched_switch_trace_start
,
285 .stop
= sched_switch_trace_stop
,
286 .wait_pipe
= poll_wait_pipe
,
287 #ifdef CONFIG_FTRACE_SELFTEST
288 .selftest
= trace_selftest_startup_sched_switch
,
292 __init
static int init_sched_switch_trace(void)
294 return register_tracer(&sched_switch_trace
);
296 device_initcall(init_sched_switch_trace
);