trace_sched_switch.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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/sched.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. probe_sched_switch(struct rq *__rq, struct task_struct *prev,
  20. struct task_struct *next)
  21. {
  22. struct trace_array_cpu *data;
  23. unsigned long flags;
  24. long disabled;
  25. int cpu;
  26. int pc;
  27. if (!atomic_read(&sched_ref))
  28. return;
  29. tracing_record_cmdline(prev);
  30. tracing_record_cmdline(next);
  31. if (!tracer_enabled)
  32. return;
  33. pc = preempt_count();
  34. local_irq_save(flags);
  35. cpu = raw_smp_processor_id();
  36. data = ctx_trace->data[cpu];
  37. disabled = atomic_inc_return(&data->disabled);
  38. if (likely(disabled == 1))
  39. tracing_sched_switch_trace(ctx_trace, data, prev, next, flags, pc);
  40. atomic_dec(&data->disabled);
  41. local_irq_restore(flags);
  42. }
  43. static void
  44. probe_sched_wakeup(struct rq *__rq, struct task_struct *wakee)
  45. {
  46. struct trace_array_cpu *data;
  47. unsigned long flags;
  48. long disabled;
  49. int cpu, pc;
  50. if (!likely(tracer_enabled))
  51. return;
  52. pc = preempt_count();
  53. tracing_record_cmdline(current);
  54. local_irq_save(flags);
  55. cpu = raw_smp_processor_id();
  56. data = ctx_trace->data[cpu];
  57. disabled = atomic_inc_return(&data->disabled);
  58. if (likely(disabled == 1))
  59. tracing_sched_wakeup_trace(ctx_trace, data, wakee, current,
  60. flags, pc);
  61. atomic_dec(&data->disabled);
  62. local_irq_restore(flags);
  63. }
  64. static void sched_switch_reset(struct trace_array *tr)
  65. {
  66. int cpu;
  67. tr->time_start = ftrace_now(tr->cpu);
  68. for_each_online_cpu(cpu)
  69. tracing_reset(tr, cpu);
  70. }
  71. static int tracing_sched_register(void)
  72. {
  73. int ret;
  74. ret = register_trace_sched_wakeup(probe_sched_wakeup);
  75. if (ret) {
  76. pr_info("wakeup trace: Couldn't activate tracepoint"
  77. " probe to kernel_sched_wakeup\n");
  78. return ret;
  79. }
  80. ret = register_trace_sched_wakeup_new(probe_sched_wakeup);
  81. if (ret) {
  82. pr_info("wakeup trace: Couldn't activate tracepoint"
  83. " probe to kernel_sched_wakeup_new\n");
  84. goto fail_deprobe;
  85. }
  86. ret = register_trace_sched_switch(probe_sched_switch);
  87. if (ret) {
  88. pr_info("sched trace: Couldn't activate tracepoint"
  89. " probe to kernel_sched_schedule\n");
  90. goto fail_deprobe_wake_new;
  91. }
  92. return ret;
  93. fail_deprobe_wake_new:
  94. unregister_trace_sched_wakeup_new(probe_sched_wakeup);
  95. fail_deprobe:
  96. unregister_trace_sched_wakeup(probe_sched_wakeup);
  97. return ret;
  98. }
  99. static void tracing_sched_unregister(void)
  100. {
  101. unregister_trace_sched_switch(probe_sched_switch);
  102. unregister_trace_sched_wakeup_new(probe_sched_wakeup);
  103. unregister_trace_sched_wakeup(probe_sched_wakeup);
  104. }
  105. static void tracing_start_sched_switch(void)
  106. {
  107. long ref;
  108. ref = atomic_inc_return(&sched_ref);
  109. if (ref == 1)
  110. tracing_sched_register();
  111. }
  112. static void tracing_stop_sched_switch(void)
  113. {
  114. long ref;
  115. ref = atomic_dec_and_test(&sched_ref);
  116. if (ref)
  117. tracing_sched_unregister();
  118. }
  119. void tracing_start_cmdline_record(void)
  120. {
  121. tracing_start_sched_switch();
  122. }
  123. void tracing_stop_cmdline_record(void)
  124. {
  125. tracing_stop_sched_switch();
  126. }
  127. static void start_sched_trace(struct trace_array *tr)
  128. {
  129. sched_switch_reset(tr);
  130. tracing_start_cmdline_record();
  131. tracer_enabled = 1;
  132. }
  133. static void stop_sched_trace(struct trace_array *tr)
  134. {
  135. tracer_enabled = 0;
  136. tracing_stop_cmdline_record();
  137. }
  138. static void sched_switch_trace_init(struct trace_array *tr)
  139. {
  140. ctx_trace = tr;
  141. if (tr->ctrl)
  142. start_sched_trace(tr);
  143. }
  144. static void sched_switch_trace_reset(struct trace_array *tr)
  145. {
  146. if (tr->ctrl)
  147. stop_sched_trace(tr);
  148. }
  149. static void sched_switch_trace_ctrl_update(struct trace_array *tr)
  150. {
  151. /* When starting a new trace, reset the buffers */
  152. if (tr->ctrl)
  153. start_sched_trace(tr);
  154. else
  155. stop_sched_trace(tr);
  156. }
  157. static struct tracer sched_switch_trace __read_mostly =
  158. {
  159. .name = "sched_switch",
  160. .init = sched_switch_trace_init,
  161. .reset = sched_switch_trace_reset,
  162. .ctrl_update = sched_switch_trace_ctrl_update,
  163. #ifdef CONFIG_FTRACE_SELFTEST
  164. .selftest = trace_selftest_startup_sched_switch,
  165. #endif
  166. };
  167. __init static int init_sched_switch_trace(void)
  168. {
  169. int ret = 0;
  170. if (atomic_read(&sched_ref))
  171. ret = tracing_sched_register();
  172. if (ret) {
  173. pr_info("error registering scheduler trace\n");
  174. return ret;
  175. }
  176. return register_tracer(&sched_switch_trace);
  177. }
  178. device_initcall(init_sched_switch_trace);