//! The unopenable path of the whole production chain — probe, status, cache, //! selection, metering — on any host. Neither Mac that runs these tests lacks //! an Energy Model group, so `simulate_no_energy_model_group()` makes this //! process's probe find none before the meter's first use. The process CPU //! energy counter, which ranks above IOReport and is live on those Macs, is //! held frozen (`simulate_frozen_process_energy()`), so that every source //! declines and "no meter at all" is reachable. This file holds this one test //! so that it runs in a process of its own and the hooks cannot reach any //! other test. use proof_cascade::meter::{ hardware_meter_available, ioreport_status, measure_marginal_uj, process_energy_status, real_probe_runs, real_process_probe_runs, simulate_frozen_process_energy, simulate_no_energy_model_group, CounterStatus, IoReportStatus, }; use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::{Arc, Barrier}; /// How many times [`counted_work`] has run in this process. static WORK_CALLS: AtomicU64 = AtomicU64::new(0); fn counted_work() { WORK_CALLS.fetch_add(1, Ordering::SeqCst); std::hint::black_box((0..4000u64).fold(0u64, |a, b| a.wrapping_mul(31).wrapping_add(b))); } /// With no Energy Model group to subscribe to, the status is `Unavailable` — /// never `Live`, never `CpuRailAbsent` — for every thread that asks; with the /// process counter held, it is `Frozen`. The meter is never selected, metering /// gives no figure and never runs the work, and each probe ran exactly once. #[test] fn a_host_with_no_energy_model_group_is_unavailable_and_every_meter_declines() { assert_eq!(real_probe_runs(), 0, "the meter was used in this process before this test: this is not its first use"); assert_eq!(real_process_probe_runs(), 0, "the process counter was used in this process before this test"); simulate_no_energy_model_group(); simulate_frozen_process_energy(); const THREADS: usize = 4; let gate = Arc::new(Barrier::new(THREADS)); let handles: Vec<_> = (0..THREADS) .map(|_| { let gate = gate.clone(); std::thread::spawn(move || { gate.wait(); let status = ioreport_status(); let process = process_energy_status(); let selected = hardware_meter_available(); let figure = if cfg!(target_os = "macos") { measure_marginal_uj(counted_work) } else { None }; (status, process, selected, figure) }) }) .collect(); let seen: Vec<_> = handles.into_iter().map(|h| h.join().expect("a thread panicked")).collect(); let runs = real_probe_runs(); let process_runs = real_process_probe_runs(); let work = WORK_CALLS.load(Ordering::SeqCst); eprintln!( "ioreport unopenable: {THREADS} threads saw {seen:?}; real probe runs: {runs}; process probe runs: \ {process_runs}; work calls: {work}" ); for (i, (status, process, selected, figure)) in seen.iter().enumerate() { assert_eq!(*status, IoReportStatus::Unavailable, "thread {i}: no Energy Model group, yet {status:?}"); if cfg!(target_os = "macos") { assert_eq!(*process, CounterStatus::Frozen, "thread {i}: a held process counter was called {process:?}"); assert!(!selected, "thread {i}: a meter was selected with no Energy Model group and a held counter"); assert_eq!(*figure, None, "thread {i}: a figure came out with no Energy Model group and a held counter"); } } assert_eq!(work, 0, "the work ran {work} times with no usable meter"); assert_eq!(runs, usize::from(cfg!(target_os = "macos")), "the liveness probe's runs in this process"); assert_eq!(process_runs, usize::from(cfg!(target_os = "macos")), "the process-counter probe's runs"); }