Skip to content

Commit

Permalink
cgroups: fix incorrect cgroups cpuset via systemd
Browse files Browse the repository at this point in the history
Signed-off-by: Yuta Maeda <[email protected]>
  • Loading branch information
mmmmaeda committed Nov 5, 2024
1 parent 7c29729 commit 4fb1865
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions pkg/cgroups/systemd_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"fmt"
"path/filepath"
"strings"
"strconv"
"math/big"

systemdDbus "github.com/coreos/go-systemd/v22/dbus"
"github.com/godbus/dbus/v5"
Expand Down Expand Up @@ -179,11 +181,11 @@ func resourcesToProps(res *configs.Resources, v2 bool) (map[string]uint64, map[s

// CPUSet
if res.CpusetCpus != "" {
bits := []byte(res.CpusetCpus)
bits := RangeToBits(res.CpusetCpus)
bMap["AllowedCPUs"] = bits
}
if res.CpusetMems != "" {
bits := []byte(res.CpusetMems)
bits := RangeToBits(res.CpusetMems)
bMap["AllowedMemoryNodes"] = bits
}

Expand Down Expand Up @@ -260,3 +262,35 @@ func resourcesToProps(res *configs.Resources, v2 bool) (map[string]uint64, map[s

return uMap, sMap, bMap, iMap, structMap
}

func RangeToBits(str string) ([]byte) {
bits := new(big.Int)

for _, r := range strings.Split(str, ",") {
// allow extra spaces around
r = strings.TrimSpace(r)
// allow empty elements (extra commas)
if r == "" {
continue
}
startr, endr, ok := strings.Cut(r, "-")
if ok {
start, _ := strconv.ParseUint(startr, 10, 32)
end, _ := strconv.ParseUint(endr, 10, 32)
for i := start; i <= end; i++ {
bits.SetBit(bits, int(i), 1)
}
} else {
val, _ := strconv.ParseUint(startr, 10, 32)
bits.SetBit(bits, int(val), 1)
}
}

ret := bits.Bytes()

// fit cpuset parsing order in systemd
for l, r := 0, len(ret)-1; l < r; l, r = l+1, r-1 {
ret[l], ret[r] = ret[r], ret[l]
}
return ret
}

0 comments on commit 4fb1865

Please sign in to comment.