trace_sched_wakeup.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 = ftrace_preempt_disable();
  46. cpu = raw_smp_processor_id();
  47. data = tr->data[cpu];
  48. disabled = atomic_inc_return(&data->disabled);
  49. if (unlikely(disabled != 1))
  50. goto out;
  51. local_irq_save(flags);
  52. __raw_spin_lock(&wakeup_lock);
  53. if (unlikely(!wakeup_task))
  54. goto unlock;
  55. /*
  56. * The task can't disappear because it needs to
  57. * wake up first, and we have the wakeup_lock.
  58. */
  59. if (task_cpu(wakeup_task) != cpu)
  60. goto unlock;
  61. trace_function(tr, data, ip, parent_ip, flags, pc);
  62. unlock:
  63. __raw_spin_unlock(&wakeup_lock);
  64. local_irq_restore(flags);
  65. out:
  66. atomic_dec(&data->disabled);
  67. ftrace_preempt_enable(resched);
  68. }
  69. static struct ftrace_ops trace_ops __read_mostly =
  70. {
  71. .func = wakeup_tracer_call,
  72. };
  73. #endif /* CONFIG_FUNCTION_TRACER */
  74. /*
  75. * Should this new latency be reported/recorded?
  76. */
  77. static int report_latency(cycle_t delta)
  78. {
  79. if (tracing_thresh) {
  80. if (delta < tracing_thresh)
  81. return 0;
  82. } else {
  83. if (delta <= tracing_max_latency)
  84. return 0;
  85. }
  86. return 1;
  87. }
  88. static void notrace
  89. probe_wakeup_sched_switch(struct rq *rq, struct task_struct *prev,
  90. struct task_struct *next)
  91. {
  92. unsigned long latency = 0, t0 = 0, t1 = 0;
  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. /* The task we are waiting for is waking up */
  114. data = wakeup_trace->data[wakeup_cpu];
  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. trace_function(wakeup_trace, data, CALLER_ADDR1, CALLER_ADDR2, flags, pc);
  126. /*
  127. * usecs conversion is slow so we try to delay the conversion
  128. * as long as possible:
  129. */
  130. T0 = data->preempt_timestamp;
  131. T1 = ftrace_now(cpu);
  132. delta = T1-T0;
  133. if (!report_latency(delta))
  134. goto out_unlock;
  135. latency = nsecs_to_usecs(delta);
  136. tracing_max_latency = delta;
  137. t0 = nsecs_to_usecs(T0);
  138. t1 = nsecs_to_usecs(T1);
  139. update_max_tr(wakeup_trace, wakeup_task, wakeup_cpu);
  140. out_unlock:
  141. __wakeup_reset(wakeup_trace);
  142. __raw_spin_unlock(&wakeup_lock);
  143. local_irq_restore(flags);
  144. out:
  145. atomic_dec(&wakeup_trace->data[cpu]->disabled);
  146. }
  147. static void __wakeup_reset(struct trace_array *tr)
  148. {
  149. struct trace_array_cpu *data;
  150. int cpu;
  151. for_each_possible_cpu(cpu) {
  152. data = tr->data[cpu];
  153. tracing_reset(tr, cpu);
  154. }
  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. int cpu = smp_processor_id();
  174. unsigned long flags;
  175. long disabled;
  176. int pc;
  177. if (likely(!tracer_enabled))
  178. return;
  179. tracing_record_cmdline(p);
  180. tracing_record_cmdline(current);
  181. if (likely(!rt_task(p)) ||
  182. p->prio >= wakeup_prio ||
  183. p->prio >= current->prio)
  184. return;
  185. pc = preempt_count();
  186. disabled = atomic_inc_return(&wakeup_trace->data[cpu]->disabled);
  187. if (unlikely(disabled != 1))
  188. goto out;
  189. /* interrupts should be off from try_to_wake_up */
  190. __raw_spin_lock(&wakeup_lock);
  191. /* check for races. */
  192. if (!tracer_enabled || p->prio >= wakeup_prio)
  193. goto out_locked;
  194. /* reset the trace */
  195. __wakeup_reset(wakeup_trace);
  196. wakeup_cpu = task_cpu(p);
  197. wakeup_prio = p->prio;
  198. wakeup_task = p;
  199. get_task_struct(wakeup_task);
  200. local_save_flags(flags);
  201. wakeup_trace->data[wakeup_cpu]->preempt_timestamp = ftrace_now(cpu);
  202. trace_function(wakeup_trace, wakeup_trace->data[wakeup_cpu],
  203. CALLER_ADDR1, CALLER_ADDR2, flags, pc);
  204. out_locked:
  205. __raw_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);
  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);
  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);
  225. if (ret) {
  226. pr_info("sched trace: Couldn't activate tracepoint"
  227. " probe to kernel_sched_schedule\n");
  228. goto fail_deprobe_wake_new;
  229. }
  230. wakeup_reset(tr);
  231. /*
  232. * Don't let the tracer_enabled = 1 show up before
  233. * the wakeup_task is reset. This may be overkill since
  234. * wakeup_reset does a spin_unlock after setting the
  235. * wakeup_task to NULL, but I want to be safe.
  236. * This is a slow path anyway.
  237. */
  238. smp_wmb();
  239. register_ftrace_function(&trace_ops);
  240. if (tracing_is_enabled())
  241. tracer_enabled = 1;
  242. else
  243. tracer_enabled = 0;
  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 int wakeup_tracer_init(struct trace_array *tr)
  259. {
  260. tracing_max_latency = 0;
  261. wakeup_trace = tr;
  262. start_wakeup_tracer(tr);
  263. return 0;
  264. }
  265. static void wakeup_tracer_reset(struct trace_array *tr)
  266. {
  267. stop_wakeup_tracer(tr);
  268. /* make sure we put back any tasks we are tracing */
  269. wakeup_reset(tr);
  270. }
  271. static void wakeup_tracer_start(struct trace_array *tr)
  272. {
  273. wakeup_reset(tr);
  274. tracer_enabled = 1;
  275. }
  276. static void wakeup_tracer_stop(struct trace_array *tr)
  277. {
  278. tracer_enabled = 0;
  279. }
  280. static struct tracer wakeup_tracer __read_mostly =
  281. {
  282. .name = "wakeup",
  283. .init = wakeup_tracer_init,
  284. .reset = wakeup_tracer_reset,
  285. .start = wakeup_tracer_start,
  286. .stop = wakeup_tracer_stop,
  287. .print_max = 1,
  288. #ifdef CONFIG_FTRACE_SELFTEST
  289. .selftest = trace_selftest_startup_wakeup,
  290. #endif
  291. };
  292. __init static int init_wakeup_tracer(void)
  293. {
  294. int ret;
  295. ret = register_tracer(&wakeup_tracer);
  296. if (ret)
  297. return ret;
  298. return 0;
  299. }
  300. device_initcall(init_wakeup_tracer);