Skip to content

Commit 7939372

Browse files
cunctatorrostedt
authored andcommitted
preemptirq_delay_test: Add the burst feature and a sysfs trigger
This burst feature enables the user to generate a burst of preempt/irqsoff latencies. This makes it possible to test whether we are able to detect latencies that systematically occur very close to each other. The maximum burst size is 10. We also create 10 identical test functions, so that we get 10 different backtraces; this is useful when we want to test whether we can detect all the latencies in a burst. Otherwise, there would be no easy way of differentiating between which latency in a burst was captured by the tracer. In addition, there is a sysfs trigger, so that it's not necessary to reload the module to repeat the test. The trigger will appear as /sys/kernel/preemptirq_delay_test/trigger in sysfs. Link: http://lkml.kernel.org/r/20191008220824.7911-3-viktor.rosendahl@gmail.com Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org> Signed-off-by: Viktor Rosendahl (BMW) <viktor.rosendahl@gmail.com> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
1 parent 91edde2 commit 7939372

2 files changed

Lines changed: 128 additions & 22 deletions

File tree

‎kernel/trace/Kconfig‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -760,9 +760,9 @@ config PREEMPTIRQ_DELAY_TEST
760760
configurable delay. The module busy waits for the duration of the
761761
critical section.
762762

763-
For example, the following invocation forces a one-time irq-disabled
764-
critical section for 500us:
765-
modprobe preemptirq_delay_test test_mode=irq delay=500000
763+
For example, the following invocation generates a burst of three
764+
irq-disabled critical sections for 500us:
765+
modprobe preemptirq_delay_test test_mode=irq delay=500 burst_size=3
766766

767767
If unsure, say N
768768

‎kernel/trace/preemptirq_delay_test.c‎

Lines changed: 125 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,25 @@
1010
#include <linux/interrupt.h>
1111
#include <linux/irq.h>
1212
#include <linux/kernel.h>
13+
#include <linux/kobject.h>
1314
#include <linux/kthread.h>
1415
#include <linux/module.h>
1516
#include <linux/printk.h>
1617
#include <linux/string.h>
18+
#include <linux/sysfs.h>
1719

1820
static ulong delay = 100;
19-
static char test_mode[10] = "irq";
21+
static char test_mode[12] = "irq";
22+
static uint burst_size = 1;
2023

21-
module_param_named(delay, delay, ulong, S_IRUGO);
22-
module_param_string(test_mode, test_mode, 10, S_IRUGO);
23-
MODULE_PARM_DESC(delay, "Period in microseconds (100 uS default)");
24-
MODULE_PARM_DESC(test_mode, "Mode of the test such as preempt or irq (default irq)");
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)");
30+
31+
#define MIN(x, y) ((x) < (y) ? (x) : (y))
2532

2633
static void busy_wait(ulong time)
2734
{
@@ -34,37 +41,136 @@ static void busy_wait(ulong time)
3441
} while ((end - start) < (time * 1000));
3542
}
3643

37-
static int preemptirq_delay_run(void *data)
44+
static __always_inline void irqoff_test(void)
3845
{
3946
unsigned long flags;
47+
local_irq_save(flags);
48+
busy_wait(delay);
49+
local_irq_restore(flags);
50+
}
4051

41-
if (!strcmp(test_mode, "irq")) {
42-
local_irq_save(flags);
43-
busy_wait(delay);
44-
local_irq_restore(flags);
45-
} else if (!strcmp(test_mode, "preempt")) {
46-
preempt_disable();
47-
busy_wait(delay);
48-
preempt_enable();
52+
static __always_inline void preemptoff_test(void)
53+
{
54+
preempt_disable();
55+
busy_wait(delay);
56+
preempt_enable();
57+
}
58+
59+
static 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();
4970
}
71+
}
72+
73+
#define DECLARE_TESTFN(POSTFIX) \
74+
static void preemptirqtest_##POSTFIX(int idx) \
75+
{ \
76+
execute_preemptirqtest(idx); \
77+
} \
5078

79+
/*
80+
* We create 10 different functions, so that we can get 10 different
81+
* backtraces.
82+
*/
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)
93+
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,
105+
};
106+
107+
#define NR_TEST_FUNCS ARRAY_SIZE(testfuncs)
108+
109+
static 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);
51116
return 0;
52117
}
53118

54-
static int __init preemptirq_delay_init(void)
119+
static struct task_struct *preemptirq_start_test(void)
55120
{
56121
char task_name[50];
57-
struct task_struct *test_task;
58122

59123
snprintf(task_name, sizeof(task_name), "%s_test", test_mode);
124+
return kthread_run(preemptirq_delay_run, NULL, task_name);
125+
}
126+
127+
128+
static 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+
135+
static struct kobj_attribute trigger_attribute =
136+
__ATTR(trigger, 0200, NULL, trigger_store);
137+
138+
static struct attribute *attrs[] = {
139+
&trigger_attribute.attr,
140+
NULL,
141+
};
142+
143+
static struct attribute_group attr_group = {
144+
.attrs = attrs,
145+
};
146+
147+
static struct kobject *preemptirq_delay_kobj;
148+
149+
static 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);
60167

61-
test_task = kthread_run(preemptirq_delay_run, NULL, task_name);
62-
return PTR_ERR_OR_ZERO(test_task);
168+
return retval;
63169
}
64170

65171
static void __exit preemptirq_delay_exit(void)
66172
{
67-
return;
173+
kobject_put(preemptirq_delay_kobj);
68174
}
69175

70176
module_init(preemptirq_delay_init)

0 commit comments

Comments
 (0)