dc87c0b98a
The fullscreen-map OOM (rust_oom in hashbrown reserve_rehash under Ground::answer during a large raid): the mod's whole state shares one 64 MiB linear memory, and three consumers grew without an effective bound. Measured on a native replica of the crash load (tests/ground_oom.rs: 64 complexes, six all-sides Oreshnik raids plus a smart raid over 10000 ticks, 304 objects airborne at once, a wide map query walk; render answers exactly as the WASM host gives them): peak 45.5 MiB steady + 21.9 MiB rehash transient = 67 MiB, over the cap. The bulk: kept replays 23.2 MiB (the scene journal 4.6 MiB each, filled with per-radar track transitions), Ground.known 10.9 MiB (277k columns; the old sweep only ran above 300k and kept everything younger than 2000 ticks, so a wide query pattern grew it without bound and the next rehash doubled the table), the live recording 7.5 MiB, radar tracks 3.2 MiB. Bounds, derived from the 64 MiB budget (Sim::memory reports each consumer): - Ground.known: KNOWN_MAX 100_000 columns (~2.7 MiB steady, ~8.2 MiB through a rehash at 24 B + control byte per entry, 7/8 load). Over the cap the oldest answers go down to three quarters (a sweep is not every tick), ordered by (age, column) so the sweep is deterministic; heights past SWEEP_AGE go with them. Evicted columns are asked again when next needed — the cache is reconstructible, no target, frequency or precision is cut. - Scene journal: MAX_SCENE_EVENTS 65_536 -> 24_576 (~1.7 MiB), and the track transitions (acquire/handoff/lock/lost — they grow with systems x contacts, the journal's bulk under a large raid) get their own 8_192 budget; beyond it the later ones are not recorded. Launches, fires and outcomes keep the rest of the journal; chains() already tolerates missing steps. - Flight track points: MAX_TRACK_POINTS 2 MiB per recording; over it every other point is kept and the ticks between points double (Track::step, the #142 decimation pattern for the systems' poses). Replay readers use the track's own step. Replica peak after: 24.5 MiB steady + 5.5 MiB transient = 30 MiB, under the 48 MiB headroom target; the fight resolves (376 intercepts, sky clears) and the same seed replays the same ordered trace. The saturation checkpoint is untouched (314 / 2022 / 184): its cache stays under the cap. Co-Authored-By: Kimi Code <noreply@moonshot.ai>
211 lines
9.9 KiB
Rust
211 lines
9.9 KiB
Rust
//! The fullscreen-map OOM replica (the `Ground::answer` rehash crash): a large raid watched
|
|
//! from all sides through a long session, with the wide ground-query pattern the command map
|
|
//! adds on top of the simulation's own. 64 integrated complexes stand where `/airdef deploy`
|
|
//! puts them; `airdef wave oreshnik 12 2600` orders come from all sides every 1500 ticks (six
|
|
//! raids, so recordings close and the kept replays pile up), and a smart raid maps its whole
|
|
//! grid. The render answers are produced exactly as the WASM test host produces them
|
|
//! (`horizon_mods::world_query::trace`, flat land at y=64). While the fight runs, a map pass
|
|
//! every other tick (the map's redraw pace) sweeps ground queries over a wide view — the
|
|
//! fullscreen map asks for the land under its whole grid, not just the flights' columns.
|
|
//!
|
|
//! The test measures what each consumer holds (`Sim::memory`) through the session, asserts the
|
|
//! peak fits the 64 MiB WASM linear memory with headroom for a hashbrown rehash transient, and
|
|
//! that the fight still resolves: intercepts happen, the sky clears, and the same seed replays
|
|
//! the same ordered combat trace.
|
|
//!
|
|
//! Run: `cargo test --test ground_oom -- --nocapture`.
|
|
use airdef::sim::world::{Fill, RayReply, SectionAnswer};
|
|
use airdef::sim::{Aim, Approach, DroneKind, Ground, MemoryReport, Out, RaidOrder, Sam, SamKind, Sim};
|
|
use glam::DVec3;
|
|
use horizon_mods::world_query::{self, BlockProbe, Cell, Trace};
|
|
|
|
/// The seed the mod derives on the first tick of a fresh world (`open_world`, game_time 1).
|
|
const SEED: u64 = 1u64.wrapping_mul(2654435761) ^ 0x9e37;
|
|
const PLAYER: DVec3 = DVec3::new(0.5, 64.0, 0.5);
|
|
const DEPLOY_AT: DVec3 = DVec3::new(0.5, 64.0, 8.5);
|
|
const GROUND_Y: i32 = 64;
|
|
const KINDS: [SamKind; 7] = [SamKind::S400, SamKind::Patriot, SamKind::Pantsir, SamKind::Gepard, SamKind::Nasams, SamKind::IrisT, SamKind::Tor];
|
|
/// The engine's cap on a mod's linear memory (`horizon-mods` runtime `MEMORY`).
|
|
const WASM_MEMORY: usize = 64 << 20;
|
|
/// The peak the mod must stay under: headroom for the allocator, the transient query/answer
|
|
/// vectors and save serialization on top of the measured steady state.
|
|
const PEAK_BUDGET: usize = 48 << 20;
|
|
/// Six raids fought out, recordings closed and replays kept; long enough for the caches to fill.
|
|
const TICKS: u64 = 10_000;
|
|
/// A wave every 1500 ticks: the sky clears between raids, so each is its own recording.
|
|
const WAVE_EVERY: u64 = 1500;
|
|
|
|
struct Flat;
|
|
impl BlockProbe for Flat {
|
|
fn cell(&mut self, _x: i32, y: i32, _z: i32) -> Cell<'_> {
|
|
if y < GROUND_Y { Cell::Solid(None) } else { Cell::Empty }
|
|
}
|
|
}
|
|
|
|
fn section_fill(sec: (i32, i32, i32)) -> SectionAnswer {
|
|
let base = i64::from(sec.1) * 16;
|
|
let solid = (i64::from(GROUND_Y) - base).clamp(0, 16) as usize;
|
|
let fill = match solid {
|
|
0 => Fill::Empty,
|
|
16 => Fill::Full,
|
|
_ => {
|
|
let mut bits = [0u8; 512];
|
|
bits[..solid * 32].fill(255);
|
|
Fill::Mixed(Box::new(bits))
|
|
}
|
|
};
|
|
SectionAnswer { sec, fill }
|
|
}
|
|
|
|
fn ray_reply(id: u32, a: DVec3, b: DVec3) -> RayReply {
|
|
match world_query::trace(&mut Flat, [a.x, a.y, a.z], [b.x, b.y, b.z]) {
|
|
Trace::Clear => RayReply { id, clear: true, hit: false, fraction: 1.0, normal: [0; 3], block: [0; 3] },
|
|
Trace::Hit { fraction, normal, block } => RayReply { id, clear: false, hit: true, fraction, normal, block },
|
|
Trace::Unknown { .. } => RayReply { id, clear: false, hit: false, fraction: 0.0, normal: [0; 3], block: [0; 3] },
|
|
}
|
|
}
|
|
|
|
fn wave(sim: &mut Sim, out: &mut Out, count: u32, aim: Aim) -> u32 {
|
|
let o = RaidOrder { kinds: vec![(DroneKind::Oreshnik, count)], approach: Approach::AllSides, dist: 2600.0, aim };
|
|
sim.order_raid(&o, &[PLAYER], out)
|
|
}
|
|
|
|
fn deploy(sim: &mut Sim) {
|
|
for _ in 0..9 {
|
|
for kind in KINDS {
|
|
let id = sim.id();
|
|
sim.sams.push(Sam::new(id, kind, DEPLOY_AT, std::f64::consts::PI));
|
|
sim.settle(id);
|
|
}
|
|
}
|
|
let id = sim.id();
|
|
sim.sams.push(Sam::new(id, SamKind::S400, DEPLOY_AT, std::f64::consts::PI));
|
|
sim.settle(id);
|
|
assert_eq!(sim.sams.len(), 64);
|
|
}
|
|
|
|
/// One run of the heavy session: the combat trace (ordered, for the determinism check), the
|
|
/// peak memory report and the counters at the end.
|
|
struct Run {
|
|
trace: Vec<(u64, u32, u32, usize)>,
|
|
peak: MemoryReport,
|
|
peak_total: usize,
|
|
peak_known: usize,
|
|
peak_airborne: usize,
|
|
sim: Sim,
|
|
}
|
|
|
|
fn run() -> Run {
|
|
let mut sim = Sim::new(SEED);
|
|
let mut ground = Ground::default();
|
|
let mut out = Out::default();
|
|
let mut columns: Vec<(i32, i32, i32)> = Vec::new();
|
|
let mut sections: Vec<SectionAnswer> = Vec::new();
|
|
let mut rays: Vec<RayReply> = Vec::new();
|
|
let mut changes_epoch = 0u32;
|
|
let mut run = Run { trace: Vec::new(), peak: MemoryReport::default(), peak_total: 0, peak_known: 0, peak_airborne: 0, sim: Sim::new(SEED) };
|
|
for now in 1..=TICKS {
|
|
if now == 3 {
|
|
deploy(&mut sim);
|
|
}
|
|
if now >= 3 && now < 8000 && (now - 3) % WAVE_EVERY == 0 {
|
|
assert!(wave(&mut sim, &mut out, 12, Aim::Objects) > 0, "wave at tick {now} was refused");
|
|
}
|
|
if now == 1203 {
|
|
assert!(wave(&mut sim, &mut out, 10, Aim::Smart) > 0, "the smart raid was refused");
|
|
}
|
|
if now == 1103 {
|
|
sim.shader = true;
|
|
}
|
|
let now0 = sim.tick;
|
|
ground.world.changed_rev(changes_epoch, 0, false, &[]);
|
|
ground.answer(std::mem::take(&mut columns), now0);
|
|
ground.world.answer(std::mem::take(&mut sections), now0);
|
|
ground.world.answer_rays(&std::mem::take(&mut rays));
|
|
sim.day_time = Some(now);
|
|
sim.night = (13000..23000).contains(&(now % 24000));
|
|
out = Out::default();
|
|
sim.step(&mut ground, Some(PLAYER), &mut out);
|
|
// The fullscreen map redraws every other tick and asks for the land under its grid:
|
|
// a wide walk over the view (32 fresh columns a redraw), as panning and placing do.
|
|
if now % 2 == 0 {
|
|
let t = now as f64;
|
|
let x0 = (t * 13.37).rem_euclid(8000.0) - 4000.0;
|
|
let z0 = (t * 7.91).rem_euclid(8000.0) - 4000.0;
|
|
for k in 0..32 {
|
|
let _ = ground.at(DVec3::new(x0 + k as f64 * 8.0, 0.0, z0 + k as f64 * 3.0), sim.tick);
|
|
}
|
|
}
|
|
columns = ground.take_wanted().into_iter().map(|(x, z)| (x, z, GROUND_Y)).collect();
|
|
let _chunks = ground.take_chunks();
|
|
let rq = ground.world.take_rays(now);
|
|
let sq = ground.world.take_wanted(now);
|
|
if !rq.is_empty() || !sq.is_empty() {
|
|
changes_epoch = 1;
|
|
}
|
|
rays = rq.into_iter().map(|(id, a, b)| ray_reply(id, a, b)).collect();
|
|
sections = sq.into_iter().map(section_fill).collect();
|
|
|
|
let airborne = sim.drones.iter().filter(|d| d.alive()).count();
|
|
run.peak_airborne = run.peak_airborne.max(airborne);
|
|
run.peak_known = run.peak_known.max(ground.known_len());
|
|
if now % 100 == 0 {
|
|
let m = sim.memory(&ground);
|
|
let total = m.total();
|
|
if total > run.peak_total {
|
|
run.peak_total = total;
|
|
run.peak = m;
|
|
}
|
|
run.trace.push((now, sim.kills, sim.hits, airborne));
|
|
}
|
|
}
|
|
run.sim = sim;
|
|
run
|
|
}
|
|
|
|
#[test]
|
|
fn the_heavy_map_session_fits_the_memory_budget_and_the_fight_resolves() {
|
|
let run = run();
|
|
let m = run.peak;
|
|
eprintln!("peak airborne {}, peak known columns {}", run.peak_airborne, run.peak_known);
|
|
eprintln!("peak memory at the worst sample:\n{}", m.text());
|
|
if let Some(rec) = run.sim.recording() {
|
|
let (tracks, journal, poses, misc) = rec.memory_parts();
|
|
eprintln!("live recording: tracks {:.2} MiB, journal {:.2} MiB, poses {:.2} MiB, misc {:.2} MiB", tracks as f64 / (1024.0 * 1024.0), journal as f64 / (1024.0 * 1024.0), poses as f64 / (1024.0 * 1024.0), misc as f64 / (1024.0 * 1024.0));
|
|
}
|
|
for (i, rec) in run.sim.replays.iter().enumerate() {
|
|
let (tracks, journal, poses, misc) = rec.memory_parts();
|
|
eprintln!("replay {}: len {} ticks, tracks {:.2} MiB, journal {:.2} MiB, poses {:.2} MiB, misc {:.2} MiB", i + 1, rec.len, tracks as f64 / (1024.0 * 1024.0), journal as f64 / (1024.0 * 1024.0), poses as f64 / (1024.0 * 1024.0), misc as f64 / (1024.0 * 1024.0));
|
|
}
|
|
eprintln!(
|
|
"end: tick {}, kills {}, hits {}, crashes {}, airborne {}, replays {}",
|
|
run.sim.tick,
|
|
run.sim.kills,
|
|
run.sim.hits,
|
|
run.sim.crashes,
|
|
run.sim.drones.iter().filter(|d| d.alive()).count(),
|
|
run.sim.replays.len()
|
|
);
|
|
// The load is the crash's load: a large raid, more than 200 objects in the sky at once.
|
|
assert!(run.peak_airborne > 200, "the replica never reached the crash's load: {}", run.peak_airborne);
|
|
// The fight resolves: intercepts happen and the sky clears by the end of the session.
|
|
assert!(run.sim.kills > 100, "the defence stopped intercepting: {}", run.sim.kills);
|
|
assert_eq!(run.sim.drones.iter().filter(|d| d.alive()).count(), 0, "the raid never fought out");
|
|
// The memory budget: the steady peak plus the worst rehash transient (the old and the new
|
|
// table of the biggest map held at once) stays under the headroom target, far under the cap.
|
|
let peak = run.peak_total + m.rehash_headroom();
|
|
assert!(peak < PEAK_BUDGET, "peak {} MiB (steady {} + rehash {}) over the {} MiB budget:\n{}", peak >> 20, run.peak_total >> 20, m.rehash_headroom() >> 20, PEAK_BUDGET >> 20, m.text());
|
|
assert!(peak < WASM_MEMORY, "peak {} MiB over the engine's {} MiB cap", peak >> 20, WASM_MEMORY >> 20);
|
|
}
|
|
|
|
#[test]
|
|
fn the_same_seed_replays_the_same_ordered_combat_trace() {
|
|
let a = run();
|
|
let b = run();
|
|
assert_eq!(a.trace, b.trace, "the combat trace diverged between identical runs");
|
|
assert_eq!(a.sim.kills, b.sim.kills);
|
|
assert_eq!(a.sim.hits, b.sim.hits);
|
|
assert_eq!(a.sim.tally.text(), b.sim.tally.text());
|
|
assert_eq!(a.peak_known, b.peak_known);
|
|
}
|