[lxc-devel] [lxd/master] VM: Converts all supplied memory byte values to mebibytes for comparison

tomponline on Github lxc-bot at linuxcontainers.org
Thu Nov 5 23:04:29 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 496 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20201105/3c4ee577/attachment-0001.bin>
-------------- next part --------------
From 4682ca05c56ab2f9587f4c12038a5fb9ff1df57e Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Thu, 5 Nov 2020 23:02:50 +0000
Subject: [PATCH] lxd/instance/drivers/driver/qemu: Converts all supplied
 memory byte values to mebibytes for comparison

As we supply mebibytes to qemu for boot time memory size.

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/instance/drivers/driver_qemu.go | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/lxd/instance/drivers/driver_qemu.go b/lxd/instance/drivers/driver_qemu.go
index abfbb346b2..760c8b8378 100644
--- a/lxd/instance/drivers/driver_qemu.go
+++ b/lxd/instance/drivers/driver_qemu.go
@@ -3270,6 +3270,7 @@ func (vm *qemu) updateMemoryLimit(newLimit string) error {
 	if err != nil {
 		return errors.Wrapf(err, "Invalid memory size")
 	}
+	newSizeMB := newSizeBytes / 1024 / 1024
 
 	// Connect to the monitor.
 	monitor, err := qmp.Connect(vm.monitorPath(), qemuSerialChardevName, vm.getMonitorEventHandler())
@@ -3281,16 +3282,18 @@ func (vm *qemu) updateMemoryLimit(newLimit string) error {
 	if err != nil {
 		return err
 	}
+	baseSizeMB := baseSizeBytes / 1024 / 1024
 
 	curSizeBytes, err := monitor.GetMemoryBalloonSizeBytes()
 	if err != nil {
 		return err
 	}
+	curSizeMB := curSizeBytes / 1024 / 1024
 
-	if curSizeBytes == newSizeBytes {
+	if curSizeMB == newSizeMB {
 		return nil
-	} else if baseSizeBytes < newSizeBytes {
-		return fmt.Errorf("Cannot increase memory size beyond boot time size when VM is running")
+	} else if baseSizeMB < newSizeMB {
+		return fmt.Errorf("Cannot increase memory to size beyond boot time size when VM is running (Boot time size %dMB, new size %dMB)", baseSizeMB, newSizeMB)
 	}
 
 	// Set effective memory size.
@@ -3301,27 +3304,28 @@ func (vm *qemu) updateMemoryLimit(newLimit string) error {
 
 	// Changing the memory balloon can take time, so poll the effectice size to check it has shrunk within 1%
 	// of the target size, which we then take as success (it may still continue to shrink closer to target).
-	for i := 0; i < 5; i++ {
+	for i := 0; i < 10; i++ {
 		curSizeBytes, err = monitor.GetMemoryBalloonSizeBytes()
 		if err != nil {
 			return err
 		}
+		curSizeMB = curSizeBytes / 1024 / 1024
 
 		var diff int64
-		if curSizeBytes < newSizeBytes {
-			diff = newSizeBytes - curSizeBytes
+		if curSizeMB < newSizeMB {
+			diff = newSizeMB - curSizeMB
 		} else {
-			diff = curSizeBytes - newSizeBytes
+			diff = curSizeMB - newSizeMB
 		}
 
-		if diff <= (newSizeBytes / 100) {
+		if diff <= (newSizeMB / 100) {
 			return nil // We reached to within 1% of our target size.
 		}
 
 		time.Sleep(500 * time.Millisecond)
 	}
 
-	return fmt.Errorf("Failed setting memory to %d bytes (currently %d bytes) as it was taking too long", newSizeBytes, curSizeBytes)
+	return fmt.Errorf("Failed setting memory to %dMB (currently %dMB) as it was taking too long", newSizeMB, curSizeMB)
 }
 
 func (vm *qemu) updateDevices(removeDevices deviceConfig.Devices, addDevices deviceConfig.Devices, updateDevices deviceConfig.Devices, oldExpandedDevices deviceConfig.Devices, isRunning bool) error {


More information about the lxc-devel mailing list