selftests/cgroup: Adjust cpu test duration based on HZ
PATCH v3 1/1 selftests/cgroup: Adjust cpu test duration based on HZ
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.
---
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.
tools/testing/selftests/cgroup/test_cpu.c | 44 ++++++++++++++++++++--- 1 file changed, 40 insertions(+), 4 deletions(-)
tools/testing/selftests/cgroup/test_cpu.c +40 -4
@@ -639,6 +639,30 @@ 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 -1 to indicate failure. +*/ +static long +_get_config_hz (void ) +{ +long hz =-1 ; +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 +670,21 @@ 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_seconds; -long duration_usec = duration_seconds *USEC_PER_SEC ; +long duration_usec;long usage_usec, n_periods, remainder_usec, expected_usage_usec;char *cpucg;char quota_buf[32 ]; +if (hz ==-1 ) + hz =1000 ; + duration_seconds =1000 / hz; + duration_usec = duration_seconds *USEC_PER_SEC ; +snprintf (quota_buf,sizeof (quota_buf),"%ld" , quota_usec); cpucg =cg_name (root,"cpucg_test" );
@@ -710,15 +740,21 @@ 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_seconds; -long duration_usec = duration_seconds *USEC_PER_SEC ; +long duration_usec;long usage_usec, n_periods, remainder_usec, expected_usage_usec;char *parent, *child;char quota_buf[32 ]; +if (hz ==-1 ) + hz =1000 ; + duration_seconds =1000 / hz; + duration_usec = duration_seconds *USEC_PER_SEC ; +snprintf (quota_buf,sizeof (quota_buf),"%ld" , quota_usec); parent =cg_name (root,"cpucg_parent" );
Signature · 1 line
2.54.0
Michal Koutný
maintainerHi.
tools/testing/selftests/cgroup/test_cpu.c test_cpucg_nested_weight_underprovisioned(const char *root) · line 647
Quoted 6 lines · Joe Simmons-Talbott
tools/testing/selftests/cgroup/test_cpu.c +6
-0+/* +* Best effort attempt to get the kernel's HZ value from the config. +* Return the HZ value if found otherwise return -1 to indicate failure. +*/ +static long +_get_config_hz (void )
drop underscore from the static function
tools/testing/selftests/cgroup/test_cpu.c test_cpucg_nested_weight_underprovisioned(const char *root) · line 649
Quoted 2 lines
tools/testing/selftests/cgroup/test_cpu.c +2
-0+{ +long hz =-1 ;
use the default 1000 here to simplify the callers
Quoted 29 lines
tools/testing/selftests/cgroup/test_cpu.c +18
-1+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 +670,21 @@ 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_seconds;
tools/testing/selftests/cgroup/test_cpu.c test_cpucg_nested_weight_underprovisioned(const char *root) · line 686
Quoted 10 lines
tools/testing/selftests/cgroup/test_cpu.c +5
-1-long duration_usec = duration_seconds *USEC_PER_SEC ; +long duration_usec;long usage_usec, n_periods, remainder_usec, expected_usage_usec;char *cpucg;char quota_buf[32 ]; +if (hz ==-1 ) + hz =1000 ; + duration_seconds =1000 / hz; + duration_usec = duration_seconds *USEC_PER_SEC ;
I'd do the calculation in usecs
duration_usec = duration_seconds * USEC_PER_SEC * 1000 / hz;
so that actual duration is more precise (for hz=300 which is the only that doesn't divide 1000)
All in all, make the adjustments for HZ with less code (since I expect this will need adjustments for SMPs in future).
Thanks,
Michal
Quoted 59 lines · Michal Koutný
Hi.
tools/testing/selftests/cgroup/test_cpu.c test_cpucg_nested_weight_underprovisioned(const char *root) · line 647
Quoted 6 lines · Joe Simmons-Talbott
tools/testing/selftests/cgroup/test_cpu.c +6
-0+/* +* Best effort attempt to get the kernel's HZ value from the config. +* Return the HZ value if found otherwise return -1 to indicate failure. +*/ +static long +_get_config_hz (void )drop underscore from the static function
tools/testing/selftests/cgroup/test_cpu.c test_cpucg_nested_weight_underprovisioned(const char *root) · line 649
Quoted 2 lines
tools/testing/selftests/cgroup/test_cpu.c +2
-0+{ +long hz =-1 ;use the default 1000 here to simplify the callers
Quoted 29 lines
tools/testing/selftests/cgroup/test_cpu.c +18
-1+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 +670,21 @@ 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_seconds;tools/testing/selftests/cgroup/test_cpu.c test_cpucg_nested_weight_underprovisioned(const char *root) · line 686
Quoted 10 lines
tools/testing/selftests/cgroup/test_cpu.c +5
-1-long duration_usec = duration_seconds *USEC_PER_SEC ; +long duration_usec;long usage_usec, n_periods, remainder_usec, expected_usage_usec;char *cpucg;char quota_buf[32 ]; +if (hz ==-1 ) + hz =1000 ; + duration_seconds =1000 / hz; + duration_usec = duration_seconds *USEC_PER_SEC ;I'd do the calculation in usecs
Quoted 7 lines
duration_usec = duration_seconds * USEC_PER_SEC * 1000 / hz;so that actual duration is more precise (for hz=300 which is the only that doesn't divide 1000)
All in all, make the adjustments for HZ with less code (since I expect this will need adjustments for SMPs in future).
Hi Michal,
Thanks for your feedback. I'll make the changes you have suggested in v4.
Thanks,
Joe