]>
Commit | Line | Data |
---|---|---|
f96e8577 JFG |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Preempt / IRQ disable delay thread to test latency tracers | |
4 | * | |
5 | * Copyright (C) 2018 Joel Fernandes (Google) <[email protected]> | |
6 | */ | |
7 | ||
12ad0cb2 | 8 | #include <linux/trace_clock.h> |
f96e8577 JFG |
9 | #include <linux/delay.h> |
10 | #include <linux/interrupt.h> | |
11 | #include <linux/irq.h> | |
12 | #include <linux/kernel.h> | |
79393723 | 13 | #include <linux/kobject.h> |
f96e8577 | 14 | #include <linux/kthread.h> |
f96e8577 JFG |
15 | #include <linux/module.h> |
16 | #include <linux/printk.h> | |
17 | #include <linux/string.h> | |
79393723 | 18 | #include <linux/sysfs.h> |
8b1fac2e | 19 | #include <linux/completion.h> |
f96e8577 JFG |
20 | |
21 | static ulong delay = 100; | |
79393723 VRB |
22 | static char test_mode[12] = "irq"; |
23 | static uint burst_size = 1; | |
f96e8577 | 24 | |
79393723 VRB |
25 | module_param_named(delay, delay, ulong, 0444); |
26 | module_param_string(test_mode, test_mode, 12, 0444); | |
27 | module_param_named(burst_size, burst_size, uint, 0444); | |
28 | MODULE_PARM_DESC(delay, "Period in microseconds (100 us default)"); | |
29 | MODULE_PARM_DESC(test_mode, "Mode of the test such as preempt, irq, or alternate (default irq)"); | |
30 | MODULE_PARM_DESC(burst_size, "The size of a burst (default 1)"); | |
31 | ||
8b1fac2e SRV |
32 | static struct completion done; |
33 | ||
79393723 | 34 | #define MIN(x, y) ((x) < (y) ? (x) : (y)) |
f96e8577 JFG |
35 | |
36 | static void busy_wait(ulong time) | |
37 | { | |
12ad0cb2 SRV |
38 | u64 start, end; |
39 | start = trace_clock_local(); | |
f96e8577 | 40 | do { |
12ad0cb2 | 41 | end = trace_clock_local(); |
f96e8577 JFG |
42 | if (kthread_should_stop()) |
43 | break; | |
12ad0cb2 | 44 | } while ((end - start) < (time * 1000)); |
f96e8577 JFG |
45 | } |
46 | ||
79393723 | 47 | static __always_inline void irqoff_test(void) |
f96e8577 JFG |
48 | { |
49 | unsigned long flags; | |
79393723 VRB |
50 | local_irq_save(flags); |
51 | busy_wait(delay); | |
52 | local_irq_restore(flags); | |
53 | } | |
f96e8577 | 54 | |
79393723 VRB |
55 | static __always_inline void preemptoff_test(void) |
56 | { | |
57 | preempt_disable(); | |
58 | busy_wait(delay); | |
59 | preempt_enable(); | |
60 | } | |
61 | ||
62 | static void execute_preemptirqtest(int idx) | |
63 | { | |
64 | if (!strcmp(test_mode, "irq")) | |
65 | irqoff_test(); | |
66 | else if (!strcmp(test_mode, "preempt")) | |
67 | preemptoff_test(); | |
68 | else if (!strcmp(test_mode, "alternate")) { | |
69 | if (idx % 2 == 0) | |
70 | irqoff_test(); | |
71 | else | |
72 | preemptoff_test(); | |
f96e8577 | 73 | } |
79393723 VRB |
74 | } |
75 | ||
76 | #define DECLARE_TESTFN(POSTFIX) \ | |
77 | static void preemptirqtest_##POSTFIX(int idx) \ | |
78 | { \ | |
79 | execute_preemptirqtest(idx); \ | |
80 | } \ | |
f96e8577 | 81 | |
79393723 VRB |
82 | /* |
83 | * We create 10 different functions, so that we can get 10 different | |
84 | * backtraces. | |
85 | */ | |
86 | DECLARE_TESTFN(0) | |
87 | DECLARE_TESTFN(1) | |
88 | DECLARE_TESTFN(2) | |
89 | DECLARE_TESTFN(3) | |
90 | DECLARE_TESTFN(4) | |
91 | DECLARE_TESTFN(5) | |
92 | DECLARE_TESTFN(6) | |
93 | DECLARE_TESTFN(7) | |
94 | DECLARE_TESTFN(8) | |
95 | DECLARE_TESTFN(9) | |
96 | ||
97 | static void (*testfuncs[])(int) = { | |
98 | preemptirqtest_0, | |
99 | preemptirqtest_1, | |
100 | preemptirqtest_2, | |
101 | preemptirqtest_3, | |
102 | preemptirqtest_4, | |
103 | preemptirqtest_5, | |
104 | preemptirqtest_6, | |
105 | preemptirqtest_7, | |
106 | preemptirqtest_8, | |
107 | preemptirqtest_9, | |
108 | }; | |
109 | ||
110 | #define NR_TEST_FUNCS ARRAY_SIZE(testfuncs) | |
111 | ||
112 | static int preemptirq_delay_run(void *data) | |
113 | { | |
114 | int i; | |
115 | int s = MIN(burst_size, NR_TEST_FUNCS); | |
116 | ||
117 | for (i = 0; i < s; i++) | |
118 | (testfuncs[i])(i); | |
d16a8c31 | 119 | |
8b1fac2e SRV |
120 | complete(&done); |
121 | ||
d16a8c31 SRV |
122 | set_current_state(TASK_INTERRUPTIBLE); |
123 | while (!kthread_should_stop()) { | |
124 | schedule(); | |
125 | set_current_state(TASK_INTERRUPTIBLE); | |
126 | } | |
127 | ||
128 | __set_current_state(TASK_RUNNING); | |
129 | ||
f96e8577 JFG |
130 | return 0; |
131 | } | |
132 | ||
d16a8c31 | 133 | static int preemptirq_run_test(void) |
f96e8577 | 134 | { |
d16a8c31 | 135 | struct task_struct *task; |
f96e8577 | 136 | char task_name[50]; |
f96e8577 | 137 | |
8b1fac2e SRV |
138 | init_completion(&done); |
139 | ||
f96e8577 | 140 | snprintf(task_name, sizeof(task_name), "%s_test", test_mode); |
d16a8c31 SRV |
141 | task = kthread_run(preemptirq_delay_run, NULL, task_name); |
142 | if (IS_ERR(task)) | |
143 | return PTR_ERR(task); | |
8b1fac2e SRV |
144 | if (task) { |
145 | wait_for_completion(&done); | |
d16a8c31 | 146 | kthread_stop(task); |
8b1fac2e | 147 | } |
d16a8c31 | 148 | return 0; |
79393723 VRB |
149 | } |
150 | ||
151 | ||
152 | static ssize_t trigger_store(struct kobject *kobj, struct kobj_attribute *attr, | |
153 | const char *buf, size_t count) | |
154 | { | |
d16a8c31 SRV |
155 | ssize_t ret; |
156 | ||
157 | ret = preemptirq_run_test(); | |
158 | if (ret) | |
159 | return ret; | |
79393723 VRB |
160 | return count; |
161 | } | |
162 | ||
163 | static struct kobj_attribute trigger_attribute = | |
164 | __ATTR(trigger, 0200, NULL, trigger_store); | |
165 | ||
166 | static struct attribute *attrs[] = { | |
167 | &trigger_attribute.attr, | |
168 | NULL, | |
169 | }; | |
170 | ||
171 | static struct attribute_group attr_group = { | |
172 | .attrs = attrs, | |
173 | }; | |
174 | ||
175 | static struct kobject *preemptirq_delay_kobj; | |
176 | ||
177 | static int __init preemptirq_delay_init(void) | |
178 | { | |
79393723 VRB |
179 | int retval; |
180 | ||
d16a8c31 | 181 | retval = preemptirq_run_test(); |
79393723 VRB |
182 | if (retval != 0) |
183 | return retval; | |
184 | ||
185 | preemptirq_delay_kobj = kobject_create_and_add("preemptirq_delay_test", | |
186 | kernel_kobj); | |
187 | if (!preemptirq_delay_kobj) | |
188 | return -ENOMEM; | |
189 | ||
190 | retval = sysfs_create_group(preemptirq_delay_kobj, &attr_group); | |
191 | if (retval) | |
192 | kobject_put(preemptirq_delay_kobj); | |
f96e8577 | 193 | |
79393723 | 194 | return retval; |
f96e8577 JFG |
195 | } |
196 | ||
197 | static void __exit preemptirq_delay_exit(void) | |
198 | { | |
79393723 | 199 | kobject_put(preemptirq_delay_kobj); |
f96e8577 JFG |
200 | } |
201 | ||
202 | module_init(preemptirq_delay_init) | |
203 | module_exit(preemptirq_delay_exit) | |
204 | MODULE_LICENSE("GPL v2"); |