blob: 8b1cf1a3aee04cf76df33d17d498852408213284 [file] [log] [blame]
Steven Rostedt35e8e302008-05-12 21:20:42 +02001/*
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
17static struct trace_array *ctx_trace;
18static int __read_mostly tracer_enabled;
19
Ingo Molnare309b412008-05-12 21:20:51 +020020static void
Steven Rostedt35e8e302008-05-12 21:20:42 +020021ctx_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
Steven Rostedt18cef372008-05-12 21:20:44 +020032 local_irq_save(flags);
Steven Rostedt35e8e302008-05-12 21:20:42 +020033 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);
Steven Rostedt18cef372008-05-12 21:20:44 +020041 local_irq_restore(flags);
Steven Rostedt35e8e302008-05-12 21:20:42 +020042}
43
Ingo Molnar57422792008-05-12 21:20:51 +020044static 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
Steven Rostedt35e8e302008-05-12 21:20:42 +020067void 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
Ingo Molnar57422792008-05-12 21:20:51 +020083void
84ftrace_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
Ingo Molnare309b412008-05-12 21:20:51 +020096static void sched_switch_reset(struct trace_array *tr)
Steven Rostedt35e8e302008-05-12 21:20:42 +020097{
98 int cpu;
99
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200100 tr->time_start = ftrace_now(tr->cpu);
Steven Rostedt35e8e302008-05-12 21:20:42 +0200101
102 for_each_online_cpu(cpu)
103 tracing_reset(tr->data[cpu]);
104}
105
Ingo Molnare309b412008-05-12 21:20:51 +0200106static void start_sched_trace(struct trace_array *tr)
Steven Rostedt35e8e302008-05-12 21:20:42 +0200107{
108 sched_switch_reset(tr);
109 tracer_enabled = 1;
110}
111
Ingo Molnare309b412008-05-12 21:20:51 +0200112static void stop_sched_trace(struct trace_array *tr)
Steven Rostedt35e8e302008-05-12 21:20:42 +0200113{
114 tracer_enabled = 0;
115}
116
Ingo Molnare309b412008-05-12 21:20:51 +0200117static void sched_switch_trace_init(struct trace_array *tr)
Steven Rostedt35e8e302008-05-12 21:20:42 +0200118{
119 ctx_trace = tr;
120
121 if (tr->ctrl)
122 start_sched_trace(tr);
123}
124
Ingo Molnare309b412008-05-12 21:20:51 +0200125static void sched_switch_trace_reset(struct trace_array *tr)
Steven Rostedt35e8e302008-05-12 21:20:42 +0200126{
127 if (tr->ctrl)
128 stop_sched_trace(tr);
129}
130
131static 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
140static 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,
Steven Rostedt60a11772008-05-12 21:20:44 +0200146#ifdef CONFIG_FTRACE_SELFTEST
147 .selftest = trace_selftest_startup_sched_switch,
148#endif
Steven Rostedt35e8e302008-05-12 21:20:42 +0200149};
150
151__init static int init_sched_switch_trace(void)
152{
153 return register_tracer(&sched_switch_trace);
154}
155device_initcall(init_sched_switch_trace);