trace_sched_wakeup.c 8.3 KB

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