]>
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 | ||
478142c3 SR |
101 | static void probe_wakeup_migrate_task(struct task_struct *task, int cpu) |
102 | { | |
103 | if (task != wakeup_task) | |
104 | return; | |
105 | ||
106 | wakeup_current_cpu = cpu; | |
107 | } | |
108 | ||
5b82a1b0 | 109 | static void notrace |
b07c3f19 | 110 | probe_wakeup_sched_switch(struct rq *rq, struct task_struct *prev, |
5b82a1b0 | 111 | struct task_struct *next) |
352ad25a | 112 | { |
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 | ||
352ad25a SR |
139 | /* disable local data, not wakeup_cpu data */ |
140 | cpu = raw_smp_processor_id(); | |
b07c3f19 | 141 | disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled); |
352ad25a SR |
142 | if (likely(disabled != 1)) |
143 | goto out; | |
144 | ||
e59494f4 SR |
145 | local_irq_save(flags); |
146 | __raw_spin_lock(&wakeup_lock); | |
352ad25a SR |
147 | |
148 | /* We could race with grabbing wakeup_lock */ | |
149 | if (unlikely(!tracer_enabled || next != wakeup_task)) | |
150 | goto out_unlock; | |
151 | ||
9be24414 SR |
152 | /* The task we are waiting for is waking up */ |
153 | data = wakeup_trace->data[wakeup_cpu]; | |
154 | ||
301fd748 | 155 | trace_function(wakeup_trace, CALLER_ADDR0, CALLER_ADDR1, flags, pc); |
7be42151 | 156 | tracing_sched_switch_trace(wakeup_trace, prev, next, flags, pc); |
352ad25a | 157 | |
352ad25a | 158 | T0 = data->preempt_timestamp; |
750ed1a4 | 159 | T1 = ftrace_now(cpu); |
352ad25a SR |
160 | delta = T1-T0; |
161 | ||
162 | if (!report_latency(delta)) | |
163 | goto out_unlock; | |
164 | ||
b5130b1e CE |
165 | if (likely(!is_tracing_stopped())) { |
166 | tracing_max_latency = delta; | |
167 | update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu); | |
168 | } | |
352ad25a | 169 | |
352ad25a | 170 | out_unlock: |
b07c3f19 | 171 | __wakeup_reset(wakeup_trace); |
e59494f4 SR |
172 | __raw_spin_unlock(&wakeup_lock); |
173 | local_irq_restore(flags); | |
352ad25a | 174 | out: |
b07c3f19 | 175 | atomic_dec(&wakeup_trace->data[cpu]->disabled); |
5b82a1b0 MD |
176 | } |
177 | ||
e309b41d | 178 | static void __wakeup_reset(struct trace_array *tr) |
352ad25a | 179 | { |
352ad25a SR |
180 | wakeup_cpu = -1; |
181 | wakeup_prio = -1; | |
182 | ||
183 | if (wakeup_task) | |
184 | put_task_struct(wakeup_task); | |
185 | ||
186 | wakeup_task = NULL; | |
187 | } | |
188 | ||
e309b41d | 189 | static void wakeup_reset(struct trace_array *tr) |
352ad25a SR |
190 | { |
191 | unsigned long flags; | |
192 | ||
2f26ebd5 SR |
193 | tracing_reset_online_cpus(tr); |
194 | ||
e59494f4 SR |
195 | local_irq_save(flags); |
196 | __raw_spin_lock(&wakeup_lock); | |
352ad25a | 197 | __wakeup_reset(tr); |
e59494f4 SR |
198 | __raw_spin_unlock(&wakeup_lock); |
199 | local_irq_restore(flags); | |
352ad25a SR |
200 | } |
201 | ||
e309b41d | 202 | static void |
468a15bb | 203 | probe_wakeup(struct rq *rq, struct task_struct *p, int success) |
352ad25a | 204 | { |
f8ec1062 | 205 | struct trace_array_cpu *data; |
352ad25a SR |
206 | int cpu = smp_processor_id(); |
207 | unsigned long flags; | |
208 | long disabled; | |
38697053 | 209 | int pc; |
352ad25a | 210 | |
b07c3f19 MD |
211 | if (likely(!tracer_enabled)) |
212 | return; | |
213 | ||
214 | tracing_record_cmdline(p); | |
215 | tracing_record_cmdline(current); | |
216 | ||
3244351c | 217 | if ((wakeup_rt && !rt_task(p)) || |
352ad25a | 218 | p->prio >= wakeup_prio || |
b07c3f19 | 219 | p->prio >= current->prio) |
352ad25a SR |
220 | return; |
221 | ||
38697053 | 222 | pc = preempt_count(); |
b07c3f19 | 223 | disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled); |
352ad25a SR |
224 | if (unlikely(disabled != 1)) |
225 | goto out; | |
226 | ||
227 | /* interrupts should be off from try_to_wake_up */ | |
e59494f4 | 228 | __raw_spin_lock(&wakeup_lock); |
352ad25a SR |
229 | |
230 | /* check for races. */ | |
231 | if (!tracer_enabled || p->prio >= wakeup_prio) | |
232 | goto out_locked; | |
233 | ||
234 | /* reset the trace */ | |
b07c3f19 | 235 | __wakeup_reset(wakeup_trace); |
352ad25a SR |
236 | |
237 | wakeup_cpu = task_cpu(p); | |
478142c3 | 238 | wakeup_current_cpu = wakeup_cpu; |
352ad25a SR |
239 | wakeup_prio = p->prio; |
240 | ||
241 | wakeup_task = p; | |
242 | get_task_struct(wakeup_task); | |
243 | ||
244 | local_save_flags(flags); | |
245 | ||
f8ec1062 SR |
246 | data = wakeup_trace->data[wakeup_cpu]; |
247 | data->preempt_timestamp = ftrace_now(cpu); | |
7be42151 | 248 | tracing_sched_wakeup_trace(wakeup_trace, p, current, flags, pc); |
301fd748 SR |
249 | |
250 | /* | |
251 | * We must be careful in using CALLER_ADDR2. But since wake_up | |
252 | * is not called by an assembly function (where as schedule is) | |
253 | * it should be safe to use it here. | |
254 | */ | |
7be42151 | 255 | trace_function(wakeup_trace, CALLER_ADDR1, CALLER_ADDR2, flags, pc); |
352ad25a SR |
256 | |
257 | out_locked: | |
e59494f4 | 258 | __raw_spin_unlock(&wakeup_lock); |
352ad25a | 259 | out: |
b07c3f19 | 260 | atomic_dec(&wakeup_trace->data[cpu]->disabled); |
352ad25a SR |
261 | } |
262 | ||
e309b41d | 263 | static void start_wakeup_tracer(struct trace_array *tr) |
352ad25a | 264 | { |
5b82a1b0 MD |
265 | int ret; |
266 | ||
b07c3f19 | 267 | ret = register_trace_sched_wakeup(probe_wakeup); |
5b82a1b0 | 268 | if (ret) { |
b07c3f19 | 269 | pr_info("wakeup trace: Couldn't activate tracepoint" |
5b82a1b0 MD |
270 | " probe to kernel_sched_wakeup\n"); |
271 | return; | |
272 | } | |
273 | ||
b07c3f19 | 274 | ret = register_trace_sched_wakeup_new(probe_wakeup); |
5b82a1b0 | 275 | if (ret) { |
b07c3f19 | 276 | pr_info("wakeup trace: Couldn't activate tracepoint" |
5b82a1b0 MD |
277 | " probe to kernel_sched_wakeup_new\n"); |
278 | goto fail_deprobe; | |
279 | } | |
280 | ||
b07c3f19 | 281 | ret = register_trace_sched_switch(probe_wakeup_sched_switch); |
5b82a1b0 | 282 | if (ret) { |
b07c3f19 | 283 | pr_info("sched trace: Couldn't activate tracepoint" |
73d8b8bc | 284 | " probe to kernel_sched_switch\n"); |
5b82a1b0 MD |
285 | goto fail_deprobe_wake_new; |
286 | } | |
287 | ||
478142c3 SR |
288 | ret = register_trace_sched_migrate_task(probe_wakeup_migrate_task); |
289 | if (ret) { | |
290 | pr_info("wakeup trace: Couldn't activate tracepoint" | |
291 | " probe to kernel_sched_migrate_task\n"); | |
292 | return; | |
293 | } | |
294 | ||
352ad25a SR |
295 | wakeup_reset(tr); |
296 | ||
297 | /* | |
298 | * Don't let the tracer_enabled = 1 show up before | |
299 | * the wakeup_task is reset. This may be overkill since | |
300 | * wakeup_reset does a spin_unlock after setting the | |
301 | * wakeup_task to NULL, but I want to be safe. | |
302 | * This is a slow path anyway. | |
303 | */ | |
304 | smp_wmb(); | |
305 | ||
7e18d8e7 | 306 | register_ftrace_function(&trace_ops); |
352ad25a | 307 | |
5bc4564b | 308 | if (tracing_is_enabled()) |
9036990d | 309 | tracer_enabled = 1; |
5bc4564b | 310 | else |
9036990d | 311 | tracer_enabled = 0; |
ad591240 | 312 | |
352ad25a | 313 | return; |
5b82a1b0 | 314 | fail_deprobe_wake_new: |
b07c3f19 | 315 | unregister_trace_sched_wakeup_new(probe_wakeup); |
5b82a1b0 | 316 | fail_deprobe: |
b07c3f19 | 317 | unregister_trace_sched_wakeup(probe_wakeup); |
352ad25a SR |
318 | } |
319 | ||
e309b41d | 320 | static void stop_wakeup_tracer(struct trace_array *tr) |
352ad25a SR |
321 | { |
322 | tracer_enabled = 0; | |
7e18d8e7 | 323 | unregister_ftrace_function(&trace_ops); |
b07c3f19 MD |
324 | unregister_trace_sched_switch(probe_wakeup_sched_switch); |
325 | unregister_trace_sched_wakeup_new(probe_wakeup); | |
326 | unregister_trace_sched_wakeup(probe_wakeup); | |
478142c3 | 327 | unregister_trace_sched_migrate_task(probe_wakeup_migrate_task); |
352ad25a SR |
328 | } |
329 | ||
3244351c | 330 | static int __wakeup_tracer_init(struct trace_array *tr) |
352ad25a | 331 | { |
e9d25fe6 SR |
332 | save_lat_flag = trace_flags & TRACE_ITER_LATENCY_FMT; |
333 | trace_flags |= TRACE_ITER_LATENCY_FMT; | |
334 | ||
745b1626 | 335 | tracing_max_latency = 0; |
352ad25a | 336 | wakeup_trace = tr; |
c76f0694 | 337 | start_wakeup_tracer(tr); |
1c80025a | 338 | return 0; |
352ad25a SR |
339 | } |
340 | ||
3244351c SR |
341 | static int wakeup_tracer_init(struct trace_array *tr) |
342 | { | |
343 | wakeup_rt = 0; | |
344 | return __wakeup_tracer_init(tr); | |
345 | } | |
346 | ||
347 | static int wakeup_rt_tracer_init(struct trace_array *tr) | |
348 | { | |
349 | wakeup_rt = 1; | |
350 | return __wakeup_tracer_init(tr); | |
351 | } | |
352 | ||
e309b41d | 353 | static void wakeup_tracer_reset(struct trace_array *tr) |
352ad25a | 354 | { |
c76f0694 SR |
355 | stop_wakeup_tracer(tr); |
356 | /* make sure we put back any tasks we are tracing */ | |
357 | wakeup_reset(tr); | |
e9d25fe6 SR |
358 | |
359 | if (!save_lat_flag) | |
360 | trace_flags &= ~TRACE_ITER_LATENCY_FMT; | |
352ad25a SR |
361 | } |
362 | ||
9036990d SR |
363 | static void wakeup_tracer_start(struct trace_array *tr) |
364 | { | |
365 | wakeup_reset(tr); | |
366 | tracer_enabled = 1; | |
9036990d SR |
367 | } |
368 | ||
369 | static void wakeup_tracer_stop(struct trace_array *tr) | |
370 | { | |
371 | tracer_enabled = 0; | |
352ad25a SR |
372 | } |
373 | ||
374 | static struct tracer wakeup_tracer __read_mostly = | |
375 | { | |
376 | .name = "wakeup", | |
377 | .init = wakeup_tracer_init, | |
378 | .reset = wakeup_tracer_reset, | |
9036990d SR |
379 | .start = wakeup_tracer_start, |
380 | .stop = wakeup_tracer_stop, | |
352ad25a | 381 | .print_max = 1, |
60a11774 SR |
382 | #ifdef CONFIG_FTRACE_SELFTEST |
383 | .selftest = trace_selftest_startup_wakeup, | |
384 | #endif | |
352ad25a SR |
385 | }; |
386 | ||
3244351c SR |
387 | static struct tracer wakeup_rt_tracer __read_mostly = |
388 | { | |
389 | .name = "wakeup_rt", | |
390 | .init = wakeup_rt_tracer_init, | |
391 | .reset = wakeup_tracer_reset, | |
392 | .start = wakeup_tracer_start, | |
393 | .stop = wakeup_tracer_stop, | |
6eaaa5d5 | 394 | .wait_pipe = poll_wait_pipe, |
3244351c SR |
395 | .print_max = 1, |
396 | #ifdef CONFIG_FTRACE_SELFTEST | |
397 | .selftest = trace_selftest_startup_wakeup, | |
398 | #endif | |
399 | }; | |
400 | ||
352ad25a SR |
401 | __init static int init_wakeup_tracer(void) |
402 | { | |
403 | int ret; | |
404 | ||
405 | ret = register_tracer(&wakeup_tracer); | |
406 | if (ret) | |
407 | return ret; | |
408 | ||
3244351c SR |
409 | ret = register_tracer(&wakeup_rt_tracer); |
410 | if (ret) | |
411 | return ret; | |
412 | ||
352ad25a SR |
413 | return 0; |
414 | } | |
415 | device_initcall(init_wakeup_tracer); |