trace-events-sample.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. static int simple_thread_cnt;
  62. void foo_bar_reg(void)
  63. {
  64. mutex_lock(&thread_mutex);
  65. if (simple_thread_cnt++)
  66. goto out;
  67. pr_info("Starting thread for foo_bar_fn\n");
  68. /*
  69. * We shouldn't be able to start a trace when the module is
  70. * unloading (there's other locks to prevent that). But
  71. * for consistency sake, we still take the thread_mutex.
  72. */
  73. simple_tsk_fn = kthread_run(simple_thread_fn, NULL, "event-sample-fn");
  74. out:
  75. mutex_unlock(&thread_mutex);
  76. }
  77. void foo_bar_unreg(void)
  78. {
  79. mutex_lock(&thread_mutex);
  80. if (--simple_thread_cnt)
  81. goto out;
  82. pr_info("Killing thread for foo_bar_fn\n");
  83. if (simple_tsk_fn)
  84. kthread_stop(simple_tsk_fn);
  85. simple_tsk_fn = NULL;
  86. out:
  87. mutex_unlock(&thread_mutex);
  88. }
  89. static int __init trace_event_init(void)
  90. {
  91. simple_tsk = kthread_run(simple_thread, NULL, "event-sample");
  92. if (IS_ERR(simple_tsk))
  93. return -1;
  94. return 0;
  95. }
  96. static void __exit trace_event_exit(void)
  97. {
  98. kthread_stop(simple_tsk);
  99. mutex_lock(&thread_mutex);
  100. if (simple_tsk_fn)
  101. kthread_stop(simple_tsk_fn);
  102. simple_tsk_fn = NULL;
  103. mutex_unlock(&thread_mutex);
  104. }
  105. module_init(trace_event_init);
  106. module_exit(trace_event_exit);
  107. MODULE_AUTHOR("Steven Rostedt");
  108. MODULE_DESCRIPTION("trace-events-sample");
  109. MODULE_LICENSE("GPL");