//! The decision deadline of the postflop solvers ("Solver ueberall"): `decide_with` marks the start of //! each decision on its thread, and a solver gets what is left of its street's total before it starts. //! Totals per decision (live table): river and flop 2 s, turn 1.25 s, each minus `MARGIN_MS` for the //! work after the solve. Without a mark (tests, tools calling a solver directly) the full total is left. use std::cell::Cell; use std::time::Instant; thread_local! { static START: Cell> = const { Cell::new(None) }; /// `fest_hier`: this thread ignores wall time like `SOLVER_ZEIT=fest` (tests, tools). static FEST_HIER: Cell = const { Cell::new(false) }; } /// Total wall time of one decision per street, in ms. pub const RIVER_MS: f64 = 2000.0; pub const TURN_MS: f64 = 1250.0; pub const FLOP_MS: f64 = 2000.0; /// Kept free after the solve (strategy readout, sampling, the answer). pub const MARGIN_MS: f64 = 150.0; /// Mark the start of a decision on this thread. pub fn begin() { START.with(|s| s.set(Some(Instant::now()))); } /// Milliseconds since the mark (0 without one). pub fn elapsed_ms() -> f64 { START.with(|s| s.get().map_or(0.0, |t| t.elapsed().as_secs_f64() * 1e3)) } /// What a solver may still spend of `total_ms` (may be ≤ 0: then it must not start). pub fn left_ms(total_ms: f64) -> f64 { total_ms - MARGIN_MS - elapsed_ms() } /// Measurement switch `SOLVER_ZEIT=fest`: solvers ignore wall time and always run their full iteration /// count, so ladder and LBR results do not depend on machine load (read once). pub fn fixed() -> bool { static FIXED: std::sync::OnceLock = std::sync::OnceLock::new(); FEST_HIER.with(Cell::get) || *FIXED.get_or_init(|| std::env::var("SOLVER_ZEIT").as_deref() == Ok("fest")) } /// While the guard lives, solvers on THIS thread run their full iteration count whatever the load (as /// `SOLVER_ZEIT=fest`, but per thread). For tests that check a solver's result rather than its deadline: /// under a busy machine the wall-time budget otherwise cuts iterations (different numbers) or drops the /// solve below its minimum (the heuristic decides). Never called on the live path. pub fn fest_hier() -> FestHier { FestHier(FEST_HIER.with(|f| f.replace(true))) } /// Guard of `fest_hier`; restores the previous state on drop. pub struct FestHier(bool); impl Drop for FestHier { fn drop(&mut self) { FEST_HIER.with(|f| f.set(self.0)); } } /// The budget a solve gets: `cap_ms` (its own limit), cut to what is left of the street's total; /// infinite under `SOLVER_ZEIT=fest`. pub fn budget_ms(cap_ms: f64, total_ms: f64) -> f64 { if fixed() { f64::INFINITY } else { cap_ms.min(left_ms(total_ms)) } }