major code refactor and improvements
This commit is contained in:
parent
2490de007a
commit
4cbc608791
112
auto-cpufreq.py
112
auto-cpufreq.py
|
@ -33,6 +33,10 @@ bat_state = p.sensors_battery().power_plugged
|
||||||
# auto-cpufreq log file
|
# auto-cpufreq log file
|
||||||
auto_cpufreq_log_file = "/var/log/auto-cpufreq.log"
|
auto_cpufreq_log_file = "/var/log/auto-cpufreq.log"
|
||||||
|
|
||||||
|
# daemon deployment marker
|
||||||
|
daemon_marker = "/etc/opt/auto-cpufreq"
|
||||||
|
|
||||||
|
|
||||||
# driver check
|
# driver check
|
||||||
driver = s.getoutput("cpufreqctl --driver")
|
driver = s.getoutput("cpufreqctl --driver")
|
||||||
|
|
||||||
|
@ -55,10 +59,6 @@ def deploy():
|
||||||
# deploy cpufreqctl script func call
|
# deploy cpufreqctl script func call
|
||||||
cpufreqctl()
|
cpufreqctl()
|
||||||
|
|
||||||
# delete /var/log/auto-cpufreq.log if it exists (make sure file gets updated accordingly)
|
|
||||||
if os.path.exists(auto_cpufreq_log_file):
|
|
||||||
os.remove(auto_cpufreq_log_file)
|
|
||||||
|
|
||||||
print("* Turn off bluetooth on boot")
|
print("* Turn off bluetooth on boot")
|
||||||
btconf="/etc/bluetooth/main.conf"
|
btconf="/etc/bluetooth/main.conf"
|
||||||
try:
|
try:
|
||||||
|
@ -75,39 +75,23 @@ def deploy():
|
||||||
print("\n* Deploy auto-cpufreq as system wide accessible binary")
|
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.py /usr/bin/auto-cpufreq")
|
||||||
|
|
||||||
print("\n* Deploy auto-cpufreq daemon deploy script")
|
print("\n* Deploy auto-cpufreq install script")
|
||||||
os.system("cp scripts/auto-cpufreq-deploy.sh /usr/bin/auto-cpufreq-deploy")
|
os.system("cp scripts/auto-cpufreq-install.sh /usr/bin/auto-cpufreq-install")
|
||||||
|
|
||||||
#print("\n* Deploy auto-cpufreq daemon run script")
|
print("\n* Deploy auto-cpufreq remove script")
|
||||||
#os.system("cp scripts/auto-cpufreq-run.sh /usr/bin/auto-cpufreq-run")
|
|
||||||
|
|
||||||
print("\n* Deploy auto-cpufreq daemon removal 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")
|
||||||
|
|
||||||
print("\n* Deploy auto-cpufreq systemd unit file")
|
print("\n* Deploy auto-cpufreq systemd unit file")
|
||||||
os.system("cp scripts/auto-cpufreq.service /lib/systemd/system/auto-cpufreq.service")
|
os.system("cp scripts/auto-cpufreq.service /lib/systemd/system/auto-cpufreq.service")
|
||||||
|
|
||||||
# run auto-cpufreq daemon deploy script
|
# run auto-cpufreq daemon deploy script
|
||||||
s.call("/usr/bin/auto-cpufreq-deploy", shell=True)
|
s.call("/usr/bin/auto-cpufreq-install", shell=True)
|
||||||
|
|
||||||
#print("auto-cpufreq daemon started and will automatically start at boot time.")
|
|
||||||
#print("\nTo disable and remove auto-cpufreq daemon, run:\nsudo auto-cpufreq --remove")
|
|
||||||
|
|
||||||
#print("\nTo view live auto-cpufreq daemon logs, run:\nauto-cpufreq --log")
|
|
||||||
#footer(79)
|
|
||||||
|
|
||||||
# remove auto-cpufreq daemon
|
# remove auto-cpufreq daemon
|
||||||
def remove():
|
def remove():
|
||||||
|
|
||||||
print("\n" + "-" * 21 + " Removing auto-cpufreq daemon " + "-" * 22 + "\n")
|
print("\n" + "-" * 21 + " Removing auto-cpufreq daemon " + "-" * 22 + "\n")
|
||||||
|
|
||||||
# delete /var/log/auto-cpufreq.log if it exists
|
|
||||||
#if os.path.exists(auto_cpufreq_log_file):
|
|
||||||
# os.remove(auto_cpufreq_log_file)
|
|
||||||
# delete /var/log/auto-cpufreq.log file
|
|
||||||
#print("\n* Delete log file")
|
|
||||||
#os.remove(auto_cpufreq_log_file)
|
|
||||||
|
|
||||||
print("* Turn on bluetooth on boot")
|
print("* Turn on bluetooth on boot")
|
||||||
btconf="/etc/bluetooth/main.conf"
|
btconf="/etc/bluetooth/main.conf"
|
||||||
try:
|
try:
|
||||||
|
@ -121,38 +105,9 @@ def remove():
|
||||||
except:
|
except:
|
||||||
print("\nERROR:\nWas unable to turn on bluetooth on boot")
|
print("\nERROR:\nWas unable to turn on bluetooth on boot")
|
||||||
|
|
||||||
#f os.path("/usr/bin/auto-cpufreq-deploy"):
|
# run auto-cpufreq daemon install script
|
||||||
## print("\n* Remove auto-cpufreq daemon deploy script")
|
|
||||||
# os.remove("/usr/bin/auto-cpufreq-deploy")
|
|
||||||
|
|
||||||
#if os.path("/usr/bin/auto-cpufreq-run"):
|
|
||||||
# print("\n* Remove auto-cpufreq daemon run script")
|
|
||||||
# os.remove("/usr/bin/auto-cpufreq-run")
|
|
||||||
|
|
||||||
# run auto-cpufreq daemon deploy script
|
|
||||||
s.call("/usr/bin/auto-cpufreq-remove", shell=True)
|
s.call("/usr/bin/auto-cpufreq-remove", shell=True)
|
||||||
|
|
||||||
#if os.path("/usr/bin/auto-cpufreq-remove"):
|
|
||||||
# print("\n* Remove auto-cpufreq-remove script")
|
|
||||||
# os.remove("/usr/bin/auto-cpufreq-remove")
|
|
||||||
|
|
||||||
#if os.path.isfile("/lib/systemd/system/auto-cpufreq.service"):
|
|
||||||
# print("\n* Remove auto-cpufreq systemd unit file")
|
|
||||||
# os.remove("/lib/systemd/system/auto-cpufreq.service")
|
|
||||||
|
|
||||||
#if os.path("/usr/bin/auto-cpufreq"):
|
|
||||||
# print("\n* Remove auto-cpufreq binary")
|
|
||||||
# os.remove("/usr/bin/auto-cpufreq")
|
|
||||||
|
|
||||||
# delete /var/log/auto-cpufreq.log if it exists
|
|
||||||
#if os.path.exists(auto_cpufreq_log_file):
|
|
||||||
# print("\n* Delete log file")
|
|
||||||
# os.remove(auto_cpufreq_log_file)
|
|
||||||
|
|
||||||
print("\nauto-cpufreq daemon removed")
|
|
||||||
|
|
||||||
footer(79)
|
|
||||||
|
|
||||||
# check for necessary scaling governors
|
# check for necessary scaling governors
|
||||||
def gov_check():
|
def gov_check():
|
||||||
avail_gov = "/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors"
|
avail_gov = "/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors"
|
||||||
|
@ -185,8 +140,6 @@ def countdown(s):
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
#sys.stdout.write("\rComplete! \n")
|
|
||||||
|
|
||||||
# set powersave and enable turbo
|
# set powersave and enable turbo
|
||||||
def set_powersave():
|
def set_powersave():
|
||||||
print("Setting to use: powersave")
|
print("Setting to use: powersave")
|
||||||
|
@ -391,38 +344,39 @@ def sysinfo():
|
||||||
#current_fans = p.sensors_fans()['thinkpad'][0].current
|
#current_fans = p.sensors_fans()['thinkpad'][0].current
|
||||||
#print("\nCPU fan speed:", current_fans, "RPM")
|
#print("\nCPU fan speed:", current_fans, "RPM")
|
||||||
|
|
||||||
|
# read log func
|
||||||
def read_log():
|
def read_log():
|
||||||
|
|
||||||
# deploy cpufreqctl script (if missing)
|
|
||||||
if os.path.isfile(auto_cpufreq_log_file):
|
if os.path.isfile(auto_cpufreq_log_file):
|
||||||
# read /var/log/auto-cpufreq.log
|
# read /var/log/auto-cpufreq.log
|
||||||
s.call(["tail", "-n 50", "-f", auto_cpufreq_log_file])
|
s.call(["tail", "-n 50", "-f", auto_cpufreq_log_file])
|
||||||
else:
|
else:
|
||||||
print("\n" + "-" * 30 + " auto-cpufreq log " + "-" * 31 + "\n")
|
print("\n" + "-" * 30 + " auto-cpufreq log " + "-" * 31 + "\n")
|
||||||
print("ERROR:\n\nauto-cpufreq log is missing.\n\nMake sure to run: \"python3 auto-cpufreq.py --daemon\" first")
|
print("ERROR: auto-cpufreq log is missing.\n\nMake sure to run: \"python3 auto-cpufreq.py --install\" first")
|
||||||
footer(79)
|
footer(79)
|
||||||
|
|
||||||
def log_check():
|
def marker_check():
|
||||||
if os.path.isfile(auto_cpufreq_log_file):
|
if os.path.isfile(daemon_marker):
|
||||||
print("\n" + "-" * 30 + " auto-cpufreq log " + "-" * 31 + "\n")
|
print("\n" + "-" * 25 + " detected auto-cpufreq daemon" + "-" * 25 + "\n")
|
||||||
print("ERROR: prevention from running multiple instances.")
|
print("ERROR: prevention from running multiple instances")
|
||||||
print("\nIt seems like auto-cpufreq daemon is already running in background.\n\nTo view live log run:\nauto-cpufreq --log")
|
print("\nIt seems like auto-cpufreq daemon is already running.\n\nTo view live log run:\nauto-cpufreq --log")
|
||||||
print("\nTo disable and remove auto-cpufreq daemon, run:\nsudo auto-cpufreq --remove")
|
print("\nTo disable and remove auto-cpufreq daemon, run:\nsudo auto-cpufreq --remove")
|
||||||
footer(79)
|
footer(79)
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
def trunk():
|
def marker_create(f):
|
||||||
with open(auto_cpufreq_log_file, 'w'):
|
f = open(f, "w")
|
||||||
pass
|
f.write("auto-cpufreq daemon intsall marker")
|
||||||
|
f.close()
|
||||||
|
|
||||||
# cli
|
# cli
|
||||||
@click.command()
|
@click.command()
|
||||||
@click.option("--monitor", is_flag=True, help="Monitor and suggest CPU optimizations")
|
@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("--live", is_flag=True, help="Monitor and make suggested CPU optimizations")
|
||||||
@click.option("--daemon/--remove", default=True, help="Automatically 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("--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, log):
|
def cli(monitor, live, daemon, install, log):
|
||||||
# print --help by default if no argument is provided when auto-cpufreq is run
|
# print --help by default if no argument is provided when auto-cpufreq is run
|
||||||
if len(sys.argv) == 1:
|
if len(sys.argv) == 1:
|
||||||
print("\n" + "-" * 22 + " auto-cpufreq " + "-" * 23 + "\n")
|
print("\n" + "-" * 22 + " auto-cpufreq " + "-" * 23 + "\n")
|
||||||
|
@ -433,9 +387,18 @@ def cli(monitor, live, daemon, log):
|
||||||
s.call(["python3", "auto-cpufreq.py", "--help"])
|
s.call(["python3", "auto-cpufreq.py", "--help"])
|
||||||
print("\n" + "-" * 59 + "\n")
|
print("\n" + "-" * 59 + "\n")
|
||||||
else:
|
else:
|
||||||
if monitor:
|
if daemon:
|
||||||
while True:
|
while True:
|
||||||
log_check()
|
root_check()
|
||||||
|
gov_check()
|
||||||
|
cpufreqctl()
|
||||||
|
sysinfo()
|
||||||
|
set_autofreq()
|
||||||
|
countdown(5)
|
||||||
|
subprocess.call("clear")
|
||||||
|
elif monitor:
|
||||||
|
while True:
|
||||||
|
marker_check()
|
||||||
root_check()
|
root_check()
|
||||||
gov_check()
|
gov_check()
|
||||||
cpufreqctl()
|
cpufreqctl()
|
||||||
|
@ -445,7 +408,7 @@ def cli(monitor, live, daemon, log):
|
||||||
subprocess.call("clear")
|
subprocess.call("clear")
|
||||||
elif live:
|
elif live:
|
||||||
while True:
|
while True:
|
||||||
#log_check()
|
marker_check()
|
||||||
root_check()
|
root_check()
|
||||||
gov_check()
|
gov_check()
|
||||||
cpufreqctl()
|
cpufreqctl()
|
||||||
|
@ -455,11 +418,12 @@ def cli(monitor, live, daemon, log):
|
||||||
subprocess.call("clear")
|
subprocess.call("clear")
|
||||||
elif log:
|
elif log:
|
||||||
read_log()
|
read_log()
|
||||||
elif daemon:
|
elif install:
|
||||||
log_check()
|
marker_check()
|
||||||
root_check()
|
root_check()
|
||||||
gov_check()
|
gov_check()
|
||||||
deploy()
|
deploy()
|
||||||
|
marker_create(daemon_marker)
|
||||||
elif remove:
|
elif remove:
|
||||||
root_check()
|
root_check()
|
||||||
remove()
|
remove()
|
||||||
|
|
|
@ -1,31 +0,0 @@
|
||||||
#!/usr/bin/env bash
|
|
||||||
#
|
|
||||||
# auto-cpufreq daemon deploy script
|
|
||||||
# reference: https://github.com/AdnanHodzic/auto-cpufreq
|
|
||||||
|
|
||||||
echo -e "\n------------------ Running auto-cpufreq daemon deploy script ------------------"
|
|
||||||
|
|
||||||
if [[ $EUID != 0 ]];
|
|
||||||
then
|
|
||||||
echo -e "\nERROR\nMust be run as root (i.e: 'sudo $0')\n"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
#touch /var/log/auto-cpufreq.log
|
|
||||||
|
|
||||||
echo -e "\n* Reloading systemd manager configuration"
|
|
||||||
systemctl daemon-reload
|
|
||||||
|
|
||||||
echo -e "\n* Stopping auto-cpufreq systemd service"
|
|
||||||
systemctl stop auto-cpufreq
|
|
||||||
|
|
||||||
echo -e "\n* Starting auto-cpufreq systemd service"
|
|
||||||
systemctl start auto-cpufreq
|
|
||||||
|
|
||||||
#echo -e "\n* Enabling auto-cpufreq systemd service at boot"
|
|
||||||
#systemctl enable auto-cpufreq
|
|
||||||
|
|
||||||
#echo -e "\n* Running auto-cpufreq binary"
|
|
||||||
#/usr/bin/python3 /usr/bin/auto-cpufreq --live > $logs_file 2>&1 &
|
|
||||||
|
|
||||||
echo -e "\n------\n"
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
#!/bin/bash
|
||||||
|
#
|
||||||
|
# auto-cpufreq daemon install script
|
||||||
|
# reference: https://github.com/AdnanHodzic/auto-cpufreq
|
||||||
|
|
||||||
|
echo -e "\n------------------ Running auto-cpufreq daemon install script ------------------"
|
||||||
|
|
||||||
|
if [[ $EUID != 0 ]];
|
||||||
|
then
|
||||||
|
echo -e "\nERROR\nMust be run as root (i.e: 'sudo $0')\n"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "\n* Reloading systemd manager configuration"
|
||||||
|
systemctl daemon-reload
|
||||||
|
|
||||||
|
echo -e "\n* Stopping auto-cpufreq daemon (systemd) service"
|
||||||
|
systemctl stop auto-cpufreq
|
||||||
|
|
||||||
|
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"
|
|
@ -11,24 +11,14 @@ then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo -e "\n* Stopping auto-cpufreq systemd service"
|
echo -e "\n* Stopping auto-cpufreq daemon (systemd) service"
|
||||||
systemctl stop auto-cpufreq
|
systemctl stop auto-cpufreq
|
||||||
|
|
||||||
echo -e "\n* Disabling auto-cpufreq systemd service at boot"
|
echo -e "\n* Disabling auto-cpufreq daemon (systemd) at boot"
|
||||||
systemctl disable auto-cpufreq
|
systemctl disable auto-cpufreq
|
||||||
|
|
||||||
#if [ -f /lib/systemd/system/auto-cpufreq.service ];
|
echo -e "\n* Removing auto-cpufreq daemon (systemd) unit file"
|
||||||
#then
|
|
||||||
# rm /lib/systemd/system/auto-cpufreq.service
|
|
||||||
# systemctl daemon-reload
|
|
||||||
|
|
||||||
#if [ -f /etc/systemd/system/auto-cpufreq.service ];
|
|
||||||
#then
|
|
||||||
# rm /etc/systemd/system/auto-cpufreq.service
|
|
||||||
# systemctl daemon-reload
|
|
||||||
|
|
||||||
rm /lib/systemd/system/auto-cpufreq.service
|
rm /lib/systemd/system/auto-cpufreq.service
|
||||||
rm /etc/systemd/system/auto-cpufreq.service
|
|
||||||
|
|
||||||
echo -e "\n* Reloading systemd manager configuration"
|
echo -e "\n* Reloading systemd manager configuration"
|
||||||
systemctl daemon-reload
|
systemctl daemon-reload
|
||||||
|
@ -36,11 +26,8 @@ systemctl daemon-reload
|
||||||
echo -e "reset failed"
|
echo -e "reset failed"
|
||||||
systemctl reset-failed
|
systemctl reset-failed
|
||||||
|
|
||||||
echo -e "\n* Removing auto-cpufreq daemon deploy script"
|
echo -e "\n* Removing auto-cpufreq daemon install script"
|
||||||
rm /usr/bin/auto-cpufreq-deploy
|
rm /usr/bin/auto-cpufreq-install
|
||||||
|
|
||||||
echo -e "\n* Removing auto-cpufreq daemon run script"
|
|
||||||
rm /usr/bin/auto-cpufreq-run
|
|
||||||
|
|
||||||
echo -e "\n* Removing auto-cpufreq binary"
|
echo -e "\n* Removing auto-cpufreq binary"
|
||||||
rm /usr/bin/auto-cpufreq
|
rm /usr/bin/auto-cpufreq
|
||||||
|
@ -48,5 +35,7 @@ rm /usr/bin/auto-cpufreq
|
||||||
echo -e "\n* Removing auto-cpufreq log file"
|
echo -e "\n* Removing auto-cpufreq log file"
|
||||||
rm /var/log/auto-cpufreq.log
|
rm /var/log/auto-cpufreq.log
|
||||||
|
|
||||||
echo "kill any remaining instances"
|
echo -e "\n* Removing auto-cpufreq daemon install marker"
|
||||||
pkill -f auto-cpufreq
|
rm /etc/opt/auto-cpufreq
|
||||||
|
|
||||||
|
echo -e "\n-------------------------------------------------------------------------------\n"
|
|
@ -5,7 +5,7 @@ After=network.target network-online.target
|
||||||
[Service]
|
[Service]
|
||||||
Type=simple
|
Type=simple
|
||||||
User=root
|
User=root
|
||||||
ExecStart=/usr/bin/python3 /usr/bin/auto-cpufreq --live
|
ExecStart=/usr/bin/python3 /usr/bin/auto-cpufreq --daemon
|
||||||
StandardOutput=file:/var/log/auto-cpufreq.log
|
StandardOutput=file:/var/log/auto-cpufreq.log
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
Loading…
Reference in New Issue