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