From 1de914c310c16abbcdeddcf90189922ff1b277fc Mon Sep 17 00:00:00 2001 From: meteorqz6 Date: Wed, 8 Oct 2025 16:10:47 +0900 Subject: [PATCH 1/4] benchmark: improve cpu.sh for safety and usability The previous cpu.sh script was minimal. This change makes it a more robust and safe utility for managing CPU governors during benchmarks. The script now includes: - Checks to ensure it only runs on Linux with root privileges. - A `reset` command to restore the CPU governor to a dynamically detected system default. - A `get` command to check the current governor for all cores. - An improved usage guide and clearer feedback messages. --- benchmark/cpu.sh | 64 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/benchmark/cpu.sh b/benchmark/cpu.sh index 9c9dd7fa4ddc58..76579202c6c2f0 100755 --- a/benchmark/cpu.sh +++ b/benchmark/cpu.sh @@ -4,21 +4,81 @@ CPUPATH=/sys/devices/system/cpu MAXID=$(cat $CPUPATH/present | awk -F- '{print $NF}') +if [ "$(uname -s)" != "Linux" ]; then + echo "Error: This script runs on Linux only." >&2 + exit 1 +fi + +if [ "$(id -u)" -ne 0 ]; then + echo "Error: Run as root (sudo) to modify CPU governor." >&2 + exit 1 +fi + +get_default_governor() { + available_governors=$(cat "$CPUPATH/cpu0/cpufreq/scaling_available_governors") + + if echo "$available_governors" | grep -q "schedutil"; then + echo "schedutil" + elif echo "$available_governors" | grep -q "ondemand"; then + echo "ondemand" + elif echo "$available_governors" | grep -q "conservative"; then + echo "conservative" + else + echo "powersave" + fi +} + set_governor() { - echo "Setting CPU frequency governor to \"$1\"" + governor_name="$1" + + echo "Setting governor for all CPU cores to \"$governor_name\"..." + i=0 while [ "$i" -le "$MAXID" ]; do echo "$1" > "$CPUPATH/cpu$i/cpufreq/scaling_governor" i=$((i + 1)) done + + echo "Done." +} + +usage() { + default_gov=$(get_default_governor) + echo "CPU Governor Management Script" + echo "----------------------------------------------------------------------------" + echo "Usage: $0 [command]" + echo + echo "Commands:" + echo " fast Sets the governor to 'performance' for maximum speed." + echo " (Warning: Increases heat/power use. Use for short-term tasks.)" + echo + echo " reset Resets the governor to the system's recommended default ('$default_gov')." + echo + echo " get Shows the current CPU governor for ALL cores." + echo "----------------------------------------------------------------------------" } case "$1" in fast | performance) + echo "Warning: The 'performance' mode locks the CPU at its highest speed." + echo "It is highly recommended to 'reset' after your task is complete." set_governor "performance" ;; + + reset | default) + default_governor=$(get_default_governor) + set_governor "$default_governor" + ;; + + get | status) + echo "Current governor status for all cores:" + grep . "$CPUPATH"/cpu*/cpufreq/scaling_governor + ;; + *) - echo "Usage: $0 fast" + usage exit 1 ;; esac + +exit 0 From 33a28d38deac96035300632631ffcaa01547e54c Mon Sep 17 00:00:00 2001 From: meteorqz6 Date: Wed, 8 Oct 2025 16:25:29 +0900 Subject: [PATCH 2/4] benchmark: fix shell script lint errors --- benchmark/cpu.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/benchmark/cpu.sh b/benchmark/cpu.sh index 76579202c6c2f0..e118917ef00c7b 100755 --- a/benchmark/cpu.sh +++ b/benchmark/cpu.sh @@ -4,12 +4,12 @@ CPUPATH=/sys/devices/system/cpu MAXID=$(cat $CPUPATH/present | awk -F- '{print $NF}') -if [ "$(uname -s)" != "Linux" ]; then +if [ "$(uname -s || true)" != "Linux" ]; then echo "Error: This script runs on Linux only." >&2 exit 1 fi -if [ "$(id -u)" -ne 0 ]; then +if [ "$(id -u || true)" -ne 0 ]; then echo "Error: Run as root (sudo) to modify CPU governor." >&2 exit 1 fi From f3ef12a7a528033276dc6b1cbe95892d11091922 Mon Sep 17 00:00:00 2001 From: Nam Yooseong <102887277+meteorqz6@users.noreply.github.com> Date: Sat, 11 Oct 2025 21:19:03 +0900 Subject: [PATCH 3/4] Update benchmark/cpu.sh Co-authored-by: Antoine du Hamel --- benchmark/cpu.sh | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/benchmark/cpu.sh b/benchmark/cpu.sh index e118917ef00c7b..96ab2076921a95 100755 --- a/benchmark/cpu.sh +++ b/benchmark/cpu.sh @@ -4,15 +4,11 @@ CPUPATH=/sys/devices/system/cpu MAXID=$(cat $CPUPATH/present | awk -F- '{print $NF}') -if [ "$(uname -s || true)" != "Linux" ]; then - echo "Error: This script runs on Linux only." >&2 - exit 1 -fi - -if [ "$(id -u || true)" -ne 0 ]; then - echo "Error: Run as root (sudo) to modify CPU governor." >&2 - exit 1 -fi +[ "$(uname -s || true)" = "Linux" ] || \ + echo "Warning: This script supports Linux only." >&2 + +[ "$(id -u || true)" = "0" ] || \ + echo "Warning: This script typically needs root access to modify CPU governor. Consider running it with sudo." >&2 get_default_governor() { available_governors=$(cat "$CPUPATH/cpu0/cpufreq/scaling_available_governors") From 9caf515327438989a8e97ea8c1884213412d0bbb Mon Sep 17 00:00:00 2001 From: Nam Yooseong <102887277+meteorqz6@users.noreply.github.com> Date: Thu, 23 Oct 2025 22:08:03 +0900 Subject: [PATCH 4/4] Update benchmark/cpu.sh Co-authored-by: Antoine du Hamel --- benchmark/cpu.sh | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/benchmark/cpu.sh b/benchmark/cpu.sh index 96ab2076921a95..7e35c0ef0de8c8 100755 --- a/benchmark/cpu.sh +++ b/benchmark/cpu.sh @@ -11,17 +11,13 @@ MAXID=$(cat $CPUPATH/present | awk -F- '{print $NF}') echo "Warning: This script typically needs root access to modify CPU governor. Consider running it with sudo." >&2 get_default_governor() { - available_governors=$(cat "$CPUPATH/cpu0/cpufreq/scaling_available_governors") - - if echo "$available_governors" | grep -q "schedutil"; then - echo "schedutil" - elif echo "$available_governors" | grep -q "ondemand"; then - echo "ondemand" - elif echo "$available_governors" | grep -q "conservative"; then - echo "conservative" - else - echo "powersave" - fi + case "$(cat "$CPUPATH/cpu0/cpufreq/scaling_available_governors")" in + *"schedutil"*) echo "schedutil" ;; + *"ondemand"*) echo "ondemand" ;; + *"conservative"*) echo "conservative";; + *) echo "powersave" ;; + esac + } set_governor() {