From c616429669bfc19807908f051f92776a02e76c7c Mon Sep 17 00:00:00 2001 From: Noah <2+noah@noreply.gitea.bluefla.re> Date: Thu, 22 Oct 2026 10:19:45 -0600 Subject: [PATCH] Map fans to thermal zones by active probing Fans are not labeled by the hardware: a fan can cool one hashboard heavily and another barely, and front/back top/bottom is physical. map_fans learns the map from a stepped run: for every fan compare zone temperatures at the lowest and highest duty, then give each zone to the fan with the strongest cooling response. A response must clear min_drop_mc and beat the runner-up by min_margin_per_mille, otherwise the zone is reported unresolved rather than guessed, so a noisy run cannot invent a map. One fan may own several zones, and a fan with no duty range contributes no evidence. --- src/fanmap.rs | 239 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 2 files changed, 240 insertions(+) create mode 100644 src/fanmap.rs diff --git a/src/fanmap.rs b/src/fanmap.rs new file mode 100644 index 0000000..209d2b1 --- /dev/null +++ b/src/fanmap.rs @@ -0,0 +1,239 @@ +// SPDX-License-Identifier: MIT +//! Active fan-to-thermal-zone mapping. +//! +//! Antminer chassis wire fans to zones in a way that is not reported by the hardware: a fan may +//! cool one hashboard heavily and another barely, and the front/back, top/bottom labels are +//! physical. The mapping is learned by stepping one fan at a time and watching which zone's +//! temperature responds, which is why this module takes measurements rather than configuration. + +use crate::detect::FanZone; + +/// One sensor reading taken while a fan was held at a given duty. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct ZoneReading { + pub zone: u8, + pub temp_mc: i32, +} + +/// One measurement step: a fan duty and the temperatures observed during it. +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct ProbeSample { + pub fan: u8, + pub pwm: u8, + pub readings: Vec, +} + +/// Thresholds that decide when a response is real. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct MapThresholds { + /// Minimum cooling (millidegrees) between the low and high duty to count as a response. + pub min_drop_mc: i32, + /// Minimum margin over the next-best fan, in parts per thousand of the best response. + pub min_margin_per_mille: u16, +} + +impl Default for MapThresholds { + fn default() -> Self { + Self { + min_drop_mc: 500, + min_margin_per_mille: 250, + } + } +} + +/// The learned mapping. +#[derive(Clone, Debug, Default, Eq, PartialEq)] +pub struct FanMap { + pub assignments: Vec, + /// Zones whose response was ambiguous and were therefore left unassigned. + pub unresolved: Vec, + /// Per assigned zone, the margin over the runner-up in parts per thousand. + pub confidence_per_mille: Vec, +} + +impl FanMap { + /// Zones a fan was confidently assigned to. + pub fn zones_for(&self, fan: u8) -> Vec { + self.assignments + .iter() + .find(|assignment| assignment.fan == fan) + .map(|assignment| assignment.zones.clone()) + .unwrap_or_default() + } +} + +/// Response per (fan, zone): positive means the zone cooled when that fan sped up. +fn responses(samples: &[ProbeSample], zones: &[u8]) -> Vec<(u8, Vec<(u8, i32)>)> { + let mut fans: Vec = samples.iter().map(|sample| sample.fan).collect(); + fans.sort_unstable(); + fans.dedup(); + let mut out = Vec::new(); + for fan in fans { + let fan_samples: Vec<&ProbeSample> = + samples.iter().filter(|sample| sample.fan == fan).collect(); + let min_pwm = fan_samples.iter().map(|sample| sample.pwm).min(); + let max_pwm = fan_samples.iter().map(|sample| sample.pwm).max(); + let (Some(low), Some(high)) = (min_pwm, max_pwm) else { + continue; + }; + if low == high { + continue; + } + let mut per_zone = Vec::with_capacity(zones.len()); + for &zone in zones { + let mean_at = |pwm: u8| -> Option { + let values: Vec = fan_samples + .iter() + .filter(|sample| sample.pwm == pwm) + .flat_map(|sample| { + sample + .readings + .iter() + .filter(|reading| reading.zone == zone) + .map(|reading| reading.temp_mc) + }) + .collect(); + if values.is_empty() { + return None; + } + let sum: i64 = values.iter().map(|value| *value as i64).sum(); + Some((sum / values.len() as i64) as i32) + }; + // Duty goes up, temperature should come down: response = warm-run minus fast-run. + if let (Some(slow_temp), Some(fast_temp)) = (mean_at(low), mean_at(high)) { + per_zone.push((zone, slow_temp - fast_temp)); + } + } + out.push((fan, per_zone)); + } + out +} + +/// Learn which fans cool which zones from a stepped measurement run. +/// +/// Every zone goes to the fan with the strongest cooling response, provided that response clears +/// `min_drop_mc` and beats the runner-up by `min_margin_per_mille`. Anything weaker stays +/// unresolved instead of being guessed, so a noisy run cannot invent a fan map. +pub fn map_fans(samples: &[ProbeSample], zones: &[u8], thresholds: MapThresholds) -> FanMap { + let responses = responses(samples, zones); + let mut map = FanMap::default(); + for &zone in zones { + let mut ranked: Vec<(i32, u8)> = responses + .iter() + .filter_map(|(fan, per_zone)| { + per_zone + .iter() + .find(|(candidate, _)| *candidate == zone) + .map(|(_, drop)| (*drop, *fan)) + }) + .collect(); + ranked.sort_unstable_by_key(|entry| std::cmp::Reverse(entry.0)); + let Some(&(best_drop, best_fan)) = ranked.first() else { + map.unresolved.push(zone); + continue; + }; + let runner_up = ranked.get(1).map(|(drop, _)| *drop).unwrap_or(0); + let margin = best_drop - runner_up.max(0); + let margin_per_mille = if best_drop > 0 { + ((margin as i64 * 1000) / best_drop as i64) as u16 + } else { + 0 + }; + if best_drop < thresholds.min_drop_mc || margin_per_mille < thresholds.min_margin_per_mille + { + map.unresolved.push(zone); + continue; + } + match map + .assignments + .iter_mut() + .find(|assignment| assignment.fan == best_fan) + { + Some(assignment) => assignment.zones.push(zone), + None => map.assignments.push(FanZone { + fan: best_fan, + zones: vec![zone], + }), + } + map.confidence_per_mille.push(margin_per_mille); + } + map +} + +#[cfg(test)] +mod tests { + use super::*; + + fn sample(fan: u8, pwm: u8, zone_temps: &[(u8, i32)]) -> ProbeSample { + ProbeSample { + fan, + pwm, + readings: zone_temps + .iter() + .map(|(zone, temp)| ZoneReading { + zone: *zone, + temp_mc: *temp, + }) + .collect(), + } + } + + #[test] + fn each_zone_is_assigned_to_the_fan_that_cools_it() { + let samples = vec![ + sample(0, 30, &[(0, 70_000), (1, 60_000)]), + sample(0, 100, &[(0, 50_000), (1, 59_000)]), + sample(1, 30, &[(0, 69_000), (1, 60_000)]), + sample(1, 100, &[(0, 68_000), (1, 40_000)]), + ]; + let map = map_fans(&samples, &[0, 1], MapThresholds::default()); + assert_eq!(map.zones_for(0), vec![0]); + assert_eq!(map.zones_for(1), vec![1]); + assert!(map.unresolved.is_empty()); + } + + #[test] + fn ambiguity_leaves_a_zone_unresolved() { + let samples = vec![ + sample(0, 30, &[(0, 70_000)]), + sample(0, 100, &[(0, 60_000)]), + sample(1, 30, &[(0, 70_500)]), + sample(1, 100, &[(0, 60_500)]), + ]; + let map = map_fans(&samples, &[0], MapThresholds::default()); + assert!(map.assignments.is_empty()); + assert_eq!(map.unresolved, vec![0]); + } + + #[test] + fn a_fan_that_does_not_change_temperature_is_not_assigned() { + let samples = vec![ + sample(0, 30, &[(0, 60_000)]), + sample(0, 100, &[(0, 59_900)]), + ]; + let map = map_fans(&samples, &[0], MapThresholds::default()); + assert!(map.assignments.is_empty()); + assert_eq!(map.unresolved, vec![0]); + } + + #[test] + fn a_fan_without_duty_range_is_skipped() { + let samples = vec![sample(0, 50, &[(0, 60_000)]), sample(0, 50, &[(0, 60_000)])]; + let map = map_fans(&samples, &[0], MapThresholds::default()); + assert!(map.assignments.is_empty()); + assert_eq!(map.unresolved, vec![0]); + } + + #[test] + fn one_fan_can_own_several_zones() { + let samples = vec![ + sample(0, 30, &[(0, 70_000), (1, 71_000), (2, 55_000)]), + sample(0, 100, &[(0, 55_000), (1, 56_000), (2, 54_000)]), + sample(1, 30, &[(0, 70_000), (1, 71_000), (2, 54_000)]), + sample(1, 100, &[(0, 69_000), (1, 70_000), (2, 50_000)]), + ]; + let map = map_fans(&samples, &[0, 1, 2], MapThresholds::default()); + assert_eq!(map.zones_for(0), vec![0, 1]); + assert_eq!(map.zones_for(1), vec![2]); + } +} diff --git a/src/lib.rs b/src/lib.rs index 64df9c4..f04f641 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ pub mod bm1370; pub mod bm13xx; pub mod detect; pub mod difficulty; +pub mod fanmap; pub mod hal_wire; pub mod management; pub mod miner;