File Index
Files and directories
Sign in
FILE

preemptirq_delay_test.c

File preview with syntax highlighting.

linux-6.19.14/kernel/trace/preemptirq_delay_test.c C
  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	cpumask_var_t cpu_mask;
121
122	if (!alloc_cpumask_var(&cpu_mask, GFP_KERNEL))
123		return -ENOMEM;
124
125	if (cpu_affinity > -1) {
126		cpumask_clear(cpu_mask);
127		cpumask_set_cpu(cpu_affinity, cpu_mask);
128		if (set_cpus_allowed_ptr(current, cpu_mask))
129			pr_err("cpu_affinity:%d, failed\n", cpu_affinity);
130	}
131
132	for (i = 0; i < s; i++)
133		(testfuncs[i])(i);
134
135	complete(&done);
136
137	set_current_state(TASK_INTERRUPTIBLE);
138	while (!kthread_should_stop()) {
139		schedule();
140		set_current_state(TASK_INTERRUPTIBLE);
141	}
142
143	__set_current_state(TASK_RUNNING);
144
145	free_cpumask_var(cpu_mask);
146
147	return 0;
148}
149
150static int preemptirq_run_test(void)
151{
152	struct task_struct *task;
153	char task_name[50];
154
155	init_completion(&done);
156
157	snprintf(task_name, sizeof(task_name), "%s_test", test_mode);
158	task =  kthread_run(preemptirq_delay_run, NULL, task_name);
159	if (IS_ERR(task))
160		return PTR_ERR(task);
161	if (task) {
162		wait_for_completion(&done);
163		kthread_stop(task);
164	}
165	return 0;
166}
167
168
169static ssize_t trigger_store(struct kobject *kobj, struct kobj_attribute *attr,
170			 const char *buf, size_t count)
171{
172	ssize_t ret;
173
174	ret = preemptirq_run_test();
175	if (ret)
176		return ret;
177	return count;
178}
179
180static struct kobj_attribute trigger_attribute =
181	__ATTR(trigger, 0200, NULL, trigger_store);
182
183static struct attribute *attrs[] = {
184	&trigger_attribute.attr,
185	NULL,
186};
187
188static struct attribute_group attr_group = {
189	.attrs = attrs,
190};
191
192static struct kobject *preemptirq_delay_kobj;
193
194static int __init preemptirq_delay_init(void)
195{
196	int retval;
197
198	retval = preemptirq_run_test();
199	if (retval != 0)
200		return retval;
201
202	preemptirq_delay_kobj = kobject_create_and_add("preemptirq_delay_test",
203						       kernel_kobj);
204	if (!preemptirq_delay_kobj)
205		return -ENOMEM;
206
207	retval = sysfs_create_group(preemptirq_delay_kobj, &attr_group);
208	if (retval)
209		kobject_put(preemptirq_delay_kobj);
210
211	return retval;
212}
213
214static void __exit preemptirq_delay_exit(void)
215{
216	kobject_put(preemptirq_delay_kobj);
217}
218
219module_init(preemptirq_delay_init)
220module_exit(preemptirq_delay_exit)
221MODULE_DESCRIPTION("Preempt / IRQ disable delay thread to test latency tracers");
222MODULE_LICENSE("GPL v2");
~/ Read-only file preview.