PowerTools/main.py

403 lines
14 KiB
Python
Raw Permalink Normal View History

import time
2022-05-09 18:14:28 +01:00
import os
2022-05-13 19:01:48 +01:00
import json
import asyncio
2022-04-18 22:21:51 +01:00
2022-05-13 19:01:48 +01:00
VERSION = "0.5.0"
2022-05-15 16:52:57 +01:00
SETTINGS_LOCATION = "~/.config/powertools.json"
LOG_LOCATION = "/tmp/powertools.log"
FANTASTIC_INSTALL_DIR = "~/homebrew/plugins/Fantastic"
2022-05-13 19:01:48 +01:00
import logging
logging.basicConfig(
filename = LOG_LOCATION,
format = '%(asctime)s %(levelname)s %(message)s',
filemode = 'w',
force = True)
logger = logging.getLogger()
2022-05-15 16:52:57 +01:00
logger.setLevel(logging.INFO)
logging.info(f"PowerTools v{VERSION} https://github.com/NGnius/PowerTools")
startup_time = time.time()
class CPU:
SCALING_FREQUENCIES = [1700000, 2400000, 2800000]
2022-05-13 19:01:48 +01:00
def __init__(self, number, settings=None):
self.number = number
2022-05-13 19:01:48 +01:00
if settings is not None:
self.set_max_boost(settings["max_boost"])
if settings["online"]:
self.enable()
else:
self.disable()
# TODO governor
if(self.status()):
self.max_boost = self._get_max_boost()
else:
self.max_boost = CPU.SCALING_FREQUENCIES[-1]
def enable(self):
# CPU number 0 is special
if(self.number == 0):
return
filepath = cpu_online_path(self.number)
write_to_sys(filepath, 1)
# The user might have changed the maximum cpu clock while the cpu was offline
self._set_max_boost(self.max_boost)
def disable(self):
# CPU number 0 is special
if(self.number == 0):
return
filepath = cpu_online_path(self.number)
write_to_sys(filepath, 0)
def set_max_boost(self, frequency):
self.max_boost = frequency
if(self.status()):
self._set_max_boost(frequency)
def status(self) -> bool:
# cpu number 0 is always online
if(self.number == 0):
return True
filepath = cpu_online_path(self.number)
return read_from_sys(filepath) == "1"
2022-05-13 19:01:48 +01:00
def governor(self) -> str:
return self._read_scaling_governor()
def settings(self) -> dict:
return {
"online": self.status(),
"max_boost": self.max_boost,
"governor": self.governor(),
}
def _read_scaling_governor(self) -> str:
filepath = cpu_governor_scaling_path(self.number)
return read_from_sys(filepath, amount=-1).strip()
def _write_scaling_governor(self, governor: str):
filepath = cpu_governor_scaling_path(self.number)
with open(filepath, mode="w") as f:
f.write(governor)
def _set_max_boost(self, frequency):
if(frequency == CPU.SCALING_FREQUENCIES[-1]):
self._write_scaling_governor("schedutil")
return
if(self._read_scaling_governor() != "userspace"):
self._write_scaling_governor("userspace")
else:
filepath = cpu_freq_scaling_path(self.number)
write_to_sys(filepath, frequency)
def _get_max_boost(self) -> int:
filepath = cpu_freq_scaling_path(self.number)
freq_maybe = read_from_sys(filepath, amount=-1).strip()
if(freq_maybe is None or len(freq_maybe) == 0 or freq_maybe == "<unsupported>"):
return CPU.SCALING_FREQUENCIES[-1]
freq = int(freq_maybe)
return freq
2022-04-30 02:01:24 +01:00
class Plugin:
CPU_COUNT = 8
FAN_SPEEDS = [0, 1000, 2000, 3000, 4000, 5000, 6000]
2022-04-28 04:03:23 +01:00
auto_fan = True
2022-05-13 19:01:48 +01:00
persistent = True
modified_settings = False
2022-04-30 02:01:24 +01:00
async def get_version(self) -> str:
return VERSION
2022-04-29 22:17:44 +01:00
# CPU stuff
# call from main_view.html with setCPUs(count, smt)
async def set_cpus(self, count, smt=True):
2022-05-13 19:01:48 +01:00
self.modified_settings = True
cpu_count = len(self.cpus)
2022-05-09 18:14:28 +01:00
self.smt = smt
2022-04-22 02:01:48 +01:00
# print("Setting CPUs")
if smt:
count = min(int(count), cpu_count)
for cpu in self.cpus[: count]:
cpu.enable()
for cpu in self.cpus[count :: 1]:
cpu.disable()
2022-04-22 02:01:48 +01:00
else:
count = min(int(count), cpu_count / 2)
# never touch cpu0, since it's special
for cpu in self.cpus[1 : cpu_count : 2]:
cpu.disable()
for cpu in self.cpus[2 : cpu_count : 2]:
if(cpu.number / 2 + 1 > count):
cpu.disable()
2022-04-22 02:01:48 +01:00
else:
cpu.enable()
async def get_cpus(self) -> int:
online_count = 0
for cpu in self.cpus:
if(cpu.status()):
online_count += 1
return online_count
2022-04-22 02:01:48 +01:00
async def get_smt(self) -> bool:
return self.smt
2022-04-21 21:17:23 +01:00
async def set_boost(self, enabled: bool) -> bool:
2022-05-13 19:01:48 +01:00
self.modified_settings = True
write_cpu_boost(enabled)
2022-04-21 21:17:23 +01:00
return True
async def get_boost(self) -> bool:
2022-05-13 19:01:48 +01:00
return read_cpu_boost()
2022-04-18 22:21:51 +01:00
async def set_max_boost(self, index):
2022-05-13 19:01:48 +01:00
self.modified_settings = True
if index < 0 or index >= len(CPU.SCALING_FREQUENCIES):
return 0
selected_freq = CPU.SCALING_FREQUENCIES[index]
for cpu in self.cpus:
cpu.set_max_boost(selected_freq)
return len(self.cpus)
2022-04-21 23:15:41 +01:00
async def get_max_boost(self) -> int:
return CPU.SCALING_FREQUENCIES.index(self.cpus[0].max_boost)
2022-04-21 23:15:41 +01:00
2022-04-30 02:16:10 +01:00
# GPU stuff
async def set_gpu_power(self, value: int, power_number: int) -> bool:
2022-05-13 19:01:48 +01:00
self.modified_settings = True
write_gpu_ppt(power_number, value)
2022-04-30 02:16:10 +01:00
return True
async def get_gpu_power(self, power_number: int) -> int:
2022-05-13 19:01:48 +01:00
return read_gpu_ppt(power_number)
2022-04-30 02:16:10 +01:00
2022-04-29 22:17:44 +01:00
# Fan stuff
2022-04-28 04:03:23 +01:00
async def set_fan_tick(self, tick: int):
2022-05-13 19:01:48 +01:00
self.modified_settings = True
if tick >= len(self.FAN_SPEEDS):
2022-04-28 04:03:23 +01:00
# automatic mode
self.auto_fan = True
2022-04-28 04:03:23 +01:00
write_to_sys("/sys/class/hwmon/hwmon5/recalculate", 0)
write_to_sys("/sys/class/hwmon/hwmon5/fan1_target", 4099) # 4099 is default
2022-05-13 19:01:48 +01:00
#subprocess.Popen("systemctl start jupiter-fan-control.service", stdout=subprocess.PIPE, shell=True).wait()
2022-04-28 04:03:23 +01:00
else:
# manual voltage
self.auto_fan = False
2022-04-28 04:03:23 +01:00
write_to_sys("/sys/class/hwmon/hwmon5/recalculate", 1)
write_to_sys("/sys/class/hwmon/hwmon5/fan1_target", self.FAN_SPEEDS[tick])
2022-05-13 19:01:48 +01:00
#subprocess.Popen("systemctl stop jupiter-fan-control.service", stdout=subprocess.PIPE, shell=True).wait()
2022-04-28 04:03:23 +01:00
async def get_fan_tick(self) -> int:
2022-05-13 19:01:48 +01:00
fan_target = read_fan_target()
2022-04-28 04:03:23 +01:00
fan_input = int(read_from_sys("/sys/class/hwmon/hwmon5/fan1_input", amount=-1).strip())
fan_target_v = float(fan_target) / 1000
fan_input_v = float(fan_input) / 1000
if self.auto_fan:
return len(self.FAN_SPEEDS)
elif fan_target == 4099 or (int(round(fan_target_v)) != int(round(fan_input_v)) and fan_target not in self.FAN_SPEEDS):
2022-04-28 04:03:23 +01:00
# cannot read /sys/class/hwmon/hwmon5/recalculate, so guess based on available fan info
# NOTE: the fan takes time to ramp up, so fan_target will never approximately equal fan_input
2022-05-13 19:01:48 +01:00
# when fan_target was changed recently (hence set RPM caching)
return len(self.FAN_SPEEDS)
2022-04-28 04:03:23 +01:00
else:
2022-05-13 19:01:48 +01:00
# quantize RPM to nearest tick (price is right rules; closest without going over)
for i in range(len(self.FAN_SPEEDS)-1):
if fan_target <= self.FAN_SPEEDS[i]:
2022-04-28 04:03:23 +01:00
return i
return len(self.FAN_SPEEDS)-1 # any higher value is considered as highest manual setting
2022-04-28 04:03:23 +01:00
2022-05-09 18:14:28 +01:00
async def fantastic_installed(self) -> bool:
2022-05-13 19:01:48 +01:00
return os.path.exists(FANTASTIC_INSTALL_DIR)
2022-05-09 18:14:28 +01:00
2022-04-29 22:17:44 +01:00
# Battery stuff
async def get_charge_now(self) -> int:
return int(read_from_sys("/sys/class/hwmon/hwmon2/device/charge_now", amount=-1).strip())
async def get_charge_full(self) -> int:
return int(read_from_sys("/sys/class/hwmon/hwmon2/device/charge_full", amount=-1).strip())
async def get_charge_design(self) -> int:
return int(read_from_sys("/sys/class/hwmon/hwmon2/device/charge_full_design", amount=-1).strip())
2022-04-18 22:21:51 +01:00
# Asyncio-compatible long-running code, executed in a task when the plugin is loaded
async def _main(self):
2022-05-13 19:01:48 +01:00
# startup: load & apply settings
if os.path.exists(SETTINGS_LOCATION):
settings = read_json(SETTINGS_LOCATION)
2022-05-15 16:52:57 +01:00
logging.debug(f"Loaded settings from {SETTINGS_LOCATION}: {settings}")
2022-05-13 19:01:48 +01:00
else:
settings = None
2022-05-15 16:52:57 +01:00
logging.debug(f"Settings {SETTINGS_LOCATION} does not exist, skipped")
2022-05-13 19:01:48 +01:00
if settings is None or settings["persistent"] == False:
2022-05-15 16:52:57 +01:00
logging.debug("Ignoring settings from file")
2022-05-13 19:01:48 +01:00
self.persistent = False
self.cpus = []
for cpu_number in range(0, Plugin.CPU_COUNT):
self.cpus.append(CPU(cpu_number))
2022-05-15 16:52:57 +01:00
# If any core has two threads, smt is True
self.smt = self.cpus[1].status()
if(not self.smt):
for cpu_number in range(2, len(self.cpus), 2):
if(self.cpus[cpu_number].status()):
self.smt = True
break
logging.info(f"SMT state is guessed to be {self.smt}")
2022-05-13 19:01:48 +01:00
else:
# apply settings
logging.debug("Restoring settings from file")
self.persistent = True
# CPU
self.cpus = []
for cpu_number in range(0, Plugin.CPU_COUNT):
self.cpus.append(CPU(cpu_number, settings=settings["cpu"]["threads"][cpu_number]))
self.smt = settings["cpu"]["smt"]
write_cpu_boost(settings["cpu"]["boost"])
# GPU
write_gpu_ppt(1, settings["gpu"]["slowppt"])
write_gpu_ppt(2, settings["gpu"]["fastppt"])
# Fan
if not (os.path.exists(FANTASTIC_INSTALL_DIR) or settings["fan"]["auto"]):
write_to_sys("/sys/class/hwmon/hwmon5/recalculate", 1)
write_to_sys("/sys/class/hwmon/hwmon5/fan1_target", settings["fan"]["target"])
self.dirty = False
2022-05-15 16:52:57 +01:00
logging.info("Handled saved settings, back-end startup complete")
2022-05-13 19:01:48 +01:00
# work loop
while True:
if self.modified_settings and self.persistent:
self.save_settings(self)
self.modified_settings = False
await asyncio.sleep(1)
# called from main_view::onViewReady
async def on_ready(self):
2022-05-15 16:52:57 +01:00
delta = time.time() - startup_time
logging.info(f"Front-end initialised {delta}s after startup")
2022-05-13 19:01:48 +01:00
# persistence
async def get_persistent(self) -> bool:
return self.persistent
async def set_persistent(self, enabled: bool):
logging.debug(f"Persistence is now: {enabled}")
self.persistent = enabled
self.save_settings(self)
def current_settings(self) -> dict:
settings = dict()
settings["cpu"] = self.current_cpu_settings(self)
settings["gpu"] = self.current_gpu_settings(self)
settings["fan"] = self.current_fan_settings(self)
settings["persistent"] = self.persistent
return settings
def current_cpu_settings(self) -> dict:
settings = dict()
cpu_settings = []
for cpu in self.cpus:
cpu_settings.append(cpu.settings())
settings["threads"] = cpu_settings
settings["smt"] = self.smt
settings["boost"] = read_cpu_boost()
return settings
def current_gpu_settings(self) -> dict:
settings = dict()
settings["slowppt"] = read_gpu_ppt(1)
settings["fastppt"] = read_gpu_ppt(2)
return settings
def current_fan_settings(self) -> dict:
settings = dict()
settings["target"] = read_fan_target()
settings["auto"] = self.auto_fan
return settings
def save_settings(self):
settings = self.current_settings(self)
2022-05-15 16:52:57 +01:00
logging.info(f"Saving settings to file: {settings}")
2022-05-13 19:01:48 +01:00
write_json(SETTINGS_LOCATION, settings)
# these are stateless (well, the state is not saved internally) functions, so there's no need for these to be called like a class method
def cpu_online_path(cpu_number: int) -> str:
2022-04-21 23:15:41 +01:00
return f"/sys/devices/system/cpu/cpu{cpu_number}/online"
def cpu_freq_scaling_path(cpu_number: int) -> str:
return f"/sys/devices/system/cpu/cpu{cpu_number}/cpufreq/scaling_setspeed"
2022-04-22 02:01:48 +01:00
def cpu_governor_scaling_path(cpu_number: int) -> str:
return f"/sys/devices/system/cpu/cpu{cpu_number}/cpufreq/scaling_governor"
2022-04-30 02:16:10 +01:00
def gpu_power_path(power_number: int) -> str:
return f"/sys/class/hwmon/hwmon4/power{power_number}_cap"
2022-05-13 19:01:48 +01:00
def read_cpu_boost() -> bool:
return read_from_sys("/sys/devices/system/cpu/cpufreq/boost") == "1"
def write_cpu_boost(enable: bool):
write_to_sys("/sys/devices/system/cpu/cpufreq/boost", int(enable))
def read_gpu_ppt(power_number: int) -> int:
return read_sys_int(gpu_power_path(power_number))
def write_gpu_ppt(power_number:int, value: int):
write_to_sys(gpu_power_path(power_number), value)
def read_fan_target() -> int:
return read_sys_int("/sys/class/hwmon/hwmon5/fan1_target")
def write_to_sys(path, value: int):
with open(path, mode="w") as f:
f.write(str(value))
2022-05-15 16:52:57 +01:00
logging.debug(f"Wrote `{value}` to {path}")
2022-04-21 23:15:41 +01:00
def read_from_sys(path, amount=1):
with open(path, mode="r") as f:
2022-05-15 16:52:57 +01:00
value = f.read(amount)
logging.debug(f"Read `{value}` from {path}")
return value
2022-05-13 19:01:48 +01:00
def read_sys_int(path) -> int:
return int(read_from_sys(path, amount=-1).strip())
def write_json(path, data):
with open(path, mode="w") as f:
json.dump(data, f) # I always guess which is which param and I hate it
def read_json(path):
with open(path, mode="r") as f:
return json.load(f)
2022-05-15 16:52:57 +01:00
os_release = read_from_sys("/etc/os-release", amount=-1).strip()
logging.info(f"/etc/os-release\n{os_release}")