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