]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - samples/trace_events/trace-events-sample.c
tracing/samples: Fix creation and deletion of simple_thread_fn creation
[mirror_ubuntu-bionic-kernel.git] / samples / trace_events / trace-events-sample.c
CommitLineData
9cfe06f8
SR
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
4e20e3a6
SRRH
13static const char *random_strings[] = {
14 "Mother Goose",
15 "Snoopy",
16 "Gandalf",
17 "Frodo",
18 "One ring to rule them all"
19};
9cfe06f8
SR
20
21static void simple_thread_func(int cnt)
22{
4e20e3a6
SRRH
23 int array[6];
24 int len = cnt % 5;
25 int i;
26
9cfe06f8
SR
27 set_current_state(TASK_INTERRUPTIBLE);
28 schedule_timeout(HZ);
4e20e3a6
SRRH
29
30 for (i = 0; i < len; i++)
31 array[i] = i + 1;
32 array[i] = 0;
33
c4c7eb29 34 /* Silly tracepoints */
4e20e3a6 35 trace_foo_bar("hello", cnt, array, random_strings[len],
0c98d344 36 &current->cpus_allowed);
c4c7eb29 37
7496946a
SRRH
38 trace_foo_with_template_simple("HELLO", cnt);
39
c4c7eb29 40 trace_foo_bar_with_cond("Some times print", cnt);
7496946a
SRRH
41
42 trace_foo_with_template_cond("prints other times", cnt);
43
44 trace_foo_with_template_print("I have to be different", cnt);
9cfe06f8
SR
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;
6adc13f8
SRRH
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);
7496946a 67 trace_foo_with_template_fn("Look at me too", cnt);
6adc13f8
SRRH
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);
6575257c 81static bool simple_thread_cnt;
6adc13f8 82
8cf868af 83int foo_bar_reg(void)
6adc13f8 84{
6575257c
SRV
85 mutex_lock(&thread_mutex);
86 if (simple_thread_cnt++)
87 goto out;
88
6adc13f8
SRRH
89 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 */
6adc13f8 95 simple_tsk_fn = kthread_run(simple_thread_fn, NULL, "event-sample-fn");
6575257c 96 out:
6adc13f8 97 mutex_unlock(&thread_mutex);
8cf868af 98 return 0;
6adc13f8
SRRH
99}
100
101void foo_bar_unreg(void)
102{
6adc13f8 103 mutex_lock(&thread_mutex);
6575257c
SRV
104 if (--simple_thread_cnt)
105 goto out;
106
107 pr_info("Killing thread for foo_bar_fn\n");
6adc13f8
SRRH
108 if (simple_tsk_fn)
109 kthread_stop(simple_tsk_fn);
110 simple_tsk_fn = NULL;
6575257c 111 out:
6adc13f8
SRRH
112 mutex_unlock(&thread_mutex);
113}
9cfe06f8
SR
114
115static int __init trace_event_init(void)
116{
117 simple_tsk = kthread_run(simple_thread, NULL, "event-sample");
118 if (IS_ERR(simple_tsk))
119 return -1;
120
121 return 0;
122}
123
124static void __exit trace_event_exit(void)
125{
126 kthread_stop(simple_tsk);
6adc13f8
SRRH
127 mutex_lock(&thread_mutex);
128 if (simple_tsk_fn)
129 kthread_stop(simple_tsk_fn);
130 simple_tsk_fn = NULL;
131 mutex_unlock(&thread_mutex);
9cfe06f8
SR
132}
133
134module_init(trace_event_init);
135module_exit(trace_event_exit);
136
137MODULE_AUTHOR("Steven Rostedt");
138MODULE_DESCRIPTION("trace-events-sample");
139MODULE_LICENSE("GPL");