Skip to content

Commit 96dbb2e

Browse files
committed
Update calculateSandboxMemory to include Hugepages Limit
Signed-off-by: James O. D. Hunt <[email protected]> Signed-off-by: Pradipta Banerjee <[email protected]>
1 parent 2a98f43 commit 96dbb2e

File tree

2 files changed

+60
-9
lines changed

2 files changed

+60
-9
lines changed

virtcontainers/sandbox.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,8 +1873,9 @@ func (s *Sandbox) updateResources() error {
18731873
sandboxVCPUs += s.hypervisor.hypervisorConfig().NumVCPUs
18741874

18751875
sandboxMemoryByte := s.calculateSandboxMemory()
1876+
18761877
// Add default / rsvd memory for sandbox.
1877-
sandboxMemoryByte += int64(s.hypervisor.hypervisorConfig().MemorySize) << utils.MibToBytesShift
1878+
sandboxMemoryByte += uint64(s.hypervisor.hypervisorConfig().MemorySize) << utils.MibToBytesShift
18781879

18791880
// Update VCPUs
18801881
s.Logger().WithField("cpus-sandbox", sandboxVCPUs).Debugf("Request to hypervisor to update vCPUs")
@@ -1894,7 +1895,9 @@ func (s *Sandbox) updateResources() error {
18941895

18951896
// Update Memory
18961897
s.Logger().WithField("memory-sandbox-size-byte", sandboxMemoryByte).Debugf("Request to hypervisor to update memory")
1897-
newMemory, updatedMemoryDevice, err := s.hypervisor.resizeMemory(uint32(sandboxMemoryByte>>utils.MibToBytesShift), s.state.GuestMemoryBlockSizeMB, s.state.GuestMemoryHotplugProbe)
1898+
newMemoryMB := uint32(sandboxMemoryByte >> utils.MibToBytesShift)
1899+
1900+
newMemory, updatedMemoryDevice, err := s.hypervisor.resizeMemory(newMemoryMB, s.state.GuestMemoryBlockSizeMB, s.state.GuestMemoryHotplugProbe)
18981901
if err != nil {
18991902
return err
19001903
}
@@ -1912,19 +1915,27 @@ func (s *Sandbox) updateResources() error {
19121915
return nil
19131916
}
19141917

1915-
func (s *Sandbox) calculateSandboxMemory() int64 {
1916-
memorySandbox := int64(0)
1918+
func (s *Sandbox) calculateSandboxMemory() uint64 {
1919+
memorySandbox := uint64(0)
19171920
for _, c := range s.config.Containers {
19181921
// Do not hot add again non-running containers resources
19191922
if cont, ok := s.containers[c.ID]; ok && cont.state.State == types.StateStopped {
19201923
s.Logger().WithField("container-id", c.ID).Debug("Do not taking into account memory resources of not running containers")
19211924
continue
19221925
}
19231926

1924-
if m := c.Resources.Memory; m != nil && m.Limit != nil {
1925-
memorySandbox += *m.Limit
1927+
if m := c.Resources.Memory; m != nil && m.Limit != nil && *m.Limit > 0 {
1928+
memorySandbox += uint64(*m.Limit)
1929+
s.Logger().WithField("memory limit", memorySandbox).Info("Memory Sandbox + Memory Limit ")
1930+
}
1931+
1932+
//Add hugepages memory
1933+
//HugepageLimit is uint64 - https://github.com/opencontainers/runtime-spec/blob/master/specs-go/config.go#L242
1934+
for _, l := range c.Resources.HugepageLimits {
1935+
memorySandbox += uint64(l.Limit)
19261936
}
19271937
}
1938+
19281939
return memorySandbox
19291940
}
19301941

virtcontainers/sandbox_test.go

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,14 @@ func TestCalculateSandboxMem(t *testing.T) {
149149
sandbox.config = &SandboxConfig{}
150150
unconstrained := newTestContainerConfigNoop("cont-00001")
151151
constrained := newTestContainerConfigNoop("cont-00001")
152-
limit := int64(4000)
153-
constrained.Resources.Memory = &specs.LinuxMemory{Limit: &limit}
152+
mlimit := int64(4000)
153+
limit := uint64(4000)
154+
constrained.Resources.Memory = &specs.LinuxMemory{Limit: &mlimit}
154155

155156
tests := []struct {
156157
name string
157158
containers []ContainerConfig
158-
want int64
159+
want uint64
159160
}{
160161
{"1-unconstrained", []ContainerConfig{unconstrained}, 0},
161162
{"2-unconstrained", []ContainerConfig{unconstrained, unconstrained}, 0},
@@ -173,6 +174,45 @@ func TestCalculateSandboxMem(t *testing.T) {
173174
}
174175
}
175176

177+
func TestSandboxHugepageLimit(t *testing.T) {
178+
contConfig1 := newTestContainerConfigNoop("cont-00001")
179+
contConfig2 := newTestContainerConfigNoop("cont-00002")
180+
limit := int64(4000)
181+
contConfig1.Resources.Memory = &specs.LinuxMemory{Limit: &limit}
182+
contConfig2.Resources.Memory = &specs.LinuxMemory{Limit: &limit}
183+
hConfig := newHypervisorConfig(nil, nil)
184+
185+
defer cleanUp()
186+
// create a sandbox
187+
s, err := testCreateSandbox(t,
188+
testSandboxID,
189+
MockHypervisor,
190+
hConfig,
191+
NoopAgentType,
192+
NetworkConfig{},
193+
[]ContainerConfig{contConfig1, contConfig2},
194+
nil)
195+
196+
assert.NoError(t, err)
197+
198+
hugepageLimits := []specs.LinuxHugepageLimit{
199+
{
200+
Pagesize: "1GB",
201+
Limit: 322122547,
202+
},
203+
{
204+
Pagesize: "2MB",
205+
Limit: 134217728,
206+
},
207+
}
208+
209+
for i := range s.config.Containers {
210+
s.config.Containers[i].Resources.HugepageLimits = hugepageLimits
211+
}
212+
err = s.updateResources()
213+
assert.NoError(t, err)
214+
}
215+
176216
func TestCreateSandboxEmptyID(t *testing.T) {
177217
hConfig := newHypervisorConfig(nil, nil)
178218
_, err := testCreateSandbox(t, "", MockHypervisor, hConfig, NoopAgentType, NetworkConfig{}, nil, nil)

0 commit comments

Comments
 (0)