From d43da88de72dc2716d63c41fe5bd4e317c4e9ca7 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Thu, 4 Jan 2024 15:07:31 +0100 Subject: [cpugovernor] Update script, handle different drivers --- .../data/opt/openslx/scripts/systemd-cpu_governor | 83 +++++++++------------- 1 file changed, 33 insertions(+), 50 deletions(-) diff --git a/core/modules/cpugovernor/data/opt/openslx/scripts/systemd-cpu_governor b/core/modules/cpugovernor/data/opt/openslx/scripts/systemd-cpu_governor index f7aa255a..cdde82d5 100755 --- a/core/modules/cpugovernor/data/opt/openslx/scripts/systemd-cpu_governor +++ b/core/modules/cpugovernor/data/opt/openslx/scripts/systemd-cpu_governor @@ -7,56 +7,39 @@ . /opt/openslx/config || \ { echo "ERROR: Could not source /opt/openslx/config."; exit 1; } -# set the governor to the one given in SLX_GOVERNOR -TARGET_GOVERNOR="" -if [ -n "$SLX_GOVERNOR" ]; then - TARGET_GOVERNOR="$SLX_GOVERNOR" -else - # use 'ondemand' per default - TARGET_GOVERNOR="ondemand" -fi -echo "Trying to set CPU governor to $TARGET_GOVERNOR" -# global information needed -# CORES is the range of cores present, on bwpc4 it has the value '0-3' -# thus the split: MINCORE=0 MAXCORE=3 -CORES="$(cat /sys/devices/system/cpu/present)" -MINCORE="$(echo $CORES | awk -F "-" '{print $1}')" -MAXCORE="$(echo $CORES | awk -F "-" '{print $2}')" +# if scaling_driver is acpi-cpufreq: +# cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors +# conservative ondemand userspace powersave performance schedutil +# -> use ondemand and performance +# if scaling_driver is intel_cpufreq: +# cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors +# conservative ondemand userspace powersave performance schedutil +# -> same as acpi +# if scaling_driver is intel_pstate: +# cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors +# performance powersave +# -> we only got two choices anyways :) +# if scaling_driver is amd_pstate: +# haven't seen this yet, relatively new as of 2024, but it should work +# almost the same as intel_pstate, i.e. only performance and powersave -# Helper function 'test_for_gov' -# Usage: -# test_for_gov -# Example: -# test_for_gov "ondemand" -# Return 0 if it is supported by all cpus, 1 otherwise -test_for_gov() { - # if no argument is given, print error and exit (yes exit the whole script!) - [ $# -ne 1 ] && echo "Usage: test_for_gov . No arguments given!" && exit 1 - local GOVERNOR="$1" - - # check for each cpu just to be safe - # ash-style loop .... - local i=$MINCORE - while [ $i -le $MAXCORE ]; do - # check if the given governor is supported - grep -q "${GOVERNOR}" /sys/devices/system/cpu/cpu${i}/cpufreq/scaling_available_governors 2>/dev/null || return 1; - # increment - true $(( i++ )) - done - return 0; -} -# now actually test the cpus for the 'ondemand' cpu governor -if test_for_gov "${TARGET_GOVERNOR}"; then - # ok, so now set the governor to 'ondemand' for all cores - i=$MINCORE - while [ $i -le $MAXCORE ]; do - if ! echo "${TARGET_GOVERNOR}" > /sys/devices/system/cpu/cpu${i}/cpufreq/scaling_governor; then - echo "ERROR: Could not set the cpu governor to '${TARGET_GOVERNOR}'!" - exit 1 - fi - true $(( i++ )) - done +# set the governor. Prioritize command-line argument, then SLX_GOVERNOR, +# fall back to powersave. +if [ -n "$1" ]; then + wanted_gov="$1" +elif [ -n "$SLX_GOVERNOR" ]; then + wanted_gov="$SLX_GOVERNOR" else - echo "ERROR: '${TARGET_GOVERNOR}' is not supported by this machine!" - exit 0 + wanted_gov="powersave" fi +echo "Trying to set CPU governor to $wanted_gov" + +for dir in /sys/devices/system/cpu/cpu*/cpufreq; do + driver=$( cat "$dir/scaling_driver" ) + newgov="$wanted_gov" + if [ "$driver" != "intel_pstate" ] && [ "$driver" != "amd_pstate" ]; then + [ "$newgov" = "powersave" ] && newgov="ondemand" + fi + echo "$newgov" > "$dir/scaling_governor" +done +exit 0 -- cgit v1.2.3-55-g7522