Tie the detection plan, the fault ladder and the tuner together, where their order is decided: a stopped machine is never tuned, a disabled chain is never dispatched to, and a missing measurement holds the last command instead of inventing one. MinerRuntime exposes the active chain set, the stop reason and an operating point that reflects the last applied command rather than the boot-time default, which is what the API snapshot reports.
This commit is contained in:
@@ -16,6 +16,7 @@ pub mod management;
|
||||
pub mod miner;
|
||||
pub mod pow;
|
||||
pub mod profiles;
|
||||
pub mod runtime;
|
||||
pub mod scrypt_pow;
|
||||
pub mod split;
|
||||
pub mod stratum_client;
|
||||
|
||||
+278
@@ -0,0 +1,278 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
//! Mining runtime: one state machine over the detection plan, the fault ladder and the tuner.
|
||||
//!
|
||||
//! Each tick takes a measurement, lets the tuner move the operating point, and lets the fault
|
||||
//! supervisor veto the machine. The pieces are tested in isolation elsewhere; this module is the
|
||||
//! place where their order is decided, and the order matters: a stopped machine must not be tuned,
|
||||
//! and a disabled chain must not be dispatched to.
|
||||
|
||||
use crate::detect::{HardwarePlan, OperatingPoint};
|
||||
use crate::fault::{FaultAction, FaultEvent, FaultPolicy, FaultSupervisor, StopReason};
|
||||
use crate::tuning::{Command, Observation, PidConfig, TuningError, TuningLoop, TuningTarget};
|
||||
|
||||
/// One control period's measurements.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct Tick {
|
||||
pub power_w: Option<f32>,
|
||||
pub hashrate_ths: Option<f32>,
|
||||
pub hottest_mc: Option<i32>,
|
||||
pub ambient_mc: Option<i32>,
|
||||
pub dt_s: f32,
|
||||
}
|
||||
|
||||
/// What the runtime decided for this period.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum TickOutcome {
|
||||
/// Apply this command to the hardware.
|
||||
Run(Command),
|
||||
/// Measurement missing; hold the previous operating point.
|
||||
Hold(Command),
|
||||
/// The machine is stopped and must not hash.
|
||||
Stopped(StopReason),
|
||||
}
|
||||
|
||||
/// Runtime state for one machine.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct MinerRuntime {
|
||||
plan: HardwarePlan,
|
||||
faults: FaultSupervisor,
|
||||
tuning: TuningLoop,
|
||||
last_command: Command,
|
||||
}
|
||||
|
||||
impl MinerRuntime {
|
||||
pub fn new(
|
||||
plan: HardwarePlan,
|
||||
policy: FaultPolicy,
|
||||
target: TuningTarget,
|
||||
pid: PidConfig,
|
||||
ceiling_mc: i32,
|
||||
) -> Self {
|
||||
let chains: Vec<u8> = plan.chains.iter().map(|chain| chain.index).collect();
|
||||
let last_command = Command {
|
||||
frequency_mhz: plan.operating_point.frequency_mhz,
|
||||
fan_pwm: None,
|
||||
};
|
||||
Self {
|
||||
faults: FaultSupervisor::new(&chains, policy),
|
||||
tuning: TuningLoop::new(target, plan.envelope, pid, ceiling_mc),
|
||||
plan,
|
||||
last_command,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn plan(&self) -> &HardwarePlan {
|
||||
&self.plan
|
||||
}
|
||||
|
||||
pub fn operating_point(&self) -> OperatingPoint {
|
||||
OperatingPoint {
|
||||
voltage_mv: self.plan.operating_point.voltage_mv,
|
||||
frequency_mhz: self.last_command.frequency_mhz,
|
||||
confirmed: self.plan.operating_point.confirmed,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn active_chains(&self) -> Vec<u8> {
|
||||
self.faults.active_chains()
|
||||
}
|
||||
|
||||
pub fn stop_reason(&self) -> Option<StopReason> {
|
||||
self.faults.stop_reason()
|
||||
}
|
||||
|
||||
/// Feed one health observation; returns what the supervisor decided.
|
||||
pub fn on_event(&mut self, event: FaultEvent) -> FaultAction {
|
||||
self.faults.observe(event)
|
||||
}
|
||||
|
||||
/// Advance one control period.
|
||||
pub fn tick(&mut self, tick: &Tick) -> TickOutcome {
|
||||
if let Some(reason) = self.faults.stop_reason() {
|
||||
return TickOutcome::Stopped(reason);
|
||||
}
|
||||
let observation = Observation {
|
||||
power_w: tick.power_w,
|
||||
hashrate_ths: tick.hashrate_ths,
|
||||
hottest_mc: tick.hottest_mc,
|
||||
ambient_mc: tick.ambient_mc,
|
||||
};
|
||||
match self.tuning.step(observation, tick.dt_s) {
|
||||
Ok(command) => {
|
||||
self.last_command = command;
|
||||
TickOutcome::Run(command)
|
||||
}
|
||||
Err(TuningError::NoMeasurement) => TickOutcome::Hold(self.last_command),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::detect::{ChainReport, ChipModel, EepromState, Envelope, FanZone};
|
||||
|
||||
fn plan() -> HardwarePlan {
|
||||
HardwarePlan {
|
||||
profile: "s19jpro-bm1362-pic".into(),
|
||||
chip: ChipModel::Bm1362,
|
||||
chains: (0..3)
|
||||
.map(|index| ChainReport {
|
||||
index,
|
||||
chip: ChipModel::Bm1362,
|
||||
chips_found: 126,
|
||||
eeprom: EepromState::Absent,
|
||||
response_per_mille: Some(950),
|
||||
})
|
||||
.collect(),
|
||||
envelope: Envelope {
|
||||
v_min_mv: 1100,
|
||||
v_max_mv: 1500,
|
||||
f_min_mhz: 300,
|
||||
f_max_mhz: 700,
|
||||
},
|
||||
operating_point: OperatingPoint {
|
||||
voltage_mv: 1200,
|
||||
frequency_mhz: 300,
|
||||
confirmed: false,
|
||||
},
|
||||
psu: "APW12".into(),
|
||||
fan_zones: vec![FanZone {
|
||||
fan: 0,
|
||||
zones: vec![0],
|
||||
}],
|
||||
}
|
||||
}
|
||||
|
||||
fn runtime() -> MinerRuntime {
|
||||
MinerRuntime::new(
|
||||
plan(),
|
||||
FaultPolicy::default(),
|
||||
TuningTarget::PowerW(2_400.0),
|
||||
PidConfig {
|
||||
kp: 0.2,
|
||||
ki: 0.05,
|
||||
kd: 0.0,
|
||||
out_min: 0.0,
|
||||
out_max: 0.0,
|
||||
max_step_per_s: 50.0,
|
||||
deadband: 0.0,
|
||||
},
|
||||
95_000,
|
||||
)
|
||||
}
|
||||
|
||||
fn tick(power: f32) -> Tick {
|
||||
Tick {
|
||||
power_w: Some(power),
|
||||
hashrate_ths: None,
|
||||
hottest_mc: Some(70_000),
|
||||
ambient_mc: None,
|
||||
dt_s: 1.0,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_fresh_runtime_exposes_every_chain() {
|
||||
assert_eq!(runtime().active_chains(), vec![0, 1, 2]);
|
||||
assert_eq!(runtime().stop_reason(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ticks_move_frequency_inside_the_envelope() {
|
||||
let mut runtime = runtime();
|
||||
let mut frequency = 300;
|
||||
for _ in 0..60 {
|
||||
let power = 4.0 * f32::from(frequency);
|
||||
if let TickOutcome::Run(command) = runtime.tick(&tick(power)) {
|
||||
frequency = command.frequency_mhz;
|
||||
assert!((300..=700).contains(&frequency));
|
||||
}
|
||||
}
|
||||
assert!(frequency > 300, "frequency never moved: {frequency}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_missing_measurement_holds_the_last_command() {
|
||||
let mut runtime = runtime();
|
||||
let empty = Tick {
|
||||
power_w: None,
|
||||
hashrate_ths: None,
|
||||
hottest_mc: None,
|
||||
ambient_mc: None,
|
||||
dt_s: 1.0,
|
||||
};
|
||||
match runtime.tick(&empty) {
|
||||
TickOutcome::Hold(command) => assert_eq!(command.frequency_mhz, 300),
|
||||
other => panic!("unexpected outcome: {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_dead_chain_is_removed_from_dispatch_without_stopping_the_machine() {
|
||||
let mut runtime = runtime();
|
||||
assert_eq!(
|
||||
runtime.on_event(FaultEvent::ChainError { chain: 1 }),
|
||||
FaultAction::Retry { chain: 1 }
|
||||
);
|
||||
assert_eq!(
|
||||
runtime.on_event(FaultEvent::ChainError { chain: 1 }),
|
||||
FaultAction::Retry { chain: 1 }
|
||||
);
|
||||
assert_eq!(
|
||||
runtime.on_event(FaultEvent::ChainError { chain: 1 }),
|
||||
FaultAction::DisableChain { chain: 1 }
|
||||
);
|
||||
assert_eq!(runtime.active_chains(), vec![0, 2]);
|
||||
assert!(matches!(runtime.tick(&tick(2_000.0)), TickOutcome::Run(_)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_temperature_fault_stops_the_machine_for_good() {
|
||||
let mut runtime = runtime();
|
||||
assert_eq!(
|
||||
runtime.on_event(FaultEvent::TemperatureFault { zone: 1 }),
|
||||
FaultAction::Stop {
|
||||
reason: StopReason::Temperature
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
runtime.tick(&tick(2_400.0)),
|
||||
TickOutcome::Stopped(StopReason::Temperature)
|
||||
);
|
||||
assert_eq!(
|
||||
runtime.tick(&tick(2_400.0)),
|
||||
TickOutcome::Stopped(StopReason::Temperature)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn losing_every_chain_stops_the_machine() {
|
||||
let mut runtime = MinerRuntime::new(
|
||||
plan(),
|
||||
FaultPolicy {
|
||||
retries: 0,
|
||||
..FaultPolicy::default()
|
||||
},
|
||||
TuningTarget::PowerW(2_400.0),
|
||||
PidConfig::default(),
|
||||
95_000,
|
||||
);
|
||||
for chain in 0..3 {
|
||||
runtime.on_event(FaultEvent::ChainError { chain });
|
||||
}
|
||||
assert_eq!(runtime.stop_reason(), Some(StopReason::NoChainsLeft));
|
||||
assert!(runtime.active_chains().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_operating_point_reflects_the_last_command() {
|
||||
let mut runtime = runtime();
|
||||
for _ in 0..10 {
|
||||
runtime.tick(&tick(1_500.0));
|
||||
}
|
||||
assert!(runtime.operating_point().frequency_mhz > 300);
|
||||
assert!(!runtime.operating_point().confirmed);
|
||||
assert_eq!(runtime.operating_point().voltage_mv, 1200);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user