trace_sched_wakeup.c 8.9 KB

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