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:
@@ -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"
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user