trace_sched_switch.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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/ftrace.h>
  13. #include <trace/events/sched.h>
  14. #include "trace.h"
  15. static struct trace_array *ctx_trace;
  16. static int __read_mostly tracer_enabled;
  17. static int sched_ref;
  18. static DEFINE_MUTEX(sched_register_mutex);
  19. static int sched_stopped;
  20. static void
  21. probe_sched_switch(struct rq *__rq, struct task_struct *prev,
  22. struct task_struct *next)
  23. {
  24. struct trace_array_cpu *data;
  25. unsigned long flags;
  26. int cpu;
  27. int pc;
  28. if (unlikely(!sched_ref))
  29. return;
  30. tracing_record_cmdline(prev);
  31. tracing_record_cmdline(next);
  32. if (!tracer_enabled || sched_stopped)
  33. return;
  34. pc = preempt_count();
  35. local_irq_save(flags);
  36. cpu = raw_smp_processor_id();
  37. data = ctx_trace->data[cpu];
  38. if (likely(!atomic_read(&data->disabled)))
  39. tracing_sched_switch_trace(ctx_trace, prev, next, flags, pc);
  40. local_irq_restore(flags);
  41. }
  42. static void
  43. probe_sched_wakeup(struct rq *__rq, struct task_struct *wakee, int success)
  44. {
  45. struct trace_array_cpu *data;
  46. unsigned long flags;
  47. int cpu, pc;
  48. if (unlikely(!sched_ref))
  49. return;
  50. tracing_record_cmdline(current);
  51. if (!tracer_enabled || sched_stopped)
  52. return;
  53. pc = preempt_count();
  54. local_irq_save(flags);
  55. cpu = raw_smp_processor_id();
  56. data = ctx_trace->data[cpu];
  57. if (likely(!atomic_read(&data->disabled)))
  58. tracing_sched_wakeup_trace(ctx_trace, wakee, current,
  59. flags, pc);
  60. local_irq_restore(flags);
  61. }
  62. static int tracing_sched_register(void)
  63. {
  64. int ret;
  65. ret = register_trace_sched_wakeup(probe_sched_wakeup);
  66. if (ret) {
  67. pr_info("wakeup trace: Couldn't activate tracepoint"
  68. " probe to kernel_sched_wakeup\n");
  69. return ret;
  70. }
  71. ret = register_trace_sched_wakeup_new(probe_sched_wakeup);
  72. if (ret) {
  73. pr_info("wakeup trace: Couldn't activate tracepoint"
  74. " probe to kernel_sched_wakeup_new\n");
  75. goto fail_deprobe;
  76. }
  77. ret = register_trace_sched_switch(probe_sched_switch);
  78. if (ret) {
  79. pr_info("sched trace: Couldn't activate tracepoint"
  80. " probe to kernel_sched_switch\n");
  81. goto fail_deprobe_wake_new;
  82. }
  83. return ret;
  84. fail_deprobe_wake_new:
  85. unregister_trace_sched_wakeup_new(probe_sched_wakeup);
  86. fail_deprobe:
  87. unregister_trace_sched_wakeup(probe_sched_wakeup);
  88. return ret;
  89. }
  90. static void tracing_sched_unregister(void)
  91. {
  92. unregister_trace_sched_switch(probe_sched_switch);
  93. unregister_trace_sched_wakeup_new(probe_sched_wakeup);
  94. unregister_trace_sched_wakeup(probe_sched_wakeup);
  95. }
  96. static void tracing_start_sched_switch(void)
  97. {
  98. mutex_lock(&sched_register_mutex);
  99. if (!(sched_ref++))
  100. tracing_sched_register();
  101. mutex_unlock(&sched_register_mutex);
  102. }
  103. static void tracing_stop_sched_switch(void)
  104. {
  105. mutex_lock(&sched_register_mutex);
  106. if (!(--sched_ref))
  107. tracing_sched_unregister();
  108. mutex_unlock(&sched_register_mutex);
  109. }
  110. void tracing_start_cmdline_record(void)
  111. {
  112. tracing_start_sched_switch();
  113. }
  114. void tracing_stop_cmdline_record(void)
  115. {
  116. tracing_stop_sched_switch();
  117. }
  118. /**
  119. * tracing_start_sched_switch_record - start tracing context switches
  120. *
  121. * Turns on context switch tracing for a tracer.
  122. */
  123. void tracing_start_sched_switch_record(void)
  124. {
  125. if (unlikely(!ctx_trace)) {
  126. WARN_ON(1);
  127. return;
  128. }
  129. tracing_start_sched_switch();
  130. mutex_lock(&sched_register_mutex);
  131. tracer_enabled++;
  132. mutex_unlock(&sched_register_mutex);
  133. }
  134. /**
  135. * tracing_stop_sched_switch_record - start tracing context switches
  136. *
  137. * Turns off context switch tracing for a tracer.
  138. */
  139. void tracing_stop_sched_switch_record(void)
  140. {
  141. mutex_lock(&sched_register_mutex);
  142. tracer_enabled--;
  143. WARN_ON(tracer_enabled < 0);
  144. mutex_unlock(&sched_register_mutex);
  145. tracing_stop_sched_switch();
  146. }
  147. /**
  148. * tracing_sched_switch_assign_trace - assign a trace array for ctx switch
  149. * @tr: trace array pointer to assign
  150. *
  151. * Some tracers might want to record the context switches in their
  152. * trace. This function lets those tracers assign the trace array
  153. * to use.
  154. */
  155. void tracing_sched_switch_assign_trace(struct trace_array *tr)
  156. {
  157. ctx_trace = tr;
  158. }
  159. static void stop_sched_trace(struct trace_array *tr)
  160. {
  161. tracing_stop_sched_switch_record();
  162. }
  163. static int sched_switch_trace_init(struct trace_array *tr)
  164. {
  165. ctx_trace = tr;
  166. tracing_reset_online_cpus(tr);
  167. tracing_start_sched_switch_record();
  168. return 0;
  169. }
  170. static void sched_switch_trace_reset(struct trace_array *tr)
  171. {
  172. if (sched_ref)
  173. stop_sched_trace(tr);
  174. }
  175. static void sched_switch_trace_start(struct trace_array *tr)
  176. {
  177. sched_stopped = 0;
  178. }
  179. static void sched_switch_trace_stop(struct trace_array *tr)
  180. {
  181. sched_stopped = 1;
  182. }
  183. static struct tracer sched_switch_trace __read_mostly =
  184. {
  185. .name = "sched_switch",
  186. .init = sched_switch_trace_init,
  187. .reset = sched_switch_trace_reset,
  188. .start = sched_switch_trace_start,
  189. .stop = sched_switch_trace_stop,
  190. .wait_pipe = poll_wait_pipe,
  191. #ifdef CONFIG_FTRACE_SELFTEST
  192. .selftest = trace_selftest_startup_sched_switch,
  193. #endif
  194. };
  195. __init static int init_sched_switch_trace(void)
  196. {
  197. return register_tracer(&sched_switch_trace);
  198. }
  199. device_initcall(init_sched_switch_trace);