trace_sched_wakeup.c 9.0 KB

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