public inbox for linux-perf-users@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1] perf tests: top/kvm don't depend on coreutils timeout
@ 2026-04-13 21:33 Ian Rogers
  2026-04-13 21:44 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Ian Rogers @ 2026-04-13 21:33 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Alexander Shishkin, Jiri Olsa, Ian Rogers,
	Adrian Hunter, James Clark, linux-kernel, linux-perf-users

If the coreutils timeout command is missing on a minimal test system
then the kvm and top tests can fail. Provide a fallback bash
implementation for these systems.

Assisted-by: Gemini:gemini-3.1-pro-preview
Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/tests/shell/kvm.sh         |  6 ++++-
 tools/perf/tests/shell/lib/timeout.sh | 36 +++++++++++++++++++++++++++
 tools/perf/tests/shell/top.sh         |  7 +++++-
 3 files changed, 47 insertions(+), 2 deletions(-)
 create mode 100644 tools/perf/tests/shell/lib/timeout.sh

diff --git a/tools/perf/tests/shell/kvm.sh b/tools/perf/tests/shell/kvm.sh
index f88e859025c4..a27b3d6414e9 100755
--- a/tools/perf/tests/shell/kvm.sh
+++ b/tools/perf/tests/shell/kvm.sh
@@ -4,6 +4,9 @@
 
 set -e
 
+# shellcheck source=lib/timeout.sh
+. "$(dirname $0)"/lib/timeout.sh
+
 err=0
 perfdata=$(mktemp /tmp/__perf_kvm_test.perf.data.XXXXX)
 qemu_pid_file=$(mktemp /tmp/__perf_kvm_test.qemu.pid.XXXXX)
@@ -102,7 +105,8 @@ test_kvm_stat_live() {
 
         # Run perf kvm live for 5 seconds, monitoring that PID
 	# Use sleep to keep stdin open but silent, preventing EOF loop or interactive spam
-	if ! sleep 10 | timeout 5s perf kvm stat live -p "${qemu_pid}" > "${log_file}" 2>&1; then
+	if ! sleep 10 | bash_timeout 5s perf kvm stat live -p "${qemu_pid}" > "${log_file}" 2>&1
+	then
 		retval=$?
 		if [ $retval -ne 124 ] && [ $retval -ne 0 ]; then
 			echo "perf kvm stat live [Failed: perf kvm stat live failed to start or run (ret=$retval)]"
diff --git a/tools/perf/tests/shell/lib/timeout.sh b/tools/perf/tests/shell/lib/timeout.sh
new file mode 100644
index 000000000000..b4d2c0acf0e2
--- /dev/null
+++ b/tools/perf/tests/shell/lib/timeout.sh
@@ -0,0 +1,36 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+# A version of timeout in pure bash that doesn't require the timeout command.
+bash_timeout() {
+    # Check if the native 'timeout' command is available
+    if command -v timeout >/dev/null 2>&1; then
+        timeout "$@"
+        return $?
+    fi
+
+    local timeout_secs="$1"
+    shift
+    local command=("$@")
+
+    # Execute the target command in the background
+    "${command[@]}" &
+    local cmd_pid=$!
+
+    # Start a watcher process in the background
+    (
+        sleep "$timeout_secs"
+        kill -TERM "$cmd_pid" 2>/dev/null || true
+    ) &
+    local watcher_pid=$!
+
+    # Wait for the main command to complete (or be killed)
+    wait "$cmd_pid" 2>/dev/null
+    local exit_code=$?
+
+    # Clean up: Kill the watcher process if the main command finished early
+    kill "$watcher_pid" 2>/dev/null || true
+
+    # Return the exit status of the target command
+    return $exit_code
+}
diff --git a/tools/perf/tests/shell/top.sh b/tools/perf/tests/shell/top.sh
index ad7fccd09025..7741bca6b41f 100755
--- a/tools/perf/tests/shell/top.sh
+++ b/tools/perf/tests/shell/top.sh
@@ -4,6 +4,9 @@
 
 set -e
 
+# shellcheck source=lib/timeout.sh
+. "$(dirname $0)"/lib/timeout.sh
+
 err=0
 log_file=$(mktemp /tmp/perf.top.log.XXXXX)
 
@@ -35,7 +38,9 @@ test_basic_perf_top() {
 	# Use -d 1 to avoid flooding output
 	# Use -e cpu-clock to ensure we get samples
 	# Use sleep to keep stdin open but silent, preventing EOF loop or interactive spam
-	if ! sleep 10 | timeout 5s perf top --stdio -d 1 -e cpu-clock -p $PID > "${log_file}" 2>&1; then
+	if ! sleep 10 | \
+	     bash_timeout 5s perf top --stdio -d 1 -e cpu-clock -p $PID > "${log_file}" 2>&1
+	then
 		retval=$?
 		if [ $retval -ne 124 ] && [ $retval -ne 0 ]; then
 			echo "Basic perf top test [Failed: perf top failed to start or run (ret=$retval)]"
-- 
2.54.0.rc0.605.g598a273b03-goog


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

end of thread, other threads:[~2026-04-13 21:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-13 21:33 [PATCH v1] perf tests: top/kvm don't depend on coreutils timeout Ian Rogers
2026-04-13 21:44 ` sashiko-bot

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