All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] rt-tests: hwlatdetect: Restore tracing parameters
@ 2026-10-19 10:29 Costa Shulyupin
  2026-10-19 22:19 ` Crystal Wood
  2026-10-20 10:16 ` Sebastian Andrzej Siewior
  0 siblings, 2 replies; 6+ messages in thread
From: Costa Shulyupin @ 2026-10-19 10:29 UTC (permalink / raw)
  To: Costa Shulyupin, John Kacur, Luis Claudio R. Goncalves,
	Bart Wensley, Crystal Wood
  Cc: linux-rt-users

hwlatdetect leaves tracing parameters changed after a run. These
parameters can affect later tests. When --cpu-list is omitted,
hwlatdetect reuses the existing tracing_cpumask.

Save the CPU mask, current tracer, threshold, window and width before
changing them. Restore them during cleanup, including after SIGTERM or
an unhandled exception. Leave tracing disabled, as before.

Add a shell test for successful and failing runs, SIGTERM handling,
--cpu-list changes.

Assisted-by: Codex:GPT-6
Signed-off-by: Costa Shulyupin <costa.shul@redhat.com>

---
v2:
Restore tracing parameters unconditionally
as suggested by Crystal Wood and Bart Wensley
---
 src/hwlatdetect/hwlatdetect.8       |  2 ++
 src/hwlatdetect/hwlatdetect.py      | 18 +++++++++-
 src/hwlatdetect/test_hwlatdetect.sh | 52 +++++++++++++++++++++++++++++
 3 files changed, 71 insertions(+), 1 deletion(-)
 create mode 100755 src/hwlatdetect/test_hwlatdetect.sh

diff --git a/src/hwlatdetect/hwlatdetect.8 b/src/hwlatdetect/hwlatdetect.8
index 560ff7cc0cc9..0a0c531502f9 100644
--- a/src/hwlatdetect/hwlatdetect.8
+++ b/src/hwlatdetect/hwlatdetect.8
@@ -57,6 +57,8 @@ The hwlatdetector script manages the mounting/unmounting of the
 debugfs as well as interacting with the ftrace hwlatdetector
 If the debugfs is already mounted then hwlatdetector will not
 unmount it after a run.
+The previous CPU mask, tracer, threshold, window, and width are
+restored after the test.
 
 .SH OPTIONS
 .TP
diff --git a/src/hwlatdetect/hwlatdetect.py b/src/hwlatdetect/hwlatdetect.py
index ee1e0171210d..0e9e3568f5ee 100755
--- a/src/hwlatdetect/hwlatdetect.py
+++ b/src/hwlatdetect/hwlatdetect.py
@@ -8,9 +8,11 @@
 # (C) 2009 Clark Williams <williams@redhat.com>
 
 import abc
+import atexit
 import errno
 import os
 import os.path
+import signal
 import subprocess
 import sys
 import time
@@ -183,6 +185,7 @@     def c_states_on(self):
         """ close the file descriptor and re-enable c-states """
         if self.dma_latency_handle:
             os.close(self.dma_latency_handle)
+            self.dma_latency_handle = None
             debug("c-states enabled")
 
     @abc.abstractmethod
@@ -323,6 +326,10 @@     def __init__(self):
         self.samples = []
         self.first = None
         self.last = None
+        fields = ("cpumask", "current_tracer", "threshold", "window", "width")
+        self.saved = {f: self.get(f).strip() for f in fields}
+        atexit.register(self.cleanup)
+        signal.signal(signal.SIGTERM, lambda sig, _: sys.exit(128 + sig))
         self.set("enable", 0)
         self.set('current_tracer', 'hwlat')
 
@@ -388,7 +395,15 @@     def display(self):
 
     def cleanup(self):
         self.set("tracing_on", "0")
-        self.set("current_tracer", "nop")
+        self.c_states_on()
+        debug("Restoring previous configuration")
+        old = self.saved
+        self.set("current_tracer", old["current_tracer"])
+        self.set("width", 0) # must be always width <= window
+        self.set("window", old["window"])
+        self.set("width", old["width"])
+        self.set("cpumask", old["cpumask"])
+        self.set("threshold", old["threshold"])
         if not self.debugfs.umount():
             raise RuntimeError("Failed to unmount debugfs")
 
@@ -606,4 +621,5 @@     def cleanup(self):
         detect.display()
 
     detect.cleanup()
+    atexit.unregister(detect.cleanup)
     sys.exit(maxlatency > hardlimit)
diff --git a/src/hwlatdetect/test_hwlatdetect.sh b/src/hwlatdetect/test_hwlatdetect.sh
new file mode 100755
index 000000000000..187536f95264
--- /dev/null
+++ b/src/hwlatdetect/test_hwlatdetect.sh
@@ -0,0 +1,52 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0-only
+
+set -o errexit
+
+out=
+trap 'echo -e "FAIL: ${BASH_SOURCE[0]}:${LINENO}: $BASH_COMMAND -> $?\noutput:\n$out" >&2' ERR
+
+[[ $(id -u) == 0 ]]
+
+d=$(dirname -- "${BASH_SOURCE[0]}")
+
+tracing=/sys/kernel/debug/tracing
+mask=$tracing/tracing_cpumask
+settings=("$mask" "$tracing/current_tracer" "$tracing/tracing_thresh"
+	"$tracing/hwlat_detector/window" "$tracing/hwlat_detector/width")
+all=$(PYTHONPATH="$d" python3 -B -c '
+from hwlatdetect import group_sep
+m = 0
+for part in open("/sys/devices/system/cpu/online").read().strip().split(","):
+    a, *b = map(int, part.split("-"))
+    for i in range(a, (b[0] if b else a) + 1):
+        m |= 1 << i
+print(group_sep(f"{m:x}"))
+')
+
+out=$("$d/hwlatdetect.py" --duration 1 --hardlimit 999 2>&1)
+[[ $out == *"test finished"* ]]
+
+! out=$("$d/hwlatdetect.py" --duration 1 --threshold 0 --hardlimit 0 2>&1)
+[ $? -eq 0 ]
+[[ $out == *"test finished"* ]]
+
+echo $all > $mask
+baseline=$(cat "${settings[@]}")
+
+! out=$("$d/hwlatdetect.py" --cpu-list 1 --duration 1 2>&1)
+[[ $out == *"test finished"* ]]
+[[ $(cat "${settings[@]}") == "$baseline" ]]
+
+rc=0
+out=$(timeout --signal=TERM 2 \
+	"$d/hwlatdetect.py" --cpu-list 1 --duration 60 2>&1) || rc=$?
+[[ $rc -eq 124 ]]
+[[ $out == *"Starting test"* ]]
+[[ $(cat "${settings[@]}") == "$baseline" ]]
+
+! out=$("$d/hwlatdetect.py" --cpu-list 1 --duration 1 2>&1)
+[[ $out == *"test finished"* ]]
+[[ $(cat "${settings[@]}") == "$baseline" ]]
+
+echo Passed
-- 
2.56.0


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

end of thread, other threads:[~2026-10-21  7:10 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-19 10:29 [PATCH v2] rt-tests: hwlatdetect: Restore tracing parameters Costa Shulyupin
2026-10-19 22:19 ` Crystal Wood
2026-10-20 10:16 ` Sebastian Andrzej Siewior
2026-10-20 17:39   ` Costa Shulyupin
2026-10-21  6:48     ` Sebastian Andrzej Siewior
2026-10-21  7:10       ` Sebastian Andrzej Siewior

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.