implement PLN-0034 internal allocation evidence

This commit is contained in:
2026-04-20 08:50:01 +01:00
parent 09821cf9cc
commit 811af09ea7
13 changed files with 125 additions and 8 deletions
@@ -7,6 +7,7 @@ mod tick;
use crate::CrashReport;
use crate::fs::{FsState, VirtualFS};
use crate::services::memcard::MemcardService;
use prometeu_bytecode::string_materialization_count;
use prometeu_hal::cartridge::AppMode;
use prometeu_hal::log::LogService;
use prometeu_hal::telemetry::{AtomicTelemetry, CertificationConfig, Certifier};
@@ -38,6 +39,8 @@ pub struct VirtualMachineRuntime {
pub paused: bool,
pub debug_step_request: bool,
pub inspection_active: bool,
pub(crate) frame_start_heap_allocations: u64,
pub(crate) frame_start_string_materializations: u64,
pub(crate) needs_prepare_entry_call: bool,
pub(crate) boot_time: Instant,
}
@@ -32,6 +32,8 @@ impl VirtualMachineRuntime {
paused: false,
debug_step_request: false,
inspection_active: false,
frame_start_heap_allocations: 0,
frame_start_string_materializations: 0,
needs_prepare_entry_call: false,
boot_time,
};
@@ -106,6 +108,8 @@ impl VirtualMachineRuntime {
self.paused = false;
self.debug_step_request = false;
self.inspection_active = false;
self.frame_start_heap_allocations = 0;
self.frame_start_string_materializations = 0;
self.needs_prepare_entry_call = false;
}
@@ -493,6 +493,8 @@ fn reset_clears_cartridge_scoped_runtime_state() {
Some(CrashReport::VmPanic { message: "stale".into(), pc: Some(55) });
runtime.paused = true;
runtime.debug_step_request = true;
runtime.frame_start_heap_allocations = 11;
runtime.frame_start_string_materializations = 22;
runtime.needs_prepare_entry_call = true;
runtime.reset(&mut vm);
@@ -518,10 +520,56 @@ fn reset_clears_cartridge_scoped_runtime_state() {
assert!(runtime.last_crash_report.is_none());
assert!(!runtime.paused);
assert!(!runtime.debug_step_request);
assert_eq!(runtime.frame_start_heap_allocations, 0);
assert_eq!(runtime.frame_start_string_materializations, 0);
assert!(!runtime.needs_prepare_entry_call);
assert_eq!(vm.pc(), 0);
}
#[test]
fn tick_numeric_happy_path_records_zero_internal_allocations() {
let mut runtime = VirtualMachineRuntime::new(None);
let mut vm = VirtualMachine::default();
let mut hardware = Hardware::new();
let signals = InputSignals::default();
let code =
assemble("PUSH_I32 1\nPUSH_I32 2\nADD\nPOP_N 1\nFRAME_SYNC\nHALT").expect("assemble");
let program = serialized_single_function_module(code, vec![]);
let cartridge = cartridge_with_program(program, caps::NONE);
runtime.initialize_vm(&mut vm, &cartridge).expect("runtime must initialize");
let report = runtime.tick(&mut vm, &signals, &mut hardware);
assert!(report.is_none());
let snapshot = runtime.atomic_telemetry.snapshot();
assert_eq!(snapshot.vm_heap_allocations, 0);
assert_eq!(snapshot.vm_string_materializations, 0);
}
#[test]
fn tick_already_materialized_string_path_records_zero_internal_allocations() {
let mut runtime = VirtualMachineRuntime::new(None);
let mut vm = VirtualMachine::default();
let mut hardware = Hardware::new();
let signals = InputSignals::default();
let code = assemble("PUSH_CONST 0\nSET_GLOBAL 0\nGET_GLOBAL 0\nPOP_N 1\nFRAME_SYNC\nHALT")
.expect("assemble");
let program = serialized_single_function_module_with_consts(
code,
vec![ConstantPoolEntry::String("steady".into())],
vec![],
);
let cartridge = cartridge_with_program(program, caps::NONE);
runtime.initialize_vm(&mut vm, &cartridge).expect("runtime must initialize");
let report = runtime.tick(&mut vm, &signals, &mut hardware);
assert!(report.is_none());
let snapshot = runtime.atomic_telemetry.snapshot();
assert_eq!(snapshot.vm_heap_allocations, 0);
assert_eq!(snapshot.vm_string_materializations, 0);
}
#[test]
fn initialize_vm_failure_clears_previous_identity_and_handles() {
let mut runtime = VirtualMachineRuntime::new(None);
@@ -7,6 +7,18 @@ use prometeu_vm::LogicalFrameEndingReason;
use std::sync::atomic::Ordering;
impl VirtualMachineRuntime {
fn refresh_internal_allocation_telemetry(&self, vm: &VirtualMachine) {
let heap_allocations =
vm.heap().allocation_count().saturating_sub(self.frame_start_heap_allocations);
let string_materializations =
string_materialization_count().saturating_sub(self.frame_start_string_materializations);
self.atomic_telemetry.vm_heap_allocations.store(heap_allocations, Ordering::Relaxed);
self.atomic_telemetry
.vm_string_materializations
.store(string_materializations, Ordering::Relaxed);
}
fn bank_telemetry_summary(
hw: &dyn HardwareBridge,
) -> (BankTelemetry, BankTelemetry, BankTelemetry) {
@@ -94,6 +106,10 @@ impl VirtualMachineRuntime {
self.atomic_telemetry.cycles_used.store(0, Ordering::Relaxed);
self.atomic_telemetry.syscalls.store(0, Ordering::Relaxed);
self.atomic_telemetry.vm_steps.store(0, Ordering::Relaxed);
self.atomic_telemetry.vm_heap_allocations.store(0, Ordering::Relaxed);
self.atomic_telemetry.vm_string_materializations.store(0, Ordering::Relaxed);
self.frame_start_heap_allocations = vm.heap().allocation_count();
self.frame_start_string_materializations = string_materialization_count();
}
let budget = std::cmp::min(Self::SLICE_PER_TICK, self.logical_frame_remaining_cycles);
@@ -177,6 +193,7 @@ impl VirtualMachineRuntime {
self.atomic_telemetry
.heap_used_bytes
.store(vm.heap().used_bytes.load(Ordering::Relaxed), Ordering::Relaxed);
self.refresh_internal_allocation_telemetry(vm);
self.atomic_telemetry
.host_cpu_time_us
.store(start.elapsed().as_micros() as u64, Ordering::Relaxed);
@@ -253,6 +270,7 @@ impl VirtualMachineRuntime {
self.atomic_telemetry
.heap_used_bytes
.store(vm.heap().used_bytes.load(Ordering::Relaxed), Ordering::Relaxed);
self.refresh_internal_allocation_telemetry(vm);
self.atomic_telemetry.frame_index.store(self.logical_frame_index, Ordering::Relaxed);
self.atomic_telemetry