Linux cgroups development
 help / color / mirror / Atom feed
* [PATCH RESEND v4] selftests/cgroup: Adjust cpu test duration based on HZ
@ 2026-06-25 20:33 Joe Simmons-Talbott
  2026-06-26 16:44 ` Michal Koutný
  0 siblings, 1 reply; 3+ messages in thread
From: Joe Simmons-Talbott @ 2026-06-25 20:33 UTC (permalink / raw)
  To: Tejun Heo, Johannes Weiner, Michal Koutný, Shuah Khan
  Cc: Joe Simmons-Talbott, cui.tao, Li Wang, Shakeel Butt, Nhat Pham,
	Guopeng Zhang, Sebastian Chlad, open list:CONTROL GROUP (CGROUP),
	open list:KERNEL SELFTEST FRAMEWORK, open list

For lower HZ values a quota of 1000us is much lower than the amount
of microseconds per tick which makes the tests test_cpucg_max and
test_cpugc_max_nested fail. Increase the test duration to accommodate
for lower HZ values.

Link: https://lore.kernel.org/lkml/20260624160358.430354-1-joest@redhat.com/
Signed-off-by: Joe Simmons-Talbott <joest@redhat.com>
---
CC: cui.tao@linux.dev
v3 -> v4:
- Use usec for adjusting test duration for better accuracy.
- Remove underscore from static function
- Use 1000 as the fallback value for hz since it's the default.

v2 -> v3:
- Instead of changing cpu.max quota extend the test duration based on
  the HZ value.
- don't call pclose() if popen() fails.
- check return value of fscanf().

v1 -> v2:
- Try checking /proc/config.gz to get the actual kernel HZ value and
  fallback to 1000 if the value cannot be determined.

 .../cgroup/lib/include/cgroup_util.h          |  1 +
 tools/testing/selftests/cgroup/test_cpu.c     | 47 ++++++++++++++++---
 2 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/tools/testing/selftests/cgroup/lib/include/cgroup_util.h b/tools/testing/selftests/cgroup/lib/include/cgroup_util.h
index febc1723d090..8ebb2b4d4ec0 100644
--- a/tools/testing/selftests/cgroup/lib/include/cgroup_util.h
+++ b/tools/testing/selftests/cgroup/lib/include/cgroup_util.h
@@ -8,6 +8,7 @@
 
 #define MB(x) (x << 20)
 
+#define NSEC_PER_USEC	1000L
 #define USEC_PER_SEC	1000000L
 #define NSEC_PER_SEC	1000000000L
 
diff --git a/tools/testing/selftests/cgroup/test_cpu.c b/tools/testing/selftests/cgroup/test_cpu.c
index 7a40d76b9548..1f280c1db68a 100644
--- a/tools/testing/selftests/cgroup/test_cpu.c
+++ b/tools/testing/selftests/cgroup/test_cpu.c
@@ -639,6 +639,31 @@ test_cpucg_nested_weight_underprovisioned(const char *root)
 	return run_cpucg_nested_weight_test(root, false);
 }
 
+/*
+ * Best effort attempt to get the kernel's HZ value from the config.
+ * Return the HZ value if found otherwise return 1000 (the default) to
+ * indicate failure.
+ */
+static long
+get_config_hz(void)
+{
+	long hz = 1000;
+	FILE *f;
+	char cmd[256] = "zcat /proc/config.gz 2>/dev/null | grep '^CONFIG_HZ='";
+
+	f = popen(cmd, "r");
+
+	if (!f)
+		return hz;
+
+	if (fscanf(f, "CONFIG_HZ=%ld", &hz) == EOF)
+		goto out;
+
+out:
+	pclose(f);
+	return hz;
+}
+
 /*
  * This test creates a cgroup with some maximum value within a period, and
  * verifies that a process in the cgroup is not overscheduled.
@@ -646,15 +671,20 @@ test_cpucg_nested_weight_underprovisioned(const char *root)
 static int test_cpucg_max(const char *root)
 {
 	int ret = KSFT_FAIL;
+	long hz = get_config_hz();
 	long quota_usec = 1000;
 	long default_period_usec = 100000; /* cpu.max's default period */
 	long duration_seconds = 1;
 
-	long duration_usec = duration_seconds * USEC_PER_SEC;
+	long duration_usec, duration_sec, duration_nsec;
 	long usage_usec, n_periods, remainder_usec, expected_usage_usec;
 	char *cpucg;
 	char quota_buf[32];
 
+	duration_usec = duration_seconds * USEC_PER_SEC * 1000 / hz;
+	duration_sec = duration_usec / USEC_PER_SEC;
+	duration_nsec = duration_usec % USEC_PER_SEC * NSEC_PER_USEC;
+
 	snprintf(quota_buf, sizeof(quota_buf), "%ld", quota_usec);
 
 	cpucg = cg_name(root, "cpucg_test");
@@ -670,8 +700,8 @@ static int test_cpucg_max(const char *root)
 	struct cpu_hog_func_param param = {
 		.nprocs = 1,
 		.ts = {
-			.tv_sec = duration_seconds,
-			.tv_nsec = 0,
+			.tv_sec = duration_sec,
+			.tv_nsec = duration_nsec,
 		},
 		.clock_type = CPU_HOG_CLOCK_WALL,
 	};
@@ -710,15 +740,20 @@ static int test_cpucg_max(const char *root)
 static int test_cpucg_max_nested(const char *root)
 {
 	int ret = KSFT_FAIL;
+	long hz = get_config_hz();
 	long quota_usec = 1000;
 	long default_period_usec = 100000; /* cpu.max's default period */
 	long duration_seconds = 1;
 
-	long duration_usec = duration_seconds * USEC_PER_SEC;
+	long duration_usec, duration_sec, duration_nsec;
 	long usage_usec, n_periods, remainder_usec, expected_usage_usec;
 	char *parent, *child;
 	char quota_buf[32];
 
+	duration_usec = duration_seconds * USEC_PER_SEC * 1000 / hz;
+	duration_sec = duration_usec / USEC_PER_SEC;
+	duration_nsec = duration_usec % USEC_PER_SEC * NSEC_PER_USEC;
+
 	snprintf(quota_buf, sizeof(quota_buf), "%ld", quota_usec);
 
 	parent = cg_name(root, "cpucg_parent");
@@ -741,8 +776,8 @@ static int test_cpucg_max_nested(const char *root)
 	struct cpu_hog_func_param param = {
 		.nprocs = 1,
 		.ts = {
-			.tv_sec = duration_seconds,
-			.tv_nsec = 0,
+			.tv_sec = duration_sec,
+			.tv_nsec = duration_nsec,
 		},
 		.clock_type = CPU_HOG_CLOCK_WALL,
 	};
-- 
2.54.0


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

* Re: [PATCH RESEND v4] selftests/cgroup: Adjust cpu test duration based on HZ
  2026-06-25 20:33 [PATCH RESEND v4] selftests/cgroup: Adjust cpu test duration based on HZ Joe Simmons-Talbott
@ 2026-06-26 16:44 ` Michal Koutný
  2026-06-26 20:19   ` Joe Simmons-Talbott
  0 siblings, 1 reply; 3+ messages in thread
From: Michal Koutný @ 2026-06-26 16:44 UTC (permalink / raw)
  To: Joe Simmons-Talbott
  Cc: Tejun Heo, Johannes Weiner, Shuah Khan, cui.tao, Li Wang,
	Shakeel Butt, Nhat Pham, Guopeng Zhang, Sebastian Chlad,
	open list:CONTROL GROUP (CGROUP),
	open list:KERNEL SELFTEST FRAMEWORK, open list

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

On Thu, Jun 25, 2026 at 04:33:04PM -0400, Joe Simmons-Talbott <joest@redhat.com> wrote:
>  static int test_cpucg_max_nested(const char *root)
>  {
>  	int ret = KSFT_FAIL;
> +	long hz = get_config_hz();
>  	long quota_usec = 1000;
>  	long default_period_usec = 100000; /* cpu.max's default period */
>  	long duration_seconds = 1;
>  
> -	long duration_usec = duration_seconds * USEC_PER_SEC;
> +	long duration_usec, duration_sec, duration_nsec;
>  	long usage_usec, n_periods, remainder_usec, expected_usage_usec;
>  	char *parent, *child;
>  	char quota_buf[32];
>  
> +	duration_usec = duration_seconds * USEC_PER_SEC * 1000 / hz;
> +	duration_sec = duration_usec / USEC_PER_SEC;
> +	duration_nsec = duration_usec % USEC_PER_SEC * NSEC_PER_USEC;

Oh, that's duration in so many units and the seconds is there twice. (I
understand why you did that for the rescale but) could you pick more
descriptive/distinctive names then and ideally keep it simple :-p

Thanks,
Michal

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

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

* Re: [PATCH RESEND v4] selftests/cgroup: Adjust cpu test duration based on HZ
  2026-06-26 16:44 ` Michal Koutný
@ 2026-06-26 20:19   ` Joe Simmons-Talbott
  0 siblings, 0 replies; 3+ messages in thread
From: Joe Simmons-Talbott @ 2026-06-26 20:19 UTC (permalink / raw)
  To: Michal Koutný
  Cc: Joe Simmons-Talbott, Tejun Heo, Johannes Weiner, Shuah Khan,
	cui.tao, Li Wang, Shakeel Butt, Nhat Pham, Guopeng Zhang,
	Sebastian Chlad, open list:CONTROL GROUP (CGROUP),
	open list:KERNEL SELFTEST FRAMEWORK, open list

On Fri, Jun 26, 2026 at 06:44:46PM +0200, Michal Koutný wrote:
> On Thu, Jun 25, 2026 at 04:33:04PM -0400, Joe Simmons-Talbott <joest@redhat.com> wrote:
> >  static int test_cpucg_max_nested(const char *root)
> >  {
> >  	int ret = KSFT_FAIL;
> > +	long hz = get_config_hz();
> >  	long quota_usec = 1000;
> >  	long default_period_usec = 100000; /* cpu.max's default period */
> >  	long duration_seconds = 1;
> >  
> > -	long duration_usec = duration_seconds * USEC_PER_SEC;
> > +	long duration_usec, duration_sec, duration_nsec;
> >  	long usage_usec, n_periods, remainder_usec, expected_usage_usec;
> >  	char *parent, *child;
> >  	char quota_buf[32];
> >  
> > +	duration_usec = duration_seconds * USEC_PER_SEC * 1000 / hz;
> > +	duration_sec = duration_usec / USEC_PER_SEC;
> > +	duration_nsec = duration_usec % USEC_PER_SEC * NSEC_PER_USEC;
> 
> Oh, that's duration in so many units and the seconds is there twice. (I
> understand why you did that for the rescale but) could you pick more
> descriptive/distinctive names then and ideally keep it simple :-p

Thanks for the feedback.  I'll send a v5 with those intermediate
variables removed and do the calculatons where they are used to setup
the cpu hog's time values.

Thanks,
Joe


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

end of thread, other threads:[~2026-06-26 20:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-25 20:33 [PATCH RESEND v4] selftests/cgroup: Adjust cpu test duration based on HZ Joe Simmons-Talbott
2026-06-26 16:44 ` Michal Koutný
2026-06-26 20:19   ` Joe Simmons-Talbott

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