accel/amdxdna: use modern PM helpers
[drm/drm-misc.git] / kernel / trace / preemptirq_delay_test.c
blob314ffc143039c53da1c0612448901a8104cdd3d5
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>
19 #include <linux/completion.h>
21 static ulong delay = 100;
22 static char test_mode[12] = "irq";
23 static uint burst_size = 1;
24 static int cpu_affinity = -1;
26 module_param_named(delay, delay, ulong, 0444);
27 module_param_string(test_mode, test_mode, 12, 0444);
28 module_param_named(burst_size, burst_size, uint, 0444);
29 module_param_named(cpu_affinity, cpu_affinity, int, 0444);
30 MODULE_PARM_DESC(delay, "Period in microseconds (100 us default)");
31 MODULE_PARM_DESC(test_mode, "Mode of the test such as preempt, irq, or alternate (default irq)");
32 MODULE_PARM_DESC(burst_size, "The size of a burst (default 1)");
33 MODULE_PARM_DESC(cpu_affinity, "Cpu num test is running on");
35 static struct completion done;
37 static void busy_wait(ulong time)
39 u64 start, end;
41 start = trace_clock_local();
43 do {
44 end = trace_clock_local();
45 if (kthread_should_stop())
46 break;
47 } while ((end - start) < (time * 1000));
50 static __always_inline void irqoff_test(void)
52 unsigned long flags;
54 local_irq_save(flags);
55 busy_wait(delay);
56 local_irq_restore(flags);
59 static __always_inline void preemptoff_test(void)
61 preempt_disable();
62 busy_wait(delay);
63 preempt_enable();
66 static void execute_preemptirqtest(int idx)
68 if (!strcmp(test_mode, "irq"))
69 irqoff_test();
70 else if (!strcmp(test_mode, "preempt"))
71 preemptoff_test();
72 else if (!strcmp(test_mode, "alternate")) {
73 if (idx % 2 == 0)
74 irqoff_test();
75 else
76 preemptoff_test();
80 #define DECLARE_TESTFN(POSTFIX) \
81 static void preemptirqtest_##POSTFIX(int idx) \
82 { \
83 execute_preemptirqtest(idx); \
84 } \
87 * We create 10 different functions, so that we can get 10 different
88 * backtraces.
90 DECLARE_TESTFN(0)
91 DECLARE_TESTFN(1)
92 DECLARE_TESTFN(2)
93 DECLARE_TESTFN(3)
94 DECLARE_TESTFN(4)
95 DECLARE_TESTFN(5)
96 DECLARE_TESTFN(6)
97 DECLARE_TESTFN(7)
98 DECLARE_TESTFN(8)
99 DECLARE_TESTFN(9)
101 static void (*testfuncs[])(int) = {
102 preemptirqtest_0,
103 preemptirqtest_1,
104 preemptirqtest_2,
105 preemptirqtest_3,
106 preemptirqtest_4,
107 preemptirqtest_5,
108 preemptirqtest_6,
109 preemptirqtest_7,
110 preemptirqtest_8,
111 preemptirqtest_9,
114 #define NR_TEST_FUNCS ARRAY_SIZE(testfuncs)
116 static int preemptirq_delay_run(void *data)
118 int i;
119 int s = MIN(burst_size, NR_TEST_FUNCS);
120 struct cpumask cpu_mask;
122 if (cpu_affinity > -1) {
123 cpumask_clear(&cpu_mask);
124 cpumask_set_cpu(cpu_affinity, &cpu_mask);
125 if (set_cpus_allowed_ptr(current, &cpu_mask))
126 pr_err("cpu_affinity:%d, failed\n", cpu_affinity);
129 for (i = 0; i < s; i++)
130 (testfuncs[i])(i);
132 complete(&done);
134 set_current_state(TASK_INTERRUPTIBLE);
135 while (!kthread_should_stop()) {
136 schedule();
137 set_current_state(TASK_INTERRUPTIBLE);
140 __set_current_state(TASK_RUNNING);
142 return 0;
145 static int preemptirq_run_test(void)
147 struct task_struct *task;
148 char task_name[50];
150 init_completion(&done);
152 snprintf(task_name, sizeof(task_name), "%s_test", test_mode);
153 task = kthread_run(preemptirq_delay_run, NULL, task_name);
154 if (IS_ERR(task))
155 return PTR_ERR(task);
156 if (task) {
157 wait_for_completion(&done);
158 kthread_stop(task);
160 return 0;
164 static ssize_t trigger_store(struct kobject *kobj, struct kobj_attribute *attr,
165 const char *buf, size_t count)
167 ssize_t ret;
169 ret = preemptirq_run_test();
170 if (ret)
171 return ret;
172 return count;
175 static struct kobj_attribute trigger_attribute =
176 __ATTR(trigger, 0200, NULL, trigger_store);
178 static struct attribute *attrs[] = {
179 &trigger_attribute.attr,
180 NULL,
183 static struct attribute_group attr_group = {
184 .attrs = attrs,
187 static struct kobject *preemptirq_delay_kobj;
189 static int __init preemptirq_delay_init(void)
191 int retval;
193 retval = preemptirq_run_test();
194 if (retval != 0)
195 return retval;
197 preemptirq_delay_kobj = kobject_create_and_add("preemptirq_delay_test",
198 kernel_kobj);
199 if (!preemptirq_delay_kobj)
200 return -ENOMEM;
202 retval = sysfs_create_group(preemptirq_delay_kobj, &attr_group);
203 if (retval)
204 kobject_put(preemptirq_delay_kobj);
206 return retval;
209 static void __exit preemptirq_delay_exit(void)
211 kobject_put(preemptirq_delay_kobj);
214 module_init(preemptirq_delay_init)
215 module_exit(preemptirq_delay_exit)
216 MODULE_DESCRIPTION("Preempt / IRQ disable delay thread to test latency tracers");
217 MODULE_LICENSE("GPL v2");