mirror of
https://github.com/AdnanHodzic/auto-cpufreq.git
synced 2025-07-25 14:44:37 +02:00
added option to deploy and run auto-cpufreq as a service (daemon)
This commit is contained in:
parent
0e6a620519
commit
84e0c6cfa1
@ -1,4 +1,6 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
#
|
||||||
|
# ToDo: add description
|
||||||
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import os
|
import os
|
||||||
@ -13,20 +15,20 @@ import platform
|
|||||||
import click
|
import click
|
||||||
|
|
||||||
# ToDo:
|
# ToDo:
|
||||||
# - check if debian based + first time setup (install necessary packages)
|
|
||||||
# - add option to run as daemon on boot (systemd)
|
|
||||||
# - in case of daemon deploy check if it's already running/daemon script exists
|
|
||||||
# - add option to disable bluetooth (only in daemon mode)
|
|
||||||
# - add revert/uninstall options for ^
|
|
||||||
# - sort out imports
|
|
||||||
# - add option to enable turbo in powersave
|
|
||||||
# - go thru all other ToDo's
|
|
||||||
# - make shortcut for platform
|
|
||||||
# - add boost options on cpu temperature?
|
|
||||||
# - if switch to psutil load (add percentage as well)
|
|
||||||
# - add print which mode is auto-cpufreq running in
|
|
||||||
|
|
||||||
# global var
|
# - add nice message at the end of deploy + add print for each action
|
||||||
|
# - add parameter to read logs if daemon is set
|
||||||
|
# - add option to disable bluetooth (only in daemon mode)
|
||||||
|
# - add uninstall options for daemon
|
||||||
|
|
||||||
|
# - sort out imports
|
||||||
|
# - make shortcut for platform
|
||||||
|
# - go thru all other ToDo's
|
||||||
|
|
||||||
|
# - fill out every TBU (cli + auto-cpufreq.service file)
|
||||||
|
# - add readme + list need to install all necessary packages
|
||||||
|
|
||||||
|
# global vars
|
||||||
p = psutil
|
p = psutil
|
||||||
s = subprocess
|
s = subprocess
|
||||||
tool_run = "python3 auto-cpufreq.py"
|
tool_run = "python3 auto-cpufreq.py"
|
||||||
@ -44,21 +46,32 @@ bat_state = p.sensors_battery().power_plugged
|
|||||||
# get CPU utilization as a percentage
|
# get CPU utilization as a percentage
|
||||||
cpuload = p.cpu_percent(interval=1)
|
cpuload = p.cpu_percent(interval=1)
|
||||||
|
|
||||||
def cpufreqctl_deploy():
|
def deploy():
|
||||||
|
|
||||||
|
# deploy cpufreqctl script (if missing)
|
||||||
if os.path.isfile("/usr/bin/cpufreqctl"):
|
if os.path.isfile("/usr/bin/cpufreqctl"):
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
os.system("scripts/cpufreqctl.sh /usr/bin/cpufreqctl")
|
os.system("cp scripts/cpufreqctl.sh /usr/bin/cpufreqctl")
|
||||||
|
|
||||||
# deploy auto-cpufreq daemon script
|
# deploy auto-cpufreq binary
|
||||||
def daemon_deploy():
|
os.system("cp auto-cpufreq.py /usr/bin/auto-cpufreq")
|
||||||
|
|
||||||
if os.path.isfile("/usr/bin/auto-cpufreq"):
|
# deploy auto-cpufreq daemon script
|
||||||
pass
|
os.system("cp scripts/auto-cpufreq-daemon.sh /usr/bin/auto-cpufreq-daemon")
|
||||||
else:
|
|
||||||
os.system("cp scripts/daemon-deploy.sh /usr/bin/auto-cpufreq")
|
# create auto-cpufreq systemd unit file
|
||||||
|
os.system("cp scripts/auto-cpufreq.service /lib/systemd/system/auto-cpufreq.service")
|
||||||
|
|
||||||
|
s.call("/usr/bin/auto-cpufreq-daemon", shell=True)
|
||||||
|
|
||||||
|
# ToDo: disable bluetooth on boot
|
||||||
|
|
||||||
|
# ToDo: add nice message as multiline
|
||||||
|
print("auto-cpufreq daemon started and running in background.")
|
||||||
|
print("Logs are available in: /var/log/auto-cpufreq.log")
|
||||||
|
print("View live logs by running i.e: \ntail -n 50 -f /var/log/auto-cpufreq.log")
|
||||||
|
|
||||||
# ToDo: add auto-cpufreq to run on boot
|
|
||||||
|
|
||||||
def footer(l):
|
def footer(l):
|
||||||
print("\n" + "-" * l + "\n")
|
print("\n" + "-" * l + "\n")
|
||||||
@ -106,7 +119,7 @@ def set_powersave():
|
|||||||
print("\nSetting: powersave")
|
print("\nSetting: powersave")
|
||||||
s.run("cpufreqctl --governor --set=powersave", shell=True)
|
s.run("cpufreqctl --governor --set=powersave", shell=True)
|
||||||
|
|
||||||
print("Setting turbo: off")
|
print("Setting turbo boost: off")
|
||||||
s.run("echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo", shell=True)
|
s.run("echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo", shell=True)
|
||||||
|
|
||||||
# enable turbo boost
|
# enable turbo boost
|
||||||
@ -307,17 +320,6 @@ 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")
|
||||||
|
|
||||||
def get_processor_info():
|
|
||||||
if platform.system() == "Windows":
|
|
||||||
return platform.processor()
|
|
||||||
elif platform.system() == "Darwin":
|
|
||||||
return subprocess.check_output(['/usr/sbin/sysctl', "-n", "machdep.cpu.brand_string"]).strip()
|
|
||||||
elif platform.system() == "Linux":
|
|
||||||
command = "cat /proc/cpuinfo"
|
|
||||||
return subprocess.check_output(command, shell=True).strip()
|
|
||||||
return ""
|
|
||||||
|
|
||||||
|
|
||||||
# cli
|
# cli
|
||||||
@click.command()
|
@click.command()
|
||||||
@click.option("--monitor", is_flag=True, help="TBU")
|
@click.option("--monitor", is_flag=True, help="TBU")
|
||||||
@ -340,39 +342,26 @@ def cli(monitor, live, daemon):
|
|||||||
#root_check()
|
#root_check()
|
||||||
driver_check()
|
driver_check()
|
||||||
gov_check()
|
gov_check()
|
||||||
cpufreqctl_deploy()
|
|
||||||
sysinfo()
|
sysinfo()
|
||||||
mon_autofreq()
|
mon_autofreq()
|
||||||
mon_turbo()
|
mon_turbo()
|
||||||
#autofreq()
|
|
||||||
countdown(10)
|
countdown(10)
|
||||||
#time.sleep(1)
|
|
||||||
subprocess.call("clear")
|
subprocess.call("clear")
|
||||||
elif live:
|
elif live:
|
||||||
while True:
|
while True:
|
||||||
root_check()
|
root_check()
|
||||||
get_processor_info()
|
|
||||||
driver_check()
|
driver_check()
|
||||||
gov_check()
|
gov_check()
|
||||||
cpufreqctl_deploy()
|
|
||||||
sysinfo()
|
sysinfo()
|
||||||
set_autofreq()
|
set_autofreq()
|
||||||
#set_turbo()
|
|
||||||
countdown(10)
|
countdown(10)
|
||||||
#time.sleep(1)
|
|
||||||
subprocess.call("clear")
|
subprocess.call("clear")
|
||||||
elif daemon:
|
elif daemon:
|
||||||
while True:
|
#while True:
|
||||||
print("daemon ...")
|
|
||||||
root_check()
|
root_check()
|
||||||
driver_check()
|
driver_check()
|
||||||
gov_check()
|
gov_check()
|
||||||
cpufreqctl_deploy()
|
deploy()
|
||||||
daemon_deploy()
|
|
||||||
#daemon_run()
|
|
||||||
#sysinfo()
|
|
||||||
#set_autofreq()
|
|
||||||
#countdown(15)
|
|
||||||
else:
|
else:
|
||||||
print("remove ...")
|
print("remove ...")
|
||||||
|
|
||||||
|
31
scripts/auto-cpufreq-daemon.sh
Executable file
31
scripts/auto-cpufreq-daemon.sh
Executable file
@ -0,0 +1,31 @@
|
|||||||
|
#!/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
|
||||||
|
|
||||||
|
logs_file="/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* Starting auto-cpufreq systemd service"
|
||||||
|
/usr/bin/python3 /usr/bin/auto-cpufreq --live > $logs_file 2>&1 &
|
||||||
|
|
||||||
|
echo -e "\n------\n"
|
11
scripts/auto-cpufreq.service
Normal file
11
scripts/auto-cpufreq.service
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=auto-cpufreq - TBU description
|
||||||
|
After=network.target network-online.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
User=root
|
||||||
|
ExecStart=/bin/bash /usr/bin/auto-cpufreq-daemon
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
@ -1,20 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
#
|
|
||||||
# auto-cpufreq daemon script
|
|
||||||
# reference: https://github.com/AdnanHodzic/auto-cpufreq
|
|
||||||
|
|
||||||
if (( $EUID != 0 ));
|
|
||||||
then
|
|
||||||
echo -e "\nERROR\nMust be run as root (i.e: 'sudo $0')."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
logs_file="/var/log/auto-cpufreq.log"
|
|
||||||
|
|
||||||
python3 auto-cpufreq.py --live > $logs_file 2>&1 &
|
|
||||||
|
|
||||||
echo -e "\n------------------------ auto-cpufreq -----------------------------\n"
|
|
||||||
echo -e "auto-cpufreq daemon started and running in background."
|
|
||||||
echo -e "\nLogs are available in:\n$logs_file"
|
|
||||||
echo -e "\nView live logs by running i.e: \ntail -n 50 -f $logs_file"
|
|
||||||
echo -e "\n-------------------------------------------------------------------\n"
|
|
Loading…
x
Reference in New Issue
Block a user