trace_sched_wakeup.c 9.0 KB

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