cgroups: remove cgroup.Resources.CpuMax

This (and the converting function) is only used by one of the four
cgroup drivers. The other three do some checking and conversion in
place, so let the fs2 do the same.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin 2020-06-03 18:19:01 -07:00
parent 8b9646775e
commit 4189cb65f8
6 changed files with 17 additions and 74 deletions

View File

@ -14,24 +14,35 @@ import (
)
func isCpuSet(cgroup *configs.Cgroup) bool {
return cgroup.Resources.CpuWeight != 0 || cgroup.Resources.CpuMax != ""
return cgroup.Resources.CpuWeight != 0 || cgroup.Resources.CpuQuota != 0 || cgroup.Resources.CpuPeriod != 0
}
func setCpu(dirPath string, cgroup *configs.Cgroup) error {
if !isCpuSet(cgroup) {
return nil
}
r := cgroup.Resources
// NOTE: .CpuShares is not used here. Conversion is the caller's responsibility.
if cgroup.Resources.CpuWeight != 0 {
if err := fscommon.WriteFile(dirPath, "cpu.weight", strconv.FormatUint(cgroup.Resources.CpuWeight, 10)); err != nil {
if r.CpuWeight != 0 {
if err := fscommon.WriteFile(dirPath, "cpu.weight", strconv.FormatUint(r.CpuWeight, 10)); err != nil {
return err
}
}
// NOTE: .CpuQuota and .CpuPeriod are not used here. Conversion is the caller's responsibility.
if cgroup.Resources.CpuMax != "" {
if err := fscommon.WriteFile(dirPath, "cpu.max", cgroup.Resources.CpuMax); err != nil {
if r.CpuQuota != 0 || r.CpuPeriod != 0 {
str := "max"
if r.CpuQuota > 0 {
str = strconv.FormatInt(r.CpuQuota, 10)
}
period := r.CpuPeriod
if period == 0 {
// This default value is documented in
// https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html
period = 100000
}
str += " " + strconv.FormatUint(period, 10)
if err := fscommon.WriteFile(dirPath, "cpu.max", str); err != nil {
return err
}
}

View File

@ -593,21 +593,6 @@ func ConvertCPUSharesToCgroupV2Value(cpuShares uint64) uint64 {
return (1 + ((cpuShares-2)*9999)/262142)
}
// ConvertCPUQuotaCPUPeriodToCgroupV2Value generates cpu.max string.
func ConvertCPUQuotaCPUPeriodToCgroupV2Value(quota int64, period uint64) string {
if quota <= 0 && period == 0 {
return ""
}
if period == 0 {
// This default value is documented in https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html
period = 100000
}
if quota <= 0 {
return fmt.Sprintf("max %d", period)
}
return fmt.Sprintf("%d %d", quota, period)
}
// ConvertMemorySwapToCgroupV2Value converts MemorySwap value from OCI spec
// for use by cgroup v2 drivers. A conversion is needed since Resources.MemorySwap
// is defined as memory+swap combined, while in cgroup v2 swap is a separate value.

View File

@ -457,51 +457,6 @@ func TestConvertCPUSharesToCgroupV2Value(t *testing.T) {
}
}
func TestConvertCPUQuotaCPUPeriodToCgroupV2Value(t *testing.T) {
cases := []struct {
quota int64
period uint64
expected string
}{
{
quota: 0,
period: 0,
expected: "",
},
{
quota: -1,
period: 0,
expected: "",
},
{
quota: 1000,
period: 5000,
expected: "1000 5000",
},
{
quota: 0,
period: 5000,
expected: "max 5000",
},
{
quota: -1,
period: 5000,
expected: "max 5000",
},
{
quota: 1000,
period: 0,
expected: "1000 100000",
},
}
for _, c := range cases {
got := ConvertCPUQuotaCPUPeriodToCgroupV2Value(c.quota, c.period)
if got != c.expected {
t.Errorf("expected ConvertCPUQuotaCPUPeriodToCgroupV2Value(%d, %d) to be %s, got %s", c.quota, c.period, c.expected, got)
}
}
}
func TestConvertMemorySwapToCgroupV2Value(t *testing.T) {
cases := []struct {
memswap, memory int64

View File

@ -126,7 +126,4 @@ type Resources struct {
// CpuWeight sets a proportional bandwidth limit.
CpuWeight uint64 `json:"cpu_weight"`
// CpuMax sets she maximum bandwidth limit (format: max period).
CpuMax string `json:"cpu_max"`
}

View File

@ -532,9 +532,6 @@ func CreateCgroupConfig(opts *CreateOpts) (*configs.Cgroup, error) {
if r.CPU.Period != nil {
c.Resources.CpuPeriod = *r.CPU.Period
}
//CpuMax is used for cgroupv2 and should be converted
c.Resources.CpuMax = cgroups.ConvertCPUQuotaCPUPeriodToCgroupV2Value(c.Resources.CpuQuota, c.Resources.CpuPeriod)
if r.CPU.RealtimeRuntime != nil {
c.Resources.CpuRtRuntime = *r.CPU.RealtimeRuntime
}

View File

@ -258,8 +258,6 @@ other options are ignored.
config.Cgroups.Resources.CpuShares = *r.CPU.Shares
//CpuWeight is used for cgroupv2 and should be converted
config.Cgroups.Resources.CpuWeight = cgroups.ConvertCPUSharesToCgroupV2Value(*r.CPU.Shares)
//CpuMax is used for cgroupv2 and should be converted
config.Cgroups.Resources.CpuMax = cgroups.ConvertCPUQuotaCPUPeriodToCgroupV2Value(*r.CPU.Quota, *r.CPU.Period)
config.Cgroups.Resources.CpuRtPeriod = *r.CPU.RealtimePeriod
config.Cgroups.Resources.CpuRtRuntime = *r.CPU.RealtimeRuntime
config.Cgroups.Resources.CpusetCpus = r.CPU.Cpus