1 // SPDX-License-Identifier: GPL-2.0
3 * Preempt / IRQ disable delay thread to test latency tracers
5 * Copyright (C) 2018 Joel Fernandes (Google) <joel@joelfernandes.org>
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>
20 static ulong delay = 100;
21 static char test_mode[12] = "irq";
22 static uint burst_size = 1;
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)");
31 #define MIN(x, y) ((x) < (y) ? (x) : (y))
33 static void busy_wait(ulong time)
36 start = trace_clock_local();
38 end = trace_clock_local();
39 if (kthread_should_stop())
41 } while ((end - start) < (time * 1000));
44 static __always_inline void irqoff_test(void)
47 local_irq_save(flags);
49 local_irq_restore(flags);
52 static __always_inline void preemptoff_test(void)
59 static void execute_preemptirqtest(int idx)
61 if (!strcmp(test_mode, "irq"))
63 else if (!strcmp(test_mode, "preempt"))
65 else if (!strcmp(test_mode, "alternate")) {
73 #define DECLARE_TESTFN(POSTFIX) \
74 static void preemptirqtest_##POSTFIX(int idx) \
76 execute_preemptirqtest(idx); \
80 * We create 10 different functions, so that we can get 10 different
94 static void (*testfuncs[])(int) = {
107 #define NR_TEST_FUNCS ARRAY_SIZE(testfuncs)
109 static int preemptirq_delay_run(void *data)
112 int s = MIN(burst_size, NR_TEST_FUNCS);
114 for (i = 0; i < s; i++)
119 static struct task_struct *preemptirq_start_test(void)
123 snprintf(task_name, sizeof(task_name), "%s_test", test_mode);
124 return kthread_run(preemptirq_delay_run, NULL, task_name);
128 static ssize_t trigger_store(struct kobject *kobj, struct kobj_attribute *attr,
129 const char *buf, size_t count)
131 preemptirq_start_test();
135 static struct kobj_attribute trigger_attribute =
136 __ATTR(trigger, 0200, NULL, trigger_store);
138 static struct attribute *attrs[] = {
139 &trigger_attribute.attr,
143 static struct attribute_group attr_group = {
147 static struct kobject *preemptirq_delay_kobj;
149 static int __init preemptirq_delay_init(void)
151 struct task_struct *test_task;
154 test_task = preemptirq_start_test();
155 retval = PTR_ERR_OR_ZERO(test_task);
159 preemptirq_delay_kobj = kobject_create_and_add("preemptirq_delay_test",
161 if (!preemptirq_delay_kobj)
164 retval = sysfs_create_group(preemptirq_delay_kobj, &attr_group);
166 kobject_put(preemptirq_delay_kobj);
171 static void __exit preemptirq_delay_exit(void)
173 kobject_put(preemptirq_delay_kobj);
176 module_init(preemptirq_delay_init)
177 module_exit(preemptirq_delay_exit)
178 MODULE_LICENSE("GPL v2");