public inbox for linux-perf-users@vger.kernel.org
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	 Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	 Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>,  Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	 James Clark <james.clark@linaro.org>,
	linux-kernel@vger.kernel.org,  linux-perf-users@vger.kernel.org
Subject: [PATCH v1] perf tests: top/kvm don't depend on coreutils timeout
Date: Mon, 13 Apr 2026 14:33:00 -0700	[thread overview]
Message-ID: <20260413213300.1700116-1-irogers@google.com> (raw)

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


             reply	other threads:[~2026-04-13 21:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-13 21:33 Ian Rogers [this message]
2026-04-13 21:44 ` [PATCH v1] perf tests: top/kvm don't depend on coreutils timeout sashiko-bot

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=20260413213300.1700116-1-irogers@google.com \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /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