Skip to contentSkip to conversation

selftests: cgroup: Report failed checks in test_memcontrol

PATCH In review Sebastian Chlad 2 participants 4 messages Sep 26 – Oct 12

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover selftests: cgroup: Report failed checks in test_memcontrol

test_memcontrol fails intermittently, but most of its checks fail silently, so a "not ok" line gives no indication of how far off the measured value was. This series makes those failures print the observed and expected values. It does not change any threshold or pass/fail behaviour.

Patch 1 converts the terminal tolerance checks to values_close_report(), as was done for test_cpu in commit 4cdde87d723a ("selftests: cgroup: Use values_close_report in test_cpu"). values_close() is kept where it is a loop or retry condition, where a mismatch is expected.

Patch 2 reports the "memory.current < 50M" check in alloc_pagecache_50M_check(). Because test_memcg_high() calls the same helper expecting it to fail, it gets a quiet variant. An alternative would be to pass the flag through the currently unused void *arg of the cg_run() callback; I am happy to switch to that if preferred.

With patch 1 applied, we observe at times failures like these:

not ok 3 test_memcg_min
[FAIL] actual=18722816 expected=30408704 | diff=11685888 | limit=7369725 | tolerance=15% | actual_error=23.78%

not ok 4 test_memcg_low
[FAIL] actual=20852736 expected=30408704 | diff=9555968 | limit=7689210 | tolerance=15% | actual_error=18.64%

not ok 4 test_memcg_low
[FAIL] actual=14364672 expected=22020096 | diff=7655424 | limit=7276940 | tolerance=20% | actual_error=21.04%

These are the child memory.current checks in test_memcg_protection() (expected ~29M and ~21M). Their tolerances were already raised in commit d2def68ae06a ("selftests: memcg: increase error tolerance of child memory.current check in test_memcg_protection()"), and the values above still fall outside them. In the first case the child is about 38% below the expected 29M. This series only makes such failures visible; whether the expectations or the tolerances should change is left for a separate discussion.

Changes in v2:
- Patch 1: pass the measured value first in the alloc_anon_50M_check() values_close_report() call, so that a failure prints actual and expected the right way round (reported by Sashiko AI review).

v1: https://lore.kernel.org/all/[email protected]/

Sebastian Chlad (2):
selftests: cgroup: Use values_close_report in test_memcontrol
selftests: cgroup: Report memory.current shortfall in
alloc_pagecache_50M_check

 .../selftests/cgroup/test_memcontrol.c        | 50 +++++++++++++------
 1 file changed, 36 insertions(+), 14 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH v2 1/2 selftests: cgroup: Use values_close_report in test_memcontrol

Convert the terminal tolerance checks in test_memcontrol to values_close_report() so that a failure prints the observed and expected values instead of failing silently. Also report the "memory.current < size" check in alloc_anon_50M_check().

values_close() is kept where it is used as a loop or retry condition (waiting for usage to settle in test_memcg_protection(), reclaim_until(), the reclaim retry loop and the socket test), since a mismatch there is expected and reporting it would print spurious failures.

Assisted-by claude-opus-5-5
---
 .../selftests/cgroup/test_memcontrol.c        | 25 +++++++++++--------
 1 file changed, 14 insertions(+), 11 deletions(-)
tools/testing/selftests/cgroup/test_memcontrol.c +14 -11
index 0ed82347044e..8882c7faeb32 100644
@@ -200,17 +200,20 @@ static int alloc_anon_50M_check(const char *cgroup, void *arg)
 		return -1;
 
 	current = cg_read_long(cgroup, "memory.current");
-	if (current < size)
+	if (current < size) {
+		fprintf(stderr, "memory.current %ld < expected %zu\n",
+			current, size);
 		goto cleanup;
+	}
 
-	if (!values_close(size, current, 3))
+	if (!values_close_report(current, size, 3))
 		goto cleanup;
 
 	anon = cg_read_key_long(cgroup, "memory.stat", "anon ");
 	if (anon < 0)
 		goto cleanup;
 
-	if (!values_close(anon, current, 3))
+	if (!values_close_report(anon, current, 3))
 		goto cleanup;
 
 	ret = 0;
@@ -241,7 +244,7 @@ static int alloc_pagecache_50M_check(const char *cgroup, void *arg)
 	if (file < 0)
 		goto cleanup;
 
-	if (!values_close(file, current, 10))
+	if (!values_close_report(file, current, 10))
 		goto cleanup;
 
 	ret = 0;
@@ -578,16 +581,16 @@ static int test_memcg_protection(const char *root, bool min)
 	if (cg_run(parent[2], alloc_anon, (void *)MB(148)))
 		goto cleanup;
 
-	if (!values_close(cg_read_long(parent[1], "memory.current"), MB(50), 3))
+	if (!values_close_report(cg_read_long(parent[1], "memory.current"), MB(50), 3))
 		goto cleanup;
 
 	for (i = 0; i < ARRAY_SIZE(children); i++)
 		c[i] = cg_read_long(children[i], "memory.current");
 
-	if (!values_close(c[0], MB(29), 15))
+	if (!values_close_report(c[0], MB(29), 15))
 		goto cleanup;
 
-	if (!values_close(c[1], MB(21), 20))
+	if (!values_close_report(c[1], MB(21), 20))
 		goto cleanup;
 
 	if (c[3] != 0)
@@ -603,7 +606,7 @@ static int test_memcg_protection(const char *root, bool min)
 	}
 
 	current = min ? MB(50) : MB(30);
-	if (!values_close(cg_read_long(parent[1], "memory.current"), current, 3))
+	if (!values_close_report(cg_read_long(parent[1], "memory.current"), current, 3))
 		goto cleanup;
 
 	if (!reclaim_until(children[0], MB(10)))
@@ -691,7 +694,7 @@ static int alloc_pagecache_max_30M(const char *cgroup, void *arg)
 		goto cleanup;
 
 	current = cg_read_long(cgroup, "memory.current");
-	if (!values_close(current, MB(30), 5))
+	if (!values_close_report(current, MB(30), 5))
 		goto cleanup;
 
 	ret = 0;
@@ -1006,12 +1009,12 @@ static int alloc_anon_50M_check_swap(const char *cgroup, void *arg)
 		return -1;
 
 	mem_current = cg_read_long(cgroup, "memory.current");
-	if (!mem_current || !values_close(mem_current, mem_max, 3))
+	if (!mem_current || !values_close_report(mem_current, mem_max, 3))
 		goto cleanup;
 
 	swap_current = cg_read_long(cgroup, "memory.swap.current");
 	if (!swap_current ||
-	    !values_close(mem_current + swap_current, size, 3))
+	    !values_close_report(mem_current + swap_current, size, 3))
 		goto cleanup;
 
 	ret = 0;
Signature · 1 line
2.51.0
#2 lore raw

PATCH v2 2/2 selftests: cgroup: Report memory.current shortfall in alloc_pagecache_50M_check

In reply to Sebastian Chlad

alloc_pagecache_50M_check() fails silently when memory.current is below the 50M that was just written to the page cache. This is the check that can trip when the page cache of the test cgroup is reclaimed under global memory pressure, so print the observed value.

test_memcg_high() runs the same helper with memory.high set to 30M and expects it to fail, so reporting there would print a misleading failure message on every passing run. Split the body into __alloc_pagecache_50M_check() with a report flag, and give test_memcg_high() a quiet variant.

Assisted-by claude-opus-5-5
---
 .../selftests/cgroup/test_memcontrol.c        | 27 ++++++++++++++++---
 1 file changed, 23 insertions(+), 4 deletions(-)
tools/testing/selftests/cgroup/test_memcontrol.c +23 -4
index 8882c7faeb32..759e1640746d 100644
@@ -222,7 +222,7 @@ static int alloc_anon_50M_check(const char *cgroup, void *arg)
 	return ret;
 }
 
-static int alloc_pagecache_50M_check(const char *cgroup, void *arg)
+static int __alloc_pagecache_50M_check(const char *cgroup, bool report)
 {
 	size_t size = MB(50);
 	int ret = -1;
@@ -237,14 +237,19 @@ static int alloc_pagecache_50M_check(const char *cgroup, void *arg)
 		goto cleanup;
 
 	current = cg_read_long(cgroup, "memory.current");
-	if (current < size)
+	if (current < size) {
+		if (report)
+			fprintf(stderr, "memory.current %ld < expected %zu\n",
+				current, size);
 		goto cleanup;
+	}
 
 	file = cg_read_key_long(cgroup, "memory.stat", "file ");
 	if (file < 0)
 		goto cleanup;
 
-	if (!values_close_report(file, current, 10))
+	if (report ? !values_close_report(file, current, 10) :
+		     !values_close(file, current, 10))
 		goto cleanup;
 
 	ret = 0;
@@ -254,6 +259,20 @@ static int alloc_pagecache_50M_check(const char *cgroup, void *arg)
 	return ret;
 }
 
+static int alloc_pagecache_50M_check(const char *cgroup, void *arg)
+{
+	return __alloc_pagecache_50M_check(cgroup, true);
+}
+
+/*
+ * Same as alloc_pagecache_50M_check(), but silent on failure, for callers
+ * that expect the allocation to be held below 50M.
+ */
+static int alloc_pagecache_50M_check_quiet(const char *cgroup, void *arg)
+{
+	return __alloc_pagecache_50M_check(cgroup, false);
+}
+
 /*
  * This test create a memory cgroup, allocates
  * some anonymous memory and some pagecache
@@ -735,7 +754,7 @@ static int test_memcg_high(const char *root)
 	if (cg_run(memcg, alloc_anon, (void *)MB(31)))
 		goto cleanup;
 
-	if (!cg_run(memcg, alloc_pagecache_50M_check, NULL))
+	if (!cg_run(memcg, alloc_pagecache_50M_check_quiet, NULL))
 		goto cleanup;
 
 	if (cg_run(memcg, alloc_pagecache_max_30M, NULL))
Signature · 1 line
2.51.0
maintainer
#3 lore raw
In reply to Sebastian Chlad

Hi Sebastian.

Quoted 44 lines · Sebastian Chlad

test_memcontrol fails intermittently, but most of its checks fail silently, so a "not ok" line gives no indication of how far off the measured value was. This series makes those failures print the observed and expected values. It does not change any threshold or pass/fail behaviour.

Patch 1 converts the terminal tolerance checks to values_close_report(), as was done for test_cpu in commit 4cdde87d723a ("selftests: cgroup: Use values_close_report in test_cpu"). values_close() is kept where it is a loop or retry condition, where a mismatch is expected.

Patch 2 reports the "memory.current < 50M" check in alloc_pagecache_50M_check(). Because test_memcg_high() calls the same helper expecting it to fail, it gets a quiet variant. An alternative would be to pass the flag through the currently unused void *arg of the cg_run() callback; I am happy to switch to that if preferred.

With patch 1 applied, we observe at times failures like these:

not ok 3 test_memcg_min
[FAIL] actual=18722816 expected=30408704 | diff=11685888 | limit=7369725 | tolerance=15% | actual_error=23.78%

not ok 4 test_memcg_low
[FAIL] actual=20852736 expected=30408704 | diff=9555968 | limit=7689210 | tolerance=15% | actual_error=18.64%

not ok 4 test_memcg_low
[FAIL] actual=14364672 expected=22020096 | diff=7655424 | limit=7276940 | tolerance=20% | actual_error=21.04%

These are the child memory.current checks in test_memcg_protection() (expected ~29M and ~21M). Their tolerances were already raised in commit d2def68ae06a ("selftests: memcg: increase error tolerance of child memory.current check in test_memcg_protection()"), and the values above still fall outside them. In the first case the child is about 38% below the expected 29M. This series only makes such failures visible; whether the expectations or the tolerances should change is left for a separate discussion.

Changes in v2:
- Patch 1: pass the measured value first in the alloc_anon_50M_check() values_close_report() call, so that a failure prints actual and expected the right way round (reported by Sashiko AI review).

v1: https://lore.kernel.org/all/[email protected]/

Quoted 7 lines

Sebastian Chlad (2):
selftests: cgroup: Use values_close_report in test_memcontrol
selftests: cgroup: Report memory.current shortfall in
alloc_pagecache_50M_check

 .../selftests/cgroup/test_memcontrol.c        | 50 +++++++++++++------
 1 file changed, 36 insertions(+), 14 deletions(-)

AFAIU, the test is catching two types of errors:
a) running under global pressure (preparations already fail),
b) mismatches in expected values of protection tests (after preparation).

I like the reporting of the latter -- with (only) that all consequent silent failures would imply^H strongly hint the error of type a), no? :-)

Thanks,
Michal