auto-cpufreq/bin/auto-cpufreq

227 lines
7.9 KiB
Python
Executable File

#!/usr/bin/env python3
#
# auto-cpufreq - Automatic CPU speed & power optimizer for Linux
#
# Blog post: https://foolcontrol.org/?p=3124
# core import
import sys
import time
import click
from subprocess import call, run
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", is_flag=True, help="Install daemon for (permanent) automatic CPU optimizations")
@click.option("--remove", is_flag=True, help="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("--force", is_flag=False, help="Force use of either \"powersave\" or \"performance\" governors. Setting to \"reset\" will go back to normal mode")
@click.option("--get-state", is_flag=True, hidden=True)
@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")
@click.option("--log", is_flag=True, hidden=True)
@click.option("--daemon", is_flag=True, hidden=True)
def main(config, daemon, debug, install, remove, live, log, monitor, stats, version, donate, force, get_state):
# display info if config file is used
def config_info_dialog():
if get_config(config) and hasattr(get_config, "using_cfg_file"):
print("\nUsing settings defined in " + config + " file")
# set governor override unless None or invalid
if force is not None:
not_running_daemon_check()
root_check() # Calling root_check before set_override as it will require sudo access
set_override(force) # Calling set override, only if force has some values
if len(sys.argv) == 1:
print("\n" + "-" * 32 + " auto-cpufreq " + "-" * 33 + "\n")
print("Automatic CPU speed & power optimizer for Linux")
print("\nExample usage:\nauto-cpufreq --monitor")
print("\n-----\n")
run(["auto-cpufreq", "--help"])
footer()
else:
if daemon:
config_info_dialog()
root_check()
file_stats()
if os.getenv("PKG_MARKER") == "SNAP" and dcheck == "enabled":
gnome_power_detect_snap()
tlp_service_detect_snap()
while True:
footer()
gov_check()
cpufreqctl()
distro_info()
sysinfo()
set_autofreq()
countdown(2)
elif os.getenv("PKG_MARKER") != "SNAP":
gnome_power_detect()
tlp_service_detect()
while True:
footer()
gov_check()
cpufreqctl()
distro_info()
sysinfo()
set_autofreq()
countdown(2)
else:
daemon_not_found()
elif monitor:
config_info_dialog()
root_check()
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()
else:
gnome_power_detect()
tlp_service_detect()
while True:
time.sleep(1)
running_daemon_check()
footer()
gov_check()
cpufreqctl()
distro_info()
sysinfo()
mon_autofreq()
countdown(2)
elif live:
root_check()
config_info_dialog()
print('\nNote: You can quit live mode by pressing "ctrl+c"')
time.sleep(1)
if os.getenv("PKG_MARKER") == "SNAP":
gnome_power_detect_snap()
tlp_service_detect_snap()
else:
gnome_power_detect_install()
gnome_power_stop_live()
tlp_service_detect()
while True:
try:
running_daemon_check()
footer()
gov_check()
cpufreqctl()
distro_info()
sysinfo()
set_autofreq()
countdown(2)
except KeyboardInterrupt:
gnome_power_start_live()
print("")
sys.exit()
elif stats:
not_running_daemon_check()
config_info_dialog()
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()
else:
gnome_power_detect()
tlp_service_detect()
read_stats()
elif log:
deprecated_log_msg()
elif get_state:
not_running_daemon_check()
override = get_override()
print(override)
elif debug:
# ToDo: add status of GNOME Power Profile service status
config_info_dialog()
root_check()
cpufreqctl()
footer()
distro_info()
sysinfo()
print("")
app_version()
print("")
python_info()
print("")
device_info()
if charging():
print("Battery is: charging")
else:
print("Battery is: discharging")
print("")
app_res_use()
display_load()
get_current_gov()
get_turbo()
footer()
elif version:
footer()
distro_info()
app_version()
footer()
elif donate:
footer()
print("If auto-cpufreq helped you out and you find it useful ...\n")
print("Show your appreciation by donating!")
print("https://github.com/AdnanHodzic/auto-cpufreq/#donate")
footer()
elif install:
if os.getenv("PKG_MARKER") == "SNAP":
root_check()
running_daemon_check()
gnome_power_detect_snap()
tlp_service_detect_snap()
bluetooth_notif_snap()
gov_check()
run("snapctl set daemon=enabled", shell=True)
run("snapctl start --enable auto-cpufreq", shell=True)
deploy_complete_msg()
else:
root_check()
running_daemon_check()
gov_check()
deploy_daemon()
deploy_complete_msg()
elif remove:
if os.getenv("PKG_MARKER") == "SNAP":
root_check()
run("snapctl set daemon=disabled", shell=True)
run("snapctl stop --disable auto-cpufreq", shell=True)
if auto_cpufreq_stats_path.exists():
if auto_cpufreq_stats_file is not None:
auto_cpufreq_stats_file.close()
auto_cpufreq_stats_path.unlink()
# ToDo:
# * undo bluetooth boot disable
gnome_power_rm_reminder_snap()
remove_complete_msg()
else:
root_check()
remove_daemon()
remove_complete_msg()
if __name__ == "__main__":
main()