ring-buffer: do not swap if recording is disabled
[deliverable/linux.git] / kernel / trace / trace_sched_wakeup.c
CommitLineData
352ad25a
SR
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>
b07c3f19 18#include <trace/sched.h>
352ad25a
SR
19
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
e59494f4
SR
29static raw_spinlock_t wakeup_lock =
30 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
352ad25a 31
e309b41d 32static void __wakeup_reset(struct trace_array *tr);
352ad25a 33
606576ce 34#ifdef CONFIG_FUNCTION_TRACER
7e18d8e7
SR
35/*
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;
38697053 47 int pc;
7e18d8e7
SR
48
49 if (likely(!wakeup_task))
50 return;
51
38697053 52 pc = preempt_count();
182e9f5f 53 resched = ftrace_preempt_disable();
7e18d8e7
SR
54
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
e59494f4
SR
61 local_irq_save(flags);
62 __raw_spin_lock(&wakeup_lock);
7e18d8e7
SR
63
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
38697053 74 trace_function(tr, data, ip, parent_ip, flags, pc);
7e18d8e7
SR
75
76 unlock:
e59494f4
SR
77 __raw_spin_unlock(&wakeup_lock);
78 local_irq_restore(flags);
7e18d8e7
SR
79
80 out:
81 atomic_dec(&data->disabled);
82
182e9f5f 83 ftrace_preempt_enable(resched);
7e18d8e7
SR
84}
85
86static struct ftrace_ops trace_ops __read_mostly =
87{
88 .func = wakeup_tracer_call,
89};
606576ce 90#endif /* CONFIG_FUNCTION_TRACER */
7e18d8e7 91
352ad25a
SR
92/*
93 * Should this new latency be reported/recorded?
94 */
e309b41d 95static int report_latency(cycle_t delta)
352ad25a
SR
96{
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
5b82a1b0 107static void notrace
b07c3f19 108probe_wakeup_sched_switch(struct rq *rq, struct task_struct *prev,
5b82a1b0 109 struct task_struct *next)
352ad25a
SR
110{
111 unsigned long latency = 0, t0 = 0, t1 = 0;
352ad25a
SR
112 struct trace_array_cpu *data;
113 cycle_t T0, T1, delta;
114 unsigned long flags;
115 long disabled;
116 int cpu;
38697053 117 int pc;
352ad25a 118
b07c3f19
MD
119 tracing_record_cmdline(prev);
120
352ad25a
SR
121 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
38697053
SR
136 pc = preempt_count();
137
7e18d8e7 138 /* The task we are waiting for is waking up */
b07c3f19 139 data = wakeup_trace->data[wakeup_cpu];
352ad25a
SR
140
141 /* disable local data, not wakeup_cpu data */
142 cpu = raw_smp_processor_id();
b07c3f19 143 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
352ad25a
SR
144 if (likely(disabled != 1))
145 goto out;
146
e59494f4
SR
147 local_irq_save(flags);
148 __raw_spin_lock(&wakeup_lock);
352ad25a
SR
149
150 /* We could race with grabbing wakeup_lock */
151 if (unlikely(!tracer_enabled || next != wakeup_task))
152 goto out_unlock;
153
38697053 154 trace_function(wakeup_trace, data, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
352ad25a
SR
155
156 /*
157 * usecs conversion is slow so we try to delay the conversion
158 * as long as possible:
159 */
160 T0 = data->preempt_timestamp;
750ed1a4 161 T1 = ftrace_now(cpu);
352ad25a
SR
162 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
b07c3f19 173 update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu);
352ad25a 174
352ad25a 175out_unlock:
b07c3f19 176 __wakeup_reset(wakeup_trace);
e59494f4
SR
177 __raw_spin_unlock(&wakeup_lock);
178 local_irq_restore(flags);
352ad25a 179out:
b07c3f19 180 atomic_dec(&wakeup_trace->data[cpu]->disabled);
5b82a1b0
MD
181}
182
e309b41d 183static void __wakeup_reset(struct trace_array *tr)
352ad25a
SR
184{
185 struct trace_array_cpu *data;
186 int cpu;
187
352ad25a
SR
188 for_each_possible_cpu(cpu) {
189 data = tr->data[cpu];
3928a8a2 190 tracing_reset(tr, cpu);
352ad25a
SR
191 }
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
e309b41d 202static void wakeup_reset(struct trace_array *tr)
352ad25a
SR
203{
204 unsigned long flags;
205
e59494f4
SR
206 local_irq_save(flags);
207 __raw_spin_lock(&wakeup_lock);
352ad25a 208 __wakeup_reset(tr);
e59494f4
SR
209 __raw_spin_unlock(&wakeup_lock);
210 local_irq_restore(flags);
352ad25a
SR
211}
212
e309b41d 213static void
468a15bb 214probe_wakeup(struct rq *rq, struct task_struct *p, int success)
352ad25a
SR
215{
216 int cpu = smp_processor_id();
217 unsigned long flags;
218 long disabled;
38697053 219 int pc;
352ad25a 220
b07c3f19
MD
221 if (likely(!tracer_enabled))
222 return;
223
224 tracing_record_cmdline(p);
225 tracing_record_cmdline(current);
226
352ad25a
SR
227 if (likely(!rt_task(p)) ||
228 p->prio >= wakeup_prio ||
b07c3f19 229 p->prio >= current->prio)
352ad25a
SR
230 return;
231
38697053 232 pc = preempt_count();
b07c3f19 233 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
352ad25a
SR
234 if (unlikely(disabled != 1))
235 goto out;
236
237 /* interrupts should be off from try_to_wake_up */
e59494f4 238 __raw_spin_lock(&wakeup_lock);
352ad25a
SR
239
240 /* check for races. */
241 if (!tracer_enabled || p->prio >= wakeup_prio)
242 goto out_locked;
243
244 /* reset the trace */
b07c3f19 245 __wakeup_reset(wakeup_trace);
352ad25a
SR
246
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
b07c3f19
MD
255 wakeup_trace->data[wakeup_cpu]->preempt_timestamp = ftrace_now(cpu);
256 trace_function(wakeup_trace, wakeup_trace->data[wakeup_cpu],
38697053 257 CALLER_ADDR1, CALLER_ADDR2, flags, pc);
352ad25a
SR
258
259out_locked:
e59494f4 260 __raw_spin_unlock(&wakeup_lock);
352ad25a 261out:
b07c3f19 262 atomic_dec(&wakeup_trace->data[cpu]->disabled);
352ad25a
SR
263}
264
e309b41d 265static void start_wakeup_tracer(struct trace_array *tr)
352ad25a 266{
5b82a1b0
MD
267 int ret;
268
b07c3f19 269 ret = register_trace_sched_wakeup(probe_wakeup);
5b82a1b0 270 if (ret) {
b07c3f19 271 pr_info("wakeup trace: Couldn't activate tracepoint"
5b82a1b0
MD
272 " probe to kernel_sched_wakeup\n");
273 return;
274 }
275
b07c3f19 276 ret = register_trace_sched_wakeup_new(probe_wakeup);
5b82a1b0 277 if (ret) {
b07c3f19 278 pr_info("wakeup trace: Couldn't activate tracepoint"
5b82a1b0
MD
279 " probe to kernel_sched_wakeup_new\n");
280 goto fail_deprobe;
281 }
282
b07c3f19 283 ret = register_trace_sched_switch(probe_wakeup_sched_switch);
5b82a1b0 284 if (ret) {
b07c3f19 285 pr_info("sched trace: Couldn't activate tracepoint"
5b82a1b0
MD
286 " probe to kernel_sched_schedule\n");
287 goto fail_deprobe_wake_new;
288 }
289
352ad25a
SR
290 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
7e18d8e7 301 register_ftrace_function(&trace_ops);
352ad25a 302
5bc4564b 303 if (tracing_is_enabled())
9036990d 304 tracer_enabled = 1;
5bc4564b 305 else
9036990d 306 tracer_enabled = 0;
ad591240 307
352ad25a 308 return;
5b82a1b0 309fail_deprobe_wake_new:
b07c3f19 310 unregister_trace_sched_wakeup_new(probe_wakeup);
5b82a1b0 311fail_deprobe:
b07c3f19 312 unregister_trace_sched_wakeup(probe_wakeup);
352ad25a
SR
313}
314
e309b41d 315static void stop_wakeup_tracer(struct trace_array *tr)
352ad25a
SR
316{
317 tracer_enabled = 0;
7e18d8e7 318 unregister_ftrace_function(&trace_ops);
b07c3f19
MD
319 unregister_trace_sched_switch(probe_wakeup_sched_switch);
320 unregister_trace_sched_wakeup_new(probe_wakeup);
321 unregister_trace_sched_wakeup(probe_wakeup);
352ad25a
SR
322}
323
1c80025a 324static int wakeup_tracer_init(struct trace_array *tr)
352ad25a 325{
745b1626 326 tracing_max_latency = 0;
352ad25a 327 wakeup_trace = tr;
c76f0694 328 start_wakeup_tracer(tr);
1c80025a 329 return 0;
352ad25a
SR
330}
331
e309b41d 332static void wakeup_tracer_reset(struct trace_array *tr)
352ad25a 333{
c76f0694
SR
334 stop_wakeup_tracer(tr);
335 /* make sure we put back any tasks we are tracing */
336 wakeup_reset(tr);
352ad25a
SR
337}
338
9036990d
SR
339static void wakeup_tracer_start(struct trace_array *tr)
340{
341 wakeup_reset(tr);
342 tracer_enabled = 1;
9036990d
SR
343}
344
345static void wakeup_tracer_stop(struct trace_array *tr)
346{
347 tracer_enabled = 0;
352ad25a
SR
348}
349
350static struct tracer wakeup_tracer __read_mostly =
351{
352 .name = "wakeup",
353 .init = wakeup_tracer_init,
354 .reset = wakeup_tracer_reset,
9036990d
SR
355 .start = wakeup_tracer_start,
356 .stop = wakeup_tracer_stop,
352ad25a 357 .print_max = 1,
60a11774
SR
358#ifdef CONFIG_FTRACE_SELFTEST
359 .selftest = trace_selftest_startup_wakeup,
360#endif
352ad25a
SR
361};
362
363__init static int init_wakeup_tracer(void)
364{
365 int ret;
366
367 ret = register_tracer(&wakeup_tracer);
368 if (ret)
369 return ret;
370
371 return 0;
372}
373device_initcall(init_wakeup_tracer);
This page took 0.07381 seconds and 5 git commands to generate.