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 static int save_lat_flag;
37 #ifdef CONFIG_FUNCTION_TRACER
39 * irqsoff uses its own tracer function to keep the overhead down:
42 wakeup_tracer_call(unsigned long ip, unsigned long parent_ip)
44 struct trace_array *tr = wakeup_trace;
45 struct trace_array_cpu *data;
52 if (likely(!wakeup_task))
56 resched = ftrace_preempt_disable();
58 cpu = raw_smp_processor_id();
60 disabled = atomic_inc_return(&data->disabled);
61 if (unlikely(disabled != 1))
64 local_irq_save(flags);
65 __raw_spin_lock(&wakeup_lock);
67 if (unlikely(!wakeup_task))
71 * The task can't disappear because it needs to
72 * wake up first, and we have the wakeup_lock.
74 if (task_cpu(wakeup_task) != cpu)
77 trace_function(tr, ip, parent_ip, flags, pc);
80 __raw_spin_unlock(&wakeup_lock);
81 local_irq_restore(flags);
84 atomic_dec(&data->disabled);
86 ftrace_preempt_enable(resched);
89 static struct ftrace_ops trace_ops __read_mostly =
91 .func = wakeup_tracer_call,
93 #endif /* CONFIG_FUNCTION_TRACER */
96 * Should this new latency be reported/recorded?
98 static int report_latency(cycle_t delta)
100 if (tracing_thresh) {
101 if (delta < tracing_thresh)
104 if (delta <= tracing_max_latency)
111 probe_wakeup_sched_switch(struct rq *rq, struct task_struct *prev,
112 struct task_struct *next)
114 unsigned long latency = 0, t0 = 0, t1 = 0;
115 struct trace_array_cpu *data;
116 cycle_t T0, T1, delta;
122 tracing_record_cmdline(prev);
124 if (unlikely(!tracer_enabled))
128 * When we start a new trace, we set wakeup_task to NULL
129 * and then set tracer_enabled = 1. We want to make sure
130 * that another CPU does not see the tracer_enabled = 1
131 * and the wakeup_task with an older task, that might
132 * actually be the same as next.
136 if (next != wakeup_task)
139 pc = preempt_count();
141 /* The task we are waiting for is waking up */
142 data = wakeup_trace->data[wakeup_cpu];
144 /* disable local data, not wakeup_cpu data */
145 cpu = raw_smp_processor_id();
146 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
147 if (likely(disabled != 1))
150 local_irq_save(flags);
151 __raw_spin_lock(&wakeup_lock);
153 /* We could race with grabbing wakeup_lock */
154 if (unlikely(!tracer_enabled || next != wakeup_task))
157 trace_function(wakeup_trace, CALLER_ADDR0, CALLER_ADDR1, flags, pc);
158 tracing_sched_switch_trace(wakeup_trace, prev, next, flags, pc);
161 * usecs conversion is slow so we try to delay the conversion
162 * as long as possible:
164 T0 = data->preempt_timestamp;
165 T1 = ftrace_now(cpu);
168 if (!report_latency(delta))
171 latency = nsecs_to_usecs(delta);
173 tracing_max_latency = delta;
174 t0 = nsecs_to_usecs(T0);
175 t1 = nsecs_to_usecs(T1);
177 update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu);
180 __wakeup_reset(wakeup_trace);
181 __raw_spin_unlock(&wakeup_lock);
182 local_irq_restore(flags);
184 atomic_dec(&wakeup_trace->data[cpu]->disabled);
187 static void __wakeup_reset(struct trace_array *tr)
191 for_each_possible_cpu(cpu)
192 tracing_reset(tr, cpu);
198 put_task_struct(wakeup_task);
203 static void wakeup_reset(struct trace_array *tr)
207 local_irq_save(flags);
208 __raw_spin_lock(&wakeup_lock);
210 __raw_spin_unlock(&wakeup_lock);
211 local_irq_restore(flags);
215 probe_wakeup(struct rq *rq, struct task_struct *p, int success)
217 struct trace_array_cpu *data;
218 int cpu = smp_processor_id();
223 if (likely(!tracer_enabled))
226 tracing_record_cmdline(p);
227 tracing_record_cmdline(current);
229 if ((wakeup_rt && !rt_task(p)) ||
230 p->prio >= wakeup_prio ||
231 p->prio >= current->prio)
234 pc = preempt_count();
235 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
236 if (unlikely(disabled != 1))
239 /* interrupts should be off from try_to_wake_up */
240 __raw_spin_lock(&wakeup_lock);
242 /* check for races. */
243 if (!tracer_enabled || p->prio >= wakeup_prio)
246 /* reset the trace */
247 __wakeup_reset(wakeup_trace);
249 wakeup_cpu = task_cpu(p);
250 wakeup_prio = p->prio;
253 get_task_struct(wakeup_task);
255 local_save_flags(flags);
257 data = wakeup_trace->data[wakeup_cpu];
258 data->preempt_timestamp = ftrace_now(cpu);
259 tracing_sched_wakeup_trace(wakeup_trace, p, current, flags, pc);
262 * We must be careful in using CALLER_ADDR2. But since wake_up
263 * is not called by an assembly function (where as schedule is)
264 * it should be safe to use it here.
266 trace_function(wakeup_trace, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
269 __raw_spin_unlock(&wakeup_lock);
271 atomic_dec(&wakeup_trace->data[cpu]->disabled);
274 static void start_wakeup_tracer(struct trace_array *tr)
278 ret = register_trace_sched_wakeup(probe_wakeup);
280 pr_info("wakeup trace: Couldn't activate tracepoint"
281 " probe to kernel_sched_wakeup\n");
285 ret = register_trace_sched_wakeup_new(probe_wakeup);
287 pr_info("wakeup trace: Couldn't activate tracepoint"
288 " probe to kernel_sched_wakeup_new\n");
292 ret = register_trace_sched_switch(probe_wakeup_sched_switch);
294 pr_info("sched trace: Couldn't activate tracepoint"
295 " probe to kernel_sched_switch\n");
296 goto fail_deprobe_wake_new;
302 * Don't let the tracer_enabled = 1 show up before
303 * the wakeup_task is reset. This may be overkill since
304 * wakeup_reset does a spin_unlock after setting the
305 * wakeup_task to NULL, but I want to be safe.
306 * This is a slow path anyway.
310 register_ftrace_function(&trace_ops);
312 if (tracing_is_enabled())
318 fail_deprobe_wake_new:
319 unregister_trace_sched_wakeup_new(probe_wakeup);
321 unregister_trace_sched_wakeup(probe_wakeup);
324 static void stop_wakeup_tracer(struct trace_array *tr)
327 unregister_ftrace_function(&trace_ops);
328 unregister_trace_sched_switch(probe_wakeup_sched_switch);
329 unregister_trace_sched_wakeup_new(probe_wakeup);
330 unregister_trace_sched_wakeup(probe_wakeup);
333 static int __wakeup_tracer_init(struct trace_array *tr)
335 save_lat_flag = trace_flags & TRACE_ITER_LATENCY_FMT;
336 trace_flags |= TRACE_ITER_LATENCY_FMT;
338 tracing_max_latency = 0;
340 start_wakeup_tracer(tr);
344 static int wakeup_tracer_init(struct trace_array *tr)
347 return __wakeup_tracer_init(tr);
350 static int wakeup_rt_tracer_init(struct trace_array *tr)
353 return __wakeup_tracer_init(tr);
356 static void wakeup_tracer_reset(struct trace_array *tr)
358 stop_wakeup_tracer(tr);
359 /* make sure we put back any tasks we are tracing */
363 trace_flags &= ~TRACE_ITER_LATENCY_FMT;
366 static void wakeup_tracer_start(struct trace_array *tr)
372 static void wakeup_tracer_stop(struct trace_array *tr)
377 static struct tracer wakeup_tracer __read_mostly =
380 .init = wakeup_tracer_init,
381 .reset = wakeup_tracer_reset,
382 .start = wakeup_tracer_start,
383 .stop = wakeup_tracer_stop,
385 #ifdef CONFIG_FTRACE_SELFTEST
386 .selftest = trace_selftest_startup_wakeup,
390 static struct tracer wakeup_rt_tracer __read_mostly =
393 .init = wakeup_rt_tracer_init,
394 .reset = wakeup_tracer_reset,
395 .start = wakeup_tracer_start,
396 .stop = wakeup_tracer_stop,
397 .wait_pipe = poll_wait_pipe,
399 #ifdef CONFIG_FTRACE_SELFTEST
400 .selftest = trace_selftest_startup_wakeup,
404 __init static int init_wakeup_tracer(void)
408 ret = register_tracer(&wakeup_tracer);
412 ret = register_tracer(&wakeup_rt_tracer);
418 device_initcall(init_wakeup_tracer);