From 9036c6e7344ed59fd692e2a9a076f40078c28f5b Mon Sep 17 00:00:00 2001 From: Semprini Date: Sat, 5 Dec 2026 16:54:01 +1300 Subject: [PATCH] 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. --- kurgan/cooling/control.py | 3 +++ kurgan/display/service.py | 5 +++++ kurgan/telemetry/readings.py | 11 ++++++++--- kurgan/telemetry/sensors.py | 29 +++++++++++++++++++++++++++-- kurgan/telemetry/service.py | 2 +- kurgan/web/static/app.js | 3 ++- 6 files changed, 46 insertions(+), 7 deletions(-) diff --git a/kurgan/cooling/control.py b/kurgan/cooling/control.py index 8652d27..718c78c 100644 --- a/kurgan/cooling/control.py +++ b/kurgan/cooling/control.py @@ -229,6 +229,8 @@ class CaseFanGroup: class CpuTemperature: """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): self._factory = factory self.pkg: IntelPackage | None = None @@ -249,6 +251,7 @@ class CpuTemperature: except OSError as e: log.warning("CPU temperature read failed, will retry: %s", e) self.close() + self._retry_at = now + self.RETRY_SECONDS if temp is None: self._recent.clear() return None diff --git a/kurgan/display/service.py b/kurgan/display/service.py index d9d66fe..de47fda 100644 --- a/kurgan/display/service.py +++ b/kurgan/display/service.py @@ -20,6 +20,9 @@ log = logging.getLogger(__name__) 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): self.renderer = renderer self.telemetry = telemetry @@ -54,6 +57,8 @@ class DisplayService: sample = self.telemetry.wait_newer(seq, timeout=1.5) if stop.is_set(): 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 t0 = time.monotonic() if lcd.send_jpeg(encode_jpeg(self.render(sample))): diff --git a/kurgan/telemetry/readings.py b/kurgan/telemetry/readings.py index 64bb5f1..801601c 100644 --- a/kurgan/telemetry/readings.py +++ b/kurgan/telemetry/readings.py @@ -22,6 +22,11 @@ class PowerReadings: @property def total(self) -> float | None: - if self.cpu is None or self.gpu is None or self.other is None: - return None - return self.cpu + self.gpu + self.other + """The known parts summed; None only when nothing is known. While a part is missing + the total under-counts -- `complete` says so.""" + 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 diff --git a/kurgan/telemetry/sensors.py b/kurgan/telemetry/sensors.py index 8c1556b..1d7fbb0 100644 --- a/kurgan/telemetry/sensors.py +++ b/kurgan/telemetry/sensors.py @@ -298,6 +298,10 @@ class IntelPackage: # ----------------------------------------------------------------------------- CPU 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): base = r"\Processor Information(_Total)" self.pdh = PerfCounters({ @@ -347,12 +351,16 @@ class CpuSensors: log.warning("MSR read failed, will retry: %s", e) self.pkg.msr.close() self.pkg = None + self._next_pkg_attempt = time.monotonic() + self.RETRY_SECONDS return Readings(temp=temp, load=load, clock=clock, power=power) # ----------------------------------------------------------------------------- GPU 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"): """load_source: "windows" = same figure as Task Manager, "nvml" = NVIDIA's kernel-busy figure.""" import pynvml @@ -361,6 +369,7 @@ class GpuSensors: self.index = index self.handle = None self._last_sample_ts = 0 + self._next_connect = 0.0 self.engine_load: GpuEngineLoad | None = None if load_source == "windows": try: @@ -418,6 +427,10 @@ class GpuSensors: def read(self) -> Readings: if self.handle is None: + now = time.monotonic() + if now < self._next_connect: + return Readings() + self._next_connect = now + self.RECONNECT_SECONDS self._connect() if self.handle is None: return Readings() @@ -441,6 +454,7 @@ class GpuSensors: except Exception: pass self.handle = None + self._last_sample_ts = 0 # the driver's sample buffer restarts with the driver return Readings( temp=None if temp is None else float(temp), load=util, @@ -596,6 +610,9 @@ class CommanderPro: CMD_FAN_RPM = 0x21 PORTS = 6 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): self.ch: HidChannel | None = None @@ -605,6 +622,7 @@ class CommanderPro: self.ports: list[int] = [] self.rpm: dict[int, int] = {} self._next_check = 0.0 + self._read_at = 0.0 self._note: str | None = None def _say(self, note: str) -> None: @@ -678,13 +696,17 @@ class CommanderPro: for p in self.ports: r = self._cmd(self.CMD_FAN_RPM, p) self.rpm[p] = (r[2] << 8) | r[3] + self._read_at = time.monotonic() 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]: - """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: if self.controlled: - return list(self.rpm.values()) + return list(self.rpm.values()) if self._fresh_rpms() else [] return self._read() def _read(self) -> list[int]: @@ -712,9 +734,12 @@ class CommanderPro: for p in self.ports: r = self._cmd(self.CMD_FAN_RPM, p) self.rpm[p] = (r[2] << 8) | r[3] + self._read_at = time.monotonic() except Exception as e: self.close() 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] diff --git a/kurgan/telemetry/service.py b/kurgan/telemetry/service.py index 38800eb..df70483 100644 --- a/kurgan/telemetry/service.py +++ b/kurgan/telemetry/service.py @@ -123,7 +123,7 @@ class Telemetry: "vram_used": None if s.vram is None else s.vram[0], "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), - "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()}}, "row": s.row(), } diff --git a/kurgan/web/static/app.js b/kurgan/web/static/app.js index 49e216f..b62f2cb 100644 --- a/kurgan/web/static/app.js +++ b/kurgan/web/static/app.js @@ -441,7 +441,8 @@ function drawCurves(state) { /* ------------------------------------------------------------------ render: header & system */ 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 elevated = s.app.token?.elevated; meta.replaceChildren(