From 8fc3b9cba2b35e02421acbc5ec13454a5a712564 Mon Sep 17 00:00:00 2001 From: Dongsheng Yang Date: Mon, 26 Oct 2015 15:39:02 -0400 Subject: [PATCH] Add support for kmem.tcp limit Signed-off-by: Dongsheng Yang --- README.md | 1 + libcontainer/cgroups/fs/memory.go | 5 ++++ libcontainer/cgroups/fs/memory_test.go | 28 +++++++++++++++++++ libcontainer/cgroups/systemd/apply_systemd.go | 6 ++++ libcontainer/configs/cgroup_unix.go | 3 ++ spec.go | 1 + 6 files changed, 44 insertions(+) diff --git a/README.md b/README.md index 44c7e871f15..ff20c43777b 100644 --- a/README.md +++ b/README.md @@ -243,6 +243,7 @@ user with uid and gid of `0` defined within that file-system. "reservation": 0, "swap": 0, "kernel": 0, + "kernelTCP": 0, "swappiness": -1 }, "cpu": { diff --git a/libcontainer/cgroups/fs/memory.go b/libcontainer/cgroups/fs/memory.go index 19dfcc72639..6738c3a3c8a 100644 --- a/libcontainer/cgroups/fs/memory.go +++ b/libcontainer/cgroups/fs/memory.go @@ -73,6 +73,11 @@ func (s *MemoryGroup) Set(path string, cgroup *configs.Cgroup) error { return err } } + if cgroup.KernelMemoryTCP > 0 { + if err := writeFile(path, "memory.kmem.tcp.limit_in_bytes", strconv.FormatInt(cgroup.KernelMemoryTCP, 10)); err != nil { + return err + } + } if cgroup.OomKillDisable { if err := writeFile(path, "memory.oom_control", "1"); err != nil { diff --git a/libcontainer/cgroups/fs/memory_test.go b/libcontainer/cgroups/fs/memory_test.go index d7b5e5f435e..6f20f603f2c 100644 --- a/libcontainer/cgroups/fs/memory_test.go +++ b/libcontainer/cgroups/fs/memory_test.go @@ -113,6 +113,34 @@ func TestMemorySetKernelMemory(t *testing.T) { } } +func TestMemorySetKernelMemory(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.c.KernelMemoryTCP = kernelMemoryTCPAfter + memory := &MemoryGroup{} + if err := memory.Set(helper.CgroupPath, helper.CgroupData.c); 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/cgroups/systemd/apply_systemd.go b/libcontainer/cgroups/systemd/apply_systemd.go index 7a422b3c794..534e892f317 100644 --- a/libcontainer/cgroups/systemd/apply_systemd.go +++ b/libcontainer/cgroups/systemd/apply_systemd.go @@ -539,6 +539,12 @@ func joinMemory(c *configs.Cgroup, pid int) error { return err } } + if c.KernelMemoryTCP > 0 { + err = writeFile(path, "memory.kmem.tcp.limit_in_bytes", strconv.FormatInt(c.KernelMemoryTCP, 10)) + if err != nil { + return err + } + } if c.OomKillDisable { if err := writeFile(path, "memory.oom_control", "1"); err != nil { return err diff --git a/libcontainer/configs/cgroup_unix.go b/libcontainer/configs/cgroup_unix.go index 24f93c1ad6e..3ad878d5807 100644 --- a/libcontainer/configs/cgroup_unix.go +++ b/libcontainer/configs/cgroup_unix.go @@ -35,6 +35,9 @@ type Cgroup struct { // Kernel memory limit (in bytes) KernelMemory int64 `json:"kernel_memory"` + // Kernel memory limit for TCP using (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 69cf47b799a..e13ba4735ac 100644 --- a/spec.go +++ b/spec.go @@ -448,6 +448,7 @@ func createCgroupConfig(name string, spec *specs.LinuxRuntimeSpec, devices []*co c.MemoryReservation = r.Memory.Reservation c.MemorySwap = r.Memory.Swap c.KernelMemory = r.Memory.Kernel + c.KernelMemoryTCP = r.Memory.KernelTCP c.MemorySwappiness = r.Memory.Swappiness c.CpuShares = r.CPU.Shares c.CpuQuota = r.CPU.Quota