trace_sched_wakeup.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 <linux/marker.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_FTRACE
  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. if (likely(!wakeup_task))
  42. return;
  43. resched = need_resched();
  44. preempt_disable_notrace();
  45. cpu = raw_smp_processor_id();
  46. data = tr->data[cpu];
  47. disabled = atomic_inc_return(&data->disabled);
  48. if (unlikely(disabled != 1))
  49. goto out;
  50. local_irq_save(flags);
  51. __raw_spin_lock(&wakeup_lock);
  52. if (unlikely(!wakeup_task))
  53. goto unlock;
  54. /*
  55. * The task can't disappear because it needs to
  56. * wake up first, and we have the wakeup_lock.
  57. */
  58. if (task_cpu(wakeup_task) != cpu)
  59. goto unlock;
  60. trace_function(tr, data, ip, parent_ip, flags);
  61. unlock:
  62. __raw_spin_unlock(&wakeup_lock);
  63. local_irq_restore(flags);
  64. out:
  65. atomic_dec(&data->disabled);
  66. /*
  67. * To prevent recursion from the scheduler, if the
  68. * resched flag was set before we entered, then
  69. * don't reschedule.
  70. */
  71. if (resched)
  72. preempt_enable_no_resched_notrace();
  73. else
  74. preempt_enable_notrace();
  75. }
  76. static struct ftrace_ops trace_ops __read_mostly =
  77. {
  78. .func = wakeup_tracer_call,
  79. };
  80. #endif /* CONFIG_FTRACE */
  81. /*
  82. * Should this new latency be reported/recorded?
  83. */
  84. static int report_latency(cycle_t delta)
  85. {
  86. if (tracing_thresh) {
  87. if (delta < tracing_thresh)
  88. return 0;
  89. } else {
  90. if (delta <= tracing_max_latency)
  91. return 0;
  92. }
  93. return 1;
  94. }
  95. static void notrace
  96. wakeup_sched_switch(void *private, void *rq, struct task_struct *prev,
  97. struct task_struct *next)
  98. {
  99. unsigned long latency = 0, t0 = 0, t1 = 0;
  100. struct trace_array **ptr = private;
  101. struct trace_array *tr = *ptr;
  102. struct trace_array_cpu *data;
  103. cycle_t T0, T1, delta;
  104. unsigned long flags;
  105. long disabled;
  106. int cpu;
  107. if (unlikely(!tracer_enabled))
  108. return;
  109. /*
  110. * When we start a new trace, we set wakeup_task to NULL
  111. * and then set tracer_enabled = 1. We want to make sure
  112. * that another CPU does not see the tracer_enabled = 1
  113. * and the wakeup_task with an older task, that might
  114. * actually be the same as next.
  115. */
  116. smp_rmb();
  117. if (next != wakeup_task)
  118. return;
  119. /* The task we are waiting for is waking up */
  120. data = tr->data[wakeup_cpu];
  121. /* disable local data, not wakeup_cpu data */
  122. cpu = raw_smp_processor_id();
  123. disabled = atomic_inc_return(&tr->data[cpu]->disabled);
  124. if (likely(disabled != 1))
  125. goto out;
  126. local_irq_save(flags);
  127. __raw_spin_lock(&wakeup_lock);
  128. /* We could race with grabbing wakeup_lock */
  129. if (unlikely(!tracer_enabled || next != wakeup_task))
  130. goto out_unlock;
  131. trace_function(tr, data, CALLER_ADDR1, CALLER_ADDR2, flags);
  132. /*
  133. * usecs conversion is slow so we try to delay the conversion
  134. * as long as possible:
  135. */
  136. T0 = data->preempt_timestamp;
  137. T1 = ftrace_now(cpu);
  138. delta = T1-T0;
  139. if (!report_latency(delta))
  140. goto out_unlock;
  141. latency = nsecs_to_usecs(delta);
  142. tracing_max_latency = delta;
  143. t0 = nsecs_to_usecs(T0);
  144. t1 = nsecs_to_usecs(T1);
  145. update_max_tr(tr, wakeup_task, wakeup_cpu);
  146. out_unlock:
  147. __wakeup_reset(tr);
  148. __raw_spin_unlock(&wakeup_lock);
  149. local_irq_restore(flags);
  150. out:
  151. atomic_dec(&tr->data[cpu]->disabled);
  152. }
  153. static notrace void
  154. sched_switch_callback(void *probe_data, void *call_data,
  155. const char *format, va_list *args)
  156. {
  157. struct task_struct *prev;
  158. struct task_struct *next;
  159. struct rq *__rq;
  160. /* skip prev_pid %d next_pid %d prev_state %ld */
  161. (void)va_arg(*args, int);
  162. (void)va_arg(*args, int);
  163. (void)va_arg(*args, long);
  164. __rq = va_arg(*args, typeof(__rq));
  165. prev = va_arg(*args, typeof(prev));
  166. next = va_arg(*args, typeof(next));
  167. tracing_record_cmdline(prev);
  168. /*
  169. * If tracer_switch_func only points to the local
  170. * switch func, it still needs the ptr passed to it.
  171. */
  172. wakeup_sched_switch(probe_data, __rq, prev, next);
  173. }
  174. static void __wakeup_reset(struct trace_array *tr)
  175. {
  176. struct trace_array_cpu *data;
  177. int cpu;
  178. for_each_possible_cpu(cpu) {
  179. data = tr->data[cpu];
  180. tracing_reset(data);
  181. }
  182. wakeup_cpu = -1;
  183. wakeup_prio = -1;
  184. if (wakeup_task)
  185. put_task_struct(wakeup_task);
  186. wakeup_task = NULL;
  187. }
  188. static void wakeup_reset(struct trace_array *tr)
  189. {
  190. unsigned long flags;
  191. local_irq_save(flags);
  192. __raw_spin_lock(&wakeup_lock);
  193. __wakeup_reset(tr);
  194. __raw_spin_unlock(&wakeup_lock);
  195. local_irq_restore(flags);
  196. }
  197. static void
  198. wakeup_check_start(struct trace_array *tr, struct task_struct *p,
  199. struct task_struct *curr)
  200. {
  201. int cpu = smp_processor_id();
  202. unsigned long flags;
  203. long disabled;
  204. if (likely(!rt_task(p)) ||
  205. p->prio >= wakeup_prio ||
  206. p->prio >= curr->prio)
  207. return;
  208. disabled = atomic_inc_return(&tr->data[cpu]->disabled);
  209. if (unlikely(disabled != 1))
  210. goto out;
  211. /* interrupts should be off from try_to_wake_up */
  212. __raw_spin_lock(&wakeup_lock);
  213. /* check for races. */
  214. if (!tracer_enabled || p->prio >= wakeup_prio)
  215. goto out_locked;
  216. /* reset the trace */
  217. __wakeup_reset(tr);
  218. wakeup_cpu = task_cpu(p);
  219. wakeup_prio = p->prio;
  220. wakeup_task = p;
  221. get_task_struct(wakeup_task);
  222. local_save_flags(flags);
  223. tr->data[wakeup_cpu]->preempt_timestamp = ftrace_now(cpu);
  224. trace_function(tr, tr->data[wakeup_cpu],
  225. CALLER_ADDR1, CALLER_ADDR2, flags);
  226. out_locked:
  227. __raw_spin_unlock(&wakeup_lock);
  228. out:
  229. atomic_dec(&tr->data[cpu]->disabled);
  230. }
  231. static notrace void
  232. wake_up_callback(void *probe_data, void *call_data,
  233. const char *format, va_list *args)
  234. {
  235. struct trace_array **ptr = probe_data;
  236. struct trace_array *tr = *ptr;
  237. struct task_struct *curr;
  238. struct task_struct *task;
  239. struct rq *__rq;
  240. if (likely(!tracer_enabled))
  241. return;
  242. /* Skip pid %d state %ld */
  243. (void)va_arg(*args, int);
  244. (void)va_arg(*args, long);
  245. /* now get the meat: "rq %p task %p rq->curr %p" */
  246. __rq = va_arg(*args, typeof(__rq));
  247. task = va_arg(*args, typeof(task));
  248. curr = va_arg(*args, typeof(curr));
  249. tracing_record_cmdline(task);
  250. tracing_record_cmdline(curr);
  251. wakeup_check_start(tr, task, curr);
  252. }
  253. static void start_wakeup_tracer(struct trace_array *tr)
  254. {
  255. int ret;
  256. ret = marker_probe_register("kernel_sched_wakeup",
  257. "pid %d state %ld ## rq %p task %p rq->curr %p",
  258. wake_up_callback,
  259. &wakeup_trace);
  260. if (ret) {
  261. pr_info("wakeup trace: Couldn't add marker"
  262. " probe to kernel_sched_wakeup\n");
  263. return;
  264. }
  265. ret = marker_probe_register("kernel_sched_wakeup_new",
  266. "pid %d state %ld ## rq %p task %p rq->curr %p",
  267. wake_up_callback,
  268. &wakeup_trace);
  269. if (ret) {
  270. pr_info("wakeup trace: Couldn't add marker"
  271. " probe to kernel_sched_wakeup_new\n");
  272. goto fail_deprobe;
  273. }
  274. ret = marker_probe_register("kernel_sched_schedule",
  275. "prev_pid %d next_pid %d prev_state %ld "
  276. "## rq %p prev %p next %p",
  277. sched_switch_callback,
  278. &wakeup_trace);
  279. if (ret) {
  280. pr_info("sched trace: Couldn't add marker"
  281. " probe to kernel_sched_schedule\n");
  282. goto fail_deprobe_wake_new;
  283. }
  284. wakeup_reset(tr);
  285. /*
  286. * Don't let the tracer_enabled = 1 show up before
  287. * the wakeup_task is reset. This may be overkill since
  288. * wakeup_reset does a spin_unlock after setting the
  289. * wakeup_task to NULL, but I want to be safe.
  290. * This is a slow path anyway.
  291. */
  292. smp_wmb();
  293. register_ftrace_function(&trace_ops);
  294. tracer_enabled = 1;
  295. return;
  296. fail_deprobe_wake_new:
  297. marker_probe_unregister("kernel_sched_wakeup_new",
  298. wake_up_callback,
  299. &wakeup_trace);
  300. fail_deprobe:
  301. marker_probe_unregister("kernel_sched_wakeup",
  302. wake_up_callback,
  303. &wakeup_trace);
  304. }
  305. static void stop_wakeup_tracer(struct trace_array *tr)
  306. {
  307. tracer_enabled = 0;
  308. unregister_ftrace_function(&trace_ops);
  309. marker_probe_unregister("kernel_sched_schedule",
  310. sched_switch_callback,
  311. &wakeup_trace);
  312. marker_probe_unregister("kernel_sched_wakeup_new",
  313. wake_up_callback,
  314. &wakeup_trace);
  315. marker_probe_unregister("kernel_sched_wakeup",
  316. wake_up_callback,
  317. &wakeup_trace);
  318. }
  319. static void wakeup_tracer_init(struct trace_array *tr)
  320. {
  321. wakeup_trace = tr;
  322. if (tr->ctrl)
  323. start_wakeup_tracer(tr);
  324. }
  325. static void wakeup_tracer_reset(struct trace_array *tr)
  326. {
  327. if (tr->ctrl) {
  328. stop_wakeup_tracer(tr);
  329. /* make sure we put back any tasks we are tracing */
  330. wakeup_reset(tr);
  331. }
  332. }
  333. static void wakeup_tracer_ctrl_update(struct trace_array *tr)
  334. {
  335. if (tr->ctrl)
  336. start_wakeup_tracer(tr);
  337. else
  338. stop_wakeup_tracer(tr);
  339. }
  340. static void wakeup_tracer_open(struct trace_iterator *iter)
  341. {
  342. /* stop the trace while dumping */
  343. if (iter->tr->ctrl)
  344. stop_wakeup_tracer(iter->tr);
  345. }
  346. static void wakeup_tracer_close(struct trace_iterator *iter)
  347. {
  348. /* forget about any processes we were recording */
  349. if (iter->tr->ctrl)
  350. start_wakeup_tracer(iter->tr);
  351. }
  352. static struct tracer wakeup_tracer __read_mostly =
  353. {
  354. .name = "wakeup",
  355. .init = wakeup_tracer_init,
  356. .reset = wakeup_tracer_reset,
  357. .open = wakeup_tracer_open,
  358. .close = wakeup_tracer_close,
  359. .ctrl_update = wakeup_tracer_ctrl_update,
  360. .print_max = 1,
  361. #ifdef CONFIG_FTRACE_SELFTEST
  362. .selftest = trace_selftest_startup_wakeup,
  363. #endif
  364. };
  365. __init static int init_wakeup_tracer(void)
  366. {
  367. int ret;
  368. ret = register_tracer(&wakeup_tracer);
  369. if (ret)
  370. return ret;
  371. return 0;
  372. }
  373. device_initcall(init_wakeup_tracer);