189ed4249SDivya Indi // SPDX-License-Identifier: GPL-2.0-only
289ed4249SDivya Indi #include <linux/module.h>
389ed4249SDivya Indi #include <linux/kthread.h>
489ed4249SDivya Indi #include <linux/trace.h>
589ed4249SDivya Indi #include <linux/trace_events.h>
689ed4249SDivya Indi #include <linux/timer.h>
789ed4249SDivya Indi #include <linux/err.h>
889ed4249SDivya Indi #include <linux/jiffies.h>
9e9b7b1c0SKefeng Wang #include <linux/workqueue.h>
1089ed4249SDivya Indi
1189ed4249SDivya Indi /*
1289ed4249SDivya Indi * Any file that uses trace points, must include the header.
1389ed4249SDivya Indi * But only one file, must include the header by defining
1489ed4249SDivya Indi * CREATE_TRACE_POINTS first. This will make the C code that
1589ed4249SDivya Indi * creates the handles for the trace points.
1689ed4249SDivya Indi */
1789ed4249SDivya Indi #define CREATE_TRACE_POINTS
1889ed4249SDivya Indi #include "sample-trace-array.h"
1989ed4249SDivya Indi
2089ed4249SDivya Indi struct trace_array *tr;
2189ed4249SDivya Indi static void mytimer_handler(struct timer_list *unused);
2289ed4249SDivya Indi static struct task_struct *simple_tsk;
2389ed4249SDivya Indi
trace_work_fn(struct work_struct * work)24e9b7b1c0SKefeng Wang static void trace_work_fn(struct work_struct *work)
25e9b7b1c0SKefeng Wang {
26e9b7b1c0SKefeng Wang /*
27e9b7b1c0SKefeng Wang * Disable tracing for event "sample_event".
28e9b7b1c0SKefeng Wang */
29e9b7b1c0SKefeng Wang trace_array_set_clr_event(tr, "sample-subsystem", "sample_event",
30e9b7b1c0SKefeng Wang false);
31e9b7b1c0SKefeng Wang }
32e9b7b1c0SKefeng Wang static DECLARE_WORK(trace_work, trace_work_fn);
33e9b7b1c0SKefeng Wang
3489ed4249SDivya Indi /*
3589ed4249SDivya Indi * mytimer: Timer setup to disable tracing for event "sample_event". This
3689ed4249SDivya Indi * timer is only for the purposes of the sample module to demonstrate access of
3789ed4249SDivya Indi * Ftrace instances from within kernel.
3889ed4249SDivya Indi */
3989ed4249SDivya Indi static DEFINE_TIMER(mytimer, mytimer_handler);
4089ed4249SDivya Indi
mytimer_handler(struct timer_list * unused)4189ed4249SDivya Indi static void mytimer_handler(struct timer_list *unused)
4289ed4249SDivya Indi {
43e9b7b1c0SKefeng Wang schedule_work(&trace_work);
4489ed4249SDivya Indi }
4589ed4249SDivya Indi
simple_thread_func(int count)4689ed4249SDivya Indi static void simple_thread_func(int count)
4789ed4249SDivya Indi {
4889ed4249SDivya Indi set_current_state(TASK_INTERRUPTIBLE);
4989ed4249SDivya Indi schedule_timeout(HZ);
5089ed4249SDivya Indi
5189ed4249SDivya Indi /*
5289ed4249SDivya Indi * Printing count value using trace_array_printk() - trace_printk()
5389ed4249SDivya Indi * equivalent for the instance buffers.
5489ed4249SDivya Indi */
5589ed4249SDivya Indi trace_array_printk(tr, _THIS_IP_, "trace_array_printk: count=%d\n",
5689ed4249SDivya Indi count);
5789ed4249SDivya Indi /*
5889ed4249SDivya Indi * Tracepoint for event "sample_event". This will print the
5989ed4249SDivya Indi * current value of count and current jiffies.
6089ed4249SDivya Indi */
6189ed4249SDivya Indi trace_sample_event(count, jiffies);
6289ed4249SDivya Indi }
6389ed4249SDivya Indi
simple_thread(void * arg)6489ed4249SDivya Indi static int simple_thread(void *arg)
6589ed4249SDivya Indi {
6689ed4249SDivya Indi int count = 0;
6789ed4249SDivya Indi unsigned long delay = msecs_to_jiffies(5000);
6889ed4249SDivya Indi
6989ed4249SDivya Indi /*
7089ed4249SDivya Indi * Enable tracing for "sample_event".
7189ed4249SDivya Indi */
7289ed4249SDivya Indi trace_array_set_clr_event(tr, "sample-subsystem", "sample_event", true);
7389ed4249SDivya Indi
7489ed4249SDivya Indi /*
7589ed4249SDivya Indi * Adding timer - mytimer. This timer will disable tracing after
7689ed4249SDivya Indi * delay seconds.
7789ed4249SDivya Indi *
7889ed4249SDivya Indi */
7989ed4249SDivya Indi add_timer(&mytimer);
8089ed4249SDivya Indi mod_timer(&mytimer, jiffies+delay);
8189ed4249SDivya Indi
8289ed4249SDivya Indi while (!kthread_should_stop())
8389ed4249SDivya Indi simple_thread_func(count++);
8489ed4249SDivya Indi
858fa7292fSThomas Gleixner timer_delete(&mytimer);
86e9b7b1c0SKefeng Wang cancel_work_sync(&trace_work);
8789ed4249SDivya Indi
8889ed4249SDivya Indi /*
8989ed4249SDivya Indi * trace_array_put() decrements the reference counter associated with
9089ed4249SDivya Indi * the trace array - "tr". We are done using the trace array, hence
9189ed4249SDivya Indi * decrement the reference counter so that it can be destroyed using
9289ed4249SDivya Indi * trace_array_destroy().
9389ed4249SDivya Indi */
9489ed4249SDivya Indi trace_array_put(tr);
9589ed4249SDivya Indi
9689ed4249SDivya Indi return 0;
9789ed4249SDivya Indi }
9889ed4249SDivya Indi
sample_trace_array_init(void)9989ed4249SDivya Indi static int __init sample_trace_array_init(void)
10089ed4249SDivya Indi {
10189ed4249SDivya Indi /*
10289ed4249SDivya Indi * Return a pointer to the trace array with name "sample-instance" if it
10389ed4249SDivya Indi * exists, else create a new trace array.
10489ed4249SDivya Indi *
10589ed4249SDivya Indi * NOTE: This function increments the reference counter
10689ed4249SDivya Indi * associated with the trace array - "tr".
10789ed4249SDivya Indi */
108d2356997SSteven Rostedt (Google) tr = trace_array_get_by_name("sample-instance", "sched,timer,kprobes");
10989ed4249SDivya Indi
11089ed4249SDivya Indi if (!tr)
11189ed4249SDivya Indi return -1;
11289ed4249SDivya Indi /*
11389ed4249SDivya Indi * If context specific per-cpu buffers havent already been allocated.
11489ed4249SDivya Indi */
115*1b0c192cSSteven Rostedt trace_array_init_printk(tr);
11689ed4249SDivya Indi
11789ed4249SDivya Indi simple_tsk = kthread_run(simple_thread, NULL, "sample-instance");
1189fbc01cdSKefeng Wang if (IS_ERR(simple_tsk)) {
1199fbc01cdSKefeng Wang trace_array_put(tr);
1209fbc01cdSKefeng Wang trace_array_destroy(tr);
12189ed4249SDivya Indi return -1;
1229fbc01cdSKefeng Wang }
1239fbc01cdSKefeng Wang
12489ed4249SDivya Indi return 0;
12589ed4249SDivya Indi }
12689ed4249SDivya Indi
sample_trace_array_exit(void)12789ed4249SDivya Indi static void __exit sample_trace_array_exit(void)
12889ed4249SDivya Indi {
12989ed4249SDivya Indi kthread_stop(simple_tsk);
13089ed4249SDivya Indi
13189ed4249SDivya Indi /*
13289ed4249SDivya Indi * We are unloading our module and no longer require the trace array.
13389ed4249SDivya Indi * Remove/destroy "tr" using trace_array_destroy()
13489ed4249SDivya Indi */
13589ed4249SDivya Indi trace_array_destroy(tr);
13689ed4249SDivya Indi }
13789ed4249SDivya Indi
13889ed4249SDivya Indi module_init(sample_trace_array_init);
13989ed4249SDivya Indi module_exit(sample_trace_array_exit);
14089ed4249SDivya Indi
14189ed4249SDivya Indi MODULE_AUTHOR("Divya Indi");
14289ed4249SDivya Indi MODULE_DESCRIPTION("Sample module for kernel access to Ftrace instances");
14389ed4249SDivya Indi MODULE_LICENSE("GPL");
144