trace_sched_switch.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/kallsyms.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/ftrace.h>
  11. #include <trace/events/sched.h>
  12. #include "trace.h"
  13. static int sched_ref;
  14. static DEFINE_MUTEX(sched_register_mutex);
  15. static void
  16. probe_sched_switch(void *ignore, bool preempt,
  17. struct task_struct *prev, struct task_struct *next)
  18. {
  19. if (unlikely(!sched_ref))
  20. return;
  21. tracing_record_cmdline(prev);
  22. tracing_record_cmdline(next);
  23. }
  24. static void
  25. probe_sched_wakeup(void *ignore, struct task_struct *wakee)
  26. {
  27. if (unlikely(!sched_ref))
  28. return;
  29. tracing_record_cmdline(current);
  30. }
  31. static int tracing_sched_register(void)
  32. {
  33. int ret;
  34. ret = register_trace_sched_wakeup(probe_sched_wakeup, NULL);
  35. if (ret) {
  36. pr_info("wakeup trace: Couldn't activate tracepoint"
  37. " probe to kernel_sched_wakeup\n");
  38. return ret;
  39. }
  40. ret = register_trace_sched_wakeup_new(probe_sched_wakeup, NULL);
  41. if (ret) {
  42. pr_info("wakeup trace: Couldn't activate tracepoint"
  43. " probe to kernel_sched_wakeup_new\n");
  44. goto fail_deprobe;
  45. }
  46. ret = register_trace_sched_switch(probe_sched_switch, NULL);
  47. if (ret) {
  48. pr_info("sched trace: Couldn't activate tracepoint"
  49. " probe to kernel_sched_switch\n");
  50. goto fail_deprobe_wake_new;
  51. }
  52. return ret;
  53. fail_deprobe_wake_new:
  54. unregister_trace_sched_wakeup_new(probe_sched_wakeup, NULL);
  55. fail_deprobe:
  56. unregister_trace_sched_wakeup(probe_sched_wakeup, NULL);
  57. return ret;
  58. }
  59. static void tracing_sched_unregister(void)
  60. {
  61. unregister_trace_sched_switch(probe_sched_switch, NULL);
  62. unregister_trace_sched_wakeup_new(probe_sched_wakeup, NULL);
  63. unregister_trace_sched_wakeup(probe_sched_wakeup, NULL);
  64. }
  65. static void tracing_start_sched_switch(void)
  66. {
  67. mutex_lock(&sched_register_mutex);
  68. if (!(sched_ref++))
  69. tracing_sched_register();
  70. mutex_unlock(&sched_register_mutex);
  71. }
  72. static void tracing_stop_sched_switch(void)
  73. {
  74. mutex_lock(&sched_register_mutex);
  75. if (!(--sched_ref))
  76. tracing_sched_unregister();
  77. mutex_unlock(&sched_register_mutex);
  78. }
  79. void tracing_start_cmdline_record(void)
  80. {
  81. tracing_start_sched_switch();
  82. }
  83. void tracing_stop_cmdline_record(void)
  84. {
  85. tracing_stop_sched_switch();
  86. }