trace_sched_switch.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. ring_buffer_unlock_commit(buffer, event);
  90. ftrace_trace_stack(tr->buffer, flags, 6, pc);
  91. ftrace_trace_userstack(tr->buffer, flags, pc);
  92. }
  93. static void
  94. probe_sched_wakeup(void *ignore, struct task_struct *wakee, int success)
  95. {
  96. struct trace_array_cpu *data;
  97. unsigned long flags;
  98. int cpu, pc;
  99. if (unlikely(!sched_ref))
  100. return;
  101. tracing_record_cmdline(current);
  102. if (!tracer_enabled || sched_stopped)
  103. return;
  104. pc = preempt_count();
  105. local_irq_save(flags);
  106. cpu = raw_smp_processor_id();
  107. data = ctx_trace->data[cpu];
  108. if (likely(!atomic_read(&data->disabled)))
  109. tracing_sched_wakeup_trace(ctx_trace, wakee, current,
  110. flags, pc);
  111. local_irq_restore(flags);
  112. }
  113. static int tracing_sched_register(void)
  114. {
  115. int ret;
  116. ret = register_trace_sched_wakeup(probe_sched_wakeup, NULL);
  117. if (ret) {
  118. pr_info("wakeup trace: Couldn't activate tracepoint"
  119. " probe to kernel_sched_wakeup\n");
  120. return ret;
  121. }
  122. ret = register_trace_sched_wakeup_new(probe_sched_wakeup, NULL);
  123. if (ret) {
  124. pr_info("wakeup trace: Couldn't activate tracepoint"
  125. " probe to kernel_sched_wakeup_new\n");
  126. goto fail_deprobe;
  127. }
  128. ret = register_trace_sched_switch(probe_sched_switch, NULL);
  129. if (ret) {
  130. pr_info("sched trace: Couldn't activate tracepoint"
  131. " probe to kernel_sched_switch\n");
  132. goto fail_deprobe_wake_new;
  133. }
  134. return ret;
  135. fail_deprobe_wake_new:
  136. unregister_trace_sched_wakeup_new(probe_sched_wakeup, NULL);
  137. fail_deprobe:
  138. unregister_trace_sched_wakeup(probe_sched_wakeup, NULL);
  139. return ret;
  140. }
  141. static void tracing_sched_unregister(void)
  142. {
  143. unregister_trace_sched_switch(probe_sched_switch, NULL);
  144. unregister_trace_sched_wakeup_new(probe_sched_wakeup, NULL);
  145. unregister_trace_sched_wakeup(probe_sched_wakeup, NULL);
  146. }
  147. static void tracing_start_sched_switch(void)
  148. {
  149. mutex_lock(&sched_register_mutex);
  150. if (!(sched_ref++))
  151. tracing_sched_register();
  152. mutex_unlock(&sched_register_mutex);
  153. }
  154. static void tracing_stop_sched_switch(void)
  155. {
  156. mutex_lock(&sched_register_mutex);
  157. if (!(--sched_ref))
  158. tracing_sched_unregister();
  159. mutex_unlock(&sched_register_mutex);
  160. }
  161. void tracing_start_cmdline_record(void)
  162. {
  163. tracing_start_sched_switch();
  164. }
  165. void tracing_stop_cmdline_record(void)
  166. {
  167. tracing_stop_sched_switch();
  168. }
  169. /**
  170. * tracing_start_sched_switch_record - start tracing context switches
  171. *
  172. * Turns on context switch tracing for a tracer.
  173. */
  174. void tracing_start_sched_switch_record(void)
  175. {
  176. if (unlikely(!ctx_trace)) {
  177. WARN_ON(1);
  178. return;
  179. }
  180. tracing_start_sched_switch();
  181. mutex_lock(&sched_register_mutex);
  182. tracer_enabled++;
  183. mutex_unlock(&sched_register_mutex);
  184. }
  185. /**
  186. * tracing_stop_sched_switch_record - start tracing context switches
  187. *
  188. * Turns off context switch tracing for a tracer.
  189. */
  190. void tracing_stop_sched_switch_record(void)
  191. {
  192. mutex_lock(&sched_register_mutex);
  193. tracer_enabled--;
  194. WARN_ON(tracer_enabled < 0);
  195. mutex_unlock(&sched_register_mutex);
  196. tracing_stop_sched_switch();
  197. }
  198. /**
  199. * tracing_sched_switch_assign_trace - assign a trace array for ctx switch
  200. * @tr: trace array pointer to assign
  201. *
  202. * Some tracers might want to record the context switches in their
  203. * trace. This function lets those tracers assign the trace array
  204. * to use.
  205. */
  206. void tracing_sched_switch_assign_trace(struct trace_array *tr)
  207. {
  208. ctx_trace = tr;
  209. }