trace-events-sample.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include <linux/module.h>
  2. #include <linux/kthread.h>
  3. /*
  4. * Any file that uses trace points, must include the header.
  5. * But only one file, must include the header by defining
  6. * CREATE_TRACE_POINTS first. This will make the C code that
  7. * creates the handles for the trace points.
  8. */
  9. #define CREATE_TRACE_POINTS
  10. #include "trace-events-sample.h"
  11. static const char *random_strings[] = {
  12. "Mother Goose",
  13. "Snoopy",
  14. "Gandalf",
  15. "Frodo",
  16. "One ring to rule them all"
  17. };
  18. static void simple_thread_func(int cnt)
  19. {
  20. int array[6];
  21. int len = cnt % 5;
  22. int i;
  23. set_current_state(TASK_INTERRUPTIBLE);
  24. schedule_timeout(HZ);
  25. for (i = 0; i < len; i++)
  26. array[i] = i + 1;
  27. array[i] = 0;
  28. /* Silly tracepoints */
  29. trace_foo_bar("hello", cnt, array, random_strings[len],
  30. tsk_cpus_allowed(current));
  31. trace_foo_with_template_simple("HELLO", cnt);
  32. trace_foo_bar_with_cond("Some times print", cnt);
  33. trace_foo_with_template_cond("prints other times", cnt);
  34. trace_foo_with_template_print("I have to be different", cnt);
  35. }
  36. static int simple_thread(void *arg)
  37. {
  38. int cnt = 0;
  39. while (!kthread_should_stop())
  40. simple_thread_func(cnt++);
  41. return 0;
  42. }
  43. static struct task_struct *simple_tsk;
  44. static struct task_struct *simple_tsk_fn;
  45. static void simple_thread_func_fn(int cnt)
  46. {
  47. set_current_state(TASK_INTERRUPTIBLE);
  48. schedule_timeout(HZ);
  49. /* More silly tracepoints */
  50. trace_foo_bar_with_fn("Look at me", cnt);
  51. trace_foo_with_template_fn("Look at me too", cnt);
  52. }
  53. static int simple_thread_fn(void *arg)
  54. {
  55. int cnt = 0;
  56. while (!kthread_should_stop())
  57. simple_thread_func_fn(cnt++);
  58. return 0;
  59. }
  60. static DEFINE_MUTEX(thread_mutex);
  61. int foo_bar_reg(void)
  62. {
  63. pr_info("Starting thread for foo_bar_fn\n");
  64. /*
  65. * We shouldn't be able to start a trace when the module is
  66. * unloading (there's other locks to prevent that). But
  67. * for consistency sake, we still take the thread_mutex.
  68. */
  69. mutex_lock(&thread_mutex);
  70. simple_tsk_fn = kthread_run(simple_thread_fn, NULL, "event-sample-fn");
  71. mutex_unlock(&thread_mutex);
  72. return 0;
  73. }
  74. void foo_bar_unreg(void)
  75. {
  76. pr_info("Killing thread for foo_bar_fn\n");
  77. /* protect against module unloading */
  78. mutex_lock(&thread_mutex);
  79. if (simple_tsk_fn)
  80. kthread_stop(simple_tsk_fn);
  81. simple_tsk_fn = NULL;
  82. mutex_unlock(&thread_mutex);
  83. }
  84. static int __init trace_event_init(void)
  85. {
  86. simple_tsk = kthread_run(simple_thread, NULL, "event-sample");
  87. if (IS_ERR(simple_tsk))
  88. return -1;
  89. return 0;
  90. }
  91. static void __exit trace_event_exit(void)
  92. {
  93. kthread_stop(simple_tsk);
  94. mutex_lock(&thread_mutex);
  95. if (simple_tsk_fn)
  96. kthread_stop(simple_tsk_fn);
  97. simple_tsk_fn = NULL;
  98. mutex_unlock(&thread_mutex);
  99. }
  100. module_init(trace_event_init);
  101. module_exit(trace_event_exit);
  102. MODULE_AUTHOR("Steven Rostedt");
  103. MODULE_DESCRIPTION("trace-events-sample");
  104. MODULE_LICENSE("GPL");