blob: 1a72b7d95cdc74701ee70832853e0f85e2d25e16 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Steven Rostedt9cfe06f2009-04-14 21:37:03 -04002#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
Steven Rostedt (Red Hat)4e20e3a2015-02-09 15:27:04 -050014static const char *random_strings[] = {
15 "Mother Goose",
16 "Snoopy",
17 "Gandalf",
18 "Frodo",
19 "One ring to rule them all"
20};
Steven Rostedt9cfe06f2009-04-14 21:37:03 -040021
22static void simple_thread_func(int cnt)
23{
Steven Rostedt (Red Hat)4e20e3a2015-02-09 15:27:04 -050024 int array[6];
25 int len = cnt % 5;
26 int i;
27
Steven Rostedt9cfe06f2009-04-14 21:37:03 -040028 set_current_state(TASK_INTERRUPTIBLE);
29 schedule_timeout(HZ);
Steven Rostedt (Red Hat)4e20e3a2015-02-09 15:27:04 -050030
31 for (i = 0; i < len; i++)
32 array[i] = i + 1;
33 array[i] = 0;
34
Steven Rostedt (Red Hat)c4c7eb22015-02-09 16:05:55 -050035 /* Silly tracepoints */
Steven Rostedt (Red Hat)4e20e3a2015-02-09 15:27:04 -050036 trace_foo_bar("hello", cnt, array, random_strings[len],
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +020037 current->cpus_ptr);
Steven Rostedt (Red Hat)c4c7eb22015-02-09 16:05:55 -050038
Steven Rostedt (Red Hat)74969462015-02-09 17:14:04 -050039 trace_foo_with_template_simple("HELLO", cnt);
40
Steven Rostedt (Red Hat)c4c7eb22015-02-09 16:05:55 -050041 trace_foo_bar_with_cond("Some times print", cnt);
Steven Rostedt (Red Hat)74969462015-02-09 17:14:04 -050042
43 trace_foo_with_template_cond("prints other times", cnt);
44
45 trace_foo_with_template_print("I have to be different", cnt);
Steven Rostedt9cfe06f2009-04-14 21:37:03 -040046}
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;
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -050059static 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);
Steven Rostedt (Red Hat)74969462015-02-09 17:14:04 -050068 trace_foo_with_template_fn("Look at me too", cnt);
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -050069}
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);
Linus Torvaldsa0cb2b52017-10-27 20:35:31 -070082static int simple_thread_cnt;
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -050083
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050084int foo_bar_reg(void)
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -050085{
Steven Rostedt (VMware)65752572017-10-17 14:55:24 -040086 mutex_lock(&thread_mutex);
87 if (simple_thread_cnt++)
88 goto out;
89
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -050090 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 */
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -050096 simple_tsk_fn = kthread_run(simple_thread_fn, NULL, "event-sample-fn");
Steven Rostedt (VMware)65752572017-10-17 14:55:24 -040097 out:
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -050098 mutex_unlock(&thread_mutex);
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050099 return 0;
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -0500100}
101
102void foo_bar_unreg(void)
103{
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -0500104 mutex_lock(&thread_mutex);
Steven Rostedt (VMware)65752572017-10-17 14:55:24 -0400105 if (--simple_thread_cnt)
106 goto out;
107
108 pr_info("Killing thread for foo_bar_fn\n");
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -0500109 if (simple_tsk_fn)
110 kthread_stop(simple_tsk_fn);
111 simple_tsk_fn = NULL;
Steven Rostedt (VMware)65752572017-10-17 14:55:24 -0400112 out:
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -0500113 mutex_unlock(&thread_mutex);
114}
Steven Rostedt9cfe06f2009-04-14 21:37:03 -0400115
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);
Steven Rostedt (Red Hat)6adc13f82015-02-09 16:32:19 -0500128 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);
Steven Rostedt9cfe06f2009-04-14 21:37:03 -0400133}
134
135module_init(trace_event_init);
136module_exit(trace_event_exit);
137
138MODULE_AUTHOR("Steven Rostedt");
139MODULE_DESCRIPTION("trace-events-sample");
140MODULE_LICENSE("GPL");