From b2da2213b7bf66ef31f9f879686db241bda18c67 Mon Sep 17 00:00:00 2001 From: Semprini Date: Sat, 5 Dec 2026 17:32:22 +1300 Subject: [PATCH] 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. --- README.md | 12 ++++++-- kurgan/cooling/control.py | 61 +++++++++++++++++++++++++++++++++++++-- tests/test_fan_control.py | 49 +++++++++++++++++++++++++++++++ tests/test_profiles.py | 1 + 4 files changed, 117 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 78cee54..99ff14a 100644 --- a/README.md +++ b/README.md @@ -213,7 +213,10 @@ every profile: - Fans run at 100% on start-up. - They run at 100% after a polling gap of over 5 seconds. - They run at 100% when a temperature is missing or invalid. -- A group runs at 100% when one of its fans is below 200 RPM after its 10-second spin-up. +- A group runs at 100% when one of its fans reads below 200 RPM on three consecutive polls + after its 10-second spin-up; a single glitchy reading doesn't count. +- The top fans run at 100% when a fan that was seen turning then reads 0 RPM on three + consecutive polls. - The top fans are held at 100% while either case-fan group runs at 85% or more. They are released once both groups are at 65% or less. ### Ports and channels @@ -250,8 +253,11 @@ On exit the case fans are left at 100% and the top fans go back to the BIOS Smar This is software control, not a hardware thermal safeguard. It can't guarantee a speed change after a USB failure, a forced process kill or a system hang, and the Commander PRO may keep its -last duty. Verify temperatures and fan rotation under your real workloads. Only SYS_FAN4 reports -RPM, so there is no stall detection on the top fans. +last duty. If a top-fan write fails, The Kurgan hands those headers back to the BIOS curve at +once; if even that fails, the UI says what duty they are left at. Verify temperatures and fan +rotation under your real workloads. Only SYS_FAN4's tach is wired to the EC, so top-fan stall +detection covers that fan only: a channel whose tach has never been seen turning is left alone +rather than crying wolf. ## Lighting diff --git a/kurgan/cooling/control.py b/kurgan/cooling/control.py index 718c78c..c260b23 100644 --- a/kurgan/cooling/control.py +++ b/kurgan/cooling/control.py @@ -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 diff --git a/tests/test_fan_control.py b/tests/test_fan_control.py index 089411d..b3e487c 100644 --- a/tests/test_fan_control.py +++ b/tests/test_fan_control.py @@ -38,6 +38,7 @@ class TopFanCurveTest(unittest.TestCase): self.mask = 0 self.duties = [] self.board.manual_mask.side_effect = lambda: self.mask + self.board.rpms.return_value = [0] * 16 # nothing has been seen turning yet def manual(ch, duty): self.mask |= 0x24 @@ -110,6 +111,49 @@ class TopFanCurveTest(unittest.TestCase): top.update(100, 60, 31) self.board.set_manual.assert_called_once_with((2, 5), 100) + def test_a_failed_write_hands_the_fans_back_to_the_bios_curve(self): + self.top.update(40, 70, 0) # manual 100% written on channels 2,5 + self.mask = 0 # the EC forgot (sleep, crash): a rewrite is due ... + self.board.set_manual.side_effect = OSError("bus busy") # ... and fails + with self.assertLogs("kurgan.cooling.control", level="WARNING"): + self.top.update(40, 70, 1) + self.board.set_auto.assert_called_with((2, 5)) + self.assertIn("BIOS curve keeps control", self.top.status) + + def test_a_failed_hand_back_says_what_the_fans_are_left_at(self): + self.top.update(40, 70, 0) + self.mask = 0 + self.board.set_manual.side_effect = OSError("bus busy") + self.board.set_auto.side_effect = OSError("still busy") # even the hand-back fails + with self.assertLogs("kurgan.cooling.control", level="WARNING"): + self.top.update(40, 70, 1) + self.assertIn("held at the last written duty (100%)", self.top.status) + + def test_a_top_fan_that_stops_turning_is_detected(self): + tachs = [0] * 16 + self.board.rpms.return_value = tachs + for now in range(10): # spinning, but no tach has been seen turning: no evidence yet + self.top.update(40, 55, now) + self.assertNotIn("FAILSAFE", self.top.status) + tachs[2], tachs[5] = 1500, 1400 + self.top.update(40, 55, 10) + self.top.update(40, 55, 11) + self.assertNotIn("FAILSAFE", self.top.status) # learned: both tachs turn + tachs[5] = 0 + self.top.update(40, 55, 12) + self.top.update(40, 55, 13) + self.assertNotIn("FAILSAFE", self.top.status) # one glitchy reading is not a stall + with self.assertLogs("kurgan.cooling.control", level="WARNING"): + self.top.update(40, 55, 14) + self.assertIn("FAILSAFE", self.top.status) + self.assertIn("[5]", self.top.status) + self.assertEqual(self.top.duty, 100) + self.board.set_manual.assert_called_with((2, 5), 100) + tachs[5] = 1300 # the fan turns again: the failsafe lifts on the next poll + self.top.update(40, 55, 15) + self.assertNotIn("FAILSAFE", self.top.status) + self.assertEqual(self.top.snapshot()["rpms"], {"2": 1500, "5": 1300}) + def test_gpu_worker_drives_top_fans_and_releases_them_on_exit(self): commander = Mock(spec=CommanderPro) commander.set_duty.return_value = {p: 900 for p in range(1, 7)} @@ -224,8 +268,13 @@ class FanWorkerTest(unittest.TestCase): self.commander.set_duty.return_value = {1: 0, 4: 900, 5: 900, 6: 900} self.settle() self.assertNotIn("FAILSAFE", self.control.status) + # One glitchy reading is not a stall; STALL_READINGS in a row are. self.commander.set_duty.return_value = {4: 900, 5: 0, 6: 900} self.control.update(50, 36) + self.control.update(50, 37) + self.assertNotIn("FAILSAFE", self.control.status) + self.commander.set_duty.reset_mock() + self.control.update(50, 38) self.commander.set_duty.assert_called_with((4, 5, 6), 100) self.assertIn("FAILSAFE", self.control.status) self.assertIn("[5]", self.control.status) diff --git a/tests/test_profiles.py b/tests/test_profiles.py index 6294688..0832f14 100644 --- a/tests/test_profiles.py +++ b/tests/test_profiles.py @@ -34,6 +34,7 @@ class ProfileSwitchTest(unittest.TestCase): self.commander.set_duty.side_effect = lambda ports, duty: {p: 1200 for p in ports} board = Mock(spec=Nct6687) board.manual_mask.return_value = 0x24 + board.rpms.return_value = [0] * 16 # no tach has been seen turning: stall detection stays quiet self.top = TopFanCurve((2, 5), board_factory=lambda: board) self.fans = CaseFanControl(self.commander, (4, 5, 6), (1, 2, 3), top=self.top, profile=BALANCED) self.now = 0.0