Linux Kernel Selftest development
 help / color / mirror / Atom feed
* [PATCH 0/2] selftests/cgroup: misc cleanups for test_cpu.c
@ 2026-07-29 10:04 Shaojie Sun
  2026-07-29 10:04 ` [PATCH 1/2] selftests/cgroup: remove unused user_usec read in test_cpucg_nice Shaojie Sun
  2026-07-29 10:04 ` [PATCH 2/2] selftests/cgroup: write explicit period to cpu.max in max tests Shaojie Sun
  0 siblings, 2 replies; 5+ messages in thread
From: Shaojie Sun @ 2026-07-29 10:04 UTC (permalink / raw)
  To: tj, hannes, mkoutny, shuah
  Cc: cgroups, linux-kselftest, linux-kernel, Shaojie Sun

This series contains two minor cleanups to the cgroup cpu
controller selftests:

  - Patch 1 removes a redundant user_usec read in test_cpucg_nice
    that is never used after being read.

  - Patch 2 makes test_cpucg_max and test_cpucg_max_nested write
    both quota and period explicitly to cpu.max instead of relying
    on the kernel default period, making the tests self-documenting
    and the remainder calculation genuinely useful. Misleading
    variable names are also cleaned up.

Shaojie Sun (2):
  selftests/cgroup: remove unused user_usec read in test_cpucg_nice
  selftests/cgroup: write explicit period to cpu.max in max tests

 tools/testing/selftests/cgroup/test_cpu.c | 27 ++++++++++++-----------
 1 file changed, 14 insertions(+), 13 deletions(-)

-- 
2.25.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/2] selftests/cgroup: remove unused user_usec read in test_cpucg_nice
  2026-07-29 10:04 [PATCH 0/2] selftests/cgroup: misc cleanups for test_cpu.c Shaojie Sun
@ 2026-07-29 10:04 ` Shaojie Sun
  2026-07-29 11:58   ` Michal Koutný
  2026-07-29 10:04 ` [PATCH 2/2] selftests/cgroup: write explicit period to cpu.max in max tests Shaojie Sun
  1 sibling, 1 reply; 5+ messages in thread
From: Shaojie Sun @ 2026-07-29 10:04 UTC (permalink / raw)
  To: tj, hannes, mkoutny, shuah
  Cc: cgroups, linux-kselftest, linux-kernel, Shaojie Sun

In test_cpucg_nice, after the child process exits, user_usec is read
from cpu.stat but never used. Only nice_usec is validated against the
expected value. Remove this redundant read.

Signed-off-by: Shaojie Sun <sunshaojie@kylinos.cn>
---
 tools/testing/selftests/cgroup/test_cpu.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/tools/testing/selftests/cgroup/test_cpu.c b/tools/testing/selftests/cgroup/test_cpu.c
index 7a40d76b9548..fe637e1d2fcd 100644
--- a/tools/testing/selftests/cgroup/test_cpu.c
+++ b/tools/testing/selftests/cgroup/test_cpu.c
@@ -289,7 +289,6 @@ static int test_cpucg_nice(const char *root)
 		if (!WIFEXITED(status))
 			goto cleanup;
 
-		user_usec = cg_read_key_long(cpucg, "cpu.stat", "user_usec");
 		nice_usec = cg_read_key_long(cpucg, "cpu.stat", "nice_usec");
 		if (!values_close_report(nice_usec, expected_nice_usec, 1))
 			goto cleanup;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/2] selftests/cgroup: write explicit period to cpu.max in max tests
  2026-07-29 10:04 [PATCH 0/2] selftests/cgroup: misc cleanups for test_cpu.c Shaojie Sun
  2026-07-29 10:04 ` [PATCH 1/2] selftests/cgroup: remove unused user_usec read in test_cpucg_nice Shaojie Sun
@ 2026-07-29 10:04 ` Shaojie Sun
  2026-07-29 11:53   ` Michal Koutný
  1 sibling, 1 reply; 5+ messages in thread
From: Shaojie Sun @ 2026-07-29 10:04 UTC (permalink / raw)
  To: tj, hannes, mkoutny, shuah
  Cc: cgroups, linux-kselftest, linux-kernel, Shaojie Sun

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 <sunshaojie@kylinos.cn>
---
 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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/2] selftests/cgroup: write explicit period to cpu.max in max tests
  2026-07-29 10:04 ` [PATCH 2/2] selftests/cgroup: write explicit period to cpu.max in max tests Shaojie Sun
@ 2026-07-29 11:53   ` Michal Koutný
  0 siblings, 0 replies; 5+ messages in thread
From: Michal Koutný @ 2026-07-29 11:53 UTC (permalink / raw)
  To: Shaojie Sun; +Cc: tj, hannes, shuah, cgroups, linux-kselftest, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 641 bytes --]

Hi.

Here I cannot appreciate the changes.

On Wed, Jul 29, 2026 at 06:04:51PM +0800, Shaojie Sun <sunshaojie@kylinos.cn> wrote:
> 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.

Yes. Is that a problem?

> Write both values explicitly so that the period can be easily
> adjusted for testing purposes and the remainder logic becomes
> genuinely useful.

I have yet to see a system where the default period is modified.
Therefore, I wouldn't open that box with non-default periods
unnecessarily.

Thanks,
Michal

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 265 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] selftests/cgroup: remove unused user_usec read in test_cpucg_nice
  2026-07-29 10:04 ` [PATCH 1/2] selftests/cgroup: remove unused user_usec read in test_cpucg_nice Shaojie Sun
@ 2026-07-29 11:58   ` Michal Koutný
  0 siblings, 0 replies; 5+ messages in thread
From: Michal Koutný @ 2026-07-29 11:58 UTC (permalink / raw)
  To: Shaojie Sun; +Cc: tj, hannes, shuah, cgroups, linux-kselftest, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 928 bytes --]

On Wed, Jul 29, 2026 at 06:04:50PM +0800, Shaojie Sun <sunshaojie@kylinos.cn> wrote:
> diff --git a/tools/testing/selftests/cgroup/test_cpu.c b/tools/testing/selftests/cgroup/test_cpu.c
> index 7a40d76b9548..fe637e1d2fcd 100644
> --- a/tools/testing/selftests/cgroup/test_cpu.c
> +++ b/tools/testing/selftests/cgroup/test_cpu.c
> @@ -289,7 +289,6 @@ static int test_cpucg_nice(const char *root)
>  		if (!WIFEXITED(status))
>  			goto cleanup;
>  
> -		user_usec = cg_read_key_long(cpucg, "cpu.stat", "user_usec");
>  		nice_usec = cg_read_key_long(cpucg, "cpu.stat", "nice_usec");

Good catch.

This test is anologous to test_cpucg_stats(), so the checks should be
analogous too, like:

 		user_usec = cg_read_key_long(cpucg, "cpu.stat", "user_usec");
 		nice_usec = cg_read_key_long(cpucg, "cpu.stat", "nice_usec");
 		if (user_usec <= 0)
 			goto cleanup;  

instead of the removal.

Thanks,
Michal

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 265 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-07-29 11:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-29 10:04 [PATCH 0/2] selftests/cgroup: misc cleanups for test_cpu.c Shaojie Sun
2026-07-29 10:04 ` [PATCH 1/2] selftests/cgroup: remove unused user_usec read in test_cpucg_nice Shaojie Sun
2026-07-29 11:58   ` Michal Koutný
2026-07-29 10:04 ` [PATCH 2/2] selftests/cgroup: write explicit period to cpu.max in max tests Shaojie Sun
2026-07-29 11:53   ` Michal Koutný

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox