// SPDX-License-Identifier: MIT //! Operator control intent for the live runtime. //! //! The management status in `management.rs` is read-only by design. This is the //! one small mutable channel the other way: the web UI (and any local client) //! WRITES a desired mode/target and a run switch here, and the serve-path //! runtime loop READS it each control period and applies it with //! [`crate::runtime::MinerRuntime::set_target`]. It owns nothing the fault //! supervisor owns: a thermal or chain fault still stops the machine regardless //! of what the operator asked for. //! //! A generation counter lets the loop notice a change cheaply without diffing //! the target every period. use crate::tuning::TuningTarget; use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::sync::Mutex; /// The operator-visible control modes. Each maps onto exactly one /// [`TuningTarget`] the runtime already implements, so the UI never claims a /// behaviour the controller does not have. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ControlMode { /// Hold a DC power draw; efficiency follows from the power point. The MPC /// "power target" mode. Power, /// Hold the hottest sensor a fixed margin above measured ambient. The MPC /// "ambient temperature target" mode. Ambient, /// Seek a hashrate ceiling. Hashrate, /// Pin the fan at a duty and ramp frequency under it. MaxFan, } impl ControlMode { pub fn as_str(self) -> &'static str { match self { ControlMode::Power => "power", ControlMode::Ambient => "ambient", ControlMode::Hashrate => "hashrate", ControlMode::MaxFan => "fan", } } pub fn label(self) -> &'static str { match self { ControlMode::Power => "Power target", ControlMode::Ambient => "Ambient temperature target", ControlMode::Hashrate => "Hashrate ceiling", ControlMode::MaxFan => "Max fan", } } pub fn all() -> [ControlMode; 4] { [ ControlMode::Power, ControlMode::Ambient, ControlMode::Hashrate, ControlMode::MaxFan, ] } pub fn of_target(target: TuningTarget) -> ControlMode { match target { TuningTarget::PowerW(_) => ControlMode::Power, TuningTarget::AmbientC { .. } => ControlMode::Ambient, TuningTarget::HashrateThs(_) => ControlMode::Hashrate, TuningTarget::MaxFan { .. } => ControlMode::MaxFan, } } } /// The unit and sensible value for a mode's input box, for the UI form. pub fn mode_input(mode: ControlMode) -> (&'static str, &'static str) { match mode { ControlMode::Power => ("W", "watts of DC draw to hold"), ControlMode::Ambient => ("C", "degrees above ambient to hold the hottest sensor"), ControlMode::Hashrate => ("TH/s", "hashrate ceiling"), ControlMode::MaxFan => ("% PWM", "fan duty to pin (0-100)"), } } /// Build a [`TuningTarget`] from a mode name and its single value. The value is /// in the mode's natural unit (watts, degrees C, TH/s, percent). pub fn parse_target(mode: &str, value: f32) -> Result { if !value.is_finite() || value < 0.0 { return Err(format!("value {value} is not a non-negative number")); } Ok(match mode { "power" => TuningTarget::PowerW(value), "hashrate" => TuningTarget::HashrateThs(value), "ambient" => TuningTarget::AmbientC { // UI gives whole degrees C; the controller works in millidegrees. margin_mc: (value * 1000.0).round() as i32, }, "fan" => { if value > 100.0 { return Err("fan duty must be 0-100".into()); } TuningTarget::MaxFan { max_pwm: value.round() as u8, } } other => return Err(format!("unknown mode {other:?}")), }) } /// The compact `kind:value` label, matching the management `target` field and /// `app::target_label`. pub fn target_label(target: TuningTarget) -> String { match target { TuningTarget::PowerW(w) => format!("power:{}", w.round() as i64), TuningTarget::HashrateThs(t) => format!("hashrate:{}", t.round() as i64), TuningTarget::AmbientC { margin_mc } => format!("ambient:{margin_mc}"), TuningTarget::MaxFan { max_pwm } => format!("fan:{max_pwm}"), } } /// Shared operator intent. Cheap to read every control period. #[derive(Debug)] pub struct Control { running: AtomicBool, generation: AtomicU64, target: Mutex, } impl Control { pub fn new(initial: TuningTarget, running: bool) -> Self { Self { running: AtomicBool::new(running), generation: AtomicU64::new(1), target: Mutex::new(initial), } } /// Monotonic counter, bumped on every mutation. The loop keeps the last /// value it applied and only re-applies when this moves. pub fn generation(&self) -> u64 { self.generation.load(Ordering::Acquire) } pub fn running(&self) -> bool { self.running.load(Ordering::Acquire) } pub fn set_running(&self, running: bool) { self.running.store(running, Ordering::Release); self.generation.fetch_add(1, Ordering::AcqRel); } pub fn target(&self) -> TuningTarget { *self.target.lock().expect("control target lock") } pub fn set_target(&self, target: TuningTarget) { *self.target.lock().expect("control target lock") = target; self.generation.fetch_add(1, Ordering::AcqRel); } pub fn set_mode_value(&self, mode: &str, value: f32) -> Result<(), String> { let target = parse_target(mode, value)?; self.set_target(target); Ok(()) } } impl Default for Control { fn default() -> Self { // Default to a conservative power target, stopped until the operator starts it. Self::new(TuningTarget::PowerW(2_400.0), false) } } #[cfg(test)] mod tests { use super::*; #[test] fn modes_round_trip_through_targets() { for mode in ControlMode::all() { let value = match mode { ControlMode::MaxFan => 80.0, ControlMode::Ambient => 20.0, _ => 2_400.0, }; let target = parse_target(mode.as_str(), value).expect("parse"); assert_eq!(ControlMode::of_target(target), mode); } } #[test] fn ambient_degrees_become_millidegrees() { let target = parse_target("ambient", 20.0).expect("parse"); assert_eq!(target, TuningTarget::AmbientC { margin_mc: 20_000 }); } #[test] fn fan_duty_is_bounded() { assert!(parse_target("fan", 120.0).is_err()); assert_eq!( parse_target("fan", 100.0).expect("parse"), TuningTarget::MaxFan { max_pwm: 100 } ); } #[test] fn negative_and_unknown_are_rejected() { assert!(parse_target("power", -1.0).is_err()); assert!(parse_target("bogus", 1.0).is_err()); } #[test] fn setting_target_or_running_bumps_the_generation() { let control = Control::new(TuningTarget::PowerW(2_000.0), false); let g0 = control.generation(); control.set_target(TuningTarget::HashrateThs(100.0)); let g1 = control.generation(); assert!(g1 > g0); assert_eq!(control.target(), TuningTarget::HashrateThs(100.0)); control.set_running(true); assert!(control.generation() > g1); assert!(control.running()); } #[test] fn label_matches_management_shape() { assert_eq!(target_label(TuningTarget::PowerW(3_000.0)), "power:3000"); assert_eq!( target_label(TuningTarget::AmbientC { margin_mc: 20_000 }), "ambient:20000" ); } }