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 <trace/events/sched.h>
22 static struct trace_array *wakeup_trace;
23 static int __read_mostly tracer_enabled;
25 static struct task_struct *wakeup_task;
26 static int wakeup_cpu;
27 static int wakeup_current_cpu;
28 static unsigned wakeup_prio = -1;
31 static arch_spinlock_t wakeup_lock =
32 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
34 static void wakeup_reset(struct trace_array *tr);
35 static void __wakeup_reset(struct trace_array *tr);
36 static int wakeup_graph_entry(struct ftrace_graph_ent *trace);
37 static void wakeup_graph_return(struct ftrace_graph_ret *trace);
39 static int save_flags;
41 #define TRACE_DISPLAY_GRAPH 1
43 static 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) },
51 static struct tracer_flags tracer_flags = {
56 #define is_graph() (tracer_flags.val & TRACE_DISPLAY_GRAPH)
58 #ifdef CONFIG_FUNCTION_TRACER
61 * Prologue for the wakeup function tracers.
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
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.
75 func_prolog_preempt_disable(struct trace_array *tr,
76 struct trace_array_cpu **data,
82 if (likely(!wakeup_task))
85 *pc = preempt_count();
86 preempt_disable_notrace();
88 cpu = raw_smp_processor_id();
89 if (cpu != wakeup_current_cpu)
92 *data = tr->data[cpu];
93 disabled = atomic_inc_return(&(*data)->disabled);
94 if (unlikely(disabled != 1))
100 atomic_dec(&(*data)->disabled);
103 preempt_enable_notrace();
108 * wakeup uses its own tracer function to keep the overhead down:
111 wakeup_tracer_call(unsigned long ip, unsigned long parent_ip,
112 struct ftrace_ops *op, struct pt_regs *pt_regs)
114 struct trace_array *tr = wakeup_trace;
115 struct trace_array_cpu *data;
119 if (!func_prolog_preempt_disable(tr, &data, &pc))
122 local_irq_save(flags);
123 trace_function(tr, ip, parent_ip, flags, pc);
124 local_irq_restore(flags);
126 atomic_dec(&data->disabled);
127 preempt_enable_notrace();
130 static struct ftrace_ops trace_ops __read_mostly =
132 .func = wakeup_tracer_call,
133 .flags = FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_RECURSION_SAFE,
135 #endif /* CONFIG_FUNCTION_TRACER */
137 static int start_func_tracer(int graph)
142 ret = register_ftrace_function(&trace_ops);
144 ret = register_ftrace_graph(&wakeup_graph_return,
145 &wakeup_graph_entry);
147 if (!ret && tracing_is_enabled())
155 static void stop_func_tracer(int graph)
160 unregister_ftrace_function(&trace_ops);
162 unregister_ftrace_graph();
165 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
166 static int wakeup_set_flag(u32 old_flags, u32 bit, int set)
169 if (!(bit & TRACE_DISPLAY_GRAPH))
172 if (!(is_graph() ^ set))
175 stop_func_tracer(!set);
177 wakeup_reset(wakeup_trace);
178 tracing_max_latency = 0;
180 return start_func_tracer(set);
183 static int wakeup_graph_entry(struct ftrace_graph_ent *trace)
185 struct trace_array *tr = wakeup_trace;
186 struct trace_array_cpu *data;
190 if (!func_prolog_preempt_disable(tr, &data, &pc))
193 local_save_flags(flags);
194 ret = __trace_graph_entry(tr, trace, flags, pc);
195 atomic_dec(&data->disabled);
196 preempt_enable_notrace();
201 static void wakeup_graph_return(struct ftrace_graph_ret *trace)
203 struct trace_array *tr = wakeup_trace;
204 struct trace_array_cpu *data;
208 if (!func_prolog_preempt_disable(tr, &data, &pc))
211 local_save_flags(flags);
212 __trace_graph_return(tr, trace, flags, pc);
213 atomic_dec(&data->disabled);
215 preempt_enable_notrace();
219 static void wakeup_trace_open(struct trace_iterator *iter)
222 graph_trace_open(iter);
225 static void wakeup_trace_close(struct trace_iterator *iter)
228 graph_trace_close(iter);
231 #define GRAPH_TRACER_FLAGS (TRACE_GRAPH_PRINT_PROC | \
232 TRACE_GRAPH_PRINT_ABS_TIME | \
233 TRACE_GRAPH_PRINT_DURATION)
235 static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
238 * In graph mode call the graph tracer output function,
239 * otherwise go with the TRACE_FN event handler
242 return print_graph_function_flags(iter, GRAPH_TRACER_FLAGS);
244 return TRACE_TYPE_UNHANDLED;
247 static void wakeup_print_header(struct seq_file *s)
250 print_graph_headers_flags(s, GRAPH_TRACER_FLAGS);
252 trace_default_header(s);
256 __trace_function(struct trace_array *tr,
257 unsigned long ip, unsigned long parent_ip,
258 unsigned long flags, int pc)
261 trace_graph_function(tr, ip, parent_ip, flags, pc);
263 trace_function(tr, ip, parent_ip, flags, pc);
266 #define __trace_function trace_function
268 static int wakeup_set_flag(u32 old_flags, u32 bit, int set)
273 static int wakeup_graph_entry(struct ftrace_graph_ent *trace)
278 static enum print_line_t wakeup_print_line(struct trace_iterator *iter)
280 return TRACE_TYPE_UNHANDLED;
283 static void wakeup_graph_return(struct ftrace_graph_ret *trace) { }
284 static void wakeup_trace_open(struct trace_iterator *iter) { }
285 static void wakeup_trace_close(struct trace_iterator *iter) { }
287 #ifdef CONFIG_FUNCTION_TRACER
288 static void wakeup_print_header(struct seq_file *s)
290 trace_default_header(s);
293 static void wakeup_print_header(struct seq_file *s)
295 trace_latency_header(s);
297 #endif /* CONFIG_FUNCTION_TRACER */
298 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
301 * Should this new latency be reported/recorded?
303 static int report_latency(cycle_t delta)
305 if (tracing_thresh) {
306 if (delta < tracing_thresh)
309 if (delta <= tracing_max_latency)
316 probe_wakeup_migrate_task(void *ignore, struct task_struct *task, int cpu)
318 if (task != wakeup_task)
321 wakeup_current_cpu = cpu;
325 probe_wakeup_sched_switch(void *ignore,
326 struct task_struct *prev, struct task_struct *next)
328 struct trace_array_cpu *data;
329 cycle_t T0, T1, delta;
335 tracing_record_cmdline(prev);
337 if (unlikely(!tracer_enabled))
341 * When we start a new trace, we set wakeup_task to NULL
342 * and then set tracer_enabled = 1. We want to make sure
343 * that another CPU does not see the tracer_enabled = 1
344 * and the wakeup_task with an older task, that might
345 * actually be the same as next.
349 if (next != wakeup_task)
352 pc = preempt_count();
354 /* disable local data, not wakeup_cpu data */
355 cpu = raw_smp_processor_id();
356 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
357 if (likely(disabled != 1))
360 local_irq_save(flags);
361 arch_spin_lock(&wakeup_lock);
363 /* We could race with grabbing wakeup_lock */
364 if (unlikely(!tracer_enabled || next != wakeup_task))
367 /* The task we are waiting for is waking up */
368 data = wakeup_trace->data[wakeup_cpu];
370 __trace_function(wakeup_trace, CALLER_ADDR0, CALLER_ADDR1, flags, pc);
371 tracing_sched_switch_trace(wakeup_trace, prev, next, flags, pc);
373 T0 = data->preempt_timestamp;
374 T1 = ftrace_now(cpu);
377 if (!report_latency(delta))
380 if (likely(!is_tracing_stopped())) {
381 tracing_max_latency = delta;
382 update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu);
386 __wakeup_reset(wakeup_trace);
387 arch_spin_unlock(&wakeup_lock);
388 local_irq_restore(flags);
390 atomic_dec(&wakeup_trace->data[cpu]->disabled);
393 static void __wakeup_reset(struct trace_array *tr)
399 put_task_struct(wakeup_task);
404 static void wakeup_reset(struct trace_array *tr)
408 tracing_reset_online_cpus(tr);
410 local_irq_save(flags);
411 arch_spin_lock(&wakeup_lock);
413 arch_spin_unlock(&wakeup_lock);
414 local_irq_restore(flags);
418 probe_wakeup(void *ignore, struct task_struct *p, int success)
420 struct trace_array_cpu *data;
421 int cpu = smp_processor_id();
426 if (likely(!tracer_enabled))
429 tracing_record_cmdline(p);
430 tracing_record_cmdline(current);
432 if ((wakeup_rt && !rt_task(p)) ||
433 p->prio >= wakeup_prio ||
434 p->prio >= current->prio)
437 pc = preempt_count();
438 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
439 if (unlikely(disabled != 1))
442 /* interrupts should be off from try_to_wake_up */
443 arch_spin_lock(&wakeup_lock);
445 /* check for races. */
446 if (!tracer_enabled || p->prio >= wakeup_prio)
449 /* reset the trace */
450 __wakeup_reset(wakeup_trace);
452 wakeup_cpu = task_cpu(p);
453 wakeup_current_cpu = wakeup_cpu;
454 wakeup_prio = p->prio;
457 get_task_struct(wakeup_task);
459 local_save_flags(flags);
461 data = wakeup_trace->data[wakeup_cpu];
462 data->preempt_timestamp = ftrace_now(cpu);
463 tracing_sched_wakeup_trace(wakeup_trace, p, current, flags, pc);
466 * We must be careful in using CALLER_ADDR2. But since wake_up
467 * is not called by an assembly function (where as schedule is)
468 * it should be safe to use it here.
470 __trace_function(wakeup_trace, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
473 arch_spin_unlock(&wakeup_lock);
475 atomic_dec(&wakeup_trace->data[cpu]->disabled);
478 static void start_wakeup_tracer(struct trace_array *tr)
482 ret = register_trace_sched_wakeup(probe_wakeup, NULL);
484 pr_info("wakeup trace: Couldn't activate tracepoint"
485 " probe to kernel_sched_wakeup\n");
489 ret = register_trace_sched_wakeup_new(probe_wakeup, NULL);
491 pr_info("wakeup trace: Couldn't activate tracepoint"
492 " probe to kernel_sched_wakeup_new\n");
496 ret = register_trace_sched_switch(probe_wakeup_sched_switch, NULL);
498 pr_info("sched trace: Couldn't activate tracepoint"
499 " probe to kernel_sched_switch\n");
500 goto fail_deprobe_wake_new;
503 ret = register_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
505 pr_info("wakeup trace: Couldn't activate tracepoint"
506 " probe to kernel_sched_migrate_task\n");
513 * Don't let the tracer_enabled = 1 show up before
514 * the wakeup_task is reset. This may be overkill since
515 * wakeup_reset does a spin_unlock after setting the
516 * wakeup_task to NULL, but I want to be safe.
517 * This is a slow path anyway.
521 if (start_func_tracer(is_graph()))
522 printk(KERN_ERR "failed to start wakeup tracer\n");
525 fail_deprobe_wake_new:
526 unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
528 unregister_trace_sched_wakeup(probe_wakeup, NULL);
531 static void stop_wakeup_tracer(struct trace_array *tr)
534 stop_func_tracer(is_graph());
535 unregister_trace_sched_switch(probe_wakeup_sched_switch, NULL);
536 unregister_trace_sched_wakeup_new(probe_wakeup, NULL);
537 unregister_trace_sched_wakeup(probe_wakeup, NULL);
538 unregister_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL);
541 static int __wakeup_tracer_init(struct trace_array *tr)
543 save_flags = trace_flags;
545 /* non overwrite screws up the latency tracers */
546 set_tracer_flag(TRACE_ITER_OVERWRITE, 1);
547 set_tracer_flag(TRACE_ITER_LATENCY_FMT, 1);
549 tracing_max_latency = 0;
551 start_wakeup_tracer(tr);
555 static int wakeup_tracer_init(struct trace_array *tr)
558 return __wakeup_tracer_init(tr);
561 static int wakeup_rt_tracer_init(struct trace_array *tr)
564 return __wakeup_tracer_init(tr);
567 static void wakeup_tracer_reset(struct trace_array *tr)
569 int lat_flag = save_flags & TRACE_ITER_LATENCY_FMT;
570 int overwrite_flag = save_flags & TRACE_ITER_OVERWRITE;
572 stop_wakeup_tracer(tr);
573 /* make sure we put back any tasks we are tracing */
576 set_tracer_flag(TRACE_ITER_LATENCY_FMT, lat_flag);
577 set_tracer_flag(TRACE_ITER_OVERWRITE, overwrite_flag);
580 static void wakeup_tracer_start(struct trace_array *tr)
586 static void wakeup_tracer_stop(struct trace_array *tr)
591 static struct tracer wakeup_tracer __read_mostly =
594 .init = wakeup_tracer_init,
595 .reset = wakeup_tracer_reset,
596 .start = wakeup_tracer_start,
597 .stop = wakeup_tracer_stop,
599 .print_header = wakeup_print_header,
600 .print_line = wakeup_print_line,
601 .flags = &tracer_flags,
602 .set_flag = wakeup_set_flag,
603 .flag_changed = trace_keep_overwrite,
604 #ifdef CONFIG_FTRACE_SELFTEST
605 .selftest = trace_selftest_startup_wakeup,
607 .open = wakeup_trace_open,
608 .close = wakeup_trace_close,
612 static struct tracer wakeup_rt_tracer __read_mostly =
615 .init = wakeup_rt_tracer_init,
616 .reset = wakeup_tracer_reset,
617 .start = wakeup_tracer_start,
618 .stop = wakeup_tracer_stop,
619 .wait_pipe = poll_wait_pipe,
621 .print_header = wakeup_print_header,
622 .print_line = wakeup_print_line,
623 .flags = &tracer_flags,
624 .set_flag = wakeup_set_flag,
625 .flag_changed = trace_keep_overwrite,
626 #ifdef CONFIG_FTRACE_SELFTEST
627 .selftest = trace_selftest_startup_wakeup,
629 .open = wakeup_trace_open,
630 .close = wakeup_trace_close,
634 __init static int init_wakeup_tracer(void)
638 ret = register_tracer(&wakeup_tracer);
642 ret = register_tracer(&wakeup_rt_tracer);
648 core_initcall(init_wakeup_tracer);