From d335b378ef748a1f9fe0c917333d831f11efb16d Mon Sep 17 00:00:00 2001 From: Gunjan Gupta Date: Mon, 16 Sep 2024 18:20:38 +0530 Subject: [PATCH] Add cpufreq options Signed-off-by: Gunjan Gupta --- lib/armbian-configng/config.ng.functions.sh | 45 +++++++++++ lib/armbian-configng/config.ng.jobs.json | 87 ++++++++++++++++++++- lib/armbian-configng/config.ng.system.sh | 42 +++++++++- 3 files changed, 172 insertions(+), 2 deletions(-) diff --git a/lib/armbian-configng/config.ng.functions.sh b/lib/armbian-configng/config.ng.functions.sh index 5b49905fa..9b5d3d324 100644 --- a/lib/armbian-configng/config.ng.functions.sh +++ b/lib/armbian-configng/config.ng.functions.sh @@ -212,6 +212,16 @@ function set_runtime_variables() { DIALOG_CANCEL=1 DIALOG_ESC=255 + # Generate empty CPU freq file in case it does not exists + if [[ ! -f /etc/default/armbian-cpufrequtils ]]; then + cat <<- EOF > "/etc/default/armbian-cpufrequtils" + ENABLE=false + MIN_SPEED= + MAX_SPEED= + GOVERNOR= + EOF + fi + # we have our own lsb_release which does not use Python. Others shell install it here if [[ ! -f /usr/bin/lsb_release ]]; then if is_package_manager_running; then @@ -730,9 +740,44 @@ show_menu() { else exit 0 fi +} + +module_options+=( +["generic_select,author"]="Gunjan Gupta" +["generic_select,ref_link"]="" +["generic_select,feature"]="generic_select" +["generic_select,desc"]="Display a menu a given list of options with a provided prompt" +["generic_select,example"]="generic_select \"true false\" \"Select an option\"" +["generic_select,doc_link"]="" +["generic_select,status"]="Active" +) +# +# Display a menu a given list of options with a provided prompt +# +function generic_select() +{ + IFS=$' ' + PARAMETER=($1) + local LIST=() + for i in "${PARAMETER[@]}" + do + if [[ -n $3 ]]; then + [[ ${i[0]} -ge $3 ]] && \ + LIST+=( "${i[0]//[[:blank:]]/}" "" ) + else + LIST+=( "${i[0]//[[:blank:]]/}" "" ) + fi + done + LIST_LENGTH=$((${#LIST[@]}/2)); + if [ "$LIST_LENGTH" -eq 1 ]; then + PARAMETER=${LIST[0]} + else + PARAMETER=$($DIALOG --title "$2" --menu "" 0 0 9 "${LIST[@]}" 3>&1 1>&2 2>&3) + fi } + module_options+=( ["get_user_continue,author"]="Joey Turner" ["get_user_continue,ref_link"]="" diff --git a/lib/armbian-configng/config.ng.jobs.json b/lib/armbian-configng/config.ng.jobs.json index 38751f3c2..3457ad1a1 100644 --- a/lib/armbian-configng/config.ng.jobs.json +++ b/lib/armbian-configng/config.ng.jobs.json @@ -383,7 +383,92 @@ "author": "Igor Pecovnik", "condition": "[ -f /etc/armbian-distribution-status ] && release_upgrade rolling verify" } - ] + ] + }, + { + "id": "S28", + "description": "Set CPU speed and governor", + "sub": [ + { + "id": "S1001", + "description": "Disable CPU frequency utilities", + "command": [ + "sed -i \"s/ENABLE=.*/ENABLE=false/\" /etc/default/armbian-cpufrequtils", + "#systemctl restart armbian-cpufrequtils" + ], + "status": "Active", + "doc_link": "", + "src_reference": "", + "author": "", + "condition": "grep -q '^ENABLE=true' /etc/default/armbian-cpufrequtils 2>/dev/null" + }, + { + "id": "S1002", + "description": "Enable CPU frequency utilities", + "command": [ + "sed -i \"s/ENABLE=.*/ENABLE=true/\" /etc/default/armbian-cpufrequtils 2>/dev/null", + "#systemctl restart armbian-cpufrequtils" + ], + "status": "Active", + "doc_link": "", + "src_reference": "", + "author": "", + "condition": "grep -q '^ENABLE=false' /etc/default/armbian-cpufrequtils 2>/dev/null" + }, + { + "id": "S1003", + "description": "Set minimum CPU speed", + "command": [ + "set_cpufreq_option MIN_SPEED" + ], + "status": "Active", + "doc_link": "", + "src_reference": "", + "author": "", + "condition": "grep -q '^ENABLE=true' /etc/default/armbian-cpufrequtils 2>/dev/null" + }, + { + "id": "S1004", + "description": "Set maximum CPU speed", + "command": [ + "set_cpufreq_option MAX_SPEED" + ], + "status": "Active", + "doc_link": "", + "src_reference": "", + "author": "", + "condition": "grep -q '^ENABLE=true' /etc/default/armbian-cpufrequtils 2>/dev/null" + }, + { + "id": "S1005", + "description": "Set CPU scaling governor", + "command": [ + "set_cpufreq_option GOVERNOR" + ], + "status": "Active", + "doc_link": "", + "src_reference": "", + "author": "", + "condition": "grep -q '^ENABLE=true' /etc/default/armbian-cpufrequtils 2>/dev/null" + }, + { + "id": "S1006", + "description": "Show configuration", + "command": [ + "show_message <<< \"$(cat /etc/default/armbian-cpufrequtils)\"" + ], + "status": "Preview", + "doc_link": "", + "src_reference": "", + "author": "Igor Pecovnik", + "condition": "grep -q '^ENABLE=true' /etc/default/armbian-cpufrequtils 2>/dev/null && [[ -f /etc/default/armbian-cpufrequtils ]]" + } + ], + "status": "Active", + "doc_link": "", + "src_reference": "", + "author": "Gunjan Gupta", + "condition": "[ -f /etc/default/armbian-cpufrequtils ]" } ] }, diff --git a/lib/armbian-configng/config.ng.system.sh b/lib/armbian-configng/config.ng.system.sh index 6b18a8634..782d53c90 100644 --- a/lib/armbian-configng/config.ng.system.sh +++ b/lib/armbian-configng/config.ng.system.sh @@ -232,7 +232,6 @@ module_options+=( # @description set/unset Armbian root filesystem to read only # function manage_overlayfs() { - if [[ "$1" == "enable" ]]; then debconf-apt-progress -- apt-get -o Dpkg::Options::="--force-confold" -y install overlayroot cryptsetup cryptsetup-bin [[ ! -f /etc/overlayroot.conf ]] && cp /etc/overlayroot.conf.dpkg-new /etc/overlayroot.conf @@ -334,3 +333,44 @@ function adjust_motd() { # adjust motd config sed -i "s/^MOTD_DISABLE=.*/MOTD_DISABLE=\"$INSERT\"/g" /etc/default/armbian-motd } + +module_options+=( +["set_cpufreq_option,author"]="Gunjan Gupta" +["set_cpufreq_option,ref_link"]="" +["set_cpufreq_option,feature"]="cpufreq" +["set_cpufreq_option,desc"]="Set cpufreq options like minimum/maximum speed and governor" +["set_cpufreq_option,example"]="set_cpufreq_option MIN_SPEED|MAX_SPEED|GOVERNOR" +["set_cpufreq_option,status"]="Active" +) +# +# @description set cpufreq options like minimum/maximum speed and governor +# +function set_cpufreq_option () { + # Assuming last policy is for the big core + local policy=$(ls /sys/devices/system/cpu/cpufreq/ | tail -n 1) + local selected_value="" + + case "$1" in + MIN_SPEED) + generic_select "$(cat /sys/devices/system/cpu/cpufreq/$policy/scaling_available_frequencies 2>/dev/null || cat /sys/devices/system/cpu/cpufreq/$policy/scaling_min_freq 2>/dev/null)" "Select minimum CPU speed" + selected_value=$PARAMETER + ;; + MAX_SPEED) + local min_speed=$(cat /sys/devices/system/cpu/cpufreq/$policy/cpuinfo_min_freq) + generic_select "$(cat /sys/devices/system/cpu/cpufreq/$policy/scaling_available_frequencies 2>/dev/null || cat /sys/devices/system/cpu/cpufreq/$policy/scaling_max_freq 2>/dev/null)" "Select maximum CPU speed" $min_speed + selected_value=$PARAMETER + ;; + GOVERNOR) + generic_select "$(cat /sys/devices/system/cpu/cpufreq/$policy/scaling_available_governors)" "Select CPU governor" + selected_value=$PARAMETER + ;; + + *) + ;; + esac + if [[ -n $selected_value ]]; then + sed -i "s/$1=.*/$1=$selected_value/" /etc/default/armbian-cpufrequtils + #systemctl restart armbiancpufrequtils + fi +} +