trace_sched_switch.c 2.2 KB

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