Skip to content

Commit

Permalink
Ensure we leave at least 3072MiB free to the host
Browse files Browse the repository at this point in the history
Leaving less than 3072MiB for the host can compromise the system's
stability, so ensure we do that unless instructed by the user. This
mainly applies to 8GB systems.

Signed-off-by: Sergio Lopez <[email protected]>
  • Loading branch information
slp authored and alyssarosenzweig committed Oct 1, 2024
1 parent d661705 commit 572f276
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions crates/krun/src/bin/krun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,27 @@ fn main() -> Result<()> {
let sysinfo = sysinfo().context("Failed to get system information")?;
let ram_total_mib = (sysinfo.ram_total() / (1024 * 1024)) as u32;

// By default, set the microVM RAM to be 80% of the system's RAM.
let ram_mib = options
.mem
.unwrap_or(MiB::from((ram_total_mib as f64 * 0.8) as u32));
let ram_mib = if let Some(ram_mib) = options.mem {
ram_mib
} else {
// By default, set the microVM RAM to be 80% of the system's RAM.
let guest_ram = (ram_total_mib as f64 * 0.8) as u32;
// Ensure we leave 3072 MiB free for the host + VRAM to avoid
// compromising the host's stability. This mainly applies to systems
// with 8GB of RAM.
let guest_ram = if ram_total_mib
.checked_sub(guest_ram)
.context("Failed to calculate the amount of free RAM")?
< 3072
{
ram_total_mib
.checked_sub(3072)
.context("Systems with less than 3072 MiB of RAM are not supported")?
} else {
guest_ram
};
MiB::from(guest_ram)
};
// VRAM is actually the SHM window for virtio-gpu. The userspace drivers in the
// guest are expected to be conservative and tell applications that the amount
// of VRAM is just a percentage of the guest's RAM, so we can afford being more
Expand Down

0 comments on commit 572f276

Please sign in to comment.