The cpu.max interface accepts "quota period" as two values, but
test_cpucg_max and test_cpucg_max_nested only wrote the quota,
relying on the kernel default period of 100ms. Since the test
duration is exactly divisible by the default period (1s / 100ms
= 10, no remainder), the remainder calculation was effectively
dead code.
Write both values explicitly so that the period can be easily
adjusted for testing purposes and the remainder logic becomes
genuinely useful.
While at it, rename misleading variable names:
- default_period_usec -> period_usec (no longer relying on a
kernel default, as it is now explicitly written to cpu.max)
- quota_buf -> max_param_buf (since the buffer holds both quota
and period)
Signed-off-by: Shaojie Sun <[email protected]>
---
tools/testing/selftests/cgroup/test_cpu.c | 26 ++++++++++++-----------
1 file changed, 14 insertions(+), 12 deletions(-)
diff --git a/tools/testing/selftests/cgroup/test_cpu.c
b/tools/testing/selftests/cgroup/test_cpu.c
index fe637e1d2fcd..10f9254cd08e 100644
--- a/tools/testing/selftests/cgroup/test_cpu.c
+++ b/tools/testing/selftests/cgroup/test_cpu.c
@@ -646,15 +646,16 @@ static int test_cpucg_max(const char *root)
{
int ret = KSFT_FAIL;
long quota_usec = 1000;
- long default_period_usec = 100000; /* cpu.max's default period */
+ long period_usec = 100000;
long duration_seconds = 1;
long duration_usec = duration_seconds * USEC_PER_SEC;
long usage_usec, n_periods, remainder_usec, expected_usage_usec;
char *cpucg;
- char quota_buf[32];
+ char max_param_buf[32];
- snprintf(quota_buf, sizeof(quota_buf), "%ld", quota_usec);
+ snprintf(max_param_buf, sizeof(max_param_buf), "%ld %ld", quota_usec,
+ period_usec);
cpucg = cg_name(root, "cpucg_test");
if (!cpucg)
@@ -663,7 +664,7 @@ static int test_cpucg_max(const char *root)
if (cg_create(cpucg))
goto cleanup;
- if (cg_write(cpucg, "cpu.max", quota_buf))
+ if (cg_write(cpucg, "cpu.max", max_param_buf))
goto cleanup;
struct cpu_hog_func_param param = {
@@ -685,8 +686,8 @@ static int test_cpucg_max(const char *root)
* The following calculation applies only since
* the cpu hog is set to run as per wall-clock time
*/
- n_periods = duration_usec / default_period_usec;
- remainder_usec = duration_usec - n_periods * default_period_usec;
+ n_periods = duration_usec / period_usec;
+ remainder_usec = duration_usec - n_periods * period_usec;
expected_usage_usec
= n_periods * quota_usec + MIN(remainder_usec, quota_usec);
@@ -710,15 +711,16 @@ static int test_cpucg_max_nested(const char *root)
{
int ret = KSFT_FAIL;
long quota_usec = 1000;
- long default_period_usec = 100000; /* cpu.max's default period */
+ long period_usec = 100000;
long duration_seconds = 1;
long duration_usec = duration_seconds * USEC_PER_SEC;
long usage_usec, n_periods, remainder_usec, expected_usage_usec;
char *parent, *child;
- char quota_buf[32];
+ char max_param_buf[32];
- snprintf(quota_buf, sizeof(quota_buf), "%ld", quota_usec);
+ snprintf(max_param_buf, sizeof(max_param_buf), "%ld %ld", quota_usec,
+ period_usec);
parent = cg_name(root, "cpucg_parent");
child = cg_name(parent, "cpucg_child");
@@ -734,7 +736,7 @@ static int test_cpucg_max_nested(const char *root)
if (cg_create(child))
goto cleanup;
- if (cg_write(parent, "cpu.max", quota_buf))
+ if (cg_write(parent, "cpu.max", max_param_buf))
goto cleanup;
struct cpu_hog_func_param param = {
@@ -756,8 +758,8 @@ static int test_cpucg_max_nested(const char *root)
* The following calculation applies only since
* the cpu hog is set to run as per wall-clock time
*/
- n_periods = duration_usec / default_period_usec;
- remainder_usec = duration_usec - n_periods * default_period_usec;
+ n_periods = duration_usec / period_usec;
+ remainder_usec = duration_usec - n_periods * period_usec;
expected_usage_usec
= n_periods * quota_usec + MIN(remainder_usec, quota_usec);
--
2.25.1