Skip to content

Commit

Permalink
Merge pull request #2272 from kolyshkin/cgroupv2-max
Browse files Browse the repository at this point in the history
cgroupv2: use "max" for negative values
  • Loading branch information
Mrunal Patel authored Mar 26, 2020
2 parents 96596cb + 6905b72 commit cebef0e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
28 changes: 22 additions & 6 deletions libcontainer/cgroups/fs2/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,38 @@ import (
"github.com/pkg/errors"
)

// numToStr converts an int64 value to a string for writing to a
// cgroupv2 files with .min, .max, .low, or .high suffix.
// Negative values are converted to "max" for cgroupv1 compatibility
// (which used to write -1 to remove the limit).
func numToStr(value int64) (ret string) {
if value > 0 {
ret = strconv.FormatInt(value, 10)
} else if value < 0 {
ret = "max"
} else {
ret = ""
}

return ret
}

func setMemory(dirPath string, cgroup *configs.Cgroup) error {
if cgroup.Resources.MemorySwap != 0 {
if err := fscommon.WriteFile(dirPath, "memory.swap.max", strconv.FormatInt(cgroup.Resources.MemorySwap, 10)); err != nil {
if val := numToStr(cgroup.Resources.MemorySwap); val != "" {
if err := fscommon.WriteFile(dirPath, "memory.swap.max", val); err != nil {
return err
}
}
if cgroup.Resources.Memory != 0 {
if err := fscommon.WriteFile(dirPath, "memory.max", strconv.FormatInt(cgroup.Resources.Memory, 10)); err != nil {
if val := numToStr(cgroup.Resources.Memory); val != "" {
if err := fscommon.WriteFile(dirPath, "memory.max", val); err != nil {
return err
}
}

// cgroup.Resources.KernelMemory is ignored

if cgroup.Resources.MemoryReservation != 0 {
if err := fscommon.WriteFile(dirPath, "memory.low", strconv.FormatInt(cgroup.Resources.MemoryReservation, 10)); err != nil {
if val := numToStr(cgroup.Resources.MemoryReservation); val != "" {
if err := fscommon.WriteFile(dirPath, "memory.low", val); err != nil {
return err
}
}
Expand Down
12 changes: 2 additions & 10 deletions libcontainer/cgroups/fs2/pids.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/opencontainers/runc/libcontainer/cgroups"
Expand All @@ -17,15 +16,8 @@ import (
)

func setPids(dirPath string, cgroup *configs.Cgroup) error {
if cgroup.Resources.PidsLimit != 0 {
// "max" is the fallback value.
limit := "max"

if cgroup.Resources.PidsLimit > 0 {
limit = strconv.FormatInt(cgroup.Resources.PidsLimit, 10)
}

if err := fscommon.WriteFile(dirPath, "pids.max", limit); err != nil {
if val := numToStr(cgroup.Resources.PidsLimit); val != "" {
if err := fscommon.WriteFile(dirPath, "pids.max", val); err != nil {
return err
}
}
Expand Down

0 comments on commit cebef0e

Please sign in to comment.