blob: 7bc4abf6fca8217f1e393b5eb603c412fb503aab [file] [log] [blame]
Steven Rostedt352ad252008-05-12 21:20:42 +02001/*
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>
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -040018#include <trace/sched.h>
Steven Rostedt352ad252008-05-12 21:20:42 +020019
20#include "trace.h"
21
22static struct trace_array *wakeup_trace;
23static int __read_mostly tracer_enabled;
24
25static struct task_struct *wakeup_task;
26static int wakeup_cpu;
27static unsigned wakeup_prio = -1;
28
Steven Rostedte59494f2008-07-16 00:13:45 -040029static raw_spinlock_t wakeup_lock =
30 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
Steven Rostedt352ad252008-05-12 21:20:42 +020031
Ingo Molnare309b412008-05-12 21:20:51 +020032static void __wakeup_reset(struct trace_array *tr);
Steven Rostedt352ad252008-05-12 21:20:42 +020033
Steven Rostedt606576c2008-10-06 19:06:12 -040034#ifdef CONFIG_FUNCTION_TRACER
Steven Rostedt7e18d8e2008-05-22 00:22:19 -040035/*
36 * irqsoff uses its own tracer function to keep the overhead down:
37 */
38static void
39wakeup_tracer_call(unsigned long ip, unsigned long parent_ip)
40{
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;
Steven Rostedt38697052008-10-01 13:14:09 -040047 int pc;
Steven Rostedt7e18d8e2008-05-22 00:22:19 -040048
49 if (likely(!wakeup_task))
50 return;
51
Steven Rostedt38697052008-10-01 13:14:09 -040052 pc = preempt_count();
Steven Rostedt182e9f52008-11-03 23:15:56 -050053 resched = ftrace_preempt_disable();
Steven Rostedt7e18d8e2008-05-22 00:22:19 -040054
55 cpu = raw_smp_processor_id();
56 data = tr->data[cpu];
57 disabled = atomic_inc_return(&data->disabled);
58 if (unlikely(disabled != 1))
59 goto out;
60
Steven Rostedte59494f2008-07-16 00:13:45 -040061 local_irq_save(flags);
62 __raw_spin_lock(&wakeup_lock);
Steven Rostedt7e18d8e2008-05-22 00:22:19 -040063
64 if (unlikely(!wakeup_task))
65 goto unlock;
66
67 /*
68 * The task can't disappear because it needs to
69 * wake up first, and we have the wakeup_lock.
70 */
71 if (task_cpu(wakeup_task) != cpu)
72 goto unlock;
73
Steven Rostedt38697052008-10-01 13:14:09 -040074 trace_function(tr, data, ip, parent_ip, flags, pc);
Steven Rostedt7e18d8e2008-05-22 00:22:19 -040075
76 unlock:
Steven Rostedte59494f2008-07-16 00:13:45 -040077 __raw_spin_unlock(&wakeup_lock);
78 local_irq_restore(flags);
Steven Rostedt7e18d8e2008-05-22 00:22:19 -040079
80 out:
81 atomic_dec(&data->disabled);
82
Steven Rostedt182e9f52008-11-03 23:15:56 -050083 ftrace_preempt_enable(resched);
Steven Rostedt7e18d8e2008-05-22 00:22:19 -040084}
85
86static struct ftrace_ops trace_ops __read_mostly =
87{
88 .func = wakeup_tracer_call,
89};
Steven Rostedt606576c2008-10-06 19:06:12 -040090#endif /* CONFIG_FUNCTION_TRACER */
Steven Rostedt7e18d8e2008-05-22 00:22:19 -040091
Steven Rostedt352ad252008-05-12 21:20:42 +020092/*
93 * Should this new latency be reported/recorded?
94 */
Ingo Molnare309b412008-05-12 21:20:51 +020095static int report_latency(cycle_t delta)
Steven Rostedt352ad252008-05-12 21:20:42 +020096{
97 if (tracing_thresh) {
98 if (delta < tracing_thresh)
99 return 0;
100 } else {
101 if (delta <= tracing_max_latency)
102 return 0;
103 }
104 return 1;
105}
106
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200107static void notrace
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400108probe_wakeup_sched_switch(struct rq *rq, struct task_struct *prev,
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200109 struct task_struct *next)
Steven Rostedt352ad252008-05-12 21:20:42 +0200110{
111 unsigned long latency = 0, t0 = 0, t1 = 0;
Steven Rostedt352ad252008-05-12 21:20:42 +0200112 struct trace_array_cpu *data;
113 cycle_t T0, T1, delta;
114 unsigned long flags;
115 long disabled;
116 int cpu;
Steven Rostedt38697052008-10-01 13:14:09 -0400117 int pc;
Steven Rostedt352ad252008-05-12 21:20:42 +0200118
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400119 tracing_record_cmdline(prev);
120
Steven Rostedt352ad252008-05-12 21:20:42 +0200121 if (unlikely(!tracer_enabled))
122 return;
123
124 /*
125 * When we start a new trace, we set wakeup_task to NULL
126 * and then set tracer_enabled = 1. We want to make sure
127 * that another CPU does not see the tracer_enabled = 1
128 * and the wakeup_task with an older task, that might
129 * actually be the same as next.
130 */
131 smp_rmb();
132
133 if (next != wakeup_task)
134 return;
135
Steven Rostedt38697052008-10-01 13:14:09 -0400136 pc = preempt_count();
137
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400138 /* The task we are waiting for is waking up */
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400139 data = wakeup_trace->data[wakeup_cpu];
Steven Rostedt352ad252008-05-12 21:20:42 +0200140
141 /* disable local data, not wakeup_cpu data */
142 cpu = raw_smp_processor_id();
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400143 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
Steven Rostedt352ad252008-05-12 21:20:42 +0200144 if (likely(disabled != 1))
145 goto out;
146
Steven Rostedte59494f2008-07-16 00:13:45 -0400147 local_irq_save(flags);
148 __raw_spin_lock(&wakeup_lock);
Steven Rostedt352ad252008-05-12 21:20:42 +0200149
150 /* We could race with grabbing wakeup_lock */
151 if (unlikely(!tracer_enabled || next != wakeup_task))
152 goto out_unlock;
153
Steven Rostedt38697052008-10-01 13:14:09 -0400154 trace_function(wakeup_trace, data, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
Steven Rostedt352ad252008-05-12 21:20:42 +0200155
156 /*
157 * usecs conversion is slow so we try to delay the conversion
158 * as long as possible:
159 */
160 T0 = data->preempt_timestamp;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200161 T1 = ftrace_now(cpu);
Steven Rostedt352ad252008-05-12 21:20:42 +0200162 delta = T1-T0;
163
164 if (!report_latency(delta))
165 goto out_unlock;
166
167 latency = nsecs_to_usecs(delta);
168
169 tracing_max_latency = delta;
170 t0 = nsecs_to_usecs(T0);
171 t1 = nsecs_to_usecs(T1);
172
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400173 update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu);
Steven Rostedt352ad252008-05-12 21:20:42 +0200174
Steven Rostedt352ad252008-05-12 21:20:42 +0200175out_unlock:
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400176 __wakeup_reset(wakeup_trace);
Steven Rostedte59494f2008-07-16 00:13:45 -0400177 __raw_spin_unlock(&wakeup_lock);
178 local_irq_restore(flags);
Steven Rostedt352ad252008-05-12 21:20:42 +0200179out:
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400180 atomic_dec(&wakeup_trace->data[cpu]->disabled);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200181}
182
Ingo Molnare309b412008-05-12 21:20:51 +0200183static void __wakeup_reset(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200184{
185 struct trace_array_cpu *data;
186 int cpu;
187
Steven Rostedt352ad252008-05-12 21:20:42 +0200188 for_each_possible_cpu(cpu) {
189 data = tr->data[cpu];
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400190 tracing_reset(tr, cpu);
Steven Rostedt352ad252008-05-12 21:20:42 +0200191 }
192
193 wakeup_cpu = -1;
194 wakeup_prio = -1;
195
196 if (wakeup_task)
197 put_task_struct(wakeup_task);
198
199 wakeup_task = NULL;
200}
201
Ingo Molnare309b412008-05-12 21:20:51 +0200202static void wakeup_reset(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200203{
204 unsigned long flags;
205
Steven Rostedte59494f2008-07-16 00:13:45 -0400206 local_irq_save(flags);
207 __raw_spin_lock(&wakeup_lock);
Steven Rostedt352ad252008-05-12 21:20:42 +0200208 __wakeup_reset(tr);
Steven Rostedte59494f2008-07-16 00:13:45 -0400209 __raw_spin_unlock(&wakeup_lock);
210 local_irq_restore(flags);
Steven Rostedt352ad252008-05-12 21:20:42 +0200211}
212
Ingo Molnare309b412008-05-12 21:20:51 +0200213static void
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400214probe_wakeup(struct rq *rq, struct task_struct *p)
Steven Rostedt352ad252008-05-12 21:20:42 +0200215{
216 int cpu = smp_processor_id();
217 unsigned long flags;
218 long disabled;
Steven Rostedt38697052008-10-01 13:14:09 -0400219 int pc;
Steven Rostedt352ad252008-05-12 21:20:42 +0200220
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400221 if (likely(!tracer_enabled))
Steven Rostedt352ad252008-05-12 21:20:42 +0200222 return;
223
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400224 tracing_record_cmdline(p);
225 tracing_record_cmdline(current);
226
227 if (likely(!rt_task(p)) ||
228 p->prio >= wakeup_prio ||
229 p->prio >= current->prio)
230 return;
231
Steven Rostedt38697052008-10-01 13:14:09 -0400232 pc = preempt_count();
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400233 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
Steven Rostedt352ad252008-05-12 21:20:42 +0200234 if (unlikely(disabled != 1))
235 goto out;
236
237 /* interrupts should be off from try_to_wake_up */
Steven Rostedte59494f2008-07-16 00:13:45 -0400238 __raw_spin_lock(&wakeup_lock);
Steven Rostedt352ad252008-05-12 21:20:42 +0200239
240 /* check for races. */
241 if (!tracer_enabled || p->prio >= wakeup_prio)
242 goto out_locked;
243
244 /* reset the trace */
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400245 __wakeup_reset(wakeup_trace);
Steven Rostedt352ad252008-05-12 21:20:42 +0200246
247 wakeup_cpu = task_cpu(p);
248 wakeup_prio = p->prio;
249
250 wakeup_task = p;
251 get_task_struct(wakeup_task);
252
253 local_save_flags(flags);
254
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400255 wakeup_trace->data[wakeup_cpu]->preempt_timestamp = ftrace_now(cpu);
256 trace_function(wakeup_trace, wakeup_trace->data[wakeup_cpu],
Steven Rostedt38697052008-10-01 13:14:09 -0400257 CALLER_ADDR1, CALLER_ADDR2, flags, pc);
Steven Rostedt352ad252008-05-12 21:20:42 +0200258
259out_locked:
Steven Rostedte59494f2008-07-16 00:13:45 -0400260 __raw_spin_unlock(&wakeup_lock);
Steven Rostedt352ad252008-05-12 21:20:42 +0200261out:
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400262 atomic_dec(&wakeup_trace->data[cpu]->disabled);
Steven Rostedt352ad252008-05-12 21:20:42 +0200263}
264
Ingo Molnare309b412008-05-12 21:20:51 +0200265static void start_wakeup_tracer(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200266{
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200267 int ret;
268
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400269 ret = register_trace_sched_wakeup(probe_wakeup);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200270 if (ret) {
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400271 pr_info("wakeup trace: Couldn't activate tracepoint"
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200272 " probe to kernel_sched_wakeup\n");
273 return;
274 }
275
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400276 ret = register_trace_sched_wakeup_new(probe_wakeup);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200277 if (ret) {
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400278 pr_info("wakeup trace: Couldn't activate tracepoint"
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200279 " probe to kernel_sched_wakeup_new\n");
280 goto fail_deprobe;
281 }
282
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400283 ret = register_trace_sched_switch(probe_wakeup_sched_switch);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200284 if (ret) {
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400285 pr_info("sched trace: Couldn't activate tracepoint"
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200286 " probe to kernel_sched_schedule\n");
287 goto fail_deprobe_wake_new;
288 }
289
Steven Rostedt352ad252008-05-12 21:20:42 +0200290 wakeup_reset(tr);
291
292 /*
293 * Don't let the tracer_enabled = 1 show up before
294 * the wakeup_task is reset. This may be overkill since
295 * wakeup_reset does a spin_unlock after setting the
296 * wakeup_task to NULL, but I want to be safe.
297 * This is a slow path anyway.
298 */
299 smp_wmb();
300
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400301 register_ftrace_function(&trace_ops);
Steven Rostedt352ad252008-05-12 21:20:42 +0200302
Steven Rostedtad591242008-07-10 20:58:13 -0400303 tracer_enabled = 1;
304
Steven Rostedt352ad252008-05-12 21:20:42 +0200305 return;
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200306fail_deprobe_wake_new:
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400307 unregister_trace_sched_wakeup_new(probe_wakeup);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200308fail_deprobe:
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400309 unregister_trace_sched_wakeup(probe_wakeup);
Steven Rostedt352ad252008-05-12 21:20:42 +0200310}
311
Ingo Molnare309b412008-05-12 21:20:51 +0200312static void stop_wakeup_tracer(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200313{
314 tracer_enabled = 0;
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400315 unregister_ftrace_function(&trace_ops);
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400316 unregister_trace_sched_switch(probe_wakeup_sched_switch);
317 unregister_trace_sched_wakeup_new(probe_wakeup);
318 unregister_trace_sched_wakeup(probe_wakeup);
Steven Rostedt352ad252008-05-12 21:20:42 +0200319}
320
Ingo Molnare309b412008-05-12 21:20:51 +0200321static void wakeup_tracer_init(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200322{
323 wakeup_trace = tr;
324
325 if (tr->ctrl)
326 start_wakeup_tracer(tr);
327}
328
Ingo Molnare309b412008-05-12 21:20:51 +0200329static void wakeup_tracer_reset(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200330{
331 if (tr->ctrl) {
332 stop_wakeup_tracer(tr);
333 /* make sure we put back any tasks we are tracing */
334 wakeup_reset(tr);
335 }
336}
337
338static void wakeup_tracer_ctrl_update(struct trace_array *tr)
339{
340 if (tr->ctrl)
341 start_wakeup_tracer(tr);
342 else
343 stop_wakeup_tracer(tr);
344}
345
Ingo Molnare309b412008-05-12 21:20:51 +0200346static void wakeup_tracer_open(struct trace_iterator *iter)
Steven Rostedt352ad252008-05-12 21:20:42 +0200347{
348 /* stop the trace while dumping */
349 if (iter->tr->ctrl)
350 stop_wakeup_tracer(iter->tr);
351}
352
Ingo Molnare309b412008-05-12 21:20:51 +0200353static void wakeup_tracer_close(struct trace_iterator *iter)
Steven Rostedt352ad252008-05-12 21:20:42 +0200354{
355 /* forget about any processes we were recording */
356 if (iter->tr->ctrl)
357 start_wakeup_tracer(iter->tr);
358}
359
360static struct tracer wakeup_tracer __read_mostly =
361{
362 .name = "wakeup",
363 .init = wakeup_tracer_init,
364 .reset = wakeup_tracer_reset,
365 .open = wakeup_tracer_open,
366 .close = wakeup_tracer_close,
367 .ctrl_update = wakeup_tracer_ctrl_update,
368 .print_max = 1,
Steven Rostedt60a11772008-05-12 21:20:44 +0200369#ifdef CONFIG_FTRACE_SELFTEST
370 .selftest = trace_selftest_startup_wakeup,
371#endif
Steven Rostedt352ad252008-05-12 21:20:42 +0200372};
373
374__init static int init_wakeup_tracer(void)
375{
376 int ret;
377
378 ret = register_tracer(&wakeup_tracer);
379 if (ret)
380 return ret;
381
382 return 0;
383}
384device_initcall(init_wakeup_tracer);