Skip to contentSkip to conversation

rtla: handle SIGTERM and test abnormal exits

PATCH New Chaosheng Qu 2 participants 4 messages Nov 21

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v4 cover rtla: handle SIGTERM and test abnormal exits

v3 split the test changes into their own patch and fixed the commit messages, but the test still signalled as soon as the instance directory appeared plus one second. On a slow guest that can beat set_signals(), which is the startup race sashiko described. v4 waits until /proc/<pid>/status reports SIGTERM in SigCgt before signalling.

Patch 1 is unchanged: it adds the SIGTERM handler that was missing. SIGINT, the -d/--duration timer and a failing --on-threshold action already reach the cleanup path; SIGTERM terminated the process and left its tracefs instance behind.

Patch 2 is the test, now with the readiness check. With it, cleanup.t passes all five cases on a 7.3-rc7 guest, including SIGTERM; without the handler, SIGTERM leaves the instance behind and the test fails.

v3: https://lore.kernel.org/linux-trace-kernel/[email protected]/
v2: https://lore.kernel.org/linux-trace-kernel/[email protected]/
v1: https://lore.kernel.org/linux-trace-kernel/[email protected]/

This replaces the v1 submission, the v2 series and v3.

Changes since v3:
- wait for the SIGTERM handler instead of a fixed sleep in the test
- no change to patch 1

Testing: cleanup.t on 7.3-rc7, 4 vCPU QEMU guest -- SIGINT, SIGTERM, -d timer, action failure and repeated interrupts all pass. With the handler removed, the SIGTERM case fails as expected.

Chaosheng Qu (2):
rtla: handle SIGTERM during a run
rtla/tests: cover cleanup on abnormal exits

 tools/tracing/rtla/src/common.c               |   2 +
 tools/tracing/rtla/tests/cleanup.t            |  70 ++++++
 .../rtla/tests/scripts/check-cleanup.sh       | 219 ++++++++++++++++++
 3 files changed, 291 insertions(+)
 create mode 100644 tools/tracing/rtla/tests/cleanup.t
 create mode 100755 tools/tracing/rtla/tests/scripts/check-cleanup.sh
Signature · 1 line
2.43.0
#1 lore raw

PATCH v4 1/2 rtla: handle SIGTERM during a run

rtla installs a handler for SIGINT, and for SIGALRM when a duration is given, but not for SIGTERM. A run stopped by the system - a shutdown or a service manager - therefore dies without going through the cleanup path and leaves its tracefs instance behind, so the next invocation finds the instance name in use.

Install the same handler for SIGTERM, and restore the default disposition in unset_signals().

Fixes 2f3172f9dd58 ("tools/rtla: Consolidate code between osnoise/timerlat and hist/top")
---
 tools/tracing/rtla/src/common.c | 2 ++
 1 file changed, 2 insertions(+)
tools/tracing/rtla/src/common.c +2 -0
index 8c7f5e75b..b86a60076 100644
@@ -39,6 +39,7 @@ static void stop_trace(int sig)
 static void set_signals(struct common_params *params)
 {
 	signal(SIGINT, stop_trace);
+	signal(SIGTERM, stop_trace);
 	if (params->duration) {
 		signal(SIGALRM, stop_trace);
 		alarm(params->duration);
@@ -51,6 +52,7 @@ static void set_signals(struct common_params *params)
 static void unset_signals(struct common_params *params)
 {
 	signal(SIGINT, SIG_DFL);
+	signal(SIGTERM, SIG_DFL);
 	if (params->duration) {
 		alarm(0);
 		signal(SIGALRM, SIG_DFL);
Signature · 1 line
2.43.0
#2 lore raw

PATCH v4 2/2 rtla/tests: cover cleanup on abnormal exits

In reply to Chaosheng Qu

rtla creates a tracefs instance for each run and removes it on the way out. Nothing verifies that, and a leaked instance only shows up as a confusing failure of the next run.

Add a test for the paths users actually hit: SIGINT, SIGTERM, rtla's own -d timer, a failing --on-threshold action, and several interrupted runs followed by a normal one. These need rtla running in the background while the test signals it, which engine.sh's check() cannot express, so cleanup.t drives a helper script and emits TAP itself.

The instance directory is created before set_signals() runs, so the helper waits until /proc/<pid>/status reports SIGTERM in SigCgt before it signals; waiting for the directory alone races startup.

The test needs root, tracefs and CONFIG_OSNOISE_TRACER, and skips when tracefs is not writable.

---
 tools/tracing/rtla/tests/cleanup.t            |  70 ++++++
 .../rtla/tests/scripts/check-cleanup.sh       | 219 ++++++++++++++++++
 2 files changed, 289 insertions(+)
 create mode 100644 tools/tracing/rtla/tests/cleanup.t
 create mode 100755 tools/tracing/rtla/tests/scripts/check-cleanup.sh
tools/tracing/rtla/tests/cleanup.t new file +70 -0
new file mode 100644
index 000000000..176b1a96f
@@ -0,0 +1,70 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# Verify that rtla removes the tracefs instance it created on the abnormal
+# exit paths users actually hit:
+#
+#   - SIGINT during a run            (interactive Ctrl-C)
+#   - SIGTERM during a run
+#   - rtla's own -d/--duration timer
+#   - a failing --on-threshold shell action
+#   - several interrupted runs in a row, followed by a normal run
+#
+# A leaked instance pollutes the next invocation.
+source tests/engine.sh
+test_begin
+
+set_timeout 2m
+
+#
+# These cases need to run rtla in the background and signal it, which
+# engine.sh's check() (one foreground command) cannot express.
+#
+run_cleanup_cases() {
+	local helper="$testdir/scripts/check-cleanup.sh"
+	local out rc timeout_cmd skip
+
+	ctr=$((ctr + 1))
+	[ -z "$TEST_COUNT" ] && return
+
+	if [ ! -x "$helper" ]; then
+		echo "not ok $ctr - cleanup helper is not executable"
+		return
+	fi
+
+	# check() normally does this before every test. Do the same here, both to
+	# leave a clean tracer state and to make a hung helper terminate.
+	reset_osnoise || {
+		echo "ok $ctr - instances cleaned up on SIGINT, timeout and action failure # SKIP tracefs is not writable"
+		return
+	}
+	timeout_cmd=${TIMEOUT:-timeout -v -k 15s 2m}
+
+	out=$($timeout_cmd "$helper" "$RTLA" 2>&1)
+	rc=$?
+	if [ $rc -eq 0 ]; then
+		# "SKIP:" is what the helper prints when it could not run at all
+		# (no writable tracefs, or the binary is unusable); a per-case skip
+		# says "note:" instead. So the first SKIP: line means nothing ran.
+		skip=$(printf '%s\n' "$out" | sed -n 's/^SKIP: //p' | head -n 1)
+		if [ -n "$skip" ]; then
+			echo "ok $ctr - instances cleaned up on SIGINT, timeout and action failure # SKIP $skip"
+			return
+		fi
+		printf '%s\n' "$out" | sed -n 's/^note: /# note: /p'
+		echo "ok $ctr - instances cleaned up on SIGINT, timeout and action failure"
+	elif [ $rc -eq 124 ] || [ $rc -eq 137 ]; then
+		echo "not ok $ctr - instances cleaned up on SIGINT, timeout and action failure"
+		echo "# helper timed out after 2m"
+	else
+		echo "not ok $ctr - instances cleaned up on SIGINT, timeout and action failure"
+		echo "# helper exited $rc"
+		printf '%s\n' "$out" | while read -r line; do
+			echo "# $line"
+		done
+	fi
+}
+
+run_cleanup_cases
+
+test_end
tools/tracing/rtla/tests/scripts/check-cleanup.sh new file +219 -0

219 lines not on this page · raw message

Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH v4 2/2] rtla/tests: cover cleanup on abnormal exits

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [Medium] rtla/tests: CPU starvation from -P F:1 on all CPUs at 100% runtime - [Medium] rtla/tests…

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [Medium] rtla/tests: CPU starvation from -P F:1 on all CPUs at 100% runtime
- [Medium] rtla/tests: unbounded wait before wait_gone() masks broken signal handling
- [Medium] rtla/tests: false passes, broken-binary skip, and leaked child on timeout
- [Low] rtla/tests: broken tracefs writability check via reset_osnoise()
- [Low] rtla/tests: wrong instance list in cleanup_all() and stale comments
--

commit 21104c6464d760e35be422c6be02a2de02526282
Author: Chaosheng Qu <[email protected]>

rtla/tests: cover cleanup on abnormal exits

This commit adds a TAP test and helper script to verify that rtla removes its tracefs instances when interrupted by signals, stopped by its duration timer, or terminated by a threshold action.

Quoted 6 lines
tools/tracing/rtla/tests/cleanup.t new file +0 -0
new file mode 100644
index 0000000000000..176b1a96f87d5
@@ -0,0 +1,70 @@

[ ... ]

Quoted 8 lines
tools/tracing/rtla/tests/cleanup.t new file +8 -0
+run_cleanup_cases() {
+	local helper="$testdir/scripts/check-cleanup.sh"
+	local out rc timeout_cmd skip
+
+	ctr=$((ctr + 1))
+	[ -z "$TEST_COUNT" ] && return
+
+	if [ ! -x "$helper" ]; then

tools/tracing/rtla/tests/cleanup.t line 40

Quoted 10 lines
tools/tracing/rtla/tests/cleanup.t new file +10 -0
+		echo "not ok $ctr - cleanup helper is not executable"
+		return
+	fi
+
+	# check() normally does this before every test. Do the same here, both to
+	# leave a clean tracer state and to make a hung helper terminate.
+	reset_osnoise || {
+		echo "ok $ctr - instances cleaned up on SIGINT, timeout and action failure # SKIP tracefs is not writable"
+		return
+	}

[Severity: Low]
Does reset_osnoise() actually return non-zero when tracefs is not writable or when CONFIG_OSNOISE_TRACER is disabled?

In tools/tracing/rtla/tests/engine.sh, reset_osnoise() runs without set -e and only returns 1 if pushd /sys/kernel/tracing fails; every subsequent echo and cd ignores errors, and the function returns the 0 exit status of popd. When /sys/kernel/tracing is searchable by an unprivileged user or mounted read-only, reset_osnoise() succeeds while printing write errors to stderr, and if /sys/kernel/tracing/osnoise is absent, cd ../osnoise fails and the redirections attempt to create files inside /sys/kernel/tracing/instances. Also, neither cleanup.t nor check-cleanup.sh calls reset_osnoise() after the helper finishes (or after stop_pid() kills rtla with SIGKILL) to restore /sys/kernel/tracing/osnoise settings.

Quoted 6 lines
tools/tracing/rtla/tests/scripts/check-cleanup.sh new file +0 -0
new file mode 100755
index 0000000000000..7bd5b1233b607
@@ -0,0 +1,219 @@

[ ... ]

tools/tracing/rtla/tests/scripts/check-cleanup.sh line 26

Quoted 5 lines
tools/tracing/rtla/tests/scripts/check-cleanup.sh new file +5 -0
+cleanup_all() {
+	for i in osnoise_top osnoise_hist timerlat_top timerlat_hist hwnoise_top; do
+		[ -d "$INSTANCES/$i" ] && rmdir "$INSTANCES/$i" 2>/dev/null
+	done
+}

[Severity: Low]
This isn't a bug in the test cases themselves, but cleanup_all() checks for hwnoise_top (hwnoise_main() uses osnoise_top_ops and creates osnoise_top) while omitting the auxiliary osnoise_trace and timerlat_aa instances that rtla creates.

A few comments in check-cleanup.sh also appear stale: the header usage names rtla_cleanup_test.sh instead of check-cleanup.sh, the comment above the --help check says main() accepts -usage and refers to --version probing from an earlier draft, and the comment above the threshold case attributes exit code 2 to action failure. In addition, run_cleanup_cases() in cleanup.t collapses all five cases into a single TAP test point and does not check $subtests or $STOPONFAIL from tests/engine.sh.

[ ... ]

tools/tracing/rtla/tests/scripts/check-cleanup.sh line 108

Quoted 5 lines
tools/tracing/rtla/tests/scripts/check-cleanup.sh new file +5 -0
+# ---------------------------------------------------------------------------
+# 1. SIGINT during a long run
+# ---------------------------------------------------------------------------
+cleanup_all
+$RTLA osnoise top -q -d 30s -P F:1 >/dev/null 2>&1 &

[Severity: Medium]
Can running osnoise top with -P F:1 across all CPUs at the default 100% runtime starve user-space tasks and hang the test?

When -c and -r are omitted, common_apply_config() places osnoise threads on every online CPU without moving rtla to a housekeeping CPU, and osnoise_apply_config() sets DEFAULT_SAMPLE_RUNTIME equal to DEFAULT_SAMPLE_PERIOD (1000000 us). Promoting every per-CPU osnoise/N kernel thread to SCHED_FIFO priority 1 with -P F:1 leaves SCHED_OTHER tasks such as check-cleanup.sh and rtla completely starved on systems with RT throttling disabled (sched_rt_runtime_us = -1), and consumes 95% of all CPU time even with default RT throttling. Existing tests in osnoise.t and hwnoise.t pair -P F:1 with -c 0 -r 900000 so a housekeeping CPU and period headroom remain available.

tools/tracing/rtla/tests/scripts/check-cleanup.sh line 121

Quoted 13 lines
tools/tracing/rtla/tests/scripts/check-cleanup.sh new file +13 -0
+pid=$!
+if ! wait_running "$INSTANCES/osnoise_top" "$pid"; then
+	fail "SIGINT: instance was never created (rtla did not start?)"
+	stop_pid "$pid"
+else
+	kill -INT "$pid" 2>/dev/null
+	wait "$pid" 2>/dev/null
+	if wait_gone "$INSTANCES/osnoise_top"; then
+		pass "SIGINT: instance removed after interrupt"
+	else
+		fail "SIGINT: instance still present after interrupt"
+	fi
+fi

[Severity: Medium]
Does the unbounded wait "$pid" before wait_gone() mask broken SIGINT or SIGTERM handling?

If rtla ignores SIGINT (or SIGTERM in the next case), wait "$pid" blocks for 30 seconds until rtla's -d duration timer expires via SIGALRM and removes the tracefs instance on normal timeout exit. By the time wait_gone() runs, the directory is already gone and the test reports PASS. Waiting for the instance or process to exit with a timeout shorter than -d (and killing the process if it is still alive) would verify that the signal actually stopped rtla.

[ ... ]

Quoted 5 lines
tools/tracing/rtla/tests/scripts/check-cleanup.sh new file +5 -0
+# ---------------------------------------------------------------------------
+# 4. shell action failure (-a / --on-threshold shell,command=...)
+# ---------------------------------------------------------------------------
+# -S is the stop-total threshold and yields exit 2 when the action fails.
+# A shell action only runs if that threshold is crossed; an idle machine may

tools/tracing/rtla/tests/scripts/check-cleanup.sh line 170

Quoted 10 lines
tools/tracing/rtla/tests/scripts/check-cleanup.sh new file +10 -0
+# never do so.  Do not call the no-threshold case a pass.
+cleanup_all
+action_rc=0
+for n in 1 2 3; do
+	$RTLA osnoise top -q -d 5s -P F:1 -r 900000 -S 1 \
+		--on-threshold 'shell,command="/bin/false"' >/dev/null 2>&1
+	rc=$?
+	action_rc=$rc
+	[ "$rc" -eq 2 ] && break
+done

[Severity: Medium]
Does this case actually test a failing --on-threshold shell action, and can other failure paths in check-cleanup.sh pass or skip silently?

In tools/tracing/rtla/src/common.c, common_threshold_handler() ignores the return value of actions_perform(), and run_tool() exits with FAILED (2) whenever tracing stops on -S without a subsequent --on-threshold continue action (as used in tests/osnoise.t), even if the shell command succeeds.

There are three related issues in the helper and wrapper:
- In the SIGINT, SIGTERM, and repeated-interrupt cases, wait "$pid" discards rtla's exit status, so if rtla hits a runtime error and removes its instance on the error path (exiting 1 instead of 0), the test still reports PASS.
- When $RTLA is missing, not executable (including when check-cleanup.sh is run standalone with default RTLA=rtla, since [ -x "rtla" ] checks ./rtla rather than PATH), or fails the --help probe, check-cleanup.sh logs SKIP:
and exits 0, which cleanup.t reports as a passing TAP skip instead of failing on a broken binary.
- check-cleanup.sh installs no EXIT/INT/TERM trap to kill $pid and run cleanup_all() if terminated by timeout, and cleanup.t does not print $out when the helper times out (exit codes 124 and 137), discarding the output showing which case hung.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2