From fe21ddf24585164ed0f8cfa8f9ad7d3db627ab79 Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Fri, 3 Feb 2023 17:28:12 +0100 Subject: [PATCH 1/5] Working governor override on Snap package --- auto_cpufreq/core.py | 20 ++++++++++++-------- bin/auto-cpufreq | 1 + snap/snapcraft.yaml | 15 ++++++++------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/auto_cpufreq/core.py b/auto_cpufreq/core.py index 4931b99b..15e9a9ec 100644 --- a/auto_cpufreq/core.py +++ b/auto_cpufreq/core.py @@ -59,7 +59,10 @@ auto_cpufreq_stats_file = None # track governor override -STORE = "/opt/auto-cpufreq/override.pickle" +if os.getenv("PKG_MARKER") == "SNAP": + governor_override_state = Path("/var/snap/auto-cpufreq/current/override.pickle") +else: + governor_override_state = Path("/opt/auto-cpufreq/current/override.pickle") if os.getenv("PKG_MARKER") == "SNAP": auto_cpufreq_stats_path = Path("/var/snap/auto-cpufreq/current/auto-cpufreq.stats") @@ -86,20 +89,20 @@ def get_config(config_file=""): return get_config.config def get_override(): - if os.path.isfile(STORE): - with open(STORE, "rb") as store: + if os.path.isfile(governor_override_state): + with open(governor_override_state, "rb") as store: return pickle.load(store) else: return "default" def set_override(override): if override in ["powersave", "performance"]: - with open(STORE, "wb") as store: + with open(governor_override_state, "wb") as store: pickle.dump(override, store) print(f"Set governor override to {override}") elif override == "reset": - if os.path.isfile(STORE): - os.remove(STORE) + if os.path.isfile(governor_override_state): + os.remove(governor_override_state) print("Governor override removed") elif override is not None: print("Invalid option.\nUse force=performance, force=powersave, or force=reset") @@ -367,6 +370,7 @@ def deploy_daemon(): bluetooth_disable() auto_cpufreq_stats_path.touch(exist_ok=True) + governor_override_state.touch(exist_ok=True) print("\n* Deploy auto-cpufreq install script") shutil.copy(SCRIPTS_DIR / "auto-cpufreq-install.sh", "/usr/local/bin/auto-cpufreq-install") @@ -442,8 +446,8 @@ def remove_daemon(): os.remove("/usr/local/bin/auto-cpufreq-remove") # delete override pickle if it exists - if os.path.exists(STORE): - os.remove(STORE) + if os.path.exists(governor_override_state): + os.remove(governor_override_state) # delete stats file if auto_cpufreq_stats_path.exists(): diff --git a/bin/auto-cpufreq b/bin/auto-cpufreq index 822f2003..0f84b671 100755 --- a/bin/auto-cpufreq +++ b/bin/auto-cpufreq @@ -164,6 +164,7 @@ def main(config, daemon, debug, install, remove, install_performance, live, log, python_info() print("") device_info() + print(f"VALUE {governor_override_state_path}") if charging(): print("Battery is: charging") else: diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index bcb83335..80de4b60 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -43,10 +43,10 @@ plugs: interface: system-files write: - /etc/auto-cpufreq.conf - opt-auto-cpufreq: - interface: system-files - write: - - /opt/auto-cpufreq/override.pickle + # opt-auto-cpufreq: + # interface: system-files + # write: + # - /opt/auto-cpufreq/override.pickle apps: auto-cpufreq: @@ -60,15 +60,16 @@ apps: - cpu-control - system-observe - hardware-observe - - opt-auto-cpufreq + - etc-auto-cpufreq-conf + # - opt-auto-cpufreq service: command: usr/bin/snapdaemon plugs: - cpu-control - system-observe - hardware-observe - - etc-auto-cpufreq - - opt-auto-cpufreq + - etc-auto-cpufreq-conf + # - opt-auto-cpufreq environment: LC_ALL: C.UTF-8 LANG: C.UTF-8 From 04b878360c7dff4b0e44fc8935e9667a597806b4 Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Fri, 3 Feb 2023 17:52:20 +0100 Subject: [PATCH 2/5] Snap tag 2.0-beta + governor_override improvements --- auto_cpufreq/core.py | 3 +-- bin/auto-cpufreq | 1 - setup.py | 2 +- snap/snapcraft.yaml | 6 ------ 4 files changed, 2 insertions(+), 10 deletions(-) diff --git a/auto_cpufreq/core.py b/auto_cpufreq/core.py index 15e9a9ec..8f67e450 100644 --- a/auto_cpufreq/core.py +++ b/auto_cpufreq/core.py @@ -62,7 +62,7 @@ if os.getenv("PKG_MARKER") == "SNAP": governor_override_state = Path("/var/snap/auto-cpufreq/current/override.pickle") else: - governor_override_state = Path("/opt/auto-cpufreq/current/override.pickle") + governor_override_state = Path("/opt/auto-cpufreq/override.pickle") if os.getenv("PKG_MARKER") == "SNAP": auto_cpufreq_stats_path = Path("/var/snap/auto-cpufreq/current/auto-cpufreq.stats") @@ -370,7 +370,6 @@ def deploy_daemon(): bluetooth_disable() auto_cpufreq_stats_path.touch(exist_ok=True) - governor_override_state.touch(exist_ok=True) print("\n* Deploy auto-cpufreq install script") shutil.copy(SCRIPTS_DIR / "auto-cpufreq-install.sh", "/usr/local/bin/auto-cpufreq-install") diff --git a/bin/auto-cpufreq b/bin/auto-cpufreq index 0f84b671..822f2003 100755 --- a/bin/auto-cpufreq +++ b/bin/auto-cpufreq @@ -164,7 +164,6 @@ def main(config, daemon, debug, install, remove, install_performance, live, log, python_info() print("") device_info() - print(f"VALUE {governor_override_state_path}") if charging(): print("Battery is: charging") else: diff --git a/setup.py b/setup.py index 348dad90..ac3d18de 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ def read(name): return f.read() # Used for the tar.gz/snap releases -VERSION = "1.9.7" +VERSION = "2.0-beta" setup( name="auto-cpufreq", diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 80de4b60..9a26f358 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -43,10 +43,6 @@ plugs: interface: system-files write: - /etc/auto-cpufreq.conf - # opt-auto-cpufreq: - # interface: system-files - # write: - # - /opt/auto-cpufreq/override.pickle apps: auto-cpufreq: @@ -61,7 +57,6 @@ apps: - system-observe - hardware-observe - etc-auto-cpufreq-conf - # - opt-auto-cpufreq service: command: usr/bin/snapdaemon plugs: @@ -69,7 +64,6 @@ apps: - system-observe - hardware-observe - etc-auto-cpufreq-conf - # - opt-auto-cpufreq environment: LC_ALL: C.UTF-8 LANG: C.UTF-8 From f574257dc475f942fa39179787acf09c567e76dd Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Fri, 3 Feb 2023 18:32:04 +0100 Subject: [PATCH 3/5] Remove install_performance flag --- bin/auto-cpufreq | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/bin/auto-cpufreq b/bin/auto-cpufreq index 822f2003..d3880dad 100755 --- a/bin/auto-cpufreq +++ b/bin/auto-cpufreq @@ -21,7 +21,6 @@ from auto_cpufreq.power_helper import * @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("--install_performance", is_flag=True, help="Install daemon in \"performance\" mode, reference:\n https://bit.ly/3bjVZW1") @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) @@ -36,7 +35,7 @@ from auto_cpufreq.power_helper import * @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, install_performance, live, log, monitor, stats, version, donate, force, get_state): +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(): @@ -185,18 +184,6 @@ def main(config, daemon, debug, install, remove, install_performance, live, log, 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("\nThis option is only available on non Snap installs.\n\n" - "Please refer to auto-cpufreq power_helper.py script for more info\n" - "Reference: https://github.com/AdnanHodzic/auto-cpufreq#configuring-auto-cpufreq\n") - else: - root_check() - running_daemon_check() - gov_check() - deploy_daemon_performance() - deploy_complete_msg() elif install: if os.getenv("PKG_MARKER") == "SNAP": root_check() From 471611de7da97b7da8580646612d4f910311e16e Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Fri, 3 Feb 2023 18:44:33 +0100 Subject: [PATCH 4/5] Remove GNOME Power Profiles Daemon performance install --- auto_cpufreq/power_helper.py | 96 ++++++++++-------------------------- 1 file changed, 25 insertions(+), 71 deletions(-) diff --git a/auto_cpufreq/power_helper.py b/auto_cpufreq/power_helper.py index bd4330af..d02ea539 100644 --- a/auto_cpufreq/power_helper.py +++ b/auto_cpufreq/power_helper.py @@ -129,7 +129,7 @@ def gnome_power_start_live(): def gnome_power_svc_enable(): if systemctl_exists: try: - print("\n* Enabling GNOME power profiles") + print("* Enabling GNOME power profiles\n") call(["systemctl", "unmask", "power-profiles-daemon"]) call(["systemctl", "start", "power-profiles-daemon"]) call(["systemctl", "enable", "power-profiles-daemon"]) @@ -256,95 +256,50 @@ def disable_power_profiles_daemon(): # default gnome_power_svc_disable func (balanced) def gnome_power_svc_disable(): - if systemctl_exists: - # set balanced profile if its running before disabling it - if gnome_power_status == 0 and powerprofilesctl_exists: - print("Using profile: ", "balanced") - call(["powerprofilesctl", "set", "balanced"]) - - disable_power_profiles_daemon() - -# default gnome_power_svc_disable func (performance) -def gnome_power_svc_disable_performance(): - if systemctl_exists: - # set performance profile if its running before disabling it - if gnome_power_status == 0 and powerprofilesctl_exists: - print("Using profile: ", "performance") - call(["powerprofilesctl", "set", "performance"]) - - disable_power_profiles_daemon() - -# cli -@click.pass_context -# external gnome power srevice disable function -def gnome_power_svc_disable_ext(ctx, power_selection): - raw_power_disable = ctx.params["gnome_power_disable"] - gnome_power_disable = str(raw_power_disable).replace('[','').replace(']','').replace(",", "").replace("(","").replace(")","").replace("'","") + # check if snap package installed + snap_pkg_check = call(['snap', 'list', '|', 'grep', 'auto-cpufreq'], + stdout=subprocess.DEVNULL, + stderr=subprocess.STDOUT) if systemctl_exists: # 0 is active if gnome_power_status != 0: try: - snap_pkg_check = call(['snap', 'list', '|', 'grep', 'auto-cpufreq'], - stdout=subprocess.DEVNULL, - stderr=subprocess.STDOUT) - # check if snapd is present and if snap package is installed | 0 is success if snap_pkg_check == 0: - print("Power Profiles Daemon is already disabled, re-enable by running:\n" + print("GNOME Power Profiles Daemon is already disabled, it can be re-enabled by running:\n" "sudo python3 power_helper.py --gnome_power_enable\n" - "\nfollowed by running:\n" - "sudo python3 power_helper.py --gnome_power_disable" ) - else: - # snapd present, snap package not installed - print("Power Profiles Daemon is already disabled, first remove auto-cpufreq:\n" - "sudo auto-cpufreq --remove\n" - "\nfollowed by installing auto-cpufreq in performance mode:\n" - "sudo auto-cpufreq --install_performance" + elif snap_pkg_check == 1: + print("auto-cpufreq snap package not installed, GNOME Power Profiles Daemon should be enabled:\n" + "sudo python3 power_helper.py --gnome_power_enable" ) - except FileNotFoundError: + except: # snapd not found on the system - print("Power Profiles Daemon is already disabled, first remove auto-cpufreq:\n" - "sudo auto-cpufreq --remove\n" - "\nfollowed by installing auto-cpufreq in performance mode:\n" - "sudo auto-cpufreq --install_performance" - ) - - # set balanced profile if its running before disabling it + print("There was a problem, couldn't determine GNOME Power Profiles Daemon") + if gnome_power_status == 0 and powerprofilesctl_exists: - # 0 is success (snap package is installed) - try: - snap_pkg_check = call(['snap', 'list', '|', 'grep', 'auto-cpufreq'], - stdout=subprocess.DEVNULL, - stderr=subprocess.STDOUT) - - if snap_pkg_check == 0: - #if snap_exist == 0 and snap_pkg_install == 0: - print("Using profile: ", gnome_power_disable) - call(["powerprofilesctl", "set", gnome_power_disable]) - - disable_power_profiles_daemon() - else: - print("Install auto-cpufreq in performance mode by running:\n" - "sudo auto-cpufreq --install_performance\n" - ) - - except FileNotFoundError: - print("Install auto-cpufreq in performance mode by running:\n" - "sudo auto-cpufreq --install_performance\n" + if snap_pkg_check == 1: + print("auto-cpufreq snap package not installed.\n\nGNOME Power Profiles Daemon should be enabled:\n" + "sudo python3 power_helper.py --gnome_power_enable\n" ) + else: + print("auto-cpufreq snap package installed, GNOME Power Profiles Daemon should be disabled.\n") + print("Using profile: ", "balanced") + call(["powerprofilesctl", "set", "balanced"]) + disable_power_profiles_daemon() +# cli @click.command() -@click.option("--gnome_power_disable", help="Disable GNOME Power profiles service (default: balanced), reference:\n https://bit.ly/3bjVZW1", type=click.Choice(['balanced', 'performance'], case_sensitive=False)) +#@click.option("--gnome_power_disable", help="Disable GNOME Power profiles service (default: balanced), reference:\n https://bit.ly/3bjVZW1", type=click.Choice(['balanced', 'performance'], case_sensitive=False)) +@click.option("--gnome_power_disable", is_flag=True, help="Disable GNOME Power profiles service") # ToDo: # * update readme/docs -@click.option("--power_selection", hidden=True) @click.option("--gnome_power_enable", is_flag=True, help="Enable GNOME Power profiles service") @click.option("--gnome_power_status", is_flag=True, help="Get status of GNOME Power profiles service" @@ -352,7 +307,6 @@ def gnome_power_svc_disable_ext(ctx, power_selection): @click.option("--bluetooth_boot_on", is_flag=True, help="Turn on Bluetooth on boot") @click.option("--bluetooth_boot_off", is_flag=True, help="Turn off Bluetooth on boot") def main( - power_selection, gnome_power_enable, gnome_power_disable, gnome_power_status, @@ -377,7 +331,7 @@ def main( elif gnome_power_disable: header() root_check() - gnome_power_svc_disable_ext(power_selection) + gnome_power_svc_disable() helper_opts() footer() elif gnome_power_status: @@ -401,4 +355,4 @@ def main( if __name__ == "__main__": - main() + main() \ No newline at end of file From dadfae087f102c0f69329d5ad79e3a648c35b459 Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Fri, 3 Feb 2023 20:02:03 +0100 Subject: [PATCH 5/5] Update README with new config options --- README.md | 35 ++++++++++++++++++++--------------- auto_cpufreq/power_helper.py | 6 +++--- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 28031318..4a572c0e 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ auto-cpufreq is looking for [co-maintainers & open source developers to help sha * [Snap store](https://github.com/AdnanHodzic/auto-cpufreq/#snap-store) * [auto-cpufreq-installer](https://github.com/AdnanHodzic/auto-cpufreq/#auto-cpufreq-installer) * [AUR package (Arch/Manjaro Linux)](https://github.com/AdnanHodzic/auto-cpufreq/#aur-package-archmanjaro-linux) -* [Post Installation] +* [Post Installation](https://github.com/AdnanHodzic/auto-cpufreq/blob/install_performance_rm/README.md#post-installation) * [Configuring auto-cpufreq](https://github.com/AdnanHodzic/auto-cpufreq/#configuring-auto-cpufreq) * [1: power_helper.py script](https://github.com/AdnanHodzic/auto-cpufreq/#1-power_helperpy-script) * [2: auto-cpufreq config file](https://github.com/AdnanHodzic/auto-cpufreq/#2-auto-cpufreq-config-file) @@ -114,32 +114,37 @@ After installation `auto-cpufreq` will be available as a binary and you can refe ## Configuring auto-cpufreq -auto-cpufreq makes all decisions automatically based on various factors like cpu usage, temperature or system load. However, it's possible to perform additional configurations in 2 ways: +auto-cpufreq makes all decisions automatically based on various factors like cpu usage, temperature or system load. However, it's possible to perform additional configurations: -### 1: power_helper.py script +### 1: power_helper.py script (Snap package install **only**) -If detected as running, auto-cpufreq will disable [GNOME Power profiles service](https://twitter.com/fooctrl/status/1467469508373884933), which would otherwise cause conflicts and cause problems. +When installing auto-cpufreq using [auto-cpufreq-installer](https://github.com/AdnanHodzic/auto-cpufreq/#auto-cpufreq-installer) if it detects [GNOME Power profiles service](https://twitter.com/fooctrl/status/1467469508373884933) is running it will automatically disable it. Otherwise this daemon will cause conflicts and various other performance issues. -By default auto-cpufreq uses `balanced` mode which works the best on various systems. However, if you're not reaching maximum frequencies your CPU is capable of with auto-cpufreq ([#361](https://github.com/AdnanHodzic/auto-cpufreq/issues/361)), you can switch to `performance` mode. Which will result in higher frequencies by default, but also results in higher energy use (battery consumption). +However, when auto-cpufreq is installed as Snap package it's running as part of a container with limited permissions to your host machine, hence it's *highly recommended* you disable GNOME Power Profiles Daemon using `power_helper.py` script. -If you installed auto-cpufreq using [auto-cpufreq-installer](https://github.com/AdnanHodzic/auto-cpufreq/edit/master/README.md#auto-cpufreq-installer), you can switch to `performance` mode by running: +**Please Note:** +The [`power_helper.py`](https://github.com/AdnanHodzic/auto-cpufreq/blob/master/auto_cpufreq/power_helper.py) script is located at `auto_cpufreq/power_helper.py`. In order to have access to it, you need to first clone +the repository: -`sudo auto-cpufreq --install_performance` +`git clone https://github.com/AdnanHodzic/auto-cpufreq` -Or if you installed auto-cpufreq using [Snap package](https://github.com/AdnanHodzic/auto-cpufreq/edit/master/README.md#snap-store) you can switch to `performance` mode by running: +Navigate to repo location where `power_helper.py` resides, i.e: -`sudo python3 power_helper.py --gnome_power_disable performance` +`cd auto-cpufreq/auto_cpufreq` -**Please Note:** -The `power_helper.py` script is located at `auto_cpufreq/power_helper.py`. In order to have access to it, you need to first clone -the repository: +Then disable GNOME Power Profiles Daemon by runing: -`git clone https://github.com/AdnanHodzic/auto-cpufreq` +`sudo python3 power_helper.py --gnome_power_disable` + +### 2: `--force` governor override + +By default auto-cpufreq uses `balanced` mode which works the best on various systems and situations. +However, you can override this behaviour by switching to `performance` or `powersave` mode manually. Performance will result in higher frequencies by default, but also results in higher energy use (battery consumption) and should be used if max performance is necessary. Otherwise `powersave` will do the opposite and extend the battery life to its maximum. -After this step, all necessary changes will still be made automatically. However, if you wish to perform additional "manual" settings this can be done by following instructions explained in next step. +See [`--force` flag](https://github.com/AdnanHodzic/auto-cpufreq/#overriding-governor) for more info. -### 2: auto-cpufreq config file +### 3: auto-cpufreq config file You can configure seperate profiles for the battery and power supply. These profiles will let you pick which governor to use, and how and when turbo boost is enabled. The possible values for turbo boost behavior are `always`, `auto` and `never`. The default behavior is `auto`, which only kicks in during high load. diff --git a/auto_cpufreq/power_helper.py b/auto_cpufreq/power_helper.py index d02ea539..b3401451 100644 --- a/auto_cpufreq/power_helper.py +++ b/auto_cpufreq/power_helper.py @@ -273,7 +273,7 @@ def gnome_power_svc_disable(): "sudo python3 power_helper.py --gnome_power_enable\n" ) elif snap_pkg_check == 1: - print("auto-cpufreq snap package not installed, GNOME Power Profiles Daemon should be enabled:\n" + print("auto-cpufreq snap package not installed\nGNOME Power Profiles Daemon should be enabled. run:\n\n" "sudo python3 power_helper.py --gnome_power_enable" ) @@ -284,8 +284,8 @@ def gnome_power_svc_disable(): if gnome_power_status == 0 and powerprofilesctl_exists: if snap_pkg_check == 1: - print("auto-cpufreq snap package not installed.\n\nGNOME Power Profiles Daemon should be enabled:\n" - "sudo python3 power_helper.py --gnome_power_enable\n" + print("auto-cpufreq snap package not installed.\nGNOME Power Profiles Daemon should be enabled, run:\n\n" + "sudo python3 power_helper.py --gnome_power_enable" ) else: print("auto-cpufreq snap package installed, GNOME Power Profiles Daemon should be disabled.\n")