From c1d58bb2b8591df062edcec6944f4b70cc40d174 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 23 Nov 2026 05:22:32 -0600 Subject: [PATCH 1/2] =?UTF-8?q?feat(id):=20stress=5Fmem=20memory=20profile?= =?UTF-8?q?=20=E2=80=94=20RSS=20per=20resident=20world,=20compaction=20spi?= =?UTF-8?q?ke=20(#13086)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit New world_bench profile measuring the #6304 scale program's memory axis: RSS per resident world at the selected stress tier (the tier's idle, active, and hot worlds restored through WorldCore::restore and held, RSS sampled between phases), and the peak additional RSS while compacting journals of 2,000 and 100,000 entries (recover via FileJournal::open, restore, hold resident, compact to the checkpoint-sized set). The generator's journal-entry construction is extracted verbatim into stress_world_entries so the profile restores exactly the worlds the generator materializes. Reference-laptop results (medians of 5 runs each) are recorded in the #6304 design's limits table: 21,011 B per resident world blended at 1x and 18,223 B at 10x, both tier resident sets fully held (no wall), and a compaction peak of 4.88 MiB over pre-op RSS at 2,000 entries and 89.41 MiB at 100,000 entries. --- ...3T07-53-00Z_design_scale_perf_stress_v1.md | 3 + pkgs/id/benches/world_bench.rs | 293 +++++++++++++++++- 2 files changed, 281 insertions(+), 15 deletions(-) diff --git a/doc/2026-11-23T07-53-00Z_design_scale_perf_stress_v1/2026-11-23T07-53-00Z_design_scale_perf_stress_v1.md b/doc/2026-11-23T07-53-00Z_design_scale_perf_stress_v1/2026-11-23T07-53-00Z_design_scale_perf_stress_v1.md index ece4987b2d..c76278be91 100644 --- a/doc/2026-11-23T07-53-00Z_design_scale_perf_stress_v1/2026-11-23T07-53-00Z_design_scale_perf_stress_v1.md +++ b/doc/2026-11-23T07-53-00Z_design_scale_perf_stress_v1/2026-11-23T07-53-00Z_design_scale_perf_stress_v1.md @@ -193,6 +193,9 @@ derived rows with measured-at-tier rows; the 100x run | Journal append + fsync | 5,180 µs median per append | measured | `benches/baseline.txt` | | Durable append ceiling, per world | ≈193 appends/s unbatched | derived | arithmetic on the row above | | Checkpoint compaction | 10,972 µs median per compact, 2,000-entry journal | measured | `benches/baseline.txt` | +| Checkpoint compaction peak memory | 4.88 MiB over pre-op RSS at 2,000 entries; 89.41 MiB at 100,000 entries | measured | `stress_mem` profile (#13086), reference laptop, median of 5 runs: recover via `FileJournal::open`, restore, hold resident, `FileJournal::compact` to the 64-entry checkpoint set; spike = peak RSS during the operation − RSS just before it | +| Resident world RSS, 1x tier | 21,011 B per world blended (1,101 worlds held, 27.07 MiB total RSS); idle 10,371 B/world; active (100 events) 94,453 B/world; hot world (10k events, 32 members) +3,358,720 B | measured | `stress_mem` profile (#13086), reference laptop, median of 5 runs, no wall — the full tier resident set is held | +| Resident world RSS, 10x tier | 18,223 B per world blended (11,001 worlds held, 196.24 MiB total RSS); idle 9,220 B/world; active (100 events) 87,912 B/world; hot world (100k events, 256 members at the codified cap) +20,324,352 B | measured | `stress_mem` profile (#13086), reference laptop, median of 5 runs, no wall — the full tier resident set is held | | Guest replay | 0.170 µs/event; 10,000 events ≈ 1.7 ms | measured | `benches/baseline.txt` | | Session fan-out | 1.437 µs per delivery, 32 clients × 100 rounds | measured | `benches/baseline.txt` | | Many-worlds command p99 | 4,549 ms vs 50 ms target — **missed** | measured | #557 (later run 5,650 ms) | diff --git a/pkgs/id/benches/world_bench.rs b/pkgs/id/benches/world_bench.rs index 19168d5bf5..d076d9ed1e 100644 --- a/pkgs/id/benches/world_bench.rs +++ b/pkgs/id/benches/world_bench.rs @@ -69,6 +69,17 @@ //! hash. Tier, seed, output root, and per-axis sizes are //! overridable via `ID_BENCH_STRESS_TIER`, `ID_BENCH_STRESS_SEED`, //! `ID_BENCH_STRESS_OUT`, and the `ID_BENCH_STRESS_*` axis knobs. +//! - `stress_mem` — the #6304 program's memory profile (#13086): RSS +//! per resident world at the selected tier (the tier's idle, +//! active, and hot worlds restored through `WorldCore::restore` +//! and held, with RSS sampled between phases), and the peak +//! additional RSS while compacting a journal of 2,000 and of +//! 100,000 entries (bulk-written fixture, recovered through +//! `FileJournal::open`, restored, then replaced with the +//! compaction bench's checkpoint-sized set). Tier and seed follow +//! the `stress_gen` knobs; `ID_BENCH_STRESS_MEM_PART` selects +//! `all` (default), `compact`, or `resident`, so each measurement +//! can run in a fresh process when attribution matters. //! - `bot_battle` (design only, not runnable yet) — a deterministic, mixed- //! strategy territory workload for N=2/4/8 users is specified in //! `../../../doc/2026-11-06T00-50-21Z_design_dynamic_world_tick_cost/`; its current- @@ -102,8 +113,8 @@ use anyhow::{Context, Result, ensure}; use id::guest_burn::GuestBurnBudget; use id::sandbox::{Sandbox, SandboxLimits}; use id::world::{ - CapabilityBounds, JoinCapability, JournalEntry, WorldCore, WorldEvent, WorldEventKind, - WorldHandle, WorldJournal, WorldLimits, WorldProgram, WorldScopes, + CapabilityBounds, JoinCapability, JournalEntry, RestoredWorld, WorldCore, WorldEvent, + WorldEventKind, WorldHandle, WorldJournal, WorldLimits, WorldProgram, WorldScopes, }; use id::world_hub::{HubLimits, OpenFuture, WorldHub, WorldOpener}; use id::world_limits::RuntimeLimits; @@ -1994,22 +2005,18 @@ fn stress_event( ) } -/// Generate one measured world directory (`//journal.jsonl`) -/// and verify it by restoring the written journal through the real -/// restore path. The expected state hash is FNV-1a over the journal -/// file's exact bytes followed by the restored core's current -/// sequence (u64 little-endian): the journal determines the state, -/// and the restore proves the journal is one the store accepts, at -/// the sequence the manifest claims. -fn generate_stress_world( - out: &Path, - name: &str, - role: &'static str, +/// The journal entries of one generated stress world, in write +/// order, with the per-shape event counts. Shared by the generator +/// (which writes them to disk and verifies the file) and the memory +/// profile (#13086, which restores them in-process): both must see +/// exactly the same world for a given (seed, world, events, members). +fn stress_world_entries( seed: u64, + name: &str, world_index: u32, events: u64, members: usize, -) -> Result { +) -> Result<(Vec, u64, u64, u64)> { ensure!( members >= 1, "stress world {name} needs at least one member" @@ -2046,6 +2053,27 @@ fn generate_stress_world( } entries.push(JournalEntry::Committed { event }); } + Ok((entries, chat, doc_edits, program_installs)) +} + +/// Generate one measured world directory (`//journal.jsonl`) +/// and verify it by restoring the written journal through the real +/// restore path. The expected state hash is FNV-1a over the journal +/// file's exact bytes followed by the restored core's current +/// sequence (u64 little-endian): the journal determines the state, +/// and the restore proves the journal is one the store accepts, at +/// the sequence the manifest claims. +fn generate_stress_world( + out: &Path, + name: &str, + role: &'static str, + seed: u64, + world_index: u32, + events: u64, + members: usize, +) -> Result { + let (entries, chat, doc_edits, program_installs) = + stress_world_entries(seed, name, world_index, events, members)?; let dir = out.join(name); std::fs::create_dir_all(&dir)?; @@ -2268,6 +2296,236 @@ fn bench_stress_gen(quick: bool) -> Result<()> { Ok(()) } +/// (Current RSS, peak RSS) of this process in bytes, from +/// `/proc/self/status` (`VmRSS` / `VmHWM`). Linux-only by design: +/// the reference machines are Linux, and a memory measurement that +/// cannot read its own resident set must fail, never fabricate. +fn rss_bytes() -> Result<(u64, u64)> { + let status = std::fs::read_to_string("/proc/self/status") + .context("read /proc/self/status for RSS sampling")?; + let mut rss_kib: Option = None; + let mut peak_kib: Option = None; + for line in status.lines() { + if let Some(value) = line.strip_prefix("VmRSS:") { + rss_kib = value.trim().trim_end_matches("kB").trim().parse().ok(); + } else if let Some(value) = line.strip_prefix("VmHWM:") { + peak_kib = value.trim().trim_end_matches("kB").trim().parse().ok(); + } + } + Ok(( + rss_kib.context("VmRSS missing from /proc/self/status")? * 1024, + peak_kib.context("VmHWM missing from /proc/self/status")? * 1024, + )) +} + +/// The tier + seed selection `stress_gen` runs under, shared with +/// the memory profile so both measure the same fixture. +fn stress_selection(quick: bool) -> Result<(StressTier, u64)> { + let tier = if quick { + StressTier::quick() + } else { + match std::env::var("ID_BENCH_STRESS_TIER") { + Ok(name) => StressTier::named(&name)?, + Err(_) => StressTier::tier_1x(), + } + }; + let seed = std::env::var("ID_BENCH_STRESS_SEED") + .ok() + .and_then(|value| value.parse().ok()) + .unwrap_or(BENCH_SEED); + Ok((tier, seed)) +} + +/// One compaction measurement of the `stress_mem` profile (#13086): +/// the peak additional resident memory while compacting a journal +/// of `events` events. The journal is bulk-written in the store's +/// on-disk format (a fixture, as in `stress_gen` — not the measured +/// append path) with the generator's own entry construction, then +/// the measured operation runs the stack's real path: recover +/// through `FileJournal::open` (which parses every entry into +/// memory), restore the world, hold it resident, and replace the +/// journal through `FileJournal::compact` with the checkpoint-sized +/// set the compaction bench uses (64 stand-in entries). The spike +/// is (peak RSS during the operation) − (RSS just before it), so it +/// attributes memory to the operation, not to whatever the process +/// already held. +fn bench_stress_mem_compact(dir: &Path, events: u64) -> Result<()> { + let name = "stress-mem-compact"; + let (entries, ..) = stress_world_entries(BENCH_SEED, name, 0, events, 1)?; + let path = dir.join(format!("stress-mem-compact-{events}.jsonl")); + { + let mut bytes: Vec = Vec::new(); + for entry in &entries { + serde_json::to_writer(&mut bytes, entry).context("serialize compact fixture entry")?; + bytes.push(b'\n'); + } + std::fs::write(&path, &bytes) + .with_context(|| format!("write compact fixture journal {path:?}"))?; + } + drop(entries); + let replacement: Vec = (1..=64) + .map(|seq| JournalEntry::Committed { + event: chat_event(seq, "checkpoint stand-in payload"), + }) + .collect(); + + let (rss_before, _) = rss_bytes()?; + let started = Instant::now(); + let (mut journal, recovered) = FileJournal::open(&path, name) + .with_context(|| format!("open compact fixture journal {path:?}"))?; + ensure!( + recovered.len() as u64 == events + 2, + "compact fixture: recovered {} entries, wrote {}", + recovered.len(), + events + 2 + ); + let restored = WorldCore::restore(name, WorldLimits::default(), recovered) + .map_err(|e| anyhow::anyhow!("restore compact fixture world: {e:#}"))?; + black_box(&restored); + journal + .compact(&replacement) + .context("compact the fixture journal")?; + let elapsed = started.elapsed(); + let (_, peak) = rss_bytes()?; + let spike = peak.saturating_sub(rss_before); + println!( + "profile stress_mem_compact events={} recovered_entries={} spike_bytes={} spike_mib={:.2} elapsed_us={}", + events, + events + 2, + spike, + spike as f64 / 1_048_576.0, + elapsed.as_micros(), + ); + drop(restored); + drop(journal); + let _ = std::fs::remove_file(&path); + Ok(()) +} + +/// The resident-set measurement of the `stress_mem` profile +/// (#13086): restore the tier's worlds through the real restore +/// path and hold them all resident, sampling RSS between phases — +/// idle worlds first (a lone `Created` header each: the resident +/// form of a world opened and evicted without a guest), then the +/// active worlds, then the hot world at the codified participant +/// cap (the generator's wall, applied the same way). A restore +/// failure stops the growth and is recorded as the tier's wall: +/// per the design's run protocol a wall is a measured limit, not a +/// failed run. +fn bench_stress_mem_resident(tier: &StressTier, seed: u64) -> Result<()> { + let started = Instant::now(); + let (rss_start, _) = rss_bytes()?; + let mut resident: Vec = Vec::new(); + let mut walls: Vec = Vec::new(); + + for index in 0..tier.idle_worlds { + let name = format!("idle-{index:05}"); + let entries = vec![JournalEntry::Created { + world_id: name.clone(), + version: 1, + }]; + match WorldCore::restore(&name, WorldLimits::default(), entries) { + Ok(restored) => resident.push(restored), + Err(error) => { + walls.push(format!("idle world {name}: {error:#}")); + break; + } + } + } + let (rss_after_idle, _) = rss_bytes()?; + let idle_held = resident.len(); + + if walls.is_empty() { + for index in 0..tier.active_worlds { + let name = format!("active-{index:04}"); + let (entries, ..) = + stress_world_entries(seed, &name, index as u32, tier.active_events, 1)?; + match WorldCore::restore(&name, WorldLimits::default(), entries) { + Ok(restored) => resident.push(restored), + Err(error) => { + walls.push(format!("active world {name}: {error:#}")); + break; + } + } + } + } + let (rss_after_active, _) = rss_bytes()?; + let active_held = resident.len() - idle_held; + + let mut hot_held = 0_usize; + if walls.is_empty() { + let participant_cap = WorldLimits::default().participants; + let hot_members = tier.hot_members.min(participant_cap); + let (entries, ..) = + stress_world_entries(seed, "hot-000", u32::MAX, tier.hot_events, hot_members)?; + match WorldCore::restore("hot-000", WorldLimits::default(), entries) { + Ok(restored) => { + resident.push(restored); + hot_held = 1; + } + Err(error) => walls.push(format!("hot world: {error:#}")), + } + } + let (rss_final, peak) = rss_bytes()?; + black_box(&resident); + + let held = resident.len(); + let per_world = + |delta: u64, count: usize| -> u64 { if count == 0 { 0 } else { delta / count as u64 } }; + println!( + "profile stress_mem tier={} seed={} worlds_held={} idle_held={} active_held={} hot_held={} rss_start_bytes={} rss_after_idle_bytes={} rss_after_active_bytes={} rss_final_bytes={} peak_rss_bytes={} rss_per_world_bytes={} rss_per_idle_world_bytes={} rss_per_active_world_bytes={} rss_hot_delta_bytes={} walls={} elapsed_secs={:.3}", + tier.name, + seed, + held, + idle_held, + active_held, + hot_held, + rss_start, + rss_after_idle, + rss_after_active, + rss_final, + peak, + per_world(rss_final.saturating_sub(rss_start), held), + per_world(rss_after_idle.saturating_sub(rss_start), idle_held), + per_world(rss_after_active.saturating_sub(rss_after_idle), active_held), + rss_final.saturating_sub(rss_after_active), + walls.len(), + started.elapsed().as_secs_f64(), + ); + for wall in &walls { + println!("profile stress_mem_wall tier={} detail={wall}", tier.name); + } + Ok(()) +} + +/// The `stress_mem` profile (#13086): the memory profile of the +/// world stack at scale — RSS per resident world at the selected +/// tier, and the compaction spike at a 100,000-entry journal +/// alongside the 2,000-entry size of the committed baseline. The +/// compaction measurements run first and in increasing size order, +/// so a fresh process attributes each spike cleanly; +/// `ID_BENCH_STRESS_MEM_PART` (`all` / `compact` / `resident`) +/// selects one part when a run wants it alone. +fn bench_stress_mem(quick: bool) -> Result<()> { + let (tier, seed) = stress_selection(quick)?; + let part = std::env::var("ID_BENCH_STRESS_MEM_PART").unwrap_or_else(|_| "all".to_owned()); + ensure!( + part == "all" || part == "compact" || part == "resident", + "unknown ID_BENCH_STRESS_MEM_PART '{part}' (valid: all, compact, resident)" + ); + let dir = std::env::temp_dir().join(format!("id-stress-mem-{}", std::process::id())); + std::fs::create_dir_all(&dir)?; + if part != "resident" { + bench_stress_mem_compact(&dir, 2_000)?; + bench_stress_mem_compact(&dir, 100_000)?; + } + if part != "compact" { + bench_stress_mem_resident(&tier, seed)?; + } + let _ = std::fs::remove_dir_all(&dir); + Ok(()) +} + fn main() -> Result<()> { let args: Vec = std::env::args().collect(); let quick = args.iter().any(|arg| arg == "--quick"); @@ -2286,7 +2544,7 @@ fn main() -> Result<()> { let dir = std::env::temp_dir().join(format!("id-bench-{}", std::process::id())); std::fs::create_dir_all(&dir)?; - const BENCHMARKS: [&str; 7] = [ + const BENCHMARKS: [&str; 8] = [ "journal_append_fsync", "checkpoint_compaction", "guest_replay", @@ -2294,6 +2552,7 @@ fn main() -> Result<()> { "many_worlds", "fuel_spin_isolation", "stress_gen", + "stress_mem", ]; if let Some(name) = only && !BENCHMARKS.contains(&name) @@ -2364,6 +2623,10 @@ fn main() -> Result<()> { bench_stress_gen(quick)?; } + if run("stress_mem") { + bench_stress_mem(quick)?; + } + if !cells.is_empty() { write_results_jsonl(&cells, quick)?; } From 5c2839eed1cecc7de16c196af50ca2d081bcdc13 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 23 Nov 2026 05:47:08 -0600 Subject: [PATCH 2/2] docs(id): record post-merge stress_mem medians in the #6304 limits table (#13086) --- .../2026-11-23T07-53-00Z_design_scale_perf_stress_v1.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/2026-11-23T07-53-00Z_design_scale_perf_stress_v1/2026-11-23T07-53-00Z_design_scale_perf_stress_v1.md b/doc/2026-11-23T07-53-00Z_design_scale_perf_stress_v1/2026-11-23T07-53-00Z_design_scale_perf_stress_v1.md index da39cb81ce..2e334b9b2a 100644 --- a/doc/2026-11-23T07-53-00Z_design_scale_perf_stress_v1/2026-11-23T07-53-00Z_design_scale_perf_stress_v1.md +++ b/doc/2026-11-23T07-53-00Z_design_scale_perf_stress_v1/2026-11-23T07-53-00Z_design_scale_perf_stress_v1.md @@ -193,9 +193,9 @@ derived rows with measured-at-tier rows; the 100x run | Journal append + fsync | 5,180 µs median per append | measured | `benches/baseline.txt` | | Durable append ceiling, per world | ≈193 appends/s unbatched | derived | arithmetic on the row above | | Checkpoint compaction | 10,972 µs median per compact, 2,000-entry journal | measured | `benches/baseline.txt` | -| Checkpoint compaction peak memory | 4.88 MiB over pre-op RSS at 2,000 entries; 89.41 MiB at 100,000 entries | measured | `stress_mem` profile (#13086), reference laptop, median of 5 runs: recover via `FileJournal::open`, restore, hold resident, `FileJournal::compact` to the 64-entry checkpoint set; spike = peak RSS during the operation − RSS just before it | -| Resident world RSS, 1x tier | 21,011 B per world blended (1,101 worlds held, 27.07 MiB total RSS); idle 10,371 B/world; active (100 events) 94,453 B/world; hot world (10k events, 32 members) +3,358,720 B | measured | `stress_mem` profile (#13086), reference laptop, median of 5 runs, no wall — the full tier resident set is held | -| Resident world RSS, 10x tier | 18,223 B per world blended (11,001 worlds held, 196.24 MiB total RSS); idle 9,220 B/world; active (100 events) 87,912 B/world; hot world (100k events, 256 members at the codified cap) +20,324,352 B | measured | `stress_mem` profile (#13086), reference laptop, median of 5 runs, no wall — the full tier resident set is held | +| Checkpoint compaction peak memory | 5.59 MiB over pre-op RSS at 2,000 entries; 89.34 MiB at 100,000 entries | measured | `stress_mem` profile (#13086), reference laptop, median of 5 runs: recover via `FileJournal::open`, restore, hold resident, `FileJournal::compact` to the 64-entry checkpoint set; spike = peak RSS during the operation − RSS just before it | +| Resident world RSS, 1x tier | 21,834 B per world blended (1,101 worlds held, 28.56 MiB total RSS); idle 11,350 B/world; active (100 events) 93,593 B/world; hot world (10k events, 32 members) +3,280,896 B | measured | `stress_mem` profile (#13086), reference laptop, median of 5 runs, no wall — the full tier resident set is held | +| Resident world RSS, 10x tier | 18,320 B per world blended (11,001 worlds held, 197.90 MiB total RSS); idle 9,334 B/world; active (100 events) 87,945 B/world; hot world (100k events, 256 members at the codified cap) +20,250,624 B | measured | `stress_mem` profile (#13086), reference laptop, median of 5 runs, no wall — the full tier resident set is held | | Guest replay | 0.170 µs/event; 10,000 events ≈ 1.7 ms | measured | `benches/baseline.txt` | | Session fan-out | 1.437 µs per delivery, 32 clients × 100 rounds | measured | `benches/baseline.txt` | | Many-worlds command p99 | 4,549 ms vs 50 ms target — **missed** | measured | #557 (later run 5,650 ms) |