mirror of https://github.com/docker/docs.git
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 <runcom@linux.com>
This commit is contained in:
parent
4ea3ff7061
commit
06f0d03ced
|
@ -91,9 +91,9 @@ lxc.mount.entry = {{$value.Source}} {{escapeFstabSpaces $ROOTFS}}/{{escapeFstabS
|
||||||
{{if .Resources}}
|
{{if .Resources}}
|
||||||
{{if .Resources.Memory}}
|
{{if .Resources.Memory}}
|
||||||
lxc.cgroup.memory.limit_in_bytes = {{.Resources.Memory}}
|
lxc.cgroup.memory.limit_in_bytes = {{.Resources.Memory}}
|
||||||
{{with $memSwap := getMemorySwap .Resources}}
|
|
||||||
lxc.cgroup.memory.memsw.limit_in_bytes = {{$memSwap}}
|
|
||||||
{{end}}
|
{{end}}
|
||||||
|
{{if gt .Resources.MemorySwap 0}}
|
||||||
|
lxc.cgroup.memory.memsw.limit_in_bytes = {{.Resources.MemorySwap}}
|
||||||
{{end}}
|
{{end}}
|
||||||
{{if gt .Resources.MemoryReservation 0}}
|
{{if gt .Resources.MemoryReservation 0}}
|
||||||
lxc.cgroup.memory.soft_limit_in_bytes = {{.Resources.MemoryReservation}}
|
lxc.cgroup.memory.soft_limit_in_bytes = {{.Resources.MemoryReservation}}
|
||||||
|
@ -209,15 +209,6 @@ func isDirectory(source string) string {
|
||||||
return "file"
|
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 {
|
func getLabel(c map[string][]string, name string) string {
|
||||||
label := c["label"]
|
label := c["label"]
|
||||||
for _, l := range label {
|
for _, l := range label {
|
||||||
|
@ -242,7 +233,6 @@ func getHostname(env []string) string {
|
||||||
func init() {
|
func init() {
|
||||||
var err error
|
var err error
|
||||||
funcMap := template.FuncMap{
|
funcMap := template.FuncMap{
|
||||||
"getMemorySwap": getMemorySwap,
|
|
||||||
"escapeFstabSpaces": escapeFstabSpaces,
|
"escapeFstabSpaces": escapeFstabSpaces,
|
||||||
"formatMountLabel": label.FormatMountLabel,
|
"formatMountLabel": label.FormatMountLabel,
|
||||||
"isDirectory": isDirectory,
|
"isDirectory": isDirectory,
|
||||||
|
|
|
@ -34,6 +34,7 @@ func TestLXCConfig(t *testing.T) {
|
||||||
memMin = 33554432
|
memMin = 33554432
|
||||||
memMax = 536870912
|
memMax = 536870912
|
||||||
mem = memMin + r.Intn(memMax-memMin)
|
mem = memMin + r.Intn(memMax-memMin)
|
||||||
|
swap = memMax
|
||||||
cpuMin = 100
|
cpuMin = 100
|
||||||
cpuMax = 10000
|
cpuMax = 10000
|
||||||
cpu = cpuMin + r.Intn(cpuMax-cpuMin)
|
cpu = cpuMin + r.Intn(cpuMax-cpuMin)
|
||||||
|
@ -46,8 +47,9 @@ func TestLXCConfig(t *testing.T) {
|
||||||
command := &execdriver.Command{
|
command := &execdriver.Command{
|
||||||
ID: "1",
|
ID: "1",
|
||||||
Resources: &execdriver.Resources{
|
Resources: &execdriver.Resources{
|
||||||
Memory: int64(mem),
|
Memory: int64(mem),
|
||||||
CPUShares: int64(cpu),
|
MemorySwap: int64(swap),
|
||||||
|
CPUShares: int64(cpu),
|
||||||
},
|
},
|
||||||
Network: &execdriver.Network{
|
Network: &execdriver.Network{
|
||||||
Mtu: 1500,
|
Mtu: 1500,
|
||||||
|
@ -63,7 +65,7 @@ func TestLXCConfig(t *testing.T) {
|
||||||
fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem))
|
fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem))
|
||||||
|
|
||||||
grepFile(t, p,
|
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) {
|
func TestCustomLxcConfig(t *testing.T) {
|
||||||
|
|
|
@ -17,6 +17,7 @@ import (
|
||||||
"github.com/docker/docker/pkg/mount"
|
"github.com/docker/docker/pkg/mount"
|
||||||
"github.com/docker/docker/pkg/parsers"
|
"github.com/docker/docker/pkg/parsers"
|
||||||
"github.com/docker/docker/pkg/sysinfo"
|
"github.com/docker/docker/pkg/sysinfo"
|
||||||
|
"github.com/docker/docker/pkg/units"
|
||||||
"github.com/go-check/check"
|
"github.com/go-check/check"
|
||||||
"github.com/kr/pty"
|
"github.com/kr/pty"
|
||||||
)
|
)
|
||||||
|
@ -435,3 +436,22 @@ func (s *DockerSuite) TestRunInvalidCPUShares(c *check.C) {
|
||||||
expected = "The maximum allowed cpu-shares is"
|
expected = "The maximum allowed cpu-shares is"
|
||||||
c.Assert(out, checker.Contains, expected)
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue