From 677646abbd965549b76437c0577048815d2308e2 Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Sun, 28 Nov 2021 14:06:46 +0100 Subject: [PATCH] Add mechanism for power-profiles-daemon detection --- auto_cpufreq/core.py | 64 ++++++++++++------------ auto_cpufreq/gnome_power.py | 97 +++++++++++++++++++++++++++++++++++++ bin/auto-cpufreq | 4 ++ 3 files changed, 132 insertions(+), 33 deletions(-) create mode 100644 auto_cpufreq/gnome_power.py diff --git a/auto_cpufreq/core.py b/auto_cpufreq/core.py index 7ba4957..86d106b 100644 --- a/auto_cpufreq/core.py +++ b/auto_cpufreq/core.py @@ -17,6 +17,8 @@ from pathlib import Path from shutil import which from subprocess import getoutput, call, run, check_output, DEVNULL +sys.path.append('../') +from auto_cpufreq.gnome_power import * warnings.filterwarnings("ignore") @@ -371,7 +373,6 @@ def remove(): # restore original cpufrectl script cpufreqctl_restore() - def gov_check(): for gov in get_avail_gov(): if gov not in ALL_GOVERNORS: @@ -386,14 +387,11 @@ def gov_check(): "ERROR:\n\nCouldn't find any of the necessary scaling governors.\n" ) - # root check func def root_check(): if not os.geteuid() == 0: print("\n" + "-" * 33 + " Root check " + "-" * 34 + "\n") - print( - "ERROR:\n\nMust be run root for this functionality to work, i.e: \nsudo auto-cpufreq" - ) + print("ERROR:\n\nMust be run root for this functionality to work, i.e: \nsudo " + app_name) footer() exit(1) @@ -1087,33 +1085,33 @@ def running_daemon(): daemon_running_msg() exit(1) -# disable gnome >= 40 power profiles (live) -def gnome_power_disable_live(): - gnome_power_stats = call(["systemctl", "is-active", "--quiet", "power-profiles-daemon"]) - if(gnome_power_stats == 0): - print("Disabling GNOME power profiles") - call(["systemctl", "stop", "power-profiles-daemon"]) - else: - print("GNOME power already disabled") +# # disable gnome >= 40 power profiles (live) +# def gnome_power_disable_live(): +# gnome_power_stats = call(["systemctl", "is-active", "--quiet", "power-profiles-daemon"]) +# if(gnome_power_stats == 0): +# print("Disabling GNOME power profiles") +# call(["systemctl", "stop", "power-profiles-daemon"]) +# else: +# print("GNOME power already disabled") -# disable gnome >= 40 power profiles (install) -def gnome_power_disable(): - gnome_power_stats = call(["systemctl", "is-active", "--quiet", "power-profiles-daemon"]) - if(gnome_power_stats == 0): - print("Disabling GNOME power profiles") - call(["systemctl", "stop", "power-profiles-daemon"]) - call(["systemctl", "disable", "power-profiles-daemon"]) - call(["systemctl", "mask", "power-profiles-daemon"]) - else: - print("GNOME power already disabled") +# # disable gnome >= 40 power profiles (install) +# def gnome_power_disable(): +# gnome_power_stats = call(["systemctl", "is-active", "--quiet", "power-profiles-daemon"]) +# if(gnome_power_stats == 0): +# print("Disabling GNOME power profiles") +# call(["systemctl", "stop", "power-profiles-daemon"]) +# call(["systemctl", "disable", "power-profiles-daemon"]) +# call(["systemctl", "mask", "power-profiles-daemon"]) +# else: +# print("GNOME power already disabled") -# enable gnome >= 40 power profiles (uninstall) -def gnome_power_enable(): - gnome_power_stats = call(["systemctl", "is-active", "--quiet", "power-profiles-daemon"]) - if(gnome_power_stats == 0): - print("Enabling GNOME power profiles") - call(["systemctl", "unmask", "power-profiles-daemon"]) - call(["systemctl", "start", "power-profiles-daemon"]) - call(["systemctl", "enable", "power-profiles-daemon"]) - else: - print("GNOME power already enabled") \ No newline at end of file +# # enable gnome >= 40 power profiles (uninstall) +# def gnome_power_enable(): +# gnome_power_stats = call(["systemctl", "is-active", "--quiet", "power-profiles-daemon"]) +# if(gnome_power_stats == 0): +# print("Enabling GNOME power profiles") +# call(["systemctl", "unmask", "power-profiles-daemon"]) +# call(["systemctl", "start", "power-profiles-daemon"]) +# call(["systemctl", "enable", "power-profiles-daemon"]) +# else: +# print("GNOME power already enabled") \ No newline at end of file diff --git a/auto_cpufreq/gnome_power.py b/auto_cpufreq/gnome_power.py new file mode 100644 index 0000000..5bf6f1d --- /dev/null +++ b/auto_cpufreq/gnome_power.py @@ -0,0 +1,97 @@ +# * add gnome_power_detect message after install (make it more visible) +# * make sure daemon is enabled after auto-cpufreq --remove (non snap) +# * if daemon is disabled and auto-cpufreq is removed (snap) remind user to enable it back +import os, sys, click +from subprocess import getoutput, call, run, check_output, DEVNULL + +sys.path.append('../') +from auto_cpufreq.core import * + +# app_name var +#if os.getenv("PKG_MARKER") == "SNAP": +# app_name = "auto-cpufreq" +if sys.argv[0] == "gnome_power.py": + app_name="gnome_power.py" +else: + app_name="auto-cpufreq" + +# detect if gnome power profile service is running +gnome_power_stats = call(["systemctl", "is-active", "--quiet", "power-profiles-daemon"]) +# ToDo: remove +print(gnome_power_stats) +print(os.getenv('PKG_MARKER')) + +# alert in case gnome power profile service is running +def gnome_power_detect(): + # ToDo: broken, can't be checked like this + if os.getenv('PKG_MARKER') == "SNAP" and gnome_power_stats == 0: + print("\nDetected running GNOME Power Profiles daemon service:") + print("This daemon might interfere with auto-cpufreq and should be disabled!\n") + print("Due to Snap limitations, it needs to be disabled manually by running, i.e:") + print("cd ~/auto-cpufreq/auto_cpufreq") + print("python3 gnome_power.py --disable") + elif gnome_power_stats == 0: + print("\nDetected running GNOME Power Profiles daemon service:") + print("This daemon might interfere with auto-cpufreq and it will be disabled!") + gnome_power_disable() + print("\nIf you wish to enable this daemon to run concurrently with auto-cpufreq run:") + print("cd ~/auto-cpufreq/auto_cpufreq") + print("python3 gnome_power.py --enable") + +# disable gnome >= 40 power profiles (live) +def gnome_power_disable_live(): + if(gnome_power_stats != 0): + # ToDo: remove + print("\nDisabling GNOME power profiles") + call(["systemctl", "stop", "power-profiles-daemon"]) + else: + print("GNOME power already disabled") + +# disable gnome >= 40 power profiles (install) +def gnome_power_disable(): + if(gnome_power_stats != 0): + # ToDo: remove + print("\nDisabling GNOME power profiles") + call(["systemctl", "stop", "power-profiles-daemon"]) + call(["systemctl", "disable", "power-profiles-daemon"]) + call(["systemctl", "mask", "power-profiles-daemon"]) + else: + print("GNOME power already disabled") + +# enable gnome >= 40 power profiles (uninstall) +def gnome_power_enable(): + # ToDo: remove + if(gnome_power_stats != 0): + print("\nEnabling GNOME power profiles") + call(["systemctl", "unmask", "power-profiles-daemon"]) + call(["systemctl", "start", "power-profiles-daemon"]) + call(["systemctl", "enable", "power-profiles-daemon"]) + else: + print("GNOME power already enabled") + +def valid_options(): + print("--enable\t\tEnable GNOME Power Profiles daemon") + print("--disable\t\tDisable GNOME Power Profiles daemon\n") + +# cli +@click.command() +@click.option("--enable", is_flag=True, help="Monitor and see suggestions for CPU optimizations") +@click.option("--disable", is_flag=True, help="Monitor and make (temp.) suggested CPU optimizations") +def main(enable, disable): + + root_check() + if len(sys.argv) == 1: + footer() + print("Provided none of valid options.\n\nRun: \"" + app_name + " --help\" for more info") + footer() + else: + if enable: + root_check() + print("Enabling") + elif disable: + print("Disabling") + else: + print("whatever") + +if __name__ == '__main__': + main() diff --git a/bin/auto-cpufreq b/bin/auto-cpufreq index a1ff4b4..61ab5ed 100755 --- a/bin/auto-cpufreq +++ b/bin/auto-cpufreq @@ -12,6 +12,7 @@ from subprocess import call, run sys.path.append('../') from auto_cpufreq.core import * +#from auto_cpufreq.gnome_power import * # cli @click.command() @@ -83,6 +84,7 @@ def main(config, daemon, debug, install, live, log, monitor, stats, version, don elif live: config_info_dialog() print("\nNote: You can quit live mode by pressing \"ctrl+c\"") + gnome_power_detect() gnome_power_disable_live() time.sleep(1) while True: @@ -139,6 +141,7 @@ def main(config, daemon, debug, install, live, log, monitor, stats, version, don if os.getenv('PKG_MARKER') == "SNAP": root_check() running_daemon() + gnome_power_detect() gnome_power_disable() gov_check() run("snapctl set daemon=enabled", shell=True) @@ -147,6 +150,7 @@ def main(config, daemon, debug, install, live, log, monitor, stats, version, don else: root_check() running_daemon() + gnome_power_detect() gnome_power_disable() gov_check() deploy_daemon()