trace_sched_wakeup.c 8.4 KB

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