auto-cpufreq/bin/auto-cpufreq

229 lines
7.7 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/--remove",
default=True,
help="Install/remove daemon for (permanent) automatic CPU optimizations",
)
@click.option("--install_performance", is_flag=True, hidden=True)
@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("--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, install_performance, live, log, monitor, stats, version, donate):
# 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")
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(5)
elif os.getenv("PKG_MARKER") != "SNAP":
gnome_power_detect()
tlp_service_detect()
while True:
footer()
gov_check()
cpufreqctl()
distro_info()
sysinfo()
set_autofreq()
countdown(5)
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()
footer()
gov_check()
cpufreqctl()
distro_info()
sysinfo()
mon_autofreq()
countdown(5)
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()
footer()
gov_check()
cpufreqctl()
distro_info()
sysinfo()
set_autofreq()
countdown(5)
except KeyboardInterrupt:
gnome_power_start_live()
print("")
sys.exit()
elif stats:
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()
time.sleep(1)
read_stats()
elif log:
deprecated_log_msg()
elif debug:
# ToDo: add status of GNOME Power Profile servie 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_performance:
if os.getenv("PKG_MARKER") == "SNAP":
root_check()
print("This option is only available on non Snap installs"
"Please refer to: power_helper.py script for more info\n")
else:
print("in performance")
root_check()
running_daemon()
gov_check()
deploy_daemon_performance()
deploy_complete_msg()
elif install:
if os.getenv("PKG_MARKER") == "SNAP":
root_check()
running_daemon()
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:
print("in install")
root_check()
running_daemon()
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()
remove_complete_msg()
if __name__ == "__main__":
main()