93cecda650b20f620ff9449cb17782e187b27d49
[safe/jmp/linux-2.6] / kernel / trace / trace_sched_wakeup.c
1 /*
2  * trace task wakeup timings
3  *
4  * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5  * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
6  *
7  * Based on code from the latency_tracer, that is:
8  *
9  *  Copyright (C) 2004-2006 Ingo Molnar
10  *  Copyright (C) 2004 William Lee Irwin III
11  */
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 <trace/sched.h>
19
20 #include "trace.h"
21
22 static struct trace_array       *wakeup_trace;
23 static int __read_mostly        tracer_enabled;
24
25 static struct task_struct       *wakeup_task;
26 static int                      wakeup_cpu;
27 static unsigned                 wakeup_prio = -1;
28 static int                      wakeup_rt;
29
30 static raw_spinlock_t wakeup_lock =
31         (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
32
33 static void __wakeup_reset(struct trace_array *tr);
34
35 #ifdef CONFIG_FUNCTION_TRACER
36 /*
37  * irqsoff uses its own tracer function to keep the overhead down:
38  */
39 static void
40 wakeup_tracer_call(unsigned long ip, unsigned long parent_ip)
41 {
42         struct trace_array *tr = wakeup_trace;
43         struct trace_array_cpu *data;
44         unsigned long flags;
45         long disabled;
46         int resched;
47         int cpu;
48         int pc;
49
50         if (likely(!wakeup_task))
51                 return;
52
53         pc = preempt_count();
54         resched = ftrace_preempt_disable();
55
56         cpu = raw_smp_processor_id();
57         data = tr->data[cpu];
58         disabled = atomic_inc_return(&data->disabled);
59         if (unlikely(disabled != 1))
60                 goto out;
61
62         local_irq_save(flags);
63         __raw_spin_lock(&wakeup_lock);
64
65         if (unlikely(!wakeup_task))
66                 goto unlock;
67
68         /*
69          * The task can't disappear because it needs to
70          * wake up first, and we have the wakeup_lock.
71          */
72         if (task_cpu(wakeup_task) != cpu)
73                 goto unlock;
74
75         trace_function(tr, data, ip, parent_ip, flags, pc);
76
77  unlock:
78         __raw_spin_unlock(&wakeup_lock);
79         local_irq_restore(flags);
80
81  out:
82         atomic_dec(&data->disabled);
83
84         ftrace_preempt_enable(resched);
85 }
86
87 static struct ftrace_ops trace_ops __read_mostly =
88 {
89         .func = wakeup_tracer_call,
90 };
91 #endif /* CONFIG_FUNCTION_TRACER */
92
93 /*
94  * Should this new latency be reported/recorded?
95  */
96 static int report_latency(cycle_t delta)
97 {
98         if (tracing_thresh) {
99                 if (delta < tracing_thresh)
100                         return 0;
101         } else {
102                 if (delta <= tracing_max_latency)
103                         return 0;
104         }
105         return 1;
106 }
107
108 static void notrace
109 probe_wakeup_sched_switch(struct rq *rq, struct task_struct *prev,
110         struct task_struct *next)
111 {
112         unsigned long latency = 0, t0 = 0, t1 = 0;
113         struct trace_array_cpu *data;
114         cycle_t T0, T1, delta;
115         unsigned long flags;
116         long disabled;
117         int cpu;
118         int pc;
119
120         tracing_record_cmdline(prev);
121
122         if (unlikely(!tracer_enabled))
123                 return;
124
125         /*
126          * When we start a new trace, we set wakeup_task to NULL
127          * and then set tracer_enabled = 1. We want to make sure
128          * that another CPU does not see the tracer_enabled = 1
129          * and the wakeup_task with an older task, that might
130          * actually be the same as next.
131          */
132         smp_rmb();
133
134         if (next != wakeup_task)
135                 return;
136
137         pc = preempt_count();
138
139         /* The task we are waiting for is waking up */
140         data = wakeup_trace->data[wakeup_cpu];
141
142         /* disable local data, not wakeup_cpu data */
143         cpu = raw_smp_processor_id();
144         disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
145         if (likely(disabled != 1))
146                 goto out;
147
148         local_irq_save(flags);
149         __raw_spin_lock(&wakeup_lock);
150
151         /* We could race with grabbing wakeup_lock */
152         if (unlikely(!tracer_enabled || next != wakeup_task))
153                 goto out_unlock;
154
155         trace_function(wakeup_trace, data, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
156         tracing_sched_switch_trace(wakeup_trace, data, prev, next, flags, pc);
157
158         /*
159          * usecs conversion is slow so we try to delay the conversion
160          * as long as possible:
161          */
162         T0 = data->preempt_timestamp;
163         T1 = ftrace_now(cpu);
164         delta = T1-T0;
165
166         if (!report_latency(delta))
167                 goto out_unlock;
168
169         latency = nsecs_to_usecs(delta);
170
171         tracing_max_latency = delta;
172         t0 = nsecs_to_usecs(T0);
173         t1 = nsecs_to_usecs(T1);
174
175         update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu);
176
177 out_unlock:
178         __wakeup_reset(wakeup_trace);
179         __raw_spin_unlock(&wakeup_lock);
180         local_irq_restore(flags);
181 out:
182         atomic_dec(&wakeup_trace->data[cpu]->disabled);
183 }
184
185 static void __wakeup_reset(struct trace_array *tr)
186 {
187         struct trace_array_cpu *data;
188         int cpu;
189
190         for_each_possible_cpu(cpu) {
191                 data = tr->data[cpu];
192                 tracing_reset(tr, cpu);
193         }
194
195         wakeup_cpu = -1;
196         wakeup_prio = -1;
197
198         if (wakeup_task)
199                 put_task_struct(wakeup_task);
200
201         wakeup_task = NULL;
202 }
203
204 static void wakeup_reset(struct trace_array *tr)
205 {
206         unsigned long flags;
207
208         local_irq_save(flags);
209         __raw_spin_lock(&wakeup_lock);
210         __wakeup_reset(tr);
211         __raw_spin_unlock(&wakeup_lock);
212         local_irq_restore(flags);
213 }
214
215 static void
216 probe_wakeup(struct rq *rq, struct task_struct *p, int success)
217 {
218         struct trace_array_cpu *data;
219         int cpu = smp_processor_id();
220         unsigned long flags;
221         long disabled;
222         int pc;
223
224         if (likely(!tracer_enabled))
225                 return;
226
227         tracing_record_cmdline(p);
228         tracing_record_cmdline(current);
229
230         if ((wakeup_rt && !rt_task(p)) ||
231                         p->prio >= wakeup_prio ||
232                         p->prio >= current->prio)
233                 return;
234
235         pc = preempt_count();
236         disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
237         if (unlikely(disabled != 1))
238                 goto out;
239
240         /* interrupts should be off from try_to_wake_up */
241         __raw_spin_lock(&wakeup_lock);
242
243         /* check for races. */
244         if (!tracer_enabled || p->prio >= wakeup_prio)
245                 goto out_locked;
246
247         /* reset the trace */
248         __wakeup_reset(wakeup_trace);
249
250         wakeup_cpu = task_cpu(p);
251         wakeup_prio = p->prio;
252
253         wakeup_task = p;
254         get_task_struct(wakeup_task);
255
256         local_save_flags(flags);
257
258         data = wakeup_trace->data[wakeup_cpu];
259         data->preempt_timestamp = ftrace_now(cpu);
260         tracing_sched_wakeup_trace(wakeup_trace, data, p, current,
261                                    flags, pc);
262         trace_function(wakeup_trace, data, CALLER_ADDR1, CALLER_ADDR2,
263                        flags, pc);
264
265 out_locked:
266         __raw_spin_unlock(&wakeup_lock);
267 out:
268         atomic_dec(&wakeup_trace->data[cpu]->disabled);
269 }
270
271 static void start_wakeup_tracer(struct trace_array *tr)
272 {
273         int ret;
274
275         ret = register_trace_sched_wakeup(probe_wakeup);
276         if (ret) {
277                 pr_info("wakeup trace: Couldn't activate tracepoint"
278                         " probe to kernel_sched_wakeup\n");
279                 return;
280         }
281
282         ret = register_trace_sched_wakeup_new(probe_wakeup);
283         if (ret) {
284                 pr_info("wakeup trace: Couldn't activate tracepoint"
285                         " probe to kernel_sched_wakeup_new\n");
286                 goto fail_deprobe;
287         }
288
289         ret = register_trace_sched_switch(probe_wakeup_sched_switch);
290         if (ret) {
291                 pr_info("sched trace: Couldn't activate tracepoint"
292                         " probe to kernel_sched_schedule\n");
293                 goto fail_deprobe_wake_new;
294         }
295
296         wakeup_reset(tr);
297
298         /*
299          * Don't let the tracer_enabled = 1 show up before
300          * the wakeup_task is reset. This may be overkill since
301          * wakeup_reset does a spin_unlock after setting the
302          * wakeup_task to NULL, but I want to be safe.
303          * This is a slow path anyway.
304          */
305         smp_wmb();
306
307         register_ftrace_function(&trace_ops);
308
309         if (tracing_is_enabled())
310                 tracer_enabled = 1;
311         else
312                 tracer_enabled = 0;
313
314         return;
315 fail_deprobe_wake_new:
316         unregister_trace_sched_wakeup_new(probe_wakeup);
317 fail_deprobe:
318         unregister_trace_sched_wakeup(probe_wakeup);
319 }
320
321 static void stop_wakeup_tracer(struct trace_array *tr)
322 {
323         tracer_enabled = 0;
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);
328 }
329
330 static int __wakeup_tracer_init(struct trace_array *tr)
331 {
332         tracing_max_latency = 0;
333         wakeup_trace = tr;
334         start_wakeup_tracer(tr);
335         return 0;
336 }
337
338 static int wakeup_tracer_init(struct trace_array *tr)
339 {
340         wakeup_rt = 0;
341         return __wakeup_tracer_init(tr);
342 }
343
344 static int wakeup_rt_tracer_init(struct trace_array *tr)
345 {
346         wakeup_rt = 1;
347         return __wakeup_tracer_init(tr);
348 }
349
350 static void wakeup_tracer_reset(struct trace_array *tr)
351 {
352         stop_wakeup_tracer(tr);
353         /* make sure we put back any tasks we are tracing */
354         wakeup_reset(tr);
355 }
356
357 static void wakeup_tracer_start(struct trace_array *tr)
358 {
359         wakeup_reset(tr);
360         tracer_enabled = 1;
361 }
362
363 static void wakeup_tracer_stop(struct trace_array *tr)
364 {
365         tracer_enabled = 0;
366 }
367
368 static struct tracer wakeup_tracer __read_mostly =
369 {
370         .name           = "wakeup",
371         .init           = wakeup_tracer_init,
372         .reset          = wakeup_tracer_reset,
373         .start          = wakeup_tracer_start,
374         .stop           = wakeup_tracer_stop,
375         .print_max      = 1,
376 #ifdef CONFIG_FTRACE_SELFTEST
377         .selftest    = trace_selftest_startup_wakeup,
378 #endif
379 };
380
381 static struct tracer wakeup_rt_tracer __read_mostly =
382 {
383         .name           = "wakeup_rt",
384         .init           = wakeup_rt_tracer_init,
385         .reset          = wakeup_tracer_reset,
386         .start          = wakeup_tracer_start,
387         .stop           = wakeup_tracer_stop,
388         .print_max      = 1,
389 #ifdef CONFIG_FTRACE_SELFTEST
390         .selftest    = trace_selftest_startup_wakeup,
391 #endif
392 };
393
394 __init static int init_wakeup_tracer(void)
395 {
396         int ret;
397
398         ret = register_tracer(&wakeup_tracer);
399         if (ret)
400                 return ret;
401
402         ret = register_tracer(&wakeup_rt_tracer);
403         if (ret)
404                 return ret;
405
406         return 0;
407 }
408 device_initcall(init_wakeup_tracer);