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