//! A fabric member priced on this process's own counters //! (`marginal_energy::measure_member_process`, what `fabric_calibrate` and //! `mpp_energy` fall back to where the IOReport CPU rail is frozen), against an //! oracle sharing no code with the meter: this process's own CPU energy count, //! read around the same span with a bare `proc_pid_rusage`. //! //! One test in its own binary, so no other test's thread runs in this process //! during the span (the span, and the oracle, read every thread of it). #![cfg(feature = "fabric-meter")] use jouleclaw_energy::span::ProcessSpanMeter; use jouleclaw_loader_gguf::marginal_energy::{measure_member_process, MemberRuns, NotMeasured}; /// This process's own CPU energy count, nJ: `proc_pid_rusage(getpid(), /// RUSAGE_INFO_V6)`'s `ri_energy_nj`, read with a bare buffer of the struct's 464 /// bytes at its SDK offset, 336. Written out here, sharing no code with the meter /// it checks. #[cfg(target_os = "macos")] fn own_energy_nj() -> u64 { unsafe extern "C" { fn proc_pid_rusage(pid: i32, flavor: i32, buffer: *mut std::ffi::c_void) -> i32; } let mut buf = [0u64; 58]; // SAFETY: `buf` is 464 writable bytes, the size of `rusage_info_v6`. let rc = unsafe { proc_pid_rusage(std::process::id() as i32, 6, buf.as_mut_ptr().cast()) }; assert_eq!(rc, 0, "proc_pid_rusage on this process"); buf[336 / 8] } #[cfg(not(target_os = "macos"))] fn own_energy_nj() -> u64 { 0 } /// A CPU member (the NEON BF16 matmul) priced on this process: where both of this /// process's counters are live, the figure is given, its GPU part an exact zero, and /// its CPU part agrees with the oracle, read inside the span at the first operation's /// start and the last one's end — so the span holds the oracle's window and differs /// from it only by the meter's last and first reads around it — and the per-GFLOP /// figure is that energy over the work. Where they are not, the member is refused, /// never priced on the host meter. Twice, three seconds each, however loaded the host. #[test] fn a_cpu_member_is_priced_on_this_process_as_its_own_count_says() { use std::cell::Cell; let meter = ProcessSpanMeter::new(jouleclaw_backend_apple::gpu_liveness_load); let (m, n, k) = (16usize, 512usize, 512usize); let gflop = 2.0 * (m * n * k) as f64 / 1e9; let a: Vec = (0..m * k).map(|i| ((i % 101) as f32 / 50.0) - 1.0).collect(); let w: Vec = (0..n * k) .flat_map(|i| jouleclaw_loader_gguf::matmul_bf16::f32_to_bf16_rne(((i % 97) as f32 / 48.0 - 1.0) * 0.1).to_le_bytes()) .collect(); let mut out = vec![0f32; m * n]; for round in 1..=2 { let (first, last) = (Cell::new(None), Cell::new(0u64)); let figure = measure_member_process(&meter, MemberRuns::Cpu, gflop, 3.0, || { if first.get().is_none() { first.set(Some(own_energy_nj())); } jouleclaw_loader_gguf::matmul_bf16::matmul_bf16_neon(&a, &w, &[], m, n, k, &mut out); last.set(own_energy_nj()); }); let oracle_uj = (last.get() - first.get().expect("the work ran")) / 1_000; eprintln!("CPU member round {round}: oracle {oracle_uj} µJ; metered={} {figure:?}", meter.is_metered()); if meter.is_metered() { let f = figure.expect("a CPU member on live counters is priced"); assert_eq!(f.gpu_uj, 0, "the NEON matmul spent GPU energy: {f:?}"); assert_eq!(f.provenance, jouleclaw_energy::Provenance::HwShunt); assert!(oracle_uj > 100_000, "setup: the work read {oracle_uj} µJ"); let ratio = f.cpu_uj as f64 / oracle_uj as f64; assert!((0.9..=1.1).contains(&ratio), "the span read {} µJ of {oracle_uj} µJ", f.cpu_uj); let total = f.uj_per_gflop * f.ops as f64 * gflop; assert!((total - f.cpu_uj as f64).abs() <= 1.0, "{total} µJ priced of {} µJ read", f.cpu_uj); // CI's macOS energy job greps for this line: the oracle ran on live counters. eprintln!("process calibration: CPU member round {round} read {} µJ of {oracle_uj} µJ", f.cpu_uj); } else { let why = figure.expect_err("no live counters, yet a figure"); assert!( matches!(why, NotMeasured::NotWhole(_) | NotMeasured::NotOwnCounters(_) | NotMeasured::SpanFailed(_)), "{why}" ); } } }