pr3.7
This commit is contained in:
@@ -92,6 +92,11 @@ pub struct VirtualMachine {
|
||||
pub halted: bool,
|
||||
/// Set of ROM addresses used for software breakpoints in the debugger.
|
||||
pub breakpoints: std::collections::HashSet<usize>,
|
||||
/// GC: number of newly allocated live objects threshold to trigger a collection at safepoint.
|
||||
/// The GC only runs at safepoints (e.g., FRAME_SYNC). 0 disables automatic GC.
|
||||
pub gc_alloc_threshold: usize,
|
||||
/// GC: snapshot of live objects count after the last collection (or VM init).
|
||||
last_gc_live_count: usize,
|
||||
}
|
||||
|
||||
|
||||
@@ -120,6 +125,8 @@ impl VirtualMachine {
|
||||
cycles: 0,
|
||||
halted: false,
|
||||
breakpoints: std::collections::HashSet::new(),
|
||||
gc_alloc_threshold: 1024, // conservative default; tests may override
|
||||
last_gc_live_count: 0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,6 +147,7 @@ impl VirtualMachine {
|
||||
self.heap = Heap::new();
|
||||
self.cycles = 0;
|
||||
self.halted = true; // execution is impossible until a successful load
|
||||
self.last_gc_live_count = 0;
|
||||
|
||||
// Only recognized format is loadable: PBS v0 industrial format
|
||||
let program = if program_bytes.starts_with(b"PBS\0") {
|
||||
@@ -967,6 +975,30 @@ impl VirtualMachine {
|
||||
OpCode::FrameSync => {
|
||||
// Marks the logical end of a frame: consume cycles and signal to the driver
|
||||
self.cycles += OpCode::FrameSync.cycles();
|
||||
|
||||
// GC Safepoint: only at FRAME_SYNC
|
||||
if self.gc_alloc_threshold > 0 {
|
||||
let live_now = self.heap.len();
|
||||
let since_last = live_now.saturating_sub(self.last_gc_live_count);
|
||||
if since_last >= self.gc_alloc_threshold {
|
||||
// Collect GC roots from VM state
|
||||
struct CollectRoots(Vec<prometeu_bytecode::HeapRef>);
|
||||
impl crate::roots::RootVisitor for CollectRoots {
|
||||
fn visit_heap_ref(&mut self, r: prometeu_bytecode::HeapRef) {
|
||||
self.0.push(r);
|
||||
}
|
||||
}
|
||||
let mut collector = CollectRoots(Vec::new());
|
||||
self.visit_roots(&mut collector);
|
||||
|
||||
// Run mark-sweep
|
||||
self.heap.mark_from_roots(collector.0);
|
||||
self.heap.sweep();
|
||||
// Update baseline for next cycles
|
||||
self.last_gc_live_count = self.heap.len();
|
||||
}
|
||||
}
|
||||
|
||||
return Err(LogicalFrameEndingReason::FrameSync);
|
||||
}
|
||||
}
|
||||
@@ -2461,4 +2493,82 @@ mod tests {
|
||||
_ => panic!("Expected Trap, got {:?}", report.reason),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gc_triggers_only_at_frame_sync() {
|
||||
use crate::object::ObjectKind;
|
||||
|
||||
// ROM: NOP; FRAME_SYNC; HALT
|
||||
let mut rom = Vec::new();
|
||||
rom.extend_from_slice(&(OpCode::Nop as u16).to_le_bytes());
|
||||
rom.extend_from_slice(&(OpCode::FrameSync as u16).to_le_bytes());
|
||||
rom.extend_from_slice(&(OpCode::Halt as u16).to_le_bytes());
|
||||
|
||||
let mut vm = new_test_vm(rom.clone(), vec![]);
|
||||
vm.program.functions = std::sync::Arc::from(vec![prometeu_bytecode::FunctionMeta {
|
||||
code_offset: 0,
|
||||
code_len: rom.len() as u32,
|
||||
..Default::default()
|
||||
}]);
|
||||
|
||||
// Set a very low threshold to trigger GC as soon as we hit FRAME_SYNC
|
||||
vm.gc_alloc_threshold = 1;
|
||||
|
||||
// Allocate an unreachable object (no roots referencing it)
|
||||
let _orphan = vm.heap.allocate_object(ObjectKind::Bytes, &[1, 2, 3]);
|
||||
assert_eq!(vm.heap.len(), 1);
|
||||
|
||||
let mut native = MockNative;
|
||||
let mut ctx = HostContext::new(None);
|
||||
|
||||
// Step 1: NOP — should not run GC
|
||||
vm.step(&mut native, &mut ctx).unwrap();
|
||||
assert_eq!(vm.heap.len(), 1, "GC must not run except at safepoints");
|
||||
|
||||
// Step 2: FRAME_SYNC — GC should run and reclaim the unreachable object
|
||||
match vm.step(&mut native, &mut ctx) {
|
||||
Err(LogicalFrameEndingReason::FrameSync) => {}
|
||||
other => panic!("Expected FrameSync, got {:?}", other),
|
||||
}
|
||||
assert_eq!(vm.heap.len(), 0, "Unreachable object must be reclaimed at FRAME_SYNC");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gc_keeps_roots_and_collects_unreachable_at_frame_sync() {
|
||||
use crate::object::ObjectKind;
|
||||
|
||||
// ROM: FRAME_SYNC; HALT
|
||||
let mut rom = Vec::new();
|
||||
rom.extend_from_slice(&(OpCode::FrameSync as u16).to_le_bytes());
|
||||
rom.extend_from_slice(&(OpCode::Halt as u16).to_le_bytes());
|
||||
|
||||
let mut vm = new_test_vm(rom.clone(), vec![]);
|
||||
vm.program.functions = std::sync::Arc::from(vec![prometeu_bytecode::FunctionMeta {
|
||||
code_offset: 0,
|
||||
code_len: rom.len() as u32,
|
||||
..Default::default()
|
||||
}]);
|
||||
|
||||
vm.gc_alloc_threshold = 1;
|
||||
|
||||
// Allocate two objects; make one a root by placing it on the operand stack
|
||||
let rooted = vm.heap.allocate_object(ObjectKind::Bytes, &[9, 9]);
|
||||
let unreachable = vm.heap.allocate_object(ObjectKind::Bytes, &[8, 8, 8]);
|
||||
assert_eq!(vm.heap.len(), 2);
|
||||
vm.operand_stack.push(Value::HeapRef(rooted));
|
||||
|
||||
let mut native = MockNative;
|
||||
let mut ctx = HostContext::new(None);
|
||||
|
||||
// Execute FRAME_SYNC: should trigger GC
|
||||
match vm.step(&mut native, &mut ctx) {
|
||||
Err(LogicalFrameEndingReason::FrameSync) => {}
|
||||
other => panic!("Expected FrameSync, got {:?}", other),
|
||||
}
|
||||
|
||||
// Rooted must survive; unreachable must be collected
|
||||
assert_eq!(vm.heap.len(), 1);
|
||||
assert!(vm.heap.is_valid(rooted));
|
||||
assert!(!vm.heap.is_valid(unreachable));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user