use std::io::Write;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Mutex, OnceLock};
static ACTIVE: AtomicBool = AtomicBool::new(false);
static SINK: Mutex<Option<std::io::BufWriter<std::fs::File>>> = Mutex::new(None);
static INIT: OnceLock<()> = OnceLock::new();
pub struct TuDecision {
pub x: u32,
pub y: u32,
pub log2_size: u8,
pub trafo_depth: u8,
pub region_idx: usize,
pub leaf_dist: u64,
pub leaf_bits: u64,
pub leaf_cost: f64,
pub split_dist: u64,
pub split_bits: u64,
pub split_cost: f64,
pub margin: f64,
pub winner_split: bool,
pub escalated: bool,
}
fn init() {
INIT.get_or_init(|| {
if let Ok(path) = std::env::var("BPG_TU_DIAG") {
if !path.trim().is_empty() {
if let Ok(f) = std::fs::File::create(path.trim()) {
let mut w = std::io::BufWriter::new(f);
let _ = writeln!(
w,
"x,y,log2,depth,region,leaf_dist,leaf_bits,leaf_cost,split_dist,split_bits,split_cost,margin,winner_split,escalated,split_won_by"
);
*SINK.lock().unwrap() = Some(w);
ACTIVE.store(true, Ordering::Relaxed);
}
}
}
});
}
#[inline]
pub fn active() -> bool {
init();
ACTIVE.load(Ordering::Relaxed)
}
pub fn record(d: &TuDecision) {
if let Some(w) = SINK.lock().unwrap().as_mut() {
let split_won_by = d.leaf_cost - d.split_cost;
let _ = writeln!(
w,
"{},{},{},{},{},{},{},{:.3},{},{},{:.3},{:.4},{},{},{:.4}",
d.x,
d.y,
d.log2_size,
d.trafo_depth,
d.region_idx,
d.leaf_dist,
d.leaf_bits,
d.leaf_cost,
d.split_dist,
d.split_bits,
d.split_cost,
d.margin,
d.winner_split as u8,
d.escalated as u8,
split_won_by,
);
}
}
pub fn flush() {
if let Some(w) = SINK.lock().unwrap().as_mut() {
let _ = w.flush();
}
}