2 * trace task wakeup timings
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Based on code from the latency_tracer, that is:
9 * Copyright (C) 2004-2006 Ingo Molnar
10 * Copyright (C) 2004 Nadia Yvette Chambers
12 #include <linux/module.h>
14 #include <linux/debugfs.h>
15 #include <linux/kallsyms.h>
16 #include <linux/uaccess.h>
17 #include <linux/ftrace.h>
18 #include <linux/sched/rt.h>
19 #include <linux/sched/deadline.h>
20 #include <trace/events/sched.h>
23 static struct trace_array *wakeup_trace;
24 static int __read_mostly tracer_enabled;
26 static struct task_struct *wakeup_task;
27 static int wakeup_cpu;
28 static int wakeup_current_cpu;
29 static unsigned wakeup_prio = -1;
32 static int tracing_dl = 0;
34 static arch_spinlock_t wakeup_lock =
35 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
37 static void wakeup_reset(struct trace_array *tr);
38 static void __wakeup_reset(struct trace_array *tr);
39 static int wakeup_graph_entry(struct ftrace_graph_ent *trace);
40 static void wakeup_graph_return(struct ftrace_graph_ret *trace);
42 static int save_flags;
43 static bool function_enabled;
45 #define TRACE_DISPLAY_GRAPH 1
47 static 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) },
55 static struct tracer_flags tracer_flags = {
60 #define is_graph() (tracer_flags.val & TRACE_DISPLAY_GRAPH)
62 #ifdef CONFIG_FUNCTION_TRACER
65 * Prologue for the wakeup function tracers.
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
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.
79 func_prolog_preempt_disable(struct trace_array *tr,
80 struct trace_array_cpu **data,
86 if (likely(!wakeup_task))
89 *pc = preempt_count();
90 preempt_disable_notrace();
92 cpu = raw_smp_processor_id();
93 if (cpu != wakeup_current_cpu)
96 *data = per_cpu_ptr(tr->trace_buffer.data, cpu);
97 disabled = atomic_inc_return(&(*data)->disabled);
98 if (unlikely(disabled != 1))
104 atomic_dec(&(*data)->disabled);
107 preempt_enable_notrace();
112 * wakeup uses its own tracer function to keep the overhead down:
115 wakeup_tracer_call(unsigned long ip, unsigned long parent_ip,
116 struct ftrace_ops *op, struct pt_regs *pt_regs)
118 struct trace_array *tr = wakeup_trace;
119 struct trace_array_cpu *data;
123 if (!func_prolog_preempt_disable(tr, &data, &pc))
126 local_irq_save(flags);
127 trace_function(tr, ip, parent_ip, flags, pc);
128 local_irq_restore(flags);
130 atomic_dec(&data->disabled);
131 preempt_enable_notrace();
134 static struct ftrace_ops trace_ops __read_mostly =
136 .func = wakeup_tracer_call,
137 .flags = FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_RECURSION_SAFE,
139 #endif /* CONFIG_FUNCTION_TRACER */
141 static int register_wakeup_function(int graph, int set)
145 /* 'set' is set if TRACE_ITER_FUNCTION is about to be set */
146 if (function_enabled || (!set && !(trace_flags & TRACE_ITER_FUNCTION)))
150 ret = register_ftrace_graph(&wakeup_graph_return,
151 &wakeup_graph_entry);
153 ret = register_ftrace_function(&trace_ops);
156 function_enabled = true;
161 static void unregister_wakeup_function(int graph)
163 if (!function_enabled)
167 unregister_ftrace_graph();
169 unregister_ftrace_function(&trace_ops);
171 function_enabled = false;
174 static void wakeup_function_set(int set)
177 register_wakeup_function(is_graph(), 1);
179 unregister_wakeup_function(is_graph());
182 static int wakeup_flag_changed(struct tracer *tracer, u32 mask, int set)
184 if (mask & TRACE_ITER_FUNCTION)
185 wakeup_function_set(set);
187 return trace_keep_overwrite(tracer, mask, set);
190 static int start_func_tracer(int graph)
194 ret = register_wakeup_function(graph, 0);
196 if (!ret && tracing_is_enabled())
204 static void stop_func_tracer(int graph)
208 unregister_wakeup_function(graph);
211 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
213 wakeup_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
216 if (!(bit & TRACE_DISPLAY_GRAPH))
219 if (!(is_graph() ^ set))
222 stop_func_tracer(!set);
224 wakeup_reset(wakeup_trace);
225 tracing_max_latency = 0;
227 return start_func_tracer(set);
230 static int wakeup_graph_entry(struct ftrace_graph_ent *trace)
232 struct trace_array *tr = wakeup_trace;
233 struct trace_array_cpu *data;
237 if (!func_prolog_preempt_disable(tr, &data, &pc))
240 local_save_flags(flags);
241 ret = __trace_graph_entry(tr, trace, flags, pc);
242 atomic_dec(&data->disabled);
243 preempt_enable_notrace();
248 static void wakeup_graph_return(struct ftrace_graph_ret *trace)
250 struct trace_array *tr = wakeup_trace;
251 struct trace_array_cpu *data;
255 if (!func_prolog_preempt_disable(tr, &data, &pc))
258 local_save_flags(flags);
259 __trace_graph_return(tr, trace, flags, pc);
260 atomic_dec(&data->disabled);
262 preempt_enable_notrace();
266 static void wakeup_trace_open(struct trace_iterator *iter)
269 graph_trace_open(iter);
272 static void wakeup_trace_close(struct trace_iterator *iter)
275 graph_trace_close(iter);
278 #define GRAPH_TRACER_FLAGS (TRACE_GRAPH_PRINT_PROC | \
279 TRACE_GRAPH_PRINT_ABS_TIME | \
280 TRACE_GRAPH_PRINT_DURATION)
282 static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
285 * In graph mode call the graph tracer output function,
286 * otherwise go with the TRACE_FN event handler
289 return print_graph_function_flags(iter, GRAPH_TRACER_FLAGS);
291 return TRACE_TYPE_UNHANDLED;
294 static void wakeup_print_header(struct seq_file *s)
297 print_graph_headers_flags(s, GRAPH_TRACER_FLAGS);
299 trace_default_header(s);
303 __trace_function(struct trace_array *tr,
304 unsigned long ip, unsigned long parent_ip,
305 unsigned long flags, int pc)
308 trace_graph_function(tr, ip, parent_ip, flags, pc);
310 trace_function(tr, ip, parent_ip, flags, pc);
313 #define __trace_function trace_function
316 wakeup_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
321 static int wakeup_graph_entry(struct ftrace_graph_ent *trace)
326 static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
328 return TRACE_TYPE_UNHANDLED;
331 static void wakeup_graph_return(struct ftrace_graph_ret *trace) { }
332 static void wakeup_trace_open(struct trace_iterator *iter) { }
333 static void wakeup_trace_close(struct trace_iterator *iter) { }
335 #ifdef CONFIG_FUNCTION_TRACER
336 static void wakeup_print_header(struct seq_file *s)
338 trace_default_header(s);
341 static void wakeup_print_header(struct seq_file *s)
343 trace_latency_header(s);
345 #endif /* CONFIG_FUNCTION_TRACER */
346 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
349 * Should this new latency be reported/recorded?
351 static int report_latency(cycle_t delta)
353 if (tracing_thresh) {
354 if (delta < tracing_thresh)
357 if (delta <= tracing_max_latency)
364 probe_wakeup_migrate_task(void *ignore, struct task_struct *task, int cpu)
366 if (task != wakeup_task)
369 wakeup_current_cpu = cpu;
373 probe_wakeup_sched_switch(void *ignore,
374 struct task_struct *prev, struct task_struct *next)
376 struct trace_array_cpu *data;
377 cycle_t T0, T1, delta;
383 tracing_record_cmdline(prev);
385 if (unlikely(!tracer_enabled))
389 * When we start a new trace, we set wakeup_task to NULL
390 * and then set tracer_enabled = 1. We want to make sure
391 * that another CPU does not see the tracer_enabled = 1
392 * and the wakeup_task with an older task, that might
393 * actually be the same as next.
397 if (next != wakeup_task)
400 pc = preempt_count();
402 /* disable local data, not wakeup_cpu data */
403 cpu = raw_smp_processor_id();
404 disabled = atomic_inc_return(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
405 if (likely(disabled != 1))
408 local_irq_save(flags);
409 arch_spin_lock(&wakeup_lock);
411 /* We could race with grabbing wakeup_lock */
412 if (unlikely(!tracer_enabled || next != wakeup_task))
415 /* The task we are waiting for is waking up */
416 data = per_cpu_ptr(wakeup_trace->trace_buffer.data, wakeup_cpu);
418 __trace_function(wakeup_trace, CALLER_ADDR0, CALLER_ADDR1, flags, pc);
419 tracing_sched_switch_trace(wakeup_trace, prev, next, flags, pc);
421 T0 = data->preempt_timestamp;
422 T1 = ftrace_now(cpu);
425 if (!report_latency(delta))
428 if (likely(!is_tracing_stopped())) {
429 tracing_max_latency = delta;
430 update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu);
434 __wakeup_reset(wakeup_trace);
435 arch_spin_unlock(&wakeup_lock);
436 local_irq_restore(flags);
438 atomic_dec(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
441 static void __wakeup_reset(struct trace_array *tr)
448 put_task_struct(wakeup_task);
453 static void wakeup_reset(struct trace_array *tr)
457 tracing_reset_online_cpus(&tr->trace_buffer);
459 local_irq_save(flags);
460 arch_spin_lock(&wakeup_lock);
462 arch_spin_unlock(&wakeup_lock);
463 local_irq_restore(flags);
467 probe_wakeup(void *ignore, struct task_struct *p, int success)
469 struct trace_array_cpu *data;
470 int cpu = smp_processor_id();
475 if (likely(!tracer_enabled))
478 tracing_record_cmdline(p);
479 tracing_record_cmdline(current);
482 * Semantic is like this:
483 * - wakeup tracer handles all tasks in the system, independently
484 * from their scheduling class;
485 * - wakeup_rt tracer handles tasks belonging to sched_dl and
487 * - wakeup_dl handles tasks belonging to sched_dl class only.
489 if (tracing_dl || (wakeup_dl && !dl_task(p)) ||
490 (wakeup_rt && !dl_task(p) && !rt_task(p)) ||
491 (!dl_task(p) && (p->prio >= wakeup_prio || p->prio >= current->prio)))
494 pc = preempt_count();
495 disabled = atomic_inc_return(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
496 if (unlikely(disabled != 1))
499 /* interrupts should be off from try_to_wake_up */
500 arch_spin_lock(&wakeup_lock);
502 /* check for races. */
503 if (!tracer_enabled || tracing_dl ||
504 (!dl_task(p) && p->prio >= wakeup_prio))
507 /* reset the trace */
508 __wakeup_reset(wakeup_trace);
510 wakeup_cpu = task_cpu(p);
511 wakeup_current_cpu = wakeup_cpu;
512 wakeup_prio = p->prio;
515 * Once you start tracing a -deadline task, don't bother tracing
516 * another task until the first one wakes up.
524 get_task_struct(wakeup_task);
526 local_save_flags(flags);
528 data = per_cpu_ptr(wakeup_trace->trace_buffer.data, wakeup_cpu);
529 data->preempt_timestamp = ftrace_now(cpu);
530 tracing_sched_wakeup_trace(wakeup_trace, p, current, flags, pc);
533 * We must be careful in using CALLER_ADDR2. But since wake_up
534 * is not called by an assembly function (where as schedule is)
535 * it should be safe to use it here.
537 __trace_function(wakeup_trace, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
540 arch_spin_unlock(&wakeup_lock);
542 atomic_dec(&per_cpu_ptr(wakeup_trace->trace_buffer.data, cpu)->disabled);
545 static void start_wakeup_tracer(struct trace_array *tr)
549 ret = register_trace_sched_wakeup(probe_wakeup, NULL);
551 pr_info("wakeup trace: Couldn't activate tracepoint"
552 " probe to kernel_sched_wakeup\n");
556 ret = register_trace_sched_wakeup_new(probe_wakeup, NULL);
558 pr_info("wakeup trace: Couldn't activate tracepoint"
559 " probe to kernel_sched_wakeup_new\n");
563 ret = register_trace_sched_switch(probe_wakeup_sched_switch, NULL);
565 pr_info("sched trace: Couldn't activate tracepoint"
566 " probe to kernel_sched_switch\n");
567 goto fail_deprobe_wake_new;
570 ret = register_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
572 pr_info("wakeup trace: Couldn't activate tracepoint"
573 " probe to kernel_sched_migrate_task\n");
580 * Don't let the tracer_enabled = 1 show up before
581 * the wakeup_task is reset. This may be overkill since
582 * wakeup_reset does a spin_unlock after setting the
583 * wakeup_task to NULL, but I want to be safe.
584 * This is a slow path anyway.
588 if (start_func_tracer(is_graph()))
589 printk(KERN_ERR "failed to start wakeup tracer\n");
592 fail_deprobe_wake_new:
593 unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
595 unregister_trace_sched_wakeup(probe_wakeup, NULL);
598 static void stop_wakeup_tracer(struct trace_array *tr)
601 stop_func_tracer(is_graph());
602 unregister_trace_sched_switch(probe_wakeup_sched_switch, NULL);
603 unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
604 unregister_trace_sched_wakeup(probe_wakeup, NULL);
605 unregister_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
608 static int __wakeup_tracer_init(struct trace_array *tr)
610 save_flags = trace_flags;
612 /* non overwrite screws up the latency tracers */
613 set_tracer_flag(tr, TRACE_ITER_OVERWRITE, 1);
614 set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, 1);
616 tracing_max_latency = 0;
618 start_wakeup_tracer(tr);
622 static int wakeup_tracer_init(struct trace_array *tr)
626 return __wakeup_tracer_init(tr);
629 static int wakeup_rt_tracer_init(struct trace_array *tr)
633 return __wakeup_tracer_init(tr);
636 static int wakeup_dl_tracer_init(struct trace_array *tr)
640 return __wakeup_tracer_init(tr);
643 static void wakeup_tracer_reset(struct trace_array *tr)
645 int lat_flag = save_flags & TRACE_ITER_LATENCY_FMT;
646 int overwrite_flag = save_flags & TRACE_ITER_OVERWRITE;
648 stop_wakeup_tracer(tr);
649 /* make sure we put back any tasks we are tracing */
652 set_tracer_flag(tr, TRACE_ITER_LATENCY_FMT, lat_flag);
653 set_tracer_flag(tr, TRACE_ITER_OVERWRITE, overwrite_flag);
656 static void wakeup_tracer_start(struct trace_array *tr)
662 static void wakeup_tracer_stop(struct trace_array *tr)
667 static struct tracer wakeup_tracer __read_mostly =
670 .init = wakeup_tracer_init,
671 .reset = wakeup_tracer_reset,
672 .start = wakeup_tracer_start,
673 .stop = wakeup_tracer_stop,
675 .print_header = wakeup_print_header,
676 .print_line = wakeup_print_line,
677 .flags = &tracer_flags,
678 .set_flag = wakeup_set_flag,
679 .flag_changed = wakeup_flag_changed,
680 #ifdef CONFIG_FTRACE_SELFTEST
681 .selftest = trace_selftest_startup_wakeup,
683 .open = wakeup_trace_open,
684 .close = wakeup_trace_close,
688 static struct tracer wakeup_rt_tracer __read_mostly =
691 .init = wakeup_rt_tracer_init,
692 .reset = wakeup_tracer_reset,
693 .start = wakeup_tracer_start,
694 .stop = wakeup_tracer_stop,
695 .wait_pipe = poll_wait_pipe,
697 .print_header = wakeup_print_header,
698 .print_line = wakeup_print_line,
699 .flags = &tracer_flags,
700 .set_flag = wakeup_set_flag,
701 .flag_changed = wakeup_flag_changed,
702 #ifdef CONFIG_FTRACE_SELFTEST
703 .selftest = trace_selftest_startup_wakeup,
705 .open = wakeup_trace_open,
706 .close = wakeup_trace_close,
710 static struct tracer wakeup_dl_tracer __read_mostly =
713 .init = wakeup_dl_tracer_init,
714 .reset = wakeup_tracer_reset,
715 .start = wakeup_tracer_start,
716 .stop = wakeup_tracer_stop,
717 .wait_pipe = poll_wait_pipe,
719 .print_header = wakeup_print_header,
720 .print_line = wakeup_print_line,
721 .flags = &tracer_flags,
722 .set_flag = wakeup_set_flag,
723 .flag_changed = wakeup_flag_changed,
724 #ifdef CONFIG_FTRACE_SELFTEST
725 .selftest = trace_selftest_startup_wakeup,
727 .open = wakeup_trace_open,
728 .close = wakeup_trace_close,
732 __init static int init_wakeup_tracer(void)
736 ret = register_tracer(&wakeup_tracer);
740 ret = register_tracer(&wakeup_rt_tracer);
744 ret = register_tracer(&wakeup_dl_tracer);
750 core_initcall(init_wakeup_tracer);