auto-cpufreq/auto-cpufreq.py

194 lines
6.0 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-30 23:28:16 +01:00
#from subprocess import call
import click
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 (systemd)
# - add revert/uninstall options for ^
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"
# check for necessary driver
def driver_check():
driver = s.getoutput("cpufreqctl --driver")
if driver != "intel_pstate":
print("\n" + "-" * 23 + " Driver check " + "-" * 23 + "\n")
sys.exit("ERROR:\n\n\"intel_pstate\" CPU Performance Scaling Driver is not enabled.\n")
# check for necessary scaling governors
def gov_check():
avail_gov = "/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors"
2019-12-29 21:41:32 +01:00
governors=['performance','powersave']
2019-12-29 21:41:32 +01:00
for line in open(avail_gov):
for keyword in governors:
if keyword in line:
pass
else:
print("\n" + "-" * 9 + " Checking for necessary scaling governors " + "-" * 9 + "\n")
sys.exit("ERROR:\n\nCouldn't find any of the necessary scaling governors.\n")
2019-12-29 21:41:32 +01:00
# root check func
def root_check():
if not os.geteuid() == 0:
2019-12-30 23:28:16 +01:00
print("\n" + "-" * 23 + " Root check " + "-" * 24 + "\n")
sys.exit(f"Must be run as root, i.e: \"sudo {tool_run}\"\n")
2019-12-29 21:41:32 +01:00
exit(1)
2019-12-31 00:37:54 +01:00
# refresh countdown
def countdown(s):
for remaining in range(s, 0, -1):
sys.stdout.write("\r")
sys.stdout.write("\"auto-cpufreq\" refresh in:{:2d}".format(remaining))
sys.stdout.flush()
time.sleep(1)
sys.stdout.write("\rRefreshing ... \n")
2019-12-29 21:41:32 +01:00
# 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)
print("\n" + "-" * 60 + "\n")
2019-12-29 21:41:32 +01:00
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)
print("\n" + "-" * 60 + "\n")
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)
print("\n" + "-" * 60 + "\n")
2019-12-29 21:41:32 +01:00
def autofreq():
2019-12-30 17:51:08 +01:00
print("\n" + "-" * 18 + " CPU frequency scaling " + "-" * 19 + "\n")
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")
2019-12-30 17:51:08 +01:00
# 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-30 23:28:16 +01:00
# cli
@click.command()
@click.option("--live", is_flag=True, help="TBU")
@click.option("--daemon/--remove", default=True, help="TBU")
def cli(live, daemon):
# print --help by default if no argument is provided when auto-cpufreq is run
if len(sys.argv) == 1:
print("\n" + "-" * 22 + " auto-cpufreq " + "-" * 23 + "\n")
print("auto-cpufreq - TBU")
print("\nExample usage: " + tool_run + "--install user")
print("\n-----\n")
s.call(["python3", "auto-cpufreq.py", "--help"])
print("\n" + "-" * 59 + "\n")
else:
if live:
while True:
root_check()
driver_check()
gov_check()
sysinfo()
autofreq()
2019-12-31 00:37:54 +01:00
countdown(15)
#time.sleep(1)
2019-12-30 23:28:16 +01:00
subprocess.call("clear")
elif daemon:
print("daemon ...")
else:
print("remove ...")
2019-12-29 21:41:32 +01:00
if __name__ == '__main__':
2019-12-30 23:28:16 +01:00
# while True:
cli()