Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
sandbox/virtcontainers: combine addResources and updateResources
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
cedriccchen authored and linzichang committed Oct 15, 2018
1 parent 8e2ee68 commit 14f480a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 55 deletions.
67 changes: 13 additions & 54 deletions virtcontainers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -1187,50 +1187,13 @@ func (c *Container) addResources() error {
return nil
}

// Container is being created, try to add the number of vCPUs specified
vCPUs := c.config.Resources.VCPUs
if vCPUs != 0 {
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
}
addResources := ContainerResources{
VCPUs: c.config.Resources.VCPUs,
MemByte: c.config.Resources.MemByte,
}

// try to add the number of Mem specified
addMemByte := c.config.Resources.MemByte
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
}
if err := c.updateResources(ContainerResources{0, 0}, addResources); err != nil {
return err
}

return nil
Expand Down Expand Up @@ -1261,7 +1224,7 @@ func (c *Container) removeResources() error {
return nil
}

func (c *Container) updateVCPUResources(oldResources, newResources ContainerResources) error {
func (c *Container) updateVCPUResources(oldResources ContainerResources, newResources *ContainerResources) error {
var vCPUs uint32
oldVCPUs := oldResources.VCPUs
newVCPUs := newResources.VCPUs
Expand All @@ -1274,12 +1237,10 @@ func (c *Container) updateVCPUResources(oldResources, newResources ContainerReso
"new-vcpus": fmt.Sprintf("%d", newVCPUs),
}).Debug("the actual number of vCPUs will not be modified")
return nil
}

if oldVCPUs < newVCPUs {
} else if oldVCPUs < newVCPUs {
// hot add vCPUs
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)
if err != nil {
return err
Expand Down Expand Up @@ -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
newResources.VCPUs = oldVCPUs - vcpusRemoved
}

return nil
}

Expand Down Expand Up @@ -1336,9 +1298,7 @@ func (c *Container) updateMemoryResources(oldResources ContainerResources, newRe
"new-mem": fmt.Sprintf("%dByte", newMemByte),
}).Debug("the actual number of Mem will not be modified")
return nil
}

if oldMemByte < newMemByte {
} else if oldMemByte < newMemByte {
// hot add memory
addMemByte := newMemByte - oldMemByte
memHotplugMB, err := c.calcHotplugMemMiBSize(addMemByte)
Expand All @@ -1362,8 +1322,7 @@ func (c *Container) updateMemoryResources(oldResources ContainerResources, newRe
if err := c.sandbox.agent.onlineCPUMem(0, false); err != nil {
return err
}
}
if oldMemByte > newMemByte {
} else {
// Try to remove a memory device with the difference
// from new memory and old memory
removeMem := &memoryDevice{
Expand All @@ -1380,6 +1339,7 @@ func (c *Container) updateMemoryResources(oldResources ContainerResources, newRe
}
newResources.MemByte = oldMemByte - int64(memoryRemoved)<<20
}

return nil
}

Expand All @@ -1390,10 +1350,9 @@ func (c *Container) updateResources(oldResources, newResources ContainerResource

// Cpu is not updated if period and/or quota not set
if newResources.VCPUs != 0 {
if err := c.updateVCPUResources(oldResources, newResources); err != nil {
if err := c.updateVCPUResources(oldResources, &newResources); err != nil {
return err
}

// Set and save container's config VCPUs field only
c.config.Resources.VCPUs = newResources.VCPUs
if err := c.storeContainer(); err != nil {
Expand Down
4 changes: 3 additions & 1 deletion virtcontainers/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,10 @@ func TestContainerAddResources(t *testing.T) {
assert.Nil(err)

vCPUs := uint32(5)
memByte := int64(104857600)
c.config.Resources = ContainerResources{
VCPUs: vCPUs,
VCPUs: vCPUs,
MemByte: memByte,
}
c.sandbox = &Sandbox{
hypervisor: &mockHypervisor{
Expand Down
1 change: 1 addition & 0 deletions virtcontainers/pkg/oci/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ func ContainerConfig(ocispec CompatOCISpec, bundlePath, cid, console string, det
if ocispec.Linux.Resources.Memory != 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
// TODO use GetGuestDetails to get the guest OS page size.
resources.MemByte = (*ocispec.Linux.Resources.Memory.Limit >> 12) << 12
}
}
Expand Down

0 comments on commit 14f480a

Please sign in to comment.