blob: 39d4484aef53b39743c68709c5c430a1d4daed58 [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
38 trace_foo_bar_with_cond("Some times print", cnt);
Steven Rostedt9cfe06f2009-04-14 21:37:03 -040039}
40
41static int simple_thread(void *arg)
42{
43 int cnt = 0;
44
45 while (!kthread_should_stop())
46 simple_thread_func(cnt++);
47
48 return 0;
49}
50
51static struct task_struct *simple_tsk;
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -050052static struct task_struct *simple_tsk_fn;
53
54static void simple_thread_func_fn(int cnt)
55{
56 set_current_state(TASK_INTERRUPTIBLE);
57 schedule_timeout(HZ);
58
59 /* More silly tracepoints */
60 trace_foo_bar_with_fn("Look at me", cnt);
61}
62
63static int simple_thread_fn(void *arg)
64{
65 int cnt = 0;
66
67 while (!kthread_should_stop())
68 simple_thread_func_fn(cnt++);
69
70 return 0;
71}
72
73static DEFINE_MUTEX(thread_mutex);
74
75void foo_bar_reg(void)
76{
77 pr_info("Starting thread for foo_bar_fn\n");
78 /*
79 * We shouldn't be able to start a trace when the module is
80 * unloading (there's other locks to prevent that). But
81 * for consistency sake, we still take the thread_mutex.
82 */
83 mutex_lock(&thread_mutex);
84 simple_tsk_fn = kthread_run(simple_thread_fn, NULL, "event-sample-fn");
85 mutex_unlock(&thread_mutex);
86}
87
88void foo_bar_unreg(void)
89{
90 pr_info("Killing thread for foo_bar_fn\n");
91 /* protect against module unloading */
92 mutex_lock(&thread_mutex);
93 if (simple_tsk_fn)
94 kthread_stop(simple_tsk_fn);
95 simple_tsk_fn = NULL;
96 mutex_unlock(&thread_mutex);
97}
Steven Rostedt9cfe06f2009-04-14 21:37:03 -040098
99static int __init trace_event_init(void)
100{
101 simple_tsk = kthread_run(simple_thread, NULL, "event-sample");
102 if (IS_ERR(simple_tsk))
103 return -1;
104
105 return 0;
106}
107
108static void __exit trace_event_exit(void)
109{
110 kthread_stop(simple_tsk);
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -0500111 mutex_lock(&thread_mutex);
112 if (simple_tsk_fn)
113 kthread_stop(simple_tsk_fn);
114 simple_tsk_fn = NULL;
115 mutex_unlock(&thread_mutex);
Steven Rostedt9cfe06f2009-04-14 21:37:03 -0400116}
117
118module_init(trace_event_init);
119module_exit(trace_event_exit);
120
121MODULE_AUTHOR("Steven Rostedt");
122MODULE_DESCRIPTION("trace-events-sample");
123MODULE_LICENSE("GPL");