Stop presenting stale data as live, and back off broken devices

- readings.py: PowerReadings.total now sums the known parts (None only when
  nothing is known), with a complete property saying whether it under-counts;
  the snapshot exposes it as partial and the web UI marks it with a tilde.
- display/service.py: telemetry older than 15 s is drawn as -- instead of
  re-rendering frozen readings as if they were current.
- sensors.py: Commander PRO last-known RPMs expire after 10 s instead of
  feeding the power model forever (empty list = unknown); a runtime MSR
  failure backs off 15 s instead of re-opening PawnIO every second; GPU NVML
  reconnects back off 10 s instead of nvmlInit() every second, and the
  sample-buffer timestamp resets on a driver-reset reconnect.
- control.py: CpuTemperature read failures back off 15 s instead of churning
  the MSR handle every poll.
This commit is contained in:
paul committed 2026-12-05 16:54:01 +13:00
1 parent 538a464257
commit 9036c6e734
6 files changed
+46 -7

No files matched your search

+3
View File
@@ -229,6 +229,8 @@ class CaseFanGroup:
class CpuTemperature: class CpuTemperature:
"""CPU package temperature averaged over CPU_AVERAGE readings; None while unavailable.""" """CPU package temperature averaged over CPU_AVERAGE readings; None while unavailable."""
RETRY_SECONDS = 15.0 # pause after a read failure, so a lost PawnIO device is not re-opened every poll
def __init__(self, factory=IntelPackage): def __init__(self, factory=IntelPackage):
self._factory = factory self._factory = factory
self.pkg: IntelPackage | None = None self.pkg: IntelPackage | None = None
@@ -249,6 +251,7 @@ class CpuTemperature:
except OSError as e: except OSError as e:
log.warning("CPU temperature read failed, will retry: %s", e) log.warning("CPU temperature read failed, will retry: %s", e)
self.close() self.close()
self._retry_at = now + self.RETRY_SECONDS
if temp is None: if temp is None:
self._recent.clear() self._recent.clear()
return None return None
+5
View File
@@ -20,6 +20,9 @@ log = logging.getLogger(__name__)
class DisplayService: class DisplayService:
# Telemetry older than this is drawn as "--" rather than presented as a live reading.
STALE_AFTER = 15.0
def __init__(self, renderer: DashboardRenderer, telemetry: Telemetry, brightness: int = 100): def __init__(self, renderer: DashboardRenderer, telemetry: Telemetry, brightness: int = 100):
self.renderer = renderer self.renderer = renderer
self.telemetry = telemetry self.telemetry = telemetry
@@ -54,6 +57,8 @@ class DisplayService:
sample = self.telemetry.wait_newer(seq, timeout=1.5) sample = self.telemetry.wait_newer(seq, timeout=1.5)
if stop.is_set(): if stop.is_set():
break break
if sample is not None and time.time() - sample.t > self.STALE_AFTER:
sample = None # frozen readings must not be presented as current
seq = sample.seq if sample is not None else seq seq = sample.seq if sample is not None else seq
t0 = time.monotonic() t0 = time.monotonic()
if lcd.send_jpeg(encode_jpeg(self.render(sample))): if lcd.send_jpeg(encode_jpeg(self.render(sample))):
+8 -3
View File
@@ -22,6 +22,11 @@ class PowerReadings:
@property @property
def total(self) -> float | None: def total(self) -> float | None:
if self.cpu is None or self.gpu is None or self.other is None: """The known parts summed; None only when nothing is known. While a part is missing
return None the total under-counts -- `complete` says so."""
return self.cpu + self.gpu + self.other known = [v for v in (self.cpu, self.gpu, self.other) if v is not None]
return sum(known) if known else None
@property
def complete(self) -> bool:
return self.cpu is not None and self.gpu is not None and self.other is not None
+27 -2
View File
@@ -298,6 +298,10 @@ class IntelPackage:
# ----------------------------------------------------------------------------- CPU # ----------------------------------------------------------------------------- CPU
class CpuSensors: class CpuSensors:
# A runtime MSR failure usually means the PawnIO device is gone: pause before re-opening
# it, or a broken driver costs a heavy CreateFile+DeviceIoControl every second.
RETRY_SECONDS = 15.0
def __init__(self): def __init__(self):
base = r"\Processor Information(_Total)" base = r"\Processor Information(_Total)"
self.pdh = PerfCounters({ self.pdh = PerfCounters({
@@ -347,12 +351,16 @@ class CpuSensors:
log.warning("MSR read failed, will retry: %s", e) log.warning("MSR read failed, will retry: %s", e)
self.pkg.msr.close() self.pkg.msr.close()
self.pkg = None self.pkg = None
self._next_pkg_attempt = time.monotonic() + self.RETRY_SECONDS
return Readings(temp=temp, load=load, clock=clock, power=power) return Readings(temp=temp, load=load, clock=clock, power=power)
# ----------------------------------------------------------------------------- GPU # ----------------------------------------------------------------------------- GPU
class GpuSensors: class GpuSensors:
# Pause between re-open attempts while the GPU/driver is gone, like CpuSensors'.
RECONNECT_SECONDS = 10.0
def __init__(self, index: int = 0, load_source: str = "windows"): def __init__(self, index: int = 0, load_source: str = "windows"):
"""load_source: "windows" = same figure as Task Manager, "nvml" = NVIDIA's kernel-busy figure.""" """load_source: "windows" = same figure as Task Manager, "nvml" = NVIDIA's kernel-busy figure."""
import pynvml import pynvml
@@ -361,6 +369,7 @@ class GpuSensors:
self.index = index self.index = index
self.handle = None self.handle = None
self._last_sample_ts = 0 self._last_sample_ts = 0
self._next_connect = 0.0
self.engine_load: GpuEngineLoad | None = None self.engine_load: GpuEngineLoad | None = None
if load_source == "windows": if load_source == "windows":
try: try:
@@ -418,6 +427,10 @@ class GpuSensors:
def read(self) -> Readings: def read(self) -> Readings:
if self.handle is None: if self.handle is None:
now = time.monotonic()
if now < self._next_connect:
return Readings()
self._next_connect = now + self.RECONNECT_SECONDS
self._connect() self._connect()
if self.handle is None: if self.handle is None:
return Readings() return Readings()
@@ -441,6 +454,7 @@ class GpuSensors:
except Exception: except Exception:
pass pass
self.handle = None self.handle = None
self._last_sample_ts = 0 # the driver's sample buffer restarts with the driver
return Readings( return Readings(
temp=None if temp is None else float(temp), temp=None if temp is None else float(temp),
load=util, load=util,
@@ -596,6 +610,9 @@ class CommanderPro:
CMD_FAN_RPM = 0x21 CMD_FAN_RPM = 0x21
PORTS = 6 PORTS = 6
RECHECK = 30.0 RECHECK = 30.0
# Last known RPMs must not feed the power model forever: after this long without a
# successful read the fan speeds are unknown, not merely stale.
RPM_STALE = 10.0
def __init__(self): def __init__(self):
self.ch: HidChannel | None = None self.ch: HidChannel | None = None
@@ -605,6 +622,7 @@ class CommanderPro:
self.ports: list[int] = [] self.ports: list[int] = []
self.rpm: dict[int, int] = {} self.rpm: dict[int, int] = {}
self._next_check = 0.0 self._next_check = 0.0
self._read_at = 0.0
self._note: str | None = None self._note: str | None = None
def _say(self, note: str) -> None: def _say(self, note: str) -> None:
@@ -678,13 +696,17 @@ class CommanderPro:
for p in self.ports: for p in self.ports:
r = self._cmd(self.CMD_FAN_RPM, p) r = self._cmd(self.CMD_FAN_RPM, p)
self.rpm[p] = (r[2] << 8) | r[3] self.rpm[p] = (r[2] << 8) | r[3]
self._read_at = time.monotonic()
return {p + 1: rpm for p, rpm in self.rpm.items()} return {p + 1: rpm for p, rpm in self.rpm.items()}
def _fresh_rpms(self) -> bool:
return time.monotonic() - self._read_at <= self.RPM_STALE
def read(self) -> list[int]: def read(self) -> list[int]:
"""RPM of each connected fan (the last known values while iCUE has the device).""" """RPM of each connected fan; empty while the device is lost or held by other software."""
with self._lock: with self._lock:
if self.controlled: if self.controlled:
return list(self.rpm.values()) return list(self.rpm.values()) if self._fresh_rpms() else []
return self._read() return self._read()
def _read(self) -> list[int]: def _read(self) -> list[int]:
@@ -712,9 +734,12 @@ class CommanderPro:
for p in self.ports: for p in self.ports:
r = self._cmd(self.CMD_FAN_RPM, p) r = self._cmd(self.CMD_FAN_RPM, p)
self.rpm[p] = (r[2] << 8) | r[3] self.rpm[p] = (r[2] << 8) | r[3]
self._read_at = time.monotonic()
except Exception as e: except Exception as e:
self.close() self.close()
self._say(f"lost ({e}), retrying in {self.RECHECK:.0f} s") self._say(f"lost ({e}), retrying in {self.RECHECK:.0f} s")
if not self._fresh_rpms():
return []
return [self.rpm[p] for p in self.ports if p in self.rpm] return [self.rpm[p] for p in self.ports if p in self.rpm]
+1 -1
View File
@@ -123,7 +123,7 @@ class Telemetry:
"vram_used": None if s.vram is None else s.vram[0], "vram_used": None if s.vram is None else s.vram[0],
"vram_total": None if s.vram is None else s.vram[1]}, "vram_total": None if s.vram is None else s.vram[1]},
"power": {"cpu": _r(s.power.cpu), "gpu": _r(s.power.gpu), "other": _r(s.power.other), "power": {"cpu": _r(s.power.cpu), "gpu": _r(s.power.gpu), "other": _r(s.power.other),
"total": _r(s.power.total), "total": _r(s.power.total), "partial": not s.power.complete,
"parts": {k: round(v, 1) for k, v in s.power.parts.items()}}, "parts": {k: round(v, 1) for k, v in s.power.parts.items()}},
"row": s.row(), "row": s.row(),
} }
+2 -1
View File
@@ -441,7 +441,8 @@ function drawCurves(state) {
/* ------------------------------------------------------------------ render: header & system */ /* ------------------------------------------------------------------ render: header & system */
function renderHeader(s) { function renderHeader(s) {
setText("hero-total", fmt.n0(s.telemetry.power?.total)); const power = s.telemetry.power;
setText("hero-total", (power?.partial ? "≈" : "") + fmt.n0(power?.total));
const meta = $("meta"); const meta = $("meta");
const elevated = s.app.token?.elevated; const elevated = s.app.token?.elevated;
meta.replaceChildren( meta.replaceChildren(