split dev_queue
[cor.git] / kernel / trace / preemptirq_delay_test.c
blob31c0fad4cb9e18910cd318abb4d76c4db09d592e
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Preempt / IRQ disable delay thread to test latency tracers
5 * Copyright (C) 2018 Joel Fernandes (Google) <joel@joelfernandes.org>
6 */
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)
35 u64 start, end;
36 start = trace_clock_local();
37 do {
38 end = trace_clock_local();
39 if (kthread_should_stop())
40 break;
41 } while ((end - start) < (time * 1000));
44 static __always_inline void irqoff_test(void)
46 unsigned long flags;
47 local_irq_save(flags);
48 busy_wait(delay);
49 local_irq_restore(flags);
52 static __always_inline void preemptoff_test(void)
54 preempt_disable();
55 busy_wait(delay);
56 preempt_enable();
59 static void execute_preemptirqtest(int idx)
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();
73 #define DECLARE_TESTFN(POSTFIX) \
74 static void preemptirqtest_##POSTFIX(int idx) \
75 { \
76 execute_preemptirqtest(idx); \
77 } \
80 * We create 10 different functions, so that we can get 10 different
81 * backtraces.
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)
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,
107 #define NR_TEST_FUNCS ARRAY_SIZE(testfuncs)
109 static int preemptirq_delay_run(void *data)
111 int i;
112 int s = MIN(burst_size, NR_TEST_FUNCS);
114 for (i = 0; i < s; i++)
115 (testfuncs[i])(i);
116 return 0;
119 static struct task_struct *preemptirq_start_test(void)
121 char task_name[50];
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();
132 return count;
135 static struct kobj_attribute trigger_attribute =
136 __ATTR(trigger, 0200, NULL, trigger_store);
138 static struct attribute *attrs[] = {
139 &trigger_attribute.attr,
140 NULL,
143 static struct attribute_group attr_group = {
144 .attrs = attrs,
147 static struct kobject *preemptirq_delay_kobj;
149 static int __init preemptirq_delay_init(void)
151 struct task_struct *test_task;
152 int retval;
154 test_task = preemptirq_start_test();
155 retval = PTR_ERR_OR_ZERO(test_task);
156 if (retval != 0)
157 return retval;
159 preemptirq_delay_kobj = kobject_create_and_add("preemptirq_delay_test",
160 kernel_kobj);
161 if (!preemptirq_delay_kobj)
162 return -ENOMEM;
164 retval = sysfs_create_group(preemptirq_delay_kobj, &attr_group);
165 if (retval)
166 kobject_put(preemptirq_delay_kobj);
168 return retval;
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");