runtime: Update calculateSandboxMemory to include Hugepages Limit

Support hugepages and port from:
96dbb2e8f0

Fixes: #3342

Signed-off-by: James O. D. Hunt <james.o.hunt@intel.com>
Signed-off-by: Pradipta Banerjee <pradipta.banerjee@gmail.com>
Signed-off-by: bin <bin@hyper.sh>
This commit is contained in:
bin 2021-12-23 13:26:40 +08:00
parent 1dcb413e68
commit 7df677c01e
2 changed files with 62 additions and 10 deletions

View File

@ -1942,11 +1942,13 @@ func (s *Sandbox) updateResources(ctx context.Context) error {
sandboxVCPUs += s.hypervisor.HypervisorConfig().NumVCPUs
sandboxMemoryByte, sandboxneedPodSwap, sandboxSwapByte := s.calculateSandboxMemory()
// Add default / rsvd memory for sandbox.
hypervisorMemoryByte := int64(s.hypervisor.HypervisorConfig().MemorySize) << utils.MibToBytesShift
hypervisorMemoryByteI64 := int64(s.hypervisor.HypervisorConfig().MemorySize) << utils.MibToBytesShift
hypervisorMemoryByte := uint64(hypervisorMemoryByteI64)
sandboxMemoryByte += hypervisorMemoryByte
if sandboxneedPodSwap {
sandboxSwapByte += hypervisorMemoryByte
sandboxSwapByte += hypervisorMemoryByteI64
}
s.Logger().WithField("sandboxMemoryByte", sandboxMemoryByte).WithField("sandboxneedPodSwap", sandboxneedPodSwap).WithField("sandboxSwapByte", sandboxSwapByte).Debugf("updateResources: after calculateSandboxMemory")
@ -1978,7 +1980,8 @@ func (s *Sandbox) updateResources(ctx context.Context) error {
// Update Memory
s.Logger().WithField("memory-sandbox-size-byte", sandboxMemoryByte).Debugf("Request to hypervisor to update memory")
newMemory, updatedMemoryDevice, err := s.hypervisor.ResizeMemory(ctx, uint32(sandboxMemoryByte>>utils.MibToBytesShift), s.state.GuestMemoryBlockSizeMB, s.state.GuestMemoryHotplugProbe)
newMemoryMB := uint32(sandboxMemoryByte >> utils.MibToBytesShift)
newMemory, updatedMemoryDevice, err := s.hypervisor.ResizeMemory(ctx, newMemoryMB, s.state.GuestMemoryBlockSizeMB, s.state.GuestMemoryHotplugProbe)
if err != nil {
if err == noGuestMemHotplugErr {
s.Logger().Warnf("%s, memory specifications cannot be guaranteed", err)
@ -2001,8 +2004,8 @@ func (s *Sandbox) updateResources(ctx context.Context) error {
return nil
}
func (s *Sandbox) calculateSandboxMemory() (int64, bool, int64) {
memorySandbox := int64(0)
func (s *Sandbox) calculateSandboxMemory() (uint64, bool, int64) {
memorySandbox := uint64(0)
needPodSwap := false
swapSandbox := int64(0)
for _, c := range s.config.Containers {
@ -2016,8 +2019,17 @@ func (s *Sandbox) calculateSandboxMemory() (int64, bool, int64) {
currentLimit := int64(0)
if m.Limit != nil && *m.Limit > 0 {
currentLimit = *m.Limit
memorySandbox += currentLimit
memorySandbox += uint64(currentLimit)
s.Logger().WithField("memory limit", memorySandbox).Info("Memory Sandbox + Memory Limit ")
}
// Add hugepages memory
// HugepageLimit is uint64 - https://github.com/opencontainers/runtime-spec/blob/master/specs-go/config.go#L242
for _, l := range c.Resources.HugepageLimits {
memorySandbox += l.Limit
}
// Add swap
if s.config.HypervisorConfig.GuestSwap && m.Swappiness != nil && *m.Swappiness > 0 {
currentSwap := int64(0)
if m.Swap != nil {
@ -2035,6 +2047,7 @@ func (s *Sandbox) calculateSandboxMemory() (int64, bool, int64) {
}
}
}
return memorySandbox, needPodSwap, swapSandbox
}

View File

@ -152,13 +152,14 @@ func TestCalculateSandboxMem(t *testing.T) {
sandbox.config = &SandboxConfig{}
unconstrained := newTestContainerConfigNoop("cont-00001")
constrained := newTestContainerConfigNoop("cont-00001")
limit := int64(4000)
constrained.Resources.Memory = &specs.LinuxMemory{Limit: &limit}
mlimit := int64(4000)
limit := uint64(4000)
constrained.Resources.Memory = &specs.LinuxMemory{Limit: &mlimit}
tests := []struct {
name string
containers []ContainerConfig
want int64
want uint64
}{
{"1-unconstrained", []ContainerConfig{unconstrained}, 0},
{"2-unconstrained", []ContainerConfig{unconstrained, unconstrained}, 0},
@ -187,7 +188,7 @@ func TestCalculateSandboxMemHandlesNegativeLimits(t *testing.T) {
sandbox.config.Containers = []ContainerConfig{container}
mem, needSwap, swap := sandbox.calculateSandboxMemory()
assert.Equal(t, mem, int64(0))
assert.Equal(t, mem, uint64(0))
assert.Equal(t, needSwap, false)
assert.Equal(t, swap, int64(0))
}
@ -1639,3 +1640,41 @@ func TestGetSandboxCpuSet(t *testing.T) {
})
}
}
func TestSandboxHugepageLimit(t *testing.T) {
contConfig1 := newTestContainerConfigNoop("cont-00001")
contConfig2 := newTestContainerConfigNoop("cont-00002")
limit := int64(4000)
contConfig1.Resources.Memory = &specs.LinuxMemory{Limit: &limit}
contConfig2.Resources.Memory = &specs.LinuxMemory{Limit: &limit}
hConfig := newHypervisorConfig(nil, nil)
defer cleanUp()
// create a sandbox
s, err := testCreateSandbox(t,
testSandboxID,
MockHypervisor,
hConfig,
NetworkConfig{},
[]ContainerConfig{contConfig1, contConfig2},
nil)
assert.NoError(t, err)
hugepageLimits := []specs.LinuxHugepageLimit{
{
Pagesize: "1GB",
Limit: 322122547,
},
{
Pagesize: "2MB",
Limit: 134217728,
},
}
for i := range s.config.Containers {
s.config.Containers[i].Resources.HugepageLimits = hugepageLimits
}
err = s.updateResources(context.Background())
assert.NoError(t, err)
}