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);
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;
48 if (likely(!wakeup_task))
51 resched = need_resched();
52 preempt_disable_notrace();
54 cpu = raw_smp_processor_id();
56 disabled = atomic_inc_return(&data->disabled);
57 if (unlikely(disabled != 1))
60 local_irq_save(flags);
61 __raw_spin_lock(&wakeup_lock);
63 if (unlikely(!wakeup_task))
67 * The task can't disappear because it needs to
68 * wake up first, and we have the wakeup_lock.
70 if (task_cpu(wakeup_task) != cpu)
73 trace_function(tr, data, ip, parent_ip, flags);
76 __raw_spin_unlock(&wakeup_lock);
77 local_irq_restore(flags);
80 atomic_dec(&data->disabled);
83 * To prevent recursion from the scheduler, if the
84 * resched flag was set before we entered, then
88 preempt_enable_no_resched_notrace();
90 preempt_enable_notrace();
93 static struct ftrace_ops trace_ops __read_mostly =
95 .func = wakeup_tracer_call,
97 #endif /* CONFIG_FTRACE */
100 * Should this new latency be reported/recorded?
102 static int report_latency(cycle_t delta)
104 if (tracing_thresh) {
105 if (delta < tracing_thresh)
108 if (delta <= tracing_max_latency)
115 probe_wakeup_sched_switch(struct rq *rq, struct task_struct *prev,
116 struct task_struct *next)
118 unsigned long latency = 0, t0 = 0, t1 = 0;
119 struct trace_array_cpu *data;
120 cycle_t T0, T1, delta;
125 tracing_record_cmdline(prev);
127 if (unlikely(!tracer_enabled))
131 * When we start a new trace, we set wakeup_task to NULL
132 * and then set tracer_enabled = 1. We want to make sure
133 * that another CPU does not see the tracer_enabled = 1
134 * and the wakeup_task with an older task, that might
135 * actually be the same as next.
139 if (next != wakeup_task)
142 /* The task we are waiting for is waking up */
143 data = wakeup_trace->data[wakeup_cpu];
145 /* disable local data, not wakeup_cpu data */
146 cpu = raw_smp_processor_id();
147 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
148 if (likely(disabled != 1))
151 local_irq_save(flags);
152 __raw_spin_lock(&wakeup_lock);
154 /* We could race with grabbing wakeup_lock */
155 if (unlikely(!tracer_enabled || next != wakeup_task))
158 trace_function(wakeup_trace, data, CALLER_ADDR1, CALLER_ADDR2, flags);
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)
189 struct trace_array_cpu *data;
192 for_each_possible_cpu(cpu) {
193 data = tr->data[cpu];
194 tracing_reset(tr, cpu);
201 put_task_struct(wakeup_task);
206 static void wakeup_reset(struct trace_array *tr)
210 local_irq_save(flags);
211 __raw_spin_lock(&wakeup_lock);
213 __raw_spin_unlock(&wakeup_lock);
214 local_irq_restore(flags);
218 probe_wakeup(struct rq *rq, struct task_struct *p)
220 int cpu = smp_processor_id();
224 if (likely(!tracer_enabled))
227 tracing_record_cmdline(p);
228 tracing_record_cmdline(current);
230 if (likely(!rt_task(p)) ||
231 p->prio >= wakeup_prio ||
232 p->prio >= current->prio)
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 wakeup_trace->data[wakeup_cpu]->preempt_timestamp = ftrace_now(cpu);
258 trace_function(wakeup_trace, wakeup_trace->data[wakeup_cpu],
259 CALLER_ADDR1, CALLER_ADDR2, flags);
262 __raw_spin_unlock(&wakeup_lock);
264 atomic_dec(&wakeup_trace->data[cpu]->disabled);
267 static void start_wakeup_tracer(struct trace_array *tr)
271 ret = register_trace_sched_wakeup(probe_wakeup);
273 pr_info("wakeup trace: Couldn't activate tracepoint"
274 " probe to kernel_sched_wakeup\n");
278 ret = register_trace_sched_wakeup_new(probe_wakeup);
280 pr_info("wakeup trace: Couldn't activate tracepoint"
281 " probe to kernel_sched_wakeup_new\n");
285 ret = register_trace_sched_switch(probe_wakeup_sched_switch);
287 pr_info("sched trace: Couldn't activate tracepoint"
288 " probe to kernel_sched_schedule\n");
289 goto fail_deprobe_wake_new;
295 * Don't let the tracer_enabled = 1 show up before
296 * the wakeup_task is reset. This may be overkill since
297 * wakeup_reset does a spin_unlock after setting the
298 * wakeup_task to NULL, but I want to be safe.
299 * This is a slow path anyway.
303 register_ftrace_function(&trace_ops);
308 fail_deprobe_wake_new:
309 unregister_trace_sched_wakeup_new(probe_wakeup);
311 unregister_trace_sched_wakeup(probe_wakeup);
314 static void stop_wakeup_tracer(struct trace_array *tr)
317 unregister_ftrace_function(&trace_ops);
318 unregister_trace_sched_switch(probe_wakeup_sched_switch);
319 unregister_trace_sched_wakeup_new(probe_wakeup);
320 unregister_trace_sched_wakeup(probe_wakeup);
323 static void wakeup_tracer_init(struct trace_array *tr)
328 start_wakeup_tracer(tr);
331 static void wakeup_tracer_reset(struct trace_array *tr)
334 stop_wakeup_tracer(tr);
335 /* make sure we put back any tasks we are tracing */
340 static void wakeup_tracer_ctrl_update(struct trace_array *tr)
343 start_wakeup_tracer(tr);
345 stop_wakeup_tracer(tr);
348 static void wakeup_tracer_open(struct trace_iterator *iter)
350 /* stop the trace while dumping */
352 stop_wakeup_tracer(iter->tr);
355 static void wakeup_tracer_close(struct trace_iterator *iter)
357 /* forget about any processes we were recording */
359 start_wakeup_tracer(iter->tr);
362 static struct tracer wakeup_tracer __read_mostly =
365 .init = wakeup_tracer_init,
366 .reset = wakeup_tracer_reset,
367 .open = wakeup_tracer_open,
368 .close = wakeup_tracer_close,
369 .ctrl_update = wakeup_tracer_ctrl_update,
371 #ifdef CONFIG_FTRACE_SELFTEST
372 .selftest = trace_selftest_startup_wakeup,
376 __init static int init_wakeup_tracer(void)
380 ret = register_tracer(&wakeup_tracer);
386 device_initcall(init_wakeup_tracer);