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/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 unsigned wakeup_prio = -1;
30 static raw_spinlock_t wakeup_lock =
31 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
33 static void __wakeup_reset(struct trace_array *tr);
35 #ifdef CONFIG_FUNCTION_TRACER
37 * irqsoff uses its own tracer function to keep the overhead down:
40 wakeup_tracer_call(unsigned long ip, unsigned long parent_ip)
42 struct trace_array *tr = wakeup_trace;
43 struct trace_array_cpu *data;
50 if (likely(!wakeup_task))
54 resched = ftrace_preempt_disable();
56 cpu = raw_smp_processor_id();
58 disabled = atomic_inc_return(&data->disabled);
59 if (unlikely(disabled != 1))
62 local_irq_save(flags);
63 __raw_spin_lock(&wakeup_lock);
65 if (unlikely(!wakeup_task))
69 * The task can't disappear because it needs to
70 * wake up first, and we have the wakeup_lock.
72 if (task_cpu(wakeup_task) != cpu)
75 trace_function(tr, ip, parent_ip, flags, pc);
78 __raw_spin_unlock(&wakeup_lock);
79 local_irq_restore(flags);
82 atomic_dec(&data->disabled);
84 ftrace_preempt_enable(resched);
87 static struct ftrace_ops trace_ops __read_mostly =
89 .func = wakeup_tracer_call,
91 #endif /* CONFIG_FUNCTION_TRACER */
94 * Should this new latency be reported/recorded?
96 static int report_latency(cycle_t delta)
99 if (delta < tracing_thresh)
102 if (delta <= tracing_max_latency)
109 probe_wakeup_sched_switch(struct rq *rq, struct task_struct *prev,
110 struct task_struct *next)
112 unsigned long latency = 0, t0 = 0, t1 = 0;
113 struct trace_array_cpu *data;
114 cycle_t T0, T1, delta;
120 tracing_record_cmdline(prev);
122 if (unlikely(!tracer_enabled))
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.
134 if (next != wakeup_task)
137 pc = preempt_count();
139 /* The task we are waiting for is waking up */
140 data = wakeup_trace->data[wakeup_cpu];
142 /* disable local data, not wakeup_cpu data */
143 cpu = raw_smp_processor_id();
144 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
145 if (likely(disabled != 1))
148 local_irq_save(flags);
149 __raw_spin_lock(&wakeup_lock);
151 /* We could race with grabbing wakeup_lock */
152 if (unlikely(!tracer_enabled || next != wakeup_task))
155 trace_function(wakeup_trace, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
156 tracing_sched_switch_trace(wakeup_trace, prev, next, flags, pc);
159 * usecs conversion is slow so we try to delay the conversion
160 * as long as possible:
162 T0 = data->preempt_timestamp;
163 T1 = ftrace_now(cpu);
166 if (!report_latency(delta))
169 latency = nsecs_to_usecs(delta);
171 tracing_max_latency = delta;
172 t0 = nsecs_to_usecs(T0);
173 t1 = nsecs_to_usecs(T1);
175 update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu);
178 __wakeup_reset(wakeup_trace);
179 __raw_spin_unlock(&wakeup_lock);
180 local_irq_restore(flags);
182 atomic_dec(&wakeup_trace->data[cpu]->disabled);
185 static void __wakeup_reset(struct trace_array *tr)
189 for_each_possible_cpu(cpu)
190 tracing_reset(tr, cpu);
196 put_task_struct(wakeup_task);
201 static void wakeup_reset(struct trace_array *tr)
205 local_irq_save(flags);
206 __raw_spin_lock(&wakeup_lock);
208 __raw_spin_unlock(&wakeup_lock);
209 local_irq_restore(flags);
213 probe_wakeup(struct rq *rq, struct task_struct *p, int success)
215 struct trace_array_cpu *data;
216 int cpu = smp_processor_id();
221 if (likely(!tracer_enabled))
224 tracing_record_cmdline(p);
225 tracing_record_cmdline(current);
227 if ((wakeup_rt && !rt_task(p)) ||
228 p->prio >= wakeup_prio ||
229 p->prio >= current->prio)
232 pc = preempt_count();
233 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
234 if (unlikely(disabled != 1))
237 /* interrupts should be off from try_to_wake_up */
238 __raw_spin_lock(&wakeup_lock);
240 /* check for races. */
241 if (!tracer_enabled || p->prio >= wakeup_prio)
244 /* reset the trace */
245 __wakeup_reset(wakeup_trace);
247 wakeup_cpu = task_cpu(p);
248 wakeup_prio = p->prio;
251 get_task_struct(wakeup_task);
253 local_save_flags(flags);
255 data = wakeup_trace->data[wakeup_cpu];
256 data->preempt_timestamp = ftrace_now(cpu);
257 tracing_sched_wakeup_trace(wakeup_trace, p, current, flags, pc);
258 trace_function(wakeup_trace, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
261 __raw_spin_unlock(&wakeup_lock);
263 atomic_dec(&wakeup_trace->data[cpu]->disabled);
266 static void start_wakeup_tracer(struct trace_array *tr)
270 ret = register_trace_sched_wakeup(probe_wakeup);
272 pr_info("wakeup trace: Couldn't activate tracepoint"
273 " probe to kernel_sched_wakeup\n");
277 ret = register_trace_sched_wakeup_new(probe_wakeup);
279 pr_info("wakeup trace: Couldn't activate tracepoint"
280 " probe to kernel_sched_wakeup_new\n");
284 ret = register_trace_sched_switch(probe_wakeup_sched_switch);
286 pr_info("sched trace: Couldn't activate tracepoint"
287 " probe to kernel_sched_switch\n");
288 goto fail_deprobe_wake_new;
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.
302 register_ftrace_function(&trace_ops);
304 if (tracing_is_enabled())
310 fail_deprobe_wake_new:
311 unregister_trace_sched_wakeup_new(probe_wakeup);
313 unregister_trace_sched_wakeup(probe_wakeup);
316 static void stop_wakeup_tracer(struct trace_array *tr)
319 unregister_ftrace_function(&trace_ops);
320 unregister_trace_sched_switch(probe_wakeup_sched_switch);
321 unregister_trace_sched_wakeup_new(probe_wakeup);
322 unregister_trace_sched_wakeup(probe_wakeup);
325 static int __wakeup_tracer_init(struct trace_array *tr)
327 tracing_max_latency = 0;
329 start_wakeup_tracer(tr);
333 static int wakeup_tracer_init(struct trace_array *tr)
336 return __wakeup_tracer_init(tr);
339 static int wakeup_rt_tracer_init(struct trace_array *tr)
342 return __wakeup_tracer_init(tr);
345 static void wakeup_tracer_reset(struct trace_array *tr)
347 stop_wakeup_tracer(tr);
348 /* make sure we put back any tasks we are tracing */
352 static void wakeup_tracer_start(struct trace_array *tr)
358 static void wakeup_tracer_stop(struct trace_array *tr)
363 static struct tracer wakeup_tracer __read_mostly =
366 .init = wakeup_tracer_init,
367 .reset = wakeup_tracer_reset,
368 .start = wakeup_tracer_start,
369 .stop = wakeup_tracer_stop,
371 #ifdef CONFIG_FTRACE_SELFTEST
372 .selftest = trace_selftest_startup_wakeup,
376 static struct tracer wakeup_rt_tracer __read_mostly =
379 .init = wakeup_rt_tracer_init,
380 .reset = wakeup_tracer_reset,
381 .start = wakeup_tracer_start,
382 .stop = wakeup_tracer_stop,
383 .wait_pipe = poll_wait_pipe,
385 #ifdef CONFIG_FTRACE_SELFTEST
386 .selftest = trace_selftest_startup_wakeup,
390 __init static int init_wakeup_tracer(void)
394 ret = register_tracer(&wakeup_tracer);
398 ret = register_tracer(&wakeup_rt_tracer);
404 device_initcall(init_wakeup_tracer);