From 4b68cba4b09002ddebdec975fbd9c80fa749293c Mon Sep 17 00:00:00 2001 From: AuxXxilium Date: Sun, 23 Jun 2024 13:09:10 +0200 Subject: [PATCH] cpufreqscaling: rework more Signed-off-by: AuxXxilium --- cpufreqscaling/all/usr/sbin/rescaler.sh | 52 +++++++++-------- cpufreqscaling/all/usr/sbin/scaler.sh | 6 +- cpufreqscaling/all/usr/sbin/unscaler.sh | 4 +- cpufreqscaling/install.sh | 78 ++++++++++++------------- 4 files changed, 74 insertions(+), 66 deletions(-) diff --git a/cpufreqscaling/all/usr/sbin/rescaler.sh b/cpufreqscaling/all/usr/sbin/rescaler.sh index 8e48070..8b97665 100755 --- a/cpufreqscaling/all/usr/sbin/rescaler.sh +++ b/cpufreqscaling/all/usr/sbin/rescaler.sh @@ -3,37 +3,41 @@ # Make things safer set -euo pipefail -if [ -f /usr/sbin/stopscale ]; then - rm -f /usr/sbin/stopscale -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 [ "${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 +# 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 else - echo "No cpufreq_${1} module found" - exit 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 fi -else - if [ "$governor" != "${1}" ]; then - for i in $(seq 0 "${cpucorecount}"); do - echo "${1}" >/sys/devices/system/cpu/cpu"${i}"/cpufreq/scaling_governor - done + sleep 5 + governor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor) + if [ "$governor" = "${1}" ]; then + break fi -fi +done -governor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor) -if [ "$governor" != "${1}" ]; then - systemctl restart cpufreqscaling.service -fi \ No newline at end of file +exit 0 \ No newline at end of file diff --git a/cpufreqscaling/all/usr/sbin/scaler.sh b/cpufreqscaling/all/usr/sbin/scaler.sh index daec173..aefb8b3 100755 --- a/cpufreqscaling/all/usr/sbin/scaler.sh +++ b/cpufreqscaling/all/usr/sbin/scaler.sh @@ -112,8 +112,10 @@ while true; do done fi governor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor) - if [ "$governor" == "userspace" ]; then + if [ "$governor" = "userspace" ]; then main fi sleep 0.5 -done \ No newline at end of file +done + +exit 0 \ No newline at end of file diff --git a/cpufreqscaling/all/usr/sbin/unscaler.sh b/cpufreqscaling/all/usr/sbin/unscaler.sh index 5bfd5b3..8e92d64 100755 --- a/cpufreqscaling/all/usr/sbin/unscaler.sh +++ b/cpufreqscaling/all/usr/sbin/unscaler.sh @@ -18,4 +18,6 @@ if [ "$governor" != "performance" ]; then done fi -touch /usr/sbin/stopscale \ No newline at end of file +touch /usr/sbin/stopscale + +exit 0 \ No newline at end of file diff --git a/cpufreqscaling/install.sh b/cpufreqscaling/install.sh index c4a341c..eea215b 100755 --- a/cpufreqscaling/install.sh +++ b/cpufreqscaling/install.sh @@ -15,47 +15,47 @@ if [ "${1}" = "late" ]; then cp -vf /usr/sbin/unscaler.sh /tmpRoot/usr/sbin/unscaler.sh cp -vf /usr/sbin/rescaler.sh /tmpRoot/usr/sbin/rescaler.sh -if [ "${2}" = "userspace" ]; then - mkdir -p "/tmpRoot/usr/lib/systemd/system" - DEST="/tmpRoot/usr/lib/systemd/system/cpufreqscaling.service" - echo "[Unit]" >${DEST} - echo "Description=Enable CPU Freq scaling" >>${DEST} - echo "DefaultDependencies=no" >>${DEST} - echo "IgnoreOnIsolate=true" >>${DEST} - echo "After=multi-user.target" >>${DEST} - echo >>${DEST} - echo "[Service]" >>${DEST} - echo "User=root" >>${DEST} - echo "Restart=always" >>${DEST} - echo "RestartSec=30" >>${DEST} - echo "ExecStart=/usr/sbin/scaler.sh" >>${DEST} - echo >>${DEST} - echo "[X-Synology]" >>${DEST} - echo "Author=Virtualization Team" >>${DEST} + if [ "${2}" = "userspace" ]; then + mkdir -p "/tmpRoot/usr/lib/systemd/system" + DEST="/tmpRoot/usr/lib/systemd/system/cpufreqscaling.service" + echo "[Unit]" >${DEST} + echo "Description=Enable CPU Freq scaling" >>${DEST} + echo "DefaultDependencies=no" >>${DEST} + echo "IgnoreOnIsolate=true" >>${DEST} + echo "After=multi-user.target" >>${DEST} + echo >>${DEST} + echo "[Service]" >>${DEST} + echo "User=root" >>${DEST} + echo "Restart=always" >>${DEST} + echo "RestartSec=30" >>${DEST} + echo "ExecStart=/bin/ash /usr/sbin/scaler.sh" >>${DEST} + echo >>${DEST} + echo "[X-Synology]" >>${DEST} + echo "Author=Virtualization Team" >>${DEST} - 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 - mkdir -p "/tmpRoot/usr/lib/systemd/system" - DEST="/tmpRoot/usr/lib/systemd/system/cpufreqscaling.service" - echo "[Unit]" >${DEST} - echo "Description=Enable CPU Freq scaling" >>${DEST} - echo "DefaultDependencies=no" >>${DEST} - echo "IgnoreOnIsolate=true" >>${DEST} - echo "After=multi-user.target" >>${DEST} - echo >>${DEST} - echo "[Service]" >>${DEST} - echo "User=root" >>${DEST} - echo "Type=oneshot" >>${DEST} - echo "RemainAfterExit=yes" >>${DEST} - echo "ExecStart=/usr/sbin/rescaler.sh ${2}" >>${DEST} - echo >>${DEST} - echo "[X-Synology]" >>${DEST} - echo "Author=Virtualization Team" >>${DEST} + 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 + mkdir -p "/tmpRoot/usr/lib/systemd/system" + DEST="/tmpRoot/usr/lib/systemd/system/cpufreqscaling.service" + echo "[Unit]" >${DEST} + echo "Description=Enable CPU Freq scaling" >>${DEST} + echo "DefaultDependencies=no" >>${DEST} + echo "IgnoreOnIsolate=true" >>${DEST} + echo "After=multi-user.target" >>${DEST} + echo >>${DEST} + echo "[Service]" >>${DEST} + echo "User=root" >>${DEST} + echo "Type=oneshot" >>${DEST} + echo "RemainAfterExit=yes" >>${DEST} + echo "ExecStart=/bin/ash /usr/sbin/rescaler.sh ${2}" >>${DEST} + echo >>${DEST} + echo "[X-Synology]" >>${DEST} + echo "Author=Virtualization Team" >>${DEST} - 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 + 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 if [ ! -f /tmpRoot/usr/syno/etc/esynoscheduler/esynoscheduler.db ]; then echo "copy esynoscheduler.db" mkdir -p /tmpRoot/usr/syno/etc/esynoscheduler