//! Count only this thread's warmed instruction region, including reallocations. use prometeu_bytecode::{assemble, load_limits::LoadBudget, registry::*}; use prometeu_hal::{HostContext, syscalls::caps}; use prometeu_test_support::program::{TestProgram, int64_signature}; use prometeu_vm::{ValidatedProgramImage, VirtualMachine}; use std::alloc::{GlobalAlloc, Layout, System}; use std::cell::Cell; struct Native; impl prometeu_hal::NativeInterface for Native { fn syscall( &mut self, _: u32, _: &[prometeu_bytecode::Value], _: &mut prometeu_hal::HostReturn, _: &mut HostContext, ) -> Result<(), prometeu_hal::vm_fault::VmFault> { panic!("no syscalls in allocation fixtures") } } thread_local! { static COUNTS: Cell> = const { Cell::new(None) }; } struct Counter; fn count(alloc: usize, free: usize) { let _ = COUNTS.try_with(|c| { if let Some((a, d)) = c.get() { c.set(Some((a + alloc, d + free))); } }); } unsafe impl GlobalAlloc for Counter { unsafe fn alloc(&self, l: Layout) -> *mut u8 { count(1, 0); unsafe { System.alloc(l) } } unsafe fn alloc_zeroed(&self, l: Layout) -> *mut u8 { count(1, 0); unsafe { System.alloc_zeroed(l) } } unsafe fn dealloc(&self, p: *mut u8, l: Layout) { count(0, 1); unsafe { System.dealloc(p, l) } } unsafe fn realloc(&self, p: *mut u8, l: Layout, n: usize) -> *mut u8 { count(1, 1); unsafe { System.realloc(p, l, n) } } } #[global_allocator] static ALLOCATOR: Counter = Counter; #[test] fn warmed_instruction_regions_do_not_allocate() { let mut observations = vec![]; for (name, caller, callee, results, steps) in [ ("scalar", "CALL 1\nPOP\nJMP 0", "PUSH_I64 42\nRET", 1, 5), ("void", "CALL 1\nJMP 0", "RET", 0, 3), ("multi", "CALL 1\nPOP\nPOP\nJMP 0", "PUSH_I64 11\nPUSH_I64 22\nRET", 2, 7), ("arithmetic_peek", "PUSH_I64 3\nDUP\nADD\nPOP\nJMP 0", "RET", 0, 5), ] { let mut code = assemble(caller).unwrap(); let offset = code.len(); let body = assemble(callee).unwrap(); let mut program = TestProgram::single(code.clone(), int64_signature(0, 0), 4, 8); program.functions.push(FunctionDecl { signature: int64_signature(0, results), code_offset: offset as u32, code_len: body.len() as u32, local_slots: 4, max_stack_slots: 8, ..Default::default() }); code.extend(body); program.code = code; let image = ValidatedProgramImage::load( &program.serialize(), &mut LoadBudget::new(Default::default()), caps::NONE, ) .unwrap(); let mut vm = VirtualMachine::from_image(image, Default::default()); vm.prepare_boot_call().unwrap(); let mut host = HostContext::new(None); for _ in 0..steps * 10 { vm.step(&mut Native, &mut host).unwrap(); } COUNTS.with(|c| c.set(Some((0, 0)))); for _ in 0..steps * 1000 { vm.step(&mut Native, &mut host).unwrap(); } let counts = COUNTS.with(|c| c.replace(None).unwrap()); println!("{name}: 1000 iterations, {counts:?} allocations/deallocations"); observations.push((name, counts)); } for (name, counts) in observations { assert_eq!(counts, (0, 0), "{name}"); } } #[cfg(feature = "resource-diagnostics")] #[allow(dead_code)] #[path = "support/callables.rs"] mod provenance_callables; #[cfg(feature = "resource-diagnostics")] #[test] fn provenance_queries_allocate_nothing_at_all_qualification_sizes() { use prometeu_bytecode::model::ConstantPoolEntry; use prometeu_vm::string_provenance_qualification::Fixture; for count in [1024_usize, 16384, 65536] { let constants = vec![ConstantPoolEntry::String("same".into()); count]; let image = provenance_callables::image_constants( &[("RET", provenance_callables::sig(&[], &[]), vec![])], vec![], &constants, ) .unwrap(); let mut fixture = Fixture::new(image, count); let work = fixture.work(); let bound = count.ilog2() as usize + 1; assert!(work[0] <= bound && work[1] <= bound); for workload in ["image_hit", "image_miss", "session_hit", "session_miss", "import_hit"] { COUNTS.with(|c| c.set(Some((0, 0)))); for _ in 0..100 { fixture.run(workload); } let counts = COUNTS.with(|c| c.replace(None)).unwrap(); assert_eq!(counts, (0, 0), "{workload}, owners={count}"); } COUNTS.with(|c| c.set(Some((0, 0)))); fixture.run("snapshot_mixed"); let counts = COUNTS.with(|c| c.replace(None)).unwrap(); assert_eq!(counts, (1, 1), "only snapshot Value storage allocates"); } } #[path = "support/identity.rs"] mod identity_support; #[test] fn identity_and_borrowed_hash_instruction_regions_do_not_allocate() { use prometeu_bytecode::model::ConstantPoolEntry as K; for (name, initialize, operand, operation, steps, closure) in [ ("hash_i64", "PUSH_I64 -73\nSET_LOCAL 0", "GET_LOCAL 0", "INTRINSIC 0x4100", 4, false), ("hash_float", "PUSH_CONST 0\nSET_LOCAL 0", "GET_LOCAL 0", "INTRINSIC 0x4101", 4, false), ("hash_string", "PUSH_CONST 1\nSET_LOCAL 0", "GET_LOCAL 0", "INTRINSIC 0x4102", 4, false), ( "struct_hash", "STRUCT_NEW 5\nSET_LOCAL 0", "GET_LOCAL 0", "REF_IDENTITY_HASH 5", 4, false, ), ( "array_hash", "PUSH_I64 0\nARRAY_NEW 6\nSET_LOCAL 0", "GET_LOCAL 0", "REF_IDENTITY_HASH 6", 4, false, ), ( "fn_hash", "MAKE_CLOSURE fn=1,type=9\nSET_LOCAL 0", "GET_LOCAL 0", "REF_IDENTITY_HASH 9", 4, true, ), ( "random_hash", "PUSH_I64 1\nINTRINSIC 0x4000\nSET_LOCAL 0", "GET_LOCAL 0", "REF_IDENTITY_HASH 10", 4, false, ), ( "trait_hash", "STRUCT_NEW 5\nREF_TO_TRAIT source=5,target=7\nSET_LOCAL 1\nSET_LOCAL 0", "GET_LOCAL 0\nGET_LOCAL 1", "REF_IDENTITY_HASH 7", 5, false, ), ( "struct_eq", "STRUCT_NEW 5\nSET_LOCAL 0", "GET_LOCAL 0\nGET_LOCAL 0", "REF_IDENTITY_EQ left=5,right=5", 5, false, ), ( "trait_eq", "STRUCT_NEW 5\nREF_TO_TRAIT source=5,target=7\nSET_LOCAL 1\nSET_LOCAL 0", "GET_LOCAL 0\nGET_LOCAL 1\nGET_LOCAL 0\nGET_LOCAL 1", "REF_IDENTITY_EQ left=7,right=7", 7, false, ), ] { let start = assemble(initialize).unwrap().len(); let text = format!("{initialize}\n{operand}\n{operation}\nPOP\nJMP {start}"); let mut bodies = vec![(text.as_str(), identity_support::sig(&[], &[]))]; if closure { bodies.push(("RET", identity_support::sig(&[], &[]))); } let mut vm = identity_support::vm(&bodies, &[K::Float64(-0.0), K::String("π\0".repeat(512))]); let mut host = HostContext::new(None); while vm.pc() != start { vm.step(&mut identity_support::Native, &mut host).unwrap(); } for _ in 0..steps * 10 { vm.step(&mut identity_support::Native, &mut host).unwrap(); } let heap_allocations = vm.heap().allocation_count(); COUNTS.with(|c| c.set(Some((0, 0)))); for _ in 0..steps * 1000 { vm.step(&mut identity_support::Native, &mut host).unwrap(); } let counts = COUNTS.with(|c| c.replace(None).unwrap()); assert_eq!(counts, (0, 0), "{name}: allocations and deallocations"); assert_eq!(vm.heap().allocation_count(), heap_allocations, "{name}: guest allocations"); } }