[LTP] [PATCH] memcg_lib.sh: poll for expected values in check_mem_stat()
Yu Miao
yumiao@kylinos.cn
Mon Nov 16 02:31:47 CET 2026
Reading memory.stat does not always return up-to-date values: the
kernel only flushes the pending stats updates synchronously when they
exceed a threshold (MEMCG_CHARGE_BATCH * num_online_cpus()), otherwise
the updates are applied by the periodic async flusher, so the values
can be stale for up to 2 seconds.
The tests only touch 65 pages (260KiB), which may generate fewer
stat updates than the threshold on machines with multiple CPUs, so
the checks right after the memory has been touched may read stale
values and fail, e.g. on 7.3.0 with 4 CPUs:
memcg_stat_rss 1 TFAIL: rss is 0, 266240 expected
Poll for the expected value with a 3s timeout instead, which follows
the polling pattern already used by signal_memcg_process() in this
file.
Signed-off-by: Yu Miao <yumiao@kylinos.cn>
---
.../controllers/memcg/functional/memcg_lib.sh | 33 ++++++++++++-------
1 file changed, 21 insertions(+), 12 deletions(-)
diff --git a/testcases/kernel/controllers/memcg/functional/memcg_lib.sh b/testcases/kernel/controllers/memcg/functional/memcg_lib.sh
index 5efede9..6960d72 100755
--- a/testcases/kernel/controllers/memcg/functional/memcg_lib.sh
+++ b/testcases/kernel/controllers/memcg/functional/memcg_lib.sh
@@ -160,21 +160,30 @@ shmmax_cleanup()
check_mem_stat()
{
local item_size
+ local cms_iter
- if [ -e $1 ]; then
- item_size=$(cat $1)
- else
- item_size=$(grep -w $1 memory.stat | cut -d " " -f 2)
- fi
-
- if [ "$3" ]; then
- if [ $item_size -ge $2 ] && [ $item_size -le $3 ]; then
- tst_res TPASS "$1 is ${2}-${3} as expected"
+ for cms_iter in $(seq 30); do
+ if [ -e $1 ]; then
+ item_size=$(cat $1)
else
- tst_res TFAIL "$1 is $item_size, ${2}-${3} expected"
+ item_size=$(grep -w $1 memory.stat | cut -d " " -f 2)
+ fi
+
+ if [ "$3" ]; then
+ if [ $item_size -ge $2 ] && [ $item_size -le $3 ]; then
+ tst_res TPASS "$1 is ${2}-${3} as expected"
+ return
+ fi
+ elif [ "$2" = "$item_size" ]; then
+ tst_res TPASS "$1 is $2 as expected"
+ return
fi
- elif [ "$2" = "$item_size" ]; then
- tst_res TPASS "$1 is $2 as expected"
+
+ tst_sleep 100ms
+ done
+
+ if [ "$3" ]; then
+ tst_res TFAIL "$1 is $item_size, ${2}-${3} expected"
else
tst_res TFAIL "$1 is $item_size, $2 expected"
fi
--
2.43.0
More information about the ltp
mailing list