Skip to content

Commit

Permalink
v7.8
Browse files Browse the repository at this point in the history
+ DietPi-Set_CPU | Assure the script always exists with no error code
+ DietPi-Set_CPU | Only obtain dietpi.txt settings when required and always perform valid min/max integer checks with a fallback default value
+ DietPi-Set_CPU | Exit early if CPU governor tunables are present but not writable, e.g. within a container
+ RC up
  • Loading branch information
MichaIng committed Nov 13, 2021
1 parent 36f7ec6 commit 733d81e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .update/version
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Available DietPi version
G_REMOTE_VERSION_CORE=7
G_REMOTE_VERSION_SUB=8
G_REMOTE_VERSION_RC=1
G_REMOTE_VERSION_RC=2
# Minimum DietPi version to allow update
G_MIN_VERSION_CORE=6
G_MIN_VERSION_SUB=0
Expand Down
8 changes: 4 additions & 4 deletions dietpi.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ AUTO_SETUP_TIMEZONE=Europe/London
AUTO_SETUP_NET_ETHERNET_ENABLED=1
AUTO_SETUP_NET_WIFI_ENABLED=0

# Force ethernet speeds: 0=automatic speed | 10 = 10Mbit | 100 = 100Mbit etc
# Force Ethernet speeds: 0=automatic speed | 10 = 10Mbit | 100 = 100Mbit etc
# - This is mainly aimed at PINE A64 which may have an HW issue that causes unstable 1Gbit link.
AUTO_SETUP_NET_ETH_FORCE_SPEED=0

Expand Down Expand Up @@ -105,7 +105,7 @@ AUTO_SETUP_DESKTOP_INDEX=0
# - It won't be installed if no desktop is installed and a manual browser selection will override it.
AUTO_SETUP_BROWSER_INDEX=-1

# DietPi-Autostart: 0=Console | 7=Console autologin | 1=Kodi | 2=Desktop autologin | 16=Desktop | 4=OpenTyrian | 5=DietPi-Cloudshell | 6=Amiberry fast boot | 8=Amiberry standard boot | 9=DDX-Rebirth | 10=CAVA Spectrum | 11=Chromium kiosk | 14=Custom autostart service
# DietPi-Autostart: 0=Console | 7=Console autologin | 1=Kodi | 2=Desktop autologin | 16=Desktop | 4=OpenTyrian | 5=DietPi-CloudShell | 6=Amiberry fast boot | 8=Amiberry standard boot | 9=DDX-Rebirth | 10=CAVA Spectrum | 11=Chromium kiosk | 14=Custom script (background) | 17=Custom script (foreground)
# - This will be effective on 2nd boot, after firstrun update and installs have been done.
# - Related software titles must be installed either on firstrun installs or via AUTO_SETUP_AUTOMATED=1 + AUTO_SETUP_INSTALL_SOFTWARE_ID (see below).
AUTO_SETUP_AUTOSTART_TARGET_INDEX=0
Expand Down Expand Up @@ -148,9 +148,9 @@ SURVEY_OPTED_IN=-1
CONFIG_CPU_GOVERNOR=schedutil
# Ondemand Sampling Rate | Min value: 10000 microseconds (10ms)
CONFIG_CPU_ONDEMAND_SAMPLE_RATE=25000
# Ondemand Sampling Down Factor: Samling Rate * Down Factor / 1000 = ms (40 = 1000ms when sampling rate is 25000)
# Ondemand Sampling Down Factor: Sampling Rate * Down Factor / 1000 = ms (40 = 1000ms when sampling rate is 25000)
CONFIG_CPU_ONDEMAND_SAMPLE_DOWNFACTOR=40
# Throttle Up Percentage: Percentage of average CPU usage during samling rate at which CPU will be throttled up/down
# Throttle Up Percentage: Percentage of average CPU usage during sampling rate at which CPU will be throttled up/down
CONFIG_CPU_USAGE_THROTTLE_UP=50

# CPU Frequency Limits: Disabled=disabled
Expand Down
2 changes: 1 addition & 1 deletion dietpi/dietpi-autostart
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ _EOF_
'' '●─ Other '
'14' ': Custom script (background, no autologin)'
'17' ': Custom script (foreground, with autologin)'
'5' ': DietPi-Cloudshell'
'5' ': DietPi-CloudShell'

)

Expand Down
2 changes: 1 addition & 1 deletion dietpi/func/dietpi-globals
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
# - Assign defaults/code version as fallback
[[ $G_DIETPI_VERSION_CORE ]] || G_DIETPI_VERSION_CORE=7
[[ $G_DIETPI_VERSION_SUB ]] || G_DIETPI_VERSION_SUB=8
[[ $G_DIETPI_VERSION_RC ]] || G_DIETPI_VERSION_RC=1
[[ $G_DIETPI_VERSION_RC ]] || G_DIETPI_VERSION_RC=2
[[ $G_GITBRANCH ]] || G_GITBRANCH='master'
[[ $G_GITOWNER ]] || G_GITOWNER='MichaIng'
# - Save current version and Git branch
Expand Down
87 changes: 45 additions & 42 deletions dietpi/func/dietpi-set_cpu
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@
#
# Info:
# - Location: /boot/dietpi/func/dietpi-set_cpu
# - Runs at boot, called by /boot/dietpi/preboot
# - Runs at boot, called by dietpi-preboot.service > /boot/dietpi/preboot
# - Applies CPU governor: ondemand, powersave, performance, etc.
# - Applies CPU governor settings: throttle up percent etc.
#////////////////////////////////////

# Exit if not supported
if [[ ! -f '/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors' ]]; then

echo 'DietPi-CPU_set | CPU governors are not supported on this device. Aborting...'
exit

if [[ ! -w '/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors' ]]
then

if [[ -f '/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors' ]]
then
echo 'DietPi-CPU_set | CPU governors cannot be changed on this system. This is probably a container guest. Aborting...'
else
echo 'DietPi-CPU_set | CPU governors are not available on this system. This is probably a virtual machine. Aborting...'
fi
exit 0
fi

# Import DietPi-Globals --------------------------------------------------------------
Expand All @@ -37,35 +42,33 @@

local available_governors=$(</sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors)

# Check if target Gov is available on system
if [[ $available_governors != *"$CPU_GOVERNOR"* ]]; then
# Check if target governor is available on system
[[ $available_governors != *"$CPU_GOVERNOR"* ]] || return 0

G_DIETPI-NOTIFY 1 "CPU governor $CPU_GOVERNOR is not supported by kernel"
G_DIETPI-NOTIFY 1 "CPU governor $CPU_GOVERNOR is not supported by system"

if [[ $available_governors == *'schedutil'* ]]; then
if [[ $available_governors == *'schedutil'* ]]; then

CPU_GOVERNOR='schedutil'
CPU_GOVERNOR='schedutil'

elif [[ $available_governors == *'ondemand'* ]]; then
elif [[ $available_governors == *'ondemand'* ]]; then

CPU_GOVERNOR='ondemand'
CPU_GOVERNOR='ondemand'

elif [[ $available_governors == *'interactive'* ]]; then
elif [[ $available_governors == *'interactive'* ]]; then

CPU_GOVERNOR='interactive'
CPU_GOVERNOR='interactive'

else
else

CPU_GOVERNOR='performance'
CPU_GOVERNOR='performance'

fi

G_DIETPI-NOTIFY 2 "Switching to CPU governor: $CPU_GOVERNOR"
fi

# Update dietpi.txt
G_CONFIG_INJECT 'CONFIG_CPU_GOVERNOR=' "CONFIG_CPU_GOVERNOR=$CPU_GOVERNOR" /boot/dietpi.txt
G_DIETPI-NOTIFY 2 "Switching to CPU governor: $CPU_GOVERNOR"

fi
# Update dietpi.txt
G_CONFIG_INJECT 'CONFIG_CPU_GOVERNOR=' "CONFIG_CPU_GOVERNOR=$CPU_GOVERNOR" /boot/dietpi.txt

}

Expand All @@ -75,9 +78,7 @@
local i
for i in /sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_governor
do

echo "$CPU_GOVERNOR" > "$i"

done

# Apply CPU max/min frequency
Expand All @@ -87,7 +88,7 @@
local cpu_max_freq=$(sed -n '/^[[:blank:]]*CONFIG_CPU_MAX_FREQ=/{s/^[^=]*=//p;q}' /boot/dietpi.txt)
local cpu_min_freq=$(sed -n '/^[[:blank:]]*CONFIG_CPU_MIN_FREQ=/{s/^[^=]*=//p;q}' /boot/dietpi.txt)

# Intel based 0-100%
# Intel pstate scheduler: 0-100%
if [[ -f '/sys/devices/system/cpu/intel_pstate/max_perf_pct' ]]; then

disable_error=1 G_CHECK_VALIDINT "$cpu_max_freq" 1 100 || cpu_max_freq=100
Expand All @@ -100,7 +101,7 @@

if [[ -f '/sys/devices/system/cpu/intel_pstate/no_turbo' ]]; then

if grep -qi '^[[:blank:]]*CONFIG_CPU_DISABLE_TURBO=1' /boot/dietpi.txt; then
if grep -q '^[[:blank:]]*CONFIG_CPU_DISABLE_TURBO=1' /boot/dietpi.txt; then

G_DIETPI-NOTIFY 2 'Disabling Intel Turbo Mode'
echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo
Expand All @@ -118,7 +119,7 @@

fi

# Standard via kernel (MHz)
# Linux CPUfreq: kHz
elif [[ -f '/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq' ]]; then

# Reset to defaults if no/invalid entry
Expand All @@ -131,7 +132,6 @@

for i in /sys/devices/system/cpu/cpu[0-9]*/cpufreq
do

if (( $converted_khz_value_min > 0 )); then

echo "$converted_khz_value_min" > "$i/scaling_min_freq"
Expand All @@ -151,7 +151,6 @@
mawk '{print $NF}' "$i/scaling_available_frequencies" > "$i/scaling_max_freq"

fi

done

else
Expand All @@ -162,14 +161,15 @@

fi

local cpu_throttle_up_percent=$(sed -n '/^[[:blank:]]*CONFIG_CPU_USAGE_THROTTLE_UP=/{s/[^=]*=//p;q}' /boot/dietpi.txt)

# Set CPU governor interactive settings
if [[ $CPU_GOVERNOR == 'interactive' ]]; then

# Set hispeed_load, if available on system (eg: XU4 kernel lacks this feature)
# Set hispeed_load if supported by system (e.g. Odroid XU4 kernel lacks this feature)
if [[ -f '/sys/devices/system/cpu/cpufreq/interactive/go_hispeed_load' ]]; then

local cpu_throttle_up_percent=$(sed -n '/^[[:blank:]]*CONFIG_CPU_USAGE_THROTTLE_UP=/{s/[^=]*=//p;q}' /boot/dietpi.txt)
disable_error=1 G_CHECK_VALIDINT "$cpu_throttle_up_percent" 1 100 || cpu_throttle_up_percent=50

G_DIETPI-NOTIFY 2 "Setting go_hispeed_load: $cpu_throttle_up_percent %"
echo "$cpu_throttle_up_percent" > /sys/devices/system/cpu/cpufreq/interactive/go_hispeed_load

Expand All @@ -182,12 +182,16 @@
# Set CPU governor ondemand settings
elif [[ $CPU_GOVERNOR == 'ondemand' ]]; then

local cpu_throttle_up_percent=$(sed -n '/^[[:blank:]]*CONFIG_CPU_USAGE_THROTTLE_UP=/{s/[^=]*=//p;q}' /boot/dietpi.txt)
local cpu_ondemand_sampling_rate=$(sed -n '/^[[:blank:]]*CONFIG_CPU_ONDEMAND_SAMPLE_RATE=/{s/[^=]*=//p;q}' /boot/dietpi.txt)
local cpu_ondemand_sampling_down_factor=$(sed -n '/^[[:blank:]]*CONFIG_CPU_ONDEMAND_SAMPLE_DOWNFACTOR=/{s/[^=]*=//p;q}' /boot/dietpi.txt)
disable_error=1 G_CHECK_VALIDINT "$cpu_throttle_up_percent" 1 100 || cpu_throttle_up_percent=50
disable_error=1 G_CHECK_VALIDINT "$cpu_ondemand_sampling_rate" 10000 || cpu_ondemand_sampling_rate=25000
disable_error=1 G_CHECK_VALIDINT "$cpu_ondemand_sampling_down_factor" 1 100 || cpu_ondemand_sampling_down_factor=40

G_DIETPI-NOTIFY 2 "Setting up_threshold: $cpu_throttle_up_percent %"
G_DIETPI-NOTIFY 2 "Setting sampling_rate: $cpu_ondemand_sampling_rate microseconds"
G_DIETPI-NOTIFY 2 "Setting sampling_down_factor: $cpu_ondemand_sampling_down_factor"
G_DIETPI-NOTIFY 2 "Setting sampling_down_factor: $cpu_ondemand_sampling_down_factor x"

# Check for different possible locations
# - All
Expand All @@ -197,17 +201,15 @@
echo "$cpu_ondemand_sampling_rate" > /sys/devices/system/cpu/cpufreq/ondemand/sampling_rate
echo "$cpu_ondemand_sampling_down_factor" > /sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor

# - XU4 3.x kernel
# - Odroid XU4 3.x kernel
elif [[ -f '/sys/devices/system/cpu/cpu0/cpufreq/ondemand/up_threshold' ]]; then

# Apply to all cores
for i in /sys/devices/system/cpu/cpu[0-9]*/cpufreq/ondemand
do

echo "$cpu_throttle_up_percent" > "$i/up_threshold"
echo "$cpu_ondemand_sampling_rate" > "$i/sampling_rate"
echo "$cpu_ondemand_sampling_down_factor" > "$i/sampling_down_factor"

done

else
Expand All @@ -219,9 +221,12 @@
# Set CPU governor conservative settings
elif [[ $CPU_GOVERNOR == 'conservative' ]]; then

local cpu_throttle_up_percent=$(sed -n '/^[[:blank:]]*CONFIG_CPU_USAGE_THROTTLE_UP=/{s/[^=]*=//p;q}' /boot/dietpi.txt)
disable_error=1 G_CHECK_VALIDINT "$cpu_throttle_up_percent" 1 100 || cpu_throttle_up_percent=50

G_DIETPI-NOTIFY 2 "Setting up_threshold: $cpu_throttle_up_percent %"

# XU3/4 different path (must apply to each core)
# Odroid XU4: Must apply to each core
if [[ $G_HW_MODEL != 11 && -f '/sys/devices/system/cpu/cpufreq/conservative/up_threshold' ]]; then

echo "$cpu_throttle_up_percent" > /sys/devices/system/cpu/cpufreq/conservative/up_threshold
Expand All @@ -230,9 +235,7 @@

for i in /sys/devices/system/cpu/cpu[0-9]*/cpufreq/conservative
do

echo "$cpu_throttle_up_percent" > "$i/up_threshold"

done

else
Expand All @@ -253,12 +256,12 @@
#-----------------------------------------------------------------------------------
Check_CPU_Gov_Available

# Apply CPU0 gov to all cores (if required)
# Apply CPU governor to all cores
Apply_CPU_Gov

G_DIETPI-NOTIFY 0 "Applied CPU governor settings: $CPU_GOVERNOR"

#-----------------------------------------------------------------------------------
exit
exit 0
#-----------------------------------------------------------------------------------
}

0 comments on commit 733d81e

Please sign in to comment.