From 1448fe95683df25e41813bc0e951691a2362fd48 Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Sun, 20 Mar 2016 21:45:52 +1100 Subject: [PATCH] libcontainer: cgroups: add support for kmem.tcp limits Kernel TCP memory has its own special knobs inside the cgroup. Signed-off-by: Aleksa Sarai --- libcontainer/cgroups/fs/memory.go | 6 ++++++ libcontainer/cgroups/fs/memory_test.go | 28 ++++++++++++++++++++++++++ libcontainer/configs/cgroup_unix.go | 3 +++ spec.go | 3 +++ 4 files changed, 40 insertions(+) diff --git a/libcontainer/cgroups/fs/memory.go b/libcontainer/cgroups/fs/memory.go index e3fd327e..179d8bc2 100644 --- a/libcontainer/cgroups/fs/memory.go +++ b/libcontainer/cgroups/fs/memory.go @@ -81,6 +81,11 @@ func (s *MemoryGroup) Set(path string, cgroup *configs.Cgroup) error { return err } } + if cgroup.Resources.KernelMemoryTCP != 0 { + if err := writeFile(path, "memory.kmem.tcp.limit_in_bytes", strconv.FormatInt(cgroup.Resources.KernelMemoryTCP, 10)); err != nil { + return err + } + } if cgroup.Resources.OomKillDisable { if err := writeFile(path, "memory.oom_control", "1"); err != nil { return err @@ -148,6 +153,7 @@ func memoryAssigned(cgroup *configs.Cgroup) bool { cgroup.Resources.MemoryReservation != 0 || cgroup.Resources.MemorySwap > 0 || cgroup.Resources.KernelMemory > 0 || + cgroup.Resources.KernelMemoryTCP > 0 || cgroup.Resources.OomKillDisable || (cgroup.Resources.MemorySwappiness != nil && *cgroup.Resources.MemorySwappiness != -1) } diff --git a/libcontainer/cgroups/fs/memory_test.go b/libcontainer/cgroups/fs/memory_test.go index 8321ce97..136393fe 100644 --- a/libcontainer/cgroups/fs/memory_test.go +++ b/libcontainer/cgroups/fs/memory_test.go @@ -114,6 +114,34 @@ func TestMemorySetKernelMemory(t *testing.T) { } } +func TestMemorySetKernelMemoryTCP(t *testing.T) { + helper := NewCgroupTestUtil("memory", t) + defer helper.cleanup() + + const ( + kernelMemoryTCPBefore = 314572800 // 300M + kernelMemoryTCPAfter = 524288000 // 500M + ) + + helper.writeFileContents(map[string]string{ + "memory.kmem.tcp.limit_in_bytes": strconv.Itoa(kernelMemoryTCPBefore), + }) + + helper.CgroupData.config.Resources.KernelMemory = kernelMemoryTCPAfter + memory := &MemoryGroup{} + if err := memory.Set(helper.CgroupPath, helper.CgroupData.config); err != nil { + t.Fatal(err) + } + + value, err := getCgroupParamUint(helper.CgroupPath, "memory.kmem.tcp.limit_in_bytes") + if err != nil { + t.Fatalf("Failed to parse memory.kmem.tcp.limit_in_bytes - %s", err) + } + if value != kernelMemoryTCPAfter { + t.Fatal("Got the wrong value, set memory.kmem.tcp.limit_in_bytes failed.") + } +} + func TestMemorySetMemorySwappinessDefault(t *testing.T) { helper := NewCgroupTestUtil("memory", t) defer helper.cleanup() diff --git a/libcontainer/configs/cgroup_unix.go b/libcontainer/configs/cgroup_unix.go index 2ea00658..f2eff91c 100644 --- a/libcontainer/configs/cgroup_unix.go +++ b/libcontainer/configs/cgroup_unix.go @@ -56,6 +56,9 @@ type Resources struct { // Kernel memory limit (in bytes) KernelMemory int64 `json:"kernel_memory"` + // Kernel memory limit for TCP use (in bytes) + KernelMemoryTCP int64 `json:"kernel_memory_tcp"` + // CPU shares (relative weight vs. other containers) CpuShares int64 `json:"cpu_shares"` diff --git a/spec.go b/spec.go index 87bbff88..92e84639 100644 --- a/spec.go +++ b/spec.go @@ -382,6 +382,9 @@ func createCgroupConfig(name string, spec *specs.Spec) (*configs.Cgroup, error) if r.Memory.Kernel != nil { c.Resources.KernelMemory = int64(*r.Memory.Kernel) } + if r.Memory.KernelTCP != nil { + c.Resources.KernelMemoryTCP = int64(*r.Memory.KernelTCP) + } if r.Memory.Swappiness != nil { swappiness := int64(*r.Memory.Swappiness) c.Resources.MemorySwappiness = &swappiness