blob: 3ae93f16b565de131887da94ee10835c183b2d6a [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 Rostedt7e18d8e2008-05-22 00:22:19 -040053 resched = need_resched();
54 preempt_disable_notrace();
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
Steven Rostedte59494f2008-07-16 00:13:45 -040062 local_irq_save(flags);
63 __raw_spin_lock(&wakeup_lock);
Steven Rostedt7e18d8e2008-05-22 00:22:19 -040064
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
Steven Rostedt38697052008-10-01 13:14:09 -040075 trace_function(tr, data, ip, parent_ip, flags, pc);
Steven Rostedt7e18d8e2008-05-22 00:22:19 -040076
77 unlock:
Steven Rostedte59494f2008-07-16 00:13:45 -040078 __raw_spin_unlock(&wakeup_lock);
79 local_irq_restore(flags);
Steven Rostedt7e18d8e2008-05-22 00:22:19 -040080
81 out:
82 atomic_dec(&data->disabled);
83
84 /*
85 * To prevent recursion from the scheduler, if the
86 * resched flag was set before we entered, then
87 * don't reschedule.
88 */
89 if (resched)
90 preempt_enable_no_resched_notrace();
91 else
92 preempt_enable_notrace();
93}
94
95static struct ftrace_ops trace_ops __read_mostly =
96{
97 .func = wakeup_tracer_call,
98};
Steven Rostedt606576c2008-10-06 19:06:12 -040099#endif /* CONFIG_FUNCTION_TRACER */
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400100
Steven Rostedt352ad252008-05-12 21:20:42 +0200101/*
102 * Should this new latency be reported/recorded?
103 */
Ingo Molnare309b412008-05-12 21:20:51 +0200104static int report_latency(cycle_t delta)
Steven Rostedt352ad252008-05-12 21:20:42 +0200105{
106 if (tracing_thresh) {
107 if (delta < tracing_thresh)
108 return 0;
109 } else {
110 if (delta <= tracing_max_latency)
111 return 0;
112 }
113 return 1;
114}
115
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200116static void notrace
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400117probe_wakeup_sched_switch(struct rq *rq, struct task_struct *prev,
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200118 struct task_struct *next)
Steven Rostedt352ad252008-05-12 21:20:42 +0200119{
120 unsigned long latency = 0, t0 = 0, t1 = 0;
Steven Rostedt352ad252008-05-12 21:20:42 +0200121 struct trace_array_cpu *data;
122 cycle_t T0, T1, delta;
123 unsigned long flags;
124 long disabled;
125 int cpu;
Steven Rostedt38697052008-10-01 13:14:09 -0400126 int pc;
Steven Rostedt352ad252008-05-12 21:20:42 +0200127
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400128 tracing_record_cmdline(prev);
129
Steven Rostedt352ad252008-05-12 21:20:42 +0200130 if (unlikely(!tracer_enabled))
131 return;
132
133 /*
134 * When we start a new trace, we set wakeup_task to NULL
135 * and then set tracer_enabled = 1. We want to make sure
136 * that another CPU does not see the tracer_enabled = 1
137 * and the wakeup_task with an older task, that might
138 * actually be the same as next.
139 */
140 smp_rmb();
141
142 if (next != wakeup_task)
143 return;
144
Steven Rostedt38697052008-10-01 13:14:09 -0400145 pc = preempt_count();
146
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400147 /* The task we are waiting for is waking up */
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400148 data = wakeup_trace->data[wakeup_cpu];
Steven Rostedt352ad252008-05-12 21:20:42 +0200149
150 /* disable local data, not wakeup_cpu data */
151 cpu = raw_smp_processor_id();
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400152 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
Steven Rostedt352ad252008-05-12 21:20:42 +0200153 if (likely(disabled != 1))
154 goto out;
155
Steven Rostedte59494f2008-07-16 00:13:45 -0400156 local_irq_save(flags);
157 __raw_spin_lock(&wakeup_lock);
Steven Rostedt352ad252008-05-12 21:20:42 +0200158
159 /* We could race with grabbing wakeup_lock */
160 if (unlikely(!tracer_enabled || next != wakeup_task))
161 goto out_unlock;
162
Steven Rostedt38697052008-10-01 13:14:09 -0400163 trace_function(wakeup_trace, data, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
Steven Rostedt352ad252008-05-12 21:20:42 +0200164
165 /*
166 * usecs conversion is slow so we try to delay the conversion
167 * as long as possible:
168 */
169 T0 = data->preempt_timestamp;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200170 T1 = ftrace_now(cpu);
Steven Rostedt352ad252008-05-12 21:20:42 +0200171 delta = T1-T0;
172
173 if (!report_latency(delta))
174 goto out_unlock;
175
176 latency = nsecs_to_usecs(delta);
177
178 tracing_max_latency = delta;
179 t0 = nsecs_to_usecs(T0);
180 t1 = nsecs_to_usecs(T1);
181
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400182 update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu);
Steven Rostedt352ad252008-05-12 21:20:42 +0200183
Steven Rostedt352ad252008-05-12 21:20:42 +0200184out_unlock:
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400185 __wakeup_reset(wakeup_trace);
Steven Rostedte59494f2008-07-16 00:13:45 -0400186 __raw_spin_unlock(&wakeup_lock);
187 local_irq_restore(flags);
Steven Rostedt352ad252008-05-12 21:20:42 +0200188out:
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400189 atomic_dec(&wakeup_trace->data[cpu]->disabled);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200190}
191
Ingo Molnare309b412008-05-12 21:20:51 +0200192static void __wakeup_reset(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200193{
194 struct trace_array_cpu *data;
195 int cpu;
196
Steven Rostedt352ad252008-05-12 21:20:42 +0200197 for_each_possible_cpu(cpu) {
198 data = tr->data[cpu];
Steven Rostedt3928a8a2008-09-29 23:02:41 -0400199 tracing_reset(tr, cpu);
Steven Rostedt352ad252008-05-12 21:20:42 +0200200 }
201
202 wakeup_cpu = -1;
203 wakeup_prio = -1;
204
205 if (wakeup_task)
206 put_task_struct(wakeup_task);
207
208 wakeup_task = NULL;
209}
210
Ingo Molnare309b412008-05-12 21:20:51 +0200211static void wakeup_reset(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200212{
213 unsigned long flags;
214
Steven Rostedte59494f2008-07-16 00:13:45 -0400215 local_irq_save(flags);
216 __raw_spin_lock(&wakeup_lock);
Steven Rostedt352ad252008-05-12 21:20:42 +0200217 __wakeup_reset(tr);
Steven Rostedte59494f2008-07-16 00:13:45 -0400218 __raw_spin_unlock(&wakeup_lock);
219 local_irq_restore(flags);
Steven Rostedt352ad252008-05-12 21:20:42 +0200220}
221
Ingo Molnare309b412008-05-12 21:20:51 +0200222static void
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400223probe_wakeup(struct rq *rq, struct task_struct *p)
Steven Rostedt352ad252008-05-12 21:20:42 +0200224{
225 int cpu = smp_processor_id();
226 unsigned long flags;
227 long disabled;
Steven Rostedt38697052008-10-01 13:14:09 -0400228 int pc;
Steven Rostedt352ad252008-05-12 21:20:42 +0200229
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400230 if (likely(!tracer_enabled))
Steven Rostedt352ad252008-05-12 21:20:42 +0200231 return;
232
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400233 tracing_record_cmdline(p);
234 tracing_record_cmdline(current);
235
236 if (likely(!rt_task(p)) ||
237 p->prio >= wakeup_prio ||
238 p->prio >= current->prio)
239 return;
240
Steven Rostedt38697052008-10-01 13:14:09 -0400241 pc = preempt_count();
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400242 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
Steven Rostedt352ad252008-05-12 21:20:42 +0200243 if (unlikely(disabled != 1))
244 goto out;
245
246 /* interrupts should be off from try_to_wake_up */
Steven Rostedte59494f2008-07-16 00:13:45 -0400247 __raw_spin_lock(&wakeup_lock);
Steven Rostedt352ad252008-05-12 21:20:42 +0200248
249 /* check for races. */
250 if (!tracer_enabled || p->prio >= wakeup_prio)
251 goto out_locked;
252
253 /* reset the trace */
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400254 __wakeup_reset(wakeup_trace);
Steven Rostedt352ad252008-05-12 21:20:42 +0200255
256 wakeup_cpu = task_cpu(p);
257 wakeup_prio = p->prio;
258
259 wakeup_task = p;
260 get_task_struct(wakeup_task);
261
262 local_save_flags(flags);
263
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400264 wakeup_trace->data[wakeup_cpu]->preempt_timestamp = ftrace_now(cpu);
265 trace_function(wakeup_trace, wakeup_trace->data[wakeup_cpu],
Steven Rostedt38697052008-10-01 13:14:09 -0400266 CALLER_ADDR1, CALLER_ADDR2, flags, pc);
Steven Rostedt352ad252008-05-12 21:20:42 +0200267
268out_locked:
Steven Rostedte59494f2008-07-16 00:13:45 -0400269 __raw_spin_unlock(&wakeup_lock);
Steven Rostedt352ad252008-05-12 21:20:42 +0200270out:
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400271 atomic_dec(&wakeup_trace->data[cpu]->disabled);
Steven Rostedt352ad252008-05-12 21:20:42 +0200272}
273
Ingo Molnare309b412008-05-12 21:20:51 +0200274static void start_wakeup_tracer(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200275{
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200276 int ret;
277
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400278 ret = register_trace_sched_wakeup(probe_wakeup);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200279 if (ret) {
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400280 pr_info("wakeup trace: Couldn't activate tracepoint"
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200281 " probe to kernel_sched_wakeup\n");
282 return;
283 }
284
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400285 ret = register_trace_sched_wakeup_new(probe_wakeup);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200286 if (ret) {
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400287 pr_info("wakeup trace: Couldn't activate tracepoint"
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200288 " probe to kernel_sched_wakeup_new\n");
289 goto fail_deprobe;
290 }
291
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400292 ret = register_trace_sched_switch(probe_wakeup_sched_switch);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200293 if (ret) {
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400294 pr_info("sched trace: Couldn't activate tracepoint"
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200295 " probe to kernel_sched_schedule\n");
296 goto fail_deprobe_wake_new;
297 }
298
Steven Rostedt352ad252008-05-12 21:20:42 +0200299 wakeup_reset(tr);
300
301 /*
302 * Don't let the tracer_enabled = 1 show up before
303 * the wakeup_task is reset. This may be overkill since
304 * wakeup_reset does a spin_unlock after setting the
305 * wakeup_task to NULL, but I want to be safe.
306 * This is a slow path anyway.
307 */
308 smp_wmb();
309
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400310 register_ftrace_function(&trace_ops);
Steven Rostedt352ad252008-05-12 21:20:42 +0200311
Steven Rostedtad591242008-07-10 20:58:13 -0400312 tracer_enabled = 1;
313
Steven Rostedt352ad252008-05-12 21:20:42 +0200314 return;
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200315fail_deprobe_wake_new:
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400316 unregister_trace_sched_wakeup_new(probe_wakeup);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200317fail_deprobe:
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400318 unregister_trace_sched_wakeup(probe_wakeup);
Steven Rostedt352ad252008-05-12 21:20:42 +0200319}
320
Ingo Molnare309b412008-05-12 21:20:51 +0200321static void stop_wakeup_tracer(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200322{
323 tracer_enabled = 0;
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400324 unregister_ftrace_function(&trace_ops);
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400325 unregister_trace_sched_switch(probe_wakeup_sched_switch);
326 unregister_trace_sched_wakeup_new(probe_wakeup);
327 unregister_trace_sched_wakeup(probe_wakeup);
Steven Rostedt352ad252008-05-12 21:20:42 +0200328}
329
Ingo Molnare309b412008-05-12 21:20:51 +0200330static void wakeup_tracer_init(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200331{
332 wakeup_trace = tr;
333
334 if (tr->ctrl)
335 start_wakeup_tracer(tr);
336}
337
Ingo Molnare309b412008-05-12 21:20:51 +0200338static void wakeup_tracer_reset(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200339{
340 if (tr->ctrl) {
341 stop_wakeup_tracer(tr);
342 /* make sure we put back any tasks we are tracing */
343 wakeup_reset(tr);
344 }
345}
346
347static void wakeup_tracer_ctrl_update(struct trace_array *tr)
348{
349 if (tr->ctrl)
350 start_wakeup_tracer(tr);
351 else
352 stop_wakeup_tracer(tr);
353}
354
Ingo Molnare309b412008-05-12 21:20:51 +0200355static void wakeup_tracer_open(struct trace_iterator *iter)
Steven Rostedt352ad252008-05-12 21:20:42 +0200356{
357 /* stop the trace while dumping */
358 if (iter->tr->ctrl)
359 stop_wakeup_tracer(iter->tr);
360}
361
Ingo Molnare309b412008-05-12 21:20:51 +0200362static void wakeup_tracer_close(struct trace_iterator *iter)
Steven Rostedt352ad252008-05-12 21:20:42 +0200363{
364 /* forget about any processes we were recording */
365 if (iter->tr->ctrl)
366 start_wakeup_tracer(iter->tr);
367}
368
369static struct tracer wakeup_tracer __read_mostly =
370{
371 .name = "wakeup",
372 .init = wakeup_tracer_init,
373 .reset = wakeup_tracer_reset,
374 .open = wakeup_tracer_open,
375 .close = wakeup_tracer_close,
376 .ctrl_update = wakeup_tracer_ctrl_update,
377 .print_max = 1,
Steven Rostedt60a11772008-05-12 21:20:44 +0200378#ifdef CONFIG_FTRACE_SELFTEST
379 .selftest = trace_selftest_startup_wakeup,
380#endif
Steven Rostedt352ad252008-05-12 21:20:42 +0200381};
382
383__init static int init_wakeup_tracer(void)
384{
385 int ret;
386
387 ret = register_tracer(&wakeup_tracer);
388 if (ret)
389 return ret;
390
391 return 0;
392}
393device_initcall(init_wakeup_tracer);