Ability to reset scaling frequencies to default (#338)

* Ability to reset scaling frequencies to default

If one of the scaling frequencies option is missing, either because
it's commented out or auto-cpufreq.conf is not being used, the default
frequency limits will be set.
TODO: perform setting only once per battery state change

* Ability to reset scaling frequencies to default 2.

Additional changes to optimize auto-cpufreq speed, by conditionally
performing frequency scaling settings.
This commit is contained in:
Ákos Várady 2021-12-30 13:29:14 +01:00 committed by GitHub
parent a4fff5fb92
commit fc2d8e3645
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -457,42 +457,66 @@ def display_load():
# set minimum and maximum CPU frequencies
def set_frequencies():
conf = get_config()
section = "charger" if charging() else "battery"
"""
Sets frequencies:
- if option is used in auto-cpufreq.conf: use configured value
- if option is disabled/no conf file used: set default frequencies
Frequency setting is performed only once on power supply change
"""
power_supply = "charger" if charging() else "battery"
# don't do anything if the power supply hasn't changed
if (
hasattr(set_frequencies, "prev_power_supply")
and power_supply == set_frequencies.prev_power_supply
):
return
else:
set_frequencies.prev_power_supply = power_supply
frequency = {
"scaling_max_freq": {
"cmdargs": "--frequency-max",
"minmax": "maximum",
"value": None,
},
"scaling_min_freq": {
"cmdargs": "--frequency-min",
"minmax": "minimum",
"value": None,
},
}
max_limit = None
min_limit = None
if not hasattr(set_frequencies, "max_limit"):
set_frequencies.max_limit = int(getoutput(f"cpufreqctl.auto-cpufreq --frequency-max-limit"))
if not hasattr(set_frequencies, "min_limit"):
set_frequencies.min_limit = int(getoutput(f"cpufreqctl.auto-cpufreq --frequency-min-limit"))
for freq_type in ["scaling_max_freq", "scaling_min_freq"]:
if not conf.has_option(section, freq_type):
frequency.pop(freq_type)
conf = get_config()
for freq_type in frequency.keys():
value = None
if not conf.has_option(power_supply, freq_type):
# fetch and use default frequencies
if freq_type == "scaling_max_freq":
curr_freq = int(getoutput(f"cpufreqctl.auto-cpufreq --frequency-max"))
value = set_frequencies.max_limit
else:
curr_freq = int(getoutput(f"cpufreqctl.auto-cpufreq --frequency-min"))
value = set_frequencies.min_limit
if curr_freq == value:
continue
try:
frequency[freq_type]["value"] = int(conf[section][freq_type].strip())
frequency[freq_type]["value"] = (
value if value else int(conf[power_supply][freq_type].strip())
)
except ValueError:
print(f"Invalid value for '{freq_type}': {frequency[freq_type]['value']}")
exit(1)
if not max_limit:
max_limit = int(getoutput(f"cpufreqctl.auto-cpufreq --frequency-max-limit"))
if not min_limit:
min_limit = int(getoutput(f"cpufreqctl.auto-cpufreq --frequency-min-limit"))
if not (min_limit <= frequency[freq_type]["value"] <= max_limit):
if not (
set_frequencies.min_limit <= frequency[freq_type]["value"] <= set_frequencies.max_limit
):
print(
f"Given value for '{freq_type}' is not within the allowed frequencies {min_limit}-{max_limit} kHz"
f"Given value for '{freq_type}' is not within the allowed frequencies {set_frequencies.min_limit}-{set_frequencies.max_limit} kHz"
)
exit(1)