Repository: nodejs/node
Pull request: 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.

<!--
Before submitting a pull request, please read:

- the CONTRIBUTING guide at https://github.com/nodejs/node/blob/HEAD/CONTRIBUTING.md
- the commit message formatting guidelines at
  https://github.com/nodejs/node/blob/HEAD/doc/contributing/pull-requests.md#commit-message-guidelines

For code changes:
1. Include tests for any bug fixes or new features.
2. Update documentation if relevant.
3. Ensure that `make -j4 test` (UNIX), or `vcbuild test` (Windows) passes.

If you believe this PR should be highlighted in the Node.js CHANGELOG
please add the `notable-change` label.

Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.
-->


Changed files:
filename: benchmark/cpu.sh
patch: @@ -4,21 +4,73 @@ CPUPATH=/sys/devices/system/cpu
 
 MAXID=$(cat $CPUPATH/present | awk -F- '{print $NF}')
 
+[ "$(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() {
+  case "$(cat "$CPUPATH/cpu0/cpufreq/scaling_available_governors")" in
+    *"schedutil"*)    echo "schedutil"   ;;
+    *"ondemand"*)     echo "ondemand"    ;;
+    *"conservative"*) echo "conservative";;
+    *)                echo "powersave"   ;;
+  esac
+
+}
+
 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