Skip to content

Commit

Permalink
Merge branch 'AdnanHodzic:master' into gui
Browse files Browse the repository at this point in the history
  • Loading branch information
shadeyg56 authored Feb 3, 2023
2 parents f50b982 + dadfae0 commit 242a8d0
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 116 deletions.
35 changes: 20 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.

Expand Down
19 changes: 11 additions & 8 deletions auto_cpufreq/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/override.pickle")

if os.getenv("PKG_MARKER") == "SNAP":
auto_cpufreq_stats_path = Path("/var/snap/auto-cpufreq/current/auto-cpufreq.stats")
Expand All @@ -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")
Expand Down Expand Up @@ -442,8 +445,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():
Expand Down
96 changes: 25 additions & 71 deletions auto_cpufreq/power_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down Expand Up @@ -256,103 +256,57 @@ 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\nGNOME Power Profiles Daemon should be enabled. run:\n\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.\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")
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"
)
@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,
Expand All @@ -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:
Expand All @@ -401,4 +355,4 @@ def main(


if __name__ == "__main__":
main()
main()
15 changes: 1 addition & 14 deletions bin/auto-cpufreq
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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():
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 2 additions & 7 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -60,15 +56,14 @@ apps:
- cpu-control
- system-observe
- hardware-observe
- opt-auto-cpufreq
- etc-auto-cpufreq-conf
service:
command: usr/bin/snapdaemon
plugs:
- cpu-control
- system-observe
- hardware-observe
- etc-auto-cpufreq
- opt-auto-cpufreq
- etc-auto-cpufreq-conf
environment:
LC_ALL: C.UTF-8
LANG: C.UTF-8
Expand Down

0 comments on commit 242a8d0

Please sign in to comment.