2020-05-04 20:46:48 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package fs2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2021-07-02 08:43:15 +00:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2020-05-04 20:46:48 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2020-05-04 20:46:48 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
|
|
|
)
|
|
|
|
|
2021-06-18 20:46:09 +00:00
|
|
|
func isIoSet(r *configs.Resources) bool {
|
|
|
|
return r.BlkioWeight != 0 ||
|
2021-07-02 08:43:15 +00:00
|
|
|
len(r.BlkioWeightDevice) > 0 ||
|
2021-06-18 20:46:09 +00:00
|
|
|
len(r.BlkioThrottleReadBpsDevice) > 0 ||
|
|
|
|
len(r.BlkioThrottleWriteBpsDevice) > 0 ||
|
|
|
|
len(r.BlkioThrottleReadIOPSDevice) > 0 ||
|
|
|
|
len(r.BlkioThrottleWriteIOPSDevice) > 0
|
2020-08-10 17:43:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
// bfqDeviceWeightSupported checks for per-device BFQ weight support (added
|
|
|
|
// in kernel v5.4, commit 795fe54c2a8) by reading from "io.bfq.weight".
|
|
|
|
func bfqDeviceWeightSupported(bfq *os.File) bool {
|
|
|
|
if bfq == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
_, _ = bfq.Seek(0, 0)
|
|
|
|
buf := make([]byte, 32)
|
|
|
|
_, _ = bfq.Read(buf)
|
|
|
|
// If only a single number (default weight) if read back, we have older kernel.
|
|
|
|
_, err := strconv.ParseInt(string(bytes.TrimSpace(buf)), 10, 64)
|
|
|
|
return err != nil
|
|
|
|
}
|
|
|
|
|
2021-06-18 20:46:09 +00:00
|
|
|
func setIo(dirPath string, r *configs.Resources) error {
|
|
|
|
if !isIoSet(r) {
|
2020-08-10 17:43:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
// If BFQ IO scheduler is available, use it.
|
|
|
|
var bfq *os.File
|
|
|
|
if r.BlkioWeight != 0 || len(r.BlkioWeightDevice) > 0 {
|
|
|
|
var err error
|
|
|
|
bfq, err = cgroups.OpenFile(dirPath, "io.bfq.weight", os.O_RDWR)
|
|
|
|
if err == nil {
|
|
|
|
defer bfq.Close()
|
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-18 20:46:09 +00:00
|
|
|
if r.BlkioWeight != 0 {
|
2021-07-02 08:43:15 +00:00
|
|
|
if bfq != nil { // Use BFQ.
|
|
|
|
if _, err := bfq.WriteString(strconv.FormatUint(uint64(r.BlkioWeight), 10)); err != nil {
|
2021-04-14 18:11:13 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-07-02 08:43:15 +00:00
|
|
|
} else {
|
|
|
|
// Fallback to io.weight with a conversion scheme.
|
2021-06-18 20:46:09 +00:00
|
|
|
v := cgroups.ConvertBlkIOToIOWeightValue(r.BlkioWeight)
|
2021-07-02 08:43:15 +00:00
|
|
|
if err := cgroups.WriteFile(dirPath, "io.weight", strconv.FormatUint(v, 10)); err != nil {
|
2021-04-14 18:11:13 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-02 08:43:15 +00:00
|
|
|
if bfqDeviceWeightSupported(bfq) {
|
|
|
|
for _, wd := range r.BlkioWeightDevice {
|
|
|
|
if _, err := bfq.WriteString(wd.WeightString() + "\n"); err != nil {
|
|
|
|
return fmt.Errorf("setting device weight %q: %w", wd.WeightString(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-18 20:46:09 +00:00
|
|
|
for _, td := range r.BlkioThrottleReadBpsDevice {
|
2021-07-02 08:43:15 +00:00
|
|
|
if err := cgroups.WriteFile(dirPath, "io.max", td.StringName("rbps")); err != nil {
|
2020-05-04 20:46:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-06-18 20:46:09 +00:00
|
|
|
for _, td := range r.BlkioThrottleWriteBpsDevice {
|
2021-07-02 08:43:15 +00:00
|
|
|
if err := cgroups.WriteFile(dirPath, "io.max", td.StringName("wbps")); err != nil {
|
2020-05-04 20:46:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-06-18 20:46:09 +00:00
|
|
|
for _, td := range r.BlkioThrottleReadIOPSDevice {
|
2021-07-02 08:43:15 +00:00
|
|
|
if err := cgroups.WriteFile(dirPath, "io.max", td.StringName("riops")); err != nil {
|
2020-05-04 20:46:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-06-18 20:46:09 +00:00
|
|
|
for _, td := range r.BlkioThrottleWriteIOPSDevice {
|
2021-07-02 08:43:15 +00:00
|
|
|
if err := cgroups.WriteFile(dirPath, "io.max", td.StringName("wiops")); err != nil {
|
2020-05-04 20:46:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readCgroup2MapFile(dirPath string, name string) (map[string][]string, error) {
|
|
|
|
ret := map[string][]string{}
|
2021-07-02 08:43:15 +00:00
|
|
|
f, err := cgroups.OpenFile(dirPath, name, os.O_RDONLY)
|
2020-05-04 20:46:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
scanner := bufio.NewScanner(f)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
parts := strings.Fields(line)
|
|
|
|
if len(parts) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret[parts[0]] = parts[1:]
|
|
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func statIo(dirPath string, stats *cgroups.Stats) error {
|
|
|
|
values, err := readCgroup2MapFile(dirPath, "io.stat")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-02 08:43:15 +00:00
|
|
|
// more details on the io.stat file format: https://www.kernel.org/doc/Documentation/cgroup-v2.txt
|
|
|
|
var parsedStats cgroups.BlkioStats
|
2020-05-04 20:46:48 +00:00
|
|
|
for k, v := range values {
|
|
|
|
d := strings.Split(k, ":")
|
|
|
|
if len(d) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
2021-07-02 08:43:15 +00:00
|
|
|
major, err := strconv.ParseUint(d[0], 10, 64)
|
2020-05-04 20:46:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-02 08:43:15 +00:00
|
|
|
minor, err := strconv.ParseUint(d[1], 10, 64)
|
2020-05-04 20:46:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range v {
|
|
|
|
d := strings.Split(item, "=")
|
|
|
|
if len(d) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
op := d[0]
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
// Map to the cgroupv1 naming and layout (in separate tables).
|
|
|
|
var targetTable *[]cgroups.BlkioStatEntry
|
2020-05-04 20:46:48 +00:00
|
|
|
switch op {
|
2021-07-02 08:43:15 +00:00
|
|
|
// Equivalent to cgroupv1's blkio.io_service_bytes.
|
2020-05-04 20:46:48 +00:00
|
|
|
case "rbytes":
|
2021-07-02 08:43:15 +00:00
|
|
|
op = "Read"
|
|
|
|
targetTable = &parsedStats.IoServiceBytesRecursive
|
2020-05-04 20:46:48 +00:00
|
|
|
case "wbytes":
|
2021-07-02 08:43:15 +00:00
|
|
|
op = "Write"
|
|
|
|
targetTable = &parsedStats.IoServiceBytesRecursive
|
|
|
|
// Equivalent to cgroupv1's blkio.io_serviced.
|
|
|
|
case "rios":
|
|
|
|
op = "Read"
|
|
|
|
targetTable = &parsedStats.IoServicedRecursive
|
|
|
|
case "wios":
|
|
|
|
op = "Write"
|
|
|
|
targetTable = &parsedStats.IoServicedRecursive
|
|
|
|
default:
|
|
|
|
// Skip over entries we cannot map to cgroupv1 stats for now.
|
|
|
|
// In the future we should expand the stats struct to include
|
|
|
|
// them.
|
|
|
|
logrus.Debugf("cgroupv2 io stats: skipping over unmappable %s entry", item)
|
|
|
|
continue
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 08:43:15 +00:00
|
|
|
value, err := strconv.ParseUint(d[1], 10, 64)
|
2020-05-04 20:46:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
entry := cgroups.BlkioStatEntry{
|
|
|
|
Op: op,
|
|
|
|
Major: major,
|
|
|
|
Minor: minor,
|
|
|
|
Value: value,
|
|
|
|
}
|
2021-07-02 08:43:15 +00:00
|
|
|
*targetTable = append(*targetTable, entry)
|
2020-05-04 20:46:48 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-02 08:43:15 +00:00
|
|
|
stats.BlkioStats = parsedStats
|
2020-05-04 20:46:48 +00:00
|
|
|
return nil
|
|
|
|
}
|