Skip to content

Commit daf675b

Browse files
committed
tests: use PRNG to generate data for tests
This replaces the multiples of a prime sequence used for the per-CPU helper tests with a slightly more self-documenting PRNG interface in both the test kernel module and Python test scaffolding. It will be used more in upcoming tests. Signed-off-by: Omar Sandoval <[email protected]>
1 parent 38b090e commit daf675b

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

tests/linux_kernel/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ def setUpClass(cls):
104104
)
105105

106106

107+
# PRNG used by the test kernel module.
108+
def prng32(seed):
109+
seed = seed.encode("ascii")
110+
assert len(seed) == 4
111+
x = int.from_bytes(seed, "big")
112+
while True:
113+
x ^= (x << 13) & 0xFFFFFFFF
114+
x ^= x >> 17
115+
x ^= (x << 5) & 0xFFFFFFFF
116+
yield x
117+
118+
107119
def wait_until(fn, *args, **kwds):
108120
TIMEOUT = 5
109121
deadline = time.monotonic() + TIMEOUT

tests/linux_kernel/helpers/test_percpu.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from drgn.helpers.linux.percpu import per_cpu, per_cpu_ptr
66
from tests.linux_kernel import (
77
LinuxKernelTestCase,
8+
prng32,
89
skip_unless_have_test_kmod,
910
smp_enabled,
1011
)
@@ -25,18 +26,14 @@ def test_per_cpu(self):
2526

2627
@skip_unless_have_test_kmod
2728
def test_per_cpu_module_static(self):
28-
expected = prime = self.prog["drgn_test_percpu_static_prime"]
29-
for cpu in for_each_possible_cpu(self.prog):
30-
expected *= prime
29+
for cpu, expected in zip(for_each_possible_cpu(self.prog), prng32("PCPU")):
3130
self.assertEqual(
3231
per_cpu(self.prog["drgn_test_percpu_static"], cpu), expected
3332
)
3433

3534
@skip_unless_have_test_kmod
3635
def test_per_cpu_module_dynamic(self):
37-
expected = prime = self.prog["drgn_test_percpu_dynamic_prime"]
38-
for cpu in for_each_possible_cpu(self.prog):
39-
expected *= prime
36+
for cpu, expected in zip(for_each_possible_cpu(self.prog), prng32("pcpu")):
4037
self.assertEqual(
4138
per_cpu_ptr(self.prog["drgn_test_percpu_dynamic"], cpu)[0], expected
4239
)

tests/linux_kernel/kmod/drgn_test.c

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@
2929
#define HAVE_XARRAY 0
3030
#endif
3131

32+
// Convert a 4-character string to a seed for drgn_test_prng32().
33+
static inline u32 drgn_test_prng32_seed(const char *s)
34+
{
35+
BUG_ON(strlen(s) != 4);
36+
return ((u32)s[0] << 24) | ((u32)s[1] << 16) | ((u32)s[2] << 8) | (u32)s[3];
37+
}
38+
39+
// x must not be 0; the return value is never 0.
40+
static u32 drgn_test_prng32(u32 x)
41+
{
42+
// Xorshift RNG with a period of 2^32 - 1.
43+
x ^= x << 13;
44+
x ^= x >> 17;
45+
x ^= x << 5;
46+
return x;
47+
}
48+
3249
// list
3350

3451
LIST_HEAD(drgn_test_empty_list);
@@ -157,26 +174,23 @@ static void drgn_test_mm_exit(void)
157174
// percpu
158175

159176
DEFINE_PER_CPU(unsigned int, drgn_test_percpu_static);
160-
const unsigned int drgn_test_percpu_static_prime = 0xa45dcfc3U;
161177
unsigned int __percpu *drgn_test_percpu_dynamic;
162-
const unsigned int drgn_test_percpu_dynamic_prime = 0x6d80a613U;
163178

164179
static int drgn_test_percpu_init(void)
165180
{
166181
int cpu;
167-
unsigned int static_prime = drgn_test_percpu_static_prime;
168-
unsigned int dynamic_prime = drgn_test_percpu_dynamic_prime;
182+
u32 static_seed = drgn_test_prng32_seed("PCPU");
183+
u32 dynamic_seed = drgn_test_prng32_seed("pcpu");
169184

170185
drgn_test_percpu_dynamic = alloc_percpu(unsigned int);
171186
if (!drgn_test_percpu_dynamic)
172187
return -ENOMEM;
173-
// Initialize the per-cpu variables to powers of a random prime number
174-
// which are extremely unlikely to appear anywhere else.
188+
// Initialize the per-cpu variables with a PRNG sequence.
175189
for_each_possible_cpu(cpu) {
176-
static_prime *= drgn_test_percpu_static_prime;
177-
per_cpu(drgn_test_percpu_static, cpu) = static_prime;
178-
dynamic_prime *= drgn_test_percpu_dynamic_prime;
179-
*per_cpu_ptr(drgn_test_percpu_dynamic, cpu) = dynamic_prime;
190+
static_seed = drgn_test_prng32(static_seed);
191+
per_cpu(drgn_test_percpu_static, cpu) = static_seed;
192+
dynamic_seed = drgn_test_prng32(dynamic_seed);
193+
*per_cpu_ptr(drgn_test_percpu_dynamic, cpu) = dynamic_seed;
180194
}
181195
return 0;
182196
}

0 commit comments

Comments
 (0)