]> git.karo-electronics.de Git - karo-tx-linux.git/blob - kernel/trace/trace_sched_switch.c
ftrace: add wakeup events to sched tracer
[karo-tx-linux.git] / kernel / trace / trace_sched_switch.c
1 /*
2  * trace context switch
3  *
4  * Copyright (C) 2007 Steven Rostedt <srostedt@redhat.com>
5  *
6  */
7 #include <linux/module.h>
8 #include <linux/fs.h>
9 #include <linux/debugfs.h>
10 #include <linux/kallsyms.h>
11 #include <linux/uaccess.h>
12 #include <linux/marker.h>
13 #include <linux/ftrace.h>
14
15 #include "trace.h"
16
17 static struct trace_array       *ctx_trace;
18 static int __read_mostly        tracer_enabled;
19
20 static void
21 ctx_switch_func(struct task_struct *prev, struct task_struct *next)
22 {
23         struct trace_array *tr = ctx_trace;
24         struct trace_array_cpu *data;
25         unsigned long flags;
26         long disabled;
27         int cpu;
28
29         if (!tracer_enabled)
30                 return;
31
32         local_irq_save(flags);
33         cpu = raw_smp_processor_id();
34         data = tr->data[cpu];
35         disabled = atomic_inc_return(&data->disabled);
36
37         if (likely(disabled == 1))
38                 tracing_sched_switch_trace(tr, data, prev, next, flags);
39
40         atomic_dec(&data->disabled);
41         local_irq_restore(flags);
42 }
43
44 static void wakeup_func(struct task_struct *wakee, struct task_struct *curr)
45 {
46         struct trace_array *tr = ctx_trace;
47         struct trace_array_cpu *data;
48         unsigned long flags;
49         long disabled;
50         int cpu;
51
52         if (!tracer_enabled)
53                 return;
54
55         local_irq_save(flags);
56         cpu = raw_smp_processor_id();
57         data = tr->data[cpu];
58         disabled = atomic_inc_return(&data->disabled);
59
60         if (likely(disabled == 1))
61                 tracing_sched_wakeup_trace(tr, data, wakee, curr, flags);
62
63         atomic_dec(&data->disabled);
64         local_irq_restore(flags);
65 }
66
67 void ftrace_ctx_switch(struct task_struct *prev, struct task_struct *next)
68 {
69         tracing_record_cmdline(prev);
70
71         /*
72          * If tracer_switch_func only points to the local
73          * switch func, it still needs the ptr passed to it.
74          */
75         ctx_switch_func(prev, next);
76
77         /*
78          * Chain to the wakeup tracer (this is a NOP if disabled):
79          */
80         wakeup_sched_switch(prev, next);
81 }
82
83 void
84 ftrace_wake_up_task(struct task_struct *wakee, struct task_struct *curr)
85 {
86         tracing_record_cmdline(curr);
87
88         wakeup_func(wakee, curr);
89
90         /*
91          * Chain to the wakeup tracer (this is a NOP if disabled):
92          */
93         wakeup_sched_wakeup(wakee, curr);
94 }
95
96 static void sched_switch_reset(struct trace_array *tr)
97 {
98         int cpu;
99
100         tr->time_start = ftrace_now(tr->cpu);
101
102         for_each_online_cpu(cpu)
103                 tracing_reset(tr->data[cpu]);
104 }
105
106 static void start_sched_trace(struct trace_array *tr)
107 {
108         sched_switch_reset(tr);
109         tracer_enabled = 1;
110 }
111
112 static void stop_sched_trace(struct trace_array *tr)
113 {
114         tracer_enabled = 0;
115 }
116
117 static void sched_switch_trace_init(struct trace_array *tr)
118 {
119         ctx_trace = tr;
120
121         if (tr->ctrl)
122                 start_sched_trace(tr);
123 }
124
125 static void sched_switch_trace_reset(struct trace_array *tr)
126 {
127         if (tr->ctrl)
128                 stop_sched_trace(tr);
129 }
130
131 static void sched_switch_trace_ctrl_update(struct trace_array *tr)
132 {
133         /* When starting a new trace, reset the buffers */
134         if (tr->ctrl)
135                 start_sched_trace(tr);
136         else
137                 stop_sched_trace(tr);
138 }
139
140 static struct tracer sched_switch_trace __read_mostly =
141 {
142         .name           = "sched_switch",
143         .init           = sched_switch_trace_init,
144         .reset          = sched_switch_trace_reset,
145         .ctrl_update    = sched_switch_trace_ctrl_update,
146 #ifdef CONFIG_FTRACE_SELFTEST
147         .selftest    = trace_selftest_startup_sched_switch,
148 #endif
149 };
150
151 __init static int init_sched_switch_trace(void)
152 {
153         return register_tracer(&sched_switch_trace);
154 }
155 device_initcall(init_sched_switch_trace);