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