Repository navigation
Add optional Virtio memory balloon support - #1276
TastyHeadphones wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3678f45d82
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
@codex review |
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback". |
|
@fkorotkov-oai @fkorotkov, do you guys have plans to add this memory balloon support? |
|
Sorry for getting back to this issue so late. Is the motivation behind this change to have an option to free as much memory as possible? I'm thinking if we should just enable it by default for Linux hosts which supports it and then just have a menu item to set |
That is a good idea |
Add a per-VM "memoryBalloon" setting (disabled by default) that, when enabled via "tart set <name> --memory-balloon true", attaches a VZVirtioTraditionalMemoryBalloonDeviceConfiguration to the VM, allowing supported guest operating systems to participate in best-effort memory reclaim. A balloon target can be requested at run time via "tart run --balloon-target-memory <MB>". The target is re-applied periodically, since a target set before the guest's virtio-balloon driver probes the device is lost when the guest resets the device while booting (the same applies to guest reboots). The balloon device is skipped for suspendable VMs (similarly to the entropy device) to not interfere with the save/restore support. VM.craftConfiguration() is split into a buildConfiguration() part and a validation part, so that the device wiring can be unit-tested without the "com.apple.security.virtualization" entitlement. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
For macOS guests, "tart run --balloon-target-memory" now also enforces the VM's own minimum supported memory size (dictated by the restore image), similarly to how "tart set --memory" restricts the configured memory size. Addresses a PR review comment. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…emory Reworks the feature per the PR feedback[1]: instead of an opt-in device plus a target size that has to be picked up front, Linux guests now get a virtio memory balloon device out of the box and the "Control" menu gained a "Free Up Memory" toggle that asks the guest to shrink as much as it can. * the "memoryBalloon" setting and "tart set --memory-balloon" are gone: the device is attached whenever the guest is Linux (macOS guests show little to no practical memory reduction) and the VM is not suspendable (similarly to the entropy device, to not interfere with the save/restore support) * "tart run --balloon-target-memory" is gone too, replaced by the menu item, which sets the balloon target to the smallest size Virtualization.Framework accepts and keeps re-applying it every 15 seconds, since a target is lost when the guest's virtio-balloon driver resets the device while booting (the same applies to guest reboots). Toggling it off deflates the balloon and gives the memory back to the guest * fixes the craftConfiguration()/buildConfiguration() split to take a VMDirectory instead of a disk URL, which stopped compiling after the stacked disk support landed in 16d186c [1]: openai#1276 (comment) Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
8de3a2e to
4d4d553
Compare
…emory Reworks the feature per the PR feedback[1]: instead of an opt-in device plus a target size that has to be picked up front, Linux guests now get a virtio memory balloon device out of the box and the "Control" menu gained a "Free Up Memory" toggle that asks the guest to shrink as much as it can. * the "memoryBalloon" setting and "tart set --memory-balloon" are gone: the device is attached whenever the guest is Linux (macOS guests show little to no practical memory reduction) and the VM is not suspendable (similarly to the entropy device, to not interfere with the save/restore support) * "tart run --balloon-target-memory" is gone too, replaced by the menu item, which sets the balloon target to the smallest size Virtualization.Framework accepts and keeps re-applying it every 15 seconds, since a target is lost when the guest's virtio-balloon driver resets the device while booting (the same applies to guest reboots). Toggling it off deflates the balloon and gives the memory back to the guest * fixes the craftConfiguration()/buildConfiguration() split to take a VMDirectory instead of a disk URL, which stopped compiling after the stacked disk support landed in 16d186c [1]: openai#1276 (comment) Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This PR adds optional support for Apple Virtualization.framework's virtio memory balloon device. When enabled, Tart attaches a
VZVirtioTraditionalMemoryBalloonDeviceConfigurationto the VM configuration, allowing supported guest operating systems to participate in best-effort memory reclaim. The feature is disabled by default and does not change existing VM behavior.Usage
What changed
VMConfiggains amemoryBalloon: Boolsetting (defaultfalse). The key is only written toconfig.jsonwhen enabled, so configurations of VMs that don't use the feature remain byte-identical to those produced by older Tart versions.tart set <vm> --memory-balloon true|falsetoggles the setting.VM.craftConfiguration()attaches aVZVirtioTraditionalMemoryBalloonDeviceConfiguration. The device is skipped for suspendable VMs (similarly to the entropy device) to not interfere with the save/restore support.tart run --balloon-target-memory <MB>setsVZVirtioTraditionalMemoryBalloonDevice.targetVirtualMachineMemorySizeat runtime, with validation: the balloon device must be enabled, the target cannot exceed the VM's configured memory size, cannot be belowVZVirtualMachineConfiguration.minimumAllowedMemorySize, and is rejected together with--suspendable.start()is lost when the guest's virtio-balloon driver resets the device during boot (the same applies to guest reboots).tart balloon <vm>command: Apple's runtime API is only reachable from the process that owns the VM, and Tart has no host-side control channel to a runningtart runprocess (the control socket proxies into the guest agent). Rather than fake it, the target is atart runoption and the docs explain why.VM.craftConfiguration()is split intobuildConfiguration()+ validation, becauseVZVirtualMachineConfiguration.validate()requires thecom.apple.security.virtualizationentitlement that unit tests don't have.docs/quick-start.md.Tests
New
MemoryBalloonTestssuite (all passing):tart set --memory-balloonargument parsing (including rejection of non-boolean values)VZVirtualMachineConfigurationwhen enabled, and never for suspendable VMsThe rest of the unit test suite passes unchanged (except the pre-existing
LayerizerTestscrash on machines without a local Docker registry, which also occurs onmain).Manual validation (ARM64 Ubuntu guest, 4096 MB VM, macOS 26 host)
virtio_balloondriver binds to the device (/sys/bus/virtio/drivers/virtio_balloonpresent).--balloon-target-memory 1024, the guest ballooned from ~3.5 GB available down to ~505 MB available within one re-apply cycle, and shutdown remained clean.Limitations
This is not dynamic memory hot-add, not transparent host memory overcommit, and not a guarantee that guests will return memory. It does not make a VM with 2 GB of real host memory appear as 32 GB to the guest — the guest still sees the configured memory size. Guest support is required, and practical results may vary.
🤖 Generated with Claude Code