lib/tests/test_ratelimit.c
Source file repositories/reference/linux-study-clean/lib/tests/test_ratelimit.c
File Facts
- System
- Linux kernel
- Corpus path
lib/tests/test_ratelimit.c- Extension
.c- Size
- 3955 bytes
- Lines
- 145
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: implementation source
- Status
- source implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
kunit/test.hlinux/ratelimit.hlinux/module.hlinux/kthread.hlinux/cpumask.h
Detected Declarations
struct stress_kthreadfunction test_ratelimit_smokefunction test_ratelimit_stress_childfunction test_ratelimit_stress
Annotated Snippet
struct stress_kthread {
unsigned long nattempts;
unsigned long nunlimited;
unsigned long nlimited;
unsigned long nmissed;
struct task_struct *tp;
};
static int test_ratelimit_stress_child(void *arg)
{
struct stress_kthread *sktp = arg;
set_user_nice(current, MAX_NICE);
WARN_ON_ONCE(!sktp->tp);
while (!READ_ONCE(doneflag)) {
sktp->nattempts++;
if (___ratelimit(&stressrl, __func__))
sktp->nunlimited++;
else
sktp->nlimited++;
cond_resched();
}
sktp->nmissed = ratelimit_state_reset_miss(&stressrl);
return 0;
}
static void test_ratelimit_stress(struct kunit *test)
{
int i;
const int n_stress_kthread = cpumask_weight(cpu_online_mask);
struct stress_kthread skt = { 0 };
struct stress_kthread *sktp = kzalloc_objs(*sktp, n_stress_kthread);
KUNIT_EXPECT_NOT_NULL_MSG(test, sktp, "Memory allocation failure");
for (i = 0; i < n_stress_kthread; i++) {
sktp[i].tp = kthread_run(test_ratelimit_stress_child, &sktp[i], "%s/%i",
"test_ratelimit_stress_child", i);
KUNIT_EXPECT_NOT_NULL_MSG(test, sktp, "kthread creation failure");
pr_alert("Spawned test_ratelimit_stress_child %d\n", i);
}
schedule_timeout_idle(stress_duration);
WRITE_ONCE(doneflag, 1);
for (i = 0; i < n_stress_kthread; i++) {
kthread_stop(sktp[i].tp);
skt.nattempts += sktp[i].nattempts;
skt.nunlimited += sktp[i].nunlimited;
skt.nlimited += sktp[i].nlimited;
skt.nmissed += sktp[i].nmissed;
}
KUNIT_ASSERT_EQ_MSG(test, skt.nunlimited + skt.nlimited, skt.nattempts,
"Outcomes not equal to attempts");
KUNIT_ASSERT_EQ_MSG(test, skt.nlimited, skt.nmissed, "Misses not equal to limits");
}
static struct kunit_case ratelimit_test_cases[] = {
KUNIT_CASE_SLOW(test_ratelimit_smoke),
KUNIT_CASE_SLOW(test_ratelimit_stress),
{}
};
static struct kunit_suite ratelimit_test_suite = {
.name = "lib_ratelimit",
.test_cases = ratelimit_test_cases,
};
kunit_test_suites(&ratelimit_test_suite);
MODULE_DESCRIPTION("___ratelimit() KUnit test suite");
MODULE_LICENSE("GPL");
Annotation
- Immediate include surface: `kunit/test.h`, `linux/ratelimit.h`, `linux/module.h`, `linux/kthread.h`, `linux/cpumask.h`.
- Detected declarations: `struct stress_kthread`, `function test_ratelimit_smoke`, `function test_ratelimit_stress_child`, `function test_ratelimit_stress`.
- Atlas domain: Kernel Services / lib.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.