Linux Audio

Check our new training course

Loading...
v6.13.7
  1// 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
  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>
 20
 21static ulong delay = 100;
 22static char test_mode[12] = "irq";
 23static uint burst_size = 1;
 24static int  cpu_affinity = -1;
 25
 26module_param_named(delay, delay, ulong, 0444);
 27module_param_string(test_mode, test_mode, 12, 0444);
 28module_param_named(burst_size, burst_size, uint, 0444);
 29module_param_named(cpu_affinity, cpu_affinity, int, 0444);
 30MODULE_PARM_DESC(delay, "Period in microseconds (100 us default)");
 31MODULE_PARM_DESC(test_mode, "Mode of the test such as preempt, irq, or alternate (default irq)");
 32MODULE_PARM_DESC(burst_size, "The size of a burst (default 1)");
 33MODULE_PARM_DESC(cpu_affinity, "Cpu num test is running on");
 34
 35static struct completion done;
 36
 
 
 37static void busy_wait(ulong time)
 38{
 39	u64 start, end;
 40
 41	start = trace_clock_local();
 42
 43	do {
 44		end = trace_clock_local();
 45		if (kthread_should_stop())
 46			break;
 47	} while ((end - start) < (time * 1000));
 48}
 49
 50static __always_inline void irqoff_test(void)
 51{
 52	unsigned long flags;
 53
 54	local_irq_save(flags);
 55	busy_wait(delay);
 56	local_irq_restore(flags);
 57}
 58
 59static __always_inline void preemptoff_test(void)
 60{
 61	preempt_disable();
 62	busy_wait(delay);
 63	preempt_enable();
 64}
 65
 66static void execute_preemptirqtest(int idx)
 67{
 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();
 77	}
 78}
 79
 80#define DECLARE_TESTFN(POSTFIX)				\
 81	static void preemptirqtest_##POSTFIX(int idx)	\
 82	{						\
 83		execute_preemptirqtest(idx);		\
 84	}						\
 85
 86/*
 87 * We create 10 different functions, so that we can get 10 different
 88 * backtraces.
 89 */
 90DECLARE_TESTFN(0)
 91DECLARE_TESTFN(1)
 92DECLARE_TESTFN(2)
 93DECLARE_TESTFN(3)
 94DECLARE_TESTFN(4)
 95DECLARE_TESTFN(5)
 96DECLARE_TESTFN(6)
 97DECLARE_TESTFN(7)
 98DECLARE_TESTFN(8)
 99DECLARE_TESTFN(9)
100
101static 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,
112};
113
114#define NR_TEST_FUNCS ARRAY_SIZE(testfuncs)
115
116static int preemptirq_delay_run(void *data)
117{
118	int i;
119	int s = MIN(burst_size, NR_TEST_FUNCS);
120	struct cpumask cpu_mask;
121
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);
127	}
128
129	for (i = 0; i < s; i++)
130		(testfuncs[i])(i);
131
132	complete(&done);
133
134	set_current_state(TASK_INTERRUPTIBLE);
135	while (!kthread_should_stop()) {
136		schedule();
137		set_current_state(TASK_INTERRUPTIBLE);
138	}
139
140	__set_current_state(TASK_RUNNING);
141
142	return 0;
143}
144
145static int preemptirq_run_test(void)
146{
147	struct task_struct *task;
148	char task_name[50];
149
150	init_completion(&done);
151
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);
159	}
160	return 0;
161}
162
163
164static ssize_t trigger_store(struct kobject *kobj, struct kobj_attribute *attr,
165			 const char *buf, size_t count)
166{
167	ssize_t ret;
168
169	ret = preemptirq_run_test();
170	if (ret)
171		return ret;
172	return count;
173}
174
175static struct kobj_attribute trigger_attribute =
176	__ATTR(trigger, 0200, NULL, trigger_store);
177
178static struct attribute *attrs[] = {
179	&trigger_attribute.attr,
180	NULL,
181};
182
183static struct attribute_group attr_group = {
184	.attrs = attrs,
185};
186
187static struct kobject *preemptirq_delay_kobj;
188
189static int __init preemptirq_delay_init(void)
190{
191	int retval;
192
193	retval = preemptirq_run_test();
194	if (retval != 0)
195		return retval;
196
197	preemptirq_delay_kobj = kobject_create_and_add("preemptirq_delay_test",
198						       kernel_kobj);
199	if (!preemptirq_delay_kobj)
200		return -ENOMEM;
201
202	retval = sysfs_create_group(preemptirq_delay_kobj, &attr_group);
203	if (retval)
204		kobject_put(preemptirq_delay_kobj);
205
206	return retval;
207}
208
209static void __exit preemptirq_delay_exit(void)
210{
211	kobject_put(preemptirq_delay_kobj);
212}
213
214module_init(preemptirq_delay_init)
215module_exit(preemptirq_delay_exit)
216MODULE_DESCRIPTION("Preempt / IRQ disable delay thread to test latency tracers");
217MODULE_LICENSE("GPL v2");
v5.9
  1// 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
  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>
 20
 21static ulong delay = 100;
 22static char test_mode[12] = "irq";
 23static uint burst_size = 1;
 
 24
 25module_param_named(delay, delay, ulong, 0444);
 26module_param_string(test_mode, test_mode, 12, 0444);
 27module_param_named(burst_size, burst_size, uint, 0444);
 
 28MODULE_PARM_DESC(delay, "Period in microseconds (100 us default)");
 29MODULE_PARM_DESC(test_mode, "Mode of the test such as preempt, irq, or alternate (default irq)");
 30MODULE_PARM_DESC(burst_size, "The size of a burst (default 1)");
 
 31
 32static struct completion done;
 33
 34#define MIN(x, y) ((x) < (y) ? (x) : (y))
 35
 36static void busy_wait(ulong time)
 37{
 38	u64 start, end;
 
 39	start = trace_clock_local();
 
 40	do {
 41		end = trace_clock_local();
 42		if (kthread_should_stop())
 43			break;
 44	} while ((end - start) < (time * 1000));
 45}
 46
 47static __always_inline void irqoff_test(void)
 48{
 49	unsigned long flags;
 
 50	local_irq_save(flags);
 51	busy_wait(delay);
 52	local_irq_restore(flags);
 53}
 54
 55static __always_inline void preemptoff_test(void)
 56{
 57	preempt_disable();
 58	busy_wait(delay);
 59	preempt_enable();
 60}
 61
 62static void execute_preemptirqtest(int idx)
 63{
 64	if (!strcmp(test_mode, "irq"))
 65		irqoff_test();
 66	else if (!strcmp(test_mode, "preempt"))
 67		preemptoff_test();
 68	else if (!strcmp(test_mode, "alternate")) {
 69		if (idx % 2 == 0)
 70			irqoff_test();
 71		else
 72			preemptoff_test();
 73	}
 74}
 75
 76#define DECLARE_TESTFN(POSTFIX)				\
 77	static void preemptirqtest_##POSTFIX(int idx)	\
 78	{						\
 79		execute_preemptirqtest(idx);		\
 80	}						\
 81
 82/*
 83 * We create 10 different functions, so that we can get 10 different
 84 * backtraces.
 85 */
 86DECLARE_TESTFN(0)
 87DECLARE_TESTFN(1)
 88DECLARE_TESTFN(2)
 89DECLARE_TESTFN(3)
 90DECLARE_TESTFN(4)
 91DECLARE_TESTFN(5)
 92DECLARE_TESTFN(6)
 93DECLARE_TESTFN(7)
 94DECLARE_TESTFN(8)
 95DECLARE_TESTFN(9)
 96
 97static void (*testfuncs[])(int)  = {
 98	preemptirqtest_0,
 99	preemptirqtest_1,
100	preemptirqtest_2,
101	preemptirqtest_3,
102	preemptirqtest_4,
103	preemptirqtest_5,
104	preemptirqtest_6,
105	preemptirqtest_7,
106	preemptirqtest_8,
107	preemptirqtest_9,
108};
109
110#define NR_TEST_FUNCS ARRAY_SIZE(testfuncs)
111
112static int preemptirq_delay_run(void *data)
113{
114	int i;
115	int s = MIN(burst_size, NR_TEST_FUNCS);
 
 
 
 
 
 
 
 
116
117	for (i = 0; i < s; i++)
118		(testfuncs[i])(i);
119
120	complete(&done);
121
122	set_current_state(TASK_INTERRUPTIBLE);
123	while (!kthread_should_stop()) {
124		schedule();
125		set_current_state(TASK_INTERRUPTIBLE);
126	}
127
128	__set_current_state(TASK_RUNNING);
129
130	return 0;
131}
132
133static int preemptirq_run_test(void)
134{
135	struct task_struct *task;
136	char task_name[50];
137
138	init_completion(&done);
139
140	snprintf(task_name, sizeof(task_name), "%s_test", test_mode);
141	task =  kthread_run(preemptirq_delay_run, NULL, task_name);
142	if (IS_ERR(task))
143		return PTR_ERR(task);
144	if (task) {
145		wait_for_completion(&done);
146		kthread_stop(task);
147	}
148	return 0;
149}
150
151
152static ssize_t trigger_store(struct kobject *kobj, struct kobj_attribute *attr,
153			 const char *buf, size_t count)
154{
155	ssize_t ret;
156
157	ret = preemptirq_run_test();
158	if (ret)
159		return ret;
160	return count;
161}
162
163static struct kobj_attribute trigger_attribute =
164	__ATTR(trigger, 0200, NULL, trigger_store);
165
166static struct attribute *attrs[] = {
167	&trigger_attribute.attr,
168	NULL,
169};
170
171static struct attribute_group attr_group = {
172	.attrs = attrs,
173};
174
175static struct kobject *preemptirq_delay_kobj;
176
177static int __init preemptirq_delay_init(void)
178{
179	int retval;
180
181	retval = preemptirq_run_test();
182	if (retval != 0)
183		return retval;
184
185	preemptirq_delay_kobj = kobject_create_and_add("preemptirq_delay_test",
186						       kernel_kobj);
187	if (!preemptirq_delay_kobj)
188		return -ENOMEM;
189
190	retval = sysfs_create_group(preemptirq_delay_kobj, &attr_group);
191	if (retval)
192		kobject_put(preemptirq_delay_kobj);
193
194	return retval;
195}
196
197static void __exit preemptirq_delay_exit(void)
198{
199	kobject_put(preemptirq_delay_kobj);
200}
201
202module_init(preemptirq_delay_init)
203module_exit(preemptirq_delay_exit)
 
204MODULE_LICENSE("GPL v2");