Skip to contentSkip to conversation

Run stress-ng via rcutorture

RFC PATCH New Joel Fernandes (Google) 1 participant 6 messages Jan 24, 2025

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

cover Run stress-ng via rcutorture

These are initial patches I wrote to build and invoke stress-ng from rcutorture. Last year when I ran 'matrixprod', I could boost the code coverage in tree.c by ~5% (80 to 85%).

I am planning to run more RCU-strenous loads like stress-ng's 'dirmany' stressor and collect coverage. Also perhaps we can span more than one stressor in a default stress-ng invocation to add some variety and concurrency to the workloads.

Joel Fernandes (Google) (5):
rcutorture: kvm: Simplify invocation of mkinitrd.sh
rcutorture: Add a stress-ng build script
rcutorture: mkinitrd: Allow to run optional commands passed to it
rcutorture: mkinitrd: Use previous init.c to check if rebuild needed
rcutorture: kvm: Invoke stress-ng building it if necessary

 tools/testing/selftests/rcutorture/bin/kvm.sh | 39 +++++++--
 .../selftests/rcutorture/bin/mkinitrd.sh      | 59 +++++++++++--
 .../selftests/rcutorture/bin/mkstress-ng.sh   | 86 +++++++++++++++++++
 3 files changed, 171 insertions(+), 13 deletions(-)
 create mode 100755 tools/testing/selftests/rcutorture/bin/mkstress-ng.sh
Signature · 1 line
2.34.1
#1 lore raw

RFC PATCH 1/5 rcutorture: kvm: Simplify invocation of mkinitrd.sh

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

---
 tools/testing/selftests/rcutorture/bin/kvm.sh | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
tools/testing/selftests/rcutorture/bin/kvm.sh +1 -4
index 42e5e8597a1a..4766c3023fed 100755
@@ -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
Signature · 1 line
2.34.1
#2 lore raw

RFC PATCH 2/5 rcutorture: Add a stress-ng build script

In reply to Joel Fernandes (Google)

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.

---
 .../selftests/rcutorture/bin/mkstress-ng.sh   | 86 +++++++++++++++++++
 1 file changed, 86 insertions(+)
 create mode 100755 tools/testing/selftests/rcutorture/bin/mkstress-ng.sh
tools/testing/selftests/rcutorture/bin/mkstress-ng.sh new file +86 -0
new file mode 100755
index 000000000000..e5a19cca6923
@@ -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) <[email protected]>
+
+# 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
Signature · 1 line
2.34.1
#3 lore raw

RFC PATCH 3/5 rcutorture: mkinitrd: Allow to run optional commands passed to it

In reply to Joel Fernandes (Google)

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.

---
 .../selftests/rcutorture/bin/mkinitrd.sh      | 35 +++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)
tools/testing/selftests/rcutorture/bin/mkinitrd.sh +33 -2
index f3f867129560..4ba5e962e3cf 100755
@@ -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.
Signature · 1 line
2.34.1
#4 lore raw

RFC PATCH 4/5 rcutorture: mkinitrd: Use previous init.c to check if rebuild needed

In reply to Joel Fernandes (Google)

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.

---
 .../selftests/rcutorture/bin/mkinitrd.sh      | 34 +++++++++++++------
 1 file changed, 24 insertions(+), 10 deletions(-)
tools/testing/selftests/rcutorture/bin/mkinitrd.sh +24 -10
index 4ba5e962e3cf..d9fbfa205384 100755
@@ -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
Signature · 1 line
2.34.1
#5 lore raw

RFC PATCH 5/5 rcutorture: kvm: Invoke stress-ng building it if necessary

In reply to Joel Fernandes (Google)

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.

---
 tools/testing/selftests/rcutorture/bin/kvm.sh | 38 +++++++++++++++++--
 1 file changed, 34 insertions(+), 4 deletions(-)
tools/testing/selftests/rcutorture/bin/kvm.sh +34 -4
index 4766c3023fed..d35496247ee6 100755
@@ -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
Signature · 1 line
2.34.1