sandbox/virtcontainers: combine addResources and updateResources

addResources is just a special case of updateResources. Combine the shared codes
so that we do not maintain the two pieces of identical code.

Signed-off-by: Clare Chen <clare.chenhui@huawei.com>
This commit is contained in:
Clare Chen 2018-10-09 15:36:30 +08:00 committed by Zichang Lin
parent 8e2ee686bd
commit 14f480af8f
3 changed files with 17 additions and 55 deletions

View File

@ -1187,50 +1187,13 @@ func (c *Container) addResources() error {
return nil return nil
} }
// Container is being created, try to add the number of vCPUs specified addResources := ContainerResources{
vCPUs := c.config.Resources.VCPUs VCPUs: c.config.Resources.VCPUs,
if vCPUs != 0 { MemByte: c.config.Resources.MemByte,
virtLog.Debugf("create container: hot adding %d vCPUs", vCPUs)
data, err := c.sandbox.hypervisor.hotplugAddDevice(vCPUs, cpuDev)
if err != nil {
return err
}
vcpusAdded, ok := data.(uint32)
if !ok {
return fmt.Errorf("Could not get the number of vCPUs added, got %+v", data)
}
// A different number of vCPUs was added, we have to update
// the resources in order to don't remove vCPUs used by other containers.
if vcpusAdded != vCPUs {
// Set and save container's config
c.config.Resources.VCPUs = vcpusAdded
if err := c.storeContainer(); err != nil {
return err
}
}
if err := c.sandbox.agent.onlineCPUMem(vcpusAdded, true); err != nil {
return err
}
} }
// try to add the number of Mem specified if err := c.updateResources(ContainerResources{0, 0}, addResources); err != nil {
addMemByte := c.config.Resources.MemByte return err
if addMemByte != 0 {
memHotplugMB, err := c.calcHotplugMemMiBSize(addMemByte)
if err != nil {
return err
}
virtLog.Debugf("create container: hotplug %dMB mem", memHotplugMB)
_, err = c.sandbox.hypervisor.hotplugAddDevice(&memoryDevice{sizeMB: int(memHotplugMB)}, memoryDev)
if err != nil {
return err
}
if err := c.sandbox.agent.onlineCPUMem(0, false); err != nil {
return err
}
} }
return nil return nil
@ -1261,7 +1224,7 @@ func (c *Container) removeResources() error {
return nil return nil
} }
func (c *Container) updateVCPUResources(oldResources, newResources ContainerResources) error { func (c *Container) updateVCPUResources(oldResources ContainerResources, newResources *ContainerResources) error {
var vCPUs uint32 var vCPUs uint32
oldVCPUs := oldResources.VCPUs oldVCPUs := oldResources.VCPUs
newVCPUs := newResources.VCPUs newVCPUs := newResources.VCPUs
@ -1274,12 +1237,10 @@ func (c *Container) updateVCPUResources(oldResources, newResources ContainerReso
"new-vcpus": fmt.Sprintf("%d", newVCPUs), "new-vcpus": fmt.Sprintf("%d", newVCPUs),
}).Debug("the actual number of vCPUs will not be modified") }).Debug("the actual number of vCPUs will not be modified")
return nil return nil
} } else if oldVCPUs < newVCPUs {
if oldVCPUs < newVCPUs {
// hot add vCPUs // hot add vCPUs
vCPUs = newVCPUs - oldVCPUs vCPUs = newVCPUs - oldVCPUs
virtLog.Debugf("update container: hot adding %d vCPUs", vCPUs) virtLog.Debugf("hot adding %d vCPUs", vCPUs)
data, err := c.sandbox.hypervisor.hotplugAddDevice(vCPUs, cpuDev) data, err := c.sandbox.hypervisor.hotplugAddDevice(vCPUs, cpuDev)
if err != nil { if err != nil {
return err return err
@ -1308,6 +1269,7 @@ func (c *Container) updateVCPUResources(oldResources, newResources ContainerReso
// recalculate the actual number of vCPUs if a different number of vCPUs was removed // recalculate the actual number of vCPUs if a different number of vCPUs was removed
newResources.VCPUs = oldVCPUs - vcpusRemoved newResources.VCPUs = oldVCPUs - vcpusRemoved
} }
return nil return nil
} }
@ -1336,9 +1298,7 @@ func (c *Container) updateMemoryResources(oldResources ContainerResources, newRe
"new-mem": fmt.Sprintf("%dByte", newMemByte), "new-mem": fmt.Sprintf("%dByte", newMemByte),
}).Debug("the actual number of Mem will not be modified") }).Debug("the actual number of Mem will not be modified")
return nil return nil
} } else if oldMemByte < newMemByte {
if oldMemByte < newMemByte {
// hot add memory // hot add memory
addMemByte := newMemByte - oldMemByte addMemByte := newMemByte - oldMemByte
memHotplugMB, err := c.calcHotplugMemMiBSize(addMemByte) memHotplugMB, err := c.calcHotplugMemMiBSize(addMemByte)
@ -1362,8 +1322,7 @@ func (c *Container) updateMemoryResources(oldResources ContainerResources, newRe
if err := c.sandbox.agent.onlineCPUMem(0, false); err != nil { if err := c.sandbox.agent.onlineCPUMem(0, false); err != nil {
return err return err
} }
} } else {
if oldMemByte > newMemByte {
// Try to remove a memory device with the difference // Try to remove a memory device with the difference
// from new memory and old memory // from new memory and old memory
removeMem := &memoryDevice{ removeMem := &memoryDevice{
@ -1380,6 +1339,7 @@ func (c *Container) updateMemoryResources(oldResources ContainerResources, newRe
} }
newResources.MemByte = oldMemByte - int64(memoryRemoved)<<20 newResources.MemByte = oldMemByte - int64(memoryRemoved)<<20
} }
return nil return nil
} }
@ -1390,10 +1350,9 @@ func (c *Container) updateResources(oldResources, newResources ContainerResource
// Cpu is not updated if period and/or quota not set // Cpu is not updated if period and/or quota not set
if newResources.VCPUs != 0 { if newResources.VCPUs != 0 {
if err := c.updateVCPUResources(oldResources, newResources); err != nil { if err := c.updateVCPUResources(oldResources, &newResources); err != nil {
return err return err
} }
// Set and save container's config VCPUs field only // Set and save container's config VCPUs field only
c.config.Resources.VCPUs = newResources.VCPUs c.config.Resources.VCPUs = newResources.VCPUs
if err := c.storeContainer(); err != nil { if err := c.storeContainer(); err != nil {

View File

@ -320,8 +320,10 @@ func TestContainerAddResources(t *testing.T) {
assert.Nil(err) assert.Nil(err)
vCPUs := uint32(5) vCPUs := uint32(5)
memByte := int64(104857600)
c.config.Resources = ContainerResources{ c.config.Resources = ContainerResources{
VCPUs: vCPUs, VCPUs: vCPUs,
MemByte: memByte,
} }
c.sandbox = &Sandbox{ c.sandbox = &Sandbox{
hypervisor: &mockHypervisor{ hypervisor: &mockHypervisor{

View File

@ -547,6 +547,7 @@ func ContainerConfig(ocispec CompatOCISpec, bundlePath, cid, console string, det
if ocispec.Linux.Resources.Memory != nil { if ocispec.Linux.Resources.Memory != nil {
if ocispec.Linux.Resources.Memory.Limit != nil { if ocispec.Linux.Resources.Memory.Limit != nil {
// do page align to memory, as cgroup memory.limit_in_bytes will be aligned to page when effect // do page align to memory, as cgroup memory.limit_in_bytes will be aligned to page when effect
// TODO use GetGuestDetails to get the guest OS page size.
resources.MemByte = (*ocispec.Linux.Resources.Memory.Limit >> 12) << 12 resources.MemByte = (*ocispec.Linux.Resources.Memory.Limit >> 12) << 12
} }
} }