nvdimm.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
From: marc.herbert@linux.intel.com
To: linux-cxl@vger.kernel.org, nvdimm@lists.linux.dev,
	alison.schofield@intel.com, dan.j.williams@intel.com,
	dave.jiang@intel.com
Cc: Marc Herbert <marc.herbert@linux.intel.com>
Subject: [ndctl PATCH 3/3] test/common: stop relying on bash $SECONDS in check_dmesg()
Date: Thu, 24 Jul 2025 22:00:46 +0000	[thread overview]
Message-ID: <20250724221323.365191-4-marc.herbert@linux.intel.com> (raw)
In-Reply-To: <20250724221323.365191-1-marc.herbert@linux.intel.com>

From: Marc Herbert <marc.herbert@linux.intel.com>

Stop relying on the imprecise bash $SECONDS variable as a test start
timestamp when scanning the logs for issues.

$SECONDS was convenient because it came "for free" and did not require
any time_init(). But it was not fine-grained enough and its rounding
process is not even documented. Keep using $SECONDS in log messages
where it is easy to use and more user-friendly than bare timestamps, but
switch the critical journalctl scan to a new, absolute NDTEST_START
timestamp initialized when test/common is sourced. Use a SECONDS-based,
rough sanity check in time_init() to make sure test/common is always
sourced early.

Signed-off-by: Marc Herbert <marc.herbert@linux.intel.com>
---
 test/common | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 50 insertions(+), 2 deletions(-)

diff --git a/test/common b/test/common
index 2d076402ef7c..0214c6aaed5f 100644
--- a/test/common
+++ b/test/common
@@ -15,6 +15,25 @@ err()
 	exit "$rc"
 }
 
+# Initialize the NDTEST_START timestamp used to scan the logs.
+# Insert an anchor/bookmark in the logs to quickly locate the start of any test.
+time_init()
+{
+	# Refuse to run if anything lasted for too long before this point
+	# because that would make NDTEST_START incorrect.
+	test "$SECONDS" -le 1 || err 'test/common must be included first!'
+
+	NDTEST_START=$(LC_TIME=C date '+%F %T.%3N')
+
+	# Log anchor, especially useful when running tests back to back
+	printf "<5>%s@%ds: sourcing test/common: NDTEST_START=%s\n" \
+		"$test_basename" "$SECONDS" "$NDTEST_START" > /dev/kmsg
+
+	# Default value, can be overridden by the environment
+	: "${NDTEST_LOG_DBG:=false}"
+}
+time_init
+
 # Global variables
 
 # NDCTL
@@ -147,11 +166,40 @@ json2var()
 # $1: line number where this is called
 check_dmesg()
 {
+	if "$NDTEST_LOG_DBG"; then
+		# Keep a record of which log lines we scanned
+		journalctl -q -b --since "$NDTEST_START" \
+			-o short-precise > journal-"$(basename "$0")".log
+	fi
+	# After enabling with `NDTEST_LOG_DBG=true meson test`, inspect with:
+	#    head -n 7 $(ls -1t build/journal-*.log | tac)
+	#    journalctl --since='- 5 min' -o short-precise -g 'test/common'
+
 	# validate no WARN or lockdep report during the run
-	sleep 1
-	log=$(journalctl -r -k --since "-$((SECONDS+1))s")
+	log=$(journalctl -r -k --since "$NDTEST_START")
 	grep -q "Call Trace" <<< "$log" && err "$1"
 	true
+
+	# Log anchor, especially useful when running tests back to back
+	printf "<5>%s@%ds: test/common: check_dmesg() OK\n" "$test_basename" "$SECONDS" > /dev/kmsg
+
+	if "$NDTEST_LOG_DBG"; then
+	    log_stress from_check_dmesg
+	fi
+}
+
+# While they should, many tests don't use check_dmesg(). So double down here. Also, this
+# runs later which is better.
+# Before enabling NDTEST_LOG_DBG=true, make sure no test started defining its own
+# EXIT trap.
+if "$NDTEST_LOG_DBG"; then
+    trap 'log_stress from_trap' EXIT
+fi
+
+log_stress()
+{
+	printf '<3>%s@%ds: NDTEST_LOG_DBG Call Trace; trying to break the next check_dmesg() %s\n' \
+		"$test_basename" "$SECONDS" "$1" > /dev/kmsg
 }
 
 # CXL COMMON
-- 
2.50.1


  parent reply	other threads:[~2025-07-24 22:16 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-24 22:00 test/common: stop relying on bash $SECONDS in check_dmesg() marc.herbert
2025-07-24 22:00 ` [ndctl PATCH 1/3] test/common: add missing quotes marc.herbert
2025-07-29 15:14   ` Dave Jiang
2025-08-06 21:43   ` Alison Schofield
2025-07-24 22:00 ` [ndctl PATCH 2/3] test/common: move err() function at the top marc.herbert
2025-07-29 15:14   ` Dave Jiang
2025-08-06 21:44   ` Alison Schofield
2025-07-24 22:00 ` marc.herbert [this message]
2025-07-29 15:19   ` [ndctl PATCH 3/3] test/common: stop relying on bash $SECONDS in check_dmesg() Dave Jiang
2025-08-22  0:10   ` Alison Schofield
2025-08-22  0:21 ` Alison Schofield

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250724221323.365191-4-marc.herbert@linux.intel.com \
    --to=marc.herbert@linux.intel.com \
    --cc=alison.schofield@intel.com \
    --cc=dan.j.williams@intel.com \
    --cc=dave.jiang@intel.com \
    --cc=linux-cxl@vger.kernel.org \
    --cc=nvdimm@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).