auto-cpufreq/auto-cpufreq.py

148 lines
4.6 KiB
Python
Raw Normal View History

2019-12-30 13:46:56 +01:00
#!/usr/bin/env python3
2019-12-29 21:41:32 +01:00
import subprocess
import os
import sys
import time
import psutil
2019-12-30 13:46:56 +01:00
import cpuinfo
import platform
2019-12-30 13:46:56 +01:00
import distro
import re
2019-12-29 21:41:32 +01:00
# ToDo:
2019-12-30 17:51:08 +01:00
# - check if debian based + first time setup (install necessary packages)
# - add option to run as daemon on boot
2019-12-29 21:41:32 +01:00
# - sort out imports
2019-12-29 22:02:27 +01:00
# - add option to enable turbo in powersave
# - go thru all other ToDo's
2019-12-30 17:51:08 +01:00
# - copy cpufreqctl script if it doesn't exist
2019-12-29 21:41:32 +01:00
# global var
2019-12-29 21:41:32 +01:00
p = psutil
s = subprocess
tool_run = "python3 auto-cpufreq.py"
def driver_check():
driver = s.getoutput("cpufreqctl --driver")
if driver != "intel_pstate":
sys.exit(f"\nError:\nOnly laptops with enabled \"intel_pstate\" (CPU Performance Scaling Driver) are supported.\n")
2019-12-29 21:41:32 +01:00
def avail_gov():
# available governors
get_avail_gov = s.getoutput("cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors")
# ToDo: make check to fail if powersave and performance are not available
# check current scaling governor
#get_gov_state = subprocess.getoutput("cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor")
2019-12-30 17:51:08 +01:00
#get_gov_state = s.getoutput("cpufreqctl --governor")
2019-12-29 21:41:32 +01:00
2019-12-30 17:51:08 +01:00
#gov_state = get_gov_state.split()[0]
#print("\nCurrent scaling_governor: " + gov_state)
2019-12-29 21:41:32 +01:00
# root check func
def root_check():
if not os.geteuid() == 0:
sys.exit(f"\nMust be run as root, i.e: \"sudo {tool_run}\"\n")
exit(1)
# set powersave
def set_powersave():
print("\nSetting: powersave")
s.run("cpufreqctl --governor --set=powersave", shell=True)
2019-12-29 21:41:32 +01:00
print("Setting turbo: off")
s.run("echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo", shell=True)
# set performance
def set_performance():
2019-12-30 17:51:08 +01:00
print("Using \"performance\" governor\n")
s.run("cpufreqctl --governor --set=performance", shell=True)
2019-12-29 21:41:32 +01:00
# enable turbo boost
set_turbo()
def set_turbo():
2019-12-30 13:46:56 +01:00
# ToDo: replace with psutil.getloadavg()? (available in 5.6.2)
2019-12-29 21:41:32 +01:00
load1m, _, _ = os.getloadavg()
2019-12-29 22:02:27 +01:00
cpuload = p.cpu_percent(interval=1)
2019-12-29 21:41:32 +01:00
2019-12-30 17:51:08 +01:00
print("Total CPU usage:", cpuload, "%")
print("Total system load:", load1m, "\n")
2019-12-29 21:41:32 +01:00
2019-12-30 13:46:56 +01:00
# ToDo: move load and cpuload to sysinfo
2019-12-29 21:41:32 +01:00
if load1m > 2:
2019-12-30 17:51:08 +01:00
print("High load, turbo bost: on")
2019-12-29 21:41:32 +01:00
s.run("echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo", shell=True)
2019-12-30 17:51:08 +01:00
# print("High load:", load1m)
# print("CPU load:", cpuload, "%")
2019-12-29 22:02:27 +01:00
elif cpuload > 25:
2019-12-30 17:51:08 +01:00
print("High CPU load, turbo boost: on")
2019-12-29 22:02:27 +01:00
s.run("echo 0 > /sys/devices/system/cpu/intel_pstate/no_turbo", shell=True)
2019-12-29 21:41:32 +01:00
else:
2019-12-30 17:51:08 +01:00
print("Load optimal, turbo boost: off")
2019-12-29 21:41:32 +01:00
s.run("echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo", shell=True)
def autofreq():
2019-12-30 17:51:08 +01:00
print("\n" + "-" * 18 + " CPU frequency scaling " + "-" * 19 + "\n")
driver_check()
2019-12-30 13:46:56 +01:00
# ToDo: make a function and more generic (move to psutil)
2019-12-29 21:41:32 +01:00
# check battery status
get_bat_state = s.getoutput("cat /sys/class/power_supply/BAT0/status")
bat_state = get_bat_state.split()[0]
# auto cpufreq based on battery state
if bat_state == "Discharging":
2019-12-30 17:51:08 +01:00
print("Battery is: discharging")
2019-12-29 21:41:32 +01:00
set_powersave()
elif bat_state == "Charging" or "Full":
2019-12-30 17:51:08 +01:00
print("Battery is: charging")
2019-12-29 21:41:32 +01:00
set_performance()
2019-12-30 17:51:08 +01:00
else:
print("Couldn't detrmine battery status. Please report this issue.")
2019-12-30 13:46:56 +01:00
def sysinfo():
2019-12-30 17:51:08 +01:00
print("\n" + "-" * 20 + " System information " + "-" * 20 + "\n")
core_usage = p.cpu_freq(percpu=True)
2019-12-30 13:46:56 +01:00
cpu_brand = cpuinfo.get_cpu_info()['brand']
cpu_arch = cpuinfo.get_cpu_info()['arch']
cpu_count = cpuinfo.get_cpu_info()['count']
fdist = distro.linux_distribution()
dist = " ".join(x for x in fdist)
print("Linux distro: " + dist)
print("Linux kernel: " + platform.release())
2019-12-30 17:51:08 +01:00
print("Architecture:", cpu_arch)
2019-12-30 13:46:56 +01:00
2019-12-30 17:51:08 +01:00
print("Processor:", cpu_brand)
print("Cores:", cpu_count)
2019-12-30 13:46:56 +01:00
2019-12-30 17:51:08 +01:00
print("\n" + "-" * 20 + " Current CPU state " + "-" * 21 + "\n")
print("CPU frequency for each core:\n")
core_num = 0
while core_num < cpu_count:
2019-12-30 17:51:08 +01:00
print("CPU" + str(core_num) + ": {:.0f}".format(core_usage[core_num].current) + " MHz")
core_num += 1
2019-12-30 17:51:08 +01:00
# ToDo: make more generic and not only for thinkpad
#print(psutil.sensors_fans())
current_fans = p.sensors_fans()['thinkpad'][0].current
print("\nCPU fan speed:", current_fans), "RPM"
# ToDo: add CPU temperature for each core
2019-12-30 13:46:56 +01:00
# issue: https://github.com/giampaolo/psutil/issues/1650
#print(psutil.sensors_temperatures()['coretemp'][1].current)
2019-12-29 21:41:32 +01:00
if __name__ == '__main__':
while True:
root_check()
2019-12-30 17:51:08 +01:00
#avail_gov()
2019-12-30 13:46:56 +01:00
sysinfo()
2019-12-29 21:41:32 +01:00
autofreq()
time.sleep(10)