trace_sched_wakeup.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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.h"
  19. static struct trace_array *wakeup_trace;
  20. static int __read_mostly tracer_enabled;
  21. static struct task_struct *wakeup_task;
  22. static int wakeup_cpu;
  23. static unsigned wakeup_prio = -1;
  24. static DEFINE_SPINLOCK(wakeup_lock);
  25. static void __wakeup_reset(struct trace_array *tr);
  26. /*
  27. * Should this new latency be reported/recorded?
  28. */
  29. static int report_latency(cycle_t delta)
  30. {
  31. if (tracing_thresh) {
  32. if (delta < tracing_thresh)
  33. return 0;
  34. } else {
  35. if (delta <= tracing_max_latency)
  36. return 0;
  37. }
  38. return 1;
  39. }
  40. void
  41. wakeup_sched_switch(struct task_struct *prev, struct task_struct *next)
  42. {
  43. unsigned long latency = 0, t0 = 0, t1 = 0;
  44. struct trace_array *tr = wakeup_trace;
  45. struct trace_array_cpu *data;
  46. cycle_t T0, T1, delta;
  47. unsigned long flags;
  48. long disabled;
  49. int cpu;
  50. if (unlikely(!tracer_enabled))
  51. return;
  52. /*
  53. * When we start a new trace, we set wakeup_task to NULL
  54. * and then set tracer_enabled = 1. We want to make sure
  55. * that another CPU does not see the tracer_enabled = 1
  56. * and the wakeup_task with an older task, that might
  57. * actually be the same as next.
  58. */
  59. smp_rmb();
  60. if (next != wakeup_task)
  61. return;
  62. /* The task we are waitng for is waking up */
  63. data = tr->data[wakeup_cpu];
  64. /* disable local data, not wakeup_cpu data */
  65. cpu = raw_smp_processor_id();
  66. disabled = atomic_inc_return(&tr->data[cpu]->disabled);
  67. if (likely(disabled != 1))
  68. goto out;
  69. spin_lock_irqsave(&wakeup_lock, flags);
  70. /* We could race with grabbing wakeup_lock */
  71. if (unlikely(!tracer_enabled || next != wakeup_task))
  72. goto out_unlock;
  73. trace_function(tr, data, CALLER_ADDR1, CALLER_ADDR2, flags);
  74. /*
  75. * usecs conversion is slow so we try to delay the conversion
  76. * as long as possible:
  77. */
  78. T0 = data->preempt_timestamp;
  79. T1 = ftrace_now(cpu);
  80. delta = T1-T0;
  81. if (!report_latency(delta))
  82. goto out_unlock;
  83. latency = nsecs_to_usecs(delta);
  84. tracing_max_latency = delta;
  85. t0 = nsecs_to_usecs(T0);
  86. t1 = nsecs_to_usecs(T1);
  87. update_max_tr(tr, wakeup_task, wakeup_cpu);
  88. if (tracing_thresh) {
  89. printk(KERN_INFO "(%16s-%-5d|#%d):"
  90. " %lu us wakeup latency violates %lu us threshold.\n",
  91. wakeup_task->comm, wakeup_task->pid,
  92. raw_smp_processor_id(),
  93. latency, nsecs_to_usecs(tracing_thresh));
  94. } else {
  95. printk(KERN_INFO "(%16s-%-5d|#%d):"
  96. " new %lu us maximum wakeup latency.\n",
  97. wakeup_task->comm, wakeup_task->pid,
  98. cpu, latency);
  99. }
  100. out_unlock:
  101. __wakeup_reset(tr);
  102. spin_unlock_irqrestore(&wakeup_lock, flags);
  103. out:
  104. atomic_dec(&tr->data[cpu]->disabled);
  105. }
  106. static void __wakeup_reset(struct trace_array *tr)
  107. {
  108. struct trace_array_cpu *data;
  109. int cpu;
  110. assert_spin_locked(&wakeup_lock);
  111. for_each_possible_cpu(cpu) {
  112. data = tr->data[cpu];
  113. tracing_reset(data);
  114. }
  115. wakeup_cpu = -1;
  116. wakeup_prio = -1;
  117. if (wakeup_task)
  118. put_task_struct(wakeup_task);
  119. wakeup_task = NULL;
  120. }
  121. static void wakeup_reset(struct trace_array *tr)
  122. {
  123. unsigned long flags;
  124. spin_lock_irqsave(&wakeup_lock, flags);
  125. __wakeup_reset(tr);
  126. spin_unlock_irqrestore(&wakeup_lock, flags);
  127. }
  128. static void
  129. wakeup_check_start(struct trace_array *tr, struct task_struct *p,
  130. struct task_struct *curr)
  131. {
  132. int cpu = smp_processor_id();
  133. unsigned long flags;
  134. long disabled;
  135. if (likely(!rt_task(p)) ||
  136. p->prio >= wakeup_prio ||
  137. p->prio >= curr->prio)
  138. return;
  139. disabled = atomic_inc_return(&tr->data[cpu]->disabled);
  140. if (unlikely(disabled != 1))
  141. goto out;
  142. /* interrupts should be off from try_to_wake_up */
  143. spin_lock(&wakeup_lock);
  144. /* check for races. */
  145. if (!tracer_enabled || p->prio >= wakeup_prio)
  146. goto out_locked;
  147. /* reset the trace */
  148. __wakeup_reset(tr);
  149. wakeup_cpu = task_cpu(p);
  150. wakeup_prio = p->prio;
  151. wakeup_task = p;
  152. get_task_struct(wakeup_task);
  153. local_save_flags(flags);
  154. tr->data[wakeup_cpu]->preempt_timestamp = ftrace_now(cpu);
  155. trace_function(tr, tr->data[wakeup_cpu],
  156. CALLER_ADDR1, CALLER_ADDR2, flags);
  157. out_locked:
  158. spin_unlock(&wakeup_lock);
  159. out:
  160. atomic_dec(&tr->data[cpu]->disabled);
  161. }
  162. void
  163. ftrace_wake_up_task(struct task_struct *wakee, struct task_struct *curr)
  164. {
  165. if (likely(!tracer_enabled))
  166. return;
  167. wakeup_check_start(wakeup_trace, wakee, curr);
  168. }
  169. void
  170. ftrace_wake_up_new_task(struct task_struct *wakee, struct task_struct *curr)
  171. {
  172. if (likely(!tracer_enabled))
  173. return;
  174. wakeup_check_start(wakeup_trace, wakee, curr);
  175. }
  176. static void start_wakeup_tracer(struct trace_array *tr)
  177. {
  178. wakeup_reset(tr);
  179. /*
  180. * Don't let the tracer_enabled = 1 show up before
  181. * the wakeup_task is reset. This may be overkill since
  182. * wakeup_reset does a spin_unlock after setting the
  183. * wakeup_task to NULL, but I want to be safe.
  184. * This is a slow path anyway.
  185. */
  186. smp_wmb();
  187. tracer_enabled = 1;
  188. return;
  189. }
  190. static void stop_wakeup_tracer(struct trace_array *tr)
  191. {
  192. tracer_enabled = 0;
  193. }
  194. static void wakeup_tracer_init(struct trace_array *tr)
  195. {
  196. wakeup_trace = tr;
  197. if (tr->ctrl)
  198. start_wakeup_tracer(tr);
  199. }
  200. static void wakeup_tracer_reset(struct trace_array *tr)
  201. {
  202. if (tr->ctrl) {
  203. stop_wakeup_tracer(tr);
  204. /* make sure we put back any tasks we are tracing */
  205. wakeup_reset(tr);
  206. }
  207. }
  208. static void wakeup_tracer_ctrl_update(struct trace_array *tr)
  209. {
  210. if (tr->ctrl)
  211. start_wakeup_tracer(tr);
  212. else
  213. stop_wakeup_tracer(tr);
  214. }
  215. static void wakeup_tracer_open(struct trace_iterator *iter)
  216. {
  217. /* stop the trace while dumping */
  218. if (iter->tr->ctrl)
  219. stop_wakeup_tracer(iter->tr);
  220. }
  221. static void wakeup_tracer_close(struct trace_iterator *iter)
  222. {
  223. /* forget about any processes we were recording */
  224. if (iter->tr->ctrl)
  225. start_wakeup_tracer(iter->tr);
  226. }
  227. static struct tracer wakeup_tracer __read_mostly =
  228. {
  229. .name = "wakeup",
  230. .init = wakeup_tracer_init,
  231. .reset = wakeup_tracer_reset,
  232. .open = wakeup_tracer_open,
  233. .close = wakeup_tracer_close,
  234. .ctrl_update = wakeup_tracer_ctrl_update,
  235. .print_max = 1,
  236. #ifdef CONFIG_FTRACE_SELFTEST
  237. .selftest = trace_selftest_startup_wakeup,
  238. #endif
  239. };
  240. __init static int init_wakeup_tracer(void)
  241. {
  242. int ret;
  243. ret = register_tracer(&wakeup_tracer);
  244. if (ret)
  245. return ret;
  246. return 0;
  247. }
  248. device_initcall(init_wakeup_tracer);