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;
29 static raw_spinlock_t wakeup_lock =
30 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
32 static void __wakeup_reset(struct trace_array *tr);
34 #ifdef CONFIG_FUNCTION_TRACER
36 * irqsoff uses its own tracer function to keep the overhead down:
39 wakeup_tracer_call(unsigned long ip, unsigned long parent_ip)
41 struct trace_array *tr = wakeup_trace;
42 struct trace_array_cpu *data;
49 if (likely(!wakeup_task))
53 resched = need_resched();
54 preempt_disable_notrace();
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, data, ip, parent_ip, flags, pc);
78 __raw_spin_unlock(&wakeup_lock);
79 local_irq_restore(flags);
82 atomic_dec(&data->disabled);
85 * To prevent recursion from the scheduler, if the
86 * resched flag was set before we entered, then
90 preempt_enable_no_resched_notrace();
92 preempt_enable_notrace();
95 static struct ftrace_ops trace_ops __read_mostly =
97 .func = wakeup_tracer_call,
99 #endif /* CONFIG_FUNCTION_TRACER */
102 * Should this new latency be reported/recorded?
104 static int report_latency(cycle_t delta)
106 if (tracing_thresh) {
107 if (delta < tracing_thresh)
110 if (delta <= tracing_max_latency)
117 probe_wakeup_sched_switch(struct rq *rq, struct task_struct *prev,
118 struct task_struct *next)
120 unsigned long latency = 0, t0 = 0, t1 = 0;
121 struct trace_array_cpu *data;
122 cycle_t T0, T1, delta;
128 tracing_record_cmdline(prev);
130 if (unlikely(!tracer_enabled))
134 * When we start a new trace, we set wakeup_task to NULL
135 * and then set tracer_enabled = 1. We want to make sure
136 * that another CPU does not see the tracer_enabled = 1
137 * and the wakeup_task with an older task, that might
138 * actually be the same as next.
142 if (next != wakeup_task)
145 pc = preempt_count();
147 /* The task we are waiting for is waking up */
148 data = wakeup_trace->data[wakeup_cpu];
150 /* disable local data, not wakeup_cpu data */
151 cpu = raw_smp_processor_id();
152 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
153 if (likely(disabled != 1))
156 local_irq_save(flags);
157 __raw_spin_lock(&wakeup_lock);
159 /* We could race with grabbing wakeup_lock */
160 if (unlikely(!tracer_enabled || next != wakeup_task))
163 trace_function(wakeup_trace, data, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
166 * usecs conversion is slow so we try to delay the conversion
167 * as long as possible:
169 T0 = data->preempt_timestamp;
170 T1 = ftrace_now(cpu);
173 if (!report_latency(delta))
176 latency = nsecs_to_usecs(delta);
178 tracing_max_latency = delta;
179 t0 = nsecs_to_usecs(T0);
180 t1 = nsecs_to_usecs(T1);
182 update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu);
185 __wakeup_reset(wakeup_trace);
186 __raw_spin_unlock(&wakeup_lock);
187 local_irq_restore(flags);
189 atomic_dec(&wakeup_trace->data[cpu]->disabled);
192 static void __wakeup_reset(struct trace_array *tr)
194 struct trace_array_cpu *data;
197 for_each_possible_cpu(cpu) {
198 data = tr->data[cpu];
199 tracing_reset(tr, cpu);
206 put_task_struct(wakeup_task);
211 static void wakeup_reset(struct trace_array *tr)
215 local_irq_save(flags);
216 __raw_spin_lock(&wakeup_lock);
218 __raw_spin_unlock(&wakeup_lock);
219 local_irq_restore(flags);
223 probe_wakeup(struct rq *rq, struct task_struct *p)
225 int cpu = smp_processor_id();
230 if (likely(!tracer_enabled))
233 tracing_record_cmdline(p);
234 tracing_record_cmdline(current);
236 if (likely(!rt_task(p)) ||
237 p->prio >= wakeup_prio ||
238 p->prio >= current->prio)
241 pc = preempt_count();
242 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
243 if (unlikely(disabled != 1))
246 /* interrupts should be off from try_to_wake_up */
247 __raw_spin_lock(&wakeup_lock);
249 /* check for races. */
250 if (!tracer_enabled || p->prio >= wakeup_prio)
253 /* reset the trace */
254 __wakeup_reset(wakeup_trace);
256 wakeup_cpu = task_cpu(p);
257 wakeup_prio = p->prio;
260 get_task_struct(wakeup_task);
262 local_save_flags(flags);
264 wakeup_trace->data[wakeup_cpu]->preempt_timestamp = ftrace_now(cpu);
265 trace_function(wakeup_trace, wakeup_trace->data[wakeup_cpu],
266 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_schedule\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);
315 fail_deprobe_wake_new:
316 unregister_trace_sched_wakeup_new(probe_wakeup);
318 unregister_trace_sched_wakeup(probe_wakeup);
321 static void stop_wakeup_tracer(struct trace_array *tr)
324 unregister_ftrace_function(&trace_ops);
325 unregister_trace_sched_switch(probe_wakeup_sched_switch);
326 unregister_trace_sched_wakeup_new(probe_wakeup);
327 unregister_trace_sched_wakeup(probe_wakeup);
330 static void wakeup_tracer_init(struct trace_array *tr)
335 start_wakeup_tracer(tr);
338 static void wakeup_tracer_reset(struct trace_array *tr)
341 stop_wakeup_tracer(tr);
342 /* make sure we put back any tasks we are tracing */
347 static void wakeup_tracer_ctrl_update(struct trace_array *tr)
350 start_wakeup_tracer(tr);
352 stop_wakeup_tracer(tr);
355 static void wakeup_tracer_open(struct trace_iterator *iter)
357 /* stop the trace while dumping */
359 stop_wakeup_tracer(iter->tr);
362 static void wakeup_tracer_close(struct trace_iterator *iter)
364 /* forget about any processes we were recording */
366 start_wakeup_tracer(iter->tr);
369 static struct tracer wakeup_tracer __read_mostly =
372 .init = wakeup_tracer_init,
373 .reset = wakeup_tracer_reset,
374 .open = wakeup_tracer_open,
375 .close = wakeup_tracer_close,
376 .ctrl_update = wakeup_tracer_ctrl_update,
378 #ifdef CONFIG_FTRACE_SELFTEST
379 .selftest = trace_selftest_startup_wakeup,
383 __init static int init_wakeup_tracer(void)
387 ret = register_tracer(&wakeup_tracer);
393 device_initcall(init_wakeup_tracer);