trace_sched_switch.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. void
  21. tracing_sched_switch_trace(struct trace_array *tr,
  22. struct task_struct *prev,
  23. struct task_struct *next,
  24. unsigned long flags, int pc)
  25. {
  26. struct ftrace_event_call *call = &event_context_switch;
  27. struct ring_buffer *buffer = tr->buffer;
  28. struct ring_buffer_event *event;
  29. struct ctx_switch_entry *entry;
  30. event = trace_buffer_lock_reserve(buffer, TRACE_CTX,
  31. sizeof(*entry), flags, pc);
  32. if (!event)
  33. return;
  34. entry = ring_buffer_event_data(event);
  35. entry->prev_pid = prev->pid;
  36. entry->prev_prio = prev->prio;
  37. entry->prev_state = prev->state;
  38. entry->next_pid = next->pid;
  39. entry->next_prio = next->prio;
  40. entry->next_state = next->state;
  41. entry->next_cpu = task_cpu(next);
  42. if (!filter_check_discard(call, entry, buffer, event))
  43. trace_buffer_unlock_commit(buffer, event, flags, pc);
  44. }
  45. static void
  46. probe_sched_switch(void *ignore, struct task_struct *prev, struct task_struct *next)
  47. {
  48. struct trace_array_cpu *data;
  49. unsigned long flags;
  50. int cpu;
  51. int pc;
  52. if (unlikely(!sched_ref))
  53. return;
  54. tracing_record_cmdline(prev);
  55. tracing_record_cmdline(next);
  56. if (!tracer_enabled || sched_stopped)
  57. return;
  58. pc = preempt_count();
  59. local_irq_save(flags);
  60. cpu = raw_smp_processor_id();
  61. data = ctx_trace->data[cpu];
  62. if (likely(!atomic_read(&data->disabled)))
  63. tracing_sched_switch_trace(ctx_trace, prev, next, flags, pc);
  64. local_irq_restore(flags);
  65. }
  66. void
  67. tracing_sched_wakeup_trace(struct trace_array *tr,
  68. struct task_struct *wakee,
  69. struct task_struct *curr,
  70. unsigned long flags, int pc)
  71. {
  72. struct ftrace_event_call *call = &event_wakeup;
  73. struct ring_buffer_event *event;
  74. struct ctx_switch_entry *entry;
  75. struct ring_buffer *buffer = tr->buffer;
  76. event = trace_buffer_lock_reserve(buffer, TRACE_WAKE,
  77. sizeof(*entry), flags, pc);
  78. if (!event)
  79. return;
  80. entry = ring_buffer_event_data(event);
  81. entry->prev_pid = curr->pid;
  82. entry->prev_prio = curr->prio;
  83. entry->prev_state = curr->state;
  84. entry->next_pid = wakee->pid;
  85. entry->next_prio = wakee->prio;
  86. entry->next_state = wakee->state;
  87. entry->next_cpu = task_cpu(wakee);
  88. if (!filter_check_discard(call, entry, buffer, event))
  89. trace_buffer_unlock_commit(buffer, event, flags, pc);
  90. }
  91. static void
  92. probe_sched_wakeup(void *ignore, struct task_struct *wakee, int success)
  93. {
  94. struct trace_array_cpu *data;
  95. unsigned long flags;
  96. int cpu, pc;
  97. if (unlikely(!sched_ref))
  98. return;
  99. tracing_record_cmdline(current);
  100. if (!tracer_enabled || sched_stopped)
  101. return;
  102. pc = preempt_count();
  103. local_irq_save(flags);
  104. cpu = raw_smp_processor_id();
  105. data = ctx_trace->data[cpu];
  106. if (likely(!atomic_read(&data->disabled)))
  107. tracing_sched_wakeup_trace(ctx_trace, wakee, current,
  108. flags, pc);
  109. local_irq_restore(flags);
  110. }
  111. static int tracing_sched_register(void)
  112. {
  113. int ret;
  114. ret = register_trace_sched_wakeup(probe_sched_wakeup, NULL);
  115. if (ret) {
  116. pr_info("wakeup trace: Couldn't activate tracepoint"
  117. " probe to kernel_sched_wakeup\n");
  118. return ret;
  119. }
  120. ret = register_trace_sched_wakeup_new(probe_sched_wakeup, NULL);
  121. if (ret) {
  122. pr_info("wakeup trace: Couldn't activate tracepoint"
  123. " probe to kernel_sched_wakeup_new\n");
  124. goto fail_deprobe;
  125. }
  126. ret = register_trace_sched_switch(probe_sched_switch, NULL);
  127. if (ret) {
  128. pr_info("sched trace: Couldn't activate tracepoint"
  129. " probe to kernel_sched_switch\n");
  130. goto fail_deprobe_wake_new;
  131. }
  132. return ret;
  133. fail_deprobe_wake_new:
  134. unregister_trace_sched_wakeup_new(probe_sched_wakeup, NULL);
  135. fail_deprobe:
  136. unregister_trace_sched_wakeup(probe_sched_wakeup, NULL);
  137. return ret;
  138. }
  139. static void tracing_sched_unregister(void)
  140. {
  141. unregister_trace_sched_switch(probe_sched_switch, NULL);
  142. unregister_trace_sched_wakeup_new(probe_sched_wakeup, NULL);
  143. unregister_trace_sched_wakeup(probe_sched_wakeup, NULL);
  144. }
  145. static void tracing_start_sched_switch(void)
  146. {
  147. mutex_lock(&sched_register_mutex);
  148. if (!(sched_ref++))
  149. tracing_sched_register();
  150. mutex_unlock(&sched_register_mutex);
  151. }
  152. static void tracing_stop_sched_switch(void)
  153. {
  154. mutex_lock(&sched_register_mutex);
  155. if (!(--sched_ref))
  156. tracing_sched_unregister();
  157. mutex_unlock(&sched_register_mutex);
  158. }
  159. void tracing_start_cmdline_record(void)
  160. {
  161. tracing_start_sched_switch();
  162. }
  163. void tracing_stop_cmdline_record(void)
  164. {
  165. tracing_stop_sched_switch();
  166. }
  167. /**
  168. * tracing_start_sched_switch_record - start tracing context switches
  169. *
  170. * Turns on context switch tracing for a tracer.
  171. */
  172. void tracing_start_sched_switch_record(void)
  173. {
  174. if (unlikely(!ctx_trace)) {
  175. WARN_ON(1);
  176. return;
  177. }
  178. tracing_start_sched_switch();
  179. mutex_lock(&sched_register_mutex);
  180. tracer_enabled++;
  181. mutex_unlock(&sched_register_mutex);
  182. }
  183. /**
  184. * tracing_stop_sched_switch_record - start tracing context switches
  185. *
  186. * Turns off context switch tracing for a tracer.
  187. */
  188. void tracing_stop_sched_switch_record(void)
  189. {
  190. mutex_lock(&sched_register_mutex);
  191. tracer_enabled--;
  192. WARN_ON(tracer_enabled < 0);
  193. mutex_unlock(&sched_register_mutex);
  194. tracing_stop_sched_switch();
  195. }
  196. /**
  197. * tracing_sched_switch_assign_trace - assign a trace array for ctx switch
  198. * @tr: trace array pointer to assign
  199. *
  200. * Some tracers might want to record the context switches in their
  201. * trace. This function lets those tracers assign the trace array
  202. * to use.
  203. */
  204. void tracing_sched_switch_assign_trace(struct trace_array *tr)
  205. {
  206. ctx_trace = tr;
  207. }