blob: 5a217e863586e3ba2bfcc609892fa6ba0786c41d [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
Ingo Molnar4e655512008-05-12 21:20:52 +020021ctx_switch_func(void *__rq, struct task_struct *prev, struct task_struct *next)
Steven Rostedt35e8e302008-05-12 21:20:42 +020022{
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
Ingo Molnard9af56f2008-05-12 21:20:53 +020032 tracing_record_cmdline(prev);
33
Steven Rostedt18cef372008-05-12 21:20:44 +020034 local_irq_save(flags);
Steven Rostedt35e8e302008-05-12 21:20:42 +020035 cpu = raw_smp_processor_id();
36 data = tr->data[cpu];
37 disabled = atomic_inc_return(&data->disabled);
38
Ingo Molnar4e655512008-05-12 21:20:52 +020039 if (likely(disabled == 1)) {
Steven Rostedt35e8e302008-05-12 21:20:42 +020040 tracing_sched_switch_trace(tr, data, prev, next, flags);
Ingo Molnar4ac3ba42008-05-12 21:20:52 +020041 if (trace_flags & TRACE_ITER_SCHED_TREE)
42 ftrace_all_fair_tasks(__rq, tr, data);
Ingo Molnar4e655512008-05-12 21:20:52 +020043 }
Steven Rostedt35e8e302008-05-12 21:20:42 +020044
45 atomic_dec(&data->disabled);
Steven Rostedt18cef372008-05-12 21:20:44 +020046 local_irq_restore(flags);
Steven Rostedt35e8e302008-05-12 21:20:42 +020047}
48
Ingo Molnar4e655512008-05-12 21:20:52 +020049static void
50wakeup_func(void *__rq, struct task_struct *wakee, struct task_struct *curr)
Ingo Molnar57422792008-05-12 21:20:51 +020051{
52 struct trace_array *tr = ctx_trace;
53 struct trace_array_cpu *data;
54 unsigned long flags;
55 long disabled;
56 int cpu;
57
58 if (!tracer_enabled)
59 return;
60
Ingo Molnard9af56f2008-05-12 21:20:53 +020061 tracing_record_cmdline(curr);
62
Ingo Molnar57422792008-05-12 21:20:51 +020063 local_irq_save(flags);
64 cpu = raw_smp_processor_id();
65 data = tr->data[cpu];
66 disabled = atomic_inc_return(&data->disabled);
67
Ingo Molnar4e655512008-05-12 21:20:52 +020068 if (likely(disabled == 1)) {
Ingo Molnar57422792008-05-12 21:20:51 +020069 tracing_sched_wakeup_trace(tr, data, wakee, curr, flags);
Ingo Molnar4ac3ba42008-05-12 21:20:52 +020070 if (trace_flags & TRACE_ITER_SCHED_TREE)
71 ftrace_all_fair_tasks(__rq, tr, data);
Ingo Molnar4e655512008-05-12 21:20:52 +020072 }
Ingo Molnar57422792008-05-12 21:20:51 +020073
74 atomic_dec(&data->disabled);
75 local_irq_restore(flags);
76}
77
Ingo Molnar4e655512008-05-12 21:20:52 +020078void
79ftrace_ctx_switch(void *__rq, struct task_struct *prev,
80 struct task_struct *next)
Steven Rostedt35e8e302008-05-12 21:20:42 +020081{
Steven Rostedt35e8e302008-05-12 21:20:42 +020082 /*
83 * If tracer_switch_func only points to the local
84 * switch func, it still needs the ptr passed to it.
85 */
Ingo Molnar4e655512008-05-12 21:20:52 +020086 ctx_switch_func(__rq, prev, next);
Steven Rostedt35e8e302008-05-12 21:20:42 +020087
88 /*
89 * Chain to the wakeup tracer (this is a NOP if disabled):
90 */
91 wakeup_sched_switch(prev, next);
92}
93
Ingo Molnar57422792008-05-12 21:20:51 +020094void
Ingo Molnar4e655512008-05-12 21:20:52 +020095ftrace_wake_up_task(void *__rq, struct task_struct *wakee,
96 struct task_struct *curr)
Ingo Molnar57422792008-05-12 21:20:51 +020097{
Ingo Molnar4e655512008-05-12 21:20:52 +020098 wakeup_func(__rq, wakee, curr);
Ingo Molnar57422792008-05-12 21:20:51 +020099
100 /*
101 * Chain to the wakeup tracer (this is a NOP if disabled):
102 */
103 wakeup_sched_wakeup(wakee, curr);
104}
105
Ingo Molnare309b412008-05-12 21:20:51 +0200106static void sched_switch_reset(struct trace_array *tr)
Steven Rostedt35e8e302008-05-12 21:20:42 +0200107{
108 int cpu;
109
Ingo Molnar750ed1a2008-05-12 21:20:46 +0200110 tr->time_start = ftrace_now(tr->cpu);
Steven Rostedt35e8e302008-05-12 21:20:42 +0200111
112 for_each_online_cpu(cpu)
113 tracing_reset(tr->data[cpu]);
114}
115
Ingo Molnare309b412008-05-12 21:20:51 +0200116static void start_sched_trace(struct trace_array *tr)
Steven Rostedt35e8e302008-05-12 21:20:42 +0200117{
118 sched_switch_reset(tr);
119 tracer_enabled = 1;
120}
121
Ingo Molnare309b412008-05-12 21:20:51 +0200122static void stop_sched_trace(struct trace_array *tr)
Steven Rostedt35e8e302008-05-12 21:20:42 +0200123{
124 tracer_enabled = 0;
125}
126
Ingo Molnare309b412008-05-12 21:20:51 +0200127static void sched_switch_trace_init(struct trace_array *tr)
Steven Rostedt35e8e302008-05-12 21:20:42 +0200128{
129 ctx_trace = tr;
130
131 if (tr->ctrl)
132 start_sched_trace(tr);
133}
134
Ingo Molnare309b412008-05-12 21:20:51 +0200135static void sched_switch_trace_reset(struct trace_array *tr)
Steven Rostedt35e8e302008-05-12 21:20:42 +0200136{
137 if (tr->ctrl)
138 stop_sched_trace(tr);
139}
140
141static void sched_switch_trace_ctrl_update(struct trace_array *tr)
142{
143 /* When starting a new trace, reset the buffers */
144 if (tr->ctrl)
145 start_sched_trace(tr);
146 else
147 stop_sched_trace(tr);
148}
149
150static struct tracer sched_switch_trace __read_mostly =
151{
152 .name = "sched_switch",
153 .init = sched_switch_trace_init,
154 .reset = sched_switch_trace_reset,
155 .ctrl_update = sched_switch_trace_ctrl_update,
Steven Rostedt60a11772008-05-12 21:20:44 +0200156#ifdef CONFIG_FTRACE_SELFTEST
157 .selftest = trace_selftest_startup_sched_switch,
158#endif
Steven Rostedt35e8e302008-05-12 21:20:42 +0200159};
160
161__init static int init_sched_switch_trace(void)
162{
163 return register_tracer(&sched_switch_trace);
164}
165device_initcall(init_sched_switch_trace);