Skip to content

Commit 5c23ce0

Browse files
committed
lib: Add stress test for ratelimit
Add a simple stress test for lib/ratelimit.c To run on x86: ./tools/testing/kunit/kunit.py run --arch x86_64 --kconfig_add CONFIG_RATELIMIT_KUNIT_TEST=y --kconfig_add CONFIG_SMP=y --qemu_args "-smp 4" lib_ratelimit On a 16-CPU system, the "4" in "-smp 4" can be varied between 1 and 8. Larger numbers have higher probabilities of introducing delays that break the smoke test. In the extreme case, increasing the number to larger than the number of CPUs in the underlying system is an excellent way to get a test failure. Link: https://lore.kernel.org/all/fbe93a52-365e-47fe-93a4-44a44547d601@paulmck-laptop/ Link: https://lore.kernel.org/all/[email protected]/ Signed-off-by: Paul E. McKenney <[email protected]> Cc: Petr Mladek <[email protected]> Cc: Andrew Morton <[email protected]> Cc: Kuniyuki Iwashima <[email protected]> Cc: Mateusz Guzik <[email protected]> Cc: Steven Rostedt <[email protected]> Cc: John Ogness <[email protected]> Cc: Sergey Senozhatsky <[email protected]> Cc: Jon Pan-Doh <[email protected]> Cc: Bjorn Helgaas <[email protected]> Cc: Karolina Stolarek <[email protected]>
1 parent 5a5c5a3 commit 5c23ce0

File tree

1 file changed

+67
-2
lines changed

1 file changed

+67
-2
lines changed

lib/tests/test_ratelimit.c

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <linux/ratelimit.h>
66
#include <linux/module.h>
7+
#include <linux/kthread.h>
8+
#include <linux/cpumask.h>
79

810
/* a simple boot-time regression test */
911

@@ -63,14 +65,77 @@ static void test_ratelimit_smoke(struct kunit *test)
6365
test_ratelimited(test, false);
6466
}
6567

66-
static struct kunit_case sort_test_cases[] = {
68+
static struct ratelimit_state stressrl = RATELIMIT_STATE_INIT_FLAGS("stressrl", HZ / 10, 3,
69+
RATELIMIT_MSG_ON_RELEASE);
70+
71+
static int doneflag;
72+
static const int stress_duration = 2 * HZ;
73+
74+
struct stress_kthread {
75+
unsigned long nattempts;
76+
unsigned long nunlimited;
77+
unsigned long nlimited;
78+
unsigned long nmissed;
79+
struct task_struct *tp;
80+
};
81+
82+
static int test_ratelimit_stress_child(void *arg)
83+
{
84+
struct stress_kthread *sktp = arg;
85+
86+
set_user_nice(current, MAX_NICE);
87+
WARN_ON_ONCE(!sktp->tp);
88+
89+
while (!READ_ONCE(doneflag)) {
90+
sktp->nattempts++;
91+
if (___ratelimit(&stressrl, __func__))
92+
sktp->nunlimited++;
93+
else
94+
sktp->nlimited++;
95+
cond_resched();
96+
}
97+
98+
sktp->nmissed = ratelimit_state_reset_miss(&stressrl);
99+
return 0;
100+
}
101+
102+
static void test_ratelimit_stress(struct kunit *test)
103+
{
104+
int i;
105+
const int n_stress_kthread = cpumask_weight(cpu_online_mask);
106+
struct stress_kthread skt = { 0 };
107+
struct stress_kthread *sktp = kcalloc(n_stress_kthread, sizeof(*sktp), GFP_KERNEL);
108+
109+
KUNIT_EXPECT_NOT_NULL_MSG(test, sktp, "Memory allocation failure");
110+
for (i = 0; i < n_stress_kthread; i++) {
111+
sktp[i].tp = kthread_run(test_ratelimit_stress_child, &sktp[i], "%s/%i",
112+
"test_ratelimit_stress_child", i);
113+
KUNIT_EXPECT_NOT_NULL_MSG(test, sktp, "kthread creation failure");
114+
pr_alert("Spawned test_ratelimit_stress_child %d\n", i);
115+
}
116+
schedule_timeout_idle(stress_duration);
117+
WRITE_ONCE(doneflag, 1);
118+
for (i = 0; i < n_stress_kthread; i++) {
119+
kthread_stop(sktp[i].tp);
120+
skt.nattempts += sktp[i].nattempts;
121+
skt.nunlimited += sktp[i].nunlimited;
122+
skt.nlimited += sktp[i].nlimited;
123+
skt.nmissed += sktp[i].nmissed;
124+
}
125+
KUNIT_ASSERT_EQ_MSG(test, skt.nunlimited + skt.nlimited, skt.nattempts,
126+
"Outcomes not equal to attempts");
127+
KUNIT_ASSERT_EQ_MSG(test, skt.nlimited, skt.nmissed, "Misses not equal to limits");
128+
}
129+
130+
static struct kunit_case ratelimit_test_cases[] = {
67131
KUNIT_CASE_SLOW(test_ratelimit_smoke),
132+
KUNIT_CASE_SLOW(test_ratelimit_stress),
68133
{}
69134
};
70135

71136
static struct kunit_suite ratelimit_test_suite = {
72137
.name = "lib_ratelimit",
73-
.test_cases = sort_test_cases,
138+
.test_cases = ratelimit_test_cases,
74139
};
75140

76141
kunit_test_suites(&ratelimit_test_suite);

0 commit comments

Comments
 (0)