luci-app-cpufreq: add support for multiple policies

This commit is contained in:
CN_SZTL 2020-12-13 00:23:07 +08:00 committed by Tianling Shen
parent ac003a674f
commit 27b79907c2
No known key found for this signature in database
GPG Key ID: 6850B6345C862176
3 changed files with 72 additions and 65 deletions

View File

@ -1,60 +1,69 @@
local fs = require "nixio.fs"
cpu_freqs = fs.readfile("/sys/devices/system/cpu/cpufreq/policy0/scaling_available_frequencies") or "100000"
cpu_freqs = string.sub(cpu_freqs, 1, -3)
cpu_governors = fs.readfile("/sys/devices/system/cpu/cpufreq/policy0/scaling_available_governors") or "performance"
cpu_governors = string.sub(cpu_governors, 1, -3)
function string.split(input, delimiter)
input = tostring(input)
delimiter = tostring(delimiter)
if (delimiter=='') then return false end
local pos,arr = 0, {}
for st,sp in function() return string.find(input, delimiter, pos, true) end do
table.insert(arr, string.sub(input, pos, st - 1))
pos = sp + 1
end
table.insert(arr, string.sub(input, pos))
return arr
input = tostring(input)
delimiter = tostring(delimiter)
if (delimiter=='') then return false end
local pos,arr = 0, {}
for st,sp in function() return string.find(input, delimiter, pos, true) end do
table.insert(arr, string.sub(input, pos, st - 1))
pos = sp + 1
end
table.insert(arr, string.sub(input, pos))
return arr
end
freq_array = string.split(cpu_freqs, " ")
governor_array = string.split(cpu_governors, " ")
mp = Map("cpufreq", translate("CPU Freq Settings"))
mp.description = translate("Set CPU Scaling Governor to Max Performance or Balance Mode")
s = mp:section(NamedSection, "cpufreq", "settings")
s.anonymouse = true
governor = s:option(ListValue, "governor", translate("CPU Scaling Governor"))
for _, e in ipairs(governor_array) do
if e ~= "" then governor:value(translate(e,string.upper(e))) end
local policy_nums = luci.sys.exec("echo -n $(find /sys/devices/system/cpu/cpufreq/policy* -maxdepth 0 | grep -Eo '[0-9]+')")
for _, policy_num in ipairs(string.split(policy_nums, " ")) do
if not fs.access("/sys/devices/system/cpu/cpufreq/policy" .. policy_num .. "/scaling_available_frequencies") then return end
cpu_freqs = fs.readfile("/sys/devices/system/cpu/cpufreq/policy" .. policy_num .. "/scaling_available_frequencies")
cpu_freqs = string.sub(cpu_freqs, 1, -3)
cpu_governors = fs.readfile("/sys/devices/system/cpu/cpufreq/policy" .. policy_num .. "/scaling_available_governors")
cpu_governors = string.sub(cpu_governors, 1, -3)
freq_array = string.split(cpu_freqs, " ")
governor_array = string.split(cpu_governors, " ")
s:tab(policy_num, translate("Policy " .. policy_num))
governor = s:taboption(policy_num, ListValue, "governor" .. policy_num, translate("CPU Scaling Governor"))
for _, e in ipairs(governor_array) do
if e ~= "" then governor:value(translate(e,string.upper(e))) end
end
minfreq = s:taboption(policy_num, ListValue, "minfreq" .. policy_num, translate("Min Idle CPU Freq"))
for _, e in ipairs(freq_array) do
if e ~= "" then minfreq:value(e) end
end
maxfreq = s:taboption(policy_num, ListValue, "maxfreq" .. policy_num, translate("Max Turbo Boost CPU Freq"))
for _, e in ipairs(freq_array) do
if e ~= "" then maxfreq:value(e) end
end
upthreshold = s:taboption(policy_num, Value, "upthreshold" .. policy_num, translate("CPU Switching Threshold"))
upthreshold.datatype="range(1,99)"
upthreshold.description = translate("Kernel make a decision on whether it should increase the frequency (%)")
upthreshold.placeholder = 50
upthreshold.default = 50
upthreshold:depends("governor", "ondemand")
factor = s:taboption(policy_num, Value, "factor" .. policy_num, translate("CPU Switching Sampling rate"))
factor.datatype="range(1,100000)"
factor.description = translate("The sampling rate determines how frequently the governor checks to tune the CPU (ms)")
factor.placeholder = 10
factor.default = 10
factor:depends("governor", "ondemand")
end
minfreq = s:option(ListValue, "minfreq", translate("Min Idle CPU Freq"))
for _, e in ipairs(freq_array) do
if e ~= "" then minfreq:value(e) end
end
maxfreq = s:option(ListValue, "maxfreq", translate("Max Turbo Boost CPU Freq"))
for _, e in ipairs(freq_array) do
if e ~= "" then maxfreq:value(e) end
end
upthreshold = s:option(Value, "upthreshold", translate("CPU Switching Threshold"))
upthreshold.datatype="range(1,99)"
upthreshold.description = translate("Kernel make a decision on whether it should increase the frequency (%)")
upthreshold.placeholder = 50
upthreshold.default = 50
upthreshold:depends("governor", "ondemand")
factor = s:option(Value, "factor", translate("CPU Switching Sampling rate"))
factor.datatype="range(1,100000)"
factor.description = translate("The sampling rate determines how frequently the governor checks to tune the CPU (ms)")
factor.placeholder = 10
factor.default = 10
factor:depends("governor", "ondemand")
return mp

View File

@ -1,8 +1,3 @@
config settings 'cpufreq'
option governor ''
option minfreq ''
option maxfreq ''
option upthreshold '50'
option factor '10'

View File

@ -3,22 +3,25 @@ START=50
NAME="cpufreq"
config_get_cpufreq()
{
config_get "$NAME" "$1"
}
start()
{
config_load cpufreq
config_get "governor" "${NAME}" "governor"
config_get "minfreq" "${NAME}" "minfreq"
config_get "maxfreq" "${NAME}" "maxfreq"
config_get "upthreshold" "${NAME}" "upthreshold" "50"
config_get "factor" "${NAME}" "factor" "10"
config_load "$NAME"
[ -z "${governor}" ] && exit 0
for i in $(find /sys/devices/system/cpu/cpufreq/policy* -maxdepth 0 | grep -Eo '[0-9]+')
do
[ -z "$(config_get_cpufreq "governor$i")" ] && return
echo "${governor}" > "/sys/devices/system/cpu/cpufreq/policy0/scaling_governor"
echo "${minfreq}" > "/sys/devices/system/cpu/cpufreq/policy0/scaling_min_freq"
echo "${maxfreq}" > "/sys/devices/system/cpu/cpufreq/policy0/scaling_max_freq"
if [ "${governor}" = "ondemand" ]; then
echo "${upthreshold}" > "/sys/devices/system/cpu/cpufreq/ondemand/up_threshold"
echo "${factor}" > "/sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor"
fi
config_get_cpufreq "governor$i" > "/sys/devices/system/cpu/cpufreq/policy$i/scaling_governor"
config_get_cpufreq "minfreq$i" > "/sys/devices/system/cpu/cpufreq/policy$i/scaling_min_freq"
config_get_cpufreq "maxfreq$i" > "/sys/devices/system/cpu/cpufreq/policy$i/scaling_max_freq"
if [ "$(config_get_cpufreq "governor$i")" = "ondemand" ]; then
config_get_cpufreq "upthreshold$i" > "/sys/devices/system/cpu/cpufreq/ondemand/up_threshold"
config_get_cpufreq "factor$i" > "/sys/devices/system/cpu/cpufreq/ondemand/sampling_down_factor"
fi
done
}