trace_sched_wakeup.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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/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 unsigned wakeup_prio = -1;
  25. static raw_spinlock_t wakeup_lock =
  26. (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  27. static void __wakeup_reset(struct trace_array *tr);
  28. #ifdef CONFIG_FTRACE
  29. /*
  30. * irqsoff uses its own tracer function to keep the overhead down:
  31. */
  32. static void
  33. wakeup_tracer_call(unsigned long ip, unsigned long parent_ip)
  34. {
  35. struct trace_array *tr = wakeup_trace;
  36. struct trace_array_cpu *data;
  37. unsigned long flags;
  38. long disabled;
  39. int resched;
  40. int cpu;
  41. if (likely(!wakeup_task))
  42. return;
  43. resched = need_resched();
  44. preempt_disable_notrace();
  45. cpu = raw_smp_processor_id();
  46. data = tr->data[cpu];
  47. disabled = atomic_inc_return(&data->disabled);
  48. if (unlikely(disabled != 1))
  49. goto out;
  50. local_irq_save(flags);
  51. __raw_spin_lock(&wakeup_lock);
  52. if (unlikely(!wakeup_task))
  53. goto unlock;
  54. /*
  55. * The task can't disappear because it needs to
  56. * wake up first, and we have the wakeup_lock.
  57. */
  58. if (task_cpu(wakeup_task) != cpu)
  59. goto unlock;
  60. trace_function(tr, data, ip, parent_ip, flags);
  61. unlock:
  62. __raw_spin_unlock(&wakeup_lock);
  63. local_irq_restore(flags);
  64. out:
  65. atomic_dec(&data->disabled);
  66. /*
  67. * To prevent recursion from the scheduler, if the
  68. * resched flag was set before we entered, then
  69. * don't reschedule.
  70. */
  71. if (resched)
  72. preempt_enable_no_resched_notrace();
  73. else
  74. preempt_enable_notrace();
  75. }
  76. static struct ftrace_ops trace_ops __read_mostly =
  77. {
  78. .func = wakeup_tracer_call,
  79. };
  80. #endif /* CONFIG_FTRACE */
  81. /*
  82. * Should this new latency be reported/recorded?
  83. */
  84. static int report_latency(cycle_t delta)
  85. {
  86. if (tracing_thresh) {
  87. if (delta < tracing_thresh)
  88. return 0;
  89. } else {
  90. if (delta <= tracing_max_latency)
  91. return 0;
  92. }
  93. return 1;
  94. }
  95. static void notrace
  96. probe_wakeup_sched_switch(struct rq *rq, struct task_struct *prev,
  97. struct task_struct *next)
  98. {
  99. unsigned long latency = 0, t0 = 0, t1 = 0;
  100. struct trace_array_cpu *data;
  101. cycle_t T0, T1, delta;
  102. unsigned long flags;
  103. long disabled;
  104. int cpu;
  105. tracing_record_cmdline(prev);
  106. if (unlikely(!tracer_enabled))
  107. return;
  108. /*
  109. * When we start a new trace, we set wakeup_task to NULL
  110. * and then set tracer_enabled = 1. We want to make sure
  111. * that another CPU does not see the tracer_enabled = 1
  112. * and the wakeup_task with an older task, that might
  113. * actually be the same as next.
  114. */
  115. smp_rmb();
  116. if (next != wakeup_task)
  117. return;
  118. /* The task we are waiting for is waking up */
  119. data = wakeup_trace->data[wakeup_cpu];
  120. /* disable local data, not wakeup_cpu data */
  121. cpu = raw_smp_processor_id();
  122. disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
  123. if (likely(disabled != 1))
  124. goto out;
  125. local_irq_save(flags);
  126. __raw_spin_lock(&wakeup_lock);
  127. /* We could race with grabbing wakeup_lock */
  128. if (unlikely(!tracer_enabled || next != wakeup_task))
  129. goto out_unlock;
  130. trace_function(wakeup_trace, data, CALLER_ADDR1, CALLER_ADDR2, flags);
  131. /*
  132. * usecs conversion is slow so we try to delay the conversion
  133. * as long as possible:
  134. */
  135. T0 = data->preempt_timestamp;
  136. T1 = ftrace_now(cpu);
  137. delta = T1-T0;
  138. if (!report_latency(delta))
  139. goto out_unlock;
  140. latency = nsecs_to_usecs(delta);
  141. tracing_max_latency = delta;
  142. t0 = nsecs_to_usecs(T0);
  143. t1 = nsecs_to_usecs(T1);
  144. update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu);
  145. out_unlock:
  146. __wakeup_reset(wakeup_trace);
  147. __raw_spin_unlock(&wakeup_lock);
  148. local_irq_restore(flags);
  149. out:
  150. atomic_dec(&wakeup_trace->data[cpu]->disabled);
  151. }
  152. static void __wakeup_reset(struct trace_array *tr)
  153. {
  154. struct trace_array_cpu *data;
  155. int cpu;
  156. for_each_possible_cpu(cpu) {
  157. data = tr->data[cpu];
  158. tracing_reset(data);
  159. }
  160. wakeup_cpu = -1;
  161. wakeup_prio = -1;
  162. if (wakeup_task)
  163. put_task_struct(wakeup_task);
  164. wakeup_task = NULL;
  165. }
  166. static void wakeup_reset(struct trace_array *tr)
  167. {
  168. unsigned long flags;
  169. local_irq_save(flags);
  170. __raw_spin_lock(&wakeup_lock);
  171. __wakeup_reset(tr);
  172. __raw_spin_unlock(&wakeup_lock);
  173. local_irq_restore(flags);
  174. }
  175. static void
  176. probe_wakeup(struct rq *rq, struct task_struct *p)
  177. {
  178. int cpu = smp_processor_id();
  179. unsigned long flags;
  180. long disabled;
  181. if (likely(!tracer_enabled))
  182. return;
  183. tracing_record_cmdline(p);
  184. tracing_record_cmdline(current);
  185. if (likely(!rt_task(p)) ||
  186. p->prio >= wakeup_prio ||
  187. p->prio >= current->prio)
  188. return;
  189. disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
  190. if (unlikely(disabled != 1))
  191. goto out;
  192. /* interrupts should be off from try_to_wake_up */
  193. __raw_spin_lock(&wakeup_lock);
  194. /* check for races. */
  195. if (!tracer_enabled || p->prio >= wakeup_prio)
  196. goto out_locked;
  197. /* reset the trace */
  198. __wakeup_reset(wakeup_trace);
  199. wakeup_cpu = task_cpu(p);
  200. wakeup_prio = p->prio;
  201. wakeup_task = p;
  202. get_task_struct(wakeup_task);
  203. local_save_flags(flags);
  204. wakeup_trace->data[wakeup_cpu]->preempt_timestamp = ftrace_now(cpu);
  205. trace_function(wakeup_trace, wakeup_trace->data[wakeup_cpu],
  206. CALLER_ADDR1, CALLER_ADDR2, flags);
  207. out_locked:
  208. __raw_spin_unlock(&wakeup_lock);
  209. out:
  210. atomic_dec(&wakeup_trace->data[cpu]->disabled);
  211. }
  212. static void start_wakeup_tracer(struct trace_array *tr)
  213. {
  214. int ret;
  215. ret = register_trace_sched_wakeup(probe_wakeup);
  216. if (ret) {
  217. pr_info("wakeup trace: Couldn't activate tracepoint"
  218. " probe to kernel_sched_wakeup\n");
  219. return;
  220. }
  221. ret = register_trace_sched_wakeup_new(probe_wakeup);
  222. if (ret) {
  223. pr_info("wakeup trace: Couldn't activate tracepoint"
  224. " probe to kernel_sched_wakeup_new\n");
  225. goto fail_deprobe;
  226. }
  227. ret = register_trace_sched_switch(probe_wakeup_sched_switch);
  228. if (ret) {
  229. pr_info("sched trace: Couldn't activate tracepoint"
  230. " probe to kernel_sched_schedule\n");
  231. goto fail_deprobe_wake_new;
  232. }
  233. wakeup_reset(tr);
  234. /*
  235. * Don't let the tracer_enabled = 1 show up before
  236. * the wakeup_task is reset. This may be overkill since
  237. * wakeup_reset does a spin_unlock after setting the
  238. * wakeup_task to NULL, but I want to be safe.
  239. * This is a slow path anyway.
  240. */
  241. smp_wmb();
  242. register_ftrace_function(&trace_ops);
  243. tracer_enabled = 1;
  244. return;
  245. fail_deprobe_wake_new:
  246. unregister_trace_sched_wakeup_new(probe_wakeup);
  247. fail_deprobe:
  248. unregister_trace_sched_wakeup(probe_wakeup);
  249. }
  250. static void stop_wakeup_tracer(struct trace_array *tr)
  251. {
  252. tracer_enabled = 0;
  253. unregister_ftrace_function(&trace_ops);
  254. unregister_trace_sched_switch(probe_wakeup_sched_switch);
  255. unregister_trace_sched_wakeup_new(probe_wakeup);
  256. unregister_trace_sched_wakeup(probe_wakeup);
  257. }
  258. static void wakeup_tracer_init(struct trace_array *tr)
  259. {
  260. wakeup_trace = tr;
  261. if (tr->ctrl)
  262. start_wakeup_tracer(tr);
  263. }
  264. static void wakeup_tracer_reset(struct trace_array *tr)
  265. {
  266. if (tr->ctrl) {
  267. stop_wakeup_tracer(tr);
  268. /* make sure we put back any tasks we are tracing */
  269. wakeup_reset(tr);
  270. }
  271. }
  272. static void wakeup_tracer_ctrl_update(struct trace_array *tr)
  273. {
  274. if (tr->ctrl)
  275. start_wakeup_tracer(tr);
  276. else
  277. stop_wakeup_tracer(tr);
  278. }
  279. static void wakeup_tracer_open(struct trace_iterator *iter)
  280. {
  281. /* stop the trace while dumping */
  282. if (iter->tr->ctrl)
  283. stop_wakeup_tracer(iter->tr);
  284. }
  285. static void wakeup_tracer_close(struct trace_iterator *iter)
  286. {
  287. /* forget about any processes we were recording */
  288. if (iter->tr->ctrl)
  289. start_wakeup_tracer(iter->tr);
  290. }
  291. static struct tracer wakeup_tracer __read_mostly =
  292. {
  293. .name = "wakeup",
  294. .init = wakeup_tracer_init,
  295. .reset = wakeup_tracer_reset,
  296. .open = wakeup_tracer_open,
  297. .close = wakeup_tracer_close,
  298. .ctrl_update = wakeup_tracer_ctrl_update,
  299. .print_max = 1,
  300. #ifdef CONFIG_FTRACE_SELFTEST
  301. .selftest = trace_selftest_startup_wakeup,
  302. #endif
  303. };
  304. __init static int init_wakeup_tracer(void)
  305. {
  306. int ret;
  307. ret = register_tracer(&wakeup_tracer);
  308. if (ret)
  309. return ret;
  310. return 0;
  311. }
  312. device_initcall(init_wakeup_tracer);