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>
| ... | ... |
@@ -91,9 +91,9 @@ lxc.mount.entry = {{$value.Source}} {{escapeFstabSpaces $ROOTFS}}/{{escapeFstabS
|
| 91 | 91 |
{{if .Resources}}
|
| 92 | 92 |
{{if .Resources.Memory}}
|
| 93 | 93 |
lxc.cgroup.memory.limit_in_bytes = {{.Resources.Memory}}
|
| 94 |
-{{with $memSwap := getMemorySwap .Resources}}
|
|
| 95 |
-lxc.cgroup.memory.memsw.limit_in_bytes = {{$memSwap}}
|
|
| 96 | 94 |
{{end}}
|
| 95 |
+{{if gt .Resources.MemorySwap 0}}
|
|
| 96 |
+lxc.cgroup.memory.memsw.limit_in_bytes = {{.Resources.MemorySwap}}
|
|
| 97 | 97 |
{{end}}
|
| 98 | 98 |
{{if gt .Resources.MemoryReservation 0}}
|
| 99 | 99 |
lxc.cgroup.memory.soft_limit_in_bytes = {{.Resources.MemoryReservation}}
|
| ... | ... |
@@ -209,15 +209,6 @@ func isDirectory(source string) string {
|
| 209 | 209 |
return "file" |
| 210 | 210 |
} |
| 211 | 211 |
|
| 212 |
-func getMemorySwap(v *execdriver.Resources) int64 {
|
|
| 213 |
- // By default, MemorySwap is set to twice the size of RAM. |
|
| 214 |
- // If you want to omit MemorySwap, set it to `-1'. |
|
| 215 |
- if v.MemorySwap < 0 {
|
|
| 216 |
- return 0 |
|
| 217 |
- } |
|
| 218 |
- return v.Memory * 2 |
|
| 219 |
-} |
|
| 220 |
- |
|
| 221 | 212 |
func getLabel(c map[string][]string, name string) string {
|
| 222 | 213 |
label := c["label"] |
| 223 | 214 |
for _, l := range label {
|
| ... | ... |
@@ -242,7 +233,6 @@ func getHostname(env []string) string {
|
| 242 | 242 |
func init() {
|
| 243 | 243 |
var err error |
| 244 | 244 |
funcMap := template.FuncMap{
|
| 245 |
- "getMemorySwap": getMemorySwap, |
|
| 246 | 245 |
"escapeFstabSpaces": escapeFstabSpaces, |
| 247 | 246 |
"formatMountLabel": label.FormatMountLabel, |
| 248 | 247 |
"isDirectory": isDirectory, |
| ... | ... |
@@ -34,6 +34,7 @@ func TestLXCConfig(t *testing.T) {
|
| 34 | 34 |
memMin = 33554432 |
| 35 | 35 |
memMax = 536870912 |
| 36 | 36 |
mem = memMin + r.Intn(memMax-memMin) |
| 37 |
+ swap = memMax |
|
| 37 | 38 |
cpuMin = 100 |
| 38 | 39 |
cpuMax = 10000 |
| 39 | 40 |
cpu = cpuMin + r.Intn(cpuMax-cpuMin) |
| ... | ... |
@@ -46,8 +47,9 @@ func TestLXCConfig(t *testing.T) {
|
| 46 | 46 |
command := &execdriver.Command{
|
| 47 | 47 |
ID: "1", |
| 48 | 48 |
Resources: &execdriver.Resources{
|
| 49 |
- Memory: int64(mem), |
|
| 50 |
- CPUShares: int64(cpu), |
|
| 49 |
+ Memory: int64(mem), |
|
| 50 |
+ MemorySwap: int64(swap), |
|
| 51 |
+ CPUShares: int64(cpu), |
|
| 51 | 52 |
}, |
| 52 | 53 |
Network: &execdriver.Network{
|
| 53 | 54 |
Mtu: 1500, |
| ... | ... |
@@ -63,7 +65,7 @@ func TestLXCConfig(t *testing.T) {
|
| 63 | 63 |
fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem))
|
| 64 | 64 |
|
| 65 | 65 |
grepFile(t, p, |
| 66 |
- fmt.Sprintf("lxc.cgroup.memory.memsw.limit_in_bytes = %d", mem*2))
|
|
| 66 |
+ fmt.Sprintf("lxc.cgroup.memory.memsw.limit_in_bytes = %d", swap))
|
|
| 67 | 67 |
} |
| 68 | 68 |
|
| 69 | 69 |
func TestCustomLxcConfig(t *testing.T) {
|
| ... | ... |
@@ -17,6 +17,7 @@ import ( |
| 17 | 17 |
"github.com/docker/docker/pkg/mount" |
| 18 | 18 |
"github.com/docker/docker/pkg/parsers" |
| 19 | 19 |
"github.com/docker/docker/pkg/sysinfo" |
| 20 |
+ "github.com/docker/docker/pkg/units" |
|
| 20 | 21 |
"github.com/go-check/check" |
| 21 | 22 |
"github.com/kr/pty" |
| 22 | 23 |
) |
| ... | ... |
@@ -435,3 +436,22 @@ func (s *DockerSuite) TestRunInvalidCPUShares(c *check.C) {
|
| 435 | 435 |
expected = "The maximum allowed cpu-shares is" |
| 436 | 436 |
c.Assert(out, checker.Contains, expected) |
| 437 | 437 |
} |
| 438 |
+ |
|
| 439 |
+func (s *DockerSuite) TestRunWithCorrectMemorySwapOnLXC(c *check.C) {
|
|
| 440 |
+ testRequires(c, memoryLimitSupport) |
|
| 441 |
+ testRequires(c, swapMemorySupport) |
|
| 442 |
+ testRequires(c, SameHostDaemon) |
|
| 443 |
+ |
|
| 444 |
+ out, _ := dockerCmd(c, "run", "-d", "-m", "16m", "--memory-swap", "64m", "busybox", "top") |
|
| 445 |
+ if _, err := os.Stat("/sys/fs/cgroup/memory/lxc"); err != nil {
|
|
| 446 |
+ c.Skip("Excecution driver must be LXC for this test")
|
|
| 447 |
+ } |
|
| 448 |
+ id := strings.TrimSpace(out) |
|
| 449 |
+ memorySwap, err := ioutil.ReadFile(fmt.Sprintf("/sys/fs/cgroup/memory/lxc/%s/memory.memsw.limit_in_bytes", id))
|
|
| 450 |
+ c.Assert(err, check.IsNil) |
|
| 451 |
+ cgSwap, err := strconv.ParseInt(strings.TrimSpace(string(memorySwap)), 10, 64) |
|
| 452 |
+ c.Assert(err, check.IsNil) |
|
| 453 |
+ swap, err := units.RAMInBytes("64m")
|
|
| 454 |
+ c.Assert(err, check.IsNil) |
|
| 455 |
+ c.Assert(cgSwap, check.Equals, swap) |
|
| 456 |
+} |