Skip to content

Commit bb27766

Browse files
meteorqz6aduh95
authored andcommitted
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. PR-URL: #60162 Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 2e55c6a commit bb27766

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

benchmark/cpu.sh

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,73 @@ CPUPATH=/sys/devices/system/cpu
44

55
MAXID=$(cat $CPUPATH/present | awk -F- '{print $NF}')
66

7+
[ "$(uname -s || true)" = "Linux" ] || \
8+
echo "Warning: This script supports Linux only." >&2
9+
10+
[ "$(id -u || true)" = "0" ] || \
11+
echo "Warning: This script typically needs root access to modify CPU governor. Consider running it with sudo." >&2
12+
13+
get_default_governor() {
14+
case "$(cat "$CPUPATH/cpu0/cpufreq/scaling_available_governors")" in
15+
*"schedutil"*) echo "schedutil" ;;
16+
*"ondemand"*) echo "ondemand" ;;
17+
*"conservative"*) echo "conservative";;
18+
*) echo "powersave" ;;
19+
esac
20+
21+
}
22+
723
set_governor() {
8-
echo "Setting CPU frequency governor to \"$1\""
24+
governor_name="$1"
25+
26+
echo "Setting governor for all CPU cores to \"$governor_name\"..."
27+
928
i=0
1029
while [ "$i" -le "$MAXID" ]; do
1130
echo "$1" > "$CPUPATH/cpu$i/cpufreq/scaling_governor"
1231
i=$((i + 1))
1332
done
33+
34+
echo "Done."
35+
}
36+
37+
usage() {
38+
default_gov=$(get_default_governor)
39+
echo "CPU Governor Management Script"
40+
echo "----------------------------------------------------------------------------"
41+
echo "Usage: $0 [command]"
42+
echo
43+
echo "Commands:"
44+
echo " fast Sets the governor to 'performance' for maximum speed."
45+
echo " (Warning: Increases heat/power use. Use for short-term tasks.)"
46+
echo
47+
echo " reset Resets the governor to the system's recommended default ('$default_gov')."
48+
echo
49+
echo " get Shows the current CPU governor for ALL cores."
50+
echo "----------------------------------------------------------------------------"
1451
}
1552

1653
case "$1" in
1754
fast | performance)
55+
echo "Warning: The 'performance' mode locks the CPU at its highest speed."
56+
echo "It is highly recommended to 'reset' after your task is complete."
1857
set_governor "performance"
1958
;;
59+
60+
reset | default)
61+
default_governor=$(get_default_governor)
62+
set_governor "$default_governor"
63+
;;
64+
65+
get | status)
66+
echo "Current governor status for all cores:"
67+
grep . "$CPUPATH"/cpu*/cpufreq/scaling_governor
68+
;;
69+
2070
*)
21-
echo "Usage: $0 fast"
71+
usage
2272
exit 1
2373
;;
2474
esac
75+
76+
exit 0

0 commit comments

Comments
 (0)