Blame view
samples/trace_events/trace-events-sample.c
2.75 KB
9cfe06f8c tracing/events: a... |
1 2 3 4 5 6 7 8 9 10 11 |
#include <linux/module.h> #include <linux/kthread.h> /* * Any file that uses trace points, must include the header. * But only one file, must include the header by defining * CREATE_TRACE_POINTS first. This will make the C code that * creates the handles for the trace points. */ #define CREATE_TRACE_POINTS #include "trace-events-sample.h" |
4e20e3a60 tracing: Update t... |
12 13 14 15 16 17 18 |
static const char *random_strings[] = { "Mother Goose", "Snoopy", "Gandalf", "Frodo", "One ring to rule them all" }; |
9cfe06f8c tracing/events: a... |
19 20 21 |
static void simple_thread_func(int cnt) { |
4e20e3a60 tracing: Update t... |
22 23 24 |
int array[6]; int len = cnt % 5; int i; |
9cfe06f8c tracing/events: a... |
25 26 |
set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(HZ); |
4e20e3a60 tracing: Update t... |
27 28 29 30 |
for (i = 0; i < len; i++) array[i] = i + 1; array[i] = 0; |
c4c7eb293 tracing: Add TRAC... |
31 |
/* Silly tracepoints */ |
4e20e3a60 tracing: Update t... |
32 |
trace_foo_bar("hello", cnt, array, random_strings[len], |
0c98d344f sched/core: Remov... |
33 |
¤t->cpus_allowed); |
c4c7eb293 tracing: Add TRAC... |
34 |
|
7496946a8 tracing: Add samp... |
35 |
trace_foo_with_template_simple("HELLO", cnt); |
c4c7eb293 tracing: Add TRAC... |
36 |
trace_foo_bar_with_cond("Some times print", cnt); |
7496946a8 tracing: Add samp... |
37 38 39 40 |
trace_foo_with_template_cond("prints other times", cnt); trace_foo_with_template_print("I have to be different", cnt); |
9cfe06f8c tracing/events: a... |
41 42 43 44 45 46 47 48 49 50 51 52 53 |
} static int simple_thread(void *arg) { int cnt = 0; while (!kthread_should_stop()) simple_thread_func(cnt++); return 0; } static struct task_struct *simple_tsk; |
6adc13f8c tracing: Add TRAC... |
54 55 56 57 58 59 60 61 62 |
static struct task_struct *simple_tsk_fn; static void simple_thread_func_fn(int cnt) { set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(HZ); /* More silly tracepoints */ trace_foo_bar_with_fn("Look at me", cnt); |
7496946a8 tracing: Add samp... |
63 |
trace_foo_with_template_fn("Look at me too", cnt); |
6adc13f8c tracing: Add TRAC... |
64 65 66 67 68 69 70 71 72 73 74 75 76 |
} static int simple_thread_fn(void *arg) { int cnt = 0; while (!kthread_should_stop()) simple_thread_func_fn(cnt++); return 0; } static DEFINE_MUTEX(thread_mutex); |
8cf868aff tracing: Have the... |
77 |
int foo_bar_reg(void) |
6adc13f8c tracing: Add TRAC... |
78 79 80 81 82 83 84 85 86 87 88 |
{ pr_info("Starting thread for foo_bar_fn "); /* * We shouldn't be able to start a trace when the module is * unloading (there's other locks to prevent that). But * for consistency sake, we still take the thread_mutex. */ mutex_lock(&thread_mutex); simple_tsk_fn = kthread_run(simple_thread_fn, NULL, "event-sample-fn"); mutex_unlock(&thread_mutex); |
8cf868aff tracing: Have the... |
89 |
return 0; |
6adc13f8c tracing: Add TRAC... |
90 91 92 93 94 95 96 97 98 99 100 101 102 |
} void foo_bar_unreg(void) { pr_info("Killing thread for foo_bar_fn "); /* protect against module unloading */ mutex_lock(&thread_mutex); if (simple_tsk_fn) kthread_stop(simple_tsk_fn); simple_tsk_fn = NULL; mutex_unlock(&thread_mutex); } |
9cfe06f8c tracing/events: a... |
103 104 105 106 107 108 109 110 111 112 113 114 115 |
static int __init trace_event_init(void) { simple_tsk = kthread_run(simple_thread, NULL, "event-sample"); if (IS_ERR(simple_tsk)) return -1; return 0; } static void __exit trace_event_exit(void) { kthread_stop(simple_tsk); |
6adc13f8c tracing: Add TRAC... |
116 117 118 119 120 |
mutex_lock(&thread_mutex); if (simple_tsk_fn) kthread_stop(simple_tsk_fn); simple_tsk_fn = NULL; mutex_unlock(&thread_mutex); |
9cfe06f8c tracing/events: a... |
121 122 123 124 125 126 127 128 |
} module_init(trace_event_init); module_exit(trace_event_exit); MODULE_AUTHOR("Steven Rostedt"); MODULE_DESCRIPTION("trace-events-sample"); MODULE_LICENSE("GPL"); |