Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Steven Rostedt | 9cfe06f | 2009-04-14 21:37:03 -0400 | [diff] [blame] | 2 | #include <linux/module.h> |
| 3 | #include <linux/kthread.h> |
| 4 | |
| 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. |
| 10 | */ |
| 11 | #define CREATE_TRACE_POINTS |
| 12 | #include "trace-events-sample.h" |
| 13 | |
Steven Rostedt (Red Hat) | 4e20e3a | 2015-02-09 15:27:04 -0500 | [diff] [blame] | 14 | static const char *random_strings[] = { |
| 15 | "Mother Goose", |
| 16 | "Snoopy", |
| 17 | "Gandalf", |
| 18 | "Frodo", |
| 19 | "One ring to rule them all" |
| 20 | }; |
Steven Rostedt | 9cfe06f | 2009-04-14 21:37:03 -0400 | [diff] [blame] | 21 | |
| 22 | static void simple_thread_func(int cnt) |
| 23 | { |
Masami Hiramatsu | b466b13 | 2021-11-22 18:30:30 +0900 | [diff] [blame] | 24 | unsigned long bitmask[1] = {0xdeadbeefUL}; |
Steven Rostedt (Red Hat) | 4e20e3a | 2015-02-09 15:27:04 -0500 | [diff] [blame] | 25 | int array[6]; |
| 26 | int len = cnt % 5; |
| 27 | int i; |
| 28 | |
Steven Rostedt | 9cfe06f | 2009-04-14 21:37:03 -0400 | [diff] [blame] | 29 | set_current_state(TASK_INTERRUPTIBLE); |
| 30 | schedule_timeout(HZ); |
Steven Rostedt (Red Hat) | 4e20e3a | 2015-02-09 15:27:04 -0500 | [diff] [blame] | 31 | |
| 32 | for (i = 0; i < len; i++) |
| 33 | array[i] = i + 1; |
| 34 | array[i] = 0; |
| 35 | |
Steven Rostedt (Red Hat) | c4c7eb2 | 2015-02-09 16:05:55 -0500 | [diff] [blame] | 36 | /* Silly tracepoints */ |
Steven Rostedt (Red Hat) | 4e20e3a | 2015-02-09 15:27:04 -0500 | [diff] [blame] | 37 | trace_foo_bar("hello", cnt, array, random_strings[len], |
Sebastian Andrzej Siewior | 3bd3706 | 2019-04-23 16:26:36 +0200 | [diff] [blame] | 38 | current->cpus_ptr); |
Steven Rostedt (Red Hat) | c4c7eb2 | 2015-02-09 16:05:55 -0500 | [diff] [blame] | 39 | |
Steven Rostedt (Red Hat) | 7496946 | 2015-02-09 17:14:04 -0500 | [diff] [blame] | 40 | trace_foo_with_template_simple("HELLO", cnt); |
| 41 | |
Steven Rostedt (Red Hat) | c4c7eb2 | 2015-02-09 16:05:55 -0500 | [diff] [blame] | 42 | trace_foo_bar_with_cond("Some times print", cnt); |
Steven Rostedt (Red Hat) | 7496946 | 2015-02-09 17:14:04 -0500 | [diff] [blame] | 43 | |
| 44 | trace_foo_with_template_cond("prints other times", cnt); |
| 45 | |
| 46 | trace_foo_with_template_print("I have to be different", cnt); |
Masami Hiramatsu | b466b13 | 2021-11-22 18:30:30 +0900 | [diff] [blame] | 47 | |
| 48 | trace_foo_rel_loc("Hello __rel_loc", cnt, bitmask); |
Steven Rostedt | 9cfe06f | 2009-04-14 21:37:03 -0400 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static int simple_thread(void *arg) |
| 52 | { |
| 53 | int cnt = 0; |
| 54 | |
| 55 | while (!kthread_should_stop()) |
| 56 | simple_thread_func(cnt++); |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static struct task_struct *simple_tsk; |
Steven Rostedt (Red Hat) | 6adc13f8 | 2015-02-09 16:32:19 -0500 | [diff] [blame] | 62 | static struct task_struct *simple_tsk_fn; |
| 63 | |
| 64 | static void simple_thread_func_fn(int cnt) |
| 65 | { |
| 66 | set_current_state(TASK_INTERRUPTIBLE); |
| 67 | schedule_timeout(HZ); |
| 68 | |
| 69 | /* More silly tracepoints */ |
| 70 | trace_foo_bar_with_fn("Look at me", cnt); |
Steven Rostedt (Red Hat) | 7496946 | 2015-02-09 17:14:04 -0500 | [diff] [blame] | 71 | trace_foo_with_template_fn("Look at me too", cnt); |
Steven Rostedt (Red Hat) | 6adc13f8 | 2015-02-09 16:32:19 -0500 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static int simple_thread_fn(void *arg) |
| 75 | { |
| 76 | int cnt = 0; |
| 77 | |
| 78 | while (!kthread_should_stop()) |
| 79 | simple_thread_func_fn(cnt++); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static DEFINE_MUTEX(thread_mutex); |
Linus Torvalds | a0cb2b5 | 2017-10-27 20:35:31 -0700 | [diff] [blame] | 85 | static int simple_thread_cnt; |
Steven Rostedt (Red Hat) | 6adc13f8 | 2015-02-09 16:32:19 -0500 | [diff] [blame] | 86 | |
Steven Rostedt (Red Hat) | 8cf868a | 2016-11-28 13:03:21 -0500 | [diff] [blame] | 87 | int foo_bar_reg(void) |
Steven Rostedt (Red Hat) | 6adc13f8 | 2015-02-09 16:32:19 -0500 | [diff] [blame] | 88 | { |
Steven Rostedt (VMware) | 6575257 | 2017-10-17 14:55:24 -0400 | [diff] [blame] | 89 | mutex_lock(&thread_mutex); |
| 90 | if (simple_thread_cnt++) |
| 91 | goto out; |
| 92 | |
Steven Rostedt (Red Hat) | 6adc13f8 | 2015-02-09 16:32:19 -0500 | [diff] [blame] | 93 | pr_info("Starting thread for foo_bar_fn\n"); |
| 94 | /* |
| 95 | * We shouldn't be able to start a trace when the module is |
| 96 | * unloading (there's other locks to prevent that). But |
| 97 | * for consistency sake, we still take the thread_mutex. |
| 98 | */ |
Steven Rostedt (Red Hat) | 6adc13f8 | 2015-02-09 16:32:19 -0500 | [diff] [blame] | 99 | simple_tsk_fn = kthread_run(simple_thread_fn, NULL, "event-sample-fn"); |
Steven Rostedt (VMware) | 6575257 | 2017-10-17 14:55:24 -0400 | [diff] [blame] | 100 | out: |
Steven Rostedt (Red Hat) | 6adc13f8 | 2015-02-09 16:32:19 -0500 | [diff] [blame] | 101 | mutex_unlock(&thread_mutex); |
Steven Rostedt (Red Hat) | 8cf868a | 2016-11-28 13:03:21 -0500 | [diff] [blame] | 102 | return 0; |
Steven Rostedt (Red Hat) | 6adc13f8 | 2015-02-09 16:32:19 -0500 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void foo_bar_unreg(void) |
| 106 | { |
Steven Rostedt (Red Hat) | 6adc13f8 | 2015-02-09 16:32:19 -0500 | [diff] [blame] | 107 | mutex_lock(&thread_mutex); |
Steven Rostedt (VMware) | 6575257 | 2017-10-17 14:55:24 -0400 | [diff] [blame] | 108 | if (--simple_thread_cnt) |
| 109 | goto out; |
| 110 | |
| 111 | pr_info("Killing thread for foo_bar_fn\n"); |
Steven Rostedt (Red Hat) | 6adc13f8 | 2015-02-09 16:32:19 -0500 | [diff] [blame] | 112 | if (simple_tsk_fn) |
| 113 | kthread_stop(simple_tsk_fn); |
| 114 | simple_tsk_fn = NULL; |
Steven Rostedt (VMware) | 6575257 | 2017-10-17 14:55:24 -0400 | [diff] [blame] | 115 | out: |
Steven Rostedt (Red Hat) | 6adc13f8 | 2015-02-09 16:32:19 -0500 | [diff] [blame] | 116 | mutex_unlock(&thread_mutex); |
| 117 | } |
Steven Rostedt | 9cfe06f | 2009-04-14 21:37:03 -0400 | [diff] [blame] | 118 | |
| 119 | static int __init trace_event_init(void) |
| 120 | { |
| 121 | simple_tsk = kthread_run(simple_thread, NULL, "event-sample"); |
| 122 | if (IS_ERR(simple_tsk)) |
| 123 | return -1; |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static void __exit trace_event_exit(void) |
| 129 | { |
| 130 | kthread_stop(simple_tsk); |
Steven Rostedt (Red Hat) | 6adc13f8 | 2015-02-09 16:32:19 -0500 | [diff] [blame] | 131 | mutex_lock(&thread_mutex); |
| 132 | if (simple_tsk_fn) |
| 133 | kthread_stop(simple_tsk_fn); |
| 134 | simple_tsk_fn = NULL; |
| 135 | mutex_unlock(&thread_mutex); |
Steven Rostedt | 9cfe06f | 2009-04-14 21:37:03 -0400 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | module_init(trace_event_init); |
| 139 | module_exit(trace_event_exit); |
| 140 | |
| 141 | MODULE_AUTHOR("Steven Rostedt"); |
| 142 | MODULE_DESCRIPTION("trace-events-sample"); |
| 143 | MODULE_LICENSE("GPL"); |