Skip to content

Commit

Permalink
Fix problem when update memory and swap memory
Browse files Browse the repository at this point in the history
Currently, if we start a container with:
`docker run -ti --name foo --memory 300M --memory-swap 500M busybox sh`

Then we want to update it with:
`docker update --memory 600M --memory-swap 800M foo`

It'll get error because we can't set memory to 600M with
the 500M limit of swap memory.

Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>
  • Loading branch information
hqhq committed Feb 26, 2016
1 parent 930dbb3 commit c8a459f
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions libcontainer/cgroups/fs/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,20 @@ func (s *MemoryGroup) SetKernelMemory(path string, cgroup *configs.Cgroup) error
}

func (s *MemoryGroup) Set(path string, cgroup *configs.Cgroup) error {
if cgroup.Resources.Memory != 0 {
if err := writeFile(path, "memory.limit_in_bytes", strconv.FormatInt(cgroup.Resources.Memory, 10)); err != nil {
// When memory and swap memory are both set, we need to release the limits first,
// it's to avoid problem when we update the configs, the new memory limit could
// be larger than the existed swap memory limit but smaller than the new swap
// memory limit, we should allow such update usage.
if cgroup.Resources.Memory != 0 && cgroup.Resources.MemorySwap != 0 {
if err := writeFile(path, "memory.memsw.limit_in_bytes", "-1"); err != nil {
return err
}
if err := writeFile(path, "memory.limit_in_bytes", "-1"); err != nil {
return err
}
}
if cgroup.Resources.MemoryReservation != 0 {
if err := writeFile(path, "memory.soft_limit_in_bytes", strconv.FormatInt(cgroup.Resources.MemoryReservation, 10)); err != nil {
if cgroup.Resources.Memory != 0 {
if err := writeFile(path, "memory.limit_in_bytes", strconv.FormatInt(cgroup.Resources.Memory, 10)); err != nil {
return err
}
}
Expand All @@ -81,6 +88,11 @@ func (s *MemoryGroup) Set(path string, cgroup *configs.Cgroup) error {
return err
}
}
if cgroup.Resources.MemoryReservation != 0 {
if err := writeFile(path, "memory.soft_limit_in_bytes", strconv.FormatInt(cgroup.Resources.MemoryReservation, 10)); err != nil {
return err
}
}
if cgroup.Resources.OomKillDisable {
if err := writeFile(path, "memory.oom_control", "1"); err != nil {
return err
Expand Down

0 comments on commit c8a459f

Please sign in to comment.