From 06f0d03cede5f6cdaca87f6b786555b023d5286f Mon Sep 17 00:00:00 2001 From: Antonio Murdaca Date: Sun, 20 Sep 2015 17:29:41 +0200 Subject: [PATCH] daemon: execdriver: lxc: fix set memory swap On LXC memory swap was only set to memory_limit*2 even if a value for memory swap was provided. This patch fix this behavior to be the same as the native driver and set correct memory swap in the template. Also add a test specifically for LXC but w/o adding a new test requirement. Signed-off-by: Antonio Murdaca --- daemon/execdriver/lxc/lxc_template.go | 14 ++----------- .../execdriver/lxc/lxc_template_unit_test.go | 8 +++++--- integration-cli/docker_cli_run_unix_test.go | 20 +++++++++++++++++++ 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/daemon/execdriver/lxc/lxc_template.go b/daemon/execdriver/lxc/lxc_template.go index 975c5f19e0..6b7996f04c 100644 --- a/daemon/execdriver/lxc/lxc_template.go +++ b/daemon/execdriver/lxc/lxc_template.go @@ -91,9 +91,9 @@ lxc.mount.entry = {{$value.Source}} {{escapeFstabSpaces $ROOTFS}}/{{escapeFstabS {{if .Resources}} {{if .Resources.Memory}} lxc.cgroup.memory.limit_in_bytes = {{.Resources.Memory}} -{{with $memSwap := getMemorySwap .Resources}} -lxc.cgroup.memory.memsw.limit_in_bytes = {{$memSwap}} {{end}} +{{if gt .Resources.MemorySwap 0}} +lxc.cgroup.memory.memsw.limit_in_bytes = {{.Resources.MemorySwap}} {{end}} {{if gt .Resources.MemoryReservation 0}} lxc.cgroup.memory.soft_limit_in_bytes = {{.Resources.MemoryReservation}} @@ -209,15 +209,6 @@ func isDirectory(source string) string { return "file" } -func getMemorySwap(v *execdriver.Resources) int64 { - // By default, MemorySwap is set to twice the size of RAM. - // If you want to omit MemorySwap, set it to `-1'. - if v.MemorySwap < 0 { - return 0 - } - return v.Memory * 2 -} - func getLabel(c map[string][]string, name string) string { label := c["label"] for _, l := range label { @@ -242,7 +233,6 @@ func getHostname(env []string) string { func init() { var err error funcMap := template.FuncMap{ - "getMemorySwap": getMemorySwap, "escapeFstabSpaces": escapeFstabSpaces, "formatMountLabel": label.FormatMountLabel, "isDirectory": isDirectory, diff --git a/daemon/execdriver/lxc/lxc_template_unit_test.go b/daemon/execdriver/lxc/lxc_template_unit_test.go index afb5b1eb6b..01bc3eaef3 100644 --- a/daemon/execdriver/lxc/lxc_template_unit_test.go +++ b/daemon/execdriver/lxc/lxc_template_unit_test.go @@ -34,6 +34,7 @@ func TestLXCConfig(t *testing.T) { memMin = 33554432 memMax = 536870912 mem = memMin + r.Intn(memMax-memMin) + swap = memMax cpuMin = 100 cpuMax = 10000 cpu = cpuMin + r.Intn(cpuMax-cpuMin) @@ -46,8 +47,9 @@ func TestLXCConfig(t *testing.T) { command := &execdriver.Command{ ID: "1", Resources: &execdriver.Resources{ - Memory: int64(mem), - CPUShares: int64(cpu), + Memory: int64(mem), + MemorySwap: int64(swap), + CPUShares: int64(cpu), }, Network: &execdriver.Network{ Mtu: 1500, @@ -63,7 +65,7 @@ func TestLXCConfig(t *testing.T) { fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem)) grepFile(t, p, - fmt.Sprintf("lxc.cgroup.memory.memsw.limit_in_bytes = %d", mem*2)) + fmt.Sprintf("lxc.cgroup.memory.memsw.limit_in_bytes = %d", swap)) } func TestCustomLxcConfig(t *testing.T) { diff --git a/integration-cli/docker_cli_run_unix_test.go b/integration-cli/docker_cli_run_unix_test.go index 785aad4108..e062a0830e 100644 --- a/integration-cli/docker_cli_run_unix_test.go +++ b/integration-cli/docker_cli_run_unix_test.go @@ -17,6 +17,7 @@ import ( "github.com/docker/docker/pkg/mount" "github.com/docker/docker/pkg/parsers" "github.com/docker/docker/pkg/sysinfo" + "github.com/docker/docker/pkg/units" "github.com/go-check/check" "github.com/kr/pty" ) @@ -435,3 +436,22 @@ func (s *DockerSuite) TestRunInvalidCPUShares(c *check.C) { expected = "The maximum allowed cpu-shares is" c.Assert(out, checker.Contains, expected) } + +func (s *DockerSuite) TestRunWithCorrectMemorySwapOnLXC(c *check.C) { + testRequires(c, memoryLimitSupport) + testRequires(c, swapMemorySupport) + testRequires(c, SameHostDaemon) + + out, _ := dockerCmd(c, "run", "-d", "-m", "16m", "--memory-swap", "64m", "busybox", "top") + if _, err := os.Stat("/sys/fs/cgroup/memory/lxc"); err != nil { + c.Skip("Excecution driver must be LXC for this test") + } + id := strings.TrimSpace(out) + memorySwap, err := ioutil.ReadFile(fmt.Sprintf("/sys/fs/cgroup/memory/lxc/%s/memory.memsw.limit_in_bytes", id)) + c.Assert(err, check.IsNil) + cgSwap, err := strconv.ParseInt(strings.TrimSpace(string(memorySwap)), 10, 64) + c.Assert(err, check.IsNil) + swap, err := units.RAMInBytes("64m") + c.Assert(err, check.IsNil) + c.Assert(cgSwap, check.Equals, swap) +}