Top fans: stall detection from the tach registers, and no manual duty left behind

- control.py: TopFanCurve reads the NCT6687D tach registers while the fans are
  commanded to spin. A channel whose fan was seen turning and then reads 0 RPM
  on three consecutive polls raises everything to 100% and says FAILSAFE. A
  channel whose tach has never been seen turning (SYS_FAN1's is not wired on
  this board) is left alone, so an unwired or empty header cannot cry wolf.
  The latest readings are in the snapshot.
- control.py: CaseFanGroup's stall check now needs three consecutive low
  readings instead of one; a single glitchy RPM no longer forces 100%.
- control.py: when a top-fan write fails, the channels are handed back to the
  BIOS curve at once (best effort); the status says so, or names the duty the
  fans are left at when even the hand-back fails. Previously the UI claimed
  "BIOS curve keeps control" even while the fans sat at a manual duty.
- Tests (three new, one reworked, board doubles given rpms) and README updated
  to match.
This commit is contained in:
paul committed 2026-12-05 17:32:22 +13:00
1 parent 8969a57045
commit b2da2213b7
4 files changed
+117 -6

No files matched your search

+58 -3
View File
@@ -25,6 +25,8 @@ BOOST_ON, BOOST_OFF = 85, 65 # case fan duty (either side) that starts / ends t
TOP_START, TOP_STOP = BALANCED.top_start, BALANCED.top_stop # top fans start at / stop below (C) ...
TOP_STOP_HOLD = 30 # ... once it has stayed below the stop temperature for this many seconds
TOP_KICK, TOP_KICK_SECONDS = 40, 3 # spin-up duty when starting from stopped
# Consecutive low/zero-RPM polls (1 s apart) before a stall is believed: one reading can glitch.
STALL_READINGS = 3
def parse_ports(text: str) -> tuple[int, ...]:
@@ -58,7 +60,8 @@ class TopFanCurve:
"""Motherboard header fans on their own curve from the hotter of CPU/GPU, stopped while cool.
They are also held at 100% while either case fan group works hard; on exit the BIOS curve
takes over again."""
takes over again. A fan that stops turning while commanded to spin raises all of them to
100% and says so (see _tach_stall)."""
RETRY = 30.0
@@ -73,12 +76,15 @@ class TopFanCurve:
self.running = False
self.duty = 0
self.status = "Starting"
self.rpms: dict[int, int] = {} # latest tach reading per channel, for the snapshot
self._retry_at = 0.0
self._written: int | None = None
self._kick_until = 0.0
self._cool_since: float | None = None
self._last_rise = 0.0
self._last_update: float | None = None
self._spun: set[int] = set() # channels whose tach has been seen turning
self._zeros = collections.Counter() # consecutive zero-RPM polls per channel
def apply_profile(self, profile: FanProfile) -> None:
self.curve, self.start, self.stop = profile.top_curve, profile.top_start, profile.top_stop
@@ -91,6 +97,7 @@ class TopFanCurve:
def snapshot(self) -> dict:
return {"channels": list(self.channels), "temp": self.temp, "duty": self.duty, "running": self.running,
"boosted": self.boosted, "status": self.status,
"rpms": {str(c): self.rpms.get(c) for c in self.channels},
"failsafe": self.status.startswith(("FAILSAFE", "UNAVAILABLE"))}
def close(self) -> None:
@@ -122,6 +129,32 @@ class TopFanCurve:
target = max(target, TOP_KICK)
return target, f"{temp:.0f} C, {{duty}}%" + (f" (case fans >= {BOOST_ON}%)" if self.boosted else "")
def _tach_stall(self, now: float) -> list[int] | None:
"""Channels whose fan was seen turning but now reads 0 RPM: stalled or unplugged.
A channel whose tachometer has never been seen turning (an empty header, or a tach this
board doesn't wire to the channel) is left alone, so a wrong channel-to-tach guess
cannot cry wolf. Returns the stalled channels; the duty is raised to 100% for the
caller to write."""
assert self.board is not None
tachs = self.board.rpms()
self.rpms = {c: tachs[c] for c in self.channels}
if self.duty <= 0 or now < self._kick_until:
self._zeros.clear() # stopped or spinning up: zero RPM proves nothing yet
return None
stalled = []
for c in self.channels:
if self.rpms[c] > 0:
self._spun.add(c)
self._zeros[c] = 0
elif c in self._spun:
self._zeros[c] += 1
if self._zeros[c] >= STALL_READINGS:
stalled.append(c)
if stalled:
self.duty, self._last_rise = 100, now
return stalled or None
def update(self, case_duty: int, temp: float | None, now: float) -> None:
self.temp = temp
target, text = self._target(case_duty, temp, now)
@@ -138,13 +171,31 @@ class TopFanCurve:
try:
if self.board is None:
self.board = self._factory()
stalled = self._tach_stall(now)
# Compared every poll, so a firmware reset (e.g. after sleep) or a crash is corrected.
if self.duty != self._written or self.board.manual_mask() & self._mask != self._mask:
self.board.set_manual(self.channels, self.duty)
self._written = self.duty
if stalled:
text = f"FAILSAFE 100%: no RPM on channels {stalled}"
self._status(text.format(duty=self.duty), warning=text.startswith("FAILSAFE"))
except (OSError, ValueError) as e:
self._status(f"UNAVAILABLE ({e}); BIOS curve keeps control", warning=True)
# Best effort: hand the channels back to the BIOS curve, so a manual duty left
# behind (0% included) doesn't persist until the next successful write or a reboot.
held, handed_back = self._written, False
try:
if self.board is not None:
self.board.set_auto(self.channels)
handed_back = True
except Exception: # the fallback must never mask the original error
log.debug("Cannot hand the top fans back to the BIOS curve", exc_info=True)
if handed_back:
note = "BIOS curve keeps control"
elif held is not None:
note = f"fans held at the last written duty ({held}%)"
else:
note = "no duty was ever written"
self._status(f"UNAVAILABLE ({e}); {note}", warning=True)
self.close()
self._retry_at = now + self.RETRY
@@ -175,6 +226,7 @@ class CaseFanGroup:
self._last_update: float | None = None
self._last_rise = 0.0
self._started: float | None = None
self._lows = {p: 0 for p in self.ports} # consecutive low-RPM polls per port
def set_status(self, text: str, warning: bool = False) -> None:
if text != self.status:
@@ -194,6 +246,7 @@ class CaseFanGroup:
self.duty = 100
self._last_rise = now
self._started = now
self._lows = {p: 0 for p in self.ports}
elapsed = 0 if self._last_update is None else min(2, max(0, now - self._last_update))
if target >= self.duty:
if target > self.duty or invalid:
@@ -204,7 +257,9 @@ class CaseFanGroup:
self._last_update = now
rpms = self.rpms = self.commander.set_duty(self.ports, self.duty)
if self._started is not None and now - self._started >= 10:
stalled = [p for p in self.ports if rpms.get(p, 0) < 200]
for p in self.ports:
self._lows[p] = self._lows[p] + 1 if rpms.get(p, 0) < 200 else 0
stalled = [p for p in self.ports if self._lows[p] >= STALL_READINGS]
if stalled:
self.duty = 100
self._last_rise = now