Skip to content

Commit 03280f3

Browse files
authored
refactor memory alignment to VZVirtualMachineInstance (#725)
Move the MiB rounding from LinuxContainer into `VZVirtualMachineInstance`, so all callers are covered in one place and the alignment constraint stays with the VMM layer that owns it. This will also ensure LinuxPod can get memory alignment for free.
1 parent 8140499 commit 03280f3

4 files changed

Lines changed: 5 additions & 4 deletions

File tree

Sources/Containerization/LinuxContainer.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,7 @@ extension LinuxContainer {
551551
var modifiedRootfs = self.rootfs
552552
modifiedRootfs.options.removeAll(where: { $0 == "ro" })
553553

554-
let mib: UInt64 = 1.mib()
555-
let vmMemory = (self.memoryInBytes + self.config.memoryOverhead + mib - 1) & ~(mib - 1)
554+
let vmMemory = self.memoryInBytes + self.config.memoryOverhead
556555

557556
let vmCpus = self.cpus + self.config.cpuOverhead
558557

Sources/Containerization/VZVirtualMachineInstance.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@ extension VZVirtualMachineInstance.Configuration {
315315
var config = VZVirtualMachineConfiguration()
316316

317317
config.cpuCount = self.cpus
318-
config.memorySize = self.memoryInBytes
318+
let mib: UInt64 = 1 << 20
319+
config.memorySize = (self.memoryInBytes + mib - 1) & ~(mib - 1)
319320
config.entropyDevices = [VZVirtioEntropyDeviceConfiguration()]
320321
config.socketDevices = [VZVirtioSocketDeviceConfiguration()]
321322

Sources/Integration/ContainerTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ extension IntegrationSuite {
3333
let bs = try await bootstrap(id)
3434
let container = try LinuxContainer(id, rootfs: bs.rootfs, vmm: bs.vmm) { config in
3535
config.process.arguments = ["/bin/true"]
36+
config.memoryInBytes = 250_000_000
3637
config.bootLog = bs.bootLog
3738
}
3839

Sources/Integration/PodTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ extension IntegrationSuite {
495495
let bs = try await bootstrap(id)
496496
let pod = try LinuxPod(id, vmm: bs.vmm) { config in
497497
config.cpus = 4
498-
config.memoryInBytes = 1024.mib()
498+
config.memoryInBytes = 1_000_000_000
499499
config.bootLog = bs.bootLog
500500
}
501501

0 commit comments

Comments
 (0)