From 239a8a307c38e016ed123e0f12944dbe8c456f81 Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Tue, 21 Jan 2020 22:20:54 +0100 Subject: [PATCH 01/31] split auto-cpufreq into bin and source (core) --- auto-cpufreq.py => app_source/core.py | 69 +-------------------------- bin/auto-cpufreq | 68 ++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 67 deletions(-) rename auto-cpufreq.py => app_source/core.py (84%) mode change 100755 => 100644 create mode 100755 bin/auto-cpufreq diff --git a/auto-cpufreq.py b/app_source/core.py old mode 100755 new mode 100644 similarity index 84% rename from auto-cpufreq.py rename to app_source/core.py index d935b09..09434b1 --- a/auto-cpufreq.py +++ b/app_source/core.py @@ -1,8 +1,6 @@ #!/usr/bin/env python3 # -# auto-cpufreq - Automatic CPU speed & power optimizer for Linux -# -# Blog post: http://foolcontrol.org/?p=3124 +# auto-cpufreq - core functionality import subprocess import os @@ -382,67 +380,4 @@ def running_check(): print("----") print("\nTo view live log run:\n\tauto-cpufreq --log") footer(79) - sys.exit() - -# cli -@click.command() -@click.option("--monitor", is_flag=True, help="Monitor and suggest CPU optimizations") -@click.option("--live", is_flag=True, help="Monitor and make suggested CPU optimizations") -@click.option("--install/--remove", default=True, help="Install/remove daemon for automatic CPU optimizations") -@click.option("--log", is_flag=True, help="View live CPU optimization log made by daemon") -@click.option("--daemon", is_flag=True, hidden=True) - -def cli(monitor, live, daemon, install, log): - # print --help by default if no argument is provided when auto-cpufreq is run - if len(sys.argv) == 1: - print("\n" + "-" * 22 + " auto-cpufreq " + "-" * 23 + "\n") - print("Automatic CPU speed & power optimizer for Linux") - print("\nExample usage:\npython3 " + tool_run + " --monitor") - print("\n-----\n") - - s.call(["python3", "auto-cpufreq.py", "--help"]) - print("\n" + "-" * 59 + "\n") - else: - if daemon: - while True: - root_check() - gov_check() - cpufreqctl() - sysinfo() - set_autofreq() - countdown(5) - subprocess.call("clear") - elif monitor: - while True: - running_check() - root_check() - gov_check() - cpufreqctl() - sysinfo() - mon_autofreq() - countdown(5) - subprocess.call("clear") - elif live: - while True: - running_check() - root_check() - gov_check() - cpufreqctl() - sysinfo() - set_autofreq() - countdown(5) - subprocess.call("clear") - elif log: - read_log() - elif install: - running_check() - root_check() - gov_check() - deploy() - elif remove: - root_check() - remove() - -if __name__ == '__main__': - # while True: - cli() + sys.exit() \ No newline at end of file diff --git a/bin/auto-cpufreq b/bin/auto-cpufreq new file mode 100755 index 0000000..f1ca988 --- /dev/null +++ b/bin/auto-cpufreq @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 + +import sys +sys.path.append('../') +from app_source.core import * + +# cli +@click.command() +@click.option("--monitor", is_flag=True, help="Monitor and suggest CPU optimizations") +@click.option("--live", is_flag=True, help="Monitor and make suggested CPU optimizations") +@click.option("--install/--remove", default=True, help="Install/remove daemon for automatic CPU optimizations") +@click.option("--log", is_flag=True, help="View live CPU optimization log made by daemon") +@click.option("--daemon", is_flag=True, hidden=True) + +def main(monitor, live, daemon, install, log): + # print --help by default if no argument is provided when auto-cpufreq is run + 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") + + s.call(["python3", "auto-cpufreq", "--help"]) + footer(79) + else: + if daemon: + while True: + root_check() + gov_check() + cpufreqctl() + sysinfo() + set_autofreq() + countdown(5) + subprocess.call("clear") + elif monitor: + while True: + running_check() + root_check() + gov_check() + cpufreqctl() + sysinfo() + mon_autofreq() + countdown(5) + subprocess.call("clear") + elif live: + while True: + running_check() + root_check() + gov_check() + cpufreqctl() + sysinfo() + set_autofreq() + countdown(5) + subprocess.call("clear") + elif log: + read_log() + elif install: + running_check() + root_check() + gov_check() + deploy() + elif remove: + root_check() + remove() + +if __name__ == '__main__': + # while True: + main() From 144c7b5aeb99ed6e4e25b705ea519eca01553843 Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Tue, 21 Jan 2020 23:44:42 +0100 Subject: [PATCH 02/31] added necessary files for snap + first working snap version --- bin/auto-cpufreq | 8 ++++++-- requirements.txt | 4 ++++ setup.py | 35 ++++++++++++++++++++++++++++++++++ snap/snapcraft.yaml | 34 +++++++++++++++++++++++++++++++++ {app_source => source}/core.py | 0 5 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 requirements.txt create mode 100644 setup.py create mode 100644 snap/snapcraft.yaml rename {app_source => source}/core.py (100%) diff --git a/bin/auto-cpufreq b/bin/auto-cpufreq index f1ca988..0b7f680 100755 --- a/bin/auto-cpufreq +++ b/bin/auto-cpufreq @@ -1,8 +1,12 @@ #!/usr/bin/env python3 +# +# auto-cpufreq - Automatic CPU speed & power optimizer for Linux +# +# Blog post: http://foolcontrol.org/?p=3124 import sys sys.path.append('../') -from app_source.core import * +from source.core import * # cli @click.command() @@ -20,7 +24,7 @@ def main(monitor, live, daemon, install, log): print("\nExample usage:\nauto-cpufreq --monitor") print("\n-----\n") - s.call(["python3", "auto-cpufreq", "--help"]) + s.call(["python3", "bin/auto-cpufreq", "--help"]) footer(79) else: if daemon: diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..5984983 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +psutil +click +distro +power \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..b32d50f --- /dev/null +++ b/setup.py @@ -0,0 +1,35 @@ +import os + +from setuptools import setup + +with open('README.md') as readme_file: + readme = readme_file.read() + +this = os.path.dirname(os.path.realpath(__file__)) + +def read(name): + with open(os.path.join(this, name)) as f: + return f.read() +setup( + name='auto-cpufreq', + version='1.0', + description='Automatic CPU speed & power optimizer for Linux', + long_description=readme, + author='Adnan Hodzic', + author_email='adnan@hodzic.org', + url='https://github.com/AdnanHodzic/auto-cpufreq', + packages=['source'], + install_requires=read('requirements.txt'), + include_package_data=True, + zip_safe=True, + licence='GPLv3', + keywords='linux cpu speed power frequency turbo optimzier auto cpufreq', + classifiers=[ + 'Development Status :: 5 - Production/Stable', + 'Intended Audience :: Developers', + 'Operating System :: POSIX :: Linux' + 'Environment :: Console' + 'Natural Language :: English' + ], + scripts=['bin/auto-cpufreq'] +) \ No newline at end of file diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml new file mode 100644 index 0000000..57709a0 --- /dev/null +++ b/snap/snapcraft.yaml @@ -0,0 +1,34 @@ +name: auto-cpufreq +base: core18 +version: '1.0' # just for humans, typically '1.2+git' or '1.3.2' +summary: Automatic CPU speed & power optimizer for Linux +description: | + Automatic CPU speed & power optimizer for Linux based on active + monitoring of laptop's battery state, CPU usage and system load. + Ultimately allowing you to improve battery life without making + any compromises. + +grade: devel # must be 'stable' to release into candidate/stable channels +confinement: devmode # use 'strict' once you have the right plugs and slots + +parts: + auto-cpufreq: + # See 'snapcraft plugins' + plugin: python + python-version: python3 + source: . + #stage-packages: + # - python3-pip + # - python3-dev + #python-packages: + # - psutil + # - click + # - distro + # - power + +apps: + auto-cpufreq: + command: bin/auto-cpufreq + environment: + LC_ALL: C.UTF-8 + LANG: C.UTF-8 \ No newline at end of file diff --git a/app_source/core.py b/source/core.py similarity index 100% rename from app_source/core.py rename to source/core.py From ee813abfdd3abcef8ff164c926d0fa4a7b1e220a Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Wed, 22 Jan 2020 20:46:02 +0100 Subject: [PATCH 03/31] added deploy-cpufreqctl --- bin/auto-cpufreq | 4 +++- snap/snapcraft.yaml | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/bin/auto-cpufreq b/bin/auto-cpufreq index 0b7f680..528a300 100755 --- a/bin/auto-cpufreq +++ b/bin/auto-cpufreq @@ -24,7 +24,9 @@ def main(monitor, live, daemon, install, log): print("\nExample usage:\nauto-cpufreq --monitor") print("\n-----\n") - s.call(["python3", "bin/auto-cpufreq", "--help"]) + # ToDo: add option what to run depending on the env + s.call(["python3", "auto-cpufreq", "--help"]) + #s.call([tool_run, " --help"]) footer(79) else: if daemon: diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 57709a0..193dfcc 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -26,6 +26,16 @@ parts: # - distro # - power + deploy-cpufrectl: + plugin: dump + source: scripts + organize: + cpufreqctl.sh: usr/bin/cpufreqctl + # organize: + # auto-cpufreq-install.sh: usr/bin/auto-cpufreq-install + # organize: + # auto-cpufreq-remove.sh: usr/bin/auto-cpufreq-remove + apps: auto-cpufreq: command: bin/auto-cpufreq From c78ea8dd597f4d4f9d04d8469e8a2ea064228aeb Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Wed, 22 Jan 2020 22:26:27 +0100 Subject: [PATCH 04/31] added option to detect if running on snap --- bin/auto-cpufreq | 1 + snap/snapcraft.yaml | 7 ++----- source/core.py | 10 +++++++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/bin/auto-cpufreq b/bin/auto-cpufreq index 528a300..11cb23c 100755 --- a/bin/auto-cpufreq +++ b/bin/auto-cpufreq @@ -4,6 +4,7 @@ # # Blog post: http://foolcontrol.org/?p=3124 +# core import import sys sys.path.append('../') from source.core import * diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 193dfcc..76e83e1 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -31,14 +31,11 @@ parts: source: scripts organize: cpufreqctl.sh: usr/bin/cpufreqctl - # organize: - # auto-cpufreq-install.sh: usr/bin/auto-cpufreq-install - # organize: - # auto-cpufreq-remove.sh: usr/bin/auto-cpufreq-remove apps: auto-cpufreq: command: bin/auto-cpufreq environment: LC_ALL: C.UTF-8 - LANG: C.UTF-8 \ No newline at end of file + LANG: C.UTF-8 + PKG_MARKER: SNAP \ No newline at end of file diff --git a/source/core.py b/source/core.py index 09434b1..72b510d 100644 --- a/source/core.py +++ b/source/core.py @@ -37,11 +37,15 @@ auto_cpufreq_log_file = "/var/log/auto-cpufreq.log" # deploy cpufreqctl script def cpufreqctl(): - # deploy cpufreqctl script (if missing) - if os.path.isfile("/usr/bin/cpufreqctl"): + # detect if running on a SNAP + if os.getenv('PKG_MARKER') == "SNAP": pass else: - os.system("cp scripts/cpufreqctl.sh /usr/bin/cpufreqctl") + # deploy cpufreqctl script (if missing) + if os.path.isfile("/usr/bin/cpufreqctl"): + pass + else: + os.system("cp scripts/cpufreqctl.sh /usr/bin/cpufreqctl") def footer(l): print("\n" + "-" * l + "\n") From 577e00b74da143e3499607d34728ee99a869c54b Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Thu, 23 Jan 2020 07:12:04 +0100 Subject: [PATCH 05/31] fix display of help dialog in snap --- bin/auto-cpufreq | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bin/auto-cpufreq b/bin/auto-cpufreq index 11cb23c..dbed8d9 100755 --- a/bin/auto-cpufreq +++ b/bin/auto-cpufreq @@ -26,7 +26,10 @@ def main(monitor, live, daemon, install, log): print("\n-----\n") # ToDo: add option what to run depending on the env - s.call(["python3", "auto-cpufreq", "--help"]) + if os.getenv('PKG_MARKER') == "SNAP": + s.call(["auto-cpufreq", "--help"]) + else: + s.call(["python3", "auto-cpufreq", "--help"]) #s.call([tool_run, " --help"]) footer(79) else: From d1f4dcd33b85dd341c5a81d8854e0849d40b1dc7 Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Fri, 24 Jan 2020 15:53:25 +0100 Subject: [PATCH 06/31] working daemon as part of snap (dirty) --- bin/auto-cpufreq | 4 ++++ scripts/daemon.sh | 3 +++ snap/snapcraft.yaml | 17 ++++++++++++++++- source/core.py | 4 ++++ 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100755 scripts/daemon.sh diff --git a/bin/auto-cpufreq b/bin/auto-cpufreq index dbed8d9..aaba36d 100755 --- a/bin/auto-cpufreq +++ b/bin/auto-cpufreq @@ -36,6 +36,9 @@ def main(monitor, live, daemon, install, log): if daemon: while True: root_check() + # this is needed on SELinux enabled systems (see also ConditionPathExists in .service) + #create_log() + # ToDo: unless daemon invoked from snap fail gov_check() cpufreqctl() sysinfo() @@ -63,6 +66,7 @@ def main(monitor, live, daemon, install, log): countdown(5) subprocess.call("clear") elif log: + # ToDo: fail if log is missing or empty (on) read_log() elif install: running_check() diff --git a/scripts/daemon.sh b/scripts/daemon.sh new file mode 100755 index 0000000..d8eebfd --- /dev/null +++ b/scripts/daemon.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +auto-cpufreq --daemon 2>&1 | tee -a /var/log/auto-cpufreq.log diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 76e83e1..a6012f9 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -32,10 +32,25 @@ parts: organize: cpufreqctl.sh: usr/bin/cpufreqctl +# ToDo: remove scripts/daemon + # deploy-demon: + # plugin: dump + # source: scripts + # organize: + # daemon.sh: usr/bin/daemon + apps: auto-cpufreq: command: bin/auto-cpufreq environment: LC_ALL: C.UTF-8 LANG: C.UTF-8 - PKG_MARKER: SNAP \ No newline at end of file + PKG_MARKER: SNAP + auto-cpufreq-service: + command: bin/auto-cpufreq --daemon 2>&1 | tee -a /var/log/auto-cpufreq.log + environment: + LC_ALL: C.UTF-8 + LANG: C.UTF-8 + PKG_MARKER: SNAP + daemon: simple + \ No newline at end of file diff --git a/source/core.py b/source/core.py index 72b510d..8d3101c 100644 --- a/source/core.py +++ b/source/core.py @@ -365,6 +365,10 @@ def read_log(): print("ERROR: auto-cpufreq log is missing.\n\nMake sure to run: \"python3 auto-cpufreq.py --install\" first") footer(79) +# create log func +def creat_log(): + open("/var/log/auto-cpufreq.log", "a").close() + def running_check(): daemon_marker = False for proc in p.process_iter(): From d6c0abe072ff05e426fcdad54836b5faffdeb7f7 Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Sat, 25 Jan 2020 17:39:17 +0100 Subject: [PATCH 07/31] fixed daemon install/removal with setup.py --- bin/auto-cpufreq | 3 --- scripts/auto-cpufreq-install.sh | 2 +- source/__init__.py | 0 source/core.py | 13 ++++++------- 4 files changed, 7 insertions(+), 11 deletions(-) create mode 100644 source/__init__.py diff --git a/bin/auto-cpufreq b/bin/auto-cpufreq index aaba36d..1a0f12a 100755 --- a/bin/auto-cpufreq +++ b/bin/auto-cpufreq @@ -36,9 +36,6 @@ def main(monitor, live, daemon, install, log): if daemon: while True: root_check() - # this is needed on SELinux enabled systems (see also ConditionPathExists in .service) - #create_log() - # ToDo: unless daemon invoked from snap fail gov_check() cpufreqctl() sysinfo() diff --git a/scripts/auto-cpufreq-install.sh b/scripts/auto-cpufreq-install.sh index bb586fa..5ec93b0 100755 --- a/scripts/auto-cpufreq-install.sh +++ b/scripts/auto-cpufreq-install.sh @@ -15,7 +15,7 @@ fi touch /var/log/auto-cpufreq.log echo -e "\n* Deploy auto-cpufreq systemd unit file" -cp scripts/auto-cpufreq.service /etc/systemd/system/auto-cpufreq.service +cp ../scripts/auto-cpufreq.service /etc/systemd/system/auto-cpufreq.service echo -e "\n* Reloading systemd manager configuration" systemctl daemon-reload diff --git a/source/__init__.py b/source/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/source/core.py b/source/core.py index 8d3101c..cc6261a 100644 --- a/source/core.py +++ b/source/core.py @@ -72,13 +72,13 @@ def deploy(): print("\nERROR:\nWas unable to turn off bluetooth on boot") print("\n* Deploy auto-cpufreq as system wide accessible binary") - os.system("cp auto-cpufreq.py /usr/bin/auto-cpufreq") + os.system("cp auto-cpufreq /usr/bin/auto-cpufreq") print("\n* Deploy auto-cpufreq install script") - os.system("cp scripts/auto-cpufreq-install.sh /usr/bin/auto-cpufreq-install") + os.system("cp ../scripts/auto-cpufreq-install.sh /usr/bin/auto-cpufreq-install") print("\n* Deploy auto-cpufreq remove script") - os.system("cp scripts/auto-cpufreq-remove.sh /usr/bin/auto-cpufreq-remove") + os.system("cp ../scripts/auto-cpufreq-remove.sh /usr/bin/auto-cpufreq-remove") # run auto-cpufreq daemon deploy script s.call("/usr/bin/auto-cpufreq-install", shell=True) @@ -104,6 +104,9 @@ def remove(): # run auto-cpufreq daemon install script s.call("/usr/bin/auto-cpufreq-remove", shell=True) + # remove auto-cpufreq-remove + os.remove("/usr/bin/auto-cpufreq-remove") + # check for necessary scaling governors def gov_check(): avail_gov = "/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors" @@ -365,10 +368,6 @@ def read_log(): print("ERROR: auto-cpufreq log is missing.\n\nMake sure to run: \"python3 auto-cpufreq.py --install\" first") footer(79) -# create log func -def creat_log(): - open("/var/log/auto-cpufreq.log", "a").close() - def running_check(): daemon_marker = False for proc in p.process_iter(): From 19b8f9a083ac0216413d9ccc480ddb44798aa2a3 Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Sun, 26 Jan 2020 10:13:16 +0100 Subject: [PATCH 08/31] improved daemon install/remove after setup.py install --- scripts/auto-cpufreq-install.sh | 5 +---- scripts/auto-cpufreq-remove.sh | 9 -------- scripts/auto-cpufreq.service | 2 +- source/core.py | 38 +++++++++++++++++++++++++++------ 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/scripts/auto-cpufreq-install.sh b/scripts/auto-cpufreq-install.sh index 5ec93b0..0243758 100755 --- a/scripts/auto-cpufreq-install.sh +++ b/scripts/auto-cpufreq-install.sh @@ -11,11 +11,8 @@ then exit 1 fi -# this is needed on SELinux enabled systems (see also ConditionPathExists in .service) -touch /var/log/auto-cpufreq.log - echo -e "\n* Deploy auto-cpufreq systemd unit file" -cp ../scripts/auto-cpufreq.service /etc/systemd/system/auto-cpufreq.service +cp /usr/local/share/auto-cpufreq/scripts/auto-cpufreq.service /etc/systemd/system/auto-cpufreq.service echo -e "\n* Reloading systemd manager configuration" systemctl daemon-reload diff --git a/scripts/auto-cpufreq-remove.sh b/scripts/auto-cpufreq-remove.sh index 2a248fd..4981696 100755 --- a/scripts/auto-cpufreq-remove.sh +++ b/scripts/auto-cpufreq-remove.sh @@ -26,13 +26,4 @@ systemctl daemon-reload echo -e "reset failed" systemctl reset-failed -echo -e "\n* Removing auto-cpufreq daemon install script" -rm /usr/bin/auto-cpufreq-install - -echo -e "\n* Removing auto-cpufreq binary" -rm /usr/bin/auto-cpufreq - -echo -e "\n* Removing auto-cpufreq log file" -rm /var/log/auto-cpufreq.log - echo -e "\n-------------------------------------------------------------------------------\n" diff --git a/scripts/auto-cpufreq.service b/scripts/auto-cpufreq.service index e47dbd3..5ab559b 100644 --- a/scripts/auto-cpufreq.service +++ b/scripts/auto-cpufreq.service @@ -6,7 +6,7 @@ ConditionPathExists=/var/log/auto-cpufreq.log [Service] Type=simple User=root -ExecStart=/usr/bin/python3 /usr/bin/auto-cpufreq --daemon +ExecStart=auto-cpufreq --daemon StandardOutput=file:/var/log/auto-cpufreq.log [Install] WantedBy=multi-user.target diff --git a/source/core.py b/source/core.py index cc6261a..ca53a84 100644 --- a/source/core.py +++ b/source/core.py @@ -2,6 +2,7 @@ # # auto-cpufreq - core functionality +# ToDo: re-order in a single line? import subprocess import os import sys @@ -10,6 +11,7 @@ import psutil import platform import click import power +import signal # ToDo: # - re-enable CPU fan speed display and make more generic and not only for thinkpad @@ -45,7 +47,7 @@ def cpufreqctl(): if os.path.isfile("/usr/bin/cpufreqctl"): pass else: - os.system("cp scripts/cpufreqctl.sh /usr/bin/cpufreqctl") + os.system("cp /usr/local/share/auto-cpufreq/scripts/cpufreqctl.sh /usr/bin/cpufreqctl") def footer(l): print("\n" + "-" * l + "\n") @@ -71,14 +73,19 @@ def deploy(): except: print("\nERROR:\nWas unable to turn off bluetooth on boot") - print("\n* Deploy auto-cpufreq as system wide accessible binary") - os.system("cp auto-cpufreq /usr/bin/auto-cpufreq") + #print("\n* Deploy auto-cpufreq as system wide accessible binary") + #os.system("cp auto-cpufreq /usr/bin/auto-cpufreq") + + # create log file + create_file(auto_cpufreq_log_file) + + # sudo python3 setup.py install --record files.txt print("\n* Deploy auto-cpufreq install script") - os.system("cp ../scripts/auto-cpufreq-install.sh /usr/bin/auto-cpufreq-install") + os.system("cp /usr/local/share/auto-cpufreq/scripts/auto-cpufreq-install.sh /usr/bin/auto-cpufreq-install") print("\n* Deploy auto-cpufreq remove script") - os.system("cp ../scripts/auto-cpufreq-remove.sh /usr/bin/auto-cpufreq-remove") + os.system("cp /usr/local/share/auto-cpufreq/scripts/auto-cpufreq-remove.sh /usr/bin/auto-cpufreq-remove") # run auto-cpufreq daemon deploy script s.call("/usr/bin/auto-cpufreq-install", shell=True) @@ -107,6 +114,9 @@ def remove(): # remove auto-cpufreq-remove os.remove("/usr/bin/auto-cpufreq-remove") + # delete log file + delete_file(auto_cpufreq_log_file) + # check for necessary scaling governors def gov_check(): avail_gov = "/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors" @@ -358,6 +368,15 @@ def sysinfo(): #current_fans = p.sensors_fans()['thinkpad'][0].current #print("\nCPU fan speed:", current_fans, "RPM") +# create file func +def create_file(file): + open(file, 'a').close() + +# delete file func +def delete_file(file): + if os.path.exists(file): + os.remove(file) + # read log func def read_log(): if os.path.isfile(auto_cpufreq_log_file): @@ -387,4 +406,11 @@ def running_check(): print("----") print("\nTo view live log run:\n\tauto-cpufreq --log") footer(79) - sys.exit() \ No newline at end of file + sys.exit() + +# kill process func +def kill_process(pstring): + for line in os.popen("ps ef | grep \"" + pstring + "\" | grep -v grep"): + fields = line.split() + pid = fields[0] + os.kill(int(pid), signal.SIGKILL) \ No newline at end of file From f30ae780ac3b78660ef0510470d23b8fddba78a5 Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Sun, 26 Jan 2020 17:45:11 +0100 Subject: [PATCH 09/31] minor code improvements/clean-up --- .gitignore | 1 + bin/auto-cpufreq | 7 +------ source/core.py | 11 +++-------- 3 files changed, 5 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index b6e4761..fa94bb5 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,7 @@ share/python-wheels/ .installed.cfg *.egg MANIFEST +files.txt # PyInstaller # Usually these files are written by a python script from a template diff --git a/bin/auto-cpufreq b/bin/auto-cpufreq index 1a0f12a..fbdda36 100755 --- a/bin/auto-cpufreq +++ b/bin/auto-cpufreq @@ -25,12 +25,7 @@ def main(monitor, live, daemon, install, log): print("\nExample usage:\nauto-cpufreq --monitor") print("\n-----\n") - # ToDo: add option what to run depending on the env - if os.getenv('PKG_MARKER') == "SNAP": - s.call(["auto-cpufreq", "--help"]) - else: - s.call(["python3", "auto-cpufreq", "--help"]) - #s.call([tool_run, " --help"]) + s.call(["auto-cpufreq", "--help"]) footer(79) else: if daemon: diff --git a/source/core.py b/source/core.py index ca53a84..fba7b89 100644 --- a/source/core.py +++ b/source/core.py @@ -21,7 +21,6 @@ import signal p = psutil pl = platform s = subprocess -tool_run = sys.argv[0] cpus = os.cpu_count() # get turbo boost state @@ -73,14 +72,9 @@ def deploy(): except: print("\nERROR:\nWas unable to turn off bluetooth on boot") - #print("\n* Deploy auto-cpufreq as system wide accessible binary") - #os.system("cp auto-cpufreq /usr/bin/auto-cpufreq") - # create log file create_file(auto_cpufreq_log_file) - # sudo python3 setup.py install --record files.txt - print("\n* Deploy auto-cpufreq install script") os.system("cp /usr/local/share/auto-cpufreq/scripts/auto-cpufreq-install.sh /usr/bin/auto-cpufreq-install") @@ -135,7 +129,8 @@ def gov_check(): def root_check(): if not os.geteuid() == 0: print("\n" + "-" * 33 + " Root check " + "-" * 34 + "\n") - sys.exit("ERROR:\n\nMust be run root for this functionality to work, i.e: \nsudo " + tool_run + "\n") + print("ERROR:\n\nMust be run root for this functionality to work, i.e: \nsudo auto-cpufreq") + footer(79) exit(1) # refresh countdown @@ -384,7 +379,7 @@ def read_log(): s.call(["tail", "-n 50", "-f", auto_cpufreq_log_file]) else: print("\n" + "-" * 30 + " auto-cpufreq log " + "-" * 31 + "\n") - print("ERROR: auto-cpufreq log is missing.\n\nMake sure to run: \"python3 auto-cpufreq.py --install\" first") + print("ERROR: auto-cpufreq log is missing.\n\nMake sure to run: \"auto-cpufreq --install\" first") footer(79) def running_check(): From 97d3754ad720513186143ccf7476627b22c0171c Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Wed, 5 Feb 2020 21:03:29 +0100 Subject: [PATCH 10/31] start snap systemd service if DAEMON env var set to ENABLED --- bin/auto-cpufreq | 55 +++++++++++++++++++++++++++++++++++---------- scripts/daemon.sh | 3 --- snap/snapcraft.yaml | 17 ++------------ 3 files changed, 45 insertions(+), 30 deletions(-) delete mode 100755 scripts/daemon.sh diff --git a/bin/auto-cpufreq b/bin/auto-cpufreq index fbdda36..024de77 100755 --- a/bin/auto-cpufreq +++ b/bin/auto-cpufreq @@ -29,14 +29,27 @@ def main(monitor, live, daemon, install, log): footer(79) else: if daemon: - while True: - root_check() - gov_check() - cpufreqctl() - sysinfo() - set_autofreq() - countdown(5) - subprocess.call("clear") + if os.getenv("PKG_MARKER") == "SNAP" and os.getenv("DAEMON") == "ENABLED": + while True: + root_check() + gov_check() + cpufreqctl() + sysinfo() + set_autofreq() + countdown(5) + subprocess.call("clear") + elif os.getenv("PKG_MARKER") != "SNAP": + while True: + root_check() + gov_check() + cpufreqctl() + sysinfo() + set_autofreq() + countdown(5) + subprocess.call("clear") + else: + sys.exit("Daemon non ENABLED") + exit(1) elif monitor: while True: running_check() @@ -61,11 +74,29 @@ def main(monitor, live, daemon, install, log): # ToDo: fail if log is missing or empty (on) read_log() elif install: - running_check() - root_check() - gov_check() - deploy() + if os.getenv('PKG_MARKER') == "SNAP": + os.environ["DAEMON"] = "ENABLED" + print(os.environ["DAEMON"]) + running_check() + root_check() + gov_check() + # ToDo: + # implement option to restart snap service? + else: + running_check() + root_check() + gov_check() + deploy() + # ToDo: add remove for snap elif remove: + if os.getenv('PKG_MARKER') == "SNAP": + root_check() + # ToDo: remove if not needed? + #kill_process("auto-cpufreq --daemon") + # ToDo: + # implement option to stop and restart snap service? + + else: root_check() remove() diff --git a/scripts/daemon.sh b/scripts/daemon.sh deleted file mode 100755 index d8eebfd..0000000 --- a/scripts/daemon.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash - -auto-cpufreq --daemon 2>&1 | tee -a /var/log/auto-cpufreq.log diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index a6012f9..48cd7e4 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -17,14 +17,6 @@ parts: plugin: python python-version: python3 source: . - #stage-packages: - # - python3-pip - # - python3-dev - #python-packages: - # - psutil - # - click - # - distro - # - power deploy-cpufrectl: plugin: dump @@ -32,13 +24,6 @@ parts: organize: cpufreqctl.sh: usr/bin/cpufreqctl -# ToDo: remove scripts/daemon - # deploy-demon: - # plugin: dump - # source: scripts - # organize: - # daemon.sh: usr/bin/daemon - apps: auto-cpufreq: command: bin/auto-cpufreq @@ -46,11 +31,13 @@ apps: LC_ALL: C.UTF-8 LANG: C.UTF-8 PKG_MARKER: SNAP + DAEMON: DISABLED auto-cpufreq-service: command: bin/auto-cpufreq --daemon 2>&1 | tee -a /var/log/auto-cpufreq.log environment: LC_ALL: C.UTF-8 LANG: C.UTF-8 PKG_MARKER: SNAP + DAEMON: DISABLED daemon: simple \ No newline at end of file From ea2c868a9d8a768790762a7dd265d002a94fc061 Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Thu, 6 Feb 2020 19:58:03 +0100 Subject: [PATCH 11/31] enable snap service by default + name change --- bin/auto-cpufreq | 1 - snap/snapcraft.yaml | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/bin/auto-cpufreq b/bin/auto-cpufreq index 024de77..fc16131 100755 --- a/bin/auto-cpufreq +++ b/bin/auto-cpufreq @@ -76,7 +76,6 @@ def main(monitor, live, daemon, install, log): elif install: if os.getenv('PKG_MARKER') == "SNAP": os.environ["DAEMON"] = "ENABLED" - print(os.environ["DAEMON"]) running_check() root_check() gov_check() diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 48cd7e4..61b40be 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -31,13 +31,12 @@ apps: LC_ALL: C.UTF-8 LANG: C.UTF-8 PKG_MARKER: SNAP - DAEMON: DISABLED - auto-cpufreq-service: + service: command: bin/auto-cpufreq --daemon 2>&1 | tee -a /var/log/auto-cpufreq.log environment: LC_ALL: C.UTF-8 LANG: C.UTF-8 PKG_MARKER: SNAP - DAEMON: DISABLED + DAEMON: ENABLED daemon: simple \ No newline at end of file From 207c176ecbb16891b4b492a81c863c0281a1b616 Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Thu, 6 Feb 2020 22:48:34 +0100 Subject: [PATCH 12/31] working remove/install of snap service --- bin/auto-cpufreq | 15 ++++++--------- snap/snapcraft.yaml | 2 +- source/core.py | 9 +-------- 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/bin/auto-cpufreq b/bin/auto-cpufreq index fc16131..9006960 100755 --- a/bin/auto-cpufreq +++ b/bin/auto-cpufreq @@ -29,7 +29,8 @@ def main(monitor, live, daemon, install, log): footer(79) else: if daemon: - if os.getenv("PKG_MARKER") == "SNAP" and os.getenv("DAEMON") == "ENABLED": + #if os.getenv("PKG_MARKER") == "SNAP" and os.getenv("DAEMON") == "ENABLED": + if os.getenv("PKG_MARKER") == "SNAP": while True: root_check() gov_check() @@ -75,25 +76,21 @@ def main(monitor, live, daemon, install, log): read_log() elif install: if os.getenv('PKG_MARKER') == "SNAP": - os.environ["DAEMON"] = "ENABLED" running_check() root_check() gov_check() - # ToDo: - # implement option to restart snap service? + #os.environ["DAEMON"] = "ENABLED" + s.run("snapctl start --enable auto-cpufreq", shell=True) else: running_check() root_check() gov_check() deploy() - # ToDo: add remove for snap elif remove: if os.getenv('PKG_MARKER') == "SNAP": root_check() - # ToDo: remove if not needed? - #kill_process("auto-cpufreq --daemon") - # ToDo: - # implement option to stop and restart snap service? + #s.run("snapctl stop --disable auto-cpufreq", shell=True) + s.run("snapctl stop --disable auto-cpufreq", shell=True) else: root_check() diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 61b40be..5ad853f 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -37,6 +37,6 @@ apps: LC_ALL: C.UTF-8 LANG: C.UTF-8 PKG_MARKER: SNAP - DAEMON: ENABLED + DAEMON: DISABLED daemon: simple \ No newline at end of file diff --git a/source/core.py b/source/core.py index fba7b89..a484c8a 100644 --- a/source/core.py +++ b/source/core.py @@ -401,11 +401,4 @@ def running_check(): print("----") print("\nTo view live log run:\n\tauto-cpufreq --log") footer(79) - sys.exit() - -# kill process func -def kill_process(pstring): - for line in os.popen("ps ef | grep \"" + pstring + "\" | grep -v grep"): - fields = line.split() - pid = fields[0] - os.kill(int(pid), signal.SIGKILL) \ No newline at end of file + sys.exit() \ No newline at end of file From 5ab8757696bbaed505ade2463091404f8b77804e Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Sat, 8 Feb 2020 14:32:58 +0100 Subject: [PATCH 13/31] daemon disabled on snap install, working install/remove --- bin/auto-cpufreq | 19 +++++++++++++------ scripts/auto-cpufreq-install.sh | 9 +-------- snap/snapcraft.yaml | 1 - source/core.py | 14 ++++++++++---- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/bin/auto-cpufreq b/bin/auto-cpufreq index 9006960..84b6dfa 100755 --- a/bin/auto-cpufreq +++ b/bin/auto-cpufreq @@ -9,6 +9,8 @@ import sys sys.path.append('../') from source.core import * +# ToD: replace every s.call with s.run + # cli @click.command() @click.option("--monitor", is_flag=True, help="Monitor and suggest CPU optimizations") @@ -29,8 +31,7 @@ def main(monitor, live, daemon, install, log): footer(79) else: if daemon: - #if os.getenv("PKG_MARKER") == "SNAP" and os.getenv("DAEMON") == "ENABLED": - if os.getenv("PKG_MARKER") == "SNAP": + if os.getenv("PKG_MARKER") == "SNAP" and dcheck == "enabled": while True: root_check() gov_check() @@ -40,6 +41,7 @@ def main(monitor, live, daemon, install, log): countdown(5) subprocess.call("clear") elif os.getenv("PKG_MARKER") != "SNAP": + # ToDo: disable running daemon without install (like on snap) while True: root_check() gov_check() @@ -49,10 +51,13 @@ def main(monitor, live, daemon, install, log): countdown(5) subprocess.call("clear") else: - sys.exit("Daemon non ENABLED") + print("\n" + "-" * 32 + " Daemon check " + "-" * 33 + "\n") + print("ERROR:\n\nDaemon not enabled, must run install first, i.e: \nsudo auto-cpufreq --install") + footer(79) exit(1) elif monitor: while True: + # ToDo: doesn't work on snap running_check() root_check() gov_check() @@ -63,6 +68,7 @@ def main(monitor, live, daemon, install, log): subprocess.call("clear") elif live: while True: + # ToDo: doesn't work on snap running_check() root_check() gov_check() @@ -79,19 +85,20 @@ def main(monitor, live, daemon, install, log): running_check() root_check() gov_check() - #os.environ["DAEMON"] = "ENABLED" + s.run("snapctl set daemon=enabled", shell=True) s.run("snapctl start --enable auto-cpufreq", shell=True) + deploy_complete_msg() else: running_check() root_check() gov_check() deploy() + deploy_complete_msg() elif remove: if os.getenv('PKG_MARKER') == "SNAP": root_check() - #s.run("snapctl stop --disable auto-cpufreq", shell=True) + s.run("snapctl set daemon=disabled", shell=True) s.run("snapctl stop --disable auto-cpufreq", shell=True) - else: root_check() remove() diff --git a/scripts/auto-cpufreq-install.sh b/scripts/auto-cpufreq-install.sh index 0243758..c800e60 100755 --- a/scripts/auto-cpufreq-install.sh +++ b/scripts/auto-cpufreq-install.sh @@ -24,11 +24,4 @@ echo -e "\n* Starting auto-cpufreq daemon (systemd) service" systemctl start auto-cpufreq echo -e "\n* Enabling auto-cpufreq daemon (systemd) service at boot" -systemctl enable auto-cpufreq - -echo -e "\n------------------ auto-cpufreq daemon installed and running -----------------\n" - -echo -e "To view live log, run:\nauto-cpufreq --log" -echo -e "\nTo disable and remove auto-cpufreq daemon, run:\nsudo auto-cpufreq --remove" - -echo -e "\n-------------------------------------------------------------------------------\n" +systemctl enable auto-cpufreq \ No newline at end of file diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 5ad853f..a8124fa 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -37,6 +37,5 @@ apps: LC_ALL: C.UTF-8 LANG: C.UTF-8 PKG_MARKER: SNAP - DAEMON: DISABLED daemon: simple \ No newline at end of file diff --git a/source/core.py b/source/core.py index a484c8a..f64a83e 100644 --- a/source/core.py +++ b/source/core.py @@ -36,6 +36,9 @@ bat_state = power.PowerManagement().get_providing_power_source_type() # auto-cpufreq log file auto_cpufreq_log_file = "/var/log/auto-cpufreq.log" +# daemon check +dcheck = subprocess.getoutput("snapctl get daemon") + # deploy cpufreqctl script def cpufreqctl(): # detect if running on a SNAP @@ -48,9 +51,16 @@ def cpufreqctl(): else: os.system("cp /usr/local/share/auto-cpufreq/scripts/cpufreqctl.sh /usr/bin/cpufreqctl") +# print footer func def footer(l): print("\n" + "-" * l + "\n") +def deploy_complete_msg(): + print("\n" + "-" * 17 + " auto-cpufreq daemon installed and running " + "-" * 17 + "\n") + print("To view live log, run:\nauto-cpufreq --log") + print("\nTo disable and remove auto-cpufreq daemon, run:\nsudo auto-cpufreq --remove") + footer(79) + # deploy auto-cpufreq daemon def deploy(): @@ -165,12 +175,10 @@ def set_powersave(): elif cpuload > 50: print("High CPU load, setting turbo boost: on") s.run("echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo", shell=True) - #print("\n" + "-" * 60 + "\n") footer(79) else: print("Load optimal, setting turbo boost: off") s.run("echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo", shell=True) - #print("\n" + "-" * 60 + "\n") footer(79) # make turbo suggestions in powersave @@ -228,12 +236,10 @@ def set_performance(): elif cpuload > 20: print("High CPU load, setting turbo boost: on") s.run("echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo", shell=True) - #print("\n" + "-" * 60 + "\n") footer(79) else: print("Load optimal, setting turbo boost: off") s.run("echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo", shell=True) - #print("\n" + "-" * 60 + "\n") footer(79) # make turbo suggestions in performance From 0627859711db8c69bf73ff1de14f4bbb26072a3a Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Sat, 8 Feb 2020 17:04:43 +0100 Subject: [PATCH 14/31] disable running multiple instances (needs improvements on snap) --- bin/auto-cpufreq | 11 ++++------- source/core.py | 42 ++++++++++++++++++++++++------------------ 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/bin/auto-cpufreq b/bin/auto-cpufreq index 84b6dfa..86e0090 100755 --- a/bin/auto-cpufreq +++ b/bin/auto-cpufreq @@ -41,7 +41,6 @@ def main(monitor, live, daemon, install, log): countdown(5) subprocess.call("clear") elif os.getenv("PKG_MARKER") != "SNAP": - # ToDo: disable running daemon without install (like on snap) while True: root_check() gov_check() @@ -57,9 +56,8 @@ def main(monitor, live, daemon, install, log): exit(1) elif monitor: while True: - # ToDo: doesn't work on snap - running_check() root_check() + running_daemon() # doesn't work on snap gov_check() cpufreqctl() sysinfo() @@ -68,9 +66,8 @@ def main(monitor, live, daemon, install, log): subprocess.call("clear") elif live: while True: - # ToDo: doesn't work on snap - running_check() root_check() + running_daemon() # doesn't work on snap gov_check() cpufreqctl() sysinfo() @@ -82,15 +79,15 @@ def main(monitor, live, daemon, install, log): read_log() elif install: if os.getenv('PKG_MARKER') == "SNAP": - running_check() root_check() + running_daemon() # doesn't work on snap gov_check() s.run("snapctl set daemon=enabled", shell=True) s.run("snapctl start --enable auto-cpufreq", shell=True) deploy_complete_msg() else: - running_check() root_check() + running_daemon() # doesn't work on snap gov_check() deploy() deploy_complete_msg() diff --git a/source/core.py b/source/core.py index f64a83e..a867f20 100644 --- a/source/core.py +++ b/source/core.py @@ -388,23 +388,29 @@ def read_log(): print("ERROR: auto-cpufreq log is missing.\n\nMake sure to run: \"auto-cpufreq --install\" first") footer(79) -def running_check(): - daemon_marker = False - for proc in p.process_iter(): +# check if program (argument) is running +def is_running(program, argument): + # iterate over all process id's found by psutil + for pid in psutil.pids(): try: - pinfo = proc.as_dict(attrs=['pid', 'name', 'cmdline']) - except p.NoSuchProcess: - pass - else: - if pinfo['name'] == 'python3' and \ - '/usr/bin/auto-cpufreq' in pinfo['cmdline'] and '--daemon' in pinfo['cmdline']: - daemon_marker = True + # requests the process information corresponding to each process id + p = psutil.Process(pid) + # check if value of program-variable that was used to call the function matches the name field of the plutil.Process(pid) output + if program in p.name(): + # check output of p.name(), output name of program + # p.cmdline() - echo the exact command line via which p was called. + for arg in p.cmdline(): + if argument in str(arg): + return True + else: + pass + else: + pass + except: + continue - if daemon_marker: - print("\n" + "-" * 25 + " detected auto-cpufreq daemon" + "-" * 25 + "\n") - print("ERROR: prevention from running multiple instances") - print("\nIt seems like auto-cpufreq daemon is already running.\n") - print("----") - print("\nTo view live log run:\n\tauto-cpufreq --log") - footer(79) - sys.exit() \ No newline at end of file +# check if auto-cpufreq --daemon is running +def running_daemon(): + if is_running('auto-cpufreq', '--daemon'): + deploy_complete_msg() + exit(1) \ No newline at end of file From cf7f553b2c358f87e0bc587a9e98ac51245eef69 Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Sat, 8 Feb 2020 20:35:39 +0100 Subject: [PATCH 15/31] extended disable running multiple instances functionality to snap --- bin/auto-cpufreq | 8 ++++---- source/core.py | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/bin/auto-cpufreq b/bin/auto-cpufreq index 86e0090..9aa950b 100755 --- a/bin/auto-cpufreq +++ b/bin/auto-cpufreq @@ -57,7 +57,7 @@ def main(monitor, live, daemon, install, log): elif monitor: while True: root_check() - running_daemon() # doesn't work on snap + running_daemon() gov_check() cpufreqctl() sysinfo() @@ -67,7 +67,7 @@ def main(monitor, live, daemon, install, log): elif live: while True: root_check() - running_daemon() # doesn't work on snap + running_daemon() gov_check() cpufreqctl() sysinfo() @@ -80,14 +80,14 @@ def main(monitor, live, daemon, install, log): elif install: if os.getenv('PKG_MARKER') == "SNAP": root_check() - running_daemon() # doesn't work on snap + running_daemon() gov_check() s.run("snapctl set daemon=enabled", shell=True) s.run("snapctl start --enable auto-cpufreq", shell=True) deploy_complete_msg() else: root_check() - running_daemon() # doesn't work on snap + running_daemon() gov_check() deploy() deploy_complete_msg() diff --git a/source/core.py b/source/core.py index a867f20..b898980 100644 --- a/source/core.py +++ b/source/core.py @@ -412,5 +412,8 @@ def is_running(program, argument): # check if auto-cpufreq --daemon is running def running_daemon(): if is_running('auto-cpufreq', '--daemon'): + deploy_complete_msg() + exit(1) + elif os.getenv("PKG_MARKER") == "SNAP" and dcheck == "enabled": deploy_complete_msg() exit(1) \ No newline at end of file From 87752c1de6bdf758fbfd8add7b0af9e8fca2695e Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Sat, 8 Feb 2020 21:04:34 +0100 Subject: [PATCH 16/31] code refactor --- bin/auto-cpufreq | 3 +++ scripts/auto-cpufreq-remove.sh | 4 +--- source/core.py | 40 +++++++++++++++++++++------------- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/bin/auto-cpufreq b/bin/auto-cpufreq index 9aa950b..aa5cb02 100755 --- a/bin/auto-cpufreq +++ b/bin/auto-cpufreq @@ -96,9 +96,12 @@ def main(monitor, live, daemon, install, log): root_check() s.run("snapctl set daemon=disabled", shell=True) s.run("snapctl stop --disable auto-cpufreq", shell=True) + delete_file(auto_cpufreq_log_file) + remove_complete_msg() else: root_check() remove() + remove_complete_msg() if __name__ == '__main__': # while True: diff --git a/scripts/auto-cpufreq-remove.sh b/scripts/auto-cpufreq-remove.sh index 4981696..a387c33 100755 --- a/scripts/auto-cpufreq-remove.sh +++ b/scripts/auto-cpufreq-remove.sh @@ -24,6 +24,4 @@ echo -e "\n* Reloading systemd manager configuration" systemctl daemon-reload echo -e "reset failed" -systemctl reset-failed - -echo -e "\n-------------------------------------------------------------------------------\n" +systemctl reset-failed \ No newline at end of file diff --git a/source/core.py b/source/core.py index b898980..b7b43cd 100644 --- a/source/core.py +++ b/source/core.py @@ -24,7 +24,12 @@ s = subprocess cpus = os.cpu_count() # get turbo boost state -cur_turbo = s.getoutput("cat /sys/devices/system/cpu/intel_pstate/no_turbo") +turbo_loc = "/sys/devices/system/cpu/intel_pstate/no_turbo" +cur_turbo = s.getoutput("cat " + turbo_loc) + + # govs/script loc +avail_gov_loc = "/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors" +scripts_dir = "/usr/local/share/auto-cpufreq/scripts/" # get current scaling governor get_cur_gov = s.getoutput("cpufreqctl --governor") @@ -37,7 +42,7 @@ bat_state = power.PowerManagement().get_providing_power_source_type() auto_cpufreq_log_file = "/var/log/auto-cpufreq.log" # daemon check -dcheck = subprocess.getoutput("snapctl get daemon") +dcheck = s.getoutput("snapctl get daemon") # deploy cpufreqctl script def cpufreqctl(): @@ -49,7 +54,7 @@ def cpufreqctl(): if os.path.isfile("/usr/bin/cpufreqctl"): pass else: - os.system("cp /usr/local/share/auto-cpufreq/scripts/cpufreqctl.sh /usr/bin/cpufreqctl") + os.system("cp " + scripts_dir + "cpufreqctl.sh /usr/bin/cpufreqctl") # print footer func def footer(l): @@ -61,6 +66,11 @@ def deploy_complete_msg(): print("\nTo disable and remove auto-cpufreq daemon, run:\nsudo auto-cpufreq --remove") footer(79) +def remove_complete_msg(): + print("\n" + "-" * 25 + " auto-cpufreq daemon removed " + "-" * 25 + "\n") + print("auto-cpufreq successfully removed.") + footer(79) + # deploy auto-cpufreq daemon def deploy(): @@ -86,10 +96,10 @@ def deploy(): create_file(auto_cpufreq_log_file) print("\n* Deploy auto-cpufreq install script") - os.system("cp /usr/local/share/auto-cpufreq/scripts/auto-cpufreq-install.sh /usr/bin/auto-cpufreq-install") + os.system("cp " + scripts_dir + "/auto-cpufreq-install.sh /usr/bin/auto-cpufreq-install") print("\n* Deploy auto-cpufreq remove script") - os.system("cp /usr/local/share/auto-cpufreq/scripts/auto-cpufreq-remove.sh /usr/bin/auto-cpufreq-remove") + os.system("cp " + scripts_dir + "/auto-cpufreq-remove.sh /usr/bin/auto-cpufreq-remove") # run auto-cpufreq daemon deploy script s.call("/usr/bin/auto-cpufreq-install", shell=True) @@ -123,7 +133,7 @@ def remove(): # check for necessary scaling governors def gov_check(): - avail_gov = "/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors" + avail_gov = avail_gov_loc governors=["performance","powersave"] @@ -170,15 +180,15 @@ def set_powersave(): # conditions for setting turbo in powersave if load1m > cpus: print("High load, setting turbo boost: on") - s.run("echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo", shell=True) + s.run("echo 0 > " + turbo_loc, shell=True) footer(79) elif cpuload > 50: print("High CPU load, setting turbo boost: on") - s.run("echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo", shell=True) + s.run("echo 0 > " + turbo_loc, shell=True) footer(79) else: print("Load optimal, setting turbo boost: off") - s.run("echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo", shell=True) + s.run("echo 1 > " + turbo_loc, shell=True) footer(79) # make turbo suggestions in powersave @@ -231,15 +241,15 @@ def set_performance(): # conditions for setting turbo in performance if load1m > 1: print("High load, setting turbo boost: on") - s.run("echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo", shell=True) + s.run("echo 0 > " + turbo_loc, shell=True) footer(79) elif cpuload > 20: print("High CPU load, setting turbo boost: on") - s.run("echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo", shell=True) + s.run("echo 0 > " + turbo_loc, shell=True) footer(79) else: print("Load optimal, setting turbo boost: off") - s.run("echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo", shell=True) + s.run("echo 1 > " + turbo_loc, shell=True) footer(79) # make turbo suggestions in performance @@ -391,12 +401,12 @@ def read_log(): # check if program (argument) is running def is_running(program, argument): # iterate over all process id's found by psutil - for pid in psutil.pids(): + for pid in psutil.pids(): try: # requests the process information corresponding to each process id - p = psutil.Process(pid) + p = psutil.Process(pid) # check if value of program-variable that was used to call the function matches the name field of the plutil.Process(pid) output - if program in p.name(): + if program in p.name(): # check output of p.name(), output name of program # p.cmdline() - echo the exact command line via which p was called. for arg in p.cmdline(): From 93e7da4e4ae9e705254b60539a4df3704a5adbbf Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Sun, 9 Feb 2020 09:16:48 +0100 Subject: [PATCH 17/31] display correct (host) distro information on snap --- source/core.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/source/core.py b/source/core.py index b7b43cd..676f6d9 100644 --- a/source/core.py +++ b/source/core.py @@ -318,9 +318,25 @@ def sysinfo(): print("\n" + "-" * 29 + " System information " + "-" * 30 + "\n") import distro - # get info about linux distro - fdist = distro.linux_distribution() - dist = " ".join(x for x in fdist) + + # get distro information in snap env. + if os.getenv("PKG_MARKER") == "SNAP": + searchfile = open("/var/lib/snapd/hostfs/etc/os-release", "r") + for line in searchfile: + if line.startswith('NAME='): + distro = line[5:line.find('$')].strip("\"") + continue + elif line.startswith('VERSION='): + version = line[8:line.find('$')].strip("\"") + continue + + dist = distro + " " + version + searchfile.close() + else: + # get distro information + fdist = distro.linux_distribution() + dist = " ".join(x for x in fdist) + print("Linux distro: " + dist) print("Linux kernel: " + pl.release()) From 83796c458c89e83419fbc9700d84f927186cfafe Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Sun, 9 Feb 2020 11:59:39 +0100 Subject: [PATCH 18/31] snapcraft file refinement --- snap/snapcraft.yaml | 10 ++++------ source/core.py | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index a8124fa..23cec3e 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -1,6 +1,6 @@ name: auto-cpufreq base: core18 -version: '1.0' # just for humans, typically '1.2+git' or '1.3.2' +version: '1.0' summary: Automatic CPU speed & power optimizer for Linux description: | Automatic CPU speed & power optimizer for Linux based on active @@ -8,12 +8,11 @@ description: | Ultimately allowing you to improve battery life without making any compromises. -grade: devel # must be 'stable' to release into candidate/stable channels -confinement: devmode # use 'strict' once you have the right plugs and slots +grade: stable +confinement: classic parts: auto-cpufreq: - # See 'snapcraft plugins' plugin: python python-version: python3 source: . @@ -37,5 +36,4 @@ apps: LC_ALL: C.UTF-8 LANG: C.UTF-8 PKG_MARKER: SNAP - daemon: simple - \ No newline at end of file + daemon: simple \ No newline at end of file diff --git a/source/core.py b/source/core.py index 676f6d9..9972fd8 100644 --- a/source/core.py +++ b/source/core.py @@ -321,7 +321,7 @@ def sysinfo(): # get distro information in snap env. if os.getenv("PKG_MARKER") == "SNAP": - searchfile = open("/var/lib/snapd/hostfs/etc/os-release", "r") + searchfile = open("/usr/lib/os-release", "r") for line in searchfile: if line.startswith('NAME='): distro = line[5:line.find('$')].strip("\"") From 2a2bbc2565e545a48aae5879934e8d8c590643ed Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Sun, 9 Feb 2020 14:44:06 +0100 Subject: [PATCH 19/31] added source code installer --- .gitignore | 4 + auto-cpufreq-installer | 163 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100755 auto-cpufreq-installer diff --git a/.gitignore b/.gitignore index fa94bb5..e21e8a4 100644 --- a/.gitignore +++ b/.gitignore @@ -128,3 +128,7 @@ dmypy.json # Pyre type checker .pyre/ + +# vim +*.swp +*.swo diff --git a/auto-cpufreq-installer b/auto-cpufreq-installer new file mode 100755 index 0000000..31c00cc --- /dev/null +++ b/auto-cpufreq-installer @@ -0,0 +1,163 @@ +#!/bin/bash +# +# auto-cpufreq install tool + +distro="$(lsb_release -is)" +release="$(lsb_release -rs)" +codename="$(lsb_release -cs)" + +separator(){ +sep="\n-------------------------------------------------------------------" +echo -e $sep +} + +# root check +root_check(){ +if (( $EUID != 0 )); +then + separator + echo -e "\nMust be run as root (i.e: 'sudo $0')." + separator + exit 1 +fi +} + +# python packages install +pip_pkg_install(){ +pip3 install psutil click distro power +} + +complete_msg(){ +echo -e "\nauto-cpufreq tool successfully installed.\n" +echo -e "For list of options, run:\nauto-cpufreq" +} + +# tool install +install(){ +python3 setup.py install --record files.txt +mkdir -p /usr/local/share/auto-cpufreq/ +cp -r scripts/ /usr/local/share/auto-cpufreq/ +} + +tool_install(){ +# Debian +if [ -f /etc/debian_version ]; +then + separator + echo -e "\nDetected Debian based distribution" + separator + echo -e "\nSetting up Python environment\n" + apt install python3-dev python3-pip -y + separator + echo -e "\nInstalling necessary Python packages\n" + pip_pkg_install + separator + echo -e "\ninstalling auto-cpufreq\n" + install + separator + complete_msg + separator +# RedHat +elif [ -f /etc/redhat-release ]; +then + separator + echo -e "\nDetected RedHat based distribution\n" + echo -e "\nSetting up Python environment\n" + # CentOS exception + if [ -f /etc/centos-release ]; + then + yum install platform-python-devel + else + yum install python-devel + fi + echo -e "\nInstalling necessary Python packages\n" + pip_pkg_install + separator + echo -e "\ninstalling auto-cpufreq\n" + install + separator + complete_msg + separator +# Other +else + separator + echo -e "\nDidn't detect Debian or RedHat based distro.\n" + echo -e "To complete installation, you need to:" + echo -e "Install: python3 and pip3\n" + echo -e "Install necessary Python packages:" + echo -e "pip3 install psutil click distro power" + echo -e "\nRun following sequence of lines:" + echo -e "\n-----" + echo -e "\npython3 setup.py install --record files.txt" + echo -e "mkdir -p /usr/local/share/auto-cpufreq/" + echo -e "cp -r scripts/ /usr/local/share/auto-cpufreq/" + echo -e "\n-----" + echo -e "\nAfter which tool is installed, for full list of options run:" + echo -e "auto-cpufreq" + separator + echo -e "\nConsider creating a feature request to add support for your distro:" + echo -e "https://github.com/AdnanHodzic/auto-cpufreq/issues/new" + echo -e "\nMake sure to include following information:\n" + echo -e "Distribution: $distro" + echo -e "Release: $release" + echo -e "Codename: $codename" + separator +fi +} + +tool_remove(){ +cat files.txt | xargs sudo rm -rf && rm -f files.txt +sudo rm -rf /usr/local/share/auto-cpufreq/ + +separator +echo -e "\nauto-cpufreq successfully removed.\n" +} + +ask_operation(){ +echo -e "\n-------------------------- auto-cpufreq installer -----------------------------" +echo -e "\nWelcome to auto-cpufreq tool installer." +echo -e "\nOptions:\n" +read -p "[I]nstall +[R]emove +[Q]uit + +Select a key: [i/r/q]: " answer +} + +root_check + +if [[ -z "${1}" ]]; +then + ask_operation +else + case "${1}" in + "--install") + answer="i" + ;; + "--remove") + answer="r" + ;; + *) + answer="n" + ;; + esac +fi + +if [[ $answer == [Ii] ]]; +then + root_check + tool_install +elif [[ $answer == [Rr] ]]; +then + root_check + tool_remove +elif [[ $answer == [Qq] ]]; +then + separator + echo "" + exit 0 +else + separator + echo -e "\nUnknown key, aborting ...\n" + exit 1 +fi From e8c0b97cfedfe7fbf4e4caf6019ba8778126ba20 Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Sun, 9 Feb 2020 14:56:20 +0100 Subject: [PATCH 20/31] updated README following new packaging format --- README.md | 71 ++++++++++++++++++------------------------------------- 1 file changed, 23 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 2b33969..1d2ddc7 100644 --- a/README.md +++ b/README.md @@ -33,74 +33,52 @@ Please note: this tool doesn't conflict and [works great in tandem with TLP](htt * System load * Automatic CPU & power optimization (temporary and persistent) -## How to run auto-cpufreq? +## Installing auto-cpufreq -#### Arch Linux +### From source code + +Get source code, run installer and follow on screen instructions: + +``` +git clone https://github.com/AdnanHodzic/auto-cpufreq.git +cd auto-cpufreq && sudo sudo ./auto-cpufreq-installer +``` + +### Arch Linux [AUR package is available](https://aur.archlinux.org/packages/auto-cpufreq-git/) for install. After which `auto-cpufreq` will be available as a binary and you can skip to [auto-cpufreq: modes and options](https://github.com/AdnanHodzic/auto-cpufreq#auto-cpufreq-modes-and-options) for further reference. -#### Get auto-cpufreq source code +## How to run auto-cpufreq -`git clone https://github.com/AdnanHodzic/auto-cpufreq.git` +auto-cpufreq can be run by simply running the `auto-cpufreq` and following on screen instructions, i.e: -#### Install requirements +`sudo auto-cpufreq` -##### Requirements installation for Debian/Ubuntu and their derivatives - -All requirements can be installed by running: - -`sudo apt install python3-dev python3-pip python3-distro python3-psutil python3-click python3-power -y` - -Since APT packages may contain older version of necessary Python packages, please make sure to have latest version by running: -`sudo pip3 install --upgrade psutil click distro power` - -##### Requirements installation for Redhat/Fedora and its derivatives - -Necessary pre-requisites can be installed by running: - -`sudo yum install python-devel` - -After which you need to[ need to install rest of the requirements](https://github.com/AdnanHodzic/auto-cpufreq#requirements-installation-for-all-other-linux-distributions) as stated below. - -##### Requirements installation for all other Linux distributions - -If you have python3 and pip3 installed simply run: - -`sudo pip3 install psutil click distro power` - -Note: libraries must be installed using root user as tool will be run as root. - -##### Running auto-cpufreq on AMD CPU and/or desktop/servers's +#### Running auto-cpufreq on AMD CPU and/or desktop/servers's Please note when this tool was made it was solely targeting Intel CPU's running on laptops. Support for both AMD CPU is [currently being worked on](https://github.com/AdnanHodzic/auto-cpufreq/issues/17). While there are plans to [extend support for desktop/servers](https://www.reddit.com/r/linux/comments/ejxx9f/github_autocpufreq_automatic_cpu_speed_power/fd5nodm/) in near future. -#### Run auto-cpufreq - -auto-cpufreq can be run by simply running the `auto-cpufreq.py` and following on screen instructions, i.e: - -`sudo python3 auto-cpufreq.py` - ## auto-cpufreq modes and options -#### Monitor +### Monitor -`sudo python3 auto-cpufreq.py --monitor` +`sudo auto-cpufreq --monitor` No changes are made to the system, and is solely made for demonstration purposes what auto-cpufreq could do differently for your system. -#### Live +### Live -`sudo python3 auto-cpufreq.py --live` +`sudo auto-cpufreq --live` Necessary changes are temporarily made to the system which are lost with system reboot. This mode is made to evaluate what the system would behave with auto-cpufreq permanently running on the system. -#### Install - auto-cpufreq daemon +### Install - auto-cpufreq daemon Necessary changes are made to the system for auto-cpufreq CPU optimizaton to persist across reboots. Daemon is deployed and then started as a systemd service. Changes are made automatically and live log is made for monitoring purposes. -`sudo python3 auto-cpufreq.py --install` +`sudo auto-cpufreq --install` After daemon is installed, `auto-cpufreq` is available as a binary and is running in the background. Its logs can be viewed by running: `auto-cpufreq --log` @@ -108,20 +86,17 @@ Since daemon is running as a systemd service, its status can be seen by running: `systemctl status auto-cpufreq` -#### Remove - auto-cpufreq daemon +### Remove - auto-cpufreq daemon auto-cpufreq daemon and its systemd service, along with all its persistent changes can be removed by running: `sudo auto-cpufreq --remove` -or -`sudo python3 auto-cpufreq.py --remove` -#### Log +### Log If daemon has been instaled, live log of CPU/system load monitoring and optimizaiton can be seen by running: `auto-cpufreq --log` -or `sudo python3 auto-cpufreq.py --log` ## Discussion: From d8e0f3fc6e89389d35c5489ee4f295073a4e1a0b Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Mon, 10 Feb 2020 17:51:18 +0100 Subject: [PATCH 21/31] added necessary interfaces for strict confinement --- snap/snapcraft.yaml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 23cec3e..d13f3bf 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -9,7 +9,7 @@ description: | any compromises. grade: stable -confinement: classic +confinement: strict parts: auto-cpufreq: @@ -30,6 +30,11 @@ apps: LC_ALL: C.UTF-8 LANG: C.UTF-8 PKG_MARKER: SNAP + plugs: + - cpu-control + - system-observe + - hardware-observe + - system-files service: command: bin/auto-cpufreq --daemon 2>&1 | tee -a /var/log/auto-cpufreq.log environment: From 232450f936ab3388842ac4b0bcef6f50f477f18d Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Mon, 10 Feb 2020 18:16:56 +0100 Subject: [PATCH 22/31] code improvements for snap to work in strict confinement --- scripts/cpufreqctl.sh | 2 +- snap/snapcraft.yaml | 14 ++++++++++---- source/core.py | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/scripts/cpufreqctl.sh b/scripts/cpufreqctl.sh index 008f0da..81c5453 100755 --- a/scripts/cpufreqctl.sh +++ b/scripts/cpufreqctl.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash VERSION='20' -cpucount=`nproc` +cpucount=`cat /proc/cpuinfo|grep processor|wc -l` FLROOT=/sys/devices/system/cpu DRIVER=auto VERBOSE=0 diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index d13f3bf..897aad6 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -26,17 +26,23 @@ parts: apps: auto-cpufreq: command: bin/auto-cpufreq - environment: - LC_ALL: C.UTF-8 - LANG: C.UTF-8 - PKG_MARKER: SNAP plugs: - cpu-control - system-observe - hardware-observe - system-files + environment: + LC_ALL: C.UTF-8 + LANG: C.UTF-8 + PKG_MARKER: SNAP + service: command: bin/auto-cpufreq --daemon 2>&1 | tee -a /var/log/auto-cpufreq.log + plugs: + - cpu-control + - system-observe + - hardware-observe + - system-files environment: LC_ALL: C.UTF-8 LANG: C.UTF-8 diff --git a/source/core.py b/source/core.py index 9972fd8..676f6d9 100644 --- a/source/core.py +++ b/source/core.py @@ -321,7 +321,7 @@ def sysinfo(): # get distro information in snap env. if os.getenv("PKG_MARKER") == "SNAP": - searchfile = open("/usr/lib/os-release", "r") + searchfile = open("/var/lib/snapd/hostfs/etc/os-release", "r") for line in searchfile: if line.startswith('NAME='): distro = line[5:line.find('$')].strip("\"") From 1d7cea31fd5a24d5680d7deccd0d1bd54904b8ad Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Tue, 11 Feb 2020 17:14:24 +0100 Subject: [PATCH 23/31] removed system-files plug --- snap/snapcraft.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 897aad6..915138c 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -30,7 +30,6 @@ apps: - cpu-control - system-observe - hardware-observe - - system-files environment: LC_ALL: C.UTF-8 LANG: C.UTF-8 @@ -42,7 +41,6 @@ apps: - cpu-control - system-observe - hardware-observe - - system-files environment: LC_ALL: C.UTF-8 LANG: C.UTF-8 From 3aebbd9a334dbbd137ebd849e48d0fcdfe7fa56c Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Wed, 12 Feb 2020 15:22:43 +0100 Subject: [PATCH 24/31] new location for daemon logs on snap --- snap/snapcraft.yaml | 2 +- source/core.py | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 915138c..ac89f99 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -36,7 +36,7 @@ apps: PKG_MARKER: SNAP service: - command: bin/auto-cpufreq --daemon 2>&1 | tee -a /var/log/auto-cpufreq.log + command: bin/auto-cpufreq --daemon 2>&1 | tee -a $SNAP_DATA/auto-cpufreq.log plugs: - cpu-control - system-observe diff --git a/source/core.py b/source/core.py index 676f6d9..79a52e1 100644 --- a/source/core.py +++ b/source/core.py @@ -40,6 +40,7 @@ bat_state = power.PowerManagement().get_providing_power_source_type() # auto-cpufreq log file auto_cpufreq_log_file = "/var/log/auto-cpufreq.log" +auto_cpufreq_log_file_snap = "/var/snap/auto-cpufreq/current/auto-cpufreq.log" # daemon check dcheck = s.getoutput("snapctl get daemon") @@ -406,8 +407,9 @@ def delete_file(file): # read log func def read_log(): - if os.path.isfile(auto_cpufreq_log_file): - # read /var/log/auto-cpufreq.log + if os.getenv("PKG_MARKER") == "SNAP": + s.call(["tail", "-n 50", "-f", auto_cpufreq_log_file_snap]) + elif os.path.isfile(auto_cpufreq_log_file): s.call(["tail", "-n 50", "-f", auto_cpufreq_log_file]) else: print("\n" + "-" * 30 + " auto-cpufreq log " + "-" * 31 + "\n") From aa80d3045b54126214dd2d852bf29d4ab4a76683 Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Sat, 15 Feb 2020 11:31:56 +0100 Subject: [PATCH 25/31] added snap store placeholder --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 1d2ddc7..4aa3869 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,10 @@ Please note: this tool doesn't conflict and [works great in tandem with TLP](htt ## Installing auto-cpufreq +### From Snap store + +To be updated ... + ### From source code Get source code, run installer and follow on screen instructions: @@ -44,6 +48,8 @@ git clone https://github.com/AdnanHodzic/auto-cpufreq.git cd auto-cpufreq && sudo sudo ./auto-cpufreq-installer ``` +In case you encounter any problems with `auto-cpufreq-installer`, please [submit a bug report](https://github.com/AdnanHodzic/auto-cpufreq/issues/new). + ### Arch Linux [AUR package is available](https://aur.archlinux.org/packages/auto-cpufreq-git/) for install. After which `auto-cpufreq` will be available as a binary and you can skip to [auto-cpufreq: modes and options](https://github.com/AdnanHodzic/auto-cpufreq#auto-cpufreq-modes-and-options) for further reference. From e9e65bff620b2107b53df6e7fd6c9dd33e2dda88 Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Sun, 16 Feb 2020 13:15:15 +0100 Subject: [PATCH 26/31] improved tool removal as part of auto-cpufreq-installer --- auto-cpufreq-installer | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/auto-cpufreq-installer b/auto-cpufreq-installer index 31c00cc..16b4e49 100755 --- a/auto-cpufreq-installer +++ b/auto-cpufreq-installer @@ -7,7 +7,7 @@ release="$(lsb_release -rs)" codename="$(lsb_release -cs)" separator(){ -sep="\n-------------------------------------------------------------------" +sep="\n-------------------------------------------------------------------------------" echo -e $sep } @@ -52,7 +52,7 @@ then echo -e "\nInstalling necessary Python packages\n" pip_pkg_install separator - echo -e "\ninstalling auto-cpufreq\n" + echo -e "\ninstalling auto-cpufreq tool\n" install separator complete_msg @@ -73,7 +73,7 @@ then echo -e "\nInstalling necessary Python packages\n" pip_pkg_install separator - echo -e "\ninstalling auto-cpufreq\n" + echo -e "\ninstalling auto-cpufreq tool\n" install separator complete_msg @@ -106,11 +106,34 @@ fi } tool_remove(){ -cat files.txt | xargs sudo rm -rf && rm -f files.txt -sudo rm -rf /usr/local/share/auto-cpufreq/ + +# remove daemon if installed/running +tool_proc="auto-cpufreq" +tool_proc_rm="auto-cpufreq --remove" + +if pgrep -x $tool_proc > /dev/null +then + eval pkill $tool_proc + eval $tool_proc_rm +fi + +# remove auto-cpufreq and all its supporting files +files="files.txt" +share_dir="/usr/local/share/auto-cpufreq/" +srv_install="/usr/bin/auto-cpufreq-install" +srv_remove="/usr/bin/auto-cpufreq-remove" +log_file="/var/log/auto-cpufreq.log" + +[ -f $files ] && cat $files | xargs sudo rm -rf && rm -f $files +[ -f $share_dir ] && rm -rf $share_dir + +# files cleanup +[ -f $srv_install ] && rm $srv_install +[ -f $srv_remove ] && rm $srv_remove +[ -f $log_file ] && rm $log_file separator -echo -e "\nauto-cpufreq successfully removed.\n" +echo -e "\nauto-cpufreq tool and all its supporting files successfully removed.\n" } ask_operation(){ From ba311ae16c7db57dc9535ed9f33e861f594ab0c8 Mon Sep 17 00:00:00 2001 From: Mert Can Demir Date: Sun, 16 Feb 2020 15:24:14 +0300 Subject: [PATCH 27/31] Fix file log freezing issue (#40) --- scripts/auto-cpufreq.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/auto-cpufreq.service b/scripts/auto-cpufreq.service index 5ab559b..e0f1fc3 100644 --- a/scripts/auto-cpufreq.service +++ b/scripts/auto-cpufreq.service @@ -7,6 +7,6 @@ ConditionPathExists=/var/log/auto-cpufreq.log Type=simple User=root ExecStart=auto-cpufreq --daemon -StandardOutput=file:/var/log/auto-cpufreq.log +StandardOutput=append:/var/log/auto-cpufreq.log [Install] WantedBy=multi-user.target From 08788a5bb763d7434d29b4a9dbb5323fec415a92 Mon Sep 17 00:00:00 2001 From: Mert Can Demir Date: Mon, 17 Feb 2020 10:48:13 +0300 Subject: [PATCH 28/31] Change some parameters for better use (#41) @validatedev these changes look good and work as equally good. Thank you for your contribution, and looking forward to seeing any of your new MR's in future :) --- source/core.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/source/core.py b/source/core.py index 79a52e1..bf03cb8 100644 --- a/source/core.py +++ b/source/core.py @@ -22,6 +22,7 @@ p = psutil pl = platform s = subprocess cpus = os.cpu_count() +pw = power # get turbo boost state turbo_loc = "/sys/devices/system/cpu/intel_pstate/no_turbo" @@ -36,7 +37,7 @@ get_cur_gov = s.getoutput("cpufreqctl --governor") gov_state = get_cur_gov.split()[0] # get battery state -bat_state = power.PowerManagement().get_providing_power_source_type() +bat_state = pw.PowerManagement().get_providing_power_source_type() # auto-cpufreq log file auto_cpufreq_log_file = "/var/log/auto-cpufreq.log" @@ -179,11 +180,11 @@ def set_powersave(): print("Total system load:", load1m, "\n") # conditions for setting turbo in powersave - if load1m > cpus: + if load1m > cpus / 7: print("High load, setting turbo boost: on") s.run("echo 0 > " + turbo_loc, shell=True) footer(79) - elif cpuload > 50: + elif cpuload > 25: print("High CPU load, setting turbo boost: on") s.run("echo 0 > " + turbo_loc, shell=True) footer(79) @@ -203,7 +204,7 @@ def mon_powersave(): print("\nTotal CPU usage:", cpuload, "%") print("Total system load:", load1m, "\n") - if load1m > 2: + if load1m > cpus / 7: print("High load, suggesting to set turbo boost: on") if cur_turbo == "0": print("Currently turbo boost is: on") @@ -240,7 +241,7 @@ def set_performance(): print("Total system load:", load1m, "\n") # conditions for setting turbo in performance - if load1m > 1: + if load1m >= cpus / 5: print("High load, setting turbo boost: on") s.run("echo 0 > " + turbo_loc, shell=True) footer(79) @@ -278,36 +279,36 @@ def set_autofreq(): print("\n" + "-" * 28 + " CPU frequency scaling " + "-" * 28 + "\n") # get battery state - bat_state = power.PowerManagement().get_providing_power_source_type() + bat_state = pw.PowerManagement().get_providing_power_source_type() # determine which governor should be used - if bat_state == power.POWER_TYPE_AC: + if bat_state == pw.POWER_TYPE_AC: print("Battery is: charging") set_performance() - elif bat_state == power.POWER_TYPE_BATTERY: + elif bat_state == pw.POWER_TYPE_BATTERY: print("Battery is: discharging") set_powersave() else: - print("Couldn't determine battery status. Please report this issue.") + print("Couldn't determine the battery status. Please report this issue.") # make cpufreq suggestions def mon_autofreq(): print("\n" + "-" * 28 + " CPU frequency scaling " + "-" * 28 + "\n") # get battery state - bat_state = power.PowerManagement().get_providing_power_source_type() + bat_state = pw.PowerManagement().get_providing_power_source_type() # determine which governor should be used - if bat_state == power.POWER_TYPE_AC: + if bat_state == pw.POWER_TYPE_AC: print("Battery is: charging") print("Suggesting use of \"performance\" governor\nCurrently using:", gov_state) mon_performance() - elif bat_state == power.POWER_TYPE_BATTERY: + elif bat_state == pw.POWER_TYPE_BATTERY: print("Battery is: discharging") print("Suggesting use of \"powersave\" governor\nCurrently using:", gov_state) mon_powersave() else: - print("Couldn't determine battery status. Please report this issue.") + print("Couldn't determine the battery status. Please report this issue.") # get system information def sysinfo(): From 5396ea23cfb284acf501a800cfcc4c8f5e971890 Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Mon, 17 Feb 2020 10:06:58 +0100 Subject: [PATCH 29/31] updated source installer removal + proccess --- auto-cpufreq-installer | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/auto-cpufreq-installer b/auto-cpufreq-installer index 16b4e49..a1f9aa6 100755 --- a/auto-cpufreq-installer +++ b/auto-cpufreq-installer @@ -107,23 +107,28 @@ fi tool_remove(){ -# remove daemon if installed/running -tool_proc="auto-cpufreq" -tool_proc_rm="auto-cpufreq --remove" - -if pgrep -x $tool_proc > /dev/null -then - eval pkill $tool_proc - eval $tool_proc_rm -fi - -# remove auto-cpufreq and all its supporting files files="files.txt" share_dir="/usr/local/share/auto-cpufreq/" srv_install="/usr/bin/auto-cpufreq-install" srv_remove="/usr/bin/auto-cpufreq-remove" log_file="/var/log/auto-cpufreq.log" +tool_proc_rm="auto-cpufreq --remove" +# stop any running auto-cpufreq argument (daemon/live/monitor) +tool_arg_pids=( $(pgrep -f "auto-cpufreq --") ) +for pid in "${tool_arg_pids[@]}"; do + if [[ $tool_arg_pids != $$ ]]; then + kill "$tool_arg_pids" + fi +done + +# run uninstall in case of installed daemon +if [ -f $srv_remove ] +then + eval $tool_proc_rm +fi + +# remove auto-cpufreq and all its supporting files [ -f $files ] && cat $files | xargs sudo rm -rf && rm -f $files [ -f $share_dir ] && rm -rf $share_dir From 04638bad232f76feda7ff8542276f6394fa77dda Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Fri, 6 Mar 2020 10:38:12 +0100 Subject: [PATCH 30/31] minor changes to auto-cpufreq-installer --- auto-cpufreq-installer | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/auto-cpufreq-installer b/auto-cpufreq-installer index a1f9aa6..1b993f9 100755 --- a/auto-cpufreq-installer +++ b/auto-cpufreq-installer @@ -1,6 +1,7 @@ #!/bin/bash # -# auto-cpufreq install tool +# auto-cpufreq-installer: +# auto-cpufreq source code based installer distro="$(lsb_release -is)" release="$(lsb_release -rs)" @@ -93,7 +94,7 @@ else echo -e "cp -r scripts/ /usr/local/share/auto-cpufreq/" echo -e "\n-----" echo -e "\nAfter which tool is installed, for full list of options run:" - echo -e "auto-cpufreq" + echo -e "auto-cpufreq" separator echo -e "\nConsider creating a feature request to add support for your distro:" echo -e "https://github.com/AdnanHodzic/auto-cpufreq/issues/new" @@ -188,4 +189,4 @@ else separator echo -e "\nUnknown key, aborting ...\n" exit 1 -fi +fi \ No newline at end of file From 0f001d9943b937543f535ad07868c39956272716 Mon Sep 17 00:00:00 2001 From: Adnan Hodzic Date: Mon, 16 Mar 2020 16:36:06 +0100 Subject: [PATCH 31/31] Update README.md added update on snap package --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4aa3869..f2013de 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,9 @@ Please note: this tool doesn't conflict and [works great in tandem with TLP](htt ### From Snap store -To be updated ... +auto-cpufreq snap package is created and is ready in [snap store](https://snapcraft.io/store). However it's still unreleased to public due to changes which first need to be [made as part of snapd 2.4.4](https://github.com/snapcore/snapd/pull/8127). + +Hence, please stay tuned for auto-cpufreq snap package release. ### From source code