cpufreqscaling: fix

Signed-off-by: AuxXxilium <info@auxxxilium.tech>
This commit is contained in:
AuxXxilium 2024-08-25 16:55:44 +02:00
parent 863fd719f8
commit d44a42db34
5 changed files with 11 additions and 186 deletions

View File

@ -1,121 +0,0 @@
#!/usr/bin/env bash
# Make things safer
set -euo pipefail
# Ensure root user
#if [ "$EUID" -ne 0 ]; then
# echo "Please run as root"
# exit
#fi
# Ensure acpi-cpufreq kernel module is loaded
#if ! modprobe acpi-cpufreq; then
# pushd /lib/modules
# insmod acpi-cpufreq.ko
# popd
#fi
# Frequency scaling function
function main {
if [ $(cat /proc/cpuinfo | grep Intel | wc -l) -gt 0 ]; then
CPU="INTEL"
else
CPU="AMD"
fi
# Get cpu cores count minus 1, to allow maping from 0
cpucorecount=$(cat /proc/cpuinfo | grep processor | wc -l)
cpucorecount=$((cpucorecount - 1))
governor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
# Rereive allowed cpu freq on the system
IFS=" " read -r -a freqlist <<<"$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies)"
# Set min and max frequencies, this user overidable
scalingminfreq=${scalingminfreq:=${freqlist[-1]}}
scalingmaxfreq=${scalingmaxfreq:=${freqlist[0]}}
# This will set user defined min and max frequencies
if [ "$governor" = "userspace" ]; then
for i in $(seq 0 "${cpucorecount}"); do
echo "$scalingminfreq" >/sys/devices/system/cpu/cpu${i}/cpufreq/scaling_min_freq
echo "$scalingmaxfreq" >/sys/devices/system/cpu/cpu${i}/cpufreq/scaling_max_freq
done
fi
if [ "$CPU" = "INTEL" ]; then
# Get current and max cpu temps for Intel
currtemp=$(cat /sys/bus/platform/devices/coretemp.0/hwmon/hwmon0/temp1_input)
maxtemp=$(cat /sys/bus/platform/devices/coretemp.0/hwmon/hwmon0/temp1_max)
# Frequencies steps definitions (only for Intel)
coolfreq=${freqlist[3]}
fi
# Frequencies steps definitions
minfreq=${freqlist[-1]}
midfreq=${freqlist[$((${#freqlist[*]} / 2))]}
maxfreq=${freqlist[0]}
# Get average load over 5m in base10 integer format
loadavg=$(awk -F . '{print $1 substr($2,1,2)}' </proc/loadavg)
# Set load steps to trigger frequencies scaling, this user overidable
lowload=$(grep cores /proc/cpuinfo | sort -u | awk '{ print $4 * 0.3 * 100 }')
midload=$(grep cores /proc/cpuinfo | sort -u | awk '{ print $4 * 0.6 * 100 }')
if [ "$CPU" = "INTEL" ]; then
if [ "$currtemp" -lt "$maxtemp" ]; then
for i in $(seq 0 "${cpucorecount}"); do
if [ "$loadavg" -le $((10#$lowload)) ]; then
echo "$minfreq" >/sys/devices/system/cpu/cpu"${i}"/cpufreq/scaling_setspeed
elif [ "$loadavg" -ge $((10#$lowload)) ] && [ "$loadavg" -le $((10#$midload)) ]; then
echo "$midfreq" >/sys/devices/system/cpu/cpu"${i}"/cpufreq/scaling_setspeed
elif [ "$loadavg" -ge $((10#$midload)) ]; then
echo "$maxfreq" >/sys/devices/system/cpu/cpu${i}/cpufreq/scaling_setspeed
fi
done
else
for i in $(seq 0 "${cpucorecount}"); do
echo "$coolfreq" >/sys/devices/system/cpu/cpu${i}/cpufreq/scaling_setspeed
done
sleep 30
fi
elif [ "$CPU" = "AMD" ]; then
for i in $(seq 0 "${cpucorecount}"); do
if [ "$loadavg" -le $((10#$lowload)) ]; then
echo "$minfreq" >/sys/devices/system/cpu/cpu${i}/cpufreq/scaling_setspeed
elif [ "$loadavg" -ge $((10#$lowload)) ] && [ "$loadavg" -le $((10#$midload)) ]; then
echo "$midfreq" >/sys/devices/system/cpu/cpu${i}/cpufreq/scaling_setspeed
elif [ "$loadavg" -ge $((10#$midload)) ]; then
echo "$maxfreq" >/sys/devices/system/cpu/cpu${i}/cpufreq/scaling_setspeed
fi
done
fi
}
# Deamonize the main function...
while true; do
if [ -f /usr/sbin/stopscale ]; then
exit 0
fi
# Get cpu cores count minus 1, to allow maping from 0
cpucorecount=$(cat /proc/cpuinfo | grep processor | wc -l)
cpucorecount=$((cpucorecount - 1))
governor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
# Set correct cpufreq governor to allow user defined frequency scaling
if [ "$governor" != "userspace" ]; then
for i in $(seq 0 ${cpucorecount}); do
echo "userspace" >/sys/devices/system/cpu/cpu${i}/cpufreq/scaling_governor
done
fi
governor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
if [ "$governor" = "userspace" ]; then
main
fi
sleep 0.5
done
exit 0

View File

@ -1,23 +0,0 @@
#!/usr/bin/env bash
# Make things safer
set -euo pipefail
systemctl stop cpufreqscaling.service
systemctl disable cpufreqscaling.service
# Get cpu cores count minus 1, to allow maping from 0
cpucorecount=$(cat /proc/cpuinfo | grep processor | wc -l)
cpucorecount=$((cpucorecount - 1))
governor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
# Set correct cpufreq governor to allow user defined frequency scaling
if [ "$governor" != "performance" ]; then
for i in $(seq 0 "${cpucorecount}"); do
echo "performance" >/sys/devices/system/cpu/cpu"${i}"/cpufreq/scaling_governor
done
fi
touch /usr/sbin/stopscale
exit 0

View File

@ -11,15 +11,13 @@ if [ "${1}" = "late" ]; then
mkdir -p "/tmpRoot/usr/arc/addons/"
cp -vf "${0}" "/tmpRoot/usr/arc/addons/"
cp -vf /usr/bin/scaler.sh /tmpRoot/usr/bin/scaler.sh
cp -vf /usr/bin/unscaler.sh /tmpRoot/usr/bin/unscaler.sh
cp -vf /usr/bin/rescaler.sh /tmpRoot/usr/bin/rescaler.sh
[ ! -f "/tmpRoot/usr/bin/echo" ] && cp -vf /usr/bin/echo /tmpRoot/usr/bin/echo
if [ "${2}" = "userspace" ]; then
mkdir -p "/tmpRoot/usr/lib/systemd/system"
DEST="/tmpRoot/usr/lib/systemd/system/cpufreqscaling.service"
cat << EOF > ${DEST}
cp -vf /usr/sbin/scaling.sh /tmpRoot/usr/sbin/scaling.sh
[ ! -f "/tmpRoot/usr/bin/echo" ] && cp -vf /usr/bin/echo /tmpRoot/usr/bin/echo || true
cp -f /usr/lib/modules/acpi_cpufreq.ko /tmpRoot/usr/lib/modules/acpi_cpufreq.ko
[ "${2}" != "schedutil" ] && cp -vf /usr/lib/modules/cpufreq_${2}.ko /tmpRoot/usr/lib/modules/cpufreq_${2}.ko && modprobe cpufreq_${2} || true
mkdir -p "/tmpRoot/usr/lib/systemd/system"
DEST="/tmpRoot/usr/lib/systemd/system/cpufreqscaling.service"
cat << EOF > ${DEST}
[Unit]
Description=Enable CPU Freq scaling
DefaultDependencies=no
@ -31,8 +29,8 @@ User=root
Type=simple
Restart=on-failure
RestartSec=10
ExecStartPre=sleep 30
ExecStart=/usr/bin/scaler.sh
ExecStartPre=sleep 10
ExecStart=/usr/sbin/scaling.sh "${2}"
[Install]
WantedBy=multi-user.target
@ -42,40 +40,11 @@ Author=Virtualization Team
EOF
mkdir -vp /tmpRoot/usr/lib/systemd/system/multi-user.target.wants
ln -vsf /usr/lib/systemd/system/cpufreqscaling.service /tmpRoot/usr/lib/systemd/system/multi-user.target.wants/cpufreqscaling.service
else
[ "${2}" != "schedutil" ] && cp -vf /usr/lib/modules/cpufreq_${2}.ko /tmpRoot/usr/lib/modules/cpufreq_${2}.ko && modprobe cpufreq_${2}
mkdir -p "/tmpRoot/usr/lib/systemd/system"
DEST="/tmpRoot/usr/lib/systemd/system/cpufreqscaling.service"
cat << EOF > ${DEST}
[Unit]
Description=Enable CPU Freq scaling
DefaultDependencies=no
IgnoreOnIsolate=true
After=udevrules.service
[Service]
User=root
Type=simple
Restart=on-failure
RestartSec=10
ExecStart=/usr/bin/rescaler.sh "${2}"
[Install]
WantedBy=multi-user.target
[X-Synology]
Author=Virtualization Team
EOF
mkdir -vp /tmpRoot/usr/lib/systemd/system/multi-user.target.wants
ln -vsf /usr/lib/systemd/system/cpufreqscaling.service /tmpRoot/usr/lib/systemd/system/multi-user.target.wants/cpufreqscaling.service
fi
elif [ "${1}" = "uninstall" ]; then
echo "Installing cpufreqscalingscaling - ${1}"
rm -f "/tmpRoot/usr/lib/systemd/system/multi-user.target.wants/cpufreqscaling.service"
rm -f "/tmpRoot/usr/lib/systemd/system/cpufreqscaling.service"
rm -f /tmpRoot/usr/bin/scaler.sh
rm -f /tmpRoot/usr/bin/rescaler.sh
rm -f /tmpRoot/usr/bin/unscaler.sh
rm -f /tmpRoot/usr/sbin/scaling.sh
fi

View File

@ -1,6 +1,6 @@
version: 1
name: cpufreqscaling
description: "Enable CPU Freqscaling for DSM - Only use if Scaling is not working"
description: "Enable CPU Freqscaling for DSM - Modify in Arc DSM Options"
system: false
beta: false
all: