Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apis/opts/memory_swappiness.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "fmt"

// ValidateMemorySwappiness verifies the correctness of memory-swappiness.
func ValidateMemorySwappiness(memorySwappiness int64) error {
if memorySwappiness < 0 || memorySwappiness > 100 {
if memorySwappiness != -1 && (memorySwappiness < 0 || memorySwappiness > 100) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about if (memorySwappiness < -1 || memorySwappiness > 100)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not do this, [0, 100] is a valid value, -1 is the value we enable to follow with moby.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, You are the boss :)

return fmt.Errorf("invalid memory swappiness: %d (its range is 0-100)", memorySwappiness)
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion apis/opts/memory_swappiness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestValidateMemorySwappiness(t *testing.T) {
testCases := []TestCase{
{
input: -1,
expected: fmt.Errorf("invalid memory swappiness: %d (its range is 0-100)", -1),
expected: nil,
},
{
input: 0,
Expand Down
2 changes: 1 addition & 1 deletion daemon/mgr/container_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func validateResource(r *types.Resources, update bool) ([]string, error) {
warnings = append(warnings, MemorySwappinessWarn)
r.MemorySwappiness = nil
}
if r.MemorySwappiness != nil && (*r.MemorySwappiness < 0 || *r.MemorySwappiness > 100) {
if r.MemorySwappiness != nil && *r.MemorySwappiness != -1 && (*r.MemorySwappiness < 0 || *r.MemorySwappiness > 100) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about if r.MemorySwappiness != nil (*r.MemorySwappiness < -1 || *r.MemorySwappiness > 100)

return warnings, fmt.Errorf("MemorySwappiness should in range [0, 100]")
}
if r.OomKillDisable != nil && !cgroupInfo.Memory.OOMKillDisable {
Expand Down
2 changes: 1 addition & 1 deletion daemon/mgr/spec_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func setupMemory(ctx context.Context, r types.Resources, s *specs.Spec) {
memory.Swap = &v
}

if r.MemorySwappiness != nil {
if r.MemorySwappiness != nil && *r.MemorySwappiness != -1 {
v := uint64(*r.MemorySwappiness)
memory.Swappiness = &v
}
Expand Down
20 changes: 18 additions & 2 deletions test/cli_run_memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (suite *PouchRunMemorySuite) TearDownTest(c *check.C) {
// TestRunWithMemoryswap is to verify the valid running container
// with --memory-swap
func (suite *PouchRunMemorySuite) TestRunWithMemoryswap(c *check.C) {
SkipIfFalse(c, environment.IsMemorySupport)
SkipIfFalse(c, environment.IsMemorySwapSupport)

cname := "TestRunWithMemoryswap"
Expand Down Expand Up @@ -90,8 +91,18 @@ func (suite *PouchRunMemorySuite) TestRunWithMemoryswap(c *check.C) {
// TestRunWithMemoryswappiness is to verify the valid running container
// with memory-swappiness
func (suite *PouchRunMemorySuite) TestRunWithMemoryswappiness(c *check.C) {
cname := "TestRunWithMemoryswappiness"
res := command.PouchRun("run", "-d", "-m", "100m",
SkipIfFalse(c, environment.IsMemorySupport)
SkipIfFalse(c, environment.IsMemorySwappinessSupport)

cname := "TestRunWithMemoryswappiness-1"
res := command.PouchRun("run", "-d",
"--memory-swappiness", "-1",
"--name", cname, busyboxImage, "top")
DelContainerForceMultyTime(c, cname)
res.Assert(c, icmd.Success)

cname = "TestRunWithMemoryswappiness"
res = command.PouchRun("run", "-d", "-m", "100m",
"--memory-swappiness", "70",
"--name", cname, busyboxImage, "sleep", "10000")
defer DelContainerForceMultyTime(c, cname)
Expand All @@ -117,6 +128,8 @@ func (suite *PouchRunMemorySuite) TestRunWithMemoryswappiness(c *check.C) {

// TestRunWithLimitedMemory is to verify the valid running container with -m
func (suite *PouchRunMemorySuite) TestRunWithLimitedMemory(c *check.C) {
SkipIfFalse(c, environment.IsMemorySupport)

cname := "TestRunWithLimitedMemory"
res := command.PouchRun("run", "-d", "-m", "100m",
"--name", cname, busyboxImage, "top")
Expand All @@ -143,6 +156,8 @@ func (suite *PouchRunMemorySuite) TestRunWithLimitedMemory(c *check.C) {

// TestRunMemoryOOM is to verify return value when a container is OOM.
func (suite *PouchRunMemorySuite) TestRunMemoryOOM(c *check.C) {
SkipIfFalse(c, environment.IsMemorySupport)

cname := "TestRunMemoryOOM"
ret := command.PouchRun("run", "-m", "20m", "--name", cname, busyboxImage, "sh", "-c", "x=a; while true; do x=$x$x$x$x; done")
defer DelContainerForceMultyTime(c, cname)
Expand All @@ -151,6 +166,7 @@ func (suite *PouchRunMemorySuite) TestRunMemoryOOM(c *check.C) {

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

cname := "RunWithOnlyMemorySwap"
Expand Down
10 changes: 10 additions & 0 deletions test/environment/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,20 @@ var (
var (
cgroupInfo *system.CgroupInfo

// IsMemorySupport checks if memory cgroup is avaible
IsMemorySupport = func() bool {
return cgroupInfo.Memory.MemoryLimit
}

// IsMemorySwapSupport checks if memory swap cgroup is avaible
IsMemorySwapSupport = func() bool {
return cgroupInfo.Memory.MemorySwap
}

// IsMemorySwappinessSupport checks if memory swappiness cgroup is avaible
IsMemorySwappinessSupport = func() bool {
return cgroupInfo.Memory.MemorySwappiness
}
)

func init() {
Expand Down