Skip to content

Commit 7720e0f

Browse files
committed
muvm: use HK_SYSMEM to set VRAM limit
By default, HK sets the heap size to be half the size of the *guest* memory. Since commit 167744dc it's also possible to override the heap size by setting the HK_SYSMEM environment variable. Previously, the vram command line option was acting on the virtio-gpu SHM window. This didn't help in most cases because userspace doesn't really care nor is aware of it. Here, we set the SHM window of virtio-gpu to always be as large as the host's RAM, not because we expect VRAM to consume 100% of the RAM (which is obviously impossible) but to account for region fragmentation. Then, we use HK_SYSMEM to tell userspace the desired heap size (the closest thing to VRAM we can aim for), which will be set to either the value passed by the user throught the "vram" argument or half the size of the *host* RAM. Signed-off-by: Sergio Lopez <[email protected]>
1 parent 92e39e7 commit 7720e0f

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

crates/muvm/src/bin/muvm.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,22 @@ fn main() -> Result<()> {
149149
};
150150
MiB::from(guest_ram)
151151
};
152-
// VRAM is actually the SHM window for virtio-gpu. The userspace drivers in the
153-
// guest are expected to be conservative and tell applications that the amount
154-
// of VRAM is just a percentage of the guest's RAM, so we can afford being more
155-
// aggressive here and set the window to be as large as the system's RAM, leaving
156-
// some room for dealing with potential fragmentation.
157-
let vram_mib = options.vram.unwrap_or(MiB::from(ram_total_mib as u32));
152+
// By default, HK sets the heap size to be half the size of the *guest* memory.
153+
// Since commit 167744dc it's also possible to override the heap size by setting
154+
// the HK_SYSMEM environment variable.
155+
//
156+
// Let's set the SHM window for virtio-gpu to be as large as the host's RAM, not
157+
// because we expect VRAM to be as large as RAM, but to account for the more than
158+
// likely region fragmentation.
159+
//
160+
// Then, let's set HK_SYSMEM to be either half the size of the *host* memory, or
161+
// the value passed by the user with the "vram" argument.
162+
let vram_shm_mib = MiB::from(ram_total_mib as u32);
163+
let vram_mib = options.vram.unwrap_or(MiB::from(ram_total_mib as u32 / 2));
164+
env.insert(
165+
"HK_SYSMEM".to_owned(),
166+
(u32::from(vram_mib) as u64 * 1024 * 1024).to_string(),
167+
);
158168

159169
// Bind the microVM to the specified CPU cores.
160170
let mut cpuset = CpuSet::new();
@@ -185,7 +195,7 @@ fn main() -> Result<()> {
185195
krun_set_gpu_options2(
186196
ctx_id,
187197
virgl_flags,
188-
(u32::from(vram_mib) as u64) * 1024 * 1024,
198+
(u32::from(vram_shm_mib) as u64) * 1024 * 1024,
189199
)
190200
};
191201
if err < 0 {

crates/muvm/src/cli_options.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,10 @@ pub fn options() -> OptionParser<Options> {
7272
.optional();
7373
let vram = long("vram")
7474
.help(
75-
"The amount of Video RAM, in MiB, that will be available to this microVM.
76-
The memory configured for the microVM will not be reserved
77-
immediately. Instead, it will be provided as the guest demands
78-
it, and will be returned to the host once the guest releases
79-
the underlying resources.
80-
[default: same as the total amount of RAM in the system]",
75+
"The amount of Video RAM, in MiB, that will reported by userspace in this microVM.
76+
The userspace drivers will report this amount as heap size
77+
to the clients running in the microVM.
78+
[default: 50% of total RAM]",
8179
)
8280
.argument("VRAM")
8381
.optional();

0 commit comments

Comments
 (0)