blob: 4ccff66523c92a4b6040229d11ccf76f25da572b [file] [log] [blame]
Steven Rostedt9cfe06f2009-04-14 21:37:03 -04001#include <linux/module.h>
2#include <linux/kthread.h>
3
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
Steven Rostedt (Red Hat)4e20e3a2015-02-09 15:27:04 -050013static const char *random_strings[] = {
14 "Mother Goose",
15 "Snoopy",
16 "Gandalf",
17 "Frodo",
18 "One ring to rule them all"
19};
Steven Rostedt9cfe06f2009-04-14 21:37:03 -040020
21static void simple_thread_func(int cnt)
22{
Steven Rostedt (Red Hat)4e20e3a2015-02-09 15:27:04 -050023 int array[6];
24 int len = cnt % 5;
25 int i;
26
Steven Rostedt9cfe06f2009-04-14 21:37:03 -040027 set_current_state(TASK_INTERRUPTIBLE);
28 schedule_timeout(HZ);
Steven Rostedt (Red Hat)4e20e3a2015-02-09 15:27:04 -050029
30 for (i = 0; i < len; i++)
31 array[i] = i + 1;
32 array[i] = 0;
33
Steven Rostedt (Red Hat)c4c7eb22015-02-09 16:05:55 -050034 /* Silly tracepoints */
Steven Rostedt (Red Hat)4e20e3a2015-02-09 15:27:04 -050035 trace_foo_bar("hello", cnt, array, random_strings[len],
36 tsk_cpus_allowed(current));
Steven Rostedt (Red Hat)c4c7eb22015-02-09 16:05:55 -050037
Steven Rostedt (Red Hat)74969462015-02-09 17:14:04 -050038 trace_foo_with_template_simple("HELLO", cnt);
39
Steven Rostedt (Red Hat)c4c7eb22015-02-09 16:05:55 -050040 trace_foo_bar_with_cond("Some times print", cnt);
Steven Rostedt (Red Hat)74969462015-02-09 17:14:04 -050041
42 trace_foo_with_template_cond("prints other times", cnt);
43
44 trace_foo_with_template_print("I have to be different", cnt);
Steven Rostedt9cfe06f2009-04-14 21:37:03 -040045}
46
47static int simple_thread(void *arg)
48{
49 int cnt = 0;
50
51 while (!kthread_should_stop())
52 simple_thread_func(cnt++);
53
54 return 0;
55}
56
57static struct task_struct *simple_tsk;
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -050058static struct task_struct *simple_tsk_fn;
59
60static void simple_thread_func_fn(int cnt)
61{
62 set_current_state(TASK_INTERRUPTIBLE);
63 schedule_timeout(HZ);
64
65 /* More silly tracepoints */
66 trace_foo_bar_with_fn("Look at me", cnt);
Steven Rostedt (Red Hat)74969462015-02-09 17:14:04 -050067 trace_foo_with_template_fn("Look at me too", cnt);
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -050068}
69
70static int simple_thread_fn(void *arg)
71{
72 int cnt = 0;
73
74 while (!kthread_should_stop())
75 simple_thread_func_fn(cnt++);
76
77 return 0;
78}
79
80static DEFINE_MUTEX(thread_mutex);
Linus Torvaldsf1e7f9d2017-10-27 20:35:31 -070081static int simple_thread_cnt;
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -050082
83void foo_bar_reg(void)
84{
Steven Rostedt (VMware)86b08532017-10-17 14:55:24 -040085 mutex_lock(&thread_mutex);
86 if (simple_thread_cnt++)
87 goto out;
88
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -050089 pr_info("Starting thread for foo_bar_fn\n");
90 /*
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.
94 */
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -050095 simple_tsk_fn = kthread_run(simple_thread_fn, NULL, "event-sample-fn");
Steven Rostedt (VMware)86b08532017-10-17 14:55:24 -040096 out:
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -050097 mutex_unlock(&thread_mutex);
98}
99
100void foo_bar_unreg(void)
101{
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -0500102 mutex_lock(&thread_mutex);
Steven Rostedt (VMware)86b08532017-10-17 14:55:24 -0400103 if (--simple_thread_cnt)
104 goto out;
105
106 pr_info("Killing thread for foo_bar_fn\n");
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -0500107 if (simple_tsk_fn)
108 kthread_stop(simple_tsk_fn);
109 simple_tsk_fn = NULL;
Steven Rostedt (VMware)86b08532017-10-17 14:55:24 -0400110 out:
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -0500111 mutex_unlock(&thread_mutex);
112}
Steven Rostedt9cfe06f2009-04-14 21:37:03 -0400113
114static int __init trace_event_init(void)
115{
116 simple_tsk = kthread_run(simple_thread, NULL, "event-sample");
117 if (IS_ERR(simple_tsk))
118 return -1;
119
120 return 0;
121}
122
123static void __exit trace_event_exit(void)
124{
125 kthread_stop(simple_tsk);
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -0500126 mutex_lock(&thread_mutex);
127 if (simple_tsk_fn)
128 kthread_stop(simple_tsk_fn);
129 simple_tsk_fn = NULL;
130 mutex_unlock(&thread_mutex);
Steven Rostedt9cfe06f2009-04-14 21:37:03 -0400131}
132
133module_init(trace_event_init);
134module_exit(trace_event_exit);
135
136MODULE_AUTHOR("Steven Rostedt");
137MODULE_DESCRIPTION("trace-events-sample");
138MODULE_LICENSE("GPL");