1 // SPDX-License-Identifier: GPL-2.0
3 * Preempt / IRQ disable delay thread to test latency tracers
8 #include <linux/trace_clock.h>
9 #include <linux/delay.h>
10 #include <linux/interrupt.h>
11 #include <linux/irq.h>
12 #include <linux/kernel.h>
13 #include <linux/kobject.h>
14 #include <linux/kthread.h>
15 #include <linux/module.h>
16 #include <linux/printk.h>
17 #include <linux/string.h>
18 #include <linux/sysfs.h>
19 #include <linux/completion.h>
21 static ulong delay = 100;
22 static char test_mode[12] = "irq";
23 static uint burst_size = 1;
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)");
32 static struct completion done;
34 #define MIN(x, y) ((x) < (y) ? (x) : (y))
36 static void busy_wait(ulong time)
39 start = trace_clock_local();
41 end = trace_clock_local();
42 if (kthread_should_stop())
44 } while ((end - start) < (time * 1000));
47 static __always_inline void irqoff_test(void)
50 local_irq_save(flags);
52 local_irq_restore(flags);
55 static __always_inline void preemptoff_test(void)
62 static void execute_preemptirqtest(int idx)
64 if (!strcmp(test_mode, "irq"))
66 else if (!strcmp(test_mode, "preempt"))
68 else if (!strcmp(test_mode, "alternate")) {
76 #define DECLARE_TESTFN(POSTFIX) \
77 static void preemptirqtest_##POSTFIX(int idx) \
79 execute_preemptirqtest(idx); \
83 * We create 10 different functions, so that we can get 10 different
97 static void (*testfuncs[])(int) = {
110 #define NR_TEST_FUNCS ARRAY_SIZE(testfuncs)
112 static int preemptirq_delay_run(void *data)
115 int s = MIN(burst_size, NR_TEST_FUNCS);
117 for (i = 0; i < s; i++)
122 set_current_state(TASK_INTERRUPTIBLE);
123 while (!kthread_should_stop()) {
125 set_current_state(TASK_INTERRUPTIBLE);
128 __set_current_state(TASK_RUNNING);
133 static int preemptirq_run_test(void)
135 struct task_struct *task;
138 init_completion(&done);
140 snprintf(task_name, sizeof(task_name), "%s_test", test_mode);
141 task = kthread_run(preemptirq_delay_run, NULL, task_name);
143 return PTR_ERR(task);
145 wait_for_completion(&done);
152 static ssize_t trigger_store(struct kobject *kobj, struct kobj_attribute *attr,
153 const char *buf, size_t count)
157 ret = preemptirq_run_test();
163 static struct kobj_attribute trigger_attribute =
164 __ATTR(trigger, 0200, NULL, trigger_store);
166 static struct attribute *attrs[] = {
167 &trigger_attribute.attr,
171 static struct attribute_group attr_group = {
175 static struct kobject *preemptirq_delay_kobj;
177 static int __init preemptirq_delay_init(void)
181 retval = preemptirq_run_test();
185 preemptirq_delay_kobj = kobject_create_and_add("preemptirq_delay_test",
187 if (!preemptirq_delay_kobj)
190 retval = sysfs_create_group(preemptirq_delay_kobj, &attr_group);
192 kobject_put(preemptirq_delay_kobj);
197 static void __exit preemptirq_delay_exit(void)
199 kobject_put(preemptirq_delay_kobj);
202 module_init(preemptirq_delay_init)
203 module_exit(preemptirq_delay_exit)
204 MODULE_LICENSE("GPL v2");