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 recording VM's RAM file once; say when one VM is bigger than the cap (#5877)

ddupont1382b4fecfa2
diff --git a/.github/workflows/cd-image-linux.yml b/.github/workflows/cd-image-linux.yml
index 64bca30e13..88d72d23a9 100644
--- a/.github/workflows/cd-image-linux.yml
+++ b/.github/workflows/cd-image-linux.yml
@@ -86,6 +86,9 @@ on:
- "libs/cua/crates/cua-cli/src/sandbox/bake.rs"
- "libs/cua/crates/cua-image/src/canonical.rs"
- "libs/cua/crates/cua-image/src/publish.rs"
+ # VM admission and QEMU launch decide whether the bake VM starts.
+ - "libs/cua/crates/cua-vmm/src/density.rs"
+ - "libs/cua/crates/cua-vmm/src/qemu/**"
- ".github/workflows/cd-image-linux.yml"
- ".github/workflows/image-doctor.yml"
workflow_dispatch:
diff --git a/libs/cua/crates/cua-vmm/src/density.rs b/libs/cua/crates/cua-vmm/src/density.rs
index 0e32e2f10f..d48ed81b55 100644
--- a/libs/cua/crates/cua-vmm/src/density.rs
+++ b/libs/cua/crates/cua-vmm/src/density.rs
@@ -462,6 +462,22 @@ pub fn decide(
)));
}
let cap = cfg.rss_cap_mb(host.total_mb);
+ // One VM bigger than everything VMs may use here never fits, however
+ // many VMs are deleted: say what it asked for and the cap.
+ if need > cap {
+ return Err(Refusal(format!(
+ "'{}' needs about {} of memory ({} asked), more than this host's VM memory \
+ limit of {} (max_rss_fraction {} of {}); give it less memory (--memory), \
+ raise CUA_VM_MAX_RSS_FRACTION on this host, or create it on a machine with \
+ more memory (on=\"auto\")",
+ req.name,
+ gib(need),
+ gib(req.memory_mb),
+ gib(cap),
+ cfg.max_rss_fraction,
+ gib(host.total_mb)
+ )));
+ }
let used = usage.vm_rss_mb() + pending_mb;
if used + need > cap {
return Err(Refusal(format!(
@@ -911,6 +927,60 @@ mod tests {
}
}
+ /// The CI runner: 7.8 GiB, nothing running, default fraction (6.2 GiB
+ /// for VMs).
+ fn runner() -> (DensityConfig, Usage) {
+ let cfg = DensityConfig {
+ min_available_mb: 0,
+ ..DensityConfig::default()
+ };
+ let u = Usage {
+ host: host(7_990, 7_200),
+ host_cpus: 4,
+ ..Default::default()
+ };
+ (cfg, u)
+ }
+
+ fn fresh(name: &str, memory_mb: u64) -> Request {
+ Request {
+ name: name.into(),
+ vcpus: 2,
+ memory_mb,
+ shared_file: None,
+ shared_file_mb: 0,
+ }
+ }
+
+ #[test]
+ fn a_vm_under_the_cap_is_admitted_on_an_empty_host() {
+ let (cfg, u) = runner();
+ // A 4 GiB bake on the 7.8 GiB runner, nothing else running.
+ let a = decide(&cfg, &u, &[], &fresh("bake", 4096)).unwrap();
+ assert!(a.expected_mb >= 4096);
+ // Right at the cap still fits.
+ let cap = cfg.rss_cap_mb(7_990);
+ let at_cap = cap - cap / 11 - 64;
+ assert!(decide(&cfg, &u, &[], &fresh("edge", at_cap)).is_ok());
+ }
+
+ #[test]
+ fn a_vm_bigger_than_the_cap_says_what_it_asked_for_and_the_cap() {
+ let (cfg, u) = runner();
+ let e = decide(&cfg, &u, &[], &fresh("big", 8192)).unwrap_err().0;
+ assert!(
+ e.starts_with("'big' needs about 8.0 GiB of memory (8.0 GiB asked)"),
+ "{e}"
+ );
+ assert!(
+ e.contains("more than this host's VM memory limit of 6.2 GiB"),
+ "{e}"
+ );
+ assert!(e.contains("CUA_VM_MAX_RSS_FRACTION"), "{e}");
+ // Not the "its VMs use 0.0 GiB" message for a host that is empty.
+ assert!(!e.contains("its VMs use"), "{e}");
+ }
+
#[test]
fn shared_snapshot_pages_count_once() {
let u = Usage {
diff --git a/libs/cua/crates/cua-vmm/src/qemu/mod.rs b/libs/cua/crates/cua-vmm/src/qemu/mod.rs
index df883b6c20..75e4622c5b 100644
--- a/libs/cua/crates/cua-vmm/src/qemu/mod.rs
+++ b/libs/cua/crates/cua-vmm/src/qemu/mod.rs
@@ -745,12 +745,13 @@ impl QemuRuntime {
boot: Boot,
persist: bool,
) -> Result<()> {
- let shared = match &boot.memory {
- Memory::File { path, .. } => Some(path.clone()),
- Memory::Ram => None,
- };
- self.admit_waiting(&st.name, spec.cpus, spec.memory_mb, shared)
- .await?;
+ self.admit_waiting(
+ &st.name,
+ spec.cpus,
+ spec.memory_mb,
+ admission_file(&boot.memory),
+ )
+ .await?;
let res = self.launch_admitted(st, spec, boot, persist).await;
if res.is_err() {
self.release(&st.name);
@@ -2870,6 +2871,23 @@ impl Runtime for QemuRuntime {
}
}
+/// The snapshot RAM file a VM shares with others, as admission counts it
+/// ([`crate::density`]): a file mapped copy-on-write (`share=off`, a
+/// restore or a fork) is counted once for every VM mapping it, on top of
+/// each VM's private pages. A file the VM writes itself (`share=on`, a
+/// golden snapshot being recorded) is that VM's own RAM, not shared with
+/// anyone: it is admitted like anonymous RAM, so its memory is not counted
+/// twice (once as the guest's RAM, again as the file).
+fn admission_file(memory: &Memory) -> Option<PathBuf> {
+ match memory {
+ Memory::File {
+ path,
+ shared: false,
+ } => Some(path.clone()),
+ Memory::File { shared: true, .. } | Memory::Ram => None,
+ }
+}
+
#[cfg(test)]
mod tests {
#[test]
@@ -3273,6 +3291,25 @@ mod tests {
/// Admission counts VMs admitted moments ago (pending) and refuses with
/// HostCapacity, saying which limit; a failed launch gives its slot back.
+ #[test]
+ fn a_recording_vm_counts_its_own_ram_file_once() {
+ // A golden snapshot being recorded writes its RAM file (share=on):
+ // that file is its RAM, not a shared one, so admission must not add
+ // it on top of the guest's memory (a 4 GiB bake counted as 8 GiB
+ // did not fit a 7.8 GiB CI runner).
+ let rec = Memory::File {
+ path: "/g/mem.ram".into(),
+ shared: true,
+ };
+ assert_eq!(admission_file(&rec), None);
+ let restore = Memory::File {
+ path: "/g/mem.ram".into(),
+ shared: false,
+ };
+ assert_eq!(admission_file(&restore), Some(PathBuf::from("/g/mem.ram")));
+ assert_eq!(admission_file(&Memory::Ram), None);
+ }
+
#[test]
fn admission_counts_pending_vms_and_refuses_with_host_capacity() {
let d = tempfile::tempdir().unwrap();