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(struct rq
*__rq
, struct task_struct
*prev
,
54 struct task_struct
*next
)
56 struct trace_array_cpu
*data
;
61 if (unlikely(!sched_ref
))
64 tracing_record_cmdline(prev
);
65 tracing_record_cmdline(next
);
67 if (!tracer_enabled
|| sched_stopped
)
71 local_irq_save(flags
);
72 cpu
= raw_smp_processor_id();
73 data
= ctx_trace
->data
[cpu
];
75 if (likely(!atomic_read(&data
->disabled
)))
76 tracing_sched_switch_trace(ctx_trace
, prev
, next
, flags
, pc
);
78 local_irq_restore(flags
);
82 tracing_sched_wakeup_trace(struct trace_array
*tr
,
83 struct task_struct
*wakee
,
84 struct task_struct
*curr
,
85 unsigned long flags
, int pc
)
87 struct ftrace_event_call
*call
= &event_wakeup
;
88 struct ring_buffer_event
*event
;
89 struct ctx_switch_entry
*entry
;
90 struct ring_buffer
*buffer
= tr
->buffer
;
92 event
= trace_buffer_lock_reserve(buffer
, TRACE_WAKE
,
93 sizeof(*entry
), flags
, pc
);
96 entry
= ring_buffer_event_data(event
);
97 entry
->prev_pid
= curr
->pid
;
98 entry
->prev_prio
= curr
->prio
;
99 entry
->prev_state
= curr
->state
;
100 entry
->next_pid
= wakee
->pid
;
101 entry
->next_prio
= wakee
->prio
;
102 entry
->next_state
= wakee
->state
;
103 entry
->next_cpu
= task_cpu(wakee
);
105 if (!filter_check_discard(call
, entry
, buffer
, event
))
106 ring_buffer_unlock_commit(buffer
, event
);
107 ftrace_trace_stack(tr
->buffer
, flags
, 6, pc
);
108 ftrace_trace_userstack(tr
->buffer
, flags
, pc
);
112 probe_sched_wakeup(struct rq
*__rq
, struct task_struct
*wakee
, int success
)
114 struct trace_array_cpu
*data
;
118 if (unlikely(!sched_ref
))
121 tracing_record_cmdline(current
);
123 if (!tracer_enabled
|| sched_stopped
)
126 pc
= preempt_count();
127 local_irq_save(flags
);
128 cpu
= raw_smp_processor_id();
129 data
= ctx_trace
->data
[cpu
];
131 if (likely(!atomic_read(&data
->disabled
)))
132 tracing_sched_wakeup_trace(ctx_trace
, wakee
, current
,
135 local_irq_restore(flags
);
138 static int tracing_sched_register(void)
142 ret
= register_trace_sched_wakeup(probe_sched_wakeup
);
144 pr_info("wakeup trace: Couldn't activate tracepoint"
145 " probe to kernel_sched_wakeup\n");
149 ret
= register_trace_sched_wakeup_new(probe_sched_wakeup
);
151 pr_info("wakeup trace: Couldn't activate tracepoint"
152 " probe to kernel_sched_wakeup_new\n");
156 ret
= register_trace_sched_switch(probe_sched_switch
);
158 pr_info("sched trace: Couldn't activate tracepoint"
159 " probe to kernel_sched_switch\n");
160 goto fail_deprobe_wake_new
;
164 fail_deprobe_wake_new
:
165 unregister_trace_sched_wakeup_new(probe_sched_wakeup
);
167 unregister_trace_sched_wakeup(probe_sched_wakeup
);
171 static void tracing_sched_unregister(void)
173 unregister_trace_sched_switch(probe_sched_switch
);
174 unregister_trace_sched_wakeup_new(probe_sched_wakeup
);
175 unregister_trace_sched_wakeup(probe_sched_wakeup
);
178 static void tracing_start_sched_switch(void)
180 mutex_lock(&sched_register_mutex
);
182 tracing_sched_register();
183 mutex_unlock(&sched_register_mutex
);
186 static void tracing_stop_sched_switch(void)
188 mutex_lock(&sched_register_mutex
);
190 tracing_sched_unregister();
191 mutex_unlock(&sched_register_mutex
);
194 void tracing_start_cmdline_record(void)
196 tracing_start_sched_switch();
199 void tracing_stop_cmdline_record(void)
201 tracing_stop_sched_switch();
205 * tracing_start_sched_switch_record - start tracing context switches
207 * Turns on context switch tracing for a tracer.
209 void tracing_start_sched_switch_record(void)
211 if (unlikely(!ctx_trace
)) {
216 tracing_start_sched_switch();
218 mutex_lock(&sched_register_mutex
);
220 mutex_unlock(&sched_register_mutex
);
224 * tracing_stop_sched_switch_record - start tracing context switches
226 * Turns off context switch tracing for a tracer.
228 void tracing_stop_sched_switch_record(void)
230 mutex_lock(&sched_register_mutex
);
232 WARN_ON(tracer_enabled
< 0);
233 mutex_unlock(&sched_register_mutex
);
235 tracing_stop_sched_switch();
239 * tracing_sched_switch_assign_trace - assign a trace array for ctx switch
240 * @tr: trace array pointer to assign
242 * Some tracers might want to record the context switches in their
243 * trace. This function lets those tracers assign the trace array
246 void tracing_sched_switch_assign_trace(struct trace_array
*tr
)
251 static void stop_sched_trace(struct trace_array
*tr
)
253 tracing_stop_sched_switch_record();
256 static int sched_switch_trace_init(struct trace_array
*tr
)
259 tracing_reset_online_cpus(tr
);
260 tracing_start_sched_switch_record();
264 static void sched_switch_trace_reset(struct trace_array
*tr
)
267 stop_sched_trace(tr
);
270 static void sched_switch_trace_start(struct trace_array
*tr
)
275 static void sched_switch_trace_stop(struct trace_array
*tr
)
280 static struct tracer sched_switch_trace __read_mostly
=
282 .name
= "sched_switch",
283 .init
= sched_switch_trace_init
,
284 .reset
= sched_switch_trace_reset
,
285 .start
= sched_switch_trace_start
,
286 .stop
= sched_switch_trace_stop
,
287 .wait_pipe
= poll_wait_pipe
,
288 #ifdef CONFIG_FTRACE_SELFTEST
289 .selftest
= trace_selftest_startup_sched_switch
,
293 __init
static int init_sched_switch_trace(void)
295 return register_tracer(&sched_switch_trace
);
297 device_initcall(init_sched_switch_trace
);