perf(registry): record bounded layout load and GC evidence for PLN-0176 step 35
This commit is contained in:
@@ -270,10 +270,11 @@ impl LoadLimits {
|
||||
pub struct LoadBudget {
|
||||
limits: LoadLimits,
|
||||
used: [u64; RESOURCE_COUNT],
|
||||
peak: [u64; RESOURCE_COUNT],
|
||||
}
|
||||
impl LoadBudget {
|
||||
pub fn new(limits: LoadLimits) -> Self {
|
||||
Self { limits, used: [0; RESOURCE_COUNT] }
|
||||
Self { limits, used: [0; RESOURCE_COUNT], peak: [0; RESOURCE_COUNT] }
|
||||
}
|
||||
pub fn limit(&self, r: LoadResource) -> u64 {
|
||||
self.limits.ceiling(r).min(LoadLimits::default().ceiling(r))
|
||||
@@ -293,6 +294,12 @@ impl LoadBudget {
|
||||
pub fn used(&self, r: LoadResource) -> u64 {
|
||||
self.used[r as usize]
|
||||
}
|
||||
/// Highest admitted reservation total during this budget's lifetime.
|
||||
/// Refunds and failed load transactions retain this measurement; refused
|
||||
/// charges do not increase it. This is accounted capacity, not process RSS.
|
||||
pub fn peak_usage(&self, r: LoadResource) -> u64 {
|
||||
self.peak[r as usize]
|
||||
}
|
||||
pub fn charge(&mut self, r: LoadResource, amount: u64) -> Result<(), LoadFailure> {
|
||||
let next = self
|
||||
.used(r)
|
||||
@@ -300,6 +307,7 @@ impl LoadBudget {
|
||||
.ok_or_else(|| LoadFailure::new(LoadCode::ResourceLimit, LoadReason::Overflow))?;
|
||||
self.check(r, next)?;
|
||||
self.used[r as usize] = next;
|
||||
self.peak[r as usize] = self.peak[r as usize].max(next);
|
||||
Ok(())
|
||||
}
|
||||
pub fn check(&self, r: LoadResource, amount: u64) -> Result<(), LoadFailure> {
|
||||
@@ -356,3 +364,29 @@ impl LoadBudget {
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn peak_tracks_admitted_capacity_through_refunds_and_failed_transactions() {
|
||||
let mut budget = LoadBudget::new(LoadLimits { temporary_bytes: 10, ..Default::default() });
|
||||
budget.charge(LoadResource::TemporaryBytes, 3).unwrap();
|
||||
let result: Result<(), LoadFailure> = budget.transaction(|budget| {
|
||||
budget.charge(LoadResource::TemporaryBytes, 7)?;
|
||||
budget.work(2)?;
|
||||
budget.charge(LoadResource::TemporaryBytes, 1)
|
||||
});
|
||||
assert!(result.is_err());
|
||||
assert_eq!(budget.used(LoadResource::TemporaryBytes), 3);
|
||||
assert_eq!(budget.peak_usage(LoadResource::TemporaryBytes), 10);
|
||||
assert_eq!(budget.used(LoadResource::Work), 2);
|
||||
assert_eq!(budget.peak_usage(LoadResource::Work), 2);
|
||||
budget.refund(LoadResource::TemporaryBytes, 3);
|
||||
assert_eq!(budget.used(LoadResource::TemporaryBytes), 0);
|
||||
assert_eq!(budget.peak_usage(LoadResource::TemporaryBytes), 10);
|
||||
assert!(budget.charge(LoadResource::TemporaryBytes, u64::MAX).is_err());
|
||||
assert_eq!(budget.peak_usage(LoadResource::TemporaryBytes), 10);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -258,3 +258,31 @@ fn constant_strings_share_image_domain_across_sessions_and_native_snapshots() {
|
||||
let Value::String(text) = &snapshot[0] else { panic!() };
|
||||
assert_eq!(text.as_ref(), "shared");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "explicit release measurement; not a timing assertion"]
|
||||
fn sealed_image_load_probe() {
|
||||
use std::time::Instant;
|
||||
let constants: Vec<_> =
|
||||
(0..512).map(|index| K::String(format!("{index:04}:{}", "x".repeat(59)))).collect();
|
||||
let bytes = fixture(&[1, 0], 0, &constants, None, &[]);
|
||||
let mut budget = budget();
|
||||
let start = Instant::now();
|
||||
let image = ValidatedProgramImage::load(&bytes, &mut budget, caps::NONE).unwrap();
|
||||
let elapsed = start.elapsed().as_nanos();
|
||||
assert_eq!(image.constant_content(), (32768, 512));
|
||||
assert_eq!(budget.used(Q::TemporaryBytes), 0);
|
||||
println!(
|
||||
"image arch={} pbx={} registry={} payload={} work={} peak_scratch={} retained_scratch={} strings={} allocations={} load_ns={}",
|
||||
std::env::consts::ARCH,
|
||||
bytes.len(),
|
||||
image.registry_bytes(),
|
||||
image.payload_bytes(),
|
||||
budget.used(Q::Work),
|
||||
budget.peak_usage(Q::TemporaryBytes),
|
||||
budget.used(Q::TemporaryBytes),
|
||||
image.constant_content().0,
|
||||
image.constant_content().1,
|
||||
elapsed
|
||||
);
|
||||
}
|
||||
|
||||
@@ -526,3 +526,156 @@ fn typed_dynamic_self_cycle_uses_one_mark_worklist_entry() {
|
||||
heap.sweep();
|
||||
assert_eq!(budget.used(Q::GuestBytes), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "explicit release measurement; not a timing assertion"]
|
||||
fn storage_and_gc_probe() {
|
||||
use crate::value_contract::{pack_dynamic, unpack_dynamic};
|
||||
use std::hint::black_box;
|
||||
use std::time::Instant;
|
||||
println!(
|
||||
"native arch={} Value={}/{} HeapRef={}/{} StringSlot={}/{} DynamicSlot={}/{} TraitSlot={}/{} StoredObject={}/{}",
|
||||
std::env::consts::ARCH,
|
||||
size_of::<Value>(),
|
||||
align_of::<Value>(),
|
||||
size_of::<HeapRef>(),
|
||||
align_of::<HeapRef>(),
|
||||
size_of::<StringSlot>(),
|
||||
align_of::<StringSlot>(),
|
||||
size_of::<DynamicSlot>(),
|
||||
align_of::<DynamicSlot>(),
|
||||
size_of::<TraitSlot>(),
|
||||
align_of::<TraitSlot>(),
|
||||
size_of::<StoredObject>(),
|
||||
align_of::<StoredObject>()
|
||||
);
|
||||
let registry = fixture_registry();
|
||||
for array in [INTS, BOOLS, VECS, PIXELS, REFS, STRINGS, TRAITS, DYNAMICS] {
|
||||
let budget = RuntimeBudget::default();
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
let layout = registry.type_layout(array).unwrap().array.unwrap();
|
||||
let count = 4096;
|
||||
let start = Instant::now();
|
||||
let reference = heap.allocate_typed_array(registry.clone(), array, count).unwrap();
|
||||
let allocation_ns = start.elapsed().as_nanos();
|
||||
let default =
|
||||
registry.type_layout(layout.element_type_id).unwrap().storage.default_values();
|
||||
assert_eq!(heap.load_typed(®istry, reference, 4095).unwrap().as_slice(), default);
|
||||
let start = Instant::now();
|
||||
for index in 0..100_000 {
|
||||
black_box(
|
||||
heap.load_typed(black_box(®istry), reference, index % count as usize).unwrap(),
|
||||
);
|
||||
}
|
||||
let access_ns = start.elapsed().as_nanos();
|
||||
println!(
|
||||
"array type={} count={} stride={} align={} guest={} aux={} handles={} allocation_ns={} reads=100000 access_ns={}",
|
||||
array.0,
|
||||
count,
|
||||
layout.stride,
|
||||
layout.element_alignment,
|
||||
budget.used(Q::GuestBytes),
|
||||
budget.used(Q::AuxiliaryBytes),
|
||||
budget.used(Q::HandleSlots),
|
||||
allocation_ns,
|
||||
access_ns
|
||||
);
|
||||
heap.mark_from_roots([reference]).unwrap();
|
||||
heap.sweep();
|
||||
assert_eq!(heap.len(), 1);
|
||||
heap.sweep();
|
||||
assert_eq!(budget.used(Q::GuestBytes), 0);
|
||||
drop(heap);
|
||||
assert_eq!(budget.used(Q::AuxiliaryBytes), 0);
|
||||
}
|
||||
for period in [1, 16, 256] {
|
||||
let mut declarations = registry.declarations().clone();
|
||||
let ty = TypeId(declarations.types.len() as u32);
|
||||
declarations.types.push(TypeDecl {
|
||||
name: None,
|
||||
kind: TypeKind::Struct(
|
||||
(0..4096)
|
||||
.map(|index| NamedType {
|
||||
name: None,
|
||||
type_id: if index % period == 0 { ENEMY } else { I64 },
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
});
|
||||
let mut load = LoadBudget::new(Default::default());
|
||||
let registry = Arc::new(Registry::resolve(declarations, 0, &mut load).unwrap());
|
||||
let budget = RuntimeBudget::default();
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
let holder = heap.allocate_typed_struct(registry.clone(), ty).unwrap();
|
||||
let child = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
let dead = heap.allocate_typed_struct(registry.clone(), ENEMY).unwrap();
|
||||
for index in (0..4096).step_by(period) {
|
||||
heap.store_typed(®istry, holder, index, &[Value::HeapRef(child)]).unwrap();
|
||||
}
|
||||
let start = Instant::now();
|
||||
for _ in 0..1000 {
|
||||
heap.mark_from_roots([holder]).unwrap();
|
||||
heap.sweep();
|
||||
}
|
||||
let gc_ns = start.elapsed().as_nanos();
|
||||
assert_eq!(heap.len(), 2);
|
||||
assert!(heap.is_valid(child));
|
||||
assert!(!heap.is_valid(dead));
|
||||
assert_eq!(budget.used(Q::TemporaryBytes), 0);
|
||||
println!(
|
||||
"gc period={} fields=4096 roots={} live={} guest={} aux={} work={} load_peak_scratch={} rounds=1000 gc_ns={}",
|
||||
period,
|
||||
4096 / period,
|
||||
heap.len(),
|
||||
budget.used(Q::GuestBytes),
|
||||
budget.used(Q::AuxiliaryBytes),
|
||||
load.used(prometeu_bytecode::load_limits::LoadResource::Work),
|
||||
load.peak_usage(prometeu_bytecode::load_limits::LoadResource::TemporaryBytes),
|
||||
gc_ns
|
||||
);
|
||||
}
|
||||
let budget = RuntimeBudget::default();
|
||||
let mut heap = Heap::with_budget(&budget);
|
||||
let start = Instant::now();
|
||||
let dynamic =
|
||||
pack_dynamic(®istry, &mut heap, VEC, &[Value::Float(3.0), Value::Float(4.0)]).unwrap();
|
||||
let pack_ns = start.elapsed().as_nanos();
|
||||
let start = Instant::now();
|
||||
for _ in 0..100_000 {
|
||||
black_box(unpack_dynamic(®istry, &heap, VEC, black_box(dynamic.as_slice())).unwrap());
|
||||
}
|
||||
println!(
|
||||
"dynamic vec2_boxes={} guest={} pack_ns={} unpacks=100000 unpack_ns={}",
|
||||
heap.len(),
|
||||
budget.used(Q::GuestBytes),
|
||||
pack_ns,
|
||||
start.elapsed().as_nanos()
|
||||
);
|
||||
let holder = heap.allocate_typed_array(registry.clone(), DYNAMICS, 1).unwrap();
|
||||
heap.store_typed(®istry, holder, 0, dynamic.as_slice()).unwrap();
|
||||
heap.mark_from_roots([holder]).unwrap();
|
||||
heap.sweep();
|
||||
assert_eq!(heap.len(), 2);
|
||||
heap.sweep();
|
||||
assert_eq!(budget.used(Q::GuestBytes), 0);
|
||||
let strings = crate::string_ledger::StringLedger::new(budget.clone()).unwrap();
|
||||
let text = strings.import("shared-native").unwrap();
|
||||
let weak = Arc::downgrade(&text);
|
||||
let root = heap.allocate_typed_struct(registry.clone(), MIXED).unwrap();
|
||||
heap.store_typed(®istry, root, 3, &[Value::String(text.clone())]).unwrap();
|
||||
heap.store_typed(®istry, root, 4, &[Value::Int64(STRING.0.into()), Value::String(text)])
|
||||
.unwrap();
|
||||
assert_eq!(budget.used(Q::StringBytes), 13);
|
||||
heap.mark_from_roots([root]).unwrap();
|
||||
heap.sweep();
|
||||
strings.reconcile();
|
||||
assert!(weak.upgrade().is_some());
|
||||
assert_eq!(budget.used(Q::StringBytes), 13);
|
||||
heap.sweep();
|
||||
strings.reconcile();
|
||||
assert!(weak.upgrade().is_none());
|
||||
assert_eq!(budget.used(Q::StringBytes), 0);
|
||||
println!(
|
||||
"native_string content=13 storage_owners=2 allocations=1 rooted_gc=retained unrooted_gc=released"
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user