]>
Commit | Line | Data |
---|---|---|
352ad25a SR |
1 | /* |
2 | * trace task wakeup timings | |
3 | * | |
4 | * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> | |
5 | * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com> | |
6 | * | |
7 | * Based on code from the latency_tracer, that is: | |
8 | * | |
9 | * Copyright (C) 2004-2006 Ingo Molnar | |
10 | * Copyright (C) 2004 William Lee Irwin III | |
11 | */ | |
12 | #include <linux/module.h> | |
13 | #include <linux/fs.h> | |
14 | #include <linux/debugfs.h> | |
15 | #include <linux/kallsyms.h> | |
16 | #include <linux/uaccess.h> | |
17 | #include <linux/ftrace.h> | |
b07c3f19 | 18 | #include <trace/sched.h> |
352ad25a SR |
19 | |
20 | #include "trace.h" | |
21 | ||
22 | static struct trace_array *wakeup_trace; | |
23 | static int __read_mostly tracer_enabled; | |
24 | ||
25 | static struct task_struct *wakeup_task; | |
26 | static int wakeup_cpu; | |
27 | static unsigned wakeup_prio = -1; | |
3244351c | 28 | static int wakeup_rt; |
352ad25a | 29 | |
e59494f4 SR |
30 | static raw_spinlock_t wakeup_lock = |
31 | (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED; | |
352ad25a | 32 | |
e309b41d | 33 | static void __wakeup_reset(struct trace_array *tr); |
352ad25a | 34 | |
606576ce | 35 | #ifdef CONFIG_FUNCTION_TRACER |
7e18d8e7 SR |
36 | /* |
37 | * irqsoff uses its own tracer function to keep the overhead down: | |
38 | */ | |
39 | static void | |
40 | wakeup_tracer_call(unsigned long ip, unsigned long parent_ip) | |
41 | { | |
42 | struct trace_array *tr = wakeup_trace; | |
43 | struct trace_array_cpu *data; | |
44 | unsigned long flags; | |
45 | long disabled; | |
46 | int resched; | |
47 | int cpu; | |
38697053 | 48 | int pc; |
7e18d8e7 SR |
49 | |
50 | if (likely(!wakeup_task)) | |
51 | return; | |
52 | ||
38697053 | 53 | pc = preempt_count(); |
182e9f5f | 54 | resched = ftrace_preempt_disable(); |
7e18d8e7 SR |
55 | |
56 | cpu = raw_smp_processor_id(); | |
57 | data = tr->data[cpu]; | |
58 | disabled = atomic_inc_return(&data->disabled); | |
59 | if (unlikely(disabled != 1)) | |
60 | goto out; | |
61 | ||
e59494f4 SR |
62 | local_irq_save(flags); |
63 | __raw_spin_lock(&wakeup_lock); | |
7e18d8e7 SR |
64 | |
65 | if (unlikely(!wakeup_task)) | |
66 | goto unlock; | |
67 | ||
68 | /* | |
69 | * The task can't disappear because it needs to | |
70 | * wake up first, and we have the wakeup_lock. | |
71 | */ | |
72 | if (task_cpu(wakeup_task) != cpu) | |
73 | goto unlock; | |
74 | ||
38697053 | 75 | trace_function(tr, data, ip, parent_ip, flags, pc); |
7e18d8e7 SR |
76 | |
77 | unlock: | |
e59494f4 SR |
78 | __raw_spin_unlock(&wakeup_lock); |
79 | local_irq_restore(flags); | |
7e18d8e7 SR |
80 | |
81 | out: | |
82 | atomic_dec(&data->disabled); | |
83 | ||
182e9f5f | 84 | ftrace_preempt_enable(resched); |
7e18d8e7 SR |
85 | } |
86 | ||
87 | static struct ftrace_ops trace_ops __read_mostly = | |
88 | { | |
89 | .func = wakeup_tracer_call, | |
90 | }; | |
606576ce | 91 | #endif /* CONFIG_FUNCTION_TRACER */ |
7e18d8e7 | 92 | |
352ad25a SR |
93 | /* |
94 | * Should this new latency be reported/recorded? | |
95 | */ | |
e309b41d | 96 | static int report_latency(cycle_t delta) |
352ad25a SR |
97 | { |
98 | if (tracing_thresh) { | |
99 | if (delta < tracing_thresh) | |
100 | return 0; | |
101 | } else { | |
102 | if (delta <= tracing_max_latency) | |
103 | return 0; | |
104 | } | |
105 | return 1; | |
106 | } | |
107 | ||
5b82a1b0 | 108 | static void notrace |
b07c3f19 | 109 | probe_wakeup_sched_switch(struct rq *rq, struct task_struct *prev, |
5b82a1b0 | 110 | struct task_struct *next) |
352ad25a SR |
111 | { |
112 | unsigned long latency = 0, t0 = 0, t1 = 0; | |
352ad25a SR |
113 | struct trace_array_cpu *data; |
114 | cycle_t T0, T1, delta; | |
115 | unsigned long flags; | |
116 | long disabled; | |
117 | int cpu; | |
38697053 | 118 | int pc; |
352ad25a | 119 | |
b07c3f19 MD |
120 | tracing_record_cmdline(prev); |
121 | ||
352ad25a SR |
122 | if (unlikely(!tracer_enabled)) |
123 | return; | |
124 | ||
125 | /* | |
126 | * When we start a new trace, we set wakeup_task to NULL | |
127 | * and then set tracer_enabled = 1. We want to make sure | |
128 | * that another CPU does not see the tracer_enabled = 1 | |
129 | * and the wakeup_task with an older task, that might | |
130 | * actually be the same as next. | |
131 | */ | |
132 | smp_rmb(); | |
133 | ||
134 | if (next != wakeup_task) | |
135 | return; | |
136 | ||
38697053 SR |
137 | pc = preempt_count(); |
138 | ||
7e18d8e7 | 139 | /* The task we are waiting for is waking up */ |
b07c3f19 | 140 | data = wakeup_trace->data[wakeup_cpu]; |
352ad25a SR |
141 | |
142 | /* disable local data, not wakeup_cpu data */ | |
143 | cpu = raw_smp_processor_id(); | |
b07c3f19 | 144 | disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled); |
352ad25a SR |
145 | if (likely(disabled != 1)) |
146 | goto out; | |
147 | ||
e59494f4 SR |
148 | local_irq_save(flags); |
149 | __raw_spin_lock(&wakeup_lock); | |
352ad25a SR |
150 | |
151 | /* We could race with grabbing wakeup_lock */ | |
152 | if (unlikely(!tracer_enabled || next != wakeup_task)) | |
153 | goto out_unlock; | |
154 | ||
38697053 | 155 | trace_function(wakeup_trace, data, CALLER_ADDR1, CALLER_ADDR2, flags, pc); |
f8ec1062 | 156 | tracing_sched_switch_trace(wakeup_trace, data, prev, next, flags, pc); |
352ad25a SR |
157 | |
158 | /* | |
159 | * usecs conversion is slow so we try to delay the conversion | |
160 | * as long as possible: | |
161 | */ | |
162 | T0 = data->preempt_timestamp; | |
750ed1a4 | 163 | T1 = ftrace_now(cpu); |
352ad25a SR |
164 | delta = T1-T0; |
165 | ||
166 | if (!report_latency(delta)) | |
167 | goto out_unlock; | |
168 | ||
169 | latency = nsecs_to_usecs(delta); | |
170 | ||
171 | tracing_max_latency = delta; | |
172 | t0 = nsecs_to_usecs(T0); | |
173 | t1 = nsecs_to_usecs(T1); | |
174 | ||
b07c3f19 | 175 | update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu); |
352ad25a | 176 | |
352ad25a | 177 | out_unlock: |
b07c3f19 | 178 | __wakeup_reset(wakeup_trace); |
e59494f4 SR |
179 | __raw_spin_unlock(&wakeup_lock); |
180 | local_irq_restore(flags); | |
352ad25a | 181 | out: |
b07c3f19 | 182 | atomic_dec(&wakeup_trace->data[cpu]->disabled); |
5b82a1b0 MD |
183 | } |
184 | ||
e309b41d | 185 | static void __wakeup_reset(struct trace_array *tr) |
352ad25a | 186 | { |
352ad25a SR |
187 | int cpu; |
188 | ||
b3a8c348 | 189 | for_each_possible_cpu(cpu) |
3928a8a2 | 190 | tracing_reset(tr, cpu); |
352ad25a SR |
191 | |
192 | wakeup_cpu = -1; | |
193 | wakeup_prio = -1; | |
194 | ||
195 | if (wakeup_task) | |
196 | put_task_struct(wakeup_task); | |
197 | ||
198 | wakeup_task = NULL; | |
199 | } | |
200 | ||
e309b41d | 201 | static void wakeup_reset(struct trace_array *tr) |
352ad25a SR |
202 | { |
203 | unsigned long flags; | |
204 | ||
e59494f4 SR |
205 | local_irq_save(flags); |
206 | __raw_spin_lock(&wakeup_lock); | |
352ad25a | 207 | __wakeup_reset(tr); |
e59494f4 SR |
208 | __raw_spin_unlock(&wakeup_lock); |
209 | local_irq_restore(flags); | |
352ad25a SR |
210 | } |
211 | ||
e309b41d | 212 | static void |
468a15bb | 213 | probe_wakeup(struct rq *rq, struct task_struct *p, int success) |
352ad25a | 214 | { |
f8ec1062 | 215 | struct trace_array_cpu *data; |
352ad25a SR |
216 | int cpu = smp_processor_id(); |
217 | unsigned long flags; | |
218 | long disabled; | |
38697053 | 219 | int pc; |
352ad25a | 220 | |
b07c3f19 MD |
221 | if (likely(!tracer_enabled)) |
222 | return; | |
223 | ||
224 | tracing_record_cmdline(p); | |
225 | tracing_record_cmdline(current); | |
226 | ||
3244351c | 227 | if ((wakeup_rt && !rt_task(p)) || |
352ad25a | 228 | p->prio >= wakeup_prio || |
b07c3f19 | 229 | p->prio >= current->prio) |
352ad25a SR |
230 | return; |
231 | ||
38697053 | 232 | pc = preempt_count(); |
b07c3f19 | 233 | disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled); |
352ad25a SR |
234 | if (unlikely(disabled != 1)) |
235 | goto out; | |
236 | ||
237 | /* interrupts should be off from try_to_wake_up */ | |
e59494f4 | 238 | __raw_spin_lock(&wakeup_lock); |
352ad25a SR |
239 | |
240 | /* check for races. */ | |
241 | if (!tracer_enabled || p->prio >= wakeup_prio) | |
242 | goto out_locked; | |
243 | ||
244 | /* reset the trace */ | |
b07c3f19 | 245 | __wakeup_reset(wakeup_trace); |
352ad25a SR |
246 | |
247 | wakeup_cpu = task_cpu(p); | |
248 | wakeup_prio = p->prio; | |
249 | ||
250 | wakeup_task = p; | |
251 | get_task_struct(wakeup_task); | |
252 | ||
253 | local_save_flags(flags); | |
254 | ||
f8ec1062 SR |
255 | data = wakeup_trace->data[wakeup_cpu]; |
256 | data->preempt_timestamp = ftrace_now(cpu); | |
257 | tracing_sched_wakeup_trace(wakeup_trace, data, p, current, | |
258 | flags, pc); | |
259 | trace_function(wakeup_trace, data, CALLER_ADDR1, CALLER_ADDR2, | |
260 | flags, pc); | |
352ad25a SR |
261 | |
262 | out_locked: | |
e59494f4 | 263 | __raw_spin_unlock(&wakeup_lock); |
352ad25a | 264 | out: |
b07c3f19 | 265 | atomic_dec(&wakeup_trace->data[cpu]->disabled); |
352ad25a SR |
266 | } |
267 | ||
e309b41d | 268 | static void start_wakeup_tracer(struct trace_array *tr) |
352ad25a | 269 | { |
5b82a1b0 MD |
270 | int ret; |
271 | ||
b07c3f19 | 272 | ret = register_trace_sched_wakeup(probe_wakeup); |
5b82a1b0 | 273 | if (ret) { |
b07c3f19 | 274 | pr_info("wakeup trace: Couldn't activate tracepoint" |
5b82a1b0 MD |
275 | " probe to kernel_sched_wakeup\n"); |
276 | return; | |
277 | } | |
278 | ||
b07c3f19 | 279 | ret = register_trace_sched_wakeup_new(probe_wakeup); |
5b82a1b0 | 280 | if (ret) { |
b07c3f19 | 281 | pr_info("wakeup trace: Couldn't activate tracepoint" |
5b82a1b0 MD |
282 | " probe to kernel_sched_wakeup_new\n"); |
283 | goto fail_deprobe; | |
284 | } | |
285 | ||
b07c3f19 | 286 | ret = register_trace_sched_switch(probe_wakeup_sched_switch); |
5b82a1b0 | 287 | if (ret) { |
b07c3f19 | 288 | pr_info("sched trace: Couldn't activate tracepoint" |
5b82a1b0 MD |
289 | " probe to kernel_sched_schedule\n"); |
290 | goto fail_deprobe_wake_new; | |
291 | } | |
292 | ||
352ad25a SR |
293 | wakeup_reset(tr); |
294 | ||
295 | /* | |
296 | * Don't let the tracer_enabled = 1 show up before | |
297 | * the wakeup_task is reset. This may be overkill since | |
298 | * wakeup_reset does a spin_unlock after setting the | |
299 | * wakeup_task to NULL, but I want to be safe. | |
300 | * This is a slow path anyway. | |
301 | */ | |
302 | smp_wmb(); | |
303 | ||
7e18d8e7 | 304 | register_ftrace_function(&trace_ops); |
352ad25a | 305 | |
5bc4564b | 306 | if (tracing_is_enabled()) |
9036990d | 307 | tracer_enabled = 1; |
5bc4564b | 308 | else |
9036990d | 309 | tracer_enabled = 0; |
ad591240 | 310 | |
352ad25a | 311 | return; |
5b82a1b0 | 312 | fail_deprobe_wake_new: |
b07c3f19 | 313 | unregister_trace_sched_wakeup_new(probe_wakeup); |
5b82a1b0 | 314 | fail_deprobe: |
b07c3f19 | 315 | unregister_trace_sched_wakeup(probe_wakeup); |
352ad25a SR |
316 | } |
317 | ||
e309b41d | 318 | static void stop_wakeup_tracer(struct trace_array *tr) |
352ad25a SR |
319 | { |
320 | tracer_enabled = 0; | |
7e18d8e7 | 321 | unregister_ftrace_function(&trace_ops); |
b07c3f19 MD |
322 | unregister_trace_sched_switch(probe_wakeup_sched_switch); |
323 | unregister_trace_sched_wakeup_new(probe_wakeup); | |
324 | unregister_trace_sched_wakeup(probe_wakeup); | |
352ad25a SR |
325 | } |
326 | ||
3244351c | 327 | static int __wakeup_tracer_init(struct trace_array *tr) |
352ad25a | 328 | { |
745b1626 | 329 | tracing_max_latency = 0; |
352ad25a | 330 | wakeup_trace = tr; |
c76f0694 | 331 | start_wakeup_tracer(tr); |
1c80025a | 332 | return 0; |
352ad25a SR |
333 | } |
334 | ||
3244351c SR |
335 | static int wakeup_tracer_init(struct trace_array *tr) |
336 | { | |
337 | wakeup_rt = 0; | |
338 | return __wakeup_tracer_init(tr); | |
339 | } | |
340 | ||
341 | static int wakeup_rt_tracer_init(struct trace_array *tr) | |
342 | { | |
343 | wakeup_rt = 1; | |
344 | return __wakeup_tracer_init(tr); | |
345 | } | |
346 | ||
e309b41d | 347 | static void wakeup_tracer_reset(struct trace_array *tr) |
352ad25a | 348 | { |
c76f0694 SR |
349 | stop_wakeup_tracer(tr); |
350 | /* make sure we put back any tasks we are tracing */ | |
351 | wakeup_reset(tr); | |
352ad25a SR |
352 | } |
353 | ||
9036990d SR |
354 | static void wakeup_tracer_start(struct trace_array *tr) |
355 | { | |
356 | wakeup_reset(tr); | |
357 | tracer_enabled = 1; | |
9036990d SR |
358 | } |
359 | ||
360 | static void wakeup_tracer_stop(struct trace_array *tr) | |
361 | { | |
362 | tracer_enabled = 0; | |
352ad25a SR |
363 | } |
364 | ||
365 | static struct tracer wakeup_tracer __read_mostly = | |
366 | { | |
367 | .name = "wakeup", | |
368 | .init = wakeup_tracer_init, | |
369 | .reset = wakeup_tracer_reset, | |
9036990d SR |
370 | .start = wakeup_tracer_start, |
371 | .stop = wakeup_tracer_stop, | |
352ad25a | 372 | .print_max = 1, |
60a11774 SR |
373 | #ifdef CONFIG_FTRACE_SELFTEST |
374 | .selftest = trace_selftest_startup_wakeup, | |
375 | #endif | |
352ad25a SR |
376 | }; |
377 | ||
3244351c SR |
378 | static struct tracer wakeup_rt_tracer __read_mostly = |
379 | { | |
380 | .name = "wakeup_rt", | |
381 | .init = wakeup_rt_tracer_init, | |
382 | .reset = wakeup_tracer_reset, | |
383 | .start = wakeup_tracer_start, | |
384 | .stop = wakeup_tracer_stop, | |
385 | .print_max = 1, | |
386 | #ifdef CONFIG_FTRACE_SELFTEST | |
387 | .selftest = trace_selftest_startup_wakeup, | |
388 | #endif | |
389 | }; | |
390 | ||
352ad25a SR |
391 | __init static int init_wakeup_tracer(void) |
392 | { | |
393 | int ret; | |
394 | ||
395 | ret = register_tracer(&wakeup_tracer); | |
396 | if (ret) | |
397 | return ret; | |
398 | ||
3244351c SR |
399 | ret = register_tracer(&wakeup_rt_tracer); |
400 | if (ret) | |
401 | return ret; | |
402 | ||
352ad25a SR |
403 | return 0; |
404 | } | |
405 | device_initcall(init_wakeup_tracer); |