added option to read log + other visual improvements

This commit is contained in:
Adnan Hodzic 2020-01-02 11:59:09 +01:00
parent 1706bc02b3
commit 3eee90e1fb
1 changed files with 32 additions and 18 deletions

View File

@ -16,8 +16,8 @@ import click
# ToDo: # ToDo:
# - add parameter to read logs if daemon is set
# - add uninstall options for daemon # - add uninstall options for daemon
# - if live is run check if log exists, if yes instruct further options (how to see or delete)
# - add potential throttling fix (set max frequency if load too high?) # - add potential throttling fix (set max frequency if load too high?)
# - sort out imports # - sort out imports
@ -30,7 +30,7 @@ import click
# global vars # global vars
p = psutil p = psutil
s = subprocess s = subprocess
tool_run = "python3 auto-cpufreq.py" tool_run = sys.argv[0]
# get turbo boost state # get turbo boost state
cur_turbo = s.getoutput("cat /sys/devices/system/cpu/intel_pstate/no_turbo") cur_turbo = s.getoutput("cat /sys/devices/system/cpu/intel_pstate/no_turbo")
@ -45,6 +45,9 @@ 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)
# auto-cpufreq log file
auto_cpufreq_log_file = "/var/log/auto-cpufreq.log"
# deploy auto-cpufreq daemon # deploy auto-cpufreq daemon
def deploy(): def deploy():
@ -54,14 +57,14 @@ def deploy():
if os.path.isfile("/usr/bin/cpufreqctl"): if os.path.isfile("/usr/bin/cpufreqctl"):
pass pass
else: else:
print("\n* Addding missing \"cpufreqctl\" script") print("* Addding missing \"cpufreqctl\" script")
os.system("cp scripts/cpufreqctl.sh /usr/bin/cpufreqctl") os.system("cp scripts/cpufreqctl.sh /usr/bin/cpufreqctl")
# delete /var/log/auto-cpufreq.log if it exists (make sure file gets updated accordingly) # delete /var/log/auto-cpufreq.log if it exists (make sure file gets updated accordingly)
if os.path.exists("/var/log/auto-cpufreq.log"): if os.path.exists(auto_cpufreq_log_file):
os.remove("/var/log/auto-cpufreq.log") os.remove(auto_cpufreq_log_file)
print("\n* Turn off bluetooth on boot") print("* Turn off bluetooth on boot")
btconf="/etc/bluetooth/main.conf" btconf="/etc/bluetooth/main.conf"
try: try:
orig_set = "AutoEnable=true" orig_set = "AutoEnable=true"
@ -86,12 +89,11 @@ def deploy():
# run auto-cpufreq daemon deploy script # run auto-cpufreq daemon deploy script
s.call("/usr/bin/auto-cpufreq-daemon", shell=True) s.call("/usr/bin/auto-cpufreq-daemon", shell=True)
print("auto-cpufreq daemon started and will automatically start at boot time.")
print("\nTo disable and remove auto-cpufreq daemon, run:\nautocpu-freq --remove")
# ToDo: add nice message as multiline print("\nTo view live auto-cpufreq daemon logs, run:\nauto-cpufreq --log")
print("auto-cpufreq daemon started and running in background.") footer(79)
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")
def footer(l): def footer(l):
print("\n" + "-" * l + "\n") print("\n" + "-" * l + "\n")
@ -107,7 +109,7 @@ def driver_check():
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"
governors=['performance','powersave'] governors=["performance","powersave"]
for line in open(avail_gov): for line in open(avail_gov):
for keyword in governors: for keyword in governors:
@ -121,7 +123,7 @@ def gov_check():
def root_check(): def root_check():
if not os.geteuid() == 0: if not os.geteuid() == 0:
print("\n" + "-" * 33 + " Root check " + "-" * 34 + "\n") print("\n" + "-" * 33 + " Root check " + "-" * 34 + "\n")
sys.exit(f"Must be run as root, i.e: \"sudo {tool_run}\"\n") sys.exit("ERROR:\n\nMust be run root for this functionality to work, i.e: \nsudo " + tool_run + "\n")
exit(1) exit(1)
# refresh countdown # refresh countdown
@ -341,18 +343,31 @@ 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 read_log():
# ToDo: check if daemon is set/log exists
# deploy cpufreqctl script (if missing)
if os.path.isfile(auto_cpufreq_log_file):
# read /var/log/auto-cpufreq.log
s.call(["tail", "-n 50", "-f", auto_cpufreq_log_file])
else:
print("\n" + "-" * 30 + " auto-cpufreq log " + "-" * 31 + "\n")
print("ERROR:\n\nauto-cpufreq log is missing.\nMake sure to run \"python3 auto-cpufreq --daemon\" first\n")
# cli # cli
@click.command() @click.command()
@click.option("--monitor", is_flag=True, help="TBU") @click.option("--monitor", is_flag=True, help="TBU")
@click.option("--live", is_flag=True, help="TBU") @click.option("--live", is_flag=True, help="TBU")
@click.option("--daemon/--remove", default=True, help="TBU") @click.option("--daemon/--remove", default=True, help="TBU")
@click.option("--log", is_flag=True, help="TBU")
def cli(monitor, live, daemon): def cli(monitor, live, daemon, 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")
print("auto-cpufreq - TBU") print("auto-cpufreq - TBU")
print("\nExample usage: " + tool_run + "--install user") print("\nExample usage: " + tool_run + " --install user")
print("\n-----\n") print("\n-----\n")
s.call(["python3", "auto-cpufreq.py", "--help"]) s.call(["python3", "auto-cpufreq.py", "--help"])
@ -377,14 +392,13 @@ def cli(monitor, live, daemon):
set_autofreq() set_autofreq()
countdown(10) countdown(10)
subprocess.call("clear") subprocess.call("clear")
elif log:
read_log()
elif daemon: elif daemon:
#while True:
root_check() root_check()
driver_check() driver_check()
gov_check() gov_check()
deploy() deploy()
else:
print("remove ...")
if __name__ == '__main__': if __name__ == '__main__':
# while True: # while True: