From fc2d8e3645d0e3896856066eb7fdb6899a5eba6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81kos=20V=C3=A1rady?= Date: Thu, 30 Dec 2021 13:29:14 +0100 Subject: [PATCH] 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. --- auto_cpufreq/core.py | 58 +++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/auto_cpufreq/core.py b/auto_cpufreq/core.py index bb44ae2..66cdc34 100644 --- a/auto_cpufreq/core.py +++ b/auto_cpufreq/core.py @@ -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)