feat(vm): add resource diagnostics and macOS stability qualification
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
Intrepid/Prometeu/Runtime/pipeline/pr-master This commit looks good
This commit is contained in:
@@ -4,6 +4,13 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
license.workspace = true
|
||||
|
||||
[features]
|
||||
resource-diagnostics = []
|
||||
|
||||
[[example]]
|
||||
name = "resource_qualification"
|
||||
required-features = ["resource-diagnostics"]
|
||||
|
||||
[dependencies]
|
||||
prometeu-bytecode = { path = "../prometeu-bytecode" }
|
||||
prometeu-hal = { path = "../prometeu-hal" }
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
//! Bounded, opt-in macOS qualification workload. See --help.
|
||||
#[path = "../tests/support/qualification.rs"]
|
||||
mod qualification;
|
||||
use prometeu_vm::resource_diagnostics::ResourceSnapshot;
|
||||
use qualification::Workload;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
fn report(name: &str, frames: u64, hash: u64, s: &ResourceSnapshot) {
|
||||
println!(
|
||||
"checkpoint name={name} frames={frames} digest={hash:016x} cycles={} live={} free={} retired={} strings={} depth={} peak_depth={} used={:?} peaks={:?} limits={:?} reservation_refusals={:?} gc={} reclaimed={} gc_p50_upper_ns={} gc_p95_upper_ns={} gc_p99_upper_ns={} gc_max_ns={} mark_max_ns={} sweep_max_ns={} reconcile_max_ns={}",
|
||||
s.cycles,
|
||||
s.live_objects,
|
||||
s.free_handles,
|
||||
s.retired_handles,
|
||||
s.tracked_strings,
|
||||
s.call_depth,
|
||||
s.peak_call_depth,
|
||||
s.budget.used,
|
||||
s.budget.peaks,
|
||||
s.budget.limits,
|
||||
s.budget.refusals,
|
||||
s.gc.collections,
|
||||
s.gc.reclaimed,
|
||||
s.gc.total.percentile_upper_ns(50),
|
||||
s.gc.total.percentile_upper_ns(95),
|
||||
s.gc.total.percentile_upper_ns(99),
|
||||
s.gc.total.max_ns,
|
||||
s.gc.mark.max_ns,
|
||||
s.gc.sweep.max_ns,
|
||||
s.gc.string_reconciliation.max_ns
|
||||
);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<_> = std::env::args().collect();
|
||||
if args.len() != 3 || !matches!(args[1].as_str(), "--frames" | "--seconds") {
|
||||
eprintln!(
|
||||
"usage: resource_qualification --frames N | --seconds N\nRuns all three stationary families with lockstep replay; --seconds counts active batches only. Seed=73. Checkpoints every 10000 frames, fixed-size memory. Does not certify desktop or other hosts."
|
||||
);
|
||||
std::process::exit(2);
|
||||
}
|
||||
let amount: u64 = args[2].parse().expect("positive integer duration/frame count");
|
||||
assert!(amount > 0);
|
||||
let timed = args[1] == "--seconds";
|
||||
println!(
|
||||
"start os={} arch={} debug={} seed=73 mode={} amount={} pid={}",
|
||||
std::env::consts::OS,
|
||||
std::env::consts::ARCH,
|
||||
cfg!(debug_assertions),
|
||||
args[1],
|
||||
amount,
|
||||
std::process::id()
|
||||
);
|
||||
let mut workloads: Vec<_> =
|
||||
qualification::NAMES.iter().map(|&name| Workload::new(name)).collect();
|
||||
let mut replays: Vec<_> =
|
||||
qualification::NAMES.iter().map(|&name| Workload::new(name)).collect();
|
||||
let started = Instant::now();
|
||||
let mut active = Duration::ZERO;
|
||||
let mut excluded = Duration::ZERO;
|
||||
while if timed { active.as_secs() < amount } else { workloads[0].frames < amount } {
|
||||
let batch_started = Instant::now();
|
||||
for (w, replay) in workloads.iter_mut().zip(&mut replays) {
|
||||
let batch = if timed { 64 } else { 64.min(amount - w.frames) };
|
||||
for _ in 0..batch {
|
||||
w.frame();
|
||||
replay.frame();
|
||||
assert_eq!(w.digest, replay.digest, "{} replay at {}", w.name, w.frames);
|
||||
if w.frames % 10_000 == 0 {
|
||||
w.check_stationary();
|
||||
replay.check_stationary();
|
||||
assert_eq!(
|
||||
w.vm.resource_snapshot().budget,
|
||||
replay.vm.resource_snapshot().budget
|
||||
);
|
||||
report(w.name, w.frames, w.digest, &w.vm.resource_snapshot());
|
||||
}
|
||||
}
|
||||
}
|
||||
let elapsed = batch_started.elapsed();
|
||||
// Conservatively exclude any long interval, including sleep/descheduling.
|
||||
// This can extend the run; it never credits suspended time as active work.
|
||||
if elapsed <= Duration::from_secs(2) {
|
||||
active += elapsed;
|
||||
} else {
|
||||
excluded += elapsed;
|
||||
}
|
||||
}
|
||||
println!(
|
||||
"load_complete active_seconds={:.3} wall_seconds={:.3} excluded_seconds={:.3}",
|
||||
active.as_secs_f64(),
|
||||
started.elapsed().as_secs_f64(),
|
||||
excluded.as_secs_f64()
|
||||
);
|
||||
for (w, replay) in workloads.into_iter().zip(replays) {
|
||||
let name = w.name;
|
||||
let frames = w.frames;
|
||||
let expected = (w.digest, w.vm.resource_snapshot().cycles);
|
||||
report(name, frames, w.digest, &w.vm.resource_snapshot());
|
||||
w.finish();
|
||||
assert_eq!(
|
||||
(replay.digest, replay.vm.resource_snapshot().cycles),
|
||||
expected,
|
||||
"{name} replay"
|
||||
);
|
||||
replay.finish();
|
||||
println!(
|
||||
"replay_pass name={name} frames={frames} digest={:016x} cycles={} disposed_usage=zero",
|
||||
expected.0, expected.1
|
||||
);
|
||||
}
|
||||
println!("qualification_pass scope=vm_workloads_only");
|
||||
}
|
||||
@@ -427,6 +427,11 @@ impl Heap {
|
||||
pub fn allocation_count(&self) -> u64 {
|
||||
self.allocation_count.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
pub(crate) fn diagnostic_handles(&self) -> (usize, usize) {
|
||||
(self.free_list.len(), self.objects.len() - self.free_list.len() - self.len())
|
||||
}
|
||||
}
|
||||
|
||||
// Internal consumer seam for PLN-0176 step 18 / AGD-0054. Executable image
|
||||
|
||||
@@ -16,6 +16,8 @@ mod local_addressing;
|
||||
mod heap;
|
||||
mod object;
|
||||
pub mod resource_budget;
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
pub mod resource_diagnostics;
|
||||
mod roots;
|
||||
// Bounded Registry preflight is consumed by the upcoming v1 image loader.
|
||||
mod program_image;
|
||||
|
||||
@@ -260,6 +260,28 @@ fn constant_strings_share_image_domain_across_sessions_and_native_snapshots() {
|
||||
assert_eq!(text.as_ref(), "shared");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn final_session_and_snapshot_drop_release_image_and_constant_domain() {
|
||||
let bytes = fixture(&[1, 0], 0, &[K::String("shared".into())], None, &[]);
|
||||
let image = ValidatedProgramImage::load(&bytes, &mut budget(), caps::NONE).unwrap();
|
||||
let image_weak = Arc::downgrade(&image);
|
||||
let registry_weak = Arc::downgrade(&image.registry);
|
||||
let strings_weak = Arc::downgrade(&image.strings);
|
||||
let lease = image.lease_constant(0).unwrap();
|
||||
let first = crate::VirtualMachine::from_image(image.clone(), Default::default());
|
||||
let second = crate::VirtualMachine::from_image(image.clone(), Default::default());
|
||||
drop(image);
|
||||
drop(first);
|
||||
assert!(image_weak.upgrade().is_some());
|
||||
drop(second);
|
||||
assert!(image_weak.upgrade().is_none());
|
||||
assert!(registry_weak.upgrade().is_none());
|
||||
assert_eq!(lease.retained_content(), (6, 1));
|
||||
assert!(strings_weak.upgrade().is_some());
|
||||
drop(lease);
|
||||
assert!(strings_weak.upgrade().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "explicit release measurement; not a timing assertion"]
|
||||
fn sealed_image_load_probe() {
|
||||
|
||||
@@ -42,6 +42,10 @@ impl RuntimeLimits {
|
||||
struct State {
|
||||
limits: RuntimeLimits,
|
||||
used: [u64; COUNT],
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
peaks: [u64; COUNT],
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
refusals: [u64; COUNT],
|
||||
}
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RuntimeBudget(Arc<Mutex<State>>);
|
||||
@@ -52,7 +56,14 @@ impl Default for RuntimeBudget {
|
||||
}
|
||||
impl RuntimeBudget {
|
||||
pub fn new(limits: RuntimeLimits) -> Self {
|
||||
Self(Arc::new(Mutex::new(State { limits, used: [0; COUNT] })))
|
||||
Self(Arc::new(Mutex::new(State {
|
||||
limits,
|
||||
used: [0; COUNT],
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
peaks: [0; COUNT],
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
refusals: [0; COUNT],
|
||||
})))
|
||||
}
|
||||
fn state(&self) -> MutexGuard<'_, State> {
|
||||
self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
@@ -73,8 +84,13 @@ impl RuntimeBudget {
|
||||
pub fn check(&self, resource: RuntimeResource, requested: u64) -> Result<(), ResourceFailure> {
|
||||
let limit = self.limit(resource);
|
||||
if requested > limit {
|
||||
Err(ResourceFailure { resource, reason: ResourceReason::Limit, requested, limit })
|
||||
Err(self.failure(resource, ResourceReason::Limit, requested))
|
||||
} else {
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
{
|
||||
let mut state = self.state();
|
||||
state.peaks[resource as usize] = state.peaks[resource as usize].max(requested);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -84,6 +100,11 @@ impl RuntimeBudget {
|
||||
reason: ResourceReason,
|
||||
requested: u64,
|
||||
) -> ResourceFailure {
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
{
|
||||
let mut state = self.state();
|
||||
state.refusals[resource as usize] += 1;
|
||||
}
|
||||
ResourceFailure { resource, reason, requested, limit: self.limit(resource) }
|
||||
}
|
||||
pub fn bytes<T>(
|
||||
@@ -104,13 +125,23 @@ impl RuntimeBudget {
|
||||
let mut state = self.state();
|
||||
let current = state.used[resource as usize];
|
||||
let limit = state.limits.get(resource);
|
||||
let requested = current.checked_add(amount).ok_or(ResourceFailure {
|
||||
resource,
|
||||
reason: ResourceReason::SizeOverflow,
|
||||
requested: u64::MAX,
|
||||
limit,
|
||||
})?;
|
||||
let Some(requested) = current.checked_add(amount) else {
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
{
|
||||
state.refusals[resource as usize] += 1;
|
||||
}
|
||||
return Err(ResourceFailure {
|
||||
resource,
|
||||
reason: ResourceReason::SizeOverflow,
|
||||
requested: u64::MAX,
|
||||
limit,
|
||||
});
|
||||
};
|
||||
if requested > limit {
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
{
|
||||
state.refusals[resource as usize] += 1;
|
||||
}
|
||||
return Err(ResourceFailure {
|
||||
resource,
|
||||
reason: ResourceReason::Limit,
|
||||
@@ -119,11 +150,27 @@ impl RuntimeBudget {
|
||||
});
|
||||
}
|
||||
state.used[resource as usize] = requested;
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
{
|
||||
state.peaks[resource as usize] = state.peaks[resource as usize].max(requested);
|
||||
}
|
||||
Ok(Reservation { budget: self.clone(), resource, amount })
|
||||
}
|
||||
pub(crate) fn empty_charge(&self, resource: RuntimeResource) -> Reservation {
|
||||
Reservation { budget: self.clone(), resource, amount: 0 }
|
||||
}
|
||||
|
||||
/// Engineering-only counters. Holding the budget does not retain guest owners.
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
pub fn diagnostic_snapshot(&self) -> crate::resource_diagnostics::BudgetSnapshot {
|
||||
let state = self.state();
|
||||
crate::resource_diagnostics::BudgetSnapshot {
|
||||
used: state.used,
|
||||
peaks: state.peaks,
|
||||
refusals: state.refusals,
|
||||
limits: state.limits.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Unique accounting ownership; moving this token never charges a second time.
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
//! Opt-in engineering observations, never a guest ABI or a source of GC roots.
|
||||
//! Timing distributions are bounded logarithmic histograms in nanoseconds.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct BudgetSnapshot {
|
||||
/// RuntimeResource discriminant order; depth/individual string size are checked,
|
||||
/// not reserved, so their current usage remains zero and their peak records checks.
|
||||
pub used: [u64; 8],
|
||||
pub peaks: [u64; 8],
|
||||
/// Includes refused speculative capacity growth that may fall back to exact growth.
|
||||
pub refusals: [u64; 8],
|
||||
pub limits: [u64; 8],
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DurationHistogram {
|
||||
buckets: [u64; 64],
|
||||
pub count: u64,
|
||||
pub max_ns: u64,
|
||||
}
|
||||
impl Default for DurationHistogram {
|
||||
fn default() -> Self {
|
||||
Self { buckets: [0; 64], count: 0, max_ns: 0 }
|
||||
}
|
||||
}
|
||||
impl DurationHistogram {
|
||||
pub(crate) fn record(&mut self, elapsed: std::time::Duration) {
|
||||
let ns = elapsed.as_nanos().min(u64::MAX as u128) as u64;
|
||||
let bucket = (63 - ns.max(1).leading_zeros()) as usize;
|
||||
self.buckets[bucket] += 1;
|
||||
self.count += 1;
|
||||
self.max_ns = self.max_ns.max(ns);
|
||||
}
|
||||
/// Inclusive upper bound for the percentile's bucket, capped by observed max.
|
||||
pub fn percentile_upper_ns(&self, percentile: u64) -> u64 {
|
||||
assert!((1..=100).contains(&percentile));
|
||||
if self.count == 0 {
|
||||
return 0;
|
||||
}
|
||||
let rank = (self.count as u128 * percentile as u128).div_ceil(100);
|
||||
let mut seen = 0u128;
|
||||
for (i, &count) in self.buckets.iter().enumerate() {
|
||||
seen += count as u128;
|
||||
if seen >= rank {
|
||||
return (u64::MAX >> (63 - i)).min(self.max_ns);
|
||||
}
|
||||
}
|
||||
self.max_ns
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct GcDiagnostics {
|
||||
pub collections: u64,
|
||||
pub reclaimed: u64,
|
||||
pub mark: DurationHistogram,
|
||||
pub sweep: DurationHistogram,
|
||||
pub total: DurationHistogram,
|
||||
pub string_reconciliation: DurationHistogram,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ResourceSnapshot {
|
||||
pub budget: BudgetSnapshot,
|
||||
pub live_objects: usize,
|
||||
pub free_handles: usize,
|
||||
pub retired_handles: usize,
|
||||
pub tracked_strings: usize,
|
||||
pub call_depth: usize,
|
||||
pub peak_call_depth: usize,
|
||||
pub cycles: u64,
|
||||
pub gc: GcDiagnostics,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn bounded_histogram_reports_upper_bounds_and_exact_max() {
|
||||
let mut h = DurationHistogram::default();
|
||||
assert_eq!(h.percentile_upper_ns(99), 0);
|
||||
for ns in 0..100 {
|
||||
h.record(std::time::Duration::from_nanos(ns));
|
||||
}
|
||||
assert_eq!(h.count, 100);
|
||||
assert_eq!(h.percentile_upper_ns(50), 63);
|
||||
assert_eq!(h.percentile_upper_ns(95), 99);
|
||||
assert_eq!(h.max_ns, 99);
|
||||
}
|
||||
}
|
||||
@@ -129,6 +129,10 @@ pub struct VirtualMachine {
|
||||
/// Syscalls are capability-gated using `prometeu_hal::syscalls::SyscallMeta::caps`.
|
||||
capabilities: prometeu_hal::syscalls::CapFlags,
|
||||
budget: RuntimeBudget,
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
gc_diagnostics: crate::resource_diagnostics::GcDiagnostics,
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
peak_call_depth: usize,
|
||||
}
|
||||
|
||||
impl Default for VirtualMachine {
|
||||
@@ -138,6 +142,26 @@ impl Default for VirtualMachine {
|
||||
}
|
||||
|
||||
impl VirtualMachine {
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
pub fn resource_observer(&self) -> RuntimeBudget {
|
||||
self.budget.clone()
|
||||
}
|
||||
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
pub fn resource_snapshot(&self) -> crate::resource_diagnostics::ResourceSnapshot {
|
||||
let (free_handles, retired_handles) = self.heap.diagnostic_handles();
|
||||
crate::resource_diagnostics::ResourceSnapshot {
|
||||
budget: self.budget.diagnostic_snapshot(),
|
||||
live_objects: self.heap.len(),
|
||||
free_handles,
|
||||
retired_handles,
|
||||
tracked_strings: self.strings.as_ref().map_or(0, |s| s.tracked_allocations()),
|
||||
call_depth: self.call_stack.len(),
|
||||
peak_call_depth: self.peak_call_depth.max(self.call_stack.len()),
|
||||
cycles: self.cycles,
|
||||
gc: self.gc_diagnostics.clone(),
|
||||
}
|
||||
}
|
||||
/// Returns the current program counter.
|
||||
pub fn pc(&self) -> usize {
|
||||
self.pc
|
||||
@@ -248,6 +272,10 @@ impl VirtualMachine {
|
||||
last_gc_live_count: 0,
|
||||
capabilities: NONE,
|
||||
budget,
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
gc_diagnostics: Default::default(),
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
peak_call_depth: 0,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,8 @@ impl VirtualMachine {
|
||||
if self.gc_alloc_threshold > 0
|
||||
&& self.heap.len().saturating_sub(self.last_gc_live_count) >= self.gc_alloc_threshold
|
||||
{
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
let (started, before) = (std::time::Instant::now(), self.heap.len());
|
||||
let roots = self
|
||||
.operand_stack
|
||||
.iter()
|
||||
@@ -12,11 +14,27 @@ impl VirtualMachine {
|
||||
.filter_map(crate::roots::value_heap_ref)
|
||||
.chain(self.call_stack.iter().filter_map(|frame| frame.closure));
|
||||
self.heap.mark_session_roots(roots)?;
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
let sweep_started = {
|
||||
self.gc_diagnostics.mark.record(started.elapsed());
|
||||
std::time::Instant::now()
|
||||
};
|
||||
self.heap.sweep();
|
||||
self.last_gc_live_count = self.heap.len();
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
{
|
||||
self.gc_diagnostics.collections += 1;
|
||||
self.gc_diagnostics.reclaimed += (before - self.last_gc_live_count) as u64;
|
||||
self.gc_diagnostics.sweep.record(sweep_started.elapsed());
|
||||
self.gc_diagnostics.total.record(started.elapsed());
|
||||
}
|
||||
}
|
||||
if let Some(strings) = &self.strings {
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
let started = std::time::Instant::now();
|
||||
strings.reconcile();
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
self.gc_diagnostics.string_reconciliation.record(started.elapsed());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -121,7 +121,16 @@ impl VirtualMachine {
|
||||
}
|
||||
let had_call = self.lifecycle.is_some();
|
||||
let before = self.cycles;
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
{
|
||||
// Include an entry/hook whose first instruction immediately returns.
|
||||
self.peak_call_depth = self.peak_call_depth.max(self.call_stack.len());
|
||||
}
|
||||
let result = self.step_inner(native, ctx);
|
||||
#[cfg(feature = "resource-diagnostics")]
|
||||
{
|
||||
self.peak_call_depth = self.peak_call_depth.max(self.call_stack.len());
|
||||
}
|
||||
if !had_call && self.lifecycle.is_some() && result.is_ok() {
|
||||
return Err(LogicalFrameEndingReason::BudgetExhausted);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
#![cfg(feature = "resource-diagnostics")]
|
||||
#[allow(dead_code)]
|
||||
#[path = "support/callables.rs"]
|
||||
mod callables;
|
||||
#[path = "support/qualification.rs"]
|
||||
mod qualification;
|
||||
use prometeu_bytecode::{Value, model::ConstantPoolEntry};
|
||||
use prometeu_vm::{
|
||||
LogicalFrameEndingReason, VirtualMachine,
|
||||
resource_budget::{RuntimeLimits, RuntimeResource as Q},
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn depth_diagnostics_include_an_entry_that_returns_immediately() {
|
||||
let mut vm = callables::vm(&[("RET", callables::sig(&[], &[]), vec![])]);
|
||||
assert_eq!(callables::run(&mut vm, 100).reason, LogicalFrameEndingReason::EndOfRom);
|
||||
let snapshot = vm.resource_snapshot();
|
||||
assert_eq!(snapshot.call_depth, 0);
|
||||
assert_eq!(snapshot.peak_call_depth, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stationary_workloads_repeat_cycles_and_release_all_session_reservations() {
|
||||
for name in qualification::NAMES {
|
||||
let mut first = qualification::Workload::new(name);
|
||||
let mut second = qualification::Workload::new(name);
|
||||
for _ in 0..32 {
|
||||
first.frame();
|
||||
second.frame();
|
||||
}
|
||||
first.check_stationary();
|
||||
second.check_stationary();
|
||||
assert_eq!(first.digest, second.digest);
|
||||
assert_eq!(first.vm.resource_snapshot().budget, second.vm.resource_snapshot().budget);
|
||||
assert_eq!(first.vm.resource_snapshot().cycles, second.vm.resource_snapshot().cycles);
|
||||
first.finish();
|
||||
second.finish();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_sync_loop_returns_each_slice_without_gc_frames_or_allocation() {
|
||||
let mut vm = callables::vm(&[("JMP 0", callables::sig(&[], &[]), vec![])]);
|
||||
let before = vm.resource_snapshot();
|
||||
for _ in 0..1_000 {
|
||||
let r = callables::run(&mut vm, 100);
|
||||
assert_eq!(r.reason, LogicalFrameEndingReason::BudgetExhausted);
|
||||
assert!(r.steps_executed > 0);
|
||||
assert_eq!(vm.pc(), 0);
|
||||
}
|
||||
let after = vm.resource_snapshot();
|
||||
assert_eq!(after.budget.used, before.budget.used);
|
||||
assert_eq!(after.gc.collections, 0);
|
||||
assert_eq!(after.call_depth, 1);
|
||||
vm.insert_breakpoint(0);
|
||||
assert_eq!(callables::run(&mut vm, 100).reason, LogicalFrameEndingReason::Breakpoint);
|
||||
let observer = vm.resource_observer();
|
||||
drop(vm);
|
||||
assert_eq!(observer.diagnostic_snapshot().used, [0; 8]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn host_string_snapshot_keeps_its_domain_alive_then_final_drop_refunds_it() {
|
||||
let image = callables::image_constants(
|
||||
&[("PUSH_CONST 0\nPUSH_CONST 1\nADD\nRET", callables::sig(&[], &[3]), vec![])],
|
||||
vec![],
|
||||
&[ConstantPoolEntry::String("abc".into()), ConstantPoolEntry::String("def".into())],
|
||||
)
|
||||
.unwrap();
|
||||
let mut vm = VirtualMachine::from_image(image, Default::default());
|
||||
vm.prepare_boot_call().unwrap();
|
||||
assert_eq!(callables::run(&mut vm, 1000).reason, LogicalFrameEndingReason::EndOfRom);
|
||||
let snapshot = vm.operand_stack_top(1);
|
||||
assert_eq!(&*snapshot, &[Value::String("abcdef".into())]);
|
||||
let observer = vm.resource_observer();
|
||||
drop(vm);
|
||||
assert_eq!(observer.used(Q::StringBytes), 6);
|
||||
assert_eq!(observer.used(Q::StringAllocations), 1);
|
||||
drop(snapshot);
|
||||
assert_eq!(observer.diagnostic_snapshot().used, [0; 8]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refused_allocation_keeps_operands_and_charges_attempt_without_rescue_gc() {
|
||||
let image = callables::image(
|
||||
&[
|
||||
("PUSH_I32 73\nMAKE_CLOSURE fn=1,type=4\nPOP\nRET", callables::sig(&[], &[]), vec![]),
|
||||
(
|
||||
"GET_CAPTURE 0\nRET",
|
||||
callables::sig(&[], &[0]),
|
||||
vec![prometeu_bytecode::registry::TypeId(0)],
|
||||
),
|
||||
],
|
||||
vec![],
|
||||
)
|
||||
.unwrap();
|
||||
let mut vm = VirtualMachine::from_image(
|
||||
image,
|
||||
RuntimeLimits::default().lowered(Q::GuestBytes, 0).unwrap(),
|
||||
);
|
||||
vm.prepare_boot_call().unwrap();
|
||||
let before = vm.resource_snapshot();
|
||||
let result = callables::run(&mut vm, 1000);
|
||||
let LogicalFrameEndingReason::Trap(trap) = result.reason else { panic!("quota must trap") };
|
||||
assert_eq!(trap.code, prometeu_bytecode::TRAP_RESOURCE_LIMIT);
|
||||
assert_eq!(&*vm.operand_stack_top(1), &[Value::Int32(73)]);
|
||||
let after = vm.resource_snapshot();
|
||||
assert!(after.cycles > before.cycles);
|
||||
assert_eq!(after.gc.collections, 0);
|
||||
assert_eq!(after.live_objects, 0);
|
||||
assert_eq!(after.budget.used[0], 0);
|
||||
assert!(after.budget.refusals[0] > 0);
|
||||
let observer = vm.resource_observer();
|
||||
drop(vm);
|
||||
assert_eq!(observer.diagnostic_snapshot().used, [0; 8]);
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
use prometeu_bytecode::{
|
||||
Value, assemble, load_limits::LoadBudget, registry::*, registry_codec::PbxModule,
|
||||
};
|
||||
use prometeu_hal::{HostContext, HostReturn, NativeInterface};
|
||||
use prometeu_vm::{LogicalFrameEndingReason, ValidatedProgramImage, VirtualMachine};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub const NAMES: [&str; 3] = ["collections", "closures", "strings_dynamic"];
|
||||
pub struct Native;
|
||||
impl NativeInterface for Native {
|
||||
fn syscall(
|
||||
&mut self,
|
||||
_: u32,
|
||||
_: &[Value],
|
||||
_: &mut HostReturn,
|
||||
_: &mut HostContext,
|
||||
) -> Result<(), prometeu_hal::vm_fault::VmFault> {
|
||||
panic!("workload has no syscalls")
|
||||
}
|
||||
}
|
||||
fn field(id: u32) -> NamedType {
|
||||
NamedType { type_id: TypeId(id), name: None }
|
||||
}
|
||||
fn sig(results: &[u32]) -> Signature {
|
||||
Signature { parameters: vec![], results: results.iter().copied().map(TypeId).collect() }
|
||||
}
|
||||
pub fn image(name: &str) -> Arc<ValidatedProgramImage> {
|
||||
let mut kinds = vec![
|
||||
TypeKind::Builtin(BuiltinId::INT64),
|
||||
TypeKind::Builtin(BuiltinId::INT32),
|
||||
TypeKind::Builtin(BuiltinId::FLOAT),
|
||||
TypeKind::Builtin(BuiltinId::BOOL),
|
||||
TypeKind::Builtin(BuiltinId::STRING),
|
||||
TypeKind::Builtin(BuiltinId::DYNAMIC),
|
||||
TypeKind::Builtin(BuiltinId::VEC2),
|
||||
];
|
||||
kinds.extend([
|
||||
TypeKind::Struct(vec![field(0), field(7), field(4), field(5)]),
|
||||
TypeKind::Array(TypeId(7)),
|
||||
TypeKind::Fn(sig(&[0])),
|
||||
TypeKind::Array(TypeId(4)),
|
||||
]);
|
||||
let (root, body, captures) = match name {
|
||||
"collections" => {
|
||||
// Each iteration publishes one self-cycle plus a rooted containing array.
|
||||
let chunk = "STRUCT_NEW 7\nSET_LOCAL 0\nGET_LOCAL 0\nGET_LOCAL 0\nSTRUCT_SET type=7,field=1\nGET_LOCAL 0\nPUSH_I64 73\nSTRUCT_SET type=7,field=0\nPUSH_I64 2\nARRAY_NEW 8\nSET_LOCAL 1\nGET_LOCAL 1\nPUSH_I64 0\nGET_LOCAL 0\nARRAY_SET 8\nGET_LOCAL 1\nPUSH_I64 0\nARRAY_GET 8\nSTRUCT_GET type=7,field=0\nPOP\n";
|
||||
(
|
||||
format!("{}GET_LOCAL 0\nSTRUCT_GET type=7,field=0\nRET", chunk.repeat(64)),
|
||||
"RET".to_owned(),
|
||||
vec![],
|
||||
)
|
||||
}
|
||||
"closures" => (
|
||||
format!(
|
||||
"{}PUSH_I64 73\nMAKE_CLOSURE fn=1,type=9\nCALL_CLOSURE 9\nRET",
|
||||
"PUSH_I64 73\nMAKE_CLOSURE fn=1,type=9\nPOP\n".repeat(127)
|
||||
),
|
||||
"FRAME_SYNC\nGET_CAPTURE 0\nRET".to_owned(),
|
||||
vec![TypeId(0)],
|
||||
),
|
||||
"strings_dynamic" => {
|
||||
let chunk = "PUSH_CONST 0\nPUSH_CONST 1\nADD\nSET_LOCAL 0\nPUSH_I64 1\nARRAY_NEW 10\nSET_LOCAL 1\nGET_LOCAL 1\nPUSH_I64 0\nGET_LOCAL 0\nARRAY_SET 10\nGET_LOCAL 1\nPUSH_I64 0\nARRAY_GET 10\nDYN_PACK 4\nDYN_UNPACK 4\nPOP\n";
|
||||
(
|
||||
format!("{}GET_LOCAL 0\nPUSH_CONST 2\nEQ\nRET", chunk.repeat(64)),
|
||||
"RET".to_owned(),
|
||||
vec![],
|
||||
)
|
||||
}
|
||||
_ => panic!("unknown workload"),
|
||||
};
|
||||
let mut code = assemble(&root).unwrap();
|
||||
let body = assemble(&body).unwrap();
|
||||
let functions = vec![
|
||||
FunctionDecl {
|
||||
signature: sig(&[if name == "strings_dynamic" { 3 } else { 0 }]),
|
||||
local_slots: 2,
|
||||
max_stack_slots: 32,
|
||||
code_len: code.len() as u32,
|
||||
..Default::default()
|
||||
},
|
||||
FunctionDecl {
|
||||
signature: if name == "closures" { sig(&[0]) } else { sig(&[]) },
|
||||
captures,
|
||||
code_offset: code.len() as u32,
|
||||
code_len: body.len() as u32,
|
||||
max_stack_slots: 4,
|
||||
..Default::default()
|
||||
},
|
||||
];
|
||||
code.extend(body);
|
||||
let mut budget = LoadBudget::new(Default::default());
|
||||
let registry = Registry::resolve(
|
||||
RegistryDeclarations {
|
||||
types: kinds.into_iter().map(|kind| TypeDecl { name: None, kind }).collect(),
|
||||
functions,
|
||||
implementations: vec![],
|
||||
},
|
||||
code.len(),
|
||||
&mut budget,
|
||||
)
|
||||
.unwrap();
|
||||
let constants = vec![
|
||||
prometeu_bytecode::model::ConstantPoolEntry::String("resource".into()),
|
||||
prometeu_bytecode::model::ConstantPoolEntry::String("-73".into()),
|
||||
prometeu_bytecode::model::ConstantPoolEntry::String("resource-73".into()),
|
||||
];
|
||||
let bytes = PbxModule {
|
||||
registry: ®istry,
|
||||
code: &code,
|
||||
constants: &constants,
|
||||
debug: None,
|
||||
exports: &[],
|
||||
syscalls: &[],
|
||||
}
|
||||
.serialize(&mut budget)
|
||||
.unwrap();
|
||||
ValidatedProgramImage::load(
|
||||
&bytes,
|
||||
&mut LoadBudget::new(Default::default()),
|
||||
prometeu_hal::syscalls::caps::NONE,
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub struct Workload {
|
||||
pub name: &'static str,
|
||||
pub vm: VirtualMachine,
|
||||
pub frames: u64,
|
||||
pub digest: u64,
|
||||
prepare: bool,
|
||||
envelope: Option<([u64; 8], usize, usize, usize)>,
|
||||
}
|
||||
impl Workload {
|
||||
pub fn new(name: &'static str) -> Self {
|
||||
Self {
|
||||
name,
|
||||
vm: VirtualMachine::from_image(image(name), Default::default()),
|
||||
frames: 0,
|
||||
digest: 0xcbf29ce484222325,
|
||||
prepare: true,
|
||||
envelope: None,
|
||||
}
|
||||
}
|
||||
pub fn frame(&mut self) {
|
||||
if self.prepare {
|
||||
self.vm.prepare_boot_call().unwrap();
|
||||
}
|
||||
let mut finished = false;
|
||||
for _ in 0..1_000 {
|
||||
let r = self.vm.run_budget(10_000, &mut Native, &mut HostContext::new(None)).unwrap();
|
||||
self.digest = (self.digest ^ r.cycles_used).wrapping_mul(0x100000001b3);
|
||||
match r.reason {
|
||||
LogicalFrameEndingReason::BudgetExhausted => {}
|
||||
LogicalFrameEndingReason::FrameSync => {
|
||||
self.prepare = false;
|
||||
finished = true;
|
||||
break;
|
||||
}
|
||||
LogicalFrameEndingReason::EndOfRom => {
|
||||
let expected = if self.name == "strings_dynamic" {
|
||||
Value::Boolean(true)
|
||||
} else {
|
||||
Value::Int64(73)
|
||||
};
|
||||
assert_eq!(&*self.vm.operand_stack_top(1), &[expected]);
|
||||
self.prepare = true;
|
||||
finished = true;
|
||||
break;
|
||||
}
|
||||
reason => panic!("{} unexpected {reason:?}", self.name),
|
||||
}
|
||||
}
|
||||
assert!(finished, "bounded workload did not complete");
|
||||
self.frames += 1;
|
||||
self.digest = (self.digest ^ self.vm.pc() as u64 ^ self.frames).wrapping_mul(0x100000001b3);
|
||||
}
|
||||
pub fn check_stationary(&mut self) {
|
||||
let s = self.vm.resource_snapshot();
|
||||
assert_eq!(s.budget.used[3], 0, "temporary reservations must be refunded");
|
||||
assert_eq!(s.retired_handles, 0);
|
||||
assert_eq!(s.call_depth, 0, "checkpoint at root completion");
|
||||
// Default threshold 1024; fixtures allocate a divisor of 1024 per entry.
|
||||
// The closure is rooted at FRAME_SYNC collection, then dies at RET.
|
||||
// RET has insufficient new allocations to trigger another collection;
|
||||
// one unrooted object legitimately waits for the next threshold sweep.
|
||||
assert_eq!(s.live_objects, usize::from(self.name == "closures"));
|
||||
assert_eq!(s.tracked_strings, 0);
|
||||
let now = (s.budget.used, s.live_objects, s.free_handles, s.tracked_strings);
|
||||
if let Some(expected) = &self.envelope {
|
||||
assert_eq!(&now, expected, "{} stationary envelope", self.name);
|
||||
} else {
|
||||
self.envelope = Some(now);
|
||||
}
|
||||
}
|
||||
pub fn finish(self) {
|
||||
let observer = self.vm.resource_observer();
|
||||
drop(self);
|
||||
assert_eq!(
|
||||
observer.diagnostic_snapshot().used,
|
||||
[0; 8],
|
||||
"last session owner must release every reservation"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,25 @@
|
||||
use prometeu_hal::vm_fault::VmFault;
|
||||
use prometeu_vm::VirtualMachine;
|
||||
use prometeu_vm::{HostContext, HostReturn, NativeInterface, SyscallId};
|
||||
#[allow(dead_code)]
|
||||
#[path = "../../../console/prometeu-vm/tests/support/callables.rs"]
|
||||
mod callables;
|
||||
use prometeu_bytecode::{registry::TypeId, Value};
|
||||
use prometeu_vm::LogicalFrameEndingReason;
|
||||
|
||||
#[test]
|
||||
fn gc_collects_unreachable_closure_but_keeps_marked() {
|
||||
// Black-box check: a fresh VM with no program initializes and can run a zero-budget slice.
|
||||
// The GC behavior is covered by unit tests inside the VM crate; this layer test remains
|
||||
// as a smoke-test for public API stability.
|
||||
struct NoopNative;
|
||||
impl NativeInterface for NoopNative {
|
||||
fn syscall(
|
||||
&mut self,
|
||||
_id: SyscallId,
|
||||
_args: &[prometeu_bytecode::Value],
|
||||
_ret: &mut HostReturn,
|
||||
_ctx: &mut HostContext,
|
||||
) -> Result<(), VmFault> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
let mut vm = VirtualMachine::default();
|
||||
let mut native = NoopNative;
|
||||
let mut ctx = HostContext::new(None);
|
||||
let res = vm.run_budget(0, &mut native, &mut ctx);
|
||||
assert!(res.is_ok());
|
||||
fn gc_collects_unreachable_closures_and_retains_active_environment_through_sync() {
|
||||
let body =
|
||||
format!("{}FRAME_SYNC\nGET_CAPTURE 0\nRET", "MAKE_CLOSURE fn=2,type=4\nPOP\n".repeat(1030));
|
||||
let mut vm = callables::vm(&[
|
||||
(
|
||||
"PUSH_I32 73\nMAKE_CLOSURE fn=1,type=4\nCALL_CLOSURE 4\nRET",
|
||||
callables::sig(&[], &[0]),
|
||||
vec![],
|
||||
),
|
||||
(&body, callables::sig(&[], &[0]), vec![TypeId(0)]),
|
||||
("PUSH_I32 1\nRET", callables::sig(&[], &[0]), vec![]),
|
||||
]);
|
||||
assert_eq!(callables::run(&mut vm, 100_000).reason, LogicalFrameEndingReason::FrameSync);
|
||||
assert_eq!(vm.heap().allocation_count(), 1031);
|
||||
assert_eq!(vm.heap().len(), 1, "only the active closure remains reachable");
|
||||
assert_eq!(callables::run(&mut vm, 100).reason, LogicalFrameEndingReason::EndOfRom);
|
||||
assert_eq!(&*vm.operand_stack_top(1), &[Value::Int32(73)]);
|
||||
}
|
||||
|
||||
@@ -523,6 +523,64 @@ mod tests {
|
||||
use std::io::{Read, Write};
|
||||
use std::net::TcpStream;
|
||||
|
||||
#[test]
|
||||
fn desktop_worker_survives_repeated_home_resume_and_cold_reset() {
|
||||
use prometeu_firmware::firmware::firmware_state::ResetStep;
|
||||
use prometeu_hal::InputSignals;
|
||||
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("../../../test-cartridges/dummy-boy");
|
||||
let mut runner = HostRunner::new(None, None);
|
||||
runner.set_boot_target(BootTarget::Cartridge {
|
||||
path: path.to_string_lossy().into_owned(),
|
||||
debug: false,
|
||||
debug_port: 0,
|
||||
});
|
||||
let signals = InputSignals::default();
|
||||
for cycle in 0..32 {
|
||||
runner.firmware.change_state(
|
||||
FirmwareState::Reset(ResetStep),
|
||||
&signals,
|
||||
&mut runner.hardware,
|
||||
);
|
||||
assert_eq!(runner.firmware.os.sessions().vm_session_count(), 0);
|
||||
for _ in 0..6000 {
|
||||
runner.firmware.tick(&signals, &mut runner.hardware);
|
||||
if matches!(runner.firmware.state, FirmwareState::GameRunning(_)) {
|
||||
break;
|
||||
}
|
||||
assert!(!matches!(runner.firmware.state, FirmwareState::AppCrashes(_)));
|
||||
std::thread::sleep(Duration::from_millis(1));
|
||||
}
|
||||
assert!(matches!(runner.firmware.state, FirmwareState::GameRunning(_)), "boot {cycle}");
|
||||
for _ in 0..8 {
|
||||
runner.firmware.tick(&signals, &mut runner.hardware);
|
||||
}
|
||||
runner.firmware.request_home_from_host();
|
||||
for _ in 0..32 {
|
||||
runner.firmware.tick(&signals, &mut runner.hardware);
|
||||
if matches!(runner.firmware.state, FirmwareState::HubHome(_)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert!(matches!(runner.firmware.state, FirmwareState::HubHome(_)), "home {cycle}");
|
||||
assert!(runner.firmware.resume_resident_game_from_home());
|
||||
for _ in 0..8 {
|
||||
runner.firmware.tick(&signals, &mut runner.hardware);
|
||||
}
|
||||
assert!(
|
||||
matches!(runner.firmware.state, FirmwareState::GameRunning(_)),
|
||||
"resume {cycle}"
|
||||
);
|
||||
assert_eq!(runner.firmware.os.sessions().vm_session_count(), 1);
|
||||
}
|
||||
runner.firmware.change_state(
|
||||
FirmwareState::Reset(ResetStep),
|
||||
&signals,
|
||||
&mut runner.hardware,
|
||||
);
|
||||
assert_eq!(runner.firmware.os.sessions().vm_session_count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn presentation_state_requires_initial_redraw_only_once() {
|
||||
let mut state = PresentationState::default();
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
"""Exercise the real Dummy Boy desktop window on localhost:17959.
|
||||
|
||||
Start the host with --debug test-cartridges/dummy-boy --port 17959 first.
|
||||
This finite smoke observes progress; its timeout is not a realtime guarantee.
|
||||
"""
|
||||
import json
|
||||
import socket
|
||||
import time
|
||||
|
||||
|
||||
def run():
|
||||
with socket.create_connection(("127.0.0.1", 17959), timeout=10) as connection:
|
||||
connection.settimeout(10)
|
||||
with connection.makefile("rb") as incoming:
|
||||
def send(kind):
|
||||
connection.sendall((json.dumps({"type": kind}) + "\n").encode())
|
||||
|
||||
def state():
|
||||
send("getState")
|
||||
deadline = time.monotonic() + 10
|
||||
while time.monotonic() < deadline:
|
||||
line = incoming.readline()
|
||||
assert line, "Desktop disconnected"
|
||||
message = json.loads(line)
|
||||
assert message.get("event") != "fault", message
|
||||
if message.get("type") == "getState":
|
||||
return message
|
||||
raise TimeoutError("Desktop did not answer getState")
|
||||
|
||||
def await_progress(previous):
|
||||
deadline = time.monotonic() + 5
|
||||
current = state()
|
||||
while current["frame_index"] == previous and time.monotonic() < deadline:
|
||||
time.sleep(0.02)
|
||||
current = state()
|
||||
return current
|
||||
|
||||
handshake = json.loads(incoming.readline())
|
||||
assert handshake["type"] == "handshake", handshake
|
||||
assert handshake["cartridge"]["app_id"] == 2, handshake
|
||||
print(json.dumps(handshake), flush=True)
|
||||
send("start")
|
||||
time.sleep(0.5)
|
||||
first = state()
|
||||
assert first["app_id"] == 2, first
|
||||
for _ in range(32):
|
||||
send("pause")
|
||||
time.sleep(0.025)
|
||||
paused = state()
|
||||
time.sleep(0.025)
|
||||
still = state()
|
||||
assert (paused["pc"], paused["frame_index"]) == (still["pc"], still["frame_index"])
|
||||
send("stepFrame")
|
||||
stepped = await_progress(paused["frame_index"])
|
||||
assert stepped["frame_index"] == paused["frame_index"] + 1, (paused, stepped)
|
||||
send("resume")
|
||||
resumed = await_progress(stepped["frame_index"])
|
||||
assert resumed["frame_index"] > stepped["frame_index"], (stepped, resumed)
|
||||
print(json.dumps({
|
||||
"result": "pass", "cycles": 32,
|
||||
"first_frame": first["frame_index"], "last_frame": resumed["frame_index"],
|
||||
"last_pc": resumed["pc"],
|
||||
}), flush=True)
|
||||
send("resume")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run()
|
||||
@@ -6,6 +6,9 @@ license.workspace = true
|
||||
repository.workspace = true
|
||||
description = "In-process Prometeu host that boots and steps cartridges without a window, display, or audio device."
|
||||
|
||||
[features]
|
||||
resource-diagnostics = ["prometeu-vm/resource-diagnostics"]
|
||||
|
||||
[dependencies]
|
||||
prometeu-bytecode = { path = "../../console/prometeu-bytecode" }
|
||||
prometeu-drivers = { path = "../../console/prometeu-drivers" }
|
||||
|
||||
@@ -451,6 +451,10 @@ impl Default for HeadlessHost {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(test, feature = "resource-diagnostics"))]
|
||||
#[path = "resource_qualification_tests.rs"]
|
||||
mod resource_qualification_tests;
|
||||
|
||||
impl RenderSubmissionSink for HeadlessPlatform {
|
||||
fn submit_render_submission(
|
||||
&mut self,
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
#[allow(dead_code)]
|
||||
#[path = "../../../console/prometeu-vm/tests/support/callables.rs"]
|
||||
mod callables;
|
||||
use super::{AdvanceFrameOutcome, HeadlessHost};
|
||||
use prometeu_bytecode::{Value, registry::TypeId};
|
||||
|
||||
#[test]
|
||||
fn thousand_reset_reload_cycles_release_session_owners_and_preserve_controls() {
|
||||
let body =
|
||||
format!("{}FRAME_SYNC\nGET_CAPTURE 0\nRET", "MAKE_CLOSURE fn=2,type=4\nPOP\n".repeat(1030));
|
||||
let bytes = callables::bytes(
|
||||
&[
|
||||
(
|
||||
"PUSH_I32 73\nMAKE_CLOSURE fn=1,type=4\nCALL_CLOSURE 4\nRET",
|
||||
callables::sig(&[], &[0]),
|
||||
vec![],
|
||||
),
|
||||
(&body, callables::sig(&[], &[0]), vec![TypeId(0)]),
|
||||
("PUSH_I32 1\nRET", callables::sig(&[], &[0]), vec![]),
|
||||
],
|
||||
vec![],
|
||||
&[],
|
||||
)
|
||||
.unwrap();
|
||||
let path = std::env::temp_dir().join(format!("prometeu-resource-reset-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&path).unwrap();
|
||||
std::fs::write(path.join("program.pbx"), bytes).unwrap();
|
||||
std::fs::write(path.join("manifest.json"),r#"{"magic":"PMTU","cartridge_version":1,"app_id":77,"title":"Resource reset qualification","app_version":"1.0.0","app_mode":"Game","capabilities":[]}"#).unwrap();
|
||||
let mut host = HeadlessHost::new();
|
||||
for cycle in 0..1000 {
|
||||
host.boot(path.to_string_lossy()).unwrap();
|
||||
let observer = host
|
||||
.firmware
|
||||
.os
|
||||
.sessions()
|
||||
.resident_game_session_for_app(77)
|
||||
.unwrap()
|
||||
.vm
|
||||
.resource_observer();
|
||||
assert!(
|
||||
matches!(
|
||||
host.advance_frame().unwrap(),
|
||||
AdvanceFrameOutcome::FrameCompleted { logical_frame_index: 1, .. }
|
||||
),
|
||||
"cycle {cycle}"
|
||||
);
|
||||
{
|
||||
let sessions = host.firmware.os.sessions();
|
||||
let vm = &sessions.resident_game_session_for_app(77).unwrap().vm;
|
||||
assert_eq!(vm.heap().len(), 1);
|
||||
assert_eq!(vm.resource_snapshot().gc.collections, 1);
|
||||
}
|
||||
let pc = host.get_state().pc;
|
||||
host.pause().unwrap();
|
||||
host.tick_once();
|
||||
assert_eq!(host.get_state().pc, pc);
|
||||
host.resume().unwrap();
|
||||
assert!(matches!(
|
||||
host.advance_frame().unwrap(),
|
||||
AdvanceFrameOutcome::FrameCompleted { logical_frame_index: 2, .. }
|
||||
));
|
||||
assert_eq!(&*host.get_state().stack_top, &[Value::Int32(73)]);
|
||||
assert!(host.get_fault().is_none());
|
||||
host.reset();
|
||||
assert_eq!(host.firmware.os.sessions().vm_session_count(), 0);
|
||||
assert_eq!(
|
||||
observer.diagnostic_snapshot().used,
|
||||
[0; 8],
|
||||
"reset {cycle} retained session allocation"
|
||||
);
|
||||
}
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
}
|
||||
Reference in New Issue
Block a user