trace_sched_wakeup.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. /*
  210. * save_tracer_enabled is used to save the state of the tracer_enabled
  211. * variable when we disable it when we open a trace output file.
  212. */
  213. static int save_tracer_enabled;
  214. static void start_wakeup_tracer(struct trace_array *tr)
  215. {
  216. int ret;
  217. ret = register_trace_sched_wakeup(probe_wakeup);
  218. if (ret) {
  219. pr_info("wakeup trace: Couldn't activate tracepoint"
  220. " probe to kernel_sched_wakeup\n");
  221. return;
  222. }
  223. ret = register_trace_sched_wakeup_new(probe_wakeup);
  224. if (ret) {
  225. pr_info("wakeup trace: Couldn't activate tracepoint"
  226. " probe to kernel_sched_wakeup_new\n");
  227. goto fail_deprobe;
  228. }
  229. ret = register_trace_sched_switch(probe_wakeup_sched_switch);
  230. if (ret) {
  231. pr_info("sched trace: Couldn't activate tracepoint"
  232. " probe to kernel_sched_schedule\n");
  233. goto fail_deprobe_wake_new;
  234. }
  235. wakeup_reset(tr);
  236. /*
  237. * Don't let the tracer_enabled = 1 show up before
  238. * the wakeup_task is reset. This may be overkill since
  239. * wakeup_reset does a spin_unlock after setting the
  240. * wakeup_task to NULL, but I want to be safe.
  241. * This is a slow path anyway.
  242. */
  243. smp_wmb();
  244. register_ftrace_function(&trace_ops);
  245. if (tracing_is_enabled()) {
  246. tracer_enabled = 1;
  247. save_tracer_enabled = 1;
  248. } else {
  249. tracer_enabled = 0;
  250. save_tracer_enabled = 0;
  251. }
  252. return;
  253. fail_deprobe_wake_new:
  254. unregister_trace_sched_wakeup_new(probe_wakeup);
  255. fail_deprobe:
  256. unregister_trace_sched_wakeup(probe_wakeup);
  257. }
  258. static void stop_wakeup_tracer(struct trace_array *tr)
  259. {
  260. tracer_enabled = 0;
  261. save_tracer_enabled = 0;
  262. unregister_ftrace_function(&trace_ops);
  263. unregister_trace_sched_switch(probe_wakeup_sched_switch);
  264. unregister_trace_sched_wakeup_new(probe_wakeup);
  265. unregister_trace_sched_wakeup(probe_wakeup);
  266. }
  267. static int wakeup_tracer_init(struct trace_array *tr)
  268. {
  269. tracing_max_latency = 0;
  270. wakeup_trace = tr;
  271. start_wakeup_tracer(tr);
  272. return 0;
  273. }
  274. static void wakeup_tracer_reset(struct trace_array *tr)
  275. {
  276. stop_wakeup_tracer(tr);
  277. /* make sure we put back any tasks we are tracing */
  278. wakeup_reset(tr);
  279. }
  280. static void wakeup_tracer_start(struct trace_array *tr)
  281. {
  282. wakeup_reset(tr);
  283. tracer_enabled = 1;
  284. save_tracer_enabled = 1;
  285. }
  286. static void wakeup_tracer_stop(struct trace_array *tr)
  287. {
  288. tracer_enabled = 0;
  289. save_tracer_enabled = 0;
  290. }
  291. static void wakeup_tracer_open(struct trace_iterator *iter)
  292. {
  293. /* stop the trace while dumping */
  294. tracer_enabled = 0;
  295. }
  296. static void wakeup_tracer_close(struct trace_iterator *iter)
  297. {
  298. /* forget about any processes we were recording */
  299. if (save_tracer_enabled) {
  300. wakeup_reset(iter->tr);
  301. tracer_enabled = 1;
  302. }
  303. }
  304. static struct tracer wakeup_tracer __read_mostly =
  305. {
  306. .name = "wakeup",
  307. .init = wakeup_tracer_init,
  308. .reset = wakeup_tracer_reset,
  309. .start = wakeup_tracer_start,
  310. .stop = wakeup_tracer_stop,
  311. .open = wakeup_tracer_open,
  312. .close = wakeup_tracer_close,
  313. .print_max = 1,
  314. #ifdef CONFIG_FTRACE_SELFTEST
  315. .selftest = trace_selftest_startup_wakeup,
  316. #endif
  317. };
  318. __init static int init_wakeup_tracer(void)
  319. {
  320. int ret;
  321. ret = register_tracer(&wakeup_tracer);
  322. if (ret)
  323. return ret;
  324. return 0;
  325. }
  326. device_initcall(init_wakeup_tracer);