trace_sched_wakeup.c 9.7 KB

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