2 * trace task wakeup timings
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Based on code from the latency_tracer, that is:
9 * Copyright (C) 2004-2006 Ingo Molnar
10 * Copyright (C) 2004 William Lee Irwin III
12 #include <linux/module.h>
14 #include <linux/debugfs.h>
15 #include <linux/kallsyms.h>
16 #include <linux/uaccess.h>
17 #include <linux/ftrace.h>
18 #include <trace/events/sched.h>
22 static struct trace_array *wakeup_trace;
23 static int __read_mostly tracer_enabled;
25 static struct task_struct *wakeup_task;
26 static int wakeup_cpu;
27 static int wakeup_current_cpu;
28 static unsigned wakeup_prio = -1;
31 static arch_spinlock_t wakeup_lock =
32 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
34 static void __wakeup_reset(struct trace_array *tr);
36 static int save_lat_flag;
38 #ifdef CONFIG_FUNCTION_TRACER
40 * irqsoff uses its own tracer function to keep the overhead down:
43 wakeup_tracer_call(unsigned long ip, unsigned long parent_ip)
45 struct trace_array *tr = wakeup_trace;
46 struct trace_array_cpu *data;
53 if (likely(!wakeup_task))
57 resched = ftrace_preempt_disable();
59 cpu = raw_smp_processor_id();
60 if (cpu != wakeup_current_cpu)
64 disabled = atomic_inc_return(&data->disabled);
65 if (unlikely(disabled != 1))
68 local_irq_save(flags);
70 trace_function(tr, ip, parent_ip, flags, pc);
72 local_irq_restore(flags);
75 atomic_dec(&data->disabled);
77 ftrace_preempt_enable(resched);
80 static struct ftrace_ops trace_ops __read_mostly =
82 .func = wakeup_tracer_call,
84 #endif /* CONFIG_FUNCTION_TRACER */
87 * Should this new latency be reported/recorded?
89 static int report_latency(cycle_t delta)
92 if (delta < tracing_thresh)
95 if (delta <= tracing_max_latency)
101 static void probe_wakeup_migrate_task(struct task_struct *task, int cpu)
103 if (task != wakeup_task)
106 wakeup_current_cpu = cpu;
110 probe_wakeup_sched_switch(struct task_struct *prev, struct task_struct *next)
112 struct trace_array_cpu *data;
113 cycle_t T0, T1, delta;
119 tracing_record_cmdline(prev);
121 if (unlikely(!tracer_enabled))
125 * When we start a new trace, we set wakeup_task to NULL
126 * and then set tracer_enabled = 1. We want to make sure
127 * that another CPU does not see the tracer_enabled = 1
128 * and the wakeup_task with an older task, that might
129 * actually be the same as next.
133 if (next != wakeup_task)
136 pc = preempt_count();
138 /* disable local data, not wakeup_cpu data */
139 cpu = raw_smp_processor_id();
140 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
141 if (likely(disabled != 1))
144 local_irq_save(flags);
145 arch_spin_lock(&wakeup_lock);
147 /* We could race with grabbing wakeup_lock */
148 if (unlikely(!tracer_enabled || next != wakeup_task))
151 /* The task we are waiting for is waking up */
152 data = wakeup_trace->data[wakeup_cpu];
154 trace_function(wakeup_trace, CALLER_ADDR0, CALLER_ADDR1, flags, pc);
155 tracing_sched_switch_trace(wakeup_trace, prev, next, flags, pc);
157 T0 = data->preempt_timestamp;
158 T1 = ftrace_now(cpu);
161 if (!report_latency(delta))
164 if (likely(!is_tracing_stopped())) {
165 tracing_max_latency = delta;
166 update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu);
170 __wakeup_reset(wakeup_trace);
171 arch_spin_unlock(&wakeup_lock);
172 local_irq_restore(flags);
174 atomic_dec(&wakeup_trace->data[cpu]->disabled);
177 static void __wakeup_reset(struct trace_array *tr)
183 put_task_struct(wakeup_task);
188 static void wakeup_reset(struct trace_array *tr)
192 tracing_reset_online_cpus(tr);
194 local_irq_save(flags);
195 arch_spin_lock(&wakeup_lock);
197 arch_spin_unlock(&wakeup_lock);
198 local_irq_restore(flags);
202 probe_wakeup(struct task_struct *p, int success)
204 struct trace_array_cpu *data;
205 int cpu = smp_processor_id();
210 if (likely(!tracer_enabled))
213 tracing_record_cmdline(p);
214 tracing_record_cmdline(current);
216 if ((wakeup_rt && !rt_task(p)) ||
217 p->prio >= wakeup_prio ||
218 p->prio >= current->prio)
221 pc = preempt_count();
222 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
223 if (unlikely(disabled != 1))
226 /* interrupts should be off from try_to_wake_up */
227 arch_spin_lock(&wakeup_lock);
229 /* check for races. */
230 if (!tracer_enabled || p->prio >= wakeup_prio)
233 /* reset the trace */
234 __wakeup_reset(wakeup_trace);
236 wakeup_cpu = task_cpu(p);
237 wakeup_current_cpu = wakeup_cpu;
238 wakeup_prio = p->prio;
241 get_task_struct(wakeup_task);
243 local_save_flags(flags);
245 data = wakeup_trace->data[wakeup_cpu];
246 data->preempt_timestamp = ftrace_now(cpu);
247 tracing_sched_wakeup_trace(wakeup_trace, p, current, flags, pc);
250 * We must be careful in using CALLER_ADDR2. But since wake_up
251 * is not called by an assembly function (where as schedule is)
252 * it should be safe to use it here.
254 trace_function(wakeup_trace, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
257 arch_spin_unlock(&wakeup_lock);
259 atomic_dec(&wakeup_trace->data[cpu]->disabled);
262 static void start_wakeup_tracer(struct trace_array *tr)
266 ret = register_trace_sched_wakeup(probe_wakeup);
268 pr_info("wakeup trace: Couldn't activate tracepoint"
269 " probe to kernel_sched_wakeup\n");
273 ret = register_trace_sched_wakeup_new(probe_wakeup);
275 pr_info("wakeup trace: Couldn't activate tracepoint"
276 " probe to kernel_sched_wakeup_new\n");
280 ret = register_trace_sched_switch(probe_wakeup_sched_switch);
282 pr_info("sched trace: Couldn't activate tracepoint"
283 " probe to kernel_sched_switch\n");
284 goto fail_deprobe_wake_new;
287 ret = register_trace_sched_migrate_task(probe_wakeup_migrate_task);
289 pr_info("wakeup trace: Couldn't activate tracepoint"
290 " probe to kernel_sched_migrate_task\n");
297 * Don't let the tracer_enabled = 1 show up before
298 * the wakeup_task is reset. This may be overkill since
299 * wakeup_reset does a spin_unlock after setting the
300 * wakeup_task to NULL, but I want to be safe.
301 * This is a slow path anyway.
305 register_ftrace_function(&trace_ops);
307 if (tracing_is_enabled())
313 fail_deprobe_wake_new:
314 unregister_trace_sched_wakeup_new(probe_wakeup);
316 unregister_trace_sched_wakeup(probe_wakeup);
319 static void stop_wakeup_tracer(struct trace_array *tr)
322 unregister_ftrace_function(&trace_ops);
323 unregister_trace_sched_switch(probe_wakeup_sched_switch);
324 unregister_trace_sched_wakeup_new(probe_wakeup);
325 unregister_trace_sched_wakeup(probe_wakeup);
326 unregister_trace_sched_migrate_task(probe_wakeup_migrate_task);
329 static int __wakeup_tracer_init(struct trace_array *tr)
331 save_lat_flag = trace_flags & TRACE_ITER_LATENCY_FMT;
332 trace_flags |= TRACE_ITER_LATENCY_FMT;
334 tracing_max_latency = 0;
336 start_wakeup_tracer(tr);
340 static int wakeup_tracer_init(struct trace_array *tr)
343 return __wakeup_tracer_init(tr);
346 static int wakeup_rt_tracer_init(struct trace_array *tr)
349 return __wakeup_tracer_init(tr);
352 static void wakeup_tracer_reset(struct trace_array *tr)
354 stop_wakeup_tracer(tr);
355 /* make sure we put back any tasks we are tracing */
359 trace_flags &= ~TRACE_ITER_LATENCY_FMT;
362 static void wakeup_tracer_start(struct trace_array *tr)
368 static void wakeup_tracer_stop(struct trace_array *tr)
373 static struct tracer wakeup_tracer __read_mostly =
376 .init = wakeup_tracer_init,
377 .reset = wakeup_tracer_reset,
378 .start = wakeup_tracer_start,
379 .stop = wakeup_tracer_stop,
381 #ifdef CONFIG_FTRACE_SELFTEST
382 .selftest = trace_selftest_startup_wakeup,
386 static struct tracer wakeup_rt_tracer __read_mostly =
389 .init = wakeup_rt_tracer_init,
390 .reset = wakeup_tracer_reset,
391 .start = wakeup_tracer_start,
392 .stop = wakeup_tracer_stop,
393 .wait_pipe = poll_wait_pipe,
395 #ifdef CONFIG_FTRACE_SELFTEST
396 .selftest = trace_selftest_startup_wakeup,
400 __init static int init_wakeup_tracer(void)
404 ret = register_tracer(&wakeup_tracer);
408 ret = register_tracer(&wakeup_rt_tracer);
414 device_initcall(init_wakeup_tracer);