Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(cua-vmm): count a fork's checkpoint RAM once when it is the first to map it (#5890)

ddupontc96105986c39
diff --git a/.github/workflows/nightly-qemu-fork.yml b/.github/workflows/nightly-qemu-fork.yml
index 1afc6da4fa..75dada6e9c 100644
--- a/.github/workflows/nightly-qemu-fork.yml
+++ b/.github/workflows/nightly-qemu-fork.yml
@@ -25,6 +25,7 @@ on:
workflow_dispatch:
pull_request:
paths:
+ - "libs/cua/crates/cua-vmm/src/density.rs"
- "libs/cua/crates/cua-vmm/src/qemu/**"
- "libs/cua/crates/cua-daemon/tests/qemu_fork_fanout.rs"
- "libs/cua/bench/fork-fanout/**"
@@ -128,6 +129,13 @@ jobs:
EXPECT_WARM: ${{ matrix.disk == 'source' && '1' || '0' }}
MEMORY_MB: "1024"
DIRTY_MB: "200"
+ # This bench measures fork memory and latency, not admission (unit
+ # tested in cua-vmm). Hosted runners of private repositories have
+ # 2 CPUs, where 8 forks of 2 vCPUs plus the world (18) pass the
+ # default 8x overcommit (16) and the last fork would be refused;
+ # public ones have 4. Allow the same forks on both, so a run stays
+ # comparable with the baseline.
+ CUA_VM_VCPU_OVERCOMMIT: "10"
# A Linux VM runs with free page reporting by default, and the RAM
# of such a VM stays in its checkpoint's stream, loaded privately
# by each fork. The loaded-stream path this lane measures (the
diff --git a/libs/cua/crates/cua-vmm/src/density.rs b/libs/cua/crates/cua-vmm/src/density.rs
index d48ed81b55..f43a5f88d0 100644
--- a/libs/cua/crates/cua-vmm/src/density.rs
+++ b/libs/cua/crates/cua-vmm/src/density.rs
@@ -398,15 +398,6 @@ pub fn decide(
pending: &[Pending],
req: &Request,
) -> Result<Admit, Refusal> {
- let expected = estimate_mb(usage, pending, req);
- if !cfg.enabled {
- return Ok(Admit {
- expected_mb: expected,
- });
- }
- // Pending VMs: whatever their measurement does not show yet.
- let totals = pending_totals(usage, pending);
- let (pending_mb, pending_vcpus) = (totals.mb, totals.vcpus);
// A shared RAM file already counted (mapped by a running VM, or reserved
// by a pending one) costs nothing more.
let new_shared = match &req.shared_file {
@@ -418,6 +409,15 @@ pub fn decide(
}
_ => 0,
};
+ let expected = fork_private_mb(estimate_mb(usage, pending, req), new_shared, req);
+ if !cfg.enabled {
+ return Ok(Admit {
+ expected_mb: expected,
+ });
+ }
+ // Pending VMs: whatever their measurement does not show yet.
+ let totals = pending_totals(usage, pending);
+ let (pending_mb, pending_vcpus) = (totals.mb, totals.vcpus);
let need = expected + new_shared;
if let Some(hv) = usage.hypervisor {
@@ -511,6 +511,23 @@ pub fn decide(
})
}
+/// The private memory a VM is expected to need when it also brings a shared
+/// RAM file nobody maps yet (`new_shared_mb`, the first fork of a checkpoint
+/// or the first restore of a snapshot): each of its guest pages is either
+/// still the file's page or its own private copy, never both, so together
+/// they are at most its guest RAM plus QEMU's own (the same bound
+/// [`estimate_mb`] applies). Charging the full guest RAM as private on top
+/// of the whole file counted one VM's memory twice. Later VMs of the same
+/// file still get the full estimate until a sibling has settled, so a burst
+/// of forks is charged its honest worst case.
+fn fork_private_mb(estimate: u64, new_shared_mb: u64, req: &Request) -> u64 {
+ if new_shared_mb == 0 {
+ return estimate;
+ }
+ let ceiling = req.memory_mb + req.memory_mb / 10 + 64;
+ estimate.min(ceiling.saturating_sub(new_shared_mb)).max(64)
+}
+
/// How much more VM memory (MiB) this host takes now: the least of what
/// the cap and the available-memory floor leave.
pub fn headroom_mb(cfg: &DensityConfig, usage: &Usage, pending: &[Pending]) -> Option<u64> {
@@ -981,6 +998,73 @@ mod tests {
assert!(!e.contains("its VMs use"), "{e}");
}
+ fn fork_of(name: &str, memory_mb: u64) -> Request {
+ Request {
+ name: name.into(),
+ vcpus: 2,
+ memory_mb,
+ shared_file: Some(PathBuf::from("/ck/mem.ram")),
+ shared_file_mb: memory_mb,
+ }
+ }
+
+ #[test]
+ fn the_first_fork_of_a_checkpoint_counts_its_ram_once() {
+ // The bake's fork: a 4 GiB checkpoint, nothing running, 7.8 GiB host.
+ let (cfg, u) = runner();
+ let a = decide(&cfg, &u, &[], &fork_of("fork", 4096)).unwrap();
+ // The file (4 GiB) plus what fits under guest RAM + QEMU's own, not
+ // the file plus another full 4 GiB.
+ assert_eq!(a.expected_mb, 4096 / 10 + 64);
+ assert!(4096 + a.expected_mb <= 4096 + 4096 / 10 + 64);
+ }
+
+ #[test]
+ fn a_burst_of_forks_is_still_charged_its_worst_case() {
+ let (cfg, u) = runner();
+ let first = decide(&cfg, &u, &[], &fork_of("f0", 4096)).unwrap();
+ let pending = [Pending {
+ name: "f0".into(),
+ vcpus: 2,
+ expected_mb: first.expected_mb,
+ at: Instant::now(),
+ shared_file: Some(PathBuf::from("/ck/mem.ram")),
+ shared_file_mb: 4096,
+ }];
+ // The file is counted once (by f0); a second fork whose sibling has
+ // not settled yet may still write all its RAM, so it is charged the
+ // full estimate, and on this host it does not fit.
+ let e = decide(&cfg, &u, &pending, &fork_of("f1", 4096))
+ .unwrap_err()
+ .0;
+ assert!(e.contains("VM memory limit"), "{e}");
+ // On a host with room it fits, charged the full guest RAM.
+ let big = Usage {
+ host: host(32_768, 30_000),
+ host_cpus: 8,
+ ..Default::default()
+ };
+ let a = decide(&cfg, &big, &pending, &fork_of("f1", 4096)).unwrap();
+ assert_eq!(a.expected_mb, 4096);
+ }
+
+ #[test]
+ fn later_forks_are_estimated_from_settled_siblings() {
+ // Once a fork has settled, the next ones are expected to use what it
+ // uses (its private pages), and the file is already counted.
+ let (cfg, mut u) = runner();
+ u.vms.push(VmUse {
+ name: "f0".into(),
+ vcpus: 2,
+ memory_mb: 4096,
+ private_mb: 300,
+ shared_file: Some(PathBuf::from("/ck/mem.ram")),
+ });
+ u.shared.push((PathBuf::from("/ck/mem.ram"), 1_500));
+ let a = decide(&cfg, &u, &[], &fork_of("f1", 4096)).unwrap();
+ assert_eq!(a.expected_mb, 300);
+ }
+
#[test]
fn shared_snapshot_pages_count_once() {
let u = Usage {