blob: 31c0fad4cb9e18910cd318abb4d76c4db09d592e [file] [log] [blame]
Joel Fernandes (Google)f96e8572018-07-12 14:36:11 -07001// 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
Steven Rostedt (VMware)12ad0cb2018-10-15 23:31:42 -04008#include <linux/trace_clock.h>
Joel Fernandes (Google)f96e8572018-07-12 14:36:11 -07009#include <linux/delay.h>
10#include <linux/interrupt.h>
11#include <linux/irq.h>
12#include <linux/kernel.h>
Viktor Rosendahl (BMW)79393722019-10-09 00:08:22 +020013#include <linux/kobject.h>
Joel Fernandes (Google)f96e8572018-07-12 14:36:11 -070014#include <linux/kthread.h>
Joel Fernandes (Google)f96e8572018-07-12 14:36:11 -070015#include <linux/module.h>
16#include <linux/printk.h>
17#include <linux/string.h>
Viktor Rosendahl (BMW)79393722019-10-09 00:08:22 +020018#include <linux/sysfs.h>
Joel Fernandes (Google)f96e8572018-07-12 14:36:11 -070019
20static ulong delay = 100;
Viktor Rosendahl (BMW)79393722019-10-09 00:08:22 +020021static char test_mode[12] = "irq";
22static uint burst_size = 1;
Joel Fernandes (Google)f96e8572018-07-12 14:36:11 -070023
Viktor Rosendahl (BMW)79393722019-10-09 00:08:22 +020024module_param_named(delay, delay, ulong, 0444);
25module_param_string(test_mode, test_mode, 12, 0444);
26module_param_named(burst_size, burst_size, uint, 0444);
27MODULE_PARM_DESC(delay, "Period in microseconds (100 us default)");
28MODULE_PARM_DESC(test_mode, "Mode of the test such as preempt, irq, or alternate (default irq)");
29MODULE_PARM_DESC(burst_size, "The size of a burst (default 1)");
30
31#define MIN(x, y) ((x) < (y) ? (x) : (y))
Joel Fernandes (Google)f96e8572018-07-12 14:36:11 -070032
33static void busy_wait(ulong time)
34{
Steven Rostedt (VMware)12ad0cb2018-10-15 23:31:42 -040035 u64 start, end;
36 start = trace_clock_local();
Joel Fernandes (Google)f96e8572018-07-12 14:36:11 -070037 do {
Steven Rostedt (VMware)12ad0cb2018-10-15 23:31:42 -040038 end = trace_clock_local();
Joel Fernandes (Google)f96e8572018-07-12 14:36:11 -070039 if (kthread_should_stop())
40 break;
Steven Rostedt (VMware)12ad0cb2018-10-15 23:31:42 -040041 } while ((end - start) < (time * 1000));
Joel Fernandes (Google)f96e8572018-07-12 14:36:11 -070042}
43
Viktor Rosendahl (BMW)79393722019-10-09 00:08:22 +020044static __always_inline void irqoff_test(void)
Joel Fernandes (Google)f96e8572018-07-12 14:36:11 -070045{
46 unsigned long flags;
Viktor Rosendahl (BMW)79393722019-10-09 00:08:22 +020047 local_irq_save(flags);
48 busy_wait(delay);
49 local_irq_restore(flags);
50}
Joel Fernandes (Google)f96e8572018-07-12 14:36:11 -070051
Viktor Rosendahl (BMW)79393722019-10-09 00:08:22 +020052static __always_inline void preemptoff_test(void)
53{
54 preempt_disable();
55 busy_wait(delay);
56 preempt_enable();
57}
58
59static 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();
Joel Fernandes (Google)f96e8572018-07-12 14:36:11 -070070 }
Viktor Rosendahl (BMW)79393722019-10-09 00:08:22 +020071}
Joel Fernandes (Google)f96e8572018-07-12 14:36:11 -070072
Viktor Rosendahl (BMW)79393722019-10-09 00:08:22 +020073#define DECLARE_TESTFN(POSTFIX) \
74 static void preemptirqtest_##POSTFIX(int idx) \
75 { \
76 execute_preemptirqtest(idx); \
77 } \
78
79/*
80 * We create 10 different functions, so that we can get 10 different
81 * backtraces.
82 */
83DECLARE_TESTFN(0)
84DECLARE_TESTFN(1)
85DECLARE_TESTFN(2)
86DECLARE_TESTFN(3)
87DECLARE_TESTFN(4)
88DECLARE_TESTFN(5)
89DECLARE_TESTFN(6)
90DECLARE_TESTFN(7)
91DECLARE_TESTFN(8)
92DECLARE_TESTFN(9)
93
94static 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
109static 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);
Joel Fernandes (Google)f96e8572018-07-12 14:36:11 -0700116 return 0;
117}
118
Viktor Rosendahl (BMW)79393722019-10-09 00:08:22 +0200119static struct task_struct *preemptirq_start_test(void)
Joel Fernandes (Google)f96e8572018-07-12 14:36:11 -0700120{
121 char task_name[50];
Joel Fernandes (Google)f96e8572018-07-12 14:36:11 -0700122
123 snprintf(task_name, sizeof(task_name), "%s_test", test_mode);
Viktor Rosendahl (BMW)79393722019-10-09 00:08:22 +0200124 return kthread_run(preemptirq_delay_run, NULL, task_name);
125}
Joel Fernandes (Google)f96e8572018-07-12 14:36:11 -0700126
Viktor Rosendahl (BMW)79393722019-10-09 00:08:22 +0200127
128static ssize_t trigger_store(struct kobject *kobj, struct kobj_attribute *attr,
129 const char *buf, size_t count)
130{
131 preemptirq_start_test();
132 return count;
133}
134
135static struct kobj_attribute trigger_attribute =
136 __ATTR(trigger, 0200, NULL, trigger_store);
137
138static struct attribute *attrs[] = {
139 &trigger_attribute.attr,
140 NULL,
141};
142
143static struct attribute_group attr_group = {
144 .attrs = attrs,
145};
146
147static struct kobject *preemptirq_delay_kobj;
148
149static int __init preemptirq_delay_init(void)
150{
151 struct task_struct *test_task;
152 int retval;
153
154 test_task = preemptirq_start_test();
155 retval = PTR_ERR_OR_ZERO(test_task);
156 if (retval != 0)
157 return retval;
158
159 preemptirq_delay_kobj = kobject_create_and_add("preemptirq_delay_test",
160 kernel_kobj);
161 if (!preemptirq_delay_kobj)
162 return -ENOMEM;
163
164 retval = sysfs_create_group(preemptirq_delay_kobj, &attr_group);
165 if (retval)
166 kobject_put(preemptirq_delay_kobj);
167
168 return retval;
Joel Fernandes (Google)f96e8572018-07-12 14:36:11 -0700169}
170
171static void __exit preemptirq_delay_exit(void)
172{
Viktor Rosendahl (BMW)79393722019-10-09 00:08:22 +0200173 kobject_put(preemptirq_delay_kobj);
Joel Fernandes (Google)f96e8572018-07-12 14:36:11 -0700174}
175
176module_init(preemptirq_delay_init)
177module_exit(preemptirq_delay_exit)
178MODULE_LICENSE("GPL v2");