trace_sched_wakeup.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 unsigned wakeup_prio = -1;
  25. static int wakeup_rt;
  26. static raw_spinlock_t wakeup_lock =
  27. (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
  28. static void __wakeup_reset(struct trace_array *tr);
  29. static int save_lat_flag;
  30. #ifdef CONFIG_FUNCTION_TRACER
  31. /*
  32. * irqsoff uses its own tracer function to keep the overhead down:
  33. */
  34. static void
  35. wakeup_tracer_call(unsigned long ip, unsigned long parent_ip)
  36. {
  37. struct trace_array *tr = wakeup_trace;
  38. struct trace_array_cpu *data;
  39. unsigned long flags;
  40. long disabled;
  41. int resched;
  42. int cpu;
  43. int pc;
  44. if (likely(!wakeup_task))
  45. return;
  46. pc = preempt_count();
  47. resched = ftrace_preempt_disable();
  48. cpu = raw_smp_processor_id();
  49. data = tr->data[cpu];
  50. disabled = atomic_inc_return(&data->disabled);
  51. if (unlikely(disabled != 1))
  52. goto out;
  53. local_irq_save(flags);
  54. __raw_spin_lock(&wakeup_lock);
  55. if (unlikely(!wakeup_task))
  56. goto unlock;
  57. /*
  58. * The task can't disappear because it needs to
  59. * wake up first, and we have the wakeup_lock.
  60. */
  61. if (task_cpu(wakeup_task) != cpu)
  62. goto unlock;
  63. trace_function(tr, ip, parent_ip, flags, pc);
  64. unlock:
  65. __raw_spin_unlock(&wakeup_lock);
  66. local_irq_restore(flags);
  67. out:
  68. atomic_dec(&data->disabled);
  69. ftrace_preempt_enable(resched);
  70. }
  71. static struct ftrace_ops trace_ops __read_mostly =
  72. {
  73. .func = wakeup_tracer_call,
  74. };
  75. #endif /* CONFIG_FUNCTION_TRACER */
  76. /*
  77. * Should this new latency be reported/recorded?
  78. */
  79. static int report_latency(cycle_t delta)
  80. {
  81. if (tracing_thresh) {
  82. if (delta < tracing_thresh)
  83. return 0;
  84. } else {
  85. if (delta <= tracing_max_latency)
  86. return 0;
  87. }
  88. return 1;
  89. }
  90. static void notrace
  91. probe_wakeup_sched_switch(struct rq *rq, struct task_struct *prev,
  92. struct task_struct *next)
  93. {
  94. unsigned long latency = 0, t0 = 0, t1 = 0;
  95. struct trace_array_cpu *data;
  96. cycle_t T0, T1, delta;
  97. unsigned long flags;
  98. long disabled;
  99. int cpu;
  100. int pc;
  101. tracing_record_cmdline(prev);
  102. if (unlikely(!tracer_enabled))
  103. return;
  104. /*
  105. * When we start a new trace, we set wakeup_task to NULL
  106. * and then set tracer_enabled = 1. We want to make sure
  107. * that another CPU does not see the tracer_enabled = 1
  108. * and the wakeup_task with an older task, that might
  109. * actually be the same as next.
  110. */
  111. smp_rmb();
  112. if (next != wakeup_task)
  113. return;
  114. pc = preempt_count();
  115. /* disable local data, not wakeup_cpu data */
  116. cpu = raw_smp_processor_id();
  117. disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
  118. if (likely(disabled != 1))
  119. goto out;
  120. local_irq_save(flags);
  121. __raw_spin_lock(&wakeup_lock);
  122. /* We could race with grabbing wakeup_lock */
  123. if (unlikely(!tracer_enabled || next != wakeup_task))
  124. goto out_unlock;
  125. /* The task we are waiting for is waking up */
  126. data = wakeup_trace->data[wakeup_cpu];
  127. trace_function(wakeup_trace, CALLER_ADDR0, CALLER_ADDR1, flags, pc);
  128. tracing_sched_switch_trace(wakeup_trace, prev, next, flags, pc);
  129. /*
  130. * usecs conversion is slow so we try to delay the conversion
  131. * as long as possible:
  132. */
  133. T0 = data->preempt_timestamp;
  134. T1 = ftrace_now(cpu);
  135. delta = T1-T0;
  136. if (!report_latency(delta))
  137. goto out_unlock;
  138. latency = nsecs_to_usecs(delta);
  139. tracing_max_latency = delta;
  140. t0 = nsecs_to_usecs(T0);
  141. t1 = nsecs_to_usecs(T1);
  142. update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu);
  143. out_unlock:
  144. __wakeup_reset(wakeup_trace);
  145. __raw_spin_unlock(&wakeup_lock);
  146. local_irq_restore(flags);
  147. out:
  148. atomic_dec(&wakeup_trace->data[cpu]->disabled);
  149. }
  150. static void __wakeup_reset(struct trace_array *tr)
  151. {
  152. int cpu;
  153. for_each_possible_cpu(cpu)
  154. tracing_reset(tr, cpu);
  155. wakeup_cpu = -1;
  156. wakeup_prio = -1;
  157. if (wakeup_task)
  158. put_task_struct(wakeup_task);
  159. wakeup_task = NULL;
  160. }
  161. static void wakeup_reset(struct trace_array *tr)
  162. {
  163. unsigned long flags;
  164. local_irq_save(flags);
  165. __raw_spin_lock(&wakeup_lock);
  166. __wakeup_reset(tr);
  167. __raw_spin_unlock(&wakeup_lock);
  168. local_irq_restore(flags);
  169. }
  170. static void
  171. probe_wakeup(struct rq *rq, struct task_struct *p, int success)
  172. {
  173. struct trace_array_cpu *data;
  174. int cpu = smp_processor_id();
  175. unsigned long flags;
  176. long disabled;
  177. int pc;
  178. if (likely(!tracer_enabled))
  179. return;
  180. tracing_record_cmdline(p);
  181. tracing_record_cmdline(current);
  182. if ((wakeup_rt && !rt_task(p)) ||
  183. p->prio >= wakeup_prio ||
  184. p->prio >= current->prio)
  185. return;
  186. pc = preempt_count();
  187. disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
  188. if (unlikely(disabled != 1))
  189. goto out;
  190. /* interrupts should be off from try_to_wake_up */
  191. __raw_spin_lock(&wakeup_lock);
  192. /* check for races. */
  193. if (!tracer_enabled || p->prio >= wakeup_prio)
  194. goto out_locked;
  195. /* reset the trace */
  196. __wakeup_reset(wakeup_trace);
  197. wakeup_cpu = task_cpu(p);
  198. wakeup_prio = p->prio;
  199. wakeup_task = p;
  200. get_task_struct(wakeup_task);
  201. local_save_flags(flags);
  202. data = wakeup_trace->data[wakeup_cpu];
  203. data->preempt_timestamp = ftrace_now(cpu);
  204. tracing_sched_wakeup_trace(wakeup_trace, p, current, flags, pc);
  205. /*
  206. * We must be careful in using CALLER_ADDR2. But since wake_up
  207. * is not called by an assembly function (where as schedule is)
  208. * it should be safe to use it here.
  209. */
  210. trace_function(wakeup_trace, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
  211. out_locked:
  212. __raw_spin_unlock(&wakeup_lock);
  213. out:
  214. atomic_dec(&wakeup_trace->data[cpu]->disabled);
  215. }
  216. static void start_wakeup_tracer(struct trace_array *tr)
  217. {
  218. int ret;
  219. ret = register_trace_sched_wakeup(probe_wakeup);
  220. if (ret) {
  221. pr_info("wakeup trace: Couldn't activate tracepoint"
  222. " probe to kernel_sched_wakeup\n");
  223. return;
  224. }
  225. ret = register_trace_sched_wakeup_new(probe_wakeup);
  226. if (ret) {
  227. pr_info("wakeup trace: Couldn't activate tracepoint"
  228. " probe to kernel_sched_wakeup_new\n");
  229. goto fail_deprobe;
  230. }
  231. ret = register_trace_sched_switch(probe_wakeup_sched_switch);
  232. if (ret) {
  233. pr_info("sched trace: Couldn't activate tracepoint"
  234. " probe to kernel_sched_switch\n");
  235. goto fail_deprobe_wake_new;
  236. }
  237. wakeup_reset(tr);
  238. /*
  239. * Don't let the tracer_enabled = 1 show up before
  240. * the wakeup_task is reset. This may be overkill since
  241. * wakeup_reset does a spin_unlock after setting the
  242. * wakeup_task to NULL, but I want to be safe.
  243. * This is a slow path anyway.
  244. */
  245. smp_wmb();
  246. register_ftrace_function(&trace_ops);
  247. if (tracing_is_enabled())
  248. tracer_enabled = 1;
  249. else
  250. tracer_enabled = 0;
  251. return;
  252. fail_deprobe_wake_new:
  253. unregister_trace_sched_wakeup_new(probe_wakeup);
  254. fail_deprobe:
  255. unregister_trace_sched_wakeup(probe_wakeup);
  256. }
  257. static void stop_wakeup_tracer(struct trace_array *tr)
  258. {
  259. tracer_enabled = 0;
  260. unregister_ftrace_function(&trace_ops);
  261. unregister_trace_sched_switch(probe_wakeup_sched_switch);
  262. unregister_trace_sched_wakeup_new(probe_wakeup);
  263. unregister_trace_sched_wakeup(probe_wakeup);
  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);