Implemented mechanism to set turbo based temperature & CPU utilization/load

This commit is contained in:
Adnan Hodzic 2020-11-28 22:38:48 +01:00
parent d0aeb8c2a2
commit 99a2b78ac2
1 changed files with 158 additions and 30 deletions

View File

@ -30,11 +30,9 @@ SCRIPTS_DIR = Path("/usr/local/share/auto-cpufreq/scripts/")
ALL_GOVERNORS = ("performance", "ondemand", "conservative", "schedutil", "userspace", "powersave") ALL_GOVERNORS = ("performance", "ondemand", "conservative", "schedutil", "userspace", "powersave")
CPUS = os.cpu_count() CPUS = os.cpu_count()
# get system/CPU load # Note:
load1m, _, _ = os.getloadavg() # "load1m" & "cpuload" can't be global vars and to in order to show correct data must be
# decraled where their execution takes place
# get CPU utilization as a percentage
cpuload = psutil.cpu_percent(interval=1)
# powersave/performance system load thresholds # powersave/performance system load thresholds
powersave_load_threshold = (75*CPUS)/100 powersave_load_threshold = (75*CPUS)/100
@ -308,11 +306,12 @@ def countdown(s):
# get cpu usage + system load for (last minute) # get cpu usage + system load for (last minute)
def display_load(): def display_load():
# get system/CPU load
load1m, _, _ = os.getloadavg()
# get CPU utilization as a percentage # get CPU utilization as a percentage
cpuload = psutil.cpu_percent(interval=1) cpuload = psutil.cpu_percent(interval=1)
# get system/CPU load
load1m, _, _ = os.getloadavg()
print("\nTotal CPU usage:", cpuload, "%") print("\nTotal CPU usage:", cpuload, "%")
print("Total system load:", load1m, "\n") print("Total system load:", load1m, "\n")
@ -324,8 +323,14 @@ def set_powersave():
run("cpufreqctl --epp --set=balance_power", shell=True) run("cpufreqctl --epp --set=balance_power", shell=True)
print("Setting to use: \"balance_power\" EPP") print("Setting to use: \"balance_power\" EPP")
# cpu usage/system load # get CPU utilization as a percentage
display_load() cpuload = psutil.cpu_percent(interval=1)
# get system/CPU load
load1m, _, _ = os.getloadavg()
print("\nTotal CPU usage:", cpuload, "%")
print("Total system load:", load1m, "\n")
# conditions for setting turbo in powersave # conditions for setting turbo in powersave
if psutil.cpu_percent(percpu=False, interval=0.01) >= 30.0 or isclose(max(psutil.cpu_percent(percpu=True, interval=0.01)), 100): if psutil.cpu_percent(percpu=False, interval=0.01) >= 30.0 or isclose(max(psutil.cpu_percent(percpu=True, interval=0.01)), 100):
@ -344,8 +349,14 @@ def set_powersave():
# make turbo suggestions in powersave # make turbo suggestions in powersave
def mon_powersave(): def mon_powersave():
# cpu usage/system load # get CPU utilization as a percentage
display_load() cpuload = psutil.cpu_percent(interval=1)
# get system/CPU load
load1m, _, _ = os.getloadavg()
print("\nTotal CPU usage:", cpuload, "%")
print("Total system load:", load1m, "\n")
if psutil.cpu_percent(percpu=False, interval=0.01) >= 30.0 or isclose(max(psutil.cpu_percent(percpu=True, interval=0.01)), 100): if psutil.cpu_percent(percpu=False, interval=0.01) >= 30.0 or isclose(max(psutil.cpu_percent(percpu=True, interval=0.01)), 100):
print("High CPU load, suggesting to set turbo boost: on") print("High CPU load, suggesting to set turbo boost: on")
@ -360,27 +371,84 @@ def mon_powersave():
get_turbo() get_turbo()
footer() footer()
# set performance and enable turbo # set performance and enable turbo
def set_performance(): def set_performance():
# access/import necessary variables from get_temp_data func
avg_cores_temp, avg_all_core_temp=sysinfo()
# get CPU utilization as a percentage
cpuload = psutil.cpu_percent(interval=1)
# get system/CPU load
load1m, _, _ = os.getloadavg()
print(f"Setting to use: \"{get_avail_performance()}\" governor") print(f"Setting to use: \"{get_avail_performance()}\" governor")
run(f"cpufreqctl --governor --set={get_avail_performance()}", shell=True) run(f"cpufreqctl --governor --set={get_avail_performance()}", 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"):
run("cpufreqctl --epp --set=balance_performance", shell=True) run("cpufreqctl --epp --set=balance_performance", shell=True)
print("Setting to use: \"balance_performance\" EPP") print("Setting to use: \"balance_performance\" EPP")
# cpu usage/system load # get CPU utilization as a percentage
display_load() cpuload = psutil.cpu_percent(interval=1)
# get system/CPU load
load1m, _, _ = os.getloadavg()
print("\nTotal CPU usage:", cpuload, "%")
print("Total system load:", load1m)
print("Average temp. of all cores:", avg_all_core_temp, "°C")
if psutil.cpu_percent(percpu=False, interval=0.01) >= 20.0 or max(psutil.cpu_percent(percpu=True, interval=0.01)) >= 75: if psutil.cpu_percent(percpu=False, interval=0.01) >= 20.0 or max(psutil.cpu_percent(percpu=True, interval=0.01)) >= 75:
print("High CPU load, setting turbo boost: on") print("\nHigh CPU load")
turbo(True)
# high cpu usage trigger
if cpuload > 25:
print("setting turbo boost: on")
turbo(True)
# set turbo state based on average of all core temperatures
elif cpuload < 25 and avg_all_core_temp >= 70:
print("Optimal total CPU usage:", cpuload, "%, high average core temp:", avg_all_core_temp, "°C")
print("setting turbo boost: off")
turbo(False)
else:
print("setting turbo boost: on")
turbo(True)
elif load1m >= performance_load_threshold: elif load1m >= performance_load_threshold:
print("High system load, setting turbo boost: on") print("\nHigh system load")
turbo(True)
# high cpu usage trigger
if cpuload > 25:
print("setting turbo boost: on")
turbo(True)
# set turbo state based on average of all core temperatures
elif cpuload < 25 and avg_all_core_temp >= 65:
print("Optimal total CPU usage:", cpuload, "%, high average core temp:", avg_all_core_temp, "°C")
print("setting turbo boost: off")
turbo(False)
else:
print("setting turbo boost: on")
turbo(True)
else: else:
print("Load optimal, setting turbo boost: off") print("\nLoad optimal")
turbo(False)
# high cpu usage trigger
if cpuload > 25:
print("setting turbo boost: on")
turbo(True)
# set turbo state based on average of all core temperatures
elif cpuload < 25 and avg_all_core_temp >= 60:
print("Optimal total CPU usage:", cpuload, "%, high average core temp:", avg_all_core_temp, "°C")
print("setting turbo boost: off")
turbo(False)
else:
print("setting turbo boost: on")
turbo(True)
footer() footer()
@ -388,20 +456,73 @@ def set_performance():
# make turbo suggestions in performance # make turbo suggestions in performance
def mon_performance(): def mon_performance():
# cpu usage/system load # access/import necessary variables from get_temp_data func
display_load() avg_cores_temp, avg_all_core_temp=sysinfo()
# get CPU utilization as a percentage
cpuload = psutil.cpu_percent(interval=1)
# get system/CPU load
load1m, _, _ = os.getloadavg()
print("\nTotal CPU usage:", cpuload, "%")
print("Total system load:", load1m)
print("Average temp. of all cores:", avg_all_core_temp, "°C")
# get system/CPU load
load1m, _, _ = os.getloadavg()
if psutil.cpu_percent(percpu=False, interval=0.01) >= 20.0 or max(psutil.cpu_percent(percpu=True, interval=0.01)) >= 75: if psutil.cpu_percent(percpu=False, interval=0.01) >= 20.0 or max(psutil.cpu_percent(percpu=True, interval=0.01)) >= 75:
print("High CPU load, suggesting to set turbo boost: on") print("High CPU load")
get_turbo()
footer() # high cpu usage trigger
if cpuload > 25:
print("suggesting to set turbo boost: on")
get_turbo()
# set turbo state based on average of all core temperatures
elif cpuload < 25 and avg_all_core_temp >= 70:
print("Optimal total CPU usage:", cpuload, "%, high average core temp:", avg_all_core_temp, "°C")
print("suggesting to set turbo boost: off")
get_turbo()
else:
print("suggesting to set turbo boost: on")
get_turbo()
elif load1m > performance_load_threshold: elif load1m > performance_load_threshold:
print("High system load, suggesting to set turbo boost: on") print("High system load")
get_turbo()
# high cpu usage trigger
if cpuload > 25:
print("suggesting to set turbo boost: on")
get_turbo()
# set turbo state based on average of all core temperatures
elif cpuload < 25 and avg_all_core_temp >= 65:
print("Optimal total CPU usage:", cpuload, "%, high average core temp:", avg_all_core_temp, "°C")
print("suggesting to set turbo boost: off")
get_turbo()
else:
print("suggesting to set turbo boost: on")
get_turbo()
footer() footer()
else: else:
print("Load optimal, suggesting to set turbo boost: off") print("\nLoad optimal")
get_turbo()
# high cpu usage trigger
if cpuload > 25:
print("suggesting to set turbo boost: on")
get_turbo()
# set turbo state based on average of all core temperatures
elif cpuload < 25 and avg_all_core_temp >= 60:
print("Optimal total CPU usage:", cpuload, "%, high average core temp:", avg_all_core_temp, "°C")
print("suggesting to set turbo boost: off")
get_turbo()
else:
print("suggesting to set turbo boost: on")
get_turbo()
footer() footer()
@ -549,6 +670,13 @@ def sysinfo():
if offline_cpus: if offline_cpus:
print(f"\nDisabled CPUs: {','.join(offline_cpus)}") print(f"\nDisabled CPUs: {','.join(offline_cpus)}")
# get average temperature of all cores
avg_cores_temp = sum(temp_per_cpu)
avg_all_core_temp = float(avg_cores_temp/online_cpu_count)
# export/make these variables accessible in other functions
return avg_cores_temp, avg_all_core_temp
# print current fan speed | temporarily commented # print current fan speed | temporarily commented
# current_fans = psutil.sensors_fans()['thinkpad'][0].current # current_fans = psutil.sensors_fans()['thinkpad'][0].current
# print("\nCPU fan speed:", current_fans, "RPM") # print("\nCPU fan speed:", current_fans, "RPM")