Refactoring and fix for #14 (AMD issue) (#62)

* refactored and fixed amd coretemp
This commit is contained in:
Vadym Stupakov 2020-08-04 19:22:38 +03:00 committed by GitHub
parent 2b65e885e4
commit d0243806e9
2 changed files with 68 additions and 51 deletions

View File

@ -6,8 +6,12 @@
# core import # core import
import sys import sys
from subprocess import call
sys.path.append('../') sys.path.append('../')
from source.core import * from source.core import *
import click
# ToD: replace every s.call with s.run # ToD: replace every s.call with s.run
@ -18,7 +22,6 @@ from source.core import *
@click.option("--install/--remove", default=True, help="Install/remove daemon for automatic 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) @click.option("--daemon", is_flag=True, hidden=True)
def main(monitor, live, daemon, install, log): def main(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:
@ -39,7 +42,7 @@ def main(monitor, live, daemon, install, log):
sysinfo() sysinfo()
set_autofreq() set_autofreq()
countdown(5) countdown(5)
subprocess.call("clear") call("clear")
elif os.getenv("PKG_MARKER") != "SNAP": elif os.getenv("PKG_MARKER") != "SNAP":
while True: while True:
root_check() root_check()
@ -48,7 +51,7 @@ def main(monitor, live, daemon, install, log):
sysinfo() sysinfo()
set_autofreq() set_autofreq()
countdown(5) countdown(5)
subprocess.call("clear") call("clear")
else: else:
print("\n" + "-" * 32 + " Daemon check " + "-" * 33 + "\n") print("\n" + "-" * 32 + " Daemon check " + "-" * 33 + "\n")
print("ERROR:\n\nDaemon not enabled, must run install first, i.e: \nsudo auto-cpufreq --install") print("ERROR:\n\nDaemon not enabled, must run install first, i.e: \nsudo auto-cpufreq --install")
@ -63,7 +66,7 @@ def main(monitor, live, daemon, install, log):
sysinfo() sysinfo()
mon_autofreq() mon_autofreq()
countdown(5) countdown(5)
subprocess.call("clear") call("clear")
elif live: elif live:
while True: while True:
root_check() root_check()
@ -73,7 +76,7 @@ def main(monitor, live, daemon, install, log):
sysinfo() sysinfo()
set_autofreq() set_autofreq()
countdown(5) countdown(5)
subprocess.call("clear") call("clear")
elif log: elif log:
# ToDo: fail if log is missing or empty (on) # ToDo: fail if log is missing or empty (on)
read_log() read_log()

View File

@ -2,27 +2,20 @@
# #
# auto-cpufreq - core functionality # auto-cpufreq - core functionality
# ToDo: re-order in a single line?
import subprocess
import os import os
import platform as pl
import subprocess as s
import sys import sys
import time import time
import psutil
import platform import power as pw
import click import psutil as p
import power
import signal
# ToDo: # ToDo:
# - re-enable CPU fan speed display and make more generic and not only for thinkpad # - re-enable CPU fan speed display and make more generic and not only for thinkpad
# - replace get system/CPU load from: psutil.getloadavg() | available in 5.6.2) # - replace get system/CPU load from: psutil.getloadavg() | available in 5.6.2)
# global vars
p = psutil
pl = platform
s = subprocess
cpus = os.cpu_count() cpus = os.cpu_count()
pw = power
# get turbo boost state # get turbo boost state
turbo_loc = "/sys/devices/system/cpu/intel_pstate/no_turbo" turbo_loc = "/sys/devices/system/cpu/intel_pstate/no_turbo"
@ -46,6 +39,7 @@ auto_cpufreq_log_file_snap = "/var/snap/auto-cpufreq/current/auto-cpufreq.log"
# daemon check # daemon check
dcheck = s.getoutput("snapctl get daemon") dcheck = s.getoutput("snapctl get daemon")
# deploy cpufreqctl script # deploy cpufreqctl script
def cpufreqctl(): def cpufreqctl():
# detect if running on a SNAP # detect if running on a SNAP
@ -59,6 +53,7 @@ def cpufreqctl():
else: else:
os.system("cp " + scripts_dir + "cpufreqctl.sh /usr/bin/cpufreqctl") os.system("cp " + scripts_dir + "cpufreqctl.sh /usr/bin/cpufreqctl")
# restore original cpufreqctl script # restore original cpufreqctl script
def cpufreqctl_restore(): def cpufreqctl_restore():
# detect if running on a SNAP # detect if running on a SNAP
@ -73,24 +68,27 @@ def cpufreqctl_restore():
# restored if overwritten by system. But during tool removal to also remove it # restored if overwritten by system. But during tool removal to also remove it
# in def cpufreqctl # in def cpufreqctl
# print footer func # print footer func
def footer(l): def footer(l):
print("\n" + "-" * l + "\n") print("\n" + "-" * l + "\n")
def deploy_complete_msg(): def deploy_complete_msg():
print("\n" + "-" * 17 + " auto-cpufreq daemon installed and running " + "-" * 17 + "\n") print("\n" + "-" * 17 + " auto-cpufreq daemon installed and running " + "-" * 17 + "\n")
print("To view live log, run:\nauto-cpufreq --log") print("To 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)
def remove_complete_msg(): def remove_complete_msg():
print("\n" + "-" * 25 + " auto-cpufreq daemon removed " + "-" * 25 + "\n") print("\n" + "-" * 25 + " auto-cpufreq daemon removed " + "-" * 25 + "\n")
print("auto-cpufreq successfully removed.") print("auto-cpufreq successfully removed.")
footer(79) footer(79)
# deploy auto-cpufreq daemon # deploy auto-cpufreq daemon
def deploy(): def deploy():
print("\n" + "-" * 21 + " Deploying auto-cpufreq as a daemon " + "-" * 22 + "\n") print("\n" + "-" * 21 + " Deploying auto-cpufreq as a daemon " + "-" * 22 + "\n")
# deploy cpufreqctl script func call # deploy cpufreqctl script func call
@ -121,9 +119,9 @@ def deploy():
# run auto-cpufreq daemon deploy script # run auto-cpufreq daemon deploy script
s.call("/usr/bin/auto-cpufreq-install", shell=True) s.call("/usr/bin/auto-cpufreq-install", shell=True)
# 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")
print("* Turn on bluetooth on boot") print("* Turn on bluetooth on boot")
@ -151,6 +149,7 @@ def remove():
# restore original cpufrectl script # restore original cpufrectl script
cpufreqctl_restore() cpufreqctl_restore()
# check for necessary scaling governors # check for necessary scaling governors
def gov_check(): def gov_check():
avail_gov = avail_gov_loc avail_gov = avail_gov_loc
@ -165,6 +164,7 @@ def gov_check():
print("\n" + "-" * 18 + " Checking for necessary scaling governors " + "-" * 19 + "\n") print("\n" + "-" * 18 + " Checking for necessary scaling governors " + "-" * 19 + "\n")
sys.exit("ERROR:\n\nCouldn't find any of the necessary scaling governors.\n") sys.exit("ERROR:\n\nCouldn't find any of the necessary scaling governors.\n")
# root check func # root check func
def root_check(): def root_check():
if not os.geteuid() == 0: if not os.geteuid() == 0:
@ -173,6 +173,7 @@ def root_check():
footer(79) footer(79)
exit(1) exit(1)
# refresh countdown # refresh countdown
def countdown(s): def countdown(s):
# Fix for wrong log output and "TERM environment variable not set" # Fix for wrong log output and "TERM environment variable not set"
@ -184,11 +185,12 @@ def countdown(s):
sys.stdout.flush() sys.stdout.flush()
time.sleep(1) time.sleep(1)
# set powersave and enable turbo # set powersave and enable turbo
def set_powersave(): def set_powersave():
print("Setting to use: \"powersave\" governor") print("Setting to use: \"powersave\" governor")
s.run("cpufreqctl --governor --set=powersave", shell=True) s.run("cpufreqctl --governor --set=powersave", shell=True)
if (os.path.exists("/sys/devices/system/cpu/cpu0/cpufreq/energy_performance_preference")): if os.path.exists("/sys/devices/system/cpu/cpu0/cpufreq/energy_performance_preference"):
s.run("cpufreqctl --epp --set=balance_power", shell=True) s.run("cpufreqctl --epp --set=balance_power", shell=True)
print("Setting to use: \"balance_power\" EPP") print("Setting to use: \"balance_power\" EPP")
@ -214,9 +216,9 @@ def set_powersave():
s.run("echo 1 > " + turbo_loc, shell=True) s.run("echo 1 > " + turbo_loc, shell=True)
footer(79) footer(79)
# make turbo suggestions in powersave # make turbo suggestions in powersave
def mon_powersave(): def mon_powersave():
# get system/CPU load # get system/CPU load
load1m, _, _ = os.getloadavg() load1m, _, _ = os.getloadavg()
# get CPU utilization as a percentage # get CPU utilization as a percentage
@ -248,6 +250,7 @@ def mon_powersave():
print("Currently turbo boost is: off") print("Currently turbo boost is: off")
footer(79) footer(79)
# set performance and enable turbo # set performance and enable turbo
def set_performance(): def set_performance():
print("Setting to use \"performance\" governor") print("Setting to use \"performance\" governor")
@ -256,7 +259,6 @@ def set_performance():
s.run("cpufreqctl --epp --set=balance_performance", shell=True) s.run("cpufreqctl --epp --set=balance_performance", shell=True)
print("Setting to use: \"balance_performance\" EPP") print("Setting to use: \"balance_performance\" EPP")
# get system/CPU load # get system/CPU load
load1m, _, _ = os.getloadavg() load1m, _, _ = os.getloadavg()
# get CPU utilization as a percentage # get CPU utilization as a percentage
@ -279,9 +281,9 @@ def set_performance():
s.run("echo 1 > " + turbo_loc, shell=True) s.run("echo 1 > " + turbo_loc, shell=True)
footer(79) footer(79)
# make turbo suggestions in performance # make turbo suggestions in performance
def mon_performance(): def mon_performance():
# get system/CPU load # get system/CPU load
load1m, _, _ = os.getloadavg() load1m, _, _ = os.getloadavg()
# get CPU utilization as a percentage # get CPU utilization as a percentage
@ -299,6 +301,7 @@ def mon_performance():
footer(79) footer(79)
# set cpufreq based if device is charging # set cpufreq based if device is charging
def set_autofreq(): def set_autofreq():
print("\n" + "-" * 28 + " CPU frequency scaling " + "-" * 28 + "\n") print("\n" + "-" * 28 + " CPU frequency scaling " + "-" * 28 + "\n")
@ -316,6 +319,7 @@ def set_autofreq():
else: else:
print("Couldn't determine the battery status. Please report this issue.") print("Couldn't determine the battery status. Please report this issue.")
# make cpufreq suggestions # make cpufreq suggestions
def mon_autofreq(): def mon_autofreq():
print("\n" + "-" * 28 + " CPU frequency scaling " + "-" * 28 + "\n") print("\n" + "-" * 28 + " CPU frequency scaling " + "-" * 28 + "\n")
@ -335,9 +339,9 @@ def mon_autofreq():
else: else:
print("Couldn't determine the battery status. Please report this issue.") print("Couldn't determine the battery status. Please report this issue.")
# get system information # get system information
def sysinfo(): def sysinfo():
# added as a temp fix for issue: https://github.com/giampaolo/psutil/issues/1650 # added as a temp fix for issue: https://github.com/giampaolo/psutil/issues/1650
import warnings import warnings
warnings.filterwarnings("ignore") warnings.filterwarnings("ignore")
@ -346,9 +350,11 @@ def sysinfo():
import distro import distro
dist = "UNKNOWN"
# get distro information in snap env. # get distro information in snap env.
if os.getenv("PKG_MARKER") == "SNAP": if os.getenv("PKG_MARKER") == "SNAP":
searchfile = open("/var/lib/snapd/hostfs/etc/os-release", "r") searchfile = open("/var/lib/snapd/hostfs/etc/os-release", "r")
version = ""
for line in searchfile: for line in searchfile:
if line.startswith('NAME='): if line.startswith('NAME='):
distro = line[5:line.find('$')].strip("\"") distro = line[5:line.find('$')].strip("\"")
@ -391,10 +397,8 @@ def sysinfo():
print("Cores:", cpu_count) print("Cores:", cpu_count)
print("\n" + "-" * 30 + " Current CPU states " + "-" * 30 + "\n") print("\n" + "-" * 30 + " Current CPU states " + "-" * 30 + "\n")
print(f"CPU max frequency: {p.cpu_freq().max:.0f}MHz")
# print cpu max frequency print(f"CPU min frequency: {p.cpu_freq().min:.0f}MHz")
max_cpu_freq = p.cpu_freq().max
print("CPU max frequency: " + "\n{:.0f}".format(max_cpu_freq) + " MHz\n")
# get current cpu frequency per core # get current cpu frequency per core
core_usage = p.cpu_freq(percpu=True) core_usage = p.cpu_freq(percpu=True)
@ -403,7 +407,7 @@ def sysinfo():
print("CPU frequency for each core:\n") print("CPU frequency for each core:\n")
core_num = 0 core_num = 0
while core_num < cpu_count: while core_num < cpu_count:
print("CPU" + str(core_num) + ": {:.0f}".format(core_usage[core_num].current) + " MHz") print(f"CPU{core_num}: {core_usage[core_num].current:.0f} MHz")
core_num += 1 core_num += 1
# get number of core temp sensors # get number of core temp sensors
@ -415,22 +419,30 @@ def sysinfo():
print("\nTemperature for each physical core:\n") print("\nTemperature for each physical core:\n")
core_num = 0 core_num = 0
while core_num < core_temp_num: while core_num < core_temp_num:
print("CPU" + str(core_num) + " temp: {:.0f}".format(core_temp['coretemp'][core_num].current) + "°C") if "coretemp" in core_temp:
temp = core_temp['coretemp'][core_num].current
else:
temp = core_temp['acpitz'][0].current
print(f"CPU{core_num} temp: {temp:.0f}°C")
core_num += 1 core_num += 1
# print current fan speed | temporarily commented # print current fan speed | temporarily commented
# 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")
# create file func # create file func
def create_file(file): def create_file(file):
open(file, 'a').close() open(file, 'a').close()
# delete file func # delete file func
def delete_file(file): def delete_file(file):
if os.path.exists(file): if os.path.exists(file):
os.remove(file) os.remove(file)
# read log func # read log func
def read_log(): def read_log():
if os.getenv("PKG_MARKER") == "SNAP": if os.getenv("PKG_MARKER") == "SNAP":
@ -442,18 +454,19 @@ def read_log():
print("ERROR: auto-cpufreq log is missing.\n\nMake sure to run: \"auto-cpufreq --install\" first") print("ERROR: auto-cpufreq log is missing.\n\nMake sure to run: \"auto-cpufreq --install\" first")
footer(79) footer(79)
# check if program (argument) is running # check if program (argument) is running
def is_running(program, argument): def is_running(program, argument):
# iterate over all process id's found by psutil # iterate over all process id's found by psutil
for pid in psutil.pids(): for pid in p.pids():
try: try:
# requests the process information corresponding to each process id # requests the process information corresponding to each process id
p = psutil.Process(pid) proc = p.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 # 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 proc.name():
# check output of p.name(), output name of program # check output of p.name(), output name of program
# p.cmdline() - echo the exact command line via which p was called. # p.cmdline() - echo the exact command line via which p was called.
for arg in p.cmdline(): for arg in proc.cmdline():
if argument in str(arg): if argument in str(arg):
return True return True
else: else:
@ -463,6 +476,7 @@ def is_running(program, argument):
except: except:
continue continue
# check if auto-cpufreq --daemon is running # check if auto-cpufreq --daemon is running
def running_daemon(): def running_daemon():
if is_running('auto-cpufreq', '--daemon'): if is_running('auto-cpufreq', '--daemon'):