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 <linux/marker.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 wakeup_sched_switch(void *private, void *rq, struct task_struct *prev,
116 struct task_struct *next)
118 unsigned long latency = 0, t0 = 0, t1 = 0;
119 struct trace_array **ptr = private;
120 struct trace_array *tr = *ptr;
121 struct trace_array_cpu *data;
122 cycle_t T0, T1, delta;
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 = tr->data[wakeup_cpu];
145 /* disable local data, not wakeup_cpu data */
146 cpu = raw_smp_processor_id();
147 disabled = atomic_inc_return(&tr->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(tr, 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(tr, wakeup_task, wakeup_cpu);
181 __raw_spin_unlock(&wakeup_lock);
182 local_irq_restore(flags);
184 atomic_dec(&tr->data[cpu]->disabled);
188 sched_switch_callback(void *probe_data, void *call_data,
189 const char *format, va_list *args)
191 struct task_struct *prev;
192 struct task_struct *next;
195 /* skip prev_pid %d next_pid %d prev_state %ld */
196 (void)va_arg(*args, int);
197 (void)va_arg(*args, int);
198 (void)va_arg(*args, long);
199 __rq = va_arg(*args, typeof(__rq));
200 prev = va_arg(*args, typeof(prev));
201 next = va_arg(*args, typeof(next));
203 tracing_record_cmdline(prev);
206 * If tracer_switch_func only points to the local
207 * switch func, it still needs the ptr passed to it.
209 wakeup_sched_switch(probe_data, __rq, prev, next);
212 static void __wakeup_reset(struct trace_array *tr)
214 struct trace_array_cpu *data;
217 for_each_possible_cpu(cpu) {
218 data = tr->data[cpu];
226 put_task_struct(wakeup_task);
231 static void wakeup_reset(struct trace_array *tr)
235 local_irq_save(flags);
236 __raw_spin_lock(&wakeup_lock);
238 __raw_spin_unlock(&wakeup_lock);
239 local_irq_restore(flags);
243 wakeup_check_start(struct trace_array *tr, struct task_struct *p,
244 struct task_struct *curr)
246 int cpu = smp_processor_id();
250 if (likely(!rt_task(p)) ||
251 p->prio >= wakeup_prio ||
252 p->prio >= curr->prio)
255 disabled = atomic_inc_return(&tr->data[cpu]->disabled);
256 if (unlikely(disabled != 1))
259 /* interrupts should be off from try_to_wake_up */
260 __raw_spin_lock(&wakeup_lock);
262 /* check for races. */
263 if (!tracer_enabled || p->prio >= wakeup_prio)
266 /* reset the trace */
269 wakeup_cpu = task_cpu(p);
270 wakeup_prio = p->prio;
273 get_task_struct(wakeup_task);
275 local_save_flags(flags);
277 tr->data[wakeup_cpu]->preempt_timestamp = ftrace_now(cpu);
278 trace_function(tr, tr->data[wakeup_cpu],
279 CALLER_ADDR1, CALLER_ADDR2, flags);
282 __raw_spin_unlock(&wakeup_lock);
284 atomic_dec(&tr->data[cpu]->disabled);
288 wake_up_callback(void *probe_data, void *call_data,
289 const char *format, va_list *args)
291 struct trace_array **ptr = probe_data;
292 struct trace_array *tr = *ptr;
293 struct task_struct *curr;
294 struct task_struct *task;
297 if (likely(!tracer_enabled))
300 /* Skip pid %d state %ld */
301 (void)va_arg(*args, int);
302 (void)va_arg(*args, long);
303 /* now get the meat: "rq %p task %p rq->curr %p" */
304 __rq = va_arg(*args, typeof(__rq));
305 task = va_arg(*args, typeof(task));
306 curr = va_arg(*args, typeof(curr));
308 tracing_record_cmdline(task);
309 tracing_record_cmdline(curr);
311 wakeup_check_start(tr, task, curr);
314 static void start_wakeup_tracer(struct trace_array *tr)
318 ret = marker_probe_register("kernel_sched_wakeup",
319 "pid %d state %ld ## rq %p task %p rq->curr %p",
323 pr_info("wakeup trace: Couldn't add marker"
324 " probe to kernel_sched_wakeup\n");
328 ret = marker_probe_register("kernel_sched_wakeup_new",
329 "pid %d state %ld ## rq %p task %p rq->curr %p",
333 pr_info("wakeup trace: Couldn't add marker"
334 " probe to kernel_sched_wakeup_new\n");
338 ret = marker_probe_register("kernel_sched_schedule",
339 "prev_pid %d next_pid %d prev_state %ld "
340 "## rq %p prev %p next %p",
341 sched_switch_callback,
344 pr_info("sched trace: Couldn't add marker"
345 " probe to kernel_sched_schedule\n");
346 goto fail_deprobe_wake_new;
352 * Don't let the tracer_enabled = 1 show up before
353 * the wakeup_task is reset. This may be overkill since
354 * wakeup_reset does a spin_unlock after setting the
355 * wakeup_task to NULL, but I want to be safe.
356 * This is a slow path anyway.
360 register_ftrace_function(&trace_ops);
365 fail_deprobe_wake_new:
366 marker_probe_unregister("kernel_sched_wakeup_new",
370 marker_probe_unregister("kernel_sched_wakeup",
375 static void stop_wakeup_tracer(struct trace_array *tr)
378 unregister_ftrace_function(&trace_ops);
379 marker_probe_unregister("kernel_sched_schedule",
380 sched_switch_callback,
382 marker_probe_unregister("kernel_sched_wakeup_new",
385 marker_probe_unregister("kernel_sched_wakeup",
390 static void wakeup_tracer_init(struct trace_array *tr)
395 start_wakeup_tracer(tr);
398 static void wakeup_tracer_reset(struct trace_array *tr)
401 stop_wakeup_tracer(tr);
402 /* make sure we put back any tasks we are tracing */
407 static void wakeup_tracer_ctrl_update(struct trace_array *tr)
410 start_wakeup_tracer(tr);
412 stop_wakeup_tracer(tr);
415 static void wakeup_tracer_open(struct trace_iterator *iter)
417 /* stop the trace while dumping */
419 stop_wakeup_tracer(iter->tr);
422 static void wakeup_tracer_close(struct trace_iterator *iter)
424 /* forget about any processes we were recording */
426 start_wakeup_tracer(iter->tr);
429 static struct tracer wakeup_tracer __read_mostly =
432 .init = wakeup_tracer_init,
433 .reset = wakeup_tracer_reset,
434 .open = wakeup_tracer_open,
435 .close = wakeup_tracer_close,
436 .ctrl_update = wakeup_tracer_ctrl_update,
438 #ifdef CONFIG_FTRACE_SELFTEST
439 .selftest = trace_selftest_startup_wakeup,
443 __init static int init_wakeup_tracer(void)
447 ret = register_tracer(&wakeup_tracer);
453 device_initcall(init_wakeup_tracer);