Linux Test Project
 help / color / mirror / Atom feed
* [LTP] [PATCH] memcg_lib.sh: poll for expected values in check_mem_stat()
@ 2026-11-16  1:31 Yu Miao
  2026-11-16  3:10 ` [LTP] " linuxtestproject.agent
  2026-11-20  8:56 ` [LTP] [PATCH] " Li Wang
  0 siblings, 2 replies; 3+ messages in thread
From: Yu Miao @ 2026-11-16  1:31 UTC (permalink / raw)
  To: ltp

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


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

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

* Re: [LTP] memcg_lib.sh: poll for expected values in check_mem_stat()
  2026-11-16  1:31 [LTP] [PATCH] memcg_lib.sh: poll for expected values in check_mem_stat() Yu Miao
@ 2026-11-16  3:10 ` linuxtestproject.agent
  2026-11-20  8:56 ` [LTP] [PATCH] " Li Wang
  1 sibling, 0 replies; 3+ messages in thread
From: linuxtestproject.agent @ 2026-11-16  3:10 UTC (permalink / raw)
  To: Yu Miao; +Cc: ltp

Hi Yu,

On Mon, Nov 16, 2026 at 09:31:47AM +0800, Yu Miao wrote:
> memcg_lib.sh: poll for expected values in check_mem_stat()

Verdict - Reviewed

---
Note:

The agent can sometimes produce false positives although often its
findings are genuine. If you find issues with the review, please
comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

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

* Re: [LTP] [PATCH] memcg_lib.sh: poll for expected values in check_mem_stat()
  2026-11-16  1:31 [LTP] [PATCH] memcg_lib.sh: poll for expected values in check_mem_stat() Yu Miao
  2026-11-16  3:10 ` [LTP] " linuxtestproject.agent
@ 2026-11-20  8:56 ` Li Wang
  1 sibling, 0 replies; 3+ messages in thread
From: Li Wang @ 2026-11-20  8:56 UTC (permalink / raw)
  To: Yu Miao; +Cc: ltp

> 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.

Patch merged, with some minor polishing:

--- a/testcases/kernel/controllers/memcg/functional/memcg_lib.sh
+++ b/testcases/kernel/controllers/memcg/functional/memcg_lib.sh
@@ -160,15 +160,17 @@ shmmax_cleanup()
 check_mem_stat()
 {
        local item_size
-       local cms_iter
+       local loops=30

-       for cms_iter in $(seq 30); do
+       while [ $loops -gt 0 ]; do
                if [ -e $1 ]; then
                        item_size=$(cat $1)
                else
                        item_size=$(grep -w $1 memory.stat | cut -d " " -f 2)
                fi

+               [ -z "$item_size" ] && break
+
                if [ "$3" ]; then
                        if [ $item_size -ge $2 ] && [ $item_size -le $3 ]; then
                                tst_res TPASS "$1 is ${2}-${3} as expected"
@@ -179,7 +181,8 @@ check_mem_stat()
                        return
                fi

-               tst_sleep 100ms
+               loops=$((loops - 1))
+               [ $loops -gt 0 ] && tst_sleep 100ms
        done

        if [ "$3" ]; then


-- 
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

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

end of thread, other threads:[~2026-11-20  8:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16  1:31 [LTP] [PATCH] memcg_lib.sh: poll for expected values in check_mem_stat() Yu Miao
2026-11-16  3:10 ` [LTP] " linuxtestproject.agent
2026-11-20  8:56 ` [LTP] [PATCH] " Li Wang

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