blob: 19bd8928ce944da6232c1f63708d723a2563020f [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
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010010 * Copyright (C) 2004 Nadia Yvette Chambers
Steven Rostedt352ad252008-05-12 21:20:42 +020011 */
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>
Clark Williams8bd75c72013-02-07 09:47:07 -060018#include <linux/sched/rt.h>
Dario Faggioli2d3d8912013-11-07 14:43:44 +010019#include <linux/sched/deadline.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040020#include <trace/events/sched.h>
Steven Rostedt352ad252008-05-12 21:20:42 +020021#include "trace.h"
22
23static struct trace_array *wakeup_trace;
24static int __read_mostly tracer_enabled;
25
26static struct task_struct *wakeup_task;
27static int wakeup_cpu;
Steven Rostedt478142c32009-09-09 10:36:01 -040028static int wakeup_current_cpu;
Steven Rostedt352ad252008-05-12 21:20:42 +020029static unsigned wakeup_prio = -1;
Steven Rostedt32443512009-01-21 16:24:46 -050030static int wakeup_rt;
Dario Faggioliaf6ace72013-11-07 14:43:42 +010031static int wakeup_dl;
32static int tracing_dl = 0;
Steven Rostedt352ad252008-05-12 21:20:42 +020033
Thomas Gleixner445c8952009-12-02 19:49:50 +010034static arch_spinlock_t wakeup_lock =
Thomas Gleixneredc35bd2009-12-03 12:38:57 +010035 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
Steven Rostedt352ad252008-05-12 21:20:42 +020036
Jiri Olsa7495a5b2010-09-23 14:00:53 +020037static void wakeup_reset(struct trace_array *tr);
Ingo Molnare309b412008-05-12 21:20:51 +020038static void __wakeup_reset(struct trace_array *tr);
Jiri Olsa7495a5b2010-09-23 14:00:53 +020039static int wakeup_graph_entry(struct ftrace_graph_ent *trace);
40static void wakeup_graph_return(struct ftrace_graph_ret *trace);
Steven Rostedt352ad252008-05-12 21:20:42 +020041
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -040042static int save_flags;
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -040043static bool function_enabled;
Steven Rostedte9d25fe2009-03-04 22:15:30 -050044
Jiri Olsa7495a5b2010-09-23 14:00:53 +020045#define TRACE_DISPLAY_GRAPH 1
46
47static struct tracer_opt trace_opts[] = {
48#ifdef CONFIG_FUNCTION_GRAPH_TRACER
49 /* display latency trace as call graph */
50 { TRACER_OPT(display-graph, TRACE_DISPLAY_GRAPH) },
51#endif
52 { } /* Empty entry */
53};
54
55static struct tracer_flags tracer_flags = {
56 .val = 0,
57 .opts = trace_opts,
58};
59
60#define is_graph() (tracer_flags.val & TRACE_DISPLAY_GRAPH)
61
Steven Rostedt606576c2008-10-06 19:06:12 -040062#ifdef CONFIG_FUNCTION_TRACER
Steven Rostedt542181d2010-10-05 16:38:49 -040063
64/*
65 * Prologue for the wakeup function tracers.
66 *
67 * Returns 1 if it is OK to continue, and preemption
68 * is disabled and data->disabled is incremented.
69 * 0 if the trace is to be ignored, and preemption
70 * is not disabled and data->disabled is
71 * kept the same.
72 *
73 * Note, this function is also used outside this ifdef but
74 * inside the #ifdef of the function graph tracer below.
75 * This is OK, since the function graph tracer is
76 * dependent on the function tracer.
77 */
78static int
79func_prolog_preempt_disable(struct trace_array *tr,
80 struct trace_array_cpu **data,
81 int *pc)
82{
83 long disabled;
84 int cpu;
85
86 if (likely(!wakeup_task))
87 return 0;
88
89 *pc = preempt_count();
90 preempt_disable_notrace();
91
92 cpu = raw_smp_processor_id();
93 if (cpu != wakeup_current_cpu)
94 goto out_enable;
95
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -050096 *data = per_cpu_ptr(tr->trace_buffer.data, cpu);
Steven Rostedt542181d2010-10-05 16:38:49 -040097 disabled = atomic_inc_return(&(*data)->disabled);
98 if (unlikely(disabled != 1))
99 goto out;
100
101 return 1;
102
103out:
104 atomic_dec(&(*data)->disabled);
105
106out_enable:
107 preempt_enable_notrace();
108 return 0;
109}
110
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400111/*
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200112 * wakeup uses its own tracer function to keep the overhead down:
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400113 */
114static void
Steven Rostedta1e2e312011-08-09 12:50:46 -0400115wakeup_tracer_call(unsigned long ip, unsigned long parent_ip,
116 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400117{
118 struct trace_array *tr = wakeup_trace;
119 struct trace_array_cpu *data;
120 unsigned long flags;
Steven Rostedt38697052008-10-01 13:14:09 -0400121 int pc;
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400122
Steven Rostedt542181d2010-10-05 16:38:49 -0400123 if (!func_prolog_preempt_disable(tr, &data, &pc))
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400124 return;
125
Steven Rostedte59494f2008-07-16 00:13:45 -0400126 local_irq_save(flags);
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500127 trace_function(tr, ip, parent_ip, flags, pc);
Steven Rostedte59494f2008-07-16 00:13:45 -0400128 local_irq_restore(flags);
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400129
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400130 atomic_dec(&data->disabled);
Steven Rostedt5168ae52010-06-03 09:36:50 -0400131 preempt_enable_notrace();
Steven Rostedt7e18d8e2008-05-22 00:22:19 -0400132}
Steven Rostedt7e407982010-10-19 10:56:19 -0400133#endif /* CONFIG_FUNCTION_TRACER */
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200134
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500135static int register_wakeup_function(struct trace_array *tr, int graph, int set)
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400136{
137 int ret;
138
139 /* 'set' is set if TRACE_ITER_FUNCTION is about to be set */
140 if (function_enabled || (!set && !(trace_flags & TRACE_ITER_FUNCTION)))
141 return 0;
142
143 if (graph)
144 ret = register_ftrace_graph(&wakeup_graph_return,
145 &wakeup_graph_entry);
146 else
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500147 ret = register_ftrace_function(tr->ops);
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400148
149 if (!ret)
150 function_enabled = true;
151
152 return ret;
153}
154
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500155static void unregister_wakeup_function(struct trace_array *tr, int graph)
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400156{
157 if (!function_enabled)
158 return;
159
160 if (graph)
161 unregister_ftrace_graph();
162 else
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500163 unregister_ftrace_function(tr->ops);
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400164
165 function_enabled = false;
166}
167
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500168static void wakeup_function_set(struct trace_array *tr, int set)
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400169{
170 if (set)
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500171 register_wakeup_function(tr, is_graph(), 1);
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400172 else
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500173 unregister_wakeup_function(tr, is_graph());
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400174}
175
Steven Rostedt (Red Hat)bf6065b2014-01-10 17:51:01 -0500176static int wakeup_flag_changed(struct trace_array *tr, u32 mask, int set)
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400177{
Steven Rostedt (Red Hat)bf6065b2014-01-10 17:51:01 -0500178 struct tracer *tracer = tr->current_trace;
179
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400180 if (mask & TRACE_ITER_FUNCTION)
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500181 wakeup_function_set(tr, set);
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400182
183 return trace_keep_overwrite(tracer, mask, set);
184}
185
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500186static int start_func_tracer(struct trace_array *tr, int graph)
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200187{
188 int ret;
189
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500190 ret = register_wakeup_function(tr, graph, 0);
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200191
192 if (!ret && tracing_is_enabled())
193 tracer_enabled = 1;
194 else
195 tracer_enabled = 0;
196
197 return ret;
198}
199
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500200static void stop_func_tracer(struct trace_array *tr, int graph)
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200201{
202 tracer_enabled = 0;
203
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500204 unregister_wakeup_function(tr, graph);
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200205}
206
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200207#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -0500208static int
209wakeup_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200210{
211
212 if (!(bit & TRACE_DISPLAY_GRAPH))
213 return -EINVAL;
214
215 if (!(is_graph() ^ set))
216 return 0;
217
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500218 stop_func_tracer(tr, !set);
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200219
220 wakeup_reset(wakeup_trace);
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500221 tr->max_latency = 0;
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200222
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500223 return start_func_tracer(tr, set);
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200224}
225
226static int wakeup_graph_entry(struct ftrace_graph_ent *trace)
227{
228 struct trace_array *tr = wakeup_trace;
229 struct trace_array_cpu *data;
230 unsigned long flags;
Steven Rostedt542181d2010-10-05 16:38:49 -0400231 int pc, ret = 0;
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200232
Steven Rostedt542181d2010-10-05 16:38:49 -0400233 if (!func_prolog_preempt_disable(tr, &data, &pc))
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200234 return 0;
235
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200236 local_save_flags(flags);
237 ret = __trace_graph_entry(tr, trace, flags, pc);
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200238 atomic_dec(&data->disabled);
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200239 preempt_enable_notrace();
Steven Rostedt542181d2010-10-05 16:38:49 -0400240
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200241 return ret;
242}
243
244static void wakeup_graph_return(struct ftrace_graph_ret *trace)
245{
246 struct trace_array *tr = wakeup_trace;
247 struct trace_array_cpu *data;
248 unsigned long flags;
Steven Rostedt542181d2010-10-05 16:38:49 -0400249 int pc;
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200250
Steven Rostedt542181d2010-10-05 16:38:49 -0400251 if (!func_prolog_preempt_disable(tr, &data, &pc))
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200252 return;
253
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200254 local_save_flags(flags);
255 __trace_graph_return(tr, trace, flags, pc);
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200256 atomic_dec(&data->disabled);
257
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200258 preempt_enable_notrace();
259 return;
260}
261
262static void wakeup_trace_open(struct trace_iterator *iter)
263{
264 if (is_graph())
265 graph_trace_open(iter);
266}
267
268static void wakeup_trace_close(struct trace_iterator *iter)
269{
270 if (iter->private)
271 graph_trace_close(iter);
272}
273
Jiri Olsa321e68b2011-06-03 16:58:47 +0200274#define GRAPH_TRACER_FLAGS (TRACE_GRAPH_PRINT_PROC | \
275 TRACE_GRAPH_PRINT_ABS_TIME | \
276 TRACE_GRAPH_PRINT_DURATION)
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200277
278static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
279{
280 /*
281 * In graph mode call the graph tracer output function,
282 * otherwise go with the TRACE_FN event handler
283 */
284 if (is_graph())
285 return print_graph_function_flags(iter, GRAPH_TRACER_FLAGS);
286
287 return TRACE_TYPE_UNHANDLED;
288}
289
290static void wakeup_print_header(struct seq_file *s)
291{
292 if (is_graph())
293 print_graph_headers_flags(s, GRAPH_TRACER_FLAGS);
294 else
295 trace_default_header(s);
296}
297
298static void
299__trace_function(struct trace_array *tr,
300 unsigned long ip, unsigned long parent_ip,
301 unsigned long flags, int pc)
302{
303 if (is_graph())
304 trace_graph_function(tr, ip, parent_ip, flags, pc);
305 else
306 trace_function(tr, ip, parent_ip, flags, pc);
307}
308#else
309#define __trace_function trace_function
310
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -0500311static int
312wakeup_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200313{
314 return -EINVAL;
315}
316
317static int wakeup_graph_entry(struct ftrace_graph_ent *trace)
318{
319 return -1;
320}
321
322static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
323{
324 return TRACE_TYPE_UNHANDLED;
325}
326
327static void wakeup_graph_return(struct ftrace_graph_ret *trace) { }
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200328static void wakeup_trace_open(struct trace_iterator *iter) { }
329static void wakeup_trace_close(struct trace_iterator *iter) { }
Jiri Olsa7e9a49e2011-11-07 16:08:49 +0100330
331#ifdef CONFIG_FUNCTION_TRACER
332static void wakeup_print_header(struct seq_file *s)
333{
334 trace_default_header(s);
335}
336#else
337static void wakeup_print_header(struct seq_file *s)
338{
339 trace_latency_header(s);
340}
341#endif /* CONFIG_FUNCTION_TRACER */
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200342#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
343
Steven Rostedt352ad252008-05-12 21:20:42 +0200344/*
345 * Should this new latency be reported/recorded?
346 */
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500347static int report_latency(struct trace_array *tr, cycle_t delta)
Steven Rostedt352ad252008-05-12 21:20:42 +0200348{
349 if (tracing_thresh) {
350 if (delta < tracing_thresh)
351 return 0;
352 } else {
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500353 if (delta <= tr->max_latency)
Steven Rostedt352ad252008-05-12 21:20:42 +0200354 return 0;
355 }
356 return 1;
357}
358
Steven Rostedt38516ab2010-04-20 17:04:50 -0400359static void
360probe_wakeup_migrate_task(void *ignore, struct task_struct *task, int cpu)
Steven Rostedt478142c32009-09-09 10:36:01 -0400361{
362 if (task != wakeup_task)
363 return;
364
365 wakeup_current_cpu = cpu;
366}
367
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200368static void notrace
Steven Rostedt38516ab2010-04-20 17:04:50 -0400369probe_wakeup_sched_switch(void *ignore,
370 struct task_struct *prev, struct task_struct *next)
Steven Rostedt352ad252008-05-12 21:20:42 +0200371{
Steven Rostedt352ad252008-05-12 21:20:42 +0200372 struct trace_array_cpu *data;
373 cycle_t T0, T1, delta;
374 unsigned long flags;
375 long disabled;
376 int cpu;
Steven Rostedt38697052008-10-01 13:14:09 -0400377 int pc;
Steven Rostedt352ad252008-05-12 21:20:42 +0200378
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400379 tracing_record_cmdline(prev);
380
Steven Rostedt352ad252008-05-12 21:20:42 +0200381 if (unlikely(!tracer_enabled))
382 return;
383
384 /*
385 * When we start a new trace, we set wakeup_task to NULL
386 * and then set tracer_enabled = 1. We want to make sure
387 * that another CPU does not see the tracer_enabled = 1
388 * and the wakeup_task with an older task, that might
389 * actually be the same as next.
390 */
391 smp_rmb();
392
393 if (next != wakeup_task)
394 return;
395
Steven Rostedt38697052008-10-01 13:14:09 -0400396 pc = preempt_count();
397
Steven Rostedt352ad252008-05-12 21:20:42 +0200398 /* disable local data, not wakeup_cpu data */
399 cpu = raw_smp_processor_id();
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500400 disabled = atomic_inc_return(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
Steven Rostedt352ad252008-05-12 21:20:42 +0200401 if (likely(disabled != 1))
402 goto out;
403
Steven Rostedte59494f2008-07-16 00:13:45 -0400404 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100405 arch_spin_lock(&wakeup_lock);
Steven Rostedt352ad252008-05-12 21:20:42 +0200406
407 /* We could race with grabbing wakeup_lock */
408 if (unlikely(!tracer_enabled || next != wakeup_task))
409 goto out_unlock;
410
Steven Rostedt9be24412009-03-26 10:25:24 -0400411 /* The task we are waiting for is waking up */
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500412 data = per_cpu_ptr(wakeup_trace->trace_buffer.data, wakeup_cpu);
Steven Rostedt9be24412009-03-26 10:25:24 -0400413
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200414 __trace_function(wakeup_trace, CALLER_ADDR0, CALLER_ADDR1, flags, pc);
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500415 tracing_sched_switch_trace(wakeup_trace, prev, next, flags, pc);
Steven Rostedt352ad252008-05-12 21:20:42 +0200416
Steven Rostedt352ad252008-05-12 21:20:42 +0200417 T0 = data->preempt_timestamp;
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200418 T1 = ftrace_now(cpu);
Steven Rostedt352ad252008-05-12 21:20:42 +0200419 delta = T1-T0;
420
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500421 if (!report_latency(wakeup_trace, delta))
Steven Rostedt352ad252008-05-12 21:20:42 +0200422 goto out_unlock;
423
Carsten Emdeb5130b12009-09-13 01:43:07 +0200424 if (likely(!is_tracing_stopped())) {
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500425 wakeup_trace->max_latency = delta;
Carsten Emdeb5130b12009-09-13 01:43:07 +0200426 update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu);
427 }
Steven Rostedt352ad252008-05-12 21:20:42 +0200428
Steven Rostedt352ad252008-05-12 21:20:42 +0200429out_unlock:
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400430 __wakeup_reset(wakeup_trace);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100431 arch_spin_unlock(&wakeup_lock);
Steven Rostedte59494f2008-07-16 00:13:45 -0400432 local_irq_restore(flags);
Steven Rostedt352ad252008-05-12 21:20:42 +0200433out:
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500434 atomic_dec(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200435}
436
Ingo Molnare309b412008-05-12 21:20:51 +0200437static void __wakeup_reset(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200438{
Steven Rostedt352ad252008-05-12 21:20:42 +0200439 wakeup_cpu = -1;
440 wakeup_prio = -1;
Dario Faggioliaf6ace72013-11-07 14:43:42 +0100441 tracing_dl = 0;
Steven Rostedt352ad252008-05-12 21:20:42 +0200442
443 if (wakeup_task)
444 put_task_struct(wakeup_task);
445
446 wakeup_task = NULL;
447}
448
Ingo Molnare309b412008-05-12 21:20:51 +0200449static void wakeup_reset(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200450{
451 unsigned long flags;
452
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500453 tracing_reset_online_cpus(&tr->trace_buffer);
Steven Rostedt2f26ebd2009-09-01 11:06:29 -0400454
Steven Rostedte59494f2008-07-16 00:13:45 -0400455 local_irq_save(flags);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100456 arch_spin_lock(&wakeup_lock);
Steven Rostedt352ad252008-05-12 21:20:42 +0200457 __wakeup_reset(tr);
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100458 arch_spin_unlock(&wakeup_lock);
Steven Rostedte59494f2008-07-16 00:13:45 -0400459 local_irq_restore(flags);
Steven Rostedt352ad252008-05-12 21:20:42 +0200460}
461
Ingo Molnare309b412008-05-12 21:20:51 +0200462static void
Steven Rostedt38516ab2010-04-20 17:04:50 -0400463probe_wakeup(void *ignore, struct task_struct *p, int success)
Steven Rostedt352ad252008-05-12 21:20:42 +0200464{
Steven Rostedtf8ec1062009-01-21 17:17:04 -0500465 struct trace_array_cpu *data;
Steven Rostedt352ad252008-05-12 21:20:42 +0200466 int cpu = smp_processor_id();
467 unsigned long flags;
468 long disabled;
Steven Rostedt38697052008-10-01 13:14:09 -0400469 int pc;
Steven Rostedt352ad252008-05-12 21:20:42 +0200470
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400471 if (likely(!tracer_enabled))
Steven Rostedt352ad252008-05-12 21:20:42 +0200472 return;
473
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400474 tracing_record_cmdline(p);
475 tracing_record_cmdline(current);
476
Dario Faggioliaf6ace72013-11-07 14:43:42 +0100477 /*
478 * Semantic is like this:
479 * - wakeup tracer handles all tasks in the system, independently
480 * from their scheduling class;
481 * - wakeup_rt tracer handles tasks belonging to sched_dl and
482 * sched_rt class;
483 * - wakeup_dl handles tasks belonging to sched_dl class only.
484 */
485 if (tracing_dl || (wakeup_dl && !dl_task(p)) ||
486 (wakeup_rt && !dl_task(p) && !rt_task(p)) ||
487 (!dl_task(p) && (p->prio >= wakeup_prio || p->prio >= current->prio)))
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400488 return;
489
Steven Rostedt38697052008-10-01 13:14:09 -0400490 pc = preempt_count();
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500491 disabled = atomic_inc_return(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
Steven Rostedt352ad252008-05-12 21:20:42 +0200492 if (unlikely(disabled != 1))
493 goto out;
494
495 /* interrupts should be off from try_to_wake_up */
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100496 arch_spin_lock(&wakeup_lock);
Steven Rostedt352ad252008-05-12 21:20:42 +0200497
498 /* check for races. */
Dario Faggioliaf6ace72013-11-07 14:43:42 +0100499 if (!tracer_enabled || tracing_dl ||
500 (!dl_task(p) && p->prio >= wakeup_prio))
Steven Rostedt352ad252008-05-12 21:20:42 +0200501 goto out_locked;
502
503 /* reset the trace */
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400504 __wakeup_reset(wakeup_trace);
Steven Rostedt352ad252008-05-12 21:20:42 +0200505
506 wakeup_cpu = task_cpu(p);
Steven Rostedt478142c32009-09-09 10:36:01 -0400507 wakeup_current_cpu = wakeup_cpu;
Steven Rostedt352ad252008-05-12 21:20:42 +0200508 wakeup_prio = p->prio;
509
Dario Faggioliaf6ace72013-11-07 14:43:42 +0100510 /*
511 * Once you start tracing a -deadline task, don't bother tracing
512 * another task until the first one wakes up.
513 */
514 if (dl_task(p))
515 tracing_dl = 1;
516 else
517 tracing_dl = 0;
518
Steven Rostedt352ad252008-05-12 21:20:42 +0200519 wakeup_task = p;
520 get_task_struct(wakeup_task);
521
522 local_save_flags(flags);
523
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500524 data = per_cpu_ptr(wakeup_trace->trace_buffer.data, wakeup_cpu);
Steven Rostedtf8ec1062009-01-21 17:17:04 -0500525 data->preempt_timestamp = ftrace_now(cpu);
Arnaldo Carvalho de Melo7be42152009-02-05 01:13:37 -0500526 tracing_sched_wakeup_trace(wakeup_trace, p, current, flags, pc);
Steven Rostedt301fd742009-04-03 11:12:23 -0400527
528 /*
529 * We must be careful in using CALLER_ADDR2. But since wake_up
530 * is not called by an assembly function (where as schedule is)
531 * it should be safe to use it here.
532 */
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200533 __trace_function(wakeup_trace, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
Steven Rostedt352ad252008-05-12 21:20:42 +0200534
535out_locked:
Thomas Gleixner0199c4e2009-12-02 20:01:25 +0100536 arch_spin_unlock(&wakeup_lock);
Steven Rostedt352ad252008-05-12 21:20:42 +0200537out:
Steven Rostedt (Red Hat)12883ef2013-03-05 09:24:35 -0500538 atomic_dec(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
Steven Rostedt352ad252008-05-12 21:20:42 +0200539}
540
Ingo Molnare309b412008-05-12 21:20:51 +0200541static void start_wakeup_tracer(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200542{
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200543 int ret;
544
Steven Rostedt38516ab2010-04-20 17:04:50 -0400545 ret = register_trace_sched_wakeup(probe_wakeup, NULL);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200546 if (ret) {
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400547 pr_info("wakeup trace: Couldn't activate tracepoint"
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200548 " probe to kernel_sched_wakeup\n");
549 return;
550 }
551
Steven Rostedt38516ab2010-04-20 17:04:50 -0400552 ret = register_trace_sched_wakeup_new(probe_wakeup, NULL);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200553 if (ret) {
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400554 pr_info("wakeup trace: Couldn't activate tracepoint"
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200555 " probe to kernel_sched_wakeup_new\n");
556 goto fail_deprobe;
557 }
558
Steven Rostedt38516ab2010-04-20 17:04:50 -0400559 ret = register_trace_sched_switch(probe_wakeup_sched_switch, NULL);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200560 if (ret) {
Mathieu Desnoyersb07c3f12008-07-18 12:16:17 -0400561 pr_info("sched trace: Couldn't activate tracepoint"
Wenji Huang73d8b8b2009-02-17 01:10:02 -0500562 " probe to kernel_sched_switch\n");
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200563 goto fail_deprobe_wake_new;
564 }
565
Steven Rostedt38516ab2010-04-20 17:04:50 -0400566 ret = register_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
Steven Rostedt478142c32009-09-09 10:36:01 -0400567 if (ret) {
568 pr_info("wakeup trace: Couldn't activate tracepoint"
569 " probe to kernel_sched_migrate_task\n");
570 return;
571 }
572
Steven Rostedt352ad252008-05-12 21:20:42 +0200573 wakeup_reset(tr);
574
575 /*
576 * Don't let the tracer_enabled = 1 show up before
577 * the wakeup_task is reset. This may be overkill since
578 * wakeup_reset does a spin_unlock after setting the
579 * wakeup_task to NULL, but I want to be safe.
580 * This is a slow path anyway.
581 */
582 smp_wmb();
583
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500584 if (start_func_tracer(tr, is_graph()))
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200585 printk(KERN_ERR "failed to start wakeup tracer\n");
Steven Rostedtad591242008-07-10 20:58:13 -0400586
Steven Rostedt352ad252008-05-12 21:20:42 +0200587 return;
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200588fail_deprobe_wake_new:
Steven Rostedt38516ab2010-04-20 17:04:50 -0400589 unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
Mathieu Desnoyers5b82a1b2008-05-12 21:21:10 +0200590fail_deprobe:
Steven Rostedt38516ab2010-04-20 17:04:50 -0400591 unregister_trace_sched_wakeup(probe_wakeup, NULL);
Steven Rostedt352ad252008-05-12 21:20:42 +0200592}
593
Ingo Molnare309b412008-05-12 21:20:51 +0200594static void stop_wakeup_tracer(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200595{
596 tracer_enabled = 0;
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500597 stop_func_tracer(tr, is_graph());
Steven Rostedt38516ab2010-04-20 17:04:50 -0400598 unregister_trace_sched_switch(probe_wakeup_sched_switch, NULL);
599 unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
600 unregister_trace_sched_wakeup(probe_wakeup, NULL);
601 unregister_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
Steven Rostedt352ad252008-05-12 21:20:42 +0200602}
603
Steven Rostedt (Red Hat)65daaca72014-01-14 07:06:29 -0500604static bool wakeup_busy;
605
Steven Rostedt32443512009-01-21 16:24:46 -0500606static int __wakeup_tracer_init(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200607{
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -0400608 save_flags = trace_flags;
609
610 /* non overwrite screws up the latency tracers */
Steven Rostedt2b6080f2012-05-11 13:29:49 -0400611 set_tracer_flag(tr, TRACE_ITER_OVERWRITE, 1);
612 set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, 1);
Steven Rostedte9d25fe2009-03-04 22:15:30 -0500613
Steven Rostedt (Red Hat)6d9b3fa2014-01-14 11:28:38 -0500614 tr->max_latency = 0;
Steven Rostedt352ad252008-05-12 21:20:42 +0200615 wakeup_trace = tr;
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500616 ftrace_init_array_ops(tr, wakeup_tracer_call);
Steven Rostedtc76f0692008-11-07 22:36:02 -0500617 start_wakeup_tracer(tr);
Steven Rostedt (Red Hat)65daaca72014-01-14 07:06:29 -0500618
619 wakeup_busy = true;
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100620 return 0;
Steven Rostedt352ad252008-05-12 21:20:42 +0200621}
622
Steven Rostedt32443512009-01-21 16:24:46 -0500623static int wakeup_tracer_init(struct trace_array *tr)
624{
Steven Rostedt (Red Hat)65daaca72014-01-14 07:06:29 -0500625 if (wakeup_busy)
626 return -EBUSY;
627
Dario Faggioliaf6ace72013-11-07 14:43:42 +0100628 wakeup_dl = 0;
Steven Rostedt32443512009-01-21 16:24:46 -0500629 wakeup_rt = 0;
630 return __wakeup_tracer_init(tr);
631}
632
633static int wakeup_rt_tracer_init(struct trace_array *tr)
634{
Steven Rostedt (Red Hat)65daaca72014-01-14 07:06:29 -0500635 if (wakeup_busy)
636 return -EBUSY;
637
Dario Faggioliaf6ace72013-11-07 14:43:42 +0100638 wakeup_dl = 0;
Steven Rostedt32443512009-01-21 16:24:46 -0500639 wakeup_rt = 1;
640 return __wakeup_tracer_init(tr);
641}
642
Dario Faggioliaf6ace72013-11-07 14:43:42 +0100643static int wakeup_dl_tracer_init(struct trace_array *tr)
644{
Steven Rostedt (Red Hat)65daaca72014-01-14 07:06:29 -0500645 if (wakeup_busy)
646 return -EBUSY;
647
Dario Faggioliaf6ace72013-11-07 14:43:42 +0100648 wakeup_dl = 1;
649 wakeup_rt = 0;
650 return __wakeup_tracer_init(tr);
651}
652
Ingo Molnare309b412008-05-12 21:20:51 +0200653static void wakeup_tracer_reset(struct trace_array *tr)
Steven Rostedt352ad252008-05-12 21:20:42 +0200654{
Steven Rostedt (Red Hat)613f04a2013-03-14 15:03:53 -0400655 int lat_flag = save_flags & TRACE_ITER_LATENCY_FMT;
656 int overwrite_flag = save_flags & TRACE_ITER_OVERWRITE;
657
Steven Rostedtc76f0692008-11-07 22:36:02 -0500658 stop_wakeup_tracer(tr);
659 /* make sure we put back any tasks we are tracing */
660 wakeup_reset(tr);
Steven Rostedte9d25fe2009-03-04 22:15:30 -0500661
Steven Rostedt2b6080f2012-05-11 13:29:49 -0400662 set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, lat_flag);
663 set_tracer_flag(tr, TRACE_ITER_OVERWRITE, overwrite_flag);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500664 ftrace_reset_array_ops(tr);
Steven Rostedt (Red Hat)65daaca72014-01-14 07:06:29 -0500665 wakeup_busy = false;
Steven Rostedt352ad252008-05-12 21:20:42 +0200666}
667
Steven Rostedt90369902008-11-05 16:05:44 -0500668static void wakeup_tracer_start(struct trace_array *tr)
669{
670 wakeup_reset(tr);
671 tracer_enabled = 1;
Steven Rostedt90369902008-11-05 16:05:44 -0500672}
673
674static void wakeup_tracer_stop(struct trace_array *tr)
675{
676 tracer_enabled = 0;
Steven Rostedt352ad252008-05-12 21:20:42 +0200677}
678
679static struct tracer wakeup_tracer __read_mostly =
680{
681 .name = "wakeup",
682 .init = wakeup_tracer_init,
683 .reset = wakeup_tracer_reset,
Steven Rostedt90369902008-11-05 16:05:44 -0500684 .start = wakeup_tracer_start,
685 .stop = wakeup_tracer_stop,
Hiraku Toyookaf43c7382012-10-02 17:27:10 +0900686 .print_max = true,
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200687 .print_header = wakeup_print_header,
688 .print_line = wakeup_print_line,
689 .flags = &tracer_flags,
690 .set_flag = wakeup_set_flag,
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400691 .flag_changed = wakeup_flag_changed,
Steven Rostedt60a11772008-05-12 21:20:44 +0200692#ifdef CONFIG_FTRACE_SELFTEST
693 .selftest = trace_selftest_startup_wakeup,
694#endif
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200695 .open = wakeup_trace_open,
696 .close = wakeup_trace_close,
Steven Rostedt (Red Hat)65daaca72014-01-14 07:06:29 -0500697 .allow_instances = true,
Hiraku Toyookaf43c7382012-10-02 17:27:10 +0900698 .use_max_tr = true,
Steven Rostedt352ad252008-05-12 21:20:42 +0200699};
700
Steven Rostedt32443512009-01-21 16:24:46 -0500701static struct tracer wakeup_rt_tracer __read_mostly =
702{
703 .name = "wakeup_rt",
704 .init = wakeup_rt_tracer_init,
705 .reset = wakeup_tracer_reset,
706 .start = wakeup_tracer_start,
707 .stop = wakeup_tracer_stop,
Hiraku Toyookaf43c7382012-10-02 17:27:10 +0900708 .print_max = true,
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200709 .print_header = wakeup_print_header,
710 .print_line = wakeup_print_line,
711 .flags = &tracer_flags,
712 .set_flag = wakeup_set_flag,
Steven Rostedt (Red Hat)328df472013-03-14 12:10:40 -0400713 .flag_changed = wakeup_flag_changed,
Steven Rostedt32443512009-01-21 16:24:46 -0500714#ifdef CONFIG_FTRACE_SELFTEST
715 .selftest = trace_selftest_startup_wakeup,
716#endif
Jiri Olsa7495a5b2010-09-23 14:00:53 +0200717 .open = wakeup_trace_open,
718 .close = wakeup_trace_close,
Steven Rostedt (Red Hat)65daaca72014-01-14 07:06:29 -0500719 .allow_instances = true,
Hiraku Toyookaf43c7382012-10-02 17:27:10 +0900720 .use_max_tr = true,
Steven Rostedt32443512009-01-21 16:24:46 -0500721};
722
Dario Faggioliaf6ace72013-11-07 14:43:42 +0100723static struct tracer wakeup_dl_tracer __read_mostly =
724{
725 .name = "wakeup_dl",
726 .init = wakeup_dl_tracer_init,
727 .reset = wakeup_tracer_reset,
728 .start = wakeup_tracer_start,
729 .stop = wakeup_tracer_stop,
Dario Faggioliaf6ace72013-11-07 14:43:42 +0100730 .print_max = true,
731 .print_header = wakeup_print_header,
732 .print_line = wakeup_print_line,
733 .flags = &tracer_flags,
734 .set_flag = wakeup_set_flag,
735 .flag_changed = wakeup_flag_changed,
736#ifdef CONFIG_FTRACE_SELFTEST
737 .selftest = trace_selftest_startup_wakeup,
738#endif
739 .open = wakeup_trace_open,
740 .close = wakeup_trace_close,
741 .use_max_tr = true,
742};
743
Steven Rostedt352ad252008-05-12 21:20:42 +0200744__init static int init_wakeup_tracer(void)
745{
746 int ret;
747
748 ret = register_tracer(&wakeup_tracer);
749 if (ret)
750 return ret;
751
Steven Rostedt32443512009-01-21 16:24:46 -0500752 ret = register_tracer(&wakeup_rt_tracer);
753 if (ret)
754 return ret;
755
Dario Faggioliaf6ace72013-11-07 14:43:42 +0100756 ret = register_tracer(&wakeup_dl_tracer);
757 if (ret)
758 return ret;
759
Steven Rostedt352ad252008-05-12 21:20:42 +0200760 return 0;
761}
Steven Rostedt6f415672012-10-05 12:13:07 -0400762core_initcall(init_wakeup_tracer);