Skip to content

Commit

Permalink
additional cgroup testing and implementation of cpu-shares (weight in…
Browse files Browse the repository at this point in the history
… systemd), and most Blkio throttle and weight devices.

Signed-off-by: Charlie Doern <cdoern@redhat.com>
  • Loading branch information
cdoern committed Jul 6, 2022
1 parent 178929c commit 6667b18
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 4 deletions.
70 changes: 69 additions & 1 deletion pkg/cgroups/cgroups_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package cgroups

import (
"fmt"
"os"
"os/exec"
"strconv"
"testing"

Expand Down Expand Up @@ -42,9 +44,38 @@ func TestResources(t *testing.T) {
return
}

wtDevices := []*configs.WeightDevice{}
devices := []*configs.ThrottleDevice{}
dev1 := &configs.ThrottleDevice{
BlockIODevice: configs.BlockIODevice{
Major: 8,
Minor: 16,
},
Rate: 2097152,
}
dev2 := &configs.ThrottleDevice{
BlockIODevice: configs.BlockIODevice{
Major: 3,
Minor: 10,
},
Rate: 2097152,
}
dev3 := &configs.WeightDevice{
BlockIODevice: configs.BlockIODevice{
Major: 5,
Minor: 9,
},
Weight: 2000,
}
devices = append(devices, dev1, dev2)
wtDevices = append(wtDevices, dev3)

var resources configs.Resources
resources.CpuPeriod = 100000
resources.CpuQuota = 100000
resources.CpuShares = 100
resources.CpusetCpus = "0"
resources.CpusetMems = "0"
resources.Memory = 900
resources.MemorySwap = 1000
resources.BlkioWeight = 300
Expand Down Expand Up @@ -74,7 +105,7 @@ func TestResources(t *testing.T) {
}

// test CPU Quota adjustment.
u, _, _, _ := resourcesToProps(&resources)
u, _, _, _, _ := resourcesToProps(&resources)

val, ok := u["CPUQuotaPerSecUSec"]
if !ok {
Expand All @@ -84,6 +115,43 @@ func TestResources(t *testing.T) {
t.Fatal("CPU Quota incorrect value expected 1000000 got " + strconv.FormatUint(val, 10))
}

err = os.Mkdir("/dev/foodevdir", os.ModePerm)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll("/dev/foodevdir")

c := exec.Command("mknod", "/dev/foodevdir/foo", "c", "8", "16")
c.Env = os.Environ()
c.Stderr = os.Stderr
c.Stdout = os.Stdout
err = c.Run()
if err != nil {
t.Fatal(err)
}

c = exec.Command("mknod", "/dev/foodevdir/bar", "c", "3", "10")
c.Env = os.Environ()
c.Stderr = os.Stderr
c.Stdout = os.Stdout
err = c.Run()
if err != nil {
t.Fatal(err)
}

c = exec.Command("mknod", "/dev/foodevdir/bat", "c", "5", "9")
c.Env = os.Environ()
c.Stderr = os.Stderr
c.Stdout = os.Stdout
err = c.Run()
if err != nil {
t.Fatal(err)
}

resources.BlkioThrottleReadBpsDevice = []*configs.ThrottleDevice{devices[0]}
resources.BlkioThrottleWriteBpsDevice = []*configs.ThrottleDevice{devices[1]}
resources.BlkioWeightDevice = wtDevices

// machine.slice = parent, libpod_pod_ID = path
err = cgr2.CreateSystemdUnit(fmt.Sprintf("%s/%s-%s%s", "machine2.slice", "machine2", "libpod_pod_1234", ".slice"))
if err != nil {
Expand Down
63 changes: 60 additions & 3 deletions pkg/cgroups/systemd_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import (
"github.com/opencontainers/runc/libcontainer/configs"
)

type BlkioDev struct {
Device string
Bytes uint64
}

func systemdCreate(resources *configs.Resources, path string, c *systemdDbus.Conn) error {
slice, name := filepath.Split(path)
slice = strings.TrimSuffix(slice, "/")
Expand Down Expand Up @@ -42,7 +47,7 @@ func systemdCreate(resources *configs.Resources, path string, c *systemdDbus.Con
properties = append(properties, p)
}

uMap, sMap, bMap, iMap := resourcesToProps(resources)
uMap, sMap, bMap, iMap, structMap := resourcesToProps(resources)
for k, v := range uMap {
p := systemdDbus.Property{
Name: k,
Expand Down Expand Up @@ -75,6 +80,14 @@ func systemdCreate(resources *configs.Resources, path string, c *systemdDbus.Con
properties = append(properties, p)
}

for k, v := range structMap {
p := systemdDbus.Property{
Name: k,
Value: dbus.MakeVariant(v),
}
properties = append(properties, p)
}

ch := make(chan string)
_, err := c.StartTransientUnitContext(context.TODO(), name, "replace", properties, ch)
if err != nil {
Expand Down Expand Up @@ -117,12 +130,14 @@ func systemdDestroyConn(path string, c *systemdDbus.Conn) error {
return nil
}

func resourcesToProps(res *configs.Resources) (map[string]uint64, map[string]string, map[string][]byte, map[string]int64) {
func resourcesToProps(res *configs.Resources) (map[string]uint64, map[string]string, map[string][]byte, map[string]int64, map[string][]BlkioDev) {

bMap := make(map[string][]byte)
// this array is not used but will be once more resource limits are added
sMap := make(map[string]string)
iMap := make(map[string]int64)
uMap := make(map[string]uint64)
structMap := make(map[string][]BlkioDev)

// CPU
if res.CpuPeriod != 0 {
Expand All @@ -140,6 +155,17 @@ func resourcesToProps(res *configs.Resources) (map[string]uint64, map[string]str
uMap["CPUQuotaPerSecUSec"] = cpuQuotaPerSecUSec
}

if res.CpuShares != 0 {
// convert from shares to weight. weight only supports 1-10000
v2, _ := IsCgroup2UnifiedMode()
if v2 {
wt := (1 + ((res.CpuShares-2)*9999)/262142)
uMap["CPUWeight"] = wt
} else {
uMap["CPUShares"] = res.CpuShares
}
}

// CPUSet
if res.CpusetCpus != "" {
bits := []byte(res.CpusetCpus)
Expand All @@ -163,5 +189,36 @@ func resourcesToProps(res *configs.Resources) (map[string]uint64, map[string]str
uMap["BlockIOWeight"] = uint64(res.BlkioWeight)
}

return uMap, sMap, bMap, iMap
if res.BlkioThrottleReadBpsDevice != nil {
for _, entry := range res.BlkioThrottleReadBpsDevice {
newThrottle := BlkioDev{
Device: fmt.Sprintf("%d:%d", entry.Major, entry.Minor),
Bytes: entry.Rate,
}
structMap["BlockIOReadBandwidth"] = append(structMap["BlockIOReadBandwidth"], newThrottle)
}
}

if res.BlkioThrottleWriteBpsDevice != nil {
for _, entry := range res.BlkioThrottleWriteBpsDevice {
newThrottle := BlkioDev{
Device: fmt.Sprintf("%d:%d", entry.Major, entry.Minor),
Bytes: entry.Rate,
}
structMap["BlockIOWriteBandwidth"] = append(structMap["BlockIOWriteBandwidth"], newThrottle)
}
}

if res.BlkioWeightDevice != nil {
for _, entry := range res.BlkioWeightDevice {
newWeight := BlkioDev{
Device: fmt.Sprintf("%d:%d", entry.Major, entry.Minor),
Bytes: uint64(entry.Weight),
}
structMap["BlockIODeviceWeight"] = append(structMap["BlockIODeviceWeight"], newWeight)

}
}

return uMap, sMap, bMap, iMap, structMap
}

0 comments on commit 6667b18

Please sign in to comment.