Skip to content

Commit

Permalink
feature: add kernel-memory support in cli and ctrd
Browse files Browse the repository at this point in the history
Signed-off-by: Allen Sun <allensun.shl@alibaba-inc.com>
  • Loading branch information
allencloud committed Mar 21, 2019
1 parent ac1a952 commit d957368
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 32 deletions.
2 changes: 1 addition & 1 deletion cli/common_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ func addCommonFlags(flagSet *pflag.FlagSet) *container {
flagSet.StringArrayVar(&c.logOpts, "log-opt", nil, "Log driver options")

// memory

flagSet.StringVarP(&c.memory, "memory", "m", "", "Memory limit")
flagSet.StringVar(&c.memorySwap, "memory-swap", "", "Swap limit equal to memory + swap, '-1' to enable unlimited swap")
flagSet.Int64Var(&c.memorySwappiness, "memory-swappiness", 0, "Container memory swappiness [0, 100]")
flagSet.StringVar(&c.kernelMemory, "kernel-memory", "", "Kernel memory limit (in bytes)")
// for alikernel isolation options
flagSet.Int64Var(&c.memoryWmarkRatio, "memory-wmark-ratio", 0, "Represent this container's memory low water mark percentage, range in [0, 100]. The value of memory low water mark is memory.limit_in_bytes * MemoryWmarkRatio")
flagSet.Int64Var(&c.memoryExtra, "memory-extra", 0, "Represent container's memory high water mark percentage, range in [0, 100]")
Expand Down
7 changes: 7 additions & 0 deletions cli/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type container struct {
memory string
memorySwap string
memorySwappiness int64
kernelMemory string

memoryWmarkRatio int64
memoryExtra int64
Expand Down Expand Up @@ -110,6 +111,11 @@ func (c *container) config() (*types.ContainerCreateConfig, error) {
return nil, err
}

kmemory, err := opts.ParseMemory(c.kernelMemory)
if err != nil {
return nil, err
}

intelRdtL3Cbm, err := opts.ParseIntelRdt(c.IntelRdtL3Cbm)
if err != nil {
return nil, err
Expand Down Expand Up @@ -223,6 +229,7 @@ func (c *container) config() (*types.ContainerCreateConfig, error) {
Memory: memory,
MemorySwap: memorySwap,
MemorySwappiness: &c.memorySwappiness,
KernelMemory: kmemory,
// FIXME: validate in client side
MemoryWmarkRatio: &c.memoryWmarkRatio,
MemoryExtra: &c.memoryExtra,
Expand Down
2 changes: 2 additions & 0 deletions ctrd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ func toLinuxResources(resources types.Resources) (*specs.LinuxResources, error)
Limit: &resources.Memory,
Swap: &resources.MemorySwap,
Reservation: &resources.MemoryReservation,
Kernel: &resources.KernelMemory,
// TODO: add other fields of specs.LinuxMemory
}

// TODO: add more fields.
Expand Down
5 changes: 5 additions & 0 deletions daemon/mgr/spec_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ func setupMemory(ctx context.Context, r types.Resources, s *specs.Spec) {
memory.DisableOOMKiller = &v
}

if r.KernelMemory > 0 {
v := r.KernelMemory
memory.Kernel = &v
}

s.Linux.Resources.Memory = memory
}

Expand Down
77 changes: 46 additions & 31 deletions test/cli_run_memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,73 +35,88 @@ func (suite *PouchRunMemorySuite) TearDownTest(c *check.C) {

// TestRunWithMemoryswap is to verify the valid running container
// with --memory-swap
func (suite *PouchRunMemorySuite) TestRunWithMemoryswap(c *check.C) {
func (suite *PouchRunMemorySuite) TestRunWithMemoryswapAndKernelMemory(c *check.C) {
SkipIfFalse(c, environment.IsMemorySupport)
SkipIfFalse(c, environment.IsMemorySwapSupport)

cname := "TestRunWithMemoryswap"
m := 1024 * 1024
cname := "TestRunWithMemoryswapAndKernelMemory"
memory := "100m"
memSwap := "200m"
expected := 200 * m
sleep := "10000"
kernelMemory := "100m"
expectedMem := strconv.Itoa(100 * 1024 * 1024) // 100 MB
expectedMemSwap := strconv.Itoa(200 * 1024 * 1024) // 200 MB
expectedKernelMem := strconv.Itoa(1024 * 1024 * 100) // 100 MB

res := command.PouchRun("run", "-d", "-m", memory,
res := command.PouchRun("run", "-d",
"--memory", memory,
"--memory-swap", memSwap,
"--name", cname, busyboxImage, "sleep", sleep)
"--kernel-memory", kernelMemory,
"--name", cname,
busyboxImage,
"sleep", "10000")
defer DelContainerForceMultyTime(c, cname)
res.Assert(c, icmd.Success)

// test if the value is in inspect result
memorySwap, err := inspectFilter(cname, ".HostConfig.MemorySwap")
memoryResult, err := inspectFilter(cname, ".HostConfig.Memory")
c.Assert(err, check.IsNil)
c.Assert(memorySwap, check.Equals, strconv.Itoa(expected))
c.Assert(memoryResult, check.Equals, expectedMem)
memorySwapResult, err := inspectFilter(cname, ".HostConfig.MemorySwap")
c.Assert(err, check.IsNil)
c.Assert(memorySwapResult, check.Equals, expectedMemSwap)
kernelMemoryResult, err := inspectFilter(cname, ".HostConfig.KernelMemory")
c.Assert(err, check.IsNil)
c.Assert(kernelMemoryResult, check.Equals, expectedKernelMem)

// test if cgroup has record the real value
containerID, err := inspectFilter(cname, ".ID")
c.Assert(err, check.IsNil)
path := fmt.Sprintf(
"/sys/fs/cgroup/memory/default/%s/memory.memsw.limit_in_bytes",
containerID)
checkFileContains(c, path, strconv.Itoa(expected))
path := fmt.Sprintf("/sys/fs/cgroup/memory/default/%s/memory.limit_in_bytes", containerID)
checkFileContains(c, path, expectedMem)
path = fmt.Sprintf("/sys/fs/cgroup/memory/default/%s/memory.memsw.limit_in_bytes", containerID)
checkFileContains(c, path, expectedMemSwap)
path = fmt.Sprintf("/sys/fs/cgroup/memory/default/%s/memory.kmem.limit_in_bytes", containerID)
checkFileContains(c, path, expectedKernelMem)

// test if the value is correct in container
memSwapLimitFile := "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
res = command.PouchRun("exec", cname, "cat", memSwapLimitFile)
res.Assert(c, icmd.Success)

out := strings.Trim(res.Stdout(), "\n")
c.Assert(out, check.Equals, strconv.Itoa(expected))
c.Assert(out, check.Equals, expectedMemSwap)
}

// test memory swap should be 2x memory if not specify it.
cname = "TestRunWithMemoryswap2x"
memory = "10m"
expected = 2 * 10 * m
// test memory swap should be 2x memory if not specify it.
func (suite *PouchRunMemorySuite) TestRunWithMemoryswap2x(c *check.C) {
cname := "TestRunWithMemoryswap2x"
memory := "20m"
expectedMemorySwap := strconv.Itoa(2 * 20 * 1024 * 1024) // 40 MB

res = command.PouchRun("run", "-d", "-m", memory,
"--name", cname, busyboxImage, "sleep", sleep)
res := command.PouchRun("run", "-d",
"-m", memory,
"--name", cname,
busyboxImage,
"sleep", "10000")
defer DelContainerForceMultyTime(c, cname)
res.Assert(c, icmd.Success)

// test if the value is in inspect result
memorySwap, err = inspectFilter(cname, ".HostConfig.MemorySwap")
memorySwap, err := inspectFilter(cname, ".HostConfig.MemorySwap")
c.Assert(err, check.IsNil)
c.Assert(memorySwap, check.Equals, strconv.Itoa(expected))
c.Assert(memorySwap, check.Equals, expectedMemorySwap)

// test if cgroup has record the real value
containerID, err = inspectFilter(cname, ".ID")
containerID, err := inspectFilter(cname, ".ID")
c.Assert(err, check.IsNil)
path = fmt.Sprintf(
"/sys/fs/cgroup/memory/default/%s/memory.memsw.limit_in_bytes",
containerID)
checkFileContains(c, path, strconv.Itoa(expected))
path := fmt.Sprintf("/sys/fs/cgroup/memory/default/%s/memory.memsw.limit_in_bytes", containerID)
checkFileContains(c, path, expectedMemorySwap)

// test if the value is correct in container
memSwapLimitFile := "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
res = command.PouchRun("exec", cname, "cat", memSwapLimitFile)
res.Assert(c, icmd.Success)

out = strings.Trim(res.Stdout(), "\n")
c.Assert(out, check.Equals, strconv.Itoa(expected))
out := strings.Trim(res.Stdout(), "\n")
c.Assert(out, check.Equals, expectedMemorySwap)
}

// TestRunWithMemoryswappiness is to verify the valid running container
Expand Down

0 comments on commit d957368

Please sign in to comment.