-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
switch c/common to use runc cgroup creation so that we can use resource limits this entails modifying both the CgroupControl struct to contain a runc configs.Cgroup entity as well as adding actual functionality to all of the Apply() functions for the various resource groups Signed-off-by: cdoern <cdoern@redhat.com> Signed-off-by: cdoern <cbdoer23@g.holycross.edu>
- Loading branch information
cdoern
committed
Mar 2, 2022
1 parent
9880eb4
commit 2c44075
Showing
39 changed files
with
4,251 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
//go:build !linux | ||
// +build !linux | ||
|
||
package cgroups | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
package cgroups | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/opencontainers/runc/libcontainer/cgroups" | ||
"github.com/opencontainers/runc/libcontainer/cgroups/fs" | ||
"github.com/opencontainers/runc/libcontainer/configs" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type linuxBlkioHandler struct { | ||
blkio fs.BlkioGroup | ||
} | ||
|
||
func getBlkioHandler() *linuxBlkioHandler { | ||
return &linuxBlkioHandler{} | ||
} | ||
|
||
// Apply set the specified constraints | ||
func (c *linuxBlkioHandler) Apply(ctr *CgroupControl, res *configs.Resources) error { | ||
if ctr.cgroup2 { | ||
path := filepath.Join(cgroupRoot, ctr.config.Path) | ||
var bfq *os.File | ||
// This has to do with checking for the bfq handler | ||
if res.BlkioWeight != 0 || len(res.BlkioWeightDevice) > 0 { | ||
var err error | ||
bfq, err = cgroups.OpenFile(path, "io.bfq.weight", os.O_RDWR) | ||
if err == nil { | ||
defer bfq.Close() | ||
} else if !os.IsNotExist(err) { | ||
return err | ||
} | ||
} | ||
|
||
if res.BlkioWeight != 0 { | ||
if bfq != nil { | ||
if _, err := bfq.WriteString(strconv.FormatUint(uint64(res.BlkioWeight), 10)); err != nil { | ||
return err | ||
} | ||
} else { | ||
v := cgroups.ConvertBlkIOToIOWeightValue(res.BlkioWeight) | ||
if err := cgroups.WriteFile(path, "io.weight", strconv.FormatUint(v, 10)); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
if bfqDeviceWeightSupported(bfq) { | ||
for _, wd := range res.BlkioWeightDevice { | ||
if _, err := bfq.WriteString(wd.WeightString() + "\n"); err != nil { | ||
return fmt.Errorf("setting device weight %q: %w", wd.WeightString(), err) | ||
} | ||
} | ||
} | ||
for _, td := range res.BlkioThrottleReadBpsDevice { | ||
if err := cgroups.WriteFile(path, "io.max", td.StringName("rbps")); err != nil { | ||
return err | ||
} | ||
} | ||
for _, td := range res.BlkioThrottleWriteBpsDevice { | ||
if err := cgroups.WriteFile(path, "io.max", td.StringName("wbps")); err != nil { | ||
return err | ||
} | ||
} | ||
for _, td := range res.BlkioThrottleReadIOPSDevice { | ||
if err := cgroups.WriteFile(path, "io.max", td.StringName("riops")); err != nil { | ||
return err | ||
} | ||
} | ||
for _, td := range res.BlkioThrottleWriteIOPSDevice { | ||
if err := cgroups.WriteFile(path, "io.max", td.StringName("wiops")); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
// use the runc fs1 functions otherwise, maintaining the fs2 functions here for future development | ||
path := filepath.Join(cgroupRoot, Blkio, ctr.config.Path) | ||
return c.blkio.Set(path, res) | ||
} | ||
|
||
// Create the cgroup | ||
func (c *linuxBlkioHandler) Create(ctr *CgroupControl) (bool, error) { | ||
return ctr.createCgroupDirectory(Blkio) | ||
} | ||
|
||
// Destroy the cgroup | ||
func (c *linuxBlkioHandler) Destroy(ctr *CgroupControl) error { | ||
return rmDirRecursively(ctr.getCgroupv1Path(Blkio)) | ||
} | ||
|
||
// Stat fills a metrics structure with usage stats for the controller | ||
func (c *linuxBlkioHandler) Stat(ctr *CgroupControl, m *cgroups.Stats) error { | ||
var ioServiceBytesRecursive []cgroups.BlkioStatEntry | ||
|
||
if ctr.cgroup2 { | ||
// more details on the io.stat file format:X https://facebookmicrosites.github.io/cgroup2/docs/io-controller.html | ||
values, err := readCgroup2MapFile(ctr, "io.stat") | ||
if err != nil { | ||
return err | ||
} | ||
for k, v := range values { | ||
d := strings.Split(k, ":") | ||
if len(d) != 2 { | ||
continue | ||
} | ||
minor, err := strconv.ParseUint(d[0], 10, 0) | ||
if err != nil { | ||
return err | ||
} | ||
major, err := strconv.ParseUint(d[1], 10, 0) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, item := range v { | ||
d := strings.Split(item, "=") | ||
if len(d) != 2 { | ||
continue | ||
} | ||
op := d[0] | ||
|
||
// Accommodate the cgroup v1 naming | ||
switch op { | ||
case "rbytes": | ||
op = "read" | ||
case "wbytes": | ||
op = "write" | ||
} | ||
|
||
value, err := strconv.ParseUint(d[1], 10, 0) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
entry := cgroups.BlkioStatEntry{ | ||
Op: op, | ||
Major: major, | ||
Minor: minor, | ||
Value: value, | ||
} | ||
ioServiceBytesRecursive = append(ioServiceBytesRecursive, entry) | ||
} | ||
} | ||
} else { | ||
BlkioRoot := ctr.getCgroupv1Path(Blkio) | ||
|
||
p := filepath.Join(BlkioRoot, "blkio.throttle.io_service_bytes_recursive") | ||
f, err := os.Open(p) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
return nil | ||
} | ||
return errors.Wrapf(err, "open %s", p) | ||
} | ||
defer f.Close() | ||
|
||
scanner := bufio.NewScanner(f) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
parts := strings.Fields(line) | ||
if len(parts) < 3 { | ||
continue | ||
} | ||
d := strings.Split(parts[0], ":") | ||
if len(d) != 2 { | ||
continue | ||
} | ||
minor, err := strconv.ParseUint(d[0], 10, 0) | ||
if err != nil { | ||
return err | ||
} | ||
major, err := strconv.ParseUint(d[1], 10, 0) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
op := parts[1] | ||
|
||
value, err := strconv.ParseUint(parts[2], 10, 0) | ||
if err != nil { | ||
return err | ||
} | ||
entry := cgroups.BlkioStatEntry{ | ||
Op: op, | ||
Major: major, | ||
Minor: minor, | ||
Value: value, | ||
} | ||
ioServiceBytesRecursive = append(ioServiceBytesRecursive, entry) | ||
} | ||
if err := scanner.Err(); err != nil { | ||
return errors.Wrapf(err, "parse %s", p) | ||
} | ||
} | ||
m.BlkioStats.IoServiceBytesRecursive = ioServiceBytesRecursive | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.