linux/tools/testing/selftests/cgroup
Shashank Balaji 954bacce36 selftests/cgroup: fix cpu.max tests
Current cpu.max tests (both the normal one and the nested one) are broken.

They setup cpu.max with 1000 us quota and the default period (100,000 us).
A cpu hog is run for a duration of 1s as per wall clock time. This corresponds
to 10 periods, hence an expected usage of 10,000 us. We want the measured
usage (as per cpu.stat) to be close to 10,000 us.

Previously, this approximate equality test was done by
`!values_close(usage_usec, expected_usage_usec, 95)`: if the absolute
difference between usage_usec and expected_usage_usec is greater than 95% of
their sum, then we pass. And expected_usage_usec was set to 1,000,000 us.
Mathematically, this translates to the following being true for pass:

	|usage - expected_usage| > (usage + expected_usage)*0.95

	If usage > expected_usage:
		usage - expected_usage > (usage + expected_usage)*0.95
		0.05*usage > 1.95*expected_usage
		usage > 39*expected_usage = 39s

	If usage < expected_usage:
		expected_usage - usage > (usage + expected_usage)*0.95
		0.05*expected_usage > 1.95*usage
		usage < 0.0256*expected_usage = 25,600 us

Combined,

	Pass if usage < 25,600 us or > 39 s,

which makes no sense given that all we need is for usage_usec to be close to
10,000 us.

Fix this by explicitly calcuating the expected usage duration based on the
configured quota, default period, and the duration, and compare usage_usec
and expected_usage_usec using values_close() with a 10% error margin.

Also, use snprintf to get the quota string to write to cpu.max instead of
hardcoding the quota, ensuring a single source of truth.

Remove the check comparing user_usec and expected_usage_usec, since on running
this test modified with printfs, it's seen that user_usec and usage_usec can
regularly exceed the theoretical expected_usage_usec:

	$ sudo ./test_cpu
	user: 10485, usage: 10485, expected: 10000
	ok 1 test_cpucg_max
	user: 11127, usage: 11127, expected: 10000
	ok 2 test_cpucg_max_nested
	$ sudo ./test_cpu
	user: 10286, usage: 10286, expected: 10000
	ok 1 test_cpucg_max
	user: 10404, usage: 11271, expected: 10000
	ok 2 test_cpucg_max_nested

Hence, a values_close() check of usage_usec and expected_usage_usec is
sufficient.

Fixes: a79906570f ("cgroup: Add test_cpucg_max_nested() testcase")
Fixes: 889ab8113e ("cgroup: Add test_cpucg_max() testcase")
Acked-by: Michal Koutný <mkoutny@suse.com>
Signed-off-by: Shashank Balaji <shashank.mahadasyam@sony.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
2025-07-17 08:12:19 -10:00
..
lib selftests: cgroup_util: Add helpers for testing named v1 hierarchies 2025-06-17 08:13:01 -10:00
.gitignore selftests: cgroup: Add basic tests for pids controller 2024-05-26 08:45:10 -10:00
config mm: remove CONFIG_MEMCG_KMEM 2024-07-10 12:14:54 -07:00
Makefile cgroup: selftests: Move cgroup_util into its own library 2025-05-16 11:45:15 -07:00
memcg_protection.m selftests: memcg: adjust expected reclaim values of protected cgroups 2022-05-27 09:33:45 -07:00
test_core.c selftests: cgroup: Fix compilation on pre-cgroupns kernels 2025-06-17 08:13:25 -10:00
test_cpu.c selftests/cgroup: fix cpu.max tests 2025-07-17 08:12:19 -10:00
test_cpuset.c selftests: cgroup: skip test_cgcore_lesser_ns_open when cgroup2 mounted without nsdelegate 2024-04-03 09:42:13 -10:00
test_cpuset_prs.sh selftest/cgroup: Add a remote partition transition test to test_cpuset_prs.sh 2025-03-31 13:28:19 -10:00
test_cpuset_v1_base.sh cgroup/cpuset: add sefltest for cpuset v1 2024-08-30 10:00:17 -10:00
test_cpuset_v1_hp.sh selftests/cgroup: use bash in test_cpuset_v1_hp.sh 2025-02-04 09:36:54 -10:00
test_freezer.c selftests: cgroup: skip test_cgcore_lesser_ns_open when cgroup2 mounted without nsdelegate 2024-04-03 09:42:13 -10:00
test_hugetlb_memcg.c Revert "selftests/cgroup: Drop define _GNU_SOURCE" 2024-05-20 09:00:15 -06:00
test_kill.c selftests: cgroup: skip test_cgcore_lesser_ns_open when cgroup2 mounted without nsdelegate 2024-04-03 09:42:13 -10:00
test_kmem.c selftests: cgroup: Allow longer timeout for kmem_dead_cgroups cleanup 2025-07-12 07:34:49 -10:00
test_memcontrol.c Generic: 2025-06-02 12:24:58 -07:00
test_pids.c selftests: cgroup: Add basic tests for pids controller 2024-05-26 08:45:10 -10:00
test_stress.sh kseltest/cgroup: Make test_stress.sh work if run interactively 2022-05-13 09:33:21 -10:00
test_zswap.c selftests: cgroup: Fix missing newline in test_zswap_writeback_one 2025-07-12 07:35:30 -10:00
wait_inotify.c kselftest/cgroup: Add cpuset v2 partition root state test 2022-09-04 10:47:28 -10:00
with_stress.sh