trace_sched_switch.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * trace context switch
  3. *
  4. * Copyright (C) 2007 Steven Rostedt <srostedt@redhat.com>
  5. *
  6. */
  7. #include <linux/module.h>
  8. #include <linux/fs.h>
  9. #include <linux/debugfs.h>
  10. #include <linux/kallsyms.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/marker.h>
  13. #include <linux/ftrace.h>
  14. #include "trace.h"
  15. static struct trace_array *ctx_trace;
  16. static int __read_mostly tracer_enabled;
  17. static atomic_t sched_ref;
  18. static void
  19. sched_switch_func(void *private, void *__rq, struct task_struct *prev,
  20. struct task_struct *next)
  21. {
  22. struct trace_array **ptr = private;
  23. struct trace_array *tr = *ptr;
  24. struct trace_array_cpu *data;
  25. unsigned long flags;
  26. long disabled;
  27. int cpu;
  28. tracing_record_cmdline(prev);
  29. tracing_record_cmdline(next);
  30. if (!tracer_enabled)
  31. return;
  32. local_irq_save(flags);
  33. cpu = raw_smp_processor_id();
  34. data = tr->data[cpu];
  35. disabled = atomic_inc_return(&data->disabled);
  36. if (likely(disabled == 1))
  37. tracing_sched_switch_trace(tr, data, prev, next, flags);
  38. atomic_dec(&data->disabled);
  39. local_irq_restore(flags);
  40. }
  41. static notrace void
  42. sched_switch_callback(void *probe_data, void *call_data,
  43. const char *format, va_list *args)
  44. {
  45. struct task_struct *prev;
  46. struct task_struct *next;
  47. struct rq *__rq;
  48. if (!atomic_read(&sched_ref))
  49. return;
  50. /* skip prev_pid %d next_pid %d prev_state %ld */
  51. (void)va_arg(*args, int);
  52. (void)va_arg(*args, int);
  53. (void)va_arg(*args, long);
  54. __rq = va_arg(*args, typeof(__rq));
  55. prev = va_arg(*args, typeof(prev));
  56. next = va_arg(*args, typeof(next));
  57. /*
  58. * If tracer_switch_func only points to the local
  59. * switch func, it still needs the ptr passed to it.
  60. */
  61. sched_switch_func(probe_data, __rq, prev, next);
  62. }
  63. static void
  64. wakeup_func(void *private, void *__rq, struct task_struct *wakee, struct
  65. task_struct *curr)
  66. {
  67. struct trace_array **ptr = private;
  68. struct trace_array *tr = *ptr;
  69. struct trace_array_cpu *data;
  70. unsigned long flags;
  71. long disabled;
  72. int cpu;
  73. if (!tracer_enabled)
  74. return;
  75. tracing_record_cmdline(curr);
  76. local_irq_save(flags);
  77. cpu = raw_smp_processor_id();
  78. data = tr->data[cpu];
  79. disabled = atomic_inc_return(&data->disabled);
  80. if (likely(disabled == 1))
  81. tracing_sched_wakeup_trace(tr, data, wakee, curr, flags);
  82. atomic_dec(&data->disabled);
  83. local_irq_restore(flags);
  84. }
  85. static notrace void
  86. wake_up_callback(void *probe_data, void *call_data,
  87. const char *format, va_list *args)
  88. {
  89. struct task_struct *curr;
  90. struct task_struct *task;
  91. struct rq *__rq;
  92. if (likely(!tracer_enabled))
  93. return;
  94. /* Skip pid %d state %ld */
  95. (void)va_arg(*args, int);
  96. (void)va_arg(*args, long);
  97. /* now get the meat: "rq %p task %p rq->curr %p" */
  98. __rq = va_arg(*args, typeof(__rq));
  99. task = va_arg(*args, typeof(task));
  100. curr = va_arg(*args, typeof(curr));
  101. tracing_record_cmdline(task);
  102. tracing_record_cmdline(curr);
  103. wakeup_func(probe_data, __rq, task, curr);
  104. }
  105. static void sched_switch_reset(struct trace_array *tr)
  106. {
  107. int cpu;
  108. tr->time_start = ftrace_now(tr->cpu);
  109. for_each_online_cpu(cpu)
  110. tracing_reset(tr->data[cpu]);
  111. }
  112. static int tracing_sched_register(void)
  113. {
  114. int ret;
  115. ret = marker_probe_register("kernel_sched_wakeup",
  116. "pid %d state %ld ## rq %p task %p rq->curr %p",
  117. wake_up_callback,
  118. &ctx_trace);
  119. if (ret) {
  120. pr_info("wakeup trace: Couldn't add marker"
  121. " probe to kernel_sched_wakeup\n");
  122. return ret;
  123. }
  124. ret = marker_probe_register("kernel_sched_wakeup_new",
  125. "pid %d state %ld ## rq %p task %p rq->curr %p",
  126. wake_up_callback,
  127. &ctx_trace);
  128. if (ret) {
  129. pr_info("wakeup trace: Couldn't add marker"
  130. " probe to kernel_sched_wakeup_new\n");
  131. goto fail_deprobe;
  132. }
  133. ret = marker_probe_register("kernel_sched_schedule",
  134. "prev_pid %d next_pid %d prev_state %ld "
  135. "## rq %p prev %p next %p",
  136. sched_switch_callback,
  137. &ctx_trace);
  138. if (ret) {
  139. pr_info("sched trace: Couldn't add marker"
  140. " probe to kernel_sched_schedule\n");
  141. goto fail_deprobe_wake_new;
  142. }
  143. return ret;
  144. fail_deprobe_wake_new:
  145. marker_probe_unregister("kernel_sched_wakeup_new",
  146. wake_up_callback,
  147. &ctx_trace);
  148. fail_deprobe:
  149. marker_probe_unregister("kernel_sched_wakeup",
  150. wake_up_callback,
  151. &ctx_trace);
  152. return ret;
  153. }
  154. static void tracing_sched_unregister(void)
  155. {
  156. marker_probe_unregister("kernel_sched_schedule",
  157. sched_switch_callback,
  158. &ctx_trace);
  159. marker_probe_unregister("kernel_sched_wakeup_new",
  160. wake_up_callback,
  161. &ctx_trace);
  162. marker_probe_unregister("kernel_sched_wakeup",
  163. wake_up_callback,
  164. &ctx_trace);
  165. }
  166. static void tracing_start_sched_switch(void)
  167. {
  168. long ref;
  169. ref = atomic_inc_return(&sched_ref);
  170. if (ref == 1)
  171. tracing_sched_register();
  172. }
  173. static void tracing_stop_sched_switch(void)
  174. {
  175. long ref;
  176. ref = atomic_dec_and_test(&sched_ref);
  177. if (ref)
  178. tracing_sched_unregister();
  179. }
  180. void tracing_start_cmdline_record(void)
  181. {
  182. tracing_start_sched_switch();
  183. }
  184. void tracing_stop_cmdline_record(void)
  185. {
  186. tracing_stop_sched_switch();
  187. }
  188. static void start_sched_trace(struct trace_array *tr)
  189. {
  190. sched_switch_reset(tr);
  191. tracing_start_cmdline_record();
  192. tracer_enabled = 1;
  193. }
  194. static void stop_sched_trace(struct trace_array *tr)
  195. {
  196. tracer_enabled = 0;
  197. tracing_stop_cmdline_record();
  198. }
  199. static void sched_switch_trace_init(struct trace_array *tr)
  200. {
  201. ctx_trace = tr;
  202. if (tr->ctrl)
  203. start_sched_trace(tr);
  204. }
  205. static void sched_switch_trace_reset(struct trace_array *tr)
  206. {
  207. if (tr->ctrl)
  208. stop_sched_trace(tr);
  209. }
  210. static void sched_switch_trace_ctrl_update(struct trace_array *tr)
  211. {
  212. /* When starting a new trace, reset the buffers */
  213. if (tr->ctrl)
  214. start_sched_trace(tr);
  215. else
  216. stop_sched_trace(tr);
  217. }
  218. static struct tracer sched_switch_trace __read_mostly =
  219. {
  220. .name = "sched_switch",
  221. .init = sched_switch_trace_init,
  222. .reset = sched_switch_trace_reset,
  223. .ctrl_update = sched_switch_trace_ctrl_update,
  224. #ifdef CONFIG_FTRACE_SELFTEST
  225. .selftest = trace_selftest_startup_sched_switch,
  226. #endif
  227. };
  228. __init static int init_sched_switch_trace(void)
  229. {
  230. int ret = 0;
  231. if (atomic_read(&sched_ref))
  232. ret = tracing_sched_register();
  233. if (ret) {
  234. pr_info("error registering scheduler trace\n");
  235. return ret;
  236. }
  237. return register_tracer(&sched_switch_trace);
  238. }
  239. device_initcall(init_sched_switch_trace);