Commit 61eb1a46 authored by Guido Trotter's avatar Guido Trotter
Browse files

hypervisors: start instances with max available mem



If we have available more than MINMEM but less than MAXMEM, start a new
instance with that value, rather than MAXMEM.
Signed-off-by: default avatarGuido Trotter <ultrotter@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent 5d1ab3a6
......@@ -361,6 +361,24 @@ class BaseHypervisor(object):
"""
raise NotImplementedError
def _InstanceStartupMemory(self, instance):
"""Get the correct startup memory for an instance
This function calculates how much memory an instance should be started
with, making sure it's a value between the minimum and the maximum memory,
but also trying to use no more than the current free memory on the node.
@type instance: L{objects.Instance}
@param instance: the instance that is being started
@rtype: integer
@return: memory the instance should be started with
"""
free_memory = self.GetNodeInfo()["memory_free"]
max_start_mem = min(instance.beparams[constants.BE_MAXMEM], free_memory)
start_mem = max(instance.beparams[constants.BE_MINMEM], max_start_mem)
return start_mem
@classmethod
def CheckParameterSyntax(cls, hvparams):
"""Check the given parameters for validity.
......
......@@ -159,7 +159,7 @@ class FakeHypervisor(hv_base.BaseHypervisor):
raise errors.HypervisorError("Failed to start instance %s: %s" %
(instance.name, "already running"))
try:
self._MarkUp(instance, instance.beparams[constants.BE_MAXMEM])
self._MarkUp(instance, self._InstanceStartupMemory(instance))
except IOError, err:
raise errors.HypervisorError("Failed to start instance %s: %s" %
(instance.name, err))
......@@ -291,7 +291,7 @@ class FakeHypervisor(hv_base.BaseHypervisor):
"""
if success:
self._MarkUp(instance, instance.beparams[constants.BE_MAXMEM])
self._MarkUp(instance, self._InstanceStartupMemory(instance))
else:
# ensure it's down
self._MarkDown(instance.name)
......
......@@ -1486,6 +1486,10 @@ class KVMHypervisor(hv_base.BaseHypervisor):
if cpu_pinning:
self._ExecuteCpuAffinity(instance.name, up_hvp[constants.HV_CPU_MASK])
start_memory = self._InstanceStartupMemory(instance)
if start_memory < instance.beparams[constants.BE_MAXMEM]:
self.BalloonInstanceMemory(instance, start_memory)
if start_kvm_paused:
# To control CPU pinning, ballooning, and vnc/spice passwords the VM was
# started in a frozen state. If freezing was not explicitely requested
......
......@@ -73,7 +73,7 @@ class XenHypervisor(hv_base.BaseHypervisor):
return "/etc/xen/%s" % instance_name
@classmethod
def _WriteConfigFile(cls, instance, block_devices):
def _WriteConfigFile(cls, instance, startup_memory, block_devices):
"""Write the Xen config file for the instance.
"""
......@@ -239,7 +239,8 @@ class XenHypervisor(hv_base.BaseHypervisor):
"""Start an instance.
"""
self._WriteConfigFile(instance, block_devices)
startup_memory = self._InstanceStartupMemory(instance)
self._WriteConfigFile(instance, startup_memory, block_devices)
cmd = [constants.XEN_CMD, "create"]
if startup_paused:
cmd.extend(["-p"])
......@@ -617,7 +618,7 @@ class XenPvmHypervisor(XenHypervisor):
}
@classmethod
def _WriteConfigFile(cls, instance, block_devices):
def _WriteConfigFile(cls, instance, startup_memory, block_devices):
"""Write the Xen config file for the instance.
"""
......@@ -650,8 +651,7 @@ class XenPvmHypervisor(XenHypervisor):
config.write("ramdisk = '%s'\n" % initrd_path)
# rest of the settings
# TODO(dynmem): use actual chosen memory for instance startup
config.write("memory = %d\n" % instance.beparams[constants.BE_MAXMEM])
config.write("memory = %d\n" % startup_memory)
config.write("maxmem = %d\n" % instance.beparams[constants.BE_MAXMEM])
config.write("vcpus = %d\n" % instance.beparams[constants.BE_VCPUS])
cpu_pinning = cls._CreateConfigCpus(hvp[constants.HV_CPU_MASK])
......@@ -737,7 +737,7 @@ class XenHvmHypervisor(XenHypervisor):
}
@classmethod
def _WriteConfigFile(cls, instance, block_devices):
def _WriteConfigFile(cls, instance, startup_memory, block_devices):
"""Create a Xen 3.1 HVM config file.
"""
......@@ -751,8 +751,7 @@ class XenHvmHypervisor(XenHypervisor):
config.write("kernel = '%s'\n" % kpath)
config.write("builder = 'hvm'\n")
# TODO(dynmem): use actual chosen memory for instance startup
config.write("memory = %d\n" % instance.beparams[constants.BE_MAXMEM])
config.write("memory = %d\n" % startup_memory)
config.write("maxmem = %d\n" % instance.beparams[constants.BE_MAXMEM])
config.write("vcpus = %d\n" % instance.beparams[constants.BE_VCPUS])
cpu_pinning = cls._CreateConfigCpus(hvp[constants.HV_CPU_MASK])
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment