1/* 2 * trace task wakeup timings 3 * 4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> 5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com> 6 * 7 * Based on code from the latency_tracer, that is: 8 * 9 * Copyright (C) 2004-2006 Ingo Molnar 10 * Copyright (C) 2004 William Lee Irwin III 11 */ 12#include <linux/module.h> 13#include <linux/fs.h> 14#include <linux/debugfs.h> 15#include <linux/kallsyms.h> 16#include <linux/uaccess.h> 17#include <linux/ftrace.h> 18#include <trace/events/sched.h> 19 20#include "trace.h" 21 22static struct trace_array *wakeup_trace; 23static int __read_mostly tracer_enabled; 24 25static struct task_struct *wakeup_task; 26static int wakeup_cpu; 27static int wakeup_current_cpu; 28static unsigned wakeup_prio = -1; 29static int wakeup_rt; 30 31static arch_spinlock_t wakeup_lock = 32 (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED; 33 34static void __wakeup_reset(struct trace_array *tr); 35 36static int save_lat_flag; 37 38#ifdef CONFIG_FUNCTION_TRACER 39/* 40 * irqsoff uses its own tracer function to keep the overhead down: 41 */ 42static void 43wakeup_tracer_call(unsigned long ip, unsigned long parent_ip) 44{ 45 struct trace_array *tr = wakeup_trace; 46 struct trace_array_cpu *data; 47 unsigned long flags; 48 long disabled; 49 int cpu; 50 int pc; 51 52 if (likely(!wakeup_task)) 53 return; 54 55 pc = preempt_count(); 56 preempt_disable_notrace(); 57 58 cpu = raw_smp_processor_id(); 59 if (cpu != wakeup_current_cpu) 60 goto out_enable; 61 62 data = tr->data[cpu]; 63 disabled = atomic_inc_return(&data->disabled); 64 if (unlikely(disabled != 1)) 65 goto out; 66 67 local_irq_save(flags); 68 69 trace_function(tr, ip, parent_ip, flags, pc); 70 71 local_irq_restore(flags); 72 73 out: 74 atomic_dec(&data->disabled); 75 out_enable: 76 preempt_enable_notrace(); 77} 78 79static struct ftrace_ops trace_ops __read_mostly = 80{ 81 .func = wakeup_tracer_call, 82}; 83#endif /* CONFIG_FUNCTION_TRACER */ 84 85/* 86 * Should this new latency be reported/recorded? 87 */ 88static int report_latency(cycle_t delta) 89{ 90 if (tracing_thresh) { 91 if (delta < tracing_thresh) 92 return 0; 93 } else { 94 if (delta <= tracing_max_latency) 95 return 0; 96 } 97 return 1; 98} 99 100static void 101probe_wakeup_migrate_task(void *ignore, struct task_struct *task, int cpu) 102{ 103 if (task != wakeup_task) 104 return; 105 106 wakeup_current_cpu = cpu; 107} 108 109static void notrace 110probe_wakeup_sched_switch(void *ignore, 111 struct task_struct *prev, struct task_struct *next) 112{ 113 struct trace_array_cpu *data; 114 cycle_t T0, T1, delta; 115 unsigned long flags; 116 long disabled; 117 int cpu; 118 int pc; 119 120 tracing_record_cmdline(prev); 121 122 if (unlikely(!tracer_enabled)) 123 return; 124 125 /* 126 * When we start a new trace, we set wakeup_task to NULL 127 * and then set tracer_enabled = 1. We want to make sure 128 * that another CPU does not see the tracer_enabled = 1 129 * and the wakeup_task with an older task, that might 130 * actually be the same as next. 131 */ 132 smp_rmb(); 133 134 if (next != wakeup_task) 135 return; 136 137 pc = preempt_count(); 138 139 /* disable local data, not wakeup_cpu data */ 140 cpu = raw_smp_processor_id(); 141 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled); 142 if (likely(disabled != 1)) 143 goto out; 144 145 local_irq_save(flags); 146 arch_spin_lock(&wakeup_lock); 147 148 /* We could race with grabbing wakeup_lock */ 149 if (unlikely(!tracer_enabled || next != wakeup_task)) 150 goto out_unlock; 151 152 /* The task we are waiting for is waking up */ 153 data = wakeup_trace->data[wakeup_cpu]; 154 155 trace_function(wakeup_trace, CALLER_ADDR0, CALLER_ADDR1, flags, pc); 156 tracing_sched_switch_trace(wakeup_trace, prev, next, flags, pc); 157 158 T0 = data->preempt_timestamp; 159 T1 = ftrace_now(cpu); 160 delta = T1-T0; 161 162 if (!report_latency(delta)) 163 goto out_unlock; 164 165 if (likely(!is_tracing_stopped())) { 166 tracing_max_latency = delta; 167 update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu); 168 } 169 170out_unlock: 171 __wakeup_reset(wakeup_trace); 172 arch_spin_unlock(&wakeup_lock); 173 local_irq_restore(flags); 174out: 175 atomic_dec(&wakeup_trace->data[cpu]->disabled); 176} 177 178static void __wakeup_reset(struct trace_array *tr) 179{ 180 wakeup_cpu = -1; 181 wakeup_prio = -1; 182 183 if (wakeup_task) 184 put_task_struct(wakeup_task); 185 186 wakeup_task = NULL; 187} 188 189static void wakeup_reset(struct trace_array *tr) 190{ 191 unsigned long flags; 192 193 tracing_reset_online_cpus(tr); 194 195 local_irq_save(flags); 196 arch_spin_lock(&wakeup_lock); 197 __wakeup_reset(tr); 198 arch_spin_unlock(&wakeup_lock); 199 local_irq_restore(flags); 200} 201 202static void 203probe_wakeup(void *ignore, struct task_struct *p, int success) 204{ 205 struct trace_array_cpu *data; 206 int cpu = smp_processor_id(); 207 unsigned long flags; 208 long disabled; 209 int pc; 210 211 if (likely(!tracer_enabled)) 212 return; 213 214 tracing_record_cmdline(p); 215 tracing_record_cmdline(current); 216 217 if ((wakeup_rt && !rt_task(p)) || 218 p->prio >= wakeup_prio || 219 p->prio >= current->prio) 220 return; 221 222 pc = preempt_count(); 223 disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled); 224 if (unlikely(disabled != 1)) 225 goto out; 226 227 /* interrupts should be off from try_to_wake_up */ 228 arch_spin_lock(&wakeup_lock); 229 230 /* check for races. */ 231 if (!tracer_enabled || p->prio >= wakeup_prio) 232 goto out_locked; 233 234 /* reset the trace */ 235 __wakeup_reset(wakeup_trace); 236 237 wakeup_cpu = task_cpu(p); 238 wakeup_current_cpu = wakeup_cpu; 239 wakeup_prio = p->prio; 240 241 wakeup_task = p; 242 get_task_struct(wakeup_task); 243 244 local_save_flags(flags); 245 246 data = wakeup_trace->data[wakeup_cpu]; 247 data->preempt_timestamp = ftrace_now(cpu); 248 tracing_sched_wakeup_trace(wakeup_trace, p, current, flags, pc); 249 250 /* 251 * We must be careful in using CALLER_ADDR2. But since wake_up 252 * is not called by an assembly function (where as schedule is) 253 * it should be safe to use it here. 254 */ 255 trace_function(wakeup_trace, CALLER_ADDR1, CALLER_ADDR2, flags, pc); 256 257out_locked: 258 arch_spin_unlock(&wakeup_lock); 259out: 260 atomic_dec(&wakeup_trace->data[cpu]->disabled); 261} 262 263static void start_wakeup_tracer(struct trace_array *tr) 264{ 265 int ret; 266 267 ret = register_trace_sched_wakeup(probe_wakeup, NULL); 268 if (ret) { 269 pr_info("wakeup trace: Couldn't activate tracepoint" 270 " probe to kernel_sched_wakeup\n"); 271 return; 272 } 273 274 ret = register_trace_sched_wakeup_new(probe_wakeup, NULL); 275 if (ret) { 276 pr_info("wakeup trace: Couldn't activate tracepoint" 277 " probe to kernel_sched_wakeup_new\n"); 278 goto fail_deprobe; 279 } 280 281 ret = register_trace_sched_switch(probe_wakeup_sched_switch, NULL); 282 if (ret) { 283 pr_info("sched trace: Couldn't activate tracepoint" 284 " probe to kernel_sched_switch\n"); 285 goto fail_deprobe_wake_new; 286 } 287 288 ret = register_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL); 289 if (ret) { 290 pr_info("wakeup trace: Couldn't activate tracepoint" 291 " probe to kernel_sched_migrate_task\n"); 292 return; 293 } 294 295 wakeup_reset(tr); 296 297 /* 298 * Don't let the tracer_enabled = 1 show up before 299 * the wakeup_task is reset. This may be overkill since 300 * wakeup_reset does a spin_unlock after setting the 301 * wakeup_task to NULL, but I want to be safe. 302 * This is a slow path anyway. 303 */ 304 smp_wmb(); 305 306 register_ftrace_function(&trace_ops); 307 308 if (tracing_is_enabled()) 309 tracer_enabled = 1; 310 else 311 tracer_enabled = 0; 312 313 return; 314fail_deprobe_wake_new: 315 unregister_trace_sched_wakeup_new(probe_wakeup, NULL); 316fail_deprobe: 317 unregister_trace_sched_wakeup(probe_wakeup, NULL); 318} 319 320static void stop_wakeup_tracer(struct trace_array *tr) 321{ 322 tracer_enabled = 0; 323 unregister_ftrace_function(&trace_ops); 324 unregister_trace_sched_switch(probe_wakeup_sched_switch, NULL); 325 unregister_trace_sched_wakeup_new(probe_wakeup, NULL); 326 unregister_trace_sched_wakeup(probe_wakeup, NULL); 327 unregister_trace_sched_migrate_task(probe_wakeup_migrate_task, NULL); 328} 329 330static int __wakeup_tracer_init(struct trace_array *tr) 331{ 332 save_lat_flag = trace_flags & TRACE_ITER_LATENCY_FMT; 333 trace_flags |= TRACE_ITER_LATENCY_FMT; 334 335 tracing_max_latency = 0; 336 wakeup_trace = tr; 337 start_wakeup_tracer(tr); 338 return 0; 339} 340 341static int wakeup_tracer_init(struct trace_array *tr) 342{ 343 wakeup_rt = 0; 344 return __wakeup_tracer_init(tr); 345} 346 347static int wakeup_rt_tracer_init(struct trace_array *tr) 348{ 349 wakeup_rt = 1; 350 return __wakeup_tracer_init(tr); 351} 352 353static void wakeup_tracer_reset(struct trace_array *tr) 354{ 355 stop_wakeup_tracer(tr); 356 /* make sure we put back any tasks we are tracing */ 357 wakeup_reset(tr); 358 359 if (!save_lat_flag) 360 trace_flags &= ~TRACE_ITER_LATENCY_FMT; 361} 362 363static void wakeup_tracer_start(struct trace_array *tr) 364{ 365 wakeup_reset(tr); 366 tracer_enabled = 1; 367} 368 369static void wakeup_tracer_stop(struct trace_array *tr) 370{ 371 tracer_enabled = 0; 372} 373 374static struct tracer wakeup_tracer __read_mostly = 375{ 376 .name = "wakeup", 377 .init = wakeup_tracer_init, 378 .reset = wakeup_tracer_reset, 379 .start = wakeup_tracer_start, 380 .stop = wakeup_tracer_stop, 381 .print_max = 1, 382#ifdef CONFIG_FTRACE_SELFTEST 383 .selftest = trace_selftest_startup_wakeup, 384#endif 385 .use_max_tr = 1, 386}; 387 388static struct tracer wakeup_rt_tracer __read_mostly = 389{ 390 .name = "wakeup_rt", 391 .init = wakeup_rt_tracer_init, 392 .reset = wakeup_tracer_reset, 393 .start = wakeup_tracer_start, 394 .stop = wakeup_tracer_stop, 395 .wait_pipe = poll_wait_pipe, 396 .print_max = 1, 397#ifdef CONFIG_FTRACE_SELFTEST 398 .selftest = trace_selftest_startup_wakeup, 399#endif 400 .use_max_tr = 1, 401}; 402 403__init static int init_wakeup_tracer(void) 404{ 405 int ret; 406 407 ret = register_tracer(&wakeup_tracer); 408 if (ret) 409 return ret; 410 411 ret = register_tracer(&wakeup_rt_tracer); 412 if (ret) 413 return ret; 414 415 return 0; 416} 417device_initcall(init_wakeup_tracer); 418