cpufreqscaling: rework

Signed-off-by: AuxXxilium <info@auxxxilium.tech>
This commit is contained in:
AuxXxilium 2024-08-20 20:30:20 +02:00
parent 325331ec91
commit d754721c74
5 changed files with 69 additions and 68 deletions

View File

@ -0,0 +1,47 @@
#!/usr/bin/env bash
#
# Copyright (C) 2023 AuxXxilium <https://github.com/AuxXxilium>
#
# This is free software, licensed under the MIT License.
# See /LICENSE for more information.
#
# Make things safer
set -euo pipefail
# Get cpu cores count minus 1, to allow maping from 0
cpucorecount=$(cat /proc/cpuinfo | grep processor | wc -l)
cpucorecount=$((cpucorecount - 1))
error=0
# Load the correct cpufreq module
if [ "${1}" = "ondemand" ] || [ "${1}" = "conservative" ]; then
if [ -f "/usr/lib/modules/cpufreq_${1}.ko" ]; then
modprobe cpufreq_${1}
echo "CPUFreqScaling: cpufreq_${1} loaded"
else
echo "CPUFreqScaling: cpufreq_${1} not found"
error=1
fi
fi
# Deamonize the main function...
for i in $(seq 0 ${cpucorecount}); do
governor=$(cat /sys/devices/system/cpu/cpu${i}/cpufreq/scaling_governor)
# Set correct cpufreq governor to allow frequency scaling
if [ "${governor}" != "${1}" ]; then
echo "${1}" >/sys/devices/system/cpu/cpu${i}/cpufreq/scaling_governor
fi
done
sleep 10
# Check if the governor is set correctly
for i in $(seq 0 ${cpucorecount}); do
governor=$(cat /sys/devices/system/cpu/cpu${i}/cpufreq/scaling_governor)
if [ "${governor}" = "${1}" ]; then
echo "CPUFreqScaling: Governor set to ${1}"
else
echo "CPUFreqScaling: Failed to set governor to ${1}"
error=1
fi
done
[ "${error}" -eq 1 ] && exit 1
exit 0

View File

@ -40,8 +40,8 @@ function main {
# 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
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
@ -74,23 +74,23 @@ function main {
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
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
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
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
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
echo "$maxfreq" >/sys/devices/system/cpu/cpu${i}/cpufreq/scaling_setspeed
fi
done
fi
@ -107,8 +107,8 @@ while true; do
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
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)

View File

@ -1,50 +0,0 @@
#!/usr/bin/env bash
#
# Copyright (C) 2023 AuxXxilium <https://github.com/AuxXxilium>
#
# This is free software, licensed under the MIT License.
# See /LICENSE for more information.
#
# 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)
# Deamonize the main function...
while true; do
if [ -f /usr/sbin/stopscale ]; then
exit 0
fi
# Set correct cpufreq governor to allow user defined frequency scaling
if [ "${1}" = "ondemand" ] || [ "${1}" = "conservative" ]; then
if [ -f "/usr/lib/modules/cpufreq_${1}.ko" ]; then
modprobe cpufreq_${1}
if [ "${governor}" != "${1}" ]; then
for i in $(seq 0 "${cpucorecount}"); do
echo "${1}" >/sys/devices/system/cpu/cpu${i}/cpufreq/scaling_governor
done
fi
else
echo "No cpufreq_${1} module found"
exit 1
fi
elif [ "${1}" = "schedutil" ]; then
if [ "${governor}" != "${1}" ]; then
for i in $(seq 0 "${cpucorecount}"); do
echo "${1}" >/sys/devices/system/cpu/cpu${i}/cpufreq/scaling_governor
done
fi
fi
# Check if the governor is set correctly
governor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)
if [ "${governor}" = "${1}" ]; then
echo "Governor set to ${1}"
break
exit 0
else
echo "Failed to set governor to ${1}"
break
exit 1
fi
done

View File

@ -11,9 +11,9 @@ if [ "${1}" = "late" ]; then
mkdir -p "/tmpRoot/usr/arc/addons/"
cp -vf "${0}" "/tmpRoot/usr/arc/addons/"
cp -vf /usr/sbin/scaler.sh /tmpRoot/usr/sbin/scaler.sh
cp -vf /usr/sbin/unscaler.sh /tmpRoot/usr/sbin/unscaler.sh
cp -vf /usr/sbin/rescaler.sh /tmpRoot/usr/sbin/rescaler.sh
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
@ -22,13 +22,16 @@ if [ "${1}" = "late" ]; then
cat << EOF > ${DEST}
[Unit]
Description=Enable CPU Freq scaling
DefaultDependencies=no
IgnoreOnIsolate=true
After=multi-user.target
[Service]
User=root
Type=simple
Restart=on-failure
RestartSec=10s
ExecStart=/usr/sbin/scaler.sh
ExecStart=/usr/bin/scaler.sh
[Install]
WantedBy=multi-user.target
@ -48,10 +51,11 @@ Description=Enable CPU Freq scaling
After=multi-user.target
[Service]
User=root
Type=simple
Restart=on-failure
RestartSec=5s
ExecStart=/usr/sbin/rescaler.sh ${2}
RestartSec=10s
ExecStart=/usr/bin/rescaler.sh "${2}"
[Install]
WantedBy=multi-user.target
@ -68,7 +72,7 @@ elif [ "${1}" = "uninstall" ]; then
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/sbin/scaler.sh
rm -f /tmpRoot/usr/sbin/rescaler.sh
rm -f /tmpRoot/usr/sbin/unscaler.sh
rm -f /tmpRoot/usr/bin/scaler.sh
rm -f /tmpRoot/usr/bin/rescaler.sh
rm -f /tmpRoot/usr/bin/unscaler.sh
fi