trace_sched_switch.c 6.8 KB

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