btrfs: use file:line format for assertion report
[linux/fpc-iii.git] / samples / trace_events / trace-events-sample.c
blob1da597aa614160b50397459f33e627b42ca2be96
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/module.h>
3 #include <linux/kthread.h>
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.
11 #define CREATE_TRACE_POINTS
12 #include "trace-events-sample.h"
14 static const char *random_strings[] = {
15 "Mother Goose",
16 "Snoopy",
17 "Gandalf",
18 "Frodo",
19 "One ring to rule them all"
22 static void simple_thread_func(int cnt)
24 int array[6];
25 int len = cnt % 5;
26 int i;
28 set_current_state(TASK_INTERRUPTIBLE);
29 schedule_timeout(HZ);
31 for (i = 0; i < len; i++)
32 array[i] = i + 1;
33 array[i] = 0;
35 /* Silly tracepoints */
36 trace_foo_bar("hello", cnt, array, random_strings[len],
37 &current->cpus_allowed);
39 trace_foo_with_template_simple("HELLO", cnt);
41 trace_foo_bar_with_cond("Some times print", cnt);
43 trace_foo_with_template_cond("prints other times", cnt);
45 trace_foo_with_template_print("I have to be different", cnt);
48 static int simple_thread(void *arg)
50 int cnt = 0;
52 while (!kthread_should_stop())
53 simple_thread_func(cnt++);
55 return 0;
58 static struct task_struct *simple_tsk;
59 static struct task_struct *simple_tsk_fn;
61 static void simple_thread_func_fn(int cnt)
63 set_current_state(TASK_INTERRUPTIBLE);
64 schedule_timeout(HZ);
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);
71 static int simple_thread_fn(void *arg)
73 int cnt = 0;
75 while (!kthread_should_stop())
76 simple_thread_func_fn(cnt++);
78 return 0;
81 static DEFINE_MUTEX(thread_mutex);
82 static int simple_thread_cnt;
84 int foo_bar_reg(void)
86 mutex_lock(&thread_mutex);
87 if (simple_thread_cnt++)
88 goto out;
90 pr_info("Starting thread for foo_bar_fn\n");
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.
96 simple_tsk_fn = kthread_run(simple_thread_fn, NULL, "event-sample-fn");
97 out:
98 mutex_unlock(&thread_mutex);
99 return 0;
102 void foo_bar_unreg(void)
104 mutex_lock(&thread_mutex);
105 if (--simple_thread_cnt)
106 goto out;
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);
116 static int __init trace_event_init(void)
118 simple_tsk = kthread_run(simple_thread, NULL, "event-sample");
119 if (IS_ERR(simple_tsk))
120 return -1;
122 return 0;
125 static void __exit trace_event_exit(void)
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);
135 module_init(trace_event_init);
136 module_exit(trace_event_exit);
138 MODULE_AUTHOR("Steven Rostedt");
139 MODULE_DESCRIPTION("trace-events-sample");
140 MODULE_LICENSE("GPL");