rfkill: add __must_check annotations
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / trace / trace_sched_wakeup.c
blobe303ccb62cdfb1284847864821d63a72694ca6af
1 /*
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>
13 #include <linux/fs.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>
20 #include "trace.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_FTRACE
36 * irqsoff uses its own tracer function to keep the overhead down:
38 static void
39 wakeup_tracer_call(unsigned long ip, unsigned long parent_ip)
41 struct trace_array *tr = wakeup_trace;
42 struct trace_array_cpu *data;
43 unsigned long flags;
44 long disabled;
45 int resched;
46 int cpu;
48 if (likely(!wakeup_task))
49 return;
51 resched = need_resched();
52 preempt_disable_notrace();
54 cpu = raw_smp_processor_id();
55 data = tr->data[cpu];
56 disabled = atomic_inc_return(&data->disabled);
57 if (unlikely(disabled != 1))
58 goto out;
60 local_irq_save(flags);
61 __raw_spin_lock(&wakeup_lock);
63 if (unlikely(!wakeup_task))
64 goto unlock;
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)
71 goto unlock;
73 trace_function(tr, data, ip, parent_ip, flags);
75 unlock:
76 __raw_spin_unlock(&wakeup_lock);
77 local_irq_restore(flags);
79 out:
80 atomic_dec(&data->disabled);
83 * To prevent recursion from the scheduler, if the
84 * resched flag was set before we entered, then
85 * don't reschedule.
87 if (resched)
88 preempt_enable_no_resched_notrace();
89 else
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)
106 return 0;
107 } else {
108 if (delta <= tracing_max_latency)
109 return 0;
111 return 1;
114 static void notrace
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;
123 unsigned long flags;
124 long disabled;
125 int cpu;
127 if (unlikely(!tracer_enabled))
128 return;
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.
137 smp_rmb();
139 if (next != wakeup_task)
140 return;
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))
149 goto out;
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))
156 goto out_unlock;
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);
166 delta = T1-T0;
168 if (!report_latency(delta))
169 goto out_unlock;
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);
179 out_unlock:
180 __wakeup_reset(tr);
181 __raw_spin_unlock(&wakeup_lock);
182 local_irq_restore(flags);
183 out:
184 atomic_dec(&tr->data[cpu]->disabled);
187 static notrace void
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;
193 struct rq *__rq;
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;
215 int cpu;
217 for_each_possible_cpu(cpu) {
218 data = tr->data[cpu];
219 tracing_reset(data);
222 wakeup_cpu = -1;
223 wakeup_prio = -1;
225 if (wakeup_task)
226 put_task_struct(wakeup_task);
228 wakeup_task = NULL;
231 static void wakeup_reset(struct trace_array *tr)
233 unsigned long flags;
235 local_irq_save(flags);
236 __raw_spin_lock(&wakeup_lock);
237 __wakeup_reset(tr);
238 __raw_spin_unlock(&wakeup_lock);
239 local_irq_restore(flags);
242 static void
243 wakeup_check_start(struct trace_array *tr, struct task_struct *p,
244 struct task_struct *curr)
246 int cpu = smp_processor_id();
247 unsigned long flags;
248 long disabled;
250 if (likely(!rt_task(p)) ||
251 p->prio >= wakeup_prio ||
252 p->prio >= curr->prio)
253 return;
255 disabled = atomic_inc_return(&tr->data[cpu]->disabled);
256 if (unlikely(disabled != 1))
257 goto out;
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)
264 goto out_locked;
266 /* reset the trace */
267 __wakeup_reset(tr);
269 wakeup_cpu = task_cpu(p);
270 wakeup_prio = p->prio;
272 wakeup_task = p;
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);
281 out_locked:
282 __raw_spin_unlock(&wakeup_lock);
283 out:
284 atomic_dec(&tr->data[cpu]->disabled);
287 static notrace void
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;
295 struct rq *__rq;
297 if (likely(!tracer_enabled))
298 return;
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)
316 int ret;
318 ret = marker_probe_register("kernel_sched_wakeup",
319 "pid %d state %ld ## rq %p task %p rq->curr %p",
320 wake_up_callback,
321 &wakeup_trace);
322 if (ret) {
323 pr_info("wakeup trace: Couldn't add marker"
324 " probe to kernel_sched_wakeup\n");
325 return;
328 ret = marker_probe_register("kernel_sched_wakeup_new",
329 "pid %d state %ld ## rq %p task %p rq->curr %p",
330 wake_up_callback,
331 &wakeup_trace);
332 if (ret) {
333 pr_info("wakeup trace: Couldn't add marker"
334 " probe to kernel_sched_wakeup_new\n");
335 goto fail_deprobe;
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,
342 &wakeup_trace);
343 if (ret) {
344 pr_info("sched trace: Couldn't add marker"
345 " probe to kernel_sched_schedule\n");
346 goto fail_deprobe_wake_new;
349 wakeup_reset(tr);
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.
358 smp_wmb();
360 register_ftrace_function(&trace_ops);
362 tracer_enabled = 1;
364 return;
365 fail_deprobe_wake_new:
366 marker_probe_unregister("kernel_sched_wakeup_new",
367 wake_up_callback,
368 &wakeup_trace);
369 fail_deprobe:
370 marker_probe_unregister("kernel_sched_wakeup",
371 wake_up_callback,
372 &wakeup_trace);
375 static void stop_wakeup_tracer(struct trace_array *tr)
377 tracer_enabled = 0;
378 unregister_ftrace_function(&trace_ops);
379 marker_probe_unregister("kernel_sched_schedule",
380 sched_switch_callback,
381 &wakeup_trace);
382 marker_probe_unregister("kernel_sched_wakeup_new",
383 wake_up_callback,
384 &wakeup_trace);
385 marker_probe_unregister("kernel_sched_wakeup",
386 wake_up_callback,
387 &wakeup_trace);
390 static void wakeup_tracer_init(struct trace_array *tr)
392 wakeup_trace = tr;
394 if (tr->ctrl)
395 start_wakeup_tracer(tr);
398 static void wakeup_tracer_reset(struct trace_array *tr)
400 if (tr->ctrl) {
401 stop_wakeup_tracer(tr);
402 /* make sure we put back any tasks we are tracing */
403 wakeup_reset(tr);
407 static void wakeup_tracer_ctrl_update(struct trace_array *tr)
409 if (tr->ctrl)
410 start_wakeup_tracer(tr);
411 else
412 stop_wakeup_tracer(tr);
415 static void wakeup_tracer_open(struct trace_iterator *iter)
417 /* stop the trace while dumping */
418 if (iter->tr->ctrl)
419 stop_wakeup_tracer(iter->tr);
422 static void wakeup_tracer_close(struct trace_iterator *iter)
424 /* forget about any processes we were recording */
425 if (iter->tr->ctrl)
426 start_wakeup_tracer(iter->tr);
429 static struct tracer wakeup_tracer __read_mostly =
431 .name = "wakeup",
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,
437 .print_max = 1,
438 #ifdef CONFIG_FTRACE_SELFTEST
439 .selftest = trace_selftest_startup_wakeup,
440 #endif
443 __init static int init_wakeup_tracer(void)
445 int ret;
447 ret = register_tracer(&wakeup_tracer);
448 if (ret)
449 return ret;
451 return 0;
453 device_initcall(init_wakeup_tracer);