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); |
352ad25a SR |
156 | |
157 | /* | |
158 | * usecs conversion is slow so we try to delay the conversion | |
159 | * as long as possible: | |
160 | */ | |
161 | T0 = data->preempt_timestamp; | |
750ed1a4 | 162 | T1 = ftrace_now(cpu); |
352ad25a SR |
163 | delta = T1-T0; |
164 | ||
165 | if (!report_latency(delta)) | |
166 | goto out_unlock; | |
167 | ||
168 | latency = nsecs_to_usecs(delta); | |
169 | ||
170 | tracing_max_latency = delta; | |
171 | t0 = nsecs_to_usecs(T0); | |
172 | t1 = nsecs_to_usecs(T1); | |
173 | ||
b07c3f19 | 174 | update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu); |
352ad25a | 175 | |
352ad25a | 176 | out_unlock: |
b07c3f19 | 177 | __wakeup_reset(wakeup_trace); |
e59494f4 SR |
178 | __raw_spin_unlock(&wakeup_lock); |
179 | local_irq_restore(flags); | |
352ad25a | 180 | out: |
b07c3f19 | 181 | atomic_dec(&wakeup_trace->data[cpu]->disabled); |
5b82a1b0 MD |
182 | } |
183 | ||
e309b41d | 184 | static void __wakeup_reset(struct trace_array *tr) |
352ad25a SR |
185 | { |
186 | struct trace_array_cpu *data; | |
187 | int cpu; | |
188 | ||
352ad25a SR |
189 | for_each_possible_cpu(cpu) { |
190 | data = tr->data[cpu]; | |
3928a8a2 | 191 | tracing_reset(tr, cpu); |
352ad25a SR |
192 | } |
193 | ||
194 | wakeup_cpu = -1; | |
195 | wakeup_prio = -1; | |
196 | ||
197 | if (wakeup_task) | |
198 | put_task_struct(wakeup_task); | |
199 | ||
200 | wakeup_task = NULL; | |
201 | } | |
202 | ||
e309b41d | 203 | static void wakeup_reset(struct trace_array *tr) |
352ad25a SR |
204 | { |
205 | unsigned long flags; | |
206 | ||
e59494f4 SR |
207 | local_irq_save(flags); |
208 | __raw_spin_lock(&wakeup_lock); | |
352ad25a | 209 | __wakeup_reset(tr); |
e59494f4 SR |
210 | __raw_spin_unlock(&wakeup_lock); |
211 | local_irq_restore(flags); | |
352ad25a SR |
212 | } |
213 | ||
e309b41d | 214 | static void |
468a15bb | 215 | probe_wakeup(struct rq *rq, struct task_struct *p, int success) |
352ad25a SR |
216 | { |
217 | int cpu = smp_processor_id(); | |
218 | unsigned long flags; | |
219 | long disabled; | |
38697053 | 220 | int pc; |
352ad25a | 221 | |
b07c3f19 MD |
222 | if (likely(!tracer_enabled)) |
223 | return; | |
224 | ||
225 | tracing_record_cmdline(p); | |
226 | tracing_record_cmdline(current); | |
227 | ||
3244351c | 228 | if ((wakeup_rt && !rt_task(p)) || |
352ad25a | 229 | p->prio >= wakeup_prio || |
b07c3f19 | 230 | p->prio >= current->prio) |
352ad25a SR |
231 | return; |
232 | ||
38697053 | 233 | pc = preempt_count(); |
b07c3f19 | 234 | disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled); |
352ad25a SR |
235 | if (unlikely(disabled != 1)) |
236 | goto out; | |
237 | ||
238 | /* interrupts should be off from try_to_wake_up */ | |
e59494f4 | 239 | __raw_spin_lock(&wakeup_lock); |
352ad25a SR |
240 | |
241 | /* check for races. */ | |
242 | if (!tracer_enabled || p->prio >= wakeup_prio) | |
243 | goto out_locked; | |
244 | ||
245 | /* reset the trace */ | |
b07c3f19 | 246 | __wakeup_reset(wakeup_trace); |
352ad25a SR |
247 | |
248 | wakeup_cpu = task_cpu(p); | |
249 | wakeup_prio = p->prio; | |
250 | ||
251 | wakeup_task = p; | |
252 | get_task_struct(wakeup_task); | |
253 | ||
254 | local_save_flags(flags); | |
255 | ||
b07c3f19 MD |
256 | wakeup_trace->data[wakeup_cpu]->preempt_timestamp = ftrace_now(cpu); |
257 | trace_function(wakeup_trace, wakeup_trace->data[wakeup_cpu], | |
38697053 | 258 | CALLER_ADDR1, CALLER_ADDR2, flags, pc); |
352ad25a SR |
259 | |
260 | out_locked: | |
e59494f4 | 261 | __raw_spin_unlock(&wakeup_lock); |
352ad25a | 262 | out: |
b07c3f19 | 263 | atomic_dec(&wakeup_trace->data[cpu]->disabled); |
352ad25a SR |
264 | } |
265 | ||
e309b41d | 266 | static void start_wakeup_tracer(struct trace_array *tr) |
352ad25a | 267 | { |
5b82a1b0 MD |
268 | int ret; |
269 | ||
b07c3f19 | 270 | ret = register_trace_sched_wakeup(probe_wakeup); |
5b82a1b0 | 271 | if (ret) { |
b07c3f19 | 272 | pr_info("wakeup trace: Couldn't activate tracepoint" |
5b82a1b0 MD |
273 | " probe to kernel_sched_wakeup\n"); |
274 | return; | |
275 | } | |
276 | ||
b07c3f19 | 277 | ret = register_trace_sched_wakeup_new(probe_wakeup); |
5b82a1b0 | 278 | if (ret) { |
b07c3f19 | 279 | pr_info("wakeup trace: Couldn't activate tracepoint" |
5b82a1b0 MD |
280 | " probe to kernel_sched_wakeup_new\n"); |
281 | goto fail_deprobe; | |
282 | } | |
283 | ||
b07c3f19 | 284 | ret = register_trace_sched_switch(probe_wakeup_sched_switch); |
5b82a1b0 | 285 | if (ret) { |
b07c3f19 | 286 | pr_info("sched trace: Couldn't activate tracepoint" |
5b82a1b0 MD |
287 | " probe to kernel_sched_schedule\n"); |
288 | goto fail_deprobe_wake_new; | |
289 | } | |
290 | ||
352ad25a SR |
291 | wakeup_reset(tr); |
292 | ||
293 | /* | |
294 | * Don't let the tracer_enabled = 1 show up before | |
295 | * the wakeup_task is reset. This may be overkill since | |
296 | * wakeup_reset does a spin_unlock after setting the | |
297 | * wakeup_task to NULL, but I want to be safe. | |
298 | * This is a slow path anyway. | |
299 | */ | |
300 | smp_wmb(); | |
301 | ||
7e18d8e7 | 302 | register_ftrace_function(&trace_ops); |
352ad25a | 303 | |
5bc4564b | 304 | if (tracing_is_enabled()) |
9036990d | 305 | tracer_enabled = 1; |
5bc4564b | 306 | else |
9036990d | 307 | tracer_enabled = 0; |
ad591240 | 308 | |
352ad25a | 309 | return; |
5b82a1b0 | 310 | fail_deprobe_wake_new: |
b07c3f19 | 311 | unregister_trace_sched_wakeup_new(probe_wakeup); |
5b82a1b0 | 312 | fail_deprobe: |
b07c3f19 | 313 | unregister_trace_sched_wakeup(probe_wakeup); |
352ad25a SR |
314 | } |
315 | ||
e309b41d | 316 | static void stop_wakeup_tracer(struct trace_array *tr) |
352ad25a SR |
317 | { |
318 | tracer_enabled = 0; | |
7e18d8e7 | 319 | unregister_ftrace_function(&trace_ops); |
b07c3f19 MD |
320 | unregister_trace_sched_switch(probe_wakeup_sched_switch); |
321 | unregister_trace_sched_wakeup_new(probe_wakeup); | |
322 | unregister_trace_sched_wakeup(probe_wakeup); | |
352ad25a SR |
323 | } |
324 | ||
3244351c | 325 | static int __wakeup_tracer_init(struct trace_array *tr) |
352ad25a | 326 | { |
745b1626 | 327 | tracing_max_latency = 0; |
352ad25a | 328 | wakeup_trace = tr; |
c76f0694 | 329 | start_wakeup_tracer(tr); |
1c80025a | 330 | return 0; |
352ad25a SR |
331 | } |
332 | ||
3244351c SR |
333 | static int wakeup_tracer_init(struct trace_array *tr) |
334 | { | |
335 | wakeup_rt = 0; | |
336 | return __wakeup_tracer_init(tr); | |
337 | } | |
338 | ||
339 | static int wakeup_rt_tracer_init(struct trace_array *tr) | |
340 | { | |
341 | wakeup_rt = 1; | |
342 | return __wakeup_tracer_init(tr); | |
343 | } | |
344 | ||
e309b41d | 345 | static void wakeup_tracer_reset(struct trace_array *tr) |
352ad25a | 346 | { |
c76f0694 SR |
347 | stop_wakeup_tracer(tr); |
348 | /* make sure we put back any tasks we are tracing */ | |
349 | wakeup_reset(tr); | |
352ad25a SR |
350 | } |
351 | ||
9036990d SR |
352 | static void wakeup_tracer_start(struct trace_array *tr) |
353 | { | |
354 | wakeup_reset(tr); | |
355 | tracer_enabled = 1; | |
9036990d SR |
356 | } |
357 | ||
358 | static void wakeup_tracer_stop(struct trace_array *tr) | |
359 | { | |
360 | tracer_enabled = 0; | |
352ad25a SR |
361 | } |
362 | ||
363 | static struct tracer wakeup_tracer __read_mostly = | |
364 | { | |
365 | .name = "wakeup", | |
366 | .init = wakeup_tracer_init, | |
367 | .reset = wakeup_tracer_reset, | |
9036990d SR |
368 | .start = wakeup_tracer_start, |
369 | .stop = wakeup_tracer_stop, | |
352ad25a | 370 | .print_max = 1, |
60a11774 SR |
371 | #ifdef CONFIG_FTRACE_SELFTEST |
372 | .selftest = trace_selftest_startup_wakeup, | |
373 | #endif | |
352ad25a SR |
374 | }; |
375 | ||
3244351c SR |
376 | static struct tracer wakeup_rt_tracer __read_mostly = |
377 | { | |
378 | .name = "wakeup_rt", | |
379 | .init = wakeup_rt_tracer_init, | |
380 | .reset = wakeup_tracer_reset, | |
381 | .start = wakeup_tracer_start, | |
382 | .stop = wakeup_tracer_stop, | |
383 | .print_max = 1, | |
384 | #ifdef CONFIG_FTRACE_SELFTEST | |
385 | .selftest = trace_selftest_startup_wakeup, | |
386 | #endif | |
387 | }; | |
388 | ||
352ad25a SR |
389 | __init static int init_wakeup_tracer(void) |
390 | { | |
391 | int ret; | |
392 | ||
393 | ret = register_tracer(&wakeup_tracer); | |
394 | if (ret) | |
395 | return ret; | |
396 | ||
3244351c SR |
397 | ret = register_tracer(&wakeup_rt_tracer); |
398 | if (ret) | |
399 | return ret; | |
400 | ||
352ad25a SR |
401 | return 0; |
402 | } | |
403 | device_initcall(init_wakeup_tracer); |