Linux Kernel Selftest development
 help / color / mirror / Atom feed
* [PATCH RFC 1/5] rcutorture: kvm: Simplify invocation of mkinitrd.sh
       [not found] <20250124015836.732086-1-joel@joelfernandes.org>
@ 2025-01-24  1:58 ` Joel Fernandes (Google)
  2025-01-24  1:58 ` [PATCH RFC 2/5] rcutorture: Add a stress-ng build script Joel Fernandes (Google)
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 5+ messages in thread
From: Joel Fernandes (Google) @ 2025-01-24  1:58 UTC (permalink / raw)
  To: linux-kernel, Paul E. McKenney, Josh Triplett, Steven Rostedt,
	Mathieu Desnoyers, Lai Jiangshan, Shuah Khan
  Cc: Joel Fernandes (Google), rcu, linux-kselftest

The else block is unnecessary and we can simply clarify the if condition
to remove the else clause. It is more readable.

Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
---
 tools/testing/selftests/rcutorture/bin/kvm.sh | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/tools/testing/selftests/rcutorture/bin/kvm.sh b/tools/testing/selftests/rcutorture/bin/kvm.sh
index 42e5e8597a1a..4766c3023fed 100755
--- a/tools/testing/selftests/rcutorture/bin/kvm.sh
+++ b/tools/testing/selftests/rcutorture/bin/kvm.sh
@@ -275,10 +275,7 @@ do
 	shift
 done
 
-if test -n "$dryrun" || test -z "$TORTURE_INITRD" || tools/testing/selftests/rcutorture/bin/mkinitrd.sh
-then
-	:
-else
+if test -z "$dryrun" && test -n "$TORTURE_INITRD" && !tools/testing/selftests/rcutorture/bin/mkinitrd.sh
 	echo No initrd and unable to create one, aborting test >&2
 	exit 1
 fi
-- 
2.34.1


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

* [PATCH RFC 2/5] rcutorture: Add a stress-ng build script
       [not found] <20250124015836.732086-1-joel@joelfernandes.org>
  2025-01-24  1:58 ` [PATCH RFC 1/5] rcutorture: kvm: Simplify invocation of mkinitrd.sh Joel Fernandes (Google)
@ 2025-01-24  1:58 ` Joel Fernandes (Google)
  2025-01-24  1:58 ` [PATCH RFC 3/5] rcutorture: mkinitrd: Allow to run optional commands passed to it Joel Fernandes (Google)
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 5+ messages in thread
From: Joel Fernandes (Google) @ 2025-01-24  1:58 UTC (permalink / raw)
  To: linux-kernel, Paul E. McKenney, Josh Triplett, Steven Rostedt,
	Mathieu Desnoyers, Lai Jiangshan, Shuah Khan
  Cc: Joel Fernandes (Google), rcu, linux-kselftest

This script automates the building of stress-ng and can be run from
any directory. It also support cross-compilation. The output is placed
into the initrd directory.

Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
---
 .../selftests/rcutorture/bin/mkstress-ng.sh   | 86 +++++++++++++++++++
 1 file changed, 86 insertions(+)
 create mode 100755 tools/testing/selftests/rcutorture/bin/mkstress-ng.sh

diff --git a/tools/testing/selftests/rcutorture/bin/mkstress-ng.sh b/tools/testing/selftests/rcutorture/bin/mkstress-ng.sh
new file mode 100755
index 000000000000..e5a19cca6923
--- /dev/null
+++ b/tools/testing/selftests/rcutorture/bin/mkstress-ng.sh
@@ -0,0 +1,86 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Clone and build the stress-ng tool, placing the binary in the
+# initrd directory. Ensure binary is up-to-date.
+#
+# Usage: ./bin/mkstress-ng.sh (run from any where).
+#
+# Copyright (C) Google LLC, 2024
+# Author: Joel Fernandes (Google) <joel@joelfernandes.org>
+
+# Get the directory where the script is located
+SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
+
+REPO_URL="https://github.com/ColinIanKing/stress-ng.git"
+SRC_DIR="${SCRIPT_DIR}/../res/stress-ng"
+INITRD_DIR="${SCRIPT_DIR}/../initrd"
+BIN_NAME="stress-ng"
+export COMPILER="${CROSS_COMPILE}gcc"
+
+is_statically_linked() {
+    if file "$1" | grep -q "statically linked"; then
+        return 0
+    else
+        return 1
+    fi
+}
+
+needs_build() {
+    if [ ! -f "$INITRD_DIR/$BIN_NAME" ]; then
+        return 0
+    fi
+    if [ "$(find "$SRC_DIR" -newer "$INITRD_DIR/$BIN_NAME")" ]; then
+        return 0
+    fi
+    if ! is_statically_linked "$INITRD_DIR/$BIN_NAME"; then
+        return 0
+    fi
+    return 1
+}
+
+if [ ! -d "$INITRD_DIR" ]; then
+    echo "Error: INITRD_DIR ($INITRD_DIR) does not exist"
+    exit 1
+fi
+
+if ! which "$COMPILER" &> /dev/null; then
+    echo "Error: Compiler $COMPILER not found."
+    exit 1
+fi
+
+if [ ! -d "$SRC_DIR" ]; then
+    echo "Cloning stress-ng repository..."
+    if ! git clone "$REPO_URL" "$SRC_DIR"; then
+        echo "Failed to clone stress-ng repository."
+        rm -rf "$SRC_DIR"
+        exit 1
+    fi
+else
+    echo "Updating stress-ng repository..."
+    cd "$SRC_DIR" || exit 1
+    git pull || { echo "Failed to update stress-ng repository"; exit 1; }
+    cd - > /dev/null || exit 1
+fi
+
+# Build stress-ng binary if needed
+if needs_build; then
+    echo "Building stress-ng binary..."
+    cd "$SRC_DIR" || exit 1
+    STATIC=1 make -j 8 || { echo "stress-ng build failed"; exit 1; }
+    cd - > /dev/null || exit 1
+
+    # Verify the stress-ng binary is static
+    if ! is_statically_linked "$SRC_DIR/$BIN_NAME"; then
+        echo "Error: The stress-ng binary is not statically linked."
+        exit 1
+    fi
+
+    echo "Copying stress-ng binary to initrd directory..."
+    cp "$SRC_DIR/$BIN_NAME" "$INITRD_DIR" || { echo "Failed to copy stress-ng binary"; exit 1; }
+else
+    echo "stress-ng binary is up-to-date, no build needed."
+fi
+
+echo "stress-ng build process completed successfully."
+exit 0
\ No newline at end of file
-- 
2.34.1


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

* [PATCH RFC 3/5] rcutorture: mkinitrd: Allow to run optional commands passed to it
       [not found] <20250124015836.732086-1-joel@joelfernandes.org>
  2025-01-24  1:58 ` [PATCH RFC 1/5] rcutorture: kvm: Simplify invocation of mkinitrd.sh Joel Fernandes (Google)
  2025-01-24  1:58 ` [PATCH RFC 2/5] rcutorture: Add a stress-ng build script Joel Fernandes (Google)
@ 2025-01-24  1:58 ` Joel Fernandes (Google)
  2025-01-24  1:58 ` [PATCH RFC 4/5] rcutorture: mkinitrd: Use previous init.c to check if rebuild needed Joel Fernandes (Google)
  2025-01-24  1:58 ` [PATCH RFC 5/5] rcutorture: kvm: Invoke stress-ng building it if necessary Joel Fernandes (Google)
  4 siblings, 0 replies; 5+ messages in thread
From: Joel Fernandes (Google) @ 2025-01-24  1:58 UTC (permalink / raw)
  To: linux-kernel, Paul E. McKenney, Josh Triplett, Steven Rostedt,
	Mathieu Desnoyers, Lai Jiangshan, Shuah Khan
  Cc: Joel Fernandes (Google), rcu, linux-kselftest

Embed commands to invoke into init.c via mkinitrd.sh args. This
allows init to spawn a child process running the command with the
required arguments.

Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
---
 .../selftests/rcutorture/bin/mkinitrd.sh      | 35 +++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/rcutorture/bin/mkinitrd.sh b/tools/testing/selftests/rcutorture/bin/mkinitrd.sh
index f3f867129560..4ba5e962e3cf 100755
--- a/tools/testing/selftests/rcutorture/bin/mkinitrd.sh
+++ b/tools/testing/selftests/rcutorture/bin/mkinitrd.sh
@@ -2,6 +2,9 @@
 # SPDX-License-Identifier: GPL-2.0+
 #
 # Create an initrd directory if one does not already exist.
+# Usage: mkinitrd.sh [command [args...]]
+# Example: mkinitrd.sh stress-ng --cpu 1 --cpu-method matrixprod --cpu-ops 1000000 --perf -t 5
+# Note that command/args are optional.
 #
 # Copyright (C) IBM Corporation, 2013
 #
@@ -25,7 +28,9 @@ echo "Creating a statically linked C-language initrd"
 cd $D
 mkdir -p initrd
 cd initrd
-cat > init.c << '___EOF___'
+
+# Generate the init.c with optional command
+cat > init.c << 'EOF_HEAD'
 #ifndef NOLIBC
 #include <unistd.h>
 #include <sys/time.h>
@@ -33,6 +38,29 @@ cat > init.c << '___EOF___'
 
 volatile unsigned long delaycount;
 
+void run_optional_command() {
+EOF_HEAD
+
+if [ $# -gt 0 ]; then
+    # If command provided, generate run_optional_command() with the specified command.
+    # We use printf to generate the command and args.
+    # Example: echo $(printf '"%s", ' cmd a1 a2) gives: "cmd", "a1", "a2",
+    cat >> init.c << EOF
+    pid_t pid = fork();
+    if (pid == 0) {
+        char *args[] = {$(printf '"%s", ' "$@")NULL};
+        execve(args[0], args, NULL);
+    }
+EOF
+else
+    # If no command provided, function will be empty
+    echo "    /* No command specified */" >> init.c
+fi
+
+# Add the rest of the program
+cat >> init.c << 'EOF_TAIL'
+}
+
 int main(int argc, char *argv[])
 {
 	int i;
@@ -43,6 +71,9 @@ int main(int argc, char *argv[])
 	for (i = 0; i < argc; i++)
 		printf(" %s", argv[i]);
 	printf("\n");
+
+	run_optional_command();
+
 	for (;;) {
 		sleep(1);
 		/* Need some userspace time. */
@@ -62,7 +93,7 @@ int main(int argc, char *argv[])
 	}
 	return 0;
 }
-___EOF___
+EOF_TAIL
 
 # build using nolibc on supported archs (smaller executable) and fall
 # back to regular glibc on other ones.
-- 
2.34.1


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

* [PATCH RFC 4/5] rcutorture: mkinitrd: Use previous init.c to check if rebuild needed
       [not found] <20250124015836.732086-1-joel@joelfernandes.org>
                   ` (2 preceding siblings ...)
  2025-01-24  1:58 ` [PATCH RFC 3/5] rcutorture: mkinitrd: Allow to run optional commands passed to it Joel Fernandes (Google)
@ 2025-01-24  1:58 ` Joel Fernandes (Google)
  2025-01-24  1:58 ` [PATCH RFC 5/5] rcutorture: kvm: Invoke stress-ng building it if necessary Joel Fernandes (Google)
  4 siblings, 0 replies; 5+ messages in thread
From: Joel Fernandes (Google) @ 2025-01-24  1:58 UTC (permalink / raw)
  To: linux-kernel, Paul E. McKenney, Josh Triplett, Steven Rostedt,
	Mathieu Desnoyers, Lai Jiangshan, Shuah Khan
  Cc: Joel Fernandes (Google), rcu, linux-kselftest

Create the init.c as a temporary init-tmp.c file, even if init already
exists.  Ensure the existing init.c matches this. If init.c doesn't
exist in initrd/ then skip the check and assume init-tmp.c as the new
init.c.  After that, check if init binary already exists as we did
before and skip the build if it does.

This does mean we keep a copy of init.c around in the initrd but this is
a small file and it is useful to see for debugging anyway.

Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
---
 .../selftests/rcutorture/bin/mkinitrd.sh      | 34 +++++++++++++------
 1 file changed, 24 insertions(+), 10 deletions(-)

diff --git a/tools/testing/selftests/rcutorture/bin/mkinitrd.sh b/tools/testing/selftests/rcutorture/bin/mkinitrd.sh
index 4ba5e962e3cf..d9fbfa205384 100755
--- a/tools/testing/selftests/rcutorture/bin/mkinitrd.sh
+++ b/tools/testing/selftests/rcutorture/bin/mkinitrd.sh
@@ -17,10 +17,6 @@ if [ ! -d "$D" ]; then
     echo >&2 "$D does not exist: Malformed kernel source tree?"
     exit 1
 fi
-if [ -s "$D/initrd/init" ]; then
-    echo "$D/initrd/init already exists, no need to create it"
-    exit 0
-fi
 
 # Create a C-language initrd/init infinite-loop program and statically
 # link it.  This results in a very small initrd.
@@ -29,8 +25,10 @@ cd $D
 mkdir -p initrd
 cd initrd
 
-# Generate the init.c with optional command
-cat > init.c << 'EOF_HEAD'
+# Generate an init-tmp.c with optional command. This will then be compared
+# with any existing init.c. The reason for this is, we want to force a
+# rebuild if the optional command or command line arguments have changed.
+cat > init-tmp.c << 'EOF_HEAD'
 #ifndef NOLIBC
 #include <unistd.h>
 #include <sys/time.h>
@@ -45,7 +43,7 @@ if [ $# -gt 0 ]; then
     # If command provided, generate run_optional_command() with the specified command.
     # We use printf to generate the command and args.
     # Example: echo $(printf '"%s", ' cmd a1 a2) gives: "cmd", "a1", "a2",
-    cat >> init.c << EOF
+    cat >> init-tmp.c << EOF
     pid_t pid = fork();
     if (pid == 0) {
         char *args[] = {$(printf '"%s", ' "$@")NULL};
@@ -54,11 +52,11 @@ if [ $# -gt 0 ]; then
 EOF
 else
     # If no command provided, function will be empty
-    echo "    /* No command specified */" >> init.c
+    echo "    /* No command specified */" >> init-tmp.c
 fi
 
 # Add the rest of the program
-cat >> init.c << 'EOF_TAIL'
+cat >> init-tmp.c << 'EOF_TAIL'
 }
 
 int main(int argc, char *argv[])
@@ -95,6 +93,23 @@ int main(int argc, char *argv[])
 }
 EOF_TAIL
 
+# Check if init.c exists and compare with init-tmp.c
+if [ -f "init.c" ]; then
+    if ! cmp -s "init.c" "init-tmp.c"; then
+        mv "init-tmp.c" "init.c"
+    else
+        rm "init-tmp.c"
+    fi
+else
+    mv "init-tmp.c" "init.c"
+fi
+
+# Now check if init binary exists and is up to date
+if [ -s "init" ] && [ "init" -nt "init.c" ]; then
+    echo "$D/initrd/init already exists and is up to date"
+    exit 0
+fi
+
 # build using nolibc on supported archs (smaller executable) and fall
 # back to regular glibc on other ones.
 if echo -e "#if __x86_64__||__i386__||__i486__||__i586__||__i686__" \
@@ -120,7 +135,6 @@ then
 	exit "$ret"
 fi
 
-rm init.c
 echo "Done creating a statically linked C-language initrd"
 
 exit 0
-- 
2.34.1


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

* [PATCH RFC 5/5] rcutorture: kvm: Invoke stress-ng building it if necessary
       [not found] <20250124015836.732086-1-joel@joelfernandes.org>
                   ` (3 preceding siblings ...)
  2025-01-24  1:58 ` [PATCH RFC 4/5] rcutorture: mkinitrd: Use previous init.c to check if rebuild needed Joel Fernandes (Google)
@ 2025-01-24  1:58 ` Joel Fernandes (Google)
  4 siblings, 0 replies; 5+ messages in thread
From: Joel Fernandes (Google) @ 2025-01-24  1:58 UTC (permalink / raw)
  To: linux-kernel, Paul E. McKenney, Josh Triplett, Steven Rostedt,
	Mathieu Desnoyers, Lai Jiangshan, Shuah Khan
  Cc: Joel Fernandes (Google), rcu, linux-kselftest

Invoke stress-ng from init using new --stress-ng and optional
--stress-ng-args. Default --stress-ng-args are used if none are
provided. Not passing --stress-ng does not change the behavior of kvm.sh
from before.

Signed-off-by: Joel Fernandes (Google) <joel@joelfernandes.org>
---
 tools/testing/selftests/rcutorture/bin/kvm.sh | 38 +++++++++++++++++--
 1 file changed, 34 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/rcutorture/bin/kvm.sh b/tools/testing/selftests/rcutorture/bin/kvm.sh
index 4766c3023fed..d35496247ee6 100755
--- a/tools/testing/selftests/rcutorture/bin/kvm.sh
+++ b/tools/testing/selftests/rcutorture/bin/kvm.sh
@@ -42,13 +42,15 @@ TORTURE_JITTER_STOP=""
 TORTURE_KCONFIG_KASAN_ARG=""
 TORTURE_KCONFIG_KCSAN_ARG=""
 TORTURE_KMAKE_ARG=""
+TORTURE_MOD=rcutorture
 TORTURE_NO_AFFINITY=""
 TORTURE_QEMU_MEM=512
 torture_qemu_mem_default=1
 TORTURE_REMOTE=
 TORTURE_SHUTDOWN_GRACE=180
+TORTURE_STRESS_NG=
+TORTURE_STRESS_NG_DEFAULT_ARGS="--cpu 1 --cpu-method matrixprod --cpu-ops 1000000 --perf -t 5"
 TORTURE_SUITE=rcu
-TORTURE_MOD=rcutorture
 TORTURE_TRUST_MAKE=""
 debuginfo="CONFIG_DEBUG_INFO_NONE=n CONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT=y"
 resdir=""
@@ -90,6 +92,8 @@ usage () {
 	echo "       --remote"
 	echo "       --results absolute-pathname"
 	echo "       --shutdown-grace seconds"
+	echo "       --stress-ng"
+	echo "       --stress-ng-args \"stress-ng arguments\""
 	echo "       --torture lock|rcu|rcuscale|refscale|scf|X*"
 	echo "       --trust-make"
 	exit 1
@@ -251,6 +255,14 @@ do
 		TORTURE_SHUTDOWN_GRACE=$2
 		shift
 		;;
+	--stress-ng)
+		TORTURE_STRESS_NG=1
+		;;
+	--stress-ng-args)
+		checkarg --stress-ng-args "(stress-ng arguments)" "$#" "$2" '.*' '^error'
+		TORTURE_STRESS_NG_DEFAULT_ARGS="$2"
+		shift
+		;;
 	--torture)
 		checkarg --torture "(suite name)" "$#" "$2" '^\(lock\|rcu\|rcuscale\|refscale\|scf\|X.*\)$' '^--'
 		TORTURE_SUITE=$2
@@ -275,9 +287,27 @@ do
 	shift
 done
 
-if test -z "$dryrun" && test -n "$TORTURE_INITRD" && !tools/testing/selftests/rcutorture/bin/mkinitrd.sh
-	echo No initrd and unable to create one, aborting test >&2
-	exit 1
+if test -n "$TORTURE_STRESS_NG"
+then
+	if ! "$RCUTORTURE/bin/mkstress-ng.sh"
+	then
+		echo "Failed to build stress-ng, aborting test" >&2
+		exit 1
+	fi
+fi
+
+if test -z "$dryrun" && test -n "$TORTURE_INITRD"
+then
+	stress_args=""
+	if test -n "$TORTURE_STRESS_NG"
+	then
+		stress_args="stress-ng $TORTURE_STRESS_NG_DEFAULT_ARGS"
+	fi
+	if ! "$RCUTORTURE/bin/mkinitrd.sh" $stress_args
+	then
+		echo "No initrd and unable to create one, aborting test" >&2
+		exit 1
+	fi
 fi
 
 CONFIGFRAG=${RCUTORTURE}/configs/${TORTURE_SUITE}; export CONFIGFRAG
-- 
2.34.1


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

end of thread, other threads:[~2025-01-24  1:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20250124015836.732086-1-joel@joelfernandes.org>
2025-01-24  1:58 ` [PATCH RFC 1/5] rcutorture: kvm: Simplify invocation of mkinitrd.sh Joel Fernandes (Google)
2025-01-24  1:58 ` [PATCH RFC 2/5] rcutorture: Add a stress-ng build script Joel Fernandes (Google)
2025-01-24  1:58 ` [PATCH RFC 3/5] rcutorture: mkinitrd: Allow to run optional commands passed to it Joel Fernandes (Google)
2025-01-24  1:58 ` [PATCH RFC 4/5] rcutorture: mkinitrd: Use previous init.c to check if rebuild needed Joel Fernandes (Google)
2025-01-24  1:58 ` [PATCH RFC 5/5] rcutorture: kvm: Invoke stress-ng building it if necessary Joel Fernandes (Google)

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