trace-events-sample.c 2.9 KB

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