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++)
117 set_current_state(TASK_INTERRUPTIBLE);
118 while (!kthread_should_stop()) {
120 set_current_state(TASK_INTERRUPTIBLE);
123 __set_current_state(TASK_RUNNING);
128 static int preemptirq_run_test(void)
130 struct task_struct *task;
134 snprintf(task_name, sizeof(task_name), "%s_test", test_mode);
135 task = kthread_run(preemptirq_delay_run, NULL, task_name);
137 return PTR_ERR(task);
144 static ssize_t trigger_store(struct kobject *kobj, struct kobj_attribute *attr,
145 const char *buf, size_t count)
149 ret = preemptirq_run_test();
155 static struct kobj_attribute trigger_attribute =
156 __ATTR(trigger, 0200, NULL, trigger_store);
158 static struct attribute *attrs[] = {
159 &trigger_attribute.attr,
163 static struct attribute_group attr_group = {
167 static struct kobject *preemptirq_delay_kobj;
169 static int __init preemptirq_delay_init(void)
173 retval = preemptirq_run_test();
177 preemptirq_delay_kobj = kobject_create_and_add("preemptirq_delay_test",
179 if (!preemptirq_delay_kobj)
182 retval = sysfs_create_group(preemptirq_delay_kobj, &attr_group);
184 kobject_put(preemptirq_delay_kobj);
189 static void __exit preemptirq_delay_exit(void)
191 kobject_put(preemptirq_delay_kobj);
194 module_init(preemptirq_delay_init)
195 module_exit(preemptirq_delay_exit)
196 MODULE_LICENSE("GPL v2");