Skip to content

Commit

Permalink
Merge pull request #511 from skoo87/mem
Browse files Browse the repository at this point in the history
feature: add memory option for container
  • Loading branch information
allencloud authored Jan 8, 2018
2 parents 019edcd + f2522d2 commit e924aed
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 18 deletions.
13 changes: 7 additions & 6 deletions apis/server/container_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,13 @@ func (s *Server) getContainer(ctx context.Context, rw http.ResponseWriter, req *
}

container := types.ContainerJSON{
ID: meta.ID,
Name: meta.Name,
Image: meta.Config.Image,
Created: meta.State.StartedAt,
State: meta.State,
Config: meta.Config,
ID: meta.ID,
Name: meta.Name,
Image: meta.Config.Image,
Created: meta.State.StartedAt,
State: meta.State,
Config: meta.Config,
HostConfig: meta.HostConfig,
}
return EncodeResponse(rw, http.StatusOK, container)
}
52 changes: 40 additions & 12 deletions cli/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@ import (

"github.com/alibaba/pouch/apis/types"

units "github.com/docker/go-units"
strfmt "github.com/go-openapi/strfmt"
)

type container struct {
labels []string
name string
tty bool
volume []string
runtime string
env []string
entrypoint string
workdir string
hostname string
cpushare int64
cpusetcpus string
cpusetmems string
labels []string
name string
tty bool
volume []string
runtime string
env []string
entrypoint string
workdir string
hostname string
cpushare int64
cpusetcpus string
cpusetmems string
memory string
memorySwap string
memorySwappiness int64
}

func (c *container) config() (*types.ContainerCreateConfig, error) {
Expand Down Expand Up @@ -63,5 +67,29 @@ func (c *container) config() (*types.ContainerCreateConfig, error) {
config.HostConfig.CpusetCpus = c.cpusetcpus
config.HostConfig.CpusetMems = c.cpusetmems

if c.memorySwappiness != -1 && (c.memorySwappiness < 0 || c.memorySwappiness > 100) {
return nil, fmt.Errorf("invalid memory swappiness: %d (it's range is 0-100)", c.memorySwappiness)
}
config.HostConfig.MemorySwappiness = &c.memorySwappiness

if c.memory != "" {
v, err := units.RAMInBytes(c.memory)
if err != nil {
return nil, err
}
config.HostConfig.Memory = v
}
if c.memorySwap != "" {
if c.memorySwap == "-1" {
config.HostConfig.MemorySwap = -1
} else {
v, err := units.RAMInBytes(c.memorySwap)
if err != nil {
return nil, err
}
config.HostConfig.MemorySwap = v
}
}

return config, nil
}
3 changes: 3 additions & 0 deletions cli/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ func (cc *CreateCommand) addFlags() {
flagSet.Int64Var(&cc.cpushare, "cpu-share", 0, "CPU shares")
flagSet.StringVar(&cc.cpusetcpus, "cpuset-cpus", "", "CPUs in cpuset")
flagSet.StringVar(&cc.cpusetmems, "cpuset-mems", "", "MEMs in cpuset")
flagSet.Int64Var(&cc.memorySwappiness, "memory-wappiness", -1, "Container memory swappiness [0, 100]")
flagSet.StringVarP(&cc.memory, "memory", "m", "", "Container memory limit")
flagSet.StringVar(&cc.memorySwap, "memory-swap", "", "Container swap limit")
}

// runCreate is the entry of create command.
Expand Down
3 changes: 3 additions & 0 deletions cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func (rc *RunCommand) addFlags() {
flagSet.Int64Var(&rc.cpushare, "cpu-share", 0, "CPU shares")
flagSet.StringVar(&rc.cpusetcpus, "cpuset-cpus", "", "CPUs in cpuset")
flagSet.StringVar(&rc.cpusetmems, "cpuset-mems", "", "MEMs in cpuset")
flagSet.Int64Var(&rc.memorySwappiness, "memory-wappiness", -1, "Container memory swappiness [0, 100]")
flagSet.StringVarP(&rc.memory, "memory", "m", "", "Container memory limit")
flagSet.StringVar(&rc.memorySwap, "memory-swap", "", "Container swap limit")
}

// runRun is the entry of run command.
Expand Down
3 changes: 3 additions & 0 deletions daemon/mgr/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ var setupFunc = []SetupFunc{
// cgroup
setupCgroupCPUShare,
setupCgroupCPUSet,
setupCgroupMemory,
setupCgroupMemorySwap,
setupCgroupMemorySwappiness,

// namespaces
setupUserNamespace,
Expand Down
41 changes: 41 additions & 0 deletions daemon/mgr/spec_cgroup_memory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package mgr

import (
"context"

specs "github.com/opencontainers/runtime-spec/specs-go"
)

func getCgroupMemory(s *specs.Spec) *specs.LinuxMemory {
if s.Linux.Resources.Memory == nil {
s.Linux.Resources.Memory = &specs.LinuxMemory{}
}
return s.Linux.Resources.Memory
}

func setupCgroupMemory(ctx context.Context, meta *ContainerMeta, s *specs.Spec) error {
mem := getCgroupMemory(s)

v := meta.HostConfig.Memory
mem.Limit = &v
return nil
}

func setupCgroupMemorySwap(ctx context.Context, meta *ContainerMeta, s *specs.Spec) error {
mem := getCgroupMemory(s)

v := meta.HostConfig.MemorySwap
mem.Swap = &v
return nil
}

func setupCgroupMemorySwappiness(ctx context.Context, meta *ContainerMeta, s *specs.Spec) error {
mem := getCgroupMemory(s)

var v uint64
if meta.HostConfig.MemorySwappiness != nil {
v = uint64(*(meta.HostConfig.MemorySwappiness))
}
mem.Swappiness = &v
return nil
}

0 comments on commit e924aed

Please sign in to comment.