ipc: do not use a negative value to re-enable msgmni automatic recomputing
[linux-2.6/linux-loongson.git] / kernel / trace / trace_sched_switch.c
blobcb817a209aa005d9b151b79e2ef52b48178dffd4
1 /*
2 * trace context switch
4 * Copyright (C) 2007 Steven Rostedt <srostedt@redhat.com>
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>
15 #include "trace.h"
17 static struct trace_array *ctx_trace;
18 static int __read_mostly tracer_enabled;
19 static atomic_t sched_ref;
21 static void
22 sched_switch_func(void *private, void *__rq, struct task_struct *prev,
23 struct task_struct *next)
25 struct trace_array **ptr = private;
26 struct trace_array *tr = *ptr;
27 struct trace_array_cpu *data;
28 unsigned long flags;
29 long disabled;
30 int cpu;
32 tracing_record_cmdline(prev);
33 tracing_record_cmdline(next);
35 if (!tracer_enabled)
36 return;
38 local_irq_save(flags);
39 cpu = raw_smp_processor_id();
40 data = tr->data[cpu];
41 disabled = atomic_inc_return(&data->disabled);
43 if (likely(disabled == 1))
44 tracing_sched_switch_trace(tr, data, prev, next, flags);
46 atomic_dec(&data->disabled);
47 local_irq_restore(flags);
50 static notrace void
51 sched_switch_callback(void *probe_data, void *call_data,
52 const char *format, va_list *args)
54 struct task_struct *prev;
55 struct task_struct *next;
56 struct rq *__rq;
58 if (!atomic_read(&sched_ref))
59 return;
61 /* skip prev_pid %d next_pid %d prev_state %ld */
62 (void)va_arg(*args, int);
63 (void)va_arg(*args, int);
64 (void)va_arg(*args, long);
65 __rq = va_arg(*args, typeof(__rq));
66 prev = va_arg(*args, typeof(prev));
67 next = va_arg(*args, typeof(next));
70 * If tracer_switch_func only points to the local
71 * switch func, it still needs the ptr passed to it.
73 sched_switch_func(probe_data, __rq, prev, next);
76 static void
77 wakeup_func(void *private, void *__rq, struct task_struct *wakee, struct
78 task_struct *curr)
80 struct trace_array **ptr = private;
81 struct trace_array *tr = *ptr;
82 struct trace_array_cpu *data;
83 unsigned long flags;
84 long disabled;
85 int cpu;
87 if (!tracer_enabled)
88 return;
90 tracing_record_cmdline(curr);
92 local_irq_save(flags);
93 cpu = raw_smp_processor_id();
94 data = tr->data[cpu];
95 disabled = atomic_inc_return(&data->disabled);
97 if (likely(disabled == 1))
98 tracing_sched_wakeup_trace(tr, data, wakee, curr, flags);
100 atomic_dec(&data->disabled);
101 local_irq_restore(flags);
104 static notrace void
105 wake_up_callback(void *probe_data, void *call_data,
106 const char *format, va_list *args)
108 struct task_struct *curr;
109 struct task_struct *task;
110 struct rq *__rq;
112 if (likely(!tracer_enabled))
113 return;
115 /* Skip pid %d state %ld */
116 (void)va_arg(*args, int);
117 (void)va_arg(*args, long);
118 /* now get the meat: "rq %p task %p rq->curr %p" */
119 __rq = va_arg(*args, typeof(__rq));
120 task = va_arg(*args, typeof(task));
121 curr = va_arg(*args, typeof(curr));
123 tracing_record_cmdline(task);
124 tracing_record_cmdline(curr);
126 wakeup_func(probe_data, __rq, task, curr);
129 static void sched_switch_reset(struct trace_array *tr)
131 int cpu;
133 tr->time_start = ftrace_now(tr->cpu);
135 for_each_online_cpu(cpu)
136 tracing_reset(tr->data[cpu]);
139 static int tracing_sched_register(void)
141 int ret;
143 ret = marker_probe_register("kernel_sched_wakeup",
144 "pid %d state %ld ## rq %p task %p rq->curr %p",
145 wake_up_callback,
146 &ctx_trace);
147 if (ret) {
148 pr_info("wakeup trace: Couldn't add marker"
149 " probe to kernel_sched_wakeup\n");
150 return ret;
153 ret = marker_probe_register("kernel_sched_wakeup_new",
154 "pid %d state %ld ## rq %p task %p rq->curr %p",
155 wake_up_callback,
156 &ctx_trace);
157 if (ret) {
158 pr_info("wakeup trace: Couldn't add marker"
159 " probe to kernel_sched_wakeup_new\n");
160 goto fail_deprobe;
163 ret = marker_probe_register("kernel_sched_schedule",
164 "prev_pid %d next_pid %d prev_state %ld "
165 "## rq %p prev %p next %p",
166 sched_switch_callback,
167 &ctx_trace);
168 if (ret) {
169 pr_info("sched trace: Couldn't add marker"
170 " probe to kernel_sched_schedule\n");
171 goto fail_deprobe_wake_new;
174 return ret;
175 fail_deprobe_wake_new:
176 marker_probe_unregister("kernel_sched_wakeup_new",
177 wake_up_callback,
178 &ctx_trace);
179 fail_deprobe:
180 marker_probe_unregister("kernel_sched_wakeup",
181 wake_up_callback,
182 &ctx_trace);
183 return ret;
186 static void tracing_sched_unregister(void)
188 marker_probe_unregister("kernel_sched_schedule",
189 sched_switch_callback,
190 &ctx_trace);
191 marker_probe_unregister("kernel_sched_wakeup_new",
192 wake_up_callback,
193 &ctx_trace);
194 marker_probe_unregister("kernel_sched_wakeup",
195 wake_up_callback,
196 &ctx_trace);
199 static void tracing_start_sched_switch(void)
201 long ref;
203 ref = atomic_inc_return(&sched_ref);
204 if (ref == 1)
205 tracing_sched_register();
208 static void tracing_stop_sched_switch(void)
210 long ref;
212 ref = atomic_dec_and_test(&sched_ref);
213 if (ref)
214 tracing_sched_unregister();
217 void tracing_start_cmdline_record(void)
219 tracing_start_sched_switch();
222 void tracing_stop_cmdline_record(void)
224 tracing_stop_sched_switch();
227 static void start_sched_trace(struct trace_array *tr)
229 sched_switch_reset(tr);
230 tracing_start_cmdline_record();
231 tracer_enabled = 1;
234 static void stop_sched_trace(struct trace_array *tr)
236 tracer_enabled = 0;
237 tracing_stop_cmdline_record();
240 static void sched_switch_trace_init(struct trace_array *tr)
242 ctx_trace = tr;
244 if (tr->ctrl)
245 start_sched_trace(tr);
248 static void sched_switch_trace_reset(struct trace_array *tr)
250 if (tr->ctrl)
251 stop_sched_trace(tr);
254 static void sched_switch_trace_ctrl_update(struct trace_array *tr)
256 /* When starting a new trace, reset the buffers */
257 if (tr->ctrl)
258 start_sched_trace(tr);
259 else
260 stop_sched_trace(tr);
263 static struct tracer sched_switch_trace __read_mostly =
265 .name = "sched_switch",
266 .init = sched_switch_trace_init,
267 .reset = sched_switch_trace_reset,
268 .ctrl_update = sched_switch_trace_ctrl_update,
269 #ifdef CONFIG_FTRACE_SELFTEST
270 .selftest = trace_selftest_startup_sched_switch,
271 #endif
274 __init static int init_sched_switch_trace(void)
276 int ret = 0;
278 if (atomic_read(&sched_ref))
279 ret = tracing_sched_register();
280 if (ret) {
281 pr_info("error registering scheduler trace\n");
282 return ret;
284 return register_tracer(&sched_switch_trace);
286 device_initcall(init_sched_switch_trace);