Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 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> |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 12 | #include <linux/ftrace.h> |
Steven Rostedt | ad8d75f | 2009-04-14 19:39:12 -0400 | [diff] [blame] | 13 | #include <trace/events/sched.h> |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 14 | |
| 15 | #include "trace.h" |
| 16 | |
| 17 | static struct trace_array *ctx_trace; |
| 18 | static int __read_mostly tracer_enabled; |
Frederic Weisbecker | efade6e | 2008-10-31 13:28:58 +0100 | [diff] [blame] | 19 | static int sched_ref; |
| 20 | static DEFINE_MUTEX(sched_register_mutex); |
Steven Rostedt | 5fec6dd | 2009-03-17 19:59:53 -0400 | [diff] [blame] | 21 | static int sched_stopped; |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 22 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 23 | static void |
Mathieu Desnoyers | b07c3f1 | 2008-07-18 12:16:17 -0400 | [diff] [blame] | 24 | probe_sched_switch(struct rq *__rq, struct task_struct *prev, |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 25 | struct task_struct *next) |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 26 | { |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 27 | struct trace_array_cpu *data; |
| 28 | unsigned long flags; |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 29 | int cpu; |
Steven Rostedt | 3869705 | 2008-10-01 13:14:09 -0400 | [diff] [blame] | 30 | int pc; |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 31 | |
Zhaolei | dcef788 | 2009-03-31 15:26:14 +0800 | [diff] [blame] | 32 | if (unlikely(!sched_ref)) |
Mathieu Desnoyers | b07c3f1 | 2008-07-18 12:16:17 -0400 | [diff] [blame] | 33 | return; |
| 34 | |
Steven Rostedt | 41bc814 | 2008-05-22 11:49:22 -0400 | [diff] [blame] | 35 | tracing_record_cmdline(prev); |
| 36 | tracing_record_cmdline(next); |
| 37 | |
Zhaolei | dcef788 | 2009-03-31 15:26:14 +0800 | [diff] [blame] | 38 | if (!tracer_enabled || sched_stopped) |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 39 | return; |
| 40 | |
Steven Rostedt | 3869705 | 2008-10-01 13:14:09 -0400 | [diff] [blame] | 41 | pc = preempt_count(); |
Steven Rostedt | 18cef37 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 42 | local_irq_save(flags); |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 43 | cpu = raw_smp_processor_id(); |
Mathieu Desnoyers | b07c3f1 | 2008-07-18 12:16:17 -0400 | [diff] [blame] | 44 | data = ctx_trace->data[cpu]; |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 45 | |
Steven Rostedt | 3ea2e6d | 2008-10-04 02:01:00 -0400 | [diff] [blame] | 46 | if (likely(!atomic_read(&data->disabled))) |
Arnaldo Carvalho de Melo | 7be4215 | 2009-02-05 01:13:37 -0500 | [diff] [blame] | 47 | tracing_sched_switch_trace(ctx_trace, prev, next, flags, pc); |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 48 | |
Steven Rostedt | 18cef37 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 49 | local_irq_restore(flags); |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 50 | } |
| 51 | |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 52 | static void |
Peter Zijlstra | 468a15b | 2008-12-16 08:07:03 +0100 | [diff] [blame] | 53 | probe_sched_wakeup(struct rq *__rq, struct task_struct *wakee, int success) |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 54 | { |
Ingo Molnar | 5742279 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 55 | struct trace_array_cpu *data; |
| 56 | unsigned long flags; |
Steven Rostedt | 3869705 | 2008-10-01 13:14:09 -0400 | [diff] [blame] | 57 | int cpu, pc; |
Ingo Molnar | 5742279 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 58 | |
Zhaolei | dcef788 | 2009-03-31 15:26:14 +0800 | [diff] [blame] | 59 | if (unlikely(!sched_ref)) |
| 60 | return; |
| 61 | |
| 62 | tracing_record_cmdline(current); |
| 63 | |
| 64 | if (!tracer_enabled || sched_stopped) |
Ingo Molnar | 5742279 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 65 | return; |
| 66 | |
Steven Rostedt | 3869705 | 2008-10-01 13:14:09 -0400 | [diff] [blame] | 67 | pc = preempt_count(); |
Ingo Molnar | 5742279 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 68 | local_irq_save(flags); |
| 69 | cpu = raw_smp_processor_id(); |
Mathieu Desnoyers | b07c3f1 | 2008-07-18 12:16:17 -0400 | [diff] [blame] | 70 | data = ctx_trace->data[cpu]; |
Ingo Molnar | 5742279 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 71 | |
Steven Rostedt | 3ea2e6d | 2008-10-04 02:01:00 -0400 | [diff] [blame] | 72 | if (likely(!atomic_read(&data->disabled))) |
Arnaldo Carvalho de Melo | 7be4215 | 2009-02-05 01:13:37 -0500 | [diff] [blame] | 73 | tracing_sched_wakeup_trace(ctx_trace, wakee, current, |
Steven Rostedt | 3869705 | 2008-10-01 13:14:09 -0400 | [diff] [blame] | 74 | flags, pc); |
Ingo Molnar | 5742279 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 75 | |
Ingo Molnar | 5742279 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 76 | local_irq_restore(flags); |
| 77 | } |
| 78 | |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 79 | static int tracing_sched_register(void) |
| 80 | { |
| 81 | int ret; |
| 82 | |
Mathieu Desnoyers | b07c3f1 | 2008-07-18 12:16:17 -0400 | [diff] [blame] | 83 | ret = register_trace_sched_wakeup(probe_sched_wakeup); |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 84 | if (ret) { |
Mathieu Desnoyers | b07c3f1 | 2008-07-18 12:16:17 -0400 | [diff] [blame] | 85 | pr_info("wakeup trace: Couldn't activate tracepoint" |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 86 | " probe to kernel_sched_wakeup\n"); |
| 87 | return ret; |
| 88 | } |
| 89 | |
Mathieu Desnoyers | b07c3f1 | 2008-07-18 12:16:17 -0400 | [diff] [blame] | 90 | ret = register_trace_sched_wakeup_new(probe_sched_wakeup); |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 91 | if (ret) { |
Mathieu Desnoyers | b07c3f1 | 2008-07-18 12:16:17 -0400 | [diff] [blame] | 92 | pr_info("wakeup trace: Couldn't activate tracepoint" |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 93 | " probe to kernel_sched_wakeup_new\n"); |
| 94 | goto fail_deprobe; |
| 95 | } |
| 96 | |
Mathieu Desnoyers | b07c3f1 | 2008-07-18 12:16:17 -0400 | [diff] [blame] | 97 | ret = register_trace_sched_switch(probe_sched_switch); |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 98 | if (ret) { |
Mathieu Desnoyers | b07c3f1 | 2008-07-18 12:16:17 -0400 | [diff] [blame] | 99 | pr_info("sched trace: Couldn't activate tracepoint" |
Wenji Huang | 73d8b8b | 2009-02-17 01:10:02 -0500 | [diff] [blame] | 100 | " probe to kernel_sched_switch\n"); |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 101 | goto fail_deprobe_wake_new; |
| 102 | } |
| 103 | |
| 104 | return ret; |
| 105 | fail_deprobe_wake_new: |
Mathieu Desnoyers | b07c3f1 | 2008-07-18 12:16:17 -0400 | [diff] [blame] | 106 | unregister_trace_sched_wakeup_new(probe_sched_wakeup); |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 107 | fail_deprobe: |
Mathieu Desnoyers | b07c3f1 | 2008-07-18 12:16:17 -0400 | [diff] [blame] | 108 | unregister_trace_sched_wakeup(probe_sched_wakeup); |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | static void tracing_sched_unregister(void) |
| 113 | { |
Mathieu Desnoyers | b07c3f1 | 2008-07-18 12:16:17 -0400 | [diff] [blame] | 114 | unregister_trace_sched_switch(probe_sched_switch); |
| 115 | unregister_trace_sched_wakeup_new(probe_sched_wakeup); |
| 116 | unregister_trace_sched_wakeup(probe_sched_wakeup); |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Ingo Molnar | f225293 | 2008-05-22 10:37:48 +0200 | [diff] [blame] | 119 | static void tracing_start_sched_switch(void) |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 120 | { |
Frederic Weisbecker | efade6e | 2008-10-31 13:28:58 +0100 | [diff] [blame] | 121 | mutex_lock(&sched_register_mutex); |
Steven Rostedt | e168e05 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 122 | if (!(sched_ref++)) |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 123 | tracing_sched_register(); |
Frederic Weisbecker | efade6e | 2008-10-31 13:28:58 +0100 | [diff] [blame] | 124 | mutex_unlock(&sched_register_mutex); |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Ingo Molnar | f225293 | 2008-05-22 10:37:48 +0200 | [diff] [blame] | 127 | static void tracing_stop_sched_switch(void) |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 128 | { |
Frederic Weisbecker | efade6e | 2008-10-31 13:28:58 +0100 | [diff] [blame] | 129 | mutex_lock(&sched_register_mutex); |
Steven Rostedt | e168e05 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 130 | if (!(--sched_ref)) |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 131 | tracing_sched_unregister(); |
Frederic Weisbecker | efade6e | 2008-10-31 13:28:58 +0100 | [diff] [blame] | 132 | mutex_unlock(&sched_register_mutex); |
Mathieu Desnoyers | 5b82a1b | 2008-05-12 21:21:10 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Steven Rostedt | 41bc814 | 2008-05-22 11:49:22 -0400 | [diff] [blame] | 135 | void tracing_start_cmdline_record(void) |
| 136 | { |
| 137 | tracing_start_sched_switch(); |
| 138 | } |
| 139 | |
| 140 | void tracing_stop_cmdline_record(void) |
| 141 | { |
| 142 | tracing_stop_sched_switch(); |
| 143 | } |
| 144 | |
Steven Rostedt | 75f5c47 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 145 | /** |
Steven Rostedt | e168e05 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 146 | * tracing_start_sched_switch_record - start tracing context switches |
| 147 | * |
| 148 | * Turns on context switch tracing for a tracer. |
| 149 | */ |
| 150 | void tracing_start_sched_switch_record(void) |
| 151 | { |
| 152 | if (unlikely(!ctx_trace)) { |
| 153 | WARN_ON(1); |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | tracing_start_sched_switch(); |
| 158 | |
| 159 | mutex_lock(&sched_register_mutex); |
| 160 | tracer_enabled++; |
| 161 | mutex_unlock(&sched_register_mutex); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * tracing_stop_sched_switch_record - start tracing context switches |
| 166 | * |
| 167 | * Turns off context switch tracing for a tracer. |
| 168 | */ |
| 169 | void tracing_stop_sched_switch_record(void) |
| 170 | { |
| 171 | mutex_lock(&sched_register_mutex); |
| 172 | tracer_enabled--; |
| 173 | WARN_ON(tracer_enabled < 0); |
| 174 | mutex_unlock(&sched_register_mutex); |
| 175 | |
| 176 | tracing_stop_sched_switch(); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * tracing_sched_switch_assign_trace - assign a trace array for ctx switch |
Steven Rostedt | 75f5c47 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 181 | * @tr: trace array pointer to assign |
| 182 | * |
| 183 | * Some tracers might want to record the context switches in their |
| 184 | * trace. This function lets those tracers assign the trace array |
| 185 | * to use. |
| 186 | */ |
Steven Rostedt | e168e05 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 187 | void tracing_sched_switch_assign_trace(struct trace_array *tr) |
Steven Rostedt | 75f5c47 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 188 | { |
| 189 | ctx_trace = tr; |
| 190 | } |
| 191 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 192 | static void stop_sched_trace(struct trace_array *tr) |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 193 | { |
Steven Rostedt | e168e05 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 194 | tracing_stop_sched_switch_record(); |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 195 | } |
| 196 | |
Frederic Weisbecker | 1c80025 | 2008-11-16 05:57:26 +0100 | [diff] [blame] | 197 | static int sched_switch_trace_init(struct trace_array *tr) |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 198 | { |
| 199 | ctx_trace = tr; |
Steven Rostedt | 5fec6dd | 2009-03-17 19:59:53 -0400 | [diff] [blame] | 200 | tracing_reset_online_cpus(tr); |
Arnaldo Carvalho de Melo | b6f11df | 2009-02-05 18:02:00 -0200 | [diff] [blame] | 201 | tracing_start_sched_switch_record(); |
Frederic Weisbecker | 1c80025 | 2008-11-16 05:57:26 +0100 | [diff] [blame] | 202 | return 0; |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Ingo Molnar | e309b41 | 2008-05-12 21:20:51 +0200 | [diff] [blame] | 205 | static void sched_switch_trace_reset(struct trace_array *tr) |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 206 | { |
Steven Rostedt | c76f069 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 207 | if (sched_ref) |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 208 | stop_sched_trace(tr); |
| 209 | } |
| 210 | |
Steven Rostedt | 9036990 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 211 | static void sched_switch_trace_start(struct trace_array *tr) |
| 212 | { |
Steven Rostedt | 5fec6dd | 2009-03-17 19:59:53 -0400 | [diff] [blame] | 213 | sched_stopped = 0; |
Steven Rostedt | 9036990 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | static void sched_switch_trace_stop(struct trace_array *tr) |
| 217 | { |
Steven Rostedt | 5fec6dd | 2009-03-17 19:59:53 -0400 | [diff] [blame] | 218 | sched_stopped = 1; |
Steven Rostedt | 9036990 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 219 | } |
| 220 | |
Steven Rostedt | 75f5c47 | 2008-11-07 22:36:02 -0500 | [diff] [blame] | 221 | static struct tracer sched_switch_trace __read_mostly = |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 222 | { |
| 223 | .name = "sched_switch", |
| 224 | .init = sched_switch_trace_init, |
| 225 | .reset = sched_switch_trace_reset, |
Steven Rostedt | 9036990 | 2008-11-05 16:05:44 -0500 | [diff] [blame] | 226 | .start = sched_switch_trace_start, |
| 227 | .stop = sched_switch_trace_stop, |
Frederic Weisbecker | 6eaaa5d | 2009-02-11 02:25:00 +0100 | [diff] [blame] | 228 | .wait_pipe = poll_wait_pipe, |
Steven Rostedt | 60a1177 | 2008-05-12 21:20:44 +0200 | [diff] [blame] | 229 | #ifdef CONFIG_FTRACE_SELFTEST |
| 230 | .selftest = trace_selftest_startup_sched_switch, |
| 231 | #endif |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | __init static int init_sched_switch_trace(void) |
| 235 | { |
Steven Rostedt | 35e8e30 | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 236 | return register_tracer(&sched_switch_trace); |
| 237 | } |
| 238 | device_initcall(init_sched_switch_trace); |
Ingo Molnar | c71dd42 | 2008-12-19 01:09:51 +0100 | [diff] [blame] | 239 | |