Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 #include <linux/module.h>
0003 #include <linux/kthread.h>
0004 
0005 /*
0006  * Any file that uses trace points, must include the header.
0007  * But only one file, must include the header by defining
0008  * CREATE_TRACE_POINTS first.  This will make the C code that
0009  * creates the handles for the trace points.
0010  */
0011 #define CREATE_TRACE_POINTS
0012 #include "trace-events-sample.h"
0013 
0014 static const char *random_strings[] = {
0015     "Mother Goose",
0016     "Snoopy",
0017     "Gandalf",
0018     "Frodo",
0019     "One ring to rule them all"
0020 };
0021 
0022 static void do_simple_thread_func(int cnt, const char *fmt, ...)
0023 {
0024     unsigned long bitmask[1] = {0xdeadbeefUL};
0025     va_list va;
0026     int array[6];
0027     int len = cnt % 5;
0028     int i;
0029 
0030     set_current_state(TASK_INTERRUPTIBLE);
0031     schedule_timeout(HZ);
0032 
0033     for (i = 0; i < len; i++)
0034         array[i] = i + 1;
0035     array[i] = 0;
0036 
0037     va_start(va, fmt);
0038 
0039     /* Silly tracepoints */
0040     trace_foo_bar("hello", cnt, array, random_strings[len],
0041               current->cpus_ptr, fmt, &va);
0042 
0043     va_end(va);
0044 
0045     trace_foo_with_template_simple("HELLO", cnt);
0046 
0047     trace_foo_bar_with_cond("Some times print", cnt);
0048 
0049     trace_foo_with_template_cond("prints other times", cnt);
0050 
0051     trace_foo_with_template_print("I have to be different", cnt);
0052 
0053     trace_foo_rel_loc("Hello __rel_loc", cnt, bitmask);
0054 }
0055 
0056 static void simple_thread_func(int cnt)
0057 {
0058     do_simple_thread_func(cnt, "iter=%d", cnt);
0059 }
0060 
0061 static int simple_thread(void *arg)
0062 {
0063     int cnt = 0;
0064 
0065     while (!kthread_should_stop())
0066         simple_thread_func(cnt++);
0067 
0068     return 0;
0069 }
0070 
0071 static struct task_struct *simple_tsk;
0072 static struct task_struct *simple_tsk_fn;
0073 
0074 static void simple_thread_func_fn(int cnt)
0075 {
0076     set_current_state(TASK_INTERRUPTIBLE);
0077     schedule_timeout(HZ);
0078 
0079     /* More silly tracepoints */
0080     trace_foo_bar_with_fn("Look at me", cnt);
0081     trace_foo_with_template_fn("Look at me too", cnt);
0082 }
0083 
0084 static int simple_thread_fn(void *arg)
0085 {
0086     int cnt = 0;
0087 
0088     while (!kthread_should_stop())
0089         simple_thread_func_fn(cnt++);
0090 
0091     return 0;
0092 }
0093 
0094 static DEFINE_MUTEX(thread_mutex);
0095 static int simple_thread_cnt;
0096 
0097 int foo_bar_reg(void)
0098 {
0099     mutex_lock(&thread_mutex);
0100     if (simple_thread_cnt++)
0101         goto out;
0102 
0103     pr_info("Starting thread for foo_bar_fn\n");
0104     /*
0105      * We shouldn't be able to start a trace when the module is
0106      * unloading (there's other locks to prevent that). But
0107      * for consistency sake, we still take the thread_mutex.
0108      */
0109     simple_tsk_fn = kthread_run(simple_thread_fn, NULL, "event-sample-fn");
0110  out:
0111     mutex_unlock(&thread_mutex);
0112     return 0;
0113 }
0114 
0115 void foo_bar_unreg(void)
0116 {
0117     mutex_lock(&thread_mutex);
0118     if (--simple_thread_cnt)
0119         goto out;
0120 
0121     pr_info("Killing thread for foo_bar_fn\n");
0122     if (simple_tsk_fn)
0123         kthread_stop(simple_tsk_fn);
0124     simple_tsk_fn = NULL;
0125  out:
0126     mutex_unlock(&thread_mutex);
0127 }
0128 
0129 static int __init trace_event_init(void)
0130 {
0131     simple_tsk = kthread_run(simple_thread, NULL, "event-sample");
0132     if (IS_ERR(simple_tsk))
0133         return -1;
0134 
0135     return 0;
0136 }
0137 
0138 static void __exit trace_event_exit(void)
0139 {
0140     kthread_stop(simple_tsk);
0141     mutex_lock(&thread_mutex);
0142     if (simple_tsk_fn)
0143         kthread_stop(simple_tsk_fn);
0144     simple_tsk_fn = NULL;
0145     mutex_unlock(&thread_mutex);
0146 }
0147 
0148 module_init(trace_event_init);
0149 module_exit(trace_event_exit);
0150 
0151 MODULE_AUTHOR("Steven Rostedt");
0152 MODULE_DESCRIPTION("trace-events-sample");
0153 MODULE_LICENSE("GPL");