trace_sched_switch.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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_event *event;
  28. struct ctx_switch_entry *entry;
  29. event = trace_buffer_lock_reserve(tr, TRACE_CTX,
  30. sizeof(*entry), flags, pc);
  31. if (!event)
  32. return;
  33. entry = ring_buffer_event_data(event);
  34. entry->prev_pid = prev->pid;
  35. entry->prev_prio = prev->prio;
  36. entry->prev_state = prev->state;
  37. entry->next_pid = next->pid;
  38. entry->next_prio = next->prio;
  39. entry->next_state = next->state;
  40. entry->next_cpu = task_cpu(next);
  41. if (!filter_check_discard(call, entry, tr->buffer, event))
  42. trace_buffer_unlock_commit(tr, event, flags, pc);
  43. }
  44. static void
  45. probe_sched_switch(struct rq *__rq, struct task_struct *prev,
  46. 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. event = trace_buffer_lock_reserve(tr, TRACE_WAKE,
  76. sizeof(*entry), flags, pc);
  77. if (!event)
  78. return;
  79. entry = ring_buffer_event_data(event);
  80. entry->prev_pid = curr->pid;
  81. entry->prev_prio = curr->prio;
  82. entry->prev_state = curr->state;
  83. entry->next_pid = wakee->pid;
  84. entry->next_prio = wakee->prio;
  85. entry->next_state = wakee->state;
  86. entry->next_cpu = task_cpu(wakee);
  87. if (!filter_check_discard(call, entry, tr->buffer, event))
  88. ring_buffer_unlock_commit(tr->buffer, event);
  89. ftrace_trace_stack(tr, flags, 6, pc);
  90. ftrace_trace_userstack(tr, flags, pc);
  91. }
  92. static void
  93. probe_sched_wakeup(struct rq *__rq, struct task_struct *wakee, int success)
  94. {
  95. struct trace_array_cpu *data;
  96. unsigned long flags;
  97. int cpu, pc;
  98. if (unlikely(!sched_ref))
  99. return;
  100. tracing_record_cmdline(current);
  101. if (!tracer_enabled || sched_stopped)
  102. return;
  103. pc = preempt_count();
  104. local_irq_save(flags);
  105. cpu = raw_smp_processor_id();
  106. data = ctx_trace->data[cpu];
  107. if (likely(!atomic_read(&data->disabled)))
  108. tracing_sched_wakeup_trace(ctx_trace, wakee, current,
  109. flags, pc);
  110. local_irq_restore(flags);
  111. }
  112. static int tracing_sched_register(void)
  113. {
  114. int ret;
  115. ret = register_trace_sched_wakeup(probe_sched_wakeup);
  116. if (ret) {
  117. pr_info("wakeup trace: Couldn't activate tracepoint"
  118. " probe to kernel_sched_wakeup\n");
  119. return ret;
  120. }
  121. ret = register_trace_sched_wakeup_new(probe_sched_wakeup);
  122. if (ret) {
  123. pr_info("wakeup trace: Couldn't activate tracepoint"
  124. " probe to kernel_sched_wakeup_new\n");
  125. goto fail_deprobe;
  126. }
  127. ret = register_trace_sched_switch(probe_sched_switch);
  128. if (ret) {
  129. pr_info("sched trace: Couldn't activate tracepoint"
  130. " probe to kernel_sched_switch\n");
  131. goto fail_deprobe_wake_new;
  132. }
  133. return ret;
  134. fail_deprobe_wake_new:
  135. unregister_trace_sched_wakeup_new(probe_sched_wakeup);
  136. fail_deprobe:
  137. unregister_trace_sched_wakeup(probe_sched_wakeup);
  138. return ret;
  139. }
  140. static void tracing_sched_unregister(void)
  141. {
  142. unregister_trace_sched_switch(probe_sched_switch);
  143. unregister_trace_sched_wakeup_new(probe_sched_wakeup);
  144. unregister_trace_sched_wakeup(probe_sched_wakeup);
  145. }
  146. static void tracing_start_sched_switch(void)
  147. {
  148. mutex_lock(&sched_register_mutex);
  149. if (!(sched_ref++))
  150. tracing_sched_register();
  151. mutex_unlock(&sched_register_mutex);
  152. }
  153. static void tracing_stop_sched_switch(void)
  154. {
  155. mutex_lock(&sched_register_mutex);
  156. if (!(--sched_ref))
  157. tracing_sched_unregister();
  158. mutex_unlock(&sched_register_mutex);
  159. }
  160. void tracing_start_cmdline_record(void)
  161. {
  162. tracing_start_sched_switch();
  163. }
  164. void tracing_stop_cmdline_record(void)
  165. {
  166. tracing_stop_sched_switch();
  167. }
  168. /**
  169. * tracing_start_sched_switch_record - start tracing context switches
  170. *
  171. * Turns on context switch tracing for a tracer.
  172. */
  173. void tracing_start_sched_switch_record(void)
  174. {
  175. if (unlikely(!ctx_trace)) {
  176. WARN_ON(1);
  177. return;
  178. }
  179. tracing_start_sched_switch();
  180. mutex_lock(&sched_register_mutex);
  181. tracer_enabled++;
  182. mutex_unlock(&sched_register_mutex);
  183. }
  184. /**
  185. * tracing_stop_sched_switch_record - start tracing context switches
  186. *
  187. * Turns off context switch tracing for a tracer.
  188. */
  189. void tracing_stop_sched_switch_record(void)
  190. {
  191. mutex_lock(&sched_register_mutex);
  192. tracer_enabled--;
  193. WARN_ON(tracer_enabled < 0);
  194. mutex_unlock(&sched_register_mutex);
  195. tracing_stop_sched_switch();
  196. }
  197. /**
  198. * tracing_sched_switch_assign_trace - assign a trace array for ctx switch
  199. * @tr: trace array pointer to assign
  200. *
  201. * Some tracers might want to record the context switches in their
  202. * trace. This function lets those tracers assign the trace array
  203. * to use.
  204. */
  205. void tracing_sched_switch_assign_trace(struct trace_array *tr)
  206. {
  207. ctx_trace = tr;
  208. }
  209. static void stop_sched_trace(struct trace_array *tr)
  210. {
  211. tracing_stop_sched_switch_record();
  212. }
  213. static int sched_switch_trace_init(struct trace_array *tr)
  214. {
  215. ctx_trace = tr;
  216. tracing_reset_online_cpus(tr);
  217. tracing_start_sched_switch_record();
  218. return 0;
  219. }
  220. static void sched_switch_trace_reset(struct trace_array *tr)
  221. {
  222. if (sched_ref)
  223. stop_sched_trace(tr);
  224. }
  225. static void sched_switch_trace_start(struct trace_array *tr)
  226. {
  227. sched_stopped = 0;
  228. }
  229. static void sched_switch_trace_stop(struct trace_array *tr)
  230. {
  231. sched_stopped = 1;
  232. }
  233. static struct tracer sched_switch_trace __read_mostly =
  234. {
  235. .name = "sched_switch",
  236. .init = sched_switch_trace_init,
  237. .reset = sched_switch_trace_reset,
  238. .start = sched_switch_trace_start,
  239. .stop = sched_switch_trace_stop,
  240. .wait_pipe = poll_wait_pipe,
  241. #ifdef CONFIG_FTRACE_SELFTEST
  242. .selftest = trace_selftest_startup_sched_switch,
  243. #endif
  244. };
  245. __init static int init_sched_switch_trace(void)
  246. {
  247. return register_tracer(&sched_switch_trace);
  248. }
  249. device_initcall(init_sched_switch_trace);