Skip to contentSkip to conversation

test/monitor.sh: replace sleep with polling after sync

PATCH In review [email protected] 3 participants 5 messages May 16, 2025 – May 17, 2025 libnvdimm: non-volatile memory device

lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

PATCH v2 1/1 test/monitor.sh: replace sleep with polling after sync

From: Alison Schofield <[email protected]>

monitor.sh runs for 50 seconds and spends 48 of those seconds sleeping after sync. It sleeps for 3 seconds each time it restarts the monitor, and 3 seconds before checking for expected log entries, a total of 16 naps.

Replace the sleeps with polling that waits for a max of 3 seconds but does so in 30 0.1 second intervals. This leaves the current behavior in place but offers a shorter run time for system configs capable of a faster sync.

Again - I'd like to see some Tested-by's on this one because it wouldn't be the first time my golden environment wasn't representative of all environments where these tests are run. Thanks!

---

Changes in v2:
- Poll for 3 seconds instead of removing sleep entirely (MarcH)
- Update commit msg & log
Link to v1: https://lore.kernel.org/nvdimm/[email protected]/

 test/monitor.sh | 43 ++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 40 insertions(+), 3 deletions(-)
test/monitor.sh +40 -3
index be8e24d6f3aa..61cad098d87c 100755
@@ -21,12 +21,45 @@ trap 'err $LINENO' ERR
 
 check_min_kver "4.15" || do_skip "kernel $KVER may not support monitor service"
 
+wait_for_logfile_update()
+{
+	local file="$1"
+	local prev_size="$2"
+	local timeout=30
+	local i=0
+
+	# prev_size is always zero because start_monitor truncates it.
+	# Set and check against it anyway to future proof.
+	while [ $i -lt $timeout ]; do
+		local new_size=$(stat -c%s "$file" 2>/dev/null || echo 0)
+		if [ "$new_size" -gt "$prev_size" ]; then
+			return 0
+		fi
+		sleep 0.1
+		i=$((i+1))
+	done
+
+	echo "logfile not updated within 3 seconds"
+	err "$LINENO"
+}
+
 start_monitor()
 {
 	logfile=$(mktemp)
 	$NDCTL monitor -c "$monitor_conf" -l "$logfile" $1 &
 	monitor_pid=$!
-	sync; sleep 3
+	sync
+	for i in {1..30}; do
+		if ps -p "$monitor_pid" > /dev/null; then
+			sleep 0.1
+			break
+		fi
+		sleep 0.1
+	done
+	if ! ps -p "$monitor_pid" > /dev/null; then
+		echo "monitor not ready within 3 seconds"
+		err "$LINENO"
+	fi
 	truncate --size 0 "$logfile" #remove startup log
 }
 
@@ -48,14 +81,18 @@ get_monitor_dimm()
 
 call_notify()
 {
+	local prev_size=$(stat -c%s "$logfile")
 	"$TEST_PATH"/smart-notify "$smart_supported_bus"
-	sync; sleep 3
+	sync
+	wait_for_logfile_update "$logfile" "$prev_size"
 }
 
 inject_smart()
 {
+	local prev_size=$(stat -c%s "$logfile")
 	$NDCTL inject-smart "$monitor_dimms" $1
-	sync; sleep 3
+	sync
+	wait_for_logfile_update "$logfile" "$prev_size"
 }
 
 check_result()
Signature · 1 line
2.37.3
#1 lore raw
Quoted 45 lines · alison.schofield@ wrote:

From: Alison Schofield <[email protected]>

monitor.sh runs for 50 seconds and spends 48 of those seconds sleeping after sync. It sleeps for 3 seconds each time it restarts the monitor, and 3 seconds before checking for expected log entries, a total of 16 naps.

Replace the sleeps with polling that waits for a max of 3 seconds but does so in 30 0.1 second intervals. This leaves the current behavior in place but offers a shorter run time for system configs capable of a faster sync.

Again - I'd like to see some Tested-by's on this one because it wouldn't be the first time my golden environment wasn't representative of all environments where these tests are run. Thanks!

---

Changes in v2:
- Poll for 3 seconds instead of removing sleep entirely (MarcH)
- Update commit msg & log
Link to v1: https://lore.kernel.org/nvdimm/[email protected]/

 test/monitor.sh | 43 ++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 40 insertions(+), 3 deletions(-)
test/monitor.sh +10 -0
index be8e24d6f3aa..61cad098d87c 100755
@@ -21,12 +21,45 @@ trap 'err $LINENO' ERR
 
 check_min_kver "4.15" || do_skip "kernel $KVER may not support monitor service"
 
+wait_for_logfile_update()
+{
+	local file="$1"
+	local prev_size="$2"
+	local timeout=30
+	local i=0
+
+	# prev_size is always zero because start_monitor truncates it.
+	# Set and check against it anyway to future proof.
+	while [ $i -lt $timeout ]; do

test/monitor.sh trap 'err $LINENO' ERR · line 43

Quoted 10 lines
test/monitor.sh +10 -0
+		local new_size=$(stat -c%s "$file" 2>/dev/null || echo 0)
+		if [ "$new_size" -gt "$prev_size" ]; then
+			return 0
+		fi
+		sleep 0.1
+		i=$((i+1))
+	done
+
+	echo "logfile not updated within 3 seconds"
+	err "$LINENO"

Hmm... not a fan of this open coded "wait for file to change" bash function. This feels like something that a tool can do... (searches)

Does inotifywait fit the bill here?

https://linux.die.net/man/1/inotifywait

Quoted 10 lines
test/monitor.sh +4 -1
+}
+
 start_monitor()
 {
 	logfile=$(mktemp)
 	$NDCTL monitor -c "$monitor_conf" -l "$logfile" $1 &
 	monitor_pid=$!
-	sync; sleep 3
+	sync
+	for i in {1..30}; do

test/monitor.sh trap 'err $LINENO' ERR · line 62

Quoted 10 lines
test/monitor.sh +10 -0
+		if ps -p "$monitor_pid" > /dev/null; then
+			sleep 0.1
+			break
+		fi
+		sleep 0.1
+	done
+	if ! ps -p "$monitor_pid" > /dev/null; then
+		echo "monitor not ready within 3 seconds"
+		err "$LINENO"
+	fi

This does not make sense to. The shell got the pid from the launching the executable. This is effectively testing that bash command execution works. About the only use I can imagine for this is checking that the monitor did not die early, but that should be determined by other parts of the test.

#2 lore raw
Quoted 27 lines · Dan Williams
Quoted 11 lines · alison.schofield@ wrote:
(quoted diff) +10 -0
+wait_for_logfile_update()
+{
+	local file="$1"
+	local prev_size="$2"
+	local timeout=30
+	local i=0
+
+	# prev_size is always zero because start_monitor truncates it.
+	# Set and check against it anyway to future proof.
+	while [ $i -lt $timeout ]; do

Review

Quoted 10 lines
(quoted diff) +10 -0
+		local new_size=$(stat -c%s "$file" 2>/dev/null || echo 0)
+		if [ "$new_size" -gt "$prev_size" ]; then
+			return 0
+		fi
+		sleep 0.1
+		i=$((i+1))
+	done
+
+	echo "logfile not updated within 3 seconds"
+	err "$LINENO"

Hmm... not a fan of this open coded "wait for file to change" bash function. This feels like something that a tool can do... (searches)

Quoted 3 lines

Does inotifywait fit the bill here?

https://linux.die.net/man/1/inotifywait

If inotify works, go for it. Blocking is always better than polling. It might be tricky because the file does not exist yet. Create an empty file yourself first, would that work? Probably not if ndctl monitor creates a brand new file.

If inotify does not work, consider adding to test/common this generic polling function that lets you poll in bash literally anything:

https://github.com/pmem/run_qemu/pull/177/files

It would require making prev_size global which does not look like an issue to me.

Before adding it run_qemu, that polling function has been used for years and thousands of runs in
https://github.com/thesofproject/sof-test/blob/main/case-lib/lib.sh
I mean it's been extremely well tested.

Even if you don't need polling here, it's unfortunately fairly common to have to poll from shell scripts. Why I'm suggesting a test/common location.

Quoted 18 lines
Quoted 18 lines
(quoted diff) +12 -1
+}
+
 start_monitor()
 {
 	logfile=$(mktemp)
 	$NDCTL monitor -c "$monitor_conf" -l "$logfile" $1 &
 	monitor_pid=$!
-	sync; sleep 3
+	sync
+	for i in {1..30}; do
+		if ps -p "$monitor_pid" > /dev/null; then
+			sleep 0.1
+			break
+		fi
+		sleep 0.1
+	done
+	if ! ps -p "$monitor_pid" > /dev/null; then
+		echo "monitor not ready within 3 seconds"
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+		err "$LINENO"
+	fi

This does not make sense to. The shell got the pid from the launching the executable. This is effectively testing that bash command execution works. About the only use I can imagine for this is checking that the monitor did not die early, but that should be determined by other parts of the test.

Agreed: I'm afraid the only thing this code does is sleeping 0.1s only once instead of 3s. Because not sleeping at all worked for you, no surprise a single sleep 0.1 works too.

I suspect the only case where the "for" loop actually iterates is when the "monitor" process crashes extremely fast, faster than the "sync". Basically racing with its parent to crash before the latter notices. That race does not look like a "feature" to me.

I agree this should be replaced by observing side-effects from the monitor. Dunno what. grep something in the ndctl monitor -v output?

By the way, UNTESTED:

test/monitor.sh +13 -1
@@ -67,7 +67,19 @@ check_result()
 
 stop_monitor()
 {
-	kill $monitor_pid
+	kill $monitor_pid || die "monitor $monitor_pid was already dead"
+
+	local ret=0
+	timeout 3 wait $monitor_pid || ret=$?
+	case "$ret") in
+	124) # timeout
+		die "monitor $monitor_pid ignored SIGTERM" ;;
+	0|127) # either success or killed fast
+		: ;;
+	*)
+		die "unexpected monitor exit status:$ret" ;;
+	esac
+
 	rm "$logfile"
 }
 

Something like that...

This is all assuming the monitor does not have any kind of "remote control"; that would be much better.

I don't know the "monitor", but if it has neither obvious side-effects nor any kind of "remote control", then it does a "great" job... getting in the way of tests! :-( Maybe the monitor is what should be fixed rather than adding shell script "creativity"?

author maintainer
#3 lore raw
Quoted 45 lines · Marc Herbert
Quoted 27 lines · Dan Williams
Quoted 11 lines · alison.schofield@ wrote:
(quoted diff) +10 -0
+wait_for_logfile_update()
+{
+	local file="$1"
+	local prev_size="$2"
+	local timeout=30
+	local i=0
+
+	# prev_size is always zero because start_monitor truncates it.
+	# Set and check against it anyway to future proof.
+	while [ $i -lt $timeout ]; do

Review

Quoted 10 lines
(quoted diff) +10 -0
+		local new_size=$(stat -c%s "$file" 2>/dev/null || echo 0)
+		if [ "$new_size" -gt "$prev_size" ]; then
+			return 0
+		fi
+		sleep 0.1
+		i=$((i+1))
+	done
+
+	echo "logfile not updated within 3 seconds"
+	err "$LINENO"

Hmm... not a fan of this open coded "wait for file to change" bash function. This feels like something that a tool can do... (searches)

Quoted 3 lines

Does inotifywait fit the bill here?

https://linux.die.net/man/1/inotifywait

If inotify works, go for it. Blocking is always better than polling. It might be tricky because the file does not exist yet. Create an empty file yourself first, would that work? Probably not if ndctl monitor creates a brand new file.

If inotify does not work, consider adding to test/common this generic polling function that lets you poll in bash literally anything:

https://github.com/pmem/run_qemu/pull/177/files

It would require making prev_size global which does not look like an issue to me.

Quoted 8 lines

Before adding it run_qemu, that polling function has been used for years and thousands of runs in
https://github.com/thesofproject/sof-test/blob/main/case-lib/lib.sh
I mean it's been extremely well tested.

Even if you don't need polling here, it's unfortunately fairly common to have to poll from shell scripts. Why I'm suggesting a test/common location.

Thanks. I'm not sure what other polling type work is going on in the tests, but I'm sure if any, it'll come out of the woodwork now that we're mentioning it.

I did drop the polling. Not that I thought polling was so horrid, but mostly because I found something better to 'look' for, so using 'tail -F' to watch the logfile made more sense.

Quoted 37 lines
Quoted 19 lines
Quoted 19 lines
(quoted diff) +12 -1
+}
+
 start_monitor()
 {
 	logfile=$(mktemp)
 	$NDCTL monitor -c "$monitor_conf" -l "$logfile" $1 &
 	monitor_pid=$!
-	sync; sleep 3
+	sync
+	for i in {1..30}; do
+		if ps -p "$monitor_pid" > /dev/null; then
+			sleep 0.1
+			break
+		fi
+		sleep 0.1
+	done
+	if ! ps -p "$monitor_pid" > /dev/null; then
+		echo "monitor not ready within 3 seconds"
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+		err "$LINENO"
+	fi

This does not make sense to. The shell got the pid from the launching the executable. This is effectively testing that bash command execution works. About the only use I can imagine for this is checking that the monitor did not die early, but that should be determined by other parts of the test.

Agreed: I'm afraid the only thing this code does is sleeping 0.1s only once instead of 3s. Because not sleeping at all worked for you, no surprise a single sleep 0.1 works too.

I suspect the only case where the "for" loop actually iterates is when the "monitor" process crashes extremely fast, faster than the "sync". Basically racing with its parent to crash before the latter notices. That race does not look like a "feature" to me.

Quoted 6 lines

I agree this should be replaced by observing side-effects from the monitor. Dunno what. grep something in the ndctl monitor -v output?

By the way, UNTESTED:

Is this because in your testing the monitor was left dangling and the test hanging? I suspect the that the cleanup on err was missing, and I added it my v3 patch.

Quoted 33 lines
test/monitor.sh +13 -1
@@ -67,7 +67,19 @@ check_result()
 
 stop_monitor()
 {
-	kill $monitor_pid
+	kill $monitor_pid || die "monitor $monitor_pid was already dead"
+
+	local ret=0
+	timeout 3 wait $monitor_pid || ret=$?
+	case "$ret") in
+	124) # timeout
+		die "monitor $monitor_pid ignored SIGTERM" ;;
+	0|127) # either success or killed fast
+		: ;;
+	*)
+		die "unexpected monitor exit status:$ret" ;;
+	esac
+
 	rm "$logfile"
 }
 

Something like that...

This is all assuming the monitor does not have any kind of "remote control"; that would be much better.

I don't know the "monitor", but if it has neither obvious side-effects nor any kind of "remote control", then it does a "great" job... getting in the way of tests! :-( Maybe the monitor is what should be fixed rather than adding shell script "creativity"?

author maintainer
#4 lore raw
In reply to Dan Williams
Quoted 60 lines · Dan Williams
Quoted 45 lines · alison.schofield@ wrote:

From: Alison Schofield <[email protected]>

monitor.sh runs for 50 seconds and spends 48 of those seconds sleeping after sync. It sleeps for 3 seconds each time it restarts the monitor, and 3 seconds before checking for expected log entries, a total of 16 naps.

Replace the sleeps with polling that waits for a max of 3 seconds but does so in 30 0.1 second intervals. This leaves the current behavior in place but offers a shorter run time for system configs capable of a faster sync.

Again - I'd like to see some Tested-by's on this one because it wouldn't be the first time my golden environment wasn't representative of all environments where these tests are run. Thanks!

---

Changes in v2:
- Poll for 3 seconds instead of removing sleep entirely (MarcH)
- Update commit msg & log
Link to v1: https://lore.kernel.org/nvdimm/[email protected]/

 test/monitor.sh | 43 ++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 40 insertions(+), 3 deletions(-)
test/monitor.sh +10 -0
index be8e24d6f3aa..61cad098d87c 100755
@@ -21,12 +21,45 @@ trap 'err $LINENO' ERR
 
 check_min_kver "4.15" || do_skip "kernel $KVER may not support monitor service"
 
+wait_for_logfile_update()
+{
+	local file="$1"
+	local prev_size="$2"
+	local timeout=30
+	local i=0
+
+	# prev_size is always zero because start_monitor truncates it.
+	# Set and check against it anyway to future proof.
+	while [ $i -lt $timeout ]; do

test/monitor.sh trap 'err $LINENO' ERR · line 43

Quoted 10 lines
test/monitor.sh +10 -0
+		local new_size=$(stat -c%s "$file" 2>/dev/null || echo 0)
+		if [ "$new_size" -gt "$prev_size" ]; then
+			return 0
+		fi
+		sleep 0.1
+		i=$((i+1))
+	done
+
+	echo "logfile not updated within 3 seconds"
+	err "$LINENO"

Hmm... not a fan of this open coded "wait for file to change" bash function. This feels like something that a tool can do... (searches)

Quoted 1 line

Does inotifywait fit the bill here?

It's not capable of knowing what happened before it started and there is no synchronizing. So a lot of extra checking when using it.

v3 will use 'tail -F' and check for the expected number of timestamps.

Quoted 21 lines

https://linux.die.net/man/1/inotifywait

Quoted 18 lines
test/monitor.sh +12 -1
+}
+
 start_monitor()
 {
 	logfile=$(mktemp)
 	$NDCTL monitor -c "$monitor_conf" -l "$logfile" $1 &
 	monitor_pid=$!
-	sync; sleep 3
+	sync
+	for i in {1..30}; do
+		if ps -p "$monitor_pid" > /dev/null; then
+			sleep 0.1
+			break
+		fi
+		sleep 0.1
+	done
+	if ! ps -p "$monitor_pid" > /dev/null; then
+		echo "monitor not ready within 3 seconds"
Quoted 8 lines

test/monitor.sh trap 'err $LINENO' ERR · line 62

Quoted 2 lines
test/monitor.sh +2 -0
+		err "$LINENO"
+	fi

This does not make sense to. The shell got the pid from the launching the executable. This is effectively testing that bash command execution works. About the only use I can imagine for this is checking that the monitor did not die early, but that should be determined by other parts of the test.

Understood. I couldn't find any clear 'signal' that monitor is ready. So, I've added one in a v3.