]>
Commit | Line | Data |
---|---|---|
35e8e302 SR |
1 | /* |
2 | * trace context switch | |
3 | * | |
4 | * Copyright (C) 2007 Steven Rostedt <srostedt@redhat.com> | |
5 | * | |
6 | */ | |
7 | #include <linux/module.h> | |
8 | #include <linux/fs.h> | |
9 | #include <linux/debugfs.h> | |
10 | #include <linux/kallsyms.h> | |
11 | #include <linux/uaccess.h> | |
35e8e302 | 12 | #include <linux/ftrace.h> |
b07c3f19 | 13 | #include <trace/sched.h> |
35e8e302 SR |
14 | |
15 | #include "trace.h" | |
16 | ||
17 | static struct trace_array *ctx_trace; | |
18 | static int __read_mostly tracer_enabled; | |
efade6e7 FW |
19 | static int sched_ref; |
20 | static DEFINE_MUTEX(sched_register_mutex); | |
35e8e302 | 21 | |
e309b41d | 22 | static void |
b07c3f19 | 23 | probe_sched_switch(struct rq *__rq, struct task_struct *prev, |
5b82a1b0 | 24 | struct task_struct *next) |
35e8e302 | 25 | { |
35e8e302 SR |
26 | struct trace_array_cpu *data; |
27 | unsigned long flags; | |
35e8e302 | 28 | int cpu; |
38697053 | 29 | int pc; |
35e8e302 | 30 | |
efade6e7 | 31 | if (!sched_ref) |
b07c3f19 MD |
32 | return; |
33 | ||
41bc8144 SR |
34 | tracing_record_cmdline(prev); |
35 | tracing_record_cmdline(next); | |
36 | ||
35e8e302 SR |
37 | if (!tracer_enabled) |
38 | return; | |
39 | ||
38697053 | 40 | pc = preempt_count(); |
18cef379 | 41 | local_irq_save(flags); |
35e8e302 | 42 | cpu = raw_smp_processor_id(); |
b07c3f19 | 43 | data = ctx_trace->data[cpu]; |
35e8e302 | 44 | |
3ea2e6d7 | 45 | if (likely(!atomic_read(&data->disabled))) |
7be42151 | 46 | tracing_sched_switch_trace(ctx_trace, prev, next, flags, pc); |
35e8e302 | 47 | |
18cef379 | 48 | local_irq_restore(flags); |
35e8e302 SR |
49 | } |
50 | ||
4e655519 | 51 | static void |
468a15bb | 52 | probe_sched_wakeup(struct rq *__rq, struct task_struct *wakee, int success) |
57422797 | 53 | { |
57422797 IM |
54 | struct trace_array_cpu *data; |
55 | unsigned long flags; | |
38697053 | 56 | int cpu, pc; |
57422797 | 57 | |
b07c3f19 | 58 | if (!likely(tracer_enabled)) |
57422797 IM |
59 | return; |
60 | ||
38697053 | 61 | pc = preempt_count(); |
b07c3f19 | 62 | tracing_record_cmdline(current); |
d9af56fb | 63 | |
57422797 IM |
64 | local_irq_save(flags); |
65 | cpu = raw_smp_processor_id(); | |
b07c3f19 | 66 | data = ctx_trace->data[cpu]; |
57422797 | 67 | |
3ea2e6d7 | 68 | if (likely(!atomic_read(&data->disabled))) |
7be42151 | 69 | tracing_sched_wakeup_trace(ctx_trace, wakee, current, |
38697053 | 70 | flags, pc); |
57422797 | 71 | |
57422797 IM |
72 | local_irq_restore(flags); |
73 | } | |
74 | ||
5b82a1b0 MD |
75 | static int tracing_sched_register(void) |
76 | { | |
77 | int ret; | |
78 | ||
b07c3f19 | 79 | ret = register_trace_sched_wakeup(probe_sched_wakeup); |
5b82a1b0 | 80 | if (ret) { |
b07c3f19 | 81 | pr_info("wakeup trace: Couldn't activate tracepoint" |
5b82a1b0 MD |
82 | " probe to kernel_sched_wakeup\n"); |
83 | return ret; | |
84 | } | |
85 | ||
b07c3f19 | 86 | ret = register_trace_sched_wakeup_new(probe_sched_wakeup); |
5b82a1b0 | 87 | if (ret) { |
b07c3f19 | 88 | pr_info("wakeup trace: Couldn't activate tracepoint" |
5b82a1b0 MD |
89 | " probe to kernel_sched_wakeup_new\n"); |
90 | goto fail_deprobe; | |
91 | } | |
92 | ||
b07c3f19 | 93 | ret = register_trace_sched_switch(probe_sched_switch); |
5b82a1b0 | 94 | if (ret) { |
b07c3f19 | 95 | pr_info("sched trace: Couldn't activate tracepoint" |
5b82a1b0 MD |
96 | " probe to kernel_sched_schedule\n"); |
97 | goto fail_deprobe_wake_new; | |
98 | } | |
99 | ||
100 | return ret; | |
101 | fail_deprobe_wake_new: | |
b07c3f19 | 102 | unregister_trace_sched_wakeup_new(probe_sched_wakeup); |
5b82a1b0 | 103 | fail_deprobe: |
b07c3f19 | 104 | unregister_trace_sched_wakeup(probe_sched_wakeup); |
5b82a1b0 MD |
105 | return ret; |
106 | } | |
107 | ||
108 | static void tracing_sched_unregister(void) | |
109 | { | |
b07c3f19 MD |
110 | unregister_trace_sched_switch(probe_sched_switch); |
111 | unregister_trace_sched_wakeup_new(probe_sched_wakeup); | |
112 | unregister_trace_sched_wakeup(probe_sched_wakeup); | |
5b82a1b0 MD |
113 | } |
114 | ||
f2252935 | 115 | static void tracing_start_sched_switch(void) |
5b82a1b0 | 116 | { |
efade6e7 | 117 | mutex_lock(&sched_register_mutex); |
e168e051 | 118 | if (!(sched_ref++)) |
5b82a1b0 | 119 | tracing_sched_register(); |
efade6e7 | 120 | mutex_unlock(&sched_register_mutex); |
5b82a1b0 MD |
121 | } |
122 | ||
f2252935 | 123 | static void tracing_stop_sched_switch(void) |
5b82a1b0 | 124 | { |
efade6e7 | 125 | mutex_lock(&sched_register_mutex); |
e168e051 | 126 | if (!(--sched_ref)) |
5b82a1b0 | 127 | tracing_sched_unregister(); |
efade6e7 | 128 | mutex_unlock(&sched_register_mutex); |
5b82a1b0 MD |
129 | } |
130 | ||
41bc8144 SR |
131 | void tracing_start_cmdline_record(void) |
132 | { | |
133 | tracing_start_sched_switch(); | |
134 | } | |
135 | ||
136 | void tracing_stop_cmdline_record(void) | |
137 | { | |
138 | tracing_stop_sched_switch(); | |
139 | } | |
140 | ||
75f5c47d | 141 | /** |
e168e051 SR |
142 | * tracing_start_sched_switch_record - start tracing context switches |
143 | * | |
144 | * Turns on context switch tracing for a tracer. | |
145 | */ | |
146 | void tracing_start_sched_switch_record(void) | |
147 | { | |
148 | if (unlikely(!ctx_trace)) { | |
149 | WARN_ON(1); | |
150 | return; | |
151 | } | |
152 | ||
153 | tracing_start_sched_switch(); | |
154 | ||
155 | mutex_lock(&sched_register_mutex); | |
156 | tracer_enabled++; | |
157 | mutex_unlock(&sched_register_mutex); | |
158 | } | |
159 | ||
160 | /** | |
161 | * tracing_stop_sched_switch_record - start tracing context switches | |
162 | * | |
163 | * Turns off context switch tracing for a tracer. | |
164 | */ | |
165 | void tracing_stop_sched_switch_record(void) | |
166 | { | |
167 | mutex_lock(&sched_register_mutex); | |
168 | tracer_enabled--; | |
169 | WARN_ON(tracer_enabled < 0); | |
170 | mutex_unlock(&sched_register_mutex); | |
171 | ||
172 | tracing_stop_sched_switch(); | |
173 | } | |
174 | ||
175 | /** | |
176 | * tracing_sched_switch_assign_trace - assign a trace array for ctx switch | |
75f5c47d SR |
177 | * @tr: trace array pointer to assign |
178 | * | |
179 | * Some tracers might want to record the context switches in their | |
180 | * trace. This function lets those tracers assign the trace array | |
181 | * to use. | |
182 | */ | |
e168e051 | 183 | void tracing_sched_switch_assign_trace(struct trace_array *tr) |
75f5c47d SR |
184 | { |
185 | ctx_trace = tr; | |
186 | } | |
187 | ||
e309b41d | 188 | static void stop_sched_trace(struct trace_array *tr) |
35e8e302 | 189 | { |
e168e051 | 190 | tracing_stop_sched_switch_record(); |
35e8e302 SR |
191 | } |
192 | ||
1c80025a | 193 | static int sched_switch_trace_init(struct trace_array *tr) |
35e8e302 SR |
194 | { |
195 | ctx_trace = tr; | |
b6f11df2 | 196 | tracing_start_sched_switch_record(); |
1c80025a | 197 | return 0; |
35e8e302 SR |
198 | } |
199 | ||
e309b41d | 200 | static void sched_switch_trace_reset(struct trace_array *tr) |
35e8e302 | 201 | { |
c76f0694 | 202 | if (sched_ref) |
35e8e302 SR |
203 | stop_sched_trace(tr); |
204 | } | |
205 | ||
9036990d SR |
206 | static void sched_switch_trace_start(struct trace_array *tr) |
207 | { | |
213cc060 | 208 | tracing_reset_online_cpus(tr); |
9036990d SR |
209 | tracing_start_sched_switch(); |
210 | } | |
211 | ||
212 | static void sched_switch_trace_stop(struct trace_array *tr) | |
213 | { | |
214 | tracing_stop_sched_switch(); | |
215 | } | |
216 | ||
75f5c47d | 217 | static struct tracer sched_switch_trace __read_mostly = |
35e8e302 SR |
218 | { |
219 | .name = "sched_switch", | |
220 | .init = sched_switch_trace_init, | |
221 | .reset = sched_switch_trace_reset, | |
9036990d SR |
222 | .start = sched_switch_trace_start, |
223 | .stop = sched_switch_trace_stop, | |
60a11774 SR |
224 | #ifdef CONFIG_FTRACE_SELFTEST |
225 | .selftest = trace_selftest_startup_sched_switch, | |
226 | #endif | |
35e8e302 SR |
227 | }; |
228 | ||
229 | __init static int init_sched_switch_trace(void) | |
230 | { | |
231 | return register_tracer(&sched_switch_trace); | |
232 | } | |
233 | device_initcall(init_sched_switch_trace); | |
c71dd42d | 234 |