
This is to check if runc 1.0.0 (to be released shortly) works with k8s. The commands used were (roughly): hack/pin-dependency.sh github.com/opencontainers/runc v1.0.0 hack/lint-dependencies.sh # Follow its recommendations. hack/pin-dependency.sh github.com/cilium/ebpf v0.6.1 hack/pin-dependency.sh github.com/opencontainers/selinux v1.8.2 hack/pin-dependency.sh github.com/sirupsen/logrus v1.8.1 # Recheck. hack/lint-dependencies.sh GO111MODULE=on go mod edit -dropreplace github.com/willf/bitset hack/update-vendor.sh # Recheck. hack/lint-dependencies.sh hack/update-internal-modules.sh # Recheck. hack/lint-dependencies.sh [v2: rebased, updated runc 3a0234e1fe2e82 -> 2f8e8e9d977500] [v3: testing master + runc pr 3019] [v4: updated to 93a01cd4d0b7a0f08a] [v5: updated to f093cca13d3cf8a484] [v6: rebased] [v7: updated to runc v1.0.0] [v8: rebased] Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
116 lines
2.9 KiB
Go
116 lines
2.9 KiB
Go
// +build linux
|
|
|
|
package fs
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
|
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
|
)
|
|
|
|
type CpuGroup struct{}
|
|
|
|
func (s *CpuGroup) Name() string {
|
|
return "cpu"
|
|
}
|
|
|
|
func (s *CpuGroup) Apply(path string, d *cgroupData) error {
|
|
// This might happen if we have no cpu cgroup mounted.
|
|
// Just do nothing and don't fail.
|
|
if path == "" {
|
|
return nil
|
|
}
|
|
if err := os.MkdirAll(path, 0o755); err != nil {
|
|
return err
|
|
}
|
|
// We should set the real-Time group scheduling settings before moving
|
|
// in the process because if the process is already in SCHED_RR mode
|
|
// and no RT bandwidth is set, adding it will fail.
|
|
if err := s.SetRtSched(path, d.config.Resources); err != nil {
|
|
return err
|
|
}
|
|
// Since we are not using join(), we need to place the pid
|
|
// into the procs file unlike other subsystems.
|
|
return cgroups.WriteCgroupProc(path, d.pid)
|
|
}
|
|
|
|
func (s *CpuGroup) SetRtSched(path string, r *configs.Resources) error {
|
|
if r.CpuRtPeriod != 0 {
|
|
if err := cgroups.WriteFile(path, "cpu.rt_period_us", strconv.FormatUint(r.CpuRtPeriod, 10)); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if r.CpuRtRuntime != 0 {
|
|
if err := cgroups.WriteFile(path, "cpu.rt_runtime_us", strconv.FormatInt(r.CpuRtRuntime, 10)); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *CpuGroup) Set(path string, r *configs.Resources) error {
|
|
if r.CpuShares != 0 {
|
|
shares := r.CpuShares
|
|
if err := cgroups.WriteFile(path, "cpu.shares", strconv.FormatUint(shares, 10)); err != nil {
|
|
return err
|
|
}
|
|
// read it back
|
|
sharesRead, err := fscommon.GetCgroupParamUint(path, "cpu.shares")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// ... and check
|
|
if shares > sharesRead {
|
|
return fmt.Errorf("the maximum allowed cpu-shares is %d", sharesRead)
|
|
} else if shares < sharesRead {
|
|
return fmt.Errorf("the minimum allowed cpu-shares is %d", sharesRead)
|
|
}
|
|
}
|
|
if r.CpuPeriod != 0 {
|
|
if err := cgroups.WriteFile(path, "cpu.cfs_period_us", strconv.FormatUint(r.CpuPeriod, 10)); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if r.CpuQuota != 0 {
|
|
if err := cgroups.WriteFile(path, "cpu.cfs_quota_us", strconv.FormatInt(r.CpuQuota, 10)); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return s.SetRtSched(path, r)
|
|
}
|
|
|
|
func (s *CpuGroup) GetStats(path string, stats *cgroups.Stats) error {
|
|
f, err := cgroups.OpenFile(path, "cpu.stat", os.O_RDONLY)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
|
|
sc := bufio.NewScanner(f)
|
|
for sc.Scan() {
|
|
t, v, err := fscommon.ParseKeyValue(sc.Text())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
switch t {
|
|
case "nr_periods":
|
|
stats.CpuStats.ThrottlingData.Periods = v
|
|
|
|
case "nr_throttled":
|
|
stats.CpuStats.ThrottlingData.ThrottledPeriods = v
|
|
|
|
case "throttled_time":
|
|
stats.CpuStats.ThrottlingData.ThrottledTime = v
|
|
}
|
|
}
|
|
return nil
|
|
}
|