1 #include <linux/module.h>
2 #include <linux/kthread.h>
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.
10 #define CREATE_TRACE_POINTS
11 #include "trace-events-sample.h"
13 static const char *random_strings[] = {
18 "One ring to rule them all"
21 static void simple_thread_func(int cnt)
27 set_current_state(TASK_INTERRUPTIBLE);
30 for (i = 0; i < len; i++)
34 /* Silly tracepoints */
35 trace_foo_bar("hello", cnt, array, random_strings[len],
36 ¤t->cpus_allowed);
38 trace_foo_with_template_simple("HELLO", cnt);
40 trace_foo_bar_with_cond("Some times print", cnt);
42 trace_foo_with_template_cond("prints other times", cnt);
44 trace_foo_with_template_print("I have to be different", cnt);
47 static int simple_thread(void *arg)
51 while (!kthread_should_stop())
52 simple_thread_func(cnt++);
57 static struct task_struct *simple_tsk;
58 static struct task_struct *simple_tsk_fn;
60 static void simple_thread_func_fn(int cnt)
62 set_current_state(TASK_INTERRUPTIBLE);
65 /* More silly tracepoints */
66 trace_foo_bar_with_fn("Look at me", cnt);
67 trace_foo_with_template_fn("Look at me too", cnt);
70 static int simple_thread_fn(void *arg)
74 while (!kthread_should_stop())
75 simple_thread_func_fn(cnt++);
80 static DEFINE_MUTEX(thread_mutex);
81 static int simple_thread_cnt;
85 mutex_lock(&thread_mutex);
86 if (simple_thread_cnt++)
89 pr_info("Starting thread for foo_bar_fn\n");
91 * We shouldn't be able to start a trace when the module is
92 * unloading (there's other locks to prevent that). But
93 * for consistency sake, we still take the thread_mutex.
95 simple_tsk_fn = kthread_run(simple_thread_fn, NULL, "event-sample-fn");
97 mutex_unlock(&thread_mutex);
101 void foo_bar_unreg(void)
103 mutex_lock(&thread_mutex);
104 if (--simple_thread_cnt)
107 pr_info("Killing thread for foo_bar_fn\n");
109 kthread_stop(simple_tsk_fn);
110 simple_tsk_fn = NULL;
112 mutex_unlock(&thread_mutex);
115 static int __init trace_event_init(void)
117 simple_tsk = kthread_run(simple_thread, NULL, "event-sample");
118 if (IS_ERR(simple_tsk))
124 static void __exit trace_event_exit(void)
126 kthread_stop(simple_tsk);
127 mutex_lock(&thread_mutex);
129 kthread_stop(simple_tsk_fn);
130 simple_tsk_fn = NULL;
131 mutex_unlock(&thread_mutex);
134 module_init(trace_event_init);
135 module_exit(trace_event_exit);
137 MODULE_AUTHOR("Steven Rostedt");
138 MODULE_DESCRIPTION("trace-events-sample");
139 MODULE_LICENSE("GPL");