]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/trace/trace_sched_wakeup.c
xps: Transmit Packet Steering
[net-next-2.6.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>
ad8d75ff 18#include <trace/events/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;
478142c3 27static int wakeup_current_cpu;
352ad25a 28static unsigned wakeup_prio = -1;
3244351c 29static int wakeup_rt;
352ad25a 30
445c8951 31static arch_spinlock_t wakeup_lock =
edc35bd7 32 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
352ad25a 33
7495a5be 34static void wakeup_reset(struct trace_array *tr);
e309b41d 35static void __wakeup_reset(struct trace_array *tr);
7495a5be
JO
36static int wakeup_graph_entry(struct ftrace_graph_ent *trace);
37static void wakeup_graph_return(struct ftrace_graph_ret *trace);
352ad25a 38
e9d25fe6
SR
39static int save_lat_flag;
40
7495a5be
JO
41#define TRACE_DISPLAY_GRAPH 1
42
43static struct tracer_opt trace_opts[] = {
44#ifdef CONFIG_FUNCTION_GRAPH_TRACER
45 /* display latency trace as call graph */
46 { TRACER_OPT(display-graph, TRACE_DISPLAY_GRAPH) },
47#endif
48 { } /* Empty entry */
49};
50
51static struct tracer_flags tracer_flags = {
52 .val = 0,
53 .opts = trace_opts,
54};
55
56#define is_graph() (tracer_flags.val & TRACE_DISPLAY_GRAPH)
57
606576ce 58#ifdef CONFIG_FUNCTION_TRACER
542181d3 59
7e18d8e7 60/*
542181d3
SR
61 * Prologue for the wakeup function tracers.
62 *
63 * Returns 1 if it is OK to continue, and preemption
64 * is disabled and data->disabled is incremented.
65 * 0 if the trace is to be ignored, and preemption
66 * is not disabled and data->disabled is
67 * kept the same.
68 *
69 * Note, this function is also used outside this ifdef but
70 * inside the #ifdef of the function graph tracer below.
71 * This is OK, since the function graph tracer is
72 * dependent on the function tracer.
7e18d8e7 73 */
542181d3
SR
74static int
75func_prolog_preempt_disable(struct trace_array *tr,
76 struct trace_array_cpu **data,
77 int *pc)
7e18d8e7 78{
7e18d8e7 79 long disabled;
7e18d8e7
SR
80 int cpu;
81
82 if (likely(!wakeup_task))
542181d3 83 return 0;
7e18d8e7 84
542181d3 85 *pc = preempt_count();
5168ae50 86 preempt_disable_notrace();
7e18d8e7
SR
87
88 cpu = raw_smp_processor_id();
478142c3
SR
89 if (cpu != wakeup_current_cpu)
90 goto out_enable;
91
542181d3
SR
92 *data = tr->data[cpu];
93 disabled = atomic_inc_return(&(*data)->disabled);
7e18d8e7
SR
94 if (unlikely(disabled != 1))
95 goto out;
96
542181d3 97 return 1;
7e18d8e7 98
542181d3
SR
99out:
100 atomic_dec(&(*data)->disabled);
7e18d8e7 101
542181d3
SR
102out_enable:
103 preempt_enable_notrace();
104 return 0;
105}
106
107/*
108 * wakeup uses its own tracer function to keep the overhead down:
109 */
110static void
111wakeup_tracer_call(unsigned long ip, unsigned long parent_ip)
112{
113 struct trace_array *tr = wakeup_trace;
114 struct trace_array_cpu *data;
115 unsigned long flags;
116 int pc;
117
118 if (!func_prolog_preempt_disable(tr, &data, &pc))
119 return;
120
121 local_irq_save(flags);
122 trace_function(tr, ip, parent_ip, flags, pc);
e59494f4 123 local_irq_restore(flags);
7e18d8e7 124
7e18d8e7 125 atomic_dec(&data->disabled);
5168ae50 126 preempt_enable_notrace();
7e18d8e7
SR
127}
128
129static struct ftrace_ops trace_ops __read_mostly =
130{
131 .func = wakeup_tracer_call,
132};
7e40798f 133#endif /* CONFIG_FUNCTION_TRACER */
7495a5be
JO
134
135static int start_func_tracer(int graph)
136{
137 int ret;
138
139 if (!graph)
140 ret = register_ftrace_function(&trace_ops);
141 else
142 ret = register_ftrace_graph(&wakeup_graph_return,
143 &wakeup_graph_entry);
144
145 if (!ret && tracing_is_enabled())
146 tracer_enabled = 1;
147 else
148 tracer_enabled = 0;
149
150 return ret;
151}
152
153static void stop_func_tracer(int graph)
154{
155 tracer_enabled = 0;
156
157 if (!graph)
158 unregister_ftrace_function(&trace_ops);
159 else
160 unregister_ftrace_graph();
161}
162
7495a5be
JO
163#ifdef CONFIG_FUNCTION_GRAPH_TRACER
164static int wakeup_set_flag(u32 old_flags, u32 bit, int set)
165{
166
167 if (!(bit & TRACE_DISPLAY_GRAPH))
168 return -EINVAL;
169
170 if (!(is_graph() ^ set))
171 return 0;
172
173 stop_func_tracer(!set);
174
175 wakeup_reset(wakeup_trace);
176 tracing_max_latency = 0;
177
178 return start_func_tracer(set);
179}
180
181static int wakeup_graph_entry(struct ftrace_graph_ent *trace)
182{
183 struct trace_array *tr = wakeup_trace;
184 struct trace_array_cpu *data;
185 unsigned long flags;
542181d3 186 int pc, ret = 0;
7495a5be 187
542181d3 188 if (!func_prolog_preempt_disable(tr, &data, &pc))
7495a5be
JO
189 return 0;
190
7495a5be
JO
191 local_save_flags(flags);
192 ret = __trace_graph_entry(tr, trace, flags, pc);
7495a5be 193 atomic_dec(&data->disabled);
7495a5be 194 preempt_enable_notrace();
542181d3 195
7495a5be
JO
196 return ret;
197}
198
199static void wakeup_graph_return(struct ftrace_graph_ret *trace)
200{
201 struct trace_array *tr = wakeup_trace;
202 struct trace_array_cpu *data;
203 unsigned long flags;
542181d3 204 int pc;
7495a5be 205
542181d3 206 if (!func_prolog_preempt_disable(tr, &data, &pc))
7495a5be
JO
207 return;
208
7495a5be
JO
209 local_save_flags(flags);
210 __trace_graph_return(tr, trace, flags, pc);
7495a5be
JO
211 atomic_dec(&data->disabled);
212
7495a5be
JO
213 preempt_enable_notrace();
214 return;
215}
216
217static void wakeup_trace_open(struct trace_iterator *iter)
218{
219 if (is_graph())
220 graph_trace_open(iter);
221}
222
223static void wakeup_trace_close(struct trace_iterator *iter)
224{
225 if (iter->private)
226 graph_trace_close(iter);
227}
228
229#define GRAPH_TRACER_FLAGS (TRACE_GRAPH_PRINT_PROC)
230
231static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
232{
233 /*
234 * In graph mode call the graph tracer output function,
235 * otherwise go with the TRACE_FN event handler
236 */
237 if (is_graph())
238 return print_graph_function_flags(iter, GRAPH_TRACER_FLAGS);
239
240 return TRACE_TYPE_UNHANDLED;
241}
242
243static void wakeup_print_header(struct seq_file *s)
244{
245 if (is_graph())
246 print_graph_headers_flags(s, GRAPH_TRACER_FLAGS);
247 else
248 trace_default_header(s);
249}
250
251static void
252__trace_function(struct trace_array *tr,
253 unsigned long ip, unsigned long parent_ip,
254 unsigned long flags, int pc)
255{
256 if (is_graph())
257 trace_graph_function(tr, ip, parent_ip, flags, pc);
258 else
259 trace_function(tr, ip, parent_ip, flags, pc);
260}
261#else
262#define __trace_function trace_function
263
264static int wakeup_set_flag(u32 old_flags, u32 bit, int set)
265{
266 return -EINVAL;
267}
268
269static int wakeup_graph_entry(struct ftrace_graph_ent *trace)
270{
271 return -1;
272}
273
274static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
275{
276 return TRACE_TYPE_UNHANDLED;
277}
278
279static void wakeup_graph_return(struct ftrace_graph_ret *trace) { }
280static void wakeup_print_header(struct seq_file *s) { }
281static void wakeup_trace_open(struct trace_iterator *iter) { }
282static void wakeup_trace_close(struct trace_iterator *iter) { }
283#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
284
352ad25a
SR
285/*
286 * Should this new latency be reported/recorded?
287 */
e309b41d 288static int report_latency(cycle_t delta)
352ad25a
SR
289{
290 if (tracing_thresh) {
291 if (delta < tracing_thresh)
292 return 0;
293 } else {
294 if (delta <= tracing_max_latency)
295 return 0;
296 }
297 return 1;
298}
299
38516ab5
SR
300static void
301probe_wakeup_migrate_task(void *ignore, struct task_struct *task, int cpu)
478142c3
SR
302{
303 if (task != wakeup_task)
304 return;
305
306 wakeup_current_cpu = cpu;
307}
308
5b82a1b0 309static void notrace
38516ab5
SR
310probe_wakeup_sched_switch(void *ignore,
311 struct task_struct *prev, struct task_struct *next)
352ad25a 312{
352ad25a
SR
313 struct trace_array_cpu *data;
314 cycle_t T0, T1, delta;
315 unsigned long flags;
316 long disabled;
317 int cpu;
38697053 318 int pc;
352ad25a 319
b07c3f19
MD
320 tracing_record_cmdline(prev);
321
352ad25a
SR
322 if (unlikely(!tracer_enabled))
323 return;
324
325 /*
326 * When we start a new trace, we set wakeup_task to NULL
327 * and then set tracer_enabled = 1. We want to make sure
328 * that another CPU does not see the tracer_enabled = 1
329 * and the wakeup_task with an older task, that might
330 * actually be the same as next.
331 */
332 smp_rmb();
333
334 if (next != wakeup_task)
335 return;
336
38697053
SR
337 pc = preempt_count();
338
352ad25a
SR
339 /* disable local data, not wakeup_cpu data */
340 cpu = raw_smp_processor_id();
b07c3f19 341 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
352ad25a
SR
342 if (likely(disabled != 1))
343 goto out;
344
e59494f4 345 local_irq_save(flags);
0199c4e6 346 arch_spin_lock(&wakeup_lock);
352ad25a
SR
347
348 /* We could race with grabbing wakeup_lock */
349 if (unlikely(!tracer_enabled || next != wakeup_task))
350 goto out_unlock;
351
9be24414
SR
352 /* The task we are waiting for is waking up */
353 data = wakeup_trace->data[wakeup_cpu];
354
7495a5be 355 __trace_function(wakeup_trace, CALLER_ADDR0, CALLER_ADDR1, flags, pc);
7be42151 356 tracing_sched_switch_trace(wakeup_trace, prev, next, flags, pc);
352ad25a 357
352ad25a 358 T0 = data->preempt_timestamp;
750ed1a4 359 T1 = ftrace_now(cpu);
352ad25a
SR
360 delta = T1-T0;
361
362 if (!report_latency(delta))
363 goto out_unlock;
364
b5130b1e
CE
365 if (likely(!is_tracing_stopped())) {
366 tracing_max_latency = delta;
367 update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu);
368 }
352ad25a 369
352ad25a 370out_unlock:
b07c3f19 371 __wakeup_reset(wakeup_trace);
0199c4e6 372 arch_spin_unlock(&wakeup_lock);
e59494f4 373 local_irq_restore(flags);
352ad25a 374out:
b07c3f19 375 atomic_dec(&wakeup_trace->data[cpu]->disabled);
5b82a1b0
MD
376}
377
e309b41d 378static void __wakeup_reset(struct trace_array *tr)
352ad25a 379{
352ad25a
SR
380 wakeup_cpu = -1;
381 wakeup_prio = -1;
382
383 if (wakeup_task)
384 put_task_struct(wakeup_task);
385
386 wakeup_task = NULL;
387}
388
e309b41d 389static void wakeup_reset(struct trace_array *tr)
352ad25a
SR
390{
391 unsigned long flags;
392
2f26ebd5
SR
393 tracing_reset_online_cpus(tr);
394
e59494f4 395 local_irq_save(flags);
0199c4e6 396 arch_spin_lock(&wakeup_lock);
352ad25a 397 __wakeup_reset(tr);
0199c4e6 398 arch_spin_unlock(&wakeup_lock);
e59494f4 399 local_irq_restore(flags);
352ad25a
SR
400}
401
e309b41d 402static void
38516ab5 403probe_wakeup(void *ignore, struct task_struct *p, int success)
352ad25a 404{
f8ec1062 405 struct trace_array_cpu *data;
352ad25a
SR
406 int cpu = smp_processor_id();
407 unsigned long flags;
408 long disabled;
38697053 409 int pc;
352ad25a 410
b07c3f19
MD
411 if (likely(!tracer_enabled))
412 return;
413
414 tracing_record_cmdline(p);
415 tracing_record_cmdline(current);
416
3244351c 417 if ((wakeup_rt && !rt_task(p)) ||
352ad25a 418 p->prio >= wakeup_prio ||
b07c3f19 419 p->prio >= current->prio)
352ad25a
SR
420 return;
421
38697053 422 pc = preempt_count();
b07c3f19 423 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
352ad25a
SR
424 if (unlikely(disabled != 1))
425 goto out;
426
427 /* interrupts should be off from try_to_wake_up */
0199c4e6 428 arch_spin_lock(&wakeup_lock);
352ad25a
SR
429
430 /* check for races. */
431 if (!tracer_enabled || p->prio >= wakeup_prio)
432 goto out_locked;
433
434 /* reset the trace */
b07c3f19 435 __wakeup_reset(wakeup_trace);
352ad25a
SR
436
437 wakeup_cpu = task_cpu(p);
478142c3 438 wakeup_current_cpu = wakeup_cpu;
352ad25a
SR
439 wakeup_prio = p->prio;
440
441 wakeup_task = p;
442 get_task_struct(wakeup_task);
443
444 local_save_flags(flags);
445
f8ec1062
SR
446 data = wakeup_trace->data[wakeup_cpu];
447 data->preempt_timestamp = ftrace_now(cpu);
7be42151 448 tracing_sched_wakeup_trace(wakeup_trace, p, current, flags, pc);
301fd748
SR
449
450 /*
451 * We must be careful in using CALLER_ADDR2. But since wake_up
452 * is not called by an assembly function (where as schedule is)
453 * it should be safe to use it here.
454 */
7495a5be 455 __trace_function(wakeup_trace, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
352ad25a
SR
456
457out_locked:
0199c4e6 458 arch_spin_unlock(&wakeup_lock);
352ad25a 459out:
b07c3f19 460 atomic_dec(&wakeup_trace->data[cpu]->disabled);
352ad25a
SR
461}
462
e309b41d 463static void start_wakeup_tracer(struct trace_array *tr)
352ad25a 464{
5b82a1b0
MD
465 int ret;
466
38516ab5 467 ret = register_trace_sched_wakeup(probe_wakeup, NULL);
5b82a1b0 468 if (ret) {
b07c3f19 469 pr_info("wakeup trace: Couldn't activate tracepoint"
5b82a1b0
MD
470 " probe to kernel_sched_wakeup\n");
471 return;
472 }
473
38516ab5 474 ret = register_trace_sched_wakeup_new(probe_wakeup, NULL);
5b82a1b0 475 if (ret) {
b07c3f19 476 pr_info("wakeup trace: Couldn't activate tracepoint"
5b82a1b0
MD
477 " probe to kernel_sched_wakeup_new\n");
478 goto fail_deprobe;
479 }
480
38516ab5 481 ret = register_trace_sched_switch(probe_wakeup_sched_switch, NULL);
5b82a1b0 482 if (ret) {
b07c3f19 483 pr_info("sched trace: Couldn't activate tracepoint"
73d8b8bc 484 " probe to kernel_sched_switch\n");
5b82a1b0
MD
485 goto fail_deprobe_wake_new;
486 }
487
38516ab5 488 ret = register_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
478142c3
SR
489 if (ret) {
490 pr_info("wakeup trace: Couldn't activate tracepoint"
491 " probe to kernel_sched_migrate_task\n");
492 return;
493 }
494
352ad25a
SR
495 wakeup_reset(tr);
496
497 /*
498 * Don't let the tracer_enabled = 1 show up before
499 * the wakeup_task is reset. This may be overkill since
500 * wakeup_reset does a spin_unlock after setting the
501 * wakeup_task to NULL, but I want to be safe.
502 * This is a slow path anyway.
503 */
504 smp_wmb();
505
7495a5be
JO
506 if (start_func_tracer(is_graph()))
507 printk(KERN_ERR "failed to start wakeup tracer\n");
ad591240 508
352ad25a 509 return;
5b82a1b0 510fail_deprobe_wake_new:
38516ab5 511 unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
5b82a1b0 512fail_deprobe:
38516ab5 513 unregister_trace_sched_wakeup(probe_wakeup, NULL);
352ad25a
SR
514}
515
e309b41d 516static void stop_wakeup_tracer(struct trace_array *tr)
352ad25a
SR
517{
518 tracer_enabled = 0;
7495a5be 519 stop_func_tracer(is_graph());
38516ab5
SR
520 unregister_trace_sched_switch(probe_wakeup_sched_switch, NULL);
521 unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
522 unregister_trace_sched_wakeup(probe_wakeup, NULL);
523 unregister_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
352ad25a
SR
524}
525
3244351c 526static int __wakeup_tracer_init(struct trace_array *tr)
352ad25a 527{
e9d25fe6
SR
528 save_lat_flag = trace_flags & TRACE_ITER_LATENCY_FMT;
529 trace_flags |= TRACE_ITER_LATENCY_FMT;
530
745b1626 531 tracing_max_latency = 0;
352ad25a 532 wakeup_trace = tr;
c76f0694 533 start_wakeup_tracer(tr);
1c80025a 534 return 0;
352ad25a
SR
535}
536
3244351c
SR
537static int wakeup_tracer_init(struct trace_array *tr)
538{
539 wakeup_rt = 0;
540 return __wakeup_tracer_init(tr);
541}
542
543static int wakeup_rt_tracer_init(struct trace_array *tr)
544{
545 wakeup_rt = 1;
546 return __wakeup_tracer_init(tr);
547}
548
e309b41d 549static void wakeup_tracer_reset(struct trace_array *tr)
352ad25a 550{
c76f0694
SR
551 stop_wakeup_tracer(tr);
552 /* make sure we put back any tasks we are tracing */
553 wakeup_reset(tr);
e9d25fe6
SR
554
555 if (!save_lat_flag)
556 trace_flags &= ~TRACE_ITER_LATENCY_FMT;
352ad25a
SR
557}
558
9036990d
SR
559static void wakeup_tracer_start(struct trace_array *tr)
560{
561 wakeup_reset(tr);
562 tracer_enabled = 1;
9036990d
SR
563}
564
565static void wakeup_tracer_stop(struct trace_array *tr)
566{
567 tracer_enabled = 0;
352ad25a
SR
568}
569
570static struct tracer wakeup_tracer __read_mostly =
571{
572 .name = "wakeup",
573 .init = wakeup_tracer_init,
574 .reset = wakeup_tracer_reset,
9036990d
SR
575 .start = wakeup_tracer_start,
576 .stop = wakeup_tracer_stop,
352ad25a 577 .print_max = 1,
7495a5be
JO
578 .print_header = wakeup_print_header,
579 .print_line = wakeup_print_line,
580 .flags = &tracer_flags,
581 .set_flag = wakeup_set_flag,
60a11774
SR
582#ifdef CONFIG_FTRACE_SELFTEST
583 .selftest = trace_selftest_startup_wakeup,
584#endif
7495a5be
JO
585 .open = wakeup_trace_open,
586 .close = wakeup_trace_close,
ef710e10 587 .use_max_tr = 1,
352ad25a
SR
588};
589
3244351c
SR
590static struct tracer wakeup_rt_tracer __read_mostly =
591{
592 .name = "wakeup_rt",
593 .init = wakeup_rt_tracer_init,
594 .reset = wakeup_tracer_reset,
595 .start = wakeup_tracer_start,
596 .stop = wakeup_tracer_stop,
6eaaa5d5 597 .wait_pipe = poll_wait_pipe,
3244351c 598 .print_max = 1,
7495a5be
JO
599 .print_header = wakeup_print_header,
600 .print_line = wakeup_print_line,
601 .flags = &tracer_flags,
602 .set_flag = wakeup_set_flag,
3244351c
SR
603#ifdef CONFIG_FTRACE_SELFTEST
604 .selftest = trace_selftest_startup_wakeup,
605#endif
7495a5be
JO
606 .open = wakeup_trace_open,
607 .close = wakeup_trace_close,
ef710e10 608 .use_max_tr = 1,
3244351c
SR
609};
610
352ad25a
SR
611__init static int init_wakeup_tracer(void)
612{
613 int ret;
614
615 ret = register_tracer(&wakeup_tracer);
616 if (ret)
617 return ret;
618
3244351c
SR
619 ret = register_tracer(&wakeup_rt_tracer);
620 if (ret)
621 return ret;
622
352ad25a
SR
623 return 0;
624}
625device_initcall(init_wakeup_tracer);