trace_sched_wakeup.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. out_unlock:
  89. __wakeup_reset(tr);
  90. spin_unlock_irqrestore(&wakeup_lock, flags);
  91. out:
  92. atomic_dec(&tr->data[cpu]->disabled);
  93. }
  94. static void __wakeup_reset(struct trace_array *tr)
  95. {
  96. struct trace_array_cpu *data;
  97. int cpu;
  98. assert_spin_locked(&wakeup_lock);
  99. for_each_possible_cpu(cpu) {
  100. data = tr->data[cpu];
  101. tracing_reset(data);
  102. }
  103. wakeup_cpu = -1;
  104. wakeup_prio = -1;
  105. if (wakeup_task)
  106. put_task_struct(wakeup_task);
  107. wakeup_task = NULL;
  108. }
  109. static void wakeup_reset(struct trace_array *tr)
  110. {
  111. unsigned long flags;
  112. spin_lock_irqsave(&wakeup_lock, flags);
  113. __wakeup_reset(tr);
  114. spin_unlock_irqrestore(&wakeup_lock, flags);
  115. }
  116. static void
  117. wakeup_check_start(struct trace_array *tr, struct task_struct *p,
  118. struct task_struct *curr)
  119. {
  120. int cpu = smp_processor_id();
  121. unsigned long flags;
  122. long disabled;
  123. if (likely(!rt_task(p)) ||
  124. p->prio >= wakeup_prio ||
  125. p->prio >= curr->prio)
  126. return;
  127. disabled = atomic_inc_return(&tr->data[cpu]->disabled);
  128. if (unlikely(disabled != 1))
  129. goto out;
  130. /* interrupts should be off from try_to_wake_up */
  131. spin_lock(&wakeup_lock);
  132. /* check for races. */
  133. if (!tracer_enabled || p->prio >= wakeup_prio)
  134. goto out_locked;
  135. /* reset the trace */
  136. __wakeup_reset(tr);
  137. wakeup_cpu = task_cpu(p);
  138. wakeup_prio = p->prio;
  139. wakeup_task = p;
  140. get_task_struct(wakeup_task);
  141. local_save_flags(flags);
  142. tr->data[wakeup_cpu]->preempt_timestamp = ftrace_now(cpu);
  143. trace_function(tr, tr->data[wakeup_cpu],
  144. CALLER_ADDR1, CALLER_ADDR2, flags);
  145. out_locked:
  146. spin_unlock(&wakeup_lock);
  147. out:
  148. atomic_dec(&tr->data[cpu]->disabled);
  149. }
  150. void wakeup_sched_wakeup(struct task_struct *wakee, struct task_struct *curr)
  151. {
  152. if (likely(!tracer_enabled))
  153. return;
  154. tracing_record_cmdline(curr);
  155. tracing_record_cmdline(wakee);
  156. wakeup_check_start(wakeup_trace, wakee, curr);
  157. }
  158. static void start_wakeup_tracer(struct trace_array *tr)
  159. {
  160. wakeup_reset(tr);
  161. /*
  162. * Don't let the tracer_enabled = 1 show up before
  163. * the wakeup_task is reset. This may be overkill since
  164. * wakeup_reset does a spin_unlock after setting the
  165. * wakeup_task to NULL, but I want to be safe.
  166. * This is a slow path anyway.
  167. */
  168. smp_wmb();
  169. tracer_enabled = 1;
  170. return;
  171. }
  172. static void stop_wakeup_tracer(struct trace_array *tr)
  173. {
  174. tracer_enabled = 0;
  175. }
  176. static void wakeup_tracer_init(struct trace_array *tr)
  177. {
  178. wakeup_trace = tr;
  179. if (tr->ctrl)
  180. start_wakeup_tracer(tr);
  181. }
  182. static void wakeup_tracer_reset(struct trace_array *tr)
  183. {
  184. if (tr->ctrl) {
  185. stop_wakeup_tracer(tr);
  186. /* make sure we put back any tasks we are tracing */
  187. wakeup_reset(tr);
  188. }
  189. }
  190. static void wakeup_tracer_ctrl_update(struct trace_array *tr)
  191. {
  192. if (tr->ctrl)
  193. start_wakeup_tracer(tr);
  194. else
  195. stop_wakeup_tracer(tr);
  196. }
  197. static void wakeup_tracer_open(struct trace_iterator *iter)
  198. {
  199. /* stop the trace while dumping */
  200. if (iter->tr->ctrl)
  201. stop_wakeup_tracer(iter->tr);
  202. }
  203. static void wakeup_tracer_close(struct trace_iterator *iter)
  204. {
  205. /* forget about any processes we were recording */
  206. if (iter->tr->ctrl)
  207. start_wakeup_tracer(iter->tr);
  208. }
  209. static struct tracer wakeup_tracer __read_mostly =
  210. {
  211. .name = "wakeup",
  212. .init = wakeup_tracer_init,
  213. .reset = wakeup_tracer_reset,
  214. .open = wakeup_tracer_open,
  215. .close = wakeup_tracer_close,
  216. .ctrl_update = wakeup_tracer_ctrl_update,
  217. .print_max = 1,
  218. #ifdef CONFIG_FTRACE_SELFTEST
  219. .selftest = trace_selftest_startup_wakeup,
  220. #endif
  221. };
  222. __init static int init_wakeup_tracer(void)
  223. {
  224. int ret;
  225. ret = register_tracer(&wakeup_tracer);
  226. if (ret)
  227. return ret;
  228. return 0;
  229. }
  230. device_initcall(init_wakeup_tracer);