~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/samples/ftrace/sample-trace-array.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 #include <linux/module.h>
  3 #include <linux/kthread.h>
  4 #include <linux/trace.h>
  5 #include <linux/trace_events.h>
  6 #include <linux/timer.h>
  7 #include <linux/err.h>
  8 #include <linux/jiffies.h>
  9 #include <linux/workqueue.h>
 10 
 11 /*
 12  * Any file that uses trace points, must include the header.
 13  * But only one file, must include the header by defining
 14  * CREATE_TRACE_POINTS first.  This will make the C code that
 15  * creates the handles for the trace points.
 16  */
 17 #define CREATE_TRACE_POINTS
 18 #include "sample-trace-array.h"
 19 
 20 struct trace_array *tr;
 21 static void mytimer_handler(struct timer_list *unused);
 22 static struct task_struct *simple_tsk;
 23 
 24 static void trace_work_fn(struct work_struct *work)
 25 {
 26         /*
 27          * Disable tracing for event "sample_event".
 28          */
 29         trace_array_set_clr_event(tr, "sample-subsystem", "sample_event",
 30                         false);
 31 }
 32 static DECLARE_WORK(trace_work, trace_work_fn);
 33 
 34 /*
 35  * mytimer: Timer setup to disable tracing for event "sample_event". This
 36  * timer is only for the purposes of the sample module to demonstrate access of
 37  * Ftrace instances from within kernel.
 38  */
 39 static DEFINE_TIMER(mytimer, mytimer_handler);
 40 
 41 static void mytimer_handler(struct timer_list *unused)
 42 {
 43         schedule_work(&trace_work);
 44 }
 45 
 46 static void simple_thread_func(int count)
 47 {
 48         set_current_state(TASK_INTERRUPTIBLE);
 49         schedule_timeout(HZ);
 50 
 51         /*
 52          * Printing count value using trace_array_printk() - trace_printk()
 53          * equivalent for the instance buffers.
 54          */
 55         trace_array_printk(tr, _THIS_IP_, "trace_array_printk: count=%d\n",
 56                         count);
 57         /*
 58          * Tracepoint for event "sample_event". This will print the
 59          * current value of count and current jiffies.
 60          */
 61         trace_sample_event(count, jiffies);
 62 }
 63 
 64 static int simple_thread(void *arg)
 65 {
 66         int count = 0;
 67         unsigned long delay = msecs_to_jiffies(5000);
 68 
 69         /*
 70          * Enable tracing for "sample_event".
 71          */
 72         trace_array_set_clr_event(tr, "sample-subsystem", "sample_event", true);
 73 
 74         /*
 75          * Adding timer - mytimer. This timer will disable tracing after
 76          * delay seconds.
 77          *
 78          */
 79         add_timer(&mytimer);
 80         mod_timer(&mytimer, jiffies+delay);
 81 
 82         while (!kthread_should_stop())
 83                 simple_thread_func(count++);
 84 
 85         del_timer(&mytimer);
 86         cancel_work_sync(&trace_work);
 87 
 88         /*
 89          * trace_array_put() decrements the reference counter associated with
 90          * the trace array - "tr". We are done using the trace array, hence
 91          * decrement the reference counter so that it can be destroyed using
 92          * trace_array_destroy().
 93          */
 94         trace_array_put(tr);
 95 
 96         return 0;
 97 }
 98 
 99 static int __init sample_trace_array_init(void)
100 {
101         /*
102          * Return a pointer to the trace array with name "sample-instance" if it
103          * exists, else create a new trace array.
104          *
105          * NOTE: This function increments the reference counter
106          * associated with the trace array - "tr".
107          */
108         tr = trace_array_get_by_name("sample-instance", "sched,timer,kprobes");
109 
110         if (!tr)
111                 return -1;
112         /*
113          * If context specific per-cpu buffers havent already been allocated.
114          */
115         trace_printk_init_buffers();
116 
117         simple_tsk = kthread_run(simple_thread, NULL, "sample-instance");
118         if (IS_ERR(simple_tsk)) {
119                 trace_array_put(tr);
120                 trace_array_destroy(tr);
121                 return -1;
122         }
123 
124         return 0;
125 }
126 
127 static void __exit sample_trace_array_exit(void)
128 {
129         kthread_stop(simple_tsk);
130 
131         /*
132          * We are unloading our module and no longer require the trace array.
133          * Remove/destroy "tr" using trace_array_destroy()
134          */
135         trace_array_destroy(tr);
136 }
137 
138 module_init(sample_trace_array_init);
139 module_exit(sample_trace_array_exit);
140 
141 MODULE_AUTHOR("Divya Indi");
142 MODULE_DESCRIPTION("Sample module for kernel access to Ftrace instances");
143 MODULE_LICENSE("GPL");
144 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php