Skip to content

Commit 6fd49d0

Browse files
authored
Merge pull request #2092 from Ace-Tang/mem_swap
cgroup: enable memory swappiness equal to -1
2 parents 8c3a539 + 4ba79ca commit 6fd49d0

File tree

6 files changed

+32
-6
lines changed

6 files changed

+32
-6
lines changed

apis/opts/memory_swappiness.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import "fmt"
66

77
// ValidateMemorySwappiness verifies the correctness of memory-swappiness.
88
func ValidateMemorySwappiness(memorySwappiness int64) error {
9-
if memorySwappiness < 0 || memorySwappiness > 100 {
9+
if memorySwappiness != -1 && (memorySwappiness < 0 || memorySwappiness > 100) {
1010
return fmt.Errorf("invalid memory swappiness: %d (its range is 0-100)", memorySwappiness)
1111
}
1212
return nil

apis/opts/memory_swappiness_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestValidateMemorySwappiness(t *testing.T) {
1616
testCases := []TestCase{
1717
{
1818
input: -1,
19-
expected: fmt.Errorf("invalid memory swappiness: %d (its range is 0-100)", -1),
19+
expected: nil,
2020
},
2121
{
2222
input: 0,

daemon/mgr/container_validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func validateResource(r *types.Resources, update bool) ([]string, error) {
100100
warnings = append(warnings, MemorySwappinessWarn)
101101
r.MemorySwappiness = nil
102102
}
103-
if r.MemorySwappiness != nil && (*r.MemorySwappiness < 0 || *r.MemorySwappiness > 100) {
103+
if r.MemorySwappiness != nil && *r.MemorySwappiness != -1 && (*r.MemorySwappiness < 0 || *r.MemorySwappiness > 100) {
104104
return warnings, fmt.Errorf("MemorySwappiness should in range [0, 100]")
105105
}
106106
if r.OomKillDisable != nil && !cgroupInfo.Memory.OOMKillDisable {

daemon/mgr/spec_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ func setupMemory(ctx context.Context, r types.Resources, s *specs.Spec) {
261261
memory.Swap = &v
262262
}
263263

264-
if r.MemorySwappiness != nil {
264+
if r.MemorySwappiness != nil && *r.MemorySwappiness != -1 {
265265
v := uint64(*r.MemorySwappiness)
266266
memory.Swappiness = &v
267267
}

test/cli_run_memory_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func (suite *PouchRunMemorySuite) TearDownTest(c *check.C) {
3636
// TestRunWithMemoryswap is to verify the valid running container
3737
// with --memory-swap
3838
func (suite *PouchRunMemorySuite) TestRunWithMemoryswap(c *check.C) {
39+
SkipIfFalse(c, environment.IsMemorySupport)
3940
SkipIfFalse(c, environment.IsMemorySwapSupport)
4041

4142
cname := "TestRunWithMemoryswap"
@@ -90,8 +91,18 @@ func (suite *PouchRunMemorySuite) TestRunWithMemoryswap(c *check.C) {
9091
// TestRunWithMemoryswappiness is to verify the valid running container
9192
// with memory-swappiness
9293
func (suite *PouchRunMemorySuite) TestRunWithMemoryswappiness(c *check.C) {
93-
cname := "TestRunWithMemoryswappiness"
94-
res := command.PouchRun("run", "-d", "-m", "100m",
94+
SkipIfFalse(c, environment.IsMemorySupport)
95+
SkipIfFalse(c, environment.IsMemorySwappinessSupport)
96+
97+
cname := "TestRunWithMemoryswappiness-1"
98+
res := command.PouchRun("run", "-d",
99+
"--memory-swappiness", "-1",
100+
"--name", cname, busyboxImage, "top")
101+
DelContainerForceMultyTime(c, cname)
102+
res.Assert(c, icmd.Success)
103+
104+
cname = "TestRunWithMemoryswappiness"
105+
res = command.PouchRun("run", "-d", "-m", "100m",
95106
"--memory-swappiness", "70",
96107
"--name", cname, busyboxImage, "sleep", "10000")
97108
defer DelContainerForceMultyTime(c, cname)
@@ -117,6 +128,8 @@ func (suite *PouchRunMemorySuite) TestRunWithMemoryswappiness(c *check.C) {
117128

118129
// TestRunWithLimitedMemory is to verify the valid running container with -m
119130
func (suite *PouchRunMemorySuite) TestRunWithLimitedMemory(c *check.C) {
131+
SkipIfFalse(c, environment.IsMemorySupport)
132+
120133
cname := "TestRunWithLimitedMemory"
121134
res := command.PouchRun("run", "-d", "-m", "100m",
122135
"--name", cname, busyboxImage, "top")
@@ -143,6 +156,8 @@ func (suite *PouchRunMemorySuite) TestRunWithLimitedMemory(c *check.C) {
143156

144157
// TestRunMemoryOOM is to verify return value when a container is OOM.
145158
func (suite *PouchRunMemorySuite) TestRunMemoryOOM(c *check.C) {
159+
SkipIfFalse(c, environment.IsMemorySupport)
160+
146161
cname := "TestRunMemoryOOM"
147162
ret := command.PouchRun("run", "-m", "20m", "--name", cname, busyboxImage, "sh", "-c", "x=a; while true; do x=$x$x$x$x; done")
148163
defer DelContainerForceMultyTime(c, cname)
@@ -151,6 +166,7 @@ func (suite *PouchRunMemorySuite) TestRunMemoryOOM(c *check.C) {
151166

152167
// TestRunWithMemoryFlag test pouch run with memory flags
153168
func (suite *PouchRunSuite) TestRunWithMemoryFlag(c *check.C) {
169+
SkipIfFalse(c, environment.IsMemorySupport)
154170
SkipIfFalse(c, environment.IsMemorySwapSupport)
155171

156172
cname := "RunWithOnlyMemorySwap"

test/environment/env.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,20 @@ var (
4848
var (
4949
cgroupInfo *system.CgroupInfo
5050

51+
// IsMemorySupport checks if memory cgroup is avaible
52+
IsMemorySupport = func() bool {
53+
return cgroupInfo.Memory.MemoryLimit
54+
}
55+
5156
// IsMemorySwapSupport checks if memory swap cgroup is avaible
5257
IsMemorySwapSupport = func() bool {
5358
return cgroupInfo.Memory.MemorySwap
5459
}
60+
61+
// IsMemorySwappinessSupport checks if memory swappiness cgroup is avaible
62+
IsMemorySwappinessSupport = func() bool {
63+
return cgroupInfo.Memory.MemorySwappiness
64+
}
5565
)
5666

5767
func init() {

0 commit comments

Comments
 (0)