Skip to content

Commit

Permalink
Add min/max allowed frequencies option (#309) (#324)
Browse files Browse the repository at this point in the history
* Add min/max allowed frequencies option (#309)

Setting the minimum/maximum allowed CPU frequencies is now possible
via 'scaling_min_freq' and 'scaling_max_freq' options in config
for both modes (charger and battery).
Values should be given in kHZ.

Example:
scaling_min_freq = 1800000
scaling_max_freq = 2000000

I also did autoformatting via 'black', using --line-length 100.
Code is now a bit tidier than before.

* Fixes for min/max allowed frequencies option

* Further modify min/max frequency option

* Fix temporary modification in min/max allowed frequency option

Co-authored-by: Akos Varady <akos.varady@ericsson.com>
  • Loading branch information
varaki and Akos Varady authored Dec 26, 2021
1 parent 64e3382 commit 1aa1983
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 54 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ Default location where config needs to be placed for it to be read automatically
# preferred governor.
governor = performance
# minimum cpu frequency (in kHz)
# example: for 800 MHz = 800000 kHz --> scaling_min_freq = 800000
# see conversion info: https://www.rapidtables.com/convert/frequency/mhz-to-hz.html
# to use this feature, uncomment the following line and set the value accordingly
# scaling_min_freq = 800000
# maximum cpu frequency (in kHz)
# example: for 1GHz = 1000 MHz = 1000000 kHz -> scaling_max_freq = 1000000
# see conversion info: https://www.rapidtables.com/convert/frequency/mhz-to-hz.html
# to use this feature, uncomment the following line and set the value accordingly
# scaling_max_freq = 1000000
# turbo boost setting. possible values: always, auto, never
turbo = auto
Expand All @@ -97,6 +109,18 @@ turbo = auto
# preferred governor
governor = powersave
# minimum cpu frequency (in kHz)
# example: for 800 MHz = 800000 kHz --> scaling_min_freq = 800000
# see conversion info: https://www.rapidtables.com/convert/frequency/mhz-to-hz.html
# to use this feature, uncomment the following line and set the value accordingly
# scaling_min_freq = 800000
# maximum cpu frequency (in kHz)
# see conversion info: https://www.rapidtables.com/convert/frequency/mhz-to-hz.html
# example: for 1GHz = 1000 MHz = 1000000 kHz -> scaling_max_freq = 1000000
# to use this feature, uncomment the following line and set the value accordingly
# scaling_max_freq = 1000000
# turbo boost setting. possible values: always, auto, never
turbo = auto
```
Expand Down
29 changes: 25 additions & 4 deletions auto-cpufreq.conf-example
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
# configuration file for auto-cpufreq
# located in /etc/auto-cpufreq.conf

# settings for when connected to a power source
[charger]
# see available governors by running: cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors
# preferred governor.
governor = performance

# minimum cpu frequency (in kHz)
# example: for 800 MHz = 800000 kHz --> scaling_min_freq = 800000
# see conversion info: https://www.rapidtables.com/convert/frequency/mhz-to-hz.html
# to use this feature, uncomment the following line and set the value accordingly
# scaling_min_freq = 800000

# maximum cpu frequency (in kHz)
# example: for 1GHz = 1000 MHz = 1000000 kHz -> scaling_max_freq = 1000000
# see conversion info: https://www.rapidtables.com/convert/frequency/mhz-to-hz.html
# to use this feature, uncomment the following line and set the value accordingly
# scaling_max_freq = 1000000

# turbo boost setting. possible values: always, auto, never
turbo = auto

#settings for when using battery power
# settings for when using battery power
[battery]
# see available governors by running: cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors
# preferred governor
governor = powersave

# minimum cpu frequency (in kHz)
# example: for 800 MHz = 800000 kHz --> scaling_min_freq = 800000
# see conversion info: https://www.rapidtables.com/convert/frequency/mhz-to-hz.html
# to use this feature, uncomment the following line and set the value accordingly
# scaling_min_freq = 800000

# maximum cpu frequency (in kHz)
# see conversion info: https://www.rapidtables.com/convert/frequency/mhz-to-hz.html
# example: for 1GHz = 1000 MHz = 1000000 kHz -> scaling_max_freq = 1000000
# to use this feature, uncomment the following line and set the value accordingly
# scaling_max_freq = 1000000

# turbo boost setting. possible values: always, auto, never
turbo = auto
95 changes: 71 additions & 24 deletions auto_cpufreq/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,14 @@ def file_stats():


def get_config(config_file=""):
if not hasattr(get_config, "dict"):
get_config.dict = dict()
if not hasattr(get_config, "config"):
get_config.config = configparser.ConfigParser()

config = configparser.ConfigParser()
config.read(config_file)
if os.path.isfile(config_file):
get_config.config.read(config_file)
get_config.using_cfg_file = True

for section in config.sections():
get_config.dict[section] = dict(config.items(section))

return get_config.dict
return get_config.config


# get distro name
Expand Down Expand Up @@ -457,12 +455,60 @@ def display_load():
print("Average temp. of all cores:", avg_all_core_temp, "°C", "\n")


# set minimum and maximum CPU frequencies
def set_frequencies():
conf = get_config()
section = "charger" if charging() else "battery"
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

for freq_type in ["scaling_max_freq", "scaling_min_freq"]:
if not conf.has_option(section, freq_type):
frequency.pop(freq_type)

for freq_type in frequency.keys():
try:
frequency[freq_type]["value"] = int(conf[section][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):
print(
f"Given value for '{freq_type}' is not within the allowed frequencies {min_limit}-{max_limit} kHz"
)
exit(1)

args = f"{frequency[freq_type]['cmdargs']} --set={frequency[freq_type]['value']}"
message = f'Setting {frequency[freq_type]["minmax"]} CPU frequency to {round(frequency[freq_type]["value"]/1000)} Mhz'

# set the frequency
print(message)
run(f"cpufreqctl.auto-cpufreq {args}", shell=True)


# set powersave and enable turbo
def set_powersave():
gov = get_config()
if "battery" in gov:
if "governor" in gov["battery"]:
gov = gov["battery"]["governor"]
conf = get_config()
if conf.has_option("battery", "governor"):
gov = conf["battery"]["governor"]
else:
gov = get_avail_powersave()
print(f'Setting to use: "{gov}" governor')
Expand All @@ -474,6 +520,9 @@ def set_powersave():
run("cpufreqctl.auto-cpufreq --epp --set=balance_power", shell=True)
print('Setting to use: "balance_power" EPP')

# set frequencies
set_frequencies()

# get CPU utilization as a percentage
cpuload = psutil.cpu_percent(interval=1)

Expand All @@ -485,10 +534,8 @@ def set_powersave():
print("Average temp. of all cores:", avg_all_core_temp, "°C")

# conditions for setting turbo in powersave
auto = get_config()
if "battery" in auto:
if "turbo" in auto["battery"]:
auto = auto["battery"]["turbo"]
if conf.has_option("battery", "turbo"):
auto = conf["battery"]["turbo"]
else:
auto = "auto"

Expand Down Expand Up @@ -664,10 +711,9 @@ def mon_powersave():

# set performance and enable turbo
def set_performance():
gov = get_config()
if "charger" in gov:
if "governor" in gov["charger"]:
gov = gov["charger"]["governor"]
conf = get_config()
if conf.has_option("charger", "governor"):
gov = conf["charger"]["governor"]
else:
gov = get_avail_performance()

Expand All @@ -683,6 +729,9 @@ def set_performance():
run("cpufreqctl.auto-cpufreq --epp --set=balance_performance", shell=True)
print('Setting to use: "balance_performance" EPP')

# set frequencies
set_frequencies()

# get CPU utilization as a percentage
cpuload = psutil.cpu_percent(interval=1)

Expand All @@ -693,10 +742,8 @@ def set_performance():
print("Total system load:", load1m)
print("Average temp. of all cores:", avg_all_core_temp, "°C")

auto = get_config()
if "charger" in auto:
if "turbo" in auto["charger"]:
auto = auto["charger"]["turbo"]
if conf.has_option("charger", "turbo"):
auto = conf["charger"]["turbo"]
else:
auto = "auto"

Expand Down
31 changes: 20 additions & 11 deletions bin/auto-cpufreq
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,26 @@ import time
import click
from subprocess import call, run

sys.path.append('../')
sys.path.append("../")
from auto_cpufreq.core import *
from auto_cpufreq.power_helper import *

# cli
@click.command()
@click.option("--monitor", is_flag=True, help="Monitor and see suggestions for CPU optimizations")
@click.option("--live", is_flag=True, help="Monitor and make (temp.) suggested CPU optimizations")
@click.option("--install/--remove", default=True, help="Install/remove daemon for (permanent) automatic CPU optimizations")
@click.option(
"--install/--remove",
default=True,
help="Install/remove daemon for (permanent) automatic CPU optimizations",
)
@click.option("--stats", is_flag=True, help="View live stats of CPU optimizations made by daemon")
@click.option("--config", is_flag=False, default="/etc/auto-cpufreq.conf", help="Use config file at defined path")
@click.option(
"--config",
is_flag=False,
default="/etc/auto-cpufreq.conf",
help="Use config file at defined path",
)
@click.option("--debug", is_flag=True, help="Show debug info (include when submitting bugs)")
@click.option("--version", is_flag=True, help="Show currently installed version")
@click.option("--donate", is_flag=True, help="Support the project")
Expand All @@ -30,7 +39,7 @@ def main(config, daemon, debug, install, live, log, monitor, stats, version, don

# display info if config file is used
def config_info_dialog():
if bool(get_config(config)):
if get_config(config) and hasattr(get_config, "using_cfg_file"):
print("\nUsing settings defined in " + config + " file")

if len(sys.argv) == 1:
Expand Down Expand Up @@ -73,7 +82,7 @@ def main(config, daemon, debug, install, live, log, monitor, stats, version, don
elif monitor:
config_info_dialog()
root_check()
print("\nNote: You can quit monitor mode by pressing \"ctrl+c\"")
print('\nNote: You can quit monitor mode by pressing "ctrl+c"')
if os.getenv("PKG_MARKER") == "SNAP":
gnome_power_detect_snap()
tlp_service_detect_snap()
Expand All @@ -93,7 +102,7 @@ def main(config, daemon, debug, install, live, log, monitor, stats, version, don
elif live:
root_check()
config_info_dialog()
print("\nNote: You can quit live mode by pressing \"ctrl+c\"")
print('\nNote: You can quit live mode by pressing "ctrl+c"')
time.sleep(1)
if os.getenv("PKG_MARKER") == "SNAP":
gnome_power_detect_snap()
Expand All @@ -112,7 +121,7 @@ def main(config, daemon, debug, install, live, log, monitor, stats, version, don
countdown(5)
elif stats:
config_info_dialog()
print("\nNote: You can quit stats mode by pressing \"ctrl+c\"")
print('\nNote: You can quit stats mode by pressing "ctrl+c"')
if os.getenv("PKG_MARKER") == "SNAP":
gnome_power_detect_snap()
tlp_service_detect_snap()
Expand Down Expand Up @@ -158,7 +167,7 @@ def main(config, daemon, debug, install, live, log, monitor, stats, version, don
print("https://github.com/AdnanHodzic/auto-cpufreq/#donate")
footer()
elif install:
if os.getenv('PKG_MARKER') == "SNAP":
if os.getenv("PKG_MARKER") == "SNAP":
root_check()
running_daemon()
gnome_power_detect_snap()
Expand All @@ -175,7 +184,7 @@ def main(config, daemon, debug, install, live, log, monitor, stats, version, don
deploy_daemon()
deploy_complete_msg()
elif remove:
if os.getenv('PKG_MARKER') == "SNAP":
if os.getenv("PKG_MARKER") == "SNAP":
root_check()
run("snapctl set daemon=disabled", shell=True)
run("snapctl stop --disable auto-cpufreq", shell=True)
Expand All @@ -184,7 +193,7 @@ def main(config, daemon, debug, install, live, log, monitor, stats, version, don
auto_cpufreq_stats_file.close()

auto_cpufreq_stats_path.unlink()
# ToDo:
# ToDo:
# * undo bluetooth boot disable
gnome_power_rm_reminder_snap()
remove_complete_msg()
Expand All @@ -194,5 +203,5 @@ def main(config, daemon, debug, install, live, log, monitor, stats, version, don
remove_complete_msg()


if __name__ == '__main__':
if __name__ == "__main__":
main()
Loading

0 comments on commit 1aa1983

Please sign in to comment.